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"
28 #include "onak-conf.h"
32 * dbenv - our database environment.
34 static DB_ENV *dbenv = NULL;
37 * dbconn - our connection to the key database.
39 static DB *dbconn = NULL;
42 * worddb - our connection to the word database.
44 static DB *worddb = NULL;
47 * txn - our current transaction id.
49 static DB_TXN *txn = NULL;
52 * makewordlist - Takes a string and splits it into a set of unique words.
53 * @wordlist: The current word list.
54 * @words: The string to split and add.
56 * We take words and split it on non alpha numeric characters. These get
57 * added to the word list if they're not already present. If the wordlist
58 * is NULL then we start a new list, otherwise it's search for already
59 * added words. Note that words is modified in the process of scanning.
61 * Returns the new word list.
63 struct ll *makewordlist(struct ll *wordlist, char *word)
69 * Walk through the words string, spliting on non alphanumerics and
70 * then checking if the word already exists in the list. If not then
74 while (end != NULL && *end != 0) {
76 while (*start != 0 && !isalnum(*start)) {
80 while (*end != 0 && isalnum(*end)) {
84 if (end - start > 1) {
90 if (llfind(wordlist, start,
92 wordlist = lladd(wordlist,
102 * initdb - Initialize the key database.
104 * This function should be called before any of the other functions in
105 * this file are called in order to allow the DB to be initialized ready
112 ret = db_env_create(&dbenv, 0);
114 fprintf(stderr, "db_env_create: %s\n", db_strerror(ret));
119 * This is a bit of a kludge. Either we run a separate process for
120 * deadlock detection or we do this every time we run. What we really
121 * want to do is specify that our locks are exclusive locks when we
122 * start to do an update.
124 ret = lock_detect(dbenv,
127 NULL); /* If non null int* for number broken */
129 ret = dbenv->open(dbenv, config.db_dir,
130 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
135 dbenv->err(dbenv, ret, "%s", config.db_dir);
139 ret = db_create(&dbconn, dbenv, 0);
141 fprintf(stderr, "db_create: %s\n", db_strerror(ret));
145 ret = dbconn->open(dbconn, "keydb.db",
151 dbconn->err(dbconn, ret, "keydb.db");
155 ret = db_create(&worddb, dbenv, 0);
157 fprintf(stderr, "db_create: %s\n", db_strerror(ret));
160 ret = worddb->set_flags(worddb, DB_DUP);
162 ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
166 worddb->err(worddb, ret, "worddb");
174 * cleanupdb - De-initialize the key database.
176 * This function should be called upon program exit to allow the DB to
177 * cleanup after itself.
181 worddb->close(worddb, 0);
183 dbconn->close(dbconn, 0);
185 dbenv->close(dbenv, 0);
190 * starttrans - Start a transaction.
192 * Start a transaction. Intended to be used if we're about to perform many
193 * operations on the database to help speed it all up, or if we want
194 * something to only succeed if all relevant operations are successful.
196 bool starttrans(void)
200 assert(dbenv != NULL);
203 ret = txn_begin(dbenv,
204 NULL, /* No parent transaction */
208 dbenv->err(dbenv, ret, "starttrans():");
216 * endtrans - End a transaction.
218 * Ends a transaction.
224 assert(dbenv != NULL);
227 ret = txn_commit(txn,
230 dbenv->err(dbenv, ret, "endtrans():");
239 * fetch_key - Given a keyid fetch the key from storage.
240 * @keyid: The keyid to fetch.
241 * @publickey: A pointer to a structure to return the key in.
242 * @intrans: If we're already in a transaction.
244 * We use the hex representation of the keyid as the filename to fetch the
245 * key from. The key is stored in the file as a binary OpenPGP stream of
246 * packets, so we can just use read_openpgp_stream() to read the packets
247 * in and then parse_keys() to parse the packets into a publickey
250 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
253 struct openpgp_packet_list *packets = NULL;
257 struct buffer_ctx fetchbuf;
259 memset(&key, 0, sizeof(key));
260 memset(&data, 0, sizeof(data));
265 key.size = sizeof(keyid);
273 ret = dbconn->get(dbconn,
280 fetchbuf.buffer = data.data;
282 fetchbuf.size = data.size;
283 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
285 parse_keys(packets, publickey);
286 free_packet_list(packets);
289 } else if (ret != DB_NOTFOUND) {
290 dbconn->err(dbconn, ret, "Problem retrieving key");
300 int worddb_cmp(const char *d1, const char *d2)
302 return memcmp(d1, d2, 12);
306 * fetch_key_text - Trys to find the keys that contain the supplied text.
307 * @search: The text to search for.
308 * @publickey: A pointer to a structure to return the key in.
310 * This function searches for the supplied text and returns the keys that
313 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
321 char *searchtext = NULL;
322 struct ll *wordlist = NULL;
323 struct ll *curword = NULL;
324 struct ll *keylist = NULL;
325 struct ll *newkeylist = NULL;
328 searchtext = strdup(search);
329 wordlist = makewordlist(wordlist, searchtext);
333 ret = worddb->cursor(worddb,
338 for (curword = wordlist; curword != NULL; curword = curword->next) {
339 memset(&key, 0, sizeof(key));
340 memset(&data, 0, sizeof(data));
341 key.data = curword->object;
342 key.size = strlen(curword->object);
343 data.flags = DB_DBT_MALLOC;
344 ret = cursor->c_get(cursor,
348 while (ret == 0 && strncmp(key.data, curword->object,
350 ((char *) curword->object)[key.size] == 0) {
352 for (i = 4; i < 12; i++) {
354 keyid += ((unsigned char *)
358 if (keylist == NULL ||
359 llfind(keylist, data.data,
360 worddb_cmp) != NULL) {
361 newkeylist = lladd(newkeylist, data.data);
367 ret = cursor->c_get(cursor,
372 llfree(keylist, free);
373 keylist = newkeylist;
375 if (data.data != NULL) {
380 llfree(wordlist, NULL);
383 for (newkeylist = keylist;
384 newkeylist != NULL && numkeys < config.maxkeys;
385 newkeylist = newkeylist->next) {
388 for (i = 4; i < 12; i++) {
390 keyid += ((unsigned char *)
391 newkeylist->object)[i];
394 numkeys += fetch_key(keyid,
398 llfree(keylist, free);
403 ret = cursor->c_close(cursor);
412 * store_key - Takes a key and stores it.
413 * @publickey: A pointer to the public key to store.
414 * @intrans: If we're already in a transaction.
415 * @update: If true the key exists and should be updated.
417 * Again we just use the hex representation of the keyid as the filename
418 * to store the key to. We flatten the public key to a list of OpenPGP
419 * packets and then use write_openpgp_stream() to write the stream out to
420 * the file. If update is true then we delete the old key first, otherwise
421 * we trust that it doesn't exist.
423 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
425 struct openpgp_packet_list *packets = NULL;
426 struct openpgp_packet_list *list_end = NULL;
427 struct openpgp_publickey *next = NULL;
430 struct buffer_ctx storebuf;
435 char *primary = NULL;
436 unsigned char worddb_data[12];
437 struct ll *wordlist = NULL;
438 struct ll *curword = NULL;
439 bool deadlock = false;
441 keyid = get_keyid(publickey);
448 * Delete the key if we already have it.
450 * TODO: Can we optimize this perhaps? Possibly when other data is
451 * involved as well? I suspect this is easiest and doesn't make a lot
452 * of difference though - the largest chunk of data is the keydata and
453 * it definitely needs updated.
456 deadlock = (delete_key(keyid, true) == -1);
460 * Convert the key to a flat set of binary data.
463 next = publickey->next;
464 publickey->next = NULL;
465 flatten_publickey(publickey, &packets, &list_end);
466 publickey->next = next;
469 storebuf.size = 8192;
470 storebuf.buffer = malloc(8192);
472 write_openpgp_stream(buffer_putchar, &storebuf, packets);
475 * Now we have the key data store it in the DB; the keyid is
478 memset(&key, 0, sizeof(key));
479 memset(&data, 0, sizeof(data));
481 key.size = sizeof(keyid);
483 data.size = storebuf.offset;
484 data.data = storebuf.buffer;
486 ret = dbconn->put(dbconn,
492 dbconn->err(dbconn, ret, "Problem storing key");
493 if (ret == DB_LOCK_DEADLOCK) {
498 free(storebuf.buffer);
499 storebuf.buffer = NULL;
503 free_packet_list(packets);
508 * Walk through our uids storing the words into the db with the keyid.
511 uids = keyuids(publickey, &primary);
514 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
515 wordlist = makewordlist(wordlist, uids[i]);
518 for (curword = wordlist; curword != NULL && !deadlock;
519 curword = curword->next) {
520 memset(&key, 0, sizeof(key));
521 memset(&data, 0, sizeof(data));
522 key.data = curword->object;
523 key.size = strlen(key.data);
524 data.data = worddb_data;
525 data.size = sizeof(worddb_data);
528 * Our data is the key creation time followed by the
531 worddb_data[ 0] = publickey->publickey->data[1];
532 worddb_data[ 1] = publickey->publickey->data[2];
533 worddb_data[ 2] = publickey->publickey->data[3];
534 worddb_data[ 3] = publickey->publickey->data[4];
535 worddb_data[ 4] = (keyid >> 56) & 0xFF;
536 worddb_data[ 5] = (keyid >> 48) & 0xFF;
537 worddb_data[ 6] = (keyid >> 40) & 0xFF;
538 worddb_data[ 7] = (keyid >> 32) & 0xFF;
539 worddb_data[ 8] = (keyid >> 24) & 0xFF;
540 worddb_data[ 9] = (keyid >> 16) & 0xFF;
541 worddb_data[10] = (keyid >> 8) & 0xFF;
542 worddb_data[11] = keyid & 0xFF;
543 ret = worddb->put(worddb,
549 worddb->err(worddb, ret,
550 "Problem storing key");
551 if (ret == DB_LOCK_DEADLOCK) {
558 * Free our UID and word lists.
560 llfree(wordlist, NULL);
561 for (i = 0; uids[i] != NULL; i++) {
573 return deadlock ? -1 : 0 ;
577 * delete_key - Given a keyid delete the key from storage.
578 * @keyid: The keyid to delete.
579 * @intrans: If we're already in a transaction.
581 * This function deletes a public key from whatever storage mechanism we
582 * are using. Returns 0 if the key existed.
584 int delete_key(uint64_t keyid, bool intrans)
586 struct openpgp_publickey *publickey = NULL;
592 char *primary = NULL;
593 unsigned char worddb_data[12];
594 struct ll *wordlist = NULL;
595 struct ll *curword = NULL;
596 bool deadlock = false;
604 fetch_key(keyid, &publickey, true);
607 * Walk through the uids removing the words from the worddb.
609 if (publickey != NULL) {
610 uids = keyuids(publickey, &primary);
613 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
614 wordlist = makewordlist(wordlist, uids[i]);
617 ret = worddb->cursor(worddb,
622 for (curword = wordlist; curword != NULL && !deadlock;
623 curword = curword->next) {
624 memset(&key, 0, sizeof(key));
625 memset(&data, 0, sizeof(data));
626 key.data = curword->object;
627 key.size = strlen(key.data);
628 data.data = worddb_data;
629 data.size = sizeof(worddb_data);
632 * Our data is the key creation time followed by the
635 worddb_data[ 0] = publickey->publickey->data[1];
636 worddb_data[ 1] = publickey->publickey->data[2];
637 worddb_data[ 2] = publickey->publickey->data[3];
638 worddb_data[ 3] = publickey->publickey->data[4];
639 worddb_data[ 4] = (keyid >> 56) & 0xFF;
640 worddb_data[ 5] = (keyid >> 48) & 0xFF;
641 worddb_data[ 6] = (keyid >> 40) & 0xFF;
642 worddb_data[ 7] = (keyid >> 32) & 0xFF;
643 worddb_data[ 8] = (keyid >> 24) & 0xFF;
644 worddb_data[ 9] = (keyid >> 16) & 0xFF;
645 worddb_data[10] = (keyid >> 8) & 0xFF;
646 worddb_data[11] = keyid & 0xFF;
648 ret = cursor->c_get(cursor,
654 ret = cursor->c_del(cursor, 0);
656 worddb->err(worddb, ret,
657 "Problem deleting word.");
662 worddb->err(worddb, ret,
663 "Problem deleting word.");
664 if (ret == DB_LOCK_DEADLOCK) {
669 ret = cursor->c_close(cursor);
673 * Free our UID and word lists.
675 llfree(wordlist, NULL);
676 for (i = 0; uids[i] != NULL; i++) {
682 free_publickey(publickey);
688 key.size = sizeof(keyid);
700 return deadlock ? (-1) : (ret == DB_NOTFOUND);
704 * dumpdb - dump the key database
705 * @filenamebase: The base filename to use for the dump.
707 * Dumps the database into one or more files, which contain pure OpenPGP
708 * that can be reimported into onak or gpg. filenamebase provides a base
709 * file name for the dump; several files may be created, all of which will
710 * begin with this string and then have a unique number and a .pgp
713 int dumpdb(char *filenamebase)
722 ret = dbconn->cursor(dbconn,
727 fd = open(filenamebase, O_CREAT | O_WRONLY | O_TRUNC, 0640);
728 memset(&key, 0, sizeof(key));
729 memset(&data, 0, sizeof(data));
730 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
732 write(fd, data.data, data.size);
733 memset(&key, 0, sizeof(key));
734 memset(&data, 0, sizeof(data));
735 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
737 dbconn->err(dbconn, ret, "Problem reading key");
741 ret = cursor->c_close(cursor);
750 * Include the basic keydb routines.
752 #define NEED_GETFULLKEYID 1
753 #define NEED_GETKEYSIGS 1
754 #define NEED_KEYID2UID 1