Adding "conf.d" configuration dir support.
[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-2013 Guus Sliepen <guus@tinc-vpn.org>
6                   2010-2011 Julien Muchembled <jm@jmuchemb.eu>
7                   2000 Cris van Pelt
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License along
20     with this program; if not, write to the Free Software Foundation, Inc.,
21     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include "system.h"
25
26 #include "splay_tree.h"
27 #include "connection.h"
28 #include "conf.h"
29 #include "list.h"
30 #include "logger.h"
31 #include "names.h"
32 #include "netutl.h"             /* for str2address */
33 #include "protocol.h"
34 #include "utils.h"              /* for cp */
35 #include "xalloc.h"
36
37 splay_tree_t *config_tree;
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         /* give priority to command line options */
52         result = !b->file - !a->file;
53         if (result)
54                 return result;
55
56         result = a->line - b->line;
57
58         if(result)
59                 return result;
60         else
61                 return a->file ? strcmp(a->file, b->file) : 0;
62 }
63
64 void init_configuration(splay_tree_t ** config_tree) {
65         *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
66 }
67
68 void exit_configuration(splay_tree_t ** config_tree) {
69         splay_delete_tree(*config_tree);
70         *config_tree = NULL;
71 }
72
73 config_t *new_config(void) {
74         return xzalloc(sizeof(config_t));
75 }
76
77 void free_config(config_t *cfg) {
78         if(cfg->variable)
79                 free(cfg->variable);
80
81         if(cfg->value)
82                 free(cfg->value);
83
84         if(cfg->file)
85                 free(cfg->file);
86
87         free(cfg);
88 }
89
90 void config_add(splay_tree_t *config_tree, config_t *cfg) {
91         splay_insert(config_tree, cfg);
92 }
93
94 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
95         config_t cfg, *found;
96
97         cfg.variable = variable;
98         cfg.file = NULL;
99         cfg.line = 0;
100
101         found = splay_search_closest_greater(config_tree, &cfg);
102
103         if(!found)
104                 return NULL;
105
106         if(strcasecmp(found->variable, variable))
107                 return NULL;
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         return NULL;
128 }
129
130 bool get_config_bool(const config_t *cfg, bool *result) {
131         if(!cfg)
132                 return false;
133
134         if(!strcasecmp(cfg->value, "yes")) {
135                 *result = true;
136                 return true;
137         } else if(!strcasecmp(cfg->value, "no")) {
138                 *result = false;
139                 return true;
140         }
141
142         logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
143                    cfg->variable, cfg->file, cfg->line);
144
145         return false;
146 }
147
148 bool get_config_int(const config_t *cfg, int *result) {
149         if(!cfg)
150                 return false;
151
152         if(sscanf(cfg->value, "%d", result) == 1)
153                 return true;
154
155         logger(DEBUG_ALWAYS, LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
156                    cfg->variable, cfg->file, cfg->line);
157
158         return false;
159 }
160
161 bool get_config_string(const config_t *cfg, char **result) {
162         if(!cfg)
163                 return false;
164
165         *result = xstrdup(cfg->value);
166
167         return true;
168 }
169
170 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
171         struct addrinfo *ai;
172
173         if(!cfg)
174                 return false;
175
176         ai = str2addrinfo(cfg->value, NULL, 0);
177
178         if(ai) {
179                 *result = ai;
180                 return true;
181         }
182
183         logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
184                    cfg->variable, cfg->file, cfg->line);
185
186         return false;
187 }
188
189 bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
190         subnet_t subnet = {NULL};
191
192         if(!cfg)
193                 return false;
194
195         if(!str2net(&subnet, cfg->value)) {
196                 logger(DEBUG_ALWAYS, LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
197                            cfg->variable, cfg->file, cfg->line);
198                 return false;
199         }
200
201         /* Teach newbies what subnets are... */
202
203         if(((subnet.type == SUBNET_IPV4)
204                 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof subnet.net.ipv4.address))
205                 || ((subnet.type == SUBNET_IPV6)
206                 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address))) {
207                 logger(DEBUG_ALWAYS, LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
208                            cfg->variable, cfg->file, cfg->line);
209                 return false;
210         }
211
212         *(*result = new_subnet()) = subnet;
213
214         return true;
215 }
216
217 /*
218   Read exactly one line and strip the trailing newline if any.
219 */
220 static char *readline(FILE * fp, char *buf, size_t buflen) {
221         char *newline = NULL;
222         char *p;
223
224         if(feof(fp))
225                 return NULL;
226
227         p = fgets(buf, buflen, fp);
228
229         if(!p)
230                 return NULL;
231
232         newline = strchr(p, '\n');
233
234         if(!newline)
235                 return buf;
236
237         /* kill newline and carriage return if necessary */
238         *newline = '\0';
239         if(newline > p && newline[-1] == '\r')
240                 newline[-1] = '\0';
241
242         return buf;
243 }
244
245 config_t *parse_config_line(char *line, const char *fname, int lineno) {
246         config_t *cfg;
247         int len;
248         char *variable, *value, *eol;
249         variable = value = line;
250
251         eol = line + strlen(line);
252         while(strchr("\t ", *--eol))
253                 *eol = '\0';
254
255         len = strcspn(value, "\t =");
256         value += len;
257         value += strspn(value, "\t ");
258         if(*value == '=') {
259                 value++;
260                 value += strspn(value, "\t ");
261         }
262         variable[len] = '\0';
263
264         if(!*value) {
265                 const char err[] = "No value for variable";
266                 if (fname)
267                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
268                                 err, variable, lineno, fname);
269                 else
270                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
271                                 err, variable, lineno);
272                 return NULL;
273         }
274
275         cfg = new_config();
276         cfg->variable = xstrdup(variable);
277         cfg->value = xstrdup(value);
278         cfg->file = fname ? xstrdup(fname) : NULL;
279         cfg->line = lineno;
280
281         return cfg;
282 }
283
284 /*
285   Parse a configuration file and put the results in the configuration tree
286   starting at *base.
287 */
288 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
289         FILE *fp;
290         char buffer[MAX_STRING_SIZE];
291         char *line;
292         int lineno = 0;
293         bool ignore = false;
294         config_t *cfg;
295         bool result = false;
296
297         fp = fopen(fname, "r");
298
299         if(!fp) {
300                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
301                 return false;
302         }
303
304         for(;;) {
305                 line = readline(fp, buffer, sizeof buffer);
306
307                 if(!line) {
308                         if(feof(fp))
309                                 result = true;
310                         break;
311                 }
312
313                 lineno++;
314
315                 if(!*line || *line == '#')
316                         continue;
317
318                 if(ignore) {
319                         if(!strncmp(line, "-----END", 8))
320                                 ignore = false;
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                 if (!cfg)
331                         break;
332                 config_add(config_tree, cfg);
333         }
334
335         fclose(fp);
336
337         return result;
338 }
339
340 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
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                 } else {
351                         if(strncmp(prefix, cfg->variable, prefix_len) ||
352                            cfg->variable[prefix_len] != '.')
353                                 continue;
354                 }
355
356                 new = new_config();
357                 if(prefix)
358                         new->variable = xstrdup(cfg->variable + prefix_len + 1);
359                 else
360                         new->variable = xstrdup(cfg->variable);
361                 new->value = xstrdup(cfg->value);
362                 new->file = NULL;
363                 new->line = cfg->line;
364
365                 config_add(config_tree, new);
366         }
367 }
368
369 bool read_server_config(void) {
370         char *fname;
371         bool x;
372
373         read_config_options(config_tree, NULL);
374
375         xasprintf(&fname, "%s" SLASH "tinc.conf", confbase);
376         errno = 0;
377         x = read_config_file(config_tree, fname);
378
379         // We will try to read the conf files in the "conf.d" dir
380         if (x) {
381                 char * dname;
382                 xasprintf(&dname, "%s" SLASH "conf.d", confbase);
383                 DIR *dir = opendir (dname);
384                 // If we can find this dir
385                 if (dir) { 
386                         struct dirent *ep;
387                         // We list all the files in it
388                         while (x && (ep = readdir (dir))) {
389                                 size_t l = strlen(ep->d_name);
390                                 // And we try to read the ones that end with ".conf"
391                                 if (l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
392                                         free(fname);
393                                         xasprintf(&fname, "%s" SLASH "%s", dname, ep->d_name);
394                                         x = read_config_file(config_tree, fname);
395                                 }
396                         }
397                         closedir (dir);
398                 }
399                 free(dname);
400         }
401
402         if(!x && errno)
403                 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
404
405         free(fname);
406
407         return x;
408 }
409
410 bool read_host_config(splay_tree_t *config_tree, const char *name) {
411         char *fname;
412         bool x;
413
414         read_config_options(config_tree, name);
415
416         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
417         x = read_config_file(config_tree, fname);
418         free(fname);
419
420         return x;
421 }
422
423 bool append_config_file(const char *name, const char *key, const char *value) {
424         char *fname;
425         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
426
427         FILE *fp = fopen(fname, "a");
428
429         if(!fp) {
430                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
431         } else {
432                 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);
433                 fclose(fp);
434         }
435
436         free(fname);
437
438         return fp != NULL;
439 }