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