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.63 by root, Thu Jul 18 13:35:16 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
297 free (conf.change_root), conf.change_root = strdup (val); 313 free (conf.change_root), conf.change_root = strdup (val);
298 314
299 // per node 315 // per node
300 else if (!strcmp (var, "node")) 316 else if (!strcmp (var, "node"))
301 { 317 {
302 parse_argv (); 318 node = conf.find_node (val);
303 319
320 if (!node)
321 {
304 conf.default_node.id++; 322 conf.default_node.id++;
305 node = new conf_node (conf.default_node); 323 node = new conf_node (conf.default_node);
306 conf.nodes.push_back (node); 324 conf.nodes.push_back (node);
307 node->nodename = strdup (val); 325 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 } 326 }
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 } 327 }
345 else if (!strcmp (var, "private-key")) 328 else if (!strcmp (var, "private-key"))
346 free (conf.prikeyfile), conf.prikeyfile = strdup (val); 329 free (conf.prikeyfile), conf.prikeyfile = strdup (val);
347 else if (!strcmp (var, "ifpersist")) 330 else if (!strcmp (var, "ifpersist"))
348 parse_bool (conf.ifpersist, "ifpersist", true, false); 331 parse_bool (conf.ifpersist, "ifpersist", true, false);
539 connectmode = C_ALWAYS; 522 connectmode = C_ALWAYS;
540 } 523 }
541} 524}
542 525
543void 526void
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) 527configuration_parser::parse_file (const char *fname)
580{ 528{
581 if (FILE *f = fopen (fname, "r")) 529 if (FILE *f = fopen (fname, "r"))
582 { 530 {
583 char line [2048]; 531 char line [2048];
592 if (warn) 540 if (warn)
593 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); 541 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno);
594 } 542 }
595 543
596 fclose (f); 544 fclose (f);
597
598 parse_argv ();
599 } 545 }
600 else 546 else
601 { 547 {
602 slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); 548 slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno));
603 exit (EXIT_FAILURE); 549 exit (EXIT_FAILURE);
606 552
607configuration_parser::configuration_parser (configuration &conf, 553configuration_parser::configuration_parser (configuration &conf,
608 bool need_keys, 554 bool need_keys,
609 int argc, 555 int argc,
610 char **argv) 556 char **argv)
611: conf (conf),need_keys (need_keys), argc (argc), argv (argv) 557: conf (conf), need_keys (need_keys), argc (argc), argv (argv)
612{ 558{
613 char *fname; 559 char *fname;
614 560
615 conf.clear (); 561 conf.clear ();
616 node = &conf.default_node; 562 node = &conf.default_node;
636 582
637 fclose (f); 583 fclose (f);
638 } 584 }
639 else 585 else
640 { 586 {
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) 587 if (need_keys)
588 {
589 slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno));
644 exit (EXIT_FAILURE); 590 exit (EXIT_FAILURE);
591 }
645 } 592 }
646 593
647 free (fname); 594 free (fname);
648 595
649 if (need_keys && ::thisnode 596 fname = conf.config_filename (conf.pidfilename);
650 && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) 597 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 598
658 for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i) 599 for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i)
600 {
601 conf_node *node = *i;
602 char *fname;
603 FILE *f;
604
605 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename);
606
607 f = fopen (fname, "r");
608 if (f)
609 {
610 node->rsa_key = RSA_new ();
611
612 if (!PEM_read_RSAPublicKey (f, &node->rsa_key, NULL, NULL))
613 {
614 ERR_load_RSA_strings (); ERR_load_PEM_strings ();
615 slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
616 exit (EXIT_FAILURE);
617 }
618
619 require (RSA_blinding_on (node->rsa_key, 0));
620
621 fclose (f);
622 }
623 else
624 {
625 slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno));
626
627 if (need_keys)
628 exit (EXIT_FAILURE);
629 }
630
631 free (fname);
632
659 (*i)->finalise (); 633 (*i)->finalise ();
634 }
635
636 if (::thisnode)
637 {
638 conf.thisnode = conf.find_node (::thisnode);
639
640 if (need_keys)
641 {
642 if (!conf.thisnode)
643 {
644 slog (L_NOTICE, _("local node ('%s') not found in config file, aborting."), ::thisnode);
645 exit (EXIT_FAILURE);
646 }
647
648 if (conf.rsa_key && conf.thisnode->rsa_key)
649 if (BN_cmp (conf.rsa_key->n, conf.thisnode->rsa_key->n) != 0
650 || BN_cmp (conf.rsa_key->e, conf.thisnode->rsa_key->e) != 0)
651 {
652 slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode);
653 exit (EXIT_FAILURE);
654 }
655 }
656 }
657
658 parse_argv ();
659}
660
661void
662configuration_parser::parse_argv ()
663{
664 for (int i = 0; i < argc; ++i)
665 {
666 char *v = argv [i];
667
668 if (!*v)
669 continue;
670
671 char *enode = v;
672
673 while (*enode != '.' && *enode > ' ' && *enode != '=' && *enode)
674 enode++;
675
676 if (*enode != '.')
677 enode = 0;
678
679 if (enode)
680 {
681 char *val = strdup (v);
682 val [enode - v] = 0;
683 node = conf.find_node (val);
684 free (val);
685
686 if (!node)
687 {
688 slog (L_WARN, _("command line option '%s' refers to unknown node, ignoring."), v);
689 continue;
690 }
691 }
692 else
693 node = &conf.default_node;
694
695 const char *warn = parse_line (enode ? enode + 1 : v);
696
697 if (warn)
698 slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v);
699 }
660} 700}
661 701
662char * 702char *
663configuration::config_filename (const char *name, const char *dflt) 703configuration::config_filename (const char *name, const char *dflt)
664{ 704{
665 char *fname; 705 char *fname;
666 706
667 asprintf (&fname, name ? name : dflt, ::thisnode); 707 asprintf (&fname, name ? name : dflt, ::thisnode ? ::thisnode : "<unset>");
668 708
669 if (!ABSOLUTE_PATH (fname)) 709 if (!ABSOLUTE_PATH (fname))
670 { 710 {
671 char *rname = fname; 711 char *rname = fname;
672 asprintf (&fname, "%s/%s", confbase, rname); 712 asprintf (&fname, "%s/%s", confbase, rname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines