2 * keydb.c - Routines to store and fetch keys.
4 * Copyright 2002-2004 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <https://www.gnu.org/licenses/>.
19 #include <sys/types.h>
31 #include "charfuncs.h"
34 #include "keystructs.h"
38 #include "onak-conf.h"
42 * starttrans - Start a transaction.
44 * This is just a no-op for flat file access.
46 static bool file_starttrans(struct onak_dbctx *dbctx)
52 * endtrans - End a transaction.
54 * This is just a no-op for flat file access.
56 static void file_endtrans(struct onak_dbctx *dbctx)
62 * fetch_key_id - Given a keyid fetch the key from storage.
63 * @keyid: The keyid to fetch.
64 * @publickey: A pointer to a structure to return the key in.
65 * @intrans: If we're already in a transaction.
67 * We use the hex representation of the keyid as the filename to fetch the
68 * key from. The key is stored in the file as a binary OpenPGP stream of
69 * packets, so we can just use read_openpgp_stream() to read the packets
70 * in and then parse_keys() to parse the packets into a publickey
73 static int file_fetch_key_id(struct onak_dbctx *dbctx,
75 struct openpgp_publickey **publickey,
78 char *db_dir = (char *) dbctx->priv;
79 struct openpgp_packet_list *packets = NULL;
83 snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
85 fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
88 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
89 parse_keys(packets, publickey);
90 free_packet_list(packets);
99 * store_key - Takes a key and stores it.
100 * @publickey: A pointer to the public key to store.
101 * @intrans: If we're already in a transaction.
102 * @update: If true the key exists and should be updated.
104 * Again we just use the hex representation of the keyid as the filename
105 * to store the key to. We flatten the public key to a list of OpenPGP
106 * packets and then use write_openpgp_stream() to write the stream out to
109 static int file_store_key(struct onak_dbctx *dbctx,
110 struct openpgp_publickey *publickey, bool intrans,
113 char *db_dir = (char *) dbctx->priv;
114 struct openpgp_packet_list *packets = NULL;
115 struct openpgp_packet_list *list_end = NULL;
116 struct openpgp_publickey *next = NULL;
121 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
122 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
125 snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
127 fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
130 next = publickey -> next;
131 publickey -> next = NULL;
132 flatten_publickey(publickey, &packets, &list_end);
133 publickey -> next = next;
135 write_openpgp_stream(file_putchar, &fd, packets);
137 free_packet_list(packets);
145 * delete_key - Given a keyid delete the key from storage.
146 * @keyid: The keyid to delete.
147 * @intrans: If we're already in a transaction.
149 * This function deletes a public key from whatever storage mechanism we
150 * are using. Returns 0 if the key existed.
152 static int file_delete_key(struct onak_dbctx *dbctx,
153 uint64_t keyid, bool intrans)
155 char *db_dir = (char *) dbctx->priv;
158 snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
161 return unlink(keyfile);
165 * fetch_key_text - Trys to find the keys that contain the supplied text.
166 * @search: The text to search for.
167 * @publickey: A pointer to a structure to return the key in.
169 * This function searches for the supplied text and returns the keys that
172 * TODO: Write for flat file access. Some sort of grep?
174 static int file_fetch_key_text(struct onak_dbctx *dbctx,
176 struct openpgp_publickey **publickey)
182 * iterate_keys - call a function once for each key in the db.
183 * @iterfunc: The function to call.
184 * @ctx: A context pointer
186 * Calls iterfunc once for each key in the database. ctx is passed
187 * unaltered to iterfunc. This function is intended to aid database dumps
188 * and statistic calculations.
190 * Returns the number of keys we iterated over.
192 static int file_iterate_keys(struct onak_dbctx *dbctx,
193 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
196 char *db_dir = (char *) dbctx->priv;
198 struct openpgp_packet_list *packets = NULL;
199 struct openpgp_publickey *key = NULL;
203 struct dirent *curfile = NULL;
205 dir = opendir(db_dir);
208 while ((curfile = readdir(dir)) != NULL) {
209 if (curfile->d_name[0] == '0' &&
210 curfile->d_name[1] == 'x') {
211 snprintf(keyfile, 1023, "%s/%s",
214 fd = open(keyfile, O_RDONLY);
217 read_openpgp_stream(file_fetchchar,
221 parse_keys(packets, &key);
227 free_packet_list(packets);
243 * Include the basic keydb routines.
245 #define NEED_KEYID2UID 1
246 #define NEED_GETKEYSIGS 1
247 #define NEED_GETFULLKEYID 1
248 #define NEED_UPDATEKEYS 1
249 #define NEED_GET_FP 1
253 * cleanupdb - De-initialize the key database.
255 * This is just a no-op for flat file access.
257 static void file_cleanupdb(struct onak_dbctx *dbctx)
259 if (dbctx->priv != NULL) {
270 * initdb - Initialize the key database.
272 * This is just a no-op for flat file access.
274 struct onak_dbctx *keydb_file_init(struct onak_db_config *dbcfg, bool readonly)
276 struct onak_dbctx *dbctx;
278 dbctx = malloc(sizeof(struct onak_dbctx));
283 dbctx->config = dbcfg;
284 dbctx->priv = strdup(dbcfg->location);
286 dbctx->cleanupdb = file_cleanupdb;
287 dbctx->starttrans = file_starttrans;
288 dbctx->endtrans = file_endtrans;
289 dbctx->fetch_key_id = file_fetch_key_id;
290 dbctx->fetch_key_fp = generic_fetch_key_fp;
291 dbctx->fetch_key_text = file_fetch_key_text;
292 dbctx->store_key = file_store_key;
293 dbctx->update_keys = generic_update_keys;
294 dbctx->delete_key = file_delete_key;
295 dbctx->getkeysigs = generic_getkeysigs;
296 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
297 dbctx->keyid2uid = generic_keyid2uid;
298 dbctx->getfullkeyid = generic_getfullkeyid;
299 dbctx->iterate_keys = file_iterate_keys;