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, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <sys/types.h>
30 #include "charfuncs.h"
33 #include "keystructs.h"
37 #include "onak-conf.h"
41 * starttrans - Start a transaction.
43 * This is just a no-op for flat file access.
45 static bool file_starttrans(struct onak_dbctx *dbctx)
51 * endtrans - End a transaction.
53 * This is just a no-op for flat file access.
55 static void file_endtrans(struct onak_dbctx *dbctx)
61 * fetch_key_id - Given a keyid fetch the key from storage.
62 * @keyid: The keyid to fetch.
63 * @publickey: A pointer to a structure to return the key in.
64 * @intrans: If we're already in a transaction.
66 * We use the hex representation of the keyid as the filename to fetch the
67 * key from. The key is stored in the file as a binary OpenPGP stream of
68 * packets, so we can just use read_openpgp_stream() to read the packets
69 * in and then parse_keys() to parse the packets into a publickey
72 static int file_fetch_key_id(struct onak_dbctx *dbctx,
74 struct openpgp_publickey **publickey,
77 char *db_dir = (char *) dbctx->priv;
78 struct openpgp_packet_list *packets = NULL;
82 snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
84 fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
87 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
88 parse_keys(packets, publickey);
89 free_packet_list(packets);
98 * store_key - Takes a key and stores it.
99 * @publickey: A pointer to the public key to store.
100 * @intrans: If we're already in a transaction.
101 * @update: If true the key exists and should be updated.
103 * Again we just use the hex representation of the keyid as the filename
104 * to store the key to. We flatten the public key to a list of OpenPGP
105 * packets and then use write_openpgp_stream() to write the stream out to
108 static int file_store_key(struct onak_dbctx *dbctx,
109 struct openpgp_publickey *publickey, bool intrans,
112 char *db_dir = (char *) dbctx->priv;
113 struct openpgp_packet_list *packets = NULL;
114 struct openpgp_packet_list *list_end = NULL;
115 struct openpgp_publickey *next = NULL;
120 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
121 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
124 snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
126 fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
129 next = publickey -> next;
130 publickey -> next = NULL;
131 flatten_publickey(publickey, &packets, &list_end);
132 publickey -> next = next;
134 write_openpgp_stream(file_putchar, &fd, packets);
136 free_packet_list(packets);
144 * delete_key - Given a keyid delete the key from storage.
145 * @keyid: The keyid to delete.
146 * @intrans: If we're already in a transaction.
148 * This function deletes a public key from whatever storage mechanism we
149 * are using. Returns 0 if the key existed.
151 static int file_delete_key(struct onak_dbctx *dbctx,
152 uint64_t keyid, bool intrans)
154 char *db_dir = (char *) dbctx->priv;
157 snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
160 return unlink(keyfile);
164 * fetch_key_text - Trys to find the keys that contain the supplied text.
165 * @search: The text to search for.
166 * @publickey: A pointer to a structure to return the key in.
168 * This function searches for the supplied text and returns the keys that
171 * TODO: Write for flat file access. Some sort of grep?
173 static int file_fetch_key_text(struct onak_dbctx *dbctx,
175 struct openpgp_publickey **publickey)
181 * iterate_keys - call a function once for each key in the db.
182 * @iterfunc: The function to call.
183 * @ctx: A context pointer
185 * Calls iterfunc once for each key in the database. ctx is passed
186 * unaltered to iterfunc. This function is intended to aid database dumps
187 * and statistic calculations.
189 * Returns the number of keys we iterated over.
191 static int file_iterate_keys(struct onak_dbctx *dbctx,
192 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
195 char *db_dir = (char *) dbctx->priv;
197 struct openpgp_packet_list *packets = NULL;
198 struct openpgp_publickey *key = NULL;
202 struct dirent *curfile = NULL;
204 dir = opendir(db_dir);
207 while ((curfile = readdir(dir)) != NULL) {
208 if (curfile->d_name[0] == '0' &&
209 curfile->d_name[1] == 'x') {
210 snprintf(keyfile, 1023, "%s/%s",
213 fd = open(keyfile, O_RDONLY);
216 read_openpgp_stream(file_fetchchar,
220 parse_keys(packets, &key);
226 free_packet_list(packets);
242 * Include the basic keydb routines.
244 #define NEED_KEYID2UID 1
245 #define NEED_GETKEYSIGS 1
246 #define NEED_GETFULLKEYID 1
247 #define NEED_UPDATEKEYS 1
248 #define NEED_GET_FP 1
252 * cleanupdb - De-initialize the key database.
254 * This is just a no-op for flat file access.
256 static void file_cleanupdb(struct onak_dbctx *dbctx)
258 if (dbctx->priv != NULL) {
269 * initdb - Initialize the key database.
271 * This is just a no-op for flat file access.
273 struct onak_dbctx *keydb_file_init(bool readonly)
275 struct onak_dbctx *dbctx;
277 dbctx = malloc(sizeof(struct onak_dbctx));
282 dbctx->priv = strdup(config.db_dir);
284 dbctx->cleanupdb = file_cleanupdb;
285 dbctx->starttrans = file_starttrans;
286 dbctx->endtrans = file_endtrans;
287 dbctx->fetch_key_id = file_fetch_key_id;
288 dbctx->fetch_key_fp = generic_fetch_key_fp;
289 dbctx->fetch_key_text = file_fetch_key_text;
290 dbctx->store_key = file_store_key;
291 dbctx->update_keys = generic_update_keys;
292 dbctx->delete_key = file_delete_key;
293 dbctx->getkeysigs = generic_getkeysigs;
294 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
295 dbctx->keyid2uid = generic_keyid2uid;
296 dbctx->getfullkeyid = generic_getfullkeyid;
297 dbctx->iterate_keys = file_iterate_keys;