… | |
… | |
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 | |
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_UDPv4) return PROT_UDPv4; |
|
|
60 | if (protset & PROT_TCPv4) return PROT_TCPv4; |
58 | |
61 | |
59 | configuration::~configuration () |
62 | return 0; |
|
|
63 | } |
|
|
64 | |
|
|
65 | const char *strprotocol (u8 protocol) |
60 | { |
66 | { |
61 | cleanup (); |
67 | if (protocol & PROT_IPv4 ) return "rawip"; |
|
|
68 | if (protocol & PROT_UDPv4) return "udp"; |
|
|
69 | if (protocol & PROT_TCPv4) return "tcp"; |
|
|
70 | |
|
|
71 | return "<unknown>"; |
|
|
72 | } |
|
|
73 | |
|
|
74 | void |
|
|
75 | conf_node::print () |
|
|
76 | { |
|
|
77 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
|
|
78 | id, |
|
|
79 | id >> 8, id & 0xff, |
|
|
80 | compress ? 'Y' : 'N', |
|
|
81 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
82 | connectmode == C_NEVER ? "never" : |
|
|
83 | connectmode == C_ALWAYS ? "always" : "", |
|
|
84 | nodename, |
|
|
85 | hostname ? hostname : "", |
|
|
86 | hostname ? ":" : "", |
|
|
87 | hostname ? udp_port : 0 |
|
|
88 | ); |
|
|
89 | } |
|
|
90 | |
|
|
91 | conf_node::~conf_node () |
|
|
92 | { |
|
|
93 | if (rsa_key) |
|
|
94 | RSA_free (rsa_key); |
|
|
95 | |
|
|
96 | free (nodename); |
|
|
97 | free (hostname); |
62 | } |
98 | } |
63 | |
99 | |
64 | void configuration::init () |
100 | void configuration::init () |
65 | { |
101 | { |
66 | memset (this, 0, sizeof (*this)); |
102 | memset (this, 0, sizeof (*this)); |
67 | |
103 | |
68 | rekey = DEFAULT_REKEY; |
104 | rekey = DEFAULT_REKEY; |
69 | keepalive = DEFAULT_KEEPALIVE; |
105 | keepalive = DEFAULT_KEEPALIVE; |
70 | llevel = L_INFO; |
106 | llevel = L_INFO; |
|
|
107 | ip_proto = IPPROTO_GRE; |
71 | |
108 | |
72 | default_node.port = DEFAULT_PORT; |
109 | default_node.udp_port = DEFAULT_UDPPORT; |
|
|
110 | default_node.tcp_port = DEFAULT_UDPPORT; |
73 | default_node.connectmode = conf_node::C_ALWAYS; |
111 | default_node.connectmode = conf_node::C_ALWAYS; |
74 | default_node.compress = true; |
112 | default_node.compress = true; |
|
|
113 | default_node.protocols = PROT_UDPv4; |
75 | } |
114 | } |
76 | |
115 | |
77 | void configuration::cleanup() |
116 | void configuration::cleanup() |
78 | { |
117 | { |
79 | if (rsa_key) |
118 | if (rsa_key) |
80 | RSA_free (rsa_key); |
119 | RSA_free (rsa_key); |
81 | |
120 | |
82 | free (ifname); |
|
|
83 | |
|
|
84 | rsa_key = 0; |
121 | rsa_key = 0; |
85 | ifname = 0; |
122 | |
|
|
123 | free (ifname); ifname = 0; |
|
|
124 | #if ENABLE_HTTP_PROXY |
|
|
125 | free (proxy_host); proxy_host = 0; |
|
|
126 | free (proxy_auth); proxy_auth = 0; |
|
|
127 | #endif |
86 | } |
128 | } |
87 | |
129 | |
88 | void |
130 | void |
89 | configuration::clear_config () |
131 | configuration::clear_config () |
90 | { |
132 | { |
… | |
… | |
94 | nodes.clear (); |
136 | nodes.clear (); |
95 | |
137 | |
96 | cleanup (); |
138 | cleanup (); |
97 | init (); |
139 | init (); |
98 | } |
140 | } |
|
|
141 | |
|
|
142 | #define parse_bool(target,name,trueval,falseval) \ |
|
|
143 | if (!strcmp (val, "yes")) target = trueval; \ |
|
|
144 | else if (!strcmp (val, "no")) target = falseval; \ |
|
|
145 | else if (!strcmp (val, "true")) target = trueval; \ |
|
|
146 | else if (!strcmp (val, "false")) target = falseval; \ |
|
|
147 | else if (!strcmp (val, "on")) target = trueval; \ |
|
|
148 | else if (!strcmp (val, "off")) target = falseval; \ |
|
|
149 | else \ |
|
|
150 | slog (L_WARN, \ |
|
|
151 | _("illegal value for '%s', only 'yes|true|on' or 'no|false|off' allowed, at '%s' line %d"), \ |
|
|
152 | name, var, fname, lineno); |
99 | |
153 | |
100 | void configuration::read_config (bool need_keys) |
154 | void configuration::read_config (bool need_keys) |
101 | { |
155 | { |
102 | char *fname; |
156 | char *fname; |
103 | FILE *f; |
157 | FILE *f; |
… | |
… | |
167 | if (l != L_NONE) |
221 | if (l != L_NONE) |
168 | llevel = l; |
222 | llevel = l; |
169 | else |
223 | else |
170 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
224 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
171 | } |
225 | } |
|
|
226 | else if (!strcmp (var, "ip-proto")) |
|
|
227 | ip_proto = atoi (val); |
172 | |
228 | |
173 | // per config |
229 | // per config |
174 | else if (!strcmp (var, "node")) |
230 | else if (!strcmp (var, "node")) |
175 | { |
231 | { |
176 | default_node.id++; |
232 | default_node.id++; |
… | |
… | |
219 | } |
275 | } |
220 | else if (!strcmp (var, "private-key")) |
276 | else if (!strcmp (var, "private-key")) |
221 | prikeyfile = strdup (val); |
277 | prikeyfile = strdup (val); |
222 | else if (!strcmp (var, "ifpersist")) |
278 | else if (!strcmp (var, "ifpersist")) |
223 | { |
279 | { |
224 | if (!strcmp (val, "yes")) |
280 | parse_bool (ifpersist, "ifpersist", true, false); |
225 | ifpersist = true; |
|
|
226 | else if (!strcmp (val, "no")) |
|
|
227 | ifpersist = false; |
|
|
228 | else |
|
|
229 | slog (L_WARN, |
|
|
230 | _("illegal value for 'ifpersist', only 'yes' or 'no' allowed, at '%s' line %d"), |
|
|
231 | var, fname, lineno); |
|
|
232 | } |
281 | } |
233 | else if (!strcmp (var, "ifname")) |
282 | else if (!strcmp (var, "ifname")) |
234 | ifname = strdup (val); |
283 | ifname = strdup (val); |
235 | else if (!strcmp (var, "rekey")) |
284 | else if (!strcmp (var, "rekey")) |
236 | rekey = atoi (val); |
285 | rekey = atoi (val); |
… | |
… | |
242 | script_if_up = strdup (val); |
291 | script_if_up = strdup (val); |
243 | else if (!strcmp (var, "node-up")) |
292 | else if (!strcmp (var, "node-up")) |
244 | script_node_up = strdup (val); |
293 | script_node_up = strdup (val); |
245 | else if (!strcmp (var, "node-down")) |
294 | else if (!strcmp (var, "node-down")) |
246 | script_node_down = strdup (val); |
295 | script_node_down = strdup (val); |
|
|
296 | #if ENABLE_HTTP_PROXY |
|
|
297 | else if (!strcmp (var, "http-proxy-host")) |
|
|
298 | proxy_host = strdup (val); |
|
|
299 | else if (!strcmp (var, "http-proxy-port")) |
|
|
300 | proxy_port = atoi (val); |
|
|
301 | else if (!strcmp (var, "http-proxy-auth")) |
|
|
302 | proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); |
|
|
303 | #endif |
247 | |
304 | |
248 | /* node-specific, non-defaultable */ |
305 | /* node-specific, non-defaultable */ |
249 | else if (node != &default_node && !strcmp (var, "hostname")) |
306 | else if (node != &default_node && !strcmp (var, "hostname")) |
250 | { |
307 | { |
251 | free (node->hostname); |
308 | free (node->hostname); |
252 | node->hostname = strdup (val); |
309 | node->hostname = strdup (val); |
253 | } |
310 | } |
254 | |
311 | |
255 | /* node-specific, defaultable */ |
312 | /* node-specific, defaultable */ |
256 | else if (!strcmp (var, "udp-port")) |
313 | else if (!strcmp (var, "udp-port")) |
257 | node->port = atoi (val); |
314 | node->udp_port = atoi (val); |
258 | else if (!strcmp (var, "port")) //deprecated |
315 | else if (!strcmp (var, "tcp-port")) |
259 | node->port = atoi (val); |
316 | node->tcp_port = atoi (val); |
260 | else if (!strcmp (var, "router-priority")) |
317 | else if (!strcmp (var, "router-priority")) |
261 | node->routerprio = atoi (val); |
318 | node->routerprio = atoi (val); |
262 | else if (!strcmp (var, "connect")) |
319 | else if (!strcmp (var, "connect")) |
263 | { |
320 | { |
264 | if (!strcmp (val, "ondemand")) |
321 | if (!strcmp (val, "ondemand")) |
… | |
… | |
274 | _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
331 | _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
275 | var, fname, lineno); |
332 | var, fname, lineno); |
276 | } |
333 | } |
277 | else if (!strcmp (var, "inherit-tos")) |
334 | else if (!strcmp (var, "inherit-tos")) |
278 | { |
335 | { |
279 | if (!strcmp (val, "yes")) |
336 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
280 | node->inherit_tos = true; |
|
|
281 | else if (!strcmp (val, "no")) |
|
|
282 | node->inherit_tos = false; |
|
|
283 | else |
337 | } |
284 | slog (L_WARN, |
|
|
285 | _("illegal value for 'compress', only 'yes' or 'no' allowed, at '%s' line %d"), |
|
|
286 | var, fname, lineno); |
|
|
287 | } |
|
|
288 | |
|
|
289 | else if (!strcmp (var, "compress")) |
338 | else if (!strcmp (var, "compress")) |
290 | { |
339 | { |
291 | if (!strcmp (val, "yes")) |
340 | parse_bool (node->compress, "compress", true, false); |
292 | node->compress = true; |
|
|
293 | else if (!strcmp (val, "no")) |
|
|
294 | node->compress = false; |
|
|
295 | else |
341 | } |
296 | slog (L_WARN, |
342 | // all these bool options really really cost a lot of executable size! |
297 | _("illegal value for 'compress', only 'yes' or 'no' allowed, at '%s' line %d"), |
343 | else if (!strcmp (var, "enable-tcp")) |
298 | var, fname, lineno); |
344 | { |
|
|
345 | #if ENABLE_TCP |
|
|
346 | u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
|
|
347 | #endif |
|
|
348 | } |
|
|
349 | else if (!strcmp (var, "enable-udp")) |
|
|
350 | { |
|
|
351 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
|
|
352 | } |
|
|
353 | else if (!strcmp (var, "enable-rawip")) |
|
|
354 | { |
|
|
355 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
299 | } |
356 | } |
300 | |
357 | |
301 | // unknown or misplaced |
358 | // unknown or misplaced |
302 | else |
359 | else |
303 | { |
|
|
304 | slog (L_WARN, |
360 | slog (L_WARN, |
305 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
361 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
306 | var, fname, lineno); |
362 | var, fname, lineno); |
307 | } |
|
|
308 | } |
363 | } |
309 | |
364 | |
310 | fclose (f); |
365 | fclose (f); |
311 | } |
366 | } |
312 | else |
367 | else |
… | |
… | |
383 | (*i)->print (); |
438 | (*i)->print (); |
384 | |
439 | |
385 | printf ("\n"); |
440 | printf ("\n"); |
386 | } |
441 | } |
387 | |
442 | |
388 | void |
443 | configuration::configuration () |
389 | conf_node::print () |
|
|
390 | { |
444 | { |
391 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
445 | init (); |
392 | id, |
|
|
393 | id >> 8, id & 0xff, |
|
|
394 | compress ? 'Y' : 'N', |
|
|
395 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
396 | connectmode == C_NEVER ? "never" : |
|
|
397 | connectmode == C_ALWAYS ? "always" : "", |
|
|
398 | nodename, |
|
|
399 | hostname ? hostname : "", |
|
|
400 | hostname ? ":" : "", |
|
|
401 | hostname ? port : 0 |
|
|
402 | ); |
|
|
403 | } |
446 | } |
404 | |
447 | |
|
|
448 | configuration::~configuration () |
|
|
449 | { |
|
|
450 | cleanup (); |
|
|
451 | } |
|
|
452 | |
|
|
453 | |