]> the.earth.li Git - onak.git/blob - keydb_fs.c
Fix up memory leaks in fs keydb backend deletion
[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                 llfree(wordlist, free);
439                 wordlist = NULL;
440
441                 subkeyids = keysubkeys(pk);
442                 i = 0;
443                 while (subkeyids != NULL && subkeyids[i].length != 0) {
444                         subkeyid = fingerprint2keyid(&subkeyids[i]);
445                         prove_path_to(subkeyid, "subkeys",
446                                 dbctx->config->location);
447
448                         subkeypath(buffer, sizeof(buffer), subkeyid,
449                                 dbctx->config->location);
450                         unlink(buffer);
451
452                         i++;
453                 }
454                 if (subkeyids != NULL) {
455                         free(subkeyids);
456                         subkeyids = NULL;
457                 }
458
459                 get_skshash(pk, &hash);
460                 skshashpath(buffer, sizeof(buffer), &hash,
461                         dbctx->config->location);
462                 unlink(buffer);
463         }
464
465         keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
466         unlink(buffer);
467
468         free_publickey(pk);
469
470         if (!intrans)
471                 fs_endtrans(dbctx);
472         return 1;
473 }
474
475 static struct ll *internal_get_key_by_word(char *word, struct ll *mct,
476                 char *basepath)
477 {
478         struct ll *keys = NULL;
479         DIR *d = NULL;
480         char buffer[PATH_MAX];
481         uint32_t hash = calchash((uint8_t *) (word));
482         struct dirent *de;
483
484         worddir(buffer, sizeof(buffer), word, hash, basepath);
485         d = opendir(buffer);
486         logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
487                  buffer);
488         if (d) {
489                 do {
490                         de = readdir(d);
491                         if (de && de->d_name[0] != '.') {
492                                 if ((!mct)
493                                     || (llfind(mct, de->d_name,
494                                         (int (*)(const void *, const void *))
495                                                     strcmp) !=
496                                         NULL)) {
497                                         logthing(LOGTHING_DEBUG,
498                                                  "Found %s // %s", word,
499                                                  de->d_name);
500                                         keys =
501                                             lladd(keys,
502                                                   strdup(de->d_name));
503                                 }
504                         }
505                 } while (de);
506                 closedir(d);
507         }
508
509         return keys;
510 }
511
512 /*
513  *      fetch_key_text - Trys to find the keys that contain the supplied text.
514  *      @search: The text to search for.
515  *      @publickey: A pointer to a structure to return the key in.
516  */
517 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
518                    const char *search,
519                    struct openpgp_publickey **publickey)
520 {
521         struct ll *wordlist = NULL, *wl = NULL;
522         struct ll *keylist = NULL;
523         char      *searchtext = NULL;
524         int addedkeys = 0;
525
526         logthing(LOGTHING_DEBUG, "Search was '%s'", search);
527
528         searchtext = strdup(search);
529         wl = wordlist = makewordlist(wordlist, searchtext);
530
531         keylist = internal_get_key_by_word(wordlist->object, NULL,
532                 dbctx->config->location);
533
534         if (!keylist) {
535                 llfree(wordlist, NULL);
536                 free(searchtext);
537                 searchtext = NULL;
538                 return 0;
539         }
540
541         wl = wl->next;
542         while (wl) {
543                 struct ll *nkl =
544                     internal_get_key_by_word(wl->object, keylist,
545                         dbctx->config->location);
546                 if (!nkl) {
547                         llfree(wordlist, NULL);
548                         llfree(keylist, free);
549                         free(searchtext);
550                         searchtext = NULL;
551                         return 0;
552                 }
553                 llfree(keylist, free);
554                 keylist = nkl;
555                 wl = wl->next;
556         }
557
558         llfree(wordlist, NULL);
559
560         /* Now add the keys... */
561         wl = keylist;
562         while (wl) {
563                 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
564                 addedkeys +=
565                     fs_fetch_key_id(dbctx,
566                               strtoull(wl->object, NULL, 16), publickey,
567                               false);
568                 if (addedkeys >= config.maxkeys)
569                         break;
570                 wl = wl->next;
571         }
572
573         llfree(keylist, free);
574         free(searchtext);
575         searchtext = NULL;
576
577         return addedkeys;
578 }
579
580 /**
581  *      fetch_key_skshash - Given an SKS hash fetch the key from storage.
582  *      @hash: The hash to fetch.
583  *      @publickey: A pointer to a structure to return the key in.
584  *      @intrans: If we're already in a transaction.
585  */
586 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
587               const struct skshash *hash,
588               struct openpgp_publickey **publickey)
589 {
590         static char buffer[PATH_MAX];
591         int ret = 0, fd;
592         struct openpgp_packet_list *packets = NULL;
593
594         skshashpath(buffer, sizeof(buffer), hash, dbctx->config->location);
595         if ((fd = open(buffer, O_RDONLY)) != -1) {
596                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
597                 parse_keys(packets, publickey);
598                 free_packet_list(packets);
599                 packets = NULL;
600                 close(fd);
601                 ret = 1;
602         }
603
604         return ret;
605 }
606
607 /**
608  *      iterate_keys - call a function once for each key in the db.
609  *      @iterfunc: The function to call.
610  *      @ctx: A context pointer
611  *
612  *      Calls iterfunc once for each key in the database. ctx is passed
613  *      unaltered to iterfunc. This function is intended to aid database dumps
614  *      and statistic calculations.
615  *
616  *      Returns the number of keys we iterated over.
617  */
618 static int fs_iterate_keys(struct onak_dbctx *dbctx,
619                 void (*iterfunc)(void *ctx,
620                 struct openpgp_publickey *key), void *ctx)
621 {
622         return 0;
623 }
624
625 /*
626  * Include the basic keydb routines.
627  */
628 #define NEED_KEYID2UID 1
629 #define NEED_GETKEYSIGS 1
630 #define NEED_UPDATEKEYS 1
631 #define NEED_GET_FP 1
632 #include "keydb.c"
633
634 /**
635  *      cleanupdb - De-initialize the key database.
636  */
637 static void fs_cleanupdb(struct onak_dbctx *dbctx)
638 {
639         struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
640
641         /* Mmmm nothing to do here? */
642         close(privctx->lockfile_fd);
643
644         free(privctx);
645         dbctx->priv = NULL;
646         free(dbctx);
647 }
648
649 /**
650  *      initdb - Initialize the key database.
651  */
652 struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
653 {
654         char buffer[PATH_MAX];
655         struct onak_dbctx *dbctx;
656         struct onak_fs_dbctx *privctx;
657
658         dbctx = malloc(sizeof(struct onak_dbctx));
659         if (dbctx == NULL) {
660                 return NULL;
661         }
662         dbctx->config = dbcfg;
663         dbctx->priv = privctx = malloc(sizeof(*privctx));
664         if (privctx == NULL) {
665                 free(dbctx);
666                 return NULL;
667         }
668
669         privctx->lockfile_readonly = readonly;
670
671         snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
672
673         if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
674                 if (errno != ENOENT) {
675                         logthing(LOGTHING_CRITICAL,
676                                  "Unable to access keydb_fs root of '%s'. (%s)",
677                                  dbcfg->location, strerror(errno));
678                         exit(1);        /* Lacking rwx on the key dir */
679                 }
680                 mkdir(dbcfg->location, 0777);
681                 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
682         }
683         if (chdir(dbcfg->location) == -1) {
684                 /* Shouldn't happen after the above */
685                 logthing(LOGTHING_CRITICAL,
686                         "Couldn't change to database directory: %s",
687                         strerror(errno));
688                 free(dbctx->priv);
689                 free(dbctx);
690                 return NULL;
691         }
692         privctx->lockfile_fd = open(buffer,
693                                  (privctx->lockfile_readonly) ?
694                                  O_RDONLY : O_RDWR);
695         if (privctx->lockfile_fd == -1)
696                 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
697         if (privctx->lockfile_fd == -1) {
698                 logthing(LOGTHING_CRITICAL,
699                          "Unable to open lockfile '%s'. (%s)",
700                          buffer, strerror(errno));
701                 exit(1);        /* Lacking rwx on the key dir */
702         }
703
704         dbctx->cleanupdb                = fs_cleanupdb;
705         dbctx->starttrans               = fs_starttrans;
706         dbctx->endtrans                 = fs_endtrans;
707         dbctx->fetch_key_id             = fs_fetch_key_id;
708         dbctx->fetch_key_fp             = generic_fetch_key_fp;
709         dbctx->fetch_key_text           = fs_fetch_key_text;
710         dbctx->fetch_key_skshash        = fs_fetch_key_skshash;
711         dbctx->store_key                = fs_store_key;
712         dbctx->update_keys              = generic_update_keys;
713         dbctx->delete_key               = fs_delete_key;
714         dbctx->getkeysigs               = generic_getkeysigs;
715         dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
716         dbctx->keyid2uid                = generic_keyid2uid;
717         dbctx->iterate_keys             = fs_iterate_keys;
718
719         return dbctx;
720 }