2 * keyd.c - key retrieval daemon
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2004 Project Purple
16 #include <sys/select.h>
17 #include <sys/socket.h>
18 #include <sys/types.h>
23 #include "charfuncs.h"
28 #include "keystructs.h"
31 #include "onak-conf.h"
35 static struct keyd_stats *stats;
44 logthing(LOGTHING_CRITICAL,
45 "Failed to fork into background: %d (%s)",
50 logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
56 freopen("/dev/null", "r", stdin);
57 freopen("/dev/null", "w", stdout);
58 freopen("/dev/null", "w", stderr);
63 void iteratefunc(void *ctx, struct openpgp_publickey *key)
65 struct openpgp_packet_list *packets = NULL;
66 struct openpgp_packet_list *list_end = NULL;
67 struct buffer_ctx storebuf;
69 int *fd = (int *) ctx;
74 storebuf.buffer = malloc(8192);
76 logthing(LOGTHING_TRACE,
77 "Iterating over 0x%016" PRIX64 ".",
80 flatten_publickey(key,
83 write_openpgp_stream(buffer_putchar,
86 logthing(LOGTHING_TRACE,
89 ret = write(*fd, &storebuf.offset,
90 sizeof(storebuf.offset));
92 write(*fd, storebuf.buffer,
96 free(storebuf.buffer);
97 storebuf.buffer = NULL;
98 storebuf.size = storebuf.offset = 0;
99 free_packet_list(packets);
100 packets = list_end = NULL;
106 int sock_init(const char *sockname)
108 struct sockaddr_un sock;
112 fd = socket(PF_UNIX, SOCK_STREAM, 0);
114 ret = fcntl(fd, F_SETFD, 1);
118 sock.sun_family = AF_UNIX;
119 strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
121 ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
133 uint32_t cmd = KEYD_CMD_UNKNOWN;
139 struct openpgp_publickey *key = NULL;
140 struct openpgp_packet_list *packets = NULL;
141 struct openpgp_packet_list *list_end = NULL;
142 struct buffer_ctx storebuf;
146 * Get the command from the client.
148 bytes = read(fd, &cmd, sizeof(cmd));
150 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
152 if (bytes != sizeof(cmd)) {
157 if (cmd < KEYD_CMD_LAST) {
158 stats->command_stats[cmd]++;
160 stats->command_stats[KEYD_CMD_UNKNOWN]++;
163 case KEYD_CMD_VERSION:
165 write(fd, &cmd, sizeof(cmd));
166 cmd = sizeof(keyd_version);
167 write(fd, &cmd, sizeof(cmd));
168 write(fd, &keyd_version, sizeof(keyd_version));
172 write(fd, &cmd, sizeof(cmd));
173 bytes = read(fd, &keyid, sizeof(keyid));
174 if (bytes != sizeof(keyid)) {
179 logthing(LOGTHING_INFO,
180 "Fetching 0x%" PRIX64
184 fetch_key(keyid, &key, false));
186 storebuf.size = 8192;
187 storebuf.buffer = malloc(8192);
189 flatten_publickey(key,
192 write_openpgp_stream(buffer_putchar,
195 logthing(LOGTHING_TRACE,
198 write(fd, &storebuf.offset,
199 sizeof(storebuf.offset));
200 write(fd, storebuf.buffer,
203 free(storebuf.buffer);
204 storebuf.buffer = NULL;
205 storebuf.size = storebuf.offset = 0;
206 free_packet_list(packets);
207 packets = list_end = NULL;
211 write(fd, &storebuf.offset,
212 sizeof(storebuf.offset));
216 case KEYD_CMD_GETTEXT:
218 write(fd, &cmd, sizeof(cmd));
219 bytes = read(fd, &count, sizeof(count));
220 if (bytes != sizeof(count)) {
225 search = malloc(count+1);
226 read(fd, search, count);
228 logthing(LOGTHING_INFO,
229 "Fetching %s, result: %d",
232 fetch_key_text(search, &key));
234 storebuf.size = 8192;
235 storebuf.buffer = malloc(8192);
237 flatten_publickey(key,
240 write_openpgp_stream(buffer_putchar,
243 logthing(LOGTHING_TRACE,
246 write(fd, &storebuf.offset,
247 sizeof(storebuf.offset));
248 write(fd, storebuf.buffer,
251 free(storebuf.buffer);
252 storebuf.buffer = NULL;
253 storebuf.size = storebuf.offset = 0;
254 free_packet_list(packets);
255 packets = list_end = NULL;
259 write(fd, &storebuf.offset,
260 sizeof(storebuf.offset));
266 write(fd, &cmd, sizeof(cmd));
268 bytes = read(fd, &storebuf.size,
269 sizeof(storebuf.size));
270 logthing(LOGTHING_TRACE, "Reading %d bytes.",
272 if (bytes != sizeof(storebuf.size)) {
275 if (ret == 0 && storebuf.size > 0) {
276 storebuf.buffer = malloc(storebuf.size);
278 while (bytes >= 0 && count < storebuf.size) {
280 &storebuf.buffer[count],
281 storebuf.size - count);
282 logthing(LOGTHING_TRACE,
287 read_openpgp_stream(buffer_fetchchar,
291 parse_keys(packets, &key);
292 config.dbbackend->store_key(key, false, false);
293 free_packet_list(packets);
297 free(storebuf.buffer);
298 storebuf.buffer = NULL;
299 storebuf.size = storebuf.offset = 0;
302 case KEYD_CMD_DELETE:
304 write(fd, &cmd, sizeof(cmd));
305 bytes = read(fd, &keyid, sizeof(keyid));
306 if (bytes != sizeof(keyid)) {
310 logthing(LOGTHING_INFO,
311 "Deleting 0x%" PRIX64
314 config.dbbackend->delete_key(
318 case KEYD_CMD_GETFULLKEYID:
320 write(fd, &cmd, sizeof(cmd));
321 bytes = read(fd, &keyid, sizeof(keyid));
322 if (bytes != sizeof(keyid)) {
326 keyid = config.dbbackend->getfullkeyid(keyid);
328 write(fd, &cmd, sizeof(cmd));
329 write(fd, &keyid, sizeof(keyid));
332 case KEYD_CMD_KEYITER:
334 write(fd, &cmd, sizeof(cmd));
335 config.dbbackend->iterate_keys(iteratefunc,
338 write(fd, &bytes, sizeof(bytes));
342 write(fd, &cmd, sizeof(cmd));
347 write(fd, &cmd, sizeof(cmd));
348 logthing(LOGTHING_NOTICE,
349 "Exiting due to quit request.");
355 write(fd, &cmd, sizeof(cmd));
356 cmd = sizeof(*stats);
357 write(fd, &cmd, sizeof(cmd));
361 case KEYD_CMD_GETSKSHASH:
363 write(fd, &cmd, sizeof(cmd));
364 bytes = read(fd, hash.hash, sizeof(hash.hash));
365 if (bytes != sizeof(hash.hash)) {
370 logthing(LOGTHING_INFO,
374 fetch_key_skshash(&hash,
377 storebuf.size = 8192;
378 storebuf.buffer = malloc(8192);
380 flatten_publickey(key,
383 write_openpgp_stream(buffer_putchar,
386 logthing(LOGTHING_TRACE,
389 write(fd, &storebuf.offset,
390 sizeof(storebuf.offset));
391 write(fd, storebuf.buffer,
394 free(storebuf.buffer);
395 storebuf.buffer = NULL;
396 storebuf.size = storebuf.offset = 0;
397 free_packet_list(packets);
398 packets = list_end = NULL;
402 write(fd, &storebuf.offset,
403 sizeof(storebuf.offset));
409 logthing(LOGTHING_ERROR, "Got unknown command: %d",
411 cmd = KEYD_REPLY_UNKNOWN_CMD;
412 write(fd, &cmd, sizeof(cmd));
419 int sock_close(int fd)
421 shutdown(fd, SHUT_RDWR);
425 int sock_accept(int fd)
427 struct sockaddr_un sock;
432 socklen = sizeof(sock);
433 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
435 ret = fcntl(srv, F_SETFD, 1);
440 while (!sock_do(srv)) ;
447 static void usage(void)
449 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
450 "onak PGP keyserver.\n");
452 puts("\tkeyd [options]\n");
453 puts("\tOptions:\n:");
454 puts("-c <file> - use <file> as the config file");
455 puts("-f - run in the foreground");
456 puts("-h - show this help text");
460 int main(int argc, char *argv[])
465 char *configfile = NULL;
466 bool foreground = false;
469 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
472 configfile = strdup(optarg);
484 readconfig(configfile);
487 initlogthing("keyd", config.logfile);
488 config.use_keyd = false;
495 signal(SIGPIPE, SIG_IGN);
498 stats = calloc(1, sizeof(*stats));
500 logthing(LOGTHING_ERROR,
501 "Couldn't allocate memory for stats structure.");
504 stats->started = time(NULL);
506 snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
507 fd = sock_init(sockname);
513 config.dbbackend->initdb(false);
515 logthing(LOGTHING_NOTICE, "Accepting connections.");
516 while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
517 logthing(LOGTHING_INFO, "Accepted connection.");
521 config.dbbackend->cleanupdb();
531 return(EXIT_SUCCESS);