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.10 2000/10/18 20:12:08 zarq Exp $
37 #include "netutl.h" /* for strtoip */
38 #include <utils.h> /* for cp */
44 config_t *config = NULL;
46 int timeout = 0; /* seconds before timeout */
47 char *confbase = NULL; /* directory in which all config files are */
48 char *netname = NULL; /* name of the vpn network */
50 /* Will be set if HUP signal is received. It will be processed when it is safe. */
54 These are all the possible configurable values
56 static internal_config_t hazahaza[] = {
57 /* Main configuration file keywords */
58 { "Name", tincname, TYPE_NAME },
59 { "ConnectTo", connectto, TYPE_NAME },
60 { "PingTimeout", pingtimeout, TYPE_INT },
61 { "TapDevice", tapdevice, TYPE_NAME },
62 { "TapSubnet", tapsubnet, TYPE_IP },
63 { "PrivateKey", privatekey, TYPE_NAME },
64 { "KeyExpire", keyexpire, TYPE_INT },
65 { "Hostnames", resolve_dns, TYPE_BOOL },
66 { "Interface", interface, TYPE_NAME },
67 { "InterfaceIP", interfaceip, TYPE_IP },
68 /* Host configuration file keywords */
69 { "Address", address, TYPE_NAME },
70 { "Port", port, TYPE_INT },
71 { "PublicKey", publickey, TYPE_NAME },
72 { "Subnet", subnet, TYPE_NAME },
73 { "RestrictHosts", restricthosts, TYPE_BOOL },
74 { "RestrictSubnets", restrictsubnets, TYPE_BOOL },
75 { "RestrictAddress", restrictaddress, TYPE_BOOL },
76 { "RestrictPort", restrictport, TYPE_BOOL },
77 { "IndirectData", indirectdata, TYPE_BOOL },
78 { "TCPonly", tcponly, TYPE_BOOL },
83 Add given value to the list of configs cfg
86 add_config_val(config_t **cfg, int argtype, char *val)
91 p = (config_t*)xmalloc(sizeof(*p));
97 p->data.val = strtol(val, &q, 0);
102 p->data.ptr = xmalloc(strlen(val) + 1);
103 strcpy(p->data.ptr, val);
106 p->data.ip = strtoip(val);
109 if(!strcasecmp("yes", val))
110 p->data.val = stupid_true;
111 else if(!strcasecmp("no", val))
112 p->data.val = stupid_false;
117 p->argtype = argtype;
135 Parse a configuration file and put the results in the configuration tree
138 int read_config_file(config_t **base, const char *fname)
142 char line[MAXBUFSIZE]; /* There really should not be any line longer than this... */
147 if((fp = fopen (fname, "r")) == NULL)
154 if(fgets(line, MAXBUFSIZE, fp) == NULL)
162 if(!index(line, '\n'))
164 syslog(LOG_ERR, _("Line %d too long while reading config file %s"), lineno, fname);
168 if((p = strtok(line, "\t\n\r =")) == NULL)
169 continue; /* no tokens on this line */
172 continue; /* comment: ignore */
174 for(i = 0; hazahaza[i].name != NULL; i++)
175 if(!strcasecmp(hazahaza[i].name, p))
178 if(!hazahaza[i].name)
180 syslog(LOG_ERR, _("Invalid variable name on line %d while reading config file %s"),
185 if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
187 fprintf(stderr, _("No value for variable on line %d while reading config file %s"),
192 cfg = add_config_val(base, hazahaza[i].argtype, q);
195 fprintf(stderr, _("Invalid value for variable on line %d while reading config file %s"),
200 cfg->which = hazahaza[i].which;
210 int read_server_config()
215 asprintf(&fname, "%s/tinc.conf", confbase);
216 x = read_config_file(&config, fname);
223 Look up the value of the config option type
225 const config_t *get_config_val(config_t *p, which_t type)
228 for(; p != NULL; p = p->next)
236 Support for multiple config lines.
237 Index is used to get a specific value, 0 being the first, 1 the second etc.
239 const config_t *get_next_config_val(config_t *p, which_t type, int index)
242 for(; p != NULL; p = p->next)
251 Remove the complete configuration tree.
253 void clear_config(config_t **base)
257 for(p = *base; p != NULL; p = next)
260 if(p->data.ptr && (p->argtype == TYPE_NAME))