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.21 by pcg, Sat Jan 17 01:18:36 2004 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-2004 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);
227 }
228 else if (!strcmp (var, "ip-proto"))
229 ip_proto = atoi (val);
230 else if (!strcmp (var, "icmp-type"))
231 {
232#if ENABLE_ICMP
233 icmp_type = atoi (val);
234#endif
170 } 235 }
171 236
172 // per config 237 // per config
173 else if (!strcmp (var, "node")) 238 else if (!strcmp (var, "node"))
174 { 239 {
218 } 283 }
219 else if (!strcmp (var, "private-key")) 284 else if (!strcmp (var, "private-key"))
220 prikeyfile = strdup (val); 285 prikeyfile = strdup (val);
221 else if (!strcmp (var, "ifpersist")) 286 else if (!strcmp (var, "ifpersist"))
222 { 287 {
223 if (!strcmp (val, "yes")) 288 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 } 289 }
232 else if (!strcmp (var, "ifname")) 290 else if (!strcmp (var, "ifname"))
233 ifname = strdup (val); 291 ifname = strdup (val);
234 else if (!strcmp (var, "rekey")) 292 else if (!strcmp (var, "rekey"))
235 rekey = atoi (val); 293 rekey = atoi (val);
241 script_if_up = strdup (val); 299 script_if_up = strdup (val);
242 else if (!strcmp (var, "node-up")) 300 else if (!strcmp (var, "node-up"))
243 script_node_up = strdup (val); 301 script_node_up = strdup (val);
244 else if (!strcmp (var, "node-down")) 302 else if (!strcmp (var, "node-down"))
245 script_node_down = strdup (val); 303 script_node_down = strdup (val);
304 else if (!strcmp (var, "http-proxy-host"))
305 {
306#if ENABLE_HTTP_PROXY
307 proxy_host = strdup (val);
308#endif
309 }
310 else if (!strcmp (var, "http-proxy-port"))
311 {
312#if ENABLE_HTTP_PROXY
313 proxy_port = atoi (val);
314#endif
315 }
316 else if (!strcmp (var, "http-proxy-auth"))
317 {
318#if ENABLE_HTTP_PROXY
319 proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val));
320#endif
321 }
246 322
247 /* node-specific, non-defaultable */ 323 /* node-specific, non-defaultable */
248 else if (node != &default_node && !strcmp (var, "hostname")) 324 else if (node != &default_node && !strcmp (var, "hostname"))
249 { 325 {
250 free (node->hostname); 326 free (node->hostname);
251 node->hostname = strdup (val); 327 node->hostname = strdup (val);
252 } 328 }
253 329
254 /* node-specific, defaultable */ 330 /* node-specific, defaultable */
255 else if (!strcmp (var, "port")) 331 else if (!strcmp (var, "udp-port"))
256 node->port = atoi (val); 332 node->udp_port = atoi (val);
333 else if (!strcmp (var, "tcp-port"))
334 node->tcp_port = atoi (val);
257 else if (!strcmp (var, "router-priority")) 335 else if (!strcmp (var, "router-priority"))
258 node->routerprio = atoi (val); 336 node->routerprio = atoi (val);
259 else if (!strcmp (var, "connect")) 337 else if (!strcmp (var, "connect"))
260 { 338 {
261 if (!strcmp (val, "ondemand")) 339 if (!strcmp (val, "ondemand"))
262 node->connectmode = conf_node::C_ONDEMAND; 340 node->connectmode = conf_node::C_ONDEMAND;
263 else if (!strcmp (val, "never")) 341 else if (!strcmp (val, "never"))
264 node->connectmode = conf_node::C_NEVER; 342 node->connectmode = conf_node::C_NEVER;
265 else if (!strcmp (val, "always")) 343 else if (!strcmp (val, "always"))
266 node->connectmode = conf_node::C_ALWAYS; 344 node->connectmode = conf_node::C_ALWAYS;
345 else if (!strcmp (val, "disabled"))
346 node->connectmode = conf_node::C_DISABLED;
267 else 347 else
268 slog (L_WARN, 348 slog (L_WARN,
269 _("illegal value for 'connectmode', use one of 'ondemand', 'never' or 'always', at '%s' line %d"), 349 _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"),
270 var, fname, lineno); 350 var, fname, lineno);
271 } 351 }
352 else if (!strcmp (var, "inherit-tos"))
353 {
354 parse_bool (node->inherit_tos, "inherit-tos", true, false);
355 }
272 else if (!strcmp (var, "compress")) 356 else if (!strcmp (var, "compress"))
273 { 357 {
274 if (!strcmp (val, "yes")) 358 parse_bool (node->compress, "compress", true, false);
275 node->compress = true;
276 else if (!strcmp (val, "no"))
277 node->compress = false;
278 else 359 }
279 slog (L_WARN, 360 // 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"), 361 else if (!strcmp (var, "enable-tcp"))
281 var, fname, lineno); 362 {
363#if ENABLE_TCP
364 u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v;
365#endif
366 }
367 else if (!strcmp (var, "enable-icmp"))
368 {
369#if ENABLE_ICMP
370 u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v;
371#endif
372 }
373 else if (!strcmp (var, "enable-udp"))
374 {
375 u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v;
376 }
377 else if (!strcmp (var, "enable-rawip"))
378 {
379 u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v;
282 } 380 }
283 381
284 // unknown or misplaced 382 // unknown or misplaced
285 else 383 else
286 {
287 slog (L_WARN, 384 slog (L_WARN,
288 _("unknown or misplaced variable `%s', at '%s' line %d"), 385 _("unknown or misplaced variable `%s', at '%s' line %d"),
289 var, fname, lineno); 386 var, fname, lineno);
290 }
291 } 387 }
292 388
293 fclose (f); 389 fclose (f);
294 } 390 }
295 else 391 else
354 printf (_("MTU: %d\n"), mtu); 450 printf (_("MTU: %d\n"), mtu);
355 printf (_("rekeying interval: %d\n"), rekey); 451 printf (_("rekeying interval: %d\n"), rekey);
356 printf (_("keepalive interval: %d\n"), keepalive); 452 printf (_("keepalive interval: %d\n"), keepalive);
357 printf (_("interface: %s\n"), ifname); 453 printf (_("interface: %s\n"), ifname);
358 printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); 454 printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>");
359 printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); 455 printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1);
360 printf ("\n"); 456 printf ("\n");
361 457
362 printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", 458 printf ("%4s %-17s %s %-8.8s %-10.10s %s\n",
363 _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); 459 _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port"));
364 460
366 (*i)->print (); 462 (*i)->print ();
367 463
368 printf ("\n"); 464 printf ("\n");
369} 465}
370 466
371void 467configuration::configuration ()
372conf_node::print ()
373{ 468{
374 printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", 469 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} 470}
387 471
472configuration::~configuration ()
473{
474 cleanup ();
475}
476
477

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines