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