2 conf.c -- configuration code
3 Copyright (C) 1998 Robert van der Meulen
4 1998-2005 Ivo Timmermans
5 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "connection.h"
29 #include "netutl.h" /* for str2address */
31 #include "utils.h" /* for cp */
34 avl_tree_t *config_tree;
36 int pinginterval = 0; /* seconds between pings */
37 int pingtimeout = 0; /* seconds to wait for response */
38 char *confbase = NULL; /* directory in which all config files are */
39 char *netname = NULL; /* name of the vpn network */
40 list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
43 static int config_compare(const config_t *a, const config_t *b) {
46 result = strcasecmp(a->variable, b->variable);
51 /* give priority to command line options */
52 result = !b->file - !a->file;
56 result = a->line - b->line;
61 return a->file ? strcmp(a->file, b->file) : 0;
64 void init_configuration(avl_tree_t ** config_tree) {
65 *config_tree = avl_alloc_tree((avl_compare_t) config_compare, (avl_action_t) free_config);
68 void exit_configuration(avl_tree_t ** config_tree) {
69 avl_delete_tree(*config_tree);
73 config_t *new_config(void) {
74 return xmalloc_and_zero(sizeof(config_t));
77 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(avl_tree_t *config_tree, char *variable) {
97 cfg.variable = variable;
101 found = avl_search_closest_greater(config_tree, &cfg);
106 if(strcasecmp(found->variable, variable))
112 config_t *lookup_config_next(avl_tree_t *config_tree, const config_t *cfg) {
116 node = avl_search_node(config_tree, cfg);
120 found = node->next->data;
122 if(!strcasecmp(found->variable, cfg->variable))
130 bool get_config_bool(const config_t *cfg, bool *result) {
134 if(!strcasecmp(cfg->value, "yes")) {
137 } else if(!strcasecmp(cfg->value, "no")) {
142 logger(LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
143 cfg->variable, cfg->file, cfg->line);
148 bool get_config_int(const config_t *cfg, int *result) {
152 if(sscanf(cfg->value, "%d", result) == 1)
155 logger(LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
156 cfg->variable, cfg->file, cfg->line);
161 bool get_config_string(const config_t *cfg, char **result) {
165 *result = xstrdup(cfg->value);
170 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
176 ai = str2addrinfo(cfg->value, NULL, 0);
183 logger(LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
184 cfg->variable, cfg->file, cfg->line);
189 bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
190 subnet_t subnet = {0};
195 if(!str2net(&subnet, cfg->value)) {
196 logger(LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
197 cfg->variable, cfg->file, cfg->line);
201 /* Teach newbies what subnets are... */
203 if(((subnet.type == SUBNET_IPV4)
204 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t)))
205 || ((subnet.type == SUBNET_IPV6)
206 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t)))) {
207 logger(LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
208 cfg->variable, cfg->file, cfg->line);
212 *(*result = new_subnet()) = subnet;
218 Read exactly one line and strip the trailing newline if any.
220 static char *readline(FILE * fp, char *buf, size_t buflen) {
221 char *newline = NULL;
227 p = fgets(buf, buflen, fp);
232 newline = strchr(p, '\n');
237 *newline = '\0'; /* kill newline */
238 if(newline > p && newline[-1] == '\r') /* and carriage return if necessary */
244 config_t *parse_config_line(char *line, const char *fname, int lineno) {
247 char *variable, *value, *eol;
248 variable = value = line;
250 eol = line + strlen(line);
251 while(strchr("\t ", *--eol))
254 len = strcspn(value, "\t =");
256 value += strspn(value, "\t ");
259 value += strspn(value, "\t ");
261 variable[len] = '\0';
264 const char err[] = "No value for variable";
266 logger(LOG_ERR, "%s `%s' on line %d while reading config file %s",
267 err, variable, lineno, fname);
269 logger(LOG_ERR, "%s `%s' in command line option %d",
270 err, variable, lineno);
275 cfg->variable = xstrdup(variable);
276 cfg->value = xstrdup(value);
277 cfg->file = fname ? xstrdup(fname) : NULL;
284 Parse a configuration file and put the results in the configuration tree
287 bool read_config_file(avl_tree_t *config_tree, const char *fname) {
289 char buffer[MAX_STRING_SIZE];
296 fp = fopen(fname, "r");
299 logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
304 line = readline(fp, buffer, sizeof buffer);
314 if(!*line || *line == '#')
318 if(!strncmp(line, "-----END", 8))
323 if(!strncmp(line, "-----BEGIN", 10)) {
328 cfg = parse_config_line(line, fname, lineno);
331 config_add(config_tree, cfg);
339 void read_config_options(avl_tree_t *config_tree, const char *prefix) {
340 list_node_t *node, *next;
341 size_t prefix_len = strlen(prefix);
343 for(node = cmdline_conf->tail; node; node = next) {
344 config_t *cfg = (config_t *)node->data;
347 if(!prefix && strchr(cfg->variable, '.'))
350 if(prefix && (strncmp(prefix, cfg->variable, prefix_len) || cfg->variable[prefix_len] != '.'))
353 config_add(config_tree, cfg);
355 list_unlink_node(cmdline_conf, node);
359 bool read_server_config() {
360 list_node_t *node, *next;
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) {
379 list_node_t *node, *next;
380 size_t name_len = strlen(c->name);
384 read_config_options(c->config_tree, c->name);
386 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
387 x = read_config_file(c->config_tree, fname);
393 FILE *ask_and_open(const char *filename, const char *what) {
399 /* Check stdin and stdout */
400 if(!isatty(0) || !isatty(1)) {
401 /* Argh, they are running us from a script or something. Write
402 the files to the current directory and let them burn in hell
406 /* Ask for a file and/or directory name. */
407 fprintf(stdout, "Please enter a file to save %s to [%s]: ",
411 fn = readline(stdin, line, sizeof line);
414 fprintf(stderr, "Error while reading stdin: %s\n",
420 /* User just pressed enter. */
425 if(fn[0] != '\\' && fn[0] != '/' && !strchr(fn, ':')) {
429 /* The directory is a relative path or a filename. */
432 directory = get_current_dir_name();
433 xasprintf(&p, "%s/%s", directory, fn);
438 umask(0077); /* Disallow everything for group and other */
440 /* Open it first to keep the inode busy */
442 r = fopen(fn, "r+") ?: fopen(fn, "w+");
445 fprintf(stderr, "Error opening file `%s': %s\n",
446 fn, strerror(errno));
453 bool disable_old_keys(FILE *f) {
456 bool disabled = false;
461 while(fgets(buf, sizeof buf, f)) {
462 if(!strncmp(buf, "-----BEGIN RSA", 14)) {
466 fseek(f, pos, SEEK_SET);
470 else if(!strncmp(buf, "-----END RSA", 12)) {
474 fseek(f, pos, SEEK_SET);