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 static int keydb_lockfile_fd = -1;
34 static bool keydb_lockfile_readonly;
36 /*****************************************************************************/
38 /* Helper functions */
40 #define FNV_offset_basis 2166136261ul
41 #define FNV_mixing_prime 16777619ul
43 static uint32_t calchash(uint8_t * ptr)
45 register uint32_t h = FNV_offset_basis;
46 register uint32_t p = FNV_mixing_prime;
47 register uint32_t n = strlen((char *) ptr);
48 register uint8_t *c = ptr;
53 return h ? h : 1; /* prevent a hash of zero happening */
57 static void keypath(char *buffer, uint64_t _keyid)
59 uint64_t keyid = _keyid << 32;
60 snprintf(buffer, PATH_MAX, "%s/key/%02X/%02X/%08X/%016" PRIX64,
61 config.db_dir, (uint8_t) ((keyid >> 56) & 0xFF),
62 (uint8_t) ((keyid >> 48) & 0xFF),
63 (uint32_t) (keyid >> 32), _keyid);
66 static void keydir(char *buffer, uint64_t _keyid)
68 uint64_t keyid = _keyid << 32;
69 snprintf(buffer, PATH_MAX, "%s/key/%02X/%02X/%08X", config.db_dir,
70 (uint8_t) ((keyid >> 56) & 0xFF),
71 (uint8_t) ((keyid >> 48) & 0xFF),
72 (uint32_t) (keyid >> 32));
75 static void prove_path_to(uint64_t keyid, char *what)
77 static char buffer[1024];
78 snprintf(buffer, PATH_MAX, "%s/%s", config.db_dir, what);
81 snprintf(buffer, PATH_MAX, "%s/%s/%02X", config.db_dir, what,
82 (uint8_t) ((keyid >> 24) & 0xFF));
85 snprintf(buffer, PATH_MAX, "%s/%s/%02X/%02X", config.db_dir, what,
86 (uint8_t) ((keyid >> 24) & 0xFF),
87 (uint8_t) ((keyid >> 16) & 0xFF));
90 snprintf(buffer, PATH_MAX, "%s/%s/%02X/%02X/%08X", config.db_dir, what,
91 (uint8_t) ((keyid >> 24) & 0xFF),
92 (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
96 static void wordpath(char *buffer, char *word, uint32_t hash, uint64_t keyid)
98 snprintf(buffer, PATH_MAX, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
99 config.db_dir, (uint8_t) ((hash >> 24) & 0xFF),
100 (uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
103 static void worddir(char *buffer, char *word, uint32_t hash)
105 snprintf(buffer, PATH_MAX, "%s/words/%02X/%02X/%08X/%s", config.db_dir,
106 (uint8_t) ((hash >> 24) & 0xFF),
107 (uint8_t) ((hash >> 16) & 0xFF), hash, word);
110 static void subkeypath(char *buffer, uint64_t subkey, uint64_t keyid)
112 snprintf(buffer, PATH_MAX, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
114 (uint8_t) ((subkey >> 24) & 0xFF),
115 (uint8_t) ((subkey >> 16) & 0xFF),
116 (uint32_t) (subkey & 0xFFFFFFFF),
120 static void subkeydir(char *buffer, uint64_t subkey)
122 snprintf(buffer, PATH_MAX, "%s/subkeys/%02X/%02X/%08X",
124 (uint8_t) ((subkey >> 24) & 0xFF),
125 (uint8_t) ((subkey >> 16) & 0xFF),
126 (uint32_t) (subkey & 0xFFFFFFFF));
129 /*****************************************************************************/
132 * initdb - Initialize the key database.
134 static void fs_initdb(bool readonly)
136 char buffer[PATH_MAX];
138 keydb_lockfile_readonly = readonly;
140 snprintf(buffer, PATH_MAX, "%s/.lock", config.db_dir);
142 if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
143 if (errno != ENOENT) {
144 logthing(LOGTHING_CRITICAL,
145 "Unable to access keydb_fs root of '%s'. (%s)",
146 config.db_dir, strerror(errno));
147 exit(1); /* Lacking rwx on the key dir */
149 mkdir(config.db_dir, 0777);
150 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
152 chdir(config.db_dir);
153 if (keydb_lockfile_fd == -1)
154 keydb_lockfile_fd = open(buffer,
155 (keydb_lockfile_readonly) ?
157 if (keydb_lockfile_fd == -1)
158 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
159 if (keydb_lockfile_fd == -1) {
160 logthing(LOGTHING_CRITICAL,
161 "Unable to open lockfile '%s'. (%s)",
162 buffer, strerror(errno));
163 exit(1); /* Lacking rwx on the key dir */
168 * cleanupdb - De-initialize the key database.
170 static void fs_cleanupdb(void)
172 /* Mmmm nothing to do here? */
173 close(keydb_lockfile_fd);
177 * starttrans - Start a transaction.
179 static bool fs_starttrans(void)
181 struct flock lockstruct;
184 F_RDLCK | ((keydb_lockfile_readonly) ? 0 : F_WRLCK);
185 lockstruct.l_whence = SEEK_SET;
186 lockstruct.l_start = 0;
187 lockstruct.l_len = 1;
189 while (fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct) == -1) {
190 if (remaining-- == 0)
191 return false; /* Hope to hell that noodles DTRT */
198 * endtrans - End a transaction.
200 static void fs_endtrans(void)
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(keydb_lockfile_fd, F_SETLK, &lockstruct);
211 static uint64_t fs_getfullkeyid(uint64_t keyid)
213 static char buffer[PATH_MAX];
215 struct dirent *de = NULL;
218 keydir(buffer, keyid);
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, keyid);
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(uint64_t keyid, struct openpgp_publickey **publickey,
258 static char buffer[PATH_MAX];
260 struct openpgp_packet_list *packets = NULL;
265 if ((keyid >> 32) == 0)
266 keyid = fs_getfullkeyid(keyid);
268 keypath(buffer, keyid);
269 if ((fd = open(buffer, O_RDONLY)) != -1) {
270 /* File is present, load it in... */
271 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
272 parse_keys(packets, publickey);
273 free_packet_list(packets);
285 * store_key - Takes a key and stores it.
286 * @publickey: A pointer to the public key to store.
287 * @intrans: If we're already in a transaction.
288 * @update: If true the key exists and should be updated.
290 static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
293 static char buffer[PATH_MAX];
294 static char wbuffer[PATH_MAX];
296 struct openpgp_packet_list *packets = NULL;
297 struct openpgp_packet_list *list_end = NULL;
298 struct openpgp_publickey *next = NULL;
299 uint64_t keyid = get_keyid(publickey);
300 struct ll *wordlist = NULL, *wl = NULL;
301 uint64_t *subkeyids = NULL;
308 prove_path_to(keyid, "key");
309 keypath(buffer, keyid);
312 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
314 next = publickey->next;
315 publickey->next = NULL;
316 flatten_publickey(publickey, &packets, &list_end);
317 publickey->next = next;
319 write_openpgp_stream(file_putchar, &fd, packets);
321 free_packet_list(packets);
327 wl = wordlist = makewordlistfromkey(wordlist, publickey);
329 uint32_t hash = calchash((uint8_t *) (wl->object));
330 prove_path_to(hash, "words");
332 worddir(wbuffer, wl->object, hash);
333 mkdir(wbuffer, 0777);
334 wordpath(wbuffer, wl->object, hash, keyid);
335 link(buffer, wbuffer);
339 llfree(wordlist, free);
341 subkeyids = keysubkeys(publickey);
343 while (subkeyids != NULL && subkeyids[i] != 0) {
344 prove_path_to(subkeyids[i], "subkeys");
346 subkeydir(wbuffer, subkeyids[i]);
347 mkdir(wbuffer, 0777);
348 subkeypath(wbuffer, subkeyids[i], keyid);
349 link(buffer, wbuffer);
353 if (subkeyids != NULL) {
365 * delete_key - Given a keyid delete the key from storage.
366 * @keyid: The keyid to delete.
367 * @intrans: If we're already in a transaction.
369 static int fs_delete_key(uint64_t keyid, bool intrans)
371 static char buffer[PATH_MAX];
373 struct openpgp_publickey *pk = NULL;
374 struct ll *wordlist = NULL, *wl = NULL;
375 uint64_t *subkeyids = NULL;
378 if ((keyid >> 32) == 0)
379 keyid = fs_getfullkeyid(keyid);
384 ret = fs_fetch_key(keyid, &pk, true);
387 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
389 wl = wordlist = makewordlistfromkey(wordlist, pk);
390 logthing(LOGTHING_DEBUG,
391 "Wordlist for key %016" PRIX64 " done", keyid);
393 uint32_t hash = calchash((uint8_t *) (wl->object));
394 prove_path_to(hash, "words");
396 wordpath(buffer, wl->object, hash, keyid);
402 subkeyids = keysubkeys(pk);
404 while (subkeyids != NULL && subkeyids[i] != 0) {
405 prove_path_to(subkeyids[i], "subkeys");
407 subkeypath(buffer, subkeyids[i], keyid);
412 if (subkeyids != NULL) {
419 keypath(buffer, keyid);
427 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
429 struct ll *keys = NULL;
431 char buffer[PATH_MAX];
432 uint32_t hash = calchash((uint8_t *) (word));
435 worddir(buffer, word, hash);
437 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
442 if (de && de->d_name[0] != '.') {
444 || (llfind(mct, de->d_name,
445 (int (*)(const void *, const void *))
448 logthing(LOGTHING_DEBUG,
449 "Found %s // %s", word,
463 * fetch_key_text - Trys to find the keys that contain the supplied text.
464 * @search: The text to search for.
465 * @publickey: A pointer to a structure to return the key in.
467 static int fs_fetch_key_text(const char *search,
468 struct openpgp_publickey **publickey)
470 struct ll *wordlist = NULL, *wl = NULL;
471 struct ll *keylist = NULL;
472 char *searchtext = NULL;
475 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
477 searchtext = strdup(search);
478 wl = wordlist = makewordlist(wordlist, searchtext);
480 keylist = internal_get_key_by_word(wordlist->object, NULL);
483 llfree(wordlist, NULL);
492 internal_get_key_by_word(wl->object, keylist);
494 llfree(wordlist, NULL);
495 llfree(keylist, free);
500 llfree(keylist, free);
505 llfree(wordlist, NULL);
507 /* Now add the keys... */
510 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
512 fs_fetch_key(strtoull(wl->object, NULL, 16), publickey,
514 if (addedkeys >= config.maxkeys)
519 llfree(keylist, free);
527 * iterate_keys - call a function once for each key in the db.
528 * @iterfunc: The function to call.
529 * @ctx: A context pointer
531 * Calls iterfunc once for each key in the database. ctx is passed
532 * unaltered to iterfunc. This function is intended to aid database dumps
533 * and statistic calculations.
535 * Returns the number of keys we iterated over.
537 static int fs_iterate_keys(void (*iterfunc)(void *ctx,
538 struct openpgp_publickey *key), void *ctx)
544 * Include the basic keydb routines.
546 #define NEED_KEYID2UID 1
547 #define NEED_GETKEYSIGS 1
548 #define NEED_UPDATEKEYS 1
551 struct dbfuncs keydb_fs_funcs = {
553 .cleanupdb = fs_cleanupdb,
554 .starttrans = fs_starttrans,
555 .endtrans = fs_endtrans,
556 .fetch_key = fs_fetch_key,
557 .fetch_key_text = fs_fetch_key_text,
558 .store_key = fs_store_key,
559 .update_keys = generic_update_keys,
560 .delete_key = fs_delete_key,
561 .getkeysigs = generic_getkeysigs,
562 .cached_getkeysigs = generic_cached_getkeysigs,
563 .keyid2uid = generic_keyid2uid,
564 .getfullkeyid = fs_getfullkeyid,
565 .iterate_keys = fs_iterate_keys,