Introducing the Big Tinc Lock.
[tinc] / src / tincd.c
index 1a9bad0..9518676 100644 (file)
@@ -1,9 +1,11 @@
 /*
     tincd.c -- the main file for tincd
     Copyright (C) 1998-2005 Ivo Timmermans
-                  2000-2009 Guus Sliepen <guus@tinc-vpn.org>
+                  2000-2010 Guus Sliepen <guus@tinc-vpn.org>
                   2008      Max Rijevski <maksuf@gmail.com>
                   2009      Michael Tokarev <mjt@tls.msk.ru>
+                  2010      Julien Muchembled <jm@jmuchemb.eu>
+                  2010      Timothy Redaelli <timothy@redaelli.eu>
 
     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
 #include <sys/mman.h>
 #endif
 
+#include <openssl/rand.h>
+#include <openssl/rsa.h>
+#include <openssl/pem.h>
+#include <openssl/evp.h>
+#include <openssl/engine.h>
+
+#ifdef HAVE_LZO
 #include LZO1X_H
+#endif
 
 #ifndef HAVE_MINGW
 #include <pwd.h>
@@ -78,8 +88,8 @@ static const char *switchuser = NULL;
 bool use_logfile = false;
 
 char *identname = NULL;                                /* program name for syslog */
-char *controlsocketname = NULL;                        /* control socket location */
 char *logfilename = NULL;                      /* log file location */
+char *controlcookiename = NULL;
 char **g_argv;                                 /* a copy of the cmdline arguments */
 
 static int status;
@@ -96,13 +106,16 @@ static struct option const long_options[] = {
        {"chroot", no_argument, NULL, 'R'},
        {"user", required_argument, NULL, 'U'},
        {"logfile", optional_argument, NULL, 4},
-       {"controlsocket", required_argument, NULL, 5},
+       {"controlcookie", required_argument, NULL, 5},
        {NULL, 0, NULL, 0}
 };
 
+mutex_t mutex;
+
 #ifdef HAVE_MINGW
 static struct WSAData wsa_state;
 CRITICAL_SECTION mutex;
