Cleanups:
[tinc] / src / net_setup.c
index b5dc727..0fe5068 100644 (file)
@@ -1,7 +1,7 @@
 /*
     net_setup.c -- Setup.
-    Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
-                  2000-2002 Guus Sliepen <guus@sliepen.warande.net>
+    Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
+                  2000-2002 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
@@ -17,7 +17,7 @@
     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.14 2002/04/01 21:28:39 guus Exp $
+    $Id: net_setup.c,v 1.1.2.24 2002/09/09 19:39:58 guus Exp $
 */
 
 #include "config.h"
 #include <fcntl.h>
 #include <netdb.h>
 #include <netinet/in.h>
-#ifdef HAVE_LINUX
+#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 <stdio.h>
@@ -77,7 +82,7 @@ int read_rsa_public_key(connection_t *c)
   FILE *fp;
   char *fname;
   char *key;
-cp
+  cp();
   if(!c->rsa_key)
     c->rsa_key = RSA_new();
 
@@ -97,7 +102,8 @@ cp
     {
       if(is_safe_path(fname))
         {
-          if((fp = fopen(fname, "r")) == NULL)
+          fp = fopen(fname, "r");
+          if(!fp)
             {
               syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
                      fname, strerror(errno));
@@ -107,13 +113,27 @@ cp
           free(fname);
           c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
           fclose(fp);
-          if(!c->rsa_key)
+         if(c->rsa_key)
+           return 0; /* Woohoo. */
+         
+         /* If it fails, try PEM_read_RSA_PUBKEY. */
+          fp = fopen(fname, "r");
+          if(!fp)
             {
-              syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
+              syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
                      fname, strerror(errno));
+              free(fname);
               return -1;
             }
-          return 0;
+          free(fname);
+          c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
+          fclose(fp);
+         if(c->rsa_key)
+           return 0;
+
+          syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
+                 fname, strerror(errno));
+          return -1;
         }
       else
         {
@@ -125,7 +145,9 @@ cp
   /* Else, check if a harnessed public key is in the config file */
 
   asprintf(&fname, "%s/hosts/%s", confbase, c->name);
-  if((fp = fopen(fname, "r")))
+  fp = fopen(fname, "r");
+
+  if(fp)
     {
       c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
       fclose(fp);
@@ -135,18 +157,32 @@ cp
 
   if(c->rsa_key)
     return 0;
-  else
+
+  /* Try again with PEM_read_RSA_PUBKEY. */
+
+  asprintf(&fname, "%s/hosts/%s", confbase, c->name);
+  fp = fopen(fname, "r");
+
+  if(fp)
     {
-      syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
-      return -1;
+      c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
+      fclose(fp);
     }
+
+  free(fname);
+
+  if(c->rsa_key)
+    return 0;
+
+  syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
+  return -1;
 }
 
 int read_rsa_private_key(void)
 {
   FILE *fp;
   char *fname, *key;
-cp
+  cp();
   if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
     {
       myself->connection->rsa_key = RSA_new();
@@ -161,7 +197,8 @@ cp
 
   if(is_safe_path(fname))
     {
-      if((fp = fopen(fname, "r")) == NULL)
+      fp = fopen(fname, "r");
+      if(!fp)
         {
           syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
                  fname, strerror(errno));
@@ -192,9 +229,10 @@ int setup_myself(void)
   config_t *cfg;
   subnet_t *subnet;
   char *name, *hostname, *mode, *afname, *cipher, *digest;
+  char *address = NULL;
   struct addrinfo hint, *ai, *aip;
   int choice, err;
-cp
+  cp();
   myself = new_node();
   myself->connection = new_connection();
   init_configuration(&myself->connection->config_tree);
@@ -221,7 +259,7 @@ cp
   myself->name = name;
   myself->connection->name = xstrdup(name);
 
-cp
+  cp();
   if(read_rsa_private_key())
     return -1;
 
@@ -233,7 +271,7 @@ cp
 
   if(read_rsa_public_key(myself->connection))
     return -1;
-cp
+  cp();
 
   if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
     asprintf(&myport, "655");
@@ -252,7 +290,7 @@ cp
       cfg = lookup_config_next(myself->connection->config_tree, cfg);
     }
 
-cp
+  cp();
   /* Check some options */
 
   if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
@@ -331,7 +369,7 @@ cp
     addressfamily = AF_INET;
 
   get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
-cp
+  cp();
   /* Generate packet encryption key */
 
   if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
@@ -342,7 +380,9 @@ cp
         }
       else
         {
-          if(!(myself->cipher = EVP_get_cipherbyname(cipher)))
+          myself->cipher = EVP_get_cipherbyname(cipher);
+
+          if(!myself->cipher)
             {
               syslog(LOG_ERR, _("Unrecognized cipher type!"));
               return -1;
@@ -377,7 +417,9 @@ cp
         }
       else
         {
-          if(!(myself->digest = EVP_get_digestbyname(digest)))
+          myself->digest = EVP_get_digestbyname(digest);
+
+          if(!myself->digest)
             {
               syslog(LOG_ERR, _("Unrecognized digest type!"));
               return -1;
@@ -424,7 +466,7 @@ cp
     myself->compression = 0;
 
   myself->connection->outcompression = 0;
-cp
+  cp();
   /* Done */
 
   myself->nexthop = myself;
@@ -435,28 +477,38 @@ cp
 
   graph();
 
-cp
+  cp();
   /* Open sockets */
   
   memset(&hint, 0, sizeof(hint));
   
+  get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
+
   hint.ai_family = addressfamily;
   hint.ai_socktype = SOCK_STREAM;
   hint.ai_protocol = IPPROTO_TCP;
   hint.ai_flags = AI_PASSIVE;
 
-  if((err = getaddrinfo(NULL, myport, &hint, &ai)) || !ai)
+  err = getaddrinfo(address, myport, &hint, &ai);
+
+  if(err || !ai)
     {
       syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err));
       return -1;
     }
 
+  listen_sockets = 0;
+
   for(aip = ai; aip; aip = aip->ai_next)
     {
-      if((listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0)
+      listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr);
+
+      if(listen_socket[listen_sockets].tcp < 0)
         continue;
 
-      if((listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0)
+      listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr);
+     if(listen_socket[listen_sockets].udp < 0)
         continue;
 
       if(debug_lvl >= DEBUG_CONNECTIONS)
@@ -479,7 +531,7 @@ cp
       syslog(LOG_ERR, _("Unable to create any listening socket!"));
       return -1;
     }
-cp
+  cp();
   return 0;
 }
 
@@ -488,7 +540,9 @@ cp
 */
 int setup_network_connections(void)
 {
-cp
+  char *envp[4];
+  int i;
+  cp();
   now = time(NULL);
 
   init_connections();
@@ -512,13 +566,21 @@ cp
     return -1;
 
   /* Run tinc-up script to further initialize the tap interface */
-  execute_script("tinc-up");
+  asprintf(&envp[0], "NETNAME=%s", netname?:"");
+  asprintf(&envp[1], "DEVICE=%s", device?:"");
+  asprintf(&envp[2], "INTERFACE=%s", interface?:"");
+  envp[3] = NULL;
+
+  execute_script("tinc-up", envp);
+
+  for(i = 0; i < 4; i++)
+    free(envp[i]);
 
   if(setup_myself() < 0)
     return -1;
 
   try_outgoing_connections();
-cp
+  cp();
   return 0;
 }
 
@@ -529,8 +591,9 @@ void close_network_connections(void)
 {
   avl_node_t *node, *next;
   connection_t *c;
+  char *envp[4];
   int i;
-cp
+  cp();
   for(node = connection_tree->head; node; node = next)
     {
       next = node->next;
@@ -556,9 +619,17 @@ cp
   exit_nodes();
   exit_connections();
 
-  execute_script("tinc-down");
+  asprintf(&envp[0], "NETNAME=%s", netname?:"");
+  asprintf(&envp[1], "DEVICE=%s", device?:"");
+  asprintf(&envp[2], "INTERFACE=%s", interface?:"");
+  envp[3] = NULL;
+
+  execute_script("tinc-down", envp);
+
+  for(i = 0; i < 4; i++)
+    free(envp[i]);
 
   close_device();
-cp
+  cp();
   return;
 }