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-2011 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.
27 #include "connection.h"
30 #include "netutl.h" /* for str2address */
32 #include "utils.h" /* for cp */
35 avl_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(avl_tree_t ** config_tree) {
66 *config_tree = avl_alloc_tree((avl_compare_t) config_compare, (avl_action_t) free_config);
69 void exit_configuration(avl_tree_t ** config_tree) {
70 avl_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(avl_tree_t *config_tree, config_t *cfg) {
92 avl_insert(config_tree, cfg);
95 config_t *lookup_config(avl_tree_t *config_tree, char *variable) {
98 cfg.variable = variable;
102 found = avl_search_closest_greater(config_tree, &cfg);
107 if(strcasecmp(found->variable, variable))
113 config_t *lookup_config_next(avl_tree_t *config_tree, const config_t *cfg) {
117 node = avl_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(ipv4_t)))
206 || ((subnet.type == SUBNET_IPV6)
207 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t)))) {
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(avl_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(avl_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 *orig_cfg, *cfg = (config_t *)node->data;
349 if(strchr(cfg->variable, '.'))
352 list_unlink_node(cmdline_conf, node);
354 if(strncmp(prefix, cfg->variable, prefix_len) ||
355 cfg->variable[prefix_len] != '.')
357 /* Because host configuration is parsed again when
358 reconnecting, nodes must not be freed when a prefix
362 cfg->variable = xstrdup(orig_cfg->variable + prefix_len + 1);
363 cfg->value = xstrdup(orig_cfg->value);
365 cfg->line = orig_cfg->line;
367 config_add(config_tree, cfg);
371 bool read_server_config() {
375 read_config_options(config_tree, NULL);
377 xasprintf(&fname, "%s/tinc.conf", confbase);
378 x = read_config_file(config_tree, fname);
380 if(!x) { /* System error: complain */
381 logger(LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
389 bool read_connection_config(connection_t *c) {
393 read_config_options(c->config_tree, c->name);
395 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
396 x = read_config_file(c->config_tree, fname);
402 FILE *ask_and_open(const char *filename, const char *what) {
408 /* Check stdin and stdout */
409 if(!isatty(0) || !isatty(1)) {
410 /* Argh, they are running us from a script or something. Write
411 the files to the current directory and let them burn in hell
415 /* Ask for a file and/or directory name. */
416 fprintf(stdout, "Please enter a file to save %s to [%s]: ",
420 fn = readline(stdin, line, sizeof line);
423 fprintf(stderr, "Error while reading stdin: %s\n",
429 /* User just pressed enter. */
434 if(fn[0] != '\\' && fn[0] != '/' && !strchr(fn, ':')) {
438 /* The directory is a relative path or a filename. */
441 directory = get_current_dir_name();
442 xasprintf(&p, "%s/%s", directory, fn);
447 umask(0077); /* Disallow everything for group and other */
449 /* Open it first to keep the inode busy */
451 r = fopen(fn, "r+") ?: fopen(fn, "w+");
454 fprintf(stderr, "Error opening file `%s': %s\n",
455 fn, strerror(errno));
462 bool disable_old_keys(FILE *f) {
465 bool disabled = false;
473 while(fgets(buf, sizeof buf, f)) {
474 if(!strncmp(buf, "-----BEGIN RSA", 14)) {
478 if(fseek(f, pos, SEEK_SET))
480 if(fputs(buf, f) <= 0)
484 else if(!strncmp(buf, "-----END RSA", 12)) {
488 if(fseek(f, pos, SEEK_SET))
490 if(fputs(buf, f) <= 0)