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