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) {
55 splay_delete_tree(*config_tree);
59 config_t *new_config(void) {
60 return xzalloc(sizeof(config_t));
63 void free_config(config_t *cfg) {
76 void config_add(splay_tree_t *config_tree, config_t *cfg) {
77 splay_insert(config_tree, cfg);
80 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
83 cfg.variable = variable;
87 found = splay_search_closest_greater(config_tree, &cfg);
92 if(strcasecmp(found->variable, variable))
98 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
102 node = splay_search_node(config_tree, cfg);
106 found = node->next->data;
108 if(!strcasecmp(found->variable, cfg->variable))
116 bool get_config_bool(const config_t *cfg, bool *result) {
120 if(!strcasecmp(cfg->value, "yes")) {
123 } else if(!strcasecmp(cfg->value, "no")) {
128 logger(NULL, MESHLINK_ERROR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
129 cfg->variable, cfg->file, cfg->line);
134 bool get_config_int(const config_t *cfg, int *result) {
138 if(sscanf(cfg->value, "%d", result) == 1)
141 logger(NULL, MESHLINK_ERROR, "Integer expected for configuration variable %s in %s line %d",
142 cfg->variable, cfg->file, cfg->line);
147 bool get_config_string(const config_t *cfg, char **result) {
151 *result = xstrdup(cfg->value);
156 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
162 ai = str2addrinfo(cfg->value, NULL, 0);
169 logger(NULL, MESHLINK_ERROR, "Hostname or IP address expected for configuration variable %s in %s line %d",
170 cfg->variable, cfg->file, cfg->line);
176 Read exactly one line and strip the trailing newline if any.
178 static char *readline(FILE * fp, char *buf, size_t buflen) {
179 char *newline = NULL;
185 p = fgets(buf, buflen, fp);
190 newline = strchr(p, '\n');
195 /* kill newline and carriage return if necessary */
197 if(newline > p && newline[-1] == '\r')
203 config_t *parse_config_line(char *line, const char *fname, int lineno) {
206 char *variable, *value, *eol;
207 variable = value = line;
209 eol = line + strlen(line);
210 while(strchr("\t ", *--eol))
213 len = strcspn(value, "\t =");
215 value += strspn(value, "\t ");
218 value += strspn(value, "\t ");
220 variable[len] = '\0';
223 const char err[] = "No value for variable";
224 logger(NULL, MESHLINK_ERROR, "%s `%s' on line %d while reading config file %s",
225 err, variable, lineno, fname);
230 cfg->variable = xstrdup(variable);
231 cfg->value = xstrdup(value);
232 cfg->file = xstrdup(fname);
239 Parse a configuration file and put the results in the configuration tree
242 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
244 char buffer[MAX_STRING_SIZE];
251 fp = fopen(fname, "r");
254 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
259 line = readline(fp, buffer, sizeof buffer);
269 if(!*line || *line == '#')
273 if(!strncmp(line, "-----END", 8))
278 if(!strncmp(line, "-----BEGIN", 10)) {
283 cfg = parse_config_line(line, fname, lineno);
286 config_add(config_tree, cfg);
294 bool read_server_config(meshlink_handle_t *mesh) {
295 char filename[PATH_MAX];
298 snprintf(filename, PATH_MAX,"%s" SLASH "meshlink.conf", mesh->confbase);
300 x = read_config_file(mesh->config, filename);
303 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", filename, strerror(errno));
308 bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const char *name) {
309 char filename[PATH_MAX];
312 snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
313 x = read_config_file(config_tree, filename);
318 bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
319 char filename[PATH_MAX];
320 snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
322 FILE *fp = fopen(filename, "a");
325 logger(mesh, MESHLINK_ERROR, "Cannot open config file %s: %s", filename, strerror(errno));
327 fprintf(fp, "%s = %s\n", key, value);