2 conf.c -- configuration code
3 Copyright (C) 1998 Robert van der Meulen
4 1998-2005 Ivo Timmermans
5 2000-2010 Guus Sliepen <guus@tinc-vpn.org>
6 2010 Julien Muchembled <jm@jmuchemb.eu>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "splay_tree.h"
27 #include "connection.h"
30 #include "netutl.h" /* for str2address */
32 #include "utils.h" /* for cp */
35 splay_tree_t *config_tree;
37 int pinginterval = 0; /* seconds between pings */
38 int pingtimeout = 0; /* seconds to wait for response */
39 char *confbase = NULL; /* directory in which all config files are */
40 char *netname = NULL; /* name of the vpn network */
41 list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
44 static int config_compare(const config_t *a, const config_t *b) {
47 result = strcasecmp(a->variable, b->variable);
52 /* give priority to command line options */
53 result = !b->file - !a->file;
57 result = a->line - b->line;
62 return a->file ? strcmp(a->file, b->file) : 0;
65 void init_configuration(splay_tree_t ** config_tree) {
66 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
69 void exit_configuration(splay_tree_t ** config_tree) {
70 splay_delete_tree(*config_tree);
74 config_t *new_config(void) {
75 return xmalloc_and_zero(sizeof(config_t));
78 void free_config(config_t *cfg) {
91 void config_add(splay_tree_t *config_tree, config_t *cfg) {
92 splay_insert(config_tree, cfg);
95 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
98 cfg.variable = variable;
102 found = splay_search_closest_greater(config_tree, &cfg);
107 if(strcasecmp(found->variable, variable))
113 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
117 node = splay_search_node(config_tree, cfg);
121 found = node->next->data;
123 if(!strcasecmp(found->variable, cfg->variable))
131 bool get_config_bool(const config_t *cfg, bool *result) {
135 if(!strcasecmp(cfg->value, "yes")) {
138 } else if(!strcasecmp(cfg->value, "no")) {
143 logger(LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
144 cfg->variable, cfg->file, cfg->line);
149 bool get_config_int(const config_t *cfg, int *result) {
153 if(sscanf(cfg->value, "%d", result) == 1)
156 logger(LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
157 cfg->variable, cfg->file, cfg->line);
162 bool get_config_string(const config_t *cfg, char **result) {
166 *result = xstrdup(cfg->value);
171 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
177 ai = str2addrinfo(cfg->value, NULL, 0);
184 logger(LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
185 cfg->variable, cfg->file, cfg->line);
190 bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
191 subnet_t subnet = {0};
196 if(!str2net(&subnet, cfg->value)) {
197 logger(LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
198 cfg->variable, cfg->file, cfg->line);
202 /* Teach newbies what subnets are... */
204 if(((subnet.type == SUBNET_IPV4)
205 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof subnet.net.ipv4.address))
206 || ((subnet.type == SUBNET_IPV6)
207 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address))) {
208 logger(LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
209 cfg->variable, cfg->file, cfg->line);
213 *(*result = new_subnet()) = subnet;
219 Read exactly one line and strip the trailing newline if any.
221 static char *readline(FILE * fp, char *buf, size_t buflen) {
222 char *newline = NULL;
228 p = fgets(buf, buflen, fp);
233 newline = strchr(p, '\n');
238 *newline = '\0'; /* kill newline */
239 if(newline > p && newline[-1] == '\r') /* and carriage return if necessary */
245 config_t *parse_config_line(char *line, const char *fname, int lineno) {
248 char *variable, *value, *eol;
249 variable = value = line;
251 eol = line + strlen(line);
252 while(strchr("\t ", *--eol))
255 len = strcspn(value, "\t =");
257 value += strspn(value, "\t ");
260 value += strspn(value, "\t ");
262 variable[len] = '\0';
265 const char err[] = "No value for variable";
267 logger(LOG_ERR, "%s `%s' on line %d while reading config file %s",
268 err, variable, lineno, fname);
270 logger(LOG_ERR, "%s `%s' in command line option %d",
271 err, variable, lineno);
276 cfg->variable = xstrdup(variable);
277 cfg->value = xstrdup(value);
278 cfg->file = fname ? xstrdup(fname) : NULL;
285 Parse a configuration file and put the results in the configuration tree
288 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
290 char buffer[MAX_STRING_SIZE];
297 fp = fopen(fname, "r");
300 logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
305 line = readline(fp, buffer, sizeof buffer);
315 if(!*line || *line == '#')
319 if(!strncmp(line, "-----END", 8))
324 if(!strncmp(line, "-----BEGIN", 10)) {
329 cfg = parse_config_line(line, fname, lineno);
332 config_add(config_tree, cfg);
340 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
341 list_node_t *node, *next;
342 size_t prefix_len = prefix ? strlen(prefix) : 0;
344 for(node = cmdline_conf->tail; node; node = next) {
345 config_t *cfg = (config_t *)node->data;
348 if(!prefix && strchr(cfg->variable, '.'))
351 if(prefix && (strncmp(prefix, cfg->variable, prefix_len) || cfg->variable[prefix_len] != '.'))
354 config_add(config_tree, cfg);
356 list_unlink_node(cmdline_conf, node);
360 bool read_server_config() {
364 read_config_options(config_tree, NULL);
366 xasprintf(&fname, "%s/tinc.conf", confbase);
367 x = read_config_file(config_tree, fname);
369 if(!x) { /* System error: complain */
370 logger(LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
378 bool read_connection_config(connection_t *c) {
382 read_config_options(c->config_tree, c->name);
384 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
385 x = read_config_file(c->config_tree, fname);
391 bool disable_old_keys(FILE *f) {
394 bool disabled = false;
402 while(fgets(buf, sizeof buf, f)) {
403 if(!strncmp(buf, "-----BEGIN RSA", 14)) {
407 if(fseek(f, pos, SEEK_SET))
409 if(fputs(buf, f) <= 0)
413 else if(!strncmp(buf, "-----END RSA", 12)) {
417 if(fseek(f, pos, SEEK_SET))
419 if(fputs(buf, f) <= 0)