botan: Encode curve OID and public key in EC private key
[strongswan.git] / src / charon-cmd / charon-cmd.c
index b2cb6e8..1293ec4 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (C) 2006-2012 Tobias Brunner
+ * Copyright (C) 2006-2013 Tobias Brunner
  * Copyright (C) 2005-2013 Martin Willi
  * Copyright (C) 2006 Daniel Roethlisberger
  * Copyright (C) 2005 Jan Hutter
- * Hochschule fuer Technik Rapperswil
+ * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
  */
 
 #include <stdio.h>
-#define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
 #include <signal.h>
-#undef _POSIX_PTHREAD_SEMANTICS
 #include <pthread.h>
 #include <sys/types.h>
 #include <sys/utsname.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <errno.h>
 
 #include <library.h>
-#include <hydra.h>
 #include <daemon.h>
 #include <utils/backtrace.h>
 #include <threading/thread.h>
 
+#include "cmd/cmd_options.h"
+#include "cmd/cmd_connection.h"
+#include "cmd/cmd_creds.h"
+
 /**
- * Loglevel configuration
+ * Default loglevel
  */
-static level_t levels[DBG_MAX];
+static level_t default_loglevel = LEVEL_CTRL;
+
+/**
+ * Connection to initiate
+ */
+static cmd_connection_t *conn;
+
+/**
+ * Credential backend
+ */
+static cmd_creds_t *creds;
 
 /**
  * hook in library for debugging messages
@@ -49,7 +61,7 @@ static void dbg_stderr(debug_t group, level_t level, char *fmt, ...)
 {
        va_list args;
 
-       if (level <= 1)
+       if (level <= default_loglevel)
        {
                va_start(args, fmt);
                fprintf(stderr, "00[%N] ", debug_names, group);
@@ -60,29 +72,49 @@ static void dbg_stderr(debug_t group, level_t level, char *fmt, ...)
 }
 
 /**
+ * Clean up connection definition atexit()
+ */
+static void cleanup_conn()
+{
+       DESTROY_IF(conn);
+}
+
+/**
+ * Clean up credentials atexit()
+ */
+static void cleanup_creds()
+{
+       DESTROY_IF(creds);
+}
+
+/**
  * Run the daemon and handle unix signals
  */
