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> |
… | |
… | |
51 | char *identname; |
47 | char *identname; |
52 | char *pidfilename; |
48 | char *pidfilename; |
53 | |
49 | |
54 | struct configuration conf; |
50 | struct configuration conf; |
55 | |
51 | |
56 | configuration::configuration () |
52 | u8 best_protocol (u8 protset) |
57 | { |
53 | { |
58 | init (); |
54 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
59 | } |
55 | if (protset & PROT_ICMPv4) return PROT_ICMPv4; |
|
|
56 | if (protset & PROT_UDPv4 ) return PROT_UDPv4; |
|
|
57 | if (protset & PROT_TCPv4 ) return PROT_TCPv4; |
60 | |
58 | |
61 | configuration::~configuration () |
59 | return 0; |
|
|
60 | } |
|
|
61 | |
|
|
62 | const char *strprotocol (u8 protocol) |
62 | { |
63 | { |
63 | cleanup (); |
64 | if (protocol & PROT_IPv4 ) return "rawip"; |
|
|
65 | if (protocol & PROT_ICMPv4) return "icmp"; |
|
|
66 | if (protocol & PROT_UDPv4 ) return "udp"; |
|
|
67 | if (protocol & PROT_TCPv4 ) return "tcp"; |
|
|
68 | |
|
|
69 | return "<unknown>"; |
|
|
70 | } |
|
|
71 | |
|
|
72 | void |
|
|
73 | conf_node::print () |
|
|
74 | { |
|
|
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 | ); |
|
|
87 | } |
|
|
88 | |
|
|
89 | conf_node::~conf_node () |
|
|
90 | { |
|
|
91 | if (rsa_key) |
|
|
92 | RSA_free (rsa_key); |
|
|
93 | |
|
|
94 | free (nodename); |
|
|
95 | free (hostname); |
64 | } |
96 | } |
65 | |
97 | |
66 | void configuration::init () |
98 | void configuration::init () |
67 | { |
99 | { |
68 | memset (this, 0, sizeof (*this)); |
100 | memset (this, 0, sizeof (*this)); |
69 | |
101 | |
|
|
102 | mtu = DEFAULT_MTU; |
70 | rekey = DEFAULT_REKEY; |
103 | rekey = DEFAULT_REKEY; |
71 | keepalive = DEFAULT_KEEPALIVE; |
104 | keepalive = DEFAULT_KEEPALIVE; |
72 | llevel = L_INFO; |
105 | llevel = L_INFO; |
73 | ip_proto = IPPROTO_GRE; |
106 | ip_proto = IPPROTO_GRE; |
|
|
107 | #if ENABLE_ICMP |
|
|
108 | icmp_type = ICMP_ECHOREPLY; |
|
|
109 | #endif |
74 | |
110 | |
75 | default_node.udp_port = DEFAULT_UDPPORT; |
111 | default_node.udp_port = DEFAULT_UDPPORT; |
|
|
112 | default_node.tcp_port = DEFAULT_UDPPORT; |
76 | default_node.connectmode = conf_node::C_ALWAYS; |
113 | default_node.connectmode = conf_node::C_ALWAYS; |
77 | default_node.compress = true; |
114 | default_node.compress = true; |
78 | default_node.can_send = PROT_UDPv4; |
115 | default_node.protocols = PROT_UDPv4; |
79 | default_node.can_recv = PROT_IPv4; |
|
|
80 | } |
116 | } |
81 | |
117 | |
82 | void configuration::cleanup() |
118 | void configuration::cleanup() |
83 | { |
119 | { |
84 | if (rsa_key) |
120 | if (rsa_key) |
85 | RSA_free (rsa_key); |
121 | RSA_free (rsa_key); |
86 | |
122 | |
87 | free (ifname); |
|
|
88 | |
|
|
89 | rsa_key = 0; |
123 | rsa_key = 0; |
90 | ifname = 0; |
124 | |
|
|
125 | free (ifname); ifname = 0; |
|
|
126 | #if ENABLE_HTTP_PROXY |
|
|
127 | free (proxy_host); proxy_host = 0; |
|
|
128 | free (proxy_auth); proxy_auth = 0; |
|
|
129 | #endif |
91 | } |
130 | } |
92 | |
131 | |
93 | void |
132 | void |
94 | configuration::clear_config () |
133 | configuration::clear_config () |
95 | { |
134 | { |
… | |
… | |
186 | else |
225 | else |
187 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
226 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
188 | } |
227 | } |
189 | else if (!strcmp (var, "ip-proto")) |
228 | else if (!strcmp (var, "ip-proto")) |
190 | ip_proto = atoi (val); |
229 | ip_proto = atoi (val); |
|
|
230 | #if ENABLE_ICMP |
|
|
231 | //TODO: error message |
|
|
232 | else if (!strcmp (var, "icmp-type")) |
|
|
233 | icmp_type = atoi (val); |
|
|
234 | #endif |
191 | |
235 | |
192 | // per config |
236 | // per config |
193 | else if (!strcmp (var, "node")) |
237 | else if (!strcmp (var, "node")) |
194 | { |
238 | { |
195 | default_node.id++; |
239 | default_node.id++; |
… | |
… | |
254 | script_if_up = strdup (val); |
298 | script_if_up = strdup (val); |
255 | else if (!strcmp (var, "node-up")) |
299 | else if (!strcmp (var, "node-up")) |
256 | script_node_up = strdup (val); |
300 | script_node_up = strdup (val); |
257 | else if (!strcmp (var, "node-down")) |
301 | else if (!strcmp (var, "node-down")) |
258 | script_node_down = strdup (val); |
302 | script_node_down = strdup (val); |
|
|
303 | #if ENABLE_HTTP_PROXY |
|
|
304 | else if (!strcmp (var, "http-proxy-host")) |
|
|
305 | proxy_host = strdup (val); |
|
|
306 | else if (!strcmp (var, "http-proxy-port")) |
|
|
307 | proxy_port = atoi (val); |
|
|
308 | else if (!strcmp (var, "http-proxy-auth")) |
|
|
309 | proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); |
|
|
310 | #endif |
259 | |
311 | |
260 | /* node-specific, non-defaultable */ |
312 | /* node-specific, non-defaultable */ |
261 | else if (node != &default_node && !strcmp (var, "hostname")) |
313 | else if (node != &default_node && !strcmp (var, "hostname")) |
262 | { |
314 | { |
263 | free (node->hostname); |
315 | free (node->hostname); |
… | |
… | |
265 | } |
317 | } |
266 | |
318 | |
267 | /* node-specific, defaultable */ |
319 | /* node-specific, defaultable */ |
268 | else if (!strcmp (var, "udp-port")) |
320 | else if (!strcmp (var, "udp-port")) |
269 | node->udp_port = atoi (val); |
321 | node->udp_port = atoi (val); |
|
|
322 | else if (!strcmp (var, "tcp-port")) |
|
|
323 | node->tcp_port = atoi (val); |
270 | else if (!strcmp (var, "router-priority")) |
324 | else if (!strcmp (var, "router-priority")) |
271 | node->routerprio = atoi (val); |
325 | node->routerprio = atoi (val); |
272 | else if (!strcmp (var, "connect")) |
326 | else if (!strcmp (var, "connect")) |
273 | { |
327 | { |
274 | if (!strcmp (val, "ondemand")) |
328 | if (!strcmp (val, "ondemand")) |
… | |
… | |
291 | else if (!strcmp (var, "compress")) |
345 | else if (!strcmp (var, "compress")) |
292 | { |
346 | { |
293 | parse_bool (node->compress, "compress", true, false); |
347 | parse_bool (node->compress, "compress", true, false); |
294 | } |
348 | } |
295 | // all these bool options really really cost a lot of executable size! |
349 | // all these bool options really really cost a lot of executable size! |
296 | else if (!strcmp (var, "can-send-udp")) |
350 | else if (!strcmp (var, "enable-tcp")) |
297 | { |
|
|
298 | u8 v; parse_bool (v, "can-send-udp", PROT_UDPv4, 0); node->can_send = (node->can_send & ~PROT_UDPv4) | v; |
|
|
299 | } |
351 | { |
300 | else if (!strcmp (var, "can-recv-udp")) |
352 | #if ENABLE_TCP |
|
|
353 | u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
|
|
354 | #endif |
301 | { |
355 | } |
302 | u8 v; parse_bool (v, "can-recv-udp", PROT_UDPv4, 0); node->can_recv = (node->can_recv & ~PROT_UDPv4) | v; |
356 | else if (!strcmp (var, "enable-icmp")) |
303 | } |
357 | { |
304 | else if (!strcmp (var, "can-send-rawip")) |
358 | #if ENABLE_ICMP |
|
|
359 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
|
|
360 | #endif |
305 | { |
361 | } |
306 | u8 v; parse_bool (v, "can-send-rawip", PROT_IPv4, 0); node->can_send = (node->can_send & ~PROT_IPv4) | v; |
362 | else if (!strcmp (var, "enable-udp")) |
307 | } |
363 | { |
308 | else if (!strcmp (var, "can-recv-rawip")) |
364 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
309 | { |
365 | } |
|
|
366 | else if (!strcmp (var, "enable-rawip")) |
|
|
367 | { |
310 | u8 v; parse_bool (v, "can-recv-rawip", PROT_IPv4, 0); node->can_recv = (node->can_recv & ~PROT_IPv4) | v; |
368 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
311 | } |
369 | } |
312 | |
370 | |
313 | // unknown or misplaced |
371 | // unknown or misplaced |
314 | else |
372 | else |
315 | { |
|
|
316 | slog (L_WARN, |
373 | slog (L_WARN, |
317 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
374 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
318 | var, fname, lineno); |
375 | var, fname, lineno); |
319 | } |
|
|
320 | } |
376 | } |
321 | |
377 | |
322 | fclose (f); |
378 | fclose (f); |
323 | } |
379 | } |
324 | else |
380 | else |
… | |
… | |
383 | printf (_("MTU: %d\n"), mtu); |
439 | printf (_("MTU: %d\n"), mtu); |
384 | printf (_("rekeying interval: %d\n"), rekey); |
440 | printf (_("rekeying interval: %d\n"), rekey); |
385 | printf (_("keepalive interval: %d\n"), keepalive); |
441 | printf (_("keepalive interval: %d\n"), keepalive); |
386 | printf (_("interface: %s\n"), ifname); |
442 | printf (_("interface: %s\n"), ifname); |
387 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
443 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
388 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); |
444 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1); |
389 | printf ("\n"); |
445 | printf ("\n"); |
390 | |
446 | |
391 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
447 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
392 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
448 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
393 | |
449 | |
… | |
… | |
395 | (*i)->print (); |
451 | (*i)->print (); |
396 | |
452 | |
397 | printf ("\n"); |
453 | printf ("\n"); |
398 | } |
454 | } |
399 | |
455 | |
400 | void |
456 | configuration::configuration () |
401 | conf_node::print () |
|
|
402 | { |
457 | { |
403 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
458 | init (); |
404 | id, |
|
|
405 | id >> 8, id & 0xff, |
|
|
406 | compress ? 'Y' : 'N', |
|
|
407 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
408 | connectmode == C_NEVER ? "never" : |
|
|
409 | connectmode == C_ALWAYS ? "always" : "", |
|
|
410 | nodename, |
|
|
411 | hostname ? hostname : "", |
|
|
412 | hostname ? ":" : "", |
|
|
413 | hostname ? udp_port : 0 |
|
|
414 | ); |
|
|
415 | } |
459 | } |
416 | |
460 | |
|
|
461 | configuration::~configuration () |
|
|
462 | { |
|
|
463 | cleanup (); |
|
|
464 | } |
|
|
465 | |
|
|
466 | |