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, see <https://www.gnu.org/licenses/>.
19 #include <sys/types.h>
33 #include "charfuncs.h"
34 #include "decodekey.h"
35 #include "key-store.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;
267 fs_starttrans(dbctx);
269 if ((keyid >> 32) == 0)
270 keyid = fs_getfullkeyid(dbctx, keyid);
272 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
273 res = onak_read_openpgp_file(buffer,
275 if (res == ONAK_E_NOT_FOUND) {
276 subkeypath(buffer, sizeof(buffer), keyid,
277 dbctx->config->location);
278 res = onak_read_openpgp_file(buffer,
282 if (res == ONAK_E_OK) {
283 /* File is present, load it in... */
284 parse_keys(packets, publickey);
285 free_packet_list(packets);
296 * store_key - Takes a key and stores it.
297 * @publickey: A pointer to the public key to store.
298 * @intrans: If we're already in a transaction.
299 * @update: If true the key exists and should be updated.
301 static int fs_store_key(struct onak_dbctx *dbctx,
302 struct openpgp_publickey *publickey, bool intrans,
305 static char buffer[PATH_MAX];
306 static char wbuffer[PATH_MAX];
308 struct openpgp_packet_list *packets = NULL;
309 struct openpgp_packet_list *list_end = NULL;
310 struct openpgp_publickey *next = NULL;
312 struct ll *wordlist = NULL, *wl = NULL;
314 struct openpgp_fingerprint *subkeyids = NULL;
318 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
319 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
324 fs_starttrans(dbctx);
326 prove_path_to(keyid, "key", dbctx->config->location);
327 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
330 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
332 next = publickey->next;
333 publickey->next = NULL;
334 flatten_publickey(publickey, &packets, &list_end);
335 publickey->next = next;
337 write_openpgp_stream(file_putchar, &fd, packets);
339 free_packet_list(packets);
345 wl = wordlist = makewordlistfromkey(wordlist, publickey);
347 uint32_t hash = calchash((uint8_t *) (wl->object));
348 prove_path_to(hash, "words", dbctx->config->location);
350 worddir(wbuffer, sizeof(wbuffer), wl->object, hash,
351 dbctx->config->location);
352 mkdir(wbuffer, 0777);
353 wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
354 keyid, dbctx->config->location);
355 link(buffer, wbuffer);
359 llfree(wordlist, free);
361 subkeyids = keysubkeys(publickey);
363 while (subkeyids != NULL && subkeyids[i].length != 0) {
364 keyid = fingerprint2keyid(&subkeyids[i]);
366 prove_path_to(keyid, "subkeys",
367 dbctx->config->location);
369 subkeydir(wbuffer, sizeof(wbuffer), keyid,
370 dbctx->config->location);
371 mkdir(wbuffer, 0777);
372 subkeypath(wbuffer, sizeof(wbuffer), keyid,
373 dbctx->config->location);
374 link(buffer, wbuffer);
378 if (subkeyids != NULL) {
383 get_skshash(publickey, &hash);
384 hashid = hash.hash[0];
386 hashid |= hash.hash[1];
388 hashid |= hash.hash[2];
390 hashid |= hash.hash[3];
391 prove_path_to(hashid, "skshash", dbctx->config->location);
392 skshashpath(wbuffer, sizeof(wbuffer), &hash,
393 dbctx->config->location);
394 link(buffer, wbuffer);
403 * delete_key - Given a keyid delete the key from storage.
404 * @fp: The fingerprint of the key to delete.
405 * @intrans: If we're already in a transaction.
407 static int fs_delete_key(struct onak_dbctx *dbctx,
408 struct openpgp_fingerprint *fp, bool intrans)
410 static char buffer[PATH_MAX];
412 struct openpgp_publickey *pk = NULL;
414 struct ll *wordlist = NULL, *wl = NULL;
415 struct openpgp_fingerprint *subkeyids = NULL;
420 keyid = fingerprint2keyid(fp);
425 fs_starttrans(dbctx);
427 ret = fs_fetch_key_id(dbctx, keyid, &pk, true);
430 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
432 wl = wordlist = makewordlistfromkey(wordlist, pk);
433 logthing(LOGTHING_DEBUG,
434 "Wordlist for key %016" PRIX64 " done", keyid);
436 uint32_t hash = calchash((uint8_t *) (wl->object));
437 prove_path_to(hash, "words", dbctx->config->location);
439 wordpath(buffer, sizeof(buffer), wl->object, hash,
440 keyid, dbctx->config->location);
445 llfree(wordlist, free);
448 subkeyids = keysubkeys(pk);
450 while (subkeyids != NULL && subkeyids[i].length != 0) {
451 subkeyid = fingerprint2keyid(&subkeyids[i]);
452 prove_path_to(subkeyid, "subkeys",
453 dbctx->config->location);
455 subkeypath(buffer, sizeof(buffer), subkeyid,
456 dbctx->config->location);
461 if (subkeyids != NULL) {
466 get_skshash(pk, &hash);
467 skshashpath(buffer, sizeof(buffer), &hash,
468 dbctx->config->location);
472 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
482 static struct ll *internal_get_key_by_word(char *word, struct ll *mct,
485 struct ll *keys = NULL;
487 char buffer[PATH_MAX];
488 uint32_t hash = calchash((uint8_t *) (word));
491 worddir(buffer, sizeof(buffer), word, hash, basepath);
493 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
498 if (de && de->d_name[0] != '.') {
500 || (llfind(mct, de->d_name,
501 (int (*)(const void *, const void *))
504 logthing(LOGTHING_DEBUG,
505 "Found %s // %s", word,
520 * fetch_key_text - Trys to find the keys that contain the supplied text.
521 * @search: The text to search for.
522 * @publickey: A pointer to a structure to return the key in.
524 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
526 struct openpgp_publickey **publickey)
528 struct ll *wordlist = NULL, *wl = NULL;
529 struct ll *keylist = NULL;
530 char *searchtext = NULL;
533 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
535 searchtext = strdup(search);
536 wl = wordlist = makewordlist(wordlist, searchtext);
538 keylist = internal_get_key_by_word(wordlist->object, NULL,
539 dbctx->config->location);
542 llfree(wordlist, NULL);
551 internal_get_key_by_word(wl->object, keylist,
552 dbctx->config->location);
554 llfree(wordlist, NULL);
555 llfree(keylist, free);
560 llfree(keylist, free);
565 llfree(wordlist, NULL);
567 /* Now add the keys... */
570 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
572 fs_fetch_key_id(dbctx,
573 strtoull(wl->object, NULL, 16), publickey,
575 if (addedkeys >= config.maxkeys)
580 llfree(keylist, free);
588 * fetch_key_skshash - Given an SKS hash fetch the key from storage.
589 * @hash: The hash to fetch.
590 * @publickey: A pointer to a structure to return the key in.
591 * @intrans: If we're already in a transaction.
593 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
594 const struct skshash *hash,
595 struct openpgp_publickey **publickey)
597 static char buffer[PATH_MAX];
599 struct openpgp_packet_list *packets = NULL;
602 skshashpath(buffer, sizeof(buffer), hash, dbctx->config->location);
603 res = onak_read_openpgp_file(buffer, &packets);
604 if (res == ONAK_E_OK) {
605 parse_keys(packets, publickey);
606 free_packet_list(packets);
615 * iterate_keys - call a function once for each key in the db.
616 * @iterfunc: The function to call.
617 * @ctx: A context pointer
619 * Calls iterfunc once for each key in the database. ctx is passed
620 * unaltered to iterfunc. This function is intended to aid database dumps
621 * and statistic calculations.
623 * Returns the number of keys we iterated over.
625 static int fs_iterate_keys(__unused struct onak_dbctx *dbctx,
626 __unused void (*iterfunc)(void *ctx,
627 struct openpgp_publickey *key),
634 * Include the basic keydb routines.
636 #define NEED_KEYID2UID 1
637 #define NEED_GETKEYSIGS 1
638 #define NEED_UPDATEKEYS 1
640 #define NEED_GET_FP 1
644 * cleanupdb - De-initialize the key database.
646 static void fs_cleanupdb(struct onak_dbctx *dbctx)
648 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
650 /* Mmmm nothing to do here? */
651 close(privctx->lockfile_fd);
659 * initdb - Initialize the key database.
661 struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
663 char buffer[PATH_MAX];
664 struct onak_dbctx *dbctx;
665 struct onak_fs_dbctx *privctx;
667 dbctx = malloc(sizeof(struct onak_dbctx));
671 dbctx->config = dbcfg;
672 dbctx->priv = privctx = malloc(sizeof(*privctx));
673 if (privctx == NULL) {
678 privctx->lockfile_readonly = readonly;
680 snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
682 if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
683 if (errno != ENOENT) {
684 logthing(LOGTHING_CRITICAL,
685 "Unable to access keydb_fs root of '%s'. (%s)",
686 dbcfg->location, strerror(errno));
687 exit(1); /* Lacking rwx on the key dir */
689 mkdir(dbcfg->location, 0777);
690 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
692 if (chdir(dbcfg->location) == -1) {
693 /* Shouldn't happen after the above */
694 logthing(LOGTHING_CRITICAL,
695 "Couldn't change to database directory: %s",
701 privctx->lockfile_fd = open(buffer,
702 (privctx->lockfile_readonly) ?
704 if (privctx->lockfile_fd == -1)
705 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
706 if (privctx->lockfile_fd == -1) {
707 logthing(LOGTHING_CRITICAL,
708 "Unable to open lockfile '%s'. (%s)",
709 buffer, strerror(errno));
710 exit(1); /* Lacking rwx on the key dir */
713 dbctx->cleanupdb = fs_cleanupdb;
714 dbctx->starttrans = fs_starttrans;
715 dbctx->endtrans = fs_endtrans;
716 dbctx->fetch_key = generic_fetch_key;
717 dbctx->fetch_key_fp = generic_fetch_key_fp;
718 dbctx->fetch_key_id = fs_fetch_key_id;
719 dbctx->fetch_key_text = fs_fetch_key_text;
720 dbctx->fetch_key_skshash = fs_fetch_key_skshash;
721 dbctx->store_key = fs_store_key;
722 dbctx->update_keys = generic_update_keys;
723 dbctx->delete_key = fs_delete_key;
724 dbctx->getkeysigs = generic_getkeysigs;
725 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
726 dbctx->keyid2uid = generic_keyid2uid;
727 dbctx->iterate_keys = fs_iterate_keys;