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