]> git.sommitrealweird.co.uk Git - onak.git/blobdiff - keyd.c
Use nettle for hashing when available rather than internal MD5/SHA1 routines
[onak.git] / keyd.c
diff --git a/keyd.c b/keyd.c
index c2d83491ea91fc6e23dcd75ee57ff0a9bbef1ec9..5911ce9104a967a10cd9d46758223896b2a7dccd 100644 (file)
--- a/keyd.c
+++ b/keyd.c
@@ -1,9 +1,20 @@
 /*
  * keyd.c - key retrieval daemon
  *
 /*
  * keyd.c - key retrieval daemon
  *
- * Jonathan McDowell <noodles@earth.li>
+ * Copyright 2004,2011 Jonathan McDowell <noodles@earth.li>
  *
  *
- * Copyright 2004 Project Purple
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #include <errno.h>
  */
 
 #include <errno.h>
@@ -17,9 +28,9 @@
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <unistd.h>
 
-#include "config.h"
 #include "charfuncs.h"
 #include "cleanup.h"
 #include "keyd.h"
 #include "charfuncs.h"
 #include "cleanup.h"
 #include "keyd.h"
@@ -30,6 +41,9 @@
 #include "mem.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 #include "mem.h"
 #include "onak-conf.h"
 #include "parsekey.h"
+#include "version.h"
+
+static struct keyd_stats *stats;
 
 void daemonize(void)
 {
 
 void daemonize(void)
 {
@@ -137,6 +151,7 @@ int sock_do(int fd)
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_packet_list *list_end = NULL;
        struct buffer_ctx storebuf;
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_packet_list *list_end = NULL;
        struct buffer_ctx storebuf;
+       struct skshash hash;
 
        /*
         * Get the command from the client.
 
        /*
         * Get the command from the client.
@@ -150,6 +165,11 @@ int sock_do(int fd)
        }
        
        if (ret == 0) {
        }
        
        if (ret == 0) {
+               if (cmd < KEYD_CMD_LAST) {
+                       stats->command_stats[cmd]++;
+               } else {
+                       stats->command_stats[KEYD_CMD_UNKNOWN]++;
+               }
                switch (cmd) {
                case KEYD_CMD_VERSION:
                        cmd = KEYD_REPLY_OK;
                switch (cmd) {
                case KEYD_CMD_VERSION:
                        cmd = KEYD_REPLY_OK;
@@ -341,6 +361,61 @@ int sock_do(int fd)
                        ret = 1;
                        trytocleanup();
                        break;
                        ret = 1;
                        trytocleanup();
                        break;
+               case KEYD_CMD_STATS:
+                       cmd = KEYD_REPLY_OK;
+                       write(fd, &cmd, sizeof(cmd));
+                       cmd = sizeof(*stats);
+                       write(fd, &cmd, sizeof(cmd));
+                       write(fd, stats,
+                               sizeof(*stats));
+                       break;
+               case KEYD_CMD_GETSKSHASH:
+                       cmd = KEYD_REPLY_OK;
+                       write(fd, &cmd, sizeof(cmd));
+                       bytes = read(fd, hash.hash, sizeof(hash.hash));
+                       if (bytes != sizeof(hash.hash)) {
+                               ret = 1;
+                       }
+                       storebuf.offset = 0;
+                       if (ret == 0) {
+                               logthing(LOGTHING_INFO,
+                                               "Fetching by hash"
+                                               ", result: %d",
+                                               config.dbbackend->
+                                               fetch_key_skshash(&hash,
+                                                       &key));
+                               if (key != NULL) {
+                                       storebuf.size = 8192;
+                                       storebuf.buffer = malloc(8192);
+
+                                       flatten_publickey(key,
+                                                       &packets,
+                                                       &list_end);
+                                       write_openpgp_stream(buffer_putchar,
+                                                       &storebuf,
+                                                       packets);
+                                       logthing(LOGTHING_TRACE,
+                                                       "Sending %d bytes.",
+                                                       storebuf.offset);
+                                       write(fd, &storebuf.offset,
+                                               sizeof(storebuf.offset));
+                                       write(fd, storebuf.buffer,
+                                               storebuf.offset);
+
+                                       free(storebuf.buffer);
+                                       storebuf.buffer = NULL;
+                                       storebuf.size = storebuf.offset = 0;
+                                       free_packet_list(packets);
+                                       packets = list_end = NULL;
+                                       free_publickey(key);
+                                       key = NULL;
+                               } else {
+                                       write(fd, &storebuf.offset,
+                                               sizeof(storebuf.offset));
+                               }
+                       }
+                       break;
+
                default:
                        logthing(LOGTHING_ERROR, "Got unknown command: %d",
                                        cmd);
                default:
                        logthing(LOGTHING_ERROR, "Got unknown command: %d",
                                        cmd);
@@ -372,6 +447,7 @@ int sock_accept(int fd)
        }
 
        if (ret != -1) {
        }
 
        if (ret != -1) {
+               stats->connects++;
                while (!sock_do(srv)) ;
                sock_close(srv);
        }
                while (!sock_do(srv)) ;
                sock_close(srv);
        }
@@ -381,7 +457,7 @@ int sock_accept(int fd)
 
 static void usage(void)
 {
 
 static void usage(void)
 {
-       puts("keyd " PACKAGE_VERSION " - backend key serving daemon for the "
+       puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
                "onak PGP keyserver.\n");
        puts("Usage:\n");
        puts("\tkeyd [options]\n");
                "onak PGP keyserver.\n");
        puts("Usage:\n");
        puts("\tkeyd [options]\n");
@@ -429,6 +505,15 @@ int main(int argc, char *argv[])
        catchsignals();
        signal(SIGPIPE, SIG_IGN);
 
        catchsignals();
        signal(SIGPIPE, SIG_IGN);
 
+
+       stats = calloc(1, sizeof(*stats));
+       if (!stats) {
+               logthing(LOGTHING_ERROR,
+                       "Couldn't allocate memory for stats structure.");
+               exit(EXIT_FAILURE);
+       }
+       stats->started = time(NULL);
+
        snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
        fd = sock_init(sockname);
 
        snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
        fd = sock_init(sockname);
 
@@ -449,6 +534,8 @@ int main(int argc, char *argv[])
                unlink(sockname);
        }
 
                unlink(sockname);
        }
 
+       free(stats);
+
        cleanuplogthing();
        cleanupconfig();
 
        cleanuplogthing();
        cleanupconfig();