2 conf.c -- configuration code
3 Copyright (C) 1998 Robert van der Meulen
4 1998-2005 Ivo Timmermans
5 2000-2014 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"
31 #include "netutl.h" /* for str2address */
33 #include "utils.h" /* for cp */
36 avl_tree_t *config_tree;
38 int pinginterval = 0; /* seconds between pings */
39 int pingtimeout = 0; /* seconds to wait for response */
40 char *confbase = NULL; /* directory in which all config files are */
41 char *netname = NULL; /* name of the vpn network */
42 list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
45 static int config_compare(const config_t *a, const config_t *b) {
48 result = strcasecmp(a->variable, b->variable);
54 /* give priority to command line options */
55 result = !b->file - !a->file;
61 result = a->line - b->line;
66 return a->file ? strcmp(a->file, b->file) : 0;
70 void init_configuration(avl_tree_t **config_tree) {
71 *config_tree = avl_alloc_tree((avl_compare_t) config_compare, (avl_action_t) free_config);
74 void exit_configuration(avl_tree_t **config_tree) {
75 avl_delete_tree(*config_tree);
79 config_t *new_config(void) {
80 return xmalloc_and_zero(sizeof(config_t));
83 void free_config(config_t *cfg) {
90 void config_add(avl_tree_t *config_tree, config_t *cfg) {
91 avl_insert(config_tree, cfg);
94 config_t *lookup_config(const avl_tree_t *config_tree, char *variable) {
97 cfg.variable = variable;
101 found = avl_search_closest_greater(config_tree, &cfg);
107 if(strcasecmp(found->variable, variable)) {
114 config_t *lookup_config_next(const avl_tree_t *config_tree, const config_t *cfg) {
118 node = avl_search_node(config_tree, cfg);
122 found = node->next->data;
124 if(!strcasecmp(found->variable, cfg->variable)) {
133 bool get_config_bool(const config_t *cfg, bool *result) {
138 if(!strcasecmp(cfg->value, "yes")) {
141 } else if(!strcasecmp(cfg->value, "no")) {
146 logger(LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
147 cfg->variable, cfg->file, cfg->line);
152 bool get_config_int(const config_t *cfg, int *result) {
157 if(sscanf(cfg->value, "%d", result) == 1) {
161 logger(LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
162 cfg->variable, cfg->file, cfg->line);
167 bool get_config_string(const config_t *cfg, char **result) {
172 *result = xstrdup(cfg->value);
177 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
184 ai = str2addrinfo(cfg->value, NULL, 0);
191 logger(LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
192 cfg->variable, cfg->file, cfg->line);
197 bool get_config_subnet(const config_t *cfg, subnet_t **result) {
198 subnet_t subnet = {0};
204 if(!str2net(&subnet, cfg->value)) {
205 logger(LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
206 cfg->variable, cfg->file, cfg->line);
210 /* Teach newbies what subnets are... */
212 if(((subnet.type == SUBNET_IPV4)
213 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t)))
214 || ((subnet.type == SUBNET_IPV6)
215 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t)))) {
216 logger(LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
217 cfg->variable, cfg->file, cfg->line);
221 *(*result = new_subnet()) = subnet;
227 Read exactly one line and strip the trailing newline if any.
229 static char *readline(FILE *fp, char *buf, size_t buflen) {
230 char *newline = NULL;
237 p = fgets(buf, buflen, fp);
243 newline = strchr(p, '\n');
249 *newline = '\0'; /* kill newline */
251 if(newline > p && newline[-1] == '\r') { /* and carriage return if necessary */
258 config_t *parse_config_line(char *line, const char *fname, int lineno) {
261 char *variable, *value, *eol;
262 variable = value = line;
264 eol = line + strlen(line);
266 while(strchr("\t ", *--eol)) {
270 len = strcspn(value, "\t =");
272 value += strspn(value, "\t ");
276 value += strspn(value, "\t ");
279 variable[len] = '\0';
282 const char err[] = "No value for variable";
285 logger(LOG_ERR, "%s `%s' on line %d while reading config file %s",
286 err, variable, lineno, fname);
288 logger(LOG_ERR, "%s `%s' in command line option %d",
289 err, variable, lineno);
295 cfg->variable = xstrdup(variable);
296 cfg->value = xstrdup(value);
297 cfg->file = fname ? xstrdup(fname) : NULL;
304 Parse a configuration file and put the results in the configuration tree
307 bool read_config_file(avl_tree_t *config_tree, const char *fname) {
309 char buffer[MAX_STRING_SIZE];
316 fp = fopen(fname, "r");
319 logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
324 line = readline(fp, buffer, sizeof(buffer));
336 if(!*line || *line == '#') {
341 if(!strncmp(line, "-----END", 8)) {
348 if(!strncmp(line, "-----BEGIN", 10)) {
353 cfg = parse_config_line(line, fname, lineno);
359 config_add(config_tree, cfg);
367 void read_config_options(avl_tree_t *config_tree, const char *prefix) {
368 size_t prefix_len = prefix ? strlen(prefix) : 0;
370 for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) {
371 const config_t *cfg = node->data;
374 if(strchr(cfg->variable, '.')) {
378 if(strncmp(prefix, cfg->variable, prefix_len) ||
379 cfg->variable[prefix_len] != '.') {
384 config_t *new = new_config();
387 new->variable = xstrdup(cfg->variable + prefix_len + 1);
389 new->variable = xstrdup(cfg->variable);
392 new->value = xstrdup(cfg->value);
394 new->line = cfg->line;
396 config_add(config_tree, new);
400 bool read_server_config(void) {
401 char fname[PATH_MAX];
404 read_config_options(config_tree, NULL);
406 snprintf(fname, sizeof(fname), "%s/tinc.conf", confbase);
408 x = read_config_file(config_tree, fname);
410 // We will try to read the conf files in the "conf.d" dir
412 char dname[PATH_MAX];
413 snprintf(dname, sizeof(dname), "%s/conf.d", confbase);
414 DIR *dir = opendir(dname);
416 // If we can find this dir
420 // We list all the files in it
421 while(x && (ep = readdir(dir))) {
422 size_t l = strlen(ep->d_name);
424 // And we try to read the ones that end with ".conf"
425 if(l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
426 if((size_t)snprintf(fname, sizeof(fname), "%s/%s", dname, ep->d_name) >= sizeof(fname)) {
427 logger(LOG_ERR, "Pathname too long: %s/%s", dname, ep->d_name);
431 x = read_config_file(config_tree, fname);
440 logger(LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
446 bool read_connection_config(connection_t *c) {
447 char fname[PATH_MAX];
450 read_config_options(c->config_tree, c->name);
452 snprintf(fname, sizeof(fname), "%s/hosts/%s", confbase, c->name);
453 x = read_config_file(c->config_tree, fname);
458 static void disable_old_keys(const char *filename) {
459 char tmpfile[PATH_MAX] = "";
461 bool disabled = false;
464 r = fopen(filename, "r");
470 snprintf(tmpfile, sizeof(tmpfile), "%s.tmp", filename);
472 w = fopen(tmpfile, "w");
474 while(fgets(buf, sizeof(buf), r)) {
475 if(!strncmp(buf, "-----BEGIN RSA", 14)) {
480 } else if(!strncmp(buf, "-----END RSA", 12)) {
487 if(w && fputs(buf, w) < 0) {
500 fprintf(stderr, "Warning: old key(s) found, remove them by hand!\n");
506 // We cannot atomically replace files on Windows.
507 char bakfile[PATH_MAX] = "";
508 snprintf(bakfile, sizeof(bakfile), "%s.bak", filename);
510 if(rename(filename, bakfile) || rename(tmpfile, filename)) {
511 rename(bakfile, filename);
514 if(rename(tmpfile, filename)) {
516 fprintf(stderr, "Warning: old key(s) found, remove them by hand!\n");
521 fprintf(stderr, "Warning: old key(s) found and disabled.\n");
528 FILE *ask_and_open(const char *filename, const char *what) {
530 char directory[PATH_MAX];
532 char abspath[PATH_MAX];
535 /* Check stdin and stdout */
536 if(!isatty(0) || !isatty(1)) {
537 /* Argh, they are running us from a script or something. Write
538 the files to the current directory and let them burn in hell
542 /* Ask for a file and/or directory name. */
543 fprintf(stdout, "Please enter a file to save %s to [%s]: ",
547 fn = readline(stdin, line, sizeof(line));
550 fprintf(stderr, "Error while reading stdin: %s\n",
556 /* User just pressed enter. */
564 if(fn[0] != '\\' && fn[0] != '/' && !strchr(fn, ':')) {
569 /* The directory is a relative path or a filename. */
570 getcwd(directory, sizeof(directory));
572 if((size_t)snprintf(abspath, sizeof(abspath), "%s/%s", directory, fn) >= sizeof(abspath)) {
573 fprintf(stderr, "Pathname too long: %s/%s\n", directory, fn);
580 umask(0077); /* Disallow everything for group and other */
582 disable_old_keys(fn);
584 /* Open it first to keep the inode busy */
589 fprintf(stderr, "Error opening file `%s': %s\n",
590 fn, strerror(errno));