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,
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 case KEYD_CMD_UPDATE:
401 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
405 bytes = read(fd, &storebuf.size,
406 sizeof(storebuf.size));
407 logthing(LOGTHING_TRACE, "Reading %d bytes.",
409 if (bytes != sizeof(storebuf.size)) {
413 if (ret == 0 && storebuf.size > 0) {
414 storebuf.buffer = malloc(storebuf.size);
417 while (bytes >= 0 && count < storebuf.size) {
419 &storebuf.buffer[count],
420 storebuf.size - count);
421 logthing(LOGTHING_TRACE,
426 read_openpgp_stream(buffer_fetchchar,
430 parse_keys(packets, &key);
431 dbctx->store_key(dbctx, key, false,
432 (cmd == KEYD_CMD_UPDATE));
433 free_packet_list(packets);
437 free(storebuf.buffer);
438 storebuf.buffer = NULL;
439 storebuf.size = storebuf.offset = 0;
442 case KEYD_CMD_DELETE:
443 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
447 bytes = read(fd, &fingerprint,
448 sizeof(fingerprint));
449 if (bytes != sizeof(fingerprint)) {
454 logthing(LOGTHING_INFO,
455 "Deleting 0x%" PRIX64
458 dbctx->delete_key(dbctx,
459 &fingerprint, false));
462 case KEYD_CMD_KEYITER:
463 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
467 dbctx->iterate_keys(dbctx, iteratefunc,
469 if (!keyd_write_size(fd, 0)) {
475 /* We're going to close the FD even if this fails */
476 (void) keyd_write_reply(fd, KEYD_REPLY_OK);
480 /* We're going to quit even if this fails */
481 (void) keyd_write_reply(fd, KEYD_REPLY_OK);
482 logthing(LOGTHING_NOTICE,
483 "Exiting due to quit request.");
488 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
492 cmd = sizeof(*stats);
493 bytes = write(fd, &cmd, sizeof(cmd));
494 if (bytes != sizeof(cmd)) {
499 bytes = write(fd, stats, sizeof(*stats));
500 if (bytes != sizeof(*stats)) {
505 case KEYD_CMD_GET_SKSHASH:
506 if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
510 bytes = read(fd, hash.hash, sizeof(hash.hash));
511 if (bytes != sizeof(hash.hash)) {
516 logthing(LOGTHING_INFO,
519 dbctx->fetch_key_skshash(dbctx,
522 keyd_write_key(fd, key);
526 if (!keyd_write_size(fd, 0)) {
534 logthing(LOGTHING_ERROR, "Got unknown command: %d",
536 if (!keyd_write_reply(fd, KEYD_REPLY_UNKNOWN_CMD)) {
545 static int sock_close(int fd)
547 shutdown(fd, SHUT_RDWR);
551 static int sock_accept(int fd)
553 struct sockaddr_un sock;
558 socklen = sizeof(sock);
559 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
561 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
571 static void usage(void)
573 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
574 "onak PGP keyserver.\n");
576 puts("\tkeyd [options]\n");
577 puts("\tOptions:\n:");
578 puts("-c <file> - use <file> as the config file");
579 puts("-f - run in the foreground");
580 puts("-h - show this help text");
584 int main(int argc, char *argv[])
586 int fd = -1, maxfd, i, clients[MAX_CLIENTS];
587 fd_set rfds = { 0 }; /* Avoid scan-build false report for FD_SET */
589 char *configfile = NULL;
590 bool foreground = false;
592 struct onak_dbctx *dbctx;
594 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
597 if (configfile != NULL) {
600 configfile = strdup(optarg);
612 readconfig(configfile);
615 initlogthing("keyd", config.logfile);
616 config.use_keyd = false;
623 signal(SIGPIPE, SIG_IGN);
626 stats = calloc(1, sizeof(*stats));
628 logthing(LOGTHING_ERROR,
629 "Couldn't allocate memory for stats structure.");
632 stats->started = time(NULL);
634 snprintf(sockname, sizeof(sockname) - 1, "%s/%s",
635 config.sock_dir, KEYD_SOCKET);
636 fd = sock_init(sockname);
642 memset(clients, -1, sizeof (clients));
644 dbctx = config.dbinit(config.backend, false);
646 logthing(LOGTHING_NOTICE, "Accepting connections.");
647 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
649 * Deal with existing clients first; if we're at our
650 * connection limit then processing them might free
651 * things up and let us accept the next client below.
653 for (i = 0; i < MAX_CLIENTS; i++) {
654 if (clients[i] != -1 &&
655 FD_ISSET(clients[i], &rfds)) {
656 logthing(LOGTHING_DEBUG,
657 "Handling connection for client %d.", i);
658 if (sock_do(dbctx, clients[i])) {
659 sock_close(clients[i]);
661 logthing(LOGTHING_DEBUG,
662 "Closed connection for client %d.", i);
667 * Check if we have a new incoming connection to accept.
669 if (FD_ISSET(fd, &rfds)) {
670 for (i = 0; i < MAX_CLIENTS; i++) {
671 if (clients[i] == -1) {
675 if (i < MAX_CLIENTS) {
676 logthing(LOGTHING_INFO,
677 "Accepted connection %d.", i);
678 clients[i] = sock_accept(fd);
684 for (i = 0; i < MAX_CLIENTS; i++) {
685 if (clients[i] != -1) {
686 FD_SET(clients[i], &rfds);
687 maxfd = (maxfd > clients[i]) ?
692 dbctx->cleanupdb(dbctx);
694 if (!using_socket_activation) {
708 return(EXIT_SUCCESS);