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