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] << 24) + (hash.hash[1] << 16) +
383 (hash.hash[2] << 8) + hash.hash[3];
384 prove_path_to(hashid, "skshash", dbctx->config->location);
385 skshashpath(wbuffer, sizeof(wbuffer), &hash,
386 dbctx->config->location);
387 link(buffer, wbuffer);
396 * delete_key - Given a keyid delete the key from storage.
397 * @keyid: The keyid to delete.
398 * @intrans: If we're already in a transaction.
400 static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
402 static char buffer[PATH_MAX];
404 struct openpgp_publickey *pk = NULL;
406 struct ll *wordlist = NULL, *wl = NULL;
407 struct openpgp_fingerprint *subkeyids = NULL;
411 if ((keyid >> 32) == 0)
412 keyid = fs_getfullkeyid(dbctx, keyid);
415 fs_starttrans(dbctx);
417 ret = fs_fetch_key_id(dbctx, keyid, &pk, true);
420 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
422 wl = wordlist = makewordlistfromkey(wordlist, pk);
423 logthing(LOGTHING_DEBUG,
424 "Wordlist for key %016" PRIX64 " done", keyid);
426 uint32_t hash = calchash((uint8_t *) (wl->object));
427 prove_path_to(hash, "words", dbctx->config->location);
429 wordpath(buffer, sizeof(buffer), wl->object, hash,
430 keyid, dbctx->config->location);
436 subkeyids = keysubkeys(pk);
438 while (subkeyids != NULL && subkeyids[i].length != 0) {
439 subkeyid = fingerprint2keyid(&subkeyids[i]);
440 prove_path_to(subkeyid, "subkeys",
441 dbctx->config->location);
443 subkeypath(buffer, sizeof(buffer), subkeyid,
444 dbctx->config->location);
449 if (subkeyids != NULL) {
454 get_skshash(pk, &hash);
455 skshashpath(buffer, sizeof(buffer), &hash,
456 dbctx->config->location);
460 keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
468 static struct ll *internal_get_key_by_word(char *word, struct ll *mct,
471 struct ll *keys = NULL;
473 char buffer[PATH_MAX];
474 uint32_t hash = calchash((uint8_t *) (word));
477 worddir(buffer, sizeof(buffer), word, hash, basepath);
479 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
484 if (de && de->d_name[0] != '.') {
486 || (llfind(mct, de->d_name,
487 (int (*)(const void *, const void *))
490 logthing(LOGTHING_DEBUG,
491 "Found %s // %s", word,
506 * fetch_key_text - Trys to find the keys that contain the supplied text.
507 * @search: The text to search for.
508 * @publickey: A pointer to a structure to return the key in.
510 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
512 struct openpgp_publickey **publickey)
514 struct ll *wordlist = NULL, *wl = NULL;
515 struct ll *keylist = NULL;
516 char *searchtext = NULL;
519 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
521 searchtext = strdup(search);
522 wl = wordlist = makewordlist(wordlist, searchtext);
524 keylist = internal_get_key_by_word(wordlist->object, NULL,
525 dbctx->config->location);
528 llfree(wordlist, NULL);
537 internal_get_key_by_word(wl->object, keylist,
538 dbctx->config->location);
540 llfree(wordlist, NULL);
541 llfree(keylist, free);
546 llfree(keylist, free);
551 llfree(wordlist, NULL);
553 /* Now add the keys... */
556 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
558 fs_fetch_key_id(dbctx,
559 strtoull(wl->object, NULL, 16), publickey,
561 if (addedkeys >= config.maxkeys)
566 llfree(keylist, free);
574 * fetch_key_skshash - Given an SKS hash fetch the key from storage.
575 * @hash: The hash to fetch.
576 * @publickey: A pointer to a structure to return the key in.
577 * @intrans: If we're already in a transaction.
579 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
580 const struct skshash *hash,
581 struct openpgp_publickey **publickey)
583 static char buffer[PATH_MAX];
585 struct openpgp_packet_list *packets = NULL;
587 skshashpath(buffer, sizeof(buffer), hash, dbctx->config->location);
588 if ((fd = open(buffer, O_RDONLY)) != -1) {
589 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
590 parse_keys(packets, publickey);
591 free_packet_list(packets);
601 * iterate_keys - call a function once for each key in the db.
602 * @iterfunc: The function to call.
603 * @ctx: A context pointer
605 * Calls iterfunc once for each key in the database. ctx is passed
606 * unaltered to iterfunc. This function is intended to aid database dumps
607 * and statistic calculations.
609 * Returns the number of keys we iterated over.
611 static int fs_iterate_keys(struct onak_dbctx *dbctx,
612 void (*iterfunc)(void *ctx,
613 struct openpgp_publickey *key), void *ctx)
619 * Include the basic keydb routines.
621 #define NEED_KEYID2UID 1
622 #define NEED_GETKEYSIGS 1
623 #define NEED_UPDATEKEYS 1
624 #define NEED_GET_FP 1
628 * cleanupdb - De-initialize the key database.
630 static void fs_cleanupdb(struct onak_dbctx *dbctx)
632 struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
634 /* Mmmm nothing to do here? */
635 close(privctx->lockfile_fd);
639 * initdb - Initialize the key database.
641 struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
643 char buffer[PATH_MAX];
644 struct onak_dbctx *dbctx;
645 struct onak_fs_dbctx *privctx;
647 dbctx = malloc(sizeof(struct onak_dbctx));
651 dbctx->config = dbcfg;
652 dbctx->priv = privctx = malloc(sizeof(*privctx));
653 if (privctx == NULL) {
658 privctx->lockfile_readonly = readonly;
660 snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
662 if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
663 if (errno != ENOENT) {
664 logthing(LOGTHING_CRITICAL,
665 "Unable to access keydb_fs root of '%s'. (%s)",
666 dbcfg->location, strerror(errno));
667 exit(1); /* Lacking rwx on the key dir */
669 mkdir(dbcfg->location, 0777);
670 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
672 if (chdir(dbcfg->location) == -1) {
673 /* Shouldn't happen after the above */
674 logthing(LOGTHING_CRITICAL,
675 "Couldn't change to database directory: %s",
681 privctx->lockfile_fd = open(buffer,
682 (privctx->lockfile_readonly) ?
684 if (privctx->lockfile_fd == -1)
685 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
686 if (privctx->lockfile_fd == -1) {
687 logthing(LOGTHING_CRITICAL,
688 "Unable to open lockfile '%s'. (%s)",
689 buffer, strerror(errno));
690 exit(1); /* Lacking rwx on the key dir */
693 dbctx->cleanupdb = fs_cleanupdb;
694 dbctx->starttrans = fs_starttrans;
695 dbctx->endtrans = fs_endtrans;
696 dbctx->fetch_key_id = fs_fetch_key_id;
697 dbctx->fetch_key_fp = generic_fetch_key_fp;
698 dbctx->fetch_key_text = fs_fetch_key_text;
699 dbctx->fetch_key_skshash = fs_fetch_key_skshash;
700 dbctx->store_key = fs_store_key;
701 dbctx->update_keys = generic_update_keys;
702 dbctx->delete_key = fs_delete_key;
703 dbctx->getkeysigs = generic_getkeysigs;
704 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
705 dbctx->keyid2uid = generic_keyid2uid;
706 dbctx->getfullkeyid = fs_getfullkeyid;
707 dbctx->iterate_keys = fs_iterate_keys;