1 | /* |
1 | /* |
2 | conf.c -- configuration code |
2 | conf.C -- configuration code |
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2008,2011 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify it |
7 | GVPE is free software; you can redistribute it and/or modify it |
8 | under the terms of the GNU General Public License as published by the |
8 | under the terms of the GNU General Public License as published by the |
… | |
… | |
151 | default_node.if_up_data = strdup (""); |
151 | default_node.if_up_data = strdup (""); |
152 | |
152 | |
153 | #if ENABLE_DNS |
153 | #if ENABLE_DNS |
154 | default_node.dns_port = 0; // default is 0 == client |
154 | default_node.dns_port = 0; // default is 0 == client |
155 | |
155 | |
|
|
156 | dns_case_preserving = true; |
156 | dns_forw_host = strdup ("127.0.0.1"); |
157 | dns_forw_host = strdup ("127.0.0.1"); |
157 | dns_forw_port = 53; |
158 | dns_forw_port = 53; |
158 | dns_timeout_factor = DEFAULT_DNS_TIMEOUT_FACTOR; |
159 | dns_timeout_factor = DEFAULT_DNS_TIMEOUT_FACTOR; |
159 | dns_send_interval = DEFAULT_DNS_SEND_INTERVAL; |
160 | dns_send_interval = DEFAULT_DNS_SEND_INTERVAL; |
160 | dns_overlap_factor = DEFAULT_DNS_OVERLAP_FACTOR; |
161 | dns_overlap_factor = DEFAULT_DNS_OVERLAP_FACTOR; |
… | |
… | |
196 | nodes.clear (); |
197 | nodes.clear (); |
197 | |
198 | |
198 | cleanup (); |
199 | cleanup (); |
199 | init (); |
200 | init (); |
200 | } |
201 | } |
|
|
202 | |
|
|
203 | //static bool |
|
|
204 | //is_true (const char *name) |
|
|
205 | //{ |
|
|
206 | //re |
|
|
207 | //} |
201 | |
208 | |
202 | #define parse_bool(target,name,trueval,falseval) do { \ |
209 | #define parse_bool(target,name,trueval,falseval) do { \ |
203 | if (!strcmp (val, "yes")) target = trueval; \ |
210 | if (!strcmp (val, "yes")) target = trueval; \ |
204 | else if (!strcmp (val, "no")) target = falseval; \ |
211 | else if (!strcmp (val, "no")) target = falseval; \ |
205 | else if (!strcmp (val, "true")) target = trueval; \ |
212 | else if (!strcmp (val, "true")) target = trueval; \ |
… | |
… | |
235 | char *val = strtok (NULL, "\t\n\r ="); |
242 | char *val = strtok (NULL, "\t\n\r ="); |
236 | |
243 | |
237 | if (!val || val[0] == '#') |
244 | if (!val || val[0] == '#') |
238 | return _("no value given for variable. (ignored)"); |
245 | return _("no value given for variable. (ignored)"); |
239 | |
246 | |
240 | if (!strcmp (var, "on")) |
247 | else if (!strcmp (var, "on")) |
241 | { |
248 | { |
242 | if (!::thisnode |
249 | if (!::thisnode |
243 | || (val[0] == '!' && strcmp (val + 1, ::thisnode)) |
250 | || (val[0] == '!' && strcmp (val + 1, ::thisnode)) |
244 | || !strcmp (val, ::thisnode)) |
251 | || !strcmp (val, ::thisnode)) |
245 | return parse_line (strtok (NULL, "\n\r")); |
252 | return parse_line (strtok (NULL, "\n\r")); |
246 | else |
253 | } |
247 | return 0; |
254 | |
|
|
255 | else if (!strcmp (var, "include")) |
|
|
256 | { |
|
|
257 | char *fname = conf.config_filename (val); |
|
|
258 | parse_file (fname); |
|
|
259 | free (fname); |
248 | } |
260 | } |
249 | |
261 | |
250 | // truly global |
262 | // truly global |
251 | if (!strcmp (var, "loglevel")) |
263 | else if (!strcmp (var, "loglevel")) |
252 | { |
264 | { |
253 | loglevel l = string_to_loglevel (val); |
265 | loglevel l = string_to_loglevel (val); |
254 | |
266 | |
255 | if (l == L_NONE) |
267 | if (l == L_NONE) |
256 | return _("unknown loglevel. (skipping)"); |
268 | return _("unknown loglevel. (skipping)"); |
… | |
… | |
368 | { |
380 | { |
369 | #if ENABLE_DNS |
381 | #if ENABLE_DNS |
370 | conf.dns_max_outstanding = atoi (val); |
382 | conf.dns_max_outstanding = atoi (val); |
371 | #endif |
383 | #endif |
372 | } |
384 | } |
|
|
385 | else if (!strcmp (var, "dns-case-preserving")) |
|
|
386 | { |
|
|
387 | #if ENABLE_DNS |
|
|
388 | parse_bool (conf.dns_case_preserving, "dns-case-preserving", true, false); |
|
|
389 | #endif |
|
|
390 | } |
373 | else if (!strcmp (var, "http-proxy-host")) |
391 | else if (!strcmp (var, "http-proxy-host")) |
374 | { |
392 | { |
375 | #if ENABLE_HTTP_PROXY |
393 | #if ENABLE_HTTP_PROXY |
376 | free (conf.proxy_host), conf.proxy_host = strdup (val); |
394 | free (conf.proxy_host), conf.proxy_host = strdup (val); |
377 | #endif |
395 | #endif |
… | |
… | |
475 | else if (!strcmp (var, "max-queue")) |
493 | else if (!strcmp (var, "max-queue")) |
476 | node->max_queue = atoi (val); |
494 | node->max_queue = atoi (val); |
477 | |
495 | |
478 | // unknown or misplaced |
496 | // unknown or misplaced |
479 | else |
497 | else |
480 | return _("unknown configuration directive. (ignored)"); |
498 | return _("unknown configuration directive - ignored"); |
481 | |
499 | |
482 | return 0; |
500 | return 0; |
483 | } |
501 | } |
484 | |
502 | |
485 | void |
503 | void |
… | |
… | |
528 | if (warn) |
546 | if (warn) |
529 | slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v); |
547 | slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v); |
530 | |
548 | |
531 | *v = 0; |
549 | *v = 0; |
532 | } |
550 | } |
|
|
551 | } |
|
|
552 | } |
|
|
553 | |
|
|
554 | void |
|
|
555 | configuration_parser::parse_file (const char *fname) |
|
|
556 | { |
|
|
557 | if (FILE *f = fopen (fname, "r")) |
|
|
558 | { |
|
|
559 | char line [2048]; |
|
|
560 | int lineno = 0; |
|
|
561 | node = &conf.default_node; |
|
|
562 | |
|
|
563 | while (fgets (line, sizeof (line), f)) |
|
|
564 | { |
|
|
565 | lineno++; |
|
|
566 | |
|
|
567 | const char *warn = parse_line (line); |
|
|
568 | |
|
|
569 | if (warn) |
|
|
570 | slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); |
|
|
571 | } |
|
|
572 | |
|
|
573 | fclose (f); |
|
|
574 | |
|
|
575 | parse_argv (); |
|
|
576 | } |
|
|
577 | else |
|
|
578 | { |
|
|
579 | slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
|
|
580 | exit (EXIT_FAILURE); |
533 | } |
581 | } |
534 | } |
582 | } |
535 | |
583 | |
536 | configuration_parser::configuration_parser (configuration &conf, |
584 | configuration_parser::configuration_parser (configuration &conf, |
537 | bool need_keys, |
585 | bool need_keys, |
538 | int argc, |
586 | int argc, |
539 | char **argv) |
587 | char **argv) |
540 | : conf (conf),need_keys (need_keys), argc (argc), argv (argv) |
588 | : conf (conf),need_keys (need_keys), argc (argc), argv (argv) |
541 | { |
589 | { |
542 | char *fname; |
590 | char *fname; |
543 | FILE *f; |
|
|
544 | |
591 | |
545 | conf.clear (); |
592 | conf.clear (); |
546 | |
593 | |
547 | asprintf (&fname, "%s/gvpe.conf", confbase); |
594 | asprintf (&fname, "%s/gvpe.conf", confbase); |
548 | f = fopen (fname, "r"); |
595 | parse_file (fname); |
549 | |
|
|
550 | if (f) |
|
|
551 | { |
|
|
552 | char line[16384]; |
|
|
553 | int lineno = 0; |
|
|
554 | node = &conf.default_node; |
|
|
555 | |
|
|
556 | while (fgets (line, sizeof (line), f)) |
|
|
557 | { |
|
|
558 | lineno++; |
|
|
559 | |
|
|
560 | const char *warn = parse_line (line); |
|
|
561 | |
|
|
562 | if (warn) |
|
|
563 | slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); |
|
|
564 | } |
|
|
565 | |
|
|
566 | fclose (f); |
|
|
567 | |
|
|
568 | parse_argv (); |
|
|
569 | } |
|
|
570 | else |
|
|
571 | { |
|
|
572 | slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
|
|
573 | exit (EXIT_FAILURE); |
|
|
574 | } |
|
|
575 | |
|
|
576 | free (fname); |
596 | free (fname); |
577 | |
597 | |
578 | fname = conf.config_filename (conf.prikeyfile, "hostkey"); |
598 | fname = conf.config_filename (conf.prikeyfile, "hostkey"); |
579 | |
599 | |
580 | f = fopen (fname, "r"); |
600 | if (FILE *f = fopen (fname, "r")) |
581 | if (f) |
|
|
582 | { |
601 | { |
583 | conf.rsa_key = RSA_new (); |
602 | conf.rsa_key = RSA_new (); |
584 | |
603 | |
585 | if (!PEM_read_RSAPrivateKey (f, &conf.rsa_key, NULL, NULL)) |
604 | if (!PEM_read_RSAPrivateKey (f, &conf.rsa_key, NULL, NULL)) |
586 | { |
605 | { |
… | |
… | |
598 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
617 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
599 | |
618 | |
600 | if (need_keys) |
619 | if (need_keys) |
601 | exit (EXIT_FAILURE); |
620 | exit (EXIT_FAILURE); |
602 | } |
621 | } |
|
|
622 | |
|
|
623 | free (fname); |
603 | |
624 | |
604 | if (need_keys && ::thisnode |
625 | if (need_keys && ::thisnode |
605 | && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) |
626 | && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) |
606 | if (BN_cmp (conf.rsa_key->n, conf.thisnode->rsa_key->n) != 0 |
627 | if (BN_cmp (conf.rsa_key->n, conf.thisnode->rsa_key->n) != 0 |
607 | || BN_cmp (conf.rsa_key->e, conf.thisnode->rsa_key->e) != 0) |
628 | || BN_cmp (conf.rsa_key->e, conf.thisnode->rsa_key->e) != 0) |
608 | { |
629 | { |
609 | slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode); |
630 | slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode); |
610 | exit (EXIT_FAILURE); |
631 | exit (EXIT_FAILURE); |
611 | } |
632 | } |
612 | |
|
|
613 | free (fname); |
|
|
614 | |
633 | |
615 | for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i) |
634 | for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i) |
616 | (*i)->finalise (); |
635 | (*i)->finalise (); |
617 | } |
636 | } |
618 | |
637 | |