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>
34 #include "charfuncs.h"
35 #include "decodekey.h"
38 #include "keystructs.h"
42 #include "onak-conf.h"
47 /* Hack: We should really dynamically allocate our path buffers */
52 struct onak_fs_dbctx {
54 bool lockfile_readonly;
57 /*****************************************************************************/
59 /* Helper functions */
61 #define FNV_offset_basis 2166136261ul
62 #define FNV_mixing_prime 16777619ul
64 static uint32_t calchash(uint8_t * ptr)
66 register uint32_t h = FNV_offset_basis;
67 register uint32_t p = FNV_mixing_prime;
68 register uint32_t n = strlen((char *) ptr);
69 register uint8_t *c = ptr;
74 return h ? h : 1; /* prevent a hash of zero happening */
78 static void keypath(char *buffer, size_t length, uint64_t _keyid,
81 uint64_t keyid = _keyid << 32;
82 snprintf(buffer, length, "%s/key/%02X/%02X/%08X/%016" PRIX64,
83 basepath, (uint8_t) ((keyid >> 56) & 0xFF),
84 (uint8_t) ((keyid >> 48) & 0xFF),
85 (uint32_t) (keyid >> 32), _keyid);
88 static void keydir(char *buffer, size_t length, uint64_t _keyid,
91 uint64_t keyid = _keyid << 32;
92 snprintf(buffer, length, "%s/key/%02X/%02X/%08X", basepath,
93 (uint8_t) ((keyid >> 56) & 0xFF),
94 (uint8_t) ((keyid >> 48) & 0xFF),
95 (uint32_t) (keyid >> 32));
98 static void prove_path_to(uint64_t keyid, char *what, char *basepath)
100 static char buffer[PATH_MAX];
101 snprintf(buffer, sizeof(buffer), "%s/%s", basepath, what);
104 snprintf(buffer, sizeof(buffer), "%s/%s/%02X", basepath, what,
105 (uint8_t) ((keyid >> 24) & 0xFF));
108 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", basepath,
110 (uint8_t) ((keyid >> 24) & 0xFF),
111 (uint8_t) ((keyid >> 16) & 0xFF));
114 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", basepath,
116 (uint8_t) ((keyid >> 24) & 0xFF),
117 (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
121 static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
122 uint64_t keyid, char *basepath)
124 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
125 basepath, (uint8_t) ((hash >> 24) & 0xFF),
126 (uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
129 static void worddir(char *buffer, size_t length, char *word, uint32_t hash,
132 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", basepath,
133 (uint8_t) ((hash >> 24) & 0xFF),
134 (uint8_t) ((hash >> 16) & 0xFF), hash, word);
137 static void subkeypath(char *buffer, size_t length, uint64_t subkey,
140 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
142 (uint8_t) ((subkey >> 24) & 0xFF),
143 (uint8_t) ((subkey >> 16) & 0xFF),
144 (uint32_t) (subkey & 0xFFFFFFFF),
148 static void subkeydir(char *buffer, size_t length, uint64_t subkey,
151 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
153 (uint8_t) ((subkey >> 24) & 0xFF),
154 (uint8_t) ((subkey >> 16) & 0xFF),
155 (uint32_t) (subkey & 0xFFFFFFFF));
158 static void skshashpath(char *buffer, size_t length,
159 const struct skshash *hash, char *basepath)
161 snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
162 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
164 hash->hash[0], hash->hash[1],
165 hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
166 hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
167 hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
168 hash->hash[12], hash->hash[13], hash->hash[14],
172 /*****************************************************************************/
175 * starttrans - Start a transaction.
177 static bool fs_starttrans(struct onak_dbctx *dbctx)
179 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
180 struct flock lockstruct;
183 F_RDLCK | ((privctx->lockfile_readonly) ? 0 : F_WRLCK);
184 lockstruct.l_whence = SEEK_SET;
185 lockstruct.l_start = 0;
186 lockstruct.l_len = 1;
188 while (fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct) == -1) {
189 if (remaining-- == 0)
190 return false; /* Hope to hell that noodles DTRT */
197 * endtrans - End a transaction.
199 static void fs_endtrans(struct onak_dbctx *dbctx)
201 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
202 struct flock lockstruct;
204 lockstruct.l_type = F_UNLCK;
205 lockstruct.l_whence = SEEK_SET;
206 lockstruct.l_start = 0;
207 lockstruct.l_len = 1;
208 fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct);
211 static uint64_t fs_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
213 static char buffer[PATH_MAX];
215 struct dirent *de = NULL;
218 keydir(buffer, sizeof(buffer), keyid, dbctx->config->location);
224 if (de && de->d_name[0] != '.') {
225 ret = strtoull(de->d_name, NULL, 16);
227 } while (de && de->d_name[0] == '.');
232 subkeydir(buffer, sizeof(buffer), keyid,
233 dbctx->config->location);
239 if (de && de->d_name[0] != '.') {
240 ret = strtoull(de->d_name, NULL, 16);
242 } while (de && de->d_name[0] == '.');
251 * fetch_key - Given a keyid fetch the key from storage.
252 * @keyid: The keyid to fetch.
253 * @publickey: A pointer to a structure to return the key in.
254 * @intrans: If we're already in a transaction.
256 static int fs_fetch_key_id(struct onak_dbctx *dbctx,
258 struct openpgp_publickey **publickey,
261 static char buffer[PATH_MAX];
263 struct openpgp_packet_list *packets = NULL;
266 fs_starttrans(dbctx);
268 if ((keyid >> 32) == 0)
269 keyid = fs_getfullkeyid(dbctx, keyid);
271 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
272 fd = open(buffer, O_RDONLY);
273 if (fd == -1 && errno == ENOENT) {
274 subkeypath(buffer, sizeof(buffer), keyid,
275 dbctx->config->location);
276 fd = open(buffer, O_RDONLY);
280 /* File is present, load it in... */
281 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
282 parse_keys(packets, publickey);
283 free_packet_list(packets);
295 * store_key - Takes a key and stores it.
296 * @publickey: A pointer to the public key to store.
297 * @intrans: If we're already in a transaction.
298 * @update: If true the key exists and should be updated.
300 static int fs_store_key(struct onak_dbctx *dbctx,
301 struct openpgp_publickey *publickey, bool intrans,
304 static char buffer[PATH_MAX];
305 static char wbuffer[PATH_MAX];
307 struct openpgp_packet_list *packets = NULL;
308 struct openpgp_packet_list *list_end = NULL;
309 struct openpgp_publickey *next = NULL;
311 struct ll *wordlist = NULL, *wl = NULL;
313 struct openpgp_fingerprint *subkeyids = NULL;
317 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
318 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
323 fs_starttrans(dbctx);
325 prove_path_to(keyid, "key", dbctx->config->location);
326 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
329 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
331 next = publickey->next;
332 publickey->next = NULL;
333 flatten_publickey(publickey, &packets, &list_end);
334 publickey->next = next;
336 write_openpgp_stream(file_putchar, &fd, packets);
338 free_packet_list(packets);
344 wl = wordlist = makewordlistfromkey(wordlist, publickey);
346 uint32_t hash = calchash((uint8_t *) (wl->object));
347 prove_path_to(hash, "words", dbctx->config->location);
349 worddir(wbuffer, sizeof(wbuffer), wl->object, hash,
350 dbctx->config->location);
351 mkdir(wbuffer, 0777);
352 wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
353 keyid, dbctx->config->location);
354 link(buffer, wbuffer);
358 llfree(wordlist, free);
360 subkeyids = keysubkeys(publickey);
362 while (subkeyids != NULL && subkeyids[i].length != 0) {
363 keyid = fingerprint2keyid(&subkeyids[i]);
365 prove_path_to(keyid, "subkeys",
366 dbctx->config->location);
368 subkeydir(wbuffer, sizeof(wbuffer), keyid,
369 dbctx->config->location);
370 mkdir(wbuffer, 0777);
371 subkeypath(wbuffer, sizeof(wbuffer), keyid,
372 dbctx->config->location);
373 link(buffer, wbuffer);
377 if (subkeyids != NULL) {
382 get_skshash(publickey, &hash);
383 hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
384 (hash.hash[2] << 8) + hash.hash[3];
385 prove_path_to(hashid, "skshash", dbctx->config->location);
386 skshashpath(wbuffer, sizeof(wbuffer), &hash,
387 dbctx->config->location);
388 link(buffer, wbuffer);
397 * delete_key - Given a keyid delete the key from storage.
398 * @keyid: The keyid to delete.
399 * @intrans: If we're already in a transaction.
401 static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
403 static char buffer[PATH_MAX];
405 struct openpgp_publickey *pk = NULL;
407 struct ll *wordlist = NULL, *wl = NULL;
408 struct openpgp_fingerprint *subkeyids = NULL;
412 if ((keyid >> 32) == 0)
413 keyid = fs_getfullkeyid(dbctx, keyid);
416 fs_starttrans(dbctx);
418 ret = fs_fetch_key_id(dbctx, keyid, &pk, true);
421 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
423 wl = wordlist = makewordlistfromkey(wordlist, pk);
424 logthing(LOGTHING_DEBUG,
425 "Wordlist for key %016" PRIX64 " done", keyid);
427 uint32_t hash = calchash((uint8_t *) (wl->object));
428 prove_path_to(hash, "words", dbctx->config->location);
430 wordpath(buffer, sizeof(buffer), wl->object, hash,
431 keyid, dbctx->config->location);
437 subkeyids = keysubkeys(pk);
439 while (subkeyids != NULL && subkeyids[i].length != 0) {
440 subkeyid = fingerprint2keyid(&subkeyids[i]);
441 prove_path_to(subkeyid, "subkeys",
442 dbctx->config->location);
444 subkeypath(buffer, sizeof(buffer), subkeyid,
445 dbctx->config->location);
450 if (subkeyids != NULL) {
455 get_skshash(pk, &hash);
456 skshashpath(buffer, sizeof(buffer), &hash,
457 dbctx->config->location);
461 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
469 static struct ll *internal_get_key_by_word(char *word, struct ll *mct,
472 struct ll *keys = NULL;
474 char buffer[PATH_MAX];
475 uint32_t hash = calchash((uint8_t *) (word));
478 worddir(buffer, sizeof(buffer), word, hash, basepath);
480 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
485 if (de && de->d_name[0] != '.') {
487 || (llfind(mct, de->d_name,
488 (int (*)(const void *, const void *))
491 logthing(LOGTHING_DEBUG,
492 "Found %s // %s", word,
507 * fetch_key_text - Trys to find the keys that contain the supplied text.
508 * @search: The text to search for.
509 * @publickey: A pointer to a structure to return the key in.
511 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
513 struct openpgp_publickey **publickey)
515 struct ll *wordlist = NULL, *wl = NULL;
516 struct ll *keylist = NULL;
517 char *searchtext = NULL;
520 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
522 searchtext = strdup(search);
523 wl = wordlist = makewordlist(wordlist, searchtext);
525 keylist = internal_get_key_by_word(wordlist->object, NULL,
526 dbctx->config->location);
529 llfree(wordlist, NULL);
538 internal_get_key_by_word(wl->object, keylist,
539 dbctx->config->location);
541 llfree(wordlist, NULL);
542 llfree(keylist, free);
547 llfree(keylist, free);
552 llfree(wordlist, NULL);
554 /* Now add the keys... */
557 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
559 fs_fetch_key_id(dbctx,
560 strtoull(wl->object, NULL, 16), publickey,
562 if (addedkeys >= config.maxkeys)
567 llfree(keylist, free);
575 * fetch_key_skshash - Given an SKS hash fetch the key from storage.
576 * @hash: The hash to fetch.
577 * @publickey: A pointer to a structure to return the key in.
578 * @intrans: If we're already in a transaction.
580 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
581 const struct skshash *hash,
582 struct openpgp_publickey **publickey)
584 static char buffer[PATH_MAX];
586 struct openpgp_packet_list *packets = NULL;
588 skshashpath(buffer, sizeof(buffer), hash, dbctx->config->location);
589 if ((fd = open(buffer, O_RDONLY)) != -1) {
590 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
591 parse_keys(packets, publickey);
592 free_packet_list(packets);
602 * iterate_keys - call a function once for each key in the db.
603 * @iterfunc: The function to call.
604 * @ctx: A context pointer
606 * Calls iterfunc once for each key in the database. ctx is passed
607 * unaltered to iterfunc. This function is intended to aid database dumps
608 * and statistic calculations.
610 * Returns the number of keys we iterated over.
612 static int fs_iterate_keys(struct onak_dbctx *dbctx,
613 void (*iterfunc)(void *ctx,
614 struct openpgp_publickey *key), void *ctx)
620 * Include the basic keydb routines.
622 #define NEED_KEYID2UID 1
623 #define NEED_GETKEYSIGS 1
624 #define NEED_UPDATEKEYS 1
625 #define NEED_GET_FP 1
629 * cleanupdb - De-initialize the key database.
631 static void fs_cleanupdb(struct onak_dbctx *dbctx)
633 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
635 /* Mmmm nothing to do here? */
636 close(privctx->lockfile_fd);
640 * initdb - Initialize the key database.
642 struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
644 char buffer[PATH_MAX];
645 struct onak_dbctx *dbctx;
646 struct onak_fs_dbctx *privctx;
648 dbctx = malloc(sizeof(struct onak_dbctx));
652 dbctx->config = dbcfg;
653 dbctx->priv = privctx = malloc(sizeof(*privctx));
654 if (privctx == NULL) {
659 privctx->lockfile_readonly = readonly;
661 snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
663 if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
664 if (errno != ENOENT) {
665 logthing(LOGTHING_CRITICAL,
666 "Unable to access keydb_fs root of '%s'. (%s)",
667 dbcfg->location, strerror(errno));
668 exit(1); /* Lacking rwx on the key dir */
670 mkdir(dbcfg->location, 0777);
671 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
673 if (chdir(dbcfg->location) == -1) {
674 /* Shouldn't happen after the above */
675 logthing(LOGTHING_CRITICAL,
676 "Couldn't change to database directory: %s",
682 privctx->lockfile_fd = open(buffer,
683 (privctx->lockfile_readonly) ?
685 if (privctx->lockfile_fd == -1)
686 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
687 if (privctx->lockfile_fd == -1) {
688 logthing(LOGTHING_CRITICAL,
689 "Unable to open lockfile '%s'. (%s)",
690 buffer, strerror(errno));
691 exit(1); /* Lacking rwx on the key dir */
694 dbctx->cleanupdb = fs_cleanupdb;
695 dbctx->starttrans = fs_starttrans;
696 dbctx->endtrans = fs_endtrans;
697 dbctx->fetch_key_id = fs_fetch_key_id;
698 dbctx->fetch_key_fp = generic_fetch_key_fp;
699 dbctx->fetch_key_text = fs_fetch_key_text;
700 dbctx->fetch_key_skshash = fs_fetch_key_skshash;
701 dbctx->store_key = fs_store_key;
702 dbctx->update_keys = generic_update_keys;
703 dbctx->delete_key = fs_delete_key;
704 dbctx->getkeysigs = generic_getkeysigs;
705 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
706 dbctx->keyid2uid = generic_keyid2uid;
707 dbctx->getfullkeyid = fs_getfullkeyid;
708 dbctx->iterate_keys = fs_iterate_keys;