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.55 by root, Sun Mar 6 19:40:28 2011 UTC vs.
Revision 1.65 by root, Fri Oct 11 07:56:07 2013 UTC

38#include <errno.h> 38#include <errno.h>
39#include <netdb.h> 39#include <netdb.h>
40#include <sys/stat.h> 40#include <sys/stat.h>
41#include <sys/types.h> 41#include <sys/types.h>
42#include <unistd.h> 42#include <unistd.h>
43#include <pwd.h>
43 44
44#include "netcompat.h" 45#include "netcompat.h"
45 46
46#include <openssl/err.h> 47#include <openssl/err.h>
47#include <openssl/pem.h> 48#include <openssl/pem.h>
160 dns_send_interval = DEFAULT_DNS_SEND_INTERVAL; 161 dns_send_interval = DEFAULT_DNS_SEND_INTERVAL;
161 dns_overlap_factor = DEFAULT_DNS_OVERLAP_FACTOR; 162 dns_overlap_factor = DEFAULT_DNS_OVERLAP_FACTOR;
162 dns_max_outstanding = DEFAULT_DNS_MAX_OUTSTANDING; 163 dns_max_outstanding = DEFAULT_DNS_MAX_OUTSTANDING;
163#endif 164#endif
164 165
165 conf.pidfilename = strdup (LOCALSTATEDIR "/run/gvpe.pid"); 166 pidfilename = strdup (LOCALSTATEDIR "/run/gvpe.pid");
167 seed_dev = strdup ("/dev/urandom");
168 reseed = DEFAULT_RESEED;
166} 169}
167 170
168void 171void
169configuration::cleanup () 172configuration::cleanup ()
170{ 173{
171 if (rsa_key) 174 if (rsa_key)
172 RSA_free (rsa_key); 175 RSA_free (rsa_key);
173 176
174 rsa_key = 0; 177 rsa_key = 0;
175 178
179 free (seed_dev); seed_dev = 0;
176 free (pidfilename); pidfilename = 0; 180 free (pidfilename); pidfilename = 0;
177 free (ifname); ifname = 0; 181 free (ifname); ifname = 0;
178#if ENABLE_HTTP_PROXY 182#if ENABLE_HTTP_PROXY
179 free (proxy_host); proxy_host = 0; 183 free (proxy_host); proxy_host = 0;
180 free (proxy_auth); proxy_auth = 0; 184 free (proxy_auth); proxy_auth = 0;
181#endif 185#endif
182#if ENABLE_DNS 186#if ENABLE_DNS
183 free (dns_forw_host); dns_forw_host = 0; 187 free (dns_forw_host); dns_forw_host = 0;
184#endif 188#endif
189 free (change_root); change_root = 0;
185 free (script_if_up); script_if_up = 0; 190 free (script_if_up); script_if_up = 0;
186 free (script_node_up); script_node_up = 0; 191 free (script_node_up); script_node_up = 0;
187 free (script_node_change); script_node_change = 0; 192 free (script_node_change); script_node_change = 0;
188 free (script_node_down); script_node_down = 0; 193 free (script_node_down); script_node_down = 0;
189} 194}
196 201
197 nodes.clear (); 202 nodes.clear ();
198 203
199 cleanup (); 204 cleanup ();
200 init (); 205 init ();
206}
207
208conf_node *
209configuration::find_node (const char *name)
210{
211 for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i)
212 if (!strcmp ((*i)->nodename, name))
213 return *i;
214
215 return 0;
201} 216}
202 217
203//static bool 218//static bool
204//is_true (const char *name) 219//is_true (const char *name)
205//{ 220//{
212 else if (!strcmp (val, "true")) target = trueval; \ 227 else if (!strcmp (val, "true")) target = trueval; \
213 else if (!strcmp (val, "false")) target = falseval; \ 228 else if (!strcmp (val, "false")) target = falseval; \
214 else if (!strcmp (val, "on")) target = trueval; \ 229 else if (!strcmp (val, "on")) target = trueval; \
215 else if (!strcmp (val, "off")) target = falseval; \ 230 else if (!strcmp (val, "off")) target = falseval; \
216 else \ 231 else \
217 return _("illegal boolean value, only 'yes|true|on' or 'no|false|off' allowed. (ignored)"); \ 232 return _("illegal boolean value, only 'yes|true|on' or 'no|false|off' allowed, ignored"); \
218} while (0) 233} while (0)
219 234
220const char * 235const char *
221configuration_parser::parse_line (char *line) 236configuration_parser::parse_line (char *line)
222{ 237{
237 return 0; /* no tokens on this line */ 252 return 0; /* no tokens on this line */
238 253
239 if (var[0] == '#') 254 if (var[0] == '#')
240 return 0; /* comment: ignore */ 255 return 0; /* comment: ignore */
241 256
257 if (!strcmp (var, "global"))
258 {
259 node = &conf.default_node;
260 return 0;
261 }
262
242 char *val = strtok (NULL, "\t\n\r ="); 263 char *val = strtok (NULL, "\t\n\r =");
243 264
244 if (!val || val[0] == '#') 265 if (!val || val[0] == '#')
245 return _("no value given for variable. (ignored)"); 266 return _("no value given for variable, ignored");
246 267
247 else if (!strcmp (var, "on")) 268 else if (!strcmp (var, "on"))
248 { 269 {
249 if (!::thisnode 270 if (::thisnode
250 || (val[0] == '!' && strcmp (val + 1, ::thisnode)) 271 && ((val[0] == '!' && strcmp (val + 1, ::thisnode))
251 || !strcmp (val, ::thisnode)) 272 || !strcmp (val, ::thisnode)))
252 return parse_line (strtok (NULL, "\n\r")); 273 return parse_line (strtok (NULL, "\n\r"));
253 } 274 }
254 275
255 else if (!strcmp (var, "include")) 276 else if (!strcmp (var, "include"))
256 { 277 {
263 else if (!strcmp (var, "loglevel")) 284 else if (!strcmp (var, "loglevel"))
264 { 285 {
265 loglevel l = string_to_loglevel (val); 286 loglevel l = string_to_loglevel (val);
266 287
267 if (l == L_NONE) 288 if (l == L_NONE)
268 return _("unknown loglevel. (skipping)"); 289 return _("unknown loglevel, ignored");
269 } 290 }
291 else if (!strcmp (var, "serial"))
292 strncpy (conf.serial, val, sizeof (conf.serial));
270 else if (!strcmp (var, "ip-proto")) 293 else if (!strcmp (var, "ip-proto"))
271 conf.ip_proto = atoi (val); 294 conf.ip_proto = atoi (val);
272 else if (!strcmp (var, "icmp-type")) 295 else if (!strcmp (var, "icmp-type"))
273 { 296 {
274#if ENABLE_ICMP 297#if ENABLE_ICMP
275 conf.icmp_type = atoi (val); 298 conf.icmp_type = atoi (val);
276#endif 299#endif
277 } 300 }
301 else if (!strcmp (var, "chuser"))
302 {
303 struct passwd *pw = getpwnam (val);
304 if (!pw)
305 return _("user specified for chuser not found");
278 306
279 // per config 307 conf.change_uid = pw->pw_uid;
308 conf.change_gid = pw->pw_gid;
309 }
310 else if (!strcmp (var, "chuid"))
311 conf.change_uid = atoi (val);
312 else if (!strcmp (var, "chgid"))
313 conf.change_gid = atoi (val);
314 else if (!strcmp (var, "chroot"))
315 free (conf.change_root), conf.change_root = strdup (val);
316
317 // per node
280 else if (!strcmp (var, "node")) 318 else if (!strcmp (var, "node"))
281 { 319 {
282 parse_argv (); 320 node = conf.find_node (val);
283 321
322 if (!node)
323 {
284 conf.default_node.id++; 324 conf.default_node.id++;
285 node = new conf_node (conf.default_node); 325 node = new conf_node (conf.default_node);
286 conf.nodes.push_back (node); 326 conf.nodes.push_back (node);
287 node->nodename = strdup (val); 327 node->nodename = strdup (val);
288
289 {
290 char *fname;
291 FILE *f;
292
293 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename);
294
295 f = fopen (fname, "r");
296 if (f)
297 {
298 node->rsa_key = RSA_new ();
299
300 if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL))
301 {
302 ERR_load_RSA_strings (); ERR_load_PEM_strings ();
303 slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
304 exit (EXIT_FAILURE);
305 }
306
307 require (RSA_blinding_on (node->rsa_key, 0));
308
309 fclose (f);
310 } 328 }
311 else
312 {
313 slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno));
314
315 if (need_keys)
316 exit (EXIT_FAILURE);
317 }
318
319 free (fname);
320 }
321
322 if (::thisnode && !strcmp (node->nodename, ::thisnode))
323 conf.thisnode = node;
324 } 329 }
325 else if (!strcmp (var, "private-key")) 330 else if (!strcmp (var, "private-key"))
326 free (conf.prikeyfile), conf.prikeyfile = strdup (val); 331 free (conf.prikeyfile), conf.prikeyfile = strdup (val);
327 else if (!strcmp (var, "ifpersist")) 332 else if (!strcmp (var, "ifpersist"))
328 parse_bool (conf.ifpersist, "ifpersist", true, false); 333 parse_bool (conf.ifpersist, "ifpersist", true, false);
334 conf.keepalive = atoi (val); 339 conf.keepalive = atoi (val);
335 else if (!strcmp (var, "mtu")) 340 else if (!strcmp (var, "mtu"))
336 conf.mtu = atoi (val); 341 conf.mtu = atoi (val);
337 else if (!strcmp (var, "nfmark")) 342 else if (!strcmp (var, "nfmark"))
338 conf.nfmark = atoi (val); 343 conf.nfmark = atoi (val);
344 else if (!strcmp (var, "seed-device"))
345 free (conf.seed_dev), conf.seed_dev = strdup (val);
346 else if (!strcmp (var, "seed-interval"))
347 conf.reseed = atoi (val);
339 else if (!strcmp (var, "if-up")) 348 else if (!strcmp (var, "if-up"))
340 free (conf.script_if_up), conf.script_if_up = strdup (val); 349 free (conf.script_if_up), conf.script_if_up = strdup (val);
341 else if (!strcmp (var, "node-up")) 350 else if (!strcmp (var, "node-up"))
342 free (conf.script_node_up), conf.script_node_up = strdup (val); 351 free (conf.script_node_up), conf.script_node_up = strdup (val);
343 else if (!strcmp (var, "node-change")) 352 else if (!strcmp (var, "node-change"))
449 else if (!strcmp (val, "always")) 458 else if (!strcmp (val, "always"))
450 node->connectmode = conf_node::C_ALWAYS; 459 node->connectmode = conf_node::C_ALWAYS;
451 else if (!strcmp (val, "disabled")) 460 else if (!strcmp (val, "disabled"))
452 node->connectmode = conf_node::C_DISABLED; 461 node->connectmode = conf_node::C_DISABLED;
453 else 462 else
454 return _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled'. (ignored)"); 463 return _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', ignored");
455 } 464 }
456 else if (!strcmp (var, "inherit-tos")) 465 else if (!strcmp (var, "inherit-tos"))
457 parse_bool (node->inherit_tos, "inherit-tos", true, false); 466 parse_bool (node->inherit_tos, "inherit-tos", true, false);
458 else if (!strcmp (var, "compress")) 467 else if (!strcmp (var, "compress"))
459 parse_bool (node->compress, "compress", true, false); 468 parse_bool (node->compress, "compress", true, false);
469 else if (!strcmp (var, "low-power"))
470 parse_bool (node->low_power, "low-power", true, false);
460 // all these bool options really really cost a lot of executable size! 471 // all these bool options really really cost a lot of executable size!
461 else if (!strcmp (var, "enable-tcp")) 472 else if (!strcmp (var, "enable-tcp"))
462 { 473 {
463#if ENABLE_TCP 474#if ENABLE_TCP
464 u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; 475 u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v;
515 connectmode = C_ALWAYS; 526 connectmode = C_ALWAYS;
516 } 527 }
517} 528}
518 529
519void 530void
520configuration_parser::parse_argv ()
521{
522 for (int i = 0; i < argc; ++i)
523 {
524 char *v = argv [i];
525
526 if (!*v)
527 continue;
528
529 char *enode = v;
530
531 while (*enode != '.' && *enode > ' ' && *enode != '=' && *enode)
532 enode++;
533
534 if (*enode != '.')
535 enode = 0;
536
537 char *wnode = node == &conf.default_node
538 ? 0
539 : node->nodename;
540
541 if ((!wnode && !enode)
542 || (wnode && enode && !strncmp (wnode, v, enode - v)))
543 {
544 const char *warn = parse_line (enode ? enode + 1 : v);
545
546 if (warn)
547 slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v);
548
549 *v = 0;
550 }
551 }
552}
553
554void
555configuration_parser::parse_file (const char *fname) 531configuration_parser::parse_file (const char *fname)
556{ 532{
557 if (FILE *f = fopen (fname, "r")) 533 if (FILE *f = fopen (fname, "r"))
558 { 534 {
559 char line [2048]; 535 char line [2048];
560 int lineno = 0; 536 int lineno = 0;
561 node = &conf.default_node;
562 537
563 while (fgets (line, sizeof (line), f)) 538 while (fgets (line, sizeof (line), f))
564 { 539 {
565 lineno++; 540 lineno++;
566 541
569 if (warn) 544 if (warn)
570 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); 545 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno);
571 } 546 }
572 547
573 fclose (f); 548 fclose (f);
574
575 parse_argv ();
576 } 549 }
577 else 550 else
578 { 551 {
579 slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); 552 slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno));
580 exit (EXIT_FAILURE); 553 exit (EXIT_FAILURE);
583 556
584configuration_parser::configuration_parser (configuration &conf, 557configuration_parser::configuration_parser (configuration &conf,
585 bool need_keys, 558 bool need_keys,
586 int argc, 559 int argc,
587 char **argv) 560 char **argv)
588: conf (conf),need_keys (need_keys), argc (argc), argv (argv) 561: conf (conf), need_keys (need_keys), argc (argc), argv (argv)
589{ 562{
590 char *fname; 563 char *fname;
591 564
592 conf.clear (); 565 conf.clear ();
566 node = &conf.default_node;
593 567
594 asprintf (&fname, "%s/gvpe.conf", confbase); 568 asprintf (&fname, "%s/gvpe.conf", confbase);
595 parse_file (fname); 569 parse_file (fname);
596 free (fname); 570 free (fname);
597 571
612 586
613 fclose (f); 587 fclose (f);
614 } 588 }
615 else 589 else
616 { 590 {
617 slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno));
618
619 if (need_keys) 591 if (need_keys)
592 {
593 slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno));
620 exit (EXIT_FAILURE); 594 exit (EXIT_FAILURE);
595 }
621 } 596 }
622 597
623 free (fname); 598 free (fname);
624 599
625 if (need_keys && ::thisnode 600 fname = conf.config_filename (conf.pidfilename);
626 && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) 601 free (conf.pidfilename); conf.pidfilename = fname;
627 if (BN_cmp (conf.rsa_key->n, conf.thisnode->rsa_key->n) != 0
628 || BN_cmp (conf.rsa_key->e, conf.thisnode->rsa_key->e) != 0)
629 {
630 slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode);
631 exit (EXIT_FAILURE);
632 }
633 602
634 for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i) 603 for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i)
604 {
605 conf_node *node = *i;
606 char *fname;
607 FILE *f;
608
609 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename);
610
611 f = fopen (fname, "r");
612 if (f)
613 {
614 node->rsa_key = RSA_new ();
615
616 if (!PEM_read_RSAPublicKey (f, &node->rsa_key, NULL, NULL))
617 {
618 ERR_load_RSA_strings (); ERR_load_PEM_strings ();
619 slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
620 exit (EXIT_FAILURE);
621 }
622
623 require (RSA_blinding_on (node->rsa_key, 0));
624
625 fclose (f);
626 }
627 else
628 {
629 slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno));
630
631 if (need_keys)
632 exit (EXIT_FAILURE);
633 }
634
635 free (fname);
636
635 (*i)->finalise (); 637 (*i)->finalise ();
638 }
639
640 if (::thisnode)
641 {
642 conf.thisnode = conf.find_node (::thisnode);
643
644 if (need_keys)
645 {
646 if (!conf.thisnode)
647 {
648 slog (L_NOTICE, _("local node ('%s') not found in config file, aborting."), ::thisnode);
649 exit (EXIT_FAILURE);
650 }
651
652 if (conf.rsa_key && conf.thisnode->rsa_key)
653 if (BN_cmp (conf.rsa_key->n, conf.thisnode->rsa_key->n) != 0
654 || BN_cmp (conf.rsa_key->e, conf.thisnode->rsa_key->e) != 0)
655 {
656 slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode);
657 exit (EXIT_FAILURE);
658 }
659 }
660 }
661
662 parse_argv ();
663}
664
665void
666configuration_parser::parse_argv ()
667{
668 for (int i = 0; i < argc; ++i)
669 {
670 char *v = argv [i];
671
672 if (!*v)
673 continue;
674
675 char *enode = v;
676
677 while (*enode != '.' && *enode > ' ' && *enode != '=' && *enode)
678 enode++;
679
680 if (*enode != '.')
681 enode = 0;
682
683 if (enode)
684 {
685 char *val = strdup (v);
686 val [enode - v] = 0;
687 node = conf.find_node (val);
688 free (val);
689
690 if (!node)
691 {
692 slog (L_WARN, _("command line option '%s' refers to unknown node, ignoring."), v);
693 continue;
694 }
695 }
696 else
697 node = &conf.default_node;
698
699 const char *warn = parse_line (enode ? enode + 1 : v);
700
701 if (warn)
702 slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v);
703 }
636} 704}
637 705
638char * 706char *
639configuration::config_filename (const char *name, const char *dflt) 707configuration::config_filename (const char *name, const char *dflt)
640{ 708{
641 char *fname; 709 char *fname;
642 710
643 asprintf (&fname, name ? name : dflt, ::thisnode); 711 asprintf (&fname, name ? name : dflt, ::thisnode ? ::thisnode : "<unset>");
644 712
645 if (!ABSOLUTE_PATH (fname)) 713 if (!ABSOLUTE_PATH (fname))
646 { 714 {
647 char *rname = fname; 715 char *rname = fname;
648 asprintf (&fname, "%s/%s", confbase, rname); 716 asprintf (&fname, "%s/%s", confbase, rname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines