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
113 ret = db_env_create(&dbenv, 0);
115 fprintf(stderr, "db_env_create: %s\n", db_strerror(ret));
119 ret = dbenv->open(dbenv, config.db_dir,
120 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
122 DB_RECOVER | DB_CREATE,
125 dbenv->err(dbenv, ret, "%s", config.db_dir);
129 ret = db_create(&dbconn, dbenv, 0);
131 fprintf(stderr, "db_create: %s\n", db_strerror(ret));
135 ret = dbconn->open(dbconn, "keydb.db",
141 dbconn->err(dbconn, ret, "keydb.db");
145 ret = db_create(&worddb, dbenv, 0);
147 fprintf(stderr, "db_create: %s\n", db_strerror(ret));
150 ret = worddb->set_flags(worddb, DB_DUP);
152 ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
156 worddb->err(worddb, ret, "worddb");
164 * cleanupdb - De-initialize the key database.
166 * This function should be called upon program exit to allow the DB to
167 * cleanup after itself.
171 worddb->close(worddb, 0);
173 dbconn->close(dbconn, 0);
175 dbenv->close(dbenv, 0);
180 * starttrans - Start a transaction.
182 * Start a transaction. Intended to be used if we're about to perform many
183 * operations on the database to help speed it all up, or if we want
184 * something to only succeed if all relevant operations are successful.
186 bool starttrans(void)
192 ret = txn_begin(dbenv,
193 NULL, /* No parent transaction */
197 dbenv->err(dbenv, ret, "starttrans():");
205 * endtrans - End a transaction.
207 * Ends a transaction.
215 ret = txn_commit(txn,
218 dbenv->err(dbenv, ret, "endtrans():");
227 * fetch_key - Given a keyid fetch the key from storage.
228 * @keyid: The keyid to fetch.
229 * @publickey: A pointer to a structure to return the key in.
230 * @intrans: If we're already in a transaction.
232 * We use the hex representation of the keyid as the filename to fetch the
233 * key from. The key is stored in the file as a binary OpenPGP stream of
234 * packets, so we can just use read_openpgp_stream() to read the packets
235 * in and then parse_keys() to parse the packets into a publickey
238 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
241 struct openpgp_packet_list *packets = NULL;
245 struct buffer_ctx fetchbuf;
247 memset(&key, 0, sizeof(key));
248 memset(&data, 0, sizeof(data));
253 key.size = sizeof(keyid);
261 ret = dbconn->get(dbconn,
268 fetchbuf.buffer = data.data;
270 fetchbuf.size = data.size;
271 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
273 parse_keys(packets, publickey);
274 free_packet_list(packets);
277 } else if (ret != DB_NOTFOUND) {
278 dbconn->err(dbconn, ret, "Problem retrieving key");
288 int worddb_cmp(const char *d1, const char *d2)
290 return memcmp(d1, d2, 12);
294 * fetch_key_text - Trys to find the keys that contain the supplied text.
295 * @search: The text to search for.
296 * @publickey: A pointer to a structure to return the key in.
298 * This function searches for the supplied text and returns the keys that
301 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
309 char *searchtext = NULL;
310 struct ll *wordlist = NULL;
311 struct ll *curword = NULL;
312 struct ll *keylist = NULL;
313 struct ll *newkeylist = NULL;
316 searchtext = strdup(search);
317 wordlist = makewordlist(wordlist, searchtext);
321 ret = worddb->cursor(worddb,
326 for (curword = wordlist; curword != NULL; curword = curword->next) {
327 memset(&key, 0, sizeof(key));
328 memset(&data, 0, sizeof(data));
329 key.data = curword->object;
330 key.size = strlen(curword->object);
331 data.flags = DB_DBT_MALLOC;
332 ret = cursor->c_get(cursor,
336 while (ret == 0 && strncmp(key.data, curword->object,
338 ((char *) curword->object)[key.size] == 0) {
340 for (i = 4; i < 12; i++) {
342 keyid += ((unsigned char *)
346 if (keylist == NULL ||
347 llfind(keylist, data.data,
348 worddb_cmp) != NULL) {
349 newkeylist = lladd(newkeylist, data.data);
355 ret = cursor->c_get(cursor,
360 llfree(keylist, free);
361 keylist = newkeylist;
363 if (data.data != NULL) {
368 llfree(wordlist, NULL);
371 for (newkeylist = keylist;
372 newkeylist != NULL && numkeys < config.maxkeys;
373 newkeylist = newkeylist->next) {
376 for (i = 4; i < 12; i++) {
378 keyid += ((unsigned char *)
379 newkeylist->object)[i];
382 numkeys += fetch_key(keyid,
386 llfree(keylist, free);
391 ret = cursor->c_close(cursor);
400 * store_key - Takes a key and stores it.
401 * @publickey: A pointer to the public key to store.
402 * @intrans: If we're already in a transaction.
403 * @update: If true the key exists and should be updated.
405 * Again we just use the hex representation of the keyid as the filename
406 * to store the key to. We flatten the public key to a list of OpenPGP
407 * packets and then use write_openpgp_stream() to write the stream out to
408 * the file. If update is true then we delete the old key first, otherwise
409 * we trust that it doesn't exist.
411 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
413 struct openpgp_packet_list *packets = NULL;
414 struct openpgp_packet_list *list_end = NULL;
415 struct openpgp_publickey *next = NULL;
418 struct buffer_ctx storebuf;
423 char *primary = NULL;
424 unsigned char worddb_data[12];
425 struct ll *wordlist = NULL;
426 struct ll *curword = NULL;
428 keyid = get_keyid(publickey);
435 * Delete the key if we already have it.
437 * TODO: Can we optimize this perhaps? Possibly when other data is
438 * involved as well? I suspect this is easiest and doesn't make a lot
439 * of difference though - the largest chunk of data is the keydata and
440 * it definitely needs updated.
443 delete_key(keyid, true);
447 * Convert the key to a flat set of binary data.
449 next = publickey->next;
450 publickey->next = NULL;
451 flatten_publickey(publickey, &packets, &list_end);
452 publickey->next = next;
455 storebuf.size = 8192;
456 storebuf.buffer = malloc(8192);
458 write_openpgp_stream(buffer_putchar, &storebuf, packets);
461 * Now we have the key data store it in the DB; the keyid is the key.
463 memset(&key, 0, sizeof(key));
464 memset(&data, 0, sizeof(data));
466 key.size = sizeof(keyid);
468 data.size = storebuf.offset;
469 data.data = storebuf.buffer;
471 ret = dbconn->put(dbconn,
477 dbconn->err(dbconn, ret, "Problem storing key");
480 free(storebuf.buffer);
481 storebuf.buffer = NULL;
485 free_packet_list(packets);
489 * Walk through our uids storing the words into the db with the keyid.
491 uids = keyuids(publickey, &primary);
493 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
494 wordlist = makewordlist(wordlist, uids[i]);
497 for (curword = wordlist; curword != NULL;
498 curword = curword->next) {
499 memset(&key, 0, sizeof(key));
500 memset(&data, 0, sizeof(data));
501 key.data = curword->object;
502 key.size = strlen(key.data);
503 data.data = worddb_data;
504 data.size = sizeof(worddb_data);
507 * Our data is the key creation time followed by the
510 worddb_data[ 0] = publickey->publickey->data[1];
511 worddb_data[ 1] = publickey->publickey->data[2];
512 worddb_data[ 2] = publickey->publickey->data[3];
513 worddb_data[ 3] = publickey->publickey->data[4];
514 worddb_data[ 4] = (keyid >> 56) & 0xFF;
515 worddb_data[ 5] = (keyid >> 48) & 0xFF;
516 worddb_data[ 6] = (keyid >> 40) & 0xFF;
517 worddb_data[ 7] = (keyid >> 32) & 0xFF;
518 worddb_data[ 8] = (keyid >> 24) & 0xFF;
519 worddb_data[ 9] = (keyid >> 16) & 0xFF;
520 worddb_data[10] = (keyid >> 8) & 0xFF;
521 worddb_data[11] = keyid & 0xFF;
522 ret = worddb->put(worddb,
528 worddb->err(worddb, ret,
529 "Problem storing key");
534 * Free our UID and word lists.
536 llfree(wordlist, NULL);
537 for (i = 0; uids[i] != NULL; i++) {
553 * delete_key - Given a keyid delete the key from storage.
554 * @keyid: The keyid to delete.
555 * @intrans: If we're already in a transaction.
557 * This function deletes a public key from whatever storage mechanism we
558 * are using. Returns 0 if the key existed.
560 int delete_key(uint64_t keyid, bool intrans)
562 struct openpgp_publickey *publickey = NULL;
568 char *primary = NULL;
569 unsigned char worddb_data[12];
570 struct ll *wordlist = NULL;
571 struct ll *curword = NULL;
579 fetch_key(keyid, &publickey, true);
582 * Walk through the uids removing the words from the worddb.
584 if (publickey != NULL) {
585 uids = keyuids(publickey, &primary);
588 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
589 wordlist = makewordlist(wordlist, uids[i]);
592 ret = worddb->cursor(worddb,
597 for (curword = wordlist; curword != NULL;
598 curword = curword->next) {
599 memset(&key, 0, sizeof(key));
600 memset(&data, 0, sizeof(data));
601 key.data = curword->object;
602 key.size = strlen(key.data);
603 data.data = worddb_data;
604 data.size = sizeof(worddb_data);
607 * Our data is the key creation time followed by the
610 worddb_data[ 0] = publickey->publickey->data[1];
611 worddb_data[ 1] = publickey->publickey->data[2];
612 worddb_data[ 2] = publickey->publickey->data[3];
613 worddb_data[ 3] = publickey->publickey->data[4];
614 worddb_data[ 4] = (keyid >> 56) & 0xFF;
615 worddb_data[ 5] = (keyid >> 48) & 0xFF;
616 worddb_data[ 6] = (keyid >> 40) & 0xFF;
617 worddb_data[ 7] = (keyid >> 32) & 0xFF;
618 worddb_data[ 8] = (keyid >> 24) & 0xFF;
619 worddb_data[ 9] = (keyid >> 16) & 0xFF;
620 worddb_data[10] = (keyid >> 8) & 0xFF;
621 worddb_data[11] = keyid & 0xFF;
623 ret = cursor->c_get(cursor,
629 ret = cursor->c_del(cursor, 0);
631 worddb->err(worddb, ret,
632 "Problem deleting word.");
637 worddb->err(worddb, ret,
638 "Problem deleting word.");
641 ret = cursor->c_close(cursor);
645 * Free our UID and word lists.
647 llfree(wordlist, NULL);
648 for (i = 0; uids[i] != NULL; i++) {
654 free_publickey(publickey);
659 key.size = sizeof(keyid);
670 return (ret == DB_NOTFOUND);
674 * Include the basic keydb routines.
676 #define NEED_GETFULLKEYID 1
677 #define NEED_GETKEYSIGS 1
678 #define NEED_KEYID2UID 1