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 |
… | |
… | |
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> |
… | |
… | |
151 | default_node.if_up_data = strdup (""); |
152 | default_node.if_up_data = strdup (""); |
152 | |
153 | |
153 | #if ENABLE_DNS |
154 | #if ENABLE_DNS |
154 | default_node.dns_port = 0; // default is 0 == client |
155 | default_node.dns_port = 0; // default is 0 == client |
155 | |
156 | |
|
|
157 | dns_case_preserving = true; |
156 | dns_forw_host = strdup ("127.0.0.1"); |
158 | dns_forw_host = strdup ("127.0.0.1"); |
157 | dns_forw_port = 53; |
159 | dns_forw_port = 53; |
158 | dns_timeout_factor = DEFAULT_DNS_TIMEOUT_FACTOR; |
160 | dns_timeout_factor = DEFAULT_DNS_TIMEOUT_FACTOR; |
159 | dns_send_interval = DEFAULT_DNS_SEND_INTERVAL; |
161 | dns_send_interval = DEFAULT_DNS_SEND_INTERVAL; |
160 | dns_overlap_factor = DEFAULT_DNS_OVERLAP_FACTOR; |
162 | dns_overlap_factor = DEFAULT_DNS_OVERLAP_FACTOR; |
161 | dns_max_outstanding = DEFAULT_DNS_MAX_OUTSTANDING; |
163 | dns_max_outstanding = DEFAULT_DNS_MAX_OUTSTANDING; |
162 | #endif |
164 | #endif |
163 | |
165 | |
164 | conf.pidfilename = strdup (LOCALSTATEDIR "/run/gvpe.pid"); |
166 | pidfilename = strdup (LOCALSTATEDIR "/run/gvpe.pid"); |
|
|
167 | seed_dev = strdup ("/dev/urandom"); |
|
|
168 | reseed = DEFAULT_RESEED; |
165 | } |
169 | } |
166 | |
170 | |
167 | void |
171 | void |
168 | configuration::cleanup () |
172 | configuration::cleanup () |
169 | { |
173 | { |
170 | if (rsa_key) |
174 | if (rsa_key) |
171 | RSA_free (rsa_key); |
175 | RSA_free (rsa_key); |
172 | |
176 | |
173 | rsa_key = 0; |
177 | rsa_key = 0; |
174 | |
178 | |
|
|
179 | free (seed_dev); seed_dev = 0; |
175 | free (pidfilename); pidfilename = 0; |
180 | free (pidfilename); pidfilename = 0; |
176 | free (ifname); ifname = 0; |
181 | free (ifname); ifname = 0; |
177 | #if ENABLE_HTTP_PROXY |
182 | #if ENABLE_HTTP_PROXY |
178 | free (proxy_host); proxy_host = 0; |
183 | free (proxy_host); proxy_host = 0; |
179 | free (proxy_auth); proxy_auth = 0; |
184 | free (proxy_auth); proxy_auth = 0; |
180 | #endif |
185 | #endif |
181 | #if ENABLE_DNS |
186 | #if ENABLE_DNS |
182 | free (dns_forw_host); dns_forw_host = 0; |
187 | free (dns_forw_host); dns_forw_host = 0; |
183 | #endif |
188 | #endif |
|
|
189 | free (change_root); change_root = 0; |
184 | free (script_if_up); script_if_up = 0; |
190 | free (script_if_up); script_if_up = 0; |
185 | free (script_node_up); script_node_up = 0; |
191 | free (script_node_up); script_node_up = 0; |
186 | free (script_node_change); script_node_change = 0; |
192 | free (script_node_change); script_node_change = 0; |
187 | free (script_node_down); script_node_down = 0; |
193 | free (script_node_down); script_node_down = 0; |
188 | } |
194 | } |
… | |
… | |
196 | nodes.clear (); |
202 | nodes.clear (); |
197 | |
203 | |
198 | cleanup (); |
204 | cleanup (); |
199 | init (); |
205 | init (); |
200 | } |
206 | } |
|
|
207 | |
|
|
208 | //static bool |
|
|
209 | //is_true (const char *name) |
|
|
210 | //{ |
|
|
211 | //re |
|
|
212 | //} |
201 | |
213 | |
202 | #define parse_bool(target,name,trueval,falseval) do { \ |
214 | #define parse_bool(target,name,trueval,falseval) do { \ |
203 | if (!strcmp (val, "yes")) target = trueval; \ |
215 | if (!strcmp (val, "yes")) target = trueval; \ |
204 | else if (!strcmp (val, "no")) target = falseval; \ |
216 | else if (!strcmp (val, "no")) target = falseval; \ |
205 | else if (!strcmp (val, "true")) target = trueval; \ |
217 | else if (!strcmp (val, "true")) target = trueval; \ |
206 | else if (!strcmp (val, "false")) target = falseval; \ |
218 | else if (!strcmp (val, "false")) target = falseval; \ |
207 | else if (!strcmp (val, "on")) target = trueval; \ |
219 | else if (!strcmp (val, "on")) target = trueval; \ |
208 | else if (!strcmp (val, "off")) target = falseval; \ |
220 | else if (!strcmp (val, "off")) target = falseval; \ |
209 | else \ |
221 | else \ |
210 | return _("illegal boolean value, only 'yes|true|on' or 'no|false|off' allowed. (ignored)"); \ |
222 | return _("illegal boolean value, only 'yes|true|on' or 'no|false|off' allowed, ignored"); \ |
211 | } while (0) |
223 | } while (0) |
212 | |
224 | |
213 | const char * |
225 | const char * |
214 | configuration_parser::parse_line (char *line) |
226 | configuration_parser::parse_line (char *line) |
215 | { |
227 | { |
… | |
… | |
233 | return 0; /* comment: ignore */ |
245 | return 0; /* comment: ignore */ |
234 | |
246 | |
235 | char *val = strtok (NULL, "\t\n\r ="); |
247 | char *val = strtok (NULL, "\t\n\r ="); |
236 | |
248 | |
237 | if (!val || val[0] == '#') |
249 | if (!val || val[0] == '#') |
238 | return _("no value given for variable. (ignored)"); |
250 | return _("no value given for variable, ignored"); |
239 | |
251 | |
240 | if (!strcmp (var, "on")) |
252 | else if (!strcmp (var, "on")) |
241 | { |
253 | { |
242 | if (!::thisnode |
254 | if (::thisnode |
243 | || (val[0] == '!' && strcmp (val + 1, ::thisnode)) |
255 | && ((val[0] == '!' && strcmp (val + 1, ::thisnode)) |
244 | || !strcmp (val, ::thisnode)) |
256 | || !strcmp (val, ::thisnode))) |
245 | return parse_line (strtok (NULL, "\n\r")); |
257 | return parse_line (strtok (NULL, "\n\r")); |
246 | else |
258 | } |
247 | return 0; |
259 | |
|
|
260 | else if (!strcmp (var, "include")) |
|
|
261 | { |
|
|
262 | char *fname = conf.config_filename (val); |
|
|
263 | parse_file (fname); |
|
|
264 | free (fname); |
248 | } |
265 | } |
249 | |
266 | |
250 | // truly global |
267 | // truly global |
251 | if (!strcmp (var, "loglevel")) |
268 | else if (!strcmp (var, "loglevel")) |
252 | { |
269 | { |
253 | loglevel l = string_to_loglevel (val); |
270 | loglevel l = string_to_loglevel (val); |
254 | |
271 | |
255 | if (l == L_NONE) |
272 | if (l == L_NONE) |
256 | return _("unknown loglevel. (skipping)"); |
273 | return _("unknown loglevel, ignored"); |
257 | } |
274 | } |
258 | else if (!strcmp (var, "ip-proto")) |
275 | else if (!strcmp (var, "ip-proto")) |
259 | conf.ip_proto = atoi (val); |
276 | conf.ip_proto = atoi (val); |
260 | else if (!strcmp (var, "icmp-type")) |
277 | else if (!strcmp (var, "icmp-type")) |
261 | { |
278 | { |
262 | #if ENABLE_ICMP |
279 | #if ENABLE_ICMP |
263 | conf.icmp_type = atoi (val); |
280 | conf.icmp_type = atoi (val); |
264 | #endif |
281 | #endif |
265 | } |
282 | } |
|
|
283 | else if (!strcmp (var, "chuser")) |
|
|
284 | { |
|
|
285 | struct passwd *pw = getpwnam (val); |
|
|
286 | if (!pw) |
|
|
287 | return _("user specified for chuser not found"); |
266 | |
288 | |
267 | // per config |
289 | conf.change_uid = pw->pw_uid; |
|
|
290 | conf.change_gid = pw->pw_gid; |
|
|
291 | } |
|
|
292 | else if (!strcmp (var, "chuid")) |
|
|
293 | conf.change_uid = atoi (val); |
|
|
294 | else if (!strcmp (var, "chgid")) |
|
|
295 | conf.change_gid = atoi (val); |
|
|
296 | else if (!strcmp (var, "chroot")) |
|
|
297 | free (conf.change_root), conf.change_root = strdup (val); |
|
|
298 | |
|
|
299 | // per node |
268 | else if (!strcmp (var, "node")) |
300 | else if (!strcmp (var, "node")) |
269 | { |
301 | { |
270 | parse_argv (); |
302 | parse_argv (); |
271 | |
303 | |
272 | conf.default_node.id++; |
304 | conf.default_node.id++; |
… | |
… | |
322 | conf.keepalive = atoi (val); |
354 | conf.keepalive = atoi (val); |
323 | else if (!strcmp (var, "mtu")) |
355 | else if (!strcmp (var, "mtu")) |
324 | conf.mtu = atoi (val); |
356 | conf.mtu = atoi (val); |
325 | else if (!strcmp (var, "nfmark")) |
357 | else if (!strcmp (var, "nfmark")) |
326 | conf.nfmark = atoi (val); |
358 | conf.nfmark = atoi (val); |
|
|
359 | else if (!strcmp (var, "seed-device")) |
|
|
360 | free (conf.seed_dev), conf.seed_dev = strdup (val); |
|
|
361 | else if (!strcmp (var, "seed-interval")) |
|
|
362 | conf.reseed = atoi (val); |
327 | else if (!strcmp (var, "if-up")) |
363 | else if (!strcmp (var, "if-up")) |
328 | free (conf.script_if_up), conf.script_if_up = strdup (val); |
364 | free (conf.script_if_up), conf.script_if_up = strdup (val); |
329 | else if (!strcmp (var, "node-up")) |
365 | else if (!strcmp (var, "node-up")) |
330 | free (conf.script_node_up), conf.script_node_up = strdup (val); |
366 | free (conf.script_node_up), conf.script_node_up = strdup (val); |
331 | else if (!strcmp (var, "node-change")) |
367 | else if (!strcmp (var, "node-change")) |
… | |
… | |
368 | { |
404 | { |
369 | #if ENABLE_DNS |
405 | #if ENABLE_DNS |
370 | conf.dns_max_outstanding = atoi (val); |
406 | conf.dns_max_outstanding = atoi (val); |
371 | #endif |
407 | #endif |
372 | } |
408 | } |
|
|
409 | else if (!strcmp (var, "dns-case-preserving")) |
|
|
410 | { |
|
|
411 | #if ENABLE_DNS |
|
|
412 | parse_bool (conf.dns_case_preserving, "dns-case-preserving", true, false); |
|
|
413 | #endif |
|
|
414 | } |
373 | else if (!strcmp (var, "http-proxy-host")) |
415 | else if (!strcmp (var, "http-proxy-host")) |
374 | { |
416 | { |
375 | #if ENABLE_HTTP_PROXY |
417 | #if ENABLE_HTTP_PROXY |
376 | free (conf.proxy_host), conf.proxy_host = strdup (val); |
418 | free (conf.proxy_host), conf.proxy_host = strdup (val); |
377 | #endif |
419 | #endif |
… | |
… | |
431 | else if (!strcmp (val, "always")) |
473 | else if (!strcmp (val, "always")) |
432 | node->connectmode = conf_node::C_ALWAYS; |
474 | node->connectmode = conf_node::C_ALWAYS; |
433 | else if (!strcmp (val, "disabled")) |
475 | else if (!strcmp (val, "disabled")) |
434 | node->connectmode = conf_node::C_DISABLED; |
476 | node->connectmode = conf_node::C_DISABLED; |
435 | else |
477 | else |
436 | return _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled'. (ignored)"); |
478 | return _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', ignored"); |
437 | } |
479 | } |
438 | else if (!strcmp (var, "inherit-tos")) |
480 | else if (!strcmp (var, "inherit-tos")) |
439 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
481 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
440 | else if (!strcmp (var, "compress")) |
482 | else if (!strcmp (var, "compress")) |
441 | parse_bool (node->compress, "compress", true, false); |
483 | parse_bool (node->compress, "compress", true, false); |
… | |
… | |
475 | else if (!strcmp (var, "max-queue")) |
517 | else if (!strcmp (var, "max-queue")) |
476 | node->max_queue = atoi (val); |
518 | node->max_queue = atoi (val); |
477 | |
519 | |
478 | // unknown or misplaced |
520 | // unknown or misplaced |
479 | else |
521 | else |
480 | return _("unknown configuration directive. (ignored)"); |
522 | return _("unknown configuration directive - ignored"); |
481 | |
523 | |
482 | return 0; |
524 | return 0; |
483 | } |
525 | } |
484 | |
526 | |
485 | void |
527 | void |
… | |
… | |
528 | if (warn) |
570 | if (warn) |
529 | slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v); |
571 | slog (L_WARN, _("%s, while parsing command line option '%s'."), warn, v); |
530 | |
572 | |
531 | *v = 0; |
573 | *v = 0; |
532 | } |
574 | } |
|
|
575 | } |
|
|
576 | } |
|
|
577 | |
|
|
578 | void |
|
|
579 | configuration_parser::parse_file (const char *fname) |
|
|
580 | { |
|
|
581 | if (FILE *f = fopen (fname, "r")) |
|
|
582 | { |
|
|
583 | char line [2048]; |
|
|
584 | int lineno = 0; |
|
|
585 | |
|
|
586 | while (fgets (line, sizeof (line), f)) |
|
|
587 | { |
|
|
588 | lineno++; |
|
|
589 | |
|
|
590 | const char *warn = parse_line (line); |
|
|
591 | |
|
|
592 | if (warn) |
|
|
593 | slog (L_WARN, _("%s, at '%s', line %d."), warn, fname, lineno); |
|
|
594 | } |
|
|
595 | |
|
|
596 | fclose (f); |
|
|
597 | |
|
|
598 | parse_argv (); |
|
|
599 | } |
|
|
600 | else |
|
|
601 | { |
|
|
602 | slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
|
|
603 | exit (EXIT_FAILURE); |
533 | } |
604 | } |
534 | } |
605 | } |
535 | |
606 | |
536 | configuration_parser::configuration_parser (configuration &conf, |
607 | configuration_parser::configuration_parser (configuration &conf, |
537 | bool need_keys, |
608 | bool need_keys, |
538 | int argc, |
609 | int argc, |
539 | char **argv) |
610 | char **argv) |
540 | : conf (conf),need_keys (need_keys), argc (argc), argv (argv) |
611 | : conf (conf),need_keys (need_keys), argc (argc), argv (argv) |
541 | { |
612 | { |
542 | char *fname; |
613 | char *fname; |
543 | FILE *f; |
|
|
544 | |
614 | |
545 | conf.clear (); |
615 | conf.clear (); |
|
|
616 | node = &conf.default_node; |
546 | |
617 | |
547 | asprintf (&fname, "%s/gvpe.conf", confbase); |
618 | asprintf (&fname, "%s/gvpe.conf", confbase); |
548 | f = fopen (fname, "r"); |
619 | 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); |
620 | free (fname); |
577 | |
621 | |
578 | fname = conf.config_filename (conf.prikeyfile, "hostkey"); |
622 | fname = conf.config_filename (conf.prikeyfile, "hostkey"); |
579 | |
623 | |
580 | f = fopen (fname, "r"); |
624 | if (FILE *f = fopen (fname, "r")) |
581 | if (f) |
|
|
582 | { |
625 | { |
583 | conf.rsa_key = RSA_new (); |
626 | conf.rsa_key = RSA_new (); |
584 | |
627 | |
585 | if (!PEM_read_RSAPrivateKey (f, &conf.rsa_key, NULL, NULL)) |
628 | if (!PEM_read_RSAPrivateKey (f, &conf.rsa_key, NULL, NULL)) |
586 | { |
629 | { |
… | |
… | |
593 | |
636 | |
594 | fclose (f); |
637 | fclose (f); |
595 | } |
638 | } |
596 | else |
639 | else |
597 | { |
640 | { |
598 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
|
|
599 | |
|
|
600 | if (need_keys) |
641 | if (need_keys) |
|
|
642 | { |
|
|
643 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
601 | exit (EXIT_FAILURE); |
644 | exit (EXIT_FAILURE); |
|
|
645 | } |
602 | } |
646 | } |
|
|
647 | |
|
|
648 | free (fname); |
603 | |
649 | |
604 | if (need_keys && ::thisnode |
650 | if (need_keys && ::thisnode |
605 | && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) |
651 | && conf.rsa_key && conf.thisnode && conf.thisnode->rsa_key) |
606 | if (BN_cmp (conf.rsa_key->n, conf.thisnode->rsa_key->n) != 0 |
652 | 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) |
653 | || BN_cmp (conf.rsa_key->e, conf.thisnode->rsa_key->e) != 0) |
608 | { |
654 | { |
609 | slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode); |
655 | slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode); |
610 | exit (EXIT_FAILURE); |
656 | exit (EXIT_FAILURE); |
611 | } |
657 | } |
612 | |
658 | |
613 | free (fname); |
659 | fname = conf.config_filename (conf.pidfilename); |
|
|
660 | free (conf.pidfilename); conf.pidfilename = fname; |
614 | |
661 | |
615 | for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i) |
662 | for (configuration::node_vector::iterator i = conf.nodes.begin(); i != conf.nodes.end(); ++i) |
616 | (*i)->finalise (); |
663 | (*i)->finalise (); |
617 | } |
664 | } |
618 | |
665 | |
619 | char * |
666 | char * |
620 | configuration::config_filename (const char *name, const char *dflt) |
667 | configuration::config_filename (const char *name, const char *dflt) |
621 | { |
668 | { |
622 | char *fname; |
669 | char *fname; |
623 | |
670 | |
624 | asprintf (&fname, name ? name : dflt, ::thisnode); |
671 | asprintf (&fname, name ? name : dflt, ::thisnode ? ::thisnode : "<unset>"); |
625 | |
672 | |
626 | if (!ABSOLUTE_PATH (fname)) |
673 | if (!ABSOLUTE_PATH (fname)) |
627 | { |
674 | { |
628 | char *rname = fname; |
675 | char *rname = fname; |
629 | asprintf (&fname, "%s/%s", confbase, rname); |
676 | asprintf (&fname, "%s/%s", confbase, rname); |