2 * keydb_db3.c - Routines to store and fetch keys in a DB3 database.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
10 #include <sys/types.h>
22 #include "charfuncs.h"
25 #include "decodekey.h"
26 #include "keystructs.h"
29 #include "onak-conf.h"
34 * dbenv - our database environment.
36 static DB_ENV *dbenv = NULL;
39 * numdb - The number of database files we have.
41 static int numdbs = 16;
44 * dbconn - our connections to the key database files.
46 static DB **dbconns = NULL;
49 * worddb - our connection to the word database.
51 static DB *worddb = NULL;
54 * id32db - our connection to the 32bit ID database.
56 static DB *id32db = NULL;
59 * txn - our current transaction id.
61 static DB_TXN *txn = NULL;
63 DB *keydb(uint64_t keyid)
69 return(dbconns[keytrun % numdbs]);
73 * initdb - Initialize the key database.
75 * This function should be called before any of the other functions in
76 * this file are called in order to allow the DB to be initialized ready
79 void initdb(bool readonly)
87 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
88 numdb = fopen(buf, "r");
90 if (fgets(buf, sizeof(buf), numdb) != NULL) {
94 } else if (!readonly) {
95 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
97 numdb = fopen(buf, "w");
99 fprintf(numdb, "%d", numdbs);
102 logthing(LOGTHING_ERROR,
103 "Couldn't write num_keydb: %s",
108 dbconns = malloc(sizeof (DB *) * numdbs);
109 if (dbconns == NULL) {
110 logthing(LOGTHING_CRITICAL,
111 "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.
127 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
129 logthing(LOGTHING_CRITICAL,
130 "db_env_create: %s", db_strerror(ret));
134 ret = dbenv->open(dbenv, config.db_dir,
135 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
140 logthing(LOGTHING_CRITICAL,
141 "Error opening db environment: %s (%s)",
147 for (i = 0; i < numdbs; i++) {
148 ret = db_create(&dbconns[i], dbenv, 0);
150 logthing(LOGTHING_CRITICAL,
151 "db_create: %s", db_strerror(ret));
155 snprintf(buf, 1023, "keydb.%d.db", i);
160 ret = dbconns[i]->open(dbconns[i], buf,
166 logthing(LOGTHING_CRITICAL,
167 "Error opening key database: %s (%s)",
174 ret = db_create(&worddb, dbenv, 0);
176 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
179 ret = worddb->set_flags(worddb, DB_DUP);
181 ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
185 logthing(LOGTHING_CRITICAL,
186 "Error opening word database: %s (%s)",
192 ret = db_create(&id32db, dbenv, 0);
194 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
197 ret = id32db->set_flags(id32db, DB_DUP);
199 ret = id32db->open(id32db, "id32db", NULL, DB_HASH,
203 logthing(LOGTHING_CRITICAL,
204 "Error opening id32 database: %s (%s)",
214 * cleanupdb - De-initialize the key database.
216 * This function should be called upon program exit to allow the DB to
217 * cleanup after itself.
223 txn_checkpoint(dbenv, 0, 0, 0);
224 id32db->close(id32db, 0);
226 worddb->close(worddb, 0);
228 for (i = 0; i < numdbs; i++) {
229 dbconns[i]->close(dbconns[i], 0);
232 dbenv->close(dbenv, 0);
237 * starttrans - Start a transaction.
239 * Start a transaction. Intended to be used if we're about to perform many
240 * operations on the database to help speed it all up, or if we want
241 * something to only succeed if all relevant operations are successful.
243 bool starttrans(void)
247 assert(dbenv != NULL);
250 ret = txn_begin(dbenv,
251 NULL, /* No parent transaction */
255 logthing(LOGTHING_CRITICAL,
256 "Error starting transaction: %s",
265 * endtrans - End a transaction.
267 * Ends a transaction.
273 assert(dbenv != NULL);
276 ret = txn_commit(txn,
279 logthing(LOGTHING_CRITICAL,
280 "Error ending transaction: %s",
290 * fetch_key - Given a keyid fetch the key from storage.
291 * @keyid: The keyid to fetch.
292 * @publickey: A pointer to a structure to return the key in.
293 * @intrans: If we're already in a transaction.
295 * We use the hex representation of the keyid as the filename to fetch the
296 * key from. The key is stored in the file as a binary OpenPGP stream of
297 * packets, so we can just use read_openpgp_stream() to read the packets
298 * in and then parse_keys() to parse the packets into a publickey
301 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
304 struct openpgp_packet_list *packets = NULL;
308 struct buffer_ctx fetchbuf;
310 if (keyid < 0x100000000LL) {
311 keyid = getfullkeyid(keyid);
314 memset(&key, 0, sizeof(key));
315 memset(&data, 0, sizeof(data));
320 key.size = sizeof(keyid);
327 ret = keydb(keyid)->get(keydb(keyid),
334 fetchbuf.buffer = data.data;
336 fetchbuf.size = data.size;
337 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
339 parse_keys(packets, publickey);
340 free_packet_list(packets);
343 } else if (ret != DB_NOTFOUND) {
344 logthing(LOGTHING_ERROR,
345 "Problem retrieving key: %s",
356 int worddb_cmp(const void *d1, const void *d2)
358 return memcmp(d1, d2, 12);
362 * fetch_key_text - Trys to find the keys that contain the supplied text.
363 * @search: The text to search for.
364 * @publickey: A pointer to a structure to return the key in.
366 * This function searches for the supplied text and returns the keys that
369 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
377 char *searchtext = NULL;
378 struct ll *wordlist = NULL;
379 struct ll *curword = NULL;
380 struct ll *keylist = NULL;
381 struct ll *newkeylist = NULL;
384 searchtext = strdup(search);
385 wordlist = makewordlist(wordlist, searchtext);
389 ret = worddb->cursor(worddb,
394 for (curword = wordlist; curword != NULL; curword = curword->next) {
395 memset(&key, 0, sizeof(key));
396 memset(&data, 0, sizeof(data));
397 key.data = curword->object;
398 key.size = strlen(curword->object);
399 data.flags = DB_DBT_MALLOC;
400 ret = cursor->c_get(cursor,
404 while (ret == 0 && strncmp(key.data, curword->object,
406 ((char *) curword->object)[key.size] == 0) {
408 for (i = 4; i < 12; i++) {
410 keyid += ((unsigned char *)
414 if (keylist == NULL ||
415 llfind(keylist, data.data,
416 worddb_cmp) != NULL) {
417 newkeylist = lladd(newkeylist, data.data);
423 ret = cursor->c_get(cursor,
428 llfree(keylist, free);
429 keylist = newkeylist;
431 if (data.data != NULL) {
436 llfree(wordlist, NULL);
439 for (newkeylist = keylist;
440 newkeylist != NULL && numkeys < config.maxkeys;
441 newkeylist = newkeylist->next) {
444 for (i = 4; i < 12; i++) {
446 keyid += ((unsigned char *)
447 newkeylist->object)[i];
450 numkeys += fetch_key(keyid,
454 llfree(keylist, free);
459 ret = cursor->c_close(cursor);
468 * store_key - Takes a key and stores it.
469 * @publickey: A pointer to the public key to store.
470 * @intrans: If we're already in a transaction.
471 * @update: If true the key exists and should be updated.
473 * Again we just use the hex representation of the keyid as the filename
474 * to store the key to. We flatten the public key to a list of OpenPGP
475 * packets and then use write_openpgp_stream() to write the stream out to
476 * the file. If update is true then we delete the old key first, otherwise
477 * we trust that it doesn't exist.
479 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
481 struct openpgp_packet_list *packets = NULL;
482 struct openpgp_packet_list *list_end = NULL;
483 struct openpgp_publickey *next = NULL;
486 struct buffer_ctx storebuf;
490 uint32_t shortkeyid = 0;
491 uint64_t *subkeyids = NULL;
493 char *primary = NULL;
494 unsigned char worddb_data[12];
495 struct ll *wordlist = NULL;
496 struct ll *curword = NULL;
497 bool deadlock = false;
499 keyid = get_keyid(publickey);
506 * Delete the key if we already have it.
508 * TODO: Can we optimize this perhaps? Possibly when other data is
509 * involved as well? I suspect this is easiest and doesn't make a lot
510 * of difference though - the largest chunk of data is the keydata and
511 * it definitely needs updated.
514 deadlock = (delete_key(keyid, true) == -1);
518 * Convert the key to a flat set of binary data.
521 next = publickey->next;
522 publickey->next = NULL;
523 flatten_publickey(publickey, &packets, &list_end);
524 publickey->next = next;
527 storebuf.size = 8192;
528 storebuf.buffer = malloc(8192);
530 write_openpgp_stream(buffer_putchar, &storebuf, packets);
533 * Now we have the key data store it in the DB; the keyid is
536 memset(&key, 0, sizeof(key));
537 memset(&data, 0, sizeof(data));
539 key.size = sizeof(keyid);
540 data.size = storebuf.offset;
541 data.data = storebuf.buffer;
543 ret = keydb(keyid)->put(keydb(keyid),
549 logthing(LOGTHING_ERROR,
550 "Problem storing key: %s",
552 if (ret == DB_LOCK_DEADLOCK) {
557 free(storebuf.buffer);
558 storebuf.buffer = NULL;
562 free_packet_list(packets);
567 * Walk through our uids storing the words into the db with the keyid.
570 uids = keyuids(publickey, &primary);
573 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
574 wordlist = makewordlist(wordlist, uids[i]);
577 for (curword = wordlist; curword != NULL && !deadlock;
578 curword = curword->next) {
579 memset(&key, 0, sizeof(key));
580 memset(&data, 0, sizeof(data));
581 key.data = curword->object;
582 key.size = strlen(key.data);
583 data.data = worddb_data;
584 data.size = sizeof(worddb_data);
587 * Our data is the key creation time followed by the
590 worddb_data[ 0] = publickey->publickey->data[1];
591 worddb_data[ 1] = publickey->publickey->data[2];
592 worddb_data[ 2] = publickey->publickey->data[3];
593 worddb_data[ 3] = publickey->publickey->data[4];
594 worddb_data[ 4] = (keyid >> 56) & 0xFF;
595 worddb_data[ 5] = (keyid >> 48) & 0xFF;
596 worddb_data[ 6] = (keyid >> 40) & 0xFF;
597 worddb_data[ 7] = (keyid >> 32) & 0xFF;
598 worddb_data[ 8] = (keyid >> 24) & 0xFF;
599 worddb_data[ 9] = (keyid >> 16) & 0xFF;
600 worddb_data[10] = (keyid >> 8) & 0xFF;
601 worddb_data[11] = keyid & 0xFF;
602 ret = worddb->put(worddb,
608 logthing(LOGTHING_ERROR,
609 "Problem storing word: %s",
611 if (ret == DB_LOCK_DEADLOCK) {
618 * Free our UID and word lists.
620 llfree(wordlist, NULL);
621 for (i = 0; uids[i] != NULL; i++) {
634 * Write the truncated 32 bit keyid so we can lookup the full id for
638 shortkeyid = keyid & 0xFFFFFFFF;
640 memset(&key, 0, sizeof(key));
641 memset(&data, 0, sizeof(data));
642 key.data = &shortkeyid;
643 key.size = sizeof(shortkeyid);
645 data.size = sizeof(keyid);
647 ret = id32db->put(id32db,
653 logthing(LOGTHING_ERROR,
654 "Problem storing short keyid: %s",
656 if (ret == DB_LOCK_DEADLOCK) {
663 subkeyids = keysubkeys(publickey);
665 while (subkeyids != NULL && subkeyids[i] != 0) {
666 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
668 memset(&key, 0, sizeof(key));
669 memset(&data, 0, sizeof(data));
670 key.data = &shortkeyid;
671 key.size = sizeof(shortkeyid);
673 data.size = sizeof(keyid);
675 ret = id32db->put(id32db,
681 logthing(LOGTHING_ERROR,
682 "Problem storing short keyid: %s",
684 if (ret == DB_LOCK_DEADLOCK) {
689 if (subkeyids != NULL) {
695 return deadlock ? -1 : 0 ;
699 * delete_key - Given a keyid delete the key from storage.
700 * @keyid: The keyid to delete.
701 * @intrans: If we're already in a transaction.
703 * This function deletes a public key from whatever storage mechanism we
704 * are using. Returns 0 if the key existed.
706 int delete_key(uint64_t keyid, bool intrans)
708 struct openpgp_publickey *publickey = NULL;
711 uint32_t shortkeyid = 0;
712 uint64_t *subkeyids = NULL;
716 char *primary = NULL;
717 unsigned char worddb_data[12];
718 struct ll *wordlist = NULL;
719 struct ll *curword = NULL;
720 bool deadlock = false;
726 fetch_key(keyid, &publickey, true);
729 * Walk through the uids removing the words from the worddb.
731 if (publickey != NULL) {
732 uids = keyuids(publickey, &primary);
735 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
736 wordlist = makewordlist(wordlist, uids[i]);
739 ret = worddb->cursor(worddb,
744 for (curword = wordlist; curword != NULL && !deadlock;
745 curword = curword->next) {
746 memset(&key, 0, sizeof(key));
747 memset(&data, 0, sizeof(data));
748 key.data = curword->object;
749 key.size = strlen(key.data);
750 data.data = worddb_data;
751 data.size = sizeof(worddb_data);
754 * Our data is the key creation time followed by the
757 worddb_data[ 0] = publickey->publickey->data[1];
758 worddb_data[ 1] = publickey->publickey->data[2];
759 worddb_data[ 2] = publickey->publickey->data[3];
760 worddb_data[ 3] = publickey->publickey->data[4];
761 worddb_data[ 4] = (keyid >> 56) & 0xFF;
762 worddb_data[ 5] = (keyid >> 48) & 0xFF;
763 worddb_data[ 6] = (keyid >> 40) & 0xFF;
764 worddb_data[ 7] = (keyid >> 32) & 0xFF;
765 worddb_data[ 8] = (keyid >> 24) & 0xFF;
766 worddb_data[ 9] = (keyid >> 16) & 0xFF;
767 worddb_data[10] = (keyid >> 8) & 0xFF;
768 worddb_data[11] = keyid & 0xFF;
770 ret = cursor->c_get(cursor,
776 ret = cursor->c_del(cursor, 0);
778 logthing(LOGTHING_ERROR,
779 "Problem deleting word: %s",
785 logthing(LOGTHING_ERROR,
786 "Problem deleting word: %s",
788 if (ret == DB_LOCK_DEADLOCK) {
793 ret = cursor->c_close(cursor);
797 * Free our UID and word lists.
799 llfree(wordlist, NULL);
800 for (i = 0; uids[i] != NULL; i++) {
806 free_publickey(publickey);
811 ret = id32db->cursor(id32db,
816 shortkeyid = keyid & 0xFFFFFFFF;
818 memset(&key, 0, sizeof(key));
819 memset(&data, 0, sizeof(data));
820 key.data = &shortkeyid;
821 key.size = sizeof(shortkeyid);
823 data.size = sizeof(keyid);
825 ret = cursor->c_get(cursor,
831 ret = cursor->c_del(cursor, 0);
833 logthing(LOGTHING_ERROR,
834 "Problem deleting short keyid: %s",
840 logthing(LOGTHING_ERROR,
841 "Problem deleting short keyid: %s",
843 if (ret == DB_LOCK_DEADLOCK) {
848 subkeyids = keysubkeys(publickey);
850 while (subkeyids != NULL && subkeyids[i] != 0) {
851 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
853 memset(&key, 0, sizeof(key));
854 memset(&data, 0, sizeof(data));
855 key.data = &shortkeyid;
856 key.size = sizeof(shortkeyid);
858 data.size = sizeof(keyid);
860 ret = cursor->c_get(cursor,
866 ret = cursor->c_del(cursor, 0);
868 logthing(LOGTHING_ERROR,
869 "Problem deleting short"
876 logthing(LOGTHING_ERROR,
877 "Problem deleting short keyid: %s",
879 if (ret == DB_LOCK_DEADLOCK) {
884 if (subkeyids != NULL) {
889 ret = cursor->c_close(cursor);
895 key.size = sizeof(keyid);
897 keydb(keyid)->del(keydb(keyid),
907 return deadlock ? (-1) : (ret == DB_NOTFOUND);
911 * dumpdb - dump the key database
912 * @filenamebase: The base filename to use for the dump.
914 * Dumps the database into one or more files, which contain pure OpenPGP
915 * that can be reimported into onak or gpg. filenamebase provides a base
916 * file name for the dump; several files may be created, all of which will
917 * begin with this string and then have a unique number and a .pgp
920 int dumpdb(char *filenamebase)
930 for (i = 0; i < numdbs; i++) {
931 ret = dbconns[i]->cursor(dbconns[i],
936 snprintf(filename, 1023, "%s.%d.pgp", filenamebase, i);
937 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
939 logthing(LOGTHING_ERROR,
940 "Error opening keydump file (%s): %s",
944 memset(&key, 0, sizeof(key));
945 memset(&data, 0, sizeof(data));
946 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
948 write(fd, data.data, data.size);
949 memset(&key, 0, sizeof(key));
950 memset(&data, 0, sizeof(data));
951 ret = cursor->c_get(cursor, &key, &data,
954 if (ret != DB_NOTFOUND) {
955 logthing(LOGTHING_ERROR,
956 "Problem reading key: %s",
962 ret = cursor->c_close(cursor);
970 * getfullkeyid - Maps a 32bit key id to a 64bit one.
971 * @keyid: The 32bit keyid.
973 * This function maps a 32bit key id to the full 64bit one. It returns the
974 * full keyid. If the key isn't found a keyid of 0 is returned.
976 uint64_t getfullkeyid(uint64_t keyid)
980 uint32_t shortkeyid = 0;
983 if (keyid < 0x100000000LL) {
984 ret = id32db->cursor(id32db,
989 shortkeyid = keyid & 0xFFFFFFFF;
991 memset(&key, 0, sizeof(key));
992 memset(&data, 0, sizeof(data));
993 key.data = &shortkeyid;
994 key.size = sizeof(shortkeyid);
995 data.flags = DB_DBT_MALLOC;
997 ret = cursor->c_get(cursor,
1003 keyid = *(uint64_t *) data.data;
1005 if (data.data != NULL) {
1011 ret = cursor->c_close(cursor);
1019 * Include the basic keydb routines.
1021 #define NEED_GETKEYSIGS 1
1022 #define NEED_KEYID2UID 1