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