2 conf.c -- configuration code
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "splay_tree.h"
23 #include "connection.h"
27 #include "netutl.h" /* for str2address */
29 #include "utils.h" /* for cp */
32 splay_tree_t *config_tree;
34 int pinginterval = 0; /* seconds between pings */
35 int pingtimeout = 0; /* seconds to wait for response */
37 static int config_compare(const config_t *a, const config_t *b) {
40 result = strcasecmp(a->variable, b->variable);
45 result = a->line - b->line;
50 return a->file ? strcmp(a->file, b->file) : 0;
53 void init_configuration(splay_tree_t ** config_tree) {
54 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
57 void exit_configuration(splay_tree_t ** config_tree) {
58 splay_delete_tree(*config_tree);
62 config_t *new_config(void) {
63 return xzalloc(sizeof(config_t));
66 void free_config(config_t *cfg) {
79 void config_add(splay_tree_t *config_tree, config_t *cfg) {
80 splay_insert(config_tree, cfg);
83 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
86 cfg.variable = variable;
90 found = splay_search_closest_greater(config_tree, &cfg);
95 if(strcasecmp(found->variable, variable))
101 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
105 node = splay_search_node(config_tree, cfg);
109 found = node->next->data;
111 if(!strcasecmp(found->variable, cfg->variable))
119 bool get_config_bool(const config_t *cfg, bool *result) {
123 if(!strcasecmp(cfg->value, "yes")) {
126 } else if(!strcasecmp(cfg->value, "no")) {
131 logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
132 cfg->variable, cfg->file, cfg->line);
137 bool get_config_int(const config_t *cfg, int *result) {
141 if(sscanf(cfg->value, "%d", result) == 1)
144 logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
145 cfg->variable, cfg->file, cfg->line);
150 bool get_config_string(const config_t *cfg, char **result) {
154 *result = xstrdup(cfg->value);
159 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
165 ai = str2addrinfo(cfg->value, NULL, 0);
172 logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
173 cfg->variable, cfg->file, cfg->line);
179 Read exactly one line and strip the trailing newline if any.
181 static char *readline(FILE * fp, char *buf, size_t buflen) {
182 char *newline = NULL;
188 p = fgets(buf, buflen, fp);
193 newline = strchr(p, '\n');
198 /* kill newline and carriage return if necessary */
200 if(newline > p && newline[-1] == '\r')
206 config_t *parse_config_line(char *line, const char *fname, int lineno) {
209 char *variable, *value, *eol;
210 variable = value = line;
212 eol = line + strlen(line);
213 while(strchr("\t ", *--eol))
216 len = strcspn(value, "\t =");
218 value += strspn(value, "\t ");
221 value += strspn(value, "\t ");
223 variable[len] = '\0';
226 const char err[] = "No value for variable";
227 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
228 err, variable, lineno, fname);
233 cfg->variable = xstrdup(variable);
234 cfg->value = xstrdup(value);
235 cfg->file = xstrdup(fname);
242 Parse a configuration file and put the results in the configuration tree
245 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
247 char buffer[MAX_STRING_SIZE];
254 fp = fopen(fname, "r");
257 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
262 line = readline(fp, buffer, sizeof buffer);
272 if(!*line || *line == '#')
276 if(!strncmp(line, "-----END", 8))
281 if(!strncmp(line, "-----BEGIN", 10)) {
286 cfg = parse_config_line(line, fname, lineno);
289 config_add(config_tree, cfg);
297 bool read_server_config(void) {
301 xasprintf(&fname, "%s" SLASH "tinc.conf", confbase);
303 x = read_config_file(config_tree, fname);
306 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
313 bool read_host_config(splay_tree_t *config_tree, const char *name) {
317 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
318 x = read_config_file(config_tree, fname);
324 bool append_config_file(const char *name, const char *key, const char *value) {
326 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
328 FILE *fp = fopen(fname, "a");
331 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
333 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);