X-Git-Url: http://the.earth.li/gitweb/?a=blobdiff_plain;f=keyd.c;h=ddd1366a9bb7c94b56b6d31fc7b181005ae2c663;hb=5e2c81ee4acb5bf3eb4afdbc766646ba06f96dd9;hp=18e9ddca873a84e37c172408bb0ecdc20b611b19;hpb=ff3b87253171dc8b76ec26b70ba738a2d3a603ac;p=onak.git
diff --git a/keyd.c b/keyd.c
index 18e9ddc..ddd1366 100644
--- a/keyd.c
+++ b/keyd.c
@@ -13,14 +13,15 @@
* more details.
*
* You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * this program. If not, see .
*/
#include
#include
#include
+#include
#include
+#include
#include
#include
#include
@@ -31,6 +32,12 @@
#include
#include
+#include "build-config.h"
+
+#ifdef HAVE_SYSTEMD
+#include
+#endif
+
#include "charfuncs.h"
#include "cleanup.h"
#include "keyd.h"
@@ -41,11 +48,14 @@
#include "mem.h"
#include "onak-conf.h"
#include "parsekey.h"
-#include "version.h"
/* Maximum number of clients we're prepared to accept at once */
#define MAX_CLIENTS 16
+#ifdef HAVE_SYSTEMD
+static bool using_socket_activation = false;
+#endif
+
static struct keyd_stats *stats;
static void daemonize(void)
@@ -189,26 +199,47 @@ static int sock_init(const char *sockname)
struct sockaddr_un sock;
int fd = -1;
int ret = -1;
+#ifdef HAVE_SYSTEMD
+ int n;
- fd = socket(PF_UNIX, SOCK_STREAM, 0);
- if (fd != -1) {
- ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
- }
+ n = sd_listen_fds(0);
+ if (n > 1) {
+ logthing(LOGTHING_ERROR,
+ "Too many file descriptors received from systemd.");
+ } else if (n == 1) {
+ fd = SD_LISTEN_FDS_START + 0;
+ if (sd_is_socket_unix(fd, SOCK_STREAM, 1, NULL, 0) <= 0) {
+ logthing(LOGTHING_ERROR,
+ "systemd passed an invalid socket.");
+ fd = -1;
+ }
+ using_socket_activation = true;
+ } else {
+#endif
+ fd = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (fd != -1) {
+ ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
+ }
- if (ret != -1) {
- sock.sun_family = AF_UNIX;
- strncpy(sock.sun_path, sockname, sizeof(sock.sun_path) - 1);
- unlink(sockname);
- ret = bind(fd, (struct sockaddr *) &sock, sizeof(sock));
- }
+ if (ret != -1) {
+ sock.sun_family = AF_UNIX;
+ strncpy(sock.sun_path, sockname,
+ sizeof(sock.sun_path) - 1);
+ unlink(sockname);
+ ret = bind(fd, (struct sockaddr *) &sock,
+ sizeof(sock));
+ }
- if (ret != -1) {
- ret = listen(fd, 5);
- if (ret == -1) {
- close(fd);
- fd = -1;
+ if (ret != -1) {
+ ret = listen(fd, 5);
+ if (ret == -1) {
+ close(fd);
+ fd = -1;
+ }
}
+#ifdef HAVE_SYSTEMD
}
+#endif
return fd;
}
@@ -366,6 +397,7 @@ static int sock_do(struct onak_dbctx *dbctx, int fd)
}
break;
case KEYD_CMD_STORE:
+ case KEYD_CMD_UPDATE:
if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
ret = 1;
}
@@ -396,7 +428,8 @@ static int sock_do(struct onak_dbctx *dbctx, int fd)
&packets,
0);
parse_keys(packets, &key);
- dbctx->store_key(dbctx, key, false, false);
+ dbctx->store_key(dbctx, key, false,
+ (cmd == KEYD_CMD_UPDATE));
free_packet_list(packets);
packets = NULL;
free_publickey(key);
@@ -411,8 +444,9 @@ static int sock_do(struct onak_dbctx *dbctx, int fd)
ret = 1;
}
if (ret == 0) {
- bytes = read(fd, &keyid, sizeof(keyid));
- if (bytes != sizeof(keyid)) {
+ bytes = read(fd, &fingerprint,
+ sizeof(fingerprint));
+ if (bytes != sizeof(fingerprint)) {
ret = 1;
}
}
@@ -422,32 +456,7 @@ static int sock_do(struct onak_dbctx *dbctx, int fd)
", result: %d",
keyid,
dbctx->delete_key(dbctx,
- keyid, false));
- }
- break;
- case KEYD_CMD_GETFULLKEYID:
- if (!keyd_write_reply(fd, KEYD_REPLY_OK)) {
- ret = 1;
- }
- if (ret == 0) {
- bytes = read(fd, &keyid, sizeof(keyid));
- if (bytes != sizeof(keyid)) {
- ret = 1;
- }
- }
- if (ret == 0) {
- keyid = dbctx->getfullkeyid(dbctx, keyid);
- cmd = sizeof(keyid);
- bytes = write(fd, &cmd, sizeof(cmd));
- if (bytes != sizeof(cmd)) {
- ret = 1;
- }
- }
- if (ret == 0) {
- bytes = write(fd, &keyid, sizeof(keyid));
- if (bytes != sizeof(keyid)) {
- ret = 1;
- }
+ &fingerprint, false));
}
break;
case KEYD_CMD_KEYITER:
@@ -575,8 +584,8 @@ static void usage(void)
int main(int argc, char *argv[])
{
int fd = -1, maxfd, i, clients[MAX_CLIENTS];
- fd_set rfds;
- char sockname[1024];
+ fd_set rfds = { 0 }; /* Avoid scan-build false report for FD_SET */
+ char sockname[100];
char *configfile = NULL;
bool foreground = false;
int optchar;
@@ -622,7 +631,8 @@ int main(int argc, char *argv[])
}
stats->started = time(NULL);
- snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
+ snprintf(sockname, sizeof(sockname) - 1, "%s/%s",
+ config.sock_dir, KEYD_SOCKET);
fd = sock_init(sockname);
if (fd != -1) {
@@ -631,7 +641,7 @@ int main(int argc, char *argv[])
maxfd = fd;
memset(clients, -1, sizeof (clients));
- dbctx = config.dbinit(false);
+ dbctx = config.dbinit(config.backend, false);
logthing(LOGTHING_NOTICE, "Accepting connections.");
while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
@@ -680,8 +690,14 @@ int main(int argc, char *argv[])
}
}
dbctx->cleanupdb(dbctx);
- sock_close(fd);
- unlink(sockname);
+#ifdef HAVE_SYSTEMD
+ if (!using_socket_activation) {
+#endif
+ sock_close(fd);
+ unlink(sockname);
+#ifdef HAVE_SYSTEMD
+ }
+#endif
}
free(stats);