lib: Add global config namespace
[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 if (lib->settings->load_files(lib->settings, NULL, FALSE))
130 {
131 charon->load_loggers(charon, levels, TRUE);
132 lib->plugins->reload(lib->plugins, NULL);
133 }
134 else
135 {
136 DBG1(DBG_DMN, "reloading config failed, keeping old");
137 }
138 break;
139 }
140 case SIGINT:
141 {
142 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
143 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
144 return 0;
145 }
146 case SIGTERM:
147 {
148 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
149 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
150 return 0;
151 }
152 case SIGUSR1:
153 { /* an error occurred */
154 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
155 return 1;
156 }
157 default:
158 {
159 DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
160 break;
161 }
162 }
163 }
164 }
165
166 /**
167 * lookup UID and GID
168 */
169 static bool lookup_uid_gid()
170 {
171 #ifdef IPSEC_USER
172 if (!lib->caps->resolve_uid(lib->caps, IPSEC_USER))
173 {
174 return FALSE;
175 }
176 #endif
177 #ifdef IPSEC_GROUP
178 if (!lib->caps->resolve_gid(lib->caps, IPSEC_GROUP))
179 {
180 return FALSE;
181 }
182 #endif
183 return TRUE;
184 }
185
186 /**
187 * Handle SIGSEGV/SIGILL signals raised by threads
188 */
189 static void segv_handler(int signal)
190 {
191 backtrace_t *backtrace;
192
193 DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
194 backtrace = backtrace_create(2);
195 backtrace->log(backtrace, stderr, TRUE);
196 backtrace->destroy(backtrace);
197
198 DBG1(DBG_DMN, "killing ourself, received critical signal");
199 abort();
200 }
201
202 /**
203 * Print command line usage and exit
204 */
205 static void usage(FILE *out, char *msg, char *binary)
206 {
207 static const int padto = 18;
208 char cmd[64], *pre, *post;
209 int i, line, pad;
210
211 if (msg)
212 {
213 fprintf(out, "%s\n", msg);
214 }
215 fprintf(out, "Usage: %s\n", binary);
216 for (i = 0; i < CMD_OPT_COUNT; i++)
217 {
218 switch (cmd_options[i].has_arg)
219 {
220 case required_argument:
221 pre = " <";
222 post = ">";
223 break;
224 case optional_argument:
225 pre = "[=";
226 post = "]";
227 break;
228 case no_argument:
229 default:
230 pre = " ";
231 post = " ";
232 break;
233 }
234 snprintf(cmd, sizeof(cmd), " --%s%s%s%s", cmd_options[i].name,
235 pre, cmd_options[i].arg, post);
236 pad = padto - strlen(cmd);
237 if (pad >= 1)
238 {
239 fprintf(out, "%s%-*s%s\n", cmd, pad, "", cmd_options[i].desc);
240 }
241 else
242 { /* write description to a separate line */
243 fprintf(out, "%s\n%-*s%s\n", cmd, padto, "", cmd_options[i].desc);
244 }
245 for (line = 0; line < countof(cmd_options[i].lines); line++)
246 {
247 if (cmd_options[i].lines[line])
248 {
249 fprintf(out, "%-*s%s\n", padto, "", cmd_options[i].lines[line]);
250 }
251 }
252 }
253 }
254
255 /**
256 * Handle command line options, if simple is TRUE only arguments like --help
257 * and --version are handled.
258 */
259 static void handle_arguments(int argc, char *argv[], bool simple)
260 {
261 struct option long_opts[CMD_OPT_COUNT + 1] = {};
262 int i, opt;
263
264 for (i = 0; i < CMD_OPT_COUNT; i++)
265 {
266 long_opts[i].name = cmd_options[i].name;
267 long_opts[i].val = cmd_options[i].id;
268 long_opts[i].has_arg = cmd_options[i].has_arg;
269 }
270 /* reset option parser */
271 optind = 1;
272 while (TRUE)
273 {
274 bool handled = FALSE;
275
276 opt = getopt_long(argc, argv, "", long_opts, NULL);
277 switch (opt)
278 {
279 case EOF:
280 break;
281 case CMD_OPT_HELP:
282 usage(stdout, NULL, argv[0]);
283 exit(0);
284 case CMD_OPT_VERSION:
285 printf("%s, strongSwan %s\n", "charon-cmd", VERSION);
286 exit(0);
287 case CMD_OPT_DEBUG:
288 default_loglevel = atoi(optarg);
289 continue;
290 default:
291 if (simple)
292 {
293 continue;
294 }
295 handled |= conn->handle(conn, opt, optarg);
296 handled |= creds->handle(creds, opt, optarg);
297 if (handled)
298 {
299 continue;
300 }
301 /* fall-through */
302 case '?':
303 /* missing argument, unrecognized option */
304 usage(stderr, NULL, argv[0]);
305 exit(1);
306 }
307 break;
308 }
309 }
310
311 /**
312 * Main function, starts the daemon.
313 */
314 int main(int argc, char *argv[])
315 {
316 struct sigaction action;
317 struct utsname utsname;
318 int group;
319
320 /* handle simple arguments */
321 handle_arguments(argc, argv, TRUE);
322
323 dbg = dbg_stderr;
324 atexit(library_deinit);
325 if (!library_init(NULL, "charon-cmd"))
326 {
327 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
328 }
329 if (lib->integrity)
330 {
331 if (!lib->integrity->check_file(lib->integrity, "charon-cmd", argv[0]))
332 {
333 exit(SS_RC_DAEMON_INTEGRITY);
334 }
335 }
336 atexit(libhydra_deinit);
337 if (!libhydra_init("charon-cmd"))
338 {
339 exit(SS_RC_INITIALIZATION_FAILED);
340 }
341 atexit(libcharon_deinit);
342 if (!libcharon_init("charon-cmd"))
343 {
344 exit(SS_RC_INITIALIZATION_FAILED);
345 }
346 for (group = 0; group < DBG_MAX; group++)
347 {
348 levels[group] = default_loglevel;
349 }
350 charon->load_loggers(charon, levels, TRUE);
351
352 if (!lookup_uid_gid())
353 {
354 exit(SS_RC_INITIALIZATION_FAILED);
355 }
356 lib->settings->set_default_str(lib->settings, "charon-cmd.port", "0");
357 lib->settings->set_default_str(lib->settings, "charon-cmd.port_nat_t", "0");
358 if (!charon->initialize(charon,
359 lib->settings->get_str(lib->settings, "charon-cmd.load", PLUGINS)))
360 {
361 exit(SS_RC_INITIALIZATION_FAILED);
362 }
363 if (!lib->caps->drop(lib->caps))
364 {
365 exit(SS_RC_INITIALIZATION_FAILED);
366 }
367
368 conn = cmd_connection_create();
369 atexit(cleanup_conn);
370 creds = cmd_creds_create();
371 atexit(cleanup_creds);
372
373 /* handle all arguments */
374 handle_arguments(argc, argv, FALSE);
375
376 if (uname(&utsname) != 0)
377 {
378 memset(&utsname, 0, sizeof(utsname));
379 }
380 DBG1(DBG_DMN, "Starting charon-cmd IKE client (strongSwan %s, %s %s, %s)",
381 VERSION, utsname.sysname, utsname.release, utsname.machine);
382 lib->plugins->status(lib->plugins, LEVEL_CTRL);
383
384 /* add handler for SEGV and ILL,
385 * INT, TERM and HUP are handled by sigwait() in run() */
386 action.sa_handler = segv_handler;
387 action.sa_flags = 0;
388 sigemptyset(&action.sa_mask);
389 sigaddset(&action.sa_mask, SIGINT);
390 sigaddset(&action.sa_mask, SIGTERM);
391 sigaddset(&action.sa_mask, SIGHUP);
392 sigaddset(&action.sa_mask, SIGUSR1);
393 sigaction(SIGSEGV, &action, NULL);
394 sigaction(SIGILL, &action, NULL);
395 sigaction(SIGBUS, &action, NULL);
396 action.sa_handler = SIG_IGN;
397 sigaction(SIGPIPE, &action, NULL);
398
399 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
400
401 /* start daemon with thread-pool */
402 charon->start(charon);
403 /* wait for signal */
404 return run();
405 }