X-Git-Url: http://the.earth.li/gitweb/?a=blobdiff_plain;f=keyd.c;h=b8b8ad37e6bf1eff136eb089807687cc51650958;hb=b2f2bc60555e2d953069c40d8d3cdecfcec9ac29;hp=a89f0e1f37aade33ecf00d4f171f92d191bf1dee;hpb=c04c1c60469823c01268187ec49102d1ff540806;p=onak.git diff --git a/keyd.c b/keyd.c index a89f0e1..b8b8ad3 100644 --- a/keyd.c +++ b/keyd.c @@ -65,7 +65,7 @@ void daemonize(void) exit(EXIT_SUCCESS); } - pid = setsid(); + setsid(); freopen("/dev/null", "r", stdin); freopen("/dev/null", "w", stdout); @@ -139,25 +139,29 @@ int sock_init(const char *sockname) if (ret != -1) { ret = listen(fd, 5); + if (ret == -1) { + close(fd); + fd = -1; + } } - + return fd; } -int sock_do(int fd) +int sock_do(struct onak_dbctx *dbctx, int fd) { uint32_t cmd = KEYD_CMD_UNKNOWN; ssize_t bytes = 0; ssize_t count = 0; int ret = 0; uint64_t keyid = 0; - uint8_t fp[MAX_FINGERPRINT_LEN]; char *search = NULL; struct openpgp_publickey *key = NULL; struct openpgp_packet_list *packets = NULL; struct openpgp_packet_list *list_end = NULL; struct buffer_ctx storebuf; struct skshash hash; + struct openpgp_fingerprint fingerprint; /* * Get the command from the client. @@ -197,8 +201,8 @@ int sock_do(int fd) "Fetching 0x%" PRIX64 ", result: %d", keyid, - config.dbbackend-> - fetch_key_id(keyid, + dbctx->fetch_key_id(dbctx, + keyid, &key, false)); if (key != NULL) { storebuf.size = 8192; @@ -238,15 +242,16 @@ int sock_do(int fd) if (bytes > MAX_FINGERPRINT_LEN) { ret = 1; } else { - read(fd, fp, bytes); + fingerprint.length = bytes; + read(fd, fingerprint.fp, bytes); } storebuf.offset = 0; if (ret == 0) { logthing(LOGTHING_INFO, "Fetching by fingerprint" ", result: %d", - config.dbbackend-> - fetch_key_fp(fp, bytes, + dbctx->fetch_key_fp(dbctx, + &fingerprint, &key, false)); if (key != NULL) { storebuf.size = 8192; @@ -295,8 +300,8 @@ int sock_do(int fd) logthing(LOGTHING_INFO, "Fetching %s, result: %d", search, - config.dbbackend-> - fetch_key_text(search, &key)); + dbctx->fetch_key_text(dbctx, + search, &key)); if (key != NULL) { storebuf.size = 8192; storebuf.buffer = malloc(8192); @@ -357,7 +362,7 @@ int sock_do(int fd) &packets, 0); parse_keys(packets, &key); - config.dbbackend->store_key(key, false, false); + dbctx->store_key(dbctx, key, false, false); free_packet_list(packets); packets = NULL; free_publickey(key); @@ -379,7 +384,7 @@ int sock_do(int fd) "Deleting 0x%" PRIX64 ", result: %d", keyid, - config.dbbackend->delete_key( + dbctx->delete_key(dbctx, keyid, false)); } break; @@ -391,7 +396,7 @@ int sock_do(int fd) ret = 1; } if (ret == 0) { - keyid = config.dbbackend->getfullkeyid(keyid); + keyid = dbctx->getfullkeyid(dbctx, keyid); cmd = sizeof(keyid); write(fd, &cmd, sizeof(cmd)); write(fd, &keyid, sizeof(keyid)); @@ -400,7 +405,7 @@ int sock_do(int fd) case KEYD_CMD_KEYITER: cmd = KEYD_REPLY_OK; write(fd, &cmd, sizeof(cmd)); - config.dbbackend->iterate_keys(iteratefunc, + dbctx->iterate_keys(dbctx, iteratefunc, &fd); bytes = 0; write(fd, &bytes, sizeof(bytes)); @@ -438,9 +443,8 @@ int sock_do(int fd) logthing(LOGTHING_INFO, "Fetching by hash" ", result: %d", - config.dbbackend-> - fetch_key_skshash(&hash, - &key)); + dbctx->fetch_key_skshash(dbctx, + &hash, &key)); if (key != NULL) { storebuf.size = 8192; storebuf.buffer = malloc(8192); @@ -531,10 +535,14 @@ int main(int argc, char *argv[]) char *configfile = NULL; bool foreground = false; int optchar; + struct onak_dbctx *dbctx; while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) { switch (optchar) { case 'c': + if (configfile != NULL) { + free(configfile); + } configfile = strdup(optarg); break; case 'f': @@ -578,7 +586,7 @@ int main(int argc, char *argv[]) maxfd = fd; memset(clients, -1, sizeof (clients)); - config.dbbackend->initdb(false); + dbctx = config.dbinit(false); logthing(LOGTHING_NOTICE, "Accepting connections."); while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) { @@ -592,7 +600,7 @@ int main(int argc, char *argv[]) FD_ISSET(clients[i], &rfds)) { logthing(LOGTHING_DEBUG, "Handling connection for client %d.", i); - if (sock_do(clients[i])) { + if (sock_do(dbctx, clients[i])) { sock_close(clients[i]); clients[i] = -1; logthing(LOGTHING_DEBUG, @@ -626,7 +634,7 @@ int main(int argc, char *argv[]) } } } - config.dbbackend->cleanupdb(); + dbctx->cleanupdb(dbctx); sock_close(fd); unlink(sockname); }