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