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