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"
33 * dbenv - our database environment.
35 static DB_ENV *dbenv = NULL;
38 * dbconn - our connection to the key database.
40 static DB *dbconn = NULL;
43 * worddb - our connection to the word database.
45 static DB *worddb = NULL;
48 * txn - our current transaction id.
50 static DB_TXN *txn = NULL;
53 * makewordlist - Takes a string and splits it into a set of unique words.
54 * @wordlist: The current word list.
55 * @words: The string to split and add.
57 * We take words and split it on non alpha numeric characters. These get
58 * added to the word list if they're not already present. If the wordlist
59 * is NULL then we start a new list, otherwise it's search for already
60 * added words. Note that words is modified in the process of scanning.
62 * Returns the new word list.
64 struct ll *makewordlist(struct ll *wordlist, char *word)
70 * Walk through the words string, spliting on non alphanumerics and
71 * then checking if the word already exists in the list. If not then
75 while (end != NULL && *end != 0) {
77 while (*start != 0 && !isalnum(*start)) {
81 while (*end != 0 && isalnum(*end)) {
85 if (end - start > 1) {
91 if (llfind(wordlist, start,
93 wordlist = lladd(wordlist,
103 * initdb - Initialize the key database.
105 * This function should be called before any of the other functions in
106 * this file are called in order to allow the DB to be initialized ready
113 ret = db_env_create(&dbenv, 0);
115 logthing(LOGTHING_CRITICAL,
116 "db_env_create: %s", db_strerror(ret));
121 * Enable deadlock detection so that we don't block indefinitely on
122 * anything. What we really want is simple 2 state locks, but I'm not
123 * sure how to make the standard DB functions do that yet.
125 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
127 logthing(LOGTHING_CRITICAL,
128 "db_env_create: %s", db_strerror(ret));
132 ret = dbenv->open(dbenv, config.db_dir,
133 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
138 logthing(LOGTHING_CRITICAL,
139 "Erroring opening db environment: %s (%s)",
145 ret = db_create(&dbconn, dbenv, 0);
147 logthing(LOGTHING_CRITICAL,
148 "db_create: %s", db_strerror(ret));
152 ret = dbconn->open(dbconn, "keydb.db",
158 logthing(LOGTHING_CRITICAL,
159 "Error opening key database: %s (%s)",
165 ret = db_create(&worddb, dbenv, 0);
167 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
170 ret = worddb->set_flags(worddb, DB_DUP);
172 ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
176 logthing(LOGTHING_CRITICAL,
177 "Error opening word database: %s (%s)",
187 * cleanupdb - De-initialize the key database.
189 * This function should be called upon program exit to allow the DB to
190 * cleanup after itself.
194 worddb->close(worddb, 0);
196 dbconn->close(dbconn, 0);
198 dbenv->close(dbenv, 0);
203 * starttrans - Start a transaction.
205 * Start a transaction. Intended to be used if we're about to perform many
206 * operations on the database to help speed it all up, or if we want
207 * something to only succeed if all relevant operations are successful.
209 bool starttrans(void)
213 assert(dbenv != NULL);
216 ret = txn_begin(dbenv,
217 NULL, /* No parent transaction */
221 logthing(LOGTHING_CRITICAL,
222 "Error starting transaction: %s",
231 * endtrans - End a transaction.
233 * Ends a transaction.
239 assert(dbenv != NULL);
242 ret = txn_commit(txn,
245 logthing(LOGTHING_CRITICAL,
246 "Error ending transaction: %s",
256 * fetch_key - Given a keyid fetch the key from storage.
257 * @keyid: The keyid to fetch.
258 * @publickey: A pointer to a structure to return the key in.
259 * @intrans: If we're already in a transaction.
261 * We use the hex representation of the keyid as the filename to fetch the
262 * key from. The key is stored in the file as a binary OpenPGP stream of
263 * packets, so we can just use read_openpgp_stream() to read the packets
264 * in and then parse_keys() to parse the packets into a publickey
267 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
270 struct openpgp_packet_list *packets = NULL;
274 struct buffer_ctx fetchbuf;
276 memset(&key, 0, sizeof(key));
277 memset(&data, 0, sizeof(data));
282 key.size = sizeof(keyid);
290 ret = dbconn->get(dbconn,
297 fetchbuf.buffer = data.data;
299 fetchbuf.size = data.size;
300 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
302 parse_keys(packets, publickey);
303 free_packet_list(packets);
306 } else if (ret != DB_NOTFOUND) {
307 logthing(LOGTHING_ERROR,
308 "Problem retrieving key: %s",
319 int worddb_cmp(const char *d1, const char *d2)
321 return memcmp(d1, d2, 12);
325 * fetch_key_text - Trys to find the keys that contain the supplied text.
326 * @search: The text to search for.
327 * @publickey: A pointer to a structure to return the key in.
329 * This function searches for the supplied text and returns the keys that
332 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
340 char *searchtext = NULL;
341 struct ll *wordlist = NULL;
342 struct ll *curword = NULL;
343 struct ll *keylist = NULL;
344 struct ll *newkeylist = NULL;
347 searchtext = strdup(search);
348 wordlist = makewordlist(wordlist, searchtext);
352 ret = worddb->cursor(worddb,
357 for (curword = wordlist; curword != NULL; curword = curword->next) {
358 memset(&key, 0, sizeof(key));
359 memset(&data, 0, sizeof(data));
360 key.data = curword->object;
361 key.size = strlen(curword->object);
362 data.flags = DB_DBT_MALLOC;
363 ret = cursor->c_get(cursor,
367 while (ret == 0 && strncmp(key.data, curword->object,
369 ((char *) curword->object)[key.size] == 0) {
371 for (i = 4; i < 12; i++) {
373 keyid += ((unsigned char *)
377 if (keylist == NULL ||
378 llfind(keylist, data.data,
379 worddb_cmp) != NULL) {
380 newkeylist = lladd(newkeylist, data.data);
386 ret = cursor->c_get(cursor,
391 llfree(keylist, free);
392 keylist = newkeylist;
394 if (data.data != NULL) {
399 llfree(wordlist, NULL);
402 for (newkeylist = keylist;
403 newkeylist != NULL && numkeys < config.maxkeys;
404 newkeylist = newkeylist->next) {
407 for (i = 4; i < 12; i++) {
409 keyid += ((unsigned char *)
410 newkeylist->object)[i];
413 numkeys += fetch_key(keyid,
417 llfree(keylist, free);
422 ret = cursor->c_close(cursor);
431 * store_key - Takes a key and stores it.
432 * @publickey: A pointer to the public key to store.
433 * @intrans: If we're already in a transaction.
434 * @update: If true the key exists and should be updated.
436 * Again we just use the hex representation of the keyid as the filename
437 * to store the key to. We flatten the public key to a list of OpenPGP
438 * packets and then use write_openpgp_stream() to write the stream out to
439 * the file. If update is true then we delete the old key first, otherwise
440 * we trust that it doesn't exist.
442 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
444 struct openpgp_packet_list *packets = NULL;
445 struct openpgp_packet_list *list_end = NULL;
446 struct openpgp_publickey *next = NULL;
449 struct buffer_ctx storebuf;
454 char *primary = NULL;
455 unsigned char worddb_data[12];
456 struct ll *wordlist = NULL;
457 struct ll *curword = NULL;
458 bool deadlock = false;
460 keyid = get_keyid(publickey);
467 * Delete the key if we already have it.
469 * TODO: Can we optimize this perhaps? Possibly when other data is
470 * involved as well? I suspect this is easiest and doesn't make a lot
471 * of difference though - the largest chunk of data is the keydata and
472 * it definitely needs updated.
475 deadlock = (delete_key(keyid, true) == -1);
479 * Convert the key to a flat set of binary data.
482 next = publickey->next;
483 publickey->next = NULL;
484 flatten_publickey(publickey, &packets, &list_end);
485 publickey->next = next;
488 storebuf.size = 8192;
489 storebuf.buffer = malloc(8192);
491 write_openpgp_stream(buffer_putchar, &storebuf, packets);
494 * Now we have the key data store it in the DB; the keyid is
497 memset(&key, 0, sizeof(key));
498 memset(&data, 0, sizeof(data));
500 key.size = sizeof(keyid);
502 data.size = storebuf.offset;
503 data.data = storebuf.buffer;
505 ret = dbconn->put(dbconn,
511 logthing(LOGTHING_ERROR,
512 "Problem storing key: %s",
514 if (ret == DB_LOCK_DEADLOCK) {
519 free(storebuf.buffer);
520 storebuf.buffer = NULL;
524 free_packet_list(packets);
529 * Walk through our uids storing the words into the db with the keyid.
532 uids = keyuids(publickey, &primary);
535 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
536 wordlist = makewordlist(wordlist, uids[i]);
539 for (curword = wordlist; curword != NULL && !deadlock;
540 curword = curword->next) {
541 memset(&key, 0, sizeof(key));
542 memset(&data, 0, sizeof(data));
543 key.data = curword->object;
544 key.size = strlen(key.data);
545 data.data = worddb_data;
546 data.size = sizeof(worddb_data);
549 * Our data is the key creation time followed by the
552 worddb_data[ 0] = publickey->publickey->data[1];
553 worddb_data[ 1] = publickey->publickey->data[2];
554 worddb_data[ 2] = publickey->publickey->data[3];
555 worddb_data[ 3] = publickey->publickey->data[4];
556 worddb_data[ 4] = (keyid >> 56) & 0xFF;
557 worddb_data[ 5] = (keyid >> 48) & 0xFF;
558 worddb_data[ 6] = (keyid >> 40) & 0xFF;
559 worddb_data[ 7] = (keyid >> 32) & 0xFF;
560 worddb_data[ 8] = (keyid >> 24) & 0xFF;
561 worddb_data[ 9] = (keyid >> 16) & 0xFF;
562 worddb_data[10] = (keyid >> 8) & 0xFF;
563 worddb_data[11] = keyid & 0xFF;
564 ret = worddb->put(worddb,
570 logthing(LOGTHING_ERROR,
571 "Problem storing word: %s",
573 if (ret == DB_LOCK_DEADLOCK) {
580 * Free our UID and word lists.
582 llfree(wordlist, NULL);
583 for (i = 0; uids[i] != NULL; i++) {
595 return deadlock ? -1 : 0 ;
599 * delete_key - Given a keyid delete the key from storage.
600 * @keyid: The keyid to delete.
601 * @intrans: If we're already in a transaction.
603 * This function deletes a public key from whatever storage mechanism we
604 * are using. Returns 0 if the key existed.
606 int delete_key(uint64_t keyid, bool intrans)
608 struct openpgp_publickey *publickey = NULL;
614 char *primary = NULL;
615 unsigned char worddb_data[12];
616 struct ll *wordlist = NULL;
617 struct ll *curword = NULL;
618 bool deadlock = false;
626 fetch_key(keyid, &publickey, true);
629 * Walk through the uids removing the words from the worddb.
631 if (publickey != NULL) {
632 uids = keyuids(publickey, &primary);
635 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
636 wordlist = makewordlist(wordlist, uids[i]);
639 ret = worddb->cursor(worddb,
644 for (curword = wordlist; curword != NULL && !deadlock;
645 curword = curword->next) {
646 memset(&key, 0, sizeof(key));
647 memset(&data, 0, sizeof(data));
648 key.data = curword->object;
649 key.size = strlen(key.data);
650 data.data = worddb_data;
651 data.size = sizeof(worddb_data);
654 * Our data is the key creation time followed by the
657 worddb_data[ 0] = publickey->publickey->data[1];
658 worddb_data[ 1] = publickey->publickey->data[2];
659 worddb_data[ 2] = publickey->publickey->data[3];
660 worddb_data[ 3] = publickey->publickey->data[4];
661 worddb_data[ 4] = (keyid >> 56) & 0xFF;
662 worddb_data[ 5] = (keyid >> 48) & 0xFF;
663 worddb_data[ 6] = (keyid >> 40) & 0xFF;
664 worddb_data[ 7] = (keyid >> 32) & 0xFF;
665 worddb_data[ 8] = (keyid >> 24) & 0xFF;
666 worddb_data[ 9] = (keyid >> 16) & 0xFF;
667 worddb_data[10] = (keyid >> 8) & 0xFF;
668 worddb_data[11] = keyid & 0xFF;
670 ret = cursor->c_get(cursor,
676 ret = cursor->c_del(cursor, 0);
678 logthing(LOGTHING_ERROR,
679 "Problem deleting word: %s",
685 logthing(LOGTHING_ERROR,
686 "Problem deleting word: %s",
688 if (ret == DB_LOCK_DEADLOCK) {
693 ret = cursor->c_close(cursor);
697 * Free our UID and word lists.
699 llfree(wordlist, NULL);
700 for (i = 0; uids[i] != NULL; i++) {
706 free_publickey(publickey);
712 key.size = sizeof(keyid);
724 return deadlock ? (-1) : (ret == DB_NOTFOUND);
728 * dumpdb - dump the key database
729 * @filenamebase: The base filename to use for the dump.
731 * Dumps the database into one or more files, which contain pure OpenPGP
732 * that can be reimported into onak or gpg. filenamebase provides a base
733 * file name for the dump; several files may be created, all of which will
734 * begin with this string and then have a unique number and a .pgp
737 int dumpdb(char *filenamebase)
746 ret = dbconn->cursor(dbconn,
751 fd = open(filenamebase, O_CREAT | O_WRONLY | O_TRUNC, 0640);
752 memset(&key, 0, sizeof(key));
753 memset(&data, 0, sizeof(data));
754 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
756 write(fd, data.data, data.size);
757 memset(&key, 0, sizeof(key));
758 memset(&data, 0, sizeof(data));
759 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
761 logthing(LOGTHING_ERROR, "Problem reading key: %s", db_strerror(ret));
765 ret = cursor->c_close(cursor);
774 * Include the basic keydb routines.
776 #define NEED_GETFULLKEYID 1
777 #define NEED_GETKEYSIGS 1
778 #define NEED_KEYID2UID 1