]> the.earth.li Git - onak.git/blob - keydb_fs.c
Extend database backends to support fetching by key fingerprint
[onak.git] / keydb_fs.c
1 /*
2  * keydb_fs.c - Routines to store and fetch keys in a filesystem hierarchy.
3  *
4  * Copyright 2004 Daniel Silverstone <dsilvers@digital-scurf.org>
5  *
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.
9  *
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
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <sys/types.h>
21 #include <sys/uio.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <limits.h>
30 #include <dirent.h>
31
32 #include "charfuncs.h"
33 #include "decodekey.h"
34 #include "keydb.h"
35 #include "keyid.h"
36 #include "keystructs.h"
37 #include "ll.h"
38 #include "mem.h"
39 #include "onak-conf.h"
40 #include "parsekey.h"
41 #include "log.h"
42 #include "wordlist.h"
43
44 /* Hack: We should really dynamically allocate our path buffers */
45 #ifndef PATH_MAX
46 #define PATH_MAX 1024
47 #endif
48
49 static int keydb_lockfile_fd = -1;
50 static bool keydb_lockfile_readonly;
51
52 /*****************************************************************************/
53
54 /* Helper functions */
55
56 #define FNV_offset_basis 2166136261ul
57 #define FNV_mixing_prime 16777619ul
58
59 static uint32_t calchash(uint8_t * ptr)
60 {
61         register uint32_t h = FNV_offset_basis;
62         register uint32_t p = FNV_mixing_prime;
63         register uint32_t n = strlen((char *) ptr);
64         register uint8_t *c = ptr;
65         while (n--) {
66                 h *= p;
67                 h ^= *c++;
68         }
69         return h ? h : 1;       /* prevent a hash of zero happening */
70 }
71
72
73 static void keypath(char *buffer, size_t length, uint64_t _keyid)
74 {
75         uint64_t keyid = _keyid << 32;
76         snprintf(buffer, length, "%s/key/%02X/%02X/%08X/%016" PRIX64,
77                  config.db_dir, (uint8_t) ((keyid >> 56) & 0xFF),
78                  (uint8_t) ((keyid >> 48) & 0xFF),
79                  (uint32_t) (keyid >> 32), _keyid);
80 }
81
82 static void keydir(char *buffer, size_t length, uint64_t _keyid)
83 {
84         uint64_t keyid = _keyid << 32;
85         snprintf(buffer, length, "%s/key/%02X/%02X/%08X", config.db_dir,
86                  (uint8_t) ((keyid >> 56) & 0xFF),
87                  (uint8_t) ((keyid >> 48) & 0xFF),
88                  (uint32_t) (keyid >> 32));
89 }
90
91 static void prove_path_to(uint64_t keyid, char *what)
92 {
93         static char buffer[PATH_MAX];
94         snprintf(buffer, sizeof(buffer), "%s/%s", config.db_dir, what);
95         mkdir(buffer, 0777);
96
97         snprintf(buffer, sizeof(buffer), "%s/%s/%02X", config.db_dir, what,
98                  (uint8_t) ((keyid >> 24) & 0xFF));
99         mkdir(buffer, 0777);
100
101         snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", config.db_dir,
102                  what,
103                  (uint8_t) ((keyid >> 24) & 0xFF),
104                  (uint8_t) ((keyid >> 16) & 0xFF));
105         mkdir(buffer, 0777);
106
107         snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", config.db_dir,
108                  what,
109                  (uint8_t) ((keyid >> 24) & 0xFF),
110                  (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
111         mkdir(buffer, 0777);
112 }
113
114 static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
115                 uint64_t keyid)
116 {
117         snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
118                  config.db_dir, (uint8_t) ((hash >> 24) & 0xFF),
119                  (uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
120 }
121
122 static void worddir(char *buffer, size_t length, char *word, uint32_t hash)
123 {
124         snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", config.db_dir,
125                  (uint8_t) ((hash >> 24) & 0xFF),
126                  (uint8_t) ((hash >> 16) & 0xFF), hash, word);
127 }
128
129 static void subkeypath(char *buffer, size_t length, uint64_t subkey,
130                 uint64_t keyid)
131 {
132         snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
133                  config.db_dir,
134                  (uint8_t) ((subkey >> 24) & 0xFF),
135                  (uint8_t) ((subkey >> 16) & 0xFF),
136                  (uint32_t) (subkey & 0xFFFFFFFF),
137                  keyid);
138 }
139
140 static void skshashpath(char *buffer, size_t length,
141                 const struct skshash *hash)
142 {
143         snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
144                 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
145                  config.db_dir,
146                  hash->hash[0], hash->hash[1],
147                  hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
148                  hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
149                  hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
150                  hash->hash[12], hash->hash[13], hash->hash[14],
151                  hash->hash[15]);
152 }
153 static void subkeydir(char *buffer, size_t length, uint64_t subkey)
154 {
155         snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
156                  config.db_dir,
157                  (uint8_t) ((subkey >> 24) & 0xFF),
158                  (uint8_t) ((subkey >> 16) & 0xFF),
159                  (uint32_t) (subkey & 0xFFFFFFFF));
160 }
161
162 /*****************************************************************************/
163
164 /**
165  *      initdb - Initialize the key database.
166  */
167 static void fs_initdb(bool readonly)
168 {
169         char buffer[PATH_MAX];
170
171         keydb_lockfile_readonly = readonly;
172
173         snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
174
175         if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
176                 if (errno != ENOENT) {
177                         logthing(LOGTHING_CRITICAL,
178                                  "Unable to access keydb_fs root of '%s'. (%s)",
179                                  config.db_dir, strerror(errno));
180                         exit(1);        /* Lacking rwx on the key dir */
181                 }
182                 mkdir(config.db_dir, 0777);
183                 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
184         }
185         chdir(config.db_dir);
186         if (keydb_lockfile_fd == -1)
187                 keydb_lockfile_fd = open(buffer,
188                                          (keydb_lockfile_readonly) ?
189                                          O_RDONLY : O_RDWR);
190         if (keydb_lockfile_fd == -1)
191                 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
192         if (keydb_lockfile_fd == -1) {
193                 logthing(LOGTHING_CRITICAL,
194                          "Unable to open lockfile '%s'. (%s)",
195                          buffer, strerror(errno));
196                 exit(1);        /* Lacking rwx on the key dir */
197         }
198 }
199
200 /**
201  *      cleanupdb - De-initialize the key database.
202  */
203 static void fs_cleanupdb(void)
204 {
205         /* Mmmm nothing to do here? */
206         close(keydb_lockfile_fd);
207 }
208
209 /**
210  *      starttrans - Start a transaction.
211  */
212 static bool fs_starttrans(void)
213 {
214         struct flock lockstruct;
215         int remaining = 20;
216         lockstruct.l_type =
217             F_RDLCK | ((keydb_lockfile_readonly) ? 0 : F_WRLCK);
218         lockstruct.l_whence = SEEK_SET;
219         lockstruct.l_start = 0;
220         lockstruct.l_len = 1;
221
222         while (fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct) == -1) {
223                 if (remaining-- == 0)
224                         return false;   /* Hope to hell that noodles DTRT */
225                 usleep(100);
226         }
227         return true;
228 }
229
230 /**
231  *      endtrans - End a transaction.
232  */
233 static void fs_endtrans(void)
234 {
235         struct flock lockstruct;
236
237         lockstruct.l_type = F_UNLCK;
238         lockstruct.l_whence = SEEK_SET;
239         lockstruct.l_start = 0;
240         lockstruct.l_len = 1;
241         fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct);
242 }
243
244 static uint64_t fs_getfullkeyid(uint64_t keyid)
245 {
246         static char buffer[PATH_MAX];
247         DIR *d = NULL;
248         struct dirent *de = NULL;
249         uint64_t ret = 0;
250
251         keydir(buffer, sizeof(buffer), keyid);
252
253         d = opendir(buffer);
254         if (d) {
255                 do {
256                         de = readdir(d);
257                         if (de && de->d_name[0] != '.') {
258                                 ret = strtoull(de->d_name, NULL, 16);
259                         }
260                 } while (de && de->d_name[0] == '.');
261                 closedir(d);    
262         }
263
264         if (ret == 0) {
265                 subkeydir(buffer, sizeof(buffer), keyid);
266
267                 d = opendir(buffer);
268                 if (d) {
269                         do {
270                                 de = readdir(d);
271                                 if (de && de->d_name[0] != '.') {
272                                         ret = strtoull(de->d_name, NULL, 16);
273                                 }
274                         } while (de && de->d_name[0] == '.');
275                         closedir(d);
276                 }
277         }
278
279         return ret;
280 }
281
282 /**
283  *      fetch_key - Given a keyid fetch the key from storage.
284  *      @keyid: The keyid to fetch.
285  *      @publickey: A pointer to a structure to return the key in.
286  *      @intrans: If we're already in a transaction.
287  */
288 static int fs_fetch_key_id(uint64_t keyid,
289               struct openpgp_publickey **publickey,
290               bool intrans)
291 {
292         static char buffer[PATH_MAX];
293         int ret = 0, fd;
294         struct openpgp_packet_list *packets = NULL;
295
296         if (!intrans)
297                 fs_starttrans();
298
299         if ((keyid >> 32) == 0)
300                 keyid = fs_getfullkeyid(keyid);
301
302         keypath(buffer, sizeof(buffer), keyid);
303         if ((fd = open(buffer, O_RDONLY)) != -1) {
304                 /* File is present, load it in... */
305                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
306                 parse_keys(packets, publickey);
307                 free_packet_list(packets);
308                 packets = NULL;
309                 close(fd);
310                 ret = 1;
311         }
312
313         if (!intrans)
314                 fs_endtrans();
315         return ret;
316 }
317
318 /**
319  *      store_key - Takes a key and stores it.
320  *      @publickey: A pointer to the public key to store.
321  *      @intrans: If we're already in a transaction.
322  *      @update: If true the key exists and should be updated.
323  */
324 static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
325               bool update)
326 {
327         static char buffer[PATH_MAX];
328         static char wbuffer[PATH_MAX];
329         int ret = 0, fd;
330         struct openpgp_packet_list *packets = NULL;
331         struct openpgp_packet_list *list_end = NULL;
332         struct openpgp_publickey *next = NULL;
333         uint64_t keyid;
334         struct ll *wordlist = NULL, *wl = NULL;
335         struct skshash hash;
336         uint64_t *subkeyids = NULL;
337         uint32_t hashid;
338         int i = 0;
339
340         if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
341                 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
342                 return 0;
343         }
344
345         if (!intrans)
346                 fs_starttrans();
347
348         prove_path_to(keyid, "key");
349         keypath(buffer, sizeof(buffer), keyid);
350
351         if ((fd =
352              open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
353                   0644)) != -1) {
354                 next = publickey->next;
355                 publickey->next = NULL;
356                 flatten_publickey(publickey, &packets, &list_end);
357                 publickey->next = next;
358
359                 write_openpgp_stream(file_putchar, &fd, packets);
360                 close(fd);
361                 free_packet_list(packets);
362                 packets = NULL;
363                 ret = 1;
364         }
365
366         if (ret) {
367                 wl = wordlist = makewordlistfromkey(wordlist, publickey);
368                 while (wl) {
369                         uint32_t hash = calchash((uint8_t *) (wl->object));
370                         prove_path_to(hash, "words");
371
372                         worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
373                         mkdir(wbuffer, 0777);
374                         wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
375                                 keyid);
376                         link(buffer, wbuffer);
377
378                         wl = wl->next;
379                 }
380                 llfree(wordlist, free);
381                 
382                 subkeyids = keysubkeys(publickey);
383                 i = 0;
384                 while (subkeyids != NULL && subkeyids[i] != 0) {
385                         prove_path_to(subkeyids[i], "subkeys");
386
387                         subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
388                         mkdir(wbuffer, 0777);
389                         subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i],
390                                 keyid);
391                         link(buffer, wbuffer);
392
393                         i++;
394                 }
395                 if (subkeyids != NULL) {
396                         free(subkeyids);
397                         subkeyids = NULL;
398                 }
399
400                 get_skshash(publickey, &hash);
401                 hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
402                                 (hash.hash[2] << 8) + hash.hash[3];
403                 prove_path_to(hashid, "skshash");
404                 skshashpath(wbuffer, sizeof(wbuffer), &hash);
405                 link(buffer, wbuffer);
406         }
407
408         if (!intrans)
409                 fs_endtrans();
410         return ret;
411 }
412
413 /**
414  *      delete_key - Given a keyid delete the key from storage.
415  *      @keyid: The keyid to delete.
416  *      @intrans: If we're already in a transaction.
417  */
418 static int fs_delete_key(uint64_t keyid, bool intrans)
419 {
420         static char buffer[PATH_MAX];
421         int ret;
422         struct openpgp_publickey *pk = NULL;
423         struct skshash hash;
424         struct ll *wordlist = NULL, *wl = NULL;
425         uint64_t *subkeyids = NULL;
426         int i = 0;
427
428         if ((keyid >> 32) == 0)
429                 keyid = fs_getfullkeyid(keyid);
430
431         if (!intrans)
432                 fs_starttrans();
433
434         ret = fs_fetch_key_id(keyid, &pk, true);
435
436         if (ret) {
437                 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
438                          keyid);
439                 wl = wordlist = makewordlistfromkey(wordlist, pk);
440                 logthing(LOGTHING_DEBUG,
441                          "Wordlist for key %016" PRIX64 " done", keyid);
442                 while (wl) {
443                         uint32_t hash = calchash((uint8_t *) (wl->object));
444                         prove_path_to(hash, "words");
445
446                         wordpath(buffer, sizeof(buffer), wl->object, hash,
447                                 keyid);
448                         unlink(buffer);
449
450                         wl = wl->next;
451                 }
452
453                 subkeyids = keysubkeys(pk);
454                 i = 0;
455                 while (subkeyids != NULL && subkeyids[i] != 0) {
456                         prove_path_to(subkeyids[i], "subkeys");
457
458                         subkeypath(buffer, sizeof(buffer), subkeyids[i],
459                                 keyid);
460                         unlink(buffer);
461
462                         i++;
463                 }
464                 if (subkeyids != NULL) {
465                         free(subkeyids);
466                         subkeyids = NULL;
467                 }
468
469                 get_skshash(pk, &hash);
470                 skshashpath(buffer, sizeof(buffer), &hash);
471                 unlink(buffer);
472         }
473
474         keypath(buffer, sizeof(buffer), keyid);
475         unlink(buffer);
476
477         if (!intrans)
478                 fs_endtrans();
479         return 1;
480 }
481
482 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
483 {
484         struct ll *keys = NULL;
485         DIR *d = NULL;
486         char buffer[PATH_MAX];
487         uint32_t hash = calchash((uint8_t *) (word));
488         struct dirent *de;
489
490         worddir(buffer, sizeof(buffer), word, hash);
491         d = opendir(buffer);
492         logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
493                  buffer);
494         if (d)
495                 do {
496                         de = readdir(d);
497                         if (de && de->d_name[0] != '.') {
498                                 if ((!mct)
499                                     || (llfind(mct, de->d_name,
500                                         (int (*)(const void *, const void *))
501                                                     strcmp) !=
502                                         NULL)) {
503                                         logthing(LOGTHING_DEBUG,
504                                                  "Found %s // %s", word,
505                                                  de->d_name);
506                                         keys =
507                                             lladd(keys,
508                                                   strdup(de->d_name));
509                                 }
510                         }
511                 } while (de);
512         closedir(d);
513
514         return keys;
515 }
516
517 /*
518  *      fetch_key_text - Trys to find the keys that contain the supplied text.
519  *      @search: The text to search for.
520  *      @publickey: A pointer to a structure to return the key in.
521  */
522 static int fs_fetch_key_text(const char *search,
523                    struct openpgp_publickey **publickey)
524 {
525         struct ll *wordlist = NULL, *wl = NULL;
526         struct ll *keylist = NULL;
527         char      *searchtext = NULL;
528         int addedkeys = 0;
529
530         logthing(LOGTHING_DEBUG, "Search was '%s'", search);
531
532         searchtext = strdup(search);
533         wl = wordlist = makewordlist(wordlist, searchtext);
534
535         keylist = internal_get_key_by_word(wordlist->object, NULL);
536
537         if (!keylist) {
538                 llfree(wordlist, NULL);
539                 free(searchtext);
540                 searchtext = NULL;
541                 return 0;
542         }
543
544         wl = wl->next;
545         while (wl) {
546                 struct ll *nkl =
547                     internal_get_key_by_word(wl->object, keylist);
548                 if (!nkl) {
549                         llfree(wordlist, NULL);
550                         llfree(keylist, free);
551                         free(searchtext);
552                         searchtext = NULL;
553                         return 0;
554                 }
555                 llfree(keylist, free);
556                 keylist = nkl;
557                 wl = wl->next;
558         }
559
560         llfree(wordlist, NULL);
561
562         /* Now add the keys... */
563         wl = keylist;
564         while (wl) {
565                 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
566                 addedkeys +=
567                     fs_fetch_key_id(strtoull(wl->object, NULL, 16), publickey,
568                               false);
569                 if (addedkeys >= config.maxkeys)
570                         break;
571                 wl = wl->next;
572         }
573
574         llfree(keylist, free);
575         free(searchtext);
576         searchtext = NULL;
577
578         return addedkeys;
579 }
580
581 /**
582  *      fetch_key_skshash - Given an SKS hash fetch the key from storage.
583  *      @hash: The hash to fetch.
584  *      @publickey: A pointer to a structure to return the key in.
585  *      @intrans: If we're already in a transaction.
586  */
587 static int fs_fetch_key_skshash(const struct skshash *hash,
588               struct openpgp_publickey **publickey)
589 {
590         static char buffer[PATH_MAX];
591         int ret = 0, fd;
592         struct openpgp_packet_list *packets = NULL;
593
594         skshashpath(buffer, sizeof(buffer), hash);
595         if ((fd = open(buffer, O_RDONLY)) != -1) {
596                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
597                 parse_keys(packets, publickey);
598                 free_packet_list(packets);
599                 packets = NULL;
600                 close(fd);
601                 ret = 1;
602         }
603
604         return ret;
605 }
606
607 /**
608  *      iterate_keys - call a function once for each key in the db.
609  *      @iterfunc: The function to call.
610  *      @ctx: A context pointer
611  *
612  *      Calls iterfunc once for each key in the database. ctx is passed
613  *      unaltered to iterfunc. This function is intended to aid database dumps
614  *      and statistic calculations.
615  *
616  *      Returns the number of keys we iterated over.
617  */
618 static int fs_iterate_keys(void (*iterfunc)(void *ctx,
619                 struct openpgp_publickey *key), void *ctx)
620 {
621         return 0;
622 }
623
624 /*
625  * Include the basic keydb routines.
626  */
627 #define NEED_KEYID2UID 1
628 #define NEED_GETKEYSIGS 1
629 #define NEED_UPDATEKEYS 1
630 #define NEED_GET_FP 1
631 #include "keydb.c"
632
633 struct dbfuncs keydb_fs_funcs = {
634         .initdb                 = fs_initdb,
635         .cleanupdb              = fs_cleanupdb,
636         .starttrans             = fs_starttrans,
637         .endtrans               = fs_endtrans,
638         .fetch_key_id           = fs_fetch_key_id,
639         .fetch_key_fp           = generic_fetch_key_fp,
640         .fetch_key_text         = fs_fetch_key_text,
641         .fetch_key_skshash      = fs_fetch_key_skshash,
642         .store_key              = fs_store_key,
643         .update_keys            = generic_update_keys,
644         .delete_key             = fs_delete_key,
645         .getkeysigs             = generic_getkeysigs,
646         .cached_getkeysigs      = generic_cached_getkeysigs,
647         .keyid2uid              = generic_keyid2uid,
648         .getfullkeyid           = fs_getfullkeyid,
649         .iterate_keys           = fs_iterate_keys,
650 };