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.23 by pcg, Thu Jan 29 19:30:08 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>
38#include <openssl/bn.h>
40 39
41#include "gettext.h" 40#include "gettext.h"
42 41
43#include "conf.h" 42#include "conf.h"
44#include "slog.h" 43#include "slog.h"
49char *identname; 48char *identname;
50char *pidfilename; 49char *pidfilename;
51 50
52struct configuration conf; 51struct configuration conf;
53 52
54configuration::configuration () 53u8 best_protocol (u8 protset)
55{ 54{
56 init (); 55 if (protset & PROT_IPv4 ) return PROT_IPv4;
57} 56 if (protset & PROT_ICMPv4) return PROT_ICMPv4;
57 if (protset & PROT_UDPv4 ) return PROT_UDPv4;
58 if (protset & PROT_TCPv4 ) return PROT_TCPv4;
58 59
59configuration::~configuration () 60 return 0;
61}
62
63const char *strprotocol (u8 protocol)
60{ 64{
61 cleanup (); 65 if (protocol & PROT_IPv4 ) return "rawip";
66 if (protocol & PROT_ICMPv4) return "icmp";
67 if (protocol & PROT_UDPv4 ) return "udp";
68 if (protocol & PROT_TCPv4 ) return "tcp";
69
70 return "<unknown>";
71}
72
73void
74conf_node::print ()
75{
76 printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n",
77 id,
78 id >> 8, id & 0xff,
79 compress ? 'Y' : 'N',
80 connectmode == C_ONDEMAND ? "ondemand" :
81 connectmode == C_NEVER ? "never" :
82 connectmode == C_ALWAYS ? "always" : "",
83 nodename,
84 hostname ? hostname : "",
85 hostname ? ":" : "",
86 hostname ? udp_port : 0
87 );
88}
89
90conf_node::~conf_node ()
91{
92 if (rsa_key)
93 RSA_free (rsa_key);
94
95 free (nodename);
96 free (hostname);
62} 97}
63 98
64void configuration::init () 99void configuration::init ()
65{ 100{
66 memset (this, 0, sizeof (*this)); 101 memset (this, 0, sizeof (*this));
67 102
103 mtu = DEFAULT_MTU;
68 rekey = DEFAULT_REKEY; 104 rekey = DEFAULT_REKEY;
69 keepalive = DEFAULT_KEEPALIVE; 105 keepalive = DEFAULT_KEEPALIVE;
106 llevel = L_INFO;
107 ip_proto = IPPROTO_GRE;
108#if ENABLE_ICMP
109 icmp_type = ICMP_ECHOREPLY;
110#endif
70 111
71 default_node.port = DEFAULT_PORT; 112 default_node.udp_port = DEFAULT_UDPPORT;
113 default_node.tcp_port = DEFAULT_UDPPORT;
72 default_node.connectmode = conf_node::C_ALWAYS; 114 default_node.connectmode = conf_node::C_ALWAYS;
73 default_node.compress = true; 115 default_node.compress = true;
116 default_node.protocols = PROT_UDPv4;
74} 117}
75 118
76void configuration::cleanup() 119void configuration::cleanup()
77{ 120{
78 if (rsa_key) 121 if (rsa_key)
79 RSA_free (rsa_key); 122 RSA_free (rsa_key);
80 123
81 free (ifname);
82
83 rsa_key = 0; 124 rsa_key = 0;
84 ifname = 0; 125
126 free (ifname); ifname = 0;
127#if ENABLE_HTTP_PROXY
128 free (proxy_host); proxy_host = 0;
129 free (proxy_auth); proxy_auth = 0;
130#endif
85} 131}
86 132
87void 133void
88configuration::clear_config () 134configuration::clear_config ()
89{ 135{
93 nodes.clear (); 139 nodes.clear ();
94 140
95 cleanup (); 141 cleanup ();
96 init (); 142 init ();
97} 143}
144
145#define parse_bool(target,name,trueval,falseval) \
146 if (!strcmp (val, "yes")) target = trueval; \
147 else if (!strcmp (val, "no")) target = falseval; \
148 else if (!strcmp (val, "true")) target = trueval; \
149 else if (!strcmp (val, "false")) target = falseval; \
150 else if (!strcmp (val, "on")) target = trueval; \
151 else if (!strcmp (val, "off")) target = falseval; \
152 else \
153 slog (L_WARN, \
154 _("illegal value for '%s', only 'yes|true|on' or 'no|false|off' allowed, at '%s' line %d"), \
155 name, var, fname, lineno);
98 156
99void configuration::read_config (bool need_keys) 157void configuration::read_config (bool need_keys)
100{ 158{
101 char *fname; 159 char *fname;
102 FILE *f; 160 FILE *f;
162 if (!strcmp (var, "loglevel")) 220 if (!strcmp (var, "loglevel"))
163 { 221 {
164 loglevel l = string_to_loglevel (val); 222 loglevel l = string_to_loglevel (val);
165 223
166 if (l != L_NONE) 224 if (l != L_NONE)
167 set_loglevel (l); 225 llevel = l;
168 else 226 else
169 slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); 227 slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line);
228 }
229 else if (!strcmp (var, "ip-proto"))
230 ip_proto = atoi (val);
231 else if (!strcmp (var, "icmp-type"))
232 {
233#if ENABLE_ICMP
234 icmp_type = atoi (val);
235#endif
170 } 236 }
171 237
172 // per config 238 // per config
173 else if (!strcmp (var, "node")) 239 else if (!strcmp (var, "node"))
174 { 240 {
193 259
194 if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) 260 if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL))
195 { 261 {
196 ERR_load_RSA_strings (); ERR_load_PEM_strings (); 262 ERR_load_RSA_strings (); ERR_load_PEM_strings ();
197 slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); 263 slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
198 exit (1); 264 exit (EXIT_FAILURE);
199 } 265 }
200 266
201 RSA_blinding_on (node->rsa_key, 0); 267 require (RSA_blinding_on (node->rsa_key, 0));
202 268
203 fclose (f); 269 fclose (f);
204 } 270 }
205 else 271 else
206 { 272 {
207 slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno)); 273 slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno));
208 274
209 if (need_keys) 275 if (need_keys)
210 exit (1); 276 exit (EXIT_FAILURE);
211 } 277 }
212 278
213 free (fname); 279 free (fname);
214 } 280 }
215 281
216 if (!::thisnode || !strcmp (node->nodename, ::thisnode)) 282 if (::thisnode && !strcmp (node->nodename, ::thisnode))
217 thisnode = node; 283 thisnode = node;
218 } 284 }
219 else if (!strcmp (var, "private-key")) 285 else if (!strcmp (var, "private-key"))
220 prikeyfile = strdup (val); 286 prikeyfile = strdup (val);
221 else if (!strcmp (var, "ifpersist")) 287 else if (!strcmp (var, "ifpersist"))
222 { 288 {
223 if (!strcmp (val, "yes")) 289 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 } 290 }
232 else if (!strcmp (var, "ifname")) 291 else if (!strcmp (var, "ifname"))
233 ifname = strdup (val); 292 ifname = strdup (val);
234 else if (!strcmp (var, "rekey")) 293 else if (!strcmp (var, "rekey"))
235 rekey = atoi (val); 294 rekey = atoi (val);
241 script_if_up = strdup (val); 300 script_if_up = strdup (val);
242 else if (!strcmp (var, "node-up")) 301 else if (!strcmp (var, "node-up"))
243 script_node_up = strdup (val); 302 script_node_up = strdup (val);
244 else if (!strcmp (var, "node-down")) 303 else if (!strcmp (var, "node-down"))
245 script_node_down = strdup (val); 304 script_node_down = strdup (val);
305 else if (!strcmp (var, "http-proxy-host"))
306 {
307#if ENABLE_HTTP_PROXY
308 proxy_host = strdup (val);
309#endif
310 }
311 else if (!strcmp (var, "http-proxy-port"))
312 {
313#if ENABLE_HTTP_PROXY
314 proxy_port = atoi (val);
315#endif
316 }
317 else if (!strcmp (var, "http-proxy-auth"))
318 {
319#if ENABLE_HTTP_PROXY
320 proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val));
321#endif
322 }
246 323
247 /* node-specific, non-defaultable */ 324 /* node-specific, non-defaultable */
248 else if (node != &default_node && !strcmp (var, "hostname")) 325 else if (node != &default_node && !strcmp (var, "hostname"))
249 { 326 {
250 free (node->hostname); 327 free (node->hostname);
251 node->hostname = strdup (val); 328 node->hostname = strdup (val);
252 } 329 }
253 330
254 /* node-specific, defaultable */ 331 /* node-specific, defaultable */
255 else if (!strcmp (var, "port")) 332 else if (!strcmp (var, "udp-port"))
256 node->port = atoi (val); 333 node->udp_port = atoi (val);
334 else if (!strcmp (var, "tcp-port"))
335 node->tcp_port = atoi (val);
257 else if (!strcmp (var, "router-priority")) 336 else if (!strcmp (var, "router-priority"))
258 node->routerprio = atoi (val); 337 node->routerprio = atoi (val);
259 else if (!strcmp (var, "connect")) 338 else if (!strcmp (var, "connect"))
260 { 339 {
261 if (!strcmp (val, "ondemand")) 340 if (!strcmp (val, "ondemand"))
262 node->connectmode = conf_node::C_ONDEMAND; 341 node->connectmode = conf_node::C_ONDEMAND;
263 else if (!strcmp (val, "never")) 342 else if (!strcmp (val, "never"))
264 node->connectmode = conf_node::C_NEVER; 343 node->connectmode = conf_node::C_NEVER;
265 else if (!strcmp (val, "always")) 344 else if (!strcmp (val, "always"))
266 node->connectmode = conf_node::C_ALWAYS; 345 node->connectmode = conf_node::C_ALWAYS;
346 else if (!strcmp (val, "disabled"))
347 node->connectmode = conf_node::C_DISABLED;
267 else 348 else
268 slog (L_WARN, 349 slog (L_WARN,
269 _("illegal value for 'connectmode', use one of 'ondemand', 'never' or 'always', at '%s' line %d"), 350 _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"),
270 var, fname, lineno); 351 var, fname, lineno);
271 } 352 }
353 else if (!strcmp (var, "inherit-tos"))
354 {
355 parse_bool (node->inherit_tos, "inherit-tos", true, false);
356 }
272 else if (!strcmp (var, "compress")) 357 else if (!strcmp (var, "compress"))
273 { 358 {
274 if (!strcmp (val, "yes")) 359 parse_bool (node->compress, "compress", true, false);
275 node->compress = true;
276 else if (!strcmp (val, "no"))
277 node->compress = false;
278 else 360 }
279 slog (L_WARN, 361 // 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"), 362 else if (!strcmp (var, "enable-tcp"))
281 var, fname, lineno); 363 {
364#if ENABLE_TCP
365 u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v;
366#endif
367 }
368 else if (!strcmp (var, "enable-icmp"))
369 {
370#if ENABLE_ICMP
371 u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v;
372#endif
373 }
374 else if (!strcmp (var, "enable-udp"))
375 {
376 u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v;
377 }
378 else if (!strcmp (var, "enable-rawip"))
379 {
380 u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v;
282 } 381 }
283 382
284 // unknown or misplaced 383 // unknown or misplaced
285 else 384 else
286 {
287 slog (L_WARN, 385 slog (L_WARN,
288 _("unknown or misplaced variable `%s', at '%s' line %d"), 386 _("unknown or misplaced variable `%s', at '%s' line %d"),
289 var, fname, lineno); 387 var, fname, lineno);
290 }
291 } 388 }
292 389
293 fclose (f); 390 fclose (f);
294 } 391 }
295 else 392 else
296 { 393 {
297 slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); 394 slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno));
298 exit (1); 395 exit (EXIT_FAILURE);
299 } 396 }
300 397
301 free (fname); 398 free (fname);
302 399
303 fname = config_filename (prikeyfile, "hostkey"); 400 fname = config_filename (prikeyfile, "hostkey");
309 406
310 if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) 407 if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL))
311 { 408 {
312 ERR_load_RSA_strings (); ERR_load_PEM_strings (); 409 ERR_load_RSA_strings (); ERR_load_PEM_strings ();
313 slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); 410 slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
314 exit (1); 411 exit (EXIT_FAILURE);
315 } 412 }
316 413
317 RSA_blinding_on (rsa_key, 0); 414 require (RSA_blinding_on (rsa_key, 0));
318 415
319 fclose (f); 416 fclose (f);
320 } 417 }
321 else 418 else
322 { 419 {
323 slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); 420 slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno));
324 421
325 if (need_keys) 422 if (need_keys)
326 exit (1); 423 exit (EXIT_FAILURE);
327 } 424 }
425
426 if (need_keys && ::thisnode
427 && rsa_key && thisnode && thisnode->rsa_key)
428 if (BN_cmp (rsa_key->n, thisnode->rsa_key->n) != 0
429 || BN_cmp (rsa_key->e, thisnode->rsa_key->e) != 0)
430 {
431 slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode);
432 exit (EXIT_FAILURE);
433 }
328 434
329 free (fname); 435 free (fname);
330} 436}
331 437
332char *configuration::config_filename (const char *name, const char *dflt) 438char *configuration::config_filename (const char *name, const char *dflt)
354 printf (_("MTU: %d\n"), mtu); 460 printf (_("MTU: %d\n"), mtu);
355 printf (_("rekeying interval: %d\n"), rekey); 461 printf (_("rekeying interval: %d\n"), rekey);
356 printf (_("keepalive interval: %d\n"), keepalive); 462 printf (_("keepalive interval: %d\n"), keepalive);
357 printf (_("interface: %s\n"), ifname); 463 printf (_("interface: %s\n"), ifname);
358 printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); 464 printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>");
359 printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); 465 printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1);
360 printf ("\n"); 466 printf ("\n");
361 467
362 printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", 468 printf ("%4s %-17s %s %-8.8s %-10.10s %s\n",
363 _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); 469 _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port"));
364 470
366 (*i)->print (); 472 (*i)->print ();
367 473
368 printf ("\n"); 474 printf ("\n");
369} 475}
370 476
371void 477configuration::configuration ()
372conf_node::print ()
373{ 478{
374 printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", 479 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} 480}
387 481
482configuration::~configuration ()
483{
484 cleanup ();
485}
486
487

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines