]> the.earth.li Git - onak.git/blob - keydb_pg.c
Add ability to drop overly large packets
[onak.git] / keydb_pg.c
1 /*
2  * keydb_pg.c - Routines to store and fetch keys in a PostGres database.
3  *
4  * Copyright 2002-2004 Jonathan McDowell <noodles@earth.li>
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 <postgresql/libpq-fe.h>
20 #include <postgresql/libpq/libpq-fs.h>
21
22 #include <sys/types.h>
23 #include <sys/uio.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "hash.h"
32 #include "keydb.h"
33 #include "keyid.h"
34 #include "decodekey.h"
35 #include "keystructs.h"
36 #include "log.h"
37 #include "mem.h"
38 #include "onak-conf.h"
39 #include "parsekey.h"
40
41 struct pg_fc_ctx {
42         PGconn *dbconn;
43         int fd;
44 };
45
46 /**
47  *      keydb_fetchchar - Fetches a char from a file.
48  */
49 static int keydb_fetchchar(void *_ctx, size_t count, void *c)
50 {
51         struct pg_fc_ctx *ctx = (struct pg_fc_ctx *) _ctx;
52
53         return (!lo_read(ctx->dbconn, ctx->fd, (char *) c, count));
54 }
55
56 /**
57  *      keydb_putchar - Puts a char to a file.
58  */
59 static int keydb_putchar(void *_ctx, size_t count, void *c)
60 {
61         struct pg_fc_ctx *ctx = (struct pg_fc_ctx *) _ctx;
62
63         return !(lo_write(ctx->dbconn, ctx->fd, (char *) c, count));
64 }
65
66 /**
67  *      starttrans - Start a transaction.
68  *
69  *      Start a transaction. Intended to be used if we're about to perform many
70  *      operations on the database to help speed it all up, or if we want
71  *      something to only succeed if all relevant operations are successful.
72  */
73 static bool pg_starttrans(struct onak_dbctx *dbctx)
74 {
75         PGconn *dbconn = (PGconn *) dbctx->priv;
76         PGresult *result = NULL;
77         
78         result = PQexec(dbconn, "BEGIN");
79         PQclear(result);
80
81         return true;
82 }
83
84 /**
85  *      endtrans - End a transaction.
86  *
87  *      Ends a transaction.
88  */
89 static void pg_endtrans(struct onak_dbctx *dbctx)
90 {
91         PGconn *dbconn = (PGconn *) dbctx->priv;
92         PGresult *result = NULL;
93
94         result = PQexec(dbconn, "COMMIT");
95         PQclear(result);
96
97         return;
98 }
99
100 /**
101  *      fetch_key_id - Given a keyid fetch the key from storage.
102  *      @keyid: The keyid to fetch.
103  *      @publickey: A pointer to a structure to return the key in.
104  *      @intrans: If we're already in a transaction.
105  *
106  *      We use the hex representation of the keyid as the filename to fetch the
107  *      key from. The key is stored in the file as a binary OpenPGP stream of
108  *      packets, so we can just use read_openpgp_stream() to read the packets
109  *      in and then parse_keys() to parse the packets into a publickey
110  *      structure.
111  */
112 static int pg_fetch_key_id(struct onak_dbctx *dbctx,
113                 uint64_t keyid,
114                 struct openpgp_publickey **publickey,
115                 bool intrans)
116 {
117         struct openpgp_packet_list *packets = NULL;
118         PGconn *dbconn = (PGconn *) dbctx->priv;
119         PGresult *result = NULL;
120         char *oids = NULL;
121         char statement[1024];
122         int i = 0;
123         int numkeys = 0;
124         Oid key_oid;
125         struct pg_fc_ctx fcctx;
126
127         if (!intrans) {
128                 result = PQexec(dbconn, "BEGIN");
129                 PQclear(result);
130         }
131         
132         if (keyid > 0xFFFFFFFF) {
133                 snprintf(statement, 1023,
134                         "SELECT keydata FROM onak_keys WHERE keyid = '%"
135                         PRIX64 "'",
136                         keyid);
137         } else {
138                 snprintf(statement, 1023,
139                         "SELECT keydata FROM onak_keys WHERE keyid "
140                         "LIKE '%%%" PRIX64 "'",
141                         keyid);
142         }
143         result = PQexec(dbconn, statement);
144
145         if (PQresultStatus(result) == PGRES_TUPLES_OK) {
146                 numkeys = PQntuples(result);
147                 for (i = 0; i < numkeys && numkeys <= config.maxkeys; i++) {
148                         oids = PQgetvalue(result, i, 0);
149                         key_oid = (Oid) atoi(oids);
150
151                         fcctx.fd = lo_open(dbconn, key_oid, INV_READ);
152                         if (fcctx.fd < 0) {
153                                 logthing(LOGTHING_ERROR,
154                                                 "Can't open large object.");
155                         } else {
156                                 fcctx.dbconn = dbconn;
157                                 read_openpgp_stream(keydb_fetchchar, &fcctx,
158                                                 &packets, 0);
159                                 parse_keys(packets, publickey);
160                                 lo_close(dbconn, fcctx.fd);
161                                 free_packet_list(packets);
162                                 packets = NULL;
163                         }
164                 }
165         } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
166                 logthing(LOGTHING_ERROR, "Problem retrieving key from DB.");
167         }
168
169         PQclear(result);
170
171         if (!intrans) {
172                 result = PQexec(dbconn, "COMMIT");
173                 PQclear(result);
174         }
175         return (numkeys);
176 }
177
178 /**
179  *      fetch_key_text - Trys to find the keys that contain the supplied text.
180  *      @search: The text to search for.
181  *      @publickey: A pointer to a structure to return the key in.
182  *
183  *      This function searches for the supplied text and returns the keys that
184  *      contain it.
185  */
186 static int pg_fetch_key_text(struct onak_dbctx *dbctx,
187                 const char *search,
188                 struct openpgp_publickey **publickey)
189 {
190         struct openpgp_packet_list *packets = NULL;
191         PGconn *dbconn = (PGconn *) dbctx->priv;
192         PGresult *result = NULL;
193         char *oids = NULL;
194         char statement[1024];
195         int i = 0;
196         int numkeys = 0;
197         Oid key_oid;
198         char *newsearch = NULL;
199         struct pg_fc_ctx fcctx;
200
201         result = PQexec(dbconn, "BEGIN");
202         PQclear(result);
203
204         newsearch = malloc(strlen(search) * 2 + 1);
205         memset(newsearch, 0, strlen(search) * 2 + 1);
206         PQescapeStringConn(dbconn, newsearch, search, strlen(search), NULL);
207         snprintf(statement, 1023,
208                         "SELECT DISTINCT onak_keys.keydata FROM onak_keys, "
209                         "onak_uids WHERE onak_keys.keyid = onak_uids.keyid "
210                         "AND onak_uids.uid LIKE '%%%s%%'",
211                         newsearch);
212         result = PQexec(dbconn, statement);
213         free(newsearch);
214         newsearch = NULL;
215
216         if (PQresultStatus(result) == PGRES_TUPLES_OK) {
217                 numkeys = PQntuples(result);
218                 for (i = 0; i < numkeys && numkeys <= config.maxkeys; i++) {
219                         oids = PQgetvalue(result, i, 0);
220                         key_oid = (Oid) atoi(oids);
221
222                         fcctx.fd = lo_open(dbconn, key_oid, INV_READ);
223                         if (fcctx.fd < 0) {
224                                 logthing(LOGTHING_ERROR,
225                                                 "Can't open large object.");
226                         } else {
227                                 fcctx.dbconn = dbconn;
228                                 read_openpgp_stream(keydb_fetchchar, &fcctx,
229                                                 &packets,
230                                                 0);
231                                 parse_keys(packets, publickey);
232                                 lo_close(dbconn, fcctx.fd);
233                                 free_packet_list(packets);
234                                 packets = NULL;
235                         }
236                 }
237         } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
238                 logthing(LOGTHING_ERROR, "Problem retrieving key from DB.");
239         }
240
241         PQclear(result);
242
243         result = PQexec(dbconn, "COMMIT");
244         PQclear(result);
245         return (numkeys);
246 }
247
248 /**
249  *      delete_key - Given a keyid delete the key from storage.
250  *      @keyid: The keyid to delete.
251  *      @intrans: If we're already in a transaction.
252  *
253  *      This function deletes a public key from whatever storage mechanism we
254  *      are using. Returns 0 if the key existed.
255  */
256 static int pg_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
257 {
258         PGconn *dbconn = (PGconn *) dbctx->priv;
259         PGresult *result = NULL;
260         char *oids = NULL;
261         char statement[1024];
262         int found = 1;
263         int i;
264         Oid key_oid;
265
266         if (!intrans) {
267                 result = PQexec(dbconn, "BEGIN");
268                 PQclear(result);
269         }
270         
271         snprintf(statement, 1023,
272                         "SELECT keydata FROM onak_keys WHERE keyid = '%"
273                         PRIX64 "'",
274                         keyid);
275         result = PQexec(dbconn, statement);
276
277         if (PQresultStatus(result) == PGRES_TUPLES_OK) {
278                 found = 0;
279                 i = PQntuples(result);
280                 while (i > 0) {
281                         oids = PQgetvalue(result, i-1, 0);
282                         key_oid = (Oid) atoi(oids);
283                         lo_unlink(dbconn, key_oid);
284                         i--;
285                 }
286                 PQclear(result);
287
288                 snprintf(statement, 1023,
289                         "DELETE FROM onak_keys WHERE keyid = '%" PRIX64 "'",
290                         keyid);
291                 result = PQexec(dbconn, statement);
292                 PQclear(result);
293
294                 snprintf(statement, 1023,
295                         "DELETE FROM onak_sigs WHERE signee = '%" PRIX64 "'",
296                         keyid);
297                 result = PQexec(dbconn, statement);
298                 PQclear(result);
299
300                 snprintf(statement, 1023,
301                         "DELETE FROM onak_uids WHERE keyid = '%" PRIX64 "'",
302                         keyid);
303                 result = PQexec(dbconn, statement);
304         } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
305                 logthing(LOGTHING_ERROR,
306                                 "Problem retrieving key (%" PRIX64
307                                 ") from DB.",
308                                 keyid);
309         }
310
311         PQclear(result);
312
313         if (!intrans) {
314                 result = PQexec(dbconn, "COMMIT");
315                 PQclear(result);
316         }
317         return (found);
318 }
319
320 /**
321  *      store_key - Takes a key and stores it.
322  *      @publickey: A pointer to the public key to store.
323  *      @intrans: If we're already in a transaction.
324  *      @update: If true the key exists and should be updated.
325  *
326  *      Again we just use the hex representation of the keyid as the filename
327  *      to store the key to. We flatten the public key to a list of OpenPGP
328  *      packets and then use write_openpgp_stream() to write the stream out to
329  *      the file. If update is true then we delete the old key first, otherwise
330  *      we trust that it doesn't exist.
331  */
332 static int pg_store_key(struct onak_dbctx *dbctx,
333                 struct openpgp_publickey *publickey, bool intrans,
334                 bool update)
335 {
336         struct openpgp_packet_list *packets = NULL;
337         struct openpgp_packet_list *list_end = NULL;
338         struct openpgp_publickey *next = NULL;
339         struct openpgp_signedpacket_list *curuid = NULL;
340         PGconn *dbconn = (PGconn *) dbctx->priv;
341         PGresult *result = NULL;
342         char statement[1024];
343         Oid key_oid;
344         char **uids = NULL;
345         char *primary = NULL;
346         char *safeuid = NULL;
347         int i;
348         uint64_t keyid;
349         struct pg_fc_ctx fcctx;
350
351         if (!intrans) {
352                 result = PQexec(dbconn, "BEGIN");
353                 PQclear(result);
354         }
355
356         if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
357                 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
358                 return 0;
359         }
360
361         /*
362          * Delete the key if we already have it.
363          *
364          * TODO: Can we optimize this perhaps? Possibly when other data is
365          * involved as well? I suspect this is easiest and doesn't make a lot
366          * of difference though - the largest chunk of data is the keydata and
367          * it definitely needs updated.
368          */
369         if (update) {
370                 pg_delete_key(dbctx, keyid, true);
371         }
372
373         next = publickey->next;
374         publickey->next = NULL;
375         flatten_publickey(publickey, &packets, &list_end);
376         publickey->next = next;
377                 
378         key_oid = lo_creat(dbconn, INV_READ | INV_WRITE);
379         if (key_oid == 0) {
380                 logthing(LOGTHING_ERROR, "Can't create key OID");
381         } else {
382                 fcctx.fd = lo_open(dbconn, key_oid, INV_WRITE);
383                 fcctx.dbconn = dbconn;
384                 write_openpgp_stream(keydb_putchar, &fcctx, packets);
385                 lo_close(dbconn, fcctx.fd);
386         }
387         free_packet_list(packets);
388         packets = NULL;
389
390         snprintf(statement, 1023, 
391                         "INSERT INTO onak_keys (keyid, keydata) VALUES "
392                         "('%" PRIX64 "', '%d')", 
393                         keyid,
394                         key_oid);
395         result = PQexec(dbconn, statement);
396
397         if (PQresultStatus(result) != PGRES_COMMAND_OK) {
398                 logthing(LOGTHING_ERROR, "Problem storing key in DB.");
399                 logthing(LOGTHING_ERROR, "%s", PQresultErrorMessage(result));
400         }
401         PQclear(result);
402
403         uids = keyuids(publickey, &primary);
404         if (uids != NULL) {
405                 for (i = 0; uids[i] != NULL; i++) {
406                         safeuid = malloc(strlen(uids[i]) * 2 + 1);
407                         if (safeuid != NULL) {
408                                 memset(safeuid, 0, strlen(uids[i]) * 2 + 1);
409                                 PQescapeStringConn(dbconn, safeuid, uids[i],
410                                                 strlen(uids[i]), NULL);
411
412                                 snprintf(statement, 1023,
413                                         "INSERT INTO onak_uids "
414                                         "(keyid, uid, pri) "
415                                         "VALUES ('%" PRIX64 "', '%s', '%c')",
416                                         keyid,
417                                         safeuid,
418                                         (uids[i] == primary) ? 't' : 'f');
419                                 result = PQexec(dbconn, statement);
420
421                                 free(safeuid);
422                                 safeuid = NULL;
423                         }
424                         if (uids[i] != NULL) {
425                                 free(uids[i]);
426                                 uids[i] = NULL;
427                         }
428
429                         if (PQresultStatus(result) != PGRES_COMMAND_OK) {
430                                 logthing(LOGTHING_ERROR,
431                                                 "Problem storing key in DB.");
432                                 logthing(LOGTHING_ERROR, "%s",
433                                                 PQresultErrorMessage(result));
434                         }
435                         /*
436                          * TODO: Check result.
437                          */
438                         PQclear(result);
439                 }
440                 free(uids);
441                 uids = NULL;
442         }
443
444         for (curuid = publickey->uids; curuid != NULL; curuid = curuid->next) {
445                 for (packets = curuid->sigs; packets != NULL; 
446                                 packets = packets->next) {
447                         snprintf(statement, 1023,
448                                 "INSERT INTO onak_sigs (signer, signee) "
449                                 "VALUES ('%" PRIX64 "', '%" PRIX64 "')",
450                                 sig_keyid(packets->packet),
451                                 keyid);
452                         result = PQexec(dbconn, statement);
453                         PQclear(result);
454                 }
455         }
456
457         if (!intrans) {
458                 result = PQexec(dbconn, "COMMIT");
459                 PQclear(result);
460         }
461         
462         return 0;
463 }
464
465 /**
466  *      keyid2uid - Takes a keyid and returns the primary UID for it.
467  *      @keyid: The keyid to lookup.
468  */
469 static char *pg_keyid2uid(struct onak_dbctx *dbctx, uint64_t keyid)
470 {
471         PGconn *dbconn = (PGconn *) dbctx->priv;
472         PGresult *result = NULL;
473         char statement[1024];
474         char *uid = NULL;
475
476         snprintf(statement, 1023,
477                 "SELECT uid FROM onak_uids WHERE keyid = '%" PRIX64
478                 "' AND pri = 't'",
479                 keyid);
480         result = PQexec(dbconn, statement);
481
482         /*
483          * Technically we only expect one response to the query; a key only has
484          * one primary ID. Better to return something than nothing though.
485          *
486          * TODO: Log if we get more than one response? Needs logging framework
487          * first though.
488          */
489         if (PQresultStatus(result) == PGRES_TUPLES_OK &&
490                         PQntuples(result) >= 1) {
491                 uid = strdup(PQgetvalue(result, 0, 0));
492         } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
493                 logthing(LOGTHING_ERROR,
494                                 "Problem retrieving key (%" PRIX64
495                                 ") from DB.",
496                                 keyid);
497         }
498
499         PQclear(result);
500
501         return uid;
502 }
503
504 /**
505  *      getkeysigs - Gets a linked list of the signatures on a key.
506  *      @keyid: The keyid to get the sigs for.
507  *      @revoked: If the key is revoked.
508  *
509  *      This function gets the list of signatures on a key. Used for key 
510  *      indexing and doing stats bits.
511  */
512 static struct ll *pg_getkeysigs(struct onak_dbctx *dbctx,
513                         uint64_t keyid, bool *revoked)
514 {
515         struct ll *sigs = NULL;
516         PGconn *dbconn = (PGconn *) dbctx->priv;
517         PGresult *result = NULL;
518         uint64_t signer;
519         char statement[1024];
520         int i, j;
521         int numsigs = 0;
522         bool intrans = false;
523         char *str;
524
525         if (!intrans) {
526                 result = PQexec(dbconn, "BEGIN");
527                 PQclear(result);
528         }
529
530         snprintf(statement, 1023,
531                 "SELECT DISTINCT signer FROM onak_sigs WHERE signee = '%"
532                 PRIX64 "'",
533                 keyid);
534         result = PQexec(dbconn, statement);
535
536         if (PQresultStatus(result) == PGRES_TUPLES_OK) {
537                 numsigs = PQntuples(result);
538                 for (i = 0; i < numsigs;  i++) {
539                         j = 0;
540                         signer = 0;
541                         str = PQgetvalue(result, i, 0);
542                         while (str[j] != 0) {
543                                 signer <<= 4;
544                                 if (str[j] >= '0' && str[j] <= '9') {
545                                         signer += str[j] - '0';
546                                 } else {
547                                         signer += str[j] - 'A' + 10;
548                                 }
549                                 j++;
550                         }
551                         sigs = lladd(sigs, createandaddtohash(signer));
552                 }
553         } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
554                 logthing(LOGTHING_ERROR, "Problem retrieving key from DB.");
555         }
556
557         PQclear(result);
558
559         if (!intrans) {
560                 result = PQexec(dbconn, "COMMIT");
561                 PQclear(result);
562         }
563
564         /*
565          * TODO: What do we do about revocations? We don't have the details
566          * stored in a separate table, so we'd have to grab the key and decode
567          * it, which we're trying to avoid by having a signers table.
568          */
569         if (revoked != NULL) {
570                 *revoked = false;
571         }
572         
573         return sigs;
574 }
575
576 /**
577  *      iterate_keys - call a function once for each key in the db.
578  *      @iterfunc: The function to call.
579  *      @ctx: A context pointer
580  *
581  *      Calls iterfunc once for each key in the database. ctx is passed
582  *      unaltered to iterfunc. This function is intended to aid database dumps
583  *      and statistic calculations.
584  *
585  *      Returns the number of keys we iterated over.
586  */
587 static int pg_iterate_keys(struct onak_dbctx *dbctx,
588                 void (*iterfunc)(void *ctx,
589                 struct openpgp_publickey *key), void *ctx)
590 {
591         struct openpgp_packet_list *packets = NULL;
592         struct openpgp_publickey *key = NULL;
593         PGconn *dbconn = (PGconn *) dbctx->priv;
594         PGresult *result = NULL;
595         char *oids = NULL;
596         int i = 0;
597         int numkeys = 0;
598         Oid key_oid;
599         struct pg_fc_ctx fcctx;
600
601         result = PQexec(dbconn, "SELECT keydata FROM onak_keys;");
602
603         if (PQresultStatus(result) == PGRES_TUPLES_OK) {
604                 numkeys = PQntuples(result);
605                 for (i = 0; i < numkeys; i++) {
606                         oids = PQgetvalue(result, i, 0);
607                         key_oid = (Oid) atoi(oids);
608
609                         fcctx.fd = lo_open(dbconn, key_oid, INV_READ);
610                         if (fcctx.fd < 0) {
611                                 logthing(LOGTHING_ERROR,
612                                                 "Can't open large object.");
613                         } else {
614                                 fcctx.dbconn = dbconn;
615                                 read_openpgp_stream(keydb_fetchchar, &fcctx,
616                                                 &packets, 0);
617                                 parse_keys(packets, &key);
618                                 lo_close(dbconn, fcctx.fd);
619
620                                 iterfunc(ctx, key);
621                                         
622                                 free_publickey(key);
623                                 key = NULL;
624                                 free_packet_list(packets);
625                                 packets = NULL;
626                         }
627                 }
628         } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
629                 logthing(LOGTHING_ERROR, "Problem retrieving key from DB.");
630         }
631
632         PQclear(result);
633
634         return (numkeys);
635 }
636
637 /*
638  * Include the basic keydb routines.
639  */
640 #define NEED_GETFULLKEYID 1
641 #define NEED_UPDATEKEYS 1
642 #define NEED_GET_FP 1
643 #include "keydb.c"
644
645 /**
646  *      cleanupdb - De-initialize the key database.
647  *
648  *      This function should be called upon program exit to allow the DB to
649  *      cleanup after itself.
650  */
651 static void pg_cleanupdb(struct onak_dbctx *dbctx)
652 {
653         PGconn *dbconn = (PGconn *) dbctx->priv;
654
655         PQfinish(dbconn);
656         dbconn = NULL;
657
658         free(dbctx);
659 }
660
661 /**
662  *      initdb - Initialize the key database.
663  *
664  *      This function should be called before any of the other functions in
665  *      this file are called in order to allow the DB to be initialized ready
666  *      for access.
667  */
668 struct onak_dbctx *keydb_pg_init(struct onak_db_config *dbcfg, bool readonly)
669 {
670         struct onak_dbctx *dbctx;
671         PGconn *dbconn;
672
673         dbctx = malloc(sizeof(struct onak_dbctx));
674         if (dbctx == NULL) {
675                 return NULL;
676         }
677         dbctx->config = dbcfg;
678
679         dbconn = PQsetdbLogin(dbcfg->hostname, // host
680                         NULL, // port
681                         NULL, // options
682                         NULL, // tty
683                         dbcfg->location, // database
684                         dbcfg->username,  //login
685                         dbcfg->password); // password
686
687         if (PQstatus(dbconn) == CONNECTION_BAD) {
688                 logthing(LOGTHING_CRITICAL, "Connection to database failed.");
689                 logthing(LOGTHING_CRITICAL, "%s", PQerrorMessage(dbconn));
690                 PQfinish(dbconn);
691                 dbconn = NULL;
692                 exit(1);
693         }
694
695         dbctx->priv = dbconn;
696
697         dbctx->cleanupdb                = pg_cleanupdb;
698         dbctx->starttrans               = pg_starttrans;
699         dbctx->endtrans                 = pg_endtrans;
700         dbctx->fetch_key_id             = pg_fetch_key_id;
701         dbctx->fetch_key_fp             = generic_fetch_key_fp;
702         dbctx->fetch_key_text           = pg_fetch_key_text;
703         dbctx->store_key                = pg_store_key;
704         dbctx->update_keys              = generic_update_keys;
705         dbctx->delete_key               = pg_delete_key;
706         dbctx->getkeysigs               = pg_getkeysigs;
707         dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
708         dbctx->keyid2uid                = pg_keyid2uid;
709         dbctx->getfullkeyid             = generic_getfullkeyid;
710         dbctx->iterate_keys             = pg_iterate_keys;
711
712         return dbctx;
713 }