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.60 by root, Sat Jul 13 04:10:29 2013 UTC vs.
Revision 1.65 by root, Fri Oct 11 07:56:07 2013 UTC

203 203
204 cleanup (); 204 cleanup ();
205 init (); 205 init ();
206} 206}
207 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;
216}
217
208//static bool 218//static bool
209//is_true (const char *name) 219//is_true (const char *name)
210//{ 220//{
211 //re 221 //re
212//} 222//}
242 return 0; /* no tokens on this line */ 252 return 0; /* no tokens on this line */
243 253
244 if (var[0] == '#') 254 if (var[0] == '#')
245 return 0; /* comment: ignore */ 255 return 0; /* comment: ignore */
246 256
257 if (!strcmp (var, "global"))
258 {
259 node = &conf.default_node;
260 return 0;
261 }
262
247 char *val = strtok (NULL, "\t\n\r ="); 263 char *val = strtok (NULL, "\t\n\r =");
248 264
249 if (!val || val[0] == '#') 265 if (!val || val[0] == '#')
250 return _("no value given for variable, ignored"); 266 return _("no value given for variable, ignored");
251 267
270 loglevel l = string_to_loglevel (val); 286 loglevel l = string_to_loglevel (val);
271 287
272 if (l == L_NONE) 288 if (l == L_NONE)
273 return _("unknown loglevel, ignored"); 289 return _("unknown loglevel, ignored");
274 } 290 }
291 else if (!strcmp (var, "serial"))
292 strncpy (conf.serial, val, sizeof (conf.serial));
275 else if (!strcmp (var, "ip-proto")) 293 else if (!strcmp (var, "ip-proto"))
276 conf.ip_proto = atoi (val); 294 conf.ip_proto = atoi (val);
277 else if (!strcmp (var, "icmp-type")) 295 else if (!strcmp (var, "icmp-type"))
278 { 296 {
279#if ENABLE_ICMP 297#if ENABLE_ICMP
297 free (conf.change_root), conf.change_root = strdup (val); 315 free (conf.change_root), conf.change_root = strdup (val);
298 316
299 // per node 317 // per node
300 else if (!strcmp (var, "node")) 318 else if (!strcmp (var, "node"))
301 { 319 {
302 parse_argv (); 320 node = conf.find_node (val);
303 321
322 if (!node)
323 {
304 conf.default_node.id++; 324 conf.default_node.id++;
305 node = new conf_node (conf.default_node); 325 node = new conf_node (conf.default_node);
306 conf.nodes.push_back (node); 326 conf.nodes.push_back (node);
307 node->nodename = strdup (val); 327 node->nodename = strdup (val);
308
309 {
310 char *fname;
311 FILE *f;
312
313 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename);
314
315 f = fopen (fname, "r");
316 if (f)
317 {
318 node->rsa_key = RSA_new ();
319
320 if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL))
321 {
322 ERR_load_RSA_strings (); ERR_load_PEM_strings ();
323 slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
324 exit (EXIT_FAILURE);
325 }
326
327 require (RSA_blinding_on (node->rsa_key, 0));
328
329 fclose (f);
330 } 328 }
331 else
332 {
333 slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno));
334
335 if (need_keys)
336 exit (EXIT_FAILURE);
337 }
338
339 free (fname);
340 }
341
342 if (::thisnode && !strcmp (node->nodename, ::thisnode))
343 conf.thisnode = node;
344 } 329 }
345 else if (!strcmp (var, "private-key")) 330 else if (!strcmp (var, "private-key"))
346 free (conf.prikeyfile), conf.prikeyfile = strdup (val); 331 free (conf.prikeyfile), conf.prikeyfile = strdup (val);
347 else if (!strcmp (var, "ifpersist")) 332 else if (!strcmp (var, "ifpersist"))
348 parse_bool (conf.ifpersist, "ifpersist", true, false); 333 parse_bool (conf.ifpersist, "ifpersist", true, false);
479 } 464 }
480 else if (!strcmp (var, "inherit-tos")) 465 else if (!strcmp (var, "inherit-tos"))
481 parse_bool (node->inherit_tos, "inherit-tos", true, false); 466 parse_bool (node->inherit_tos, "inherit-tos", true, false);
482 else if (!strcmp (var, "compress")) 467 else if (!strcmp (var, "compress"))
483 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);
484 // 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!
485 else if (!strcmp (var, "enable-tcp")) 472 else if (!strcmp (var, "enable-tcp"))
486 { 473 {
487#if ENABLE_TCP 474#if ENABLE_TCP
488 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;
539 connectmode = C_ALWAYS; 526 connectmode = C_ALWAYS;
540 } 527 }
541} 528}
542 529
543void 530void
544configuration_parser::parse_argv ()
545{
546 for (int i = 0; i < argc; ++i)
547 {
548 char *v = argv [i];
549
550 if (!*v)
551 continue;
552
553 char *enode = v;
554
555 while (*enode != '.' && *enode > ' ' && *enode != '=' && *enode)
556 enode++;
557
558 if (*enode != '.')
559 enode = 0;
560
561 char *wnode = node == &conf.default_node
562 ? 0
563 : node->nodename;
564
565 if ((!wnode && !enode)
566 || (wnode && enode && !strncmp (wnode, v, enode - v)))
567 {
568 const char *warn = parse_line (enode ? enode + 1 : v);
569
570 if (warn)
571 slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v);
572
573 *v = 0;
574 }
575 }
576}
577
578void
579configuration_parser::parse_file (const char *fname) 531configuration_parser::parse_file (const char *fname)
580{ 532{
581 if (FILE *f = fopen (fname, "r")) 533 if (FILE *f = fopen (fname, "r"))
582 { 534 {
583 char line [2048]; 535 char line [2048];
592 if (warn) 544 if (warn)
593 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); 545 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno);
594 } 546 }
595 547
596 fclose (f); 548 fclose (f);
597
598 parse_argv ();
599 } 549 }
600 else 550 else
601 { 551 {
602 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));
603 exit (EXIT_FAILURE); 553 exit (EXIT_FAILURE);
606 556
607configuration_parser::configuration_parser (configuration &conf, 557configuration_parser::configuration_parser (configuration &conf,
608 bool need_keys, 558 bool need_keys,
609 int argc, 559 int argc,
610 char **argv) 560 char **argv)
611: conf (conf),need_keys (need_keys), argc (argc), argv (argv) 561: conf (conf), need_keys (need_keys), argc (argc), argv (argv)
612{ 562{
613 char *fname; 563 char *fname;
614 564
615 conf.clear (); 565 conf.clear ();
616 node = &conf.default_node; 566 node = &conf.default_node;
636 586
637 fclose (f); 587 fclose (f);
638 } 588 }
639 else 589 else
640 { 590 {
641 slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno));
642
643 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));
644 exit (EXIT_FAILURE); 594 exit (EXIT_FAILURE);
595 }
645 } 596 }
646 597
647 free (fname); 598 free (fname);
648 599
649 if (need_keys && ::thisnode 600 fname = conf.config_filename (conf.pidfilename);
650 && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) 601 free (conf.pidfilename); conf.pidfilename = fname;
651 if (BN_cmp (conf.rsa_key->n, conf.thisnode->rsa_key->n) != 0
652 || BN_cmp (conf.rsa_key->e, conf.thisnode->rsa_key->e) != 0)
653 {
654 slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode);
655 exit (EXIT_FAILURE);
656 }
657 602
658 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
659 (*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 }
660} 704}
661 705
662char * 706char *
663configuration::config_filename (const char *name, const char *dflt) 707configuration::config_filename (const char *name, const char *dflt)
664{ 708{
665 char *fname; 709 char *fname;
666 710
667 asprintf (&fname, name ? name : dflt, ::thisnode); 711 asprintf (&fname, name ? name : dflt, ::thisnode ? ::thisnode : "<unset>");
668 712
669 if (!ABSOLUTE_PATH (fname)) 713 if (!ABSOLUTE_PATH (fname))
670 { 714 {
671 char *rname = fname; 715 char *rname = fname;
672 asprintf (&fname, "%s/%s", confbase, rname); 716 asprintf (&fname, "%s/%s", confbase, rname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines