2 * keyd.c - key retrieval daemon
4 * Copyright 2004,2011 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <sys/select.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
34 #include "charfuncs.h"
39 #include "keystructs.h"
42 #include "onak-conf.h"
46 /* Maximum number of clients we're prepared to accept at once */
47 #define MAX_CLIENTS 16
49 static struct keyd_stats *stats;
58 logthing(LOGTHING_CRITICAL,
59 "Failed to fork into background: %d (%s)",
64 logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
70 freopen("/dev/null", "r", stdin);
71 freopen("/dev/null", "w", stdout);
72 freopen("/dev/null", "w", stderr);
77 void iteratefunc(void *ctx, struct openpgp_publickey *key)
79 struct openpgp_packet_list *packets = NULL;
80 struct openpgp_packet_list *list_end = NULL;
81 struct buffer_ctx storebuf;
83 int *fd = (int *) ctx;
89 storebuf.buffer = malloc(8192);
91 get_keyid(key, &keyid);
92 logthing(LOGTHING_TRACE,
93 "Iterating over 0x%016" PRIX64 ".",
96 flatten_publickey(key,
99 write_openpgp_stream(buffer_putchar,
102 logthing(LOGTHING_TRACE,
105 ret = write(*fd, &storebuf.offset,
106 sizeof(storebuf.offset));
108 write(*fd, storebuf.buffer,
112 free(storebuf.buffer);
113 storebuf.buffer = NULL;
114 storebuf.size = storebuf.offset = 0;
115 free_packet_list(packets);
116 packets = list_end = NULL;
122 int sock_init(const char *sockname)
124 struct sockaddr_un sock;
128 fd = socket(PF_UNIX, SOCK_STREAM, 0);
130 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
134 sock.sun_family = AF_UNIX;
135 strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
137 ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
151 int sock_do(struct onak_dbctx *dbctx, int fd)
153 uint32_t cmd = KEYD_CMD_UNKNOWN;
159 struct openpgp_publickey *key = NULL;
160 struct openpgp_packet_list *packets = NULL;
161 struct openpgp_packet_list *list_end = NULL;
162 struct buffer_ctx storebuf;
164 struct openpgp_fingerprint fingerprint;
167 * Get the command from the client.
169 bytes = read(fd, &cmd, sizeof(cmd));
171 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
173 if (bytes != sizeof(cmd)) {
178 if (cmd < KEYD_CMD_LAST) {
179 stats->command_stats[cmd]++;
181 stats->command_stats[KEYD_CMD_UNKNOWN]++;
184 case KEYD_CMD_VERSION:
186 write(fd, &cmd, sizeof(cmd));
187 cmd = sizeof(keyd_version);
188 write(fd, &cmd, sizeof(cmd));
189 write(fd, &keyd_version, sizeof(keyd_version));
191 case KEYD_CMD_GET_ID:
193 write(fd, &cmd, sizeof(cmd));
194 bytes = read(fd, &keyid, sizeof(keyid));
195 if (bytes != sizeof(keyid)) {
200 logthing(LOGTHING_INFO,
201 "Fetching 0x%" PRIX64
204 dbctx->fetch_key_id(dbctx,
208 storebuf.size = 8192;
209 storebuf.buffer = malloc(8192);
211 flatten_publickey(key,
214 write_openpgp_stream(buffer_putchar,
217 logthing(LOGTHING_TRACE,
220 write(fd, &storebuf.offset,
221 sizeof(storebuf.offset));
222 write(fd, storebuf.buffer,
225 free(storebuf.buffer);
226 storebuf.buffer = NULL;
227 storebuf.size = storebuf.offset = 0;
228 free_packet_list(packets);
229 packets = list_end = NULL;
233 write(fd, &storebuf.offset,
234 sizeof(storebuf.offset));
238 case KEYD_CMD_GET_FP:
240 write(fd, &cmd, sizeof(cmd));
242 if (bytes > MAX_FINGERPRINT_LEN) {
245 fingerprint.length = bytes;
246 read(fd, fingerprint.fp, bytes);
250 logthing(LOGTHING_INFO,
251 "Fetching by fingerprint"
253 dbctx->fetch_key_fp(dbctx,
257 storebuf.size = 8192;
258 storebuf.buffer = malloc(8192);
260 flatten_publickey(key,
263 write_openpgp_stream(buffer_putchar,
266 logthing(LOGTHING_TRACE,
269 write(fd, &storebuf.offset,
270 sizeof(storebuf.offset));
271 write(fd, storebuf.buffer,
274 free(storebuf.buffer);
275 storebuf.buffer = NULL;
276 storebuf.size = storebuf.offset = 0;
277 free_packet_list(packets);
278 packets = list_end = NULL;
282 write(fd, &storebuf.offset,
283 sizeof(storebuf.offset));
288 case KEYD_CMD_GET_TEXT:
290 write(fd, &cmd, sizeof(cmd));
291 bytes = read(fd, &count, sizeof(count));
292 if (bytes != sizeof(count)) {
297 search = malloc(count+1);
298 read(fd, search, count);
300 logthing(LOGTHING_INFO,
301 "Fetching %s, result: %d",
303 dbctx->fetch_key_text(dbctx,
306 storebuf.size = 8192;
307 storebuf.buffer = malloc(8192);
309 flatten_publickey(key,
312 write_openpgp_stream(buffer_putchar,
315 logthing(LOGTHING_TRACE,
318 write(fd, &storebuf.offset,
319 sizeof(storebuf.offset));
320 write(fd, storebuf.buffer,
323 free(storebuf.buffer);
324 storebuf.buffer = NULL;
325 storebuf.size = storebuf.offset = 0;
326 free_packet_list(packets);
327 packets = list_end = NULL;
331 write(fd, &storebuf.offset,
332 sizeof(storebuf.offset));
339 write(fd, &cmd, sizeof(cmd));
341 bytes = read(fd, &storebuf.size,
342 sizeof(storebuf.size));
343 logthing(LOGTHING_TRACE, "Reading %d bytes.",
345 if (bytes != sizeof(storebuf.size)) {
348 if (ret == 0 && storebuf.size > 0) {
349 storebuf.buffer = malloc(storebuf.size);
351 while (bytes >= 0 && count < storebuf.size) {
353 &storebuf.buffer[count],
354 storebuf.size - count);
355 logthing(LOGTHING_TRACE,
360 read_openpgp_stream(buffer_fetchchar,
364 parse_keys(packets, &key);
365 dbctx->store_key(dbctx, key, false, false);
366 free_packet_list(packets);
370 free(storebuf.buffer);
371 storebuf.buffer = NULL;
372 storebuf.size = storebuf.offset = 0;
375 case KEYD_CMD_DELETE:
377 write(fd, &cmd, sizeof(cmd));
378 bytes = read(fd, &keyid, sizeof(keyid));
379 if (bytes != sizeof(keyid)) {
383 logthing(LOGTHING_INFO,
384 "Deleting 0x%" PRIX64
387 dbctx->delete_key(dbctx,
391 case KEYD_CMD_GETFULLKEYID:
393 write(fd, &cmd, sizeof(cmd));
394 bytes = read(fd, &keyid, sizeof(keyid));
395 if (bytes != sizeof(keyid)) {
399 keyid = dbctx->getfullkeyid(dbctx, keyid);
401 write(fd, &cmd, sizeof(cmd));
402 write(fd, &keyid, sizeof(keyid));
405 case KEYD_CMD_KEYITER:
407 write(fd, &cmd, sizeof(cmd));
408 dbctx->iterate_keys(dbctx, iteratefunc,
411 write(fd, &bytes, sizeof(bytes));
415 write(fd, &cmd, sizeof(cmd));
420 write(fd, &cmd, sizeof(cmd));
421 logthing(LOGTHING_NOTICE,
422 "Exiting due to quit request.");
428 write(fd, &cmd, sizeof(cmd));
429 cmd = sizeof(*stats);
430 write(fd, &cmd, sizeof(cmd));
434 case KEYD_CMD_GET_SKSHASH:
436 write(fd, &cmd, sizeof(cmd));
437 bytes = read(fd, hash.hash, sizeof(hash.hash));
438 if (bytes != sizeof(hash.hash)) {
443 logthing(LOGTHING_INFO,
446 dbctx->fetch_key_skshash(dbctx,
449 storebuf.size = 8192;
450 storebuf.buffer = malloc(8192);
452 flatten_publickey(key,
455 write_openpgp_stream(buffer_putchar,
458 logthing(LOGTHING_TRACE,
461 write(fd, &storebuf.offset,
462 sizeof(storebuf.offset));
463 write(fd, storebuf.buffer,
466 free(storebuf.buffer);
467 storebuf.buffer = NULL;
468 storebuf.size = storebuf.offset = 0;
469 free_packet_list(packets);
470 packets = list_end = NULL;
474 write(fd, &storebuf.offset,
475 sizeof(storebuf.offset));
481 logthing(LOGTHING_ERROR, "Got unknown command: %d",
483 cmd = KEYD_REPLY_UNKNOWN_CMD;
484 write(fd, &cmd, sizeof(cmd));
491 int sock_close(int fd)
493 shutdown(fd, SHUT_RDWR);
497 int sock_accept(int fd)
499 struct sockaddr_un sock;
504 socklen = sizeof(sock);
505 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
507 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
517 static void usage(void)
519 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
520 "onak PGP keyserver.\n");
522 puts("\tkeyd [options]\n");
523 puts("\tOptions:\n:");
524 puts("-c <file> - use <file> as the config file");
525 puts("-f - run in the foreground");
526 puts("-h - show this help text");
530 int main(int argc, char *argv[])
532 int fd = -1, maxfd, i, clients[MAX_CLIENTS];
535 char *configfile = NULL;
536 bool foreground = false;
538 struct onak_dbctx *dbctx;
540 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
543 if (configfile != NULL) {
546 configfile = strdup(optarg);
558 readconfig(configfile);
561 initlogthing("keyd", config.logfile);
562 config.use_keyd = false;
569 signal(SIGPIPE, SIG_IGN);
572 stats = calloc(1, sizeof(*stats));
574 logthing(LOGTHING_ERROR,
575 "Couldn't allocate memory for stats structure.");
578 stats->started = time(NULL);
580 snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
581 fd = sock_init(sockname);
587 memset(clients, -1, sizeof (clients));
589 dbctx = config.dbinit(false);
591 logthing(LOGTHING_NOTICE, "Accepting connections.");
592 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
594 * Deal with existing clients first; if we're at our
595 * connection limit then processing them might free
596 * things up and let us accept the next client below.
598 for (i = 0; i < MAX_CLIENTS; i++) {
599 if (clients[i] != -1 &&
600 FD_ISSET(clients[i], &rfds)) {
601 logthing(LOGTHING_DEBUG,
602 "Handling connection for client %d.", i);
603 if (sock_do(dbctx, clients[i])) {
604 sock_close(clients[i]);
606 logthing(LOGTHING_DEBUG,
607 "Closed connection for client %d.", i);
612 * Check if we have a new incoming connection to accept.
614 if (FD_ISSET(fd, &rfds)) {
615 for (i = 0; i < MAX_CLIENTS; i++) {
616 if (clients[i] == -1) {
620 if (i < MAX_CLIENTS) {
621 logthing(LOGTHING_INFO,
622 "Accepted connection %d.", i);
623 clients[i] = sock_accept(fd);
629 for (i = 0; i < MAX_CLIENTS; i++) {
630 if (clients[i] != -1) {
631 FD_SET(clients[i], &rfds);
632 maxfd = (maxfd > clients[i]) ?
637 dbctx->cleanupdb(dbctx);
647 return(EXIT_SUCCESS);