Big header file cleanup: everything that has to do with standard system
[oweals/tinc.git] / src / net_setup.c
index acb156cc4c0d404a764b7e077b09eedd06b2eeab..ca7aa61ed80aa83c5fa5527c75e16265054ffbcb 100644 (file)
@@ -1,7 +1,7 @@
 /*
     net_setup.c -- Setup.
-    Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
-                  2000-2002 Guus Sliepen <guus@sliepen.eu.org>
+    Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>,
+                  2000-2003 Guus Sliepen <guus@sliepen.eu.org>
 
     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
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: net_setup.c,v 1.1.2.28 2003/01/14 12:53:59 guus Exp $
+    $Id: net_setup.c,v 1.1.2.35 2003/07/17 15:06:26 guus Exp $
 */
 
-#include "config.h"
-
-#include <errno.h>
-#include <fcntl.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <syslog.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-/* SunOS really wants sys/socket.h BEFORE net/if.h,
-   and FreeBSD wants these lines below the rest. */
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#include <net/if.h>
-#ifdef HAVE_NETINET_IN_SYSTM_H
-#include <netinet/in_systm.h>
-#endif
-#ifdef HAVE_NETINET_IP_H
-#include <netinet/ip.h>
-#endif
-#ifdef HAVE_NETINET_TCP_H
-#include <netinet/tcp.h>
-#endif
+#include "system.h"
 
 #include <openssl/pem.h>
 #include <openssl/rsa.h>
 #include <openssl/rand.h>
 
-#include <utils.h>
-#include <xalloc.h>
-#include <avl_tree.h>
-#include <list.h>
-
+#include "avl_tree.h"
 #include "conf.h"
 #include "connection.h"
-#include "meta.h"
+#include "device.h"
+#include "event.h"
+#include "graph.h"
+#include "logger.h"
 #include "net.h"
 #include "netutl.h"
 #include "process.h"
 #include "protocol.h"
-#include "subnet.h"
-#include "graph.h"
-#include "process.h"
 #include "route.h"
-#include "device.h"
-#include "event.h"
-
-#include "system.h"
+#include "subnet.h"
+#include "utils.h"
+#include "xalloc.h"
 
 char *myport;
 
@@ -85,8 +52,10 @@ int read_rsa_public_key(connection_t *c)
 
        cp();
 
-       if(!c->rsa_key)
+       if(!c->rsa_key) {
                c->rsa_key = RSA_new();
+//             RSA_blinding_on(c->rsa_key, NULL);
+       }
 
        /* First, check for simple PublicKey statement */
 
@@ -105,7 +74,7 @@ int read_rsa_public_key(connection_t *c)
                        fp = fopen(fname, "r");
 
                        if(!fp) {
-                               syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
+                               logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
                                           fname, strerror(errno));
                                free(fname);
                                return -1;
@@ -122,7 +91,7 @@ int read_rsa_public_key(connection_t *c)
                        fp = fopen(fname, "r");
 
                        if(!fp) {
-                               syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
+                               logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
                                           fname, strerror(errno));
                                free(fname);
                                return -1;
@@ -132,10 +101,12 @@ int read_rsa_public_key(connection_t *c)
                        c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
                        fclose(fp);
 
-                       if(c->rsa_key)
+                       if(c->rsa_key) {
+//                             RSA_blinding_on(c->rsa_key, NULL);
                                return 0;
+                       }
 
-                       syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
+                       logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
                                   fname, strerror(errno));
                        return -1;
                } else {
@@ -166,6 +137,7 @@ int read_rsa_public_key(connection_t *c)
 
        if(fp) {
                c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
+//             RSA_blinding_on(c->rsa_key, NULL);
                fclose(fp);
        }
 
@@ -174,7 +146,7 @@ int read_rsa_public_key(connection_t *c)
        if(c->rsa_key)
                return 0;
 
-       syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
+       logger(LOG_ERR, _("No public key for %s specified!"), c->name);
 
        return -1;
 }
@@ -188,6 +160,7 @@ int read_rsa_private_key(void)
 
        if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
                myself->connection->rsa_key = RSA_new();
+//             RSA_blinding_on(myself->connection->rsa_key, NULL);
                BN_hex2bn(&myself->connection->rsa_key->d, key);
                BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
                free(key);
@@ -201,7 +174,7 @@ int read_rsa_private_key(void)
                fp = fopen(fname, "r");
 
                if(!fp) {
-                       syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
+                       logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
                                   fname, strerror(errno));
                        free(fname);
                        return -1;
@@ -213,7 +186,7 @@ int read_rsa_private_key(void)
                fclose(fp);
 
                if(!myself->connection->rsa_key) {
-                       syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
+                       logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
                                   fname, strerror(errno));
                        return -1;
                }
@@ -250,12 +223,12 @@ int setup_myself(void)
        myself->connection->protocol_version = PROT_CURRENT;
 
        if(!get_config_string(lookup_config(config_tree, "Name"), &name)) {     /* Not acceptable */
-               syslog(LOG_ERR, _("Name for tinc daemon required!"));
+               logger(LOG_ERR, _("Name for tinc daemon required!"));
                return -1;
        }
 
        if(check_id(name)) {
-               syslog(LOG_ERR, _("Invalid name for myself!"));
+               logger(LOG_ERR, _("Invalid name for myself!"));
                free(name);
                return -1;
        }
