]> git.meshlink.io Git - meshlink/blob - src/conf.c
81d8359799ef3c7dc6d380a0709253a8d3feee6a
[meshlink] / 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-2013 Guus Sliepen <guus@meshlink.io>
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 "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 /*
190   Read exactly one line and strip the trailing newline if any.
191 */
192 static char *readline(FILE * fp, char *buf, size_t buflen) {
193         char *newline = NULL;
194         char *p;
195
196         if(feof(fp))
197                 return NULL;
198
199         p = fgets(buf, buflen, fp);
200
201         if(!p)
202                 return NULL;
203
204         newline = strchr(p, '\n');
205
206         if(!newline)
207                 return buf;
208
209         /* kill newline and carriage return if necessary */
210         *newline = '\0';
211         if(newline > p && newline[-1] == '\r')
212                 newline[-1] = '\0';
213
214         return buf;
215 }
216
217 config_t *parse_config_line(char *line, const char *fname, int lineno) {
218         config_t *cfg;
219         int len;
220         char *variable, *value, *eol;
221         variable = value = line;
222
223         eol = line + strlen(line);
224         while(strchr("\t ", *--eol))
225                 *eol = '\0';
226
227         len = strcspn(value, "\t =");
228         value += len;
229         value += strspn(value, "\t ");
230         if(*value == '=') {
231                 value++;
232                 value += strspn(value, "\t ");
233         }
234         variable[len] = '\0';
235
236         if(!*value) {
237                 const char err[] = "No value for variable";
238                 if (fname)
239                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
240                                 err, variable, lineno, fname);
241                 else
242                         logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' in command line option %d",
243                                 err, variable, lineno);
244                 return NULL;
245         }
246
247         cfg = new_config();
248         cfg->variable = xstrdup(variable);
249         cfg->value = xstrdup(value);
250         cfg->file = fname ? xstrdup(fname) : NULL;
251         cfg->line = lineno;
252
253         return cfg;
254 }
255
256 /*
257   Parse a configuration file and put the results in the configuration tree
258   starting at *base.
259 */
260 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
261         FILE *fp;
262         char buffer[MAX_STRING_SIZE];
263         char *line;
264         int lineno = 0;
265         bool ignore = false;
266         config_t *cfg;
267         bool result = false;
268
269         fp = fopen(fname, "r");
270
271         if(!fp) {
272                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
273                 return false;
274         }
275
276         for(;;) {
277                 line = readline(fp, buffer, sizeof buffer);
278
279                 if(!line) {
280                         if(feof(fp))
281                                 result = true;
282                         break;
283                 }
284
285                 lineno++;
286
287                 if(!*line || *line == '#')
288                         continue;
289
290                 if(ignore) {
291                         if(!strncmp(line, "-----END", 8))
292                                 ignore = false;
293                         continue;
294                 }
295
296                 if(!strncmp(line, "-----BEGIN", 10)) {
297                         ignore = true;
298                         continue;
299                 }
300
301                 cfg = parse_config_line(line, fname, lineno);
302                 if (!cfg)
303                         break;
304                 config_add(config_tree, cfg);
305         }
306
307         fclose(fp);
308
309         return result;
310 }
311
312 void read_config_options(splay_tree_t *config_tree, const char *prefix) {
313         size_t prefix_len = prefix ? strlen(prefix) : 0;
314
315         for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) {
316                 const config_t *cfg = node->data;
317                 config_t *new;
318
319                 if(!prefix) {
320                         if(strchr(cfg->variable, '.'))
321                                 continue;
322                 } else {
323                         if(strncmp(prefix, cfg->variable, prefix_len) ||
324                            cfg->variable[prefix_len] != '.')
325                                 continue;
326                 }
327
328                 new = new_config();
329                 if(prefix)
330                         new->variable = xstrdup(cfg->variable + prefix_len + 1);
331                 else
332                         new->variable = xstrdup(cfg->variable);
333                 new->value = xstrdup(cfg->value);
334                 new->file = NULL;
335                 new->line = cfg->line;
336
337                 config_add(config_tree, new);
338         }
339 }
340
341 bool read_server_config(void) {
342         char *fname;
343         bool x;
344
345         //read_config_options(config_tree, NULL);
346
347         xasprintf(&fname, "%s" SLASH "tinc.conf", confbase);
348         errno = 0;
349         x = read_config_file(config_tree, fname);
350
351         // We will try to read the conf files in the "conf.d" dir
352         if (x) {
353                 char * dname;
354                 xasprintf(&dname, "%s" SLASH "conf.d", confbase);
355                 DIR *dir = opendir (dname);
356                 // If we can find this dir
357                 if (dir) { 
358                         struct dirent *ep;
359                         // We list all the files in it
360                         while (x && (ep = readdir (dir))) {
361                                 size_t l = strlen(ep->d_name);
362                                 // And we try to read the ones that end with ".conf"
363                                 if (l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
364                                         free(fname);
365                                         xasprintf(&fname, "%s" SLASH "%s", dname, ep->d_name);
366                                         x = read_config_file(config_tree, fname);
367                                 }
368                         }
369                         closedir (dir);
370                 }
371                 free(dname);
372         }
373
374         if(!x && errno)
375                 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
376
377         free(fname);
378
379         return x;
380 }
381
382 bool read_host_config(splay_tree_t *config_tree, const char *name) {
383         char *fname;
384         bool x;
385
386         //read_config_options(config_tree, name);
387
388         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
389         x = read_config_file(config_tree, fname);
390         free(fname);
391
392         return x;
393 }
394
395 bool append_config_file(const char *name, const char *key, const char *value) {
396         char *fname;
397         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
398
399         FILE *fp = fopen(fname, "a");
400
401         if(!fp) {
402                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
403         } else {
404                 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);
405                 fclose(fp);
406         }
407
408         free(fname);
409
410         return fp != NULL;
411 }