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