2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Reto Buerki
4 * Copyright (C) 2012 Adrian-Ken Rueegsegger
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 #include <sys/types.h>
30 #include <plugins/kernel_netlink/kernel_netlink_net.h>
32 #include <utils/backtrace.h>
33 #include <threading/thread.h>
34 #include <sa/keymat.h>
37 #include "tkm_nonceg.h"
38 #include "tkm_diffie_hellman.h"
39 #include "tkm_keymat.h"
40 #include "tkm_listener.h"
41 #include "tkm_kernel_ipsec.h"
44 * TKM bus listener for IKE authorize events.
46 static tkm_listener_t
*listener
;
49 * PID file, in which charon-tkm stores its process id
51 static char *pidfile_name
= NULL
;
54 * Global reference to PID file (required to truncate, if undeletable)
56 static FILE *pidfile
= NULL
;
59 * Hook in library for debugging messages
61 extern void (*dbg
) (debug_t group
, level_t level
, char *fmt
, ...);
64 * Simple logging hook for library logs, using syslog output
66 static void dbg_syslog(debug_t group
, level_t level
, char *fmt
, ...)
74 /* write in memory buffer first */
75 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
76 syslog(LOG_DAEMON
|LOG_INFO
, "00[%s] %s", debug_names
->names
[group
],
83 * Run the daemon and handle unix signals
89 /* handle SIGINT and SIGTERM in this handler */
91 sigaddset(&set
, SIGINT
);
92 sigaddset(&set
, SIGTERM
);
93 sigprocmask(SIG_BLOCK
, &set
, NULL
);
100 error
= sigwait(&set
, &sig
);
103 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
110 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
111 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
116 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
117 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
122 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
130 * Handle SIGSEGV/SIGILL signals raised by threads
132 static void segv_handler(int signal
)
134 backtrace_t
*backtrace
;
136 DBG1(DBG_DMN
, "thread %u received %d", thread_current_id(), signal
);
137 backtrace
= backtrace_create(2);
138 backtrace
->log(backtrace
, stderr
, TRUE
);
139 backtrace
->destroy(backtrace
);
141 DBG1(DBG_DMN
, "killing ourself, received critical signal");
148 static bool lookup_uid_gid()
151 if (!charon
->caps
->resolve_uid(charon
->caps
, IPSEC_USER
))
157 if (!charon
->caps
->resolve_gid(charon
->caps
, IPSEC_GROUP
))
166 * Check/create PID file, return TRUE if already running
168 static bool check_pidfile()
172 if (stat(pidfile_name
, &stb
) == 0)
174 pidfile
= fopen(pidfile_name
, "r");
180 memset(buf
, 0, sizeof(buf
));
181 if (fread(buf
, 1, sizeof(buf
), pidfile
))
183 buf
[sizeof(buf
) - 1] = '\0';
187 if (pid
&& kill(pid
, 0) == 0)
188 { /* such a process is running */
192 DBG1(DBG_DMN
, "removing pidfile '%s', process not running", pidfile_name
);
193 unlink(pidfile_name
);
196 /* create new pidfile */
197 pidfile
= fopen(pidfile_name
, "w");
200 ignore_result(fchown(fileno(pidfile
),
201 charon
->caps
->get_uid(charon
->caps
),
202 charon
->caps
->get_gid(charon
->caps
)));
203 fprintf(pidfile
, "%d\n", getpid());
210 * Delete/truncate the PID file
212 static void unlink_pidfile()
214 /* because unlinking the PID file may fail, we truncate it to ensure the
215 * daemon can be properly restarted. one probable cause for this is the
216 * combination of not running as root and the effective user lacking
217 * permissions on the parent dir(s) of the PID file */
220 ignore_result(ftruncate(fileno(pidfile
), 0));
223 unlink(pidfile_name
);
226 * Main function, starts TKM backend.
228 int main(int argc
, char *argv
[])
231 if (argc
> 0 && strlen(argv
[0]) > 0)
233 dmn_name
= basename(argv
[0]);
237 dmn_name
= "charon-tkm";
240 struct sigaction action
;
241 int status
= SS_RC_INITIALIZATION_FAILED
;
243 /* logging for library during initialization, as we have no bus yet */
246 /* initialize library */
247 if (!library_init(NULL
))
253 if (!libhydra_init(dmn_name
))
255 dbg_syslog(DBG_DMN
, 1, "initialization failed - aborting %s", dmn_name
);
261 if (!libcharon_init(dmn_name
))
263 dbg_syslog(DBG_DMN
, 1, "initialization failed - aborting %s", dmn_name
);
267 if (!lookup_uid_gid())
269 dbg_syslog(DBG_DMN
, 1, "invalid uid/gid - aborting %s", dmn_name
);
273 /* make sure we log to the DAEMON facility by default */
274 lib
->settings
->set_int(lib
->settings
, "%s.syslog.daemon.default",
275 lib
->settings
->get_int(lib
->settings
, "%s.syslog.daemon.default", 1,
276 dmn_name
), dmn_name
);
277 charon
->load_loggers(charon
, NULL
, FALSE
);
279 DBG1(DBG_DMN
, "Starting charon with TKM backend (strongSwan "VERSION
")");
281 /* register TKM specific plugins */
282 static plugin_feature_t features
[] = {
283 PLUGIN_REGISTER(NONCE_GEN
, tkm_nonceg_create
),
284 PLUGIN_PROVIDE(NONCE_GEN
),
285 PLUGIN_REGISTER(DH
, tkm_diffie_hellman_create
),
286 PLUGIN_PROVIDE(DH
, MODP_3072_BIT
),
287 PLUGIN_PROVIDE(DH
, MODP_4096_BIT
),
288 PLUGIN_CALLBACK(kernel_ipsec_register
, tkm_kernel_ipsec_create
),
289 PLUGIN_PROVIDE(CUSTOM
, "kernel-ipsec"),
290 PLUGIN_DEPENDS(RNG
, RNG_WEAK
),
291 PLUGIN_CALLBACK(kernel_net_register
, kernel_netlink_net_create
),
292 PLUGIN_PROVIDE(CUSTOM
, "kernel-net"),
295 lib
->plugins
->add_static_features(lib
->plugins
, "tkm-backend", features
,
296 countof(features
), TRUE
);
298 /* register TKM keymat variant */
299 keymat_register_constructor(IKEV2
, (keymat_constructor_t
)tkm_keymat_create
);
301 /* initialize daemon */
302 if (!charon
->initialize(charon
, PLUGINS
))
304 DBG1(DBG_DMN
, "initialization failed - aborting %s", dmn_name
);
308 /* set global pidfile name depending on daemon name */
309 if (asprintf(&pidfile_name
, IPSEC_PIDDIR
"/%s.pid", dmn_name
) < 0)
311 DBG1(DBG_DMN
, "unable to set pidfile name - aborting %s", dmn_name
);
317 DBG1(DBG_DMN
, "%s already running (\"%s\" exists)", dmn_name
,
322 if (!charon
->caps
->drop(charon
->caps
))
324 DBG1(DBG_DMN
, "capability dropping failed - aborting %s", dmn_name
);
328 /* initialize TKM client */
331 DBG1(DBG_DMN
, "init of TKM client failed - aborting %s", dmn_name
);
335 /* register TKM authorization hook */
336 listener
= tkm_listener_create();
337 charon
->bus
->add_listener(charon
->bus
, &listener
->listener
);
339 /* add handler for SEGV and ILL,
340 * INT and TERM are handled by sigwait() in run() */
341 action
.sa_handler
= segv_handler
;
343 sigemptyset(&action
.sa_mask
);
344 sigaddset(&action
.sa_mask
, SIGINT
);
345 sigaddset(&action
.sa_mask
, SIGTERM
);
346 sigaction(SIGSEGV
, &action
, NULL
);
347 sigaction(SIGILL
, &action
, NULL
);
348 sigaction(SIGBUS
, &action
, NULL
);
349 action
.sa_handler
= SIG_IGN
;
350 sigaction(SIGPIPE
, &action
, NULL
);
352 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, NULL
);
354 /* start daemon (i.e. the threads in the thread-pool) */
355 charon
->start(charon
);
357 /* main thread goes to run loop */
362 charon
->bus
->remove_listener(charon
->bus
, &listener
->listener
);
363 listener
->destroy(listener
);