ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/conf.C
Revision: 1.21
Committed: Sat Jan 17 01:18:36 2004 UTC (20 years, 4 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: VPE_1_4
Changes since 1.20: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     conf.c -- configuration code
3 pcg 1.21 Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com>
4 pcg 1.1
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 pcg 1.17 #include "netcompat.h"
33 pcg 1.5
34 pcg 1.1 #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 pcg 1.7 u8 best_protocol (u8 protset)
53     {
54 pcg 1.13 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 pcg 1.7
59 pcg 1.9 return 0;
60 pcg 1.7 }
61    
62     const char *strprotocol (u8 protocol)
63     {
64 pcg 1.13 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 pcg 1.7
69     return "<unknown>";
70     }
71    
72 pcg 1.12 void
73     conf_node::print ()
74 pcg 1.1 {
75 pcg 1.12 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 pcg 1.1 }
88    
89 pcg 1.12 conf_node::~conf_node ()
90 pcg 1.1 {
91 pcg 1.12 if (rsa_key)
92     RSA_free (rsa_key);
93    
94     free (nodename);
95     free (hostname);
96 pcg 1.1 }
97    
98     void configuration::init ()
99     {
100     memset (this, 0, sizeof (*this));
101    
102 pcg 1.19 mtu = DEFAULT_MTU;
103 pcg 1.1 rekey = DEFAULT_REKEY;
104     keepalive = DEFAULT_KEEPALIVE;
105 pcg 1.2 llevel = L_INFO;
106 pcg 1.5 ip_proto = IPPROTO_GRE;
107 pcg 1.16 #if ENABLE_ICMP
108 pcg 1.13 icmp_type = ICMP_ECHOREPLY;
109 pcg 1.16 #endif
110 pcg 1.1
111 pcg 1.5 default_node.udp_port = DEFAULT_UDPPORT;
112 pcg 1.8 default_node.tcp_port = DEFAULT_UDPPORT;
113 pcg 1.1 default_node.connectmode = conf_node::C_ALWAYS;
114     default_node.compress = true;
115 pcg 1.6 default_node.protocols = PROT_UDPv4;
116 pcg 1.1 }
117    
118     void configuration::cleanup()
119     {
120     if (rsa_key)
121     RSA_free (rsa_key);
122    
123 pcg 1.12 rsa_key = 0;
124 pcg 1.1
125 pcg 1.12 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 pcg 1.1 }
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 pcg 1.5 #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 pcg 1.1 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 pcg 1.2 llevel = l;
225 pcg 1.1 else
226     slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line);
227     }
228 pcg 1.5 else if (!strcmp (var, "ip-proto"))
229     ip_proto = atoi (val);
230 pcg 1.20 else if (!strcmp (var, "icmp-type"))
231     {
232 pcg 1.16 #if ENABLE_ICMP
233 pcg 1.20 icmp_type = atoi (val);
234 pcg 1.16 #endif
235 pcg 1.20 }
236 pcg 1.1
237     // per config
238     else if (!strcmp (var, "node"))
239     {
240     default_node.id++;
241    
242     node = new conf_node (default_node);
243    
244     nodes.push_back (node);
245    
246     node->nodename = strdup (val);
247    
248     {
249     char *fname;
250     FILE *f;
251    
252     asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename);
253    
254     f = fopen (fname, "r");
255     if (f)
256     {
257     node->rsa_key = RSA_new ();
258    
259     if (!PEM_read_RSAPublicKey(f, &node->rsa_key, NULL, NULL))
260     {
261     ERR_load_RSA_strings (); ERR_load_PEM_strings ();
262     slog (L_ERR, _("unable to open public rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
263     exit (1);
264     }
265    
266     RSA_blinding_on (node->rsa_key, 0);
267    
268     fclose (f);
269     }
270     else
271     {
272     slog (need_keys ? L_ERR : L_NOTICE, _("unable to read public rsa key file '%s': %s"), fname, strerror (errno));
273    
274     if (need_keys)
275     exit (1);
276     }
277    
278     free (fname);
279     }
280    
281     if (!::thisnode || !strcmp (node->nodename, ::thisnode))
282     thisnode = node;
283     }
284     else if (!strcmp (var, "private-key"))
285     prikeyfile = strdup (val);
286     else if (!strcmp (var, "ifpersist"))
287     {
288 pcg 1.5 parse_bool (ifpersist, "ifpersist", true, false);
289 pcg 1.1 }
290     else if (!strcmp (var, "ifname"))
291     ifname = strdup (val);
292     else if (!strcmp (var, "rekey"))
293     rekey = atoi (val);
294     else if (!strcmp (var, "keepalive"))
295     keepalive = atoi (val);
296     else if (!strcmp (var, "mtu"))
297     mtu = atoi (val);
298     else if (!strcmp (var, "if-up"))
299     script_if_up = strdup (val);
300     else if (!strcmp (var, "node-up"))
301     script_node_up = strdup (val);
302     else if (!strcmp (var, "node-down"))
303     script_node_down = strdup (val);
304 pcg 1.20 else if (!strcmp (var, "http-proxy-host"))
305     {
306 pcg 1.12 #if ENABLE_HTTP_PROXY
307 pcg 1.20 proxy_host = strdup (val);
308     #endif
309     }
310 pcg 1.12 else if (!strcmp (var, "http-proxy-port"))
311 pcg 1.20 {
312     #if ENABLE_HTTP_PROXY
313     proxy_port = atoi (val);
314     #endif
315     }
316 pcg 1.12 else if (!strcmp (var, "http-proxy-auth"))
317 pcg 1.20 {
318     #if ENABLE_HTTP_PROXY
319     proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val));
320 pcg 1.12 #endif
321 pcg 1.20 }
322 pcg 1.1
323     /* node-specific, non-defaultable */
324     else if (node != &default_node && !strcmp (var, "hostname"))
325     {
326     free (node->hostname);
327     node->hostname = strdup (val);
328     }
329    
330     /* node-specific, defaultable */
331 pcg 1.4 else if (!strcmp (var, "udp-port"))
332 pcg 1.5 node->udp_port = atoi (val);
333 pcg 1.8 else if (!strcmp (var, "tcp-port"))
334     node->tcp_port = atoi (val);
335 pcg 1.1 else if (!strcmp (var, "router-priority"))
336     node->routerprio = atoi (val);
337     else if (!strcmp (var, "connect"))
338     {
339     if (!strcmp (val, "ondemand"))
340     node->connectmode = conf_node::C_ONDEMAND;
341     else if (!strcmp (val, "never"))
342     node->connectmode = conf_node::C_NEVER;
343     else if (!strcmp (val, "always"))
344     node->connectmode = conf_node::C_ALWAYS;
345 pcg 1.4 else if (!strcmp (val, "disabled"))
346     node->connectmode = conf_node::C_DISABLED;
347 pcg 1.1 else
348     slog (L_WARN,
349 pcg 1.4 _("illegal value for 'connectmode', use one of 'ondemand', 'never', 'always' or 'disabled', at '%s' line %d"),
350 pcg 1.1 var, fname, lineno);
351     }
352 pcg 1.3 else if (!strcmp (var, "inherit-tos"))
353     {
354 pcg 1.5 parse_bool (node->inherit_tos, "inherit-tos", true, false);
355 pcg 1.3 }
356 pcg 1.1 else if (!strcmp (var, "compress"))
357     {
358 pcg 1.5 parse_bool (node->compress, "compress", true, false);
359     }
360     // all these bool options really really cost a lot of executable size!
361 pcg 1.8 else if (!strcmp (var, "enable-tcp"))
362     {
363 pcg 1.11 #if ENABLE_TCP
364 pcg 1.8 u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v;
365 pcg 1.13 #endif
366     }
367     else if (!strcmp (var, "enable-icmp"))
368     {
369     #if ENABLE_ICMP
370     u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v;
371 pcg 1.11 #endif
372 pcg 1.8 }
373 pcg 1.6 else if (!strcmp (var, "enable-udp"))
374 pcg 1.5 {
375 pcg 1.6 u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v;
376 pcg 1.5 }
377 pcg 1.6 else if (!strcmp (var, "enable-rawip"))
378 pcg 1.5 {
379 pcg 1.6 u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v;
380 pcg 1.1 }
381    
382     // unknown or misplaced
383     else
384 pcg 1.12 slog (L_WARN,
385     _("unknown or misplaced variable `%s', at '%s' line %d"),
386     var, fname, lineno);
387 pcg 1.1 }
388    
389     fclose (f);
390     }
391     else
392     {
393     slog (L_ERR, _("unable to read config file '%s': %s"), fname, strerror (errno));
394     exit (1);
395     }
396    
397     free (fname);
398    
399     fname = config_filename (prikeyfile, "hostkey");
400    
401     f = fopen (fname, "r");
402     if (f)
403     {
404     rsa_key = RSA_new ();
405    
406     if (!PEM_read_RSAPrivateKey (f, &rsa_key, NULL, NULL))
407     {
408     ERR_load_RSA_strings (); ERR_load_PEM_strings ();
409     slog (L_ERR, _("unable to read private rsa key file '%s': %s"), fname, ERR_error_string (ERR_get_error (), 0));
410     exit (1);
411     }
412    
413     RSA_blinding_on (rsa_key, 0);
414    
415     fclose (f);
416     }
417     else
418     {
419     slog (need_keys ? L_ERR : L_NOTICE, _("unable to open private rsa key file '%s': %s"), fname, strerror (errno));
420    
421     if (need_keys)
422     exit (1);
423     }
424    
425     free (fname);
426     }
427    
428     char *configuration::config_filename (const char *name, const char *dflt)
429     {
430     char *fname;
431    
432     asprintf (&fname, name ? name : dflt, ::thisnode);
433    
434     if (!ABSOLUTE_PATH (fname))
435     {
436     char *rname = fname;
437     asprintf (&fname, "%s/%s", confbase, rname);
438     free (rname);
439     }
440    
441     return fname;
442     }
443    
444     void
445     configuration::print ()
446     {
447     printf (_("\nConfiguration\n\n"));
448     printf (_("# of nodes: %d\n"), nodes.size ());
449     printf (_("this node: %s\n"), thisnode ? thisnode->nodename : "<unset>");
450     printf (_("MTU: %d\n"), mtu);
451     printf (_("rekeying interval: %d\n"), rekey);
452     printf (_("keepalive interval: %d\n"), keepalive);
453     printf (_("interface: %s\n"), ifname);
454     printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>");
455 pcg 1.15 printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1);
456 pcg 1.1 printf ("\n");
457    
458     printf ("%4s %-17s %s %-8.8s %-10.10s %s\n",
459     _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port"));
460    
461     for (node_vector::iterator i = nodes.begin (); i != nodes.end (); ++i)
462     (*i)->print ();
463    
464     printf ("\n");
465     }
466    
467 pcg 1.12 configuration::configuration ()
468     {
469     init ();
470     }
471    
472     configuration::~configuration ()
473 pcg 1.1 {
474 pcg 1.12 cleanup ();
475 pcg 1.1 }
476 pcg 1.12
477 pcg 1.1