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