1 | /* |
1 | /* |
2 | conf.c -- configuration code |
2 | conf.c -- configuration code |
3 | Copyright (C) 1998 Robert van der Meulen |
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
4 | 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
|
|
5 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
|
|
6 | 2000 Cris van Pelt <tribbel@arise.dhs.org> |
|
|
7 | 2003 Marc Lehmann <pcg@goof.com> |
|
|
8 | |
4 | |
9 | 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 |
10 | 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 |
11 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
12 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
31 | #include <netdb.h> |
27 | #include <netdb.h> |
32 | #include <sys/stat.h> |
28 | #include <sys/stat.h> |
33 | #include <sys/types.h> |
29 | #include <sys/types.h> |
34 | #include <unistd.h> |
30 | #include <unistd.h> |
35 | |
31 | |
36 | #include <netinet/in.h> |
32 | #include "netcompat.h" |
37 | |
33 | |
38 | #include <openssl/err.h> |
34 | #include <openssl/err.h> |
39 | #include <openssl/pem.h> |
35 | #include <openssl/pem.h> |
40 | #include <openssl/rsa.h> |
36 | #include <openssl/rsa.h> |
41 | #include <openssl/rand.h> |
37 | #include <openssl/rand.h> |
… | |
… | |
53 | |
49 | |
54 | struct configuration conf; |
50 | struct configuration conf; |
55 | |
51 | |
56 | u8 best_protocol (u8 protset) |
52 | u8 best_protocol (u8 protset) |
57 | { |
53 | { |
58 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
54 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
|
|
55 | if (protset & PROT_ICMPv4) return PROT_ICMPv4; |
59 | if (protset & PROT_UDPv4) return PROT_UDPv4; |
56 | if (protset & PROT_UDPv4 ) return PROT_UDPv4; |
60 | if (protset & PROT_TCPv4) return PROT_TCPv4; |
57 | if (protset & PROT_TCPv4 ) return PROT_TCPv4; |
61 | |
58 | |
62 | return 0; |
59 | return 0; |
63 | } |
60 | } |
64 | |
61 | |
65 | const char *strprotocol (u8 protocol) |
62 | const char *strprotocol (u8 protocol) |
66 | { |
63 | { |
67 | if (protocol & PROT_IPv4 ) return "rawip"; |
64 | if (protocol & PROT_IPv4 ) return "rawip"; |
|
|
65 | if (protocol & PROT_ICMPv4) return "icmp"; |
68 | if (protocol & PROT_UDPv4) return "udp"; |
66 | if (protocol & PROT_UDPv4 ) return "udp"; |
69 | if (protocol & PROT_TCPv4) return "tcp"; |
67 | if (protocol & PROT_TCPv4 ) return "tcp"; |
70 | |
68 | |
71 | return "<unknown>"; |
69 | return "<unknown>"; |
72 | } |
70 | } |
73 | |
71 | |
74 | configuration::configuration () |
72 | void |
|
|
73 | conf_node::print () |
75 | { |
74 | { |
76 | init (); |
75 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
|
|
76 | id, |
|
|
77 | id >> 8, id & 0xff, |
|
|
78 | compress ? 'Y' : 'N', |
|
|
79 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
80 | connectmode == C_NEVER ? "never" : |
|
|
81 | connectmode == C_ALWAYS ? "always" : "", |
|
|
82 | nodename, |
|
|
83 | hostname ? hostname : "", |
|
|
84 | hostname ? ":" : "", |
|
|
85 | hostname ? udp_port : 0 |
|
|
86 | ); |
77 | } |
87 | } |
78 | |
88 | |
79 | configuration::~configuration () |
89 | conf_node::~conf_node () |
80 | { |
90 | { |
81 | cleanup (); |
91 | if (rsa_key) |
|
|
92 | RSA_free (rsa_key); |
|
|
93 | |
|
|
94 | free (nodename); |
|
|
95 | free (hostname); |
82 | } |
96 | } |
83 | |
97 | |
84 | void configuration::init () |
98 | void configuration::init () |
85 | { |
99 | { |
86 | memset (this, 0, sizeof (*this)); |
100 | memset (this, 0, sizeof (*this)); |
87 | |
101 | |
88 | rekey = DEFAULT_REKEY; |
102 | rekey = DEFAULT_REKEY; |
89 | keepalive = DEFAULT_KEEPALIVE; |
103 | keepalive = DEFAULT_KEEPALIVE; |
90 | llevel = L_INFO; |
104 | llevel = L_INFO; |
91 | ip_proto = IPPROTO_GRE; |
105 | ip_proto = IPPROTO_GRE; |
|
|
106 | #if ENABLE_ICMP |
|
|
107 | icmp_type = ICMP_ECHOREPLY; |
|
|
108 | #endif |
92 | |
109 | |
93 | default_node.udp_port = DEFAULT_UDPPORT; |
110 | default_node.udp_port = DEFAULT_UDPPORT; |
94 | default_node.tcp_port = DEFAULT_UDPPORT; |
111 | default_node.tcp_port = DEFAULT_UDPPORT; |
95 | default_node.connectmode = conf_node::C_ALWAYS; |
112 | default_node.connectmode = conf_node::C_ALWAYS; |
96 | default_node.compress = true; |
113 | default_node.compress = true; |
… | |
… | |
100 | void configuration::cleanup() |
117 | void configuration::cleanup() |
101 | { |
118 | { |
102 | if (rsa_key) |
119 | if (rsa_key) |
103 | RSA_free (rsa_key); |
120 | RSA_free (rsa_key); |
104 | |
121 | |
105 | free (ifname); |
|
|
106 | |
|
|
107 | rsa_key = 0; |
122 | rsa_key = 0; |
108 | ifname = 0; |
123 | |
|
|
124 | free (ifname); ifname = 0; |
|
|
125 | #if ENABLE_HTTP_PROXY |
|
|
126 | free (proxy_host); proxy_host = 0; |
|
|
127 | free (proxy_auth); proxy_auth = 0; |
|
|
128 | #endif |
109 | } |
129 | } |
110 | |
130 | |
111 | void |
131 | void |
112 | configuration::clear_config () |
132 | configuration::clear_config () |
113 | { |
133 | { |
… | |
… | |
204 | else |
224 | else |
205 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
225 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
206 | } |
226 | } |
207 | else if (!strcmp (var, "ip-proto")) |
227 | else if (!strcmp (var, "ip-proto")) |
208 | ip_proto = atoi (val); |
228 | ip_proto = atoi (val); |
|
|
229 | #if ENABLE_ICMP |
|
|
230 | //TODO: error message |
|
|
231 | else if (!strcmp (var, "icmp-type")) |
|
|
232 | icmp_type = atoi (val); |
|
|
233 | #endif |
209 | |
234 | |
210 | // per config |
235 | // per config |
211 | else if (!strcmp (var, "node")) |
236 | else if (!strcmp (var, "node")) |
212 | { |
237 | { |
213 | default_node.id++; |
238 | default_node.id++; |
… | |
… | |
272 | script_if_up = strdup (val); |
297 | script_if_up = strdup (val); |
273 | else if (!strcmp (var, "node-up")) |
298 | else if (!strcmp (var, "node-up")) |
274 | script_node_up = strdup (val); |
299 | script_node_up = strdup (val); |
275 | else if (!strcmp (var, "node-down")) |
300 | else if (!strcmp (var, "node-down")) |
276 | script_node_down = strdup (val); |
301 | script_node_down = strdup (val); |
|
|
302 | #if ENABLE_HTTP_PROXY |
|
|
303 | else if (!strcmp (var, "http-proxy-host")) |
|
|
304 | proxy_host = strdup (val); |
|
|
305 | else if (!strcmp (var, "http-proxy-port")) |
|
|
306 | proxy_port = atoi (val); |
|
|
307 | else if (!strcmp (var, "http-proxy-auth")) |
|
|
308 | proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); |
|
|
309 | #endif |
277 | |
310 | |
278 | /* node-specific, non-defaultable */ |
311 | /* node-specific, non-defaultable */ |
279 | else if (node != &default_node && !strcmp (var, "hostname")) |
312 | else if (node != &default_node && !strcmp (var, "hostname")) |
280 | { |
313 | { |
281 | free (node->hostname); |
314 | free (node->hostname); |
… | |
… | |
317 | { |
350 | { |
318 | #if ENABLE_TCP |
351 | #if ENABLE_TCP |
319 | u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
352 | u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
320 | #endif |
353 | #endif |
321 | } |
354 | } |
|
|
355 | else if (!strcmp (var, "enable-icmp")) |
|
|
356 | { |
|
|
357 | #if ENABLE_ICMP |
|
|
358 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
|
|
359 | #endif |
|
|
360 | } |
322 | else if (!strcmp (var, "enable-udp")) |
361 | else if (!strcmp (var, "enable-udp")) |
323 | { |
362 | { |
324 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
363 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
325 | } |
364 | } |
326 | else if (!strcmp (var, "enable-rawip")) |
365 | else if (!strcmp (var, "enable-rawip")) |
… | |
… | |
328 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
367 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
329 | } |
368 | } |
330 | |
369 | |
331 | // unknown or misplaced |
370 | // unknown or misplaced |
332 | else |
371 | else |
333 | { |
|
|
334 | slog (L_WARN, |
372 | slog (L_WARN, |
335 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
373 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
336 | var, fname, lineno); |
374 | var, fname, lineno); |
337 | } |
|
|
338 | } |
375 | } |
339 | |
376 | |
340 | fclose (f); |
377 | fclose (f); |
341 | } |
378 | } |
342 | else |
379 | else |
… | |
… | |
401 | printf (_("MTU: %d\n"), mtu); |
438 | printf (_("MTU: %d\n"), mtu); |
402 | printf (_("rekeying interval: %d\n"), rekey); |
439 | printf (_("rekeying interval: %d\n"), rekey); |
403 | printf (_("keepalive interval: %d\n"), keepalive); |
440 | printf (_("keepalive interval: %d\n"), keepalive); |
404 | printf (_("interface: %s\n"), ifname); |
441 | printf (_("interface: %s\n"), ifname); |
405 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
442 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
406 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); |
443 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1); |
407 | printf ("\n"); |
444 | printf ("\n"); |
408 | |
445 | |
409 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
446 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
410 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
447 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
411 | |
448 | |
… | |
… | |
413 | (*i)->print (); |
450 | (*i)->print (); |
414 | |
451 | |
415 | printf ("\n"); |
452 | printf ("\n"); |
416 | } |
453 | } |
417 | |
454 | |
418 | void |
455 | configuration::configuration () |
419 | conf_node::print () |
|
|
420 | { |
456 | { |
421 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
457 | init (); |
422 | id, |
|
|
423 | id >> 8, id & 0xff, |
|
|
424 | compress ? 'Y' : 'N', |
|
|
425 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
426 | connectmode == C_NEVER ? "never" : |
|
|
427 | connectmode == C_ALWAYS ? "always" : "", |
|
|
428 | nodename, |
|
|
429 | hostname ? hostname : "", |
|
|
430 | hostname ? ":" : "", |
|
|
431 | hostname ? udp_port : 0 |
|
|
432 | ); |
|
|
433 | } |
458 | } |
434 | |
459 | |
|
|
460 | configuration::~configuration () |
|
|
461 | { |
|
|
462 | cleanup (); |
|
|
463 | } |
|
|
464 | |
|
|
465 | |