]> git.sommitrealweird.co.uk Git - onak.git/commitdiff
Start pulling non-library material out of core source files
authorJonathan McDowell <noodles@earth.li>
Wed, 25 Jul 2012 04:44:51 +0000 (21:44 -0700)
committerJonathan McDowell <noodles@earth.li>
Wed, 25 Jul 2012 04:44:51 +0000 (21:44 -0700)
As part of moving towards a libonak start pulling things that are related
to the onak keyserver out of the core PGP related source files. Start
with logthing, our logging framework, instead moving towards an onak_status_t
enum to allow up to bubble up errors to the caller.

21 files changed:
armor.c
decodekey.c
keyd.c
keydb.c
keydb_db4.c
keydb_dynamic.c
keydb_file.c
keydb_fs.c
keydb_keyd.c
keyid.c
keyid.h
keyindex.c
lookup.c
mem.c
merge.c
onak.c
parsekey.c
parsekey.h
photoid.c
photoid.h
stripkey.c

diff --git a/armor.c b/armor.c
index 230510a24ed7fbaecbb3c4e919fa56d37a6b638f..9b61b1acde9ce17832907cfae5a8302771b4089a 100644 (file)
--- a/armor.c
+++ b/armor.c
@@ -22,7 +22,6 @@
 
 #include "armor.h"
 #include "keystructs.h"
 
 #include "armor.h"
 #include "keystructs.h"
-#include "log.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 #include "version.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 #include "version.h"
@@ -56,7 +55,7 @@ static unsigned char encode64(unsigned char c) {
        } else if (c == 63) {
                c = '/';
        } else {
        } else if (c == 63) {
                c = '/';
        } else {
-               log_assert(c < 64);
+               c = '?';
        }
 
        return c;
        }
 
        return c;
@@ -165,7 +164,6 @@ static int armor_putchar_int(void *ctx, unsigned char c)
        unsigned char t;
        int i;
 
        unsigned char t;
        int i;
 
