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>
35 #include <credentials/sets/mem_cred.h>
38 #include "tkm_nonceg.h"
39 #include "tkm_diffie_hellman.h"
40 #include "tkm_keymat.h"
41 #include "tkm_listener.h"
42 #include "tkm_kernel_ipsec.h"
43 #include "tkm_public_key.h"
44 #include "tkm_private_key.h"
47 * TKM bus listener for IKE authorize events.
49 static tkm_listener_t
*listener
;
52 * PID file, in which charon-tkm stores its process id
54 static char *pidfile_name
= NULL
;
57 * Global reference to PID file (required to truncate, if undeletable)
59 static FILE *pidfile
= NULL
;
62 * Hook in library for debugging messages
64 extern void (*dbg
) (debug_t group
, level_t level
, char *fmt
, ...);
67 * Simple logging hook for library logs, using syslog output
69 static void dbg_syslog(debug_t group
, level_t level
, char *fmt
, ...)
77 /* write in memory buffer first */
78 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
79 syslog(LOG_DAEMON
|LOG_INFO
, "00[%s] %s", debug_names
->names
[group
],
86 * Run the daemon and handle unix signals
92 /* handle SIGINT and SIGTERM in this handler */
94 sigaddset(&set
, SIGINT
);
95 sigaddset(&set
, SIGTERM
);
96 sigprocmask(SIG_BLOCK
, &set
, NULL
);
103 error
= sigwait(&set
, &sig
);
106 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
113 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
114 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
119 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
120 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
125 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
133 * Handle SIGSEGV/SIGILL signals raised by threads
135 static void segv_handler(int signal
)
137 backtrace_t
*backtrace
;
139 DBG1(DBG_DMN
, "thread %u received %d", thread_current_id(), signal
);
140 backtrace
= backtrace_create(2);
141 backtrace
->log(backtrace
, stderr
, TRUE
);
142 backtrace
->destroy(backtrace
);
144 DBG1(DBG_DMN
, "killing ourself, received critical signal");
151 static bool lookup_uid_gid()
154 if (!charon
->caps
->resolve_uid(charon
->caps
, IPSEC_USER
))
160 if (!charon
->caps
->resolve_gid(charon
->caps
, IPSEC_GROUP
))
169 * Check/create PID file, return TRUE if already running
171 static bool check_pidfile()
175 if (stat(pidfile_name
, &stb
) == 0)
177 pidfile
= fopen(pidfile_name
, "r");
183 memset(buf
, 0, sizeof(buf
));
184 if (fread(buf
, 1, sizeof(buf
), pidfile
))
186 buf
[sizeof(buf
) - 1] = '\0';
190 if (pid
&& kill(pid
, 0) == 0)
191 { /* such a process is running */
195 DBG1(DBG_DMN
, "removing pidfile '%s', process not running", pidfile_name
);
196 unlink(pidfile_name
);
199 /* create new pidfile */
200 pidfile
= fopen(pidfile_name
, "w");
203 ignore_result(fchown(fileno(pidfile
),
204 charon
->caps
->get_uid(charon
->caps
),
205 charon
->caps
->get_gid(charon
->caps
)));
206 fprintf(pidfile
, "%d\n", getpid());
213 * Delete/truncate the PID file
215 static void unlink_pidfile()
217 /* because unlinking the PID file may fail, we truncate it to ensure the
218 * daemon can be properly restarted. one probable cause for this is the
219 * combination of not running as root and the effective user lacking
220 * permissions on the parent dir(s) of the PID file */
223 ignore_result(ftruncate(fileno(pidfile
), 0));
226 unlink(pidfile_name
);
229 * Main function, starts TKM backend.
231 int main(int argc
, char *argv
[])
234 if (argc
> 0 && strlen(argv
[0]) > 0)
236 dmn_name
= basename(argv
[0]);
240 dmn_name
= "charon-tkm";
243 /* credential set and TKM private key */
245 tkm_private_key_t
*key
;
247 struct sigaction action
;
248 int status
= SS_RC_INITIALIZATION_FAILED
;
250 /* logging for library during initialization, as we have no bus yet */
253 /* initialize library */
254 if (!library_init(NULL
))
260 if (!libhydra_init(dmn_name
))
262 dbg_syslog(DBG_DMN
, 1, "initialization failed - aborting %s", dmn_name
);
268 if (!libcharon_init(dmn_name
))
270 dbg_syslog(DBG_DMN
, 1, "initialization failed - aborting %s", dmn_name
);
274 if (!lookup_uid_gid())
276 dbg_syslog(DBG_DMN
, 1, "invalid uid/gid - aborting %s", dmn_name
);
280 /* make sure we log to the DAEMON facility by default */
281 lib
->settings
->set_int(lib
->settings
, "%s.syslog.daemon.default",
282 lib
->settings
->get_int(lib
->settings
, "%s.syslog.daemon.default", 1,
283 dmn_name
), dmn_name
);
284 charon
->load_loggers(charon
, NULL
, FALSE
);
286 DBG1(DBG_DMN
, "Starting charon with TKM backend (strongSwan "VERSION
")");
288 /* register TKM specific plugins */
289 static plugin_feature_t features
[] = {
290 PLUGIN_REGISTER(NONCE_GEN
, tkm_nonceg_create
),
291 PLUGIN_PROVIDE(NONCE_GEN
),
292 PLUGIN_REGISTER(DH
, tkm_diffie_hellman_create
),
293 PLUGIN_PROVIDE(DH
, MODP_3072_BIT
),
294 PLUGIN_PROVIDE(DH
, MODP_4096_BIT
),
295 PLUGIN_REGISTER(PUBKEY
, tkm_public_key_load
, TRUE
),
296 PLUGIN_PROVIDE(PUBKEY
, KEY_RSA
),
297 PLUGIN_PROVIDE(PUBKEY_VERIFY
, SIGN_RSA_EMSA_PKCS1_SHA1
),
298 PLUGIN_PROVIDE(PUBKEY_VERIFY
, SIGN_RSA_EMSA_PKCS1_SHA256
),
299 PLUGIN_CALLBACK(kernel_ipsec_register
, tkm_kernel_ipsec_create
),
300 PLUGIN_PROVIDE(CUSTOM
, "kernel-ipsec"),
301 PLUGIN_DEPENDS(RNG
, RNG_WEAK
),
302 PLUGIN_CALLBACK(kernel_net_register
, kernel_netlink_net_create
),
303 PLUGIN_PROVIDE(CUSTOM
, "kernel-net"),
306 lib
->plugins
->add_static_features(lib
->plugins
, "tkm-backend", features
,
307 countof(features
), TRUE
);
309 /* register TKM keymat variant */
310 keymat_register_constructor(IKEV2
, (keymat_constructor_t
)tkm_keymat_create
);
312 /* initialize daemon */
313 if (!charon
->initialize(charon
, PLUGINS
))
315 DBG1(DBG_DMN
, "initialization failed - aborting %s", dmn_name
);
319 /* set global pidfile name depending on daemon name */
320 if (asprintf(&pidfile_name
, IPSEC_PIDDIR
"/%s.pid", dmn_name
) < 0)
322 DBG1(DBG_DMN
, "unable to set pidfile name - aborting %s", dmn_name
);
328 DBG1(DBG_DMN
, "%s already running (\"%s\" exists)", dmn_name
,
333 if (!charon
->caps
->drop(charon
->caps
))
335 DBG1(DBG_DMN
, "capability dropping failed - aborting %s", dmn_name
);
339 /* initialize TKM client */
342 DBG1(DBG_DMN
, "init of TKM client failed - aborting %s", dmn_name
);
346 /* register TKM authorization hook */
347 listener
= tkm_listener_create();
348 charon
->bus
->add_listener(charon
->bus
, &listener
->listener
);
350 /* register TKM private key */
351 creds
= mem_cred_create();
352 key
= tkm_private_key_init();
353 creds
->add_key(creds
, (private_key_t
*)key
);
354 lib
->credmgr
->add_set(lib
->credmgr
, (credential_set_t
*)creds
);
356 /* add handler for SEGV and ILL,
357 * INT and TERM are handled by sigwait() in run() */
358 action
.sa_handler
= segv_handler
;
360 sigemptyset(&action
.sa_mask
);
361 sigaddset(&action
.sa_mask
, SIGINT
);
362 sigaddset(&action
.sa_mask
, SIGTERM
);
363 sigaction(SIGSEGV
, &action
, NULL
);
364 sigaction(SIGILL
, &action
, NULL
);
365 sigaction(SIGBUS
, &action
, NULL
);
366 action
.sa_handler
= SIG_IGN
;
367 sigaction(SIGPIPE
, &action
, NULL
);
369 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, NULL
);
371 /* start daemon (i.e. the threads in the thread-pool) */
372 charon
->start(charon
);
374 /* main thread goes to run loop */
379 charon
->bus
->remove_listener(charon
->bus
, &listener
->listener
);
380 listener
->destroy(listener
);
381 creds
->destroy(creds
);