Internationalization of tinc.
[tinc] / src / conf.c
1 /*
2     conf.c -- configuration code
3     Copyright (C) 1998 Emphyrio,
4     Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /* Created more dynamic storage for lines read from the config file. --
22         Cris van Pelt */
23
24 #include "config.h"
25
26 #include <ctype.h>
27 #include <errno.h>
28 #include <netdb.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <xalloc.h>
34
35 #include "conf.h"
36 #include "netutl.h" /* for strtoip */
37
38 #include "system.h"
39
40 config_t *config;
41 int debug_lvl = 0;
42 int timeout = 0; /* seconds before timeout */
43
44 typedef struct internal_config_t {
45   char *name;
46   enum which_t which;
47   int argtype;
48 } internal_config_t;
49
50 /*
51   These are all the possible configurable values
52 */
53 static internal_config_t hazahaza[] = {
54   { "AllowConnect", allowconnect,   TYPE_BOOL },   /* Is not used anywhere. Remove? */
55   { "ConnectTo",    upstreamip,     TYPE_IP },
56   { "ConnectPort",  upstreamport,   TYPE_INT },
57   { "ListenPort",   listenport,     TYPE_INT },
58   { "MyOwnVPNIP",   myvpnip,        TYPE_IP },
59   { "MyVirtualIP",  myvpnip,        TYPE_IP },   /* an alias */
60   { "Passphrases",  passphrasesdir, TYPE_NAME },
61   { "PingTimeout",  pingtimeout,    TYPE_INT },
62   { "TapDevice",    tapdevice,      TYPE_NAME },
63   { "KeyExpire",    keyexpire,      TYPE_INT },
64   { "VpnMask",      vpnmask,        TYPE_IP },
65   { NULL, 0, 0 }
66 };
67
68 /*
69   Add given value to the list of configs cfg
70 */
71 config_t *
72 add_config_val(config_t **cfg, int argtype, char *val)
73 {
74   config_t *p;
75   char *q;
76
77   p = (config_t*)xmalloc(sizeof(*p));
78   p->data.val = 0;
79   
80   switch(argtype)
81     {
82     case TYPE_INT:
83       p->data.val = strtol(val, &q, 0);
84       if(q && *q)
85         p->data.val = 0;
86       break;
87     case TYPE_NAME:
88       p->data.ptr = xmalloc(strlen(val) + 1);
89       strcpy(p->data.ptr, val);
90       break;
91     case TYPE_IP:
92       p->data.ip = strtoip(val);
93       break;
94     case TYPE_BOOL:
95       if(!strcasecmp("yes", val))
96         p->data.val = stupid_true;
97       else if(!strcasecmp("no", val))
98         p->data.val = stupid_false;
99       else
100         p->data.val = 0;
101     }
102
103   if(p->data.val)
104     {
105       p->next = *cfg;
106       *cfg = p;
107       return p;
108     }
109
110   free(p);
111   return NULL;
112 }
113
114 /*
115   Get variable from a section in a configfile. returns -1 on failure.
116 */
117 int
118 readconfig(const char *fname, FILE *fp)
119 {
120   char *line, *temp_buf;
121   char *p, *q;
122   int i, lineno = 0;
123   config_t *cfg;
124
125   line = (char *)xmalloc(80 * sizeof(char));
126   temp_buf = (char *)xmalloc(80 * sizeof(char));
127         
128   for(;;)
129     {
130       if(fgets(line, 80, fp) == NULL)
131         return 0;
132
133       while(!index(line, '\n'))
134         {
135           fgets(temp_buf, (strlen(line)+1) * 80, fp);
136           if(!temp_buf)
137             break;
138           strcat(line, temp_buf);
139           line = (char *)xrealloc(line, (strlen(line)+1) * sizeof(char));
140         }        
141       lineno++;
142
143       if((p = strtok(line, "\t\n\r =")) == NULL)
144         continue; /* no tokens on this line */
145
146       if(p[0] == '#')
147         continue; /* comment: ignore */
148
149       for(i = 0; hazahaza[i].name != NULL; i++)
150         if(!strcasecmp(hazahaza[i].name, p))
151           break;
152
153       if(!hazahaza[i].name)
154         {
155           fprintf(stderr, _("%s: %d: Invalid variable name `%s'.\n"),
156                   fname, lineno, p);
157           return -1;
158         }
159
160       if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
161         {
162           fprintf(stderr, _("%s: %d: No value given for `%s'.\n"),
163                   fname, lineno, hazahaza[i].name);
164           return -1;
165         }
166
167       cfg = add_config_val(&config, hazahaza[i].argtype, q);
168       if(cfg == NULL)
169         {
170           fprintf(stderr, _("%s: %d: Invalid value `%s' for variable `%s'.\n"),
171                   fname, lineno, q, hazahaza[i].name);
172           return -1;
173         }
174
175       cfg->which = hazahaza[i].which;
176       if(!config)
177         config = cfg;
178     }
179 }
180
181 /*
182   wrapper function for readconfig
183 */
184 int
185 read_config_file(const char *fname)
186 {
187   FILE *fp;
188
189   if((fp = fopen (fname, "r")) == NULL)
190     {
191       fprintf(stderr, _("Could not open %s: %s\n"), fname, sys_errlist[errno]);
192       return 1;
193     }
194
195   if(readconfig(fname, fp))
196     return -1;
197
198   fclose (fp);
199
200   return 0;
201 }
202
203 /*
204   Look up the value of the config option type
205 */
206 const config_t *
207 get_config_val(which_t type)
208 {
209   config_t *p;
210
211   for(p = config; p != NULL; p = p->next)
212     if(p->which == type)
213       return p;
214
215   /* Not found */
216   return NULL;
217 }