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-2022 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 */
37 int pinginterval = 0; /* seconds between pings */
38 int pingtimeout = 0; /* seconds to wait for response */
40 /* global/host configuration values given at the command line */
41 list_t cmdline_conf = {
45 .delete = (list_action_t)free_config,
48 static int config_compare(const config_t *a, const config_t *b) {
51 result = strcasecmp(a->variable, b->variable);
57 /* give priority to command line options */
58 result = !b->file - !a->file;
64 result = a->line - b->line;
69 return a->file ? strcmp(a->file, b->file) : 0;
73 splay_tree_t config_tree = {
74 .compare = (splay_compare_t) config_compare,
75 .delete = (splay_action_t) free_config,
78 splay_tree_t *create_configuration(void) {
79 splay_tree_t *tree = splay_alloc_tree(NULL, NULL);
80 init_configuration(tree);
84 void init_configuration(splay_tree_t *tree) {
85 memset(tree, 0, sizeof(*tree));
86 tree->compare = (splay_compare_t) config_compare;
87 tree->delete = (splay_action_t) free_config;
90 void exit_configuration(splay_tree_t **config_tree) {
91 splay_delete_tree(*config_tree);
95 config_t *new_config(void) {
96 return xzalloc(sizeof(config_t));
99 void free_config(config_t *cfg) {
101 free_string(cfg->value);
106 void config_add(splay_tree_t *config_tree, config_t *cfg) {
107 splay_insert(config_tree, cfg);
110 config_t *lookup_config(splay_tree_t *config_tree, const char *variable) {
111 const config_t cfg = {
112 .variable = (char *)variable,
117 config_t *found = splay_search_closest_greater(config_tree, &cfg);
123 if(strcasecmp(found->variable, variable)) {
130 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
134 node = splay_search_node(config_tree, cfg);
138 found = node->next->data;
140 if(!strcasecmp(found->variable, cfg->variable)) {
149 bool get_config_bool(const config_t *cfg, bool *result) {
154 if(!strcasecmp(cfg->value, "yes")) {
157 } else if(!strcasecmp(cfg->value, "no")) {
162 logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
163 cfg->variable, cfg->file, cfg->line);
168 bool get_config_int(const config_t *cfg, int *result) {
173 if(sscanf(cfg->value, "%d", result) == 1) {
177 logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
178 cfg->variable, cfg->file, cfg->line);
183 bool get_config_string(const config_t *cfg, char **result) {
188 *result = xstrdup(cfg->value);
193 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
200 ai = str2addrinfo(cfg->value, NULL, 0);
207 logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
208 cfg->variable, cfg->file, cfg->line);
214 Read exactly one line and strip the trailing newline if any.
216 static char *readline(FILE *fp, char *buf, size_t buflen) {
217 char *newline = NULL;
224 p = fgets(buf, (int) buflen, fp);
230 newline = strchr(p, '\n');
236 /* kill newline and carriage return if necessary */
239 if(newline > p && newline[-1] == '\r') {
246 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);
253 while(strchr("\t ", *--eol)) {
257 size_t len = strcspn(value, "\t =");
259 value += strspn(value, "\t ");
263 value += strspn(value, "\t ");
266 variable[len] = '\0';
269 const char err[] = "No value for variable";
272 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
273 err, variable, lineno, fname);
275 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
276 err, variable, lineno);
282 cfg->variable = xstrdup(variable);
283 cfg->value = xstrdup(value);
284 cfg->file = fname ? xstrdup(fname) : NULL;
291 Parse a configuration file and put the results in the configuration tree
294 bool read_config_file(splay_tree_t *config_tree, const char *fname, bool verbose) {
296 char buffer[MAX_STRING_SIZE];
303 fp = fopen(fname, "r");
306 logger(verbose ? DEBUG_ALWAYS : DEBUG_CONNECTIONS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
311 line = readline(fp, buffer, sizeof(buffer));
323 if(!*line || *line == '#') {
328 if(!strncmp(line, "-----END", 8)) {
335 if(!strncmp(line, "-----BEGIN", 10)) {
340 cfg = parse_config_line(line, fname, lineno);
346 config_add(config_tree, cfg);
354 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
355 size_t prefix_len = prefix ? strlen(prefix) : 0;
357 for(const list_node_t *node = cmdline_conf.tail; node; node = node->prev) {
358 const config_t *cfg = node->data;
362 if(strchr(cfg->variable, '.')) {
366 if(strncmp(prefix, cfg->variable, prefix_len) ||
367 cfg->variable[prefix_len] != '.') {
375 new->variable = xstrdup(cfg->variable + prefix_len + 1);
377 new->variable = xstrdup(cfg->variable);
380 new->value = xstrdup(cfg->value);
382 new->line = cfg->line;
384 config_add(config_tree, new);
388 bool read_server_config(splay_tree_t *config_tree) {
389 char fname[PATH_MAX];
392 read_config_options(config_tree, NULL);
394 snprintf(fname, sizeof(fname), "%s" SLASH "tinc.conf", confbase);
396 x = read_config_file(config_tree, fname, true);
398 // We will try to read the conf files in the "conf.d" dir
400 char dname[PATH_MAX];
401 snprintf(dname, sizeof(dname), "%s" SLASH "conf.d", confbase);
402 DIR *dir = opendir(dname);
404 // If we can find this dir
408 // We list all the files in it
409 while(x && (ep = readdir(dir))) {
410 size_t l = strlen(ep->d_name);
412 // And we try to read the ones that end with ".conf"
413 if(l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
414 if((size_t)snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name) >= sizeof(fname)) {
415 logger(DEBUG_ALWAYS, LOG_ERR, "Pathname too long: %s/%s", dname, ep->d_name);
419 x = read_config_file(config_tree, fname, true);
428 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
434 bool read_host_config(splay_tree_t *config_tree, const char *name, bool verbose) {
435 read_config_options(config_tree, name);
437 char fname[PATH_MAX];
438 snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, name);
439 return read_config_file(config_tree, fname, verbose);
442 bool append_config_file(const char *name, const char *key, const char *value) {
443 char fname[PATH_MAX];
444 snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, name);
446 FILE *fp = fopen(fname, "a");
449 logger(DEBUG_ALWAYS, LOG_DEBUG, "Cannot open config file %s: %s", fname, strerror(errno));
453 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);