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