ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/conf.C
(Generate patch)

Comparing gvpe/src/conf.C (file contents):
Revision 1.1 by pcg, Sat Mar 1 15:53:03 2003 UTC vs.
Revision 1.19 by pcg, Thu Oct 16 20:35:14 2003 UTC

1/* 1/*
2 conf.c -- configuration code 2 conf.c -- configuration code
3 Copyright (C) 1998 Robert van der Meulen 3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
4 1998-2002 Ivo Timmermans <ivo@o2w.nl>
5 2000-2002 Guus Sliepen <guus@sliepen.eu.org>
6 2000 Cris van Pelt <tribbel@arise.dhs.org>
7 2003 Marc Lehmann <pcg@goof.com>
8 4
9 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version. 8 (at your option) any later version.
31#include <netdb.h> 27#include <netdb.h>
32#include <sys/stat.h> 28#include <sys/stat.h>
33#include <sys/types.h> 29#include <sys/types.h>
34#include <unistd.h> 30#include <unistd.h>
35 31
32#include "netcompat.h"
33
36#include <openssl/err.h> 34#include <openssl/err.h>
37#include <openssl/pem.h> 35#include <openssl/pem.h>
38#include <openssl/rsa.h> 36#include <openssl/rsa.h>
39#include <openssl/rand.h> 37#include <openssl/rand.h>
40 38
49char *identname; 47char *identname;
50char *pidfilename; 48char *pidfilename;
51 49
52struct configuration conf; 50struct configuration conf;
53 51
54configuration::configuration () 52u8 best_protocol (u8 protset)
55{ 53{
56 init (); 54 if (protset & PROT_IPv4 ) return PROT_IPv4;
57} 55 if (protset & PROT_ICMPv4) return PROT_ICMPv4;
56 if (protset & PROT_UDPv4 ) return PROT_UDPv4;
57 if (protset & PROT_TCPv4 ) return PROT_TCPv4;
58 58
59configuration::~configuration () 59 return 0;
60}
61
62const char *strprotocol (u8 protocol)
60{ 63{
61 cleanup (); 64 if (protocol & PROT_IPv4 ) return "rawip";
65 if (protocol & PROT_ICMPv4) return "icmp";
66 if (protocol & PROT_UDPv4 ) return "udp";
67 if (protocol & PROT_TCPv4 ) return "tcp";
68
69 return "<unknown>";
70}
71
72void
73conf_node::print ()
74{
75 printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n",
76 id,
77 id >> 8, id & 0xff,
78 compress ? 'Y' : 'N',
79 connectmode == C_ONDEMAND ? "ondemand" :
80 connectmode == C_NEVER ? "never" :
81 connectmode == C_ALWAYS ? "always" : "",
82 nodename,
83 hostname ? hostname : "",
84 hostname ? ":" : "",
85 hostname ? udp_port : 0
86 );
87}
88
89conf_node::~conf_node ()
90{
91 if (rsa_key)
92 RSA_free (rsa_key);
93
94 free (nodename);
95 free (hostname);
62} 96}
63 97
64void configuration::init () 98void configuration::init ()
65{ 99{
66 memset (this, 0, sizeof (*this)); 100 memset (this, 0, sizeof (*this));
67 101
102 mtu = DEFAULT_MTU;
68 rekey = DEFAULT_REKEY; 103 rekey = DEFAULT_REKEY;
69 keepalive = DEFAULT_KEEPALIVE; 104 keepalive = DEFAULT_KEEPALIVE;
105 llevel = L_INFO;
106 ip_proto = IPPROTO_GRE;
107#if ENABLE_ICMP
108 icmp_type = ICMP_ECHOREPLY;
109#endif
70 110
71 default_node.port = DEFAULT_PORT; 111 default_node.udp_port = DEFAULT_UDPPORT;
112 default_node.tcp_port = DEFAULT_UDPPORT;
72 default_node.connectmode = conf_node::C_ALWAYS; 113 default_node.connectmode = conf_node::C_ALWAYS;
73 default_node.compress = true; 114 default_node.compress = true;
115 default_node.protocols = PROT_UDPv4;
74} 116}
75 117
76void configuration::cleanup() 118void configuration::cleanup()
77{ 119{
78 if (rsa_key) 120 if (rsa_key)
79 RSA_free (rsa_key); 121 RSA_free (rsa_key);
80 122
81 free (ifname);
82
83 rsa_key = 0; 123 rsa_key = 0;
84 ifname = 0; 124
125 free (ifname); ifname = 0;
126#if ENABLE_HTTP_PROXY
127 free (proxy_host); proxy_host = 0;
128 free (proxy_auth); proxy_auth = 0;
129#endif
85} 130}
86 131
87void 132void
88configuration::clear_config () 133configuration::clear_config ()
89{ 134{
93 nodes.clear (); 138 nodes.clear ();
94 139
95 cleanup (); 140 cleanup ();
96 init (); 141 init ();
97} 142}
143
144#define parse_bool(target,name,trueval,falseval) \
145 if (!strcmp (val, "yes")) target = trueval; \
146 else if (!strcmp (val, "no")) target = falseval; \
147 else if (!strcmp (val, "true")) target = trueval; \
148 else if (!strcmp (val, "false")) target = falseval; \
149 else if (!strcmp (val, "on")) target = trueval; \
150 else if (!strcmp (val, "off")) target = falseval; \
151 else \
152 slog (L_WARN, \
153 _("illegal value for '%s', only 'yes|true|on' or 'no|false|off' allowed, at '%s' line %d"), \
154 name, var, fname, lineno);
98 155
99void configuration::read_config (bool need_keys) 156void configuration::read_config (bool need_keys)
100{ 157{
101 char *fname; 158 char *fname;
102 FILE *f; 159 FILE *f;
162 if (!strcmp (var, "loglevel")) 219 if (!strcmp (var, "loglevel"))
163 { 220 {
164 loglevel l = string_to_loglevel (val); 221 loglevel l = string_to_loglevel (val);
165 222
166 if (l != L_NONE) 223 if (l != L_NONE)
167 set_loglevel (l); 224 llevel = l;
168 else 225 else
169 slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); 226 slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line);
170 } 227 }
228 else if (!strcmp (var, "ip-proto"))
229 ip_proto = atoi (val);
230#if ENABLE_ICMP
231 //TODO: error message
232 else if (!strcmp (var, "icmp-type"))
233 icmp_type = atoi (val);
234#endif
171 235
172 // per config 236 // per config
173 else if (!strcmp (var, "node")) 237 else if (!strcmp (var, "node"))
174 { 238 {
175 default_node.id++; 239 default_node.id++;
218 } 282 }
219 else if (!strcmp (var, "private-key")) 283 else if (!strcmp (var, "private-key"))
220 prikeyfile = strdup (val); 284 prikeyfile = strdup (val);
221 else if (!strcmp (var, "ifpersist")) 285 else if (!strcmp (var, "ifpersist"))
222 { 286 {
223 if (!strcmp (val, "yes")) 287 parse_bool (ifpersist, "ifpersist", true, false);
224 ifpersist = true;
225 else if (!strcmp (val, "no"))
226 ifpersist = false;
227 else
228 slog (L_WARN,
229 _("illegal value for 'ifpersist', only 'yes' or 'no' allowed, at '%s' line %d"),
230 var, fname, lineno);
231 } 288 }
232 else if (!strcmp (var, "ifname")) 289 else if (!strcmp (var, "ifname"))
233 ifname = strdup (val); 290 ifname = strdup (val);
234 else if (!strcmp (var, "rekey")) 291 else if (!strcmp (var, "rekey"))
235 rekey = atoi (val); 292 rekey = atoi (val);
241 script_if_up = strdup (val); 298 script_if_up = strdup (val);
242 else if (!strcmp (var, "node-up")) 299 else if (!strcmp (var, "node-up"))
243 script_node_up = strdup (val); 300 script_node_up = strdup (val);
244 else if (!strcmp (var, "node-down")) 301 else if (!strcmp (var, "node-down"))
245 script_node_down = strdup (val); 302 script_node_down = strdup (val);
303#if ENABLE_HTTP_PROXY
304 else if (!strcmp (var, "http-proxy-host"))
305 proxy_host = strdup (val);
306 else if (!strcmp (var, "http-proxy-port"))
307 proxy_port = atoi (val);
308 else if (!strcmp (var, "http-proxy-auth"))
309 proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val));
310#endif
246 311
247 /* node-specific, non-defaultable */ 312 /* node-specific, non-defaultable */
248 else if (node != &default_node && !strcmp (var, "hostname")) 313 else if (node != &default_node && !strcmp (var, "hostname"))
249 { 314 {
250 free (node->hostname); 315 free (node->hostname);
251 node->hostname = strdup (val); 316 node->hostname = strdup (val);
252 } 317 }
253 318
254 /* node-specific, defaultable */ 319 /* node-specific, defaultable */
255 else if (!strcmp (var, "port")) 320 else if (!strcmp (var, "udp-port"))
256 node->port = atoi (val); 321 node->udp_port = atoi (val);
322 else if (!strcmp (var, "tcp-port"))
323 node->tcp_port = atoi (val);
257 else if (!strcmp (var, "router-priority")) 324 else if (!strcmp (var, "router-priority"))
258 node->routerprio = atoi (val); 325 node->routerprio = atoi (val);
259 else if (!strcmp (var, "connect")) 326 else if (!strcmp (var, "connect"))
260 { 327 {
261 if (!strcmp (val, "ondemand")) 328 if (!strcmp (val, "ondemand"))
262 node->connectmode = conf_node::C_ONDEMAND; 329 node->connectmode = conf_node::C_ONDEMAND;
263 else if (!strcmp (val, "never")) 330 else if (!strcmp (val, "never"))
264 node->connectmode = conf_node::C_NEVER; 331 node->connectmode = conf_node::C_NEVER;
265 else if (!strcmp (val, "always")) 332 else if (!strcmp (val, "always"))
266 node->connectmode = conf_node::C_ALWAYS; 333 node->connectmode = conf_node::C_ALWAYS;
334 else if (!strcmp (val, "disabled"))
335 node->connectmode = conf_node::C_DISABLED;
267 else 336 else
268 slog (L_WARN, 337 slog (L_WARN,
269 _("illegal value for 'connectmode', use one of 'ondemand', 'never' or 'always', at '%s' line %d"), 338 _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"),
270 var, fname, lineno); 339 var, fname, lineno);
271 } 340 }
341 else if (!strcmp (var, "inherit-tos"))
342 {
343 parse_bool (node->inherit_tos, "inherit-tos", true, false);
344 }
272 else if (!strcmp (var, "compress")) 345 else if (!strcmp (var, "compress"))
273 { 346 {
274 if (!strcmp (val, "yes")) 347 parse_bool (node->compress, "compress", true, false);
275 node->compress = true;
276 else if (!strcmp (val, "no"))
277 node->compress = false;
278 else 348 }
279 slog (L_WARN, 349 // all these bool options really really cost a lot of executable size!
280 _("illegal value for 'compress', only 'yes' or 'no' allowed, at '%s' line %d"), 350 else if (!strcmp (var, "enable-tcp"))
281 var, fname, lineno); 351 {
352#if ENABLE_TCP
353 u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v;
354#endif
355 }
356 else if (!strcmp (var, "enable-icmp"))
357 {
358#if ENABLE_ICMP
359 u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v;
360#endif
361 }
362 else if (!strcmp (var, "enable-udp"))
363 {
364 u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v;
365 }
366 else if (!strcmp (var, "enable-rawip"))
367 {
368 u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v;
282 } 369 }
283 370
284 // unknown or misplaced 371 // unknown or misplaced
285 else 372 else
286 {
287 slog (L_WARN, 373 slog (L_WARN,
288 _("unknown or misplaced variable `%s', at '%s' line %d"), 374 _("unknown or misplaced variable `%s', at '%s' line %d"),
289 var, fname, lineno); 375 var, fname, lineno);
290 }
291 } 376 }
292 377
293 fclose (f); 378 fclose (f);
294 } 379 }
295 else 380 else
354 printf (_("MTU: %d\n"), mtu); 439 printf (_("MTU: %d\n"), mtu);
355 printf (_("rekeying interval: %d\n"), rekey); 440 printf (_("rekeying interval: %d\n"), rekey);
356 printf (_("keepalive interval: %d\n"), keepalive); 441 printf (_("keepalive interval: %d\n"), keepalive);
357 printf (_("interface: %s\n"), ifname); 442 printf (_("interface: %s\n"), ifname);
358 printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); 443 printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>");
359 printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); 444 printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1);
360 printf ("\n"); 445 printf ("\n");
361 446
362 printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", 447 printf ("%4s %-17s %s %-8.8s %-10.10s %s\n",
363 _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); 448 _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port"));
364 449
366 (*i)->print (); 451 (*i)->print ();
367 452
368 printf ("\n"); 453 printf ("\n");
369} 454}
370 455
371void 456configuration::configuration ()
372conf_node::print ()
373{ 457{
374 printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", 458 init ();
375 id,
376 id >> 8, id & 0xff,
377 compress ? 'Y' : 'N',
378 connectmode == C_ONDEMAND ? "ondemand" :
379 connectmode == C_NEVER ? "never" :
380 connectmode == C_ALWAYS ? "always" : "",
381 nodename,
382 hostname ? hostname : "",
383 hostname ? ":" : "",
384 hostname ? port : 0
385 );
386} 459}
387 460
461configuration::~configuration ()
462{
463 cleanup ();
464}
465
466

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines