charon-cmd: authenticate with EAP if no private key is given
[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 bool handled = FALSE;
258
259 opt = getopt_long(argc, argv, "", long_opts, NULL);
260 switch (opt)
261 {
262 case EOF:
263 break;
264 case CMD_OPT_HELP:
265 usage(stdout, NULL, argv[0]);
266 exit(0);
267 case CMD_OPT_VERSION:
268 printf("%s, strongSwan %s\n", "charon-cmd", VERSION);
269 exit(0);
270 default:
271 handled |= conn->handle(conn, opt, optarg);
272 handled |= creds->handle(creds, opt, optarg);
273 if (handled)
274 {
275 continue;
276 }
277 usage(stderr, NULL, argv[0]);
278 exit(1);
279 }
280 break;
281 }
282 }
283
284 /**
285 * Main function, starts the daemon.
286 */
287 int main(int argc, char *argv[])
288 {
289 struct sigaction action;
290 struct utsname utsname;
291 int group;
292
293 dbg = dbg_stderr;
294 atexit(library_deinit);
295 if (!library_init(NULL))
296 {
297 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
298 }
299 if (lib->integrity)
300 {
301 if (!lib->integrity->check_file(lib->integrity, "charon-cmd", argv[0]))
302 {
303 exit(SS_RC_DAEMON_INTEGRITY);
304 }
305 }
306 atexit(libhydra_deinit);
307 if (!libhydra_init("charon-cmd"))
308 {
309 exit(SS_RC_INITIALIZATION_FAILED);
310 }
311 atexit(libcharon_deinit);
312 if (!libcharon_init("charon-cmd"))
313 {
314 exit(SS_RC_INITIALIZATION_FAILED);
315 }
316 for (group = 0; group < DBG_MAX; group++)
317 {
318 levels[group] = LEVEL_CTRL;
319 }
320 charon->load_loggers(charon, levels, TRUE);
321
322 if (!lookup_uid_gid())
323 {
324 exit(SS_RC_INITIALIZATION_FAILED);
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 conn = cmd_connection_create();
337 atexit(cleanup_conn);
338 creds = cmd_creds_create();
339 atexit(cleanup_creds);
340
341 handle_arguments(argc, argv);
342
343 if (uname(&utsname) != 0)
344 {
345 memset(&utsname, 0, sizeof(utsname));
346 }
347 DBG1(DBG_DMN, "Starting charon-cmd IKE client (strongSwan %s, %s %s, %s)",
348 VERSION, utsname.sysname, utsname.release, utsname.machine);
349
350 /* add handler for SEGV and ILL,
351 * INT, TERM and HUP are handled by sigwait() in run() */
352 action.sa_handler = segv_handler;
353 action.sa_flags = 0;
354 sigemptyset(&action.sa_mask);
355 sigaddset(&action.sa_mask, SIGINT);
356 sigaddset(&action.sa_mask, SIGTERM);
357 sigaddset(&action.sa_mask, SIGHUP);
358 sigaction(SIGSEGV, &action, NULL);
359 sigaction(SIGILL, &action, NULL);
360 sigaction(SIGBUS, &action, NULL);
361 action.sa_handler = SIG_IGN;
362 sigaction(SIGPIPE, &action, NULL);
363
364 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
365
366 /* start daemon with thread-pool */
367 charon->start(charon);
368 /* wait for signal */
369 return run();
370 }