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