Add help text for keyd
[onak.git] / keydb_fs.c
1 /*
2  * keydb.h - Routines to store and fetch keys.
3  *
4  * Daniel Silverstone <dsilvers@digital-scurf.org>
5  *
6  * Copyright 2004 Daniel Silverstone and Project Purple
7  */
8
9 #include <sys/types.h>
10 #include <sys/uio.h>
11 #include <sys/stat.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <limits.h>
19 #include <dirent.h>
20
21 #include "charfuncs.h"
22 #include "decodekey.h"
23 #include "keydb.h"
24 #include "keyid.h"
25 #include "keystructs.h"
26 #include "ll.h"
27 #include "mem.h"
28 #include "onak-conf.h"
29 #include "parsekey.h"
30 #include "log.h"
31 #include "wordlist.h"
32
33 static int keydb_lockfile_fd = -1;
34 static bool keydb_lockfile_readonly;
35
36 /*****************************************************************************/
37
38 /* Helper functions */
39
40 #define FNV_offset_basis 2166136261ul
41 #define FNV_mixing_prime 16777619ul
42
43 static uint32_t calchash(uint8_t * ptr)
44 {
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;
49         while (n--) {
50                 h *= p;
51                 h ^= *c++;
52         }
53         return h ? h : 1;       /* prevent a hash of zero happening */
54 }
55
56
57 static void keypath(char *buffer, uint64_t _keyid)
58 {
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);
64 }
65
66 static void keydir(char *buffer, uint64_t _keyid)
67 {
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));
73 }
74
75 static void prove_path_to(uint64_t keyid, char *what)
76 {
77         static char buffer[1024];
78         snprintf(buffer, PATH_MAX, "%s/%s", config.db_dir, what);
79         mkdir(buffer, 0777);
80
81         snprintf(buffer, PATH_MAX, "%s/%s/%02X", config.db_dir, what,
82                  (uint8_t) ((keyid >> 24) & 0xFF));
83         mkdir(buffer, 0777);
84
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));
88         mkdir(buffer, 0777);
89
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));
93         mkdir(buffer, 0777);
94 }
95
96 static void wordpath(char *buffer, char *word, uint32_t hash, uint64_t keyid)
97 {
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);
101 }
102
103 static void worddir(char *buffer, char *word, uint32_t hash)
104 {
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);
108 }
109
110 static void subkeypath(char *buffer, uint64_t subkey, uint64_t keyid)
111 {
112         snprintf(buffer, PATH_MAX, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
113                  config.db_dir,
114                  (uint8_t) ((subkey >> 24) & 0xFF),
115                  (uint8_t) ((subkey >> 16) & 0xFF),
116                  (uint32_t) (subkey & 0xFFFFFFFF),
117                  keyid);
118 }
119
120 static void subkeydir(char *buffer, uint64_t subkey)
121 {
122         snprintf(buffer, PATH_MAX, "%s/subkeys/%02X/%02X/%08X",
123                  config.db_dir,
124                  (uint8_t) ((subkey >> 24) & 0xFF),
125                  (uint8_t) ((subkey >> 16) & 0xFF),
126                  (uint32_t) (subkey & 0xFFFFFFFF));
127 }
128
129 /*****************************************************************************/
130
131 /**
132  *      initdb - Initialize the key database.
133  */
134 static void fs_initdb(bool readonly)
135 {
136         char buffer[PATH_MAX];
137
138         keydb_lockfile_readonly = readonly;
139
140         snprintf(buffer, PATH_MAX, "%s/.lock", config.db_dir);
141
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 */
148                 }
149                 mkdir(config.db_dir, 0777);
150                 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
151         }
152         chdir(config.db_dir);
153         if (keydb_lockfile_fd == -1)
154                 keydb_lockfile_fd = open(buffer,
155                                          (keydb_lockfile_readonly) ?
156                                          O_RDONLY : O_RDWR);
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 */
164         }
165 }
166
167 /**
168  *      cleanupdb - De-initialize the key database.
169  */
170 static void fs_cleanupdb(void)
171 {
172         /* Mmmm nothing to do here? */
173         close(keydb_lockfile_fd);
174 }
175
176 /**
177  *      starttrans - Start a transaction.
178  */
179 static bool fs_starttrans(void)
180 {
181         struct flock lockstruct;
182         int remaining = 20;
183         lockstruct.l_type =
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;
188
189         while (fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct) == -1) {
190                 if (remaining-- == 0)
191                         return false;   /* Hope to hell that noodles DTRT */
192                 usleep(100);
193         }
194         return true;
195 }
196
197 /**
198  *      endtrans - End a transaction.
199  */
200 static void fs_endtrans(void)
201 {
202         struct flock lockstruct;
203
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);
209 }
210
211 static uint64_t fs_getfullkeyid(uint64_t keyid)
212 {
213         static char buffer[PATH_MAX];
214         DIR *d = NULL;
215         struct dirent *de = NULL;
216         uint64_t ret = 0;
217
218         keydir(buffer, keyid);
219
220         d = opendir(buffer);
221         if (d) {
222                 do {
223                         de = readdir(d);
224                         if (de && de->d_name[0] != '.') {
225                                 ret = strtoull(de->d_name, NULL, 16);
226                         }
227                 } while (de && de->d_name[0] == '.');
228                 closedir(d);    
229         }
230
231         if (ret == 0) {
232                 subkeydir(buffer, keyid);
233
234                 d = opendir(buffer);
235                 if (d) {
236                         do {
237                                 de = readdir(d);
238                                 if (de && de->d_name[0] != '.') {
239                                         ret = strtoull(de->d_name, NULL, 16);
240                                 }
241                         } while (de && de->d_name[0] == '.');
242                         closedir(d);
243                 }
244         }
245
246         return ret;
247 }
248
249 /**
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.
254  */
255 static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
256               bool intrans)
257 {
258         static char buffer[PATH_MAX];
259         int ret = 0, fd;
260         struct openpgp_packet_list *packets = NULL;
261
262         if (!intrans)
263                 fs_starttrans();
264
265         if ((keyid >> 32) == 0)
266                 keyid = fs_getfullkeyid(keyid);
267
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);
274                 packets = NULL;
275                 close(fd);
276                 ret = 1;
277         }
278
279         if (!intrans)
280                 fs_endtrans();
281         return ret;
282 }
283
284 /**
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.
289  */
290 static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
291               bool update)
292 {
293         static char buffer[PATH_MAX];
294         static char wbuffer[PATH_MAX];
295         int ret = 0, fd;
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;
302         int i = 0;
303
304
305         if (!intrans)
306                 fs_starttrans();
307
308         prove_path_to(keyid, "key");
309         keypath(buffer, keyid);
310
311         if ((fd =
312              open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
313                   0644)) != -1) {
314                 next = publickey->next;
315                 publickey->next = NULL;
316                 flatten_publickey(publickey, &packets, &list_end);
317                 publickey->next = next;
318
319                 write_openpgp_stream(file_putchar, &fd, packets);
320                 close(fd);
321                 free_packet_list(packets);
322                 packets = NULL;
323                 ret = 1;
324         }
325
326         if (ret) {
327                 wl = wordlist = makewordlistfromkey(wordlist, publickey);
328                 while (wl) {
329                         uint32_t hash = calchash((uint8_t *) (wl->object));
330                         prove_path_to(hash, "words");
331
332                         worddir(wbuffer, wl->object, hash);
333                         mkdir(wbuffer, 0777);
334                         wordpath(wbuffer, wl->object, hash, keyid);
335                         link(buffer, wbuffer);
336
337                         wl = wl->next;
338                 }
339                 llfree(wordlist, free);
340                 
341                 subkeyids = keysubkeys(publickey);
342                 i = 0;
343                 while (subkeyids != NULL && subkeyids[i] != 0) {
344                         prove_path_to(subkeyids[i], "subkeys");
345
346                         subkeydir(wbuffer, subkeyids[i]);
347                         mkdir(wbuffer, 0777);
348                         subkeypath(wbuffer, subkeyids[i], keyid);
349                         link(buffer, wbuffer);
350
351                         i++;
352                 }
353                 if (subkeyids != NULL) {
354                         free(subkeyids);
355                         subkeyids = NULL;
356                 }
357         }
358
359         if (!intrans)
360                 fs_endtrans();
361         return ret;
362 }
363
364 /**
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.
368  */
369 static int fs_delete_key(uint64_t keyid, bool intrans)
370 {
371         static char buffer[PATH_MAX];
372         int ret;
373         struct openpgp_publickey *pk = NULL;
374         struct ll *wordlist = NULL, *wl = NULL;
375         uint64_t *subkeyids = NULL;
376         int i = 0;
377
378         if ((keyid >> 32) == 0)
379                 keyid = fs_getfullkeyid(keyid);
380
381         if (!intrans)
382                 fs_starttrans();
383
384         ret = fs_fetch_key(keyid, &pk, true);
385
386         if (ret) {
387                 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
388                          keyid);
389                 wl = wordlist = makewordlistfromkey(wordlist, pk);
390                 logthing(LOGTHING_DEBUG,
391                          "Wordlist for key %016" PRIX64 " done", keyid);
392                 while (wl) {
393                         uint32_t hash = calchash((uint8_t *) (wl->object));
394                         prove_path_to(hash, "words");
395
396                         wordpath(buffer, wl->object, hash, keyid);
397                         unlink(buffer);
398
399                         wl = wl->next;
400                 }
401
402                 subkeyids = keysubkeys(pk);
403                 i = 0;
404                 while (subkeyids != NULL && subkeyids[i] != 0) {
405                         prove_path_to(subkeyids[i], "subkeys");
406
407                         subkeypath(buffer, subkeyids[i], keyid);
408                         unlink(buffer);
409
410                         i++;
411                 }
412                 if (subkeyids != NULL) {
413                         free(subkeyids);
414                         subkeyids = NULL;
415                 }
416
417         }
418
419         keypath(buffer, keyid);
420         unlink(buffer);
421
422         if (!intrans)
423                 fs_endtrans();
424         return 1;
425 }
426
427 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
428 {
429         struct ll *keys = NULL;
430         DIR *d = NULL;
431         char buffer[PATH_MAX];
432         uint32_t hash = calchash((uint8_t *) (word));
433         struct dirent *de;
434
435         worddir(buffer, word, hash);
436         d = opendir(buffer);
437         logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
438                  buffer);
439         if (d)
440                 do {
441                         de = readdir(d);
442                         if (de && de->d_name[0] != '.') {
443                                 if ((!mct)
444                                     || (llfind(mct, de->d_name,
445                                         (int (*)(const void *, const void *))
446                                                     strcmp) !=
447                                         NULL)) {
448                                         logthing(LOGTHING_DEBUG,
449                                                  "Found %s // %s", word,
450                                                  de->d_name);
451                                         keys =
452                                             lladd(keys,
453                                                   strdup(de->d_name));
454                                 }
455                         }
456                 } while (de);
457         closedir(d);
458
459         return keys;
460 }
461
462 /*
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.
466  */
467 static int fs_fetch_key_text(const char *search,
468                    struct openpgp_publickey **publickey)
469 {
470         struct ll *wordlist = NULL, *wl = NULL;
471         struct ll *keylist = NULL;
472         char      *searchtext = NULL;
473         int addedkeys = 0;
474
475         logthing(LOGTHING_DEBUG, "Search was '%s'", search);
476
477         searchtext = strdup(search);
478         wl = wordlist = makewordlist(wordlist, searchtext);
479
480         keylist = internal_get_key_by_word(wordlist->object, NULL);
481
482         if (!keylist) {
483                 llfree(wordlist, NULL);
484                 free(searchtext);
485                 searchtext = NULL;
486                 return 0;
487         }
488
489         wl = wl->next;
490         while (wl) {
491                 struct ll *nkl =
492                     internal_get_key_by_word(wl->object, keylist);
493                 if (!nkl) {
494                         llfree(wordlist, NULL);
495                         llfree(keylist, free);
496                         free(searchtext);
497                         searchtext = NULL;
498                         return 0;
499                 }
500                 llfree(keylist, free);
501                 keylist = nkl;
502                 wl = wl->next;
503         }
504
505         llfree(wordlist, NULL);
506
507         /* Now add the keys... */
508         wl = keylist;
509         while (wl) {
510                 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
511                 addedkeys +=
512                     fs_fetch_key(strtoull(wl->object, NULL, 16), publickey,
513                               false);
514                 if (addedkeys >= config.maxkeys)
515                         break;
516                 wl = wl->next;
517         }
518
519         llfree(keylist, free);
520         free(searchtext);
521         searchtext = NULL;
522
523         return addedkeys;
524 }
525
526 /**
527  *      iterate_keys - call a function once for each key in the db.
528  *      @iterfunc: The function to call.
529  *      @ctx: A context pointer
530  *
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.
534  *
535  *      Returns the number of keys we iterated over.
536  */
537 static int fs_iterate_keys(void (*iterfunc)(void *ctx,
538                 struct openpgp_publickey *key), void *ctx)
539 {
540         return 0;
541 }
542
543 /*
544  * Include the basic keydb routines.
545  */
546 #define NEED_KEYID2UID 1
547 #define NEED_GETKEYSIGS 1
548 #define NEED_UPDATEKEYS 1
549 #include "keydb.c"
550
551 struct dbfuncs keydb_fs_funcs = {
552         .initdb                 = fs_initdb,
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,
566 };