cscvs to tla changeset 58
[onak.git] / keydb_db3.c
index 6ef3473189de8e9060e654f0a3c0eaff2541f7c7..2cb61aacaccacc4b353df1a25ea314ca3100c34d 100644 (file)
@@ -6,6 +6,7 @@
  * Copyright 2002 Project Purple
  */
 
+#include <assert.h>
 #include <sys/types.h>
 #include <sys/uio.h>
 #include <ctype.h>
 #include "charfuncs.h"
 #include "keydb.h"
 #include "keyid.h"
-#include "keyindex.h"
+#include "decodekey.h"
 #include "keystructs.h"
 #include "mem.h"
+#include "log.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 
+/**
+ *     dbenv - our database environment.
+ */
+static DB_ENV *dbenv = NULL;
+
 /**
  *     dbconn - our connection to the key database.
  */
@@ -37,6 +44,11 @@ static DB *dbconn = NULL;
  */
 static DB *worddb = NULL;
 
+/**
+ *     txn - our current transaction id.
+ */
+static DB_TXN *txn = NULL;
+
 /**
  *     makewordlist - Takes a string and splits it into a set of unique words.
  *     @wordlist: The current word list.
@@ -96,41 +108,65 @@ struct ll *makewordlist(struct ll *wordlist, char *word)
  */
 void initdb(void)
 {
-       char buf[1024];
        int ret = 0;
 
-       strcpy(buf, config.db2_dbpath);
-       strcat(buf, "/keydb.db");
-       
-       ret = db_create(&dbconn, NULL, 0);
+       ret = db_env_create(&dbenv, 0);
+       if (ret != 0) {
+               logthing(LOGTHING_CRITICAL,
+                       "db_env_create: %s", db_strerror(ret));
+               exit(1);
+       }
+
+       /*
+        * This is a bit of a kludge. Either we run a separate process for
+        * deadlock detection or we do this every time we run. What we really
+        * want to do is specify that our locks are exclusive locks when we
+        * start to do an update.
+        */
+       ret = lock_detect(dbenv,
+                       0, /* flags */
+                       DB_LOCK_RANDOM,
+                       NULL); /* If non null int* for number broken */
+
+       ret = dbenv->open(dbenv, config.db_dir,
+                       DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
+                       DB_INIT_TXN |
+                       DB_CREATE,
+                       0);
        if (ret != 0) {
-               fprintf(stderr, "db_create: %s\n", db_strerror(ret));
+               dbenv->err(dbenv, ret, "%s", config.db_dir);
                exit(1);
        }
 
-       ret = dbconn->open(dbconn, buf, NULL, DB_HASH,
+       ret = db_create(&dbconn, dbenv, 0);
+       if (ret != 0) {
+               logthing(LOGTHING_CRITICAL,
+                               "db_create: %s", db_strerror(ret));
+               exit(1);
+       }
+
+       ret = dbconn->open(dbconn, "keydb.db", 
+                       NULL,
+                       DB_HASH,
                        DB_CREATE,
                        0664);
        if (ret != 0) {
-               dbconn->err(dbconn, ret, "%s", buf);
+               dbconn->err(dbconn, ret, "keydb.db");
                exit(1);
        }
 
-       strcpy(buf, config.db2_dbpath);
-       strcat(buf, "/worddb");
-       
-       ret = db_create(&worddb, NULL, 0);
+       ret = db_create(&worddb, dbenv, 0);
        if (ret != 0) {
-               fprintf(stderr, "db_create: %s\n", db_strerror(ret));
+               logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
                exit(1);
        }
        ret = worddb->set_flags(worddb, DB_DUP);
 
-       ret = worddb->open(worddb, buf, NULL, DB_BTREE,
+       ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
                        DB_CREATE,
                        0664);
        if (ret != 0) {
-               worddb->err(worddb, ret, "%s", buf);
+               worddb->err(worddb, ret, "worddb");
                exit(1);
        }
        
@@ -149,6 +185,8 @@ void cleanupdb(void)
        worddb = NULL;
        dbconn->close(dbconn, 0);
        dbconn = NULL;
+       dbenv->close(dbenv, 0);
+       dbenv = NULL;
 }
 
 /**
@@ -160,6 +198,20 @@ void cleanupdb(void)
  */
 bool starttrans(void)
 {
+       int ret;
+
+       assert(dbenv != NULL);
+       assert(txn == NULL);
+
+       ret = txn_begin(dbenv,
+               NULL, /* No parent transaction */
+               &txn,
+               0);
+       if (ret != 0) {
+               dbenv->err(dbenv, ret, "starttrans():");
+               exit(1);
+       }
+
        return true;
 }
 
@@ -170,6 +222,19 @@ bool starttrans(void)
  */
 void endtrans(void)
 {
+       int ret;
+
+       assert(dbenv != NULL);
+       assert(txn != NULL);
+
+       ret = txn_commit(txn,
+               0);
+       if (ret != 0) {
+               dbenv->err(dbenv, ret, "endtrans():");
+               exit(1);
+       }
+       txn = NULL;
+
        return;
 }
 
@@ -204,8 +269,12 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
        key.data = &keyid;
        keyid &= 0xFFFFFFFF;
 
+       if (!intrans) {
+               starttrans();
+       }
+
        ret = dbconn->get(dbconn,
-                       NULL, /* txn id */
+                       txn,
                        &key,
                        &data,
                        0); /* flags*/
@@ -217,11 +286,17 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
                read_openpgp_stream(buffer_fetchchar, &fetchbuf,
                                &packets);
                parse_keys(packets, publickey);
+               free_packet_list(packets);
+               packets = NULL;
                numkeys++;
        } else if (ret != DB_NOTFOUND) {
                dbconn->err(dbconn, ret, "Problem retrieving key");
        }
 
