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.
28 #include "netutl.h" /* for str2address */
30 #include "utils.h" /* for cp */
33 avl_tree_t *config_tree;
35 int pinginterval = 0; /* seconds between pings */
36 int pingtimeout = 0; /* seconds to wait for response */
37 char *confbase = NULL; /* directory in which all config files are */
38 char *netname = NULL; /* name of the vpn network */
39 list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
42 static int config_compare(const config_t *a, const config_t *b) {
45 result = strcasecmp(a->variable, b->variable);
50 /* give priority to command line options */
51 result = !b->file - !a->file;
55 result = a->line - b->line;
60 return a->file ? strcmp(a->file, b->file) : 0;
63 void init_configuration(avl_tree_t ** config_tree) {
64 *config_tree = avl_alloc_tree((avl_compare_t) config_compare, (avl_action_t) free_config);
67 void exit_configuration(avl_tree_t ** config_tree) {
68 avl_delete_tree(*config_tree);
72 config_t *new_config(void) {
73 return xmalloc_and_zero(sizeof(config_t));
76 void free_config(config_t *cfg) {
89 void config_add(avl_tree_t *config_tree, config_t *cfg) {
90 avl_insert(config_tree, cfg);
93 config_t *lookup_config(avl_tree_t *config_tree, char *variable) {
96 cfg.variable = variable;
100 found = avl_search_closest_greater(config_tree, &cfg);
105 if(strcasecmp(found->variable, variable))
111 config_t *lookup_config_next(avl_tree_t *config_tree, const config_t *cfg) {
115 node = avl_search_node(config_tree, cfg);
119 found = node->next->data;
121 if(!strcasecmp(found->variable, cfg->variable))
129 bool get_config_bool(const config_t *cfg, bool *result) {
133 if(!strcasecmp(cfg->value, "yes")) {
136 } else if(!strcasecmp(cfg->value, "no")) {
141 logger(LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
142 cfg->variable, cfg->file, cfg->line);
147 bool get_config_int(const config_t *cfg, int *result) {
151 if(sscanf(cfg->value, "%d", result) == 1)
154 logger(LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
155 cfg->variable, cfg->file, cfg->line);
160 bool get_config_string(const config_t *cfg, char **result) {
164 *result = xstrdup(cfg->value);
169 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
175 ai = str2addrinfo(cfg->value, NULL, 0);
182 logger(LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
183 cfg->variable, cfg->file, cfg->line);
188 bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
189 subnet_t subnet = {0};
194 if(!str2net(&subnet, cfg->value)) {
195 logger(LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
196 cfg->variable, cfg->file, cfg->line);
200 /* Teach newbies what subnets are... */
202 if(((subnet.type == SUBNET_IPV4)
203 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t)))
204 || ((subnet.type == SUBNET_IPV6)
205 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t)))) {
206 logger(LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
207 cfg->variable, cfg->file, cfg->line);
211 *(*result = new_subnet()) = subnet;
217 Read exactly one line and strip the trailing newline if any.
219 static char *readline(FILE * fp, char *buf, size_t buflen) {
220 char *newline = NULL;
226 p = fgets(buf, buflen, fp);
231 newline = strchr(p, '\n');
236 *newline = '\0'; /* kill newline */
237 if(newline > p && newline[-1] == '\r') /* and carriage return if necessary */
243 config_t *parse_config_line(char *line, const char *fname, int lineno) {
246 char *variable, *value, *eol;
247 variable = value = line;
249 eol = line + strlen(line);
250 while(strchr("\t ", *--eol))
253 len = strcspn(value, "\t =");
255 value += strspn(value, "\t ");
258 value += strspn(value, "\t ");
260 variable[len] = '\0';
263 const char err[] = "No value for variable";
265 logger(LOG_ERR, "%s `%s' on line %d while reading config file %s",
266 err, variable, lineno, fname);
268 logger(LOG_ERR, "%s `%s' in command line option %d",
269 err, variable, lineno);
274 cfg->variable = xstrdup(variable);
275 cfg->value = xstrdup(value);
276 cfg->file = fname ? xstrdup(fname) : NULL;
283 Parse a configuration file and put the results in the configuration tree
286 bool read_config_file(avl_tree_t *config_tree, const char *fname) {
288 char buffer[MAX_STRING_SIZE];
295 fp = fopen(fname, "r");
298 logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
303 line = readline(fp, buffer, sizeof buffer);
313 if(!*line || *line == '#')
317 if(!strncmp(line, "-----END", 8))
322 if(!strncmp(line, "-----BEGIN", 10)) {
327 cfg = parse_config_line(line, fname, lineno);
330 config_add(config_tree, cfg);
338 bool read_server_config() {
339 list_node_t *node, *next;
343 for(node = cmdline_conf->tail; node; node = next) {
344 config_t *cfg = (config_t *)node->data;
346 if (!strchr(cfg->variable, '.')) {
347 config_add(config_tree, cfg);
349 list_unlink_node(cmdline_conf, node);
353 xasprintf(&fname, "%s/tinc.conf", confbase);
354 x = read_config_file(config_tree, fname);
356 if(!x) { /* System error: complain */
357 logger(LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
365 FILE *ask_and_open(const char *filename, const char *what) {
371 /* Check stdin and stdout */
372 if(!isatty(0) || !isatty(1)) {
373 /* Argh, they are running us from a script or something. Write
374 the files to the current directory and let them burn in hell
378 /* Ask for a file and/or directory name. */
379 fprintf(stdout, "Please enter a file to save %s to [%s]: ",
383 fn = readline(stdin, line, sizeof line);
386 fprintf(stderr, "Error while reading stdin: %s\n",
392 /* User just pressed enter. */
397 if(fn[0] != '\\' && fn[0] != '/' && !strchr(fn, ':')) {
401 /* The directory is a relative path or a filename. */
404 directory = get_current_dir_name();
405 xasprintf(&p, "%s/%s", directory, fn);
410 umask(0077); /* Disallow everything for group and other */
412 /* Open it first to keep the inode busy */
414 r = fopen(fn, "r+") ?: fopen(fn, "w+");
417 fprintf(stderr, "Error opening file `%s': %s\n",
418 fn, strerror(errno));
425 bool disable_old_keys(FILE *f) {
428 bool disabled = false;
433 while(fgets(buf, sizeof buf, f)) {
434 if(!strncmp(buf, "-----BEGIN RSA", 14)) {
438 fseek(f, pos, SEEK_SET);
442 else if(!strncmp(buf, "-----END RSA", 12)) {
446 fseek(f, pos, SEEK_SET);