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