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.
23 #include "splay_tree.h"
24 #include "connection.h"
28 #include "meshlink_internal.h"
29 #include "netutl.h" /* for str2address */
31 #include "utils.h" /* for cp */
34 static int config_compare(const config_t *a, const config_t *b) {
37 result = strcasecmp(a->variable, b->variable);
43 result = a->line - b->line;
48 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) {
58 splay_delete_tree(*config_tree);
64 config_t *new_config(void) {
65 return xzalloc(sizeof(config_t));
68 void free_config(config_t *cfg) {
84 void config_add(splay_tree_t *config_tree, config_t *cfg) {
85 splay_insert(config_tree, cfg);
88 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
91 cfg.variable = variable;
95 found = splay_search_closest_greater(config_tree, &cfg);
101 if(strcasecmp(found->variable, variable)) {
108 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
112 node = splay_search_node(config_tree, cfg);
116 found = node->next->data;
118 if(!strcasecmp(found->variable, cfg->variable)) {
127 bool get_config_bool(const config_t *cfg, bool *result) {
132 if(!strcasecmp(cfg->value, "yes")) {
135 } else if(!strcasecmp(cfg->value, "no")) {
140 logger(NULL, MESHLINK_ERROR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
141 cfg->variable, cfg->file, cfg->line);
146 bool get_config_int(const config_t *cfg, int *result) {
151 if(sscanf(cfg->value, "%d", result) == 1) {
155 logger(NULL, MESHLINK_ERROR, "Integer expected for configuration variable %s in %s line %d",
156 cfg->variable, cfg->file, cfg->line);
161 bool set_config_int(config_t *cfg, int val) {
167 snprintf(val_str, sizeof(val_str), "%d", val);
173 cfg->value = xstrdup(val_str);
178 bool get_config_string(const config_t *cfg, char **result) {
183 *result = xstrdup(cfg->value);
188 bool set_config_string(config_t *cfg, const char *val) {
197 cfg->value = xstrdup(val);
202 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
209 ai = str2addrinfo(cfg->value, NULL, 0);
216 logger(NULL, MESHLINK_ERROR, "Hostname or IP address expected for configuration variable %s in %s line %d",
217 cfg->variable, cfg->file, cfg->line);
223 Read exactly one line and strip the trailing newline if any.
225 static char *readline(FILE *fp, char *buf, size_t buflen) {
226 char *newline = NULL;
233 p = fgets(buf, buflen, fp);
239 newline = strchr(p, '\n');
245 /* kill newline and carriage return if necessary */
248 if(newline > p && newline[-1] == '\r') {
255 config_t *parse_config_line(char *line, const char *fname, int lineno) {
258 char *variable, *value, *eol;
259 variable = value = line;
261 eol = line + strlen(line);
263 while(strchr("\t ", *--eol)) {
267 len = strcspn(value, "\t =");
269 value += strspn(value, "\t ");
273 value += strspn(value, "\t ");
276 variable[len] = '\0';
279 const char err[] = "No value for variable";
280 logger(NULL, MESHLINK_ERROR, "%s `%s' on line %d while reading config file %s",
281 err, variable, lineno, fname);
286 cfg->variable = xstrdup(variable);
287 cfg->value = xstrdup(value);
288 cfg->file = xstrdup(fname);
295 Parse a configuration file and put the results in the configuration tree
298 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
300 char buffer[MAX_STRING_SIZE];
307 fp = fopen(fname, "r");
310 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
315 line = readline(fp, buffer, sizeof(buffer));
327 if(!*line || *line == '#') {
332 if(!strncmp(line, "-----END", 8)) {
339 if(!strncmp(line, "-----BEGIN", 10)) {
344 cfg = parse_config_line(line, fname, lineno);
350 config_add(config_tree, cfg);
358 bool write_config_file(const struct splay_tree_t *config_tree, const char *fname) {
361 fp = fopen(fname, "w+");
364 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
368 for splay_each(config_t, cnf, config_tree) {
369 if(fwrite(cnf->variable, sizeof(char), strlen(cnf->variable), fp) < strlen(cnf->variable)) {
373 if(fwrite(" = ", sizeof(char), 3, fp) < 3) {
377 if(fwrite(cnf->value, sizeof(char), strlen(cnf->value), fp) < strlen(cnf->value)) {
381 if(fwrite("\n", sizeof(char), 1, fp) < 1) {
390 logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno));
395 bool read_server_config(meshlink_handle_t *mesh) {
396 char filename[PATH_MAX];
399 snprintf(filename, PATH_MAX, "%s" SLASH "meshlink.conf", mesh->confbase);
401 x = read_config_file(mesh->config, filename);
404 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", filename, strerror(errno));
410 bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const char *name) {
411 char filename[PATH_MAX];
414 snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
415 x = read_config_file(config_tree, filename);
420 bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *config_tree, const char *name) {
421 char filename[PATH_MAX];
423 snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
424 return write_config_file(config_tree, filename);
427 bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, int trim) {
428 assert(mesh && name && key);
430 char filename[PATH_MAX];
431 char tmpname[PATH_MAX];
434 if(snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name) >= PATH_MAX) {
435 logger(mesh, MESHLINK_ERROR, "Filename too long: %s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
439 if(snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename) >= PATH_MAX) {
440 logger(mesh, MESHLINK_ERROR, "Filename too long: %s.tmp", filename);
444 FILE *fr = fopen(filename, "r");
447 logger(mesh, MESHLINK_ERROR, "Cannot open config file %s: %s", filename, strerror(errno));
451 FILE *fw = fopen(tmpname, "w");
454 logger(mesh, MESHLINK_ERROR, "Cannot open temporary file %s: %s", tmpname, strerror(errno));
464 fprintf(fw, "%s = %s\n", key, value);
468 while(readline(fr, buf, sizeof(buf))) {
469 if(!*buf || *buf == '#') {
473 sep = strchr(buf, ' ');
481 if(strcmp(buf, key)) {
486 // We found the key and the value. We already added it at the top, so ignore this one.
487 if(value && sep[1] == '=' && sep[2] == ' ' && !strcmp(sep + 3, value)) {
491 // We found the key but with a different value, delete it if wanted.
494 if((!value || trim) && found > trim) {
501 fprintf(fw, "%s\n", buf);
518 // If any error occured during reading or writing, exit.
524 // Try to atomically replace the old config file with the new one.
526 char bakname[PATH_MAX];
527 snprintf(bakname, sizeof(bakname), "%s.bak", filename);
529 if(rename(filename, bakname) || rename(tmpname, filename)) {
530 rename(bakname, filename);
533 if(rename(tmpname, filename)) {
544 bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
545 return modify_config_file(mesh, name, key, value, 0);