2 conf.c -- configuration code
3 Copyright (C) 1998 Robert van der Meulen
4 1998-2005 Ivo Timmermans
5 2000-2013 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"
32 #include "netutl.h" /* for str2address */
34 #include "utils.h" /* for cp */
37 splay_tree_t *config_tree;
39 int pinginterval = 0; /* seconds between pings */
40 int pingtimeout = 0; /* seconds to wait for response */
41 list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
43 static int config_compare(const config_t *a, const config_t *b) {
46 result = strcasecmp(a->variable, b->variable);
51 /* give priority to command line options */
52 result = !b->file - !a->file;
56 result = a->line - b->line;
61 return a->file ? strcmp(a->file, b->file) : 0;
64 void init_configuration(splay_tree_t ** config_tree) {
65 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
68 void exit_configuration(splay_tree_t ** config_tree) {
69 splay_delete_tree(*config_tree);
73 config_t *new_config(void) {
74 return xmalloc_and_zero(sizeof(config_t));
77 void free_config(config_t *cfg) {
90 void config_add(splay_tree_t *config_tree, config_t *cfg) {
91 splay_insert(config_tree, cfg);
94 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
97 cfg.variable = variable;
101 found = splay_search_closest_greater(config_tree, &cfg);
106 if(strcasecmp(found->variable, variable))
112 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
116 node = splay_search_node(config_tree, cfg);
120 found = node->next->data;
122 if(!strcasecmp(found->variable, cfg->variable))
130 bool get_config_bool(const config_t *cfg, bool *result) {
134 if(!strcasecmp(cfg->value, "yes")) {
137 } else if(!strcasecmp(cfg->value, "no")) {
142 logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
143 cfg->variable, cfg->file, cfg->line);
148 bool get_config_int(const config_t *cfg, int *result) {
152 if(sscanf(cfg->value, "%d", result) == 1)
155 logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
156 cfg->variable, cfg->file, cfg->line);
161 bool get_config_string(const config_t *cfg, char **result) {
165 *result = xstrdup(cfg->value);
170 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
176 ai = str2addrinfo(cfg->value, NULL, 0);
183 logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
184 cfg->variable, cfg->file, cfg->line);
189 bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
190 subnet_t subnet = {NULL};
195 if(!str2net(&subnet, cfg->value)) {
196 logger(DEBUG_ALWAYS, LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
197 cfg->variable, cfg->file, cfg->line);
201 /* Teach newbies what subnets are... */
203 if(((subnet.type == SUBNET_IPV4)
204 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof subnet.net.ipv4.address))
205 || ((subnet.type == SUBNET_IPV6)
206 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address))) {
207 logger(DEBUG_ALWAYS, LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
208 cfg->variable, cfg->file, cfg->line);
212 *(*result = new_subnet()) = subnet;
218 Read exactly one line and strip the trailing newline if any.
220 static char *readline(FILE * fp, char *buf, size_t buflen) {
221 char *newline = NULL;
227 p = fgets(buf, buflen, fp);
232 newline = strchr(p, '\n');
237 /* kill newline and carriage return if necessary */
239 if(newline > p && newline[-1] == '\r')
245 config_t *parse_config_line(char *line, const char *fname, int lineno) {
248 char *variable, *value, *eol;
249 variable = value = line;
251 eol = line + strlen(line);
252 while(strchr("\t ", *--eol))
255 len = strcspn(value, "\t =");
257 value += strspn(value, "\t ");
260 value += strspn(value, "\t ");
262 variable[len] = '\0';
265 const char err[] = "No value for variable";
267 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
268 err, variable, lineno, fname);
270 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
271 err, variable, lineno);
276 cfg->variable = xstrdup(variable);
277 cfg->value = xstrdup(value);
278 cfg->file = fname ? xstrdup(fname) : NULL;
285 Parse a configuration file and put the results in the configuration tree
288 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
290 char buffer[MAX_STRING_SIZE];
297 fp = fopen(fname, "r");
300 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
305 line = readline(fp, buffer, sizeof buffer);
315 if(!*line || *line == '#')
319 if(!strncmp(line, "-----END", 8))
324 if(!strncmp(line, "-----BEGIN", 10)) {
329 cfg = parse_config_line(line, fname, lineno);
332 config_add(config_tree, cfg);
340 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
341 size_t prefix_len = prefix ? strlen(prefix) : 0;
343 for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) {
344 const config_t *cfg = node->data;
348 if(strchr(cfg->variable, '.'))
351 if(strncmp(prefix, cfg->variable, prefix_len) ||
352 cfg->variable[prefix_len] != '.')
358 new->variable = xstrdup(cfg->variable + prefix_len + 1);
360 new->variable = xstrdup(cfg->variable);
361 new->value = xstrdup(cfg->value);
363 new->line = cfg->line;
365 config_add(config_tree, new);
369 bool read_server_config(void) {
373 read_config_options(config_tree, NULL);
375 xasprintf(&fname, "%s" SLASH "tinc.conf", confbase);
376 x = read_config_file(config_tree, fname);
379 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
386 bool read_host_config(splay_tree_t *config_tree, const char *name) {
390 read_config_options(config_tree, name);
392 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
393 x = read_config_file(config_tree, fname);
399 bool append_config_file(const char *name, const char *key, const char *value) {
401 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
403 FILE *fp = fopen(fname, "a");
406 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
408 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);