X-Git-Url: https://the.earth.li/gitweb/?a=blobdiff_plain;f=keyd.c;h=9994dea303b812588fe00ff9d18cec5424ce8668;hb=a799cc2909f47d918d1ec7171a9edba28a9f5136;hp=2784257925a6f170ed7c8d8d172da26eafa77ad7;hpb=855bdec49dfc68aaa27b77b52abd2b6f01b58bac;p=onak.git
diff --git a/keyd.c b/keyd.c
index 2784257..9994dea 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;
}
@@ -344,6 +375,7 @@ static int sock_do(struct onak_dbctx *dbctx, int fd)
bytes = read(fd, search, count);
if (bytes != count) {
ret = 1;
+ free(search);
break;
}
search[count] = 0;
@@ -365,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;
}
@@ -395,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);
@@ -574,8 +608,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;
@@ -621,7 +655,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) {
@@ -630,7 +665,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) {
@@ -679,8 +714,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);