ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/conf.C
Revision: 1.15
Committed: Sun Apr 13 00:35:46 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: VPE_0_9, VPE_1_0
Changes since 1.14: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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