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 "meshlink_internal.h"
28 #include "netutl.h" /* for str2address */
30 #include "utils.h" /* for cp */
33 int pinginterval = 0; /* seconds between pings */
34 int pingtimeout = 0; /* seconds to wait for response */
36 static int config_compare(const config_t *a, const config_t *b) {
39 result = strcasecmp(a->variable, b->variable);
44 result = a->line - b->line;
49 return a->file ? strcmp(a->file, b->file) : 0;
52 void init_configuration(splay_tree_t **config_tree) {
53 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
56 void exit_configuration(splay_tree_t **config_tree) {
57 splay_delete_tree(*config_tree);
61 config_t *new_config(void) {
62 return xzalloc(sizeof(config_t));
65 void free_config(config_t *cfg) {
78 void config_add(splay_tree_t *config_tree, config_t *cfg) {
79 splay_insert(config_tree, cfg);
82 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
85 cfg.variable = variable;
89 found = splay_search_closest_greater(config_tree, &cfg);
94 if(strcasecmp(found->variable, variable))
100 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
104 node = splay_search_node(config_tree, cfg);
108 found = node->next->data;
110 if(!strcasecmp(found->variable, cfg->variable))
118 bool get_config_bool(const config_t *cfg, bool *result) {
122 if(!strcasecmp(cfg->value, "yes")) {
125 } else if(!strcasecmp(cfg->value, "no")) {
130 logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
131 cfg->variable, cfg->file, cfg->line);
136 bool get_config_int(const config_t *cfg, int *result) {
140 if(sscanf(cfg->value, "%d", result) == 1)
143 logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
144 cfg->variable, cfg->file, cfg->line);
149 bool get_config_string(const config_t *cfg, char **result) {
153 *result = xstrdup(cfg->value);
158 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
164 ai = str2addrinfo(cfg->value, NULL, 0);
171 logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
172 cfg->variable, cfg->file, cfg->line);
178 Read exactly one line and strip the trailing newline if any.
180 static char *readline(FILE * fp, char *buf, size_t buflen) {
181 char *newline = NULL;
187 p = fgets(buf, buflen, fp);
192 newline = strchr(p, '\n');
197 /* kill newline and carriage return if necessary */
199 if(newline > p && newline[-1] == '\r')
205 config_t *parse_config_line(char *line, const char *fname, int lineno) {
208 char *variable, *value, *eol;
209 variable = value = line;
211 eol = line + strlen(line);
212 while(strchr("\t ", *--eol))
215 len = strcspn(value, "\t =");
217 value += strspn(value, "\t ");
220 value += strspn(value, "\t ");
222 variable[len] = '\0';
225 const char err[] = "No value for variable";
226 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
227 err, variable, lineno, fname);
232 cfg->variable = xstrdup(variable);
233 cfg->value = xstrdup(value);
234 cfg->file = xstrdup(fname);
241 Parse a configuration file and put the results in the configuration tree
244 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
246 char buffer[MAX_STRING_SIZE];
253 fp = fopen(fname, "r");
256 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
261 line = readline(fp, buffer, sizeof buffer);
271 if(!*line || *line == '#')
275 if(!strncmp(line, "-----END", 8))
280 if(!strncmp(line, "-----BEGIN", 10)) {
285 cfg = parse_config_line(line, fname, lineno);
288 config_add(config_tree, cfg);
296 bool read_server_config(void) {
300 xasprintf(&fname, "%s" SLASH "meshlink.conf", mesh->confbase);
302 x = read_config_file(mesh->config, fname);
305 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
312 bool read_host_config(splay_tree_t *config_tree, const char *name) {
316 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
317 x = read_config_file(config_tree, fname);
323 bool append_config_file(const char *name, const char *key, const char *value) {
325 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
327 FILE *fp = fopen(fname, "a");
330 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
332 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);