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,
134 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
136 (uint8_t) ((subkey >> 24) & 0xFF),
137 (uint8_t) ((subkey >> 16) & 0xFF),
138 (uint32_t) (subkey & 0xFFFFFFFF),
142 static void skshashpath(char *buffer, size_t length,
143 const struct skshash *hash)
145 snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
146 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
148 hash->hash[0], hash->hash[1],
149 hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
150 hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
151 hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
152 hash->hash[12], hash->hash[13], hash->hash[14],
155 static void subkeydir(char *buffer, size_t length, uint64_t subkey)
157 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
159 (uint8_t) ((subkey >> 24) & 0xFF),
160 (uint8_t) ((subkey >> 16) & 0xFF),
161 (uint32_t) (subkey & 0xFFFFFFFF));
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 if ((fd = open(buffer, O_RDONLY)) != -1) {
264 /* File is present, load it in... */
265 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
266 parse_keys(packets, publickey);
267 free_packet_list(packets);
279 * store_key - Takes a key and stores it.
280 * @publickey: A pointer to the public key to store.
281 * @intrans: If we're already in a transaction.
282 * @update: If true the key exists and should be updated.
284 static int fs_store_key(struct onak_dbctx *dbctx,
285 struct openpgp_publickey *publickey, bool intrans,
288 static char buffer[PATH_MAX];
289 static char wbuffer[PATH_MAX];
291 struct openpgp_packet_list *packets = NULL;
292 struct openpgp_packet_list *list_end = NULL;
293 struct openpgp_publickey *next = NULL;
295 struct ll *wordlist = NULL, *wl = NULL;
297 uint64_t *subkeyids = NULL;
301 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
302 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
307 fs_starttrans(dbctx);
309 prove_path_to(keyid, "key");
310 keypath(buffer, sizeof(buffer), keyid);
313 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
315 next = publickey->next;
316 publickey->next = NULL;
317 flatten_publickey(publickey, &packets, &list_end);
318 publickey->next = next;
320 write_openpgp_stream(file_putchar, &fd, packets);
322 free_packet_list(packets);
328 wl = wordlist = makewordlistfromkey(wordlist, publickey);
330 uint32_t hash = calchash((uint8_t *) (wl->object));
331 prove_path_to(hash, "words");
333 worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
334 mkdir(wbuffer, 0777);
335 wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
337 link(buffer, wbuffer);
341 llfree(wordlist, free);
343 subkeyids = keysubkeys(publickey);
345 while (subkeyids != NULL && subkeyids[i] != 0) {
346 prove_path_to(subkeyids[i], "subkeys");
348 subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
349 mkdir(wbuffer, 0777);
350 subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i],
352 link(buffer, wbuffer);
356 if (subkeyids != NULL) {
361 get_skshash(publickey, &hash);
362 hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
363 (hash.hash[2] << 8) + hash.hash[3];
364 prove_path_to(hashid, "skshash");
365 skshashpath(wbuffer, sizeof(wbuffer), &hash);
366 link(buffer, wbuffer);
375 * delete_key - Given a keyid delete the key from storage.
376 * @keyid: The keyid to delete.
377 * @intrans: If we're already in a transaction.
379 static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
381 static char buffer[PATH_MAX];
383 struct openpgp_publickey *pk = NULL;
385 struct ll *wordlist = NULL, *wl = NULL;
386 uint64_t *subkeyids = NULL;
389 if ((keyid >> 32) == 0)
390 keyid = fs_getfullkeyid(dbctx, keyid);
393 fs_starttrans(dbctx);
395 ret = fs_fetch_key_id(dbctx, keyid, &pk, true);
398 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
400 wl = wordlist = makewordlistfromkey(wordlist, pk);
401 logthing(LOGTHING_DEBUG,
402 "Wordlist for key %016" PRIX64 " done", keyid);
404 uint32_t hash = calchash((uint8_t *) (wl->object));
405 prove_path_to(hash, "words");
407 wordpath(buffer, sizeof(buffer), wl->object, hash,
414 subkeyids = keysubkeys(pk);
416 while (subkeyids != NULL && subkeyids[i] != 0) {
417 prove_path_to(subkeyids[i], "subkeys");
419 subkeypath(buffer, sizeof(buffer), subkeyids[i],
425 if (subkeyids != NULL) {
430 get_skshash(pk, &hash);
431 skshashpath(buffer, sizeof(buffer), &hash);
435 keypath(buffer, sizeof(buffer), keyid);
443 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
445 struct ll *keys = NULL;
447 char buffer[PATH_MAX];
448 uint32_t hash = calchash((uint8_t *) (word));
451 worddir(buffer, sizeof(buffer), word, hash);
453 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
458 if (de && de->d_name[0] != '.') {
460 || (llfind(mct, de->d_name,
461 (int (*)(const void *, const void *))
464 logthing(LOGTHING_DEBUG,
465 "Found %s // %s", word,
479 * fetch_key_text - Trys to find the keys that contain the supplied text.
480 * @search: The text to search for.
481 * @publickey: A pointer to a structure to return the key in.
483 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
485 struct openpgp_publickey **publickey)
487 struct ll *wordlist = NULL, *wl = NULL;
488 struct ll *keylist = NULL;
489 char *searchtext = NULL;
492 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
494 searchtext = strdup(search);
495 wl = wordlist = makewordlist(wordlist, searchtext);
497 keylist = internal_get_key_by_word(wordlist->object, NULL);
500 llfree(wordlist, NULL);
509 internal_get_key_by_word(wl->object, keylist);
511 llfree(wordlist, NULL);
512 llfree(keylist, free);
517 llfree(keylist, free);
522 llfree(wordlist, NULL);
524 /* Now add the keys... */
527 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
529 fs_fetch_key_id(dbctx,
530 strtoull(wl->object, NULL, 16), publickey,
532 if (addedkeys >= config.maxkeys)
537 llfree(keylist, free);
545 * fetch_key_skshash - Given an SKS hash fetch the key from storage.
546 * @hash: The hash to fetch.
547 * @publickey: A pointer to a structure to return the key in.
548 * @intrans: If we're already in a transaction.
550 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
551 const struct skshash *hash,
552 struct openpgp_publickey **publickey)
554 static char buffer[PATH_MAX];
556 struct openpgp_packet_list *packets = NULL;
558 skshashpath(buffer, sizeof(buffer), hash);
559 if ((fd = open(buffer, O_RDONLY)) != -1) {
560 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
561 parse_keys(packets, publickey);
562 free_packet_list(packets);
572 * iterate_keys - call a function once for each key in the db.
573 * @iterfunc: The function to call.
574 * @ctx: A context pointer
576 * Calls iterfunc once for each key in the database. ctx is passed
577 * unaltered to iterfunc. This function is intended to aid database dumps
578 * and statistic calculations.
580 * Returns the number of keys we iterated over.
582 static int fs_iterate_keys(struct onak_dbctx *dbctx,
583 void (*iterfunc)(void *ctx,
584 struct openpgp_publickey *key), void *ctx)
590 * Include the basic keydb routines.
592 #define NEED_KEYID2UID 1
593 #define NEED_GETKEYSIGS 1
594 #define NEED_UPDATEKEYS 1
595 #define NEED_GET_FP 1
599 * cleanupdb - De-initialize the key database.
601 static void fs_cleanupdb(struct onak_dbctx *dbctx)
603 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
605 /* Mmmm nothing to do here? */
606 close(privctx->lockfile_fd);
610 * initdb - Initialize the key database.
612 struct onak_dbctx *keydb_fs_init(bool readonly)
614 char buffer[PATH_MAX];
615 struct onak_dbctx *dbctx;
616 struct onak_fs_dbctx *privctx;
618 dbctx = malloc(sizeof(struct onak_dbctx));
622 dbctx->priv = privctx = malloc(sizeof(*privctx));
623 if (privctx == NULL) {
628 privctx->lockfile_readonly = readonly;
630 snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
632 if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
633 if (errno != ENOENT) {
634 logthing(LOGTHING_CRITICAL,
635 "Unable to access keydb_fs root of '%s'. (%s)",
636 config.db_dir, strerror(errno));
637 exit(1); /* Lacking rwx on the key dir */
639 mkdir(config.db_dir, 0777);
640 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
642 chdir(config.db_dir);
643 if (privctx->lockfile_fd == -1)
644 privctx->lockfile_fd = open(buffer,
645 (privctx->lockfile_readonly) ?
647 if (privctx->lockfile_fd == -1)
648 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
649 if (privctx->lockfile_fd == -1) {
650 logthing(LOGTHING_CRITICAL,
651 "Unable to open lockfile '%s'. (%s)",
652 buffer, strerror(errno));
653 exit(1); /* Lacking rwx on the key dir */
656 dbctx->cleanupdb = fs_cleanupdb;
657 dbctx->starttrans = fs_starttrans;
658 dbctx->endtrans = fs_endtrans;
659 dbctx->fetch_key_id = fs_fetch_key_id;
660 dbctx->fetch_key_fp = generic_fetch_key_fp;
661 dbctx->fetch_key_text = fs_fetch_key_text;
662 dbctx->fetch_key_skshash = fs_fetch_key_skshash;
663 dbctx->store_key = fs_store_key;
664 dbctx->update_keys = generic_update_keys;
665 dbctx->delete_key = fs_delete_key;
666 dbctx->getkeysigs = generic_getkeysigs;
667 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
668 dbctx->keyid2uid = generic_keyid2uid;
669 dbctx->getfullkeyid = fs_getfullkeyid;
670 dbctx->iterate_keys = fs_iterate_keys;