]> git.sommitrealweird.co.uk Git - onak.git/commitdiff
Make keyd command handling more consistent
authorJonathan McDowell <noodles@earth.li>
Wed, 30 Mar 2011 06:28:07 +0000 (23:28 -0700)
committerJonathan McDowell <noodles@earth.li>
Wed, 30 Mar 2011 06:28:07 +0000 (23:28 -0700)
  Output KEYD_REPLY_OK response for close/quite commands as well
  as data size for version/longkeyid commands. Means that commands
  can always expect to get an OK response if they've been understood
  and a size if they're going to result in some more data.

keyd.c
keydb_keyd.c

diff --git a/keyd.c b/keyd.c
index 987724db53320bae70f708bf7f68d2a438eddd35..919b61b899a886609f29caebf86e841b725c9fce 100644 (file)
--- a/keyd.c
+++ b/keyd.c
@@ -152,6 +152,8 @@ int sock_do(int fd)
                case KEYD_CMD_VERSION:
                        cmd = KEYD_REPLY_OK;
                        write(fd, &cmd, sizeof(cmd));
                case KEYD_CMD_VERSION:
                        cmd = KEYD_REPLY_OK;
                        write(fd, &cmd, sizeof(cmd));
+                       cmd = sizeof(keyd_version);
+                       write(fd, &cmd, sizeof(cmd));
                        write(fd, &keyd_version, sizeof(keyd_version));
                        break;
                case KEYD_CMD_GET:
                        write(fd, &keyd_version, sizeof(keyd_version));
                        break;
                case KEYD_CMD_GET:
@@ -311,6 +313,8 @@ int sock_do(int fd)
                        }
                        if (ret == 0) {
                                keyid = config.dbbackend->getfullkeyid(keyid);
                        }
                        if (ret == 0) {
                                keyid = config.dbbackend->getfullkeyid(keyid);
+                               cmd = sizeof(keyid);
+                               write(fd, &cmd, sizeof(cmd));
                                write(fd, &keyid, sizeof(keyid));
                        }
                        break;
                                write(fd, &keyid, sizeof(keyid));
                        }
                        break;
@@ -323,9 +327,14 @@ int sock_do(int fd)
                        write(fd, &bytes, sizeof(bytes));
                        break;
                case KEYD_CMD_CLOSE:
                        write(fd, &bytes, sizeof(bytes));
                        break;
                case KEYD_CMD_CLOSE:
+                       cmd = KEYD_REPLY_OK;
+                       write(fd, &cmd, sizeof(cmd));
                        ret = 1;
                        break;
                case KEYD_CMD_QUIT:
                        ret = 1;
                        break;
                case KEYD_CMD_QUIT:
+                       cmd = KEYD_REPLY_OK;
+                       write(fd, &cmd, sizeof(cmd));
+                       ret = 1;
                        trytocleanup();
                        break;
                default:
                        trytocleanup();
                        break;
                default:
index e5d46a335a4b1ea17d3a13bf3c39a04d22de0666..2a9faafa40cc72c9a21d0fcb698819e94a6c5345 100644 (file)
@@ -76,18 +76,25 @@ static void keyd_initdb(bool readonly)
                                errno);
        } else {
                count = read(keyd_fd, &reply, sizeof(reply));
                                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);
                                                keyd_version, reply);
-                               }
                        }
                }
        }
                        }
                }
        }
@@ -111,6 +118,16 @@ static void keyd_cleanupdb(void)
                                strerror(errno),
                                errno);
        }
                                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",
 
        if (shutdown(keyd_fd, SHUT_RDWR) < 0) {
                logthing(LOGTHING_NOTICE, "Error shutting down socket: %d",
@@ -350,6 +367,10 @@ static uint64_t keyd_getfullkeyid(uint64_t keyid)
        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 == 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));
        }
 
                read(keyd_fd, &keyid, sizeof(keyid));
        }