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 |
configuration::configuration () |
57 |
{ |
58 |
init (); |
59 |
} |
60 |
|
61 |
configuration::~configuration () |
62 |
{ |
63 |
cleanup (); |
64 |
} |
65 |
|
66 |
void configuration::init () |
67 |
{ |
68 |
memset (this, 0, sizeof (*this)); |
69 |
|
70 |
rekey = DEFAULT_REKEY; |
71 |
keepalive = DEFAULT_KEEPALIVE; |
72 |
llevel = L_INFO; |
73 |
ip_proto = IPPROTO_GRE; |
74 |
|
75 |
default_node.udp_port = DEFAULT_UDPPORT; |
76 |
default_node.connectmode = conf_node::C_ALWAYS; |
77 |
default_node.compress = true; |
78 |
default_node.can_send = PROT_UDPv4; |
79 |
default_node.can_recv = PROT_IPv4; |
80 |
} |
81 |
|
82 |
void configuration::cleanup() |
83 |
{ |
84 |
if (rsa_key) |
85 |
RSA_free (rsa_key); |
86 |
|
87 |
free (ifname); |
88 |
|
89 |
rsa_key = 0; |
90 |
ifname = 0; |
91 |
} |
92 |
|
93 |
void |
94 |
configuration::clear_config () |
95 |
{ |
96 |
for (configuration::node_vector::iterator i = nodes.begin(); i != nodes.end(); ++i) |
97 |
delete *i; |
98 |
|
99 |
nodes.clear (); |
100 |
|
101 |
cleanup (); |
102 |
init (); |
103 |
} |
104 |
|
105 |
#define parse_bool(target,name,trueval,falseval) \ |
106 |
if (!strcmp (val, "yes")) target = trueval; \ |
107 |
else if (!strcmp (val, "no")) target = falseval; \ |
108 |
else if (!strcmp (val, "true")) target = trueval; \ |
109 |
else if (!strcmp (val, "false")) target = falseval; \ |
110 |
else if (!strcmp (val, "on")) target = trueval; \ |
111 |
else if (!strcmp (val, "off")) target = falseval; \ |
112 |
else \ |
113 |
slog (L_WARN, \ |
114 |
_("illegal value for '%s', only 'yes|true|on' or 'no|false|off' allowed, at '%s' line %d"), \ |
115 |
name, var, fname, lineno); |
116 |
|
117 |
void configuration::read_config (bool need_keys) |
118 |
{ |
119 |
char *fname; |
120 |
FILE *f; |
121 |
|
122 |
clear_config (); |
123 |
|
124 |
asprintf (&fname, "%s/vped.conf", confbase); |
125 |
f = fopen (fname, "r"); |
126 |
|
127 |
if (f) |
128 |
{ |
129 |
char line[16384]; |
130 |
int lineno = 0; |
131 |
char *var, *val; |
132 |
conf_node *node = &default_node; |
133 |
|
134 |
while (fgets (line, sizeof (line), f)) |
135 |
{ |
136 |
lineno++; |
137 |
|
138 |
{ |
139 |
char *end = line + strlen (line); |
140 |
|
141 |
while (*end < ' ' && end >= line) |
142 |
end--; |
143 |
|
144 |
*++end = 0; |
145 |
} |
146 |
|
147 |
char *tok = line; |
148 |
|
149 |
retry: |
150 |
var = strtok (tok, "\t ="); |
151 |
tok = 0; |
152 |
|
153 |
if (!var || !var[0]) |
154 |
continue; /* no tokens on this line */ |
155 |
|
156 |
if (var[0] == '#') |
157 |
continue; /* comment: ignore */ |
158 |
|
159 |
val = strtok (NULL, "\t\n\r ="); |
160 |
|
161 |
if (!val || val[0] == '#') |
162 |
{ |
163 |
slog (L_WARN, |
164 |
_("no value for variable `%s', at '%s' line %d"), |
165 |
var, fname, lineno); |
166 |
break; |
167 |
} |
168 |
|
169 |
if (!strcmp (var, "on")) |
170 |
{ |
171 |
if (!::thisnode |
172 |
|| (val[0] == '!' && strcmp (val + 1, ::thisnode)) |
173 |
|| !strcmp (val, ::thisnode)) |
174 |
goto retry; |
175 |
|
176 |
continue; |
177 |
} |
178 |
|
179 |
// truly global |
180 |
if (!strcmp (var, "loglevel")) |
181 |
{ |
182 |
loglevel l = string_to_loglevel (val); |
183 |
|
184 |
if (l != L_NONE) |
185 |
llevel = l; |
186 |
else |
187 |
slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
188 |
} |
189 |
else if (!strcmp (var, "ip-proto")) |
190 |
ip_proto = atoi (val); |
191 |
|
192 |
// per config |
193 |
else if (!strcmp (var, "node")) |
194 |
{ |
195 |
default_node.id++; |
196 |
|
197 |
node = new conf_node (default_node); |
198 |
|
199 |
nodes.push_back (node); |
200 |
|
201 |
node->nodename = strdup (val); |
202 |
|
203 |
{ |
204 |
char *fname; |
205 |
FILE *f; |
206 |
|
207 |
asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
208 |
|
209 |
f = fopen (fname, "r"); |
210 |
if (f) |
211 |
{ |
212 |
node->rsa_key = RSA_new (); |
213 |
|
214 |
if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL)) |
215 |
{ |
216 |
ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
217 |
slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
218 |
exit (1); |
219 |
} |
220 |
|
221 |
RSA_blinding_on (node->rsa_key, 0); |
222 |
|
223 |
fclose (f); |
224 |
} |
225 |
else |
226 |
{ |
227 |
slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno)); |
228 |
|
229 |
if (need_keys) |
230 |
exit (1); |
231 |
} |
232 |
|
233 |
free (fname); |
234 |
} |
235 |
|
236 |
if (!::thisnode || !strcmp (node->nodename, ::thisnode)) |
237 |
thisnode = node; |
238 |
} |
239 |
else if (!strcmp (var, "private-key")) |
240 |
prikeyfile = strdup (val); |
241 |
else if (!strcmp (var, "ifpersist")) |
242 |
{ |
243 |
parse_bool (ifpersist, "ifpersist", true, false); |
244 |
} |
245 |
else if (!strcmp (var, "ifname")) |
246 |
ifname = strdup (val); |
247 |
else if (!strcmp (var, "rekey")) |
248 |
rekey = atoi (val); |
249 |
else if (!strcmp (var, "keepalive")) |
250 |
keepalive = atoi (val); |
251 |
else if (!strcmp (var, "mtu")) |
252 |
mtu = atoi (val); |
253 |
else if (!strcmp (var, "if-up")) |
254 |
script_if_up = strdup (val); |
255 |
else if (!strcmp (var, "node-up")) |
256 |
script_node_up = strdup (val); |
257 |
else if (!strcmp (var, "node-down")) |
258 |
script_node_down = strdup (val); |
259 |
|
260 |
/* node-specific, non-defaultable */ |
261 |
else if (node != &default_node && !strcmp (var, "hostname")) |
262 |
{ |
263 |
free (node->hostname); |
264 |
node->hostname = strdup (val); |
265 |
} |
266 |
|
267 |
/* node-specific, defaultable */ |
268 |
else if (!strcmp (var, "udp-port")) |
269 |
node->udp_port = atoi (val); |
270 |
else if (!strcmp (var, "router-priority")) |
271 |
node->routerprio = atoi (val); |
272 |
else if (!strcmp (var, "connect")) |
273 |
{ |
274 |
if (!strcmp (val, "ondemand")) |
275 |
node->connectmode = conf_node::C_ONDEMAND; |
276 |
else if (!strcmp (val, "never")) |
277 |
node->connectmode = conf_node::C_NEVER; |
278 |
else if (!strcmp (val, "always")) |
279 |
node->connectmode = conf_node::C_ALWAYS; |
280 |
else if (!strcmp (val, "disabled")) |
281 |
node->connectmode = conf_node::C_DISABLED; |
282 |
else |
283 |
slog (L_WARN, |
284 |
_("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"), |
285 |
var, fname, lineno); |
286 |
} |
287 |
else if (!strcmp (var, "inherit-tos")) |
288 |
{ |
289 |
parse_bool (node->inherit_tos, "inherit-tos", true, false); |
290 |
} |
291 |
else if (!strcmp (var, "compress")) |
292 |
{ |
293 |
parse_bool (node->compress, "compress", true, false); |
294 |
} |
295 |
// all these bool options really really cost a lot of executable size! |
296 |
else if (!strcmp (var, "can-send-udp")) |
297 |
{ |
298 |
u8 v; parse_bool (v, "can-send-udp", PROT_UDPv4, 0); node->can_send = (node->can_send & ~PROT_UDPv4) | v; |
299 |
} |
300 |
else if (!strcmp (var, "can-recv-udp")) |
301 |
{ |
302 |
u8 v; parse_bool (v, "can-recv-udp", PROT_UDPv4, 0); node->can_recv = (node->can_recv & ~PROT_UDPv4) | v; |
303 |
} |
304 |
else if (!strcmp (var, "can-send-rawip")) |
305 |
{ |
306 |
u8 v; parse_bool (v, "can-send-rawip", PROT_IPv4, 0); node->can_send = (node->can_send & ~PROT_IPv4) | v; |
307 |
} |
308 |
else if (!strcmp (var, "can-recv-rawip")) |
309 |
{ |
310 |
u8 v; parse_bool (v, "can-recv-rawip", PROT_IPv4, 0); node->can_recv = (node->can_recv & ~PROT_IPv4) | v; |
311 |
} |
312 |
|
313 |
// unknown or misplaced |
314 |
else |
315 |
{ |
316 |
slog (L_WARN, |
317 |
_("unknown or misplaced variable `%s', at '%s' line %d"), |
318 |
var, fname, lineno); |
319 |
} |
320 |
} |
321 |
|
322 |
fclose (f); |
323 |
} |
324 |
else |
325 |
{ |
326 |
slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno)); |
327 |
exit (1); |
328 |
} |
329 |
|
330 |
free (fname); |
331 |
|
332 |
fname = config_filename (prikeyfile, "hostkey"); |
333 |
|
334 |
f = fopen (fname, "r"); |
335 |
if (f) |
336 |
{ |
337 |
rsa_key = RSA_new (); |
338 |
|
339 |
if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL)) |
340 |
{ |
341 |
ERR_load_RSA_strings (); ERR_load_PEM_strings (); |
342 |
slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0)); |
343 |
exit (1); |
344 |
} |
345 |
|
346 |
RSA_blinding_on (rsa_key, 0); |
347 |
|
348 |
fclose (f); |
349 |
} |
350 |
else |
351 |
{ |
352 |
slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno)); |
353 |
|
354 |
if (need_keys) |
355 |
exit (1); |
356 |
} |
357 |
|
358 |
free (fname); |
359 |
} |
360 |
|
361 |
char *configuration::config_filename (const char *name, const char *dflt) |
362 |
{ |
363 |
char *fname; |
364 |
|
365 |
asprintf (&fname, name ? name : dflt, ::thisnode); |
366 |
|
367 |
if (!ABSOLUTE_PATH (fname)) |
368 |
{ |
369 |
char *rname = fname; |
370 |
asprintf (&fname, "%s/%s", confbase, rname); |
371 |
free (rname); |
372 |
} |
373 |
|
374 |
return fname; |
375 |
} |
376 |
|
377 |
void |
378 |
configuration::print () |
379 |
{ |
380 |
printf (_("\nConfiguration\n\n")); |
381 |
printf (_("# of nodes: %d\n"), nodes.size ()); |
382 |
printf (_("this node: %s\n"), thisnode ? thisnode->nodename : "<unset>"); |
383 |
printf (_("MTU: %d\n"), mtu); |
384 |
printf (_("rekeying interval: %d\n"), rekey); |
385 |
printf (_("keepalive interval: %d\n"), keepalive); |
386 |
printf (_("interface: %s\n"), ifname); |
387 |
printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
388 |
printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); |
389 |
printf ("\n"); |
390 |
|
391 |
printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
392 |
_("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
393 |
|
394 |
for (node_vector::iterator i = nodes.begin (); i != nodes.end (); ++i) |
395 |
(*i)->print (); |
396 |
|
397 |
printf ("\n"); |
398 |
} |
399 |
|
400 |
void |
401 |
conf_node::print () |
402 |
{ |
403 |
printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
404 |
id, |
405 |
id >> 8, id & 0xff, |
406 |
compress ? 'Y' : 'N', |
407 |
connectmode == C_ONDEMAND ? "ondemand" : |
408 |
connectmode == C_NEVER ? "never" : |
409 |
connectmode == C_ALWAYS ? "always" : "", |
410 |
nodename, |
411 |
hostname ? hostname : "", |
412 |
hostname ? ":" : "", |
413 |
hostname ? udp_port : 0 |
414 |
); |
415 |
} |
416 |
|