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 static int config_compare(const config_t *a, const config_t *b) {
36 result = strcasecmp(a->variable, b->variable);
41 result = a->line - b->line;
46 return a->file ? strcmp(a->file, b->file) : 0;
49 void init_configuration(splay_tree_t **config_tree) {
50 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
53 void exit_configuration(splay_tree_t **config_tree) {
54 splay_delete_tree(*config_tree);
58 config_t *new_config(void) {
59 return xzalloc(sizeof(config_t));
62 void free_config(config_t *cfg) {
75 void config_add(splay_tree_t *config_tree, config_t *cfg) {
76 splay_insert(config_tree, cfg);
79 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
82 cfg.variable = variable;
86 found = splay_search_closest_greater(config_tree, &cfg);
91 if(strcasecmp(found->variable, variable))
97 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
101 node = splay_search_node(config_tree, cfg);
105 found = node->next->data;
107 if(!strcasecmp(found->variable, cfg->variable))
115 bool get_config_bool(const config_t *cfg, bool *result) {
119 if(!strcasecmp(cfg->value, "yes")) {
122 } else if(!strcasecmp(cfg->value, "no")) {
127 logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
128 cfg->variable, cfg->file, cfg->line);
133 bool get_config_int(const config_t *cfg, int *result) {
137 if(sscanf(cfg->value, "%d", result) == 1)
140 logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
141 cfg->variable, cfg->file, cfg->line);
146 bool get_config_string(const config_t *cfg, char **result) {
150 *result = xstrdup(cfg->value);
155 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
161 ai = str2addrinfo(cfg->value, NULL, 0);
168 logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
169 cfg->variable, cfg->file, cfg->line);
175 Read exactly one line and strip the trailing newline if any.
177 static char *readline(FILE * fp, char *buf, size_t buflen) {
178 char *newline = NULL;
184 p = fgets(buf, buflen, fp);
189 newline = strchr(p, '\n');
194 /* kill newline and carriage return if necessary */
196 if(newline > p && newline[-1] == '\r')
202 config_t *parse_config_line(char *line, const char *fname, int lineno) {
205 char *variable, *value, *eol;
206 variable = value = line;
208 eol = line + strlen(line);
209 while(strchr("\t ", *--eol))
212 len = strcspn(value, "\t =");
214 value += strspn(value, "\t ");
217 value += strspn(value, "\t ");
219 variable[len] = '\0';
222 const char err[] = "No value for variable";
223 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
224 err, variable, lineno, fname);
229 cfg->variable = xstrdup(variable);
230 cfg->value = xstrdup(value);
231 cfg->file = xstrdup(fname);
238 Parse a configuration file and put the results in the configuration tree
241 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
243 char buffer[MAX_STRING_SIZE];
250 fp = fopen(fname, "r");
253 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
258 line = readline(fp, buffer, sizeof buffer);
268 if(!*line || *line == '#')
272 if(!strncmp(line, "-----END", 8))
277 if(!strncmp(line, "-----BEGIN", 10)) {
282 cfg = parse_config_line(line, fname, lineno);
285 config_add(config_tree, cfg);
293 bool read_server_config(meshlink_handle_t *mesh) {
294 char filename[PATH_MAX];
297 snprintf(filename, PATH_MAX,"%s" SLASH "meshlink.conf", mesh->confbase);
299 x = read_config_file(mesh->config, filename);
302 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", filename, strerror(errno));
307 bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const char *name) {
308 char filename[PATH_MAX];
311 snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
312 x = read_config_file(config_tree, filename);
317 bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
318 char filename[PATH_MAX];
319 snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
321 FILE *fp = fopen(filename, "a");
324 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", filename, strerror(errno));
326 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);