Add Debian init.d file 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 /* Hack: We should really dynamically allocate our path buffers */
34 #ifndef PATH_MAX
35 #define PATH_MAX 1024
36 #endif
37
38 static int keydb_lockfile_fd = -1;
39 static bool keydb_lockfile_readonly;
40
41 /*****************************************************************************/
42
43 /* Helper functions */
44
45 #define FNV_offset_basis 2166136261ul
46 #define FNV_mixing_prime 16777619ul
47
48 static uint32_t calchash(uint8_t * ptr)
49 {
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;
54         while (n--) {
55                 h *= p;
56                 h ^= *c++;
57         }
58         return h ? h : 1;       /* prevent a hash of zero happening */
59 }
60
61
62 static void keypath(char *buffer, size_t length, uint64_t _keyid)
63 {
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);
69 }
70
71 static void keydir(char *buffer, size_t length, uint64_t _keyid)
72 {
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));
78 }
79
80 static void prove_path_to(uint64_t keyid, char *what)
81 {
82         static char buffer[PATH_MAX];
83         snprintf(buffer, sizeof(buffer), "%s/%s", config.db_dir, what);
84         mkdir(buffer, 0777);
85
86         snprintf(buffer, sizeof(buffer), "%s/%s/%02X", config.db_dir, what,
87                  (uint8_t) ((keyid >> 24) & 0xFF));
88         mkdir(buffer, 0777);
89
90         snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", config.db_dir,
91                  what,
92                  (uint8_t) ((keyid >> 24) & 0xFF),
93                  (uint8_t) ((keyid >> 16) & 0xFF));
94         mkdir(buffer, 0777);
95
96         snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", config.db_dir,
97                  what,
98                  (uint8_t) ((keyid >> 24) & 0xFF),
99                  (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
100         mkdir(buffer, 0777);
101 }
102
103 static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
104                 uint64_t keyid)
105 {
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);
109 }
110
111 static void worddir(char *buffer, size_t length, char *word, uint32_t hash)
112 {
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);
116 }
117
118 static void subkeypath(char *buffer, size_t length, uint64_t subkey,
119                 uint64_t keyid)
120 {
121         snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
122                  config.db_dir,
123                  (uint8_t) ((subkey >> 24) & 0xFF),
124                  (uint8_t) ((subkey >> 16) & 0xFF),
125                  (uint32_t) (subkey & 0xFFFFFFFF),
126                  keyid);
127 }
128
129 static void subkeydir(char *buffer, size_t length, uint64_t subkey)
130 {
131         snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
132                  config.db_dir,
133                  (uint8_t) ((subkey >> 24) & 0xFF),
134                  (uint8_t) ((subkey >> 16) & 0xFF),
135                  (uint32_t) (subkey & 0xFFFFFFFF));
136 }
137
138 /*****************************************************************************/
139
140 /**
141  *      initdb - Initialize the key database.
142  */
143 static void fs_initdb(bool readonly)
144 {
145         char buffer[PATH_MAX];
146
147         keydb_lockfile_readonly = readonly;
148
149         snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
150
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 */
157                 }
158                 mkdir(config.db_dir, 0777);
159                 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
160         }
161         chdir(config.db_dir);
162         if (keydb_lockfile_fd == -1)
163                 keydb_lockfile_fd = open(buffer,
164                                          (keydb_lockfile_readonly) ?
165                                          O_RDONLY : O_RDWR);
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 */
173         }
174 }
175
176 /**
177  *      cleanupdb - De-initialize the key database.
178  */
179 static void fs_cleanupdb(void)
180 {
181         /* Mmmm nothing to do here? */
182         close(keydb_lockfile_fd);
183 }
184
185 /**
186  *      starttrans - Start a transaction.
187  */
188 static bool fs_starttrans(void)
189 {
190         struct flock lockstruct;
191         int remaining = 20;
192         lockstruct.l_type =
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;
197
198         while (fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct) == -1) {
199                 if (remaining-- == 0)
200                         return false;   /* Hope to hell that noodles DTRT */
201                 usleep(100);
202         }
203         return true;
204 }
205
206 /**
207  *      endtrans - End a transaction.
208  */
209 static void fs_endtrans(void)
210 {
211         struct flock lockstruct;
212
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);
218 }
219
220 static uint64_t fs_getfullkeyid(uint64_t keyid)
221 {
222         static char buffer[PATH_MAX];
223         DIR *d = NULL;
224         struct dirent *de = NULL;
225         uint64_t ret = 0;
226
227         keydir(buffer, sizeof(buffer), keyid);
228
229         d = opendir(buffer);
230         if (d) {
231                 do {
232                         de = readdir(d);
233                         if (de && de->d_name[0] != '.') {
234                                 ret = strtoull(de->d_name, NULL, 16);
235                         }
236                 } while (de && de->d_name[0] == '.');
237                 closedir(d);    
238         }
239
240         if (ret == 0) {
241                 subkeydir(buffer, sizeof(buffer), keyid);
242
243                 d = opendir(buffer);
244                 if (d) {
245                         do {
246                                 de = readdir(d);
247                                 if (de && de->d_name[0] != '.') {
248                                         ret = strtoull(de->d_name, NULL, 16);
249                                 }
250                         } while (de && de->d_name[0] == '.');
251                         closedir(d);
252                 }
253         }
254
255         return ret;
256 }
257
258 /**
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.
263  */
264 static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
265               bool intrans)
266 {
267         static char buffer[PATH_MAX];
268         int ret = 0, fd;
269         struct openpgp_packet_list *packets = NULL;
270
271         if (!intrans)
272                 fs_starttrans();
273
274         if ((keyid >> 32) == 0)
275                 keyid = fs_getfullkeyid(keyid);
276
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);
283                 packets = NULL;
284                 close(fd);
285                 ret = 1;
286         }
287
288         if (!intrans)
289                 fs_endtrans();
290         return ret;
291 }
292
293 /**
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.
298  */
299 static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
300               bool update)
301 {
302         static char buffer[PATH_MAX];
303         static char wbuffer[PATH_MAX];
304         int ret = 0, fd;
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;
311         int i = 0;
312
313
314         if (!intrans)
315                 fs_starttrans();
316
317         prove_path_to(keyid, "key");
318         keypath(buffer, sizeof(buffer), keyid);
319
320         if ((fd =
321              open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
322                   0644)) != -1) {
323                 next = publickey->next;
324                 publickey->next = NULL;
325                 flatten_publickey(publickey, &packets, &list_end);
326                 publickey->next = next;
327
328                 write_openpgp_stream(file_putchar, &fd, packets);
329                 close(fd);
330                 free_packet_list(packets);
331                 packets = NULL;
332                 ret = 1;
333         }
334
335         if (ret) {
336                 wl = wordlist = makewordlistfromkey(wordlist, publickey);
337                 while (wl) {
338                         uint32_t hash = calchash((uint8_t *) (wl->object));
339                         prove_path_to(hash, "words");
340
341                         worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
342                         mkdir(wbuffer, 0777);
343                         wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
344                                 keyid);
345                         link(buffer, wbuffer);
346
347                         wl = wl->next;
348                 }
349                 llfree(wordlist, free);
350                 
351                 subkeyids = keysubkeys(publickey);
352                 i = 0;
353                 while (subkeyids != NULL && subkeyids[i] != 0) {
354                         prove_path_to(subkeyids[i], "subkeys");
355
356                         subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
357                         mkdir(wbuffer, 0777);
358                         subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i],
359                                 keyid);
360                         link(buffer, wbuffer);
361
362                         i++;
363                 }
364                 if (subkeyids != NULL) {
365                         free(subkeyids);
366                         subkeyids = NULL;
367                 }
368         }
369
370         if (!intrans)
371                 fs_endtrans();
372         return ret;
373 }
374
375 /**
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.
379  */
380 static int fs_delete_key(uint64_t keyid, bool intrans)
381 {
382         static char buffer[PATH_MAX];
383         int ret;
384         struct openpgp_publickey *pk = NULL;
385         struct ll *wordlist = NULL, *wl = NULL;
386         uint64_t *subkeyids = NULL;
387         int i = 0;
388
389         if ((keyid >> 32) == 0)
390                 keyid = fs_getfullkeyid(keyid);
391
392         if (!intrans)
393                 fs_starttrans();
394
395         ret = fs_fetch_key(keyid, &pk, true);
396
397         if (ret) {
398                 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
399                          keyid);
400                 wl = wordlist = makewordlistfromkey(wordlist, pk);
401                 logthing(LOGTHING_DEBUG,
402                          "Wordlist for key %016" PRIX64 " done", keyid);
403                 while (wl) {
404                         uint32_t hash = calchash((uint8_t *) (wl->object));
405                         prove_path_to(hash, "words");
406
407                         wordpath(buffer, sizeof(buffer), wl->object, hash,
408                                 keyid);
409                         unlink(buffer);
410
411                         wl = wl->next;
412                 }
413
414                 subkeyids = keysubkeys(pk);
415                 i = 0;
416                 while (subkeyids != NULL && subkeyids[i] != 0) {
417                         prove_path_to(subkeyids[i], "subkeys");
418
419                         subkeypath(buffer, sizeof(buffer), subkeyids[i],
420                                 keyid);
421                         unlink(buffer);
422
423                         i++;
424                 }
425                 if (subkeyids != NULL) {
426                         free(subkeyids);
427                         subkeyids = NULL;
428                 }
429
430         }
431
432         keypath(buffer, sizeof(buffer), keyid);
433         unlink(buffer);
434
435         if (!intrans)
436                 fs_endtrans();
437         return 1;
438 }
439
440 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
441 {
442         struct ll *keys = NULL;
443         DIR *d = NULL;
444         char buffer[PATH_MAX];
445         uint32_t hash = calchash((uint8_t *) (word));
446         struct dirent *de;
447
448         worddir(buffer, sizeof(buffer), word, hash);
449         d = opendir(buffer);
450         logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
451                  buffer);
452         if (d)
453                 do {
454                         de = readdir(d);
455                         if (de && de->d_name[0] != '.') {
456                                 if ((!mct)
457                                     || (llfind(mct, de->d_name,
458                                         (int (*)(const void *, const void *))
459                                                     strcmp) !=
460                                         NULL)) {
461                                         logthing(LOGTHING_DEBUG,
462                                                  "Found %s // %s", word,
463                                                  de->d_name);
464                                         keys =
465                                             lladd(keys,
466                                                   strdup(de->d_name));
467                                 }
468                         }
469                 } while (de);
470         closedir(d);
471
472         return keys;
473 }
474
475 /*
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.
479  */
480 static int fs_fetch_key_text(const char *search,
481                    struct openpgp_publickey **publickey)
482 {
483         struct ll *wordlist = NULL, *wl = NULL;
484         struct ll *keylist = NULL;
485         char      *searchtext = NULL;
486         int addedkeys = 0;
487
488         logthing(LOGTHING_DEBUG, "Search was '%s'", search);
489
490         searchtext = strdup(search);
491         wl = wordlist = makewordlist(wordlist, searchtext);
492
493         keylist = internal_get_key_by_word(wordlist->object, NULL);
494
495         if (!keylist) {
496                 llfree(wordlist, NULL);
497                 free(searchtext);
498                 searchtext = NULL;
499                 return 0;
500         }
501
502         wl = wl->next;
503         while (wl) {
504                 struct ll *nkl =
505                     internal_get_key_by_word(wl->object, keylist);
506                 if (!nkl) {
507                         llfree(wordlist, NULL);
508                         llfree(keylist, free);
509                         free(searchtext);
510                         searchtext = NULL;
511                         return 0;
512                 }
513                 llfree(keylist, free);
514                 keylist = nkl;
515                 wl = wl->next;
516         }
517
518         llfree(wordlist, NULL);
519
520         /* Now add the keys... */
521         wl = keylist;
522         while (wl) {
523                 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
524                 addedkeys +=
525                     fs_fetch_key(strtoull(wl->object, NULL, 16), publickey,
526                               false);
527                 if (addedkeys >= config.maxkeys)
528                         break;
529                 wl = wl->next;
530         }
531
532         llfree(keylist, free);
533         free(searchtext);
534         searchtext = NULL;
535
536         return addedkeys;
537 }
538
539 /**
540  *      iterate_keys - call a function once for each key in the db.
541  *      @iterfunc: The function to call.
542  *      @ctx: A context pointer
543  *
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.
547  *
548  *      Returns the number of keys we iterated over.
549  */
550 static int fs_iterate_keys(void (*iterfunc)(void *ctx,
551                 struct openpgp_publickey *key), void *ctx)
552 {
553         return 0;
554 }
555
556 /*
557  * Include the basic keydb routines.
558  */
559 #define NEED_KEYID2UID 1
560 #define NEED_GETKEYSIGS 1
561 #define NEED_UPDATEKEYS 1
562 #include "keydb.c"
563
564 struct dbfuncs keydb_fs_funcs = {
565         .initdb                 = fs_initdb,
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,
579 };