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