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"
41 logthing(LOGTHING_CRITICAL,
42 "Failed to fork into background: %d (%s)",
47 logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
53 freopen("/dev/null", "r", stdin);
54 freopen("/dev/null", "w", stdout);
55 freopen("/dev/null", "w", stderr);
60 void iteratefunc(void *ctx, struct openpgp_publickey *key)
62 struct openpgp_packet_list *packets = NULL;
63 struct openpgp_packet_list *list_end = NULL;
64 struct buffer_ctx storebuf;
66 int *fd = (int *) ctx;
71 storebuf.buffer = malloc(8192);
73 logthing(LOGTHING_TRACE,
74 "Iterating over 0x%016" PRIX64 ".",
77 flatten_publickey(key,
80 write_openpgp_stream(buffer_putchar,
83 logthing(LOGTHING_TRACE,
86 ret = write(*fd, &storebuf.offset,
87 sizeof(storebuf.offset));
89 write(*fd, storebuf.buffer,
93 free(storebuf.buffer);
94 storebuf.buffer = NULL;
95 storebuf.size = storebuf.offset = 0;
96 free_packet_list(packets);
97 packets = list_end = NULL;
103 int sock_init(const char *sockname)
105 struct sockaddr_un sock;
109 fd = socket(PF_UNIX, SOCK_STREAM, 0);
111 ret = fcntl(fd, F_SETFD, 1);
115 sock.sun_family = AF_UNIX;
116 strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
118 ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
130 uint32_t cmd = KEYD_CMD_UNKNOWN;
136 struct openpgp_publickey *key = NULL;
137 struct openpgp_packet_list *packets = NULL;
138 struct openpgp_packet_list *list_end = NULL;
139 struct buffer_ctx storebuf;
142 * Get the command from the client.
144 bytes = read(fd, &cmd, sizeof(cmd));
146 logthing(LOGTHING_DEBUG, "Read %d bytes, command: %d", bytes, cmd);
148 if (bytes != sizeof(cmd)) {
154 case KEYD_CMD_VERSION:
156 write(fd, &cmd, sizeof(cmd));
157 cmd = sizeof(keyd_version);
158 write(fd, &cmd, sizeof(cmd));
159 write(fd, &keyd_version, sizeof(keyd_version));
163 write(fd, &cmd, sizeof(cmd));
164 bytes = read(fd, &keyid, sizeof(keyid));
165 if (bytes != sizeof(keyid)) {
170 logthing(LOGTHING_INFO,
171 "Fetching 0x%" PRIX64
175 fetch_key(keyid, &key, false));
177 storebuf.size = 8192;
178 storebuf.buffer = malloc(8192);
180 flatten_publickey(key,
183 write_openpgp_stream(buffer_putchar,
186 logthing(LOGTHING_TRACE,
189 write(fd, &storebuf.offset,
190 sizeof(storebuf.offset));
191 write(fd, storebuf.buffer,
194 free(storebuf.buffer);
195 storebuf.buffer = NULL;
196 storebuf.size = storebuf.offset = 0;
197 free_packet_list(packets);
198 packets = list_end = NULL;
202 write(fd, &storebuf.offset,
203 sizeof(storebuf.offset));
207 case KEYD_CMD_GETTEXT:
209 write(fd, &cmd, sizeof(cmd));
210 bytes = read(fd, &count, sizeof(count));
211 if (bytes != sizeof(count)) {
216 search = malloc(count+1);
217 read(fd, search, count);
219 logthing(LOGTHING_INFO,
220 "Fetching %s, result: %d",
223 fetch_key_text(search, &key));
225 storebuf.size = 8192;
226 storebuf.buffer = malloc(8192);
228 flatten_publickey(key,
231 write_openpgp_stream(buffer_putchar,
234 logthing(LOGTHING_TRACE,
237 write(fd, &storebuf.offset,
238 sizeof(storebuf.offset));
239 write(fd, storebuf.buffer,
242 free(storebuf.buffer);
243 storebuf.buffer = NULL;
244 storebuf.size = storebuf.offset = 0;
245 free_packet_list(packets);
246 packets = list_end = NULL;
250 write(fd, &storebuf.offset,
251 sizeof(storebuf.offset));
257 write(fd, &cmd, sizeof(cmd));
259 bytes = read(fd, &storebuf.size,
260 sizeof(storebuf.size));
261 logthing(LOGTHING_TRACE, "Reading %d bytes.",
263 if (bytes != sizeof(storebuf.size)) {
266 if (ret == 0 && storebuf.size > 0) {
267 storebuf.buffer = malloc(storebuf.size);
269 while (bytes >= 0 && count < storebuf.size) {
271 &storebuf.buffer[count],
272 storebuf.size - count);
273 logthing(LOGTHING_TRACE,
278 read_openpgp_stream(buffer_fetchchar,
282 parse_keys(packets, &key);
283 config.dbbackend->store_key(key, false, false);
284 free_packet_list(packets);
288 free(storebuf.buffer);
289 storebuf.buffer = NULL;
290 storebuf.size = storebuf.offset = 0;
293 case KEYD_CMD_DELETE:
295 write(fd, &cmd, sizeof(cmd));
296 bytes = read(fd, &keyid, sizeof(keyid));
297 if (bytes != sizeof(keyid)) {
301 logthing(LOGTHING_INFO,
302 "Deleting 0x%" PRIX64
305 config.dbbackend->delete_key(
309 case KEYD_CMD_GETFULLKEYID:
311 write(fd, &cmd, sizeof(cmd));
312 bytes = read(fd, &keyid, sizeof(keyid));
313 if (bytes != sizeof(keyid)) {
317 keyid = config.dbbackend->getfullkeyid(keyid);
319 write(fd, &cmd, sizeof(cmd));
320 write(fd, &keyid, sizeof(keyid));
323 case KEYD_CMD_KEYITER:
325 write(fd, &cmd, sizeof(cmd));
326 config.dbbackend->iterate_keys(iteratefunc,
329 write(fd, &bytes, sizeof(bytes));
333 write(fd, &cmd, sizeof(cmd));
338 write(fd, &cmd, sizeof(cmd));
339 logthing(LOGTHING_NOTICE,
340 "Exiting due to quit request.");
345 logthing(LOGTHING_ERROR, "Got unknown command: %d",
347 cmd = KEYD_REPLY_UNKNOWN_CMD;
348 write(fd, &cmd, sizeof(cmd));
355 int sock_close(int fd)
357 shutdown(fd, SHUT_RDWR);
361 int sock_accept(int fd)
363 struct sockaddr_un sock;
368 socklen = sizeof(sock);
369 srv = accept(fd, (struct sockaddr *) &sock, &socklen);
371 ret = fcntl(srv, F_SETFD, 1);
375 while (!sock_do(srv)) ;
382 static void usage(void)
384 puts("keyd " PACKAGE_VERSION " - backend key serving daemon for the "
385 "onak PGP keyserver.\n");
387 puts("\tkeyd [options]\n");
388 puts("\tOptions:\n:");
389 puts("-c <file> - use <file> as the config file");
390 puts("-f - run in the foreground");
391 puts("-h - show this help text");
395 int main(int argc, char *argv[])
400 char *configfile = NULL;
401 bool foreground = false;
404 while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
407 configfile = strdup(optarg);
419 readconfig(configfile);
422 initlogthing("keyd", config.logfile);
423 config.use_keyd = false;
430 signal(SIGPIPE, SIG_IGN);
432 snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
433 fd = sock_init(sockname);
439 config.dbbackend->initdb(false);
441 logthing(LOGTHING_NOTICE, "Accepting connections.");
442 while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
443 logthing(LOGTHING_INFO, "Accepted connection.");
447 config.dbbackend->cleanupdb();
455 return(EXIT_SUCCESS);