]> the.earth.li Git - onak.git/blob - keydb_fs.c
Fix issues found by llvm scan-build static analysis
[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                 uint64_t keyid)
133 {
134         snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
135                  config.db_dir,
136                  (uint8_t) ((subkey >> 24) & 0xFF),
137                  (uint8_t) ((subkey >> 16) & 0xFF),
138                  (uint32_t) (subkey & 0xFFFFFFFF),
139                  keyid);
140 }
141
142 static void skshashpath(char *buffer, size_t length,
143                 const struct skshash *hash)
144 {
145         snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
146                 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
147                  config.db_dir,
148                  hash->hash[0], hash->hash[1],
149                  hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
150                  hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
151                  hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
152                  hash->hash[12], hash->hash[13], hash->hash[14],
153                  hash->hash[15]);
154 }
155 static void subkeydir(char *buffer, size_t length, uint64_t subkey)
156 {
157         snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
158                  config.db_dir,
159                  (uint8_t) ((subkey >> 24) & 0xFF),
160                  (uint8_t) ((subkey >> 16) & 0xFF),
161                  (uint32_t) (subkey & 0xFFFFFFFF));
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         if ((fd = open(buffer, O_RDONLY)) != -1) {
264                 /* File is present, load it in... */
265                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
266                 parse_keys(packets, publickey);
267                 free_packet_list(packets);
268                 packets = NULL;
269                 close(fd);
270                 ret = 1;
271         }
272
273         if (!intrans)
274                 fs_endtrans(dbctx);
275         return ret;
276 }
277
278 /**
279  *      store_key - Takes a key and stores it.
280  *      @publickey: A pointer to the public key to store.
281  *      @intrans: If we're already in a transaction.
282  *      @update: If true the key exists and should be updated.
283  */
284 static int fs_store_key(struct onak_dbctx *dbctx,
285               struct openpgp_publickey *publickey, bool intrans,
286               bool update)
287 {
288         static char buffer[PATH_MAX];
289         static char wbuffer[PATH_MAX];
290         int ret = 0, fd;
291         struct openpgp_packet_list *packets = NULL;
292         struct openpgp_packet_list *list_end = NULL;
293         struct openpgp_publickey *next = NULL;
294         uint64_t keyid;
295         struct ll *wordlist = NULL, *wl = NULL;
296         struct skshash hash;
297         uint64_t *subkeyids = NULL;
298         uint32_t hashid;
299         int i = 0;
300
301         if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
302                 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
303                 return 0;
304         }
305
306         if (!intrans)
307                 fs_starttrans(dbctx);
308
309         prove_path_to(keyid, "key");
310         keypath(buffer, sizeof(buffer), keyid);
311
312         if ((fd =
313              open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
314                   0644)) != -1) {
315                 next = publickey->next;
316                 publickey->next = NULL;
317                 flatten_publickey(publickey, &packets, &list_end);
318                 publickey->next = next;
319
320                 write_openpgp_stream(file_putchar, &fd, packets);
321                 close(fd);
322                 free_packet_list(packets);
323                 packets = NULL;
324                 ret = 1;
325         }
326
327         if (ret) {
328                 wl = wordlist = makewordlistfromkey(wordlist, publickey);
329                 while (wl) {
330                         uint32_t hash = calchash((uint8_t *) (wl->object));
331                         prove_path_to(hash, "words");
332
333                         worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
334                         mkdir(wbuffer, 0777);
335                         wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
336                                 keyid);
337                         link(buffer, wbuffer);
338
339                         wl = wl->next;
340                 }
341                 llfree(wordlist, free);
342                 
343                 subkeyids = keysubkeys(publickey);
344                 i = 0;
345                 while (subkeyids != NULL && subkeyids[i] != 0) {
346                         prove_path_to(subkeyids[i], "subkeys");
347
348                         subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
349                         mkdir(wbuffer, 0777);
350                         subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i],
351                                 keyid);
352                         link(buffer, wbuffer);
353
354                         i++;
355                 }
356                 if (subkeyids != NULL) {
357                         free(subkeyids);
358                         subkeyids = NULL;
359                 }
360
361                 get_skshash(publickey, &hash);
362                 hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
363                                 (hash.hash[2] << 8) + hash.hash[3];
364                 prove_path_to(hashid, "skshash");
365                 skshashpath(wbuffer, sizeof(wbuffer), &hash);
366                 link(buffer, wbuffer);
367         }
368
369         if (!intrans)
370                 fs_endtrans(dbctx);
371         return ret;
372 }
373
374 /**
375  *      delete_key - Given a keyid delete the key from storage.
376  *      @keyid: The keyid to delete.
377  *      @intrans: If we're already in a transaction.
378  */
379 static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
380 {
381         static char buffer[PATH_MAX];
382         int ret;
383         struct openpgp_publickey *pk = NULL;
384         struct skshash hash;
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(dbctx, keyid);
391
392         if (!intrans)
393                 fs_starttrans(dbctx);
394
395         ret = fs_fetch_key_id(dbctx, 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                 get_skshash(pk, &hash);
431                 skshashpath(buffer, sizeof(buffer), &hash);
432                 unlink(buffer);
433         }
434
435         keypath(buffer, sizeof(buffer), keyid);
436         unlink(buffer);
437
438         if (!intrans)
439                 fs_endtrans(dbctx);
440         return 1;
441 }
442
443 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
444 {
445         struct ll *keys = NULL;
446         DIR *d = NULL;
447         char buffer[PATH_MAX];
448         uint32_t hash = calchash((uint8_t *) (word));
449         struct dirent *de;
450
451         worddir(buffer, sizeof(buffer), word, hash);
452         d = opendir(buffer);
453         logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
454                  buffer);
455         if (d) {
456                 do {
457                         de = readdir(d);
458                         if (de && de->d_name[0] != '.') {
459                                 if ((!mct)
460                                     || (llfind(mct, de->d_name,
461                                         (int (*)(const void *, const void *))
462                                                     strcmp) !=
463                                         NULL)) {
464                                         logthing(LOGTHING_DEBUG,
465                                                  "Found %s // %s", word,
466                                                  de->d_name);
467                                         keys =
468                                             lladd(keys,
469                                                   strdup(de->d_name));
470                                 }
471                         }
472                 } while (de);
473                 closedir(d);
474         }
475
476         return keys;
477 }
478
479 /*
480  *      fetch_key_text - Trys to find the keys that contain the supplied text.
481  *      @search: The text to search for.
482  *      @publickey: A pointer to a structure to return the key in.
483  */
484 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
485                    const char *search,
486                    struct openpgp_publickey **publickey)
487 {
488         struct ll *wordlist = NULL, *wl = NULL;
489         struct ll *keylist = NULL;
490         char      *searchtext = NULL;
491         int addedkeys = 0;
492
493         logthing(LOGTHING_DEBUG, "Search was '%s'", search);
494
495         searchtext = strdup(search);
496         wl = wordlist = makewordlist(wordlist, searchtext);
497
498         keylist = internal_get_key_by_word(wordlist->object, NULL);
499
500         if (!keylist) {
501                 llfree(wordlist, NULL);
502                 free(searchtext);
503                 searchtext = NULL;
504                 return 0;
505         }
506
507         wl = wl->next;
508         while (wl) {
509                 struct ll *nkl =
510                     internal_get_key_by_word(wl->object, keylist);
511                 if (!nkl) {
512                         llfree(wordlist, NULL);
513                         llfree(keylist, free);
514                         free(searchtext);
515                         searchtext = NULL;
516                         return 0;
517                 }
518                 llfree(keylist, free);
519                 keylist = nkl;
520                 wl = wl->next;
521         }
522
523         llfree(wordlist, NULL);
524
525         /* Now add the keys... */
526         wl = keylist;
527         while (wl) {
528                 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
529                 addedkeys +=
530                     fs_fetch_key_id(dbctx,
531                               strtoull(wl->object, NULL, 16), publickey,
532                               false);
533                 if (addedkeys >= config.maxkeys)
534                         break;
535                 wl = wl->next;
536         }
537
538         llfree(keylist, free);
539         free(searchtext);
540         searchtext = NULL;
541
542         return addedkeys;
543 }
544
545 /**
546  *      fetch_key_skshash - Given an SKS hash fetch the key from storage.
547  *      @hash: The hash to fetch.
548  *      @publickey: A pointer to a structure to return the key in.
549  *      @intrans: If we're already in a transaction.
550  */
551 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
552               const struct skshash *hash,
553               struct openpgp_publickey **publickey)
554 {
555         static char buffer[PATH_MAX];
556         int ret = 0, fd;
557         struct openpgp_packet_list *packets = NULL;
558
559         skshashpath(buffer, sizeof(buffer), hash);
560         if ((fd = open(buffer, O_RDONLY)) != -1) {
561                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
562                 parse_keys(packets, publickey);
563                 free_packet_list(packets);
564                 packets = NULL;
565                 close(fd);
566                 ret = 1;
567         }
568
569         return ret;
570 }
571
572 /**
573  *      iterate_keys - call a function once for each key in the db.
574  *      @iterfunc: The function to call.
575  *      @ctx: A context pointer
576  *
577  *      Calls iterfunc once for each key in the database. ctx is passed
578  *      unaltered to iterfunc. This function is intended to aid database dumps
579  *      and statistic calculations.
580  *
581  *      Returns the number of keys we iterated over.
582  */
583 static int fs_iterate_keys(struct onak_dbctx *dbctx,
584                 void (*iterfunc)(void *ctx,
585                 struct openpgp_publickey *key), void *ctx)
586 {
587         return 0;
588 }
589
590 /*
591  * Include the basic keydb routines.
592  */
593 #define NEED_KEYID2UID 1
594 #define NEED_GETKEYSIGS 1
595 #define NEED_UPDATEKEYS 1
596 #define NEED_GET_FP 1
597 #include "keydb.c"
598
599 /**
600  *      cleanupdb - De-initialize the key database.
601  */
602 static void fs_cleanupdb(struct onak_dbctx *dbctx)
603 {
604         struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
605
606         /* Mmmm nothing to do here? */
607         close(privctx->lockfile_fd);
608 }
609
610 /**
611  *      initdb - Initialize the key database.
612  */
613 struct onak_dbctx *keydb_fs_init(bool readonly)
614 {
615         char buffer[PATH_MAX];
616         struct onak_dbctx *dbctx;
617         struct onak_fs_dbctx *privctx;
618
619         dbctx = malloc(sizeof(struct onak_dbctx));
620         if (dbctx == NULL) {
621                 return NULL;
622         }
623         dbctx->priv = privctx = malloc(sizeof(*privctx));
624         if (privctx == NULL) {
625                 free(dbctx);
626                 return NULL;
627         }
628
629         privctx->lockfile_readonly = readonly;
630
631         snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
632
633         if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
634                 if (errno != ENOENT) {
635                         logthing(LOGTHING_CRITICAL,
636                                  "Unable to access keydb_fs root of '%s'. (%s)",
637                                  config.db_dir, strerror(errno));
638                         exit(1);        /* Lacking rwx on the key dir */
639                 }
640                 mkdir(config.db_dir, 0777);
641                 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
642         }
643         chdir(config.db_dir);
644         privctx->lockfile_fd = open(buffer,
645                                  (privctx->lockfile_readonly) ?
646                                  O_RDONLY : O_RDWR);
647         if (privctx->lockfile_fd == -1)
648                 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
649         if (privctx->lockfile_fd == -1) {
650                 logthing(LOGTHING_CRITICAL,
651                          "Unable to open lockfile '%s'. (%s)",
652                          buffer, strerror(errno));
653                 exit(1);        /* Lacking rwx on the key dir */
654         }
655
656         dbctx->cleanupdb                = fs_cleanupdb;
657         dbctx->starttrans               = fs_starttrans;
658         dbctx->endtrans                 = fs_endtrans;
659         dbctx->fetch_key_id             = fs_fetch_key_id;
660         dbctx->fetch_key_fp             = generic_fetch_key_fp;
661         dbctx->fetch_key_text           = fs_fetch_key_text;
662         dbctx->fetch_key_skshash        = fs_fetch_key_skshash;
663         dbctx->store_key                = fs_store_key;
664         dbctx->update_keys              = generic_update_keys;
665         dbctx->delete_key               = fs_delete_key;
666         dbctx->getkeysigs               = generic_getkeysigs;
667         dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
668         dbctx->keyid2uid                = generic_keyid2uid;
669         dbctx->getfullkeyid             = fs_getfullkeyid;
670         dbctx->iterate_keys             = fs_iterate_keys;
671
672         return dbctx;
673 }