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, see <https://www.gnu.org/licenses/>.
28 #include <sys/select.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
35 #include "build-config.h"
38 #include <systemd/sd-daemon.h>
41 #include "charfuncs.h"
46 #include "keystructs.h"
49 #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,
238 logthing(LOGTHING_ERROR,
239 "Couldn't open socket to listen on: %s (%d)",
252 static int sock_do(struct onak_dbctx *dbctx, int fd)
254 uint32_t cmd = KEYD_CMD_UNKNOWN;
260 struct openpgp_publickey *key = NULL;
261 struct openpgp_packet_list *packets = NULL;
262 struct buffer_ctx storebuf;
264 struct openpgp_fingerprint fingerprint;
267 * Get the command from the client.
269 bytes = read(fd, &cmd, sizeof(cmd));
271 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
273 if (bytes != sizeof(cmd)) {
278 if (cmd < KEYD_CMD_LAST) {
279 stats->command_stats[cmd]++;
281 stats->command_stats[KEYD_CMD_UNKNOWN]++;
284 case KEYD_CMD_VERSION:
285 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
289 cmd = sizeof(keyd_version);
290 bytes = write(fd, &cmd, sizeof(cmd));
291 if (bytes != sizeof(cmd)) {
296 bytes = write(fd, &keyd_version,
297 sizeof(keyd_version));
298 if (bytes != sizeof(keyd_version)) {
303 case KEYD_CMD_GET_ID:
304 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
308 bytes = read(fd, &keyid, sizeof(keyid));
309 if (bytes != sizeof(keyid)) {
314 logthing(LOGTHING_INFO,
315 "Fetching 0x%" PRIX64
318 dbctx->fetch_key_id(dbctx,
322 keyd_write_key(fd, key);
326 if (!keyd_write_size(fd, 0)) {
332 case KEYD_CMD_GET_FP:
333 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
337 if ((read(fd, &bytes, 1) != 1) ||
338 (bytes > MAX_FINGERPRINT_LEN)) {
341 fingerprint.length = bytes;
342 bytes = read(fd, fingerprint.fp,
344 if (bytes != fingerprint.length) {
350 logthing(LOGTHING_INFO,
351 "Fetching by fingerprint"
353 dbctx->fetch_key_fp(dbctx,
357 keyd_write_key(fd, key);
361 if (!keyd_write_size(fd, 0)) {
368 case KEYD_CMD_GET_TEXT:
369 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
373 bytes = read(fd, &count, sizeof(count));
374 if (bytes != sizeof(count)) {
379 search = malloc(count+1);
380 bytes = read(fd, search, count);
381 if (bytes != count) {
387 logthing(LOGTHING_INFO,
388 "Fetching %s, result: %d",
390 dbctx->fetch_key_text(dbctx,
393 keyd_write_key(fd, key);
397 if (!keyd_write_size(fd, 0)) {
405 case KEYD_CMD_UPDATE:
406 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
410 bytes = read(fd, &storebuf.size,
411 sizeof(storebuf.size));
412 logthing(LOGTHING_TRACE, "Reading %d bytes.",
414 if (bytes != sizeof(storebuf.size)) {
418 if (ret == 0 && storebuf.size > 0) {
419 storebuf.buffer = malloc(storebuf.size);
422 while (bytes >= 0 && count < storebuf.size) {
424 &storebuf.buffer[count],
425 storebuf.size - count);
426 logthing(LOGTHING_TRACE,
431 read_openpgp_stream(buffer_fetchchar,
435 parse_keys(packets, &key);
436 dbctx->store_key(dbctx, key, false,
437 (cmd == KEYD_CMD_UPDATE));
438 free_packet_list(packets);
442 free(storebuf.buffer);
443 storebuf.buffer = NULL;
444 storebuf.size = storebuf.offset = 0;
447 case KEYD_CMD_DELETE:
448 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
452 bytes = read(fd, &fingerprint,
453 sizeof(fingerprint));
454 if (bytes != sizeof(fingerprint)) {
459 logthing(LOGTHING_INFO,
460 "Deleting 0x%" PRIX64
463 dbctx->delete_key(dbctx,
464 &fingerprint, false));
467 case KEYD_CMD_KEYITER:
468 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
472 dbctx->iterate_keys(dbctx, iteratefunc,
474 if (!keyd_write_size(fd, 0)) {
480 /* We're going to close the FD even if this fails */
481 (void) keyd_write_reply(fd, KEYD_REPLY_OK);
485 /* We're going to quit even if this fails */
486 (void) keyd_write_reply(fd, KEYD_REPLY_OK);
487 logthing(LOGTHING_NOTICE,
488 "Exiting due to quit request.");
493 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
497 cmd = sizeof(*stats);
498 bytes = write(fd, &cmd, sizeof(cmd));
499 if (bytes != sizeof(cmd)) {
504 bytes = write(fd, stats, sizeof(*stats));
505 if (bytes != sizeof(*stats)) {
510 case KEYD_CMD_GET_SKSHASH:
511 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
515 bytes = read(fd, hash.hash, sizeof(hash.hash));
516 if (bytes != sizeof(hash.hash)) {
521 logthing(LOGTHING_INFO,
524 dbctx->fetch_key_skshash(dbctx,
527 keyd_write_key(fd, key);
531 if (!keyd_write_size(fd, 0)) {
539 logthing(LOGTHING_ERROR, "Got unknown command: %d",
541 if (!keyd_write_reply(fd, KEYD_REPLY_UNKNOWN_CMD)) {
550 static int sock_close(int fd)
552 shutdown(fd, SHUT_RDWR);
556 static int sock_accept(int fd)
558 struct sockaddr_un sock;
563 socklen = sizeof(sock);
564 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
566 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
576 static void usage(void)
578 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
579 "onak PGP keyserver.\n");
581 puts("\tkeyd [options]\n");
582 puts("\tOptions:\n:");
583 puts("-c <file> - use <file> as the config file");
584 puts("-f - run in the foreground");
585 puts("-h - show this help text");
589 int main(int argc, char *argv[])
591 int fd = -1, maxfd, i, clients[MAX_CLIENTS];
592 fd_set rfds = { 0 }; /* Avoid scan-build false report for FD_SET */
594 char *configfile = NULL;
595 bool foreground = false;
597 struct onak_dbctx *dbctx;
599 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
602 if (configfile != NULL) {
605 configfile = strdup(optarg);
617 readconfig(configfile);
620 initlogthing("keyd", config.logfile);
621 config.use_keyd = false;
628 signal(SIGPIPE, SIG_IGN);
631 stats = calloc(1, sizeof(*stats));
633 logthing(LOGTHING_ERROR,
634 "Couldn't allocate memory for stats structure.");
637 stats->started = time(NULL);
639 snprintf(sockname, sizeof(sockname) - 1, "%s/%s",
640 config.sock_dir, KEYD_SOCKET);
641 fd = sock_init(sockname);
647 memset(clients, -1, sizeof (clients));
649 dbctx = config.dbinit(config.backend, false);
651 logthing(LOGTHING_NOTICE, "Accepting connections.");
652 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
654 * Deal with existing clients first; if we're at our
655 * connection limit then processing them might free
656 * things up and let us accept the next client below.
658 for (i = 0; i < MAX_CLIENTS; i++) {
659 if (clients[i] != -1 &&
660 FD_ISSET(clients[i], &rfds)) {
661 logthing(LOGTHING_DEBUG,
662 "Handling connection for client %d.", i);
663 if (sock_do(dbctx, clients[i])) {
664 sock_close(clients[i]);
666 logthing(LOGTHING_DEBUG,
667 "Closed connection for client %d.", i);
672 * Check if we have a new incoming connection to accept.
674 if (FD_ISSET(fd, &rfds)) {
675 for (i = 0; i < MAX_CLIENTS; i++) {
676 if (clients[i] == -1) {
680 if (i < MAX_CLIENTS) {
681 logthing(LOGTHING_INFO,
682 "Accepted connection %d.", i);
683 clients[i] = sock_accept(fd);
689 for (i = 0; i < MAX_CLIENTS; i++) {
690 if (clients[i] != -1) {
691 FD_SET(clients[i], &rfds);
692 maxfd = (maxfd > clients[i]) ?
697 dbctx->cleanupdb(dbctx);
699 if (!using_socket_activation) {
713 return(EXIT_SUCCESS);