- Fixed indirectdata=no problem
[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.2 2000/06/27 15:08:57 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
40 #include "system.h"
41
42 config_t *config;
43 int debug_lvl = 0;
44 int timeout = 0; /* seconds before timeout */
45
46 typedef struct internal_config_t {
47   char *name;
48   enum which_t which;
49   int argtype;
50 } internal_config_t;
51
52 /*
53   These are all the possible configurable values
54 */
55 static internal_config_t hazahaza[] = {
56   { "AllowConnect", allowconnect,   TYPE_BOOL },   /* Is not used anywhere. Remove? */
57   { "ConnectTo",    upstreamip,     TYPE_IP },
58   { "ConnectPort",  upstreamport,   TYPE_INT },
59   { "ListenPort",   listenport,     TYPE_INT },
60   { "MyOwnVPNIP",   myvpnip,        TYPE_IP },
61   { "MyVirtualIP",  myvpnip,        TYPE_IP },   /* an alias */
62   { "Passphrases",  passphrasesdir, TYPE_NAME },
63   { "PingTimeout",  pingtimeout,    TYPE_INT },
64   { "TapDevice",    tapdevice,      TYPE_NAME },
65   { "KeyExpire",    keyexpire,      TYPE_INT },
66   { "VpnMask",      vpnmask,        TYPE_IP },
67   { "Hostnames",    resolve_dns,    TYPE_BOOL },
68   { "IndirectData", indirectdata,   TYPE_BOOL },
69   { NULL, 0, 0 }
70 };
71
72 /*
73   Add given value to the list of configs cfg
74 */
75 config_t *
76 add_config_val(config_t **cfg, int argtype, char *val)
77 {
78   config_t *p;
79   char *q;
80
81   p = (config_t*)xmalloc(sizeof(*p));
82   p->data.val = 0;
83   
84   switch(argtype)
85     {
86     case TYPE_INT:
87       p->data.val = strtol(val, &q, 0);
88       if(q && *q)
89         p->data.val = 0;
90       break;
91     case TYPE_NAME:
92       p->data.ptr = xmalloc(strlen(val) + 1);
93       strcpy(p->data.ptr, val);
94       break;
95     case TYPE_IP:
96       p->data.ip = strtoip(val);
97       break;
98     case TYPE_BOOL:
99       if(!strcasecmp("yes", val))
100         p->data.val = stupid_true;
101       else if(!strcasecmp("no", val))
102         p->data.val = stupid_false;
103       else
104         p->data.val = 0;
105     }
106
107   if(p->data.val)
108     {
109       p->next = *cfg;
110       *cfg = p;
111       return p;
112     }
113
114   free(p);
115   return NULL;
116 }
117
118 /*
119   Get variable from a section in a configfile. returns -1 on failure.
120 */
121 int
122 readconfig(const char *fname, FILE *fp)
123 {
124   char *line, *temp_buf;
125   char *p, *q;
126   int i, lineno = 0;
127   config_t *cfg;
128
129   line = (char *)xmalloc(80 * sizeof(char));
130   temp_buf = (char *)xmalloc(80 * sizeof(char));
131         
132   for(;;)
133     {
134       if(fgets(line, 80, fp) == NULL)
135         return 0;
136
137       while(!index(line, '\n'))
138         {
139           fgets(temp_buf, (strlen(line)+1) * 80, fp);
140           if(!temp_buf)
141             break;
142           strcat(line, temp_buf);
143           line = (char *)xrealloc(line, (strlen(line)+1) * sizeof(char));
144         }        
145       lineno++;
146
147       if((p = strtok(line, "\t\n\r =")) == NULL)
148         continue; /* no tokens on this line */
149
150       if(p[0] == '#')
151         continue; /* comment: ignore */
152
153       for(i = 0; hazahaza[i].name != NULL; i++)
154         if(!strcasecmp(hazahaza[i].name, p))
155           break;
156
157       if(!hazahaza[i].name)
158         {
159           fprintf(stderr, _("%s: %d: Invalid variable name `%s'.\n"),
160                   fname, lineno, p);
161           return -1;
162         }
163
164       if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
165         {
166           fprintf(stderr, _("%s: %d: No value given for `%s'.\n"),
167                   fname, lineno, hazahaza[i].name);
168           return -1;
169         }
170
171       cfg = add_config_val(&config, hazahaza[i].argtype, q);
172       if(cfg == NULL)
173         {
174           fprintf(stderr, _("%s: %d: Invalid value `%s' for variable `%s'.\n"),
175                   fname, lineno, q, hazahaza[i].name);
176           return -1;
177         }
178
179       cfg->which = hazahaza[i].which;
180       if(!config)
181         config = cfg;
182     }
183 }
184
185 /*
186   wrapper function for readconfig
187 */
188 int
189 read_config_file(const char *fname)
190 {
191   FILE *fp;
192
193   if((fp = fopen (fname, "r")) == NULL)
194     {
195       fprintf(stderr, _("Could not open %s: %s\n"), fname, sys_errlist[errno]);
196       return 1;
197     }
198
199   if(readconfig(fname, fp))
200     return -1;
201
202   fclose (fp);
203
204   return 0;
205 }
206
207 /*
208   Look up the value of the config option type
209 */
210 const config_t *
211 get_config_val(which_t type)
212 {
213   config_t *p;
214
215   for(p = config; p != NULL; p = p->next)
216     if(p->which == type)
217       return p;
218
219   /* Not found */
220   return NULL;
221 }
222
223 /*
224   Support for multiple config lines.
225   Index is used to get a specific value, 0 being the first, 1 the second etc.
226 */
227 const config_t *
228 get_next_config_val(which_t type, int index)
229 {
230   config_t *p;
231   
232   for(p = config; p != NULL; p = p->next)
233     if(p->which == type)
234       if(--index < 0)
235         return p;
236   
237   /* Not found */
238   return NULL;
239 }