Configuration variables were still handled case sensitively.
[tinc] / src / conf.c
index eb13c42..b39f4b6 100644 (file)
@@ -1,8 +1,8 @@
 /*
     conf.c -- configuration code
     Copyright (C) 1998 Robert van der Meulen
-                  1998-2001 Ivo Timmermans <itimmermans@bigfoot.com>
-                  2000,2001 Guus Sliepen <guus@sliepen.warande.net>
+                  1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
+                  2000-2002 Guus Sliepen <guus@sliepen.warande.net>
                  2000 Cris van Pelt <tribbel@arise.dhs.org>
 
     This program is free software; you can redistribute it and/or modify
@@ -19,7 +19,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: conf.c,v 1.9.4.49 2001/11/16 12:17:03 zarq Exp $
+    $Id: conf.c,v 1.9.4.54 2002/03/24 17:14:01 guus Exp $
 */
 
 #include "config.h"
 #include <avl_tree.h>
 
 #include "conf.h"
+#include "netutl.h" /* for str2address */
 
 #include "system.h"
 
 avl_tree_t *config_tree;
 
 int debug_lvl = 0;
-int timeout = 0; /* seconds before timeout */
+int pingtimeout = 0;             /* seconds before timeout */
 char *confbase = NULL;           /* directory in which all config files are */
 char *netname = NULL;            /* name of the vpn network */
 
-/* Will be set if HUP signal is received. It will be processed when it is safe. */
-int sighup = 0;
-
 int config_compare(config_t *a, config_t *b)
 {
   int result;
-  
-  result = strcmp(a->variable, b->variable);
-  
+
+  result = strcasecmp(a->variable, b->variable);
+
   if(result)
     return result;
 
   result = a->line - b->line;
-  
+
   if(result)
     return result;
   else
@@ -92,7 +90,7 @@ config_t *new_config(void)
   config_t *cfg;
 cp
   cfg = (config_t *)xmalloc_and_zero(sizeof(*cfg));
-  
+
   return cfg;
 }
 
@@ -128,8 +126,8 @@ cp
 
   if(!found)
     return NULL;
-  
-  if(strcmp(found->variable, variable))
+
+  if(strcasecmp(found->variable, variable))
     return NULL;
 
   return found;
@@ -141,20 +139,20 @@ config_t *lookup_config_next(avl_tree_t *config_tree, config_t *cfg)
   config_t *found;
 cp
   node = avl_search_node(config_tree, cfg);
-  
+
   if(node)
     {
       if(node->next)
         {
           found = (config_t *)node->next->data;
-          if(!strcmp(found->variable, cfg->variable))
+          if(!strcasecmp(found->variable, cfg->variable))
             return found;
         }
     }
-  
+
   return NULL;
 }
-  
+
 int get_config_bool(config_t *cfg, int *result)
 {
 cp
@@ -166,7 +164,7 @@ cp
       *result = 1;
       return 1;
     }
-  else if(!strcasecmp(cfg->value, "np"))
+  else if(!strcasecmp(cfg->value, "no"))
     {
       *result = 0;
       return 1;
@@ -186,7 +184,7 @@ cp
 
   if(sscanf(cfg->value, "%d", result) == 1)
     return 1;
-    
+
   syslog(LOG_ERR, _("Integer expected for configuration variable %s in %s line %d"),
          cfg->variable, cfg->file, cfg->line);
   return 0;
@@ -198,46 +196,76 @@ cp
   if(!cfg)
     return 0;
 
-  *result = cfg->value;
+  *result = xstrdup(cfg->value);
   return 1;
 }
 
