- Added experimental hackish tunneling-over-TCP support.
[tinc] / src / conf.c
1 /*
2     conf.c -- configuration code
3     Copyright (C) 1998 Emphyrio,
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.7 2000/08/07 14:52:14 guus Exp $
23 */
24
25
26 #include "config.h"
27
28 #include <ctype.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <xalloc.h>
36
37 #include "conf.h"
38 #include "netutl.h" /* for strtoip */
39 #include <utils.h> /* for cp */
40
41 #include "system.h"
42
43 config_t *config;
44 int debug_lvl = 0;
45 int timeout = 0; /* seconds before timeout */
46 char *configfilename = NULL;
47
48 /* Will be set if HUP signal is received. It will be processed when it is safe. */
49 int sighup = 0;
50
51 typedef struct internal_config_t {
52   char *name;
53   enum which_t which;
54   int argtype;
55 } internal_config_t;
56
57 /*
58   These are all the possible configurable values
59 */
60 static internal_config_t hazahaza[] = {
61   { "AllowConnect", allowconnect,   TYPE_BOOL },   /* Is not used anywhere. Remove? */
62   { "ConnectTo",    upstreamip,     TYPE_NAME },
63   { "ConnectPort",  upstreamport,   TYPE_INT },
64   { "ListenPort",   listenport,     TYPE_INT },
65   { "MyOwnVPNIP",   myvpnip,        TYPE_IP },
66   { "MyVirtualIP",  myvpnip,        TYPE_IP },   /* an alias */
67   { "Passphrases",  passphrasesdir, TYPE_NAME },
68   { "PingTimeout",  pingtimeout,    TYPE_INT },
69   { "TapDevice",    tapdevice,      TYPE_NAME },
70   { "KeyExpire",    keyexpire,      TYPE_INT },
71   { "VpnMask",      vpnmask,        TYPE_IP },
72   { "Hostnames",    resolve_dns,    TYPE_BOOL },
73   { "IndirectData", indirectdata,   TYPE_BOOL },
74   { "TCPonly",      tcponly,        TYPE_BOOL },
75   { NULL, 0, 0 }
76 };
77
78 /*
79   Add given value to the list of configs cfg
80 */
81 config_t *
82 add_config_val(config_t **cfg, int argtype, char *val)
83 {
84   config_t *p, *r;
85   char *q;
86 cp
87   p = (config_t*)xmalloc(sizeof(*p));
88   p->data.val = 0;
89   
90   switch(argtype)
91     {
92     case TYPE_INT:
93       p->data.val = strtol(val, &q, 0);
94       if(q && *q)
95         p->data.val = 0;
96       break;
97     case TYPE_NAME:
98       p->data.ptr = xmalloc(strlen(val) + 1);
99       strcpy(p->data.ptr, val);
100       break;
101     case TYPE_IP:
102       p->data.ip = strtoip(val);
103       break;
104     case TYPE_BOOL:
105       if(!strcasecmp("yes", val))
106         p->data.val = stupid_true;
107       else if(!strcasecmp("no", val))
108         p->data.val = stupid_false;
109       else
110         p->data.val = 0;
111     }
112
113   p->argtype = argtype;
114
115   if(p->data.val)
116     {
117       if(*cfg)
118         {
119           r = *cfg;
120           while(r->next)
121             r = r->next;
122           r->next = p;
123         }
124       else
125         *cfg = p;
126       p->next = NULL;
127       return p;
128     }
129
130   free(p);
131 cp
132   return NULL;
133 }
134
135 /*
136   Get variable from a section in a configfile. returns -1 on failure.
137 */
138 int
139 readconfig(const char *fname, FILE *fp)
140 {
141   char *line, *temp_buf;
142   char *p, *q;
143   int i, lineno = 0;
144   config_t *cfg;
145 cp
146   line = (char *)xmalloc(80 * sizeof(char));
147   temp_buf = (char *)xmalloc(80 * sizeof(char));
148         
149   for(;;)
150     {
151       if(fgets(line, 80, fp) == NULL)
152         return 0;
153
154       while(!index(line, '\n'))
155         {
156           fgets(temp_buf, (strlen(line)+1) * 80, fp);
157           if(!temp_buf)
158             break;
159           strcat(line, temp_buf);
160           line = (char *)xrealloc(line, (strlen(line)+1) * sizeof(char));
161         }        
162       lineno++;
163
164       if((p = strtok(line, "\t\n\r =")) == NULL)
165         continue; /* no tokens on this line */
166
167       if(p[0] == '#')
168         continue; /* comment: ignore */
169
170       for(i = 0; hazahaza[i].name != NULL; i++)
171         if(!strcasecmp(hazahaza[i].name, p))
172           break;
173
174       if(!hazahaza[i].name)
175         {
176           fprintf(stderr, _("%s: %d: Invalid variable name `%s'.\n"),
177                   fname, lineno, p);
178           return -1;
179         }
180
181       if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
182         {
183           fprintf(stderr, _("%s: %d: No value given for `%s'.\n"),
184                   fname, lineno, hazahaza[i].name);
185           return -1;
186         }
187
188       cfg = add_config_val(&config, hazahaza[i].argtype, q);
189       if(cfg == NULL)
190         {
191           fprintf(stderr, _("%s: %d: Invalid value `%s' for variable `%s'.\n"),
192                   fname, lineno, q, hazahaza[i].name);
193           return -1;
194         }
195
196       cfg->which = hazahaza[i].which;
197       if(!config)
198         config = cfg;
199     }
200 cp
201   return 0;
202 }
203
204 /*
205   wrapper function for readconfig
206 */
207 int
208 read_config_file(const char *fname)
209 {
210   int err;
211   FILE *fp;
212 cp
213   if((fp = fopen (fname, "r")) == NULL)
214     {
215       fprintf(stderr, _("Could not open %s: %s\n"), fname, sys_errlist[errno]);
216       return 1;
217     }
218
219   err = readconfig(fname, fp);
220   fclose (fp);
221 cp
222   return err;
223 }
224
225 /*
226   Look up the value of the config option type
227 */
228 const config_t *
229 get_config_val(which_t type)
230 {
231   config_t *p;
232 cp
233   for(p = config; p != NULL; p = p->next)
234     if(p->which == type)
235       return p;
236 cp
237   /* Not found */
238   return NULL;
239 }
240
241 /*
242   Support for multiple config lines.
243   Index is used to get a specific value, 0 being the first, 1 the second etc.
244 */
245 const config_t *
246 get_next_config_val(which_t type, int index)
247 {
248   config_t *p;
249 cp  
250   for(p = config; p != NULL; p = p->next)
251     if(p->which == type)
252       if(--index < 0)
253         return p;
254 cp  
255   /* Not found */
256   return NULL;
257 }
258
259 /*
260   Remove the complete configuration tree.
261 */
262 void clear_config()
263 {
264   config_t *p, *next;
265 cp
266   for(p = config; p != NULL; p = next)
267     {
268       next = p->next;
269       if(p->data.ptr && (p->argtype == TYPE_NAME))
270         {
271           free(p->data.ptr);
272         }
273       free(p);
274     }
275   config = NULL;
276 cp
277 }