Merge branch 'settings-parser'
[strongswan.git] / src / charon-cmd / charon-cmd.c
1 /*
2 * Copyright (C) 2006-2013 Tobias Brunner
3 * Copyright (C) 2005-2013 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include <stdio.h>
20 #define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
21 #include <signal.h>
22 #undef _POSIX_PTHREAD_SEMANTICS
23 #include <pthread.h>
24 #include <sys/types.h>
25 #include <sys/utsname.h>
26 #include <unistd.h>
27 #include <getopt.h>
28
29 #include <library.h>
30 #include <hydra.h>
31 #include <daemon.h>
32 #include <utils/backtrace.h>
33 #include <threading/thread.h>
34
35 #include "cmd/cmd_options.h"
36 #include "cmd/cmd_connection.h"
37 #include "cmd/cmd_creds.h"
38
39 /**
40 * Default loglevel
41 */
42 static level_t default_loglevel = LEVEL_CTRL;
43
44 /**
45 * Loglevel configuration
46 */
47 static level_t levels[DBG_MAX];
48
49 /**
50 * Connection to initiate
51 */
52 static cmd_connection_t *conn;
53
54 /**
55 * Credential backend
56 */
57 static cmd_creds_t *creds;
58
59 /**
60 * hook in library for debugging messages
61 */
62 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
63
64 /**
65 * Logging hook for library logs, using stderr output
66 */
67 static void dbg_stderr(debug_t group, level_t level, char *fmt, ...)
68 {
69 va_list args;
70
71 if (level <= default_loglevel)
72 {
73 va_start(args, fmt);
74 fprintf(stderr, "00[%N] ", debug_names, group);
75 vfprintf(stderr, fmt, args);
76 fprintf(stderr, "\n");
77 va_end(args);
78 }
79 }
80
81 /**
82 * Clean up connection definition atexit()
83 */
84 static void cleanup_conn()
85 {
86 DESTROY_IF(conn);
87 }
88
89 /**
90 * Clean up credentials atexit()
91 */
92 static void cleanup_creds()
93 {
94 DESTROY_IF(creds);
95 }
96
97 /**
98 * Run the daemon and handle unix signals
99 */
100 static int run()
101 {
102 sigset_t set;
103
104 /* handle SIGINT, SIGHUP and SIGTERM in this handler */
105 sigemptyset(&set);
106 sigaddset(&set, SIGINT);
107 sigaddset(&set, SIGHUP);
108 sigaddset(&set, SIGTERM);
109 sigaddset(&set, SIGUSR1);
110 sigprocmask(SIG_BLOCK, &set, NULL);
111
112 while (TRUE)
113 {
114 int sig;
115 int error;
116
117 error = sigwait(&set, &sig);
118 if (error)
119 {
120 DBG1(DBG_DMN, "error %d while waiting for a signal", error);
121 return 1;
122 }
123 switch (sig)
124 {
125 case SIGHUP:
126 {
127 DBG1(DBG_DMN, "signal of type SIGHUP received. Reloading "
128 "configuration");
129 #ifdef STRONGSWAN_CONF
130 if (lib->settings->load_files(lib->settings, STRONGSWAN_CONF,
131 FALSE))
132 {
133 charon->load_loggers(charon, levels, TRUE);
134 lib->plugins->reload(lib->plugins, NULL);
135 }
136 else
137 #endif
138 {
139 DBG1(DBG_DMN, "reloading config failed, keeping old");
140 }
141 break;
142 }
143 case SIGINT:
144 {
145 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
146 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
147 return 0;
148 }
149 case SIGTERM:
150 {
151 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
152 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
153 return 0;
154 }
155 case SIGUSR1:
156 { /* an error occurred */
157 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
158 return 1;
159 }
160 default:
161 {
162 DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
163 break;
164 }
165 }
166 }
167 }
168
169 /**
170 * lookup UID and GID
171 */
172 static bool lookup_uid_gid()
173 {
174 #ifdef IPSEC_USER
175 if (!lib->caps->resolve_uid(lib->caps, IPSEC_USER))
176 {
177 return FALSE;
178 }
179 #endif
180 #ifdef IPSEC_GROUP
181 if (!lib->caps->resolve_gid(lib->caps, IPSEC_GROUP))
182 {
183 return FALSE;
184 }
185 #endif
186 return TRUE;
187 }
188
189 /**
190 * Handle SIGSEGV/SIGILL signals raised by threads
191 */
192 static void segv_handler(int signal)
193 {
194 backtrace_t *backtrace;
195
196 DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
197 backtrace = backtrace_create(2);
198 backtrace->log(backtrace, stderr, TRUE);
199 backtrace->destroy(backtrace);
200
201 DBG1(DBG_DMN, "killing ourself, received critical signal");
202 abort();
203 }
204
205 /**
206 * Print command line usage and exit
207 */
208 static void usage(FILE *out, char *msg, char *binary)
209 {
210 static const int padto = 18;
211 char cmd[64], *pre, *post;
212 int i, line, pad;
213
214 if (msg)
215 {
216 fprintf(out, "%s\n", msg);
217 }
218 fprintf(out, "Usage: %s\n", binary);
219 for (i = 0; i < CMD_OPT_COUNT; i++)
220 {
221 switch (cmd_options[i].has_arg)
222 {
223 case required_argument:
224 pre = " <";
225 post = ">";
226 break;
227 case optional_argument:
228 pre = "[=";
229 post = "]";
230 break;
231 case no_argument:
232 default:
233 pre = " ";
234 post = " ";
235 break;
236 }
237 snprintf(cmd, sizeof(cmd), " --%s%s%s%s", cmd_options[i].name,
238 pre, cmd_options[i].arg, post);
239 pad = padto - strlen(cmd);
240 if (pad >= 1)
241 {
242 fprintf(out, "%s%-*s%s\n", cmd, pad, "", cmd_options[i].desc);
243 }
244 else
245 { /* write description to a separate line */
246 fprintf(out, "%s\n%-*s%s\n", cmd, padto, "", cmd_options[i].desc);
247 }
248 for (line = 0; line < countof(cmd_options[i].lines); line++)
249 {
250 if (cmd_options[i].lines[line])
251 {
252 fprintf(out, "%-*s%s\n", padto, "", cmd_options[i].lines[line]);
253 }
254 }
255 }
256 }
257
258 /**
259 * Handle command line options, if simple is TRUE only arguments like --help
260 * and --version are handled.
261 */
262 static void handle_arguments(int argc, char *argv[], bool simple)
263 {
264 struct option long_opts[CMD_OPT_COUNT + 1] = {};
265 int i, opt;
266
267 for (i = 0; i < CMD_OPT_COUNT; i++)
268 {
269 long_opts[i].name = cmd_options[i].name;
270 long_opts[i].val = cmd_options[i].id;
271 long_opts[i].has_arg = cmd_options[i].has_arg;
272 }
273 /* reset option parser */
274 optind = 1;
275 while (TRUE)
276 {
277 bool handled = FALSE;
278
279 opt = getopt_long(argc, argv, "", long_opts, NULL);
280 switch (opt)
281 {
282 case EOF:
283 break;
284 case CMD_OPT_HELP:
285 usage(stdout, NULL, argv[0]);
286 exit(0);
287 case CMD_OPT_VERSION:
288 printf("%s, strongSwan %s\n", "charon-cmd", VERSION);
289 exit(0);
290 case CMD_OPT_DEBUG:
291 default_loglevel = atoi(optarg);
292 continue;
293 default:
294 if (simple)
295 {
296 continue;
297 }
298 handled |= conn->handle(conn, opt, optarg);
299 handled |= creds->handle(creds, opt, optarg);
300 if (handled)
301 {
302 continue;
303 }
304 /* fall-through */
305 case '?':
306 /* missing argument, unrecognized option */
307 usage(stderr, NULL, argv[0]);
308 exit(1);
309 }
310 break;
311 }
312 }
313
314 /**
315 * Main function, starts the daemon.
316 */
317 int main(int argc, char *argv[])
318 {
319 struct sigaction action;
320 struct utsname utsname;
321 int group;
322
323 /* handle simple arguments */
324 handle_arguments(argc, argv, TRUE);
325
326 dbg = dbg_stderr;
327 atexit(library_deinit);
328 if (!library_init(NULL, "charon-cmd"))
329 {
330 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
331 }
332 if (lib->integrity)
333 {
334 if (!lib->integrity->check_file(lib->integrity, "charon-cmd", argv[0]))
335 {
336 exit(SS_RC_DAEMON_INTEGRITY);
337 }
338 }
339 atexit(libhydra_deinit);
340 if (!libhydra_init())
341 {
342 exit(SS_RC_INITIALIZATION_FAILED);
343 }
344 atexit(libcharon_deinit);
345 if (!libcharon_init())
346 {
347 exit(SS_RC_INITIALIZATION_FAILED);
348 }
349 for (group = 0; group < DBG_MAX; group++)
350 {
351 levels[group] = default_loglevel;
352 }
353 charon->load_loggers(charon, levels, TRUE);
354
355 if (!lookup_uid_gid())
356 {
357 exit(SS_RC_INITIALIZATION_FAILED);
358 }
359 lib->settings->set_default_str(lib->settings, "charon-cmd.port", "0");
360 lib->settings->set_default_str(lib->settings, "charon-cmd.port_nat_t", "0");
361 if (!charon->initialize(charon,
362 lib->settings->get_str(lib->settings, "charon-cmd.load", PLUGINS)))
363 {
364 exit(SS_RC_INITIALIZATION_FAILED);
365 }
366 if (!lib->caps->drop(lib->caps))
367 {
368 exit(SS_RC_INITIALIZATION_FAILED);
369 }
370
371 conn = cmd_connection_create();
372 atexit(cleanup_conn);
373 creds = cmd_creds_create();
374 atexit(cleanup_creds);
375
376 /* handle all arguments */
377 handle_arguments(argc, argv, FALSE);
378
379 if (uname(&utsname) != 0)
380 {
381 memset(&utsname, 0, sizeof(utsname));
382 }
383 DBG1(DBG_DMN, "Starting charon-cmd IKE client (strongSwan %s, %s %s, %s)",
384 VERSION, utsname.sysname, utsname.release, utsname.machine);
385 lib->plugins->status(lib->plugins, LEVEL_CTRL);
386
387 /* add handler for SEGV and ILL,
388 * INT, TERM and HUP are handled by sigwait() in run() */
389 action.sa_handler = segv_handler;
390 action.sa_flags = 0;
391 sigemptyset(&action.sa_mask);
392 sigaddset(&action.sa_mask, SIGINT);
393 sigaddset(&action.sa_mask, SIGTERM);
394 sigaddset(&action.sa_mask, SIGHUP);
395 sigaddset(&action.sa_mask, SIGUSR1);
396 sigaction(SIGSEGV, &action, NULL);
397 sigaction(SIGILL, &action, NULL);
398 sigaction(SIGBUS, &action, NULL);
399 action.sa_handler = SIG_IGN;
400 sigaction(SIGPIPE, &action, NULL);
401
402 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
403
404 /* start daemon with thread-pool */
405 charon->start(charon);
406 /* wait for signal */
407 return run();
408 }