… | |
… | |
31 | #include <netdb.h> |
31 | #include <netdb.h> |
32 | #include <sys/stat.h> |
32 | #include <sys/stat.h> |
33 | #include <sys/types.h> |
33 | #include <sys/types.h> |
34 | #include <unistd.h> |
34 | #include <unistd.h> |
35 | |
35 | |
|
|
36 | #include "netcompat.h" |
|
|
37 | |
36 | #include <openssl/err.h> |
38 | #include <openssl/err.h> |
37 | #include <openssl/pem.h> |
39 | #include <openssl/pem.h> |
38 | #include <openssl/rsa.h> |
40 | #include <openssl/rsa.h> |
39 | #include <openssl/rand.h> |
41 | #include <openssl/rand.h> |
40 | |
42 | |
… | |
… | |
49 | char *identname; |
51 | char *identname; |
50 | char *pidfilename; |
52 | char *pidfilename; |
51 | |
53 | |
52 | struct configuration conf; |
54 | struct configuration conf; |
53 | |
55 | |
54 | configuration::configuration () |
56 | u8 best_protocol (u8 protset) |
55 | { |
57 | { |
56 | init (); |
58 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
57 | } |
59 | if (protset & PROT_ICMPv4) return PROT_ICMPv4; |
|
|
60 | if (protset & PROT_UDPv4 ) return PROT_UDPv4; |
|
|
61 | if (protset & PROT_TCPv4 ) return PROT_TCPv4; |
58 | |
62 | |
59 | configuration::~configuration () |
63 | return 0; |
|
|
64 | } |
|
|
65 | |
|
|
66 | const char *strprotocol (u8 protocol) |
60 | { |
67 | { |
61 | cleanup (); |
68 | if (protocol & PROT_IPv4 ) return "rawip"; |
|
|
69 | if (protocol & PROT_ICMPv4) return "icmp"; |
|
|
70 | if (protocol & PROT_UDPv4 ) return "udp"; |
|
|
71 | if (protocol & PROT_TCPv4 ) return "tcp"; |
|
|
72 | |
|
|
73 | return "<unknown>"; |
|
|
74 | } |
|
|
75 | |
|
|
76 | void |
|
|
77 | conf_node::print () |
|
|
78 | { |
|
|
79 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
|
|
80 | id, |
|
|
81 | id >> 8, id & 0xff, |
|
|
82 | compress ? 'Y' : 'N', |
|
|
83 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
84 | connectmode == C_NEVER ? "never" : |
|
|
85 | connectmode == C_ALWAYS ? "always" : "", |
|
|
86 | nodename, |
|
|
87 | hostname ? hostname : "", |
|
|
88 | hostname ? ":" : "", |
|
|
89 | hostname ? udp_port : 0 |
|
|
90 | ); |
|
|
91 | } |
|
|
92 | |
|
|
93 | conf_node::~conf_node () |
|
|
94 | { |
|
|
95 | if (rsa_key) |
|
|
96 | RSA_free (rsa_key); |
|
|
97 | |
|
|
98 | free (nodename); |
|
|
99 | free (hostname); |
62 | } |
100 | } |
63 | |
101 | |
64 | void configuration::init () |
102 | void configuration::init () |
65 | { |
103 | { |
66 | memset (this, 0, sizeof (*this)); |
104 | memset (this, 0, sizeof (*this)); |
67 | |
105 | |
68 | rekey = DEFAULT_REKEY; |
106 | rekey = DEFAULT_REKEY; |
69 | keepalive = DEFAULT_KEEPALIVE; |
107 | keepalive = DEFAULT_KEEPALIVE; |
|
|
108 | llevel = L_INFO; |
|
|
109 | ip_proto = IPPROTO_GRE; |
|
|
110 | #if ENABLE_ICMP |
|
|
111 | icmp_type = ICMP_ECHOREPLY; |
|
|
112 | #endif |
70 | |
113 | |
71 | default_node.port = DEFAULT_PORT; |
114 | default_node.udp_port = DEFAULT_UDPPORT; |
|
|
115 | default_node.tcp_port = DEFAULT_UDPPORT; |
72 | default_node.connectmode = conf_node::C_ALWAYS; |
116 | default_node.connectmode = conf_node::C_ALWAYS; |
73 | default_node.compress = true; |
117 | default_node.compress = true; |
|
|
118 | default_node.protocols = PROT_UDPv4; |
74 | } |
119 | } |
75 | |
120 | |
76 | void configuration::cleanup() |
121 | void configuration::cleanup() |
77 | { |
122 | { |
78 | if (rsa_key) |
123 | if (rsa_key) |
79 | RSA_free (rsa_key); |
124 | RSA_free (rsa_key); |
80 | |
125 | |
81 | free (ifname); |
|
|
82 | |
|
|
83 | rsa_key = 0; |
126 | rsa_key = 0; |
84 | ifname = 0; |
127 | |
|
|
128 | free (ifname); ifname = 0; |
|
|
129 | #if ENABLE_HTTP_PROXY |
|
|
130 | free (proxy_host); proxy_host = 0; |
|
|
131 | free (proxy_auth); proxy_auth = 0; |
|
|
132 | #endif |
85 | } |
133 | } |
86 | |
134 | |
87 | void |
135 | void |
88 | configuration::clear_config () |
136 | configuration::clear_config () |
89 | { |
137 | { |
… | |
… | |
93 | nodes.clear (); |
141 | nodes.clear (); |
94 | |
142 | |
95 | cleanup (); |
143 | cleanup (); |
96 | init (); |
144 | init (); |
97 | } |
145 | } |
|
|
146 | |
|
|
147 | #define parse_bool(target,name,trueval,falseval) \ |
|
|
148 | if (!strcmp (val, "yes")) target = trueval; \ |
|
|
149 | else if (!strcmp (val, "no")) target = falseval; \ |
|
|
150 | else if (!strcmp (val, "true")) target = trueval; \ |
|
|
151 | else if (!strcmp (val, "false")) target = falseval; \ |
|
|
152 | else if (!strcmp (val, "on")) target = trueval; \ |
|
|
153 | else if (!strcmp (val, "off")) target = falseval; \ |
|
|
154 | else \ |
|
|
155 | slog (L_WARN, \ |
|
|
156 | _("illegal value for '%s', only 'yes|true|on' or 'no|false|off' allowed, at '%s' line %d"), \ |
|
|
157 | name, var, fname, lineno); |
98 | |
158 | |
99 | void configuration::read_config (bool need_keys) |
159 | void configuration::read_config (bool need_keys) |
100 | { |
160 | { |
101 | char *fname; |
161 | char *fname; |
102 | FILE *f; |
162 | FILE *f; |
… | |
… | |
162 | if (!strcmp (var, "loglevel")) |
222 | if (!strcmp (var, "loglevel")) |
163 | { |
223 | { |
164 | loglevel l = string_to_loglevel (val); |
224 | loglevel l = string_to_loglevel (val); |
165 | |
225 | |
166 | if (l != L_NONE) |
226 | if (l != L_NONE) |
167 | set_loglevel (l); |
227 | llevel = l; |
168 | else |
228 | else |
169 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
229 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
170 | } |
230 | } |
|
|
231 | else if (!strcmp (var, "ip-proto")) |
|
|
232 | ip_proto = atoi (val); |
|
|
233 | #if ENABLE_ICMP |
|
|
234 | //TODO: error message |
|
|
235 | else if (!strcmp (var, "icmp-type")) |
|
|
236 | icmp_type = atoi (val); |
|
|
237 | #endif |
171 | |
238 | |
172 | // per config |
239 | // per config |
173 | else if (!strcmp (var, "node")) |
240 | else if (!strcmp (var, "node")) |
174 | { |
241 | { |
175 | default_node.id++; |
242 | default_node.id++; |
… | |
… | |
218 | } |
285 | } |
219 | else if (!strcmp (var, "private-key")) |
286 | else if (!strcmp (var, "private-key")) |
220 | prikeyfile = strdup (val); |
287 | prikeyfile = strdup (val); |
221 | else if (!strcmp (var, "ifpersist")) |
288 | else if (!strcmp (var, "ifpersist")) |
222 | { |
289 | { |
223 | if (!strcmp (val, "yes")) |
290 | parse_bool (ifpersist, "ifpersist", true, false); |
224 | ifpersist = true; |
|
|
225 | else if (!strcmp (val, "no")) |
|
|
226 | ifpersist = false; |
|
|
227 | else |
|
|
228 | slog (L_WARN, |
|
|
229 | _("illegal value for 'ifpersist', only 'yes' or 'no' allowed, at '%s' line %d"), |
|
|
230 | var, fname, lineno); |
|
|
231 | } |
291 | } |
232 | else if (!strcmp (var, "ifname")) |
292 | else if (!strcmp (var, "ifname")) |
233 | ifname = strdup (val); |
293 | ifname = strdup (val); |
234 | else if (!strcmp (var, "rekey")) |
294 | else if (!strcmp (var, "rekey")) |
235 | rekey = atoi (val); |
295 | rekey = atoi (val); |
… | |
… | |
241 | script_if_up = strdup (val); |
301 | script_if_up = strdup (val); |
242 | else if (!strcmp (var, "node-up")) |
302 | else if (!strcmp (var, "node-up")) |
243 | script_node_up = strdup (val); |
303 | script_node_up = strdup (val); |
244 | else if (!strcmp (var, "node-down")) |
304 | else if (!strcmp (var, "node-down")) |
245 | script_node_down = strdup (val); |
305 | script_node_down = strdup (val); |
|
|
306 | #if ENABLE_HTTP_PROXY |
|
|
307 | else if (!strcmp (var, "http-proxy-host")) |
|
|
308 | proxy_host = strdup (val); |
|
|
309 | else if (!strcmp (var, "http-proxy-port")) |
|
|
310 | proxy_port = atoi (val); |
|
|
311 | else if (!strcmp (var, "http-proxy-auth")) |
|
|
312 | proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); |
|
|
313 | #endif |
246 | |
314 | |
247 | /* node-specific, non-defaultable */ |
315 | /* node-specific, non-defaultable */ |
248 | else if (node != &default_node && !strcmp (var, "hostname")) |
316 | else if (node != &default_node && !strcmp (var, "hostname")) |
249 | { |
317 | { |
250 | free (node->hostname); |
318 | free (node->hostname); |
251 | node->hostname = strdup (val); |
319 | node->hostname = strdup (val); |
252 | } |
320 | } |
253 | |
321 | |
254 | /* node-specific, defaultable */ |
322 | /* node-specific, defaultable */ |
255 | else if (!strcmp (var, "port")) |
323 | else if (!strcmp (var, "udp-port")) |
256 | node->port = atoi (val); |
324 | node->udp_port = atoi (val); |
|
|
325 | else if (!strcmp (var, "tcp-port")) |
|
|
326 | node->tcp_port = atoi (val); |
257 | else if (!strcmp (var, "router-priority")) |
327 | else if (!strcmp (var, "router-priority")) |
258 | node->routerprio = atoi (val); |
328 | node->routerprio = atoi (val); |
259 | else if (!strcmp (var, "connect")) |
329 | else if (!strcmp (var, "connect")) |
260 | { |
330 | { |
261 | if (!strcmp (val, "ondemand")) |
331 | if (!strcmp (val, "ondemand")) |
262 | node->connectmode = conf_node::C_ONDEMAND; |
332 | node->connectmode = conf_node::C_ONDEMAND; |
263 | else if (!strcmp (val, "never")) |
333 | else if (!strcmp (val, "never")) |
264 | node->connectmode = conf_node::C_NEVER; |
334 | node->connectmode = conf_node::C_NEVER; |
265 | else if (!strcmp (val, "always")) |
335 | else if (!strcmp (val, "always")) |
266 | node->connectmode = conf_node::C_ALWAYS; |
336 | node->connectmode = conf_node::C_ALWAYS; |
|
|
337 | else if (!strcmp (val, "disabled")) |
|
|
338 | node->connectmode = conf_node::C_DISABLED; |
267 | else |
339 | else |
268 | slog (L_WARN, |
340 | slog (L_WARN, |
269 | _("illegal value for 'connectmode', use one of 'ondemand', 'never' or 'always', at '%s' line %d"), |
341 | _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
270 | var, fname, lineno); |
342 | var, fname, lineno); |
271 | } |
343 | } |
|
|
344 | else if (!strcmp (var, "inherit-tos")) |
|
|
345 | { |
|
|
346 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
|
|
347 | } |
272 | else if (!strcmp (var, "compress")) |
348 | else if (!strcmp (var, "compress")) |
273 | { |
349 | { |
274 | if (!strcmp (val, "yes")) |
350 | parse_bool (node->compress, "compress", true, false); |
275 | node->compress = true; |
|
|
276 | else if (!strcmp (val, "no")) |
|
|
277 | node->compress = false; |
|
|
278 | else |
351 | } |
279 | slog (L_WARN, |
352 | // all these bool options really really cost a lot of executable size! |
280 | _("illegal value for 'compress', only 'yes' or 'no' allowed, at '%s' line %d"), |
353 | else if (!strcmp (var, "enable-tcp")) |
281 | var, fname, lineno); |
354 | { |
|
|
355 | #if ENABLE_TCP |
|
|
356 | u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
|
|
357 | #endif |
|
|
358 | } |
|
|
359 | else if (!strcmp (var, "enable-icmp")) |
|
|
360 | { |
|
|
361 | #if ENABLE_ICMP |
|
|
362 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
|
|
363 | #endif |
|
|
364 | } |
|
|
365 | else if (!strcmp (var, "enable-udp")) |
|
|
366 | { |
|
|
367 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
|
|
368 | } |
|
|
369 | else if (!strcmp (var, "enable-rawip")) |
|
|
370 | { |
|
|
371 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
282 | } |
372 | } |
283 | |
373 | |
284 | // unknown or misplaced |
374 | // unknown or misplaced |
285 | else |
375 | else |
286 | { |
|
|
287 | slog (L_WARN, |
376 | slog (L_WARN, |
288 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
377 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
289 | var, fname, lineno); |
378 | var, fname, lineno); |
290 | } |
|
|
291 | } |
379 | } |
292 | |
380 | |
293 | fclose (f); |
381 | fclose (f); |
294 | } |
382 | } |
295 | else |
383 | else |
… | |
… | |
354 | printf (_("MTU: %d\n"), mtu); |
442 | printf (_("MTU: %d\n"), mtu); |
355 | printf (_("rekeying interval: %d\n"), rekey); |
443 | printf (_("rekeying interval: %d\n"), rekey); |
356 | printf (_("keepalive interval: %d\n"), keepalive); |
444 | printf (_("keepalive interval: %d\n"), keepalive); |
357 | printf (_("interface: %s\n"), ifname); |
445 | printf (_("interface: %s\n"), ifname); |
358 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
446 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
359 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); |
447 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1); |
360 | printf ("\n"); |
448 | printf ("\n"); |
361 | |
449 | |
362 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
450 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
363 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
451 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
364 | |
452 | |
… | |
… | |
366 | (*i)->print (); |
454 | (*i)->print (); |
367 | |
455 | |
368 | printf ("\n"); |
456 | printf ("\n"); |
369 | } |
457 | } |
370 | |
458 | |
371 | void |
459 | configuration::configuration () |
372 | conf_node::print () |
|
|
373 | { |
460 | { |
374 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
461 | init (); |
375 | id, |
|
|
376 | id >> 8, id & 0xff, |
|
|
377 | compress ? 'Y' : 'N', |
|
|
378 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
379 | connectmode == C_NEVER ? "never" : |
|
|
380 | connectmode == C_ALWAYS ? "always" : "", |
|
|
381 | nodename, |
|
|
382 | hostname ? hostname : "", |
|
|
383 | hostname ? ":" : "", |
|
|
384 | hostname ? port : 0 |
|
|
385 | ); |
|
|
386 | } |
462 | } |
387 | |
463 | |
|
|
464 | configuration::~configuration () |
|
|
465 | { |
|
|
466 | cleanup (); |
|
|
467 | } |
|
|
468 | |
|
|
469 | |