2 * keydb_fs.c - Routines to store and fetch keys in a filesystem hierarchy.
4 * Copyright 2004 Daniel Silverstone <dsilvers@digital-scurf.org>
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>
32 #include "charfuncs.h"
33 #include "decodekey.h"
36 #include "keystructs.h"
39 #include "onak-conf.h"
44 /* Hack: We should really dynamically allocate our path buffers */
49 struct onak_fs_dbctx {
51 bool lockfile_readonly;
54 /*****************************************************************************/
56 /* Helper functions */
58 #define FNV_offset_basis 2166136261ul
59 #define FNV_mixing_prime 16777619ul
61 static uint32_t calchash(uint8_t * ptr)
63 register uint32_t h = FNV_offset_basis;
64 register uint32_t p = FNV_mixing_prime;
65 register uint32_t n = strlen((char *) ptr);
66 register uint8_t *c = ptr;
71 return h ? h : 1; /* prevent a hash of zero happening */
75 static void keypath(char *buffer, size_t length, uint64_t _keyid)
77 uint64_t keyid = _keyid << 32;
78 snprintf(buffer, length, "%s/key/%02X/%02X/%08X/%016" PRIX64,
79 config.db_dir, (uint8_t) ((keyid >> 56) & 0xFF),
80 (uint8_t) ((keyid >> 48) & 0xFF),
81 (uint32_t) (keyid >> 32), _keyid);
84 static void keydir(char *buffer, size_t length, uint64_t _keyid)
86 uint64_t keyid = _keyid << 32;
87 snprintf(buffer, length, "%s/key/%02X/%02X/%08X", config.db_dir,
88 (uint8_t) ((keyid >> 56) & 0xFF),
89 (uint8_t) ((keyid >> 48) & 0xFF),
90 (uint32_t) (keyid >> 32));
93 static void prove_path_to(uint64_t keyid, char *what)
95 static char buffer[PATH_MAX];
96 snprintf(buffer, sizeof(buffer), "%s/%s", config.db_dir, what);
99 snprintf(buffer, sizeof(buffer), "%s/%s/%02X", config.db_dir, what,
100 (uint8_t) ((keyid >> 24) & 0xFF));
103 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", config.db_dir,
105 (uint8_t) ((keyid >> 24) & 0xFF),
106 (uint8_t) ((keyid >> 16) & 0xFF));
109 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", config.db_dir,
111 (uint8_t) ((keyid >> 24) & 0xFF),
112 (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
116 static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
119 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
120 config.db_dir, (uint8_t) ((hash >> 24) & 0xFF),
121 (uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
124 static void worddir(char *buffer, size_t length, char *word, uint32_t hash)
126 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", config.db_dir,
127 (uint8_t) ((hash >> 24) & 0xFF),
128 (uint8_t) ((hash >> 16) & 0xFF), hash, word);
131 static void subkeypath(char *buffer, size_t length, uint64_t subkey)
133 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
135 (uint8_t) ((subkey >> 24) & 0xFF),
136 (uint8_t) ((subkey >> 16) & 0xFF),
137 (uint32_t) (subkey & 0xFFFFFFFF),
141 static void subkeydir(char *buffer, size_t length, uint64_t subkey)
143 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
145 (uint8_t) ((subkey >> 24) & 0xFF),
146 (uint8_t) ((subkey >> 16) & 0xFF),
147 (uint32_t) (subkey & 0xFFFFFFFF));
150 static void skshashpath(char *buffer, size_t length,
151 const struct skshash *hash)
153 snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
154 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
156 hash->hash[0], hash->hash[1],
157 hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
158 hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
159 hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
160 hash->hash[12], hash->hash[13], hash->hash[14],
164 /*****************************************************************************/
167 * starttrans - Start a transaction.
169 static bool fs_starttrans(struct onak_dbctx *dbctx)
171 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
172 struct flock lockstruct;
175 F_RDLCK | ((privctx->lockfile_readonly) ? 0 : F_WRLCK);
176 lockstruct.l_whence = SEEK_SET;
177 lockstruct.l_start = 0;
178 lockstruct.l_len = 1;
180 while (fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct) == -1) {
181 if (remaining-- == 0)
182 return false; /* Hope to hell that noodles DTRT */
189 * endtrans - End a transaction.
191 static void fs_endtrans(struct onak_dbctx *dbctx)
193 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
194 struct flock lockstruct;
196 lockstruct.l_type = F_UNLCK;
197 lockstruct.l_whence = SEEK_SET;
198 lockstruct.l_start = 0;
199 lockstruct.l_len = 1;
200 fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct);
203 static uint64_t fs_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
205 static char buffer[PATH_MAX];
207 struct dirent *de = NULL;
210 keydir(buffer, sizeof(buffer), keyid);
216 if (de && de->d_name[0] != '.') {
217 ret = strtoull(de->d_name, NULL, 16);
219 } while (de && de->d_name[0] == '.');
224 subkeydir(buffer, sizeof(buffer), keyid);
230 if (de && de->d_name[0] != '.') {
231 ret = strtoull(de->d_name, NULL, 16);
233 } while (de && de->d_name[0] == '.');
242 * fetch_key - Given a keyid fetch the key from storage.
243 * @keyid: The keyid to fetch.
244 * @publickey: A pointer to a structure to return the key in.
245 * @intrans: If we're already in a transaction.
247 static int fs_fetch_key_id(struct onak_dbctx *dbctx,
249 struct openpgp_publickey **publickey,
252 static char buffer[PATH_MAX];
254 struct openpgp_packet_list *packets = NULL;
257 fs_starttrans(dbctx);
259 if ((keyid >> 32) == 0)
260 keyid = fs_getfullkeyid(dbctx, keyid);
262 keypath(buffer, sizeof(buffer), keyid);
263 fd = open(buffer, O_RDONLY);
264 if (fd == -1 && errno == ENOENT) {
265 subkeypath(buffer, sizeof(buffer), keyid);
266 fd = open(buffer, O_RDONLY);
270 /* File is present, load it in... */
271 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
272 parse_keys(packets, publickey);
273 free_packet_list(packets);
285 * store_key - Takes a key and stores it.
286 * @publickey: A pointer to the public key to store.
287 * @intrans: If we're already in a transaction.
288 * @update: If true the key exists and should be updated.
290 static int fs_store_key(struct onak_dbctx *dbctx,
291 struct openpgp_publickey *publickey, bool intrans,
294 static char buffer[PATH_MAX];
295 static char wbuffer[PATH_MAX];
297 struct openpgp_packet_list *packets = NULL;
298 struct openpgp_packet_list *list_end = NULL;
299 struct openpgp_publickey *next = NULL;
301 struct ll *wordlist = NULL, *wl = NULL;
303 uint64_t *subkeyids = NULL;
307 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
308 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
313 fs_starttrans(dbctx);
315 prove_path_to(keyid, "key");
316 keypath(buffer, sizeof(buffer), keyid);
319 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
321 next = publickey->next;
322 publickey->next = NULL;
323 flatten_publickey(publickey, &packets, &list_end);
324 publickey->next = next;
326 write_openpgp_stream(file_putchar, &fd, packets);
328 free_packet_list(packets);
334 wl = wordlist = makewordlistfromkey(wordlist, publickey);
336 uint32_t hash = calchash((uint8_t *) (wl->object));
337 prove_path_to(hash, "words");
339 worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
340 mkdir(wbuffer, 0777);
341 wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
343 link(buffer, wbuffer);
347 llfree(wordlist, free);
349 subkeyids = keysubkeys(publickey);
351 while (subkeyids != NULL && subkeyids[i] != 0) {
352 prove_path_to(subkeyids[i], "subkeys");
354 subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
355 mkdir(wbuffer, 0777);
356 subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i]);
357 link(buffer, wbuffer);
361 if (subkeyids != NULL) {
366 get_skshash(publickey, &hash);
367 hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
368 (hash.hash[2] << 8) + hash.hash[3];
369 prove_path_to(hashid, "skshash");
370 skshashpath(wbuffer, sizeof(wbuffer), &hash);
371 link(buffer, wbuffer);
380 * delete_key - Given a keyid delete the key from storage.
381 * @keyid: The keyid to delete.
382 * @intrans: If we're already in a transaction.
384 static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
386 static char buffer[PATH_MAX];
388 struct openpgp_publickey *pk = NULL;
390 struct ll *wordlist = NULL, *wl = NULL;
391 uint64_t *subkeyids = NULL;
394 if ((keyid >> 32) == 0)
395 keyid = fs_getfullkeyid(dbctx, keyid);
398 fs_starttrans(dbctx);
400 ret = fs_fetch_key_id(dbctx, keyid, &pk, true);
403 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
405 wl = wordlist = makewordlistfromkey(wordlist, pk);
406 logthing(LOGTHING_DEBUG,
407 "Wordlist for key %016" PRIX64 " done", keyid);
409 uint32_t hash = calchash((uint8_t *) (wl->object));
410 prove_path_to(hash, "words");
412 wordpath(buffer, sizeof(buffer), wl->object, hash,
419 subkeyids = keysubkeys(pk);
421 while (subkeyids != NULL && subkeyids[i] != 0) {
422 prove_path_to(subkeyids[i], "subkeys");
424 subkeypath(buffer, sizeof(buffer), subkeyids[i]);
429 if (subkeyids != NULL) {
434 get_skshash(pk, &hash);
435 skshashpath(buffer, sizeof(buffer), &hash);
439 keypath(buffer, sizeof(buffer), keyid);
447 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
449 struct ll *keys = NULL;
451 char buffer[PATH_MAX];
452 uint32_t hash = calchash((uint8_t *) (word));
455 worddir(buffer, sizeof(buffer), word, hash);
457 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
462 if (de && de->d_name[0] != '.') {
464 || (llfind(mct, de->d_name,
465 (int (*)(const void *, const void *))
468 logthing(LOGTHING_DEBUG,
469 "Found %s // %s", word,
484 * fetch_key_text - Trys to find the keys that contain the supplied text.
485 * @search: The text to search for.
486 * @publickey: A pointer to a structure to return the key in.
488 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
490 struct openpgp_publickey **publickey)
492 struct ll *wordlist = NULL, *wl = NULL;
493 struct ll *keylist = NULL;
494 char *searchtext = NULL;
497 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
499 searchtext = strdup(search);
500 wl = wordlist = makewordlist(wordlist, searchtext);
502 keylist = internal_get_key_by_word(wordlist->object, NULL);
505 llfree(wordlist, NULL);
514 internal_get_key_by_word(wl->object, keylist);
516 llfree(wordlist, NULL);
517 llfree(keylist, free);
522 llfree(keylist, free);
527 llfree(wordlist, NULL);
529 /* Now add the keys... */
532 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
534 fs_fetch_key_id(dbctx,
535 strtoull(wl->object, NULL, 16), publickey,
537 if (addedkeys >= config.maxkeys)
542 llfree(keylist, free);
550 * fetch_key_skshash - Given an SKS hash fetch the key from storage.
551 * @hash: The hash to fetch.
552 * @publickey: A pointer to a structure to return the key in.
553 * @intrans: If we're already in a transaction.
555 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
556 const struct skshash *hash,
557 struct openpgp_publickey **publickey)
559 static char buffer[PATH_MAX];
561 struct openpgp_packet_list *packets = NULL;
563 skshashpath(buffer, sizeof(buffer), hash);
564 if ((fd = open(buffer, O_RDONLY)) != -1) {
565 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
566 parse_keys(packets, publickey);
567 free_packet_list(packets);
577 * iterate_keys - call a function once for each key in the db.
578 * @iterfunc: The function to call.
579 * @ctx: A context pointer
581 * Calls iterfunc once for each key in the database. ctx is passed
582 * unaltered to iterfunc. This function is intended to aid database dumps
583 * and statistic calculations.
585 * Returns the number of keys we iterated over.
587 static int fs_iterate_keys(struct onak_dbctx *dbctx,
588 void (*iterfunc)(void *ctx,
589 struct openpgp_publickey *key), void *ctx)
595 * Include the basic keydb routines.
597 #define NEED_KEYID2UID 1
598 #define NEED_GETKEYSIGS 1
599 #define NEED_UPDATEKEYS 1
600 #define NEED_GET_FP 1
604 * cleanupdb - De-initialize the key database.
606 static void fs_cleanupdb(struct onak_dbctx *dbctx)
608 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
610 /* Mmmm nothing to do here? */
611 close(privctx->lockfile_fd);
615 * initdb - Initialize the key database.
617 struct onak_dbctx *keydb_fs_init(bool readonly)
619 char buffer[PATH_MAX];
620 struct onak_dbctx *dbctx;
621 struct onak_fs_dbctx *privctx;
623 dbctx = malloc(sizeof(struct onak_dbctx));
627 dbctx->priv = privctx = malloc(sizeof(*privctx));
628 if (privctx == NULL) {
633 privctx->lockfile_readonly = readonly;
635 snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
637 if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
638 if (errno != ENOENT) {
639 logthing(LOGTHING_CRITICAL,
640 "Unable to access keydb_fs root of '%s'. (%s)",
641 config.db_dir, strerror(errno));
642 exit(1); /* Lacking rwx on the key dir */
644 mkdir(config.db_dir, 0777);
645 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
647 chdir(config.db_dir);
648 privctx->lockfile_fd = open(buffer,
649 (privctx->lockfile_readonly) ?
651 if (privctx->lockfile_fd == -1)
652 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
653 if (privctx->lockfile_fd == -1) {
654 logthing(LOGTHING_CRITICAL,
655 "Unable to open lockfile '%s'. (%s)",
656 buffer, strerror(errno));
657 exit(1); /* Lacking rwx on the key dir */
660 dbctx->cleanupdb = fs_cleanupdb;
661 dbctx->starttrans = fs_starttrans;
662 dbctx->endtrans = fs_endtrans;
663 dbctx->fetch_key_id = fs_fetch_key_id;
664 dbctx->fetch_key_fp = generic_fetch_key_fp;
665 dbctx->fetch_key_text = fs_fetch_key_text;
666 dbctx->fetch_key_skshash = fs_fetch_key_skshash;
667 dbctx->store_key = fs_store_key;
668 dbctx->update_keys = generic_update_keys;
669 dbctx->delete_key = fs_delete_key;
670 dbctx->getkeysigs = generic_getkeysigs;
671 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
672 dbctx->keyid2uid = generic_keyid2uid;
673 dbctx->getfullkeyid = fs_getfullkeyid;
674 dbctx->iterate_keys = fs_iterate_keys;