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