-       log_assert(ctx != NULL);
        state = (struct armor_context *) ctx;
 
        switch (state->curoctet++) {
        state = (struct armor_context *) ctx;
 
        switch (state->curoctet++) {
@@ -210,7 +208,6 @@ static int armor_putchar(void *ctx, size_t count, void *c)
 {
        int i;
 
 {
        int i;
 
-       log_assert(c != NULL);
 
        for (i = 0; i < count; i++) {
                armor_putchar_int(ctx, ((char *) c)[i]);
 
        for (i = 0; i < count; i++) {
                armor_putchar_int(ctx, ((char *) c)[i]);
@@ -269,7 +266,6 @@ static int dearmor_getchar(void *ctx, unsigned char *c)
        unsigned char tmpc;
        int i;
 
        unsigned char tmpc;
        int i;
 
-       log_assert(ctx != NULL);
        state = (struct dearmor_context *) ctx;
        *c = 0;
        
        state = (struct dearmor_context *) ctx;
        *c = 0;
        
index 5b2092e024c0b6016e9a77b679fc1c8006f2f019..b4f7ceb46781d45f376385c4cea0ac42affffb63 100644 (file)
@@ -315,13 +315,13 @@ uint64_t *keysubkeys(struct openpgp_publickey *key)
        struct openpgp_signedpacket_list *cursubkey = NULL;
        uint64_t                         *subkeys = NULL;
        int                               count = 0;
        struct openpgp_signedpacket_list *cursubkey = NULL;
        uint64_t                         *subkeys = NULL;
        int                               count = 0;
-        
+
        if (key != NULL && key->subkeys != NULL) {
                subkeys = malloc((spsize(key->subkeys) + 1) *
                                sizeof (uint64_t));
                cursubkey = key->subkeys;
                while (cursubkey != NULL) {
        if (key != NULL && key->subkeys != NULL) {
                subkeys = malloc((spsize(key->subkeys) + 1) *
                                sizeof (uint64_t));
                cursubkey = key->subkeys;
                while (cursubkey != NULL) {
-                       subkeys[count++] = get_packetid(cursubkey->packet);
+                       get_packetid(cursubkey->packet, &subkeys[count++]);
                        cursubkey = cursubkey -> next;
                }
                subkeys[count] = 0;
                        cursubkey = cursubkey -> next;
                }
                subkeys[count] = 0;
diff --git a/keyd.c b/keyd.c
index 5911ce9104a967a10cd9d46758223896b2a7dccd..e43115d48bdda7cb26f81628f563e4324f937698 100644 (file)
--- a/keyd.c
+++ b/keyd.c
@@ -78,15 +78,17 @@ void iteratefunc(void *ctx, struct openpgp_publickey *key)
        struct buffer_ctx           storebuf;
        int                         ret = 0;
        int                         *fd = (int *) ctx;
        struct buffer_ctx           storebuf;
        int                         ret = 0;
        int                         *fd = (int *) ctx;
+       uint64_t                    keyid;
 
        if (key != NULL) {
                storebuf.offset = 0;
                storebuf.size = 8192;
                storebuf.buffer = malloc(8192);
 
 
        if (key != NULL) {
                storebuf.offset = 0;
                storebuf.size = 8192;
                storebuf.buffer = malloc(8192);
 
+               get_keyid(key, &keyid);
                logthing(LOGTHING_TRACE,
                                "Iterating over 0x%016" PRIX64 ".",
                logthing(LOGTHING_TRACE,
                                "Iterating over 0x%016" PRIX64 ".",
-                               get_keyid(key));
+                               keyid);
 
                flatten_publickey(key,
                                &packets,
 
                flatten_publickey(key,
                                &packets,
diff --git a/keydb.c b/keydb.c
index 00bb3ff815e2216f8042934cdced6747c59642f5..c257eb589db31b1c51962049c0e79a3384969dfe 100644 (file)
--- a/keydb.c
+++ b/keydb.c
@@ -162,7 +162,7 @@ uint64_t generic_getfullkeyid(uint64_t keyid)
        if (keyid < 0x100000000LL) {
                config.dbbackend->fetch_key(keyid, &publickey, false);
                if (publickey != NULL) {
        if (keyid < 0x100000000LL) {
                config.dbbackend->fetch_key(keyid, &publickey, false);
                if (publickey != NULL) {
-                       keyid = get_keyid(publickey);
+                       get_keyid(publickey, &keyid);
                        free_publickey(publickey);
                        publickey = NULL;
                } else {
                        free_publickey(publickey);
                        publickey = NULL;
                } else {
@@ -193,13 +193,15 @@ int generic_update_keys(struct openpgp_publickey **keys, bool sendsync)
        struct openpgp_publickey *prev = NULL;
        int newkeys = 0;
        bool intrans;
        struct openpgp_publickey *prev = NULL;
        int newkeys = 0;
        bool intrans;
+       uint64_t keyid;
 
        for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
                intrans = config.dbbackend->starttrans();
 
        for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
                intrans = config.dbbackend->starttrans();
+               get_keyid(curkey, &keyid);
                logthing(LOGTHING_INFO,
                        "Fetching key 0x%" PRIX64 ", result: %d",
                logthing(LOGTHING_INFO,
                        "Fetching key 0x%" PRIX64 ", result: %d",
-                       get_keyid(curkey),
-                       config.dbbackend->fetch_key(get_keyid(curkey), &oldkey,
+                       keyid,
+                       config.dbbackend->fetch_key(keyid, &oldkey,
                                        intrans));
 
                /*
                                        intrans));
 
                /*
index 802903caed6b8c373ab0ece3843494c19ebb74e5..d582eaaf8334c4618c8962d0c5bcd40271468cde 100644 (file)
@@ -1089,7 +1089,7 @@ static int db4_store_key(struct openpgp_publickey *publickey, bool intrans,
        bool       deadlock = false;
        struct skshash hash;
 
        bool       deadlock = false;
        struct skshash hash;
 
-       keyid = get_keyid(publickey);
+       get_keyid(publickey, &keyid);
 
        if (!intrans) {
                db4_starttrans();
 
        if (!intrans) {
                db4_starttrans();
index 10bec165e6decd21c6820095fd6f21abfd0057fa..65373860461243548727048858493850d2a597d7 100644 (file)
@@ -393,7 +393,7 @@ static uint64_t dynamic_getfullkeyid(uint64_t keyid)
        if (keyid < 0x100000000LL) {
                dynamic_fetch_key(keyid, &publickey, false);
                if (publickey != NULL) {
        if (keyid < 0x100000000LL) {
                dynamic_fetch_key(keyid, &publickey, false);
                if (publickey != NULL) {
-                       keyid = get_keyid(publickey);
+                       get_keyid(publickey, &keyid);
                        free_publickey(publickey);
                        publickey = NULL;
                } else {
                        free_publickey(publickey);
                        publickey = NULL;
                } else {
@@ -422,6 +422,7 @@ static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync)
        struct openpgp_publickey *prev = NULL;
        int newkeys = 0;
        bool intrans;
        struct openpgp_publickey *prev = NULL;
        int newkeys = 0;
        bool intrans;
+       uint64_t keyid;
        
        if (loaded_backend == NULL) {
                load_backend();
        
        if (loaded_backend == NULL) {
                load_backend();
@@ -435,10 +436,11 @@ static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync)
 
        for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
                intrans = dynamic_starttrans();
 
        for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
                intrans = dynamic_starttrans();
+               get_keyid(curkey, &keyid);
                logthing(LOGTHING_INFO,
                        "Fetching key 0x%" PRIX64 ", result: %d",
                logthing(LOGTHING_INFO,
                        "Fetching key 0x%" PRIX64 ", result: %d",
-                       get_keyid(curkey),
-                       dynamic_fetch_key(get_keyid(curkey), &oldkey, intrans));
+                       keyid,
+                       dynamic_fetch_key(keyid, &oldkey, intrans));
 
                /*
                 * If we already have the key stored in the DB then merge it
 
                /*
                 * If we already have the key stored in the DB then merge it
index e1d0b528bc1f06eb08e3fdd473830603d82c1bcf..65e71f559d409b28bb7a9d341f398412788f6eef 100644 (file)
@@ -128,9 +128,11 @@ static int file_store_key(struct openpgp_publickey *publickey, bool intrans,
        struct openpgp_publickey *next = NULL;
        char keyfile[1024];
        int fd = -1;
        struct openpgp_publickey *next = NULL;
        char keyfile[1024];
        int fd = -1;
+       uint64_t keyid;
 
 
+       get_keyid(publickey, &keyid);
        snprintf(keyfile, 1023, "%s/0x%" PRIX64, config.db_dir,
        snprintf(keyfile, 1023, "%s/0x%" PRIX64, config.db_dir,
-                       get_keyid(publickey) & 0xFFFFFFFF);
+                       keyid & 0xFFFFFFFF);
        fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
 
        if (fd > -1) {
        fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
 
        if (fd > -1) {
index ea09c7c0dae882e0684f3174c35397e26fccadaa..eacf1c65b15cf1dae48c79e9e59f52503655f873 100644 (file)
@@ -329,13 +329,14 @@ static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_packet_list *list_end = NULL;
        struct openpgp_publickey *next = NULL;
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_packet_list *list_end = NULL;
        struct openpgp_publickey *next = NULL;
-       uint64_t keyid = get_keyid(publickey);
+       uint64_t keyid;
        struct ll *wordlist = NULL, *wl = NULL;
        struct skshash hash;
        uint64_t *subkeyids = NULL;
        uint32_t hashid;
        int i = 0;
 
        struct ll *wordlist = NULL, *wl = NULL;
        struct skshash hash;
        uint64_t *subkeyids = NULL;
        uint32_t hashid;
        int i = 0;
 
+       get_keyid(publickey, &keyid);
 
        if (!intrans)
                fs_starttrans();
 
        if (!intrans)
                fs_starttrans();
index 0f501fe47c07e17b09f1d63b5913c336f7968e66..26f2f2797a1465627a195983f221953a0c8d0fff 100644 (file)
@@ -274,7 +274,7 @@ static int keyd_store_key(struct openpgp_publickey *publickey, bool intrans,
        uint32_t                    cmd = KEYD_CMD_STORE;
        uint64_t                    keyid;
 
        uint32_t                    cmd = KEYD_CMD_STORE;
        uint64_t                    keyid;
 
-       keyid = get_keyid(publickey);
+       get_keyid(publickey, &keyid);
        
        if (update) {
                keyd_delete_key(keyid, false);
        
        if (update) {
                keyd_delete_key(keyid, false);
diff --git a/keyid.c b/keyid.c
index c75f0a178854b3146bb8aa252cc1b4f27c43af07..d114852bd3081b2f44da24fd785ccce3bada8dfe 100644 (file)
--- a/keyid.c
+++ b/keyid.c
@@ -24,7 +24,7 @@
 #include "config.h"
 #include "keyid.h"
 #include "keystructs.h"
 #include "config.h"
 #include "keyid.h"
 #include "keystructs.h"
-#include "log.h"
+#include "onak.h"
 #include "parsekey.h"
 #include "mem.h"
 #include "merge.h"
 #include "parsekey.h"
 #include "mem.h"
 #include "merge.h"
  *     get_keyid - Given a public key returns the keyid.
  *     @publickey: The key to calculate the id for.
  */
  *     get_keyid - Given a public key returns the keyid.
  *     @publickey: The key to calculate the id for.
  */
-uint64_t get_keyid(struct openpgp_publickey *publickey)
+onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid)
 {
 {
-       return (get_packetid(publickey->publickey));
+       return (get_packetid(publickey->publickey, keyid));
 }
 
 /**
  *     get_fingerprint - Given a public key returns the fingerprint.
  *     @publickey: The key to calculate the id for.
 }
 
 /**
  *     get_fingerprint - Given a public key returns the fingerprint.
  *     @publickey: The key to calculate the id for.
- *     @fingerprint: The fingerprint (must be at least 20 bytes of space). 
+ *     @fingerprint: The fingerprint (must be at least 20 bytes of space).
  *     @len: The length of the returned fingerprint.
  *
  *     This function returns the fingerprint for a given public key. As Type 3
  *     fingerprints are 16 bytes and Type 4 are 20 the len field indicates
  *     which we've returned.
  */
  *     @len: The length of the returned fingerprint.
  *
  *     This function returns the fingerprint for a given public key. As Type 3
  *     fingerprints are 16 bytes and Type 4 are 20 the len field indicates
  *     which we've returned.
  */
-unsigned char *get_fingerprint(struct openpgp_packet *packet,
+onak_status_t get_fingerprint(struct openpgp_packet *packet,
        unsigned char *fingerprint,
        size_t *len)
 {
        unsigned char *fingerprint,
        size_t *len)
 {
@@ -66,8 +66,10 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet,
        unsigned char c;
        size_t         modlen, explen;
 
        unsigned char c;
        size_t         modlen, explen;
 
-       log_assert(fingerprint != NULL);
-       log_assert(len != NULL);
+       if (fingerprint == NULL)
+               return ONAK_E_INVALID_PARAM;
+       if (len == NULL)
+               return ONAK_E_INVALID_PARAM;
 
        *len = 0;
 
 
        *len = 0;
 
@@ -111,11 +113,10 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet,
 
                break;
        default:
 
                break;
        default:
-               logthing(LOGTHING_ERROR, "Unknown key type: %d",
-                               packet->data[0]);
+               return ONAK_E_UNKNOWN_VER;
        }
 
        }
 
-       return fingerprint;
+       return ONAK_E_OK;
 }
 
 
 }
 
 
@@ -123,15 +124,15 @@ unsigned char *get_fingerprint(struct openpgp_packet *packet,
  *     get_packetid - Given a PGP packet returns the keyid.
  *     @packet: The packet to calculate the id for.
  */
  *     get_packetid - Given a PGP packet returns the keyid.
  *     @packet: The packet to calculate the id for.
  */
-uint64_t get_packetid(struct openpgp_packet *packet)
+onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid)
 {
 {
-       uint64_t        keyid = 0;
        int             offset = 0;
        int             i = 0;
        size_t          length = 0;
        unsigned char   buff[20];
 
        int             offset = 0;
        int             i = 0;
        size_t          length = 0;
        unsigned char   buff[20];
 
-       log_assert(packet != NULL);
+       if (packet == NULL)
+               return ONAK_E_INVALID_PARAM;
 
        switch (packet->data[0]) {
        case 2:
 
        switch (packet->data[0]) {
        case 2:
@@ -145,38 +146,34 @@ uint64_t get_packetid(struct openpgp_packet *packet)
                        packet->data[9];
                offset = ((offset + 7) / 8) + 2;
 
                        packet->data[9];
                offset = ((offset + 7) / 8) + 2;
 
-               for (keyid = 0, i = 0; i < 8; i++) {
-                       keyid <<= 8;
-                       keyid += packet->data[offset++];
+               for (*keyid = 0, i = 0; i < 8; i++) {
+                       *keyid <<= 8;
+                       *keyid += packet->data[offset++];
                }
                /*
                }
                /*
-                * Check for an RSA key; if not then log but accept anyway.
+                * Check for an RSA key; if not return an error.
                 * 1 == RSA
                 * 2 == RSA Encrypt-Only
                 * 3 == RSA Sign-Only
                 */
                if (packet->data[7] < 1 || packet->data[7] > 3) {
                 * 1 == RSA
                 * 2 == RSA Encrypt-Only
                 * 3 == RSA Sign-Only
                 */
                if (packet->data[7] < 1 || packet->data[7] > 3) {
-                       logthing(LOGTHING_NOTICE,
-                               "Type 2 or 3 key, but not RSA: %llx (type %d)",
-                               keyid,
-                               packet->data[7]);
+                       return ONAK_E_INVALID_PKT;
                }
                break;
        case 4:
                get_fingerprint(packet, buff, &length);
                
                }
                break;
        case 4:
                get_fingerprint(packet, buff, &length);
                
-               for (keyid = 0, i = 12; i < 20; i++) {
-                       keyid <<= 8;
-                       keyid += buff[i];
+               for (*keyid = 0, i = 12; i < 20; i++) {
+                       *keyid <<= 8;
+                       *keyid += buff[i];
                }
 
                break;
        default:
                }
 
                break;
        default:
-               logthing(LOGTHING_ERROR, "Unknown key type: %d",
-                               packet->data[0]);
+               return ONAK_E_UNKNOWN_VER;
        }
 
        }
 
-       return keyid;
+       return ONAK_E_OK;
 }
 
 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
 }
 
 static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
@@ -200,7 +197,7 @@ static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
        return sorted;
 }
 
        return sorted;
 }
 
-void get_skshash(struct openpgp_publickey *key, struct skshash *hash)
+onak_status_t get_skshash(struct openpgp_publickey *key, struct skshash *hash)
 {
        struct openpgp_packet_list *packets = NULL, *list_end = NULL;
        struct openpgp_packet_list *curpacket;
 {
        struct openpgp_packet_list *packets = NULL, *list_end = NULL;
        struct openpgp_packet_list *curpacket;
@@ -233,6 +230,8 @@ void get_skshash(struct openpgp_publickey *key, struct skshash *hash)
 
        md5_digest(&md5_context, 16, (uint8_t *) &hash->hash);
        free_packet_list(packets);
 
        md5_digest(&md5_context, 16, (uint8_t *) &hash->hash);
        free_packet_list(packets);
+
+       return ONAK_E_OK;
 }
 
 uint8_t hexdigit(char c)
 }
 
 uint8_t hexdigit(char c)
diff --git a/keyid.h b/keyid.h
index d6e48e6ceb1d54ca6f2a0b511464dc8db54ef0d5..a0ae4ffe916bcd1bf28395f5127dc91eb3ae9897 100644 (file)
--- a/keyid.h
+++ b/keyid.h
 #include <inttypes.h>
 
 #include "keystructs.h"
 #include <inttypes.h>
 
 #include "keystructs.h"
+#include "onak.h"
 
 /**
  *     get_keyid - Given a public key returns the keyid.
  *     @publickey: The key to calculate the id for.
 
 /**
  *     get_keyid - Given a public key returns the keyid.
  *     @publickey: The key to calculate the id for.
+ *     @keeyid: The returned keyid
  *
  *     This function returns the key id for a given public key.
  */
  *
  *     This function returns the key id for a given public key.
  */
-uint64_t get_keyid(struct openpgp_publickey *publickey);
+onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid);
 
 /**
  *     get_fingerprint - Given a public key returns the fingerprint.
  *     @publickey: The key to calculate the id for.
 
 /**
  *     get_fingerprint - Given a public key returns the fingerprint.
  *     @publickey: The key to calculate the id for.
- *     @fingerprint: The fingerprint (must be at least 20 bytes of space). 
+ *     @fingerprint: The fingerprint (must be at least 20 bytes of space).
  *     @len: The length of the returned fingerprint.
  *
  *     This function returns the fingerprint for a given public key. As Type 3
  *     fingerprints are 16 bytes and Type 4 are 20 the len field indicates
  *     which we've returned.
  */
  *     @len: The length of the returned fingerprint.
  *
  *     This function returns the fingerprint for a given public key. As Type 3
  *     fingerprints are 16 bytes and Type 4 are 20 the len field indicates
  *     which we've returned.
  */
-unsigned char *get_fingerprint(struct openpgp_packet *packet,
+onak_status_t get_fingerprint(struct openpgp_packet *packet,
        unsigned char *fingerprint,
        size_t *len);
 
 /**
  *     get_packetid - Given a PGP packet returns the keyid.
  *     @packet: The packet to calculate the id for.
        unsigned char *fingerprint,
        size_t *len);
 
 /**
  *     get_packetid - Given a PGP packet returns the keyid.
  *     @packet: The packet to calculate the id for.
+ *     @keyid: The returned keyid
  *
  *     This function returns the key id for a given PGP packet.
  */
  *
  *     This function returns the key id for a given PGP packet.
  */
-uint64_t get_packetid(struct openpgp_packet *packet);
+onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid);
 
 /**
  *     get_skshash - Given a public key returns the SKS hash for it.
 
 /**
  *     get_skshash - Given a public key returns the SKS hash for it.
@@ -64,7 +67,8 @@ uint64_t get_packetid(struct openpgp_packet *packet);
  *     make up the key. The caller should allocate the memory for the
  *     hash.
  */
  *     make up the key. The caller should allocate the memory for the
  *     hash.
  */
-void get_skshash(struct openpgp_publickey *publickey, struct skshash *hash);
+onak_status_t get_skshash(struct openpgp_publickey *publickey,
+       struct skshash *hash);
 
 /**
  *     parse_skshash - Parse a string into an SKS hash structure.
 
 /**
  *     parse_skshash - Parse a string into an SKS hash structure.
index 8de8b233e50c8f57f413fe713e0c2538e77f2824..46e778c21b3822f6a48e7228e93a641461849843 100644 (file)
@@ -32,6 +32,7 @@
 #include "keyindex.h"
 #include "keystructs.h"
 #include "log.h"
 #include "keyindex.h"
 #include "keystructs.h"
 #include "log.h"
+#include "onak.h"
 #include "onak-conf.h"
 #include "openpgp.h"
 
 #include "onak-conf.h"
 #include "openpgp.h"
 
@@ -127,6 +128,7 @@ int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
        time_t          created_time = 0;
        int             type = 0;
        int             length = 0;
        time_t          created_time = 0;
        int             type = 0;
        int             length = 0;
+       uint64_t        keyid = 0;
 
        while (subkeys != NULL) {
                if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
 
        while (subkeys != NULL) {
                if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
@@ -154,14 +156,17 @@ int list_subkeys(struct openpgp_signedpacket_list *subkeys, bool verbose,
                                        "Unknown key type: %d",
                                        subkeys->packet->data[0]);
                        }
                                        "Unknown key type: %d",
                                        subkeys->packet->data[0]);
                        }
-               
+
+                       if (get_packetid(subkeys->packet,
+                                       &keyid) != ONAK_E_OK) {
+                               logthing(LOGTHING_ERROR, "Couldn't get keyid.");
+                       }
                        printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
                                length,
                                (type == OPENPGP_PKALGO_RSA) ? 'R' :
                                ((type == OPENPGP_PKALGO_ELGAMAL_ENC) ? 'g' :
                                ((type == OPENPGP_PKALGO_DSA) ? 'D' : '?')),
                        printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
                                length,
                                (type == OPENPGP_PKALGO_RSA) ? 'R' :
                                ((type == OPENPGP_PKALGO_ELGAMAL_ENC) ? 'g' :
                                ((type == OPENPGP_PKALGO_DSA) ? 'D' : '?')),
-                               (uint32_t) (get_packetid(subkeys->packet) &
-                                           0xFFFFFFFF),
+                               (uint32_t) (keyid & 0xFFFFFFFF),
                                created->tm_year + 1900,
                                created->tm_mon + 1,
                                created->tm_mday);
                                created->tm_year + 1900,
                                created->tm_mon + 1,
                                created->tm_mday);
@@ -275,7 +280,9 @@ int key_index(struct openpgp_publickey *keys, bool verbose, bool fingerprint,
                                keys->publickey->data[0]);
                }
                
                                keys->publickey->data[0]);
                }
                
-               keyid = get_keyid(keys);
+               if (get_keyid(keys, &keyid) != ONAK_E_OK) {
+                       logthing(LOGTHING_ERROR, "Couldn't get keyid.");
+               }
 
                switch (type) {
                case OPENPGP_PKALGO_RSA:
 
                switch (type) {
                case OPENPGP_PKALGO_RSA:
@@ -381,6 +388,7 @@ int mrkey_index(struct openpgp_publickey *keys)
        size_t                                   fplength = 0;
        unsigned char                            fp[20];
        int                                      c;
        size_t                                   fplength = 0;
        unsigned char                            fp[20];
        int                                      c;
+       uint64_t                                 keyid;
 
        while (keys != NULL) {
                created_time = (keys->publickey->data[1] << 24) +
 
        while (keys != NULL) {
                created_time = (keys->publickey->data[1] << 24) +
@@ -393,7 +401,10 @@ int mrkey_index(struct openpgp_publickey *keys)
                switch (keys->publickey->data[0]) {
                case 2:
                case 3:
                switch (keys->publickey->data[0]) {
                case 2:
                case 3:
-                       printf("%016" PRIX64, get_keyid(keys));
+                       if (get_keyid(keys, &keyid) != ONAK_E_OK) {
+                               logthing(LOGTHING_ERROR, "Couldn't get keyid");
+                       }
+                       printf("%016" PRIX64, keyid);
                        type = keys->publickey->data[7];
                        length = (keys->publickey->data[8] << 8) +
                                        keys->publickey->data[9];
                        type = keys->publickey->data[7];
                        length = (keys->publickey->data[8] << 8) +
                                        keys->publickey->data[9];
index 273afc225c04d567636d92bcfc50d44eaa541752..9af11d93204dac0c75b57dd26a856cdc4cade1bf 100644 (file)
--- a/lookup.c
+++ b/lookup.c
@@ -245,7 +245,7 @@ int main(int argc, char *argv[])
                                size_t         length = 0;
 
                                if (getphoto(publickey, indx, &photo,
                                size_t         length = 0;
 
                                if (getphoto(publickey, indx, &photo,
-                                               &length)) {
+                                               &length) == ONAK_E_OK) {
                                        fwrite(photo,
                                                        1,
                                                        length,
                                        fwrite(photo,
                                                        1,
                                                        length,
diff --git a/mem.c b/mem.c
index 2f13cf5fffc71fb07d92d4b74c7b0c12760324d3..2b45158028857d2c57b71f947110d7a621980e21 100644 (file)
--- a/mem.c
+++ b/mem.c
@@ -23,7 +23,6 @@
 
 #include "keystructs.h"
 #include "ll.h"
 
 #include "keystructs.h"
 #include "ll.h"
-#include "log.h"
 #include "mem.h"
 
 /**
 #include "mem.h"
 
 /**
@@ -38,7 +37,8 @@ struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
 {
        struct openpgp_packet *newpacket = NULL;
 
 {
        struct openpgp_packet *newpacket = NULL;
 
-       log_assert(packet != NULL);
+       if (packet == NULL)
+               return NULL;
 
        newpacket = malloc(sizeof (struct openpgp_packet));
        if (newpacket != NULL) {
 
        newpacket = malloc(sizeof (struct openpgp_packet));
        if (newpacket != NULL) {
@@ -70,9 +70,6 @@ void packet_list_add(struct openpgp_packet_list **list,
                struct openpgp_packet_list **list_end,
                struct openpgp_packet_list *packet_list)
 {
                struct openpgp_packet_list **list_end,
                struct openpgp_packet_list *packet_list)
 {
-       log_assert(list != NULL);
-       log_assert(list_end != NULL);
-
        for (; packet_list != NULL; packet_list = packet_list->next) {
                ADD_PACKET_TO_LIST((*list_end),
                                packet_dup(packet_list->packet));
        for (; packet_list != NULL; packet_list = packet_list->next) {
                ADD_PACKET_TO_LIST((*list_end),
                                packet_dup(packet_list->packet));
@@ -92,8 +89,6 @@ void packet_list_add(struct openpgp_packet_list **list,
  *     including the data part.
  */
 void free_packet(struct openpgp_packet *packet) {
  *     including the data part.
  */
 void free_packet(struct openpgp_packet *packet) {
-       log_assert(packet != NULL);
-
        if (packet->data != NULL) {
                free(packet->data);
                packet->data = NULL;
        if (packet->data != NULL) {
                free(packet->data);
                packet->data = NULL;
diff --git a/merge.c b/merge.c
index 9dcbf4d20fc3c784df361cb68e6c6a2d8ec5dac2..353a844e6de9a1d346e7f102d7872b4378206137 100644 (file)
--- a/merge.c
+++ b/merge.c
@@ -327,13 +327,20 @@ int merge_keys(struct openpgp_publickey *a, struct openpgp_publickey *b)
        struct openpgp_packet_list      *curpacket = NULL; 
        struct openpgp_packet_list      *lastpacket = NULL;
        struct openpgp_packet_list      *nextpacket = NULL;
        struct openpgp_packet_list      *curpacket = NULL; 
        struct openpgp_packet_list      *lastpacket = NULL;
        struct openpgp_packet_list      *nextpacket = NULL;
+       uint64_t keya, keyb;
 
        if (a == NULL || b == NULL) {
                /*
                 * Do nothing.
                 */
 
        if (a == NULL || b == NULL) {
                /*
                 * Do nothing.
                 */
-               rc = 1;
-       } else if (get_keyid(a) != get_keyid(b)) {
+               return 1;
+       }
+
+       if (get_keyid(a, &keya) != ONAK_E_OK) {
+               return 1;
+       } else if (get_keyid(b, &keyb) != ONAK_E_OK) {
+               return 1;
+       } else if (keya != keyb) {
                /*
                 * Key IDs are different.
                 */
                /*
                 * Key IDs are different.
                 */
diff --git a/onak.c b/onak.c
index fb14df0f9fcbd2b3cfe808205cc65614c835a2c5..6baea3debb96715cfd16350480eed24dc57cd119 100644 (file)
--- a/onak.c
+++ b/onak.c
@@ -330,7 +330,8 @@ int main(int argc, char *argv[])
                                unsigned char *photo = NULL;
                                size_t         length = 0;
 
                                unsigned char *photo = NULL;
                                size_t         length = 0;
 
-                               if (getphoto(keys, 0, &photo, &length)) {
+                               if (getphoto(keys, 0, &photo,
+                                               &length) == ONAK_E_OK) {
                                        fwrite(photo,
                                                1,
                                                length,
                                        fwrite(photo,
                                                1,
                                                length,
index 517b0294a1452d26f23717052df85e64697365a3..aedbd14d8361a395b71d18abe7ee92eec4298c0d 100644 (file)
@@ -25,8 +25,8 @@
 #include "keyid.h"
 #include "keystructs.h"
 #include "ll.h"
 #include "keyid.h"
 #include "keystructs.h"
 #include "ll.h"
-#include "log.h"
 #include "mem.h"
 #include "mem.h"
+#include "onak.h"
 #include "openpgp.h"
 #include "parsekey.h"
 
 #include "openpgp.h"
 #include "parsekey.h"
 
@@ -62,7 +62,8 @@ int parse_keys(struct openpgp_packet_list *packets,
                         * It's a signature packet. Add it to either the public
                         * key, to the current UID or the current subkey.
                         */
                         * It's a signature packet. Add it to either the public
                         * key, to the current UID or the current subkey.
                         */
-                       log_assert(curkey != NULL);
+                       if (curkey == NULL)
+                               return ONAK_E_INVALID_PARAM;
                        if (curkey->subkeys != NULL) {
                                ADD_PACKET_TO_LIST_END(curkey->last_subkey,
                                        sig,
                        if (curkey->subkeys != NULL) {
                                ADD_PACKET_TO_LIST_END(curkey->last_subkey,
                                        sig,
@@ -117,8 +118,10 @@ int parse_keys(struct openpgp_packet_list *packets,
                        /*
                         * It's a UID packet (or a photo id, which is similar).
                         */
                        /*
                         * It's a UID packet (or a photo id, which is similar).
                         */
-                       log_assert(curkey != NULL);
-                       log_assert(curkey->subkeys == NULL);
+                       if (curkey == NULL)
+                               return ONAK_E_INVALID_PARAM;
+                       if (curkey->subkeys != NULL)
+                               return ONAK_E_INVALID_PARAM;
                        ADD_PACKET_TO_LIST_END(curkey,
                                uid,
                                packet_dup(packets->packet));
                        ADD_PACKET_TO_LIST_END(curkey,
                                uid,
                                packet_dup(packets->packet));
@@ -127,7 +130,8 @@ int parse_keys(struct openpgp_packet_list *packets,
                        /*
                         * It's a subkey packet.
                         */
                        /*
                         * It's a subkey packet.
                         */
-                       log_assert(curkey != NULL);
+                       if (curkey == NULL)
+                               return ONAK_E_INVALID_PARAM;
                        ADD_PACKET_TO_LIST_END(curkey,
                                subkey,
                                packet_dup(packets->packet));
                        ADD_PACKET_TO_LIST_END(curkey,
                                subkey,
                                packet_dup(packets->packet));
@@ -142,9 +146,8 @@ int parse_keys(struct openpgp_packet_list *packets,
                         */
                        break;
                default:
                         */
                        break;
                default:
-                       logthing(LOGTHING_ERROR,
-                                       "Unsupported packet type: %d",
-                                       packets->packet->tag);
+                       /* Unsupported packet. Do what? Ignore for now. */
+                       break;
                }
                packets = packets->next;
        }
                }
                packets = packets->next;
        }
@@ -181,7 +184,7 @@ int debug_packet(struct openpgp_packet *packet)
  *     packet stream and reads the packets into a linked list of packets
  *     ready for parsing as a public key or whatever.
  */
  *     packet stream and reads the packets into a linked list of packets
  *     ready for parsing as a public key or whatever.
  */
-int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
+onak_status_t read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                                void *c),
                                void *ctx,
                                struct openpgp_packet_list **packets,
                                void *c),
                                void *ctx,
                                struct openpgp_packet_list **packets,
@@ -189,11 +192,13 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
 {
        unsigned char                    curchar = 0;
        struct openpgp_packet_list      *curpacket = NULL;
 {
        unsigned char                    curchar = 0;
        struct openpgp_packet_list      *curpacket = NULL;
-       int                              rc = 0;
+       onak_status_t                    rc = ONAK_E_OK;
        int                              keys = 0;
        bool                             inpacket = false;
 
        int                              keys = 0;
        bool                             inpacket = false;
 
-       log_assert(packets != NULL);
+       if (packets == NULL)
+               return ONAK_E_INVALID_PARAM;
+
        curpacket = *packets;
        if (curpacket != NULL) {
                while (curpacket->next != NULL) {
        curpacket = *packets;
        if (curpacket != NULL) {
                while (curpacket->next != NULL) {
@@ -240,9 +245,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                                        curpacket->packet->length += 192;
                                } else if (curpacket->packet->length > 223 &&
                                        curpacket->packet->length < 255) {
                                        curpacket->packet->length += 192;
                                } else if (curpacket->packet->length > 223 &&
                                        curpacket->packet->length < 255) {
-                                       logthing(LOGTHING_NOTICE,
-                                               "Partial length;"
-                                               " not supported.");
+                                       return ONAK_E_UNSUPPORTED_FEATURE;
                                } else if (curpacket->packet->length == 255) {
                                        /*
                                         * 5 byte length; ie 255 followed by 3
                                } else if (curpacket->packet->length == 255) {
                                        /*
                                         * 5 byte length; ie 255 followed by 3
@@ -288,11 +291,9 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                                        curpacket->packet->length += curchar;
                                        break;
                                case 3:
                                        curpacket->packet->length += curchar;
                                        break;
                                case 3:
-                                       logthing(LOGTHING_ERROR,
-                                               "Unsupported length type 3.");
+                                       rc = ONAK_E_UNSUPPORTED_FEATURE;
                                        curpacket->packet->length = 0;
                                        curpacket->packet->data = NULL;
                                        curpacket->packet->length = 0;
                                        curpacket->packet->data = NULL;
-                                       rc = -1;
                                        break;
                                }
                        }
                                        break;
                                }
                        }
@@ -306,10 +307,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                                        malloc(curpacket->packet->length *
                                        sizeof(unsigned char));
                                if (curpacket->packet->data == NULL) {
                                        malloc(curpacket->packet->length *
                                        sizeof(unsigned char));
                                if (curpacket->packet->data == NULL) {
-                                       logthing(LOGTHING_ERROR, 
-                                               "Can't allocate memory for "
-                                               "packet!");
-                                       rc = -1;
+                                       rc = ONAK_E_NOMEM;
                                } else {
                                        rc = getchar_func(ctx,
                                                curpacket->packet->length,
                                } else {
                                        rc = getchar_func(ctx,
                                                curpacket->packet->length,
@@ -318,9 +316,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                        }
                        inpacket = false;
                } else {
                        }
                        inpacket = false;
                } else {
-                       logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
-                               curchar);
-                       rc = 1;
+                       rc = ONAK_E_INVALID_PKT;
                }
        }
 
                }
        }
 
@@ -336,7 +332,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
  *     This function uses putchar_func to write characters to an OpenPGP
  *     packet stream from a linked list of packets.
  */
  *     This function uses putchar_func to write characters to an OpenPGP
  *     packet stream from a linked list of packets.
  */
-int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
+onak_status_t write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
                                                void *c),
                                void *ctx,
                                struct openpgp_packet_list *packets)
                                                void *c),
                                void *ctx,
                                struct openpgp_packet_list *packets)
@@ -364,8 +360,6 @@ int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
                                putchar_func(ctx, 1, &curchar);
                        } else if (packets->packet->length > 8382 &&
                                packets->packet->length < 0xFFFFFFFF) {
                                putchar_func(ctx, 1, &curchar);
                        } else if (packets->packet->length > 8382 &&
                                packets->packet->length < 0xFFFFFFFF) {
-                               logthing(LOGTHING_DEBUG,
-                                       "Writing 5 byte length");
                                curchar = 255;
                                putchar_func(ctx, 1, &curchar);
                                
                                curchar = 255;
                                putchar_func(ctx, 1, &curchar);
                                
@@ -385,8 +379,7 @@ int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
                                curchar &= 0xFF;
                                putchar_func(ctx, 1, &curchar);
                        } else {
                                curchar &= 0xFF;
                                putchar_func(ctx, 1, &curchar);
                        } else {
-                               logthing(LOGTHING_ERROR,
-                                       "Unsupported new format length.");
+                               return ONAK_E_UNSUPPORTED_FEATURE;
                        }
                } else {
                        curchar |= (packets->packet->tag << 2);
                        }
                } else {
                        curchar |= (packets->packet->tag << 2);
@@ -419,7 +412,8 @@ int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
                                packets->packet->data);
                packets = packets->next;
        }
                                packets->packet->data);
                packets = packets->next;
        }
-       return 0;
+
+       return ONAK_E_OK;
 }
 
 /**
 }
 
 /**
index e9bea4114be8c28b79b819f7b45e240bda8d2b78..bc208d2a66cd912c8ee5485ea2821566617f2d30 100644 (file)
@@ -21,6 +21,7 @@
 #define __PARSEKEY_H__
 
 #include "keystructs.h"
 #define __PARSEKEY_H__
 
 #include "keystructs.h"
+#include "onak.h"
 
 /**
  *     parse_keys - Process a stream of packets for public keys + sigs.
 
 /**
  *     parse_keys - Process a stream of packets for public keys + sigs.
@@ -59,7 +60,7 @@ int debug_packet(struct openpgp_packet *packet);
  *     then only the public key component of the last key will be returned,
  *     none of the other packets of the key will be read.
  */
  *     then only the public key component of the last key will be returned,
  *     none of the other packets of the key will be read.
  */
-int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
+onak_status_t read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                                void *c),
                                void *ctx,
                                struct openpgp_packet_list **packets,
                                void *c),
                                void *ctx,
                                struct openpgp_packet_list **packets,
@@ -74,7 +75,7 @@ int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
  *     This function uses putchar_func to write characters to an OpenPGP
  *     packet stream from a linked list of packets.
  */
  *     This function uses putchar_func to write characters to an OpenPGP
  *     packet stream from a linked list of packets.
  */
-int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
+onak_status_t write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
                                                void *c),
                                void *ctx,
                                struct openpgp_packet_list *packets);
                                                void *c),
                                void *ctx,
                                struct openpgp_packet_list *packets);
index febc3f5441847be211fb02e40c405201c8789447..2501f4a10629557e41e6817d8aa98ccc5f801046 100644 (file)
--- a/photoid.c
+++ b/photoid.c
@@ -25,7 +25,7 @@
 #include "keyid.h"
 #include "keyindex.h"
 #include "keystructs.h"
 #include "keyid.h"
 #include "keyindex.h"
 #include "keystructs.h"
-#include "log.h"
+#include "onak.h"
 #include "photoid.h"
 
 /**
 #include "photoid.h"
 
 /**
  *     photo id NULL is returned. The returned data pointer refers to the key
  *     data supplied rather than a copy of it.
  */
  *     photo id NULL is returned. The returned data pointer refers to the key
  *     data supplied rather than a copy of it.
  */
-int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo,
-               size_t *length)
+onak_status_t getphoto(struct openpgp_publickey *key, int index,
+               unsigned char **photo, size_t *length)
 {
        struct openpgp_signedpacket_list *curuid = NULL;
        int                               i = 0;
        int                               j = 0;
 
 {
        struct openpgp_signedpacket_list *curuid = NULL;
        int                               i = 0;
        int                               j = 0;
 
-       log_assert(key != NULL);
-       log_assert(photo != NULL);
-       log_assert(length != NULL);
+       if (key == NULL || photo == NULL || length == NULL)
+               return ONAK_E_INVALID_PARAM;
 
        *photo = NULL;
        
 
        *photo = NULL;
        
@@ -76,8 +75,6 @@ int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo,
                                        *length <<= 8;
                                        *length += curuid->packet->data[j++];
                                }
                                        *length <<= 8;
                                        *length += curuid->packet->data[j++];
                                }
-                               logthing(LOGTHING_DEBUG, "Got photo, size %d",
-                                               *length);
                                j++;
                                *length -= 17;
                                *photo = &(curuid->packet->data[j+16]);
                                j++;
                                *length -= 17;
                                *photo = &(curuid->packet->data[j+16]);
@@ -88,5 +85,5 @@ int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo,
                curuid = curuid->next;
        }
 
                curuid = curuid->next;
        }
 
-       return (*photo != NULL);
+       return *photo == NULL ? ONAK_E_NOT_FOUND : ONAK_E_OK;
 }
 }
index aaafe34aeddd358d3673033abc77d89648ca77e2..0e0373b26548c555059495c2738ccac7b247281e 100644 (file)
--- a/photoid.h
+++ b/photoid.h
@@ -34,7 +34,7 @@
  *     photo id NULL is returned. The returned data pointer refers to the key
  *     data supplied rather than a copy of it.
  */
  *     photo id NULL is returned. The returned data pointer refers to the key
  *     data supplied rather than a copy of it.
  */
-int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo,
-               size_t *length);
+onak_status_t getphoto(struct openpgp_publickey *key, int index,
+               unsigned char **photo, size_t *length);
 
 #endif /* __PHOTOID_H__ */
 
 #endif /* __PHOTOID_H__ */
index 13aeb1d23b3d22014f855109db34495805edb189..e0fb5e38a56957699c70b7630ca1e7291b2e2a48 100644 (file)
@@ -63,7 +63,8 @@ int main(int argc, char** argv) {
   cleankeys( keys );
   /* Iterate over the keys... */
   for( key = keys; key; key = key->next ) {
   cleankeys( keys );
   /* Iterate over the keys... */
   for( key = keys; key; key = key->next ) {
-    uint64_t keyid = get_keyid( key );
+    uint64_t keyid;
+    get_keyid( key, &keyid );
     for( uid = key->uids; uid; uid = uid->next ) {
       REPEATTHISUID: 
       for( sig = uid->sigs, prevsig = NULL; 
     for( uid = key->uids; uid; uid = uid->next ) {
       REPEATTHISUID: 
       for( sig = uid->sigs, prevsig = NULL;