+       if (!intrans) {
+               endtrans();
+       }
+
        return (numkeys);
 }
 
@@ -256,9 +331,10 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
        searchtext = strdup(search);
        wordlist = makewordlist(wordlist, searchtext);
 
+       starttrans();
 
        ret = worddb->cursor(worddb,
-                       NULL, /* txn */
+                       txn,
                        &cursor,
                        0);   /* flags */
 
@@ -286,6 +362,7 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
                                        llfind(keylist, data.data,
                                                worddb_cmp) != NULL) {
                                newkeylist = lladd(newkeylist, data.data);
+                               data.data = NULL;
                        } else {
                                free(data.data);
                                data.data = NULL;
@@ -298,9 +375,16 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
                llfree(keylist, free);
                keylist = newkeylist;
                newkeylist = NULL;
+               if (data.data != NULL) {
+                       free(data.data);
+                       data.data = NULL;
+               }
        }
+       llfree(wordlist, NULL);
+       wordlist = NULL;
        
-       for (newkeylist = keylist; newkeylist != NULL;
+       for (newkeylist = keylist;
+                       newkeylist != NULL && numkeys < config.maxkeys;
                        newkeylist = newkeylist->next) {
 
                        keyid = 0;
@@ -312,7 +396,7 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
 
                        numkeys += fetch_key(keyid,
                                        publickey,
-                                       false);
+                                       true);
        }
        llfree(keylist, free);
        keylist = NULL;
@@ -321,6 +405,8 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
 
        ret = cursor->c_close(cursor);
        cursor = NULL;
+
+       endtrans();
        
        return (numkeys);
 }
@@ -353,9 +439,14 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
        unsigned char worddb_data[12];
        struct ll *wordlist = NULL;
        struct ll *curword  = NULL;
+       bool       deadlock = false;
 
        keyid = get_keyid(publickey);
 
+       if (!intrans) {
+               starttrans();
+       }
+
        /*
         * Delete the key if we already have it.
         *
@@ -365,53 +456,69 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
         * it definitely needs updated.
         */
        if (update) {
-               delete_key(keyid, true);
+               deadlock = (delete_key(keyid, true) == -1);
        }
 
        /*
         * Convert the key to a flat set of binary data.
         */
-       next = publickey->next;
-       publickey->next = NULL;
-       flatten_publickey(publickey, &packets, &list_end);
-       publickey->next = next;
-
-       storebuf.offset = 0; 
-       storebuf.size = 8192;
-       storebuf.buffer = malloc(8192);
+       if (!deadlock) {
+               next = publickey->next;
+               publickey->next = NULL;
+               flatten_publickey(publickey, &packets, &list_end);
+               publickey->next = next;
+
+               storebuf.offset = 0; 
+               storebuf.size = 8192;
+               storebuf.buffer = malloc(8192);
        
-       write_openpgp_stream(buffer_putchar, &storebuf, packets);
+               write_openpgp_stream(buffer_putchar, &storebuf, packets);
 
-       /*
-        * Now we have the key data store it in the DB; the keyid is the key.
-        */
-       memset(&key, 0, sizeof(key));
-       memset(&data, 0, sizeof(data));
-       key.data = &keyid;
-       key.size = sizeof(keyid);
-       keyid &= 0xFFFFFFFF;
-       data.size = storebuf.offset;
-       data.data = storebuf.buffer;
+               /*
+                * Now we have the key data store it in the DB; the keyid is
+                * the key.
+                */
+               memset(&key, 0, sizeof(key));
+               memset(&data, 0, sizeof(data));
+               key.data = &keyid;
+               key.size = sizeof(keyid);
+               keyid &= 0xFFFFFFFF;
+               data.size = storebuf.offset;
+               data.data = storebuf.buffer;
+
+               ret = dbconn->put(dbconn,
+                               txn,
+                               &key,
+                               &data,
+                               0); /* flags*/
+               if (ret != 0) {
+                       dbconn->err(dbconn, ret, "Problem storing key");
+                       if (ret == DB_LOCK_DEADLOCK) {
+                               deadlock = true;
+                       }
+               }
 
-       ret = dbconn->put(dbconn,
-                       NULL, /* txn id */
-                       &key,
-                       &data,
-                       0); /* flags*/
-       if (ret != 0) {
-               dbconn->err(dbconn, ret, "Problem storing key");
+               free(storebuf.buffer);
+               storebuf.buffer = NULL;
+               storebuf.size = 0;
+               storebuf.offset = 0; 
+       
+               free_packet_list(packets);
+               packets = NULL;
        }
 
        /*
         * Walk through our uids storing the words into the db with the keyid.
         */
-       uids = keyuids(publickey, &primary);
+       if (!deadlock) {
+               uids = keyuids(publickey, &primary);
+       }
        if (uids != NULL) {
                for (i = 0; ret == 0 && uids[i] != NULL; i++) {
                        wordlist = makewordlist(wordlist, uids[i]);
                }
 
-               for (curword = wordlist; curword != NULL;
+               for (curword = wordlist; curword != NULL && !deadlock;
                                curword = curword->next) {
                        memset(&key, 0, sizeof(key));
                        memset(&data, 0, sizeof(data));
@@ -437,13 +544,16 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                        worddb_data[10] = (keyid >>  8) & 0xFF;
                        worddb_data[11] = keyid & 0xFF; 
                        ret = worddb->put(worddb,
-                               0,
+                               txn,
                                &key,
                                &data,
                                0);
                        if (ret != 0) {
                                worddb->err(worddb, ret,
                                        "Problem storing key");
+                               if (ret == DB_LOCK_DEADLOCK) {
+                                       deadlock = true;
+                               }
                        }
                }
 
@@ -459,7 +569,11 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                uids = NULL;
        }
 
-       return 0;
+       if (!intrans) {
+               endtrans();
+       }
+
+       return deadlock ? -1 : 0 ;
 }
 
 /**
@@ -482,10 +596,15 @@ int delete_key(uint64_t keyid, bool intrans)
        unsigned char worddb_data[12];
        struct ll *wordlist = NULL;
        struct ll *curword  = NULL;
+       bool deadlock = false;
 
        keyid &= 0xFFFFFFFF;
 
-       fetch_key(keyid, &publickey, intrans);
+       if (!intrans) {
+               starttrans();
+       }
+
+       fetch_key(keyid, &publickey, true);
 
        /*
         * Walk through the uids removing the words from the worddb.
@@ -499,11 +618,11 @@ int delete_key(uint64_t keyid, bool intrans)
                }
                                
                ret = worddb->cursor(worddb,
-                       NULL, /* txn */
+                       txn,
                        &cursor,
                        0);   /* flags */
 
-               for (curword = wordlist; curword != NULL;
+               for (curword = wordlist; curword != NULL && !deadlock;
                                curword = curword->next) {
                        memset(&key, 0, sizeof(key));
                        memset(&data, 0, sizeof(data));
@@ -533,13 +652,21 @@ int delete_key(uint64_t keyid, bool intrans)
                                &key,
                                &data,
                                DB_GET_BOTH);
+
                        if (ret == 0) {
                                ret = cursor->c_del(cursor, 0);
+                               if (ret != 0) {
+                                       worddb->err(worddb, ret,
+                                               "Problem deleting word.");
+                               }
                        }
 
                        if (ret != 0) {
                                worddb->err(worddb, ret,
                                        "Problem deleting word.");
+                               if (ret == DB_LOCK_DEADLOCK) {
+                                       deadlock = true;
+                               }
                        }
                }
                ret = cursor->c_close(cursor);
@@ -559,15 +686,67 @@ int delete_key(uint64_t keyid, bool intrans)
                publickey = NULL;
        }
 
-       key.data = &keyid;
-       key.size = sizeof(keyid);
+       if (!deadlock) {
+               key.data = &keyid;
+               key.size = sizeof(keyid);
 
-       dbconn->del(dbconn,
-                       NULL, /* txn id */
-                       &key,
-                       0); /* flags */
+               dbconn->del(dbconn,
+                               txn,
+                               &key,
+                               0); /* flags */
+       }
+
+       if (!intrans) {
+               endtrans();
+       }
+
+       return deadlock ? (-1) : (ret == DB_NOTFOUND);
+}
+
+/**
+ *     dumpdb - dump the key database
+ *     @filenamebase: The base filename to use for the dump.
+ *
+ *     Dumps the database into one or more files, which contain pure OpenPGP
+ *     that can be reimported into onak or gpg. filenamebase provides a base
+ *     file name for the dump; several files may be created, all of which will
+ *     begin with this string and then have a unique number and a .pgp
+ *     extension.
+ */
+int dumpdb(char *filenamebase)
+{
+       DBT key, data;
+       DBC *cursor = NULL;
+       int ret = 0;
+       int fd = -1;
+
+       starttrans();
+       
+       ret = dbconn->cursor(dbconn,
+               txn,
+               &cursor,
+               0);   /* flags */
 
-       return (ret == DB_NOTFOUND);
+       fd = open(filenamebase, O_CREAT | O_WRONLY | O_TRUNC, 0640);
+       memset(&key, 0, sizeof(key));
+       memset(&data, 0, sizeof(data));
+       ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
+       while (ret == 0) {
+               write(fd, data.data, data.size);
+               memset(&key, 0, sizeof(key));
+               memset(&data, 0, sizeof(data));
+               ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
+       }
+       dbconn->err(dbconn, ret, "Problem reading key");
+
+       close(fd);
+
+       ret = cursor->c_close(cursor);
+       cursor = NULL;
+       
+       endtrans();
+       
+       return 0;
 }
 
 /*