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 * This is a bit of a kludge. Either we run a separate process for
122 * deadlock detection or we do this every time we run. What we really
123 * want to do is specify that our locks are exclusive locks when we
124 * start to do an update.
126 ret = lock_detect(dbenv,
129 NULL); /* If non null int* for number broken */
131 ret = dbenv->open(dbenv, config.db_dir,
132 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
137 dbenv->err(dbenv, ret, "%s", config.db_dir);
141 ret = db_create(&dbconn, dbenv, 0);
143 logthing(LOGTHING_CRITICAL,
144 "db_create: %s", db_strerror(ret));
148 ret = dbconn->open(dbconn, "keydb.db",
154 dbconn->err(dbconn, ret, "keydb.db");
158 ret = db_create(&worddb, dbenv, 0);
160 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
163 ret = worddb->set_flags(worddb, DB_DUP);
165 ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
169 worddb->err(worddb, ret, "worddb");
177 * cleanupdb - De-initialize the key database.
179 * This function should be called upon program exit to allow the DB to
180 * cleanup after itself.
184 worddb->close(worddb, 0);
186 dbconn->close(dbconn, 0);
188 dbenv->close(dbenv, 0);
193 * starttrans - Start a transaction.
195 * Start a transaction. Intended to be used if we're about to perform many
196 * operations on the database to help speed it all up, or if we want
197 * something to only succeed if all relevant operations are successful.
199 bool starttrans(void)
203 assert(dbenv != NULL);
206 ret = txn_begin(dbenv,
207 NULL, /* No parent transaction */
211 dbenv->err(dbenv, ret, "starttrans():");
219 * endtrans - End a transaction.
221 * Ends a transaction.
227 assert(dbenv != NULL);
230 ret = txn_commit(txn,
233 dbenv->err(dbenv, ret, "endtrans():");
242 * fetch_key - Given a keyid fetch the key from storage.
243 * @keyid: The keyid to fetch.
244 * @publickey: A pointer to a structure to return the key in.
245 * @intrans: If we're already in a transaction.
247 * We use the hex representation of the keyid as the filename to fetch the
248 * key from. The key is stored in the file as a binary OpenPGP stream of
249 * packets, so we can just use read_openpgp_stream() to read the packets
250 * in and then parse_keys() to parse the packets into a publickey
253 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
256 struct openpgp_packet_list *packets = NULL;
260 struct buffer_ctx fetchbuf;
262 memset(&key, 0, sizeof(key));
263 memset(&data, 0, sizeof(data));
268 key.size = sizeof(keyid);
276 ret = dbconn->get(dbconn,
283 fetchbuf.buffer = data.data;
285 fetchbuf.size = data.size;
286 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
288 parse_keys(packets, publickey);
289 free_packet_list(packets);
292 } else if (ret != DB_NOTFOUND) {
293 dbconn->err(dbconn, ret, "Problem retrieving key");
303 int worddb_cmp(const char *d1, const char *d2)
305 return memcmp(d1, d2, 12);
309 * fetch_key_text - Trys to find the keys that contain the supplied text.
310 * @search: The text to search for.
311 * @publickey: A pointer to a structure to return the key in.
313 * This function searches for the supplied text and returns the keys that
316 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
324 char *searchtext = NULL;
325 struct ll *wordlist = NULL;
326 struct ll *curword = NULL;
327 struct ll *keylist = NULL;
328 struct ll *newkeylist = NULL;
331 searchtext = strdup(search);
332 wordlist = makewordlist(wordlist, searchtext);
336 ret = worddb->cursor(worddb,
341 for (curword = wordlist; curword != NULL; curword = curword->next) {
342 memset(&key, 0, sizeof(key));
343 memset(&data, 0, sizeof(data));
344 key.data = curword->object;
345 key.size = strlen(curword->object);
346 data.flags = DB_DBT_MALLOC;
347 ret = cursor->c_get(cursor,
351 while (ret == 0 && strncmp(key.data, curword->object,
353 ((char *) curword->object)[key.size] == 0) {
355 for (i = 4; i < 12; i++) {
357 keyid += ((unsigned char *)
361 if (keylist == NULL ||
362 llfind(keylist, data.data,
363 worddb_cmp) != NULL) {
364 newkeylist = lladd(newkeylist, data.data);
370 ret = cursor->c_get(cursor,
375 llfree(keylist, free);
376 keylist = newkeylist;
378 if (data.data != NULL) {
383 llfree(wordlist, NULL);
386 for (newkeylist = keylist;
387 newkeylist != NULL && numkeys < config.maxkeys;
388 newkeylist = newkeylist->next) {
391 for (i = 4; i < 12; i++) {
393 keyid += ((unsigned char *)
394 newkeylist->object)[i];
397 numkeys += fetch_key(keyid,
401 llfree(keylist, free);
406 ret = cursor->c_close(cursor);
415 * store_key - Takes a key and stores it.
416 * @publickey: A pointer to the public key to store.
417 * @intrans: If we're already in a transaction.
418 * @update: If true the key exists and should be updated.
420 * Again we just use the hex representation of the keyid as the filename
421 * to store the key to. We flatten the public key to a list of OpenPGP
422 * packets and then use write_openpgp_stream() to write the stream out to
423 * the file. If update is true then we delete the old key first, otherwise
424 * we trust that it doesn't exist.
426 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
428 struct openpgp_packet_list *packets = NULL;
429 struct openpgp_packet_list *list_end = NULL;
430 struct openpgp_publickey *next = NULL;
433 struct buffer_ctx storebuf;
438 char *primary = NULL;
439 unsigned char worddb_data[12];
440 struct ll *wordlist = NULL;
441 struct ll *curword = NULL;
442 bool deadlock = false;
444 keyid = get_keyid(publickey);
451 * Delete the key if we already have it.
453 * TODO: Can we optimize this perhaps? Possibly when other data is
454 * involved as well? I suspect this is easiest and doesn't make a lot
455 * of difference though - the largest chunk of data is the keydata and
456 * it definitely needs updated.
459 deadlock = (delete_key(keyid, true) == -1);
463 * Convert the key to a flat set of binary data.
466 next = publickey->next;
467 publickey->next = NULL;
468 flatten_publickey(publickey, &packets, &list_end);
469 publickey->next = next;
472 storebuf.size = 8192;
473 storebuf.buffer = malloc(8192);
475 write_openpgp_stream(buffer_putchar, &storebuf, packets);
478 * Now we have the key data store it in the DB; the keyid is
481 memset(&key, 0, sizeof(key));
482 memset(&data, 0, sizeof(data));
484 key.size = sizeof(keyid);
486 data.size = storebuf.offset;
487 data.data = storebuf.buffer;
489 ret = dbconn->put(dbconn,
495 dbconn->err(dbconn, ret, "Problem storing key");
496 if (ret == DB_LOCK_DEADLOCK) {
501 free(storebuf.buffer);
502 storebuf.buffer = NULL;
506 free_packet_list(packets);
511 * Walk through our uids storing the words into the db with the keyid.
514 uids = keyuids(publickey, &primary);
517 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
518 wordlist = makewordlist(wordlist, uids[i]);
521 for (curword = wordlist; curword != NULL && !deadlock;
522 curword = curword->next) {
523 memset(&key, 0, sizeof(key));
524 memset(&data, 0, sizeof(data));
525 key.data = curword->object;
526 key.size = strlen(key.data);
527 data.data = worddb_data;
528 data.size = sizeof(worddb_data);
531 * Our data is the key creation time followed by the
534 worddb_data[ 0] = publickey->publickey->data[1];
535 worddb_data[ 1] = publickey->publickey->data[2];
536 worddb_data[ 2] = publickey->publickey->data[3];
537 worddb_data[ 3] = publickey->publickey->data[4];
538 worddb_data[ 4] = (keyid >> 56) & 0xFF;
539 worddb_data[ 5] = (keyid >> 48) & 0xFF;
540 worddb_data[ 6] = (keyid >> 40) & 0xFF;
541 worddb_data[ 7] = (keyid >> 32) & 0xFF;
542 worddb_data[ 8] = (keyid >> 24) & 0xFF;
543 worddb_data[ 9] = (keyid >> 16) & 0xFF;
544 worddb_data[10] = (keyid >> 8) & 0xFF;
545 worddb_data[11] = keyid & 0xFF;
546 ret = worddb->put(worddb,
552 worddb->err(worddb, ret,
553 "Problem storing key");
554 if (ret == DB_LOCK_DEADLOCK) {
561 * Free our UID and word lists.
563 llfree(wordlist, NULL);
564 for (i = 0; uids[i] != NULL; i++) {
576 return deadlock ? -1 : 0 ;
580 * delete_key - Given a keyid delete the key from storage.
581 * @keyid: The keyid to delete.
582 * @intrans: If we're already in a transaction.
584 * This function deletes a public key from whatever storage mechanism we
585 * are using. Returns 0 if the key existed.
587 int delete_key(uint64_t keyid, bool intrans)
589 struct openpgp_publickey *publickey = NULL;
595 char *primary = NULL;
596 unsigned char worddb_data[12];
597 struct ll *wordlist = NULL;
598 struct ll *curword = NULL;
599 bool deadlock = false;
607 fetch_key(keyid, &publickey, true);
610 * Walk through the uids removing the words from the worddb.
612 if (publickey != NULL) {
613 uids = keyuids(publickey, &primary);
616 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
617 wordlist = makewordlist(wordlist, uids[i]);
620 ret = worddb->cursor(worddb,
625 for (curword = wordlist; curword != NULL && !deadlock;
626 curword = curword->next) {
627 memset(&key, 0, sizeof(key));
628 memset(&data, 0, sizeof(data));
629 key.data = curword->object;
630 key.size = strlen(key.data);
631 data.data = worddb_data;
632 data.size = sizeof(worddb_data);
635 * Our data is the key creation time followed by the
638 worddb_data[ 0] = publickey->publickey->data[1];
639 worddb_data[ 1] = publickey->publickey->data[2];
640 worddb_data[ 2] = publickey->publickey->data[3];
641 worddb_data[ 3] = publickey->publickey->data[4];
642 worddb_data[ 4] = (keyid >> 56) & 0xFF;
643 worddb_data[ 5] = (keyid >> 48) & 0xFF;
644 worddb_data[ 6] = (keyid >> 40) & 0xFF;
645 worddb_data[ 7] = (keyid >> 32) & 0xFF;
646 worddb_data[ 8] = (keyid >> 24) & 0xFF;
647 worddb_data[ 9] = (keyid >> 16) & 0xFF;
648 worddb_data[10] = (keyid >> 8) & 0xFF;
649 worddb_data[11] = keyid & 0xFF;
651 ret = cursor->c_get(cursor,
657 ret = cursor->c_del(cursor, 0);
659 worddb->err(worddb, ret,
660 "Problem deleting word.");
665 worddb->err(worddb, ret,
666 "Problem deleting word.");
667 if (ret == DB_LOCK_DEADLOCK) {
672 ret = cursor->c_close(cursor);
676 * Free our UID and word lists.
678 llfree(wordlist, NULL);
679 for (i = 0; uids[i] != NULL; i++) {
685 free_publickey(publickey);
691 key.size = sizeof(keyid);
703 return deadlock ? (-1) : (ret == DB_NOTFOUND);
707 * dumpdb - dump the key database
708 * @filenamebase: The base filename to use for the dump.
710 * Dumps the database into one or more files, which contain pure OpenPGP
711 * that can be reimported into onak or gpg. filenamebase provides a base
712 * file name for the dump; several files may be created, all of which will
713 * begin with this string and then have a unique number and a .pgp
716 int dumpdb(char *filenamebase)
725 ret = dbconn->cursor(dbconn,
730 fd = open(filenamebase, O_CREAT | O_WRONLY | O_TRUNC, 0640);
731 memset(&key, 0, sizeof(key));
732 memset(&data, 0, sizeof(data));
733 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
735 write(fd, data.data, data.size);
736 memset(&key, 0, sizeof(key));
737 memset(&data, 0, sizeof(data));
738 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
740 dbconn->err(dbconn, ret, "Problem reading key");
744 ret = cursor->c_close(cursor);
753 * Include the basic keydb routines.
755 #define NEED_GETFULLKEYID 1
756 #define NEED_GETKEYSIGS 1
757 #define NEED_KEYID2UID 1