]> the.earth.li Git - onak.git/blob - keydb/keydb_fs.c
0.6.3 release
[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 "key-store.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;
263         struct openpgp_packet_list *packets = NULL;
264         onak_status_t res;
265
266         if (!intrans)
267                 fs_starttrans(dbctx);
268
269         if ((keyid >> 32) == 0)
270                 keyid = fs_getfullkeyid(dbctx, keyid);
271
272         keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
273         res = onak_read_openpgp_file(buffer,
274                                         &packets);
275         if (res == ONAK_E_NOT_FOUND) {
276                 subkeypath(buffer, sizeof(buffer), keyid,
277                         dbctx->config->location);
278                 res = onak_read_openpgp_file(buffer,
279                                         &packets);
280         }
281
282         if (res == ONAK_E_OK) {
283                 /* File is present, load it in... */
284                 parse_keys(packets, publickey);
285                 free_packet_list(packets);
286                 packets = NULL;
287                 ret = 1;
288         }
289
290         if (!intrans)
291                 fs_endtrans(dbctx);
292         return ret;
293 }
294
295 /**
296  *      store_key - Takes a key and stores it.
297  *      @publickey: A pointer to the public key to store.
298  *      @intrans: If we're already in a transaction.
299  *      @update: If true the key exists and should be updated.
300  */
301 static int fs_store_key(struct onak_dbctx *dbctx,
302               struct openpgp_publickey *publickey, bool intrans,
303               bool update)
304 {
305         static char buffer[PATH_MAX];
306         static char wbuffer[PATH_MAX];
307         int ret = 0, fd;
308         struct openpgp_packet_list *packets = NULL;
309         struct openpgp_packet_list *list_end = NULL;
310         struct openpgp_publickey *next = NULL;
311         uint64_t keyid;
312         struct ll *wordlist = NULL, *wl = NULL;
313         struct skshash hash;
314         struct openpgp_fingerprint *subkeyids = NULL;
315         uint32_t hashid;
316         int i = 0;
317
318         if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
319                 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
320                 return 0;
321         }
322
323         if (!intrans)
324                 fs_starttrans(dbctx);
325
326         prove_path_to(keyid, "key", dbctx->config->location);
327         keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
328
329         if ((fd =
330              open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
331                   0644)) != -1) {
332                 next = publickey->next;
333                 publickey->next = NULL;
334                 flatten_publickey(publickey, &packets, &list_end);
335                 publickey->next = next;
336
337                 write_openpgp_stream(file_putchar, &fd, packets);
338                 close(fd);
339                 free_packet_list(packets);
340                 packets = NULL;
341                 ret = 1;
342         }
343
344         if (ret) {
345                 wl = wordlist = makewordlistfromkey(wordlist, publickey);
346                 while (wl) {
347                         uint32_t hash = calchash((uint8_t *) (wl->object));
348                         prove_path_to(hash, "words", dbctx->config->location);
349
350                         worddir(wbuffer, sizeof(wbuffer), wl->object, hash,
351                                 dbctx->config->location);
352                         mkdir(wbuffer, 0777);
353                         wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
354                                 keyid, dbctx->config->location);
355                         link(buffer, wbuffer);
356
357                         wl = wl->next;
358                 }
359                 llfree(wordlist, free);
360                 
361                 subkeyids = keysubkeys(publickey);
362                 i = 0;
363                 while (subkeyids != NULL && subkeyids[i].length != 0) {
364                         keyid = fingerprint2keyid(&subkeyids[i]);
365
366                         prove_path_to(keyid, "subkeys",
367                                 dbctx->config->location);
368
369                         subkeydir(wbuffer, sizeof(wbuffer), keyid,
370                                 dbctx->config->location);
371                         mkdir(wbuffer, 0777);
372                         subkeypath(wbuffer, sizeof(wbuffer), keyid,
373                                 dbctx->config->location);
374                         link(buffer, wbuffer);
375
376                         i++;
377                 }
378                 if (subkeyids != NULL) {
379                         free(subkeyids);
380                         subkeyids = NULL;
381                 }
382
383                 get_skshash(publickey, &hash);
384                 hashid = hash.hash[0];
385                 hashid <<= 8;
386                 hashid |= hash.hash[1];
387                 hashid <<= 8;
388                 hashid |= hash.hash[2];
389                 hashid <<= 8;
390                 hashid |= hash.hash[3];
391                 prove_path_to(hashid, "skshash", dbctx->config->location);
392                 skshashpath(wbuffer, sizeof(wbuffer), &hash,
393                         dbctx->config->location);
394                 link(buffer, wbuffer);
395         }
396
397         if (!intrans)
398                 fs_endtrans(dbctx);
399         return ret;
400 }
401
402 /**
403  *      delete_key - Given a keyid delete the key from storage.
404  *      @fp: The fingerprint of the key to delete.
405  *      @intrans: If we're already in a transaction.
406  */
407 static int fs_delete_key(struct onak_dbctx *dbctx,
408                 struct openpgp_fingerprint *fp, bool intrans)
409 {
410         static char buffer[PATH_MAX];
411         int ret;
412         struct openpgp_publickey *pk = NULL;
413         struct skshash hash;
414         struct ll *wordlist = NULL, *wl = NULL;
415         struct openpgp_fingerprint *subkeyids = NULL;
416         uint64_t subkeyid;
417         int i = 0;
418         uint64_t keyid;
419
420         keyid = fingerprint2keyid(fp);
421         if (keyid == 0)
422                 return 1;
423
424         if (!intrans)
425                 fs_starttrans(dbctx);
426
427         ret = fs_fetch_key_id(dbctx, keyid, &pk, true);
428
429         if (ret) {
430                 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
431                          keyid);
432                 wl = wordlist = makewordlistfromkey(wordlist, pk);
433                 logthing(LOGTHING_DEBUG,
434                          "Wordlist for key %016" PRIX64 " done", keyid);
435                 while (wl) {
436                         uint32_t hash = calchash((uint8_t *) (wl->object));
437                         prove_path_to(hash, "words", dbctx->config->location);
438
439                         wordpath(buffer, sizeof(buffer), wl->object, hash,
440                                 keyid, dbctx->config->location);
441                         unlink(buffer);
442
443                         wl = wl->next;
444                 }
445                 llfree(wordlist, free);
446                 wordlist = NULL;
447
448                 subkeyids = keysubkeys(pk);
449                 i = 0;
450                 while (subkeyids != NULL && subkeyids[i].length != 0) {
451                         subkeyid = fingerprint2keyid(&subkeyids[i]);
452                         prove_path_to(subkeyid, "subkeys",
453                                 dbctx->config->location);
454
455                         subkeypath(buffer, sizeof(buffer), subkeyid,
456                                 dbctx->config->location);
457                         unlink(buffer);
458
459                         i++;
460                 }
461                 if (subkeyids != NULL) {
462                         free(subkeyids);
463                         subkeyids = NULL;
464                 }
465
466                 get_skshash(pk, &hash);
467                 skshashpath(buffer, sizeof(buffer), &hash,
468                         dbctx->config->location);
469                 unlink(buffer);
470         }
471
472         keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
473         unlink(buffer);
474
475         free_publickey(pk);
476
477         if (!intrans)
478                 fs_endtrans(dbctx);
479         return 1;
480 }
481
482 static struct ll *internal_get_key_by_word(char *word, struct ll *mct,
483                 char *basepath)
484 {
485         struct ll *keys = NULL;
486         DIR *d = NULL;
487         char buffer[PATH_MAX];
488         uint32_t hash = calchash((uint8_t *) (word));
489         struct dirent *de;
490
491         worddir(buffer, sizeof(buffer), word, hash, basepath);
492         d = opendir(buffer);
493         logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
494                  buffer);
495         if (d) {
496                 do {
497                         de = readdir(d);
498                         if (de && de->d_name[0] != '.') {
499                                 if ((!mct)
500                                     || (llfind(mct, de->d_name,
501                                         (int (*)(const void *, const void *))
502                                                     strcmp) !=
503                                         NULL)) {
504                                         logthing(LOGTHING_DEBUG,
505                                                  "Found %s // %s", word,
506                                                  de->d_name);
507                                         keys =
508                                             lladd(keys,
509                                                   strdup(de->d_name));
510                                 }
511                         }
512                 } while (de);
513                 closedir(d);
514         }
515
516         return keys;
517 }
518
519 /*
520  *      fetch_key_text - Trys to find the keys that contain the supplied text.
521  *      @search: The text to search for.
522  *      @publickey: A pointer to a structure to return the key in.
523  */
524 static int fs_fetch_key_text(struct onak_dbctx *dbctx,
525                    const char *search,
526                    struct openpgp_publickey **publickey)
527 {
528         struct ll *wordlist = NULL, *wl = NULL;
529         struct ll *keylist = NULL;
530         char      *searchtext = NULL;
531         int addedkeys = 0;
532
533         logthing(LOGTHING_DEBUG, "Search was '%s'", search);
534
535         searchtext = strdup(search);
536         wl = wordlist = makewordlist(wordlist, searchtext);
537
538         keylist = internal_get_key_by_word(wordlist->object, NULL,
539                 dbctx->config->location);
540
541         if (!keylist) {
542                 llfree(wordlist, NULL);
543                 free(searchtext);
544                 searchtext = NULL;
545                 return 0;
546         }
547
548         wl = wl->next;
549         while (wl) {
550                 struct ll *nkl =
551                     internal_get_key_by_word(wl->object, keylist,
552                         dbctx->config->location);
553                 if (!nkl) {
554                         llfree(wordlist, NULL);
555                         llfree(keylist, free);
556                         free(searchtext);
557                         searchtext = NULL;
558                         return 0;
559                 }
560                 llfree(keylist, free);
561                 keylist = nkl;
562                 wl = wl->next;
563         }
564
565         llfree(wordlist, NULL);
566
567         /* Now add the keys... */
568         wl = keylist;
569         while (wl) {
570                 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
571                 addedkeys +=
572                     fs_fetch_key_id(dbctx,
573                               strtoull(wl->object, NULL, 16), publickey,
574                               false);
575                 if (addedkeys >= config.maxkeys)
576                         break;
577                 wl = wl->next;
578         }
579
580         llfree(keylist, free);
581         free(searchtext);
582         searchtext = NULL;
583
584         return addedkeys;
585 }
586
587 /**
588  *      fetch_key_skshash - Given an SKS hash fetch the key from storage.
589  *      @hash: The hash to fetch.
590  *      @publickey: A pointer to a structure to return the key in.
591  *      @intrans: If we're already in a transaction.
592  */
593 static int fs_fetch_key_skshash(struct onak_dbctx *dbctx,
594               const struct skshash *hash,
595               struct openpgp_publickey **publickey)
596 {
597         static char buffer[PATH_MAX];
598         int ret = 0;
599         struct openpgp_packet_list *packets = NULL;
600         onak_status_t res;
601
602         skshashpath(buffer, sizeof(buffer), hash, dbctx->config->location);
603         res = onak_read_openpgp_file(buffer, &packets);
604         if (res == ONAK_E_OK) {
605                 parse_keys(packets, publickey);
606                 free_packet_list(packets);
607                 packets = NULL;
608                 ret = 1;
609         }
610
611         return ret;
612 }
613
614 /**
615  *      iterate_keys - call a function once for each key in the db.
616  *      @iterfunc: The function to call.
617  *      @ctx: A context pointer
618  *
619  *      Calls iterfunc once for each key in the database. ctx is passed
620  *      unaltered to iterfunc. This function is intended to aid database dumps
621  *      and statistic calculations.
622  *
623  *      Returns the number of keys we iterated over.
624  */
625 static int fs_iterate_keys(__unused struct onak_dbctx *dbctx,
626                 __unused void (*iterfunc)(void *ctx,
627                         struct openpgp_publickey *key),
628                 __unused void *ctx)
629 {
630         return 0;
631 }
632
633 /*
634  * Include the basic keydb routines.
635  */
636 #define NEED_KEYID2UID 1
637 #define NEED_GETKEYSIGS 1
638 #define NEED_UPDATEKEYS 1
639 #define NEED_GET 1
640 #define NEED_GET_FP 1
641 #include "keydb.c"
642
643 /**
644  *      cleanupdb - De-initialize the key database.
645  */
646 static void fs_cleanupdb(struct onak_dbctx *dbctx)
647 {
648         struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
649
650         /* Mmmm nothing to do here? */
651         close(privctx->lockfile_fd);
652
653         free(privctx);
654         dbctx->priv = NULL;
655         free(dbctx);
656 }
657
658 /**
659  *      initdb - Initialize the key database.
660  */
661 struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
662 {
663         char buffer[PATH_MAX];
664         struct onak_dbctx *dbctx;
665         struct onak_fs_dbctx *privctx;
666
667         dbctx = malloc(sizeof(struct onak_dbctx));
668         if (dbctx == NULL) {
669                 return NULL;
670         }
671         dbctx->config = dbcfg;
672         dbctx->priv = privctx = malloc(sizeof(*privctx));
673         if (privctx == NULL) {
674                 free(dbctx);
675                 return NULL;
676         }
677
678         privctx->lockfile_readonly = readonly;
679
680         snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
681
682         if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
683                 if (errno != ENOENT) {
684                         logthing(LOGTHING_CRITICAL,
685                                  "Unable to access keydb_fs root of '%s'. (%s)",
686                                  dbcfg->location, strerror(errno));
687                         exit(1);        /* Lacking rwx on the key dir */
688                 }
689                 mkdir(dbcfg->location, 0777);
690                 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
691         }
692         if (chdir(dbcfg->location) == -1) {
693                 /* Shouldn't happen after the above */
694                 logthing(LOGTHING_CRITICAL,
695                         "Couldn't change to database directory: %s",
696                         strerror(errno));
697                 free(dbctx->priv);
698                 free(dbctx);
699                 return NULL;
700         }
701         privctx->lockfile_fd = open(buffer,
702                                  (privctx->lockfile_readonly) ?
703                                  O_RDONLY : O_RDWR);
704         if (privctx->lockfile_fd == -1)
705                 privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
706         if (privctx->lockfile_fd == -1) {
707                 logthing(LOGTHING_CRITICAL,
708                          "Unable to open lockfile '%s'. (%s)",
709                          buffer, strerror(errno));
710                 exit(1);        /* Lacking rwx on the key dir */
711         }
712
713         dbctx->cleanupdb                = fs_cleanupdb;
714         dbctx->starttrans               = fs_starttrans;
715         dbctx->endtrans                 = fs_endtrans;
716         dbctx->fetch_key                = generic_fetch_key;
717         dbctx->fetch_key_fp             = generic_fetch_key_fp;
718         dbctx->fetch_key_id             = fs_fetch_key_id;
719         dbctx->fetch_key_text           = fs_fetch_key_text;
720         dbctx->fetch_key_skshash        = fs_fetch_key_skshash;
721         dbctx->store_key                = fs_store_key;
722         dbctx->update_keys              = generic_update_keys;
723         dbctx->delete_key               = fs_delete_key;
724         dbctx->getkeysigs               = generic_getkeysigs;
725         dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
726         dbctx->keyid2uid                = generic_keyid2uid;
727         dbctx->iterate_keys             = fs_iterate_keys;
728
729         return dbctx;
730 }