37bfe9469639d3fd423ff57566ec97e2ccc96e99
[oweals/tinc.git] / src / conf.c
1 /*
2     conf.c -- configuration code
3     Copyright (C) 1998 Robert van der Meulen
4     Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
5                             2000 Guus Sliepen <guus@sliepen.warande.net>
6                             2000 Cris van Pelt <tribbel@arise.dhs.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22     $Id: conf.c,v 1.9.4.23 2000/11/28 23:12:56 zarq Exp $
23 */
24
25 #include "config.h"
26
27 #include <ctype.h>
28 #include <errno.h>
29 #include <netdb.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <syslog.h>
34
35 #include <xalloc.h>
36 #include <utils.h> /* for cp */
37
38 #include "conf.h"
39 #include "netutl.h" /* for strtoip */
40
41 #include "config.h"
42 #include "system.h"
43
44 config_t *config = NULL;
45 int debug_lvl = 0;
46 int timeout = 0; /* seconds before timeout */
47 char *confbase = NULL;           /* directory in which all config files are */
48 char *netname = NULL;            /* name of the vpn network */
49
50 /* Will be set if HUP signal is received. It will be processed when it is safe. */
51 int sighup = 0;
52
53 /*
54   These are all the possible configurable values
55 */
56 static internal_config_t hazahaza[] = {
57 /* Main configuration file keywords */
58   { "Name",         config_name,       TYPE_NAME },
59   { "ConnectTo",    config_connectto,      TYPE_NAME },
60   { "PingTimeout",  config_pingtimeout,    TYPE_INT },
61   { "TapDevice",    config_tapdevice,      TYPE_NAME },
62   { "PrivateKey",   config_privatekey,     TYPE_NAME },
63   { "KeyExpire",    config_keyexpire,      TYPE_INT },
64   { "Hostnames",    config_hostnames,    TYPE_BOOL },
65   { "Interface",    config_interface,      TYPE_NAME },
66   { "InterfaceIP",  config_interfaceip,    TYPE_IP },
67 /* Host configuration file keywords */
68   { "Address",      config_address,        TYPE_NAME },
69   { "Port",         config_port,           TYPE_INT },
70   { "PublicKey",    config_publickey,      TYPE_NAME },
71   { "Subnet",       config_subnet,         TYPE_IP },           /* Use IPv4 subnets only for now */
72   { "RestrictHosts", config_restricthosts, TYPE_BOOL },
73   { "RestrictSubnets", config_restrictsubnets, TYPE_BOOL },
74   { "RestrictAddress", config_restrictaddress, TYPE_BOOL },
75   { "RestrictPort", config_restrictport,   TYPE_BOOL },
76   { "IndirectData", config_indirectdata,   TYPE_BOOL },
77   { "TCPonly",      config_tcponly,        TYPE_BOOL },
78   { NULL, 0, 0 }
79 };
80
81 /*
82   Add given value to the list of configs cfg
83 */
84 config_t *
85 add_config_val(config_t **cfg, int argtype, char *val)
86 {
87   config_t *p;
88   char *q;
89 cp
90   p = (config_t*)xmalloc(sizeof(*p));
91   p->data.val = 0;
92
93   switch(argtype)
94     {
95     case TYPE_INT:
96       p->data.val = strtol(val, &q, 0);
97       if(q && *q)
98         p->data.val = 0;
99       break;
100     case TYPE_NAME:
101       p->data.ptr = xmalloc(strlen(val) + 1);
102       strcpy(p->data.ptr, val);
103       break;
104     case TYPE_IP:
105       p->data.ip = strtoip(val);
106       break;
107     case TYPE_BOOL:
108       if(!strcasecmp("yes", val))
109         p->data.val = stupid_true;
110       else if(!strcasecmp("no", val))
111         p->data.val = stupid_false;
112       else
113         p->data.val = 0;
114     }
115
116   p->argtype = argtype;
117
118   if(p->data.val)
119     {
120       p->next = *cfg;
121       *cfg = p;
122 cp
123       return p;
124     }
125   else
126     {
127       free(p);
128 cp
129       return NULL;
130     }
131 }
132
133 /*
134   Parse a configuration file and put the results in the configuration tree
135   starting at *base.
136 */
137 int read_config_file(config_t **base, const char *fname)
138 {
139   int err = -1;
140   FILE *fp;
141   char line[MAXBUFSIZE];        /* There really should not be any line longer than this... */
142   char *p, *q;
143   int i, lineno = 0;
144   config_t *cfg;
145 cp
146   if((fp = fopen (fname, "r")) == NULL)
147     {
148       return -1;
149     }
150
151   for(;;)
152     {
153       if(fgets(line, MAXBUFSIZE, fp) == NULL)
154         {
155           err = 0;
156           break;
157         }
158         
159       lineno++;
160
161       if(!index(line, '\n'))
162         {
163           syslog(LOG_ERR, _("Line %d too long while reading config file %s"), lineno, fname);
164           break;
165         }        
166
167       if((p = strtok(line, "\t\n\r =")) == NULL)
168         continue; /* no tokens on this line */
169
170       if(p[0] == '#')
171         continue; /* comment: ignore */
172
173       for(i = 0; hazahaza[i].name != NULL; i++)
174         if(!strcasecmp(hazahaza[i].name, p))
175           break;
176
177       if(!hazahaza[i].name)
178         {
179           syslog(LOG_ERR, _("Invalid variable name on line %d while reading config file %s"),
180                   lineno, fname);
181           break;
182         }
183
184       if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
185         {
186           fprintf(stderr, _("No value for variable on line %d while reading config file %s"),
187                   lineno, fname);
188           break;
189         }
190
191       cfg = add_config_val(base, hazahaza[i].argtype, q);
192       if(cfg == NULL)
193         {
194           fprintf(stderr, _("Invalid value for variable on line %d while reading config file %s"),
195                   lineno, fname);
196           break;
197         }
198
199       cfg->which = hazahaza[i].which;
200       if(!config)
201         config = cfg;
202     }
203
204   fclose (fp);
205 cp
206   return err;
207 }
208
209 int read_server_config()
210 {
211   char *fname;
212   int x;
213 cp
214   asprintf(&fname, "%s/tinc.conf", confbase);
215   x = read_config_file(&config, fname);
216   if(x != 0)
217     {
218       fprintf(stderr, _("Failed to read `%s': %m\n"),
219               fname);
220     }
221   free(fname);
222 cp
223   return x;  
224 }
225
226 /*
227   Look up the value of the config option type
228 */
229 const config_t *get_config_val(config_t *p, which_t type)
230 {
231 cp
232   for(; p != NULL; p = p->next)
233     if(p->which == type)
234       break;
235 cp
236   return p;
237 }
238
239 /*
240   Remove the complete configuration tree.
241 */
242 void clear_config(config_t **base)
243 {
244   config_t *p, *next;
245 cp
246   for(p = *base; p != NULL; p = next)
247     {
248       next = p->next;
249       if(p->data.ptr && (p->argtype == TYPE_NAME))
250         {
251           free(p->data.ptr);
252         }
253       free(p);
254     }
255   *base = NULL;
256 cp
257 }
258
259 #define is_safe_file(p) 1
260
261 FILE *ask_and_safe_open(const char* filename)
262 {
263   FILE *r;
264   char *directory;
265   char *fn;
266   int len;
267
268   if(!isatty(0))
269     {
270       /* Argh, they are running us from a script or something.  Write
271          the files to the current directory and let them burn in hell
272          for ever. */
273       directory = "."; /* get_current_directory */
274     }
275   else
276     {
277       directory = ".";
278     }
279
280   len = strlen(filename) + strlen(directory) + 2; /* 1 for the / */
281   fn = xmalloc(len);
282   snprintf(fn, len, "%s/%s", directory, filename);
283
284   if(!is_safe_file(fn))
285     {
286       fprintf(stderr, _("The file `%s' (or any of the leading directories) has unsafe permissions.\n"
287                         "I will not create or overwrite this file.\n"),
288                         fn);
289       return NULL;
290     }
291
292   if((r = fopen(fn, "w")) == NULL)
293     {
294       fprintf(stderr, _("Error opening file `%s': %m"),
295               fn);
296     }
297
298   free(fn);
299   
300   return r;
301 }