X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fconf.c;h=a33bdfea3b0b0624ef5681e79924582e20568b2b;hb=refs%2Fheads%2F1.1;hp=5304ab5ea3085f1f860ff4cacbd8bf845ec49156;hpb=6123ed30992d671b94fc016660086be6a62a3871;p=tinc diff --git a/src/conf.c b/src/conf.c index 5304ab5e..d4c76ec8 100644 --- a/src/conf.c +++ b/src/conf.c @@ -4,7 +4,7 @@ 1998-2005 Ivo Timmermans 2000 Cris van Pelt 2010-2011 Julien Muchembled - 2000-2015 Guus Sliepen + 2000-2022 Guus Sliepen 2013 Florent Clairambault This program is free software; you can redistribute it and/or modify @@ -32,14 +32,18 @@ #include "names.h" #include "netutl.h" /* for str2address */ #include "protocol.h" -#include "utils.h" /* for cp */ #include "xalloc.h" -splay_tree_t *config_tree; - int pinginterval = 0; /* seconds between pings */ int pingtimeout = 0; /* seconds to wait for response */ -list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */ + +/* global/host configuration values given at the command line */ +list_t cmdline_conf = { + .head = NULL, + .tail = NULL, + .count = 0, + .delete = (list_action_t)free_config, +}; static int config_compare(const config_t *a, const config_t *b) { int result; @@ -66,13 +70,25 @@ static int config_compare(const config_t *a, const config_t *b) { } } -void init_configuration(splay_tree_t **config_tree) { - *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config); +splay_tree_t config_tree = { + .compare = (splay_compare_t) config_compare, + .delete = (splay_action_t) free_config, +}; + +splay_tree_t *create_configuration(void) { + splay_tree_t *tree = splay_alloc_tree(NULL, NULL); + init_configuration(tree); + return tree; +} + +void init_configuration(splay_tree_t *tree) { + memset(tree, 0, sizeof(*tree)); + tree->compare = (splay_compare_t) config_compare; + tree->delete = (splay_action_t) free_config; } -void exit_configuration(splay_tree_t **config_tree) { - splay_delete_tree(*config_tree); - *config_tree = NULL; +void exit_configuration(splay_tree_t *config_tree) { + splay_delete_tree(config_tree); } config_t *new_config(void) { @@ -80,18 +96,9 @@ config_t *new_config(void) { } void free_config(config_t *cfg) { - if(cfg->variable) { - free(cfg->variable); - } - - if(cfg->value) { - free(cfg->value); - } - - if(cfg->file) { - free(cfg->file); - } - + free(cfg->variable); + free_string(cfg->value); + free(cfg->file); free(cfg); } @@ -99,14 +106,14 @@ void config_add(splay_tree_t *config_tree, config_t *cfg) { splay_insert(config_tree, cfg); } -config_t *lookup_config(splay_tree_t *config_tree, char *variable) { - config_t cfg, *found; - - cfg.variable = variable; - cfg.file = NULL; - cfg.line = 0; +config_t *lookup_config(splay_tree_t *config_tree, const char *variable) { + const config_t cfg = { + .variable = (char *)variable, + .file = NULL, + .line = 0, + }; - found = splay_search_closest_greater(config_tree, &cfg); + config_t *found = splay_search_closest_greater(config_tree, &cfg); if(!found) { return NULL; @@ -202,35 +209,6 @@ bool get_config_address(const config_t *cfg, struct addrinfo **result) { return false; } -bool get_config_subnet(const config_t *cfg, subnet_t **result) { - subnet_t subnet = {NULL}; - - if(!cfg) { - return false; - } - - if(!str2net(&subnet, cfg->value)) { - logger(DEBUG_ALWAYS, LOG_ERR, "Subnet expected for configuration variable %s in %s line %d", - cfg->variable, cfg->file, cfg->line); - return false; - } - - /* Teach newbies what subnets are... */ - - if(((subnet.type == SUBNET_IPV4) - && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(subnet.net.ipv4.address))) - || ((subnet.type == SUBNET_IPV6) - && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(subnet.net.ipv6.address)))) { - logger(DEBUG_ALWAYS, LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d", - cfg->variable, cfg->file, cfg->line); - return false; - } - - *(*result = new_subnet()) = subnet; - - return true; -} - /* Read exactly one line and strip the trailing newline if any. */ @@ -242,7 +220,7 @@ static char *readline(FILE *fp, char *buf, size_t buflen) { return NULL; } - p = fgets(buf, buflen, fp); + p = fgets(buf, (int) buflen, fp); if(!p) { return NULL; @@ -266,7 +244,6 @@ static char *readline(FILE *fp, char *buf, size_t buflen) { config_t *parse_config_line(char *line, const char *fname, int lineno) { config_t *cfg; - int len; char *variable, *value, *eol; variable = value = line; @@ -276,7 +253,7 @@ config_t *parse_config_line(char *line, const char *fname, int lineno) { *eol = '\0'; } - len = strcspn(value, "\t ="); + size_t len = strcspn(value, "\t ="); value += len; value += strspn(value, "\t "); @@ -376,7 +353,7 @@ bool read_config_file(splay_tree_t *config_tree, const char *fname, bool verbose void read_config_options(splay_tree_t *config_tree, const char *prefix) { size_t prefix_len = prefix ? strlen(prefix) : 0; - for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) { + for(const list_node_t *node = cmdline_conf.tail; node; node = node->prev) { const config_t *cfg = node->data; config_t *new; @@ -407,7 +384,7 @@ void read_config_options(splay_tree_t *config_tree, const char *prefix) { } } -bool read_server_config(void) { +bool read_server_config(splay_tree_t *config_tree) { char fname[PATH_MAX]; bool x; @@ -433,7 +410,11 @@ bool read_server_config(void) { // And we try to read the ones that end with ".conf" if(l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) { - snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name); + if((size_t)snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name) >= sizeof(fname)) { + logger(DEBUG_ALWAYS, LOG_ERR, "Pathname too long: %s/%s", dname, ep->d_name); + return false; + } + x = read_config_file(config_tree, fname, true); } }