4dd8fe7a828888a2afc02111942744d0063501e7
[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      Cris van Pelt
6                   2010-2011 Julien Muchembled <jm@jmuchemb.eu>
7                   2000-2021 Guus Sliepen <guus@tinc-vpn.org>
8                   2013      Florent Clairambault <florent@clairambault.fr>
9
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License along
21     with this program; if not, write to the Free Software Foundation, Inc.,
22     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25 #include "system.h"
26
27 #include "splay_tree.h"
28 #include "connection.h"
29 #include "conf.h"
30 #include "list.h"
31 #include "logger.h"
32 #include "names.h"
33 #include "netutl.h"             /* for str2address */
34 #include "protocol.h"
35 #include "xalloc.h"
36
37 int pinginterval = 0;           /* seconds between pings */
38 int pingtimeout = 0;            /* seconds to wait for response */
39
40 /* global/host configuration values given at the command line */
41 list_t cmdline_conf = {
42         .head = NULL,
43         .tail = NULL,
44         .count = 0,
45         .delete = (list_action_t)free_config,
46 };
47
48 static int config_compare(const config_t *a, const config_t *b) {
49         int result;
50
51         result = strcasecmp(a->variable, b->variable);
52
53         if(result) {
54                 return result;
55         }
56
57         /* give priority to command line options */
58         result = !b->file - !a->file;
59
60         if(result) {
61                 return result;
62         }
63
64         result = a->line - b->line;
65
66         if(result) {
67                 return result;
68         } else {
69                 return a->file ? strcmp(a->file, b->file) : 0;
70         }
71 }
72
73 splay_tree_t config_tree = {
74         .compare = (splay_compare_t) config_compare,
75         .delete = (splay_action_t) free_config,
76 };
77
78 splay_tree_t *create_configuration() {
79         splay_tree_t *tree = splay_alloc_tree(NULL, NULL);
80         init_configuration(tree);
81         return tree;
82 }
83
84 void init_configuration(splay_tree_t *tree) {
85         memset(tree, 0, sizeof(*tree));
86         tree->compare = (splay_compare_t) config_compare;
87         tree->delete = (splay_action_t) free_config;
88 }
89
90 void exit_configuration(splay_tree_t **config_tree) {
91         splay_delete_tree(*config_tree);
92         *config_tree = NULL;
93 }
94
95 config_t *new_config(void) {
96         return xzalloc(sizeof(config_t));
97 }
98
99 void free_config(config_t *cfg) {
100         free(cfg->variable);
101         free(cfg->value);
102         free(cfg->file);
103         free(cfg);
104 }
105
106 void config_add(splay_tree_t *config_tree, config_t *cfg) {
107         splay_insert(config_tree, cfg);
108 }
109
110 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
111         config_t cfg, *found;
112
113         cfg.variable = variable;
114         cfg.file = NULL;
115         cfg.line = 0;
116
117         found = splay_search_closest_greater(config_tree, &cfg);
118
119         if(!found) {
120                 return NULL;
121         }
122
123         if(strcasecmp(found->variable, variable)) {
124                 return NULL;
125         }
126
127         return found;
128 }
129
130 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
131         splay_node_t *node;
132         config_t *found;
133
134         node = splay_search_node(config_tree, cfg);
135
136         if(node) {
137                 if(node->next) {
138                         found = node->next->data;
139
140                         if(!strcasecmp(found->variable, cfg->variable)) {
141                                 return found;
142                         }
143                 }
144         }
145
146         return NULL;
147 }
148
149 bool get_config_bool(const config_t *cfg, bool *result) {
150         if(!cfg) {
151                 return false;
152         }
153
154         if(!strcasecmp(cfg->value, "yes")) {
155                 *result = true;
156                 return true;
157         } else if(!strcasecmp(cfg->value, "no")) {
158                 *result = false;
159                 return true;
160         }
161
162         logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
163                cfg->variable, cfg->file, cfg->line);
164
165         return false;
166 }
167
168 bool get_config_int(const config_t *cfg, int *result) {
169         if(!cfg) {
170                 return false;
171         }
172
173         if(sscanf(cfg->value, "%d", result) == 1) {
174                 return true;
175         }
176
177         logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
178                cfg->variable, cfg->file, cfg->line);
179
180         return false;
181 }
182
183 bool get_config_string(const config_t *cfg, char **result) {
184         if(!cfg) {
185                 return false;
186         }
187
188         *result = xstrdup(cfg->value);
189
190         return true;
191 }
192
193 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
194         struct addrinfo *ai;
195
196         if(!cfg) {
197                 return false;
198         }
199
200         ai = str2addrinfo(cfg->value, NULL, 0);
201
202         if(ai) {
203                 *result = ai;
204                 return true;
205         }
206
207         logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
208                cfg->variable, cfg->file, cfg->line);
209
210         return false;
211 }
212
213 /*
214   Read exactly one line and strip the trailing newline if any.
215 */
216 static char *readline(FILE *fp, char *buf, size_t buflen) {
217         char *newline = NULL;
218         char *p;
219
220         if(feof(fp)) {
221                 return NULL;
222         }
223
224         p = fgets(buf, (int) buflen, fp);
225
226         if(!p) {
227                 return NULL;
228         }
229
230         newline = strchr(p, '\n');
231
232         if(!newline) {
233                 return buf;
234         }
235
236         /* kill newline and carriage return if necessary */
237         *newline = '\0';
238
239         if(newline > p && newline[-1] == '\r') {
240                 newline[-1] = '\0';
241         }
242
243         return buf;
244 }
245
246 config_t *parse_config_line(char *line, const char *fname, int lineno) {
247         config_t *cfg;
248         char *variable, *value, *eol;
249         variable = value = line;
250
251         eol = line + strlen(line);
252
253         while(strchr("\t ", *--eol)) {
254                 *eol = '\0';
255         }
256
257         size_t len = strcspn(value, "\t =");
258         value += len;
259         value += strspn(value, "\t ");
260
261         if(*value == '=') {
262                 value++;
263                 value += strspn(value, "\t ");
264         }
265
266         variable[len] = '\0';
267
268         if(!*value) {
269                 const char err[] = "No value for variable";
270
271                 if(fname)
272                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
273                                err, variable, lineno, fname);
274                 else
275                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
276                                err, variable, lineno);
277
278                 return NULL;
279         }
280
281         cfg = new_config();
282         cfg->variable = xstrdup(variable);
283         cfg->value = xstrdup(value);
284         cfg->file = fname ? xstrdup(fname) : NULL;
285         cfg->line = lineno;
286
287         return cfg;
288 }
289
290 /*
291   Parse a configuration file and put the results in the configuration tree
292   starting at *base.
293 */
294 bool read_config_file(splay_tree_t *config_tree, const char *fname, bool verbose) {
295         FILE *fp;
296         char buffer[MAX_STRING_SIZE];
297         char *line;
298         int lineno = 0;
299         bool ignore = false;
300         config_t *cfg;
301         bool result = false;
302
303         fp = fopen(fname, "r");
304
305         if(!fp) {
306                 logger(verbose ? DEBUG_ALWAYS : DEBUG_CONNECTIONS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
307                 return false;
308         }
309
310         for(;;) {
311                 line = readline(fp, buffer, sizeof(buffer));
312
313                 if(!line) {
314                         if(feof(fp)) {
315                                 result = true;
316                         }
317
318                         break;
319                 }
320
321                 lineno++;
322
323                 if(!*line || *line == '#') {
324                         continue;
325                 }
326
327                 if(ignore) {
328                         if(!strncmp(line, "-----END", 8)) {
329                                 ignore = false;
330                         }
331
332                         continue;
333                 }
334
335                 if(!strncmp(line, "-----BEGIN", 10)) {
336                         ignore = true;
337                         continue;
338                 }
339
340                 cfg = parse_config_line(line, fname, lineno);
341
342                 if(!cfg) {
343                         break;
344                 }
345
346                 config_add(config_tree, cfg);
347         }
348
349         fclose(fp);
350
351         return result;
352 }
353
354 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
355         size_t prefix_len = prefix ? strlen(prefix) : 0;
356
357         for(const list_node_t *node = cmdline_conf.tail; node; node = node->prev) {
358                 const config_t *cfg = node->data;
359                 config_t *new;
360
361                 if(!prefix) {
362                         if(strchr(cfg->variable, '.')) {
363                                 continue;
364                         }
365                 } else {
366                         if(strncmp(prefix, cfg->variable, prefix_len) ||
367                                         cfg->variable[prefix_len] != '.') {
368                                 continue;
369                         }
370                 }
371
372                 new = new_config();
373
374                 if(prefix) {
375                         new->variable = xstrdup(cfg->variable + prefix_len + 1);
376                 } else {
377                         new->variable = xstrdup(cfg->variable);
378                 }
379
380                 new->value = xstrdup(cfg->value);
381                 new->file = NULL;
382                 new->line = cfg->line;
383
384                 config_add(config_tree, new);
385         }
386 }
387
388 bool read_server_config(splay_tree_t *config_tree) {
389         char fname[PATH_MAX];
390         bool x;
391
392         read_config_options(config_tree, NULL);
393
394         snprintf(fname, sizeof(fname), "%s" SLASH "tinc.conf", confbase);
395         errno = 0;
396         x = read_config_file(config_tree, fname, true);
397
398         // We will try to read the conf files in the "conf.d" dir
399         if(x) {
400                 char dname[PATH_MAX];
401                 snprintf(dname, sizeof(dname), "%s" SLASH "conf.d", confbase);
402                 DIR *dir = opendir(dname);
403
404                 // If we can find this dir
405                 if(dir) {
406                         struct dirent *ep;
407
408                         // We list all the files in it
409                         while(x && (ep = readdir(dir))) {
410                                 size_t l = strlen(ep->d_name);
411
412                                 // And we try to read the ones that end with ".conf"
413                                 if(l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
414                                         if((size_t)snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name) >= sizeof(fname)) {
415                                                 logger(DEBUG_ALWAYS, LOG_ERR, "Pathname too long: %s/%s", dname, ep->d_name);
416                                                 return false;
417                                         }
418
419                                         x = read_config_file(config_tree, fname, true);
420                                 }
421                         }
422
423                         closedir(dir);
424                 }
425         }
426
427         if(!x && errno) {
428                 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
429         }
430
431         return x;
432 }
433
434 bool read_host_config(splay_tree_t *config_tree, const char *name, bool verbose) {
435         read_config_options(config_tree, name);
436
437         char fname[PATH_MAX];
438         snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, name);
439         return read_config_file(config_tree, fname, verbose);
440 }
441
442 bool append_config_file(const char *name, const char *key, const char *value) {
443         char fname[PATH_MAX];
444         snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, name);
445
446         FILE *fp = fopen(fname, "a");
447
448         if(!fp) {
449                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Cannot open config file %s: %s", fname, strerror(errno));
450                 return false;
451         }
452
453         fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);
454         fclose(fp);
455         return true;
456 }