35ca4d78160da58db39a8c35c1f4e7bb9c11b765
[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 2000/05/30 11:18:12 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
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   { NULL, 0, 0 }
69 };
70
71 /*
72   Add given value to the list of configs cfg
73 */
74 config_t *
75 add_config_val(config_t **cfg, int argtype, char *val)
76 {
77   config_t *p;
78   char *q;
79
80   p = (config_t*)xmalloc(sizeof(*p));
81   p->data.val = 0;
82   
83   switch(argtype)
84     {
85     case TYPE_INT:
86       p->data.val = strtol(val, &q, 0);
87       if(q && *q)
88         p->data.val = 0;
89       break;
90     case TYPE_NAME:
91       p->data.ptr = xmalloc(strlen(val) + 1);
92       strcpy(p->data.ptr, val);
93       break;
94     case TYPE_IP:
95       p->data.ip = strtoip(val);
96       break;
97     case TYPE_BOOL:
98       if(!strcasecmp("yes", val))
99         p->data.val = stupid_true;
100       else if(!strcasecmp("no", val))
101         p->data.val = stupid_false;
102       else
103         p->data.val = 0;
104     }
105
106   if(p->data.val)
107     {
108       p->next = *cfg;
109       *cfg = p;
110       return p;
111     }
112
113   free(p);
114   return NULL;
115 }
116
117 /*
118   Get variable from a section in a configfile. returns -1 on failure.
119 */
120 int
121 readconfig(const char *fname, FILE *fp)
122 {
123   char *line, *temp_buf;
124   char *p, *q;
125   int i, lineno = 0;
126   config_t *cfg;
127
128   line = (char *)xmalloc(80 * sizeof(char));
129   temp_buf = (char *)xmalloc(80 * sizeof(char));
130         
131   for(;;)
132     {
133       if(fgets(line, 80, fp) == NULL)
134         return 0;
135
136       while(!index(line, '\n'))
137         {
138           fgets(temp_buf, (strlen(line)+1) * 80, fp);
139           if(!temp_buf)
140             break;
141           strcat(line, temp_buf);
142           line = (char *)xrealloc(line, (strlen(line)+1) * sizeof(char));
143         }        
144       lineno++;
145
146       if((p = strtok(line, "\t\n\r =")) == NULL)
147         continue; /* no tokens on this line */
148
149       if(p[0] == '#')
150         continue; /* comment: ignore */
151
152       for(i = 0; hazahaza[i].name != NULL; i++)
153         if(!strcasecmp(hazahaza[i].name, p))
154           break;
155
156       if(!hazahaza[i].name)
157         {
158           fprintf(stderr, _("%s: %d: Invalid variable name `%s'.\n"),
159                   fname, lineno, p);
160           return -1;
161         }
162
163       if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
164         {
165           fprintf(stderr, _("%s: %d: No value given for `%s'.\n"),
166                   fname, lineno, hazahaza[i].name);
167           return -1;
168         }
169
170       cfg = add_config_val(&config, hazahaza[i].argtype, q);
171       if(cfg == NULL)
172         {
173           fprintf(stderr, _("%s: %d: Invalid value `%s' for variable `%s'.\n"),
174                   fname, lineno, q, hazahaza[i].name);
175           return -1;
176         }
177
178       cfg->which = hazahaza[i].which;
179       if(!config)
180         config = cfg;
181     }
182 }
183
184 /*
185   wrapper function for readconfig
186 */
187 int
188 read_config_file(const char *fname)
189 {
190   FILE *fp;
191
192   if((fp = fopen (fname, "r")) == NULL)
193     {
194       fprintf(stderr, _("Could not open %s: %s\n"), fname, sys_errlist[errno]);
195       return 1;
196     }
197
198   if(readconfig(fname, fp))
199     return -1;
200
201   fclose (fp);
202
203   return 0;
204 }
205
206 /*
207   Look up the value of the config option type
208 */
209 const config_t *
210 get_config_val(which_t type)
211 {
212   config_t *p;
213
214   for(p = config; p != NULL; p = p->next)
215     if(p->which == type)
216       return p;
217
218   /* Not found */
219   return NULL;
220 }