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));
149 uint32_t cmd = KEYD_CMD_UNKNOWN;
155 struct openpgp_publickey *key = NULL;
156 struct openpgp_packet_list *packets = NULL;
157 struct openpgp_packet_list *list_end = NULL;
158 struct buffer_ctx storebuf;
162 * Get the command from the client.
164 bytes = read(fd, &cmd, sizeof(cmd));
166 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
168 if (bytes != sizeof(cmd)) {
173 if (cmd < KEYD_CMD_LAST) {
174 stats->command_stats[cmd]++;
176 stats->command_stats[KEYD_CMD_UNKNOWN]++;
179 case KEYD_CMD_VERSION:
181 write(fd, &cmd, sizeof(cmd));
182 cmd = sizeof(keyd_version);
183 write(fd, &cmd, sizeof(cmd));
184 write(fd, &keyd_version, sizeof(keyd_version));
188 write(fd, &cmd, sizeof(cmd));
189 bytes = read(fd, &keyid, sizeof(keyid));
190 if (bytes != sizeof(keyid)) {
195 logthing(LOGTHING_INFO,
196 "Fetching 0x%" PRIX64
200 fetch_key(keyid, &key, false));
202 storebuf.size = 8192;
203 storebuf.buffer = malloc(8192);
205 flatten_publickey(key,
208 write_openpgp_stream(buffer_putchar,
211 logthing(LOGTHING_TRACE,
214 write(fd, &storebuf.offset,
215 sizeof(storebuf.offset));
216 write(fd, storebuf.buffer,
219 free(storebuf.buffer);
220 storebuf.buffer = NULL;
221 storebuf.size = storebuf.offset = 0;
222 free_packet_list(packets);
223 packets = list_end = NULL;
227 write(fd, &storebuf.offset,
228 sizeof(storebuf.offset));
232 case KEYD_CMD_GETTEXT:
234 write(fd, &cmd, sizeof(cmd));
235 bytes = read(fd, &count, sizeof(count));
236 if (bytes != sizeof(count)) {
241 search = malloc(count+1);
242 read(fd, search, count);
244 logthing(LOGTHING_INFO,
245 "Fetching %s, result: %d",
248 fetch_key_text(search, &key));
250 storebuf.size = 8192;
251 storebuf.buffer = malloc(8192);
253 flatten_publickey(key,
256 write_openpgp_stream(buffer_putchar,
259 logthing(LOGTHING_TRACE,
262 write(fd, &storebuf.offset,
263 sizeof(storebuf.offset));
264 write(fd, storebuf.buffer,
267 free(storebuf.buffer);
268 storebuf.buffer = NULL;
269 storebuf.size = storebuf.offset = 0;
270 free_packet_list(packets);
271 packets = list_end = NULL;
275 write(fd, &storebuf.offset,
276 sizeof(storebuf.offset));
283 write(fd, &cmd, sizeof(cmd));
285 bytes = read(fd, &storebuf.size,
286 sizeof(storebuf.size));
287 logthing(LOGTHING_TRACE, "Reading %d bytes.",
289 if (bytes != sizeof(storebuf.size)) {
292 if (ret == 0 && storebuf.size > 0) {
293 storebuf.buffer = malloc(storebuf.size);
295 while (bytes >= 0 && count < storebuf.size) {
297 &storebuf.buffer[count],
298 storebuf.size - count);
299 logthing(LOGTHING_TRACE,
304 read_openpgp_stream(buffer_fetchchar,
308 parse_keys(packets, &key);
309 config.dbbackend->store_key(key, false, false);
310 free_packet_list(packets);
314 free(storebuf.buffer);
315 storebuf.buffer = NULL;
316 storebuf.size = storebuf.offset = 0;
319 case KEYD_CMD_DELETE:
321 write(fd, &cmd, sizeof(cmd));
322 bytes = read(fd, &keyid, sizeof(keyid));
323 if (bytes != sizeof(keyid)) {
327 logthing(LOGTHING_INFO,
328 "Deleting 0x%" PRIX64
331 config.dbbackend->delete_key(
335 case KEYD_CMD_GETFULLKEYID:
337 write(fd, &cmd, sizeof(cmd));
338 bytes = read(fd, &keyid, sizeof(keyid));
339 if (bytes != sizeof(keyid)) {
343 keyid = config.dbbackend->getfullkeyid(keyid);
345 write(fd, &cmd, sizeof(cmd));
346 write(fd, &keyid, sizeof(keyid));
349 case KEYD_CMD_KEYITER:
351 write(fd, &cmd, sizeof(cmd));
352 config.dbbackend->iterate_keys(iteratefunc,
355 write(fd, &bytes, sizeof(bytes));
359 write(fd, &cmd, sizeof(cmd));
364 write(fd, &cmd, sizeof(cmd));
365 logthing(LOGTHING_NOTICE,
366 "Exiting due to quit request.");
372 write(fd, &cmd, sizeof(cmd));
373 cmd = sizeof(*stats);
374 write(fd, &cmd, sizeof(cmd));
378 case KEYD_CMD_GETSKSHASH:
380 write(fd, &cmd, sizeof(cmd));
381 bytes = read(fd, hash.hash, sizeof(hash.hash));
382 if (bytes != sizeof(hash.hash)) {
387 logthing(LOGTHING_INFO,
391 fetch_key_skshash(&hash,
394 storebuf.size = 8192;
395 storebuf.buffer = malloc(8192);
397 flatten_publickey(key,
400 write_openpgp_stream(buffer_putchar,
403 logthing(LOGTHING_TRACE,
406 write(fd, &storebuf.offset,
407 sizeof(storebuf.offset));
408 write(fd, storebuf.buffer,
411 free(storebuf.buffer);
412 storebuf.buffer = NULL;
413 storebuf.size = storebuf.offset = 0;
414 free_packet_list(packets);
415 packets = list_end = NULL;
419 write(fd, &storebuf.offset,
420 sizeof(storebuf.offset));
426 logthing(LOGTHING_ERROR, "Got unknown command: %d",
428 cmd = KEYD_REPLY_UNKNOWN_CMD;
429 write(fd, &cmd, sizeof(cmd));
436 int sock_close(int fd)
438 shutdown(fd, SHUT_RDWR);
442 int sock_accept(int fd)
444 struct sockaddr_un sock;
449 socklen = sizeof(sock);
450 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
452 ret = fcntl(srv, F_SETFD, FD_CLOEXEC);
462 static void usage(void)
464 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
465 "onak PGP keyserver.\n");
467 puts("\tkeyd [options]\n");
468 puts("\tOptions:\n:");
469 puts("-c <file> - use <file> as the config file");
470 puts("-f - run in the foreground");
471 puts("-h - show this help text");
475 int main(int argc, char *argv[])
477 int fd = -1, maxfd, i, clients[MAX_CLIENTS];
480 char *configfile = NULL;
481 bool foreground = false;
484 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
487 configfile = strdup(optarg);
499 readconfig(configfile);
502 initlogthing("keyd", config.logfile);
503 config.use_keyd = false;
510 signal(SIGPIPE, SIG_IGN);
513 stats = calloc(1, sizeof(*stats));
515 logthing(LOGTHING_ERROR,
516 "Couldn't allocate memory for stats structure.");
519 stats->started = time(NULL);
521 snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
522 fd = sock_init(sockname);
528 memset(clients, -1, sizeof (clients));
530 config.dbbackend->initdb(false);
532 logthing(LOGTHING_NOTICE, "Accepting connections.");
533 while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
535 * Deal with existing clients first; if we're at our
536 * connection limit then processing them might free
537 * things up and let us accept the next client below.
539 for (i = 0; i < MAX_CLIENTS; i++) {
540 if (clients[i] != -1 &&
541 FD_ISSET(clients[i], &rfds)) {
542 logthing(LOGTHING_DEBUG,
543 "Handling connection for client %d.", i);
544 if (sock_do(clients[i])) {
545 sock_close(clients[i]);
547 logthing(LOGTHING_DEBUG,
548 "Closed connection for client %d.", i);
553 * Check if we have a new incoming connection to accept.
555 if (FD_ISSET(fd, &rfds)) {
556 for (i = 0; i < MAX_CLIENTS; i++) {
557 if (clients[i] == -1) {
561 if (i < MAX_CLIENTS) {
562 logthing(LOGTHING_INFO,
563 "Accepted connection %d.", i);
564 clients[i] = sock_accept(fd);
570 for (i = 0; i < MAX_CLIENTS; i++) {
571 if (clients[i] != -1) {
572 FD_SET(clients[i], &rfds);
573 maxfd = (maxfd > clients[i]) ?
578 config.dbbackend->cleanupdb();
588 return(EXIT_SUCCESS);