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;
158 uint8_t fp[MAX_FINGERPRINT_LEN];
160 struct openpgp_publickey *key = NULL;
161 struct openpgp_packet_list *packets = NULL;
162 struct openpgp_packet_list *list_end = NULL;
163 struct buffer_ctx storebuf;
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) {
249 logthing(LOGTHING_INFO,
250 "Fetching by fingerprint"
252 dbctx->fetch_key_fp(dbctx,
256 storebuf.size = 8192;
257 storebuf.buffer = malloc(8192);
259 flatten_publickey(key,
262 write_openpgp_stream(buffer_putchar,
265 logthing(LOGTHING_TRACE,
268 write(fd, &storebuf.offset,
269 sizeof(storebuf.offset));
270 write(fd, storebuf.buffer,
273 free(storebuf.buffer);
274 storebuf.buffer = NULL;
275 storebuf.size = storebuf.offset = 0;
276 free_packet_list(packets);
277 packets = list_end = NULL;
281 write(fd, &storebuf.offset,
282 sizeof(storebuf.offset));
287 case KEYD_CMD_GET_TEXT:
289 write(fd, &cmd, sizeof(cmd));
290 bytes = read(fd, &count, sizeof(count));
291 if (bytes != sizeof(count)) {
296 search = malloc(count+1);
297 read(fd, search, count);
299 logthing(LOGTHING_INFO,
300 "Fetching %s, result: %d",
302 dbctx->fetch_key_text(dbctx,
305 storebuf.size = 8192;
306 storebuf.buffer = malloc(8192);
308 flatten_publickey(key,
311 write_openpgp_stream(buffer_putchar,
314 logthing(LOGTHING_TRACE,
317 write(fd, &storebuf.offset,
318 sizeof(storebuf.offset));
319 write(fd, storebuf.buffer,
322 free(storebuf.buffer);
323 storebuf.buffer = NULL;
324 storebuf.size = storebuf.offset = 0;
325 free_packet_list(packets);
326 packets = list_end = NULL;
330 write(fd, &storebuf.offset,
331 sizeof(storebuf.offset));
338 write(fd, &cmd, sizeof(cmd));
340 bytes = read(fd, &storebuf.size,
341 sizeof(storebuf.size));
342 logthing(LOGTHING_TRACE, "Reading %d bytes.",
344 if (bytes != sizeof(storebuf.size)) {
347 if (ret == 0 && storebuf.size > 0) {
348 storebuf.buffer = malloc(storebuf.size);
350 while (bytes >= 0 && count < storebuf.size) {
352 &storebuf.buffer[count],
353 storebuf.size - count);
354 logthing(LOGTHING_TRACE,
359 read_openpgp_stream(buffer_fetchchar,
363 parse_keys(packets, &key);
364 dbctx->store_key(dbctx, key, false, false);
365 free_packet_list(packets);
369 free(storebuf.buffer);
370 storebuf.buffer = NULL;
371 storebuf.size = storebuf.offset = 0;
374 case KEYD_CMD_DELETE:
376 write(fd, &cmd, sizeof(cmd));
377 bytes = read(fd, &keyid, sizeof(keyid));
378 if (bytes != sizeof(keyid)) {
382 logthing(LOGTHING_INFO,
383 "Deleting 0x%" PRIX64
386 dbctx->delete_key(dbctx,
390 case KEYD_CMD_GETFULLKEYID:
392 write(fd, &cmd, sizeof(cmd));
393 bytes = read(fd, &keyid, sizeof(keyid));
394 if (bytes != sizeof(keyid)) {
398 keyid = dbctx->getfullkeyid(dbctx, keyid);
400 write(fd, &cmd, sizeof(cmd));
401 write(fd, &keyid, sizeof(keyid));
404 case KEYD_CMD_KEYITER:
406 write(fd, &cmd, sizeof(cmd));
407 dbctx->iterate_keys(dbctx, iteratefunc,
410 write(fd, &bytes, sizeof(bytes));
414 write(fd, &cmd, sizeof(cmd));
419 write(fd, &cmd, sizeof(cmd));
420 logthing(LOGTHING_NOTICE,
421 "Exiting due to quit request.");
427 write(fd, &cmd, sizeof(cmd));
428 cmd = sizeof(*stats);
429 write(fd, &cmd, sizeof(cmd));
433 case KEYD_CMD_GET_SKSHASH:
435 write(fd, &cmd, sizeof(cmd));
436 bytes = read(fd, hash.hash, sizeof(hash.hash));
437 if (bytes != sizeof(hash.hash)) {
442 logthing(LOGTHING_INFO,
445 dbctx->fetch_key_skshash(dbctx,
448 storebuf.size = 8192;
449 storebuf.buffer = malloc(8192);
451 flatten_publickey(key,
454 write_openpgp_stream(buffer_putchar,
457 logthing(LOGTHING_TRACE,
460 write(fd, &storebuf.offset,
461 sizeof(storebuf.offset));
462 write(fd, storebuf.buffer,
465 free(storebuf.buffer);
466 storebuf.buffer = NULL;
467 storebuf.size = storebuf.offset = 0;
468 free_packet_list(packets);
469 packets = list_end = NULL;
473 write(fd, &storebuf.offset,
474 sizeof(storebuf.offset));
480 logthing(LOGTHING_ERROR, "Got unknown command: %d",
482 cmd = KEYD_REPLY_UNKNOWN_CMD;
483 write(fd, &cmd, sizeof(cmd));
490 int sock_close(int fd)
492 shutdown(fd, SHUT_RDWR);
496 int sock_accept(int fd)
498 struct sockaddr_un sock;
503 socklen = sizeof(sock);
504 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
506 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
516 static void usage(void)
518 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
519 "onak PGP keyserver.\n");
521 puts("\tkeyd [options]\n");
522 puts("\tOptions:\n:");
523 puts("-c <file> - use <file> as the config file");
524 puts("-f - run in the foreground");
525 puts("-h - show this help text");
529 int main(int argc, char *argv[])
531 int fd = -1, maxfd, i, clients[MAX_CLIENTS];
534 char *configfile = NULL;
535 bool foreground = false;
537 struct onak_dbctx *dbctx;
539 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
542 if (configfile != NULL) {
545 configfile = strdup(optarg);
557 readconfig(configfile);
560 initlogthing("keyd", config.logfile);
561 config.use_keyd = false;
568 signal(SIGPIPE, SIG_IGN);
571 stats = calloc(1, sizeof(*stats));
573 logthing(LOGTHING_ERROR,
574 "Couldn't allocate memory for stats structure.");
577 stats->started = time(NULL);
579 snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
580 fd = sock_init(sockname);
586 memset(clients, -1, sizeof (clients));
588 dbctx = config.dbinit(false);
590 logthing(LOGTHING_NOTICE, "Accepting connections.");
591 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
593 * Deal with existing clients first; if we're at our
594 * connection limit then processing them might free
595 * things up and let us accept the next client below.
597 for (i = 0; i < MAX_CLIENTS; i++) {
598 if (clients[i] != -1 &&
599 FD_ISSET(clients[i], &rfds)) {
600 logthing(LOGTHING_DEBUG,
601 "Handling connection for client %d.", i);
602 if (sock_do(dbctx, clients[i])) {
603 sock_close(clients[i]);
605 logthing(LOGTHING_DEBUG,
606 "Closed connection for client %d.", i);
611 * Check if we have a new incoming connection to accept.
613 if (FD_ISSET(fd, &rfds)) {
614 for (i = 0; i < MAX_CLIENTS; i++) {
615 if (clients[i] == -1) {
619 if (i < MAX_CLIENTS) {
620 logthing(LOGTHING_INFO,
621 "Accepted connection %d.", i);
622 clients[i] = sock_accept(fd);
628 for (i = 0; i < MAX_CLIENTS; i++) {
629 if (clients[i] != -1) {
630 FD_SET(clients[i], &rfds);
631 maxfd = (maxfd > clients[i]) ?
636 dbctx->cleanupdb(dbctx);
646 return(EXIT_SUCCESS);