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.59 by root, Tue Dec 4 10:29:43 2012 UTC vs.
Revision 1.64 by root, Thu Jul 18 17:35:10 2013 UTC

161 dns_send_interval = DEFAULT_DNS_SEND_INTERVAL; 161 dns_send_interval = DEFAULT_DNS_SEND_INTERVAL;
162 dns_overlap_factor = DEFAULT_DNS_OVERLAP_FACTOR; 162 dns_overlap_factor = DEFAULT_DNS_OVERLAP_FACTOR;
163 dns_max_outstanding = DEFAULT_DNS_MAX_OUTSTANDING; 163 dns_max_outstanding = DEFAULT_DNS_MAX_OUTSTANDING;
164#endif 164#endif
165 165
166 conf.pidfilename = strdup (LOCALSTATEDIR "/run/gvpe.pid"); 166 pidfilename = strdup (LOCALSTATEDIR "/run/gvpe.pid");
167 seed_dev = strdup ("/dev/urandom");
168 reseed = DEFAULT_RESEED;
167} 169}
168 170
169void 171void
170configuration::cleanup () 172configuration::cleanup ()
171{ 173{
172 if (rsa_key) 174 if (rsa_key)
173 RSA_free (rsa_key); 175 RSA_free (rsa_key);
174 176
175 rsa_key = 0; 177 rsa_key = 0;
176 178
179 free (seed_dev); seed_dev = 0;
177 free (pidfilename); pidfilename = 0; 180 free (pidfilename); pidfilename = 0;
178 free (ifname); ifname = 0; 181 free (ifname); ifname = 0;
179#if ENABLE_HTTP_PROXY 182#if ENABLE_HTTP_PROXY
180 free (proxy_host); proxy_host = 0; 183 free (proxy_host); proxy_host = 0;
181 free (proxy_auth); proxy_auth = 0; 184 free (proxy_auth); proxy_auth = 0;
198 201
199 nodes.clear (); 202 nodes.clear ();
200 203
201 cleanup (); 204 cleanup ();
202 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;
203} 216}
204 217
205//static bool 218//static bool
206//is_true (const char *name) 219//is_true (const char *name)
207//{ 220//{
239 return 0; /* no tokens on this line */ 252 return 0; /* no tokens on this line */
240 253
241 if (var[0] == '#') 254 if (var[0] == '#')
242 return 0; /* comment: ignore */ 255 return 0; /* comment: ignore */
243 256
257 if (!strcmp (var, "global"))
258 {
259 node = &conf.default_node;
260 return 0;
261 }
262
244 char *val = strtok (NULL, "\t\n\r ="); 263 char *val = strtok (NULL, "\t\n\r =");
245 264
246 if (!val || val[0] == '#') 265 if (!val || val[0] == '#')
247 return _("no value given for variable, ignored"); 266 return _("no value given for variable, ignored");
248 267
267 loglevel l = string_to_loglevel (val); 286 loglevel l = string_to_loglevel (val);
268 287
269 if (l == L_NONE) 288 if (l == L_NONE)
270 return _("unknown loglevel, ignored"); 289 return _("unknown loglevel, ignored");
271 } 290 }
291 else if (!strcmp (var, "serial"))
292 strncpy (conf.serial, val, sizeof (conf.serial));
272 else if (!strcmp (var, "ip-proto")) 293 else if (!strcmp (var, "ip-proto"))
273 conf.ip_proto = atoi (val); 294 conf.ip_proto = atoi (val);
274 else if (!strcmp (var, "icmp-type")) 295 else if (!strcmp (var, "icmp-type"))
275 { 296 {
276#if ENABLE_ICMP 297#if ENABLE_ICMP
294 free (conf.change_root), conf.change_root = strdup (val); 315 free (conf.change_root), conf.change_root = strdup (val);
295 316
296 // per node 317 // per node
297 else if (!strcmp (var, "node")) 318 else if (!strcmp (var, "node"))
298 { 319 {
299 parse_argv (); 320 node = conf.find_node (val);
300 321
322 if (!node)
323 {
301 conf.default_node.id++; 324 conf.default_node.id++;
302 node = new conf_node (conf.default_node); 325 node = new conf_node (conf.default_node);
303 conf.nodes.push_back (node); 326 conf.nodes.push_back (node);
304 node->nodename = strdup (val); 327 node->nodename = strdup (val);
305
306 {
307 char *fname;
308 FILE *f;
309
310 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename);
311
312 f = fopen (fname, "r");
313 if (f)
314 {
315 node->rsa_key = RSA_new ();
316
317 if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL))
318 {
319 ERR_load_RSA_strings (); ERR_load_PEM_strings ();
320 slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
321 exit (EXIT_FAILURE);
322 }
323
324 require (RSA_blinding_on (node->rsa_key, 0));
325
326 fclose (f);
327 } 328 }
328 else
329 {
330 slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno));
331
332 if (need_keys)
333 exit (EXIT_FAILURE);
334 }
335
336 free (fname);
337 }
338
339 if (::thisnode && !strcmp (node->nodename, ::thisnode))
340 conf.thisnode = node;
341 } 329 }
342 else if (!strcmp (var, "private-key")) 330 else if (!strcmp (var, "private-key"))
343 free (conf.prikeyfile), conf.prikeyfile = strdup (val); 331 free (conf.prikeyfile), conf.prikeyfile = strdup (val);
344 else if (!strcmp (var, "ifpersist")) 332 else if (!strcmp (var, "ifpersist"))
345 parse_bool (conf.ifpersist, "ifpersist", true, false); 333 parse_bool (conf.ifpersist, "ifpersist", true, false);
351 conf.keepalive = atoi (val); 339 conf.keepalive = atoi (val);
352 else if (!strcmp (var, "mtu")) 340 else if (!strcmp (var, "mtu"))
353 conf.mtu = atoi (val); 341 conf.mtu = atoi (val);
354 else if (!strcmp (var, "nfmark")) 342 else if (!strcmp (var, "nfmark"))
355 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);
356 else if (!strcmp (var, "if-up")) 348 else if (!strcmp (var, "if-up"))
357 free (conf.script_if_up), conf.script_if_up = strdup (val); 349 free (conf.script_if_up), conf.script_if_up = strdup (val);
358 else if (!strcmp (var, "node-up")) 350 else if (!strcmp (var, "node-up"))
359 free (conf.script_node_up), conf.script_node_up = strdup (val); 351 free (conf.script_node_up), conf.script_node_up = strdup (val);
360 else if (!strcmp (var, "node-change")) 352 else if (!strcmp (var, "node-change"))
532 connectmode = C_ALWAYS; 524 connectmode = C_ALWAYS;
533 } 525 }
534} 526}
535 527
536void 528void
537configuration_parser::parse_argv ()
538{
539 for (int i = 0; i < argc; ++i)
540 {
541 char *v = argv [i];
542
543 if (!*v)
544 continue;
545
546 char *enode = v;
547
548 while (*enode != '.' && *enode > ' ' && *enode != '=' && *enode)
549 enode++;
550
551 if (*enode != '.')
552 enode = 0;
553
554 char *wnode = node == &conf.default_node
555 ? 0
556 : node->nodename;
557
558 if ((!wnode && !enode)
559 || (wnode && enode && !strncmp (wnode, v, enode - v)))
560 {
561 const char *warn = parse_line (enode ? enode + 1 : v);
562
563 if (warn)
564 slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v);
565
566 *v = 0;
567 }
568 }
569}
570
571void
572configuration_parser::parse_file (const char *fname) 529configuration_parser::parse_file (const char *fname)
573{ 530{
574 if (FILE *f = fopen (fname, "r")) 531 if (FILE *f = fopen (fname, "r"))
575 { 532 {
576 char line [2048]; 533 char line [2048];
585 if (warn) 542 if (warn)
586 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); 543 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno);
587 } 544 }
588 545
589 fclose (f); 546 fclose (f);
590
591 parse_argv ();
592 } 547 }
593 else 548 else
594 { 549 {
595 slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); 550 slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno));
596 exit (EXIT_FAILURE); 551 exit (EXIT_FAILURE);
599 554
600configuration_parser::configuration_parser (configuration &conf, 555configuration_parser::configuration_parser (configuration &conf,
601 bool need_keys, 556 bool need_keys,
602 int argc, 557 int argc,
603 char **argv) 558 char **argv)
604: conf (conf),need_keys (need_keys), argc (argc), argv (argv) 559: conf (conf), need_keys (need_keys), argc (argc), argv (argv)
605{ 560{
606 char *fname; 561 char *fname;
607 562
608 conf.clear (); 563 conf.clear ();
609 node = &conf.default_node; 564 node = &conf.default_node;
629 584
630 fclose (f); 585 fclose (f);
631 } 586 }
632 else 587 else
633 { 588 {
634 slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno));
635
636 if (need_keys) 589 if (need_keys)
590 {
591 slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno));
637 exit (EXIT_FAILURE); 592 exit (EXIT_FAILURE);
593 }
638 } 594 }
639 595
640 free (fname); 596 free (fname);
641 597
642 if (need_keys && ::thisnode 598 fname = conf.config_filename (conf.pidfilename);
643 && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) 599 free (conf.pidfilename); conf.pidfilename = fname;
644 if (BN_cmp (conf.rsa_key->n, conf.thisnode->rsa_key->n) != 0
645 || BN_cmp (conf.rsa_key->e, conf.thisnode->rsa_key->e) != 0)
646 {
647 slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode);
648 exit (EXIT_FAILURE);
649 }
650 600
651 for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i) 601 for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i)
602 {
603 conf_node *node = *i;
604 char *fname;
605 FILE *f;
606
607 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename);
608
609 f = fopen (fname, "r");
610 if (f)
611 {
612 node->rsa_key = RSA_new ();
613
614 if (!PEM_read_RSAPublicKey (f, &node->rsa_key, NULL, NULL))
615 {
616 ERR_load_RSA_strings (); ERR_load_PEM_strings ();
617 slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
618 exit (EXIT_FAILURE);
619 }
620
621 require (RSA_blinding_on (node->rsa_key, 0));
622
623 fclose (f);
624 }
625 else
626 {
627 slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno));
628
629 if (need_keys)
630 exit (EXIT_FAILURE);
631 }
632
633 free (fname);
634
652 (*i)->finalise (); 635 (*i)->finalise ();
636 }
637
638 if (::thisnode)
639 {
640 conf.thisnode = conf.find_node (::thisnode);
641
642 if (need_keys)
643 {
644 if (!conf.thisnode)
645 {
646 slog (L_NOTICE, _("local node ('%s') not found in config file, aborting."), ::thisnode);
647 exit (EXIT_FAILURE);
648 }
649
650 if (conf.rsa_key && conf.thisnode->rsa_key)
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 }
658 }
659
660 parse_argv ();
661}
662
663void
664configuration_parser::parse_argv ()
665{
666 for (int i = 0; i < argc; ++i)
667 {
668 char *v = argv [i];
669
670 if (!*v)
671 continue;
672
673 char *enode = v;
674
675 while (*enode != '.' && *enode > ' ' && *enode != '=' && *enode)
676 enode++;
677
678 if (*enode != '.')
679 enode = 0;
680
681 if (enode)
682 {
683 char *val = strdup (v);
684 val [enode - v] = 0;
685 node = conf.find_node (val);
686 free (val);
687
688 if (!node)
689 {
690 slog (L_WARN, _("command line option '%s' refers to unknown node, ignoring."), v);
691 continue;
692 }
693 }
694 else
695 node = &conf.default_node;
696
697 const char *warn = parse_line (enode ? enode + 1 : v);
698
699 if (warn)
700 slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v);
701 }
653} 702}
654 703
655char * 704char *
656configuration::config_filename (const char *name, const char *dflt) 705configuration::config_filename (const char *name, const char *dflt)
657{ 706{
658 char *fname; 707 char *fname;
659 708
660 asprintf (&fname, name ? name : dflt, ::thisnode); 709 asprintf (&fname, name ? name : dflt, ::thisnode ? ::thisnode : "<unset>");
661 710
662 if (!ABSOLUTE_PATH (fname)) 711 if (!ABSOLUTE_PATH (fname))
663 { 712 {
664 char *rname = fname; 713 char *rname = fname;
665 asprintf (&fname, "%s/%s", confbase, rname); 714 asprintf (&fname, "%s/%s", confbase, rname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines