]> git.sommitrealweird.co.uk Git - onak.git/blobdiff - keydb_db3.c
cscvs to tla changeset 53
[onak.git] / keydb_db3.c
index 263397a9074978673e7a36c173919c8764076140..6177ac70f404adb0ef0764434501e64b4c194a1a 100644 (file)
 
 #include <db.h>
 
+#include "charfuncs.h"
 #include "keydb.h"
 #include "keyid.h"
-#include "keyindex.h"
+#include "decodekey.h"
 #include "keystructs.h"
 #include "mem.h"
 #include "onak-conf.h"
@@ -36,57 +37,6 @@ static DB *dbconn = NULL;
  */
 static DB *worddb = NULL;
 
-/**
- *     db3_get_ctx - Shared with CGI buffer stuff...
- */
-struct db3_get_ctx {
-       char *buffer;
-       int offset;
-       int size;
-};
-
-/**
- *     keydb_fetchchar - Fetches a char from a file.
- */
-static int keydb_fetchchar(void *ctx, size_t count, unsigned char *c)
-{
-       struct db3_get_ctx *buf = NULL;
-       int i;
-       
-       buf = (struct db3_get_ctx *) ctx;
-       for (i = 0; i < count; i++) {
-               c[i] = buf->buffer[buf->offset++];
-       }
-
-       return (((buf->offset) == (buf->size)) ? 1 : 0);
-}
-
-/**
- *     keydb_putchar - Puts a char to a file.
- */
-static int keydb_putchar(void *ctx, size_t count, unsigned char *c)
-{
-       struct db3_get_ctx *buf = NULL;
-       size_t newsize = 0;
-       int i;
-       
-       buf = (struct db3_get_ctx *) ctx;
-
-       for (newsize = buf->size; newsize < (buf->offset + count);
-                       newsize *= 2) ;
-
-       if (newsize != buf->size) {
-               buf->buffer = realloc(buf->buffer, newsize);
-               buf->size = newsize;
-       }
-       
-       for (i = 0; i < count; i++) {
-               buf->buffer[buf->offset++] = c[i];
-       }
-
-       return 1;
-}
-
 /**
  *     makewordlist - Takes a string and splits it into a set of unique words.
  *     @wordlist: The current word list.
@@ -149,7 +99,7 @@ void initdb(void)
        char buf[1024];
        int ret = 0;
 
-       strcpy(buf, config.db2_dbpath);
+       strcpy(buf, config.db_dir);
        strcat(buf, "/keydb.db");
        
        ret = db_create(&dbconn, NULL, 0);
@@ -166,7 +116,7 @@ void initdb(void)
                exit(1);
        }
 
-       strcpy(buf, config.db2_dbpath);
+       strcpy(buf, config.db_dir);
        strcat(buf, "/worddb");
        
        ret = db_create(&worddb, NULL, 0);
@@ -242,7 +192,7 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
        DBT key, data;
        int ret = 0;
        int numkeys = 0;
-       struct db3_get_ctx fetchbuf;
+       struct buffer_ctx fetchbuf;
 
        memset(&key, 0, sizeof(key));
        memset(&data, 0, sizeof(data));
@@ -264,9 +214,11 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
                fetchbuf.buffer = data.data;
                fetchbuf.offset = 0;
                fetchbuf.size = data.size;
-               read_openpgp_stream(keydb_fetchchar, &fetchbuf,
+               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");
@@ -275,6 +227,11 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
        return (numkeys);
 }
 
+int worddb_cmp(const char *d1, const char *d2)
+{
+       return memcmp(d1, d2, 12);
+}
+
 /**
  *     fetch_key_text - Trys to find the keys that contain the supplied text.
  *     @search: The text to search for.
@@ -291,39 +248,89 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
        uint64_t keyid;
        int i;
        int numkeys;
+       char *searchtext = NULL;
+       struct ll *wordlist = NULL;
+       struct ll *curword = NULL;
+       struct ll *keylist = NULL;
+       struct ll *newkeylist = NULL;
 
        numkeys = 0;
+       searchtext = strdup(search);
+       wordlist = makewordlist(wordlist, searchtext);
+
 
        ret = worddb->cursor(worddb,
                        NULL, /* txn */
                        &cursor,
                        0);   /* flags */
-       if (ret == 0) {
+
+       for (curword = wordlist; curword != NULL; curword = curword->next) {
                memset(&key, 0, sizeof(key));
                memset(&data, 0, sizeof(data));
-               key.data = (void *) search;
-               key.size = strlen(search);
+               key.data = curword->object;
+               key.size = strlen(curword->object);
+               data.flags = DB_DBT_MALLOC;
                ret = cursor->c_get(cursor,
                                &key,
                                &data,
                                DB_SET);
-               while (ret == 0 && strcmp(key.data, search) == 0) {
+               while (ret == 0 && strncmp(key.data, curword->object,
+                                       key.size) == 0 &&
+                               ((char *) curword->object)[key.size] == 0) {
                        keyid = 0;
                        for (i = 4; i < 12; i++) {
                                keyid <<= 8;
-                               keyid += ((unsigned char *) data.data)[i];
+                               keyid += ((unsigned char *)
+                                               data.data)[i];
+                       }
+
+                       if (keylist == NULL ||
+                                       llfind(keylist, data.data,
+                                               worddb_cmp) != NULL) {
+                               newkeylist = lladd(newkeylist, data.data);
+                               data.data = NULL;
+                       } else {
+                               free(data.data);
+                               data.data = NULL;
                        }
-                       numkeys += fetch_key(keyid,
-                                       publickey,
-                                       false);
                        ret = cursor->c_get(cursor,
                                        &key,
                                        &data,
                                        DB_NEXT);
                }
-               ret = cursor->c_close(cursor);
-               cursor = NULL;
+               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 && numkeys < config.maxkeys;
+                       newkeylist = newkeylist->next) {
+
+                       keyid = 0;
+                       for (i = 4; i < 12; i++) {
+                               keyid <<= 8;
+                               keyid += ((unsigned char *)
+                                               newkeylist->object)[i];
+                       }
+
+                       numkeys += fetch_key(keyid,
+                                       publickey,
+                                       false);
+       }
+       llfree(keylist, free);
+       keylist = NULL;
+       free(searchtext);
+       searchtext = NULL;
+
+       ret = cursor->c_close(cursor);
+       cursor = NULL;
        
        return (numkeys);
 }
@@ -347,7 +354,7 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
        struct     openpgp_publickey *next = NULL;
        int        ret = 0;
        int        i = 0;
-       struct     db3_get_ctx storebuf;
+       struct     buffer_ctx storebuf;
        DBT        key;
        DBT        data;
        uint64_t   keyid = 0;
@@ -383,7 +390,7 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
        storebuf.size = 8192;
        storebuf.buffer = malloc(8192);
        
-       write_openpgp_stream(keydb_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.
@@ -405,6 +412,14 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                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.
         */