1 | /* |
1 | /* |
2 | conf.c -- configuration code |
2 | conf.c -- configuration code |
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
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 |
… | |
… | |
97 | |
99 | |
98 | void configuration::init () |
100 | void configuration::init () |
99 | { |
101 | { |
100 | memset (this, 0, sizeof (*this)); |
102 | memset (this, 0, sizeof (*this)); |
101 | |
103 | |
|
|
104 | mtu = DEFAULT_MTU; |
102 | rekey = DEFAULT_REKEY; |
105 | rekey = DEFAULT_REKEY; |
103 | keepalive = DEFAULT_KEEPALIVE; |
106 | keepalive = DEFAULT_KEEPALIVE; |
104 | llevel = L_INFO; |
107 | llevel = L_INFO; |
105 | ip_proto = IPPROTO_GRE; |
108 | ip_proto = IPPROTO_GRE; |
106 | #if ENABLE_ICMP |
109 | #if ENABLE_ICMP |
107 | icmp_type = ICMP_ECHOREPLY; |
110 | icmp_type = ICMP_ECHOREPLY; |
108 | #endif |
111 | #endif |
109 | |
112 | |
110 | default_node.udp_port = DEFAULT_UDPPORT; |
113 | default_node.udp_port = DEFAULT_UDPPORT; |
111 | default_node.tcp_port = DEFAULT_UDPPORT; |
114 | default_node.tcp_port = DEFAULT_UDPPORT; // ehrm |
112 | default_node.connectmode = conf_node::C_ALWAYS; |
115 | default_node.connectmode = conf_node::C_ALWAYS; |
113 | default_node.compress = true; |
116 | default_node.compress = true; |
114 | 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"); |
115 | } |
121 | } |
116 | |
122 | |
117 | void configuration::cleanup() |
123 | void configuration::cleanup() |
118 | { |
124 | { |
119 | if (rsa_key) |
125 | if (rsa_key) |
120 | RSA_free (rsa_key); |
126 | RSA_free (rsa_key); |
121 | |
127 | |
122 | rsa_key = 0; |
128 | rsa_key = 0; |
123 | |
129 | |
|
|
130 | free (pidfilename); pidfilename = 0; |
124 | free (ifname); ifname = 0; |
131 | free (ifname); ifname = 0; |
125 | #if ENABLE_HTTP_PROXY |
132 | #if ENABLE_HTTP_PROXY |
126 | free (proxy_host); proxy_host = 0; |
133 | free (proxy_host); proxy_host = 0; |
127 | free (proxy_auth); proxy_auth = 0; |
134 | free (proxy_auth); proxy_auth = 0; |
128 | #endif |
135 | #endif |
129 | } |
136 | } |
130 | |
137 | |
131 | void |
138 | void |
132 | configuration::clear_config () |
139 | configuration::clear_config () |
… | |
… | |
157 | char *fname; |
164 | char *fname; |
158 | FILE *f; |
165 | FILE *f; |
159 | |
166 | |
160 | clear_config (); |
167 | clear_config (); |
161 | |
168 | |
162 | asprintf (&fname, "%s/vped.conf", confbase); |
169 | asprintf (&fname, "%s/gvpe.conf", confbase); |
163 | f = fopen (fname, "r"); |
170 | f = fopen (fname, "r"); |
164 | |
171 | |
165 | if (f) |
172 | if (f) |
166 | { |
173 | { |
167 | char line[16384]; |
174 | char line[16384]; |
… | |
… | |
224 | else |
231 | else |
225 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
232 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
226 | } |
233 | } |
227 | else if (!strcmp (var, "ip-proto")) |
234 | else if (!strcmp (var, "ip-proto")) |
228 | ip_proto = atoi (val); |
235 | ip_proto = atoi (val); |
|
|
236 | else if (!strcmp (var, "icmp-type")) |
|
|
237 | { |
229 | #if ENABLE_ICMP |
238 | #if ENABLE_ICMP |
230 | //TODO: error message |
|
|
231 | else if (!strcmp (var, "icmp-type")) |
|
|
232 | icmp_type = atoi (val); |
239 | icmp_type = atoi (val); |
233 | #endif |
240 | #endif |
|
|
241 | } |
234 | |
242 | |
235 | // per config |
243 | // per config |
236 | else if (!strcmp (var, "node")) |
244 | else if (!strcmp (var, "node")) |
237 | { |
245 | { |
238 | default_node.id++; |
246 | default_node.id++; |
… | |
… | |
256 | |
264 | |
257 | if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) |
265 | if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) |
258 | { |
266 | { |
259 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
267 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
260 | 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)); |
261 | exit (1); |
269 | exit (EXIT_FAILURE); |
262 | } |
270 | } |
263 | |
271 | |
264 | RSA_blinding_on (node->rsa_key, 0); |
272 | require (RSA_blinding_on (node->rsa_key, 0)); |
265 | |
273 | |
266 | fclose (f); |
274 | fclose (f); |
267 | } |
275 | } |
268 | else |
276 | else |
269 | { |
277 | { |
270 | 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)); |
271 | |
279 | |
272 | if (need_keys) |
280 | if (need_keys) |
273 | exit (1); |
281 | exit (EXIT_FAILURE); |
274 | } |
282 | } |
275 | |
283 | |
276 | free (fname); |
284 | free (fname); |
277 | } |
285 | } |
278 | |
286 | |
279 | if (!::thisnode || !strcmp (node->nodename, ::thisnode)) |
287 | if (::thisnode && !strcmp (node->nodename, ::thisnode)) |
280 | thisnode = node; |
288 | thisnode = node; |
281 | } |
289 | } |
282 | else if (!strcmp (var, "private-key")) |
290 | else if (!strcmp (var, "private-key")) |
283 | prikeyfile = strdup (val); |
291 | free (prikeyfile), prikeyfile = strdup (val); |
284 | else if (!strcmp (var, "ifpersist")) |
292 | else if (!strcmp (var, "ifpersist")) |
285 | { |
293 | { |
286 | parse_bool (ifpersist, "ifpersist", true, false); |
294 | parse_bool (ifpersist, "ifpersist", true, false); |
287 | } |
295 | } |
288 | else if (!strcmp (var, "ifname")) |
296 | else if (!strcmp (var, "ifname")) |
289 | ifname = strdup (val); |
297 | free (ifname), ifname = strdup (val); |
290 | else if (!strcmp (var, "rekey")) |
298 | else if (!strcmp (var, "rekey")) |
291 | rekey = atoi (val); |
299 | rekey = atoi (val); |
292 | else if (!strcmp (var, "keepalive")) |
300 | else if (!strcmp (var, "keepalive")) |
293 | keepalive = atoi (val); |
301 | keepalive = atoi (val); |
294 | else if (!strcmp (var, "mtu")) |
302 | else if (!strcmp (var, "mtu")) |
295 | mtu = atoi (val); |
303 | mtu = atoi (val); |
296 | else if (!strcmp (var, "if-up")) |
304 | else if (!strcmp (var, "if-up")) |
297 | script_if_up = strdup (val); |
305 | free (script_if_up), script_if_up = strdup (val); |
298 | else if (!strcmp (var, "node-up")) |
306 | else if (!strcmp (var, "node-up")) |
299 | script_node_up = strdup (val); |
307 | free (script_node_up), script_node_up = strdup (val); |
300 | else if (!strcmp (var, "node-down")) |
308 | else if (!strcmp (var, "node-down")) |
301 | 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); |
|
|
312 | else if (!strcmp (var, "http-proxy-host")) |
|
|
313 | { |
302 | #if ENABLE_HTTP_PROXY |
314 | #if ENABLE_HTTP_PROXY |
303 | else if (!strcmp (var, "http-proxy-host")) |
|
|
304 | proxy_host = strdup (val); |
315 | free (proxy_host), proxy_host = strdup (val); |
|
|
316 | #endif |
|
|
317 | } |
305 | else if (!strcmp (var, "http-proxy-port")) |
318 | else if (!strcmp (var, "http-proxy-port")) |
|
|
319 | { |
|
|
320 | #if ENABLE_HTTP_PROXY |
306 | proxy_port = atoi (val); |
321 | proxy_port = atoi (val); |
|
|
322 | #endif |
|
|
323 | } |
307 | else if (!strcmp (var, "http-proxy-auth")) |
324 | else if (!strcmp (var, "http-proxy-auth")) |
|
|
325 | { |
|
|
326 | #if ENABLE_HTTP_PROXY |
308 | proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); |
327 | proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); |
309 | #endif |
328 | #endif |
|
|
329 | } |
310 | |
330 | |
311 | /* node-specific, non-defaultable */ |
331 | /* node-specific, non-defaultable */ |
312 | else if (node != &default_node && !strcmp (var, "hostname")) |
332 | else if (node != &default_node && !strcmp (var, "hostname")) |
313 | { |
|
|
314 | free (node->hostname); |
|
|
315 | node->hostname = strdup (val); |
333 | free (node->hostname), node->hostname = strdup (val); |
316 | } |
|
|
317 | |
334 | |
318 | /* node-specific, defaultable */ |
335 | /* node-specific, defaultable */ |
319 | else if (!strcmp (var, "udp-port")) |
336 | else if (!strcmp (var, "udp-port")) |
320 | node->udp_port = atoi (val); |
337 | node->udp_port = atoi (val); |
321 | else if (!strcmp (var, "tcp-port")) |
338 | else if (!strcmp (var, "tcp-port")) |
322 | node->tcp_port = atoi (val); |
339 | node->tcp_port = atoi (val); |
|
|
340 | else if (!strcmp (var, "dns-port")) |
|
|
341 | node->dns_port = atoi (val); |
323 | else if (!strcmp (var, "router-priority")) |
342 | else if (!strcmp (var, "router-priority")) |
324 | node->routerprio = atoi (val); |
343 | node->routerprio = atoi (val); |
|
|
344 | else if (!strcmp (var, "max-retry")) |
|
|
345 | node->max_retry = atoi (val); |
325 | else if (!strcmp (var, "connect")) |
346 | else if (!strcmp (var, "connect")) |
326 | { |
347 | { |
327 | if (!strcmp (val, "ondemand")) |
348 | if (!strcmp (val, "ondemand")) |
328 | node->connectmode = conf_node::C_ONDEMAND; |
349 | node->connectmode = conf_node::C_ONDEMAND; |
329 | else if (!strcmp (val, "never")) |
350 | else if (!strcmp (val, "never")) |
… | |
… | |
332 | node->connectmode = conf_node::C_ALWAYS; |
353 | node->connectmode = conf_node::C_ALWAYS; |
333 | else if (!strcmp (val, "disabled")) |
354 | else if (!strcmp (val, "disabled")) |
334 | node->connectmode = conf_node::C_DISABLED; |
355 | node->connectmode = conf_node::C_DISABLED; |
335 | else |
356 | else |
336 | slog (L_WARN, |
357 | slog (L_WARN, |
337 | _("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"), |
338 | var, fname, lineno); |
359 | var, fname, lineno); |
339 | } |
360 | } |
340 | else if (!strcmp (var, "inherit-tos")) |
361 | else if (!strcmp (var, "inherit-tos")) |
341 | { |
362 | { |
342 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
363 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
343 | } |
364 | } |
… | |
… | |
356 | { |
377 | { |
357 | #if ENABLE_ICMP |
378 | #if ENABLE_ICMP |
358 | 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; |
359 | #endif |
380 | #endif |
360 | } |
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 | } |
361 | else if (!strcmp (var, "enable-udp")) |
388 | else if (!strcmp (var, "enable-udp")) |
362 | { |
389 | { |
363 | 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; |
364 | } |
391 | } |
365 | else if (!strcmp (var, "enable-rawip")) |
392 | else if (!strcmp (var, "enable-rawip")) |
366 | { |
393 | {; |
367 | 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; |
368 | } |
395 | } |
369 | |
396 | |
370 | // unknown or misplaced |
397 | // unknown or misplaced |
371 | else |
398 | else |
… | |
… | |
377 | fclose (f); |
404 | fclose (f); |
378 | } |
405 | } |
379 | else |
406 | else |
380 | { |
407 | { |
381 | 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)); |
382 | exit (1); |
409 | exit (EXIT_FAILURE); |
383 | } |
410 | } |
384 | |
411 | |
385 | free (fname); |
412 | free (fname); |
386 | |
413 | |
387 | fname = config_filename (prikeyfile, "hostkey"); |
414 | fname = config_filename (prikeyfile, "hostkey"); |
… | |
… | |
393 | |
420 | |
394 | if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) |
421 | if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) |
395 | { |
422 | { |
396 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
423 | ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
397 | 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)); |
398 | exit (1); |
425 | exit (EXIT_FAILURE); |
399 | } |
426 | } |
400 | |
427 | |
401 | RSA_blinding_on (rsa_key, 0); |
428 | require (RSA_blinding_on (rsa_key, 0)); |
402 | |
429 | |
403 | fclose (f); |
430 | fclose (f); |
404 | } |
431 | } |
405 | else |
432 | else |
406 | { |
433 | { |
407 | 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)); |
408 | |
435 | |
409 | if (need_keys) |
436 | if (need_keys) |
410 | exit (1); |
437 | exit (EXIT_FAILURE); |
411 | } |
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 | } |
412 | |
448 | |
413 | free (fname); |
449 | free (fname); |
414 | } |
450 | } |
415 | |
451 | |
416 | char *configuration::config_filename (const char *name, const char *dflt) |
452 | char *configuration::config_filename (const char *name, const char *dflt) |
… | |
… | |
452 | printf ("\n"); |
488 | printf ("\n"); |
453 | } |
489 | } |
454 | |
490 | |
455 | configuration::configuration () |
491 | configuration::configuration () |
456 | { |
492 | { |
|
|
493 | asprintf (&confbase, "%s/gvpe", CONFDIR); |
|
|
494 | |
457 | init (); |
495 | init (); |
458 | } |
496 | } |
459 | |
497 | |
460 | configuration::~configuration () |
498 | configuration::~configuration () |
461 | { |
499 | { |