]> git.meshlink.io Git - meshlink/blob - src/conf.c
Rename mesh_mutex to mutex.
[meshlink] / src / conf.c
1 /*
2     econf.c -- configuration code
3     Copyright (C) 2018 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 #include <sys/types.h>
23 #include <utime.h>
24
25 #include "conf.h"
26 #include "crypto.h"
27 #include "logger.h"
28 #include "meshlink_internal.h"
29 #include "xalloc.h"
30 #include "packmsg.h"
31
32 /// Generate a path to the main configuration file.
33 static void make_main_path(meshlink_handle_t *mesh, const char *conf_subdir, char *path, size_t len) {
34         assert(conf_subdir);
35         assert(path);
36         assert(len);
37
38         snprintf(path, len, "%s" SLASH "%s" SLASH "meshlink.conf", mesh->confbase, conf_subdir);
39 }
40
41 /// Generate a path to a host configuration file.
42 static void make_host_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
43         assert(conf_subdir);
44         assert(name);
45         assert(path);
46         assert(len);
47
48         snprintf(path, len, "%s" SLASH "%s" SLASH "hosts" SLASH "%s", mesh->confbase, conf_subdir, name);
49 }
50
51 /// Generate a path to an unused invitation file.
52 static void make_invitation_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
53         assert(conf_subdir);
54         assert(name);
55         assert(path);
56         assert(len);
57
58         snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s", mesh->confbase, conf_subdir, name);
59 }
60
61 /// Generate a path to a used invitation file.
62 static void make_used_invitation_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
63         assert(conf_subdir);
64         assert(name);
65         assert(path);
66         assert(len);
67
68         snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, conf_subdir, name);
69 }
70
71 /// Remove a directory recursively
72 static void deltree(const char *dirname) {
73         assert(dirname);
74
75         DIR *d = opendir(dirname);
76
77         if(d) {
78                 struct dirent *ent;
79
80                 while((ent = readdir(d))) {
81                         if(ent->d_name[0] == '.') {
82                                 continue;
83                         }
84
85                         char filename[PATH_MAX];
86                         snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
87
88                         if(unlink(filename)) {
89                                 deltree(filename);
90                         }
91                 }
92
93                 closedir(d);
94         }
95
96         rmdir(dirname);
97 }
98
99 static bool sync_path(const char *pathname) {
100         assert(pathname);
101
102         int fd = open(pathname, O_RDONLY);
103
104         if(fd < 0) {
105                 logger(NULL, MESHLINK_ERROR, "Failed to open %s: %s\n", pathname, strerror(errno));
106                 meshlink_errno = MESHLINK_ESTORAGE;
107                 return false;
108         }
109
110         if(fsync(fd)) {
111                 logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
112                 close(fd);
113                 meshlink_errno = MESHLINK_ESTORAGE;
114                 return false;
115         }
116
117         if(close(fd)) {
118                 logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
119                 close(fd);
120                 meshlink_errno = MESHLINK_ESTORAGE;
121                 return false;
122         }
123
124         return true;
125 }
126
127 /// Try decrypting the main configuration file from the given sub-directory.
128 static bool main_config_decrypt(meshlink_handle_t *mesh, const char *conf_subdir) {
129         assert(mesh->config_key);
130         assert(mesh->confbase);
131         assert(conf_subdir);
132
133         config_t config;
134
135         if(!main_config_read(mesh, conf_subdir, &config, mesh->config_key)) {
136                 logger(mesh, MESHLINK_ERROR, "Could not read main configuration file");
137                 return false;
138         }
139
140         packmsg_input_t in = {config.buf, config.len};
141
142         uint32_t version = packmsg_get_uint32(&in);
143         config_free(&config);
144
145         return version == MESHLINK_CONFIG_VERSION;
146 }
147
148 /// Create a fresh configuration directory
149 bool config_init(meshlink_handle_t *mesh, const char *conf_subdir) {
150         assert(conf_subdir);
151
152         if(!mesh->confbase) {
153                 return true;
154         }
155
156         if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
157                 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno));
158                 return false;
159         }
160
161         char path[PATH_MAX];
162
163         // Create "current" sub-directory in the confbase
164         snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
165         deltree(path);
166
167         if(mkdir(path, 0700)) {
168                 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
169                 return false;
170         }
171
172         make_host_path(mesh, conf_subdir, "", path, sizeof(path));
173
174         if(mkdir(path, 0700)) {
175                 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
176                 return false;
177         }
178
179         make_invitation_path(mesh, conf_subdir, "", path, sizeof(path));
180
181         if(mkdir(path, 0700)) {
182                 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
183                 return false;
184         }
185
186         return true;
187 }
188
189 /// Wipe an existing configuration directory
190 bool config_destroy(const char *confbase, const char *conf_subdir) {
191         assert(conf_subdir);
192
193         if(!confbase) {
194                 return false;
195         }
196
197         struct stat st;
198
199         char path[PATH_MAX];
200
201         // Check the presence of configuration base sub directory.
202         snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
203
204         if(stat(path, &st)) {
205                 if(errno == ENOENT) {
206                         return true;
207                 } else {
208                         logger(NULL, MESHLINK_ERROR, "Cannot stat %s: %s\n", path, strerror(errno));
209                         meshlink_errno = MESHLINK_ESTORAGE;
210                         return false;
211                 }
212         }
213
214         // Remove meshlink.conf
215         snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "meshlink.conf", confbase, conf_subdir);
216
217         if(unlink(path)) {
218                 if(errno != ENOENT) {
219                         logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
220                         meshlink_errno = MESHLINK_ESTORAGE;
221                         return false;
222                 }
223         }
224
225         snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
226         deltree(path);
227         return true;
228 }
229
230 static bool copytree(const char *src_dir_name, const void *src_key, const char *dst_dir_name, const void *dst_key) {
231         assert(src_dir_name);
232         assert(dst_dir_name);
233
234         char src_filename[PATH_MAX];
235         char dst_filename[PATH_MAX];
236         struct dirent *ent;
237
238         DIR *src_dir = opendir(src_dir_name);
239
240         if(!src_dir) {
241                 logger(NULL, MESHLINK_ERROR, "Could not open directory file %s\n", src_dir_name);
242                 return false;
243         }
244
245         // Delete if already exists and create a new destination directory
246         deltree(dst_dir_name);
247
248         if(mkdir(dst_dir_name, 0700)) {
249                 logger(NULL, MESHLINK_ERROR, "Could not create directory %s\n", dst_filename);
250                 return false;
251         }
252
253         while((ent = readdir(src_dir))) {
254                 if(ent->d_name[0] == '.') {
255                         continue;
256                 }
257
258                 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", dst_dir_name, ent->d_name);
259                 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", src_dir_name, ent->d_name);
260
261                 if(ent->d_type == DT_DIR) {
262                         if(!copytree(src_filename, src_key, dst_filename, dst_key)) {
263                                 logger(NULL, MESHLINK_ERROR, "Copying %s to %s failed\n", src_filename, dst_filename);
264                                 return false;
265                         }
266
267                         if(!sync_path(dst_filename)) {
268                                 return false;
269                         }
270                 } else if(ent->d_type == DT_REG) {
271                         struct stat st;
272                         config_t config;
273
274                         if(stat(src_filename, &st)) {
275                                 logger(NULL, MESHLINK_ERROR, "Could not stat file `%s': %s\n", src_filename, strerror(errno));
276                                 return false;
277                         }
278
279                         FILE *f = fopen(src_filename, "r");
280
281                         if(!f) {
282                                 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s\n", src_filename, strerror(errno));
283                                 return false;
284                         }
285
286                         if(!config_read_file(NULL, f, &config, src_key)) {
287                                 logger(NULL, MESHLINK_ERROR, "Failed to read `%s': %s\n", src_filename, strerror(errno));
288                                 fclose(f);
289                                 return false;
290                         }
291
292                         if(fclose(f)) {
293                                 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s\n", src_filename, strerror(errno));
294                                 config_free(&config);
295                                 return false;
296                         }
297
298                         f = fopen(dst_filename, "w");
299
300                         if(!f) {
301                                 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s", dst_filename, strerror(errno));
302                                 config_free(&config);
303                                 return false;
304                         }
305
306                         if(!config_write_file(NULL, f, &config, dst_key)) {
307                                 logger(NULL, MESHLINK_ERROR, "Failed to write `%s': %s", dst_filename, strerror(errno));
308                                 config_free(&config);
309                                 fclose(f);
310                                 return false;
311                         }
312
313                         if(fclose(f)) {
314                                 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s", dst_filename, strerror(errno));
315                                 config_free(&config);
316                                 return false;
317                         }
318
319                         config_free(&config);
320
321                         struct utimbuf times;
322                         times.modtime = st.st_mtime;
323                         times.actime = st.st_atime;
324
325                         if(utime(dst_filename, &times)) {
326                                 logger(NULL, MESHLINK_ERROR, "Failed to utime `%s': %s", dst_filename, strerror(errno));
327                                 return false;
328                         }
329                 }
330         }
331
332         closedir(src_dir);
333         return true;
334 }
335
336 bool config_copy(meshlink_handle_t *mesh, const char *src_dir_name, const void *src_key, const char *dst_dir_name, const void *dst_key) {
337         assert(src_dir_name);
338         assert(dst_dir_name);
339
340         char src_filename[PATH_MAX];
341         char dst_filename[PATH_MAX];
342
343         snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", mesh->confbase, dst_dir_name);
344         snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", mesh->confbase, src_dir_name);
345
346         return copytree(src_filename, src_key, dst_filename, dst_key);
347 }
348
349 /// Check the presence of the main configuration file.
350 bool main_config_exists(meshlink_handle_t *mesh, const char *conf_subdir) {
351         assert(conf_subdir);
352
353         if(!mesh->confbase) {
354                 return false;
355         }
356
357         char path[PATH_MAX];
358         make_main_path(mesh, conf_subdir, path, sizeof(path));
359         return access(path, F_OK) == 0;
360 }
361
362 bool config_rename(meshlink_handle_t *mesh, const char *old_conf_subdir, const char *new_conf_subdir) {
363         assert(old_conf_subdir);
364         assert(new_conf_subdir);
365
366         if(!mesh->confbase) {
367                 return false;
368         }
369
370         char old_path[PATH_MAX];
371         char new_path[PATH_MAX];
372
373         snprintf(old_path, sizeof(old_path), "%s" SLASH "%s", mesh->confbase, old_conf_subdir);
374         snprintf(new_path, sizeof(new_path), "%s" SLASH "%s", mesh->confbase, new_conf_subdir);
375
376         return rename(old_path, new_path) == 0;
377 }
378
379 bool config_sync(meshlink_handle_t *mesh, const char *conf_subdir) {
380         assert(conf_subdir);
381
382         if(!mesh->confbase) {
383                 return true;
384         }
385
386         char path[PATH_MAX];
387         snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "hosts", mesh->confbase, conf_subdir);
388
389         if(!sync_path(path)) {
390                 return false;
391         }
392
393         snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
394
395         if(!sync_path(path)) {
396                 return false;
397         }
398
399         return true;
400 }
401
402 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
403         if(!mesh->confbase) {
404                 return false;
405         }
406
407         bool confbase_exists = false;
408         bool confbase_decryptable = false;
409
410         if(main_config_exists(mesh, "current")) {
411                 confbase_exists = true;
412
413                 if(mesh->config_key && main_config_decrypt(mesh, "current")) {
414                         confbase_decryptable = true;
415                 }
416         }
417
418         if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "new")) {
419                 confbase_exists = true;
420
421                 if(main_config_decrypt(mesh, "new")) {
422                         if(!config_destroy(mesh->confbase, "current")) {
423                                 return false;
424                         }
425
426                         if(!config_rename(mesh, "new", "current")) {
427                                 return false;
428                         }
429
430                         confbase_decryptable = true;
431                 }
432         }
433
434         if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "old")) {
435                 confbase_exists = true;
436
437                 if(main_config_decrypt(mesh, "old")) {
438                         if(!config_destroy(mesh->confbase, "current")) {
439                                 return false;
440                         }
441
442                         if(!config_rename(mesh, "old", "current")) {
443                                 return false;
444                         }
445
446                         confbase_decryptable = true;
447                 }
448         }
449
450         // Cleanup if current is existing with old and new
451         if(confbase_exists && confbase_decryptable) {
452                 config_destroy(mesh->confbase, "old");
453                 config_destroy(mesh->confbase, "new");
454         }
455
456         return confbase_exists;
457 }
458
459 /// Lock the main configuration file.
460 bool main_config_lock(meshlink_handle_t *mesh) {
461         if(!mesh->confbase) {
462                 return true;
463         }
464
465         char path[PATH_MAX];
466         make_main_path(mesh, "current", path, sizeof(path));
467
468         mesh->conffile = fopen(path, "r");
469
470         if(!mesh->conffile) {
471                 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
472                 meshlink_errno = MESHLINK_ESTORAGE;
473                 return false;
474         }
475
476 #ifdef FD_CLOEXEC
477         fcntl(fileno(mesh->conffile), F_SETFD, FD_CLOEXEC);
478 #endif
479
480 #ifdef HAVE_MINGW
481         // TODO: use _locking()?
482 #else
483
484         if(flock(fileno(mesh->conffile), LOCK_EX | LOCK_NB) != 0) {
485                 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
486                 fclose(mesh->conffile);
487                 mesh->conffile = NULL;
488                 meshlink_errno = MESHLINK_EBUSY;
489                 return false;
490         }
491
492 #endif
493
494         return true;
495 }
496
497 /// Unlock the main configuration file.
498 void main_config_unlock(meshlink_handle_t *mesh) {
499         if(mesh->conffile) {
500                 fclose(mesh->conffile);
501                 mesh->conffile = NULL;
502         }
503 }
504
505 /// Read a configuration file from a FILE handle.
506 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
507         assert(f);
508
509         long len;
510
511         if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
512                 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
513                 meshlink_errno = MESHLINK_ESTORAGE;
514                 return false;
515         }
516
517         uint8_t *buf = xmalloc(len);
518
519         if(fread(buf, len, 1, f) != 1) {
520                 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
521                 meshlink_errno = MESHLINK_ESTORAGE;
522                 return false;
523         }
524
525         if(key) {
526                 uint8_t *decrypted = xmalloc(len);
527                 size_t decrypted_len = len;
528                 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
529                 chacha_poly1305_set_key(ctx, key);
530
531                 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
532                         chacha_poly1305_exit(ctx);
533                         free(buf);
534                         config->buf = decrypted;
535                         config->len = decrypted_len;
536                         return true;
537                 } else {
538                         logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
539                         meshlink_errno = MESHLINK_ESTORAGE;
540                         chacha_poly1305_exit(ctx);
541                         free(decrypted);
542                         free(buf);
543                         return false;
544                 }
545         }
546
547         config->buf = buf;
548         config->len = len;
549
550         return true;
551 }
552
553 /// Write a configuration file to a FILE handle.
554 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
555         assert(f);
556
557         if(key) {
558                 uint8_t buf[config->len + 16];
559                 size_t len = sizeof(buf);
560                 uint8_t seqbuf[12];
561                 randomize(&seqbuf, sizeof(seqbuf));
562                 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
563                 chacha_poly1305_set_key(ctx, key);
564                 bool success = false;
565
566                 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
567                         success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
568                 } else {
569                         logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
570                         meshlink_errno = MESHLINK_ESTORAGE;
571                 }
572
573                 chacha_poly1305_exit(ctx);
574                 return success;
575         }
576
577         if(fwrite(config->buf, config->len, 1, f) != 1) {
578                 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
579                 meshlink_errno = MESHLINK_ESTORAGE;
580                 return false;
581         }
582
583         if(fsync(fileno(f))) {
584                 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
585                 return false;
586         }
587
588         return true;
589 }
590
591 /// Free resources of a loaded configuration file.
592 void config_free(config_t *config) {
593         assert(!config->len || config->buf);
594
595         free((uint8_t *)config->buf);
596         config->buf = NULL;
597         config->len = 0;
598 }
599
600 /// Check the presence of a host configuration file.
601 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
602         assert(conf_subdir);
603
604         if(!mesh->confbase) {
605                 return false;
606         }
607
608         char path[PATH_MAX];
609         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
610
611         return access(path, F_OK) == 0;
612 }
613
614 /// Read a host configuration file.
615 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
616         assert(conf_subdir);
617
618         if(!mesh->confbase) {
619                 return false;
620         }
621
622         char path[PATH_MAX];
623         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
624
625         FILE *f = fopen(path, "r");
626
627         if(!f) {
628                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
629                 return false;
630         }
631
632         if(!config_read_file(mesh, f, config, key)) {
633                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
634                 fclose(f);
635                 return false;
636         }
637
638         fclose(f);
639
640         return true;
641 }
642
643 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
644         assert(conf_subdir);
645         assert(conf_type);
646
647         if(!mesh->confbase) {
648                 return true;
649         }
650
651         DIR *dir;
652         struct dirent *ent;
653         char dname[PATH_MAX];
654         snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
655
656         dir = opendir(dname);
657
658         if(!dir) {
659                 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
660                 meshlink_errno = MESHLINK_ESTORAGE;
661                 return false;
662         }
663
664         while((ent = readdir(dir))) {
665                 if(ent->d_name[0] == '.') {
666                         continue;
667                 }
668
669                 if(!action(mesh, ent->d_name, arg)) {
670                         closedir(dir);
671                         return false;
672                 }
673         }
674
675         closedir(dir);
676         return true;
677 }
678
679 /// Write a host configuration file.
680 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
681         assert(conf_subdir);
682         assert(name);
683         assert(config);
684
685         if(!mesh->confbase) {
686                 return true;
687         }
688
689         char path[PATH_MAX];
690         char tmp_path[PATH_MAX + 4];
691         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
692         snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
693
694         FILE *f = fopen(tmp_path, "w");
695
696         if(!f) {
697                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
698                 return false;
699         }
700
701         if(!config_write_file(mesh, f, config, key)) {
702                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
703                 fclose(f);
704                 return false;
705         }
706
707         if(fsync(fileno(f))) {
708                 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno));
709                 fclose(f);
710                 return false;
711         }
712
713         if(fclose(f)) {
714                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
715                 return false;
716         }
717
718         if(rename(tmp_path, path)) {
719                 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
720                 return false;
721         }
722
723         return true;
724 }
725
726 /// Read the main configuration file.
727 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
728         assert(conf_subdir);
729         assert(config);
730
731         if(!mesh->confbase) {
732                 return false;
733         }
734
735         char path[PATH_MAX];
736         make_main_path(mesh, conf_subdir, path, sizeof(path));
737
738         FILE *f = fopen(path, "r");
739
740         if(!f) {
741                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
742                 return false;
743         }
744
745         if(!config_read_file(mesh, f, config, key)) {
746                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
747                 fclose(f);
748                 return false;
749         }
750
751         fclose(f);
752
753         return true;
754 }
755
756 /// Write the main configuration file.
757 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
758         assert(conf_subdir);
759         assert(config);
760
761         if(!mesh->confbase) {
762                 return true;
763         }
764
765         char path[PATH_MAX];
766         char tmp_path[PATH_MAX + 4];
767         make_main_path(mesh, conf_subdir, path, sizeof(path));
768         snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
769
770         FILE *f = fopen(tmp_path, "w");
771
772         if(!f) {
773                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
774                 return false;
775         }
776
777         if(!config_write_file(mesh, f, config, key)) {
778                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
779                 fclose(f);
780                 return false;
781         }
782
783         if(fsync(fileno(f))) {
784                 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno));
785                 fclose(f);
786                 return false;
787         }
788
789         if(rename(tmp_path, path)) {
790                 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
791                 fclose(f);
792                 return false;
793         }
794
795         if(fclose(f)) {
796                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
797                 return false;
798         }
799
800         return true;
801 }
802
803 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
804 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
805         assert(conf_subdir);
806         assert(name);
807         assert(config);
808
809         if(!mesh->confbase) {
810                 return false;
811         }
812
813         char path[PATH_MAX];
814         char used_path[PATH_MAX];
815         make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
816         make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
817
818         // Atomically rename the invitation file
819         if(rename(path, used_path)) {
820                 if(errno == ENOENT) {
821                         logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
822                 } else {
823                         logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
824                 }
825
826                 return false;
827         }
828
829         FILE *f = fopen(used_path, "r");
830
831         if(!f) {
832                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
833                 return false;
834         }
835
836         // Check the timestamp
837         struct stat st;
838
839         if(fstat(fileno(f), &st)) {
840                 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
841                 fclose(f);
842                 unlink(used_path);
843                 return false;
844         }
845
846         if(mesh->loop.now.tv_sec > st.st_mtime + mesh->invitation_timeout) {
847                 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
848                 fclose(f);
849                 unlink(used_path);
850                 return false;
851         }
852
853         if(!config_read_file(mesh, f, config, key)) {
854                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
855                 fclose(f);
856                 unlink(used_path);
857                 return false;
858         }
859
860         fclose(f);
861
862         unlink(used_path);
863         return true;
864 }
865
866 /// Write an invitation file.
867 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
868         assert(conf_subdir);
869         assert(name);
870         assert(config);
871
872         if(!mesh->confbase) {
873                 return false;
874         }
875
876         char path[PATH_MAX];
877         make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
878
879         FILE *f = fopen(path, "w");
880
881         if(!f) {
882                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
883                 return false;
884         }
885
886         if(!config_write_file(mesh, f, config, key)) {
887                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
888                 fclose(f);
889                 return false;
890         }
891
892         if(fsync(fileno(f))) {
893                 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
894                 fclose(f);
895                 return false;
896         }
897
898         if(fclose(f)) {
899                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
900                 return false;
901         }
902
903         return true;
904 }
905
906 /// Purge old invitation files
907 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
908         if(!mesh->confbase) {
909                 return true;
910         }
911
912         char path[PATH_MAX];
913         make_invitation_path(mesh, "current", "", path, sizeof(path));
914
915         DIR *dir = opendir(path);
916
917         if(!dir) {
918                 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
919                 meshlink_errno = MESHLINK_ESTORAGE;
920                 return 0;
921         }
922
923         errno = 0;
924         size_t count = 0;
925         struct dirent *ent;
926
927         while((ent = readdir(dir))) {
928                 if(strlen(ent->d_name) != 24) {
929                         continue;
930                 }
931
932                 char invname[PATH_MAX];
933                 struct stat st;
934
935                 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
936                         logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
937                         continue;
938                 }
939
940                 if(!stat(invname, &st)) {
941                         if(mesh->invitation_key && deadline < st.st_mtime) {
942                                 count++;
943                         } else {
944                                 unlink(invname);
945                         }
946                 } else {
947                         logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
948                         errno = 0;
949                 }
950         }
951
952         if(errno) {
953                 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
954                 closedir(dir);
955                 meshlink_errno = MESHLINK_ESTORAGE;
956                 return 0;
957         }
958
959         closedir(dir);
960
961         return count;
962 }