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>
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.
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.
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.
22 $Id: conf.c,v 1.9.4.8 2000/08/09 14:02:15 guus Exp $
38 #include "netutl.h" /* for strtoip */
39 #include <utils.h> /* for cp */
45 int timeout = 0; /* seconds before timeout */
46 char *configfilename = NULL;
48 /* Will be set if HUP signal is received. It will be processed when it is safe. */
51 typedef struct internal_config_t {
58 These are all the possible configurable values
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 },
81 Add given value to the list of configs cfg
84 add_config_val(config_t **cfg, int argtype, char *val)
89 p = (config_t*)xmalloc(sizeof(*p));
95 p->data.val = strtol(val, &q, 0);
100 p->data.ptr = xmalloc(strlen(val) + 1);
101 strcpy(p->data.ptr, val);
104 p->data.ip = strtoip(val);
107 if(!strcasecmp("yes", val))
108 p->data.val = stupid_true;
109 else if(!strcasecmp("no", val))
110 p->data.val = stupid_false;
115 p->argtype = argtype;
138 Get variable from a section in a configfile. returns -1 on failure.
141 readconfig(const char *fname, FILE *fp)
143 char *line, *temp_buf;
148 line = (char *)xmalloc(80 * sizeof(char));
149 temp_buf = (char *)xmalloc(80 * sizeof(char));
153 if(fgets(line, 80, fp) == NULL)
156 while(!index(line, '\n'))
158 fgets(temp_buf, (strlen(line)+1) * 80, fp);
161 strcat(line, temp_buf);
162 line = (char *)xrealloc(line, (strlen(line)+1) * sizeof(char));
166 if((p = strtok(line, "\t\n\r =")) == NULL)
167 continue; /* no tokens on this line */
170 continue; /* comment: ignore */
172 for(i = 0; hazahaza[i].name != NULL; i++)
173 if(!strcasecmp(hazahaza[i].name, p))
176 if(!hazahaza[i].name)
178 fprintf(stderr, _("%s: %d: Invalid variable name `%s'.\n"),
183 if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
185 fprintf(stderr, _("%s: %d: No value given for `%s'.\n"),
186 fname, lineno, hazahaza[i].name);
190 cfg = add_config_val(&config, hazahaza[i].argtype, q);
193 fprintf(stderr, _("%s: %d: Invalid value `%s' for variable `%s'.\n"),
194 fname, lineno, q, hazahaza[i].name);
198 cfg->which = hazahaza[i].which;
207 wrapper function for readconfig
210 read_config_file(const char *fname)
215 if((fp = fopen (fname, "r")) == NULL)
217 fprintf(stderr, _("Could not open %s: %s\n"), fname, sys_errlist[errno]);
221 err = readconfig(fname, fp);
228 Look up the value of the config option type
231 get_config_val(which_t type)
235 for(p = config; p != NULL; p = p->next)
244 Support for multiple config lines.
245 Index is used to get a specific value, 0 being the first, 1 the second etc.
248 get_next_config_val(which_t type, int index)
252 for(p = config; p != NULL; p = p->next)
262 Remove the complete configuration tree.
268 for(p = config; p != NULL; p = next)
271 if(p->data.ptr && (p->argtype == TYPE_NAME))