* keyd.c - key retrieval daemon
*
* Jonathan McDowell <noodles@earth.li>
- *
+ *
* Copyright 2004 Project Purple
*/
+#include <errno.h>
#include <fcntl.h>
+#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include "onak-conf.h"
#include "parsekey.h"
+void daemonize(void)
+{
+ pid_t pid;
+
+ pid = fork();
+
+ if (pid < 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Failed to fork into background: %d (%s)",
+ errno,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ } else if (pid > 0) {
+ logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
+ exit(EXIT_SUCCESS);
+ }
+
+ pid = setsid();
+
+ freopen("/dev/null", "r", stdin);
+ freopen("/dev/null", "w", stdout);
+ freopen("/dev/null", "w", stderr);
+
+ return;
+}
+
void iteratefunc(void *ctx, struct openpgp_publickey *key)
{
struct openpgp_packet_list *packets = NULL;
struct openpgp_packet_list *list_end = NULL;
struct buffer_ctx storebuf;
int ret = 0;
- int fd = (int) ctx;
+ int *fd = (int *) ctx;
if (key != NULL) {
storebuf.offset = 0;
storebuf.buffer = malloc(8192);
logthing(LOGTHING_TRACE,
- "Iterating over 0x%016llX.",
+ "Iterating over 0x%016" PRIX64 ".",
get_keyid(key));
flatten_publickey(key,
logthing(LOGTHING_TRACE,
"Sending %d bytes.",
storebuf.offset);
- ret = write(fd, &storebuf.offset,
+ ret = write(*fd, &storebuf.offset,
sizeof(storebuf.offset));
if (ret != 0) {
- write(fd, storebuf.buffer,
+ write(*fd, storebuf.buffer,
storebuf.offset);
}
int sock_do(int fd)
{
- int cmd = KEYD_CMD_UNKNOWN;
+ uint32_t cmd = KEYD_CMD_UNKNOWN;
ssize_t bytes = 0;
ssize_t count = 0;
int ret = 0;
case KEYD_CMD_VERSION:
cmd = KEYD_REPLY_OK;
write(fd, &cmd, sizeof(cmd));
+ cmd = sizeof(keyd_version);
+ write(fd, &cmd, sizeof(cmd));
write(fd, &keyd_version, sizeof(keyd_version));
break;
case KEYD_CMD_GET:
storebuf.offset = 0;
if (ret == 0) {
logthing(LOGTHING_INFO,
- "Fetching 0x%llX, result: %d",
+ "Fetching 0x%" PRIX64
+ ", result: %d",
keyid,
+ config.dbbackend->
fetch_key(keyid, &key, false));
if (key != NULL) {
storebuf.size = 8192;
logthing(LOGTHING_INFO,
"Fetching %s, result: %d",
search,
+ config.dbbackend->
fetch_key_text(search, &key));
if (key != NULL) {
storebuf.size = 8192;
&packets,
0);
parse_keys(packets, &key);
- store_key(key, false, false);
+ config.dbbackend->store_key(key, false, false);
free_packet_list(packets);
packets = NULL;
free_publickey(key);
}
if (ret == 0) {
logthing(LOGTHING_INFO,
- "Deleting 0x%llX, result: %d",
+ "Deleting 0x%" PRIX64
+ ", result: %d",
keyid,
- delete_key(keyid, false));
+ config.dbbackend->delete_key(
+ keyid, false));
}
break;
case KEYD_CMD_GETFULLKEYID:
ret = 1;
}
if (ret == 0) {
- keyid = getfullkeyid(keyid);
+ keyid = config.dbbackend->getfullkeyid(keyid);
+ cmd = sizeof(keyid);
+ write(fd, &cmd, sizeof(cmd));
write(fd, &keyid, sizeof(keyid));
}
break;
case KEYD_CMD_KEYITER:
cmd = KEYD_REPLY_OK;
write(fd, &cmd, sizeof(cmd));
- iterate_keys(iteratefunc, (void *) fd);
+ config.dbbackend->iterate_keys(iteratefunc,
+ &fd);
bytes = 0;
write(fd, &bytes, sizeof(bytes));
break;
case KEYD_CMD_CLOSE:
+ cmd = KEYD_REPLY_OK;
+ write(fd, &cmd, sizeof(cmd));
ret = 1;
break;
case KEYD_CMD_QUIT:
+ cmd = KEYD_REPLY_OK;
+ write(fd, &cmd, sizeof(cmd));
+ ret = 1;
trytocleanup();
break;
default:
int sock_close(int fd)
{
- return shutdown(fd, SHUT_RDWR);
+ shutdown(fd, SHUT_RDWR);
+ return close(fd);
}
int sock_accept(int fd)
int fd = -1;
fd_set rfds;
char sockname[1024];
+ char *configfile = NULL;
+ bool foreground = false;
+ int optchar;
+
+ while ((optchar = getopt(argc, argv, "c:f")) != -1 ) {
+ switch (optchar) {
+ case 'c':
+ configfile = strdup(optarg);
+ break;
+ case 'f':
+ foreground = true;
+ break;
+ }
+ }
- readconfig(NULL);
+ readconfig(configfile);
+ free(configfile);
+ configfile = NULL;
initlogthing("keyd", config.logfile);
+ config.use_keyd = false;
+
+ if (!foreground) {
+ daemonize();
+ }
catchsignals();
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
- initdb(false);
+ config.dbbackend->initdb(false);
logthing(LOGTHING_NOTICE, "Accepting connections.");
while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
sock_accept(fd);
FD_SET(fd, &rfds);
}
- cleanupdb();
+ config.dbbackend->cleanupdb();
sock_close(fd);
unlink(sockname);
}
cleanuplogthing();
cleanupconfig();
-
+
return(EXIT_SUCCESS);
}