Fix whitespace.
[tinc] / src / conf.c
1 /*
2     conf.c -- configuration code
3     Copyright (C) 1998 Robert van der Meulen
4                   1998-2005 Ivo Timmermans
5                   2000-2012 Guus Sliepen <guus@tinc-vpn.org>
6                   2010-2011 Julien Muchembled <jm@jmuchemb.eu>
7                   2000 Cris van Pelt
8
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.
13
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.
18
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.
22 */
23
24 #include "system.h"
25
26 #include "splay_tree.h"
27 #include "connection.h"
28 #include "conf.h"
29 #include "list.h"
30 #include "logger.h"
31 #include "netutl.h"             /* for str2address */
32 #include "protocol.h"
33 #include "utils.h"              /* for cp */
34 #include "xalloc.h"
35
36 splay_tree_t *config_tree;
37
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 */
43
44
45 static int config_compare(const config_t *a, const config_t *b) {
46         int result;
47
48         result = strcasecmp(a->variable, b->variable);
49
50         if(result)
51                 return result;
52
53         /* give priority to command line options */
54         result = !b->file - !a->file;
55         if (result)
56                 return result;
57
58         result = a->line - b->line;
59
60         if(result)
61                 return result;
62         else
63                 return a->file ? strcmp(a->file, b->file) : 0;
64 }
65
66 void init_configuration(splay_tree_t ** config_tree) {
67         *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
68 }
69
70 void exit_configuration(splay_tree_t ** config_tree) {
71         splay_delete_tree(*config_tree);
72         *config_tree = NULL;
73 }
74
75 config_t *new_config(void) {
76         return xmalloc_and_zero(sizeof(config_t));
77 }
78
79 void free_config(config_t *cfg) {
80         if(cfg->variable)
81                 free(cfg->variable);
82
83         if(cfg->value)
84                 free(cfg->value);
85
86         if(cfg->file)
87                 free(cfg->file);
88
89         free(cfg);
90 }
91
92 void config_add(splay_tree_t *config_tree, config_t *cfg) {
93         splay_insert(config_tree, cfg);
94 }
95
96 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
97         config_t cfg, *found;
98
99         cfg.variable = variable;
100         cfg.file = NULL;
101         cfg.line = 0;
102
103         found = splay_search_closest_greater(config_tree, &cfg);
104
105         if(!found)
106                 return NULL;
107
108         if(strcasecmp(found->variable, variable))
109                 return NULL;
110
111         return found;
112 }
113
114 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
115         splay_node_t *node;
116         config_t *found;
117
118         node = splay_search_node(config_tree, cfg);
119
120         if(node) {
121                 if(node->next) {
122                         found = node->next->data;
123
124                         if(!strcasecmp(found->variable, cfg->variable))
125                                 return found;
126                 }
127         }
128
129         return NULL;
130 }
131
132 bool get_config_bool(const config_t *cfg, bool *result) {
133         if(!cfg)
134                 return false;
135
136         if(!strcasecmp(cfg->value, "yes")) {
137                 *result = true;
138                 return true;
139         } else if(!strcasecmp(cfg->value, "no")) {
140                 *result = false;
141                 return true;
142         }
143
144         logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
145                    cfg->variable, cfg->file, cfg->line);
146
147         return false;
148 }
149
150 bool get_config_int(const config_t *cfg, int *result) {
151         if(!cfg)
152                 return false;
153
154         if(sscanf(cfg->value, "%d", result) == 1)
155                 return true;
156
157         logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
158                    cfg->variable, cfg->file, cfg->line);
159
160         return false;
161 }
162
163 bool get_config_string(const config_t *cfg, char **result) {
164         if(!cfg)
165                 return false;
166
167         *result = xstrdup(cfg->value);
168
169         return true;
170 }
171
172 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
173         struct addrinfo *ai;
174
175         if(!cfg)
176                 return false;
177
178         ai = str2addrinfo(cfg->value, NULL, 0);
179
180         if(ai) {
181                 *result = ai;
182                 return true;
183         }
184
185         logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
186                    cfg->variable, cfg->file, cfg->line);
187
188         return false;
189 }
190
191 bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
192         subnet_t subnet = {NULL};
193
194         if(!cfg)
195                 return false;
196
197         if(!str2net(&subnet, cfg->value)) {
198                 logger(DEBUG_ALWAYS, LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
199                            cfg->variable, cfg->file, cfg->line);
200                 return false;
201         }
202
203         /* Teach newbies what subnets are... */
204
205         if(((subnet.type == SUBNET_IPV4)
206                 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof subnet.net.ipv4.address))
207                 || ((subnet.type == SUBNET_IPV6)
208                 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address))) {
209                 logger(DEBUG_ALWAYS, LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
210                            cfg->variable, cfg->file, cfg->line);
211                 return false;
212         }
213
214         *(*result = new_subnet()) = subnet;
215
216         return true;
217 }
218
219 /*
220   Read exactly one line and strip the trailing newline if any.
221 */
222 static char *readline(FILE * fp, char *buf, size_t buflen) {
223         char *newline = NULL;
224         char *p;
225
226         if(feof(fp))
227                 return NULL;
228
229         p = fgets(buf, buflen, fp);
230
231         if(!p)
232                 return NULL;
233
234         newline = strchr(p, '\n');
235
236         if(!newline)
237                 return buf;
238
239         /* kill newline and carriage return if necessary */
240         *newline = '\0';
241         if(newline > p && newline[-1] == '\r')
242                 newline[-1] = '\0';
243
244         return buf;
245 }
246
247 config_t *parse_config_line(char *line, const char *fname, int lineno) {
248         config_t *cfg;
249         int len;
250         char *variable, *value, *eol;
251         variable = value = line;
252
253         eol = line + strlen(line);
254         while(strchr("\t ", *--eol))
255                 *eol = '\0';
256
257         len = strcspn(value, "\t =");
258         value += len;
259         value += strspn(value, "\t ");
260         if(*value == '=') {
261                 value++;
262                 value += strspn(value, "\t ");
263         }
264         variable[len] = '\0';
265
266         if(!*value) {
267                 const char err[] = "No value for variable";
268                 if (fname)
269                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
270                                 err, variable, lineno, fname);
271                 else
272                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
273                                 err, variable, lineno);
274                 return NULL;
275         }
276
277         cfg = new_config();
278         cfg->variable = xstrdup(variable);
279         cfg->value = xstrdup(value);
280         cfg->file = fname ? xstrdup(fname) : NULL;
281         cfg->line = lineno;
282
283         return cfg;
284 }
285
286 /*
287   Parse a configuration file and put the results in the configuration tree
288   starting at *base.
289 */
290 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
291         FILE *fp;
292         char buffer[MAX_STRING_SIZE];
293         char *line;
294         int lineno = 0;
295         bool ignore = false;
296         config_t *cfg;
297         bool result = false;
298
299         fp = fopen(fname, "r");
300
301         if(!fp) {
302                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
303                 return false;
304         }
305
306         for(;;) {
307                 line = readline(fp, buffer, sizeof buffer);
308
309                 if(!line) {
310                         if(feof(fp))
311                                 result = true;
312                         break;
313                 }
314
315                 lineno++;
316
317                 if(!*line || *line == '#')
318                         continue;
319
320                 if(ignore) {
321                         if(!strncmp(line, "-----END", 8))
322                                 ignore = false;
323                         continue;
324                 }
325
326                 if(!strncmp(line, "-----BEGIN", 10)) {
327                         ignore = true;
328                         continue;
329                 }
330
331                 cfg = parse_config_line(line, fname, lineno);
332                 if (!cfg)
333                         break;
334                 config_add(config_tree, cfg);
335         }
336
337         fclose(fp);
338
339         return result;
340 }
341
342 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
343         size_t prefix_len = prefix ? strlen(prefix) : 0;
344
345         for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) {
346                 const config_t *cfg = node->data;
347                 config_t *new;
348
349                 if(!prefix) {
350                         if(strchr(cfg->variable, '.'))
351                                 continue;
352                 } else {
353                         if(strncmp(prefix, cfg->variable, prefix_len) ||
354                            cfg->variable[prefix_len] != '.')
355                                 continue;
356                 }
357
358                 new = new_config();
359                 if(prefix)
360                         new->variable = xstrdup(cfg->variable + prefix_len + 1);
361                 else
362                         new->variable = xstrdup(cfg->variable);
363                 new->value = xstrdup(cfg->value);
364                 new->file = NULL;
365                 new->line = cfg->line;
366
367                 config_add(config_tree, new);
368         }
369 }
370
371 bool read_server_config(void) {
372         char *fname;
373         bool x;
374
375         read_config_options(config_tree, NULL);
376
377         xasprintf(&fname, "%s" SLASH "tinc.conf", confbase);
378         x = read_config_file(config_tree, fname);
379
380         if(!x)
381                 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
382
383         free(fname);
384
385         return x;
386 }
387
388 bool read_host_config(splay_tree_t *config_tree, const char *name) {
389         char *fname;
390         bool x;
391
392         read_config_options(config_tree, name);
393
394         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
395         x = read_config_file(config_tree, fname);
396         free(fname);
397
398         return x;
399 }
400
401 bool append_config_file(const char *name, const char *key, const char *value) {
402         char *fname;
403         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
404
405         FILE *fp = fopen(fname, "a");
406
407         if(!fp) {
408                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
409         } else {
410                 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);
411                 fclose(fp);
412         }
413
414         free(fname);
415
416         return fp != NULL;
417 }