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.
25 #include "splay_tree.h"
28 #include "netutl.h" /* for str2address */
30 #include "utils.h" /* for cp */
33 splay_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 */
40 static int config_compare(const config_t *a, const config_t *b) {
43 result = strcasecmp(a->variable, b->variable);
48 result = a->line - b->line;
53 return strcmp(a->file, b->file);
56 void init_configuration(splay_tree_t ** config_tree) {
57 *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
60 void exit_configuration(splay_tree_t ** config_tree) {
61 splay_delete_tree(*config_tree);
65 config_t *new_config(void) {
66 return xmalloc_and_zero(sizeof(config_t));
69 void free_config(config_t *cfg) {
82 void config_add(splay_tree_t *config_tree, config_t *cfg) {
83 splay_insert(config_tree, cfg);
86 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
89 cfg.variable = variable;
93 found = splay_search_closest_greater(config_tree, &cfg);
98 if(strcasecmp(found->variable, variable))
104 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
108 node = splay_search_node(config_tree, cfg);
112 found = node->next->data;
114 if(!strcasecmp(found->variable, cfg->variable))
122 bool get_config_bool(const config_t *cfg, bool *result) {
126 if(!strcasecmp(cfg->value, "yes")) {
129 } else if(!strcasecmp(cfg->value, "no")) {
134 logger(LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
135 cfg->variable, cfg->file, cfg->line);
140 bool get_config_int(const config_t *cfg, int *result) {
144 if(sscanf(cfg->value, "%d", result) == 1)
147 logger(LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
148 cfg->variable, cfg->file, cfg->line);
153 bool get_config_string(const config_t *cfg, char **result) {
157 *result = xstrdup(cfg->value);
162 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
168 ai = str2addrinfo(cfg->value, NULL, 0);
175 logger(LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
176 cfg->variable, cfg->file, cfg->line);
181 bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
182 subnet_t subnet = {0};
187 if(!str2net(&subnet, cfg->value)) {
188 logger(LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
189 cfg->variable, cfg->file, cfg->line);
193 /* Teach newbies what subnets are... */
195 if(((subnet.type == SUBNET_IPV4)
196 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof subnet.net.ipv4.address))
197 || ((subnet.type == SUBNET_IPV6)
198 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address))) {
199 logger(LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
200 cfg->variable, cfg->file, cfg->line);
204 *(*result = new_subnet()) = subnet;
210 Read exactly one line and strip the trailing newline if any.
212 static char *readline(FILE * fp, char *buf, size_t buflen) {
213 char *newline = NULL;
219 p = fgets(buf, buflen, fp);
224 newline = strchr(p, '\n');
229 *newline = '\0'; /* kill newline */
230 if(newline > p && newline[-1] == '\r') /* and carriage return if necessary */
237 Parse a configuration file and put the results in the configuration tree
240 int read_config_file(splay_tree_t *config_tree, const char *fname) {
242 char buffer[MAX_STRING_SIZE];
244 char *variable, *value, *eol;
251 fp = fopen(fname, "r");
254 logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
259 line = readline(fp, buffer, sizeof buffer);
269 if(!*line || *line == '#')
273 if(!strncmp(line, "-----END", 8))
278 if(!strncmp(line, "-----BEGIN", 10)) {
283 variable = value = line;
285 eol = line + strlen(line);
286 while(strchr("\t ", *--eol))
289 len = strcspn(value, "\t =");
291 value += strspn(value, "\t ");
294 value += strspn(value, "\t ");
296 variable[len] = '\0';
300 logger(LOG_ERR, "No value for variable `%s' on line %d while reading config file %s",
301 variable, lineno, fname);
306 cfg->variable = xstrdup(variable);
307 cfg->value = xstrdup(value);
308 cfg->file = xstrdup(fname);
311 config_add(config_tree, cfg);
319 bool read_server_config() {
323 xasprintf(&fname, "%s/tinc.conf", confbase);
324 x = read_config_file(config_tree, fname);
326 if(!x) { /* System error: complain */
327 logger(LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
335 bool disable_old_keys(FILE *f) {
338 bool disabled = false;
343 while(fgets(buf, sizeof buf, f)) {
344 if(!strncmp(buf, "-----BEGIN RSA", 14)) {
348 fseek(f, pos, SEEK_SET);
352 else if(!strncmp(buf, "-----END RSA", 12)) {
356 fseek(f, pos, SEEK_SET);