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>
37 #include <systemd/sd-daemon.h>
40 #include "charfuncs.h"
45 #include "keystructs.h"
48 #include "onak-conf.h"
52 /* Maximum number of clients we're prepared to accept at once */
53 #define MAX_CLIENTS 16
56 static bool using_socket_activation = false;
59 static struct keyd_stats *stats;
61 static void daemonize(void)
68 logthing(LOGTHING_CRITICAL,
69 "Failed to fork into background: %d (%s)",
74 logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
79 logthing(LOGTHING_CRITICAL,
80 "Couldn't set process group leader: %d (%s)",
86 if (!freopen("/dev/null", "r", stdin)) {
87 logthing(LOGTHING_CRITICAL,
88 "Couldn't reopen stdin to NULL: %d (%s)",
93 if (!freopen("/dev/null", "w", stdout)) {
94 logthing(LOGTHING_CRITICAL,
95 "Couldn't reopen stdout to NULL: %d (%s)",
100 if (!freopen("/dev/null", "w", stderr)) {
101 logthing(LOGTHING_CRITICAL,
102 "Couldn't reopen stderr to NULL: %d (%s)",
111 static bool keyd_write_key(int fd, struct openpgp_publickey *key)
113 struct openpgp_packet_list *packets = NULL;
114 struct openpgp_packet_list *list_end = NULL;
115 struct buffer_ctx storebuf;
120 storebuf.size = 8192;
121 storebuf.buffer = malloc(8192);
123 flatten_publickey(key,
126 write_openpgp_stream(buffer_putchar,
129 logthing(LOGTHING_TRACE,
132 written = write(fd, &storebuf.offset,
133 sizeof(storebuf.offset));
137 written = write(fd, storebuf.buffer,
139 if (written != storebuf.offset) {
144 free(storebuf.buffer);
145 storebuf.buffer = NULL;
146 storebuf.size = storebuf.offset = 0;
147 free_packet_list(packets);
148 packets = list_end = NULL;
153 static bool keyd_write_reply(int fd, enum keyd_reply _reply)
155 uint32_t reply = _reply;
159 written = write(fd, &reply, sizeof(reply));
160 if (written != sizeof(reply)) {
167 static bool keyd_write_size(int fd, size_t size)
172 written = write(fd, &size, sizeof(size));
173 if (written != sizeof(size)) {
180 static void iteratefunc(void *ctx, struct openpgp_publickey *key)
182 int *fd = (int *) ctx;
186 get_keyid(key, &keyid);
187 logthing(LOGTHING_TRACE,
188 "Iterating over 0x%016" PRIX64 ".",
191 keyd_write_key(*fd, key);
197 static int sock_init(const char *sockname)
199 struct sockaddr_un sock;
205 n = sd_listen_fds(0);
207 logthing(LOGTHING_ERROR,
208 "Too many file descriptors received from systemd.");
210 fd = SD_LISTEN_FDS_START + 0;
211 if (sd_is_socket_unix(fd, SOCK_STREAM, 1, NULL, 0) <= 0) {
212 logthing(LOGTHING_ERROR,
213 "systemd passed an invalid socket.");
216 using_socket_activation = true;
219 fd = socket(PF_UNIX, SOCK_STREAM, 0);
221 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
225 sock.sun_family = AF_UNIX;
226 strncpy(sock.sun_path, sockname,
227 sizeof(sock.sun_path) - 1);
229 ret = bind(fd, (struct sockaddr *) &sock,
247 static int sock_do(struct onak_dbctx *dbctx, int fd)
249 uint32_t cmd = KEYD_CMD_UNKNOWN;
255 struct openpgp_publickey *key = NULL;
256 struct openpgp_packet_list *packets = NULL;
257 struct buffer_ctx storebuf;
259 struct openpgp_fingerprint fingerprint;
262 * Get the command from the client.
264 bytes = read(fd, &cmd, sizeof(cmd));
266 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
268 if (bytes != sizeof(cmd)) {
273 if (cmd < KEYD_CMD_LAST) {
274 stats->command_stats[cmd]++;
276 stats->command_stats[KEYD_CMD_UNKNOWN]++;
279 case KEYD_CMD_VERSION:
280 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
284 cmd = sizeof(keyd_version);
285 bytes = write(fd, &cmd, sizeof(cmd));
286 if (bytes != sizeof(cmd)) {
291 bytes = write(fd, &keyd_version,
292 sizeof(keyd_version));
293 if (bytes != sizeof(keyd_version)) {
298 case KEYD_CMD_GET_ID:
299 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
303 bytes = read(fd, &keyid, sizeof(keyid));
304 if (bytes != sizeof(keyid)) {
309 logthing(LOGTHING_INFO,
310 "Fetching 0x%" PRIX64
313 dbctx->fetch_key_id(dbctx,
317 keyd_write_key(fd, key);
321 if (!keyd_write_size(fd, 0)) {
327 case KEYD_CMD_GET_FP:
328 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
332 if ((read(fd, &bytes, 1) != 1) ||
333 (bytes > MAX_FINGERPRINT_LEN)) {
336 fingerprint.length = bytes;
337 bytes = read(fd, fingerprint.fp,
339 if (bytes != fingerprint.length) {
345 logthing(LOGTHING_INFO,
346 "Fetching by fingerprint"
348 dbctx->fetch_key_fp(dbctx,
352 keyd_write_key(fd, key);
356 if (!keyd_write_size(fd, 0)) {
363 case KEYD_CMD_GET_TEXT:
364 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
368 bytes = read(fd, &count, sizeof(count));
369 if (bytes != sizeof(count)) {
374 search = malloc(count+1);
375 bytes = read(fd, search, count);
376 if (bytes != count) {
382 logthing(LOGTHING_INFO,
383 "Fetching %s, result: %d",
385 dbctx->fetch_key_text(dbctx,
388 keyd_write_key(fd, key);
392 if (!keyd_write_size(fd, 0)) {
400 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
404 bytes = read(fd, &storebuf.size,
405 sizeof(storebuf.size));
406 logthing(LOGTHING_TRACE, "Reading %d bytes.",
408 if (bytes != sizeof(storebuf.size)) {
412 if (ret == 0 && storebuf.size > 0) {
413 storebuf.buffer = malloc(storebuf.size);
416 while (bytes >= 0 && count < storebuf.size) {
418 &storebuf.buffer[count],
419 storebuf.size - count);
420 logthing(LOGTHING_TRACE,
425 read_openpgp_stream(buffer_fetchchar,
429 parse_keys(packets, &key);
430 dbctx->store_key(dbctx, key, false, false);
431 free_packet_list(packets);
435 free(storebuf.buffer);
436 storebuf.buffer = NULL;
437 storebuf.size = storebuf.offset = 0;
440 case KEYD_CMD_DELETE:
441 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
445 bytes = read(fd, &keyid, sizeof(keyid));
446 if (bytes != sizeof(keyid)) {
451 logthing(LOGTHING_INFO,
452 "Deleting 0x%" PRIX64
455 dbctx->delete_key(dbctx,
459 case KEYD_CMD_GETFULLKEYID:
460 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
464 bytes = read(fd, &keyid, sizeof(keyid));
465 if (bytes != sizeof(keyid)) {
470 keyid = dbctx->getfullkeyid(dbctx, keyid);
472 bytes = write(fd, &cmd, sizeof(cmd));
473 if (bytes != sizeof(cmd)) {
478 bytes = write(fd, &keyid, sizeof(keyid));
479 if (bytes != sizeof(keyid)) {
484 case KEYD_CMD_KEYITER:
485 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
489 dbctx->iterate_keys(dbctx, iteratefunc,
491 if (!keyd_write_size(fd, 0)) {
497 /* We're going to close the FD even if this fails */
498 (void) keyd_write_reply(fd, KEYD_REPLY_OK);
502 /* We're going to quit even if this fails */
503 (void) keyd_write_reply(fd, KEYD_REPLY_OK);
504 logthing(LOGTHING_NOTICE,
505 "Exiting due to quit request.");
510 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
514 cmd = sizeof(*stats);
515 bytes = write(fd, &cmd, sizeof(cmd));
516 if (bytes != sizeof(cmd)) {
521 bytes = write(fd, stats, sizeof(*stats));
522 if (bytes != sizeof(*stats)) {
527 case KEYD_CMD_GET_SKSHASH:
528 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
532 bytes = read(fd, hash.hash, sizeof(hash.hash));
533 if (bytes != sizeof(hash.hash)) {
538 logthing(LOGTHING_INFO,
541 dbctx->fetch_key_skshash(dbctx,
544 keyd_write_key(fd, key);
548 if (!keyd_write_size(fd, 0)) {
556 logthing(LOGTHING_ERROR, "Got unknown command: %d",
558 if (!keyd_write_reply(fd, KEYD_REPLY_UNKNOWN_CMD)) {
567 static int sock_close(int fd)
569 shutdown(fd, SHUT_RDWR);
573 static int sock_accept(int fd)
575 struct sockaddr_un sock;
580 socklen = sizeof(sock);
581 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
583 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
593 static void usage(void)
595 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
596 "onak PGP keyserver.\n");
598 puts("\tkeyd [options]\n");
599 puts("\tOptions:\n:");
600 puts("-c <file> - use <file> as the config file");
601 puts("-f - run in the foreground");
602 puts("-h - show this help text");
606 int main(int argc, char *argv[])
608 int fd = -1, maxfd, i, clients[MAX_CLIENTS];
611 char *configfile = NULL;
612 bool foreground = false;
614 struct onak_dbctx *dbctx;
616 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
619 if (configfile != NULL) {
622 configfile = strdup(optarg);
634 readconfig(configfile);
637 initlogthing("keyd", config.logfile);
638 config.use_keyd = false;
645 signal(SIGPIPE, SIG_IGN);
648 stats = calloc(1, sizeof(*stats));
650 logthing(LOGTHING_ERROR,
651 "Couldn't allocate memory for stats structure.");
654 stats->started = time(NULL);
656 snprintf(sockname, 1023, "%s/%s", config.sock_dir, KEYD_SOCKET);
657 fd = sock_init(sockname);
663 memset(clients, -1, sizeof (clients));
665 dbctx = config.dbinit(false);
667 logthing(LOGTHING_NOTICE, "Accepting connections.");
668 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
670 * Deal with existing clients first; if we're at our
671 * connection limit then processing them might free
672 * things up and let us accept the next client below.
674 for (i = 0; i < MAX_CLIENTS; i++) {
675 if (clients[i] != -1 &&
676 FD_ISSET(clients[i], &rfds)) {
677 logthing(LOGTHING_DEBUG,
678 "Handling connection for client %d.", i);
679 if (sock_do(dbctx, clients[i])) {
680 sock_close(clients[i]);
682 logthing(LOGTHING_DEBUG,
683 "Closed connection for client %d.", i);
688 * Check if we have a new incoming connection to accept.
690 if (FD_ISSET(fd, &rfds)) {
691 for (i = 0; i < MAX_CLIENTS; i++) {
692 if (clients[i] == -1) {
696 if (i < MAX_CLIENTS) {
697 logthing(LOGTHING_INFO,
698 "Accepted connection %d.", i);
699 clients[i] = sock_accept(fd);
705 for (i = 0; i < MAX_CLIENTS; i++) {
706 if (clients[i] != -1) {
707 FD_SET(clients[i], &rfds);
708 maxfd = (maxfd > clients[i]) ?
713 dbctx->cleanupdb(dbctx);
715 if (!using_socket_activation) {
729 return(EXIT_SUCCESS);