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;
145 * Get the command from the client.
147 bytes = read(fd, &cmd, sizeof(cmd));
149 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
151 if (bytes != sizeof(cmd)) {
156 if (cmd < KEYD_CMD_LAST) {
157 stats->command_stats[cmd]++;
159 stats->command_stats[KEYD_CMD_UNKNOWN]++;
162 case KEYD_CMD_VERSION:
164 write(fd, &cmd, sizeof(cmd));
165 cmd = sizeof(keyd_version);
166 write(fd, &cmd, sizeof(cmd));
167 write(fd, &keyd_version, sizeof(keyd_version));
171 write(fd, &cmd, sizeof(cmd));
172 bytes = read(fd, &keyid, sizeof(keyid));
173 if (bytes != sizeof(keyid)) {
178 logthing(LOGTHING_INFO,
179 "Fetching 0x%" PRIX64
183 fetch_key(keyid, &key, false));
185 storebuf.size = 8192;
186 storebuf.buffer = malloc(8192);
188 flatten_publickey(key,
191 write_openpgp_stream(buffer_putchar,
194 logthing(LOGTHING_TRACE,
197 write(fd, &storebuf.offset,
198 sizeof(storebuf.offset));
199 write(fd, storebuf.buffer,
202 free(storebuf.buffer);
203 storebuf.buffer = NULL;
204 storebuf.size = storebuf.offset = 0;
205 free_packet_list(packets);
206 packets = list_end = NULL;
210 write(fd, &storebuf.offset,
211 sizeof(storebuf.offset));
215 case KEYD_CMD_GETTEXT:
217 write(fd, &cmd, sizeof(cmd));
218 bytes = read(fd, &count, sizeof(count));
219 if (bytes != sizeof(count)) {
224 search = malloc(count+1);
225 read(fd, search, count);
227 logthing(LOGTHING_INFO,
228 "Fetching %s, result: %d",
231 fetch_key_text(search, &key));
233 storebuf.size = 8192;
234 storebuf.buffer = malloc(8192);
236 flatten_publickey(key,
239 write_openpgp_stream(buffer_putchar,
242 logthing(LOGTHING_TRACE,
245 write(fd, &storebuf.offset,
246 sizeof(storebuf.offset));
247 write(fd, storebuf.buffer,
250 free(storebuf.buffer);
251 storebuf.buffer = NULL;
252 storebuf.size = storebuf.offset = 0;
253 free_packet_list(packets);
254 packets = list_end = NULL;
258 write(fd, &storebuf.offset,
259 sizeof(storebuf.offset));
265 write(fd, &cmd, sizeof(cmd));
267 bytes = read(fd, &storebuf.size,
268 sizeof(storebuf.size));
269 logthing(LOGTHING_TRACE, "Reading %d bytes.",
271 if (bytes != sizeof(storebuf.size)) {
274 if (ret == 0 && storebuf.size > 0) {
275 storebuf.buffer = malloc(storebuf.size);
277 while (bytes >= 0 && count < storebuf.size) {
279 &storebuf.buffer[count],
280 storebuf.size - count);
281 logthing(LOGTHING_TRACE,
286 read_openpgp_stream(buffer_fetchchar,
290 parse_keys(packets, &key);
291 config.dbbackend->store_key(key, false, false);
292 free_packet_list(packets);
296 free(storebuf.buffer);
297 storebuf.buffer = NULL;
298 storebuf.size = storebuf.offset = 0;
301 case KEYD_CMD_DELETE:
303 write(fd, &cmd, sizeof(cmd));
304 bytes = read(fd, &keyid, sizeof(keyid));
305 if (bytes != sizeof(keyid)) {
309 logthing(LOGTHING_INFO,
310 "Deleting 0x%" PRIX64
313 config.dbbackend->delete_key(
317 case KEYD_CMD_GETFULLKEYID:
319 write(fd, &cmd, sizeof(cmd));
320 bytes = read(fd, &keyid, sizeof(keyid));
321 if (bytes != sizeof(keyid)) {
325 keyid = config.dbbackend->getfullkeyid(keyid);
327 write(fd, &cmd, sizeof(cmd));
328 write(fd, &keyid, sizeof(keyid));
331 case KEYD_CMD_KEYITER:
333 write(fd, &cmd, sizeof(cmd));
334 config.dbbackend->iterate_keys(iteratefunc,
337 write(fd, &bytes, sizeof(bytes));
341 write(fd, &cmd, sizeof(cmd));
346 write(fd, &cmd, sizeof(cmd));
347 logthing(LOGTHING_NOTICE,
348 "Exiting due to quit request.");
354 write(fd, &cmd, sizeof(cmd));
355 cmd = sizeof(*stats);
356 write(fd, &cmd, sizeof(cmd));
361 logthing(LOGTHING_ERROR, "Got unknown command: %d",
363 cmd = KEYD_REPLY_UNKNOWN_CMD;
364 write(fd, &cmd, sizeof(cmd));
371 int sock_close(int fd)
373 shutdown(fd, SHUT_RDWR);
377 int sock_accept(int fd)
379 struct sockaddr_un sock;
384 socklen = sizeof(sock);
385 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
387 ret = fcntl(srv, F_SETFD, 1);
392 while (!sock_do(srv)) ;
399 static void usage(void)
401 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
402 "onak PGP keyserver.\n");
404 puts("\tkeyd [options]\n");
405 puts("\tOptions:\n:");
406 puts("-c <file> - use <file> as the config file");
407 puts("-f - run in the foreground");
408 puts("-h - show this help text");
412 int main(int argc, char *argv[])
417 char *configfile = NULL;
418 bool foreground = false;
421 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
424 configfile = strdup(optarg);
436 readconfig(configfile);
439 initlogthing("keyd", config.logfile);
440 config.use_keyd = false;
447 signal(SIGPIPE, SIG_IGN);
450 stats = calloc(1, sizeof(*stats));
452 logthing(LOGTHING_ERROR,
453 "Couldn't allocate memory for stats structure.");
456 stats->started = time(NULL);
458 snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
459 fd = sock_init(sockname);
465 config.dbbackend->initdb(false);
467 logthing(LOGTHING_NOTICE, "Accepting connections.");
468 while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
469 logthing(LOGTHING_INFO, "Accepted connection.");
473 config.dbbackend->cleanupdb();
483 return(EXIT_SUCCESS);