+int main2(int argc, char **argv);
 #endif
 
 static void usage(bool status) {
@@ -117,8 +130,9 @@ static void usage(bool status) {
                                "  -n, --net=NETNAME             Connect to net NETNAME.\n"
                                "  -L, --mlock                   Lock tinc into main memory.\n"
                                "      --logfile[=FILENAME]      Write log entries to a logfile.\n"
-                               "      --controlsocket=FILENAME  Open control socket at FILENAME.\n"
+                               "      --controlcookie=FILENAME  Write control socket cookie to FILENAME.\n"
                                "      --bypass-security         Disables meta protocol security, for debugging.\n"
+                               "  -o [HOST.]KEY=VALUE           Set global/host configuration value.\n"
                                "  -R, --chroot                  chroot to NET dir at startup.\n"
                                "  -U, --user=USER               setuid to given USER at startup.\n"                            "      --help                    Display this help and exit.\n"
                                "      --version                 Output version information and exit.\n\n");
@@ -127,10 +141,14 @@ static void usage(bool status) {
 }
 
 static bool parse_options(int argc, char **argv) {
+       config_t *cfg;
        int r;
        int option_index = 0;
+       int lineno = 0;
+
+       cmdline_conf = list_alloc((list_action_t)free_config);
 
-       while((r = getopt_long(argc, argv, "c:DLd::n:RU:", long_options, &option_index)) != EOF) {
+       while((r = getopt_long(argc, argv, "c:DLd::n:o:RU:", long_options, &option_index)) != EOF) {
                switch (r) {
                        case 0:                         /* long option */
                                break;
@@ -160,7 +178,16 @@ static bool parse_options(int argc, char **argv) {
                                break;
 
                        case 'n':                               /* net name given */
-                               netname = xstrdup(optarg);
+                               /* netname "." is special: a "top-level name" */
+                               netname = strcmp(optarg, ".") != 0 ?
+                                               xstrdup(optarg) : NULL;
+                               break;
+
+                       case 'o':                               /* option */
+                               cfg = parse_config_line(optarg, NULL, ++lineno);
+                               if (!cfg)
+                                       return false;
+                               list_insert_tail(cmdline_conf, cfg);
                                break;
 
                        case 'R':                               /* chroot to NETNAME dir */
@@ -190,7 +217,7 @@ static bool parse_options(int argc, char **argv) {
                                break;
 
                        case 5:                                 /* open control socket here */
-                               controlsocketname = xstrdup(optarg);
+                               controlcookiename = xstrdup(optarg);
                                break;
 
                        case '?':
@@ -231,6 +258,8 @@ static void make_names(void) {
                                else
                                        xasprintf(&confbase, "%s", installdir);
                        }
+                       if(!controlcookiename)
+                               xasprintf(&controlcookiename, "%s/cookie", confbase);
                }
                RegCloseKey(key);
                if(*installdir)
@@ -238,12 +267,12 @@ static void make_names(void) {
        }
 #endif
 
-       if(!controlsocketname)
-               xasprintf(&controlsocketname, "%s/run/%s.control/socket", LOCALSTATEDIR, identname);
-
        if(!logfilename)
                xasprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
 
+       if(!controlcookiename)
+               xasprintf(&controlcookiename, LOCALSTATEDIR "/run/%s.cookie", identname);
+
        if(netname) {
                if(!confbase)
                        xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
@@ -258,7 +287,7 @@ static void make_names(void) {
 static void free_names() {
        if (identname) free(identname);
        if (netname) free(netname);
-       if (controlsocketname) free(controlsocketname);
+       if (controlcookiename) free(controlcookiename);
        if (logfilename) free(logfilename);
        if (confbase) free(confbase);
 }
@@ -331,7 +360,7 @@ int main(int argc, char **argv) {
        if(show_version) {
                printf("%s version %s (built %s %s, protocol %d)\n", PACKAGE,
                           VERSION, __DATE__, __TIME__, PROT_CURRENT);
-               printf("Copyright (C) 1998-2009 Ivo Timmermans, Guus Sliepen and others.\n"
+               printf("Copyright (C) 1998-2010 Ivo Timmermans, Guus Sliepen and others.\n"
                                "See the AUTHORS file for a complete list.\n\n"
                                "tinc comes with ABSOLUTELY NO WARRANTY.  This is free software,\n"
                                "and you are welcome to redistribute it under certain conditions;\n"
@@ -345,18 +374,20 @@ int main(int argc, char **argv) {
                return 0;
        }
 
-       openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
-
-       if(!event_init()) {
-               logger(LOG_ERR, "Error initializing libevent!");
+#ifdef HAVE_MINGW
+       if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+               logger(LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
                return 1;
        }
+#endif
 
-       if(!init_control())
-               return 1;
+       openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
 
        g_argv = argv;
 
+       mutex_create(&mutex);
+       mutex_lock(&mutex);
+       init_events();
        init_configuration(&config_tree);
 
        /* Slllluuuuuuurrrrp! */
@@ -367,17 +398,14 @@ int main(int argc, char **argv) {
        if(!read_server_config())
                return 1;
 
+#ifdef HAVE_LZO
        if(lzo_init() != LZO_E_OK) {
                logger(LOG_ERR, "Error initializing LZO compressor!");
                return 1;
        }
+#endif
 
 #ifdef HAVE_MINGW
-       if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
-               logger(LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
-               return 1;
-       }
-
        if(!do_detach || !init_service())
                return main2(argc, argv);
        else
@@ -408,6 +436,9 @@ int main2(int argc, char **argv) {
        if(!setup_network())
                goto end;
 
+       if(!init_control())
+               return 1;
+
        /* Initiate all outgoing connections. */
 
        try_outgoing_connections();
@@ -417,13 +448,25 @@ int main2(int argc, char **argv) {
         char *priority = 0;
 
         if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) {
-                if(!strcasecmp(priority, "Normal"))
-                        setpriority(NORMAL_PRIORITY_CLASS);
-                else if(!strcasecmp(priority, "Low"))
-                        setpriority(BELOW_NORMAL_PRIORITY_CLASS);
-                else if(!strcasecmp(priority, "High"))
-                        setpriority(HIGH_PRIORITY_CLASS);
-                else {
+                if(!strcasecmp(priority, "Normal")) {
+                        if (setpriority(NORMAL_PRIORITY_CLASS) != 0) {
+                                logger(LOG_ERR, "System call `%s' failed: %s",
+                                       "setpriority", strerror(errno));
+                                goto end;
+                        }
+                } else if(!strcasecmp(priority, "Low")) {
+                        if (setpriority(BELOW_NORMAL_PRIORITY_CLASS) != 0) {
+                                       logger(LOG_ERR, "System call `%s' failed: %s",
+                                       "setpriority", strerror(errno));
+                                goto end;
+                        }
+                } else if(!strcasecmp(priority, "High")) {
+                        if (setpriority(HIGH_PRIORITY_CLASS) != 0) {
+                                logger(LOG_ERR, "System call `%s' failed: %s",
+                                       "setpriority", strerror(errno));
+                                goto end;
+                        }
+                } else {
                         logger(LOG_ERR, "Invalid priority `%s`!", priority);
                         goto end;
                 }
@@ -447,13 +490,12 @@ int main2(int argc, char **argv) {
 end:
        logger(LOG_NOTICE, "Terminating");
 
-#ifndef HAVE_MINGW
        exit_control();
-#endif
 
        crypto_exit();
 
        exit_configuration(&config_tree);
+       exit_events();
        free_names();
 
        return status;