systemd: Provide a charon-systemd daemon targeting full systemd integration
authorMartin Willi <martin@revosec.ch>
Thu, 17 Jul 2014 14:16:23 +0000 (16:16 +0200)
committerMartin Willi <martin@revosec.ch>
Mon, 22 Sep 2014 11:55:11 +0000 (13:55 +0200)
configure.ac
init/Makefile.am
src/Makefile.am
src/charon-systemd/.gitignore [new file with mode: 0644]
src/charon-systemd/Makefile.am [new file with mode: 0644]
src/charon-systemd/charon-systemd.c [new file with mode: 0644]

index b3a7366..b4c4d85 100644 (file)
@@ -73,7 +73,6 @@ if test -n "$PKG_CONFIG"; then
        systemdsystemunitdir_default=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)
 fi
 ARG_WITH_SET([systemdsystemunitdir], [$systemdsystemunitdir_default], [directory for systemd service files])
-AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$systemdsystemunitdir" -a "x$systemdsystemunitdir" != xno])
 AC_SUBST(systemdsystemunitdir)
 
 AC_ARG_WITH(
@@ -273,6 +272,7 @@ ARG_DISBL_SET([pki],            [disable pki certificate utility.])
 ARG_DISBL_SET([scepclient],     [disable SCEP client tool.])
 ARG_DISBL_SET([scripts],        [disable additional utilities (found in directory scripts).])
 ARG_ENABL_SET([svc],            [enable charon Windows service.])
+ARG_ENABL_SET([systemd],        [enable systemd specific IKE daemon charon-systemd.])
 ARG_ENABL_SET([swanctl],        [enable swanctl configuration and control tool.])
 ARG_ENABL_SET([tkm],            [enable Trusted Key Manager support.])
 # optional features
@@ -1467,9 +1467,9 @@ AM_CONDITIONAL(USE_PKI, test x$pki = xtrue)
 AM_CONDITIONAL(USE_SCEPCLIENT, test x$scepclient = xtrue)
 AM_CONDITIONAL(USE_SCRIPTS, test x$scripts = xtrue)
 AM_CONDITIONAL(USE_CONFTEST, test x$conftest = xtrue)
-AM_CONDITIONAL(USE_LIBSTRONGSWAN, test x$charon = xtrue -o x$pki = xtrue -o x$scepclient = xtrue -o x$conftest = xtrue -o x$fast = xtrue -o x$imcv = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$tls = xtrue -o x$tnc_tnccs = xtrue -o x$aikgen = xtrue -o x$svc = xtrue)
-AM_CONDITIONAL(USE_LIBHYDRA, test x$charon = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$svc = xtrue)
-AM_CONDITIONAL(USE_LIBCHARON, test x$charon = xtrue -o x$conftest = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$svc = xtrue)
+AM_CONDITIONAL(USE_LIBSTRONGSWAN, test x$charon = xtrue -o x$pki = xtrue -o x$scepclient = xtrue -o x$conftest = xtrue -o x$fast = xtrue -o x$imcv = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$tls = xtrue -o x$tnc_tnccs = xtrue -o x$aikgen = xtrue -o x$svc = xtrue -o x$systemd = xtrue)
+AM_CONDITIONAL(USE_LIBHYDRA, test x$charon = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$svc = xtrue -o x$systemd = xtrue)
+AM_CONDITIONAL(USE_LIBCHARON, test x$charon = xtrue -o x$conftest = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$svc = xtrue -o x$systemd = xtrue)
 AM_CONDITIONAL(USE_LIBIPSEC, test x$libipsec = xtrue)
 AM_CONDITIONAL(USE_LIBTNCIF, test x$tnc_tnccs = xtrue -o x$imcv = xtrue)
 AM_CONDITIONAL(USE_LIBTNCCS, test x$tnc_tnccs = xtrue)
@@ -1494,6 +1494,8 @@ AM_CONDITIONAL(USE_CMD, test x$cmd = xtrue)
 AM_CONDITIONAL(USE_AIKGEN, test x$aikgen = xtrue)
 AM_CONDITIONAL(USE_SWANCTL, test x$swanctl = xtrue)
 AM_CONDITIONAL(USE_SVC, test x$svc = xtrue)
+AM_CONDITIONAL(USE_SYSTEMD, test x$systemd = xtrue)
+AM_CONDITIONAL(USE_LEGACY_SYSTEMD, test -n "$systemdsystemunitdir" -a "x$systemdsystemunitdir" != xno)
 
 # ========================
 #  set global definitions
@@ -1637,6 +1639,7 @@ AC_CONFIG_FILES([
        src/charon-tkm/Makefile
        src/charon-cmd/Makefile
        src/charon-svc/Makefile
+       src/charon-systemd/Makefile
        src/libcharon/Makefile
        src/libcharon/plugins/eap_aka/Makefile
        src/libcharon/plugins/eap_aka_3gpp2/Makefile
index 69439a1..83faf9d 100644 (file)
@@ -1,6 +1,6 @@
 
 SUBDIRS =
 
-if HAVE_SYSTEMD
+if USE_LEGACY_SYSTEMD
   SUBDIRS += systemd
 endif
index 95c68d0..603c9d1 100644 (file)
@@ -60,6 +60,10 @@ if USE_CHARON
   SUBDIRS += charon
 endif
 
+if USE_SYSTEMD
+  SUBDIRS += charon-systemd
+endif
+
 if USE_NM
   SUBDIRS += charon-nm
 endif
diff --git a/src/charon-systemd/.gitignore b/src/charon-systemd/.gitignore
new file mode 100644 (file)
index 0000000..da7b648
--- /dev/null
@@ -0,0 +1 @@
+charon-systemd
diff --git a/src/charon-systemd/Makefile.am b/src/charon-systemd/Makefile.am
new file mode 100644 (file)
index 0000000..677ea3a
--- /dev/null
@@ -0,0 +1,18 @@
+sbin_PROGRAMS = charon-systemd
+
+charon_systemd_SOURCES = \
+charon-systemd.c
+
+charon-systemd.o :     $(top_builddir)/config.status
+
+charon_systemd_CPPFLAGS = \
+       -I$(top_srcdir)/src/libstrongswan \
+       -I$(top_srcdir)/src/libhydra \
+       -I$(top_srcdir)/src/libcharon \
+       -DPLUGINS=\""${charon_plugins}\""
+
+charon_systemd_LDADD = \
+       $(top_builddir)/src/libstrongswan/libstrongswan.la \
+       $(top_builddir)/src/libhydra/libhydra.la \
+       $(top_builddir)/src/libcharon/libcharon.la \
+       -lsystemd-daemon -lm $(PTHREADLIB) $(DLLIB)
diff --git a/src/charon-systemd/charon-systemd.c b/src/charon-systemd/charon-systemd.c
new file mode 100644 (file)
index 0000000..de9d9b5
--- /dev/null
@@ -0,0 +1,219 @@
+/*
+ * Copyright (C) 2006-2012 Tobias Brunner
+ * Copyright (C) 2005-2009 Martin Willi
+ * Copyright (C) 2006 Daniel Roethlisberger
+ * Copyright (C) 2005 Jan Hutter
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#include <signal.h>
+#include <stdio.h>
+#include <pthread.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <unistd.h>
+#include <errno.h>
+#include <systemd/sd-daemon.h>
+
+#include <hydra.h>
+#include <daemon.h>
+
+#include <library.h>
+#include <utils/backtrace.h>
+#include <threading/thread.h>
+
+/**
+ * hook in library for debugging messages
+ */
+extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
+
+/**
+ * Logging hook for library logs, using stderr output
+ */
+static void dbg_stderr(debug_t group, level_t level, char *fmt, ...)
+{
+       va_list args;
+
+       if (level <= 1)
+       {
+               va_start(args, fmt);
+               fprintf(stderr, "00[%N] ", debug_names, group);
+               vfprintf(stderr, fmt, args);
+               fprintf(stderr, "\n");
+               va_end(args);
+       }
+}
+
+/**
+ * Run the daemon and handle unix signals
+ */
+static int run()
+{
+       sigset_t set;
+
+       sigemptyset(&set);
+       sigaddset(&set, SIGTERM);
+       sigprocmask(SIG_BLOCK, &set, NULL);
+
+       sd_notify(0, "READY=1\n");
+
+       while (TRUE)
+       {
+               int sig, error;
+
+               error = sigwait(&set, &sig);
+               if (error)
+               {
+                       DBG1(DBG_DMN, "waiting for signal failed: %s", strerror(error));
+                       return SS_RC_INITIALIZATION_FAILED;
+               }
+               switch (sig)
+               {
+                       case SIGTERM:
+                       {
+                               DBG1(DBG_DMN, "SIGTERM received, shutting down");
+                               charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
+                               return 0;
+                       }
+                       default:
+                       {
+                               DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
+                               break;
+                       }
+               }
+       }
+}
+
+/**
+ * lookup UID and GID
+ */
+static bool lookup_uid_gid()
+{
+#ifdef IPSEC_USER
+       if (!lib->caps->resolve_uid(lib->caps, IPSEC_USER))
+       {
+               return FALSE;
+       }
+#endif /* IPSEC_USER */
+#ifdef IPSEC_GROUP
+       if (!lib->caps->resolve_gid(lib->caps, IPSEC_GROUP))
+       {
+               return FALSE;
+       }
+#endif /* IPSEC_GROUP */
+       return TRUE;
+}
+
+/**
+ * Handle SIGSEGV/SIGILL signals raised by threads
+ */
+static void segv_handler(int signal)
+{
+       backtrace_t *backtrace;
+
+       DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
+       backtrace = backtrace_create(2);
+       backtrace->log(backtrace, NULL, TRUE);
+       backtrace->log(backtrace, stderr, TRUE);
+       backtrace->destroy(backtrace);
+
+       DBG1(DBG_DMN, "killing ourself, received critical signal");
+       abort();
+}
+
+/**
+ * Main function, starts the daemon.
+ */
+int main(int argc, char *argv[])
+{
+       struct sigaction action;
+       struct utsname utsname;
+
+       dbg = dbg_stderr;
+
+       if (uname(&utsname) != 0)
+       {
+               memset(&utsname, 0, sizeof(utsname));
+       }
+
+       sd_notifyf(0, "STATUS=Starting charon-systemd, strongSwan %s, %s %s, %s",
+                          VERSION, utsname.sysname, utsname.release, utsname.machine);
+
+       atexit(library_deinit);
+       if (!library_init(NULL, "charon-systemd"))
+       {
+               sd_notifyf(0, "STATUS=libstrongswan initialization failed");
+               return SS_RC_INITIALIZATION_FAILED;
+       }
+       if (lib->integrity &&
+               !lib->integrity->check_file(lib->integrity, "charon-systemd", argv[0]))
+       {
+               sd_notifyf(0, "STATUS=integrity check of charon-systemd failed");
+               return SS_RC_INITIALIZATION_FAILED;
+       }
+       atexit(libhydra_deinit);
+       if (!libhydra_init())
+       {
+               sd_notifyf(0, "STATUS=libhydra initialization failed");
+               return SS_RC_INITIALIZATION_FAILED;
+       }
+       atexit(libcharon_deinit);
+       if (!libcharon_init())
+       {
+               sd_notifyf(0, "STATUS=libcharon initialization failed");
+               return SS_RC_INITIALIZATION_FAILED;
+       }
+       if (!lookup_uid_gid())
+       {
+               sd_notifyf(0, "STATUS=unkown uid/gid");
+               return SS_RC_INITIALIZATION_FAILED;
+       }
+       charon->load_loggers(charon, NULL, FALSE);
+
+       if (!charon->initialize(charon, PLUGINS))
+       {
+               sd_notifyf(0, "STATUS=charon initialization failed");
+               return SS_RC_INITIALIZATION_FAILED;
+       }
+       lib->plugins->status(lib->plugins, LEVEL_CTRL);
+
+       if (!lib->caps->drop(lib->caps))
+       {
+               sd_notifyf(0, "STATUS=dropping capabilities failed");
+               return SS_RC_INITIALIZATION_FAILED;
+       }
+
+       /* add handler for SEGV and ILL,
+        * INT, TERM and HUP are handled by sigwait() in run() */
+       action.sa_handler = segv_handler;
+       action.sa_flags = 0;
+       sigemptyset(&action.sa_mask);
+       sigaddset(&action.sa_mask, SIGINT);
+       sigaddset(&action.sa_mask, SIGTERM);
+       sigaddset(&action.sa_mask, SIGHUP);
+       sigaction(SIGSEGV, &action, NULL);
+       sigaction(SIGILL, &action, NULL);
+       sigaction(SIGBUS, &action, NULL);
+       action.sa_handler = SIG_IGN;
+       sigaction(SIGPIPE, &action, NULL);
+
+       pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
+
+       charon->start(charon);
+
+       sd_notifyf(0, "STATUS=charon-systemd running, strongSwan %s, %s %s, %s",
+                          VERSION, utsname.sysname, utsname.release, utsname.machine);
+
+       return run();
+}