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"
32 #include "key-store.h"
35 #include "keystructs.h"
39 #include "onak-conf.h"
43 * starttrans - Start a transaction.
45 * This is just a no-op for flat file access.
47 static bool file_starttrans(struct onak_dbctx *dbctx)
53 * endtrans - End a transaction.
55 * This is just a no-op for flat file access.
57 static void file_endtrans(struct onak_dbctx *dbctx)
63 * fetch_key_id - Given a keyid fetch the key from storage.
64 * @keyid: The keyid to fetch.
65 * @publickey: A pointer to a structure to return the key in.
66 * @intrans: If we're already in a transaction.
68 * We use the hex representation of the keyid as the filename to fetch the
69 * key from. The key is stored in the file as a binary OpenPGP stream of
70 * packets, so we can just use read_openpgp_stream() to read the packets
71 * in and then parse_keys() to parse the packets into a publickey
74 static int file_fetch_key_id(struct onak_dbctx *dbctx,
76 struct openpgp_publickey **publickey,
79 char *db_dir = (char *) dbctx->priv;
80 struct openpgp_packet_list *packets = NULL;
84 snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
86 res = onak_read_openpgp_file(keyfile, &packets);
88 if (res == ONAK_E_OK) {
89 parse_keys(packets, publickey);
90 free_packet_list(packets);
94 return (res == ONAK_E_OK);
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 * @fp: The fingerprint of the key 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 struct openpgp_fingerprint *fp, bool intrans)
154 char *db_dir = (char *) dbctx->priv;
157 snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
158 fingerprint2keyid(fp) & 0xFFFFFFFF);
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;
201 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 res = onak_read_openpgp_file(keyfile,
216 if (res == ONAK_E_OK) {
217 parse_keys(packets, &key);
223 free_packet_list(packets);
238 * Include the basic keydb routines.
240 #define NEED_KEYID2UID 1
241 #define NEED_GETKEYSIGS 1
242 #define NEED_UPDATEKEYS 1
243 #define NEED_GET_FP 1
247 * cleanupdb - De-initialize the key database.
249 * This is just a no-op for flat file access.
251 static void file_cleanupdb(struct onak_dbctx *dbctx)
253 if (dbctx->priv != NULL) {
264 * initdb - Initialize the key database.
266 * This is just a no-op for flat file access.
268 struct onak_dbctx *keydb_file_init(struct onak_db_config *dbcfg, bool readonly)
270 struct onak_dbctx *dbctx;
272 dbctx = malloc(sizeof(struct onak_dbctx));
277 dbctx->config = dbcfg;
278 dbctx->priv = strdup(dbcfg->location);
280 dbctx->cleanupdb = file_cleanupdb;
281 dbctx->starttrans = file_starttrans;
282 dbctx->endtrans = file_endtrans;
283 /* Our fetch fp doesn't look at subkeys */
284 dbctx->fetch_key = generic_fetch_key_fp;
285 dbctx->fetch_key_fp = generic_fetch_key_fp;
286 dbctx->fetch_key_id = file_fetch_key_id;
287 dbctx->fetch_key_text = file_fetch_key_text;
288 dbctx->store_key = file_store_key;
289 dbctx->update_keys = generic_update_keys;
290 dbctx->delete_key = file_delete_key;
291 dbctx->getkeysigs = generic_getkeysigs;
292 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
293 dbctx->keyid2uid = generic_keyid2uid;
294 dbctx->iterate_keys = file_iterate_keys;