2 conf.c -- configuration code
3 Copyright (C) 1998 Robert van der Meulen
4 1998-2005 Ivo Timmermans
6 2010-2011 Julien Muchembled <jm@jmuchemb.eu>
7 2000-2013 Guus Sliepen <guus@tinc-vpn.org>
8 2013 Florent Clairambault <florent@clairambault.fr>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "splay_tree.h"
28 #include "connection.h"
33 #include "netutl.h" /* for str2address */
35 #include "utils.h" /* for cp */
38 splay_tree_t *config_tree;
40 int pinginterval = 0; /* seconds between pings */
41 int pingtimeout = 0; /* seconds to wait for response */
42 list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
44 static int config_compare(const config_t *a, const config_t *b) {
47 result = strcasecmp(a->variable, b->variable);
52 /* give priority to command line options */
53 result = !b->file - !a->file;
57 result = a->line - b->line;
62 return a->file ? strcmp(a->file, b->file) : 0;
65 void init_configuration(splay_tree_t ** config_tree) {
66 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
69 void exit_configuration(splay_tree_t ** config_tree) {
70 splay_delete_tree(*config_tree);
74 config_t *new_config(void) {
75 return xzalloc(sizeof(config_t));
78 void free_config(config_t *cfg) {
91 void config_add(splay_tree_t *config_tree, config_t *cfg) {
92 splay_insert(config_tree, cfg);
95 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
98 cfg.variable = variable;
102 found = splay_search_closest_greater(config_tree, &cfg);
107 if(strcasecmp(found->variable, variable))
113 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
117 node = splay_search_node(config_tree, cfg);
121 found = node->next->data;
123 if(!strcasecmp(found->variable, cfg->variable))
131 bool get_config_bool(const config_t *cfg, bool *result) {
135 if(!strcasecmp(cfg->value, "yes")) {
138 } else if(!strcasecmp(cfg->value, "no")) {
143 logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
144 cfg->variable, cfg->file, cfg->line);
149 bool get_config_int(const config_t *cfg, int *result) {
153 if(sscanf(cfg->value, "%d", result) == 1)
156 logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
157 cfg->variable, cfg->file, cfg->line);
162 bool get_config_string(const config_t *cfg, char **result) {
166 *result = xstrdup(cfg->value);
171 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
177 ai = str2addrinfo(cfg->value, NULL, 0);
184 logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
185 cfg->variable, cfg->file, cfg->line);
191 Read exactly one line and strip the trailing newline if any.
193 static char *readline(FILE * fp, char *buf, size_t buflen) {
194 char *newline = NULL;
200 p = fgets(buf, buflen, fp);
205 newline = strchr(p, '\n');
210 /* kill newline and carriage return if necessary */
212 if(newline > p && newline[-1] == '\r')
218 config_t *parse_config_line(char *line, const char *fname, int lineno) {
221 char *variable, *value, *eol;
222 variable = value = line;
224 eol = line + strlen(line);
225 while(strchr("\t ", *--eol))
228 len = strcspn(value, "\t =");
230 value += strspn(value, "\t ");
233 value += strspn(value, "\t ");
235 variable[len] = '\0';
238 const char err[] = "No value for variable";
240 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
241 err, variable, lineno, fname);
243 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
244 err, variable, lineno);
249 cfg->variable = xstrdup(variable);
250 cfg->value = xstrdup(value);
251 cfg->file = fname ? xstrdup(fname) : NULL;
258 Parse a configuration file and put the results in the configuration tree
261 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
263 char buffer[MAX_STRING_SIZE];
270 fp = fopen(fname, "r");
273 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
278 line = readline(fp, buffer, sizeof buffer);
288 if(!*line || *line == '#')
292 if(!strncmp(line, "-----END", 8))
297 if(!strncmp(line, "-----BEGIN", 10)) {
302 cfg = parse_config_line(line, fname, lineno);
305 config_add(config_tree, cfg);
313 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
314 size_t prefix_len = prefix ? strlen(prefix) : 0;
316 for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) {
317 const config_t *cfg = node->data;
321 if(strchr(cfg->variable, '.'))
324 if(strncmp(prefix, cfg->variable, prefix_len) ||
325 cfg->variable[prefix_len] != '.')
331 new->variable = xstrdup(cfg->variable + prefix_len + 1);
333 new->variable = xstrdup(cfg->variable);
334 new->value = xstrdup(cfg->value);
336 new->line = cfg->line;
338 config_add(config_tree, new);
342 bool read_server_config(void) {
346 //read_config_options(config_tree, NULL);
348 xasprintf(&fname, "%s" SLASH "tinc.conf", confbase);
350 x = read_config_file(config_tree, fname);
352 // We will try to read the conf files in the "conf.d" dir
355 xasprintf(&dname, "%s" SLASH "conf.d", confbase);
356 DIR *dir = opendir (dname);
357 // If we can find this dir
360 // We list all the files in it
361 while (x && (ep = readdir (dir))) {
362 size_t l = strlen(ep->d_name);
363 // And we try to read the ones that end with ".conf"
364 if (l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
366 xasprintf(&fname, "%s" SLASH "%s", dname, ep->d_name);
367 x = read_config_file(config_tree, fname);
376 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
383 bool read_host_config(splay_tree_t *config_tree, const char *name) {
387 //read_config_options(config_tree, name);
389 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
390 x = read_config_file(config_tree, fname);
396 bool append_config_file(const char *name, const char *key, const char *value) {
398 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
400 FILE *fp = fopen(fname, "a");
403 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
405 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);