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.56 by root, Sun Mar 6 21:01:36 2011 UTC vs.
Revision 1.63 by root, Thu Jul 18 13:35:16 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//{
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
273 { 294 {
274#if ENABLE_ICMP 295#if ENABLE_ICMP
275 conf.icmp_type = atoi (val); 296 conf.icmp_type = atoi (val);
276#endif 297#endif
277 } 298 }
299 else if (!strcmp (var, "chuser"))
300 {
301 struct passwd *pw = getpwnam (val);
302 if (!pw)
303 return _("user specified for chuser not found");
278 304
279 // per config 305 conf.change_uid = pw->pw_uid;
306 conf.change_gid = pw->pw_gid;
307 }
308 else if (!strcmp (var, "chuid"))
309 conf.change_uid = atoi (val);
310 else if (!strcmp (var, "chgid"))
311 conf.change_gid = atoi (val);
312 else if (!strcmp (var, "chroot"))
313 free (conf.change_root), conf.change_root = strdup (val);
314
315 // per node
280 else if (!strcmp (var, "node")) 316 else if (!strcmp (var, "node"))
281 { 317 {
282 parse_argv (); 318 node = conf.find_node (val);
283 319
320 if (!node)
321 {
284 conf.default_node.id++; 322 conf.default_node.id++;
285 node = new conf_node (conf.default_node); 323 node = new conf_node (conf.default_node);
286 conf.nodes.push_back (node); 324 conf.nodes.push_back (node);
287 node->nodename = strdup (val); 325 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 } 326 }
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 } 327 }
325 else if (!strcmp (var, "private-key")) 328 else if (!strcmp (var, "private-key"))
326 free (conf.prikeyfile), conf.prikeyfile = strdup (val); 329 free (conf.prikeyfile), conf.prikeyfile = strdup (val);
327 else if (!strcmp (var, "ifpersist")) 330 else if (!strcmp (var, "ifpersist"))
328 parse_bool (conf.ifpersist, "ifpersist", true, false); 331 parse_bool (conf.ifpersist, "ifpersist", true, false);
334 conf.keepalive = atoi (val); 337 conf.keepalive = atoi (val);
335 else if (!strcmp (var, "mtu")) 338 else if (!strcmp (var, "mtu"))
336 conf.mtu = atoi (val); 339 conf.mtu = atoi (val);
337 else if (!strcmp (var, "nfmark")) 340 else if (!strcmp (var, "nfmark"))
338 conf.nfmark = atoi (val); 341 conf.nfmark = atoi (val);
342 else if (!strcmp (var, "seed-device"))
343 free (conf.seed_dev), conf.seed_dev = strdup (val);
344 else if (!strcmp (var, "seed-interval"))
345 conf.reseed = atoi (val);
339 else if (!strcmp (var, "if-up")) 346 else if (!strcmp (var, "if-up"))
340 free (conf.script_if_up), conf.script_if_up = strdup (val); 347 free (conf.script_if_up), conf.script_if_up = strdup (val);
341 else if (!strcmp (var, "node-up")) 348 else if (!strcmp (var, "node-up"))
342 free (conf.script_node_up), conf.script_node_up = strdup (val); 349 free (conf.script_node_up), conf.script_node_up = strdup (val);
343 else if (!strcmp (var, "node-change")) 350 else if (!strcmp (var, "node-change"))
515 connectmode = C_ALWAYS; 522 connectmode = C_ALWAYS;
516 } 523 }
517} 524}
518 525
519void 526void
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) 527configuration_parser::parse_file (const char *fname)
556{ 528{
557 if (FILE *f = fopen (fname, "r")) 529 if (FILE *f = fopen (fname, "r"))
558 { 530 {
559 char line [2048]; 531 char line [2048];
568 if (warn) 540 if (warn)
569 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); 541 slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno);
570 } 542 }
571 543
572 fclose (f); 544 fclose (f);
573
574 parse_argv ();
575 } 545 }
576 else 546 else
577 { 547 {
578 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));
579 exit (EXIT_FAILURE); 549 exit (EXIT_FAILURE);
582 552
583configuration_parser::configuration_parser (configuration &conf, 553configuration_parser::configuration_parser (configuration &conf,
584 bool need_keys, 554 bool need_keys,
585 int argc, 555 int argc,
586 char **argv) 556 char **argv)
587: conf (conf),need_keys (need_keys), argc (argc), argv (argv) 557: conf (conf), need_keys (need_keys), argc (argc), argv (argv)
588{ 558{
589 char *fname; 559 char *fname;
590 560
591 conf.clear (); 561 conf.clear ();
592 node = &conf.default_node; 562 node = &conf.default_node;
612 582
613 fclose (f); 583 fclose (f);
614 } 584 }
615 else 585 else
616 { 586 {
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) 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));
620 exit (EXIT_FAILURE); 590 exit (EXIT_FAILURE);
591 }
621 } 592 }
622 593
623 free (fname); 594 free (fname);
624 595
625 if (need_keys && ::thisnode 596 fname = conf.config_filename (conf.pidfilename);
626 && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) 597 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 598
634 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
635 (*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 }
636} 700}
637 701
638char * 702char *
639configuration::config_filename (const char *name, const char *dflt) 703configuration::config_filename (const char *name, const char *dflt)
640{ 704{
641 char *fname; 705 char *fname;
642 706
643 asprintf (&fname, name ? name : dflt, ::thisnode); 707 asprintf (&fname, name ? name : dflt, ::thisnode ? ::thisnode : "<unset>");
644 708
645 if (!ABSOLUTE_PATH (fname)) 709 if (!ABSOLUTE_PATH (fname))
646 { 710 {
647 char *rname = fname; 711 char *rname = fname;
648 asprintf (&fname, "%s/%s", confbase, rname); 712 asprintf (&fname, "%s/%s", confbase, rname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines