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,
78 uint64_t keyid = _keyid << 32;
79 snprintf(buffer, length, "%s/key/%02X/%02X/%08X/%016" PRIX64,
80 basepath, (uint8_t) ((keyid >> 56) & 0xFF),
81 (uint8_t) ((keyid >> 48) & 0xFF),
82 (uint32_t) (keyid >> 32), _keyid);
85 static void keydir(char *buffer, size_t length, uint64_t _keyid,
88 uint64_t keyid = _keyid << 32;
89 snprintf(buffer, length, "%s/key/%02X/%02X/%08X", basepath,
90 (uint8_t) ((keyid >> 56) & 0xFF),
91 (uint8_t) ((keyid >> 48) & 0xFF),
92 (uint32_t) (keyid >> 32));
95 static void prove_path_to(uint64_t keyid, char *what, char *basepath)
97 static char buffer[PATH_MAX];
98 snprintf(buffer, sizeof(buffer), "%s/%s", basepath, what);
101 snprintf(buffer, sizeof(buffer), "%s/%s/%02X", basepath, what,
102 (uint8_t) ((keyid >> 24) & 0xFF));
105 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", basepath,
107 (uint8_t) ((keyid >> 24) & 0xFF),
108 (uint8_t) ((keyid >> 16) & 0xFF));
111 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", basepath,
113 (uint8_t) ((keyid >> 24) & 0xFF),
114 (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
118 static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
119 uint64_t keyid, char *basepath)
121 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
122 basepath, (uint8_t) ((hash >> 24) & 0xFF),
123 (uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
126 static void worddir(char *buffer, size_t length, char *word, uint32_t hash,
129 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", basepath,
130 (uint8_t) ((hash >> 24) & 0xFF),
131 (uint8_t) ((hash >> 16) & 0xFF), hash, word);
134 static void subkeypath(char *buffer, size_t length, uint64_t subkey,
137 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
139 (uint8_t) ((subkey >> 24) & 0xFF),
140 (uint8_t) ((subkey >> 16) & 0xFF),
141 (uint32_t) (subkey & 0xFFFFFFFF),
145 static void subkeydir(char *buffer, size_t length, uint64_t subkey,
148 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
150 (uint8_t) ((subkey >> 24) & 0xFF),
151 (uint8_t) ((subkey >> 16) & 0xFF),
152 (uint32_t) (subkey & 0xFFFFFFFF));
155 static void skshashpath(char *buffer, size_t length,
156 const struct skshash *hash, char *basepath)
158 snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
159 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
161 hash->hash[0], hash->hash[1],
162 hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
163 hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
164 hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
165 hash->hash[12], hash->hash[13], hash->hash[14],
169 /*****************************************************************************/
172 * starttrans - Start a transaction.
174 static bool fs_starttrans(struct onak_dbctx *dbctx)
176 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
177 struct flock lockstruct;
180 F_RDLCK | ((privctx->lockfile_readonly) ? 0 : F_WRLCK);
181 lockstruct.l_whence = SEEK_SET;
182 lockstruct.l_start = 0;
183 lockstruct.l_len = 1;
185 while (fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct) == -1) {
186 if (remaining-- == 0)
187 return false; /* Hope to hell that noodles DTRT */
194 * endtrans - End a transaction.
196 static void fs_endtrans(struct onak_dbctx *dbctx)
198 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
199 struct flock lockstruct;
201 lockstruct.l_type = F_UNLCK;
202 lockstruct.l_whence = SEEK_SET;
203 lockstruct.l_start = 0;
204 lockstruct.l_len = 1;
205 fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct);
208 static uint64_t fs_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
210 static char buffer[PATH_MAX];
212 struct dirent *de = NULL;
215 keydir(buffer, sizeof(buffer), keyid, dbctx->config->location);
221 if (de && de->d_name[0] != '.') {
222 ret = strtoull(de->d_name, NULL, 16);
224 } while (de && de->d_name[0] == '.');
229 subkeydir(buffer, sizeof(buffer), keyid,
230 dbctx->config->location);
236 if (de && de->d_name[0] != '.') {
237 ret = strtoull(de->d_name, NULL, 16);
239 } while (de && de->d_name[0] == '.');
248 * fetch_key - Given a keyid fetch the key from storage.
249 * @keyid: The keyid to fetch.
250 * @publickey: A pointer to a structure to return the key in.
251 * @intrans: If we're already in a transaction.
253 static int fs_fetch_key_id(struct onak_dbctx *dbctx,
255 struct openpgp_publickey **publickey,
258 static char buffer[PATH_MAX];
260 struct openpgp_packet_list *packets = NULL;
263 fs_starttrans(dbctx);
265 if ((keyid >> 32) == 0)
266 keyid = fs_getfullkeyid(dbctx, keyid);
268 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
269 fd = open(buffer, O_RDONLY);
270 if (fd == -1 && errno == ENOENT) {
271 subkeypath(buffer, sizeof(buffer), keyid,
272 dbctx->config->location);
273 fd = open(buffer, O_RDONLY);
277 /* File is present, load it in... */
278 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
279 parse_keys(packets, publickey);
280 free_packet_list(packets);
292 * store_key - Takes a key and stores it.
293 * @publickey: A pointer to the public key to store.
294 * @intrans: If we're already in a transaction.
295 * @update: If true the key exists and should be updated.
297 static int fs_store_key(struct onak_dbctx *dbctx,
298 struct openpgp_publickey *publickey, bool intrans,
301 static char buffer[PATH_MAX];
302 static char wbuffer[PATH_MAX];
304 struct openpgp_packet_list *packets = NULL;
305 struct openpgp_packet_list *list_end = NULL;
306 struct openpgp_publickey *next = NULL;
308 struct ll *wordlist = NULL, *wl = NULL;
310 struct openpgp_fingerprint *subkeyids = NULL;
314 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
315 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
320 fs_starttrans(dbctx);
322 prove_path_to(keyid, "key", dbctx->config->location);
323 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
326 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
328 next = publickey->next;
329 publickey->next = NULL;
330 flatten_publickey(publickey, &packets, &list_end);
331 publickey->next = next;
333 write_openpgp_stream(file_putchar, &fd, packets);
335 free_packet_list(packets);
341 wl = wordlist = makewordlistfromkey(wordlist, publickey);
343 uint32_t hash = calchash((uint8_t *) (wl->object));
344 prove_path_to(hash, "words", dbctx->config->location);
346 worddir(wbuffer, sizeof(wbuffer), wl->object, hash,
347 dbctx->config->location);
348 mkdir(wbuffer, 0777);
349 wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
350 keyid, dbctx->config->location);
351 link(buffer, wbuffer);
355 llfree(wordlist, free);
357 subkeyids = keysubkeys(publickey);
359 while (subkeyids != NULL && subkeyids[i].length != 0) {
360 keyid = fingerprint2keyid(&subkeyids[i]);
362 prove_path_to(keyid, "subkeys",
363 dbctx->config->location);
365 subkeydir(wbuffer, sizeof(wbuffer), keyid,
366 dbctx->config->location);
367 mkdir(wbuffer, 0777);
368 subkeypath(wbuffer, sizeof(wbuffer), keyid,
369 dbctx->config->location);
370 link(buffer, wbuffer);
374 if (subkeyids != NULL) {
379 get_skshash(publickey, &hash);
380 hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
381 (hash.hash[2] << 8) + hash.hash[3];
382 prove_path_to(hashid, "skshash", dbctx->config->location);
383 skshashpath(wbuffer, sizeof(wbuffer), &hash,
384 dbctx->config->location);
385 link(buffer, wbuffer);
394 * delete_key - Given a keyid delete the key from storage.
395 * @keyid: The keyid to delete.
396 * @intrans: If we're already in a transaction.
398 static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
400 static char buffer[PATH_MAX];
402 struct openpgp_publickey *pk = NULL;
404 struct ll *wordlist = NULL, *wl = NULL;
405 struct openpgp_fingerprint *subkeyids = NULL;
409 if ((keyid >> 32) == 0)
410 keyid = fs_getfullkeyid(dbctx, keyid);
413 fs_starttrans(dbctx);
415 ret = fs_fetch_key_id(dbctx, keyid, &pk, true);
418 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
420 wl = wordlist = makewordlistfromkey(wordlist, pk);
421 logthing(LOGTHING_DEBUG,
422 "Wordlist for key %016" PRIX64 " done", keyid);
424 uint32_t hash = calchash((uint8_t *) (wl->object));
425 prove_path_to(hash, "words", dbctx->config->location);
427 wordpath(buffer, sizeof(buffer), wl->object, hash,
428 keyid, dbctx->config->location);
434 subkeyids = keysubkeys(pk);
436 while (subkeyids != NULL && subkeyids[i].length != 0) {
437 subkeyid = fingerprint2keyid(&subkeyids[i]);
438 prove_path_to(subkeyid, "subkeys",
439 dbctx->config->location);
441 subkeypath(buffer, sizeof(buffer), subkeyid,
442 dbctx->config->location);
447 if (subkeyids != NULL) {
452 get_skshash(pk, &hash);
453 skshashpath(buffer, sizeof(buffer), &hash,
454 dbctx->config->location);
458 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
466 static struct ll *internal_get_key_by_word(char *word, struct ll *mct,
469 struct ll *keys = NULL;
471 char buffer[PATH_MAX];
472 uint32_t hash = calchash((uint8_t *) (word));
475 worddir(buffer, sizeof(buffer), word, hash, basepath);
477 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
482 if (de && de->d_name[0] != '.') {
484 || (llfind(mct, de->d_name,
485 (int (*)(const void *, const void *))
488 logthing(LOGTHING_DEBUG,
489 "Found %s // %s", word,
504 * fetch_key_text - Trys to find the keys that contain the supplied text.
505 * @search: The text to search for.
506 * @publickey: A pointer to a structure to return the key in.
508 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
510 struct openpgp_publickey **publickey)
512 struct ll *wordlist = NULL, *wl = NULL;
513 struct ll *keylist = NULL;
514 char *searchtext = NULL;
517 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
519 searchtext = strdup(search);
520 wl = wordlist = makewordlist(wordlist, searchtext);
522 keylist = internal_get_key_by_word(wordlist->object, NULL,
523 dbctx->config->location);
526 llfree(wordlist, NULL);
535 internal_get_key_by_word(wl->object, keylist,
536 dbctx->config->location);
538 llfree(wordlist, NULL);
539 llfree(keylist, free);
544 llfree(keylist, free);
549 llfree(wordlist, NULL);
551 /* Now add the keys... */
554 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
556 fs_fetch_key_id(dbctx,
557 strtoull(wl->object, NULL, 16), publickey,
559 if (addedkeys >= config.maxkeys)
564 llfree(keylist, free);
572 * fetch_key_skshash - Given an SKS hash fetch the key from storage.
573 * @hash: The hash to fetch.
574 * @publickey: A pointer to a structure to return the key in.
575 * @intrans: If we're already in a transaction.
577 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
578 const struct skshash *hash,
579 struct openpgp_publickey **publickey)
581 static char buffer[PATH_MAX];
583 struct openpgp_packet_list *packets = NULL;
585 skshashpath(buffer, sizeof(buffer), hash, dbctx->config->location);
586 if ((fd = open(buffer, O_RDONLY)) != -1) {
587 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
588 parse_keys(packets, publickey);
589 free_packet_list(packets);
599 * iterate_keys - call a function once for each key in the db.
600 * @iterfunc: The function to call.
601 * @ctx: A context pointer
603 * Calls iterfunc once for each key in the database. ctx is passed
604 * unaltered to iterfunc. This function is intended to aid database dumps
605 * and statistic calculations.
607 * Returns the number of keys we iterated over.
609 static int fs_iterate_keys(struct onak_dbctx *dbctx,
610 void (*iterfunc)(void *ctx,
611 struct openpgp_publickey *key), void *ctx)
617 * Include the basic keydb routines.
619 #define NEED_KEYID2UID 1
620 #define NEED_GETKEYSIGS 1
621 #define NEED_UPDATEKEYS 1
622 #define NEED_GET_FP 1
626 * cleanupdb - De-initialize the key database.
628 static void fs_cleanupdb(struct onak_dbctx *dbctx)
630 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
632 /* Mmmm nothing to do here? */
633 close(privctx->lockfile_fd);
637 * initdb - Initialize the key database.
639 struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
641 char buffer[PATH_MAX];
642 struct onak_dbctx *dbctx;
643 struct onak_fs_dbctx *privctx;
645 dbctx = malloc(sizeof(struct onak_dbctx));
649 dbctx->config = dbcfg;
650 dbctx->priv = privctx = malloc(sizeof(*privctx));
651 if (privctx == NULL) {
656 privctx->lockfile_readonly = readonly;
658 snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
660 if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
661 if (errno != ENOENT) {
662 logthing(LOGTHING_CRITICAL,
663 "Unable to access keydb_fs root of '%s'. (%s)",
664 dbcfg->location, strerror(errno));
665 exit(1); /* Lacking rwx on the key dir */
667 mkdir(dbcfg->location, 0777);
668 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
670 if (chdir(dbcfg->location) == -1) {
671 /* Shouldn't happen after the above */
672 logthing(LOGTHING_CRITICAL,
673 "Couldn't change to database directory: %s",
679 privctx->lockfile_fd = open(buffer,
680 (privctx->lockfile_readonly) ?
682 if (privctx->lockfile_fd == -1)
683 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
684 if (privctx->lockfile_fd == -1) {
685 logthing(LOGTHING_CRITICAL,
686 "Unable to open lockfile '%s'. (%s)",
687 buffer, strerror(errno));
688 exit(1); /* Lacking rwx on the key dir */
691 dbctx->cleanupdb = fs_cleanupdb;
692 dbctx->starttrans = fs_starttrans;
693 dbctx->endtrans = fs_endtrans;
694 dbctx->fetch_key_id = fs_fetch_key_id;
695 dbctx->fetch_key_fp = generic_fetch_key_fp;
696 dbctx->fetch_key_text = fs_fetch_key_text;
697 dbctx->fetch_key_skshash = fs_fetch_key_skshash;
698 dbctx->store_key = fs_store_key;
699 dbctx->update_keys = generic_update_keys;
700 dbctx->delete_key = fs_delete_key;
701 dbctx->getkeysigs = generic_getkeysigs;
702 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
703 dbctx->keyid2uid = generic_keyid2uid;
704 dbctx->getfullkeyid = fs_getfullkeyid;
705 dbctx->iterate_keys = fs_iterate_keys;