]> git.meshlink.io Git - meshlink/blob - src/conf.c
fcaefdd27ff8a122fff0b0fa70dc127cb1666f91
[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 #include <assert.h>
22
23 #include "splay_tree.h"
24 #include "connection.h"
25 #include "conf.h"
26 #include "list.h"
27 #include "logger.h"
28 #include "meshlink_internal.h"
29 #include "netutl.h"             /* for str2address */
30 #include "protocol.h"
31 #include "utils.h"              /* for cp */
32 #include "xalloc.h"
33
34 static int config_compare(const config_t *a, const config_t *b) {
35         int result;
36
37         result = strcasecmp(a->variable, b->variable);
38
39         if(result) {
40                 return result;
41         }
42
43         result = a->line - b->line;
44
45         if(result) {
46                 return result;
47         } else {
48                 return a->file ? strcmp(a->file, b->file) : 0;
49         }
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         if(*config_tree) {
58                 splay_delete_tree(*config_tree);
59         }
60
61         *config_tree = NULL;
62 }
63
64 config_t *new_config(void) {
65         return xzalloc(sizeof(config_t));
66 }
67
68 void free_config(config_t *cfg) {
69         if(cfg->variable) {
70                 free(cfg->variable);
71         }
72
73         if(cfg->value) {
74                 free(cfg->value);
75         }
76
77         if(cfg->file) {
78                 free(cfg->file);
79         }
80
81         free(cfg);
82 }
83
84 void config_add(splay_tree_t *config_tree, config_t *cfg) {
85         splay_insert(config_tree, cfg);
86 }
87
88 config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
89         config_t cfg, *found;
90
91         cfg.variable = variable;
92         cfg.file = NULL;
93         cfg.line = 0;
94
95         found = splay_search_closest_greater(config_tree, &cfg);
96
97         if(!found) {
98                 return NULL;
99         }
100
101         if(strcasecmp(found->variable, variable)) {
102                 return NULL;
103         }
104
105         return found;
106 }
107
108 config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
109         splay_node_t *node;
110         config_t *found;
111
112         node = splay_search_node(config_tree, cfg);
113
114         if(node) {
115                 if(node->next) {
116                         found = node->next->data;
117
118                         if(!strcasecmp(found->variable, cfg->variable)) {
119                                 return found;
120                         }
121                 }
122         }
123
124         return NULL;
125 }
126
127 bool get_config_bool(const config_t *cfg, bool *result) {
128         if(!cfg) {
129                 return false;
130         }
131
132         if(!strcasecmp(cfg->value, "yes")) {
133                 *result = true;
134                 return true;
135         } else if(!strcasecmp(cfg->value, "no")) {
136                 *result = false;
137                 return true;
138         }
139
140         logger(NULL, MESHLINK_ERROR, "\"yes\" or \"no\" 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_int(const config_t *cfg, int *result) {
147         if(!cfg) {
148                 return false;
149         }
150
151         if(sscanf(cfg->value, "%d", result) == 1) {
152                 return true;
153         }
154
155         logger(NULL, MESHLINK_ERROR, "Integer expected for configuration variable %s in %s line %d",
156                cfg->variable, cfg->file, cfg->line);
157
158         return false;
159 }
160
161 bool set_config_int(config_t *cfg, int val) {
162         if(!cfg) {
163                 return false;
164         }
165
166         char val_str[1024];
167         snprintf(val_str, sizeof(val_str), "%d", val);
168
169         if(cfg->value) {
170                 free(cfg->value);
171         }
172
173         cfg->value = xstrdup(val_str);
174
175         return true;
176 }
177
178 bool get_config_string(const config_t *cfg, char **result) {
179         if(!cfg) {
180                 return false;
181         }
182
183         *result = xstrdup(cfg->value);
184
185         return true;
186 }
187
188 bool set_config_string(config_t *cfg, const char *val) {
189         if(!cfg) {
190                 return false;
191         }
192
193         if(cfg->value) {
194                 free(cfg->value);
195         }
196
197         cfg->value = xstrdup(val);
198
199         return true;
200 }
201
202 bool get_config_address(const config_t *cfg, struct addrinfo **result) {
203         struct addrinfo *ai;
204
205         if(!cfg) {
206                 return false;
207         }
208
209         ai = str2addrinfo(cfg->value, NULL, 0);
210
211         if(ai) {
212                 *result = ai;
213                 return true;
214         }
215
216         logger(NULL, MESHLINK_ERROR, "Hostname or IP address expected for configuration variable %s in %s line %d",
217                cfg->variable, cfg->file, cfg->line);
218
219         return false;
220 }
221
222 /*
223   Read exactly one line and strip the trailing newline if any.
224 */
225 static char *readline(FILE *fp, char *buf, size_t buflen) {
226         char *newline = NULL;
227         char *p;
228
229         if(feof(fp)) {
230                 return NULL;
231         }
232
233         p = fgets(buf, buflen, fp);
234
235         if(!p) {
236                 return NULL;
237         }
238
239         newline = strchr(p, '\n');
240
241         if(!newline) {
242                 return buf;
243         }
244
245         /* kill newline and carriage return if necessary */
246         *newline = '\0';
247
248         if(newline > p && newline[-1] == '\r') {
249                 newline[-1] = '\0';
250         }
251
252         return buf;
253 }
254
255 config_t *parse_config_line(char *line, const char *fname, int lineno) {
256         config_t *cfg;
257         int len;
258         char *variable, *value, *eol;
259         variable = value = line;
260
261         eol = line + strlen(line);
262
263         while(strchr("\t ", *--eol)) {
264                 *eol = '\0';
265         }
266
267         len = strcspn(value, "\t =");
268         value += len;
269         value += strspn(value, "\t ");
270
271         if(*value == '=') {
272                 value++;
273                 value += strspn(value, "\t ");
274         }
275
276         variable[len] = '\0';
277
278         if(!*value) {
279                 const char err[] = "No value for variable";
280                 logger(NULL, MESHLINK_ERROR, "%s `%s' on line %d while reading config file %s",
281                        err, variable, lineno, fname);
282                 return NULL;
283         }
284
285         cfg = new_config();
286         cfg->variable = xstrdup(variable);
287         cfg->value = xstrdup(value);
288         cfg->file = xstrdup(fname);
289         cfg->line = lineno;
290
291         return cfg;
292 }
293
294 /*
295   Parse a configuration file and put the results in the configuration tree
296   starting at *base.
297 */
298 bool read_config_file(splay_tree_t *config_tree, const char *fname) {
299         FILE *fp;
300         char buffer[MAX_STRING_SIZE];
301         char *line;
302         int lineno = 0;
303         bool ignore = false;
304         config_t *cfg;
305         bool result = false;
306
307         fp = fopen(fname, "r");
308
309         if(!fp) {
310                 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
311                 return false;
312         }
313
314         for(;;) {
315                 line = readline(fp, buffer, sizeof(buffer));
316
317                 if(!line) {
318                         if(feof(fp)) {
319                                 result = true;
320                         }
321
322                         break;
323                 }
324
325                 lineno++;
326
327                 if(!*line || *line == '#') {
328                         continue;
329                 }
330
331                 if(ignore) {
332                         if(!strncmp(line, "-----END", 8)) {
333                                 ignore = false;
334                         }
335
336                         continue;
337                 }
338
339                 if(!strncmp(line, "-----BEGIN", 10)) {
340                         ignore = true;
341                         continue;
342                 }
343
344                 cfg = parse_config_line(line, fname, lineno);
345
346                 if(!cfg) {
347                         break;
348                 }
349
350                 config_add(config_tree, cfg);
351         }
352
353         fclose(fp);
354
355         return result;
356 }
357
358 bool write_config_file(const struct splay_tree_t *config_tree, const char *fname) {
359         FILE *fp;
360
361         fp = fopen(fname, "w+");
362
363         if(!fp) {
364                 logger(NULL, MESHLINK_ERROR, "Cannot open config file %s: %s", fname, strerror(errno));
365                 return false;
366         }
367
368         for splay_each(config_t, cnf, config_tree) {
369                 if(fwrite(cnf->variable, sizeof(char), strlen(cnf->variable), fp) < strlen(cnf->variable)) {
370                         goto error;
371                 }
372
373                 if(fwrite(" = ", sizeof(char), 3, fp) < 3) {
374                         goto error;
375                 }
376
377                 if(fwrite(cnf->value, sizeof(char), strlen(cnf->value), fp) < strlen(cnf->value)) {
378                         goto error;
379                 }
380
381                 if(fwrite("\n", sizeof(char), 1, fp) < 1) {
382                         goto error;
383                 }
384         }
385
386         fclose(fp);
387         return true;
388
389 error:
390         logger(NULL, MESHLINK_ERROR, "Cannot write to config file %s: %s", fname, strerror(errno));
391         fclose(fp);
392         return false;
393 }
394
395 bool read_server_config(meshlink_handle_t *mesh) {
396         char filename[PATH_MAX];
397         bool x;
398
399         snprintf(filename, PATH_MAX, "%s" SLASH "meshlink.conf", mesh->confbase);
400         errno = 0;
401         x = read_config_file(mesh->config, filename);
402
403         if(!x && errno) {
404                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", filename, strerror(errno));
405         }
406
407         return x;
408 }
409
410 bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const char *name) {
411         char filename[PATH_MAX];
412         bool x;
413
414         snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
415         x = read_config_file(config_tree, filename);
416
417         return x;
418 }
419
420 bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *config_tree, const char *name) {
421         char filename[PATH_MAX];
422
423         snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
424         return write_config_file(config_tree, filename);
425 }
426
427 bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, int trim) {
428         assert(mesh && name && key);
429
430         char filename[PATH_MAX];
431         char tmpname[PATH_MAX];
432         bool error = false;
433
434         snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
435         snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename);
436
437         FILE *fr = fopen(filename, "r");
438
439         if(!fr) {
440                 logger(mesh, MESHLINK_ERROR, "Cannot open config file %s: %s", filename, strerror(errno));
441                 return false;
442         }
443
444         FILE *fw = fopen(tmpname, "w");
445
446         if(!fw) {
447                 logger(mesh, MESHLINK_ERROR, "Cannot open temporary file %s: %s", tmpname, strerror(errno));
448                 fclose(fr);
449                 return false;
450         }
451
452         char buf[4096];
453         char *sep;
454         int found = 0;
455
456         if(value) {
457                 fprintf(fw, "%s = %s\n", key, value);
458                 found++;
459         }
460
461         while(readline(fr, buf, sizeof(buf))) {
462                 if(!*buf || *buf == '#') {
463                         goto copy;
464                 }
465
466                 sep = strchr(buf, ' ');
467
468                 if(!sep) {
469                         goto copy;
470                 }
471
472                 *sep = 0;
473
474                 if(strcmp(buf, key)) {
475                         *sep = ' ';
476                         goto copy;
477                 }
478
479                 // We found the key and the value. We already added it at the top, so ignore this one.
480                 if(value && sep[1] == '=' && sep[2] == ' ' && !strcmp(sep + 3, value)) {
481                         continue;
482                 }
483
484                 // We found the key but with a different value, delete it if wanted.
485                 found++;
486
487                 if((!value || trim) && found > trim) {
488                         continue;
489                 }
490
491                 *sep = ' ';
492
493 copy:
494                 fprintf(fw, "%s\n", buf);
495         }
496
497         if(ferror(fr)) {
498                 error = true;
499         }
500
501         fclose(fr);
502
503         if(ferror(fw)) {
504                 error = true;
505         }
506
507         if(fclose(fw)) {
508                 error = true;
509         }
510
511         // If any error occured during reading or writing, exit.
512         if(error) {
513                 unlink(tmpname);
514                 return false;
515         }
516
517         // Try to atomically replace the old config file with the new one.
518 #ifdef HAVE_MINGW
519         char bakname[PATH_MAX];
520         snprintf(bakname, sizeof(bakname), "%s.bak", filename);
521
522         if(rename(filename, bakname) || rename(tmpname, filename)) {
523                 rename(bakname, filename);
524 #else
525
526         if(rename(tmpname, filename)) {
527 #endif
528                 return false;
529         } else {
530 #ifdef HAVE_MINGW
531                 unlink(bakname);
532 #endif
533                 return true;
534         }
535 }
536
537 bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
538         return modify_config_file(mesh, name, key, value, 0);
539 }