+int get_config_address(config_t *cfg, struct addrinfo **result)
+{
+  struct addrinfo *ai;
+cp
+  if(!cfg)
+    return 0;
+
+  ai = str2addrinfo(cfg->value, NULL, 0);
+
+  if(ai)
+    {
+      *result = ai;
+      return 1;
+    }
+
+  syslog(LOG_ERR, _("Hostname or IP address expected for configuration variable %s in %s line %d"),
+         cfg->variable, cfg->file, cfg->line);
+  return 0;
+}
+
+int get_config_port(config_t *cfg, port_t *result)
+{
+cp
+  if(!cfg)
+    return 0;
+
+  if(sscanf(cfg->value, "%hu", result) == 1)
+    {
+      *result = htons(*result);
+      return 1;
+    }
+
+  syslog(LOG_ERR, _("Port number expected for configuration variable %s in %s line %d"),
+         cfg->variable, cfg->file, cfg->line);
+  return 0;
+}
+
 int get_config_subnet(config_t *cfg, subnet_t **result)
 {
-  ip_mask_t *ip;
   subnet_t *subnet;
 cp
   if(!cfg)
     return 0;
 
-  ip = strtoip(cfg->value);
+  subnet = str2net(cfg->value);
 
-  if(!ip)
+  if(!subnet)
     {
-      syslog(LOG_ERR, _("IP address expected for configuration variable %s in %s line %d"),
+      syslog(LOG_ERR, _("Subnet expected for configuration variable %s in %s line %d"),
              cfg->variable, cfg->file, cfg->line);
       return 0;
     }
-  
+
   /* Teach newbies what subnets are... */
 
-  if((ip->address & ip->mask) != ip->address)
+  if(((subnet->type == SUBNET_IPV4) && maskcheck((char *)&subnet->net.ipv4.address, subnet->net.ipv4.masklength, sizeof(ipv4_t)))
+     || ((subnet->type == SUBNET_IPV6) && maskcheck((char *)&subnet->net.ipv6.address, subnet->net.ipv6.masklength, sizeof(ipv6_t))))
     {
-      syslog(LOG_ERR, _("Network address and subnet mask for configuration variable %s in %s line %d"),
+      syslog(LOG_ERR, _("Network address and mask length do not match for configuration variable %s in %s line %d"),
              cfg->variable, cfg->file, cfg->line);
-      free(ip);
+      free(subnet);
       return 0;
     }
 
-  subnet = new_subnet();
-  subnet->type = SUBNET_IPV4;
-  subnet->net.ipv4.address = ip->address;
-  subnet->net.ipv4.mask = ip->mask;
-  
-  free(ip);
-
   *result = subnet;
-  
+
   return 1;
 }
 
@@ -245,7 +273,7 @@ cp
   Read exactly one line and strip the trailing newline if any.  If the
   file was on EOF, return NULL. Otherwise, return all the data in a
   dynamically allocated buffer.
-  
+
   If line is non-NULL, it will be used as an initial buffer, to avoid
   unnecessary mallocing each time this function is called.  If buf is
   given, and buf needs to be expanded, the var pointed to by buflen
@@ -334,17 +362,17 @@ int read_config_file(avl_tree_t *config_tree, const char *fname)
   int lineno = 0, ignore = 0;
   config_t *cfg;
   size_t bufsize;
-  
+
 cp
   if((fp = fopen (fname, "r")) == NULL)
     {
-      syslog(LOG_ERR, _("Cannot open config file %s: %m"), fname);
+      syslog(LOG_ERR, _("Cannot open config file %s: %s"), fname, strerror(errno));
       return -3;
     }
 
   bufsize = 100;
   buffer = xmalloc(bufsize);
-  
+
   for(;;)
     {
       if((line = readline(fp, &buffer, &bufsize)) == NULL)
@@ -369,7 +397,7 @@ cp
 
       if(!strcmp(variable, "-----BEGIN"))
         ignore = 1;
-        
+
       if(!ignore)
         {
           if(((value = strtok(NULL, "\t\n\r =")) == NULL) || value[0] == '#')
@@ -407,12 +435,11 @@ cp
   x = read_config_file(config_tree, fname);
   if(x == -1) /* System error: complain */
     {
-      syslog(LOG_ERR, _("Failed to read `%s': %m"),
-             fname);
+      syslog(LOG_ERR, _("Failed to read `%s': %s"), fname, strerror(errno));
     }
   free(fname);
 cp
-  return x;  
+  return x;
 }
 
 int isadir(const char* f)
@@ -440,10 +467,10 @@ int is_safe_path(const char *file)
     }
 
   p = strrchr(file, '/');
-  
+
   if(p == file)                /* It's in the root */
     p++;
-    
+
   x = *p;
   *p = '\0';
 
@@ -451,8 +478,7 @@ int is_safe_path(const char *file)
 check1:
   if(lstat(f, &s) < 0)
     {
-      syslog(LOG_ERR, _("Couldn't stat `%s': %m"),
-             f);
+      syslog(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno));
       return 0;
     }
 
@@ -470,25 +496,24 @@ check1:
 
       if(readlink(f, l, MAXBUFSIZE) < 0)
         {
-          syslog(LOG_ERR, _("Unable to read symbolic link `%s': %m"), f);
+          syslog(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f, strerror(errno));
           return 0;
         }
-      
+
       f = l;
       goto check1;
     }
 
   *p = x;
   f = file;
-  
+
 check2:
   if(lstat(f, &s) < 0 && errno != ENOENT)
     {
-      syslog(LOG_ERR, _("Couldn't stat `%s': %m"),
-             f);
+      syslog(LOG_ERR, _("Couldn't stat `%s': %s"), f, strerror(errno));
       return 0;
     }
-    
+
   if(errno == ENOENT)
     return 1;
 
@@ -506,10 +531,10 @@ check2:
 
       if(readlink(f, l, MAXBUFSIZE) < 0)
         {
-          syslog(LOG_ERR, _("Unable to read symbolic link `%s': %m"), f);
+          syslog(LOG_ERR, _("Unable to read symbolic link `%s': %s"), f, strerror(errno));
           return 0;
         }
-      
+
       f = l;
       goto check2;
     }
@@ -521,7 +546,7 @@ check2:
              f);
       return 0;
     }
-  
+
   return 1;
 }
 
@@ -561,7 +586,7 @@ FILE *ask_and_safe_open(const char* filename, const char* what, const char* mode
     {
       /* The directory is a relative path or a filename. */
       char *p;
-      
+
       directory = get_current_dir_name();
       asprintf(&p, "%s/%s", directory, fn);
       free(fn);
@@ -570,7 +595,7 @@ FILE *ask_and_safe_open(const char* filename, const char* what, const char* mode
     }
 
   umask(0077); /* Disallow everything for group and other */
-  
+
   /* Open it first to keep the inode busy */
   if((r = fopen(fn, mode)) == NULL)
     {
@@ -579,7 +604,7 @@ FILE *ask_and_safe_open(const char* filename, const char* what, const char* mode
       free(fn);
       return NULL;
     }
-    
+
   /* Then check the file for nasty attacks */
   if(!is_safe_path(fn))  /* Do not permit any directories that are
                             readable or writeable by other users. */