ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/conf.C
Revision: 1.18
Committed: Thu Oct 16 02:41:21 2003 UTC (20 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.17: +1 -5 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 conf.c -- configuration code
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "config.h"
21
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cstring>
25
26 #include <errno.h>
27 #include <netdb.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include "netcompat.h"
33
34 #include <openssl/err.h>
35 #include <openssl/pem.h>
36 #include <openssl/rsa.h>
37 #include <openssl/rand.h>
38
39 #include "gettext.h"
40
41 #include "conf.h"
42 #include "slog.h"
43 #include "util.h"
44
45 char *confbase;
46 char *thisnode;
47 char *identname;
48 char *pidfilename;
49
50 struct configuration conf;
51
52 u8 best_protocol (u8 protset)
53 {
54 if (protset & PROT_IPv4 ) return PROT_IPv4;
55 if (protset & PROT_ICMPv4) return PROT_ICMPv4;
56 if (protset & PROT_UDPv4 ) return PROT_UDPv4;
57 if (protset & PROT_TCPv4 ) return PROT_TCPv4;
58
59 return 0;
60 }
61
62 const char *strprotocol (u8 protocol)
63 {
64 if (protocol & PROT_IPv4 ) return "rawip";
65 if (protocol & PROT_ICMPv4) return "icmp";
66 if (protocol & PROT_UDPv4 ) return "udp";
67 if (protocol & PROT_TCPv4 ) return "tcp";
68
69 return "<unknown>";
70 }
71
72 void
73 conf_node::print ()
74 {
75 printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n",
76 id,
77 id >> 8, id & 0xff,
78 compress ? 'Y' : 'N',
79 connectmode == C_ONDEMAND ? "ondemand" :
80 connectmode == C_NEVER ? "never" :
81 connectmode == C_ALWAYS ? "always" : "",
82 nodename,
83 hostname ? hostname : "",
84 hostname ? ":" : "",
85 hostname ? udp_port : 0
86 );
87 }
88
89 conf_node::~conf_node ()
90 {
91 if (rsa_key)
92 RSA_free (rsa_key);
93
94 free (nodename);
95 free (hostname);
96 }
97
98 void configuration::init ()
99 {
100 memset (this, 0, sizeof (*this));
101
102 rekey = DEFAULT_REKEY;
103 keepalive = DEFAULT_KEEPALIVE;
104 llevel = L_INFO;
105 ip_proto = IPPROTO_GRE;
106 #if ENABLE_ICMP
107 icmp_type = ICMP_ECHOREPLY;
108 #endif
109
110 default_node.udp_port = DEFAULT_UDPPORT;
111 default_node.tcp_port = DEFAULT_UDPPORT;
112 default_node.connectmode = conf_node::C_ALWAYS;
113 default_node.compress = true;
114 default_node.protocols = PROT_UDPv4;
115 }
116
117 void configuration::cleanup()
118 {
119 if (rsa_key)
120 RSA_free (rsa_key);
121
122 rsa_key = 0;
123
124 free (ifname); ifname = 0;
125 #if ENABLE_HTTP_PROXY
126 free (proxy_host); proxy_host = 0;
127 free (proxy_auth); proxy_auth = 0;
128 #endif
129 }
130
131 void
132 configuration::clear_config ()
133 {
134 for (configuration::node_vector::iterator i = nodes.begin(); i != nodes.end(); ++i)
135 delete *i;
136
137 nodes.clear ();
138
139 cleanup ();
140 init ();
141 }
142
143 #define parse_bool(target,name,trueval,falseval) \
144 if (!strcmp (val, "yes")) target = trueval; \
145 else if (!strcmp (val, "no")) target = falseval; \
146 else if (!strcmp (val, "true")) target = trueval; \
147 else if (!strcmp (val, "false")) target = falseval; \
148 else if (!strcmp (val, "on")) target = trueval; \
149 else if (!strcmp (val, "off")) target = falseval; \
150 else \
151 slog (L_WARN, \
152 _("illegal value for '%s', only 'yes|true|on' or 'no|false|off' allowed, at '%s' line %d"), \
153 name, var, fname, lineno);
154
155 void configuration::read_config (bool need_keys)
156 {
157 char *fname;
158 FILE *f;
159
160 clear_config ();
161
162 asprintf (&fname, "%s/vped.conf", confbase);
163 f = fopen (fname, "r");
164
165 if (f)
166 {
167 char line[16384];
168 int lineno = 0;
169 char *var, *val;
170 conf_node *node = &default_node;
171
172 while (fgets (line, sizeof (line), f))
173 {
174 lineno++;
175
176 {
177 char *end = line + strlen (line);
178
179 while (*end < ' ' && end >= line)
180 end--;
181
182 *++end = 0;
183 }
184
185 char *tok = line;
186
187 retry:
188 var = strtok (tok, "\t =");
189 tok = 0;
190
191 if (!var || !var[0])
192 continue; /* no tokens on this line */
193
194 if (var[0] == '#')
195 continue; /* comment: ignore */
196
197 val = strtok (NULL, "\t\n\r =");
198
199 if (!val || val[0] == '#')
200 {
201 slog (L_WARN,
202 _("no value for variable `%s', at '%s' line %d"),
203 var, fname, lineno);
204 break;
205 }
206
207 if (!strcmp (var, "on"))
208 {
209 if (!::thisnode
210 || (val[0] == '!' && strcmp (val + 1, ::thisnode))
211 || !strcmp (val, ::thisnode))
212 goto retry;
213
214 continue;
215 }
216
217 // truly global
218 if (!strcmp (var, "loglevel"))
219 {
220 loglevel l = string_to_loglevel (val);
221
222 if (l != L_NONE)
223 llevel = l;
224 else
225 slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line);
226 }
227 else if (!strcmp (var, "ip-proto"))
228 ip_proto = atoi (val);
229 #if ENABLE_ICMP
230 //TODO: error message
231 else if (!strcmp (var, "icmp-type"))
232 icmp_type = atoi (val);
233 #endif
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