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