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 set_config_int(config_t *cfg, int val)
153 snprintf(val_str, sizeof(val_str), "%d", val);
158 cfg->value = xstrdup(val_str);
163 bool get_config_string(const config_t *cfg, char **result) {
167 *result = xstrdup(cfg->value);
172 bool set_config_string(config_t *cfg, const char* val)
180 cfg->value = xstrdup(val);
185 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
191 ai = str2addrinfo(cfg->value, NULL, 0);
198 logger(NULL, MESHLINK_ERROR, "Hostname or IP address expected for configuration variable %s in %s line %d",
199 cfg->variable, cfg->file, cfg->line);
205 Read exactly one line and strip the trailing newline if any.
207 static char *readline(FILE * fp, char *buf, size_t buflen) {
208 char *newline = NULL;
214 p = fgets(buf, buflen, fp);
219 newline = strchr(p, '\n');
224 /* kill newline and carriage return if necessary */
226 if(newline > p && newline[-1] == '\r')
232 config_t *parse_config_line(char *line, const char *fname, int lineno) {
235 char *variable, *value, *eol;
236 variable = value = line;
238 eol = line + strlen(line);
239 while(strchr("\t ", *--eol))
242 len = strcspn(value, "\t =");
244 value += strspn(value, "\t ");
247 value += strspn(value, "\t ");
249 variable[len] = '\0';
252 const char err[] = "No value for variable";
253 logger(NULL, MESHLINK_ERROR, "%s `%s' on line %d while reading config file %s",
254 err, variable, lineno, fname);
259 cfg->variable = xstrdup(variable);
260 cfg->value = xstrdup(value);
261 cfg->file = xstrdup(fname);
268 Parse a configuration file and put the results in the configuration tree
271 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
273 char buffer[MAX_STRING_SIZE];
280 fp = fopen(fname, "r");
283 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
288 line = readline(fp, buffer, sizeof buffer);
298 if(!*line || *line == '#')
302 if(!strncmp(line, "-----END", 8))
307 if(!strncmp(line, "-----BEGIN", 10)) {
312 cfg = parse_config_line(line, fname, lineno);
315 config_add(config_tree, cfg);
323 bool write_config_file(const struct splay_tree_t *config_tree, const char *fname)
327 fp = fopen(fname, "w+");
330 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
334 for splay_each(config_t, cnf, config_tree)
336 if(fwrite(cnf->variable, sizeof(char), strlen(cnf->variable), fp) < strlen(cnf->variable)) {
337 logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno));
341 if(fwrite(" = ", sizeof(char), 3, fp) < 3) {
342 logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno));
346 if(fwrite(cnf->value, sizeof(char), strlen(cnf->value), fp) < strlen(cnf->value)) {
347 logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno));
351 if(fwrite("\n", sizeof(char), 1, fp) < 1) {
352 logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno));
362 bool read_server_config(meshlink_handle_t *mesh) {
363 char filename[PATH_MAX];
366 snprintf(filename, PATH_MAX,"%s" SLASH "meshlink.conf", mesh->confbase);
368 x = read_config_file(mesh->config, filename);
371 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", filename, strerror(errno));
376 bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const char *name) {
377 char filename[PATH_MAX];
380 snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
381 x = read_config_file(config_tree, filename);
386 bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *config_tree, const char *name)
388 char filename[PATH_MAX];
390 snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
391 return write_config_file(config_tree, filename);
394 bool change_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
395 char filename[PATH_MAX];
396 char tmpname[PATH_MAX];
397 char buf[MAX_STRING_SIZE];
398 const int keylen = strlen(key);
400 snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
401 snprintf(tmpname, PATH_MAX, "%s.tmp", filename);
403 FILE *in = fopen(filename, "r");
405 // Hm, maybe the file does not exist? Try appending.
406 return append_config_file(mesh, name, key, value);
409 FILE *out = fopen(tmpname, "w");
411 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmpname, strerror(errno));
418 while(readline(in, buf, sizeof buf)) {
420 if(!strncmp(buf, "-----END", 8))
423 if(!strncmp(buf, "-----BEGIN", 10))
427 if(!ignore && !strncmp(buf, key, keylen)) {
428 if(strchr("\t =", buf[keylen])) {
438 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", filename, strerror(errno));
446 fprintf(out, "%s = %s\n", key, value);
449 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmpname, strerror(errno));
457 // We cannot atomically replace files on Windows.
458 char bakname[PATH_MAX];
459 snprintf(bakname, PATH_MAX, "%s.bak", filename);
460 if(rename(tmpname, bakfile) || rename(bakfile, filename)) {
461 rename(bakfile, filename);
463 if(rename(tmpname, filename)) {
465 logger(mesh, MESHLINK_ERROR, "Failed to update `%s': %s", filename, strerror(errno));
472 bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
473 char filename[PATH_MAX];
474 snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
476 FILE *fp = fopen(filename, "a");
479 logger(mesh, MESHLINK_ERROR, "Cannot open config file %s: %s", filename, strerror(errno));
481 fprintf(fp, "%s = %s\n", key, value);