-static void run()
+static int run()
 {
        sigset_t set;
 
-       /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
+       /* handle SIGINT, SIGHUP and SIGTERM in this handler */
        sigemptyset(&set);
        sigaddset(&set, SIGINT);
        sigaddset(&set, SIGHUP);
        sigaddset(&set, SIGTERM);
+       sigaddset(&set, SIGUSR1);
        sigprocmask(SIG_BLOCK, &set, NULL);
 
        while (TRUE)
        {
                int sig;
-               int error;
 
-               error = sigwait(&set, &sig);
-               if (error)
+               sig = sigwaitinfo(&set, NULL);
+               if (sig == -1)
                {
-                       DBG1(DBG_DMN, "error %d while waiting for a signal", error);
-                       return;
+                       if (errno == EINTR)
+                       {       /* ignore signals we didn't wait for */
+                               continue;
+                       }
+                       DBG1(DBG_DMN, "waiting for signal failed: %s", strerror(errno));
+                       return 1;
                }
                switch (sig)
                {
@@ -90,9 +122,9 @@ static void run()
                        {
                                DBG1(DBG_DMN, "signal of type SIGHUP received. Reloading "
                                         "configuration");
-                               if (lib->settings->load_files(lib->settings, NULL, FALSE))
+                               if (lib->settings->load_files(lib->settings, lib->conf, FALSE))
                                {
-                                       charon->load_loggers(charon, levels, TRUE);
+                                       charon->load_loggers(charon);
                                        lib->plugins->reload(lib->plugins, NULL);
                                }
                                else
@@ -105,18 +137,18 @@ static void run()
                        {
                                DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
                                charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
-                               return;
+                               return 0;
                        }
                        case SIGTERM:
                        {
                                DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
                                charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
-                               return;
+                               return 0;
                        }
-                       default:
-                       {
-                               DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
-                               break;
+                       case SIGUSR1:
+                       {       /* an error occurred */
+                               charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
+                               return 1;
                        }
                }
        }
@@ -128,13 +160,13 @@ static void run()
 static bool lookup_uid_gid()
 {
 #ifdef IPSEC_USER
-       if (!charon->caps->resolve_uid(charon->caps, IPSEC_USER))
+       if (!lib->caps->resolve_uid(lib->caps, IPSEC_USER))
        {
                return FALSE;
        }
 #endif
 #ifdef IPSEC_GROUP
-       if (!charon->caps->resolve_gid(charon->caps, IPSEC_GROUP))
+       if (!lib->caps->resolve_gid(lib->caps, IPSEC_GROUP))
        {
                return FALSE;
        }
@@ -159,111 +191,107 @@ static void segv_handler(int signal)
 }
 
 /**
- * Command line arguments, similar to "struct option", but with descriptions
- */
-static struct {
-       /** long option name */
-       const char *lng;
-       /** short option name */
-       const char shrt;
-       /** takes argument */
-       int has_arg;
-       /** decription of argument */
-       const char *arg;
-       /** description to option */
-       const char *desc;
-} options[] = {
-       { "help", 'h', no_argument, "",
-         "print this usage information and exit" },
-       { "version", 'v', no_argument, "",
-         "show version information and exit" },
-};
-
-/**
  * Print command line usage and exit
  */
 static void usage(FILE *out, char *msg, char *binary)
 {
-       int i, pre, post, padto = 0, spacing = 2;
-
-       for (i = 0; i < countof(options); i++)
-       {
-               padto = max(padto, strlen(options[i].lng) + strlen(options[i].arg));
-       }
-       padto += spacing;
+       static const int padto = 18;
+       char cmd[64], *pre, *post;
+       int i, line, pad;
 
        if (msg)
        {
                fprintf(out, "%s\n", msg);
        }
        fprintf(out, "Usage: %s\n", binary);
-       for (i = 0; i < countof(options); i++)
+       for (i = 0; i < CMD_OPT_COUNT; i++)
        {
-               switch (options[i].has_arg)
+               switch (cmd_options[i].has_arg)
                {
                        case required_argument:
-                               pre = '<';
-                               post = '>';
+                               pre = " <";
+                               post = ">";
                                break;
                        case optional_argument:
-                               pre = '[';
-                               post = ']';
+                               pre = "[=";
+                               post = "]";
                                break;
                        case no_argument:
                        default:
-                               pre = post = ' ';
+                               pre = "  ";
+                               post = " ";
                                break;
                }
-               fprintf(out, "  --%s (-%-c) %c%s%c %-*s%s\n",
-                       options[i].lng, options[i].shrt,
-                       pre, options[i].arg, post,
-                       padto - strlen(options[i].lng) - strlen(options[i].arg), "",
-                       options[i].desc);
+               snprintf(cmd, sizeof(cmd), "  --%s%s%s%s", cmd_options[i].name,
+                                pre, cmd_options[i].arg, post);
+               pad = padto - strlen(cmd);
+               if (pad >= 1)
+               {
+                       fprintf(out, "%s%-*s%s\n", cmd, pad, "", cmd_options[i].desc);
+               }
+               else
+               {       /* write description to a separate line */
+                       fprintf(out, "%s\n%-*s%s\n", cmd, padto, "", cmd_options[i].desc);
+               }
+               for (line = 0; line < countof(cmd_options[i].lines); line++)
+               {
+                       if (cmd_options[i].lines[line])
+                       {
+                               fprintf(out, "%-*s%s\n", padto, "", cmd_options[i].lines[line]);
+                       }
+               }
        }
 }
 
 /**
- * Handle command line options
+ * Handle command line options, if simple is TRUE only arguments like --help
+ * and --version are handled.
  */
-static void handle_arguments(int argc, char *argv[])
+static void handle_arguments(int argc, char *argv[], bool simple)
 {
+       struct option long_opts[CMD_OPT_COUNT + 1] = {};
+       int i, opt;
+
+       for (i = 0; i < CMD_OPT_COUNT; i++)
+       {
+               long_opts[i].name = cmd_options[i].name;
+               long_opts[i].val = cmd_options[i].id;
+               long_opts[i].has_arg = cmd_options[i].has_arg;
+       }
+       /* reset option parser */
+       optind = 1;
        while (TRUE)
        {
-               struct option long_opts[countof(options) + 1] = {};
-               char optstring[countof(options) * 3 + 1] = {};
-               int i, pos = 0;
+               bool handled = FALSE;
 
-               for (i = 0; i < countof(options); i++)
-               {
-                       long_opts[i].name = options[i].lng;
-                       long_opts[i].val = options[i].shrt;
-                       long_opts[i].has_arg = options[i].has_arg;
-                       optstring[pos++] = options[i].shrt;
-                       switch (options[i].has_arg)
-                       {
-                               case optional_argument:
-                                       optstring[pos++] = ':';
-                                       /* FALL */
-                               case required_argument:
-                                       optstring[pos++] = ':';
-                                       /* FALL */
-                               case no_argument:
-                               default:
-                                       break;
-                       }
-               }
-
-               switch (getopt_long(argc, argv, optstring, long_opts, NULL))
+               opt = getopt_long(argc, argv, "", long_opts, NULL);
+               switch (opt)
                {
                        case EOF:
                                break;
-                       case 'h':
+                       case CMD_OPT_HELP:
                                usage(stdout, NULL, argv[0]);
                                exit(0);
-                       case 'v':
+                       case CMD_OPT_VERSION:
                                printf("%s, strongSwan %s\n", "charon-cmd", VERSION);
                                exit(0);
+                       case CMD_OPT_DEBUG:
+                               default_loglevel = atoi(optarg);
+                               continue;
                        default:
+                               if (simple)
+                               {
+                                       continue;
+                               }
+                               handled |= conn->handle(conn, opt, optarg);
+                               handled |= creds->handle(creds, opt, optarg);
+                               if (handled)
+                               {
+                                       continue;
+                               }
+                               /* fall-through */
+                       case '?':
+                               /* missing argument, unrecognized option */
                                usage(stderr, NULL, argv[0]);
                                exit(1);
                }
@@ -278,11 +306,15 @@ int main(int argc, char *argv[])
 {
        struct sigaction action;
        struct utsname utsname;
+       level_t levels[DBG_MAX];
        int group;
 
+       /* handle simple arguments */
+       handle_arguments(argc, argv, TRUE);
+
        dbg = dbg_stderr;
        atexit(library_deinit);
-       if (!library_init(NULL))
+       if (!library_init(NULL, "charon-cmd"))
        {
                exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
        }
@@ -293,54 +325,59 @@ int main(int argc, char *argv[])
                        exit(SS_RC_DAEMON_INTEGRITY);
                }
        }
-       atexit(libhydra_deinit);
-       if (!libhydra_init("charon-cmd"))
-       {
-               exit(SS_RC_INITIALIZATION_FAILED);
-       }
        atexit(libcharon_deinit);
-       if (!libcharon_init("charon-cmd"))
+       if (!libcharon_init())
        {
                exit(SS_RC_INITIALIZATION_FAILED);
        }
        for (group = 0; group < DBG_MAX; group++)
        {
-               levels[group] = LEVEL_CTRL;
+               levels[group] = default_loglevel;
        }
-
-       handle_arguments(argc, argv);
+       charon->set_default_loggers(charon, levels, TRUE);
+       charon->load_loggers(charon);
 
        if (!lookup_uid_gid())
        {
                exit(SS_RC_INITIALIZATION_FAILED);
        }
-       charon->load_loggers(charon, levels, TRUE);
-
-       if (uname(&utsname) != 0)
-       {
-               memset(&utsname, 0, sizeof(utsname));
-       }
-       DBG1(DBG_DMN, "Starting charon-cmd IKE client (strongSwan %s, %s %s, %s)",
-                VERSION, utsname.sysname, utsname.release, utsname.machine);
-
+       lib->settings->set_default_str(lib->settings, "charon-cmd.port", "0");
+       lib->settings->set_default_str(lib->settings, "charon-cmd.port_nat_t", "0");
        if (!charon->initialize(charon,
                        lib->settings->get_str(lib->settings, "charon-cmd.load", PLUGINS)))
        {
                exit(SS_RC_INITIALIZATION_FAILED);
        }
-       if (!charon->caps->drop(charon->caps))
+       if (!lib->caps->drop(lib->caps))
        {
                exit(SS_RC_INITIALIZATION_FAILED);
        }
 
+       conn = cmd_connection_create();
+       atexit(cleanup_conn);
+       creds = cmd_creds_create();
+       atexit(cleanup_creds);
+
+       /* handle all arguments */
+       handle_arguments(argc, argv, FALSE);
+
+       if (uname(&utsname) != 0)
+       {
+               memset(&utsname, 0, sizeof(utsname));
+       }
+       DBG1(DBG_DMN, "Starting charon-cmd IKE client (strongSwan %s, %s %s, %s)",
+                VERSION, utsname.sysname, utsname.release, utsname.machine);
+       lib->plugins->status(lib->plugins, LEVEL_CTRL);
+
        /* add handler for SEGV and ILL,
-        * INT, TERM and HUP are handled by sigwait() in run() */
+        * INT, TERM and HUP are handled by sigwaitinfo() in run() */
        action.sa_handler = segv_handler;
        action.sa_flags = 0;
        sigemptyset(&action.sa_mask);
        sigaddset(&action.sa_mask, SIGINT);
        sigaddset(&action.sa_mask, SIGTERM);
        sigaddset(&action.sa_mask, SIGHUP);
+       sigaddset(&action.sa_mask, SIGUSR1);
        sigaction(SIGSEGV, &action, NULL);
        sigaction(SIGILL, &action, NULL);
        sigaction(SIGBUS, &action, NULL);
@@ -352,7 +389,5 @@ int main(int argc, char *argv[])
        /* start daemon with thread-pool */
        charon->start(charon);
        /* wait for signal */
-       run();
-
-       return 0;
+       return run();
 }