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