]> git.sommitrealweird.co.uk Git - onak.git/blobdiff - keydb_keyd.c
Ignore SIGPIPE in keyd
[onak.git] / keydb_keyd.c
index dcdcb85f3669eeb6919a92de3dedefd7cec5a3cd..2a9faafa40cc72c9a21d0fcb698819e94a6c5345 100644 (file)
@@ -42,8 +42,8 @@ static int keyd_fd = -1;
 static void keyd_initdb(bool readonly)
 {
        struct sockaddr_un sock;
-       int                cmd = KEYD_CMD_UNKNOWN;
-       int                reply = KEYD_REPLY_UNKNOWN_CMD;
+       uint32_t           cmd = KEYD_CMD_UNKNOWN;
+       uint32_t           reply = KEYD_REPLY_UNKNOWN_CMD;
        ssize_t            count;
 
        keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
@@ -76,18 +76,25 @@ static void keyd_initdb(bool readonly)
                                errno);
        } else {
                count = read(keyd_fd, &reply, sizeof(reply));
-               if (count == sizeof(reply)) {
-                       if (reply == KEYD_REPLY_OK) {
-                               count = read(keyd_fd, &reply, sizeof(reply));
-                               logthing(LOGTHING_DEBUG,
-                                               "keyd protocol version %d",
-                                               reply);
-                               if (reply != keyd_version) {
-                                       logthing(LOGTHING_CRITICAL,
-                                               "Error! keyd protocol version "
-                                               "mismatch. (us = %d, it = %d)",
+               if (count == sizeof(reply) && reply == KEYD_REPLY_OK) {
+                       count = read(keyd_fd, &reply, sizeof(reply));
+                       if (count != sizeof(reply) || reply != sizeof(reply)) {
+                               logthing(LOGTHING_CRITICAL,
+                                       "Error! Unexpected keyd version "
+                                       "length: %d != %d",
+                                       reply, sizeof(reply));
+                               exit(EXIT_FAILURE);
+                       }
+
+                       count = read(keyd_fd, &reply, sizeof(reply));
+                       logthing(LOGTHING_DEBUG,
+                                       "keyd protocol version %d",
+                                       reply);
+                       if (reply != keyd_version) {
+                               logthing(LOGTHING_CRITICAL,
+                                       "Error! keyd protocol version "
+                                       "mismatch. (us = %d, it = %d)",
                                                keyd_version, reply);
-                               }
                        }
                }
        }
@@ -103,7 +110,7 @@ static void keyd_initdb(bool readonly)
  */
 static void keyd_cleanupdb(void)
 {
-       int cmd = KEYD_CMD_CLOSE;
+       uint32_t cmd = KEYD_CMD_CLOSE;
 
        if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
                logthing(LOGTHING_CRITICAL,
@@ -111,11 +118,25 @@ static void keyd_cleanupdb(void)
                                strerror(errno),
                                errno);
        }
+       
+       if (read(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't read close cmd reply: %s (%d)",
+                       strerror(errno),
+                       errno);
+       } else if (cmd != KEYD_REPLY_OK) {
+               logthing(LOGTHING_CRITICAL,
+                       "Got bad reply to KEYD_CMD_CLOSE: %d", cmd);
+       }
 
        if (shutdown(keyd_fd, SHUT_RDWR) < 0) {
                logthing(LOGTHING_NOTICE, "Error shutting down socket: %d",
                                errno);
        }
+       if (close(keyd_fd) < 0) {
+               logthing(LOGTHING_NOTICE, "Error closing down socket: %d",
+                               errno);
+       }
        keyd_fd = -1;
 
        return;
@@ -160,7 +181,7 @@ static int keyd_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
 {
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
-       int                         cmd = KEYD_CMD_GET;
+       uint32_t                    cmd = KEYD_CMD_GET;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
 
@@ -207,7 +228,7 @@ static int keyd_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
 */
 static int keyd_delete_key(uint64_t keyid, bool intrans)
 {
-       int cmd = KEYD_CMD_DELETE;
+       uint32_t cmd = KEYD_CMD_DELETE;
 
        write(keyd_fd, &cmd, sizeof(cmd));
        read(keyd_fd, &cmd, sizeof(cmd));
@@ -239,7 +260,7 @@ static int keyd_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;
-       int                         cmd = KEYD_CMD_STORE;
+       uint32_t                    cmd = KEYD_CMD_STORE;
        uint64_t                    keyid;
 
        keyid = get_keyid(publickey);
@@ -290,7 +311,7 @@ static int keyd_fetch_key_text(const char *search,
 {
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
-       int                         cmd = KEYD_CMD_GETTEXT;
+       uint32_t                    cmd = KEYD_CMD_GETTEXT;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
 
@@ -340,12 +361,16 @@ static int keyd_fetch_key_text(const char *search,
  */
 static uint64_t keyd_getfullkeyid(uint64_t keyid)
 {
-       int cmd = KEYD_CMD_GETFULLKEYID;
+       uint32_t cmd = KEYD_CMD_GETFULLKEYID;
 
        write(keyd_fd, &cmd, sizeof(cmd));
        read(keyd_fd, &cmd, sizeof(cmd));
        if (cmd == KEYD_REPLY_OK) {
                write(keyd_fd, &keyid, sizeof(keyid));
+               read(keyd_fd, &cmd, sizeof(cmd));
+               if (cmd != sizeof(keyid)) {
+                       return 0;
+               }
                read(keyd_fd, &keyid, sizeof(keyid));
        }
 
@@ -369,7 +394,7 @@ static int keyd_iterate_keys(void (*iterfunc)(void *ctx,
        struct buffer_ctx           keybuf;
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_publickey   *key = NULL;
-       int                         cmd = KEYD_CMD_KEYITER;
+       uint32_t                    cmd = KEYD_CMD_KEYITER;
        ssize_t                     bytes = 0;
        ssize_t                     count = 0;
        int                         numkeys = 0;