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