Don't manually register kernel_netlink_net
[strongswan.git] / src / charon-tkm / src / charon-tkm.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Reto Buerki
4 * Copyright (C) 2012 Adrian-Ken Rueegsegger
5 * Hochschule fuer Technik Rapperswil
6 *
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>.
11 *
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
15 * for more details.
16 */
17
18 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <syslog.h>
22 #include <signal.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <libgen.h>
27
28 #include <hydra.h>
29 #include <daemon.h>
30 #include <library.h>
31 #include <utils/backtrace.h>
32 #include <threading/thread.h>
33 #include <sa/keymat.h>
34 #include <credentials/credential_manager.h>
35
36 #include "tkm.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"
42 #include "tkm_public_key.h"
43 #include "tkm_cred.h"
44
45 /**
46 * TKM bus listener for IKE authorize events.
47 */
48 static tkm_listener_t *listener;
49
50 /**
51 * PID file, in which charon-tkm stores its process id
52 */
53 static char *pidfile_name = NULL;
54
55 /**
56 * Global reference to PID file (required to truncate, if undeletable)
57 */
58 static FILE *pidfile = NULL;
59
60 /**
61 * Hook in library for debugging messages
62 */
63 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
64
65 /**
66 * Simple logging hook for library logs, using syslog output
67 */
68 static void dbg_syslog(debug_t group, level_t level, char *fmt, ...)
69 {
70 if (level <= 1)
71 {
72 char buffer[8192];
73 va_list args;
74
75 va_start(args, fmt);
76 /* write in memory buffer first */
77 vsnprintf(buffer, sizeof(buffer), fmt, args);
78 syslog(LOG_DAEMON|LOG_INFO, "00[%s] %s", debug_names->names[group],
79 buffer);
80 va_end(args);
81 }
82 }
83
84 /**
85 * Run the daemon and handle unix signals
86 */
87 static void run()
88 {
89 sigset_t set;
90
91 /* handle SIGINT and SIGTERM in this handler */
92 sigemptyset(&set);
93 sigaddset(&set, SIGINT);
94 sigaddset(&set, SIGTERM);
95 sigprocmask(SIG_BLOCK, &set, NULL);
96
97 while (TRUE)
98 {
99 int sig;
100 int error;
101
102 error = sigwait(&set, &sig);
103 if (error)
104 {
105 DBG1(DBG_DMN, "error %d while waiting for a signal", error);
106 return;
107 }
108 switch (sig)
109 {
110 case SIGINT:
111 {
112 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
113 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
114 return;
115 }
116 case SIGTERM:
117 {
118 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
119 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
120 return;
121 }
122 default:
123 {
124 DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
125 break;
126 }
127 }
128 }
129 }
130
131 /**
132 * Handle SIGSEGV/SIGILL signals raised by threads
133 */
134 static void segv_handler(int signal)
135 {
136 backtrace_t *backtrace;
137
138 DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
139 backtrace = backtrace_create(2);
140 backtrace->log(backtrace, stderr, TRUE);
141 backtrace->destroy(backtrace);
142
143 DBG1(DBG_DMN, "killing ourself, received critical signal");
144 abort();
145 }
146
147 /**
148 * Lookup UID and GID
149 */
150 static bool lookup_uid_gid()
151 {
152 #ifdef IPSEC_USER
153 if (!charon->caps->resolve_uid(charon->caps, IPSEC_USER))
154 {
155 return FALSE;
156 }
157 #endif
158 #ifdef IPSEC_GROUP
159 if (!charon->caps->resolve_gid(charon->caps, IPSEC_GROUP))
160 {
161 return FALSE;
162 }
163 #endif
164 return TRUE;
165 }
166
167 /**
168 * Check/create PID file, return TRUE if already running
169 */
170 static bool check_pidfile()
171 {
172 struct stat stb;
173
174 if (stat(pidfile_name, &stb) == 0)
175 {
176 pidfile = fopen(pidfile_name, "r");
177 if (pidfile)
178 {
179 char buf[64];
180 pid_t pid = 0;
181
182 memset(buf, 0, sizeof(buf));
183 if (fread(buf, 1, sizeof(buf), pidfile))
184 {
185 buf[sizeof(buf) - 1] = '\0';
186 pid = atoi(buf);
187 }
188 fclose(pidfile);
189 if (pid && kill(pid, 0) == 0)
190 { /* such a process is running */
191 return TRUE;
192 }
193 }
194 DBG1(DBG_DMN, "removing pidfile '%s', process not running", pidfile_name);
195 unlink(pidfile_name);
196 }
197
198 /* create new pidfile */
199 pidfile = fopen(pidfile_name, "w");
200 if (pidfile)
201 {
202 ignore_result(fchown(fileno(pidfile),
203 charon->caps->get_uid(charon->caps),
204 charon->caps->get_gid(charon->caps)));
205 fprintf(pidfile, "%d\n", getpid());
206 fflush(pidfile);
207 }
208 return FALSE;
209 }
210
211 /**
212 * Delete/truncate the PID file
213 */
214 static void unlink_pidfile()
215 {
216 /* because unlinking the PID file may fail, we truncate it to ensure the
217 * daemon can be properly restarted. one probable cause for this is the
218 * combination of not running as root and the effective user lacking
219 * permissions on the parent dir(s) of the PID file */
220 if (pidfile)
221 {
222 ignore_result(ftruncate(fileno(pidfile), 0));
223 fclose(pidfile);
224 }
225 unlink(pidfile_name);
226 }
227 /**
228 * Main function, starts TKM backend.
229 */
230 int main(int argc, char *argv[])
231 {
232 char *dmn_name;
233 if (argc > 0 && strlen(argv[0]) > 0)
234 {
235 dmn_name = basename(argv[0]);
236 }
237 else
238 {
239 dmn_name = "charon-tkm";
240 }
241
242 /* TKM credential set */
243 tkm_cred_t *creds;
244
245 struct sigaction action;
246 int status = SS_RC_INITIALIZATION_FAILED;
247
248 /* logging for library during initialization, as we have no bus yet */
249 dbg = dbg_syslog;
250
251 /* initialize library */
252 if (!library_init(NULL))
253 {
254 library_deinit();
255 exit(status);
256 }
257
258 if (!libhydra_init(dmn_name))
259 {
260 dbg_syslog(DBG_DMN, 1, "initialization failed - aborting %s", dmn_name);
261 libhydra_deinit();
262 library_deinit();
263 exit(status);
264 }
265
266 if (!libcharon_init(dmn_name))
267 {
268 dbg_syslog(DBG_DMN, 1, "initialization failed - aborting %s", dmn_name);
269 goto deinit;
270 }
271
272 if (!lookup_uid_gid())
273 {
274 dbg_syslog(DBG_DMN, 1, "invalid uid/gid - aborting %s", dmn_name);
275 goto deinit;
276 }
277
278 /* make sure we log to the DAEMON facility by default */
279 lib->settings->set_int(lib->settings, "%s.syslog.daemon.default",
280 lib->settings->get_int(lib->settings, "%s.syslog.daemon.default", 1,
281 dmn_name), dmn_name);
282 charon->load_loggers(charon, NULL, FALSE);
283
284 DBG1(DBG_DMN, "Starting charon with TKM backend (strongSwan "VERSION")");
285
286 /* register TKM specific plugins */
287 static plugin_feature_t features[] = {
288 PLUGIN_REGISTER(NONCE_GEN, tkm_nonceg_create),
289 PLUGIN_PROVIDE(NONCE_GEN),
290 PLUGIN_REGISTER(DH, tkm_diffie_hellman_create),
291 PLUGIN_PROVIDE(DH, MODP_3072_BIT),
292 PLUGIN_PROVIDE(DH, MODP_4096_BIT),
293 PLUGIN_REGISTER(PUBKEY, tkm_public_key_load, TRUE),
294 PLUGIN_PROVIDE(PUBKEY, KEY_RSA),
295 PLUGIN_PROVIDE(PUBKEY_VERIFY, SIGN_RSA_EMSA_PKCS1_SHA1),
296 PLUGIN_PROVIDE(PUBKEY_VERIFY, SIGN_RSA_EMSA_PKCS1_SHA256),
297 PLUGIN_CALLBACK(kernel_ipsec_register, tkm_kernel_ipsec_create),
298 PLUGIN_PROVIDE(CUSTOM, "kernel-ipsec"),
299 };
300 lib->plugins->add_static_features(lib->plugins, "tkm-backend", features,
301 countof(features), TRUE);
302
303 /* register TKM keymat variant */
304 keymat_register_constructor(IKEV2, (keymat_constructor_t)tkm_keymat_create);
305
306 /* initialize daemon */
307 if (!charon->initialize(charon, PLUGINS))
308 {
309 DBG1(DBG_DMN, "initialization failed - aborting %s", dmn_name);
310 goto deinit;
311 }
312
313 /* set global pidfile name depending on daemon name */
314 if (asprintf(&pidfile_name, IPSEC_PIDDIR"/%s.pid", dmn_name) < 0)
315 {
316 DBG1(DBG_DMN, "unable to set pidfile name - aborting %s", dmn_name);
317 goto deinit;
318 };
319
320 if (check_pidfile())
321 {
322 DBG1(DBG_DMN, "%s already running (\"%s\" exists)", dmn_name,
323 pidfile_name);
324 goto deinit;
325 }
326
327 if (!charon->caps->drop(charon->caps))
328 {
329 DBG1(DBG_DMN, "capability dropping failed - aborting %s", dmn_name);
330 goto deinit;
331 }
332
333 /* initialize TKM client */
334 if (!tkm_init())
335 {
336 DBG1(DBG_DMN, "init of TKM client failed - aborting %s", dmn_name);
337 goto deinit;
338 }
339
340 /* register TKM authorization hook */
341 listener = tkm_listener_create();
342 charon->bus->add_listener(charon->bus, &listener->listener);
343
344 /* register TKM credential set */
345 creds = tkm_cred_create();
346 lib->credmgr->add_set(lib->credmgr, (credential_set_t*)creds);
347
348 /* add handler for SEGV and ILL,
349 * INT and TERM are handled by sigwait() in run() */
350 action.sa_handler = segv_handler;
351 action.sa_flags = 0;
352 sigemptyset(&action.sa_mask);
353 sigaddset(&action.sa_mask, SIGINT);
354 sigaddset(&action.sa_mask, SIGTERM);
355 sigaction(SIGSEGV, &action, NULL);
356 sigaction(SIGILL, &action, NULL);
357 sigaction(SIGBUS, &action, NULL);
358 action.sa_handler = SIG_IGN;
359 sigaction(SIGPIPE, &action, NULL);
360
361 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
362
363 /* start daemon (i.e. the threads in the thread-pool) */
364 charon->start(charon);
365
366 /* main thread goes to run loop */
367 run();
368
369 unlink_pidfile();
370 status = 0;
371 charon->bus->remove_listener(charon->bus, &listener->listener);
372 listener->destroy(listener);
373 creds->destroy(creds);
374
375 deinit:
376 libcharon_deinit();
377 libhydra_deinit();
378 library_deinit();
379 tkm_deinit();
380 return status;
381 }