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