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"
37 #include "keystructs.h"
41 #include "onak-conf.h"
46 /* Hack: We should really dynamically allocate our path buffers */
51 struct onak_fs_dbctx {
53 bool lockfile_readonly;
56 /*****************************************************************************/
58 /* Helper functions */
60 #define FNV_offset_basis 2166136261ul
61 #define FNV_mixing_prime 16777619ul
63 static uint32_t calchash(uint8_t * ptr)
65 register uint32_t h = FNV_offset_basis;
66 register uint32_t p = FNV_mixing_prime;
67 register uint32_t n = strlen((char *) ptr);
68 register uint8_t *c = ptr;
73 return h ? h : 1; /* prevent a hash of zero happening */
77 static void keypath(char *buffer, size_t length, uint64_t _keyid,
80 uint64_t keyid = _keyid << 32;
81 snprintf(buffer, length, "%s/key/%02X/%02X/%08X/%016" PRIX64,
82 basepath, (uint8_t) ((keyid >> 56) & 0xFF),
83 (uint8_t) ((keyid >> 48) & 0xFF),
84 (uint32_t) (keyid >> 32), _keyid);
87 static void keydir(char *buffer, size_t length, uint64_t _keyid,
90 uint64_t keyid = _keyid << 32;
91 snprintf(buffer, length, "%s/key/%02X/%02X/%08X", basepath,
92 (uint8_t) ((keyid >> 56) & 0xFF),
93 (uint8_t) ((keyid >> 48) & 0xFF),
94 (uint32_t) (keyid >> 32));
97 static void prove_path_to(uint64_t keyid, char *what, char *basepath)
99 static char buffer[PATH_MAX];
100 snprintf(buffer, sizeof(buffer), "%s/%s", basepath, what);
103 snprintf(buffer, sizeof(buffer), "%s/%s/%02X", basepath, what,
104 (uint8_t) ((keyid >> 24) & 0xFF));
107 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", basepath,
109 (uint8_t) ((keyid >> 24) & 0xFF),
110 (uint8_t) ((keyid >> 16) & 0xFF));
113 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", basepath,
115 (uint8_t) ((keyid >> 24) & 0xFF),
116 (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
120 static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
121 uint64_t keyid, char *basepath)
123 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
124 basepath, (uint8_t) ((hash >> 24) & 0xFF),
125 (uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
128 static void worddir(char *buffer, size_t length, char *word, uint32_t hash,
131 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", basepath,
132 (uint8_t) ((hash >> 24) & 0xFF),
133 (uint8_t) ((hash >> 16) & 0xFF), hash, word);
136 static void subkeypath(char *buffer, size_t length, uint64_t subkey,
139 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
141 (uint8_t) ((subkey >> 24) & 0xFF),
142 (uint8_t) ((subkey >> 16) & 0xFF),
143 (uint32_t) (subkey & 0xFFFFFFFF),
147 static void subkeydir(char *buffer, size_t length, uint64_t subkey,
150 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
152 (uint8_t) ((subkey >> 24) & 0xFF),
153 (uint8_t) ((subkey >> 16) & 0xFF),
154 (uint32_t) (subkey & 0xFFFFFFFF));
157 static void skshashpath(char *buffer, size_t length,
158 const struct skshash *hash, char *basepath)
160 snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
161 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
163 hash->hash[0], hash->hash[1],
164 hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
165 hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
166 hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
167 hash->hash[12], hash->hash[13], hash->hash[14],
171 /*****************************************************************************/
174 * starttrans - Start a transaction.
176 static bool fs_starttrans(struct onak_dbctx *dbctx)
178 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
179 struct flock lockstruct;
182 F_RDLCK | ((privctx->lockfile_readonly) ? 0 : F_WRLCK);
183 lockstruct.l_whence = SEEK_SET;
184 lockstruct.l_start = 0;
185 lockstruct.l_len = 1;
187 while (fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct) == -1) {
188 if (remaining-- == 0)
189 return false; /* Hope to hell that noodles DTRT */
196 * endtrans - End a transaction.
198 static void fs_endtrans(struct onak_dbctx *dbctx)
200 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
201 struct flock lockstruct;
203 lockstruct.l_type = F_UNLCK;
204 lockstruct.l_whence = SEEK_SET;
205 lockstruct.l_start = 0;
206 lockstruct.l_len = 1;
207 fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct);
210 static uint64_t fs_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
212 static char buffer[PATH_MAX];
214 struct dirent *de = NULL;
217 keydir(buffer, sizeof(buffer), keyid, dbctx->config->location);
223 if (de && de->d_name[0] != '.') {
224 ret = strtoull(de->d_name, NULL, 16);
226 } while (de && de->d_name[0] == '.');
231 subkeydir(buffer, sizeof(buffer), keyid,
232 dbctx->config->location);
238 if (de && de->d_name[0] != '.') {
239 ret = strtoull(de->d_name, NULL, 16);
241 } while (de && de->d_name[0] == '.');
250 * fetch_key - Given a keyid fetch the key from storage.
251 * @keyid: The keyid to fetch.
252 * @publickey: A pointer to a structure to return the key in.
253 * @intrans: If we're already in a transaction.
255 static int fs_fetch_key_id(struct onak_dbctx *dbctx,
257 struct openpgp_publickey **publickey,
260 static char buffer[PATH_MAX];
262 struct openpgp_packet_list *packets = NULL;
265 fs_starttrans(dbctx);
267 if ((keyid >> 32) == 0)
268 keyid = fs_getfullkeyid(dbctx, keyid);
270 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
271 fd = open(buffer, O_RDONLY);
272 if (fd == -1 && errno == ENOENT) {
273 subkeypath(buffer, sizeof(buffer), keyid,
274 dbctx->config->location);
275 fd = open(buffer, O_RDONLY);
279 /* File is present, load it in... */
280 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
281 parse_keys(packets, publickey);
282 free_packet_list(packets);
294 * store_key - Takes a key and stores it.
295 * @publickey: A pointer to the public key to store.
296 * @intrans: If we're already in a transaction.
297 * @update: If true the key exists and should be updated.
299 static int fs_store_key(struct onak_dbctx *dbctx,
300 struct openpgp_publickey *publickey, bool intrans,
303 static char buffer[PATH_MAX];
304 static char wbuffer[PATH_MAX];
306 struct openpgp_packet_list *packets = NULL;
307 struct openpgp_packet_list *list_end = NULL;
308 struct openpgp_publickey *next = NULL;
310 struct ll *wordlist = NULL, *wl = NULL;
312 struct openpgp_fingerprint *subkeyids = NULL;
316 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
317 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
322 fs_starttrans(dbctx);
324 prove_path_to(keyid, "key", dbctx->config->location);
325 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
328 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
330 next = publickey->next;
331 publickey->next = NULL;
332 flatten_publickey(publickey, &packets, &list_end);
333 publickey->next = next;
335 write_openpgp_stream(file_putchar, &fd, packets);
337 free_packet_list(packets);
343 wl = wordlist = makewordlistfromkey(wordlist, publickey);
345 uint32_t hash = calchash((uint8_t *) (wl->object));
346 prove_path_to(hash, "words", dbctx->config->location);
348 worddir(wbuffer, sizeof(wbuffer), wl->object, hash,
349 dbctx->config->location);
350 mkdir(wbuffer, 0777);
351 wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
352 keyid, dbctx->config->location);
353 link(buffer, wbuffer);
357 llfree(wordlist, free);
359 subkeyids = keysubkeys(publickey);
361 while (subkeyids != NULL && subkeyids[i].length != 0) {
362 keyid = fingerprint2keyid(&subkeyids[i]);
364 prove_path_to(keyid, "subkeys",
365 dbctx->config->location);
367 subkeydir(wbuffer, sizeof(wbuffer), keyid,
368 dbctx->config->location);
369 mkdir(wbuffer, 0777);
370 subkeypath(wbuffer, sizeof(wbuffer), keyid,
371 dbctx->config->location);
372 link(buffer, wbuffer);
376 if (subkeyids != NULL) {
381 get_skshash(publickey, &hash);
382 hashid = hash.hash[0];
384 hashid |= hash.hash[1];
386 hashid |= hash.hash[2];
388 hashid |= hash.hash[3];
389 prove_path_to(hashid, "skshash", dbctx->config->location);
390 skshashpath(wbuffer, sizeof(wbuffer), &hash,
391 dbctx->config->location);
392 link(buffer, wbuffer);
401 * delete_key - Given a keyid delete the key from storage.
402 * @fp: The fingerprint of the key to delete.
403 * @intrans: If we're already in a transaction.
405 static int fs_delete_key(struct onak_dbctx *dbctx,
406 struct openpgp_fingerprint *fp, bool intrans)
408 static char buffer[PATH_MAX];
410 struct openpgp_publickey *pk = NULL;
412 struct ll *wordlist = NULL, *wl = NULL;
413 struct openpgp_fingerprint *subkeyids = NULL;
418 keyid = fingerprint2keyid(fp);
423 fs_starttrans(dbctx);
425 ret = fs_fetch_key_id(dbctx, keyid, &pk, true);
428 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
430 wl = wordlist = makewordlistfromkey(wordlist, pk);
431 logthing(LOGTHING_DEBUG,
432 "Wordlist for key %016" PRIX64 " done", keyid);
434 uint32_t hash = calchash((uint8_t *) (wl->object));
435 prove_path_to(hash, "words", dbctx->config->location);
437 wordpath(buffer, sizeof(buffer), wl->object, hash,
438 keyid, dbctx->config->location);
443 llfree(wordlist, free);
446 subkeyids = keysubkeys(pk);
448 while (subkeyids != NULL && subkeyids[i].length != 0) {
449 subkeyid = fingerprint2keyid(&subkeyids[i]);
450 prove_path_to(subkeyid, "subkeys",
451 dbctx->config->location);
453 subkeypath(buffer, sizeof(buffer), subkeyid,
454 dbctx->config->location);
459 if (subkeyids != NULL) {
464 get_skshash(pk, &hash);
465 skshashpath(buffer, sizeof(buffer), &hash,
466 dbctx->config->location);
470 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
480 static struct ll *internal_get_key_by_word(char *word, struct ll *mct,
483 struct ll *keys = NULL;
485 char buffer[PATH_MAX];
486 uint32_t hash = calchash((uint8_t *) (word));
489 worddir(buffer, sizeof(buffer), word, hash, basepath);
491 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
496 if (de && de->d_name[0] != '.') {
498 || (llfind(mct, de->d_name,
499 (int (*)(const void *, const void *))
502 logthing(LOGTHING_DEBUG,
503 "Found %s // %s", word,
518 * fetch_key_text - Trys to find the keys that contain the supplied text.
519 * @search: The text to search for.
520 * @publickey: A pointer to a structure to return the key in.
522 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
524 struct openpgp_publickey **publickey)
526 struct ll *wordlist = NULL, *wl = NULL;
527 struct ll *keylist = NULL;
528 char *searchtext = NULL;
531 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
533 searchtext = strdup(search);
534 wl = wordlist = makewordlist(wordlist, searchtext);
536 keylist = internal_get_key_by_word(wordlist->object, NULL,
537 dbctx->config->location);
540 llfree(wordlist, NULL);
549 internal_get_key_by_word(wl->object, keylist,
550 dbctx->config->location);
552 llfree(wordlist, NULL);
553 llfree(keylist, free);
558 llfree(keylist, free);
563 llfree(wordlist, NULL);
565 /* Now add the keys... */
568 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
570 fs_fetch_key_id(dbctx,
571 strtoull(wl->object, NULL, 16), publickey,
573 if (addedkeys >= config.maxkeys)
578 llfree(keylist, free);
586 * fetch_key_skshash - Given an SKS hash fetch the key from storage.
587 * @hash: The hash to fetch.
588 * @publickey: A pointer to a structure to return the key in.
589 * @intrans: If we're already in a transaction.
591 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
592 const struct skshash *hash,
593 struct openpgp_publickey **publickey)
595 static char buffer[PATH_MAX];
597 struct openpgp_packet_list *packets = NULL;
599 skshashpath(buffer, sizeof(buffer), hash, dbctx->config->location);
600 if ((fd = open(buffer, O_RDONLY)) != -1) {
601 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
602 parse_keys(packets, publickey);
603 free_packet_list(packets);
613 * iterate_keys - call a function once for each key in the db.
614 * @iterfunc: The function to call.
615 * @ctx: A context pointer
617 * Calls iterfunc once for each key in the database. ctx is passed
618 * unaltered to iterfunc. This function is intended to aid database dumps
619 * and statistic calculations.
621 * Returns the number of keys we iterated over.
623 static int fs_iterate_keys(struct onak_dbctx *dbctx,
624 void (*iterfunc)(void *ctx,
625 struct openpgp_publickey *key), void *ctx)
631 * Include the basic keydb routines.
633 #define NEED_KEYID2UID 1
634 #define NEED_GETKEYSIGS 1
635 #define NEED_UPDATEKEYS 1
636 #define NEED_GET_FP 1
640 * cleanupdb - De-initialize the key database.
642 static void fs_cleanupdb(struct onak_dbctx *dbctx)
644 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
646 /* Mmmm nothing to do here? */
647 close(privctx->lockfile_fd);
655 * initdb - Initialize the key database.
657 struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
659 char buffer[PATH_MAX];
660 struct onak_dbctx *dbctx;
661 struct onak_fs_dbctx *privctx;
663 dbctx = malloc(sizeof(struct onak_dbctx));
667 dbctx->config = dbcfg;
668 dbctx->priv = privctx = malloc(sizeof(*privctx));
669 if (privctx == NULL) {
674 privctx->lockfile_readonly = readonly;
676 snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
678 if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
679 if (errno != ENOENT) {
680 logthing(LOGTHING_CRITICAL,
681 "Unable to access keydb_fs root of '%s'. (%s)",
682 dbcfg->location, strerror(errno));
683 exit(1); /* Lacking rwx on the key dir */
685 mkdir(dbcfg->location, 0777);
686 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
688 if (chdir(dbcfg->location) == -1) {
689 /* Shouldn't happen after the above */
690 logthing(LOGTHING_CRITICAL,
691 "Couldn't change to database directory: %s",
697 privctx->lockfile_fd = open(buffer,
698 (privctx->lockfile_readonly) ?
700 if (privctx->lockfile_fd == -1)
701 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
702 if (privctx->lockfile_fd == -1) {
703 logthing(LOGTHING_CRITICAL,
704 "Unable to open lockfile '%s'. (%s)",
705 buffer, strerror(errno));
706 exit(1); /* Lacking rwx on the key dir */
709 dbctx->cleanupdb = fs_cleanupdb;
710 dbctx->starttrans = fs_starttrans;
711 dbctx->endtrans = fs_endtrans;
712 dbctx->fetch_key_id = fs_fetch_key_id;
713 dbctx->fetch_key_fp = generic_fetch_key_fp;
714 dbctx->fetch_key_text = fs_fetch_key_text;
715 dbctx->fetch_key_skshash = fs_fetch_key_skshash;
716 dbctx->store_key = fs_store_key;
717 dbctx->update_keys = generic_update_keys;
718 dbctx->delete_key = fs_delete_key;
719 dbctx->getkeysigs = generic_getkeysigs;
720 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
721 dbctx->keyid2uid = generic_keyid2uid;
722 dbctx->iterate_keys = fs_iterate_keys;