1 |
/* |
2 |
conf.c -- configuration code |
3 |
Copyright (C) 1998 Robert van der Meulen |
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 |
|
9 |
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 |
11 |
the Free Software Foundation; either version 2 of the License, or |
12 |
(at your option) any later version. |
13 |
|
14 |
This program is distributed in the hope that it will be useful, |
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
GNU General Public License for more details. |
18 |
|
19 |
You should have received a copy of the GNU General Public License |
20 |
along with this program; if not, write to the Free Software |
21 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
*/ |
23 |
|
24 |
#include "config.h" |
25 |
|
26 |
#include <cstdio> |
27 |
#include <cstdlib> |
28 |
#include <cstring> |
29 |
|
30 |
#include <errno.h> |
31 |
#include <netdb.h> |
32 |
#include <sys/stat.h> |
33 |
#include <sys/types.h> |
34 |
#include <unistd.h> |
35 |
|
36 |
#include <netinet/in.h> |
37 |
|
38 |
#include <openssl/err.h> |
39 |
#include <openssl/pem.h> |
40 |
#include <openssl/rsa.h> |
41 |
#include <openssl/rand.h> |
42 |
|
43 |
#include "gettext.h" |
44 |
|
45 |
#include "conf.h" |
46 |
#include "slog.h" |
47 |
#include "util.h" |
48 |
|
49 |
char *confbase; |
50 |
char *thisnode; |
51 |
char *identname; |
52 |
char *pidfilename; |
53 |
|
54 |
struct configuration conf; |
55 |
|
56 |
u8 best_protocol (u8 protset) |
57 |
{ |
58 |
if (protset & PROT_IPv4 ) return PROT_IPv4; |
59 |
if (protset & PROT_UDPv4) return PROT_UDPv4; |
60 |
if (protset & PROT_TCPv4) return PROT_TCPv4; |
61 |
|
62 |
return 0; |
63 |
} |
64 |
|
65 |
const char *strprotocol (u8 protocol) |
66 |
{ |
67 |
if (protocol & PROT_IPv4 ) return "rawip"; |
68 |
if (protocol & PROT_UDPv4) return "udp"; |
69 |
|
70 |
return "<unknown>"; |
71 |
} |
72 |
|
73 |
configuration::configuration () |
74 |
{ |
75 |
init (); |
76 |
} |
77 |
|
78 |
configuration::~configuration () |
79 |
{ |
80 |
cleanup (); |
81 |
} |
82 |
|
83 |
void configuration::init () |
84 |
{ |
85 |
memset (this, 0, sizeof (*this)); |
86 |
|
87 |
rekey = DEFAULT_REKEY; |
88 |
keepalive = DEFAULT_KEEPALIVE; |
89 |
llevel = L_INFO; |
90 |
ip_proto = IPPROTO_GRE; |
91 |
|
92 |
default_node.udp_port = DEFAULT_UDPPORT; |
93 |
default_node.tcp_port = DEFAULT_UDPPORT; |
94 |
default_node.connectmode = conf_node::C_ALWAYS; |
95 |
default_node.compress = true; |
96 |
default_node.protocols = PROT_UDPv4; |
97 |
} |
98 |
|
99 |
void configuration::cleanup() |
100 |
{ |
101 |
if (rsa_key) |
102 |
RSA_free (rsa_key); |
103 |
|
104 |
free (ifname); |
105 |
|
106 |
rsa_key = 0; |
107 |
ifname = 0; |
108 |
} |
109 |
|
110 |
void |
111 |
configuration::clear_config () |
112 |
{ |
113 |
for (configuration::node_vector::iterator i = nodes.begin(); i != nodes.end(); ++i) |
114 |
delete *i; |
115 |
|
116 |
nodes.clear (); |
117 |
|
118 |
cleanup (); |
119 |
init (); |
120 |
} |
121 |
|
122 |
#define parse_bool(target,name,trueval,falseval) \ |
123 |
if (!strcmp (val, "yes")) target = trueval; \ |
124 |
else if (!strcmp (val, "no")) target = falseval; \ |
125 |
else if (!strcmp (val, "true")) target = trueval; \ |
126 |
else if (!strcmp (val, "false")) target = falseval; \ |
127 |
else if (!strcmp (val, "on")) target = trueval; \ |
128 |
else if (!strcmp (val, "off")) target = falseval; \ |
129 |
else \ |
130 |
slog (L_WARN, \ |
131 |
_("illegal value for '%s', only 'yes|true|on' or 'no|false|off' allowed, at '%s' line %d"), \ |
132 |
name, var, fname, lineno); |
133 |
|
134 |
void configuration::read_config (bool need_keys) |
135 |
{ |
136 |
char *fname; |
137 |
FILE *f; |
138 |
|
139 |
clear_config (); |
140 |
|
141 |
asprintf (&fname, "%s/vped.conf", confbase); |
142 |
f = fopen (fname, "r"); |
143 |
|
144 |
if (f) |
145 |
{ |
146 |
char line[16384]; |
147 |
int lineno = 0; |
148 |
char *var, *val; |
149 |
conf_node *node = &default_node; |
150 |
|
151 |
while (fgets (line, sizeof (line), f)) |
152 |
{ |
153 |
lineno++; |
154 |
|
155 |
{ |
156 |
char *end = line + strlen (line); |
157 |
|
158 |
while (*end < ' ' && end >= line) |
159 |
end--; |
160 |
|
161 |
*++end = 0; |
162 |
} |
163 |
|
164 |
char *tok = line; |
165 |
|
166 |
retry: |
167 |
var = strtok (tok, "\t ="); |
168 |
tok = 0; |
169 |
|
170 |
if (!var || !var[0]) |
171 |
continue; /* no tokens on this line */ |
172 |
|
173 |
if (var[0] == '#') |
174 |
continue; /* comment: ignore */ |
175 |
|
176 |
val = strtok (NULL, "\t\n\r ="); |
177 |
|
178 |
if (!val || val[0] == '#') |
179 |
{ |
180 |
slog (L_WARN, |
181 |
_("no value for variable `%s', at '%s' line %d"), |
182 |
var, fname, lineno); |
183 |
break; |
184 |
} |
185 |
|
186 |
if (!strcmp (var, "on")) |
187 |
{ |
188 |
if (!::thisnode |
189 |
|| (val[0] == '!' && strcmp (val + 1, ::thisnode)) |
190 |
|| !strcmp (val, ::thisnode)) |
191 |
goto retry; |
192 |
|
193 |
continue; |
194 |
} |
195 |
|
196 |
// truly global |
197 |
if (!strcmp (var, "loglevel")) |
198 |
{ |
199 |
loglevel l = string_to_loglevel (val); |
200 |
|
201 |
if (l != L_NONE) |
202 |
llevel = l; |
203 |
else |
204 |
slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
205 |
} |
206 |
else if (!strcmp (var, "ip-proto")) |
207 |
ip_proto = atoi (val); |
208 |
|
209 |
// per config |
210 |
else if (!strcmp (var, "node")) |
211 |
{ |
212 |
default_node.id++; |
213 |
|
214 |
node = new conf_node (default_node); |
215 |
|
216 |
nodes.push_back (node); |
217 |
|
218 |
node->nodename = strdup (val); |
219 |
|
220 |
{ |
221 |
char *fname; |
222 |
FILE *f; |
223 |
|
224 |
asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
225 |
|
226 |
f = fopen (fname, "r"); |
227 |
if (f) |
228 |
{ |
229 |
node->rsa_key = RSA_new (); |
230 |
|
231 |
if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) |
232 |
{ |
233 |
ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
234 |
slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
235 |
exit (1); |
236 |
} |
237 |
|
238 |
RSA_blinding_on (node->rsa_key, 0); |
239 |
|
240 |
fclose (f); |
241 |
} |
242 |
else |
243 |
{ |
244 |
slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno)); |
245 |
|
246 |
if (need_keys) |
247 |
exit (1); |
248 |
} |
249 |
|
250 |
free (fname); |
251 |
} |
252 |
|
253 |
if (!::thisnode || !strcmp (node->nodename, ::thisnode)) |
254 |
thisnode = node; |
255 |
} |
256 |
else if (!strcmp (var, "private-key")) |
257 |
prikeyfile = strdup (val); |
258 |
else if (!strcmp (var, "ifpersist")) |
259 |
{ |
260 |
parse_bool (ifpersist, "ifpersist", true, false); |
261 |
} |
262 |
else if (!strcmp (var, "ifname")) |
263 |
ifname = strdup (val); |
264 |
else if (!strcmp (var, "rekey")) |
265 |
rekey = atoi (val); |
266 |
else if (!strcmp (var, "keepalive")) |
267 |
keepalive = atoi (val); |
268 |
else if (!strcmp (var, "mtu")) |
269 |
mtu = atoi (val); |
270 |
else if (!strcmp (var, "if-up")) |
271 |
script_if_up = strdup (val); |
272 |
else if (!strcmp (var, "node-up")) |
273 |
script_node_up = strdup (val); |
274 |
else if (!strcmp (var, "node-down")) |
275 |
script_node_down = strdup (val); |
276 |
|
277 |
/* node-specific, non-defaultable */ |
278 |
else if (node != &default_node && !strcmp (var, "hostname")) |
279 |
{ |
280 |
free (node->hostname); |
281 |
node->hostname = strdup (val); |
282 |
} |
283 |
|
284 |
/* node-specific, defaultable */ |
285 |
else if (!strcmp (var, "udp-port")) |
286 |
node->udp_port = atoi (val); |
287 |
else if (!strcmp (var, "tcp-port")) |
288 |
node->tcp_port = atoi (val); |
289 |
else if (!strcmp (var, "router-priority")) |
290 |
node->routerprio = atoi (val); |
291 |
else if (!strcmp (var, "connect")) |
292 |
{ |
293 |
if (!strcmp (val, "ondemand")) |
294 |
node->connectmode = conf_node::C_ONDEMAND; |
295 |
else if (!strcmp (val, "never")) |
296 |
node->connectmode = conf_node::C_NEVER; |
297 |
else if (!strcmp (val, "always")) |
298 |
node->connectmode = conf_node::C_ALWAYS; |
299 |
else if (!strcmp (val, "disabled")) |
300 |
node->connectmode = conf_node::C_DISABLED; |
301 |
else |
302 |
slog (L_WARN, |
303 |
_("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
304 |
var, fname, lineno); |
305 |
} |
306 |
else if (!strcmp (var, "inherit-tos")) |
307 |
{ |
308 |
parse_bool (node->inherit_tos, "inherit-tos", true, false); |
309 |
} |
310 |
else if (!strcmp (var, "compress")) |
311 |
{ |
312 |
parse_bool (node->compress, "compress", true, false); |
313 |
} |
314 |
// all these bool options really really cost a lot of executable size! |
315 |
else if (!strcmp (var, "enable-tcp")) |
316 |
{ |
317 |
u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
318 |
} |
319 |
else if (!strcmp (var, "enable-udp")) |
320 |
{ |
321 |
u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
322 |
} |
323 |
else if (!strcmp (var, "enable-rawip")) |
324 |
{ |
325 |
u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
326 |
} |
327 |
|
328 |
// unknown or misplaced |
329 |
else |
330 |
{ |
331 |
slog (L_WARN, |
332 |
_("unknown or misplaced variable `%s', at '%s' line %d"), |
333 |
var, fname, lineno); |
334 |
} |
335 |
} |
336 |
|
337 |
fclose (f); |
338 |
} |
339 |
else |
340 |
{ |
341 |
slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
342 |
exit (1); |
343 |
} |
344 |
|
345 |
free (fname); |
346 |
|
347 |
fname = config_filename (prikeyfile, "hostkey"); |
348 |
|
349 |
f = fopen (fname, "r"); |
350 |
if (f) |
351 |
{ |
352 |
rsa_key = RSA_new (); |
353 |
|
354 |
if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) |
355 |
{ |
356 |
ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
357 |
slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
358 |
exit (1); |
359 |
} |
360 |
|
361 |
RSA_blinding_on (rsa_key, 0); |
362 |
|
363 |
fclose (f); |
364 |
} |
365 |
else |
366 |
{ |
367 |
slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
368 |
|
369 |
if (need_keys) |
370 |
exit (1); |
371 |
} |
372 |
|
373 |
free (fname); |
374 |
} |
375 |
|
376 |
char *configuration::config_filename (const char *name, const char *dflt) |
377 |
{ |
378 |
char *fname; |
379 |
|
380 |
asprintf (&fname, name ? name : dflt, ::thisnode); |
381 |
|
382 |
if (!ABSOLUTE_PATH (fname)) |
383 |
{ |
384 |
char *rname = fname; |
385 |
asprintf (&fname, "%s/%s", confbase, rname); |
386 |
free (rname); |
387 |
} |
388 |
|
389 |
return fname; |
390 |
} |
391 |
|
392 |
void |
393 |
configuration::print () |
394 |
{ |
395 |
printf (_("\nConfiguration\n\n")); |
396 |
printf (_("# of nodes: %d\n"), nodes.size ()); |
397 |
printf (_("this node: %s\n"), thisnode ? thisnode->nodename : "<unset>"); |
398 |
printf (_("MTU: %d\n"), mtu); |
399 |
printf (_("rekeying interval: %d\n"), rekey); |
400 |
printf (_("keepalive interval: %d\n"), keepalive); |
401 |
printf (_("interface: %s\n"), ifname); |
402 |
printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
403 |
printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); |
404 |
printf ("\n"); |
405 |
|
406 |
printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
407 |
_("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
408 |
|
409 |
for (node_vector::iterator i = nodes.begin (); i != nodes.end (); ++i) |
410 |
(*i)->print (); |
411 |
|
412 |
printf ("\n"); |
413 |
} |
414 |
|
415 |
void |
416 |
conf_node::print () |
417 |
{ |
418 |
printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
419 |
id, |
420 |
id >> 8, id & 0xff, |
421 |
compress ? 'Y' : 'N', |
422 |
connectmode == C_ONDEMAND ? "ondemand" : |
423 |
connectmode == C_NEVER ? "never" : |
424 |
connectmode == C_ALWAYS ? "always" : "", |
425 |
nodename, |
426 |
hostname ? hostname : "", |
427 |
hostname ? ":" : "", |
428 |
hostname ? udp_port : 0 |
429 |
); |
430 |
} |
431 |
|