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;
85 storebuf.buffer = malloc(8192);
87 logthing(LOGTHING_TRACE,
88 "Iterating over 0x%016" PRIX64 ".",
91 flatten_publickey(key,
94 write_openpgp_stream(buffer_putchar,
97 logthing(LOGTHING_TRACE,
100 ret = write(*fd, &storebuf.offset,
101 sizeof(storebuf.offset));
103 write(*fd, storebuf.buffer,
107 free(storebuf.buffer);
108 storebuf.buffer = NULL;
109 storebuf.size = storebuf.offset = 0;
110 free_packet_list(packets);
111 packets = list_end = NULL;
117 int sock_init(const char *sockname)
119 struct sockaddr_un sock;
123 fd = socket(PF_UNIX, SOCK_STREAM, 0);
125 ret = fcntl(fd, F_SETFD, 1);
129 sock.sun_family = AF_UNIX;
130 strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
132 ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
144 uint32_t cmd = KEYD_CMD_UNKNOWN;
150 struct openpgp_publickey *key = NULL;
151 struct openpgp_packet_list *packets = NULL;
152 struct openpgp_packet_list *list_end = NULL;
153 struct buffer_ctx storebuf;
157 * Get the command from the client.
159 bytes = read(fd, &cmd, sizeof(cmd));
161 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
163 if (bytes != sizeof(cmd)) {
168 if (cmd < KEYD_CMD_LAST) {
169 stats->command_stats[cmd]++;
171 stats->command_stats[KEYD_CMD_UNKNOWN]++;
174 case KEYD_CMD_VERSION:
176 write(fd, &cmd, sizeof(cmd));
177 cmd = sizeof(keyd_version);
178 write(fd, &cmd, sizeof(cmd));
179 write(fd, &keyd_version, sizeof(keyd_version));
183 write(fd, &cmd, sizeof(cmd));
184 bytes = read(fd, &keyid, sizeof(keyid));
185 if (bytes != sizeof(keyid)) {
190 logthing(LOGTHING_INFO,
191 "Fetching 0x%" PRIX64
195 fetch_key(keyid, &key, false));
197 storebuf.size = 8192;
198 storebuf.buffer = malloc(8192);
200 flatten_publickey(key,
203 write_openpgp_stream(buffer_putchar,
206 logthing(LOGTHING_TRACE,
209 write(fd, &storebuf.offset,
210 sizeof(storebuf.offset));
211 write(fd, storebuf.buffer,
214 free(storebuf.buffer);
215 storebuf.buffer = NULL;
216 storebuf.size = storebuf.offset = 0;
217 free_packet_list(packets);
218 packets = list_end = NULL;
222 write(fd, &storebuf.offset,
223 sizeof(storebuf.offset));
227 case KEYD_CMD_GETTEXT:
229 write(fd, &cmd, sizeof(cmd));
230 bytes = read(fd, &count, sizeof(count));
231 if (bytes != sizeof(count)) {
236 search = malloc(count+1);
237 read(fd, search, count);
239 logthing(LOGTHING_INFO,
240 "Fetching %s, result: %d",
243 fetch_key_text(search, &key));
245 storebuf.size = 8192;
246 storebuf.buffer = malloc(8192);
248 flatten_publickey(key,
251 write_openpgp_stream(buffer_putchar,
254 logthing(LOGTHING_TRACE,
257 write(fd, &storebuf.offset,
258 sizeof(storebuf.offset));
259 write(fd, storebuf.buffer,
262 free(storebuf.buffer);
263 storebuf.buffer = NULL;
264 storebuf.size = storebuf.offset = 0;
265 free_packet_list(packets);
266 packets = list_end = NULL;
270 write(fd, &storebuf.offset,
271 sizeof(storebuf.offset));
277 write(fd, &cmd, sizeof(cmd));
279 bytes = read(fd, &storebuf.size,
280 sizeof(storebuf.size));
281 logthing(LOGTHING_TRACE, "Reading %d bytes.",
283 if (bytes != sizeof(storebuf.size)) {
286 if (ret == 0 && storebuf.size > 0) {
287 storebuf.buffer = malloc(storebuf.size);
289 while (bytes >= 0 && count < storebuf.size) {
291 &storebuf.buffer[count],
292 storebuf.size - count);
293 logthing(LOGTHING_TRACE,
298 read_openpgp_stream(buffer_fetchchar,
302 parse_keys(packets, &key);
303 config.dbbackend->store_key(key, false, false);
304 free_packet_list(packets);
308 free(storebuf.buffer);
309 storebuf.buffer = NULL;
310 storebuf.size = storebuf.offset = 0;
313 case KEYD_CMD_DELETE:
315 write(fd, &cmd, sizeof(cmd));
316 bytes = read(fd, &keyid, sizeof(keyid));
317 if (bytes != sizeof(keyid)) {
321 logthing(LOGTHING_INFO,
322 "Deleting 0x%" PRIX64
325 config.dbbackend->delete_key(
329 case KEYD_CMD_GETFULLKEYID:
331 write(fd, &cmd, sizeof(cmd));
332 bytes = read(fd, &keyid, sizeof(keyid));
333 if (bytes != sizeof(keyid)) {
337 keyid = config.dbbackend->getfullkeyid(keyid);
339 write(fd, &cmd, sizeof(cmd));
340 write(fd, &keyid, sizeof(keyid));
343 case KEYD_CMD_KEYITER:
345 write(fd, &cmd, sizeof(cmd));
346 config.dbbackend->iterate_keys(iteratefunc,
349 write(fd, &bytes, sizeof(bytes));
353 write(fd, &cmd, sizeof(cmd));
358 write(fd, &cmd, sizeof(cmd));
359 logthing(LOGTHING_NOTICE,
360 "Exiting due to quit request.");
366 write(fd, &cmd, sizeof(cmd));
367 cmd = sizeof(*stats);
368 write(fd, &cmd, sizeof(cmd));
372 case KEYD_CMD_GETSKSHASH:
374 write(fd, &cmd, sizeof(cmd));
375 bytes = read(fd, hash.hash, sizeof(hash.hash));
376 if (bytes != sizeof(hash.hash)) {
381 logthing(LOGTHING_INFO,
385 fetch_key_skshash(&hash,
388 storebuf.size = 8192;
389 storebuf.buffer = malloc(8192);
391 flatten_publickey(key,
394 write_openpgp_stream(buffer_putchar,
397 logthing(LOGTHING_TRACE,
400 write(fd, &storebuf.offset,
401 sizeof(storebuf.offset));
402 write(fd, storebuf.buffer,
405 free(storebuf.buffer);
406 storebuf.buffer = NULL;
407 storebuf.size = storebuf.offset = 0;
408 free_packet_list(packets);
409 packets = list_end = NULL;
413 write(fd, &storebuf.offset,
414 sizeof(storebuf.offset));
420 logthing(LOGTHING_ERROR, "Got unknown command: %d",
422 cmd = KEYD_REPLY_UNKNOWN_CMD;
423 write(fd, &cmd, sizeof(cmd));
430 int sock_close(int fd)
432 shutdown(fd, SHUT_RDWR);
436 int sock_accept(int fd)
438 struct sockaddr_un sock;
443 socklen = sizeof(sock);
444 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
446 ret = fcntl(srv, F_SETFD, 1);
451 while (!sock_do(srv)) ;
458 static void usage(void)
460 puts("keyd " ONAK_VERSION " - backend key serving daemon for the "
461 "onak PGP keyserver.\n");
463 puts("\tkeyd [options]\n");
464 puts("\tOptions:\n:");
465 puts("-c <file> - use <file> as the config file");
466 puts("-f - run in the foreground");
467 puts("-h - show this help text");
471 int main(int argc, char *argv[])
476 char *configfile = NULL;
477 bool foreground = false;
480 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
483 configfile = strdup(optarg);
495 readconfig(configfile);
498 initlogthing("keyd", config.logfile);
499 config.use_keyd = false;
506 signal(SIGPIPE, SIG_IGN);
509 stats = calloc(1, sizeof(*stats));
511 logthing(LOGTHING_ERROR,
512 "Couldn't allocate memory for stats structure.");
515 stats->started = time(NULL);
517 snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
518 fd = sock_init(sockname);
524 config.dbbackend->initdb(false);
526 logthing(LOGTHING_NOTICE, "Accepting connections.");
527 while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
528 logthing(LOGTHING_INFO, "Accepted connection.");
532 config.dbbackend->cleanupdb();
542 return(EXIT_SUCCESS);