--- gvpe/src/conf.C 2003/04/02 05:14:59 1.9 +++ gvpe/src/conf.C 2005/03/20 02:34:16 1.36 @@ -1,12 +1,10 @@ /* conf.c -- configuration code - Copyright (C) 1998 Robert van der Meulen - 1998-2002 Ivo Timmermans - 2000-2002 Guus Sliepen - 2000 Cris van Pelt - 2003 Marc Lehmann + Copyright (C) 2003-2005 Marc Lehmann - This program is free software; you can redistribute it and/or modify + This file is part of GVPE. + + GVPE is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -17,7 +15,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software + along with gvpe; if not, write to the Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -33,14 +31,13 @@ #include #include -#include +#include "netcompat.h" #include #include #include #include - -#include "gettext.h" +#include #include "conf.h" #include "slog.h" @@ -49,51 +46,88 @@ char *confbase; char *thisnode; char *identname; -char *pidfilename; struct configuration conf; u8 best_protocol (u8 protset) { - if (protset & PROT_IPv4 ) return PROT_IPv4; - if (protset & PROT_UDPv4) return PROT_UDPv4; - if (protset & PROT_TCPv4) return PROT_TCPv4; + if (protset & PROT_IPv4 ) return PROT_IPv4; + if (protset & PROT_ICMPv4) return PROT_ICMPv4; + if (protset & PROT_UDPv4 ) return PROT_UDPv4; + if (protset & PROT_TCPv4 ) return PROT_TCPv4; + if (protset & PROT_DNSv4 ) return PROT_DNSv4; return 0; } const char *strprotocol (u8 protocol) { - if (protocol & PROT_IPv4 ) return "rawip"; - if (protocol & PROT_UDPv4) return "udp"; + if (protocol & PROT_IPv4 ) return "rawip"; + if (protocol & PROT_ICMPv4) return "icmp"; + if (protocol & PROT_UDPv4 ) return "udp"; + if (protocol & PROT_TCPv4 ) return "tcp"; + if (protocol & PROT_DNSv4 ) return "dns"; return ""; } -configuration::configuration () +void +conf_node::print () { - init (); + printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", + id, + id >> 8, id & 0xff, + compress ? 'Y' : 'N', + connectmode == C_ONDEMAND ? "ondemand" : + connectmode == C_NEVER ? "never" : + connectmode == C_ALWAYS ? "always" : "", + nodename, + hostname ? hostname : "", + hostname ? ":" : "", + hostname ? udp_port : 0 + ); } -configuration::~configuration () +conf_node::~conf_node () { - cleanup (); + if (rsa_key) + RSA_free (rsa_key); + + free (nodename); + free (hostname); +#if ENABLE_DNS + free (domain); + free (dns_hostname); +#endif } void configuration::init () { memset (this, 0, sizeof (*this)); + mtu = DEFAULT_MTU; rekey = DEFAULT_REKEY; keepalive = DEFAULT_KEEPALIVE; llevel = L_INFO; ip_proto = IPPROTO_GRE; +#if ENABLE_ICMP + icmp_type = ICMP_ECHOREPLY; +#endif default_node.udp_port = DEFAULT_UDPPORT; - default_node.tcp_port = DEFAULT_UDPPORT; + default_node.tcp_port = DEFAULT_UDPPORT; // ehrm default_node.connectmode = conf_node::C_ALWAYS; default_node.compress = true; - default_node.protocols = PROT_UDPv4; + default_node.protocols = 0; + default_node.max_retry = DEFAULT_MAX_RETRY; + +#if ENABLE_DNS + default_node.dns_port = 0; // default is 0 == client + dns_forw_host = strdup ("127.0.0.1"); + dns_forw_port = 53; +#endif + + conf.pidfilename = strdup (LOCALSTATEDIR "/run/gvpe.pid"); } void configuration::cleanup() @@ -101,10 +135,17 @@ if (rsa_key) RSA_free (rsa_key); - free (ifname); - rsa_key = 0; - ifname = 0; + + free (pidfilename); pidfilename = 0; + free (ifname); ifname = 0; +#if ENABLE_HTTP_PROXY + free (proxy_host); proxy_host = 0; + free (proxy_auth); proxy_auth = 0; +#endif +#if ENABLE_DNS + free (dns_forw_host); dns_forw_host = 0; +#endif } void @@ -138,7 +179,7 @@ clear_config (); - asprintf (&fname, "%s/vped.conf", confbase); + asprintf (&fname, "%s/gvpe.conf", confbase); f = fopen (fname, "r"); if (f) @@ -178,9 +219,9 @@ if (!val || val[0] == '#') { slog (L_WARN, - _("no value for variable `%s', at '%s' line %d"), + _("no value for variable `%s', at '%s' line %d, skipping."), var, fname, lineno); - break; + continue; } if (!strcmp (var, "on")) @@ -205,6 +246,12 @@ } else if (!strcmp (var, "ip-proto")) ip_proto = atoi (val); + else if (!strcmp (var, "icmp-type")) + { +#if ENABLE_ICMP + icmp_type = atoi (val); +#endif + } // per config else if (!strcmp (var, "node")) @@ -232,10 +279,10 @@ { ERR_load_RSA_strings (); ERR_load_PEM_strings (); slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); - exit (1); + exit (EXIT_FAILURE); } - RSA_blinding_on (node->rsa_key, 0); + require (RSA_blinding_on (node->rsa_key, 0)); fclose (f); } @@ -244,23 +291,21 @@ slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno)); if (need_keys) - exit (1); + exit (EXIT_FAILURE); } free (fname); } - if (!::thisnode || !strcmp (node->nodename, ::thisnode)) + if (::thisnode && !strcmp (node->nodename, ::thisnode)) thisnode = node; } else if (!strcmp (var, "private-key")) - prikeyfile = strdup (val); + free (prikeyfile), prikeyfile = strdup (val); else if (!strcmp (var, "ifpersist")) - { - parse_bool (ifpersist, "ifpersist", true, false); - } + parse_bool (ifpersist, "ifpersist", true, false); else if (!strcmp (var, "ifname")) - ifname = strdup (val); + free (ifname), ifname = strdup (val); else if (!strcmp (var, "rekey")) rekey = atoi (val); else if (!strcmp (var, "keepalive")) @@ -268,26 +313,75 @@ else if (!strcmp (var, "mtu")) mtu = atoi (val); else if (!strcmp (var, "if-up")) - script_if_up = strdup (val); + free (script_if_up), script_if_up = strdup (val); else if (!strcmp (var, "node-up")) - script_node_up = strdup (val); + free (script_node_up), script_node_up = strdup (val); else if (!strcmp (var, "node-down")) - script_node_down = strdup (val); + free (script_node_down), script_node_down = strdup (val); + else if (!strcmp (var, "pid-file")) + free (pidfilename), pidfilename = strdup (val); + else if (!strcmp (var, "dns-forw-host")) + { +#if ENABLE_DNS + free (dns_forw_host), dns_forw_host = strdup (val); +#endif + } + else if (!strcmp (var, "dns-forw-port")) + { +#if ENABLE_DNS + dns_forw_port = atoi (val); +#endif + } + else if (!strcmp (var, "http-proxy-host")) + { +#if ENABLE_HTTP_PROXY + free (proxy_host), proxy_host = strdup (val); +#endif + } + else if (!strcmp (var, "http-proxy-port")) + { +#if ENABLE_HTTP_PROXY + proxy_port = atoi (val); +#endif + } + else if (!strcmp (var, "http-proxy-auth")) + { +#if ENABLE_HTTP_PROXY + proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); +#endif + } /* node-specific, non-defaultable */ else if (node != &default_node && !strcmp (var, "hostname")) - { - free (node->hostname); - node->hostname = strdup (val); - } + free (node->hostname), node->hostname = strdup (val); /* node-specific, defaultable */ else if (!strcmp (var, "udp-port")) node->udp_port = atoi (val); else if (!strcmp (var, "tcp-port")) node->tcp_port = atoi (val); + else if (!strcmp (var, "dns-hostname")) + { +#if ENABLE_DNS + free (node->dns_hostname), node->dns_hostname = strdup (val); +#endif + } + else if (!strcmp (var, "dns-port")) + { +#if ENABLE_DNS + node->dns_port = atoi (val); +#endif + } + else if (!strcmp (var, "dns-domain")) + { +#if ENABLE_DNS + free (node->domain), node->domain = strdup (val); +#endif + } else if (!strcmp (var, "router-priority")) node->routerprio = atoi (val); + else if (!strcmp (var, "max-retry")) + node->max_retry = atoi (val); else if (!strcmp (var, "connect")) { if (!strcmp (val, "ondemand")) @@ -300,21 +394,31 @@ node->connectmode = conf_node::C_DISABLED; else slog (L_WARN, - _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), - var, fname, lineno); + _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d, ignoring."), + var, fname, lineno); } else if (!strcmp (var, "inherit-tos")) - { - parse_bool (node->inherit_tos, "inherit-tos", true, false); - } + parse_bool (node->inherit_tos, "inherit-tos", true, false); else if (!strcmp (var, "compress")) - { - parse_bool (node->compress, "compress", true, false); - } + parse_bool (node->compress, "compress", true, false); // all these bool options really really cost a lot of executable size! else if (!strcmp (var, "enable-tcp")) { +#if ENABLE_TCP u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; +#endif + } + else if (!strcmp (var, "enable-icmp")) + { +#if ENABLE_ICMP + u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; +#endif + } + else if (!strcmp (var, "enable-dns")) + { +#if ENABLE_DNS + u8 v; parse_bool (v, "enable-dns" , PROT_DNSv4, 0); node->protocols = (node->protocols & ~PROT_DNSv4) | v; +#endif } else if (!strcmp (var, "enable-udp")) { @@ -327,11 +431,9 @@ // unknown or misplaced else - { - slog (L_WARN, - _("unknown or misplaced variable `%s', at '%s' line %d"), - var, fname, lineno); - } + slog (L_WARN, + _("unknown or misplaced variable `%s', at '%s' line %d, skipping."), + var, fname, lineno); } fclose (f); @@ -339,7 +441,7 @@ else { slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); - exit (1); + exit (EXIT_FAILURE); } free (fname); @@ -355,10 +457,10 @@ { ERR_load_RSA_strings (); ERR_load_PEM_strings (); slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); - exit (1); + exit (EXIT_FAILURE); } - RSA_blinding_on (rsa_key, 0); + require (RSA_blinding_on (rsa_key, 0)); fclose (f); } @@ -367,9 +469,18 @@ slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); if (need_keys) - exit (1); + exit (EXIT_FAILURE); } + if (need_keys && ::thisnode + && rsa_key && thisnode && thisnode->rsa_key) + if (BN_cmp (rsa_key->n, thisnode->rsa_key->n) != 0 + || BN_cmp (rsa_key->e, thisnode->rsa_key->e) != 0) + { + slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode); + exit (EXIT_FAILURE); + } + free (fname); } @@ -400,7 +511,7 @@ printf (_("keepalive interval: %d\n"), keepalive); printf (_("interface: %s\n"), ifname); printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : ""); - printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); + printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1); printf ("\n"); printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", @@ -412,20 +523,16 @@ printf ("\n"); } -void -conf_node::print () +configuration::configuration () { - printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", - id, - id >> 8, id & 0xff, - compress ? 'Y' : 'N', - connectmode == C_ONDEMAND ? "ondemand" : - connectmode == C_NEVER ? "never" : - connectmode == C_ALWAYS ? "always" : "", - nodename, - hostname ? hostname : "", - hostname ? ":" : "", - hostname ? udp_port : 0 - ); + asprintf (&confbase, "%s/gvpe", CONFDIR); + + init (); +} + +configuration::~configuration () +{ + cleanup (); } +