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