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