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
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>.
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
20 #define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
22 #undef _POSIX_PTHREAD_SEMANTICS
24 #include <sys/types.h>
25 #include <sys/utsname.h>
32 #include <utils/backtrace.h>
33 #include <threading/thread.h>
35 #include "cmd/cmd_options.h"
36 #include "cmd/cmd_connection.h"
37 #include "cmd/cmd_creds.h"
42 static level_t default_loglevel
= LEVEL_CTRL
;
45 * Loglevel configuration
47 static level_t levels
[DBG_MAX
];
50 * Connection to initiate
52 static cmd_connection_t
*conn
;
57 static cmd_creds_t
*creds
;
60 * hook in library for debugging messages
62 extern void (*dbg
) (debug_t group
, level_t level
, char *fmt
, ...);
65 * Logging hook for library logs, using stderr output
67 static void dbg_stderr(debug_t group
, level_t level
, char *fmt
, ...)
71 if (level
<= default_loglevel
)
74 fprintf(stderr
, "00[%N] ", debug_names
, group
);
75 vfprintf(stderr
, fmt
, args
);
76 fprintf(stderr
, "\n");
82 * Clean up connection definition atexit()
84 static void cleanup_conn()
90 * Clean up credentials atexit()
92 static void cleanup_creds()
98 * Run the daemon and handle unix signals
104 /* handle SIGINT, SIGHUP and SIGTERM in this handler */
106 sigaddset(&set
, SIGINT
);
107 sigaddset(&set
, SIGHUP
);
108 sigaddset(&set
, SIGTERM
);
109 sigaddset(&set
, SIGUSR1
);
110 sigprocmask(SIG_BLOCK
, &set
, NULL
);
117 error
= sigwait(&set
, &sig
);
120 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
127 DBG1(DBG_DMN
, "signal of type SIGHUP received. Reloading "
129 if (lib
->settings
->load_files(lib
->settings
, NULL
, FALSE
))
131 charon
->load_loggers(charon
, levels
, TRUE
);
132 lib
->plugins
->reload(lib
->plugins
, NULL
);
136 DBG1(DBG_DMN
, "reloading config failed, keeping old");
142 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
143 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
148 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
149 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
153 { /* an error occurred */
154 charon
->bus
->alert(charon
->bus
, ALERT_SHUTDOWN_SIGNAL
, sig
);
159 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
169 static bool lookup_uid_gid()
172 if (!lib
->caps
->resolve_uid(lib
->caps
, IPSEC_USER
))
178 if (!lib
->caps
->resolve_gid(lib
->caps
, IPSEC_GROUP
))
187 * Handle SIGSEGV/SIGILL signals raised by threads
189 static void segv_handler(int signal
)
191 backtrace_t
*backtrace
;
193 DBG1(DBG_DMN
, "thread %u received %d", thread_current_id(), signal
);
194 backtrace
= backtrace_create(2);
195 backtrace
->log(backtrace
, stderr
, TRUE
);
196 backtrace
->destroy(backtrace
);
198 DBG1(DBG_DMN
, "killing ourself, received critical signal");
203 * Print command line usage and exit
205 static void usage(FILE *out
, char *msg
, char *binary
)
207 static const int padto
= 18;
208 char cmd
[64], *pre
, *post
;
213 fprintf(out
, "%s\n", msg
);
215 fprintf(out
, "Usage: %s\n", binary
);
216 for (i
= 0; i
< CMD_OPT_COUNT
; i
++)
218 switch (cmd_options
[i
].has_arg
)
220 case required_argument
:
224 case optional_argument
:
234 snprintf(cmd
, sizeof(cmd
), " --%s%s%s%s", cmd_options
[i
].name
,
235 pre
, cmd_options
[i
].arg
, post
);
236 pad
= padto
- strlen(cmd
);
239 fprintf(out
, "%s%-*s%s\n", cmd
, pad
, "", cmd_options
[i
].desc
);
242 { /* write description to a separate line */
243 fprintf(out
, "%s\n%-*s%s\n", cmd
, padto
, "", cmd_options
[i
].desc
);
245 for (line
= 0; line
< countof(cmd_options
[i
].lines
); line
++)
247 if (cmd_options
[i
].lines
[line
])
249 fprintf(out
, "%-*s%s\n", padto
, "", cmd_options
[i
].lines
[line
]);
256 * Handle command line options, if simple is TRUE only arguments like --help
257 * and --version are handled.
259 static void handle_arguments(int argc
, char *argv
[], bool simple
)
261 struct option long_opts
[CMD_OPT_COUNT
+ 1] = {};
264 for (i
= 0; i
< CMD_OPT_COUNT
; i
++)
266 long_opts
[i
].name
= cmd_options
[i
].name
;
267 long_opts
[i
].val
= cmd_options
[i
].id
;
268 long_opts
[i
].has_arg
= cmd_options
[i
].has_arg
;
270 /* reset option parser */
274 bool handled
= FALSE
;
276 opt
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
282 usage(stdout
, NULL
, argv
[0]);
284 case CMD_OPT_VERSION
:
285 printf("%s, strongSwan %s\n", "charon-cmd", VERSION
);
288 default_loglevel
= atoi(optarg
);
295 handled
|= conn
->handle(conn
, opt
, optarg
);
296 handled
|= creds
->handle(creds
, opt
, optarg
);
303 /* missing argument, unrecognized option */
304 usage(stderr
, NULL
, argv
[0]);
312 * Main function, starts the daemon.
314 int main(int argc
, char *argv
[])
316 struct sigaction action
;
317 struct utsname utsname
;
320 /* handle simple arguments */
321 handle_arguments(argc
, argv
, TRUE
);
324 atexit(library_deinit
);
325 if (!library_init(NULL
))
327 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
331 if (!lib
->integrity
->check_file(lib
->integrity
, "charon-cmd", argv
[0]))
333 exit(SS_RC_DAEMON_INTEGRITY
);
336 atexit(libhydra_deinit
);
337 if (!libhydra_init("charon-cmd"))
339 exit(SS_RC_INITIALIZATION_FAILED
);
341 atexit(libcharon_deinit
);
342 if (!libcharon_init("charon-cmd"))
344 exit(SS_RC_INITIALIZATION_FAILED
);
346 for (group
= 0; group
< DBG_MAX
; group
++)
348 levels
[group
] = default_loglevel
;
350 charon
->load_loggers(charon
, levels
, TRUE
);
352 if (!lookup_uid_gid())
354 exit(SS_RC_INITIALIZATION_FAILED
);
356 lib
->settings
->set_default_str(lib
->settings
, "charon-cmd.port", "0");
357 lib
->settings
->set_default_str(lib
->settings
, "charon-cmd.port_nat_t", "0");
358 if (!charon
->initialize(charon
,
359 lib
->settings
->get_str(lib
->settings
, "charon-cmd.load", PLUGINS
)))
361 exit(SS_RC_INITIALIZATION_FAILED
);
363 if (!lib
->caps
->drop(lib
->caps
))
365 exit(SS_RC_INITIALIZATION_FAILED
);
368 conn
= cmd_connection_create();
369 atexit(cleanup_conn
);
370 creds
= cmd_creds_create();
371 atexit(cleanup_creds
);
373 /* handle all arguments */
374 handle_arguments(argc
, argv
, FALSE
);
376 if (uname(&utsname
) != 0)
378 memset(&utsname
, 0, sizeof(utsname
));
380 DBG1(DBG_DMN
, "Starting charon-cmd IKE client (strongSwan %s, %s %s, %s)",
381 VERSION
, utsname
.sysname
, utsname
.release
, utsname
.machine
);
382 lib
->plugins
->status(lib
->plugins
, LEVEL_CTRL
);
384 /* add handler for SEGV and ILL,
385 * INT, TERM and HUP are handled by sigwait() in run() */
386 action
.sa_handler
= segv_handler
;
388 sigemptyset(&action
.sa_mask
);
389 sigaddset(&action
.sa_mask
, SIGINT
);
390 sigaddset(&action
.sa_mask
, SIGTERM
);
391 sigaddset(&action
.sa_mask
, SIGHUP
);
392 sigaddset(&action
.sa_mask
, SIGUSR1
);
393 sigaction(SIGSEGV
, &action
, NULL
);
394 sigaction(SIGILL
, &action
, NULL
);
395 sigaction(SIGBUS
, &action
, NULL
);
396 action
.sa_handler
= SIG_IGN
;
397 sigaction(SIGPIPE
, &action
, NULL
);
399 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, NULL
);
401 /* start daemon with thread-pool */
402 charon
->start(charon
);
403 /* wait for signal */