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);
212 * fetch_key - Given a keyid fetch the key from storage.
213 * @keyid: The keyid to fetch.
214 * @publickey: A pointer to a structure to return the key in.
215 * @intrans: If we're already in a transaction.
217 static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
220 static char buffer[PATH_MAX];
222 struct openpgp_packet_list *packets = NULL;
227 if ((keyid >> 32) == 0)
228 keyid = getfullkeyid(keyid);
230 keypath(buffer, keyid);
231 if ((fd = open(buffer, O_RDONLY)) != -1) {
232 /* File is present, load it in... */
233 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
234 parse_keys(packets, publickey);
235 free_packet_list(packets);
247 * store_key - Takes a key and stores it.
248 * @publickey: A pointer to the public key to store.
249 * @intrans: If we're already in a transaction.
250 * @update: If true the key exists and should be updated.
252 static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
255 static char buffer[PATH_MAX];
256 static char wbuffer[PATH_MAX];
258 struct openpgp_packet_list *packets = NULL;
259 struct openpgp_packet_list *list_end = NULL;
260 struct openpgp_publickey *next = NULL;
261 uint64_t keyid = get_keyid(publickey);
262 struct ll *wordlist = NULL, *wl = NULL;
263 uint64_t *subkeyids = NULL;
270 prove_path_to(keyid, "key");
271 keypath(buffer, keyid);
274 open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
276 next = publickey->next;
277 publickey->next = NULL;
278 flatten_publickey(publickey, &packets, &list_end);
279 publickey->next = next;
281 write_openpgp_stream(file_putchar, &fd, packets);
283 free_packet_list(packets);
289 wl = wordlist = makewordlistfromkey(wordlist, publickey);
291 uint32_t hash = calchash((uint8_t *) (wl->object));
292 prove_path_to(hash, "words");
294 worddir(wbuffer, wl->object, hash);
295 mkdir(wbuffer, 0777);
296 wordpath(wbuffer, wl->object, hash, keyid);
297 link(buffer, wbuffer);
301 llfree(wordlist, free);
303 subkeyids = keysubkeys(publickey);
305 while (subkeyids != NULL && subkeyids[i] != 0) {
306 prove_path_to(subkeyids[i], "subkeys");
308 subkeydir(wbuffer, subkeyids[i]);
309 mkdir(wbuffer, 0777);
310 subkeypath(wbuffer, subkeyids[i], keyid);
311 link(buffer, wbuffer);
315 if (subkeyids != NULL) {
327 * delete_key - Given a keyid delete the key from storage.
328 * @keyid: The keyid to delete.
329 * @intrans: If we're already in a transaction.
331 static int fs_delete_key(uint64_t keyid, bool intrans)
333 static char buffer[PATH_MAX];
335 struct openpgp_publickey *pk = NULL;
336 struct ll *wordlist = NULL, *wl = NULL;
337 uint64_t *subkeyids = NULL;
340 if ((keyid >> 32) == 0)
341 keyid = getfullkeyid(keyid);
346 ret = fetch_key(keyid, &pk, true);
349 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
351 wl = wordlist = makewordlistfromkey(wordlist, pk);
352 logthing(LOGTHING_DEBUG,
353 "Wordlist for key %016" PRIX64 " done", keyid);
355 uint32_t hash = calchash((uint8_t *) (wl->object));
356 prove_path_to(hash, "words");
358 wordpath(buffer, wl->object, hash, keyid);
364 subkeyids = keysubkeys(pk);
366 while (subkeyids != NULL && subkeyids[i] != 0) {
367 prove_path_to(subkeyids[i], "subkeys");
369 subkeypath(buffer, subkeyids[i], keyid);
374 if (subkeyids != NULL) {
381 keypath(buffer, keyid);
389 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
391 struct ll *keys = NULL;
393 char buffer[PATH_MAX];
394 uint32_t hash = calchash((uint8_t *) (word));
397 worddir(buffer, word, hash);
399 logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
404 if (de && de->d_name[0] != '.') {
406 || (llfind(mct, de->d_name,
407 (int (*)(const void *, const void *))
410 logthing(LOGTHING_DEBUG,
411 "Found %s // %s", word,
425 * fetch_key_text - Trys to find the keys that contain the supplied text.
426 * @search: The text to search for.
427 * @publickey: A pointer to a structure to return the key in.
429 static int fs_fetch_key_text(const char *search,
430 struct openpgp_publickey **publickey)
432 struct ll *wordlist = NULL, *wl = NULL;
433 struct ll *keylist = NULL;
434 char *searchtext = NULL;
437 logthing(LOGTHING_DEBUG, "Search was '%s'", search);
439 searchtext = strdup(search);
440 wl = wordlist = makewordlist(wordlist, searchtext);
442 keylist = internal_get_key_by_word(wordlist->object, NULL);
445 llfree(wordlist, NULL);
454 internal_get_key_by_word(wl->object, keylist);
456 llfree(wordlist, NULL);
457 llfree(keylist, free);
462 llfree(keylist, free);
467 llfree(wordlist, NULL);
469 /* Now add the keys... */
472 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
474 fetch_key(strtoull(wl->object, NULL, 16), publickey,
476 if (addedkeys >= config.maxkeys)
481 llfree(keylist, free);
488 static uint64_t fs_getfullkeyid(uint64_t keyid)
490 static char buffer[PATH_MAX];
492 struct dirent *de = NULL;
495 keydir(buffer, keyid);
501 if (de && de->d_name[0] != '.') {
502 ret = strtoull(de->d_name, NULL, 16);
504 } while (de && de->d_name[0] == '.');
509 subkeydir(buffer, keyid);
515 if (de && de->d_name[0] != '.') {
516 ret = strtoull(de->d_name, NULL, 16);
518 } while (de && de->d_name[0] == '.');
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,