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 subkeydir(char *buffer, size_t length, uint64_t subkey)
131 snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
133 (uint8_t) ((subkey >> 24) & 0xFF),
134 (uint8_t) ((subkey >> 16) & 0xFF),
135 (uint32_t) (subkey & 0xFFFFFFFF));
138 /*****************************************************************************/
141 * initdb - Initialize the key database.
143 static void fs_initdb(bool readonly)
145 char buffer[PATH_MAX];
147 keydb_lockfile_readonly = readonly;
149 snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
151 if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
152 if (errno != ENOENT) {
153 logthing(LOGTHING_CRITICAL,
154 "Unable to access keydb_fs root of '%s'. (%s)",
155 config.db_dir, strerror(errno));
156 exit(1); /* Lacking rwx on the key dir */
158 mkdir(config.db_dir, 0777);
159 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
161 chdir(config.db_dir);
162 if (keydb_lockfile_fd == -1)
163 keydb_lockfile_fd = open(buffer,
164 (keydb_lockfile_readonly) ?
166 if (keydb_lockfile_fd == -1)
167 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
168 if (keydb_lockfile_fd == -1) {
169 logthing(LOGTHING_CRITICAL,
170 "Unable to open lockfile '%s'. (%s)",
171 buffer, strerror(errno));
172 exit(1); /* Lacking rwx on the key dir */
177 * cleanupdb - De-initialize the key database.
179 static void fs_cleanupdb(void)
181 /* Mmmm nothing to do here? */
182 close(keydb_lockfile_fd);
186 * starttrans - Start a transaction.
188 static bool fs_starttrans(void)
190 struct flock lockstruct;
193 F_RDLCK | ((keydb_lockfile_readonly) ? 0 : F_WRLCK);
194 lockstruct.l_whence = SEEK_SET;
195 lockstruct.l_start = 0;
196 lockstruct.l_len = 1;
198 while (fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct) == -1) {
199 if (remaining-- == 0)
200 return false; /* Hope to hell that noodles DTRT */
207 * endtrans - End a transaction.
209 static void fs_endtrans(void)
211 struct flock lockstruct;
213 lockstruct.l_type = F_UNLCK;
214 lockstruct.l_whence = SEEK_SET;
215 lockstruct.l_start = 0;
216 lockstruct.l_len = 1;
217 fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct);
220 static uint64_t fs_getfullkeyid(uint64_t keyid)
222 static char buffer[PATH_MAX];
224 struct dirent *de = NULL;
227 keydir(buffer, sizeof(buffer), keyid);
233 if (de && de->d_name[0] != '.') {
234 ret = strtoull(de->d_name, NULL, 16);
236 } while (de && de->d_name[0] == '.');
241 subkeydir(buffer, sizeof(buffer), keyid);
247 if (de && de->d_name[0] != '.') {
248 ret = strtoull(de->d_name, NULL, 16);
250 } while (de && de->d_name[0] == '.');
259 * fetch_key - Given a keyid fetch the key from storage.
260 * @keyid: The keyid to fetch.
261 * @publickey: A pointer to a structure to return the key in.
262 * @intrans: If we're already in a transaction.
264 static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
267 static char buffer[PATH_MAX];
269 struct openpgp_packet_list *packets = NULL;
274 if ((keyid >> 32) == 0)
275 keyid = fs_getfullkeyid(keyid);
277 keypath(buffer, sizeof(buffer), keyid);
278 if ((fd = open(buffer, O_RDONLY)) != -1) {
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 openpgp_publickey *publickey, bool intrans,
302 static char buffer[PATH_MAX];
303 static char wbuffer[PATH_MAX];
305 struct openpgp_packet_list *packets = NULL;
306 struct openpgp_packet_list *list_end = NULL;
307 struct openpgp_publickey *next = NULL;
308 uint64_t keyid = get_keyid(publickey);
309 struct ll *wordlist = NULL, *wl = NULL;
310 uint64_t *subkeyids = NULL;
317 prove_path_to(keyid, "key");
318 keypath(buffer, sizeof(buffer), keyid);
321 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
323 next = publickey->next;
324 publickey->next = NULL;
325 flatten_publickey(publickey, &packets, &list_end);
326 publickey->next = next;
328 write_openpgp_stream(file_putchar, &fd, packets);
330 free_packet_list(packets);
336 wl = wordlist = makewordlistfromkey(wordlist, publickey);
338 uint32_t hash = calchash((uint8_t *) (wl->object));
339 prove_path_to(hash, "words");
341 worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
342 mkdir(wbuffer, 0777);
343 wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
345 link(buffer, wbuffer);
349 llfree(wordlist, free);
351 subkeyids = keysubkeys(publickey);
353 while (subkeyids != NULL && subkeyids[i] != 0) {
354 prove_path_to(subkeyids[i], "subkeys");
356 subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
357 mkdir(wbuffer, 0777);
358 subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i],
360 link(buffer, wbuffer);
364 if (subkeyids != NULL) {
376 * delete_key - Given a keyid delete the key from storage.
377 * @keyid: The keyid to delete.
378 * @intrans: If we're already in a transaction.
380 static int fs_delete_key(uint64_t keyid, bool intrans)
382 static char buffer[PATH_MAX];
384 struct openpgp_publickey *pk = NULL;
385 struct ll *wordlist = NULL, *wl = NULL;
386 uint64_t *subkeyids = NULL;
389 if ((keyid >> 32) == 0)
390 keyid = fs_getfullkeyid(keyid);
395 ret = fs_fetch_key(keyid, &pk, true);
398 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
400 wl = wordlist = makewordlistfromkey(wordlist, pk);
401 logthing(LOGTHING_DEBUG,
402 "Wordlist for key %016" PRIX64 " done", keyid);
404 uint32_t hash = calchash((uint8_t *) (wl->object));
405 prove_path_to(hash, "words");
407 wordpath(buffer, sizeof(buffer), wl->object, hash,
414 subkeyids = keysubkeys(pk);
416 while (subkeyids != NULL && subkeyids[i] != 0) {
417 prove_path_to(subkeyids[i], "subkeys");
419 subkeypath(buffer, sizeof(buffer), subkeyids[i],
425 if (subkeyids != NULL) {
432 keypath(buffer, sizeof(buffer), keyid);
440 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
442 struct ll *keys = NULL;
444 char buffer[PATH_MAX];
445 uint32_t hash = calchash((uint8_t *) (word));
448 worddir(buffer, sizeof(buffer), word, hash);
450 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
455 if (de && de->d_name[0] != '.') {
457 || (llfind(mct, de->d_name,
458 (int (*)(const void *, const void *))
461 logthing(LOGTHING_DEBUG,
462 "Found %s // %s", word,
476 * fetch_key_text - Trys to find the keys that contain the supplied text.
477 * @search: The text to search for.
478 * @publickey: A pointer to a structure to return the key in.
480 static int fs_fetch_key_text(const char *search,
481 struct openpgp_publickey **publickey)
483 struct ll *wordlist = NULL, *wl = NULL;
484 struct ll *keylist = NULL;
485 char *searchtext = NULL;
488 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
490 searchtext = strdup(search);
491 wl = wordlist = makewordlist(wordlist, searchtext);
493 keylist = internal_get_key_by_word(wordlist->object, NULL);
496 llfree(wordlist, NULL);
505 internal_get_key_by_word(wl->object, keylist);
507 llfree(wordlist, NULL);
508 llfree(keylist, free);
513 llfree(keylist, free);
518 llfree(wordlist, NULL);
520 /* Now add the keys... */
523 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
525 fs_fetch_key(strtoull(wl->object, NULL, 16), publickey,
527 if (addedkeys >= config.maxkeys)
532 llfree(keylist, free);
540 * iterate_keys - call a function once for each key in the db.
541 * @iterfunc: The function to call.
542 * @ctx: A context pointer
544 * Calls iterfunc once for each key in the database. ctx is passed
545 * unaltered to iterfunc. This function is intended to aid database dumps
546 * and statistic calculations.
548 * Returns the number of keys we iterated over.
550 static int fs_iterate_keys(void (*iterfunc)(void *ctx,
551 struct openpgp_publickey *key), void *ctx)
557 * Include the basic keydb routines.
559 #define NEED_KEYID2UID 1
560 #define NEED_GETKEYSIGS 1
561 #define NEED_UPDATEKEYS 1
564 struct dbfuncs keydb_fs_funcs = {
566 .cleanupdb = fs_cleanupdb,
567 .starttrans = fs_starttrans,
568 .endtrans = fs_endtrans,
569 .fetch_key = fs_fetch_key,
570 .fetch_key_text = fs_fetch_key_text,
571 .store_key = fs_store_key,
572 .update_keys = generic_update_keys,
573 .delete_key = fs_delete_key,
574 .getkeysigs = generic_getkeysigs,
575 .cached_getkeysigs = generic_cached_getkeysigs,
576 .keyid2uid = generic_keyid2uid,
577 .getfullkeyid = fs_getfullkeyid,
578 .iterate_keys = fs_iterate_keys,