… | |
… | |
33 | |
33 | |
34 | #include <openssl/err.h> |
34 | #include <openssl/err.h> |
35 | #include <openssl/pem.h> |
35 | #include <openssl/pem.h> |
36 | #include <openssl/rsa.h> |
36 | #include <openssl/rsa.h> |
37 | #include <openssl/rand.h> |
37 | #include <openssl/rand.h> |
|
|
38 | #include <openssl/bn.h> |
38 | |
39 | |
39 | #include "gettext.h" |
40 | #include "gettext.h" |
40 | |
41 | |
41 | #include "conf.h" |
42 | #include "conf.h" |
42 | #include "slog.h" |
43 | #include "slog.h" |
43 | #include "util.h" |
44 | #include "util.h" |
44 | |
45 | |
45 | char *confbase; |
46 | char *confbase; |
46 | char *thisnode; |
47 | char *thisnode; |
47 | char *identname; |
48 | char *identname; |
48 | char *pidfilename; |
|
|
49 | |
49 | |
50 | struct configuration conf; |
50 | struct configuration conf; |
51 | |
51 | |
52 | u8 best_protocol (u8 protset) |
52 | u8 best_protocol (u8 protset) |
53 | { |
53 | { |
54 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
54 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
55 | if (protset & PROT_ICMPv4) return PROT_ICMPv4; |
55 | if (protset & PROT_ICMPv4) return PROT_ICMPv4; |
56 | if (protset & PROT_UDPv4 ) return PROT_UDPv4; |
56 | if (protset & PROT_UDPv4 ) return PROT_UDPv4; |
57 | if (protset & PROT_TCPv4 ) return PROT_TCPv4; |
57 | if (protset & PROT_TCPv4 ) return PROT_TCPv4; |
|
|
58 | if (protset & PROT_DNSv4 ) return PROT_DNSv4; |
58 | |
59 | |
59 | return 0; |
60 | return 0; |
60 | } |
61 | } |
61 | |
62 | |
62 | const char *strprotocol (u8 protocol) |
63 | const char *strprotocol (u8 protocol) |
63 | { |
64 | { |
64 | if (protocol & PROT_IPv4 ) return "rawip"; |
65 | if (protocol & PROT_IPv4 ) return "rawip"; |
65 | if (protocol & PROT_ICMPv4) return "icmp"; |
66 | if (protocol & PROT_ICMPv4) return "icmp"; |
66 | if (protocol & PROT_UDPv4 ) return "udp"; |
67 | if (protocol & PROT_UDPv4 ) return "udp"; |
67 | if (protocol & PROT_TCPv4 ) return "tcp"; |
68 | if (protocol & PROT_TCPv4 ) return "tcp"; |
|
|
69 | if (protocol & PROT_DNSv4 ) return "dns"; |
68 | |
70 | |
69 | return "<unknown>"; |
71 | return "<unknown>"; |
70 | } |
72 | } |
71 | |
73 | |
72 | void |
74 | void |
… | |
… | |
107 | #if ENABLE_ICMP |
109 | #if ENABLE_ICMP |
108 | icmp_type = ICMP_ECHOREPLY; |
110 | icmp_type = ICMP_ECHOREPLY; |
109 | #endif |
111 | #endif |
110 | |
112 | |
111 | default_node.udp_port = DEFAULT_UDPPORT; |
113 | default_node.udp_port = DEFAULT_UDPPORT; |
112 | default_node.tcp_port = DEFAULT_UDPPORT; |
114 | default_node.tcp_port = DEFAULT_UDPPORT; // ehrm |
113 | default_node.connectmode = conf_node::C_ALWAYS; |
115 | default_node.connectmode = conf_node::C_ALWAYS; |
114 | default_node.compress = true; |
116 | default_node.compress = true; |
115 | default_node.protocols = PROT_UDPv4; |
117 | default_node.protocols = PROT_UDPv4; |
|
|
118 | default_node.max_retry = DEFAULT_MAX_RETRY; |
|
|
119 | |
|
|
120 | conf.pidfilename = strdup (LOCALSTATEDIR "/run/gvpe.pid"); |
116 | } |
121 | } |
117 | |
122 | |
118 | void configuration::cleanup() |
123 | void configuration::cleanup() |
119 | { |
124 | { |
120 | if (rsa_key) |
125 | if (rsa_key) |
121 | RSA_free (rsa_key); |
126 | RSA_free (rsa_key); |
122 | |
127 | |
123 | rsa_key = 0; |
128 | rsa_key = 0; |
124 | |
129 | |
|
|
130 | free (pidfilename); pidfilename = 0; |
125 | free (ifname); ifname = 0; |
131 | free (ifname); ifname = 0; |
126 | #if ENABLE_HTTP_PROXY |
132 | #if ENABLE_HTTP_PROXY |
127 | free (proxy_host); proxy_host = 0; |
133 | free (proxy_host); proxy_host = 0; |
128 | free (proxy_auth); proxy_auth = 0; |
134 | free (proxy_auth); proxy_auth = 0; |
129 | #endif |
135 | #endif |
130 | } |
136 | } |
131 | |
137 | |
132 | void |
138 | void |
133 | configuration::clear_config () |
139 | configuration::clear_config () |
… | |
… | |
158 | char *fname; |
164 | char *fname; |
159 | FILE *f; |
165 | FILE *f; |
160 | |
166 | |
161 | clear_config (); |
167 | clear_config (); |
162 | |
168 | |
163 | asprintf (&fname, "%s/vped.conf", confbase); |
169 | asprintf (&fname, "%s/gvpe.conf", confbase); |
164 | f = fopen (fname, "r"); |
170 | f = fopen (fname, "r"); |
165 | |
171 | |
166 | if (f) |
172 | if (f) |
167 | { |
173 | { |
168 | char line[16384]; |
174 | char line[16384]; |
… | |
… | |
258 | |
264 | |
259 | if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) |
265 | if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) |
260 | { |
266 | { |
261 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
267 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
262 | slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
268 | slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
263 | exit (1); |
269 | exit (EXIT_FAILURE); |
264 | } |
270 | } |
265 | |
271 | |
266 | RSA_blinding_on (node->rsa_key, 0); |
272 | require (RSA_blinding_on (node->rsa_key, 0)); |
267 | |
273 | |
268 | fclose (f); |
274 | fclose (f); |
269 | } |
275 | } |
270 | else |
276 | else |
271 | { |
277 | { |
272 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno)); |
278 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno)); |
273 | |
279 | |
274 | if (need_keys) |
280 | if (need_keys) |
275 | exit (1); |
281 | exit (EXIT_FAILURE); |
276 | } |
282 | } |
277 | |
283 | |
278 | free (fname); |
284 | free (fname); |
279 | } |
285 | } |
280 | |
286 | |
281 | if (!::thisnode || !strcmp (node->nodename, ::thisnode)) |
287 | if (::thisnode && !strcmp (node->nodename, ::thisnode)) |
282 | thisnode = node; |
288 | thisnode = node; |
283 | } |
289 | } |
284 | else if (!strcmp (var, "private-key")) |
290 | else if (!strcmp (var, "private-key")) |
285 | prikeyfile = strdup (val); |
291 | free (prikeyfile), prikeyfile = strdup (val); |
286 | else if (!strcmp (var, "ifpersist")) |
292 | else if (!strcmp (var, "ifpersist")) |
287 | { |
293 | { |
288 | parse_bool (ifpersist, "ifpersist", true, false); |
294 | parse_bool (ifpersist, "ifpersist", true, false); |
289 | } |
295 | } |
290 | else if (!strcmp (var, "ifname")) |
296 | else if (!strcmp (var, "ifname")) |
291 | ifname = strdup (val); |
297 | free (ifname), ifname = strdup (val); |
292 | else if (!strcmp (var, "rekey")) |
298 | else if (!strcmp (var, "rekey")) |
293 | rekey = atoi (val); |
299 | rekey = atoi (val); |
294 | else if (!strcmp (var, "keepalive")) |
300 | else if (!strcmp (var, "keepalive")) |
295 | keepalive = atoi (val); |
301 | keepalive = atoi (val); |
296 | else if (!strcmp (var, "mtu")) |
302 | else if (!strcmp (var, "mtu")) |
297 | mtu = atoi (val); |
303 | mtu = atoi (val); |
298 | else if (!strcmp (var, "if-up")) |
304 | else if (!strcmp (var, "if-up")) |
299 | script_if_up = strdup (val); |
305 | free (script_if_up), script_if_up = strdup (val); |
300 | else if (!strcmp (var, "node-up")) |
306 | else if (!strcmp (var, "node-up")) |
301 | script_node_up = strdup (val); |
307 | free (script_node_up), script_node_up = strdup (val); |
302 | else if (!strcmp (var, "node-down")) |
308 | else if (!strcmp (var, "node-down")) |
303 | script_node_down = strdup (val); |
309 | free (script_node_down), script_node_down = strdup (val); |
|
|
310 | else if (!strcmp (var, "pid-file")) |
|
|
311 | free (pidfilename), pidfilename = strdup (val); |
304 | else if (!strcmp (var, "http-proxy-host")) |
312 | else if (!strcmp (var, "http-proxy-host")) |
305 | { |
313 | { |
306 | #if ENABLE_HTTP_PROXY |
314 | #if ENABLE_HTTP_PROXY |
307 | proxy_host = strdup (val); |
315 | free (proxy_host), proxy_host = strdup (val); |
308 | #endif |
316 | #endif |
309 | } |
317 | } |
310 | else if (!strcmp (var, "http-proxy-port")) |
318 | else if (!strcmp (var, "http-proxy-port")) |
311 | { |
319 | { |
312 | #if ENABLE_HTTP_PROXY |
320 | #if ENABLE_HTTP_PROXY |
… | |
… | |
320 | #endif |
328 | #endif |
321 | } |
329 | } |
322 | |
330 | |
323 | /* node-specific, non-defaultable */ |
331 | /* node-specific, non-defaultable */ |
324 | else if (node != &default_node && !strcmp (var, "hostname")) |
332 | else if (node != &default_node && !strcmp (var, "hostname")) |
325 | { |
|
|
326 | free (node->hostname); |
|
|
327 | node->hostname = strdup (val); |
333 | free (node->hostname), node->hostname = strdup (val); |
328 | } |
|
|
329 | |
334 | |
330 | /* node-specific, defaultable */ |
335 | /* node-specific, defaultable */ |
331 | else if (!strcmp (var, "udp-port")) |
336 | else if (!strcmp (var, "udp-port")) |
332 | node->udp_port = atoi (val); |
337 | node->udp_port = atoi (val); |
333 | else if (!strcmp (var, "tcp-port")) |
338 | else if (!strcmp (var, "tcp-port")) |
334 | node->tcp_port = atoi (val); |
339 | node->tcp_port = atoi (val); |
|
|
340 | else if (!strcmp (var, "dns-port")) |
|
|
341 | node->dns_port = atoi (val); |
335 | else if (!strcmp (var, "router-priority")) |
342 | else if (!strcmp (var, "router-priority")) |
336 | node->routerprio = atoi (val); |
343 | node->routerprio = atoi (val); |
|
|
344 | else if (!strcmp (var, "max-retry")) |
|
|
345 | node->max_retry = atoi (val); |
337 | else if (!strcmp (var, "connect")) |
346 | else if (!strcmp (var, "connect")) |
338 | { |
347 | { |
339 | if (!strcmp (val, "ondemand")) |
348 | if (!strcmp (val, "ondemand")) |
340 | node->connectmode = conf_node::C_ONDEMAND; |
349 | node->connectmode = conf_node::C_ONDEMAND; |
341 | else if (!strcmp (val, "never")) |
350 | else if (!strcmp (val, "never")) |
… | |
… | |
344 | node->connectmode = conf_node::C_ALWAYS; |
353 | node->connectmode = conf_node::C_ALWAYS; |
345 | else if (!strcmp (val, "disabled")) |
354 | else if (!strcmp (val, "disabled")) |
346 | node->connectmode = conf_node::C_DISABLED; |
355 | node->connectmode = conf_node::C_DISABLED; |
347 | else |
356 | else |
348 | slog (L_WARN, |
357 | slog (L_WARN, |
349 | _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
358 | _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
350 | var, fname, lineno); |
359 | var, fname, lineno); |
351 | } |
360 | } |
352 | else if (!strcmp (var, "inherit-tos")) |
361 | else if (!strcmp (var, "inherit-tos")) |
353 | { |
362 | { |
354 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
363 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
355 | } |
364 | } |
… | |
… | |
368 | { |
377 | { |
369 | #if ENABLE_ICMP |
378 | #if ENABLE_ICMP |
370 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
379 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
371 | #endif |
380 | #endif |
372 | } |
381 | } |
|
|
382 | else if (!strcmp (var, "enable-dns")) |
|
|
383 | { |
|
|
384 | #if ENABLE_DNS |
|
|
385 | u8 v; parse_bool (v, "enable-dns" , PROT_DNSv4, 0); node->protocols = (node->protocols & ~PROT_DNSv4) | v; |
|
|
386 | #endif |
|
|
387 | } |
373 | else if (!strcmp (var, "enable-udp")) |
388 | else if (!strcmp (var, "enable-udp")) |
374 | { |
389 | { |
375 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
390 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
376 | } |
391 | } |
377 | else if (!strcmp (var, "enable-rawip")) |
392 | else if (!strcmp (var, "enable-rawip")) |
378 | { |
393 | {; |
379 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
394 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
380 | } |
395 | } |
381 | |
396 | |
382 | // unknown or misplaced |
397 | // unknown or misplaced |
383 | else |
398 | else |
… | |
… | |
389 | fclose (f); |
404 | fclose (f); |
390 | } |
405 | } |
391 | else |
406 | else |
392 | { |
407 | { |
393 | slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
408 | slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
394 | exit (1); |
409 | exit (EXIT_FAILURE); |
395 | } |
410 | } |
396 | |
411 | |
397 | free (fname); |
412 | free (fname); |
398 | |
413 | |
399 | fname = config_filename (prikeyfile, "hostkey"); |
414 | fname = config_filename (prikeyfile, "hostkey"); |
… | |
… | |
405 | |
420 | |
406 | if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) |
421 | if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) |
407 | { |
422 | { |
408 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
423 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
409 | slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
424 | slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
410 | exit (1); |
425 | exit (EXIT_FAILURE); |
411 | } |
426 | } |
412 | |
427 | |
413 | RSA_blinding_on (rsa_key, 0); |
428 | require (RSA_blinding_on (rsa_key, 0)); |
414 | |
429 | |
415 | fclose (f); |
430 | fclose (f); |
416 | } |
431 | } |
417 | else |
432 | else |
418 | { |
433 | { |
419 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
434 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
420 | |
435 | |
421 | if (need_keys) |
436 | if (need_keys) |
422 | exit (1); |
437 | exit (EXIT_FAILURE); |
423 | } |
438 | } |
|
|
439 | |
|
|
440 | if (need_keys && ::thisnode |
|
|
441 | && rsa_key && thisnode && thisnode->rsa_key) |
|
|
442 | if (BN_cmp (rsa_key->n, thisnode->rsa_key->n) != 0 |
|
|
443 | || BN_cmp (rsa_key->e, thisnode->rsa_key->e) != 0) |
|
|
444 | { |
|
|
445 | slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode); |
|
|
446 | exit (EXIT_FAILURE); |
|
|
447 | } |
424 | |
448 | |
425 | free (fname); |
449 | free (fname); |
426 | } |
450 | } |
427 | |
451 | |
428 | char *configuration::config_filename (const char *name, const char *dflt) |
452 | char *configuration::config_filename (const char *name, const char *dflt) |
… | |
… | |
464 | printf ("\n"); |
488 | printf ("\n"); |
465 | } |
489 | } |
466 | |
490 | |
467 | configuration::configuration () |
491 | configuration::configuration () |
468 | { |
492 | { |
|
|
493 | asprintf (&confbase, "%s/gvpe", CONFDIR); |
|
|
494 | |
469 | init (); |
495 | init (); |
470 | } |
496 | } |
471 | |
497 | |
472 | configuration::~configuration () |
498 | configuration::~configuration () |
473 | { |
499 | { |