@@ -267,7 +240,7 @@ int setup_myself(void)
                return -1;
 
        if(read_connection_config(myself->connection)) {
-               syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
+               logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
                return -1;
        }
 
@@ -323,7 +296,7 @@ int setup_myself(void)
                else if(!strcasecmp(mode, "hub"))
                        routing_mode = RMODE_HUB;
                else {
-                       syslog(LOG_ERR, _("Invalid routing mode!"));
+                       logger(LOG_ERR, _("Invalid routing mode!"));
                        return -1;
                }
                free(mode);
@@ -334,7 +307,7 @@ int setup_myself(void)
                                        &priorityinheritance);
 #if !defined(SOL_IP) || !defined(IP_TOS)
        if(priorityinheritance)
-               syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
+               logger(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
 #endif
 
        if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
@@ -344,7 +317,7 @@ int setup_myself(void)
           (lookup_config(myself->connection->config_tree, "MaxTimeout"),
                &maxtimeout)) {
                if(maxtimeout <= 0) {
-                       syslog(LOG_ERR, _("Bogus maximum timeout!"));
+                       logger(LOG_ERR, _("Bogus maximum timeout!"));
                        return -1;
                }
        } else
@@ -358,12 +331,11 @@ int setup_myself(void)
                else if(!strcasecmp(afname, "any"))
                        addressfamily = AF_UNSPEC;
                else {
-                       syslog(LOG_ERR, _("Invalid address family!"));
+                       logger(LOG_ERR, _("Invalid address family!"));
                        return -1;
                }
                free(afname);
-       } else
-               addressfamily = AF_INET;
+       }
 
        get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
 
@@ -377,7 +349,7 @@ int setup_myself(void)
                        myself->cipher = EVP_get_cipherbyname(cipher);
 
                        if(!myself->cipher) {
-                               syslog(LOG_ERR, _("Unrecognized cipher type!"));
+                               logger(LOG_ERR, _("Unrecognized cipher type!"));
                                return -1;
                        }
                }
@@ -398,6 +370,9 @@ int setup_myself(void)
                keylifetime = 3600;
 
        keyexpires = now + keylifetime;
+       
+       EVP_CIPHER_CTX_init(&packet_ctx);
+       EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
 
        /* Check if we want to use message authentication codes... */
 
@@ -409,7 +384,7 @@ int setup_myself(void)
                        myself->digest = EVP_get_digestbyname(digest);
 
                        if(!myself->digest) {
-                               syslog(LOG_ERR, _("Unrecognized digest type!"));
+                               logger(LOG_ERR, _("Unrecognized digest type!"));
                                return -1;
                        }
                }
@@ -423,10 +398,10 @@ int setup_myself(void)
                &myself->maclength)) {
                if(myself->digest) {
                        if(myself->maclength > myself->digest->md_size) {
-                               syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
+                               logger(LOG_ERR, _("MAC length exceeds size of digest!"));
                                return -1;
                        } else if(myself->maclength < 0) {
-                               syslog(LOG_ERR, _("Bogus MAC length!"));
+                               logger(LOG_ERR, _("Bogus MAC length!"));
                                return -1;
                        }
                }
@@ -440,8 +415,8 @@ int setup_myself(void)
        if(get_config_int
           (lookup_config(myself->connection->config_tree, "Compression"),
                &myself->compression)) {
-               if(myself->compression < 0 || myself->compression > 9) {
-                       syslog(LOG_ERR, _("Bogus compression level!"));
+               if(myself->compression < 0 || myself->compression > 11) {
+                       logger(LOG_ERR, _("Bogus compression level!"));
                        return -1;
                }
        } else
@@ -473,7 +448,7 @@ int setup_myself(void)
        err = getaddrinfo(address, myport, &hint, &ai);
 
        if(err || !ai) {
-               syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
                           gai_strerror(err));
                return -1;
        }
@@ -493,9 +468,9 @@ int setup_myself(void)
                if(listen_socket[listen_sockets].udp < 0)
                        continue;
 
-               if(debug_lvl >= DEBUG_CONNECTIONS) {
+               ifdebug(CONNECTIONS) {
                        hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
-                       syslog(LOG_NOTICE, _("Listening on %s"), hostname);
+                       logger(LOG_NOTICE, _("Listening on %s"), hostname);
                        free(hostname);
                }
 
@@ -506,9 +481,9 @@ int setup_myself(void)
        freeaddrinfo(ai);
 
        if(listen_sockets)
-               syslog(LOG_NOTICE, _("Ready"));
+               logger(LOG_NOTICE, _("Ready"));
        else {
-               syslog(LOG_ERR, _("Unable to create any listening socket!"));
+               logger(LOG_ERR, _("Unable to create any listening socket!"));
                return -1;
        }