Explicitly use 32 bits for keyd socket commands
[onak.git] / keydb_keyd.c
index 32cf0f020d23bb1075f3f5a6a5f4096c712df1b0..e5d46a335a4b1ea17d3a13bf3c39a04d22de0666 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);
@@ -103,10 +103,23 @@ static void keyd_initdb(bool readonly)
  */
 static void keyd_cleanupdb(void)
 {
+       uint32_t cmd = KEYD_CMD_CLOSE;
+
+       if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
+               logthing(LOGTHING_CRITICAL,
+                               "Couldn't send close cmd: %s (%d)",
+                               strerror(errno),
+                               errno);
+       }
+
        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;
@@ -151,7 +164,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;
 
@@ -188,6 +201,27 @@ static int keyd_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
        return (count > 0) ? 1 : 0;
 }
 
+/**
+*      delete_key - Given a keyid delete the key from storage.
+*      @keyid: The keyid to delete.
+*      @intrans: If we're already in a transaction.
+*
+*      This function deletes a public key from whatever storage mechanism we
+*      are using. Returns 0 if the key existed.
+*/
+static int keyd_delete_key(uint64_t keyid, bool intrans)
+{
+       uint32_t cmd = KEYD_CMD_DELETE;
+
+       write(keyd_fd, &cmd, sizeof(cmd));
+       read(keyd_fd, &cmd, sizeof(cmd));
+       if (cmd == KEYD_REPLY_OK) {
+               write(keyd_fd, &keyid, sizeof(keyid));
+       }
+
+       return 0;
+}
+
 /**
  *     store_key - Takes a key and stores it.
  *     @publickey: A pointer to the public key to store.
@@ -209,13 +243,13 @@ 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);
        
        if (update) {
-               delete_key(keyid, false);
+               keyd_delete_key(keyid, false);
        }
 
        write(keyd_fd, &cmd, sizeof(cmd));
@@ -247,27 +281,6 @@ static int keyd_store_key(struct openpgp_publickey *publickey, bool intrans,
        return 0;
 }
 
-/**
- *     delete_key - Given a keyid delete the key from storage.
- *     @keyid: The keyid to delete.
- *     @intrans: If we're already in a transaction.
- *
- *     This function deletes a public key from whatever storage mechanism we
- *     are using. Returns 0 if the key existed.
- */
-static int keyd_delete_key(uint64_t keyid, bool intrans)
-{
-       int cmd = KEYD_CMD_DELETE;
-
-       write(keyd_fd, &cmd, sizeof(cmd));
-       read(keyd_fd, &cmd, sizeof(cmd));
-       if (cmd == KEYD_REPLY_OK) {
-               write(keyd_fd, &keyid, sizeof(keyid));
-       }
-       
-       return 0;
-}
-
 /**
  *     fetch_key_text - Trys to find the keys that contain the supplied text.
  *     @search: The text to search for.
@@ -281,7 +294,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;
 
@@ -331,7 +344,7 @@ 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));
@@ -360,7 +373,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;