New directive: Name.
[oweals/tinc.git] / 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.10 2000/09/14 14:34:38 zarq 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   { "Interface",    interface,      TYPE_NAME },
76   { "InterfaceIP",  interfaceip,    TYPE_IP },
77   { "Name",         tincname,       TYPE_NAME },
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, *r;
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       if(*cfg)
121         {
122           r = *cfg;
123           while(r->next)
124             r = r->next;
125           r->next = p;
126         }
127       else
128         *cfg = p;
129       p->next = NULL;
130       return p;
131     }
132
133   free(p);
134 cp
135   return NULL;
136 }
137
138 /*
139   Get variable from a section in a configfile. returns -1 on failure.
140 */
141 int
142 readconfig(const char *fname, FILE *fp)
143 {
144   char *line, *temp_buf;
145   char *p, *q;
146   int i, lineno = 0;
147   config_t *cfg;
148 cp
149   line = (char *)xmalloc(80 * sizeof(char));
150   temp_buf = (char *)xmalloc(80 * sizeof(char));
151         
152   for(;;)
153     {
154       if(fgets(line, 80, fp) == NULL)
155         return 0;
156
157       while(!index(line, '\n'))
158         {
159           fgets(temp_buf, (strlen(line)+1) * 80, fp);
160           if(!temp_buf)
161             break;
162           strcat(line, temp_buf);
163           line = (char *)xrealloc(line, (strlen(line)+1) * sizeof(char));
164         }        
165       lineno++;
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           fprintf(stderr, _("%s: %d: Invalid variable name `%s'.\n"),
180                   fname, lineno, p);
181           return -1;
182         }
183
184       if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
185         {
186           fprintf(stderr, _("%s: %d: No value given for `%s'.\n"),
187                   fname, lineno, hazahaza[i].name);
188           return -1;
189         }
190
191       cfg = add_config_val(&config, hazahaza[i].argtype, q);
192       if(cfg == NULL)
193         {
194           fprintf(stderr, _("%s: %d: Invalid value `%s' for variable `%s'.\n"),
195                   fname, lineno, q, hazahaza[i].name);
196           return -1;
197         }
198
199       cfg->which = hazahaza[i].which;
200       if(!config)
201         config = cfg;
202     }
203 cp
204   return 0;
205 }
206
207 /*
208   wrapper function for readconfig
209 */
210 int
211 read_config_file(const char *fname)
212 {
213   int err;
214   FILE *fp;
215 cp
216   if((fp = fopen (fname, "r")) == NULL)
217     {
218       fprintf(stderr, _("Could not open %s: %s\n"), fname, strerror(errno));
219       return 1;
220     }
221
222   err = readconfig(fname, fp);
223   fclose (fp);
224 cp
225   return err;
226 }
227
228 /*
229   Look up the value of the config option type
230 */
231 const config_t *
232 get_config_val(which_t type)
233 {
234   config_t *p;
235 cp
236   for(p = config; p != NULL; p = p->next)
237     if(p->which == type)
238       return p;
239 cp
240   /* Not found */
241   return NULL;
242 }
243
244 /*
245   Support for multiple config lines.
246   Index is used to get a specific value, 0 being the first, 1 the second etc.
247 */
248 const config_t *
249 get_next_config_val(which_t type, int index)
250 {
251   config_t *p;
252 cp  
253   for(p = config; p != NULL; p = p->next)
254     if(p->which == type)
255       if(--index < 0)
256         return p;
257 cp  
258   /* Not found */
259   return NULL;
260 }
261
262 /*
263   Remove the complete configuration tree.
264 */
265 void clear_config()
266 {
267   config_t *p, *next;
268 cp
269   for(p = config; p != NULL; p = next)
270     {
271       next = p->next;
272       if(p->data.ptr && (p->argtype == TYPE_NAME))
273         {
274           free(p->data.ptr);
275         }
276       free(p);
277     }
278   config = NULL;
279 cp
280 }