2 * keydb.h - Routines to store and fetch keys.
4 * Daniel Silverstone <dsilvers@digital-scurf.org>
6 * Copyright 2004 Daniel Silverstone and Project Purple
21 #include "charfuncs.h"
22 #include "decodekey.h"
25 #include "keystructs.h"
28 #include "onak-conf.h"
33 /* Hack: We should really dynamically allocate our path buffers */
38 static int keydb_lockfile_fd = -1;
39 static bool keydb_lockfile_readonly;
41 /*****************************************************************************/
43 /* Helper functions */
45 #define FNV_offset_basis 2166136261ul
46 #define FNV_mixing_prime 16777619ul
48 static uint32_t calchash(uint8_t * ptr)
50 register uint32_t h = FNV_offset_basis;
51 register uint32_t p = FNV_mixing_prime;
52 register uint32_t n = strlen((char *) ptr);
53 register uint8_t *c = ptr;
58 return h ? h : 1; /* prevent a hash of zero happening */
62 static void keypath(char *buffer, size_t length, uint64_t _keyid)
64 uint64_t keyid = _keyid << 32;
65 snprintf(buffer, length, "%s/key/%02X/%02X/%08X/%016" PRIX64,
66 config.db_dir, (uint8_t) ((keyid >> 56) & 0xFF),
67 (uint8_t) ((keyid >> 48) & 0xFF),
68 (uint32_t) (keyid >> 32), _keyid);
71 static void keydir(char *buffer, size_t length, uint64_t _keyid)
73 uint64_t keyid = _keyid << 32;
74 snprintf(buffer, length, "%s/key/%02X/%02X/%08X", config.db_dir,
75 (uint8_t) ((keyid >> 56) & 0xFF),
76 (uint8_t) ((keyid >> 48) & 0xFF),
77 (uint32_t) (keyid >> 32));
80 static void prove_path_to(uint64_t keyid, char *what)
82 static char buffer[PATH_MAX];
83 snprintf(buffer, sizeof(buffer), "%s/%s", config.db_dir, what);
86 snprintf(buffer, sizeof(buffer), "%s/%s/%02X", config.db_dir, what,
87 (uint8_t) ((keyid >> 24) & 0xFF));
90 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", config.db_dir,
92 (uint8_t) ((keyid >> 24) & 0xFF),
93 (uint8_t) ((keyid >> 16) & 0xFF));
96 snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", config.db_dir,
98 (uint8_t) ((keyid >> 24) & 0xFF),
99 (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
103 static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
106 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
107 config.db_dir, (uint8_t) ((hash >> 24) & 0xFF),
108 (uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
111 static void worddir(char *buffer, size_t length, char *word, uint32_t hash)
113 snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", config.db_dir,
114 (uint8_t) ((hash >> 24) & 0xFF),
115 (uint8_t) ((hash >> 16) & 0xFF), hash, word);
118 static void subkeypath(char *buffer, size_t length, uint64_t subkey,
121 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
123 (uint8_t) ((subkey >> 24) & 0xFF),
124 (uint8_t) ((subkey >> 16) & 0xFF),
125 (uint32_t) (subkey & 0xFFFFFFFF),
129 static void skshashpath(char *buffer, size_t length,
130 const struct skshash *hash)
132 snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
133 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
135 hash->hash[0], hash->hash[1],
136 hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
137 hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
138 hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
139 hash->hash[12], hash->hash[13], hash->hash[14],
142 static void subkeydir(char *buffer, size_t length, uint64_t subkey)
144 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
146 (uint8_t) ((subkey >> 24) & 0xFF),
147 (uint8_t) ((subkey >> 16) & 0xFF),
148 (uint32_t) (subkey & 0xFFFFFFFF));
151 /*****************************************************************************/
154 * initdb - Initialize the key database.
156 static void fs_initdb(bool readonly)
158 char buffer[PATH_MAX];
160 keydb_lockfile_readonly = readonly;
162 snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
164 if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
165 if (errno != ENOENT) {
166 logthing(LOGTHING_CRITICAL,
167 "Unable to access keydb_fs root of '%s'. (%s)",
168 config.db_dir, strerror(errno));
169 exit(1); /* Lacking rwx on the key dir */
171 mkdir(config.db_dir, 0777);
172 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
174 chdir(config.db_dir);
175 if (keydb_lockfile_fd == -1)
176 keydb_lockfile_fd = open(buffer,
177 (keydb_lockfile_readonly) ?
179 if (keydb_lockfile_fd == -1)
180 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
181 if (keydb_lockfile_fd == -1) {
182 logthing(LOGTHING_CRITICAL,
183 "Unable to open lockfile '%s'. (%s)",
184 buffer, strerror(errno));
185 exit(1); /* Lacking rwx on the key dir */
190 * cleanupdb - De-initialize the key database.
192 static void fs_cleanupdb(void)
194 /* Mmmm nothing to do here? */
195 close(keydb_lockfile_fd);
199 * starttrans - Start a transaction.
201 static bool fs_starttrans(void)
203 struct flock lockstruct;
206 F_RDLCK | ((keydb_lockfile_readonly) ? 0 : F_WRLCK);
207 lockstruct.l_whence = SEEK_SET;
208 lockstruct.l_start = 0;
209 lockstruct.l_len = 1;
211 while (fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct) == -1) {
212 if (remaining-- == 0)
213 return false; /* Hope to hell that noodles DTRT */
220 * endtrans - End a transaction.
222 static void fs_endtrans(void)
224 struct flock lockstruct;
226 lockstruct.l_type = F_UNLCK;
227 lockstruct.l_whence = SEEK_SET;
228 lockstruct.l_start = 0;
229 lockstruct.l_len = 1;
230 fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct);
233 static uint64_t fs_getfullkeyid(uint64_t keyid)
235 static char buffer[PATH_MAX];
237 struct dirent *de = NULL;
240 keydir(buffer, sizeof(buffer), keyid);
246 if (de && de->d_name[0] != '.') {
247 ret = strtoull(de->d_name, NULL, 16);
249 } while (de && de->d_name[0] == '.');
254 subkeydir(buffer, sizeof(buffer), keyid);
260 if (de && de->d_name[0] != '.') {
261 ret = strtoull(de->d_name, NULL, 16);
263 } while (de && de->d_name[0] == '.');
272 * fetch_key - Given a keyid fetch the key from storage.
273 * @keyid: The keyid to fetch.
274 * @publickey: A pointer to a structure to return the key in.
275 * @intrans: If we're already in a transaction.
277 static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
280 static char buffer[PATH_MAX];
282 struct openpgp_packet_list *packets = NULL;
287 if ((keyid >> 32) == 0)
288 keyid = fs_getfullkeyid(keyid);
290 keypath(buffer, sizeof(buffer), keyid);
291 if ((fd = open(buffer, O_RDONLY)) != -1) {
292 /* File is present, load it in... */
293 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
294 parse_keys(packets, publickey);
295 free_packet_list(packets);
307 * store_key - Takes a key and stores it.
308 * @publickey: A pointer to the public key to store.
309 * @intrans: If we're already in a transaction.
310 * @update: If true the key exists and should be updated.
312 static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
315 static char buffer[PATH_MAX];
316 static char wbuffer[PATH_MAX];
318 struct openpgp_packet_list *packets = NULL;
319 struct openpgp_packet_list *list_end = NULL;
320 struct openpgp_publickey *next = NULL;
321 uint64_t keyid = get_keyid(publickey);
322 struct ll *wordlist = NULL, *wl = NULL;
324 uint64_t *subkeyids = NULL;
332 prove_path_to(keyid, "key");
333 keypath(buffer, sizeof(buffer), keyid);
336 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
338 next = publickey->next;
339 publickey->next = NULL;
340 flatten_publickey(publickey, &packets, &list_end);
341 publickey->next = next;
343 write_openpgp_stream(file_putchar, &fd, packets);
345 free_packet_list(packets);
351 wl = wordlist = makewordlistfromkey(wordlist, publickey);
353 uint32_t hash = calchash((uint8_t *) (wl->object));
354 prove_path_to(hash, "words");
356 worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
357 mkdir(wbuffer, 0777);
358 wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
360 link(buffer, wbuffer);
364 llfree(wordlist, free);
366 subkeyids = keysubkeys(publickey);
368 while (subkeyids != NULL && subkeyids[i] != 0) {
369 prove_path_to(subkeyids[i], "subkeys");
371 subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
372 mkdir(wbuffer, 0777);
373 subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i],
375 link(buffer, wbuffer);
379 if (subkeyids != NULL) {
384 get_skshash(publickey, &hash);
385 hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
386 (hash.hash[2] << 8) + hash.hash[3];
387 prove_path_to(hashid, "skshash");
388 skshashpath(wbuffer, sizeof(wbuffer), &hash);
389 link(buffer, wbuffer);
398 * delete_key - Given a keyid delete the key from storage.
399 * @keyid: The keyid to delete.
400 * @intrans: If we're already in a transaction.
402 static int fs_delete_key(uint64_t keyid, bool intrans)
404 static char buffer[PATH_MAX];
406 struct openpgp_publickey *pk = NULL;
408 struct ll *wordlist = NULL, *wl = NULL;
409 uint64_t *subkeyids = NULL;
412 if ((keyid >> 32) == 0)
413 keyid = fs_getfullkeyid(keyid);
418 ret = fs_fetch_key(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");
430 wordpath(buffer, sizeof(buffer), wl->object, hash,
437 subkeyids = keysubkeys(pk);
439 while (subkeyids != NULL && subkeyids[i] != 0) {
440 prove_path_to(subkeyids[i], "subkeys");
442 subkeypath(buffer, sizeof(buffer), subkeyids[i],
448 if (subkeyids != NULL) {
453 get_skshash(pk, &hash);
454 skshashpath(buffer, sizeof(buffer), &hash);
458 keypath(buffer, sizeof(buffer), keyid);
466 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
468 struct ll *keys = NULL;
470 char buffer[PATH_MAX];
471 uint32_t hash = calchash((uint8_t *) (word));
474 worddir(buffer, sizeof(buffer), word, hash);
476 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
481 if (de && de->d_name[0] != '.') {
483 || (llfind(mct, de->d_name,
484 (int (*)(const void *, const void *))
487 logthing(LOGTHING_DEBUG,
488 "Found %s // %s", word,
502 * fetch_key_text - Trys to find the keys that contain the supplied text.
503 * @search: The text to search for.
504 * @publickey: A pointer to a structure to return the key in.
506 static int fs_fetch_key_text(const char *search,
507 struct openpgp_publickey **publickey)
509 struct ll *wordlist = NULL, *wl = NULL;
510 struct ll *keylist = NULL;
511 char *searchtext = NULL;
514 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
516 searchtext = strdup(search);
517 wl = wordlist = makewordlist(wordlist, searchtext);
519 keylist = internal_get_key_by_word(wordlist->object, NULL);
522 llfree(wordlist, NULL);
531 internal_get_key_by_word(wl->object, keylist);
533 llfree(wordlist, NULL);
534 llfree(keylist, free);
539 llfree(keylist, free);
544 llfree(wordlist, NULL);
546 /* Now add the keys... */
549 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
551 fs_fetch_key(strtoull(wl->object, NULL, 16), publickey,
553 if (addedkeys >= config.maxkeys)
558 llfree(keylist, free);
566 * fetch_key_skshash - Given an SKS hash fetch the key from storage.
567 * @hash: The hash to fetch.
568 * @publickey: A pointer to a structure to return the key in.
569 * @intrans: If we're already in a transaction.
571 static int fs_fetch_key_skshash(const struct skshash *hash,
572 struct openpgp_publickey **publickey)
574 static char buffer[PATH_MAX];
576 struct openpgp_packet_list *packets = NULL;
578 skshashpath(buffer, sizeof(buffer), hash);
579 if ((fd = open(buffer, O_RDONLY)) != -1) {
580 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
581 parse_keys(packets, publickey);
582 free_packet_list(packets);
592 * iterate_keys - call a function once for each key in the db.
593 * @iterfunc: The function to call.
594 * @ctx: A context pointer
596 * Calls iterfunc once for each key in the database. ctx is passed
597 * unaltered to iterfunc. This function is intended to aid database dumps
598 * and statistic calculations.
600 * Returns the number of keys we iterated over.
602 static int fs_iterate_keys(void (*iterfunc)(void *ctx,
603 struct openpgp_publickey *key), void *ctx)
609 * Include the basic keydb routines.
611 #define NEED_KEYID2UID 1
612 #define NEED_GETKEYSIGS 1
613 #define NEED_UPDATEKEYS 1
616 struct dbfuncs keydb_fs_funcs = {
618 .cleanupdb = fs_cleanupdb,
619 .starttrans = fs_starttrans,
620 .endtrans = fs_endtrans,
621 .fetch_key = fs_fetch_key,
622 .fetch_key_text = fs_fetch_key_text,
623 .fetch_key_skshash = fs_fetch_key_skshash,
624 .store_key = fs_store_key,
625 .update_keys = generic_update_keys,
626 .delete_key = fs_delete_key,
627 .getkeysigs = generic_getkeysigs,
628 .cached_getkeysigs = generic_cached_getkeysigs,
629 .keyid2uid = generic_keyid2uid,
630 .getfullkeyid = fs_getfullkeyid,
631 .iterate_keys = fs_iterate_keys,