ff592a866c2b4422ab0ed74ebcd7d510d8092d0a
[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 "utils.h"              /* for cp */
36 #include "xalloc.h"
37
38 splay_tree_t *config_tree;
39
40 int pinginterval = 0;           /* seconds between pings */
41 int pingtimeout = 0;            /* seconds to wait for response */
42 list_t *cmdline_conf = NULL;    /* global/host configuration values given at the command line */
43
44 static int config_compare(const config_t *a, const config_t *b) {
45         int result;
46
47         result = strcasecmp(a->variable, b->variable);
48
49         if(result) {
50                 return result;
51         }
52
53         /* give priority to command line options */
54         result = !b->file - !a->file;
55
56         if(result) {
57                 return result;
58         }
59
60         result = a->line - b->line;
61
62         if(result) {
63                 return result;
64         } else {
65                 return a->file ? strcmp(a->file, b->file) : 0;
66         }
67 }
68
69 void init_configuration(splay_tree_t **config_tree) {
70         *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
71 }
72
73 void exit_configuration(splay_tree_t **config_tree) {
74         splay_delete_tree(*config_tree);
75         *config_tree = NULL;
76 }
77
78 config_t *new_config(void) {
79         return xzalloc(sizeof(config_t));
80 }
81
82 void free_config(config_t *cfg) {
83         free(cfg->variable);
84         free(cfg->value);
85         free(cfg->file);
86         free(cfg);
87 }
88
89 void config_add(splay_tree_t *config_tree, config_t *cfg) {
90         splay_insert(config_tree, cfg);
91 }
92
93 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
94         config_t cfg, *found;
95
96         cfg.variable = variable;
97         cfg.file = NULL;
98         cfg.line = 0;
99
100         found = splay_search_closest_greater(config_tree, &cfg);
101
102         if(!found) {
103                 return NULL;
104         }
105
106         if(strcasecmp(found->variable, variable)) {
107                 return NULL;
108         }
109
110         return found;
111 }
112
113 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
114         splay_node_t *node;
115         config_t *found;
116
117         node = splay_search_node(config_tree, cfg);
118
119         if(node) {
120                 if(node->next) {
121                         found = node->next->data;
122
123                         if(!strcasecmp(found->variable, cfg->variable)) {
124                                 return found;
125                         }
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
137         if(!strcasecmp(cfg->value, "yes")) {
138                 *result = true;
139                 return true;
140         } else if(!strcasecmp(cfg->value, "no")) {
141                 *result = false;
142                 return true;
143         }
144
145         logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
146                cfg->variable, cfg->file, cfg->line);
147
148         return false;
149 }
150
151 bool get_config_int(const config_t *cfg, int *result) {
152         if(!cfg) {
153                 return false;
154         }
155
156         if(sscanf(cfg->value, "%d", result) == 1) {
157                 return true;
158         }
159
160         logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
161                cfg->variable, cfg->file, cfg->line);
162
163         return false;
164 }
165
166 bool get_config_string(const config_t *cfg, char **result) {
167         if(!cfg) {
168                 return false;
169         }
170
171         *result = xstrdup(cfg->value);
172
173         return true;
174 }
175
176 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
177         struct addrinfo *ai;
178
179         if(!cfg) {
180                 return false;
181         }
182
183         ai = str2addrinfo(cfg->value, NULL, 0);
184
185         if(ai) {
186                 *result = ai;
187                 return true;
188         }
189
190         logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
191                cfg->variable, cfg->file, cfg->line);
192
193         return false;
194 }
195
196 bool get_config_subnet(const config_t *cfg, subnet_t **result) {
197         subnet_t subnet = {0};
198
199         if(!cfg) {
200                 return false;
201         }
202
203         if(!str2net(&subnet, cfg->value)) {
204                 logger(DEBUG_ALWAYS, LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
205                        cfg->variable, cfg->file, cfg->line);
206                 return false;
207         }
208
209         if(subnetcheck(subnet)) {
210                 *(*result = new_subnet()) = subnet;
211                 return true;
212         }
213
214         logger(DEBUG_ALWAYS, LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
215                cfg->variable, cfg->file, cfg->line);
216         return false;
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
230         p = fgets(buf, buflen, fp);
231
232         if(!p) {
233                 return NULL;
234         }
235
236         newline = strchr(p, '\n');
237
238         if(!newline) {
239                 return buf;
240         }
241
242         /* kill newline and carriage return if necessary */
243         *newline = '\0';
244
245         if(newline > p && newline[-1] == '\r') {
246                 newline[-1] = '\0';
247         }
248
249         return buf;
250 }
251
252 config_t *parse_config_line(char *line, const char *fname, int lineno) {
253         config_t *cfg;
254         int len;
255         char *variable, *value, *eol;
256         variable = value = line;
257
258         eol = line + strlen(line);
259
260         while(strchr("\t ", *--eol)) {
261                 *eol = '\0';
262         }
263
264         len = strcspn(value, "\t =");
265         value += len;
266         value += strspn(value, "\t ");
267
268         if(*value == '=') {
269                 value++;
270                 value += strspn(value, "\t ");
271         }
272
273         variable[len] = '\0';
274
275         if(!*value) {
276                 const char err[] = "No value for variable";
277
278                 if(fname)
279                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
280                                err, variable, lineno, fname);
281                 else
282                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
283                                err, variable, lineno);
284
285                 return NULL;
286         }
287
288         cfg = new_config();
289         cfg->variable = xstrdup(variable);
290         cfg->value = xstrdup(value);
291         cfg->file = fname ? xstrdup(fname) : NULL;
292         cfg->line = lineno;
293
294         return cfg;
295 }
296
297 /*
298   Parse a configuration file and put the results in the configuration tree
299   starting at *base.
300 */
301 bool read_config_file(splay_tree_t *config_tree, const char *fname, bool verbose) {
302         FILE *fp;
303         char buffer[MAX_STRING_SIZE];
304         char *line;
305         int lineno = 0;
306         bool ignore = false;
307         config_t *cfg;
308         bool result = false;
309
310         fp = fopen(fname, "r");
311
312         if(!fp) {
313                 logger(verbose ? DEBUG_ALWAYS : DEBUG_CONNECTIONS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
314                 return false;
315         }
316
317         for(;;) {
318                 line = readline(fp, buffer, sizeof(buffer));
319
320                 if(!line) {
321                         if(feof(fp)) {
322                                 result = true;
323                         }
324
325                         break;
326                 }
327
328                 lineno++;
329
330                 if(!*line || *line == '#') {
331                         continue;
332                 }
333
334                 if(ignore) {
335                         if(!strncmp(line, "-----END", 8)) {
336                                 ignore = false;
337                         }
338
339                         continue;
340                 }
341
342                 if(!strncmp(line, "-----BEGIN", 10)) {
343                         ignore = true;
344                         continue;
345                 }
346
347                 cfg = parse_config_line(line, fname, lineno);
348
349                 if(!cfg) {
350                         break;
351                 }
352
353                 config_add(config_tree, cfg);
354         }
355
356         fclose(fp);
357
358         return result;
359 }
360
361 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
362         size_t prefix_len = prefix ? strlen(prefix) : 0;
363
364         for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) {
365                 const config_t *cfg = node->data;
366                 config_t *new;
367
368                 if(!prefix) {
369                         if(strchr(cfg->variable, '.')) {
370                                 continue;
371                         }
372                 } else {
373                         if(strncmp(prefix, cfg->variable, prefix_len) ||
374                                         cfg->variable[prefix_len] != '.') {
375                                 continue;
376                         }
377                 }
378
379                 new = new_config();
380
381                 if(prefix) {
382                         new->variable = xstrdup(cfg->variable + prefix_len + 1);
383                 } else {
384                         new->variable = xstrdup(cfg->variable);
385                 }
386
387                 new->value = xstrdup(cfg->value);
388                 new->file = NULL;
389                 new->line = cfg->line;
390
391                 config_add(config_tree, new);
392         }
393 }
394
395 bool read_server_config(void) {
396         char fname[PATH_MAX];
397         bool x;
398
399         read_config_options(config_tree, NULL);
400
401         snprintf(fname, sizeof(fname), "%s" SLASH "tinc.conf", confbase);
402         errno = 0;
403         x = read_config_file(config_tree, fname, true);
404
405         // We will try to read the conf files in the "conf.d" dir
406         if(x) {
407                 char dname[PATH_MAX];
408                 snprintf(dname, sizeof(dname), "%s" SLASH "conf.d", confbase);
409                 DIR *dir = opendir(dname);
410
411                 // If we can find this dir
412                 if(dir) {
413                         struct dirent *ep;
414
415                         // We list all the files in it
416                         while(x && (ep = readdir(dir))) {
417                                 size_t l = strlen(ep->d_name);
418
419                                 // And we try to read the ones that end with ".conf"
420                                 if(l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
421                                         if((size_t)snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ep->d_name) >= sizeof(fname)) {
422                                                 logger(DEBUG_ALWAYS, LOG_ERR, "Pathname too long: %s/%s", dname, ep->d_name);
423                                                 return false;
424                                         }
425
426                                         x = read_config_file(config_tree, fname, true);
427                                 }
428                         }
429
430                         closedir(dir);
431                 }
432         }
433
434         if(!x && errno) {
435                 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
436         }
437
438         return x;
439 }
440
441 bool read_host_config(splay_tree_t *config_tree, const char *name, bool verbose) {
442         read_config_options(config_tree, name);
443
444         char fname[PATH_MAX];
445         snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, name);
446         return read_config_file(config_tree, fname, verbose);
447 }
448
449 bool append_config_file(const char *name, const char *key, const char *value) {
450         char fname[PATH_MAX];
451         snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, name);
452
453         FILE *fp = fopen(fname, "a");
454
455         if(!fp) {
456                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Cannot open config file %s: %s", fname, strerror(errno));
457                 return false;
458         }
459
460         fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);
461         fclose(fp);
462         return true;
463 }