2  * keydb_db4.c - Routines to store and fetch keys in a DB4 database.
 
   4  * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
 
   6  * This program is free software: you can redistribute it and/or modify it
 
   7  * under the terms of the GNU General Public License as published by the Free
 
   8  * Software Foundation; version 2 of the License.
 
  10  * This program is distributed in the hope that it will be useful, but WITHOUT
 
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
  12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 
  15  * You should have received a copy of the GNU General Public License along with
 
  16  * this program; if not, write to the Free Software Foundation, Inc., 51
 
  17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
  20 #include <sys/types.h>
 
  33 #include "charfuncs.h"
 
  37 #include "decodekey.h"
 
  38 #include "keystructs.h"
 
  41 #include "onak-conf.h"
 
  45 #define DB4_UPGRADE_FILE "db_upgrade.lck"
 
  48  *      dbenv - our database environment.
 
  50 static DB_ENV *dbenv = NULL;
 
  53  *      numdb - The number of database files we have.
 
  55 static int numdbs = 16;
 
  58  *      dbconn - our connections to the key database files.
 
  60 static DB **dbconns = NULL;
 
  63  *      worddb - our connection to the word database.
 
  65 static DB *worddb = NULL;
 
  68  *      id32db - our connection to the 32bit ID database.
 
  70 static DB *id32db = NULL;
 
  73  *      skshashdb - our connection to the SKS hash database.
 
  75 static DB *skshashdb = NULL;
 
  78  *      txn - our current transaction id.
 
  80 static DB_TXN *txn = NULL;
 
  82 DB *keydb(uint64_t keyid)
 
  88         return(dbconns[keytrun % numdbs]);
 
  92  *      db4_errfunc - Direct DB errors to logfile
 
  94  *      Basic function to take errors from the DB library and output them to
 
  95  *      the logfile rather than stderr.
 
  97 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR < 3)
 
  98 static void db4_errfunc(const char *errpfx, const char *errmsg)
 
 100 static void db4_errfunc(const DB_ENV *edbenv, const char *errpfx,
 
 105                 logthing(LOGTHING_DEBUG, "db4 error: %s:%s", errpfx, errmsg);
 
 107                 logthing(LOGTHING_DEBUG, "db4 error: %s", errmsg);
 
 114  *      starttrans - Start a transaction.
 
 116  *      Start a transaction. Intended to be used if we're about to perform many
 
 117  *      operations on the database to help speed it all up, or if we want
 
 118  *      something to only succeed if all relevant operations are successful.
 
 120 static bool db4_starttrans(void)
 
 124         log_assert(dbenv != NULL);
 
 125         log_assert(txn == NULL);
 
 127         ret = dbenv->txn_begin(dbenv,
 
 128                 NULL, /* No parent transaction */
 
 132                 logthing(LOGTHING_CRITICAL,
 
 133                                 "Error starting transaction: %s",
 
 142  *      endtrans - End a transaction.
 
 144  *      Ends a transaction.
 
 146 static void db4_endtrans(void)
 
 150         log_assert(dbenv != NULL);
 
 151         log_assert(txn != NULL);
 
 153         ret = txn->commit(txn,
 
 156                 logthing(LOGTHING_CRITICAL,
 
 157                                 "Error ending transaction: %s",
 
 167  *      cleanupdb - De-initialize the key database.
 
 169  *      This function should be called upon program exit to allow the DB to
 
 170  *      cleanup after itself.
 
 172 static void db4_cleanupdb(void)
 
 177                 dbenv->txn_checkpoint(dbenv, 0, 0, 0);
 
 178                 if (skshashdb != NULL) {
 
 179                         skshashdb->close(skshashdb, 0);
 
 182                 if (id32db != NULL) {
 
 183                         id32db->close(id32db, 0);
 
 186                 if (worddb != NULL) {
 
 187                         worddb->close(worddb, 0);
 
 190                 for (i = 0; i < numdbs; i++) {
 
 191                         if (dbconns[i] != NULL) {
 
 192                                 dbconns[i]->close(dbconns[i], 0);
 
 198                 dbenv->close(dbenv, 0);
 
 204  *      db4_upgradedb - Upgrade a DB4 database
 
 206  *      Called if we discover we need to upgrade our DB4 database; ie if
 
 207  *      we're running with a newer version of db4 than the database was
 
 210 static int db4_upgradedb(int numdb)
 
 219         snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
 
 221         lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
 
 222         if (lockfile_fd < 0) {
 
 223                 if (errno == EEXIST) {
 
 224                         while (stat(buf, &statbuf) == 0) ;
 
 227                         logthing(LOGTHING_CRITICAL, "Couldn't open database "
 
 228                                 "update lock file: %s", strerror(errno));
 
 232         snprintf(buf, sizeof(buf) - 1, "%d", getpid());
 
 233         write(lockfile_fd, buf, strlen(buf));
 
 236         logthing(LOGTHING_NOTICE, "Upgrading DB4 database");
 
 237         ret = db_env_create(&dbenv, 0);
 
 238         dbenv->set_errcall(dbenv, &db4_errfunc);
 
 239         dbenv->remove(dbenv, config.db_dir, 0);
 
 241         for (i = 0; i < numdb; i++) {
 
 242                 ret = db_create(&curdb, NULL, 0);
 
 244                         snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
 
 246                         logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
 
 247                         ret = curdb->upgrade(curdb, buf, 0);
 
 248                         curdb->close(curdb, 0);
 
 250                         logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
 
 256         ret = db_create(&curdb, NULL, 0);
 
 258                 snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir);
 
 259                 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
 
 260                 ret = curdb->upgrade(curdb, buf, 0);
 
 261                 curdb->close(curdb, 0);
 
 263                 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
 
 268         ret = db_create(&curdb, NULL, 0);
 
 270                 snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir);
 
 271                 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
 
 272                 ret = curdb->upgrade(curdb, buf, 0);
 
 273                 curdb->close(curdb, 0);
 
 275                 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
 
 280         ret = db_create(&curdb, NULL, 0);
 
 282                 snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir);
 
 283                 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
 
 284                 ret = curdb->upgrade(curdb, buf, 0);
 
 285                 curdb->close(curdb, 0);
 
 287                 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
 
 292         snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
 
 300  *      initdb - Initialize the key database.
 
 302  *      This function should be called before any of the other functions in
 
 303  *      this file are called in order to allow the DB to be initialized ready
 
 306 static void db4_initdb(bool readonly)
 
 316         snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
 
 318         ret = stat(buf, &statbuf);
 
 319         while ((ret == 0) || (errno != ENOENT)) {
 
 321                         logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
 
 322                                 "lock file: %s (%d)", strerror(errno), ret);
 
 325                 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
 
 327                 ret = stat(buf, &statbuf);
 
 331         snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
 
 332         numdb = fopen(buf, "r");
 
 334                 if (fgets(buf, sizeof(buf), numdb) != NULL) {
 
 338         } else if (!readonly) {
 
 339                 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
 
 341                 numdb = fopen(buf, "w");
 
 343                         fprintf(numdb, "%d", numdbs);
 
 346                         logthing(LOGTHING_ERROR,
 
 347                                 "Couldn't write num_keydb: %s",
 
 352         dbconns = calloc(numdbs, sizeof (DB *));
 
 353         if (dbconns == NULL) {
 
 354                 logthing(LOGTHING_CRITICAL,
 
 355                                 "Couldn't allocate memory for dbconns");
 
 360                 ret = db_env_create(&dbenv, 0);
 
 362                         logthing(LOGTHING_CRITICAL,
 
 363                                 "db_env_create: %s", db_strerror(ret));
 
 368          * Up the number of locks we're allowed at once. We base this on
 
 369          * the maximum number of keys we're going to return.
 
 371         maxlocks = config.maxkeys * 16;
 
 372         if (maxlocks < 1000) {
 
 375         dbenv->set_lk_max_locks(dbenv, maxlocks);
 
 376         dbenv->set_lk_max_objects(dbenv, maxlocks);
 
 379          * Enable deadlock detection so that we don't block indefinitely on
 
 380          * anything. What we really want is simple 2 state locks, but I'm not
 
 381          * sure how to make the standard DB functions do that yet.
 
 384                 dbenv->set_errcall(dbenv, &db4_errfunc);
 
 385                 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
 
 387                         logthing(LOGTHING_CRITICAL,
 
 388                                 "db_env_create: %s", db_strerror(ret));
 
 393                 ret = dbenv->open(dbenv, config.db_dir,
 
 394                                 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
 
 398 #ifdef DB_VERSION_MISMATCH
 
 399                 if (ret == DB_VERSION_MISMATCH) {
 
 400                         dbenv->close(dbenv, 0);
 
 402                         ret = db4_upgradedb(numdbs);
 
 404                                 ret = db_env_create(&dbenv, 0);
 
 407                                 dbenv->set_errcall(dbenv, &db4_errfunc);
 
 408                                 dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
 
 409                                 ret = dbenv->open(dbenv, config.db_dir,
 
 410                                         DB_INIT_LOG | DB_INIT_MPOOL |
 
 411                                         DB_INIT_LOCK | DB_INIT_TXN |
 
 412                                         DB_CREATE | DB_RECOVER,
 
 416                                         dbenv->txn_checkpoint(dbenv,
 
 425                         logthing(LOGTHING_CRITICAL,
 
 426                                         "Error opening db environment: %s (%s)",
 
 429                         dbenv->close(dbenv, 0);
 
 437                 for (i = 0; !ret && i < numdbs; i++) {
 
 438                         ret = db_create(&dbconns[i], dbenv, 0);
 
 440                                 logthing(LOGTHING_CRITICAL,
 
 441                                         "db_create: %s", db_strerror(ret));
 
 445                                 snprintf(buf, 1023, "keydb.%d.db", i);
 
 450                                 ret = dbconns[i]->open(dbconns[i],
 
 458                                         logthing(LOGTHING_CRITICAL,
 
 459                                                 "Error opening key database:"
 
 470                 ret = db_create(&worddb, dbenv, 0);
 
 472                         logthing(LOGTHING_CRITICAL, "db_create: %s",
 
 478                 ret = worddb->set_flags(worddb, DB_DUP);
 
 482                 ret = worddb->open(worddb, txn, "worddb", "worddb", DB_BTREE,
 
 486                         logthing(LOGTHING_CRITICAL,
 
 487                                         "Error opening word database: %s (%s)",
 
 494                 ret = db_create(&id32db, dbenv, 0);
 
 496                         logthing(LOGTHING_CRITICAL, "db_create: %s",
 
 502                 ret = id32db->set_flags(id32db, DB_DUP);
 
 506                 ret = id32db->open(id32db, txn, "id32db", "id32db", DB_HASH,
 
 510                         logthing(LOGTHING_CRITICAL,
 
 511                                         "Error opening id32 database: %s (%s)",
 
 518                 ret = db_create(&skshashdb, dbenv, 0);
 
 520                         logthing(LOGTHING_CRITICAL, "db_create: %s",
 
 526                 ret = skshashdb->open(skshashdb, txn, "skshashdb",
 
 527                                 "skshashdb", DB_HASH,
 
 531                         logthing(LOGTHING_CRITICAL,
 
 532                                 "Error opening skshash database: %s (%s)",
 
 544                 logthing(LOGTHING_CRITICAL,
 
 545                                 "Error opening database; exiting");
 
 553  *      getfullkeyid - Maps a 32bit key id to a 64bit one.
 
 554  *      @keyid: The 32bit keyid.
 
 556  *      This function maps a 32bit key id to the full 64bit one. It returns the
 
 557  *      full keyid. If the key isn't found a keyid of 0 is returned.
 
 559 static uint64_t db4_getfullkeyid(uint64_t keyid)
 
 563         uint32_t  shortkeyid = 0;
 
 566         if (keyid < 0x100000000LL) {
 
 567                 ret = id32db->cursor(id32db,
 
 572                 shortkeyid = keyid & 0xFFFFFFFF;
 
 574                 memset(&key, 0, sizeof(key));
 
 575                 memset(&data, 0, sizeof(data));
 
 576                 key.data = &shortkeyid;
 
 577                 key.size = sizeof(shortkeyid);
 
 578                 data.flags = DB_DBT_MALLOC;
 
 580                 ret = cursor->c_get(cursor,
 
 586                         keyid = *(uint64_t *) data.data;
 
 588                         if (data.data != NULL) {
 
 594                 ret = cursor->c_close(cursor);
 
 602  *      fetch_key - Given a keyid fetch the key from storage.
 
 603  *      @keyid: The keyid to fetch.
 
 604  *      @publickey: A pointer to a structure to return the key in.
 
 605  *      @intrans: If we're already in a transaction.
 
 607  *      We use the hex representation of the keyid as the filename to fetch the
 
 608  *      key from. The key is stored in the file as a binary OpenPGP stream of
 
 609  *      packets, so we can just use read_openpgp_stream() to read the packets
 
 610  *      in and then parse_keys() to parse the packets into a publickey
 
 613 static int db4_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
 
 616         struct openpgp_packet_list *packets = NULL;
 
 620         struct buffer_ctx fetchbuf;
 
 622         if (keyid < 0x100000000LL) {
 
 623                 keyid = db4_getfullkeyid(keyid);
 
 626         memset(&key, 0, sizeof(key));
 
 627         memset(&data, 0, sizeof(data));
 
 632         key.size = sizeof(keyid);
 
 639         ret = keydb(keyid)->get(keydb(keyid),
 
 646                 fetchbuf.buffer = data.data;
 
 648                 fetchbuf.size = data.size;
 
 649                 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
 
 651                 parse_keys(packets, publickey);
 
 652                 free_packet_list(packets);
 
 655         } else if (ret != DB_NOTFOUND) {
 
 656                 logthing(LOGTHING_ERROR,
 
 657                                 "Problem retrieving key: %s",
 
 668 int worddb_cmp(const void *d1, const void *d2)
 
 670         return memcmp(d1, d2, 12);
 
 674  *      fetch_key_text - Trys to find the keys that contain the supplied text.
 
 675  *      @search: The text to search for.
 
 676  *      @publickey: A pointer to a structure to return the key in.
 
 678  *      This function searches for the supplied text and returns the keys that
 
 681 static int db4_fetch_key_text(const char *search,
 
 682                 struct openpgp_publickey **publickey)
 
 690         char *searchtext = NULL;
 
 691         struct ll *wordlist = NULL;
 
 692         struct ll *curword = NULL;
 
 693         struct keyarray keylist = { NULL, 0, 0 };
 
 694         struct keyarray newkeylist = { NULL, 0, 0 };
 
 698         searchtext = strdup(search);
 
 699         wordlist = makewordlist(wordlist, searchtext);
 
 701         for (curword = wordlist; curword != NULL; curword = curword->next) {
 
 704                 ret = worddb->cursor(worddb,
 
 709                 memset(&key, 0, sizeof(key));
 
 710                 memset(&data, 0, sizeof(data));
 
 711                 key.data = curword->object;
 
 712                 key.size = strlen(curword->object);
 
 713                 data.flags = DB_DBT_MALLOC;
 
 714                 ret = cursor->c_get(cursor,
 
 718                 while (ret == 0 && strncmp(key.data, curword->object,
 
 720                                 ((char *) curword->object)[key.size] == 0) {
 
 722                         for (i = 4; i < 12; i++) {
 
 724                                 keyid += ((unsigned char *)
 
 729                          * Only add the keys containing this word if this is
 
 730                          * our first pass (ie we have no existing key list),
 
 731                          * or the key contained a previous word.
 
 733                         if (firstpass || array_find(&keylist, keyid)) {
 
 734                                 array_add(&newkeylist, keyid);
 
 740                         ret = cursor->c_get(cursor,
 
 745                 array_free(&keylist);
 
 746                 keylist = newkeylist;
 
 747                 newkeylist.keys = NULL;
 
 748                 newkeylist.count = newkeylist.size = 0;
 
 749                 if (data.data != NULL) {
 
 753                 ret = cursor->c_close(cursor);
 
 758         llfree(wordlist, NULL);
 
 761         if (keylist.count > config.maxkeys) {
 
 762                 keylist.count = config.maxkeys;
 
 766         for (i = 0; i < keylist.count; i++) {
 
 767                 numkeys += db4_fetch_key(keylist.keys[i],
 
 771         array_free(&keylist);
 
 780 static int db4_fetch_key_skshash(const struct skshash *hash,
 
 781                 struct openpgp_publickey **publickey)
 
 788         ret = skshashdb->cursor(skshashdb,
 
 793         memset(&key, 0, sizeof(key));
 
 794         memset(&data, 0, sizeof(data));
 
 795         key.data = (void *) hash->hash;
 
 796         key.size = sizeof(hash->hash);
 
 797         data.flags = DB_DBT_MALLOC;
 
 799         ret = cursor->c_get(cursor,
 
 805                 keyid = *(uint64_t *) data.data;
 
 807                 if (data.data != NULL) {
 
 813         ret = cursor->c_close(cursor);
 
 816         return db4_fetch_key(keyid, publickey, false);
 
 820  *      delete_key - Given a keyid delete the key from storage.
 
 821  *      @keyid: The keyid to delete.
 
 822  *      @intrans: If we're already in a transaction.
 
 824  *      This function deletes a public key from whatever storage mechanism we
 
 825  *      are using. Returns 0 if the key existed.
 
 827 static int db4_delete_key(uint64_t keyid, bool intrans)
 
 829         struct openpgp_publickey *publickey = NULL;
 
 832         uint32_t   shortkeyid = 0;
 
 833         uint64_t  *subkeyids = NULL;
 
 837         char *primary = NULL;
 
 838         unsigned char worddb_data[12];
 
 839         struct ll *wordlist = NULL;
 
 840         struct ll *curword  = NULL;
 
 841         bool deadlock = false;
 
 848         db4_fetch_key(keyid, &publickey, true);
 
 851          * Walk through the uids removing the words from the worddb.
 
 853         if (publickey != NULL) {
 
 854                 uids = keyuids(publickey, &primary);
 
 857                 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
 
 858                         wordlist = makewordlist(wordlist, uids[i]);
 
 861                 ret = worddb->cursor(worddb,
 
 866                 for (curword = wordlist; curword != NULL && !deadlock;
 
 867                                 curword = curword->next) {
 
 868                         memset(&key, 0, sizeof(key));
 
 869                         memset(&data, 0, sizeof(data));
 
 870                         key.data = curword->object;
 
 871                         key.size = strlen(key.data);
 
 872                         data.data = worddb_data;
 
 873                         data.size = sizeof(worddb_data);
 
 876                          * Our data is the key creation time followed by the
 
 879                         worddb_data[ 0] = publickey->publickey->data[1];
 
 880                         worddb_data[ 1] = publickey->publickey->data[2];
 
 881                         worddb_data[ 2] = publickey->publickey->data[3];
 
 882                         worddb_data[ 3] = publickey->publickey->data[4];
 
 883                         worddb_data[ 4] = (keyid >> 56) & 0xFF;
 
 884                         worddb_data[ 5] = (keyid >> 48) & 0xFF;
 
 885                         worddb_data[ 6] = (keyid >> 40) & 0xFF;
 
 886                         worddb_data[ 7] = (keyid >> 32) & 0xFF;
 
 887                         worddb_data[ 8] = (keyid >> 24) & 0xFF;
 
 888                         worddb_data[ 9] = (keyid >> 16) & 0xFF;
 
 889                         worddb_data[10] = (keyid >>  8) & 0xFF;
 
 890                         worddb_data[11] = keyid & 0xFF; 
 
 892                         ret = cursor->c_get(cursor,
 
 898                                 ret = cursor->c_del(cursor, 0);
 
 900                                         logthing(LOGTHING_ERROR,
 
 901                                                 "Problem deleting word: %s",
 
 907                                 logthing(LOGTHING_ERROR,
 
 908                                         "Problem deleting word: %s",
 
 910                                 if (ret == DB_LOCK_DEADLOCK) {
 
 915                 ret = cursor->c_close(cursor);
 
 919                  * Free our UID and word lists.
 
 921                 llfree(wordlist, NULL);
 
 922                 for (i = 0; uids[i] != NULL; i++) {
 
 928                 free_publickey(publickey);
 
 933                 ret = id32db->cursor(id32db,
 
 938                 shortkeyid = keyid & 0xFFFFFFFF;
 
 940                 memset(&key, 0, sizeof(key));
 
 941                 memset(&data, 0, sizeof(data));
 
 942                 key.data = &shortkeyid;
 
 943                 key.size = sizeof(shortkeyid);
 
 945                 data.size = sizeof(keyid);
 
 947                 ret = cursor->c_get(cursor,
 
 953                         ret = cursor->c_del(cursor, 0);
 
 955                                 logthing(LOGTHING_ERROR,
 
 956                                         "Problem deleting short keyid: %s",
 
 962                         logthing(LOGTHING_ERROR,
 
 963                                 "Problem deleting short keyid: %s",
 
 965                         if (ret == DB_LOCK_DEADLOCK) {
 
 970                 subkeyids = keysubkeys(publickey);
 
 972                 while (subkeyids != NULL && subkeyids[i] != 0) {
 
 973                         shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
 
 975                         memset(&key, 0, sizeof(key));
 
 976                         memset(&data, 0, sizeof(data));
 
 977                         key.data = &shortkeyid;
 
 978                         key.size = sizeof(shortkeyid);
 
 980                         data.size = sizeof(keyid);
 
 982                         ret = cursor->c_get(cursor,
 
 988                                 ret = cursor->c_del(cursor, 0);
 
 990                                         logthing(LOGTHING_ERROR,
 
 991                                                 "Problem deleting short"
 
 998                                 logthing(LOGTHING_ERROR,
 
 999                                         "Problem deleting short keyid: %s",
 
1001                                 if (ret == DB_LOCK_DEADLOCK) {
 
1006                 if (subkeyids != NULL) {
 
1010                 ret = cursor->c_close(cursor);
 
1016                 ret = skshashdb->cursor(skshashdb,
 
1020                 get_skshash(publickey, &hash);
 
1022                 memset(&key, 0, sizeof(key));
 
1023                 memset(&data, 0, sizeof(data));
 
1024                 key.data = hash.hash;
 
1025                 key.size = sizeof(hash.hash);
 
1027                 data.size = sizeof(keyid);
 
1029                 ret = cursor->c_get(cursor,
 
1035                         ret = cursor->c_del(cursor, 0);
 
1039                         logthing(LOGTHING_ERROR,
 
1040                                 "Problem deleting skshash: %s",
 
1042                         if (ret == DB_LOCK_DEADLOCK) {
 
1047                 ret = cursor->c_close(cursor);
 
1053                 key.size = sizeof(keyid);
 
1055                 keydb(keyid)->del(keydb(keyid),
 
1065         return deadlock ? (-1) : (ret == DB_NOTFOUND);
 
1069  *      store_key - Takes a key and stores it.
 
1070  *      @publickey: A pointer to the public key to store.
 
1071  *      @intrans: If we're already in a transaction.
 
1072  *      @update: If true the key exists and should be updated.
 
1074  *      Again we just use the hex representation of the keyid as the filename
 
1075  *      to store the key to. We flatten the public key to a list of OpenPGP
 
1076  *      packets and then use write_openpgp_stream() to write the stream out to
 
1077  *      the file. If update is true then we delete the old key first, otherwise
 
1078  *      we trust that it doesn't exist.
 
1080 static int db4_store_key(struct openpgp_publickey *publickey, bool intrans,
 
1083         struct     openpgp_packet_list *packets = NULL;
 
1084         struct     openpgp_packet_list *list_end = NULL;
 
1085         struct     openpgp_publickey *next = NULL;
 
1088         struct     buffer_ctx storebuf;
 
1092         uint32_t   shortkeyid = 0;
 
1093         uint64_t  *subkeyids = NULL;
 
1095         char      *primary = NULL;
 
1096         unsigned char worddb_data[12];
 
1097         struct ll *wordlist = NULL;
 
1098         struct ll *curword  = NULL;
 
1099         bool       deadlock = false;
 
1100         struct skshash hash;
 
1102         keyid = get_keyid(publickey);
 
1109          * Delete the key if we already have it.
 
1111          * TODO: Can we optimize this perhaps? Possibly when other data is
 
1112          * involved as well? I suspect this is easiest and doesn't make a lot
 
1113          * of difference though - the largest chunk of data is the keydata and
 
1114          * it definitely needs updated.
 
1117                 deadlock = (db4_delete_key(keyid, true) == -1);
 
1121          * Convert the key to a flat set of binary data.
 
1124                 next = publickey->next;
 
1125                 publickey->next = NULL;
 
1126                 flatten_publickey(publickey, &packets, &list_end);
 
1127                 publickey->next = next;
 
1129                 storebuf.offset = 0; 
 
1130                 storebuf.size = 8192;
 
1131                 storebuf.buffer = malloc(8192);
 
1133                 write_openpgp_stream(buffer_putchar, &storebuf, packets);
 
1136                  * Now we have the key data store it in the DB; the keyid is
 
1139                 memset(&key, 0, sizeof(key));
 
1140                 memset(&data, 0, sizeof(data));
 
1142                 key.size = sizeof(keyid);
 
1143                 data.size = storebuf.offset;
 
1144                 data.data = storebuf.buffer;
 
1146                 ret = keydb(keyid)->put(keydb(keyid),
 
1152                         logthing(LOGTHING_ERROR,
 
1153                                         "Problem storing key: %s",
 
1155                         if (ret == DB_LOCK_DEADLOCK) {
 
1160                 free(storebuf.buffer);
 
1161                 storebuf.buffer = NULL;
 
1163                 storebuf.offset = 0; 
 
1165                 free_packet_list(packets);
 
1170          * Walk through our uids storing the words into the db with the keyid.
 
1173                 uids = keyuids(publickey, &primary);
 
1176                 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
 
1177                         wordlist = makewordlist(wordlist, uids[i]);
 
1180                 for (curword = wordlist; curword != NULL && !deadlock;
 
1181                                 curword = curword->next) {
 
1182                         memset(&key, 0, sizeof(key));
 
1183                         memset(&data, 0, sizeof(data));
 
1184                         key.data = curword->object;
 
1185                         key.size = strlen(key.data);
 
1186                         data.data = worddb_data;
 
1187                         data.size = sizeof(worddb_data);
 
1190                          * Our data is the key creation time followed by the
 
1193                         worddb_data[ 0] = publickey->publickey->data[1];
 
1194                         worddb_data[ 1] = publickey->publickey->data[2];
 
1195                         worddb_data[ 2] = publickey->publickey->data[3];
 
1196                         worddb_data[ 3] = publickey->publickey->data[4];
 
1197                         worddb_data[ 4] = (keyid >> 56) & 0xFF;
 
1198                         worddb_data[ 5] = (keyid >> 48) & 0xFF;
 
1199                         worddb_data[ 6] = (keyid >> 40) & 0xFF;
 
1200                         worddb_data[ 7] = (keyid >> 32) & 0xFF;
 
1201                         worddb_data[ 8] = (keyid >> 24) & 0xFF;
 
1202                         worddb_data[ 9] = (keyid >> 16) & 0xFF;
 
1203                         worddb_data[10] = (keyid >>  8) & 0xFF;
 
1204                         worddb_data[11] = keyid & 0xFF; 
 
1205                         ret = worddb->put(worddb,
 
1211                                 logthing(LOGTHING_ERROR,
 
1212                                         "Problem storing word: %s",
 
1214                                 if (ret == DB_LOCK_DEADLOCK) {
 
1221                  * Free our UID and word lists.
 
1223                 llfree(wordlist, NULL);
 
1224                 for (i = 0; uids[i] != NULL; i++) {
 
1233          * Write the truncated 32 bit keyid so we can lookup the full id for
 
1237                 shortkeyid = keyid & 0xFFFFFFFF;
 
1239                 memset(&key, 0, sizeof(key));
 
1240                 memset(&data, 0, sizeof(data));
 
1241                 key.data = &shortkeyid;
 
1242                 key.size = sizeof(shortkeyid);
 
1244                 data.size = sizeof(keyid);
 
1246                 ret = id32db->put(id32db,
 
1252                         logthing(LOGTHING_ERROR,
 
1253                                 "Problem storing short keyid: %s",
 
1255                         if (ret == DB_LOCK_DEADLOCK) {
 
1262                 subkeyids = keysubkeys(publickey);
 
1264                 while (subkeyids != NULL && subkeyids[i] != 0) {
 
1265                         shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
 
1267                         memset(&key, 0, sizeof(key));
 
1268                         memset(&data, 0, sizeof(data));
 
1269                         key.data = &shortkeyid;
 
1270                         key.size = sizeof(shortkeyid);
 
1272                         data.size = sizeof(keyid);
 
1274                         ret = id32db->put(id32db,
 
1280                                 logthing(LOGTHING_ERROR,
 
1281                                         "Problem storing short keyid: %s",
 
1283                                 if (ret == DB_LOCK_DEADLOCK) {
 
1288                 if (subkeyids != NULL) {
 
1295                 get_skshash(publickey, &hash);
 
1296                 memset(&key, 0, sizeof(key));
 
1297                 memset(&data, 0, sizeof(data));
 
1298                 key.data = hash.hash;
 
1299                 key.size = sizeof(hash.hash);
 
1301                 data.size = sizeof(keyid);
 
1303                 ret = skshashdb->put(skshashdb,
 
1309                         logthing(LOGTHING_ERROR,
 
1310                                 "Problem storing SKS hash: %s",
 
1312                         if (ret == DB_LOCK_DEADLOCK) {
 
1322         return deadlock ? -1 : 0 ;
 
1326  *      iterate_keys - call a function once for each key in the db.
 
1327  *      @iterfunc: The function to call.
 
1328  *      @ctx: A context pointer
 
1330  *      Calls iterfunc once for each key in the database. ctx is passed
 
1331  *      unaltered to iterfunc. This function is intended to aid database dumps
 
1332  *      and statistic calculations.
 
1334  *      Returns the number of keys we iterated over.
 
1336 static int db4_iterate_keys(void (*iterfunc)(void *ctx,
 
1337                 struct openpgp_publickey *key), void *ctx)
 
1344         struct buffer_ctx           fetchbuf;
 
1345         struct openpgp_packet_list *packets = NULL;
 
1346         struct openpgp_publickey   *key = NULL;
 
1348         for (i = 0; i < numdbs; i++) {
 
1349                 ret = dbconns[i]->cursor(dbconns[i],
 
1354                 memset(&dbkey, 0, sizeof(dbkey));
 
1355                 memset(&data, 0, sizeof(data));
 
1356                 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
 
1358                         fetchbuf.buffer = data.data;
 
1359                         fetchbuf.offset = 0;
 
1360                         fetchbuf.size = data.size;
 
1361                         read_openpgp_stream(buffer_fetchchar, &fetchbuf,
 
1363                         parse_keys(packets, &key);
 
1367                         free_publickey(key);
 
1369                         free_packet_list(packets);
 
1372                         memset(&dbkey, 0, sizeof(dbkey));
 
1373                         memset(&data, 0, sizeof(data));
 
1374                         ret = cursor->c_get(cursor, &dbkey, &data,
 
1378                 if (ret != DB_NOTFOUND) {
 
1379                         logthing(LOGTHING_ERROR,
 
1380                                 "Problem reading key: %s",
 
1384                 ret = cursor->c_close(cursor);
 
1392  * Include the basic keydb routines.
 
1394 #define NEED_GETKEYSIGS 1
 
1395 #define NEED_KEYID2UID 1
 
1396 #define NEED_UPDATEKEYS 1
 
1399 struct dbfuncs keydb_db4_funcs = {
 
1400         .initdb                 = db4_initdb,
 
1401         .cleanupdb              = db4_cleanupdb,
 
1402         .starttrans             = db4_starttrans,
 
1403         .endtrans               = db4_endtrans,
 
1404         .fetch_key              = db4_fetch_key,
 
1405         .fetch_key_text         = db4_fetch_key_text,
 
1406         .fetch_key_skshash      = db4_fetch_key_skshash,
 
1407         .store_key              = db4_store_key,
 
1408         .update_keys            = generic_update_keys,
 
1409         .delete_key             = db4_delete_key,
 
1410         .getkeysigs             = generic_getkeysigs,
 
1411         .cached_getkeysigs      = generic_cached_getkeysigs,
 
1412         .keyid2uid              = generic_keyid2uid,
 
1413         .getfullkeyid           = db4_getfullkeyid,
 
1414         .iterate_keys           = db4_iterate_keys,