X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fconf.c;h=90bd3697b9f0ae46b2af7fc63d34a6ec1a8e2262;hb=0871c3095151bce6a4031a2662aa51b7193b855c;hp=e789f72c1ab447874ae4d74aea4ac9ad36373c9c;hpb=f6e87ab476a0faf8b124ecaaa27f967d825e6457;p=tinc diff --git a/src/conf.c b/src/conf.c index e789f72c..90bd3697 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-2021 Guus Sliepen 2013 Florent Clairambault This program is free software; you can redistribute it and/or modify @@ -32,14 +32,20 @@ #include "names.h" #include "netutl.h" /* for str2address */ #include "protocol.h" -#include "utils.h" /* for cp */ #include "xalloc.h" -splay_tree_t *config_tree; +splay_tree_t *config_tree = NULL; 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; @@ -80,18 +86,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(cfg->value); + free(cfg->file); free(cfg); } @@ -202,35 +199,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 +210,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 +234,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 +243,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 "); @@ -313,7 +280,7 @@ config_t *parse_config_line(char *line, const char *fname, int lineno) { Parse a configuration file and put the results in the configuration tree starting at *base. */ -bool read_config_file(splay_tree_t *config_tree, const char *fname) { +bool read_config_file(splay_tree_t *config_tree, const char *fname, bool verbose) { FILE *fp; char buffer[MAX_STRING_SIZE]; char *line; @@ -325,7 +292,7 @@ bool read_config_file(splay_tree_t *config_tree, const char *fname) { fp = fopen(fname, "r"); if(!fp) { - logger(DEBUG_ALWAYS, LOG_DEBUG, "Cannot open config file %s: %s", fname, strerror(errno)); + logger(verbose ? DEBUG_ALWAYS : DEBUG_CONNECTIONS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno)); return false; } @@ -376,7 +343,7 @@ bool read_config_file(splay_tree_t *config_tree, const char *fname) { 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 +374,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; @@ -415,7 +382,7 @@ bool read_server_config(void) { snprintf(fname, sizeof(fname), "%s" SLASH "tinc.conf", confbase); errno = 0; - x = read_config_file(config_tree, fname); + x = read_config_file(config_tree, fname, true); // We will try to read the conf files in the "conf.d" dir if(x) { @@ -433,8 +400,12 @@ 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); - x = read_config_file(config_tree, fname); + 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); } } @@ -449,16 +420,12 @@ bool read_server_config(void) { return x; } -bool read_host_config(splay_tree_t *config_tree, const char *name) { - char fname[PATH_MAX]; - bool x; - +bool read_host_config(splay_tree_t *config_tree, const char *name, bool verbose) { read_config_options(config_tree, name); + char fname[PATH_MAX]; snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, name); - x = read_config_file(config_tree, fname); - - return x; + return read_config_file(config_tree, fname, verbose); } bool append_config_file(const char *name, const char *key, const char *value) {