… | |
… | |
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 | |
169 | void |
171 | void |
170 | configuration::cleanup () |
172 | configuration::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 | |
|
|
208 | conf_node * |
|
|
209 | configuration::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 | |
… | |
… | |
294 | free (conf.change_root), conf.change_root = strdup (val); |
313 | free (conf.change_root), conf.change_root = strdup (val); |
295 | |
314 | |
296 | // per node |
315 | // per node |
297 | else if (!strcmp (var, "node")) |
316 | else if (!strcmp (var, "node")) |
298 | { |
317 | { |
299 | parse_argv (); |
318 | node = conf.find_node (val); |
300 | |
319 | |
|
|
320 | if (!node) |
|
|
321 | { |
301 | conf.default_node.id++; |
322 | conf.default_node.id++; |
302 | node = new conf_node (conf.default_node); |
323 | node = new conf_node (conf.default_node); |
303 | conf.nodes.push_back (node); |
324 | conf.nodes.push_back (node); |
304 | node->nodename = strdup (val); |
325 | 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 | } |
326 | } |
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 | } |
327 | } |
342 | else if (!strcmp (var, "private-key")) |
328 | else if (!strcmp (var, "private-key")) |
343 | free (conf.prikeyfile), conf.prikeyfile = strdup (val); |
329 | free (conf.prikeyfile), conf.prikeyfile = strdup (val); |
344 | else if (!strcmp (var, "ifpersist")) |
330 | else if (!strcmp (var, "ifpersist")) |
345 | parse_bool (conf.ifpersist, "ifpersist", true, false); |
331 | parse_bool (conf.ifpersist, "ifpersist", true, false); |
… | |
… | |
351 | conf.keepalive = atoi (val); |
337 | conf.keepalive = atoi (val); |
352 | else if (!strcmp (var, "mtu")) |
338 | else if (!strcmp (var, "mtu")) |
353 | conf.mtu = atoi (val); |
339 | conf.mtu = atoi (val); |
354 | else if (!strcmp (var, "nfmark")) |
340 | else if (!strcmp (var, "nfmark")) |
355 | 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); |
356 | else if (!strcmp (var, "if-up")) |
346 | else if (!strcmp (var, "if-up")) |
357 | free (conf.script_if_up), conf.script_if_up = strdup (val); |
347 | free (conf.script_if_up), conf.script_if_up = strdup (val); |
358 | else if (!strcmp (var, "node-up")) |
348 | else if (!strcmp (var, "node-up")) |
359 | free (conf.script_node_up), conf.script_node_up = strdup (val); |
349 | free (conf.script_node_up), conf.script_node_up = strdup (val); |
360 | else if (!strcmp (var, "node-change")) |
350 | else if (!strcmp (var, "node-change")) |
… | |
… | |
532 | connectmode = C_ALWAYS; |
522 | connectmode = C_ALWAYS; |
533 | } |
523 | } |
534 | } |
524 | } |
535 | |
525 | |
536 | void |
526 | void |
537 | configuration_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 | |
|
|
571 | void |
|
|
572 | configuration_parser::parse_file (const char *fname) |
527 | configuration_parser::parse_file (const char *fname) |
573 | { |
528 | { |
574 | if (FILE *f = fopen (fname, "r")) |
529 | if (FILE *f = fopen (fname, "r")) |
575 | { |
530 | { |
576 | char line [2048]; |
531 | char line [2048]; |
… | |
… | |
585 | if (warn) |
540 | if (warn) |
586 | slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); |
541 | slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); |
587 | } |
542 | } |
588 | |
543 | |
589 | fclose (f); |
544 | fclose (f); |
590 | |
|
|
591 | parse_argv (); |
|
|
592 | } |
545 | } |
593 | else |
546 | else |
594 | { |
547 | { |
595 | 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)); |
596 | exit (EXIT_FAILURE); |
549 | exit (EXIT_FAILURE); |
… | |
… | |
599 | |
552 | |
600 | configuration_parser::configuration_parser (configuration &conf, |
553 | configuration_parser::configuration_parser (configuration &conf, |
601 | bool need_keys, |
554 | bool need_keys, |
602 | int argc, |
555 | int argc, |
603 | char **argv) |
556 | char **argv) |
604 | : conf (conf),need_keys (need_keys), argc (argc), argv (argv) |
557 | : conf (conf), need_keys (need_keys), argc (argc), argv (argv) |
605 | { |
558 | { |
606 | char *fname; |
559 | char *fname; |
607 | |
560 | |
608 | conf.clear (); |
561 | conf.clear (); |
609 | node = &conf.default_node; |
562 | node = &conf.default_node; |
… | |
… | |
629 | |
582 | |
630 | fclose (f); |
583 | fclose (f); |
631 | } |
584 | } |
632 | else |
585 | else |
633 | { |
586 | { |
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) |
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)); |
637 | exit (EXIT_FAILURE); |
590 | exit (EXIT_FAILURE); |
|
|
591 | } |
638 | } |
592 | } |
639 | |
593 | |
640 | free (fname); |
594 | free (fname); |
641 | |
595 | |
642 | if (need_keys && ::thisnode |
596 | fname = conf.config_filename (conf.pidfilename); |
643 | && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) |
597 | 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 | |
598 | |
651 | 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 | |
652 | (*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 | |
|
|
661 | void |
|
|
662 | configuration_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 | } |
653 | } |
700 | } |
654 | |
701 | |
655 | char * |
702 | char * |
656 | configuration::config_filename (const char *name, const char *dflt) |
703 | configuration::config_filename (const char *name, const char *dflt) |
657 | { |
704 | { |
658 | char *fname; |
705 | char *fname; |
659 | |
706 | |
660 | asprintf (&fname, name ? name : dflt, ::thisnode); |
707 | asprintf (&fname, name ? name : dflt, ::thisnode ? ::thisnode : "<unset>"); |
661 | |
708 | |
662 | if (!ABSOLUTE_PATH (fname)) |
709 | if (!ABSOLUTE_PATH (fname)) |
663 | { |
710 | { |
664 | char *rname = fname; |
711 | char *rname = fname; |
665 | asprintf (&fname, "%s/%s", confbase, rname); |
712 | asprintf (&fname, "%s/%s", confbase, rname); |