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);
42 return result = a->line - b->line;
45 void init_configuration(splay_tree_t **config_tree) {
46 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
49 void exit_configuration(splay_tree_t **config_tree) {
51 splay_delete_tree(*config_tree);
57 config_t *new_config(void) {
58 return xzalloc(sizeof(config_t));
61 void free_config(config_t *cfg) {
67 void config_add(splay_tree_t *config_tree, config_t *cfg) {
68 splay_insert(config_tree, cfg);
71 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
74 cfg.variable = variable;
77 found = splay_search_closest_greater(config_tree, &cfg);
83 if(strcasecmp(found->variable, variable)) {
90 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
94 node = splay_search_node(config_tree, cfg);
98 found = node->next->data;
100 if(!strcasecmp(found->variable, cfg->variable)) {
109 bool get_config_bool(const config_t *cfg, bool *result) {
114 if(!strcasecmp(cfg->value, "yes")) {
117 } else if(!strcasecmp(cfg->value, "no")) {
122 logger(NULL, MESHLINK_ERROR, "\"yes\" or \"no\" expected for configuration variable %s in line %d",
123 cfg->variable, cfg->line);
128 bool get_config_int(const config_t *cfg, int *result) {
133 if(sscanf(cfg->value, "%d", result) == 1) {
137 logger(NULL, MESHLINK_ERROR, "Integer expected for configuration variable %s in line %d",
138 cfg->variable, cfg->line);
143 bool set_config_int(config_t *cfg, int val) {
149 snprintf(val_str, sizeof(val_str), "%d", val);
155 cfg->value = xstrdup(val_str);
160 bool get_config_string(const config_t *cfg, char **result) {
165 *result = xstrdup(cfg->value);
170 bool set_config_string(config_t *cfg, const char *val) {
179 cfg->value = xstrdup(val);
184 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 line %d",
199 cfg->variable, 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;
215 p = fgets(buf, buflen, fp);
221 newline = strchr(p, '\n');
227 /* kill newline and carriage return if necessary */
230 if(newline > p && newline[-1] == '\r') {
237 config_t *parse_config_line(char *line, const char *fname, int lineno) {
240 char *variable, *value, *eol;
241 variable = value = line;
243 eol = line + strlen(line);
245 while(strchr("\t ", *--eol)) {
249 len = strcspn(value, "\t =");
251 value += strspn(value, "\t ");
255 value += strspn(value, "\t ");
258 variable[len] = '\0';
261 const char err[] = "No value for variable";
262 logger(NULL, MESHLINK_ERROR, "%s `%s' on line %d while reading config file %s",
263 err, variable, lineno, fname);
268 cfg->variable = xstrdup(variable);
269 cfg->value = xstrdup(value);
276 Parse a configuration file and put the results in the configuration tree
279 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
281 char buffer[MAX_STRING_SIZE];
288 fp = fopen(fname, "r");
291 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
296 line = readline(fp, buffer, sizeof(buffer));
308 if(!*line || *line == '#') {
313 if(!strncmp(line, "-----END", 8)) {
320 if(!strncmp(line, "-----BEGIN", 10)) {
325 cfg = parse_config_line(line, fname, lineno);
331 config_add(config_tree, cfg);
339 bool write_config_file(const struct splay_tree_t *config_tree, const char *fname) {
342 fp = fopen(fname, "w+");
345 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
349 for splay_each(config_t, cnf, config_tree) {
350 if(fwrite(cnf->variable, sizeof(char), strlen(cnf->variable), fp) < strlen(cnf->variable)) {
354 if(fwrite(" = ", sizeof(char), 3, fp) < 3) {
358 if(fwrite(cnf->value, sizeof(char), strlen(cnf->value), fp) < strlen(cnf->value)) {
362 if(fwrite("\n", sizeof(char), 1, fp) < 1) {
371 logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno));
376 bool read_server_config(meshlink_handle_t *mesh) {
377 char filename[PATH_MAX];
380 snprintf(filename, PATH_MAX, "%s" SLASH "meshlink.conf", mesh->confbase);
382 x = read_config_file(mesh->config, filename);
385 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", filename, strerror(errno));
391 bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const char *name) {
392 char filename[PATH_MAX];
395 snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
396 x = read_config_file(config_tree, filename);
401 bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *config_tree, const char *name) {
402 char filename[PATH_MAX];
404 snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
405 return write_config_file(config_tree, filename);
408 bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, int trim) {
409 assert(mesh && name && key);
411 char filename[PATH_MAX];
412 char tmpname[PATH_MAX];
415 if(snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name) >= PATH_MAX) {
416 logger(mesh, MESHLINK_ERROR, "Filename too long: %s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
420 if(snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename) >= PATH_MAX) {
421 logger(mesh, MESHLINK_ERROR, "Filename too long: %s.tmp", filename);
425 FILE *fr = fopen(filename, "r");
428 logger(mesh, MESHLINK_ERROR, "Cannot open config file %s: %s", filename, strerror(errno));
432 FILE *fw = fopen(tmpname, "w");
435 logger(mesh, MESHLINK_ERROR, "Cannot open temporary file %s: %s", tmpname, strerror(errno));
445 fprintf(fw, "%s = %s\n", key, value);
449 while(readline(fr, buf, sizeof(buf))) {
450 if(!*buf || *buf == '#') {
454 sep = strchr(buf, ' ');
462 if(strcmp(buf, key)) {
467 // We found the key and the value. We already added it at the top, so ignore this one.
468 if(value && sep[1] == '=' && sep[2] == ' ' && !strcmp(sep + 3, value)) {
472 // We found the key but with a different value, delete it if wanted.
475 if((!value || trim) && found > trim) {
482 fprintf(fw, "%s\n", buf);
499 // If any error occured during reading or writing, exit.
505 // Try to atomically replace the old config file with the new one.
507 char bakname[PATH_MAX];
508 snprintf(bakname, sizeof(bakname), "%s.bak", filename);
510 if(rename(filename, bakname) || rename(tmpname, filename)) {
511 rename(bakname, filename);
514 if(rename(tmpname, filename)) {
525 bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
526 return modify_config_file(mesh, name, key, value, 0);