2 conf.c -- configuration code
3 Copyright (C) 1998 Robert van der Meulen
4 1998-2005 Ivo Timmermans
5 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
6 2010-2011 Julien Muchembled <jm@jmuchemb.eu>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "splay_tree.h"
27 #include "connection.h"
31 #include "netutl.h" /* for str2address */
33 #include "utils.h" /* for cp */
36 splay_tree_t *config_tree;
38 int pinginterval = 0; /* seconds between pings */
39 int pingtimeout = 0; /* seconds to wait for response */
40 char *confbase = NULL; /* directory in which all config files are */
41 char *netname = NULL; /* name of the vpn network */
42 list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
45 static int config_compare(const config_t *a, const config_t *b) {
48 result = strcasecmp(a->variable, b->variable);
53 /* give priority to command line options */
54 result = !b->file - !a->file;
58 result = a->line - b->line;
63 return a->file ? strcmp(a->file, b->file) : 0;
66 void init_configuration(splay_tree_t ** config_tree) {
67 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
70 void exit_configuration(splay_tree_t ** config_tree) {
71 splay_delete_tree(*config_tree);
75 config_t *new_config(void) {
76 return xmalloc_and_zero(sizeof(config_t));
79 void free_config(config_t *cfg) {
92 void config_add(splay_tree_t *config_tree, config_t *cfg) {
93 splay_insert(config_tree, cfg);
96 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
99 cfg.variable = variable;
103 found = splay_search_closest_greater(config_tree, &cfg);
108 if(strcasecmp(found->variable, variable))
114 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
118 node = splay_search_node(config_tree, cfg);
122 found = node->next->data;
124 if(!strcasecmp(found->variable, cfg->variable))
132 bool get_config_bool(const config_t *cfg, bool *result) {
136 if(!strcasecmp(cfg->value, "yes")) {
139 } else if(!strcasecmp(cfg->value, "no")) {
144 logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
145 cfg->variable, cfg->file, cfg->line);
150 bool get_config_int(const config_t *cfg, int *result) {
154 if(sscanf(cfg->value, "%d", result) == 1)
157 logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
158 cfg->variable, cfg->file, cfg->line);
163 bool get_config_string(const config_t *cfg, char **result) {
167 *result = xstrdup(cfg->value);
172 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
178 ai = str2addrinfo(cfg->value, NULL, 0);
185 logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
186 cfg->variable, cfg->file, cfg->line);
191 bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
192 subnet_t subnet = {NULL};
197 if(!str2net(&subnet, cfg->value)) {
198 logger(DEBUG_ALWAYS, LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
199 cfg->variable, cfg->file, cfg->line);
203 /* Teach newbies what subnets are... */
205 if(((subnet.type == SUBNET_IPV4)
206 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof subnet.net.ipv4.address))
207 || ((subnet.type == SUBNET_IPV6)
208 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address))) {
209 logger(DEBUG_ALWAYS, LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
210 cfg->variable, cfg->file, cfg->line);
214 *(*result = new_subnet()) = subnet;
220 Read exactly one line and strip the trailing newline if any.
222 static char *readline(FILE * fp, char *buf, size_t buflen) {
223 char *newline = NULL;
229 p = fgets(buf, buflen, fp);
234 newline = strchr(p, '\n');
239 /* kill newline and carriage return if necessary */
241 if(newline > p && newline[-1] == '\r')
247 config_t *parse_config_line(char *line, const char *fname, int lineno) {
250 char *variable, *value, *eol;
251 variable = value = line;
253 eol = line + strlen(line);
254 while(strchr("\t ", *--eol))
257 len = strcspn(value, "\t =");
259 value += strspn(value, "\t ");
262 value += strspn(value, "\t ");
264 variable[len] = '\0';
267 const char err[] = "No value for variable";
269 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
270 err, variable, lineno, fname);
272 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
273 err, variable, lineno);
278 cfg->variable = xstrdup(variable);
279 cfg->value = xstrdup(value);
280 cfg->file = fname ? xstrdup(fname) : NULL;
287 Parse a configuration file and put the results in the configuration tree
290 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
292 char buffer[MAX_STRING_SIZE];
299 fp = fopen(fname, "r");
302 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
307 line = readline(fp, buffer, sizeof buffer);
317 if(!*line || *line == '#')
321 if(!strncmp(line, "-----END", 8))
326 if(!strncmp(line, "-----BEGIN", 10)) {
331 cfg = parse_config_line(line, fname, lineno);
334 config_add(config_tree, cfg);
342 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
343 size_t prefix_len = prefix ? strlen(prefix) : 0;
345 for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) {
346 const config_t *cfg = node->data;
350 if(strchr(cfg->variable, '.'))
353 if(strncmp(prefix, cfg->variable, prefix_len) ||
354 cfg->variable[prefix_len] != '.')
360 new->variable = xstrdup(cfg->variable + prefix_len + 1);
362 new->variable = xstrdup(cfg->variable);
363 new->value = xstrdup(cfg->value);
365 new->line = cfg->line;
367 config_add(config_tree, new);
371 bool read_server_config(void) {
375 read_config_options(config_tree, NULL);
377 xasprintf(&fname, "%s" SLASH "tinc.conf", confbase);
378 x = read_config_file(config_tree, fname);
381 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
388 bool read_host_config(splay_tree_t *config_tree, const char *name) {
392 read_config_options(config_tree, name);
394 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
395 x = read_config_file(config_tree, fname);
401 bool append_config_file(const char *name, const char *key, const char *value) {
403 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
405 FILE *fp = fopen(fname, "a");
408 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
410 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);