0a72a3711025484fad55817e19c2eebd603780ee
[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.4 2000/06/29 19:47:02 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_IP },
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   { NULL, 0, 0 }
75 };
76
77 /*
78   Add given value to the list of configs cfg
79 */
80 config_t *
81 add_config_val(config_t **cfg, int argtype, char *val)
82 {
83   config_t *p, *r;
84   char *q;
85 cp
86   p = (config_t*)xmalloc(sizeof(*p));
87   p->data.val = 0;
88   
89   switch(argtype)
90     {
91     case TYPE_INT:
92       p->data.val = strtol(val, &q, 0);
93       if(q && *q)
94         p->data.val = 0;
95       break;
96     case TYPE_NAME:
97       p->data.ptr = xmalloc(strlen(val) + 1);
98       strcpy(p->data.ptr, val);
99       break;
100     case TYPE_IP:
101       p->data.ip = strtoip(val);
102       break;
103     case TYPE_BOOL:
104       if(!strcasecmp("yes", val))
105         p->data.val = stupid_true;
106       else if(!strcasecmp("no", val))
107         p->data.val = stupid_false;
108       else
109         p->data.val = 0;
110     }
111
112   if(p->data.val)
113     {
114       if(*cfg)
115         {
116           r = *cfg;
117           while(r->next)
118             r = r->next;
119           r->next = p;
120         }
121       else
122         *cfg = p;
123       p->next = NULL;
124       return p;
125     }
126
127   free(p);
128 cp
129   return NULL;
130 }
131
132 /*
133   Get variable from a section in a configfile. returns -1 on failure.
134 */
135 int
136 readconfig(const char *fname, FILE *fp)
137 {
138   char *line, *temp_buf;
139   char *p, *q;
140   int i, lineno = 0;
141   config_t *cfg;
142 cp
143   line = (char *)xmalloc(80 * sizeof(char));
144   temp_buf = (char *)xmalloc(80 * sizeof(char));
145         
146   for(;;)
147     {
148       if(fgets(line, 80, fp) == NULL)
149         return 0;
150
151       while(!index(line, '\n'))
152         {
153           fgets(temp_buf, (strlen(line)+1) * 80, fp);
154           if(!temp_buf)
155             break;
156           strcat(line, temp_buf);
157           line = (char *)xrealloc(line, (strlen(line)+1) * sizeof(char));
158         }        
159       lineno++;
160
161       if((p = strtok(line, "\t\n\r =")) == NULL)
162         continue; /* no tokens on this line */
163
164       if(p[0] == '#')
165         continue; /* comment: ignore */
166
167       for(i = 0; hazahaza[i].name != NULL; i++)
168         if(!strcasecmp(hazahaza[i].name, p))
169           break;
170
171       if(!hazahaza[i].name)
172         {
173           fprintf(stderr, _("%s: %d: Invalid variable name `%s'.\n"),
174                   fname, lineno, p);
175           return -1;
176         }
177
178       if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
179         {
180           fprintf(stderr, _("%s: %d: No value given for `%s'.\n"),
181                   fname, lineno, hazahaza[i].name);
182           return -1;
183         }
184
185       cfg = add_config_val(&config, hazahaza[i].argtype, q);
186       if(cfg == NULL)
187         {
188           fprintf(stderr, _("%s: %d: Invalid value `%s' for variable `%s'.\n"),
189                   fname, lineno, q, hazahaza[i].name);
190           return -1;
191         }
192
193       cfg->which = hazahaza[i].which;
194       if(!config)
195         config = cfg;
196     }
197 cp
198 }
199
200 /*
201   wrapper function for readconfig
202 */
203 int
204 read_config_file(const char *fname)
205 {
206   FILE *fp;
207 cp
208   if((fp = fopen (fname, "r")) == NULL)
209     {
210       fprintf(stderr, _("Could not open %s: %s\n"), fname, sys_errlist[errno]);
211       return 1;
212     }
213
214   if(readconfig(fname, fp))
215     return -1;
216
217   fclose (fp);
218 cp
219   return 0;
220 }
221
222 /*
223   Look up the value of the config option type
224 */
225 const config_t *
226 get_config_val(which_t type)
227 {
228   config_t *p;
229 cp
230   for(p = config; p != NULL; p = p->next)
231     if(p->which == type)
232       return p;
233 cp
234   /* Not found */
235   return NULL;
236 }
237
238 /*
239   Support for multiple config lines.
240   Index is used to get a specific value, 0 being the first, 1 the second etc.
241 */
242 const config_t *
243 get_next_config_val(which_t type, int index)
244 {
245   config_t *p;
246 cp  
247   for(p = config; p != NULL; p = p->next)
248     if(p->which == type)
249       if(--index < 0)
250         return p;
251 cp  
252   /* Not found */
253   return NULL;
254 }
255
256 /*
257   Remove the complete configuration tree.
258 */
259 void clear_config()
260 {
261   config_t *p, *next;
262 cp
263   for(p = config; p; p = next)
264     {
265       next = p->next;
266       if(p->data.ptr)
267         free(p->data.ptr);
268       free(p);
269     }
270   config = NULL;
271 cp
272 }