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