1 | /* |
1 | /* |
2 | conf.c -- configuration code |
2 | conf.c -- configuration code |
3 | Copyright (C) 1998 Robert van der Meulen |
3 | Copyright (C) 2003 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 | #if ENABLE_ICMP |
|
|
231 | //TODO: error message |
|
|
232 | else if (!strcmp (var, "icmp-type")) |
|
|
233 | icmp_type = atoi (val); |
|
|
234 | #endif |
172 | |
235 | |
173 | // per config |
236 | // per config |
174 | else if (!strcmp (var, "node")) |
237 | else if (!strcmp (var, "node")) |
175 | { |
238 | { |
176 | default_node.id++; |
239 | default_node.id++; |
… | |
… | |
219 | } |
282 | } |
220 | else if (!strcmp (var, "private-key")) |
283 | else if (!strcmp (var, "private-key")) |
221 | prikeyfile = strdup (val); |
284 | prikeyfile = strdup (val); |
222 | else if (!strcmp (var, "ifpersist")) |
285 | else if (!strcmp (var, "ifpersist")) |
223 | { |
286 | { |
224 | if (!strcmp (val, "yes")) |
287 | 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 | } |
288 | } |
233 | else if (!strcmp (var, "ifname")) |
289 | else if (!strcmp (var, "ifname")) |
234 | ifname = strdup (val); |
290 | ifname = strdup (val); |
235 | else if (!strcmp (var, "rekey")) |
291 | else if (!strcmp (var, "rekey")) |
236 | rekey = atoi (val); |
292 | rekey = atoi (val); |
… | |
… | |
242 | script_if_up = strdup (val); |
298 | script_if_up = strdup (val); |
243 | else if (!strcmp (var, "node-up")) |
299 | else if (!strcmp (var, "node-up")) |
244 | script_node_up = strdup (val); |
300 | script_node_up = strdup (val); |
245 | else if (!strcmp (var, "node-down")) |
301 | else if (!strcmp (var, "node-down")) |
246 | script_node_down = strdup (val); |
302 | script_node_down = strdup (val); |
|
|
303 | #if ENABLE_HTTP_PROXY |
|
|
304 | else if (!strcmp (var, "http-proxy-host")) |
|
|
305 | proxy_host = strdup (val); |
|
|
306 | else if (!strcmp (var, "http-proxy-port")) |
|
|
307 | proxy_port = atoi (val); |
|
|
308 | else if (!strcmp (var, "http-proxy-auth")) |
|
|
309 | proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); |
|
|
310 | #endif |
247 | |
311 | |
248 | /* node-specific, non-defaultable */ |
312 | /* node-specific, non-defaultable */ |
249 | else if (node != &default_node && !strcmp (var, "hostname")) |
313 | else if (node != &default_node && !strcmp (var, "hostname")) |
250 | { |
314 | { |
251 | free (node->hostname); |
315 | free (node->hostname); |
252 | node->hostname = strdup (val); |
316 | node->hostname = strdup (val); |
253 | } |
317 | } |
254 | |
318 | |
255 | /* node-specific, defaultable */ |
319 | /* node-specific, defaultable */ |
256 | else if (!strcmp (var, "port")) |
320 | else if (!strcmp (var, "udp-port")) |
257 | node->port = atoi (val); |
321 | node->udp_port = atoi (val); |
|
|
322 | else if (!strcmp (var, "tcp-port")) |
|
|
323 | node->tcp_port = atoi (val); |
258 | else if (!strcmp (var, "router-priority")) |
324 | else if (!strcmp (var, "router-priority")) |
259 | node->routerprio = atoi (val); |
325 | node->routerprio = atoi (val); |
260 | else if (!strcmp (var, "connect")) |
326 | else if (!strcmp (var, "connect")) |
261 | { |
327 | { |
262 | if (!strcmp (val, "ondemand")) |
328 | if (!strcmp (val, "ondemand")) |
263 | node->connectmode = conf_node::C_ONDEMAND; |
329 | node->connectmode = conf_node::C_ONDEMAND; |
264 | else if (!strcmp (val, "never")) |
330 | else if (!strcmp (val, "never")) |
265 | node->connectmode = conf_node::C_NEVER; |
331 | node->connectmode = conf_node::C_NEVER; |
266 | else if (!strcmp (val, "always")) |
332 | else if (!strcmp (val, "always")) |
267 | node->connectmode = conf_node::C_ALWAYS; |
333 | node->connectmode = conf_node::C_ALWAYS; |
|
|
334 | else if (!strcmp (val, "disabled")) |
|
|
335 | node->connectmode = conf_node::C_DISABLED; |
268 | else |
336 | else |
269 | slog (L_WARN, |
337 | slog (L_WARN, |
270 | _("illegal value for 'connectmode', use one of 'ondemand', 'never' or 'always', at '%s' line %d"), |
338 | _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
271 | var, fname, lineno); |
339 | var, fname, lineno); |
272 | } |
340 | } |
|
|
341 | else if (!strcmp (var, "inherit-tos")) |
|
|
342 | { |
|
|
343 | parse_bool (node->inherit_tos, "inherit-tos", true, false); |
|
|
344 | } |
273 | else if (!strcmp (var, "compress")) |
345 | else if (!strcmp (var, "compress")) |
274 | { |
346 | { |
275 | if (!strcmp (val, "yes")) |
347 | parse_bool (node->compress, "compress", true, false); |
276 | node->compress = true; |
|
|
277 | else if (!strcmp (val, "no")) |
|
|
278 | node->compress = false; |
|
|
279 | else |
348 | } |
280 | slog (L_WARN, |
349 | // all these bool options really really cost a lot of executable size! |
281 | _("illegal value for 'compress', only 'yes' or 'no' allowed, at '%s' line %d"), |
350 | else if (!strcmp (var, "enable-tcp")) |
282 | var, fname, lineno); |
351 | { |
|
|
352 | #if ENABLE_TCP |
|
|
353 | u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
|
|
354 | #endif |
|
|
355 | } |
|
|
356 | else if (!strcmp (var, "enable-icmp")) |
|
|
357 | { |
|
|
358 | #if ENABLE_ICMP |
|
|
359 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
|
|
360 | #endif |
|
|
361 | } |
|
|
362 | else if (!strcmp (var, "enable-udp")) |
|
|
363 | { |
|
|
364 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
|
|
365 | } |
|
|
366 | else if (!strcmp (var, "enable-rawip")) |
|
|
367 | { |
|
|
368 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
283 | } |
369 | } |
284 | |
370 | |
285 | // unknown or misplaced |
371 | // unknown or misplaced |
286 | else |
372 | else |
287 | { |
|
|
288 | slog (L_WARN, |
373 | slog (L_WARN, |
289 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
374 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
290 | var, fname, lineno); |
375 | var, fname, lineno); |
291 | } |
|
|
292 | } |
376 | } |
293 | |
377 | |
294 | fclose (f); |
378 | fclose (f); |
295 | } |
379 | } |
296 | else |
380 | else |
… | |
… | |
355 | printf (_("MTU: %d\n"), mtu); |
439 | printf (_("MTU: %d\n"), mtu); |
356 | printf (_("rekeying interval: %d\n"), rekey); |
440 | printf (_("rekeying interval: %d\n"), rekey); |
357 | printf (_("keepalive interval: %d\n"), keepalive); |
441 | printf (_("keepalive interval: %d\n"), keepalive); |
358 | printf (_("interface: %s\n"), ifname); |
442 | printf (_("interface: %s\n"), ifname); |
359 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
443 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
360 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); |
444 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1); |
361 | printf ("\n"); |
445 | printf ("\n"); |
362 | |
446 | |
363 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
447 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
364 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
448 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
365 | |
449 | |
… | |
… | |
367 | (*i)->print (); |
451 | (*i)->print (); |
368 | |
452 | |
369 | printf ("\n"); |
453 | printf ("\n"); |
370 | } |
454 | } |
371 | |
455 | |
372 | void |
456 | configuration::configuration () |
373 | conf_node::print () |
|
|
374 | { |
457 | { |
375 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
458 | init (); |
376 | id, |
|
|
377 | id >> 8, id & 0xff, |
|
|
378 | compress ? 'Y' : 'N', |
|
|
379 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
380 | connectmode == C_NEVER ? "never" : |
|
|
381 | connectmode == C_ALWAYS ? "always" : "", |
|
|
382 | nodename, |
|
|
383 | hostname ? hostname : "", |
|
|
384 | hostname ? ":" : "", |
|
|
385 | hostname ? port : 0 |
|
|
386 | ); |
|
|
387 | } |
459 | } |
388 | |
460 | |
|
|
461 | configuration::~configuration () |
|
|
462 | { |
|
|
463 | cleanup (); |
|
|
464 | } |
|
|
465 | |
|
|
466 | |