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
21 #include "charfuncs.h"
25 #include "keystructs.h"
27 #include "onak-conf.h"
31 * dbconn - our connection to the key database.
33 static DB *dbconn = NULL;
36 * worddb - our connection to the word database.
38 static DB *worddb = NULL;
41 * makewordlist - Takes a string and splits it into a set of unique words.
42 * @wordlist: The current word list.
43 * @words: The string to split and add.
45 * We take words and split it on non alpha numeric characters. These get
46 * added to the word list if they're not already present. If the wordlist
47 * is NULL then we start a new list, otherwise it's search for already
48 * added words. Note that words is modified in the process of scanning.
50 * Returns the new word list.
52 struct ll *makewordlist(struct ll *wordlist, char *word)
58 * Walk through the words string, spliting on non alphanumerics and
59 * then checking if the word already exists in the list. If not then
63 while (end != NULL && *end != 0) {
65 while (*start != 0 && !isalnum(*start)) {
69 while (*end != 0 && isalnum(*end)) {
73 if (end - start > 1) {
79 if (llfind(wordlist, start,
81 wordlist = lladd(wordlist,
91 * initdb - Initialize the key database.
93 * This function should be called before any of the other functions in
94 * this file are called in order to allow the DB to be initialized ready
102 strcpy(buf, config.db_dir);
103 strcat(buf, "/keydb.db");
105 ret = db_create(&dbconn, NULL, 0);
107 fprintf(stderr, "db_create: %s\n", db_strerror(ret));
111 ret = dbconn->open(dbconn, buf, NULL, DB_HASH,
115 dbconn->err(dbconn, ret, "%s", buf);
119 strcpy(buf, config.db_dir);
120 strcat(buf, "/worddb");
122 ret = db_create(&worddb, NULL, 0);
124 fprintf(stderr, "db_create: %s\n", db_strerror(ret));
127 ret = worddb->set_flags(worddb, DB_DUP);
129 ret = worddb->open(worddb, buf, NULL, DB_BTREE,
133 worddb->err(worddb, ret, "%s", buf);
141 * cleanupdb - De-initialize the key database.
143 * This function should be called upon program exit to allow the DB to
144 * cleanup after itself.
148 worddb->close(worddb, 0);
150 dbconn->close(dbconn, 0);
155 * starttrans - Start a transaction.
157 * Start a transaction. Intended to be used if we're about to perform many
158 * operations on the database to help speed it all up, or if we want
159 * something to only succeed if all relevant operations are successful.
161 bool starttrans(void)
167 * endtrans - End a transaction.
169 * Ends a transaction.
177 * fetch_key - Given a keyid fetch the key from storage.
178 * @keyid: The keyid to fetch.
179 * @publickey: A pointer to a structure to return the key in.
180 * @intrans: If we're already in a transaction.
182 * We use the hex representation of the keyid as the filename to fetch the
183 * key from. The key is stored in the file as a binary OpenPGP stream of
184 * packets, so we can just use read_openpgp_stream() to read the packets
185 * in and then parse_keys() to parse the packets into a publickey
188 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
191 struct openpgp_packet_list *packets = NULL;
195 struct buffer_ctx fetchbuf;
197 memset(&key, 0, sizeof(key));
198 memset(&data, 0, sizeof(data));
203 key.size = sizeof(keyid);
207 ret = dbconn->get(dbconn,
214 fetchbuf.buffer = data.data;
216 fetchbuf.size = data.size;
217 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
219 parse_keys(packets, publickey);
220 free_packet_list(packets);
223 } else if (ret != DB_NOTFOUND) {
224 dbconn->err(dbconn, ret, "Problem retrieving key");
230 int worddb_cmp(const char *d1, const char *d2)
232 return memcmp(d1, d2, 12);
236 * fetch_key_text - Trys to find the keys that contain the supplied text.
237 * @search: The text to search for.
238 * @publickey: A pointer to a structure to return the key in.
240 * This function searches for the supplied text and returns the keys that
243 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
251 char *searchtext = NULL;
252 struct ll *wordlist = NULL;
253 struct ll *curword = NULL;
254 struct ll *keylist = NULL;
255 struct ll *newkeylist = NULL;
258 searchtext = strdup(search);
259 wordlist = makewordlist(wordlist, searchtext);
262 ret = worddb->cursor(worddb,
267 for (curword = wordlist; curword != NULL; curword = curword->next) {
268 memset(&key, 0, sizeof(key));
269 memset(&data, 0, sizeof(data));
270 key.data = curword->object;
271 key.size = strlen(curword->object);
272 data.flags = DB_DBT_MALLOC;
273 ret = cursor->c_get(cursor,
277 while (ret == 0 && strncmp(key.data, curword->object,
279 ((char *) curword->object)[key.size] == 0) {
281 for (i = 4; i < 12; i++) {
283 keyid += ((unsigned char *)
287 if (keylist == NULL ||
288 llfind(keylist, data.data,
289 worddb_cmp) != NULL) {
290 newkeylist = lladd(newkeylist, data.data);
296 ret = cursor->c_get(cursor,
301 llfree(keylist, free);
302 keylist = newkeylist;
304 if (data.data != NULL) {
309 llfree(wordlist, NULL);
312 for (newkeylist = keylist; newkeylist != NULL;
313 newkeylist = newkeylist->next) {
316 for (i = 4; i < 12; i++) {
318 keyid += ((unsigned char *)
319 newkeylist->object)[i];
322 numkeys += fetch_key(keyid,
326 llfree(keylist, free);
331 ret = cursor->c_close(cursor);
338 * store_key - Takes a key and stores it.
339 * @publickey: A pointer to the public key to store.
340 * @intrans: If we're already in a transaction.
341 * @update: If true the key exists and should be updated.
343 * Again we just use the hex representation of the keyid as the filename
344 * to store the key to. We flatten the public key to a list of OpenPGP
345 * packets and then use write_openpgp_stream() to write the stream out to
346 * the file. If update is true then we delete the old key first, otherwise
347 * we trust that it doesn't exist.
349 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
351 struct openpgp_packet_list *packets = NULL;
352 struct openpgp_packet_list *list_end = NULL;
353 struct openpgp_publickey *next = NULL;
356 struct buffer_ctx storebuf;
361 char *primary = NULL;
362 unsigned char worddb_data[12];
363 struct ll *wordlist = NULL;
364 struct ll *curword = NULL;
366 keyid = get_keyid(publickey);
369 * Delete the key if we already have it.
371 * TODO: Can we optimize this perhaps? Possibly when other data is
372 * involved as well? I suspect this is easiest and doesn't make a lot
373 * of difference though - the largest chunk of data is the keydata and
374 * it definitely needs updated.
377 delete_key(keyid, true);
381 * Convert the key to a flat set of binary data.
383 next = publickey->next;
384 publickey->next = NULL;
385 flatten_publickey(publickey, &packets, &list_end);
386 publickey->next = next;
389 storebuf.size = 8192;
390 storebuf.buffer = malloc(8192);
392 write_openpgp_stream(buffer_putchar, &storebuf, packets);
395 * Now we have the key data store it in the DB; the keyid is the key.
397 memset(&key, 0, sizeof(key));
398 memset(&data, 0, sizeof(data));
400 key.size = sizeof(keyid);
402 data.size = storebuf.offset;
403 data.data = storebuf.buffer;
405 ret = dbconn->put(dbconn,
411 dbconn->err(dbconn, ret, "Problem storing key");
414 free(storebuf.buffer);
415 storebuf.buffer = NULL;
419 free_packet_list(packets);
423 * Walk through our uids storing the words into the db with the keyid.
425 uids = keyuids(publickey, &primary);
427 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
428 wordlist = makewordlist(wordlist, uids[i]);
431 for (curword = wordlist; curword != NULL;
432 curword = curword->next) {
433 memset(&key, 0, sizeof(key));
434 memset(&data, 0, sizeof(data));
435 key.data = curword->object;
436 key.size = strlen(key.data);
437 data.data = worddb_data;
438 data.size = sizeof(worddb_data);
441 * Our data is the key creation time followed by the
444 worddb_data[ 0] = publickey->publickey->data[1];
445 worddb_data[ 1] = publickey->publickey->data[2];
446 worddb_data[ 2] = publickey->publickey->data[3];
447 worddb_data[ 3] = publickey->publickey->data[4];
448 worddb_data[ 4] = (keyid >> 56) & 0xFF;
449 worddb_data[ 5] = (keyid >> 48) & 0xFF;
450 worddb_data[ 6] = (keyid >> 40) & 0xFF;
451 worddb_data[ 7] = (keyid >> 32) & 0xFF;
452 worddb_data[ 8] = (keyid >> 24) & 0xFF;
453 worddb_data[ 9] = (keyid >> 16) & 0xFF;
454 worddb_data[10] = (keyid >> 8) & 0xFF;
455 worddb_data[11] = keyid & 0xFF;
456 ret = worddb->put(worddb,
462 worddb->err(worddb, ret,
463 "Problem storing key");
468 * Free our UID and word lists.
470 llfree(wordlist, NULL);
471 for (i = 0; uids[i] != NULL; i++) {
483 * delete_key - Given a keyid delete the key from storage.
484 * @keyid: The keyid to delete.
485 * @intrans: If we're already in a transaction.
487 * This function deletes a public key from whatever storage mechanism we
488 * are using. Returns 0 if the key existed.
490 int delete_key(uint64_t keyid, bool intrans)
492 struct openpgp_publickey *publickey = NULL;
498 char *primary = NULL;
499 unsigned char worddb_data[12];
500 struct ll *wordlist = NULL;
501 struct ll *curword = NULL;
505 fetch_key(keyid, &publickey, intrans);
508 * Walk through the uids removing the words from the worddb.
510 if (publickey != NULL) {
511 uids = keyuids(publickey, &primary);
514 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
515 wordlist = makewordlist(wordlist, uids[i]);
518 ret = worddb->cursor(worddb,
523 for (curword = wordlist; curword != NULL;
524 curword = curword->next) {
525 memset(&key, 0, sizeof(key));
526 memset(&data, 0, sizeof(data));
527 key.data = curword->object;
528 key.size = strlen(key.data);
529 data.data = worddb_data;
530 data.size = sizeof(worddb_data);
533 * Our data is the key creation time followed by the
536 worddb_data[ 0] = publickey->publickey->data[1];
537 worddb_data[ 1] = publickey->publickey->data[2];
538 worddb_data[ 2] = publickey->publickey->data[3];
539 worddb_data[ 3] = publickey->publickey->data[4];
540 worddb_data[ 4] = (keyid >> 56) & 0xFF;
541 worddb_data[ 5] = (keyid >> 48) & 0xFF;
542 worddb_data[ 6] = (keyid >> 40) & 0xFF;
543 worddb_data[ 7] = (keyid >> 32) & 0xFF;
544 worddb_data[ 8] = (keyid >> 24) & 0xFF;
545 worddb_data[ 9] = (keyid >> 16) & 0xFF;
546 worddb_data[10] = (keyid >> 8) & 0xFF;
547 worddb_data[11] = keyid & 0xFF;
549 ret = cursor->c_get(cursor,
554 ret = cursor->c_del(cursor, 0);
558 worddb->err(worddb, ret,
559 "Problem deleting word.");
562 ret = cursor->c_close(cursor);
566 * Free our UID and word lists.
568 llfree(wordlist, NULL);
569 for (i = 0; uids[i] != NULL; i++) {
575 free_publickey(publickey);
580 key.size = sizeof(keyid);
587 return (ret == DB_NOTFOUND);
591 * Include the basic keydb routines.
593 #define NEED_GETFULLKEYID 1
594 #define NEED_GETKEYSIGS 1
595 #define NEED_KEYID2UID 1