]> git.meshlink.io Git - meshlink/blob - src/conf.c
a1c0a3df600f80ecbaee2df8807b98396583495f
[meshlink] / src / conf.c
1 /*
2     conf.c -- configuration code
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "splay_tree.h"
23 #include "connection.h"
24 #include "conf.h"
25 #include "list.h"
26 #include "logger.h"
27 #include "meshlink_internal.h"
28 #include "netutl.h"             /* for str2address */
29 #include "protocol.h"
30 #include "utils.h"              /* for cp */
31 #include "xalloc.h"
32
33 int pinginterval = 0;           /* seconds between pings */
34 int pingtimeout = 0;            /* seconds to wait for response */
35
36 static int config_compare(const config_t *a, const config_t *b) {
37         int result;
38
39         result = strcasecmp(a->variable, b->variable);
40
41         if(result)
42                 return result;
43
44         result = a->line - b->line;
45
46         if(result)
47                 return result;
48         else
49                 return a->file ? strcmp(a->file, b->file) : 0;
50 }
51
52 void init_configuration(splay_tree_t **config_tree) {
53         *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
54 }
55
56 void exit_configuration(splay_tree_t **config_tree) {
57         splay_delete_tree(*config_tree);
58         *config_tree = NULL;
59 }
60
61 config_t *new_config(void) {
62         return xzalloc(sizeof(config_t));
63 }
64
65 void free_config(config_t *cfg) {
66         if(cfg->variable)
67                 free(cfg->variable);
68
69         if(cfg->value)
70                 free(cfg->value);
71
72         if(cfg->file)
73                 free(cfg->file);
74
75         free(cfg);
76 }
77
78 void config_add(splay_tree_t *config_tree, config_t *cfg) {
79         splay_insert(config_tree, cfg);
80 }
81
82 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
83         config_t cfg, *found;
84
85         cfg.variable = variable;
86         cfg.file = NULL;
87         cfg.line = 0;
88
89         found = splay_search_closest_greater(config_tree, &cfg);
90
91         if(!found)
92                 return NULL;
93
94         if(strcasecmp(found->variable, variable))
95                 return NULL;
96
97         return found;
98 }
99
100 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
101         splay_node_t *node;
102         config_t *found;
103
104         node = splay_search_node(config_tree, cfg);
105
106         if(node) {
107                 if(node->next) {
108                         found = node->next->data;
109
110                         if(!strcasecmp(found->variable, cfg->variable))
111                                 return found;
112                 }
113         }
114
115         return NULL;
116 }
117
118 bool get_config_bool(const config_t *cfg, bool *result) {
119         if(!cfg)
120                 return false;
121
122         if(!strcasecmp(cfg->value, "yes")) {
123                 *result = true;
124                 return true;
125         } else if(!strcasecmp(cfg->value, "no")) {
126                 *result = false;
127                 return true;
128         }
129
130         logger(DEBUG_ALWAYS, LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
131                    cfg->variable, cfg->file, cfg->line);
132
133         return false;
134 }
135
136 bool get_config_int(const config_t *cfg, int *result) {
137         if(!cfg)
138                 return false;
139
140         if(sscanf(cfg->value, "%d", result) == 1)
141                 return true;
142
143         logger(DEBUG_ALWAYS, LOG_ERR, "Integer 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_string(const config_t *cfg, char **result) {
150         if(!cfg)
151                 return false;
152
153         *result = xstrdup(cfg->value);
154
155         return true;
156 }
157
158 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
159         struct addrinfo *ai;
160
161         if(!cfg)
162                 return false;
163
164         ai = str2addrinfo(cfg->value, NULL, 0);
165
166         if(ai) {
167                 *result = ai;
168                 return true;
169         }
170
171         logger(DEBUG_ALWAYS, LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
172                    cfg->variable, cfg->file, cfg->line);
173
174         return false;
175 }
176
177 /*
178   Read exactly one line and strip the trailing newline if any.
179 */
180 static char *readline(FILE * fp, char *buf, size_t buflen) {
181         char *newline = NULL;
182         char *p;
183
184         if(feof(fp))
185                 return NULL;
186
187         p = fgets(buf, buflen, fp);
188
189         if(!p)
190                 return NULL;
191
192         newline = strchr(p, '\n');
193
194         if(!newline)
195                 return buf;
196
197         /* kill newline and carriage return if necessary */
198         *newline = '\0';
199         if(newline > p && newline[-1] == '\r')
200                 newline[-1] = '\0';
201
202         return buf;
203 }
204
205 config_t *parse_config_line(char *line, const char *fname, int lineno) {
206         config_t *cfg;
207         int len;
208         char *variable, *value, *eol;
209         variable = value = line;
210
211         eol = line + strlen(line);
212         while(strchr("\t ", *--eol))
213                 *eol = '\0';
214
215         len = strcspn(value, "\t =");
216         value += len;
217         value += strspn(value, "\t ");
218         if(*value == '=') {
219                 value++;
220                 value += strspn(value, "\t ");
221         }
222         variable[len] = '\0';
223
224         if(!*value) {
225                 const char err[] = "No value for variable";
226                 logger(DEBUG_ALWAYS, LOG_ERR, "%s `%s' on line %d while reading config file %s",
227                         err, variable, lineno, fname);
228                 return NULL;
229         }
230
231         cfg = new_config();
232         cfg->variable = xstrdup(variable);
233         cfg->value = xstrdup(value);
234         cfg->file = xstrdup(fname);
235         cfg->line = lineno;
236
237         return cfg;
238 }
239
240 /*
241   Parse a configuration file and put the results in the configuration tree
242   starting at *base.
243 */
244 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
245         FILE *fp;
246         char buffer[MAX_STRING_SIZE];
247         char *line;
248         int lineno = 0;
249         bool ignore = false;
250         config_t *cfg;
251         bool result = false;
252
253         fp = fopen(fname, "r");
254
255         if(!fp) {
256                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
257                 return false;
258         }
259
260         for(;;) {
261                 line = readline(fp, buffer, sizeof buffer);
262
263                 if(!line) {
264                         if(feof(fp))
265                                 result = true;
266                         break;
267                 }
268
269                 lineno++;
270
271                 if(!*line || *line == '#')
272                         continue;
273
274                 if(ignore) {
275                         if(!strncmp(line, "-----END", 8))
276                                 ignore = false;
277                         continue;
278                 }
279
280                 if(!strncmp(line, "-----BEGIN", 10)) {
281                         ignore = true;
282                         continue;
283                 }
284
285                 cfg = parse_config_line(line, fname, lineno);
286                 if (!cfg)
287                         break;
288                 config_add(config_tree, cfg);
289         }
290
291         fclose(fp);
292
293         return result;
294 }
295
296 bool read_server_config(void) {
297         char *fname;
298         bool x;
299
300         xasprintf(&fname, "%s" SLASH "tinc.conf", mesh->confbase);
301         errno = 0;
302         x = read_config_file(mesh->config, fname);
303
304         if(!x && errno)
305                 logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
306
307         free(fname);
308
309         return x;
310 }
311
312 bool read_host_config(splay_tree_t *config_tree, const char *name) {
313         char *fname;
314         bool x;
315
316         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
317         x = read_config_file(config_tree, fname);
318         free(fname);
319
320         return x;
321 }
322
323 bool append_config_file(const char *name, const char *key, const char *value) {
324         char *fname;
325         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
326
327         FILE *fp = fopen(fname, "a");
328
329         if(!fp) {
330                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
331         } else {
332                 fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);
333                 fclose(fp);
334         }
335
336         free(fname);
337
338         return fp != NULL;
339 }