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