… | |
… | |
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 |
… | |
… | |
95 | free (hostname); |
97 | free (hostname); |
96 | } |
98 | } |
97 | |
99 | |
98 | void configuration::init () |
100 | void configuration::init () |
99 | { |
101 | { |
|
|
102 | asprintf (&confbase, "%s/vpe", CONFDIR); |
|
|
103 | |
100 | memset (this, 0, sizeof (*this)); |
104 | memset (this, 0, sizeof (*this)); |
101 | |
105 | |
102 | mtu = DEFAULT_MTU; |
106 | mtu = DEFAULT_MTU; |
103 | rekey = DEFAULT_REKEY; |
107 | rekey = DEFAULT_REKEY; |
104 | keepalive = DEFAULT_KEEPALIVE; |
108 | keepalive = DEFAULT_KEEPALIVE; |
… | |
… | |
107 | #if ENABLE_ICMP |
111 | #if ENABLE_ICMP |
108 | icmp_type = ICMP_ECHOREPLY; |
112 | icmp_type = ICMP_ECHOREPLY; |
109 | #endif |
113 | #endif |
110 | |
114 | |
111 | default_node.udp_port = DEFAULT_UDPPORT; |
115 | default_node.udp_port = DEFAULT_UDPPORT; |
112 | default_node.tcp_port = DEFAULT_UDPPORT; |
116 | default_node.tcp_port = DEFAULT_UDPPORT; // ehrm |
113 | default_node.connectmode = conf_node::C_ALWAYS; |
117 | default_node.connectmode = conf_node::C_ALWAYS; |
114 | default_node.compress = true; |
118 | default_node.compress = true; |
115 | default_node.protocols = PROT_UDPv4; |
119 | default_node.protocols = PROT_UDPv4; |
|
|
120 | |
|
|
121 | conf.pidfilename = strdup (LOCALSTATEDIR "/run/vped.pid"); |
116 | } |
122 | } |
117 | |
123 | |
118 | void configuration::cleanup() |
124 | void configuration::cleanup() |
119 | { |
125 | { |
120 | if (rsa_key) |
126 | if (rsa_key) |
121 | RSA_free (rsa_key); |
127 | RSA_free (rsa_key); |
122 | |
128 | |
123 | rsa_key = 0; |
129 | rsa_key = 0; |
124 | |
130 | |
|
|
131 | free (pidfilename); pidfilename = 0; |
125 | free (ifname); ifname = 0; |
132 | free (ifname); ifname = 0; |
126 | #if ENABLE_HTTP_PROXY |
133 | #if ENABLE_HTTP_PROXY |
127 | free (proxy_host); proxy_host = 0; |
134 | free (proxy_host); proxy_host = 0; |
128 | free (proxy_auth); proxy_auth = 0; |
135 | free (proxy_auth); proxy_auth = 0; |
129 | #endif |
136 | #endif |
130 | } |
137 | } |
131 | |
138 | |
132 | void |
139 | void |
133 | configuration::clear_config () |
140 | configuration::clear_config () |
… | |
… | |
258 | |
265 | |
259 | if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) |
266 | if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) |
260 | { |
267 | { |
261 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
268 | 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)); |
269 | slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
263 | exit (1); |
270 | exit (EXIT_FAILURE); |
264 | } |
271 | } |
265 | |
272 | |
266 | RSA_blinding_on (node->rsa_key, 0); |
273 | require (RSA_blinding_on (node->rsa_key, 0)); |
267 | |
274 | |
268 | fclose (f); |
275 | fclose (f); |
269 | } |
276 | } |
270 | else |
277 | else |
271 | { |
278 | { |
272 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno)); |
279 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno)); |
273 | |
280 | |
274 | if (need_keys) |
281 | if (need_keys) |
275 | exit (1); |
282 | exit (EXIT_FAILURE); |
276 | } |
283 | } |
277 | |
284 | |
278 | free (fname); |
285 | free (fname); |
279 | } |
286 | } |
280 | |
287 | |
281 | if (!::thisnode || !strcmp (node->nodename, ::thisnode)) |
288 | if (::thisnode && !strcmp (node->nodename, ::thisnode)) |
282 | thisnode = node; |
289 | thisnode = node; |
283 | } |
290 | } |
284 | else if (!strcmp (var, "private-key")) |
291 | else if (!strcmp (var, "private-key")) |
285 | prikeyfile = strdup (val); |
292 | free (prikeyfile), prikeyfile = strdup (val); |
286 | else if (!strcmp (var, "ifpersist")) |
293 | else if (!strcmp (var, "ifpersist")) |
287 | { |
294 | { |
288 | parse_bool (ifpersist, "ifpersist", true, false); |
295 | parse_bool (ifpersist, "ifpersist", true, false); |
289 | } |
296 | } |
290 | else if (!strcmp (var, "ifname")) |
297 | else if (!strcmp (var, "ifname")) |
291 | ifname = strdup (val); |
298 | free (ifname), ifname = strdup (val); |
292 | else if (!strcmp (var, "rekey")) |
299 | else if (!strcmp (var, "rekey")) |
293 | rekey = atoi (val); |
300 | rekey = atoi (val); |
294 | else if (!strcmp (var, "keepalive")) |
301 | else if (!strcmp (var, "keepalive")) |
295 | keepalive = atoi (val); |
302 | keepalive = atoi (val); |
296 | else if (!strcmp (var, "mtu")) |
303 | else if (!strcmp (var, "mtu")) |
297 | mtu = atoi (val); |
304 | mtu = atoi (val); |
298 | else if (!strcmp (var, "if-up")) |
305 | else if (!strcmp (var, "if-up")) |
299 | script_if_up = strdup (val); |
306 | free (script_if_up), script_if_up = strdup (val); |
300 | else if (!strcmp (var, "node-up")) |
307 | else if (!strcmp (var, "node-up")) |
301 | script_node_up = strdup (val); |
308 | free (script_node_up), script_node_up = strdup (val); |
302 | else if (!strcmp (var, "node-down")) |
309 | else if (!strcmp (var, "node-down")) |
303 | script_node_down = strdup (val); |
310 | free (script_node_down), script_node_down = strdup (val); |
|
|
311 | else if (!strcmp (var, "pid-file")) |
|
|
312 | free (pidfilename), pidfilename = strdup (val); |
304 | else if (!strcmp (var, "http-proxy-host")) |
313 | else if (!strcmp (var, "http-proxy-host")) |
305 | { |
314 | { |
306 | #if ENABLE_HTTP_PROXY |
315 | #if ENABLE_HTTP_PROXY |
307 | proxy_host = strdup (val); |
316 | free (proxy_host), proxy_host = strdup (val); |
308 | #endif |
317 | #endif |
309 | } |
318 | } |
310 | else if (!strcmp (var, "http-proxy-port")) |
319 | else if (!strcmp (var, "http-proxy-port")) |
311 | { |
320 | { |
312 | #if ENABLE_HTTP_PROXY |
321 | #if ENABLE_HTTP_PROXY |
… | |
… | |
320 | #endif |
329 | #endif |
321 | } |
330 | } |
322 | |
331 | |
323 | /* node-specific, non-defaultable */ |
332 | /* node-specific, non-defaultable */ |
324 | else if (node != &default_node && !strcmp (var, "hostname")) |
333 | else if (node != &default_node && !strcmp (var, "hostname")) |
325 | { |
|
|
326 | free (node->hostname); |
|
|
327 | node->hostname = strdup (val); |
334 | free (node->hostname), node->hostname = strdup (val); |
328 | } |
|
|
329 | |
335 | |
330 | /* node-specific, defaultable */ |
336 | /* node-specific, defaultable */ |
331 | else if (!strcmp (var, "udp-port")) |
337 | else if (!strcmp (var, "udp-port")) |
332 | node->udp_port = atoi (val); |
338 | node->udp_port = atoi (val); |
333 | else if (!strcmp (var, "tcp-port")) |
339 | else if (!strcmp (var, "tcp-port")) |
334 | node->tcp_port = atoi (val); |
340 | node->tcp_port = atoi (val); |
|
|
341 | else if (!strcmp (var, "dns-port")) |
|
|
342 | node->dns_port = atoi (val); |
335 | else if (!strcmp (var, "router-priority")) |
343 | else if (!strcmp (var, "router-priority")) |
336 | node->routerprio = atoi (val); |
344 | node->routerprio = atoi (val); |
337 | else if (!strcmp (var, "connect")) |
345 | else if (!strcmp (var, "connect")) |
338 | { |
346 | { |
339 | if (!strcmp (val, "ondemand")) |
347 | if (!strcmp (val, "ondemand")) |
… | |
… | |
368 | { |
376 | { |
369 | #if ENABLE_ICMP |
377 | #if ENABLE_ICMP |
370 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
378 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
371 | #endif |
379 | #endif |
372 | } |
380 | } |
|
|
381 | else if (!strcmp (var, "enable-dns")) |
|
|
382 | { |
|
|
383 | #if ENABLE_DNS |
|
|
384 | u8 v; parse_bool (v, "enable-dns" , PROT_DNSv4, 0); node->protocols = (node->protocols & ~PROT_DNSv4) | v; |
|
|
385 | #endif |
|
|
386 | } |
373 | else if (!strcmp (var, "enable-udp")) |
387 | else if (!strcmp (var, "enable-udp")) |
374 | { |
388 | { |
375 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
389 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
376 | } |
390 | } |
377 | else if (!strcmp (var, "enable-rawip")) |
391 | else if (!strcmp (var, "enable-rawip")) |
378 | { |
392 | {; |
379 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
393 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
380 | } |
394 | } |
381 | |
395 | |
382 | // unknown or misplaced |
396 | // unknown or misplaced |
383 | else |
397 | else |
… | |
… | |
389 | fclose (f); |
403 | fclose (f); |
390 | } |
404 | } |
391 | else |
405 | else |
392 | { |
406 | { |
393 | slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
407 | slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
394 | exit (1); |
408 | exit (EXIT_FAILURE); |
395 | } |
409 | } |
396 | |
410 | |
397 | free (fname); |
411 | free (fname); |
398 | |
412 | |
399 | fname = config_filename (prikeyfile, "hostkey"); |
413 | fname = config_filename (prikeyfile, "hostkey"); |
… | |
… | |
405 | |
419 | |
406 | if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) |
420 | if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) |
407 | { |
421 | { |
408 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
422 | 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)); |
423 | slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
410 | exit (1); |
424 | exit (EXIT_FAILURE); |
411 | } |
425 | } |
412 | |
426 | |
413 | RSA_blinding_on (rsa_key, 0); |
427 | require (RSA_blinding_on (rsa_key, 0)); |
414 | |
428 | |
415 | fclose (f); |
429 | fclose (f); |
416 | } |
430 | } |
417 | else |
431 | else |
418 | { |
432 | { |
419 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
433 | slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
420 | |
434 | |
421 | if (need_keys) |
435 | if (need_keys) |
422 | exit (1); |
436 | exit (EXIT_FAILURE); |
423 | } |
437 | } |
|
|
438 | |
|
|
439 | if (need_keys && ::thisnode |
|
|
440 | && rsa_key && thisnode && thisnode->rsa_key) |
|
|
441 | if (BN_cmp (rsa_key->n, thisnode->rsa_key->n) != 0 |
|
|
442 | || BN_cmp (rsa_key->e, thisnode->rsa_key->e) != 0) |
|
|
443 | { |
|
|
444 | slog (L_NOTICE, _("private hostkey and public node key mismatch: is '%s' the correct node?"), ::thisnode); |
|
|
445 | exit (EXIT_FAILURE); |
|
|
446 | } |
424 | |
447 | |
425 | free (fname); |
448 | free (fname); |
426 | } |
449 | } |
427 | |
450 | |
428 | char *configuration::config_filename (const char *name, const char *dflt) |
451 | char *configuration::config_filename (const char *name, const char *dflt) |