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 static struct keyd_stats *stats;
55 logthing(LOGTHING_CRITICAL,
56 "Failed to fork into background: %d (%s)",
61 logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
67 freopen("/dev/null", "r", stdin);
68 freopen("/dev/null", "w", stdout);
69 freopen("/dev/null", "w", stderr);
74 void iteratefunc(void *ctx, struct openpgp_publickey *key)
76 struct openpgp_packet_list *packets = NULL;
77 struct openpgp_packet_list *list_end = NULL;
78 struct buffer_ctx storebuf;
80 int *fd = (int *) ctx;
86 storebuf.buffer = malloc(8192);
88 get_keyid(key, &keyid);
89 logthing(LOGTHING_TRACE,
90 "Iterating over 0x%016" PRIX64 ".",
93 flatten_publickey(key,
96 write_openpgp_stream(buffer_putchar,
99 logthing(LOGTHING_TRACE,
102 ret = write(*fd, &storebuf.offset,
103 sizeof(storebuf.offset));
105 write(*fd, storebuf.buffer,
109 free(storebuf.buffer);
110 storebuf.buffer = NULL;
111 storebuf.size = storebuf.offset = 0;
112 free_packet_list(packets);
113 packets = list_end = NULL;
119 int sock_init(const char *sockname)
121 struct sockaddr_un sock;
125 fd = socket(PF_UNIX, SOCK_STREAM, 0);
127 ret = fcntl(fd, F_SETFD, 1);
131 sock.sun_family = AF_UNIX;
132 strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
134 ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
146 uint32_t cmd = KEYD_CMD_UNKNOWN;
152 struct openpgp_publickey *key = NULL;
153 struct openpgp_packet_list *packets = NULL;
154 struct openpgp_packet_list *list_end = NULL;
155 struct buffer_ctx storebuf;
159 * Get the command from the client.
161 bytes = read(fd, &cmd, sizeof(cmd));
163 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
165 if (bytes != sizeof(cmd)) {
170 if (cmd < KEYD_CMD_LAST) {
171 stats->command_stats[cmd]++;
173 stats->command_stats[KEYD_CMD_UNKNOWN]++;
176 case KEYD_CMD_VERSION:
178 write(fd, &cmd, sizeof(cmd));
179 cmd = sizeof(keyd_version);
180 write(fd, &cmd, sizeof(cmd));
181 write(fd, &keyd_version, sizeof(keyd_version));
185 write(fd, &cmd, sizeof(cmd));
186 bytes = read(fd, &keyid, sizeof(keyid));
187 if (bytes != sizeof(keyid)) {
192 logthing(LOGTHING_INFO,
193 "Fetching 0x%" PRIX64
197 fetch_key(keyid, &key, false));
199 storebuf.size = 8192;
200 storebuf.buffer = malloc(8192);
202 flatten_publickey(key,
205 write_openpgp_stream(buffer_putchar,
208 logthing(LOGTHING_TRACE,
211 write(fd, &storebuf.offset,
212 sizeof(storebuf.offset));
213 write(fd, storebuf.buffer,
216 free(storebuf.buffer);
217 storebuf.buffer = NULL;
218 storebuf.size = storebuf.offset = 0;
219 free_packet_list(packets);
220 packets = list_end = NULL;
224 write(fd, &storebuf.offset,
225 sizeof(storebuf.offset));
229 case KEYD_CMD_GETTEXT:
231 write(fd, &cmd, sizeof(cmd));
232 bytes = read(fd, &count, sizeof(count));
233 if (bytes != sizeof(count)) {
238 search = malloc(count+1);
239 read(fd, search, count);
241 logthing(LOGTHING_INFO,
242 "Fetching %s, result: %d",
245 fetch_key_text(search, &key));
247 storebuf.size = 8192;
248 storebuf.buffer = malloc(8192);
250 flatten_publickey(key,
253 write_openpgp_stream(buffer_putchar,
256 logthing(LOGTHING_TRACE,
259 write(fd, &storebuf.offset,
260 sizeof(storebuf.offset));
261 write(fd, storebuf.buffer,
264 free(storebuf.buffer);
265 storebuf.buffer = NULL;
266 storebuf.size = storebuf.offset = 0;
267 free_packet_list(packets);
268 packets = list_end = NULL;
272 write(fd, &storebuf.offset,
273 sizeof(storebuf.offset));
279 write(fd, &cmd, sizeof(cmd));
281 bytes = read(fd, &storebuf.size,
282 sizeof(storebuf.size));
283 logthing(LOGTHING_TRACE, "Reading %d bytes.",
285 if (bytes != sizeof(storebuf.size)) {
288 if (ret == 0 && storebuf.size > 0) {
289 storebuf.buffer = malloc(storebuf.size);
291 while (bytes >= 0 && count < storebuf.size) {
293 &storebuf.buffer[count],
294 storebuf.size - count);
295 logthing(LOGTHING_TRACE,
300 read_openpgp_stream(buffer_fetchchar,
304 parse_keys(packets, &key);
305 config.dbbackend->store_key(key, false, false);
306 free_packet_list(packets);
310 free(storebuf.buffer);
311 storebuf.buffer = NULL;
312 storebuf.size = storebuf.offset = 0;
315 case KEYD_CMD_DELETE:
317 write(fd, &cmd, sizeof(cmd));
318 bytes = read(fd, &keyid, sizeof(keyid));
319 if (bytes != sizeof(keyid)) {
323 logthing(LOGTHING_INFO,
324 "Deleting 0x%" PRIX64
327 config.dbbackend->delete_key(
331 case KEYD_CMD_GETFULLKEYID:
333 write(fd, &cmd, sizeof(cmd));
334 bytes = read(fd, &keyid, sizeof(keyid));
335 if (bytes != sizeof(keyid)) {
339 keyid = config.dbbackend->getfullkeyid(keyid);
341 write(fd, &cmd, sizeof(cmd));
342 write(fd, &keyid, sizeof(keyid));
345 case KEYD_CMD_KEYITER:
347 write(fd, &cmd, sizeof(cmd));
348 config.dbbackend->iterate_keys(iteratefunc,
351 write(fd, &bytes, sizeof(bytes));
355 write(fd, &cmd, sizeof(cmd));
360 write(fd, &cmd, sizeof(cmd));
361 logthing(LOGTHING_NOTICE,
362 "Exiting due to quit request.");
368 write(fd, &cmd, sizeof(cmd));
369 cmd = sizeof(*stats);
370 write(fd, &cmd, sizeof(cmd));
374 case KEYD_CMD_GETSKSHASH:
376 write(fd, &cmd, sizeof(cmd));
377 bytes = read(fd, hash.hash, sizeof(hash.hash));
378 if (bytes != sizeof(hash.hash)) {
383 logthing(LOGTHING_INFO,
387 fetch_key_skshash(&hash,
390 storebuf.size = 8192;
391 storebuf.buffer = malloc(8192);
393 flatten_publickey(key,
396 write_openpgp_stream(buffer_putchar,
399 logthing(LOGTHING_TRACE,
402 write(fd, &storebuf.offset,
403 sizeof(storebuf.offset));
404 write(fd, storebuf.buffer,
407 free(storebuf.buffer);
408 storebuf.buffer = NULL;
409 storebuf.size = storebuf.offset = 0;
410 free_packet_list(packets);
411 packets = list_end = NULL;
415 write(fd, &storebuf.offset,
416 sizeof(storebuf.offset));
422 logthing(LOGTHING_ERROR, "Got unknown command: %d",
424 cmd = KEYD_REPLY_UNKNOWN_CMD;
425 write(fd, &cmd, sizeof(cmd));
432 int sock_close(int fd)
434 shutdown(fd, SHUT_RDWR);
438 int sock_accept(int fd)
440 struct sockaddr_un sock;
445 socklen = sizeof(sock);
446 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
448 ret = fcntl(srv, F_SETFD, 1);
453 while (!sock_do(srv)) ;
460 static void usage(void)
462 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
463 "onak PGP keyserver.\n");
465 puts("\tkeyd [options]\n");
466 puts("\tOptions:\n:");
467 puts("-c <file> - use <file> as the config file");
468 puts("-f - run in the foreground");
469 puts("-h - show this help text");
473 int main(int argc, char *argv[])
478 char *configfile = NULL;
479 bool foreground = false;
482 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
485 configfile = strdup(optarg);
497 readconfig(configfile);
500 initlogthing("keyd", config.logfile);
501 config.use_keyd = false;
508 signal(SIGPIPE, SIG_IGN);
511 stats = calloc(1, sizeof(*stats));
513 logthing(LOGTHING_ERROR,
514 "Couldn't allocate memory for stats structure.");
517 stats->started = time(NULL);
519 snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
520 fd = sock_init(sockname);
526 config.dbbackend->initdb(false);
528 logthing(LOGTHING_NOTICE, "Accepting connections.");
529 while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
530 logthing(LOGTHING_INFO, "Accepted connection.");
534 config.dbbackend->cleanupdb();
544 return(EXIT_SUCCESS);