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