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