2 * keydb_db4.c - Routines to store and fetch keys in a DB4 database.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002-2004 Project Purple
21 #include "charfuncs.h"
24 #include "decodekey.h"
25 #include "keystructs.h"
28 #include "onak-conf.h"
33 * dbenv - our database environment.
35 static DB_ENV *dbenv = NULL;
38 * numdb - The number of database files we have.
40 static int numdbs = 16;
43 * dbconn - our connections to the key database files.
45 static DB **dbconns = NULL;
48 * worddb - our connection to the word database.
50 static DB *worddb = NULL;
53 * id32db - our connection to the 32bit ID database.
55 static DB *id32db = NULL;
58 * txn - our current transaction id.
60 static DB_TXN *txn = NULL;
62 DB *keydb(uint64_t keyid)
68 return(dbconns[keytrun % numdbs]);
72 * initdb - Initialize the key database.
74 * This function should be called before any of the other functions in
75 * this file are called in order to allow the DB to be initialized ready
78 void initdb(bool readonly)
86 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
87 numdb = fopen(buf, "r");
89 if (fgets(buf, sizeof(buf), numdb) != NULL) {
93 } else if (!readonly) {
94 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
96 numdb = fopen(buf, "w");
98 fprintf(numdb, "%d", numdbs);
101 logthing(LOGTHING_ERROR,
102 "Couldn't write num_keydb: %s",
107 dbconns = malloc(sizeof (DB *) * numdbs);
108 if (dbconns == NULL) {
109 logthing(LOGTHING_CRITICAL,
110 "Couldn't allocate memory for dbconns");
115 ret = db_env_create(&dbenv, 0);
117 logthing(LOGTHING_CRITICAL,
118 "db_env_create: %s", db_strerror(ret));
123 * Enable deadlock detection so that we don't block indefinitely on
124 * anything. What we really want is simple 2 state locks, but I'm not
125 * sure how to make the standard DB functions do that yet.
128 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
130 logthing(LOGTHING_CRITICAL,
131 "db_env_create: %s", db_strerror(ret));
136 ret = dbenv->open(dbenv, config.db_dir,
137 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
142 logthing(LOGTHING_CRITICAL,
143 "Error opening db environment: %s (%s)",
152 for (i = 0; !ret && i < numdbs; i++) {
153 ret = db_create(&dbconns[i], dbenv, 0);
155 logthing(LOGTHING_CRITICAL,
156 "db_create: %s", db_strerror(ret));
160 snprintf(buf, 1023, "keydb.%d.db", i);
165 ret = dbconns[i]->open(dbconns[i],
173 logthing(LOGTHING_CRITICAL,
174 "Error opening key database:"
185 ret = db_create(&worddb, dbenv, 0);
187 logthing(LOGTHING_CRITICAL, "db_create: %s",
193 ret = worddb->set_flags(worddb, DB_DUP);
197 ret = worddb->open(worddb, txn, "worddb", "worddb", DB_BTREE,
201 logthing(LOGTHING_CRITICAL,
202 "Error opening word database: %s (%s)",
209 ret = db_create(&id32db, dbenv, 0);
211 logthing(LOGTHING_CRITICAL, "db_create: %s",
217 ret = id32db->set_flags(id32db, DB_DUP);
221 ret = id32db->open(id32db, txn, "id32db", "id32db", DB_HASH,
225 logthing(LOGTHING_CRITICAL,
226 "Error opening id32 database: %s (%s)",
238 logthing(LOGTHING_CRITICAL,
239 "Error opening database; exiting");
247 * cleanupdb - De-initialize the key database.
249 * This function should be called upon program exit to allow the DB to
250 * cleanup after itself.
257 dbenv->txn_checkpoint(dbenv, 0, 0, 0);
258 if (id32db != NULL) {
259 id32db->close(id32db, 0);
262 if (worddb != NULL) {
263 worddb->close(worddb, 0);
266 for (i = 0; i < numdbs; i++) {
267 if (dbconns[i] != NULL) {
268 dbconns[i]->close(dbconns[i], 0);
272 dbenv->close(dbenv, 0);
278 * starttrans - Start a transaction.
280 * Start a transaction. Intended to be used if we're about to perform many
281 * operations on the database to help speed it all up, or if we want
282 * something to only succeed if all relevant operations are successful.
284 bool starttrans(void)
288 log_assert(dbenv != NULL);
289 log_assert(txn == NULL);
291 ret = dbenv->txn_begin(dbenv,
292 NULL, /* No parent transaction */
296 logthing(LOGTHING_CRITICAL,
297 "Error starting transaction: %s",
306 * endtrans - End a transaction.
308 * Ends a transaction.
314 log_assert(dbenv != NULL);
315 log_assert(txn != NULL);
317 ret = txn->commit(txn,
320 logthing(LOGTHING_CRITICAL,
321 "Error ending transaction: %s",
331 * fetch_key - Given a keyid fetch the key from storage.
332 * @keyid: The keyid to fetch.
333 * @publickey: A pointer to a structure to return the key in.
334 * @intrans: If we're already in a transaction.
336 * We use the hex representation of the keyid as the filename to fetch the
337 * key from. The key is stored in the file as a binary OpenPGP stream of
338 * packets, so we can just use read_openpgp_stream() to read the packets
339 * in and then parse_keys() to parse the packets into a publickey
342 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
345 struct openpgp_packet_list *packets = NULL;
349 struct buffer_ctx fetchbuf;
351 if (keyid < 0x100000000LL) {
352 keyid = getfullkeyid(keyid);
355 memset(&key, 0, sizeof(key));
356 memset(&data, 0, sizeof(data));
361 key.size = sizeof(keyid);
368 ret = keydb(keyid)->get(keydb(keyid),
375 fetchbuf.buffer = data.data;
377 fetchbuf.size = data.size;
378 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
380 parse_keys(packets, publickey);
381 free_packet_list(packets);
384 } else if (ret != DB_NOTFOUND) {
385 logthing(LOGTHING_ERROR,
386 "Problem retrieving key: %s",
397 int worddb_cmp(const void *d1, const void *d2)
399 return memcmp(d1, d2, 12);
403 * fetch_key_text - Trys to find the keys that contain the supplied text.
404 * @search: The text to search for.
405 * @publickey: A pointer to a structure to return the key in.
407 * This function searches for the supplied text and returns the keys that
410 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
418 char *searchtext = NULL;
419 struct ll *wordlist = NULL;
420 struct ll *curword = NULL;
421 struct ll *keylist = NULL;
422 struct ll *newkeylist = NULL;
425 searchtext = strdup(search);
426 wordlist = makewordlist(wordlist, searchtext);
430 ret = worddb->cursor(worddb,
435 for (curword = wordlist; curword != NULL; curword = curword->next) {
436 memset(&key, 0, sizeof(key));
437 memset(&data, 0, sizeof(data));
438 key.data = curword->object;
439 key.size = strlen(curword->object);
440 data.flags = DB_DBT_MALLOC;
441 ret = cursor->c_get(cursor,
445 while (ret == 0 && strncmp(key.data, curword->object,
447 ((char *) curword->object)[key.size] == 0) {
449 for (i = 4; i < 12; i++) {
451 keyid += ((unsigned char *)
455 if (keylist == NULL ||
456 llfind(keylist, data.data,
457 worddb_cmp) != NULL) {
458 newkeylist = lladd(newkeylist, data.data);
464 ret = cursor->c_get(cursor,
469 llfree(keylist, free);
470 keylist = newkeylist;
472 if (data.data != NULL) {
477 llfree(wordlist, NULL);
480 for (newkeylist = keylist;
481 newkeylist != NULL && numkeys < config.maxkeys;
482 newkeylist = newkeylist->next) {
485 for (i = 4; i < 12; i++) {
487 keyid += ((unsigned char *)
488 newkeylist->object)[i];
491 numkeys += fetch_key(keyid,
495 llfree(keylist, free);
500 ret = cursor->c_close(cursor);
509 * store_key - Takes a key and stores it.
510 * @publickey: A pointer to the public key to store.
511 * @intrans: If we're already in a transaction.
512 * @update: If true the key exists and should be updated.
514 * Again we just use the hex representation of the keyid as the filename
515 * to store the key to. We flatten the public key to a list of OpenPGP
516 * packets and then use write_openpgp_stream() to write the stream out to
517 * the file. If update is true then we delete the old key first, otherwise
518 * we trust that it doesn't exist.
520 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
522 struct openpgp_packet_list *packets = NULL;
523 struct openpgp_packet_list *list_end = NULL;
524 struct openpgp_publickey *next = NULL;
527 struct buffer_ctx storebuf;
531 uint32_t shortkeyid = 0;
532 uint64_t *subkeyids = NULL;
534 char *primary = NULL;
535 unsigned char worddb_data[12];
536 struct ll *wordlist = NULL;
537 struct ll *curword = NULL;
538 bool deadlock = false;
540 keyid = get_keyid(publickey);
547 * Delete the key if we already have it.
549 * TODO: Can we optimize this perhaps? Possibly when other data is
550 * involved as well? I suspect this is easiest and doesn't make a lot
551 * of difference though - the largest chunk of data is the keydata and
552 * it definitely needs updated.
555 deadlock = (delete_key(keyid, true) == -1);
559 * Convert the key to a flat set of binary data.
562 next = publickey->next;
563 publickey->next = NULL;
564 flatten_publickey(publickey, &packets, &list_end);
565 publickey->next = next;
568 storebuf.size = 8192;
569 storebuf.buffer = malloc(8192);
571 write_openpgp_stream(buffer_putchar, &storebuf, packets);
574 * Now we have the key data store it in the DB; the keyid is
577 memset(&key, 0, sizeof(key));
578 memset(&data, 0, sizeof(data));
580 key.size = sizeof(keyid);
581 data.size = storebuf.offset;
582 data.data = storebuf.buffer;
584 ret = keydb(keyid)->put(keydb(keyid),
590 logthing(LOGTHING_ERROR,
591 "Problem storing key: %s",
593 if (ret == DB_LOCK_DEADLOCK) {
598 free(storebuf.buffer);
599 storebuf.buffer = NULL;
603 free_packet_list(packets);
608 * Walk through our uids storing the words into the db with the keyid.
611 uids = keyuids(publickey, &primary);
614 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
615 wordlist = makewordlist(wordlist, uids[i]);
618 for (curword = wordlist; curword != NULL && !deadlock;
619 curword = curword->next) {
620 memset(&key, 0, sizeof(key));
621 memset(&data, 0, sizeof(data));
622 key.data = curword->object;
623 key.size = strlen(key.data);
624 data.data = worddb_data;
625 data.size = sizeof(worddb_data);
628 * Our data is the key creation time followed by the
631 worddb_data[ 0] = publickey->publickey->data[1];
632 worddb_data[ 1] = publickey->publickey->data[2];
633 worddb_data[ 2] = publickey->publickey->data[3];
634 worddb_data[ 3] = publickey->publickey->data[4];
635 worddb_data[ 4] = (keyid >> 56) & 0xFF;
636 worddb_data[ 5] = (keyid >> 48) & 0xFF;
637 worddb_data[ 6] = (keyid >> 40) & 0xFF;
638 worddb_data[ 7] = (keyid >> 32) & 0xFF;
639 worddb_data[ 8] = (keyid >> 24) & 0xFF;
640 worddb_data[ 9] = (keyid >> 16) & 0xFF;
641 worddb_data[10] = (keyid >> 8) & 0xFF;
642 worddb_data[11] = keyid & 0xFF;
643 ret = worddb->put(worddb,
649 logthing(LOGTHING_ERROR,
650 "Problem storing word: %s",
652 if (ret == DB_LOCK_DEADLOCK) {
659 * Free our UID and word lists.
661 llfree(wordlist, NULL);
662 for (i = 0; uids[i] != NULL; i++) {
671 * Write the truncated 32 bit keyid so we can lookup the full id for
675 shortkeyid = keyid & 0xFFFFFFFF;
677 memset(&key, 0, sizeof(key));
678 memset(&data, 0, sizeof(data));
679 key.data = &shortkeyid;
680 key.size = sizeof(shortkeyid);
682 data.size = sizeof(keyid);
684 ret = id32db->put(id32db,
690 logthing(LOGTHING_ERROR,
691 "Problem storing short keyid: %s",
693 if (ret == DB_LOCK_DEADLOCK) {
700 subkeyids = keysubkeys(publickey);
702 while (subkeyids != NULL && subkeyids[i] != 0) {
703 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
705 memset(&key, 0, sizeof(key));
706 memset(&data, 0, sizeof(data));
707 key.data = &shortkeyid;
708 key.size = sizeof(shortkeyid);
710 data.size = sizeof(keyid);
712 ret = id32db->put(id32db,
718 logthing(LOGTHING_ERROR,
719 "Problem storing short keyid: %s",
721 if (ret == DB_LOCK_DEADLOCK) {
726 if (subkeyids != NULL) {
736 return deadlock ? -1 : 0 ;
740 * delete_key - Given a keyid delete the key from storage.
741 * @keyid: The keyid to delete.
742 * @intrans: If we're already in a transaction.
744 * This function deletes a public key from whatever storage mechanism we
745 * are using. Returns 0 if the key existed.
747 int delete_key(uint64_t keyid, bool intrans)
749 struct openpgp_publickey *publickey = NULL;
752 uint32_t shortkeyid = 0;
753 uint64_t *subkeyids = NULL;
757 char *primary = NULL;
758 unsigned char worddb_data[12];
759 struct ll *wordlist = NULL;
760 struct ll *curword = NULL;
761 bool deadlock = false;
767 fetch_key(keyid, &publickey, true);
770 * Walk through the uids removing the words from the worddb.
772 if (publickey != NULL) {
773 uids = keyuids(publickey, &primary);
776 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
777 wordlist = makewordlist(wordlist, uids[i]);
780 ret = worddb->cursor(worddb,
785 for (curword = wordlist; curword != NULL && !deadlock;
786 curword = curword->next) {
787 memset(&key, 0, sizeof(key));
788 memset(&data, 0, sizeof(data));
789 key.data = curword->object;
790 key.size = strlen(key.data);
791 data.data = worddb_data;
792 data.size = sizeof(worddb_data);
795 * Our data is the key creation time followed by the
798 worddb_data[ 0] = publickey->publickey->data[1];
799 worddb_data[ 1] = publickey->publickey->data[2];
800 worddb_data[ 2] = publickey->publickey->data[3];
801 worddb_data[ 3] = publickey->publickey->data[4];
802 worddb_data[ 4] = (keyid >> 56) & 0xFF;
803 worddb_data[ 5] = (keyid >> 48) & 0xFF;
804 worddb_data[ 6] = (keyid >> 40) & 0xFF;
805 worddb_data[ 7] = (keyid >> 32) & 0xFF;
806 worddb_data[ 8] = (keyid >> 24) & 0xFF;
807 worddb_data[ 9] = (keyid >> 16) & 0xFF;
808 worddb_data[10] = (keyid >> 8) & 0xFF;
809 worddb_data[11] = keyid & 0xFF;
811 ret = cursor->c_get(cursor,
817 ret = cursor->c_del(cursor, 0);
819 logthing(LOGTHING_ERROR,
820 "Problem deleting word: %s",
826 logthing(LOGTHING_ERROR,
827 "Problem deleting word: %s",
829 if (ret == DB_LOCK_DEADLOCK) {
834 ret = cursor->c_close(cursor);
838 * Free our UID and word lists.
840 llfree(wordlist, NULL);
841 for (i = 0; uids[i] != NULL; i++) {
847 free_publickey(publickey);
852 ret = id32db->cursor(id32db,
857 shortkeyid = keyid & 0xFFFFFFFF;
859 memset(&key, 0, sizeof(key));
860 memset(&data, 0, sizeof(data));
861 key.data = &shortkeyid;
862 key.size = sizeof(shortkeyid);
864 data.size = sizeof(keyid);
866 ret = cursor->c_get(cursor,
872 ret = cursor->c_del(cursor, 0);
874 logthing(LOGTHING_ERROR,
875 "Problem deleting short keyid: %s",
881 logthing(LOGTHING_ERROR,
882 "Problem deleting short keyid: %s",
884 if (ret == DB_LOCK_DEADLOCK) {
889 subkeyids = keysubkeys(publickey);
891 while (subkeyids != NULL && subkeyids[i] != 0) {
892 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
894 memset(&key, 0, sizeof(key));
895 memset(&data, 0, sizeof(data));
896 key.data = &shortkeyid;
897 key.size = sizeof(shortkeyid);
899 data.size = sizeof(keyid);
901 ret = cursor->c_get(cursor,
907 ret = cursor->c_del(cursor, 0);
909 logthing(LOGTHING_ERROR,
910 "Problem deleting short"
917 logthing(LOGTHING_ERROR,
918 "Problem deleting short keyid: %s",
920 if (ret == DB_LOCK_DEADLOCK) {
925 if (subkeyids != NULL) {
930 ret = cursor->c_close(cursor);
936 key.size = sizeof(keyid);
938 keydb(keyid)->del(keydb(keyid),
948 return deadlock ? (-1) : (ret == DB_NOTFOUND);
952 * dumpdb - dump the key database
953 * @filenamebase: The base filename to use for the dump.
955 * Dumps the database into one or more files, which contain pure OpenPGP
956 * that can be reimported into onak or gpg. filenamebase provides a base
957 * file name for the dump; several files may be created, all of which will
958 * begin with this string and then have a unique number and a .pgp
961 int dumpdb(char *filenamebase)
971 for (i = 0; i < numdbs; i++) {
972 ret = dbconns[i]->cursor(dbconns[i],
977 snprintf(filename, 1023, "%s.%d.pgp", filenamebase, i);
978 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
980 logthing(LOGTHING_ERROR,
981 "Error opening keydump file (%s): %s",
985 memset(&key, 0, sizeof(key));
986 memset(&data, 0, sizeof(data));
987 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
989 write(fd, data.data, data.size);
990 memset(&key, 0, sizeof(key));
991 memset(&data, 0, sizeof(data));
992 ret = cursor->c_get(cursor, &key, &data,
995 if (ret != DB_NOTFOUND) {
996 logthing(LOGTHING_ERROR,
997 "Problem reading key: %s",
1003 ret = cursor->c_close(cursor);
1011 * iterate_keys - call a function once for each key in the db.
1012 * @iterfunc: The function to call.
1013 * @ctx: A context pointer
1015 * Calls iterfunc once for each key in the database. ctx is passed
1016 * unaltered to iterfunc. This function is intended to aid database dumps
1017 * and statistic calculations.
1019 * Returns the number of keys we iterated over.
1021 int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1029 struct buffer_ctx fetchbuf;
1030 struct openpgp_packet_list *packets = NULL;
1031 struct openpgp_publickey *key = NULL;
1033 for (i = 0; i < numdbs; i++) {
1034 ret = dbconns[i]->cursor(dbconns[i],
1039 memset(&dbkey, 0, sizeof(dbkey));
1040 memset(&data, 0, sizeof(data));
1041 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1043 fetchbuf.buffer = data.data;
1044 fetchbuf.offset = 0;
1045 fetchbuf.size = data.size;
1046 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1048 parse_keys(packets, &key);
1052 free_publickey(key);
1054 free_packet_list(packets);
1057 memset(&dbkey, 0, sizeof(dbkey));
1058 memset(&data, 0, sizeof(data));
1059 ret = cursor->c_get(cursor, &dbkey, &data,
1063 if (ret != DB_NOTFOUND) {
1064 logthing(LOGTHING_ERROR,
1065 "Problem reading key: %s",
1069 ret = cursor->c_close(cursor);
1077 * getfullkeyid - Maps a 32bit key id to a 64bit one.
1078 * @keyid: The 32bit keyid.
1080 * This function maps a 32bit key id to the full 64bit one. It returns the
1081 * full keyid. If the key isn't found a keyid of 0 is returned.
1083 uint64_t getfullkeyid(uint64_t keyid)
1087 uint32_t shortkeyid = 0;
1090 if (keyid < 0x100000000LL) {
1091 ret = id32db->cursor(id32db,
1096 shortkeyid = keyid & 0xFFFFFFFF;
1098 memset(&key, 0, sizeof(key));
1099 memset(&data, 0, sizeof(data));
1100 key.data = &shortkeyid;
1101 key.size = sizeof(shortkeyid);
1102 data.flags = DB_DBT_MALLOC;
1104 ret = cursor->c_get(cursor,
1110 keyid = *(uint64_t *) data.data;
1112 if (data.data != NULL) {
1118 ret = cursor->c_close(cursor);
1126 * Include the basic keydb routines.
1128 #define NEED_GETKEYSIGS 1
1129 #define NEED_KEYID2UID 1
1130 #define NEED_UPDATEKEYS 1