… | |
… | |
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; |
|
|
106 | llevel = L_INFO; |
|
|
107 | ip_proto = IPPROTO_GRE; |
70 | |
108 | |
71 | default_node.port = DEFAULT_PORT; |
109 | default_node.udp_port = DEFAULT_UDPPORT; |
|
|
110 | default_node.tcp_port = DEFAULT_UDPPORT; |
72 | default_node.connectmode = conf_node::C_ALWAYS; |
111 | default_node.connectmode = conf_node::C_ALWAYS; |
73 | default_node.compress = true; |
112 | default_node.compress = true; |
|
|
113 | default_node.protocols = PROT_UDPv4; |
74 | } |
114 | } |
75 | |
115 | |
76 | void configuration::cleanup() |
116 | void configuration::cleanup() |
77 | { |
117 | { |
78 | if (rsa_key) |
118 | if (rsa_key) |
79 | RSA_free (rsa_key); |
119 | RSA_free (rsa_key); |
80 | |
120 | |
81 | free (ifname); |
|
|
82 | |
|
|
83 | rsa_key = 0; |
121 | rsa_key = 0; |
84 | 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 |
85 | } |
128 | } |
86 | |
129 | |
87 | void |
130 | void |
88 | configuration::clear_config () |
131 | configuration::clear_config () |
89 | { |
132 | { |
… | |
… | |
93 | nodes.clear (); |
136 | nodes.clear (); |
94 | |
137 | |
95 | cleanup (); |
138 | cleanup (); |
96 | init (); |
139 | init (); |
97 | } |
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); |
98 | |
153 | |
99 | void configuration::read_config (bool need_keys) |
154 | void configuration::read_config (bool need_keys) |
100 | { |
155 | { |
101 | char *fname; |
156 | char *fname; |
102 | FILE *f; |
157 | FILE *f; |
… | |
… | |
162 | if (!strcmp (var, "loglevel")) |
217 | if (!strcmp (var, "loglevel")) |
163 | { |
218 | { |
164 | loglevel l = string_to_loglevel (val); |
219 | loglevel l = string_to_loglevel (val); |
165 | |
220 | |
166 | if (l != L_NONE) |
221 | if (l != L_NONE) |
167 | set_loglevel (l); |
222 | llevel = l; |
168 | else |
223 | else |
169 | 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); |
170 | } |
225 | } |
|
|
226 | else if (!strcmp (var, "ip-proto")) |
|
|
227 | ip_proto = atoi (val); |
171 | |
228 | |
172 | // per config |
229 | // per config |
173 | else if (!strcmp (var, "node")) |
230 | else if (!strcmp (var, "node")) |
174 | { |
231 | { |
175 | default_node.id++; |
232 | default_node.id++; |
… | |
… | |
218 | } |
275 | } |
219 | else if (!strcmp (var, "private-key")) |
276 | else if (!strcmp (var, "private-key")) |
220 | prikeyfile = strdup (val); |
277 | prikeyfile = strdup (val); |
221 | else if (!strcmp (var, "ifpersist")) |
278 | else if (!strcmp (var, "ifpersist")) |
222 | { |
279 | { |
223 | if (!strcmp (val, "yes")) |
280 | 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 | } |
281 | } |
232 | else if (!strcmp (var, "ifname")) |
282 | else if (!strcmp (var, "ifname")) |
233 | ifname = strdup (val); |
283 | ifname = strdup (val); |
234 | else if (!strcmp (var, "rekey")) |
284 | else if (!strcmp (var, "rekey")) |
235 | rekey = atoi (val); |
285 | rekey = atoi (val); |
… | |
… | |
241 | script_if_up = strdup (val); |
291 | script_if_up = strdup (val); |
242 | else if (!strcmp (var, "node-up")) |
292 | else if (!strcmp (var, "node-up")) |
243 | script_node_up = strdup (val); |
293 | script_node_up = strdup (val); |
244 | else if (!strcmp (var, "node-down")) |
294 | else if (!strcmp (var, "node-down")) |
245 | 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 |
246 | |
304 | |
247 | /* node-specific, non-defaultable */ |
305 | /* node-specific, non-defaultable */ |
248 | else if (node != &default_node && !strcmp (var, "hostname")) |
306 | else if (node != &default_node && !strcmp (var, "hostname")) |
249 | { |
307 | { |
250 | free (node->hostname); |
308 | free (node->hostname); |
251 | node->hostname = strdup (val); |
309 | node->hostname = strdup (val); |
252 | } |
310 | } |
253 | |
311 | |
254 | /* node-specific, defaultable */ |
312 | /* node-specific, defaultable */ |
255 | else if (!strcmp (var, "port")) |
313 | else if (!strcmp (var, "udp-port")) |
256 | node->port = atoi (val); |
314 | node->udp_port = atoi (val); |
|
|
315 | else if (!strcmp (var, "tcp-port")) |
|
|
316 | node->tcp_port = atoi (val); |
257 | else if (!strcmp (var, "router-priority")) |
317 | else if (!strcmp (var, "router-priority")) |
258 | node->routerprio = atoi (val); |
318 | node->routerprio = atoi (val); |
259 | else if (!strcmp (var, "connect")) |
319 | else if (!strcmp (var, "connect")) |
260 | { |
320 | { |
261 | if (!strcmp (val, "ondemand")) |
321 | if (!strcmp (val, "ondemand")) |
262 | node->connectmode = conf_node::C_ONDEMAND; |
322 | node->connectmode = conf_node::C_ONDEMAND; |
263 | else if (!strcmp (val, "never")) |
323 | else if (!strcmp (val, "never")) |
264 | node->connectmode = conf_node::C_NEVER; |
324 | node->connectmode = conf_node::C_NEVER; |
265 | else if (!strcmp (val, "always")) |
325 | else if (!strcmp (val, "always")) |
266 | node->connectmode = conf_node::C_ALWAYS; |
326 | node->connectmode = conf_node::C_ALWAYS; |
|
|
327 | else if (!strcmp (val, "disabled")) |
|
|
328 | node->connectmode = conf_node::C_DISABLED; |
267 | else |
329 | else |
268 | slog (L_WARN, |
330 | slog (L_WARN, |
269 | _("illegal value for 'connectmode', use one of 'ondemand', 'never' or 'always', at '%s' line %d"), |
331 | _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
270 | var, fname, lineno); |
332 | var, fname, lineno); |
271 | } |
333 | } |
|
|
334 | else if (!strcmp (var, "inherit-tos")) |
|
|
335 | { |
|
|
336 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
|
|
337 | } |
272 | else if (!strcmp (var, "compress")) |
338 | else if (!strcmp (var, "compress")) |
273 | { |
339 | { |
274 | if (!strcmp (val, "yes")) |
340 | parse_bool (node->compress, "compress", true, false); |
275 | node->compress = true; |
|
|
276 | else if (!strcmp (val, "no")) |
|
|
277 | node->compress = false; |
|
|
278 | else |
341 | } |
279 | slog (L_WARN, |
342 | // 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"), |
343 | else if (!strcmp (var, "enable-tcp")) |
281 | 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; |
282 | } |
356 | } |
283 | |
357 | |
284 | // unknown or misplaced |
358 | // unknown or misplaced |
285 | else |
359 | else |
286 | { |
|
|
287 | slog (L_WARN, |
360 | slog (L_WARN, |
288 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
361 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
289 | var, fname, lineno); |
362 | var, fname, lineno); |
290 | } |
|
|
291 | } |
363 | } |
292 | |
364 | |
293 | fclose (f); |
365 | fclose (f); |
294 | } |
366 | } |
295 | else |
367 | else |
… | |
… | |
366 | (*i)->print (); |
438 | (*i)->print (); |
367 | |
439 | |
368 | printf ("\n"); |
440 | printf ("\n"); |
369 | } |
441 | } |
370 | |
442 | |
371 | void |
443 | configuration::configuration () |
372 | conf_node::print () |
|
|
373 | { |
444 | { |
374 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
445 | 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 | } |
446 | } |
387 | |
447 | |
|
|
448 | configuration::~configuration () |
|
|
449 | { |
|
|
450 | cleanup (); |
|
|
451 | } |
|
|
452 | |
|
|
453 | |