X-Git-Url: http://the.earth.li/gitweb/?a=blobdiff_plain;f=keyd.c;h=4528c96fef874963542b56eccac17896887ab493;hb=f31f6d8cf4bda52b8ef1b1d1d280884e904429a9;hp=c2d83491ea91fc6e23dcd75ee57ff0a9bbef1ec9;hpb=28d25177e72f84408c99072fbe6cf05b4fc556fb;p=onak.git diff --git a/keyd.c b/keyd.c index c2d8349..4528c96 100644 --- a/keyd.c +++ b/keyd.c @@ -1,9 +1,20 @@ /* * keyd.c - key retrieval daemon * - * Jonathan McDowell + * Copyright 2004,2011 Jonathan McDowell * - * 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 @@ -17,9 +28,9 @@ #include #include #include +#include #include -#include "config.h" #include "charfuncs.h" #include "cleanup.h" #include "keyd.h" @@ -30,6 +41,12 @@ #include "mem.h" #include "onak-conf.h" #include "parsekey.h" +#include "version.h" + +/* Maximum number of clients we're prepared to accept at once */ +#define MAX_CLIENTS 16 + +static struct keyd_stats *stats; void daemonize(void) { @@ -64,15 +81,17 @@ void iteratefunc(void *ctx, struct openpgp_publickey *key) 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); + get_keyid(key, &keyid); logthing(LOGTHING_TRACE, "Iterating over 0x%016" PRIX64 ".", - get_keyid(key)); + keyid); flatten_publickey(key, &packets, @@ -108,7 +127,7 @@ int sock_init(const char *sockname) fd = socket(PF_UNIX, SOCK_STREAM, 0); if (fd != -1) { - ret = fcntl(fd, F_SETFD, 1); + ret = fcntl(fd, F_SETFD, FD_CLOEXEC); } if (ret != -1) { @@ -137,6 +156,7 @@ int sock_do(int fd) 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. @@ -150,6 +170,11 @@ int sock_do(int fd) } 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; @@ -341,6 +366,61 @@ int sock_do(int fd) 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); @@ -368,20 +448,19 @@ int sock_accept(int fd) socklen = sizeof(sock); srv = accept(fd, (struct sockaddr *) &sock, &socklen); if (srv != -1) { - ret = fcntl(srv, F_SETFD, 1); + ret = fcntl(srv, F_SETFD, FD_CLOEXEC); } if (ret != -1) { - while (!sock_do(srv)) ; - sock_close(srv); + stats->connects++; } - return 1; + return (srv); } 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"); @@ -394,7 +473,7 @@ static void usage(void) int main(int argc, char *argv[]) { - int fd = -1; + int fd = -1, maxfd, i, clients[MAX_CLIENTS]; fd_set rfds; char sockname[1024]; char *configfile = NULL; @@ -429,26 +508,79 @@ int main(int argc, char *argv[]) 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); if (fd != -1) { FD_ZERO(&rfds); FD_SET(fd, &rfds); + maxfd = fd; + memset(clients, -1, sizeof (clients)); config.dbbackend->initdb(false); logthing(LOGTHING_NOTICE, "Accepting connections."); - while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) { - logthing(LOGTHING_INFO, "Accepted connection."); - sock_accept(fd); + while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) { + /* + * Deal with existing clients first; if we're at our + * connection limit then processing them might free + * things up and let us accept the next client below. + */ + for (i = 0; i < MAX_CLIENTS; i++) { + if (clients[i] != -1 && + FD_ISSET(clients[i], &rfds)) { + logthing(LOGTHING_DEBUG, + "Handling connection for client %d.", i); + if (sock_do(clients[i])) { + sock_close(clients[i]); + clients[i] = -1; + logthing(LOGTHING_DEBUG, + "Closed connection for client %d.", i); + } + } + } + /* + * Check if we have a new incoming connection to accept. + */ + if (FD_ISSET(fd, &rfds)) { + for (i = 0; i < MAX_CLIENTS; i++) { + if (clients[i] == -1) { + break; + } + } + if (i < MAX_CLIENTS) { + logthing(LOGTHING_INFO, + "Accepted connection %d.", i); + clients[i] = sock_accept(fd); + } + } + FD_ZERO(&rfds); FD_SET(fd, &rfds); + maxfd = fd; + for (i = 0; i < MAX_CLIENTS; i++) { + if (clients[i] != -1) { + FD_SET(clients[i], &rfds); + maxfd = (maxfd > clients[i]) ? + maxfd : clients[i]; + } + } } config.dbbackend->cleanupdb(); sock_close(fd); unlink(sockname); } + free(stats); + cleanuplogthing(); cleanupconfig();