]> git.meshlink.io Git - meshlink/blob - src/conf.c
Add missing calls to fflush().
[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(fflush(f)) {
584                 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
585                 meshlink_errno = MESHLINK_ESTORAGE;
586                 return false;
587         }
588
589         if(fsync(fileno(f))) {
590                 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
591                 return false;
592         }
593
594         return true;
595 }
596
597 /// Free resources of a loaded configuration file.
598 void config_free(config_t *config) {
599         assert(!config->len || config->buf);
600
601         free((uint8_t *)config->buf);
602         config->buf = NULL;
603         config->len = 0;
604 }
605
606 /// Check the presence of a host configuration file.
607 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
608         assert(conf_subdir);
609
610         if(!mesh->confbase) {
611                 return false;
612         }
613
614         char path[PATH_MAX];
615         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
616
617         return access(path, F_OK) == 0;
618 }
619
620 /// Read a host configuration file.
621 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
622         assert(conf_subdir);
623
624         if(!mesh->confbase) {
625                 return false;
626         }
627
628         char path[PATH_MAX];
629         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
630
631         FILE *f = fopen(path, "r");
632
633         if(!f) {
634                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
635                 return false;
636         }
637
638         if(!config_read_file(mesh, f, config, key)) {
639                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
640                 fclose(f);
641                 return false;
642         }
643
644         fclose(f);
645
646         return true;
647 }
648
649 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
650         assert(conf_subdir);
651         assert(conf_type);
652
653         if(!mesh->confbase) {
654                 return true;
655         }
656
657         DIR *dir;
658         struct dirent *ent;
659         char dname[PATH_MAX];
660         snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
661
662         dir = opendir(dname);
663
664         if(!dir) {
665                 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
666                 meshlink_errno = MESHLINK_ESTORAGE;
667                 return false;
668         }
669
670         while((ent = readdir(dir))) {
671                 if(ent->d_name[0] == '.') {
672                         continue;
673                 }
674
675                 if(!action(mesh, ent->d_name, arg)) {
676                         closedir(dir);
677                         return false;
678                 }
679         }
680
681         closedir(dir);
682         return true;
683 }
684
685 /// Write a host configuration file.
686 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
687         assert(conf_subdir);
688         assert(name);
689         assert(config);
690
691         if(!mesh->confbase) {
692                 return true;
693         }
694
695         char path[PATH_MAX];
696         char tmp_path[PATH_MAX + 4];
697         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
698         snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
699
700         FILE *f = fopen(tmp_path, "w");
701
702         if(!f) {
703                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
704                 return false;
705         }
706
707         if(!config_write_file(mesh, f, config, key)) {
708                 logger(mesh, MESHLINK_ERROR, "Failed to write `%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(rename(tmp_path, path)) {
784                 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
785                 fclose(f);
786                 return false;
787         }
788
789         if(fclose(f)) {
790                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
791                 return false;
792         }
793
794         return true;
795 }
796
797 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
798 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
799         assert(conf_subdir);
800         assert(name);
801         assert(config);
802
803         if(!mesh->confbase) {
804                 return false;
805         }
806
807         char path[PATH_MAX];
808         char used_path[PATH_MAX];
809         make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
810         make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
811
812         // Atomically rename the invitation file
813         if(rename(path, used_path)) {
814                 if(errno == ENOENT) {
815                         logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
816                 } else {
817                         logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
818                 }
819
820                 return false;
821         }
822
823         FILE *f = fopen(used_path, "r");
824
825         if(!f) {
826                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
827                 return false;
828         }
829
830         // Check the timestamp
831         struct stat st;
832
833         if(fstat(fileno(f), &st)) {
834                 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
835                 fclose(f);
836                 unlink(used_path);
837                 return false;
838         }
839
840         if(mesh->loop.now.tv_sec >= st.st_mtime + mesh->invitation_timeout) {
841                 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
842                 fclose(f);
843                 unlink(used_path);
844                 return false;
845         }
846
847         if(!config_read_file(mesh, f, config, key)) {
848                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
849                 fclose(f);
850                 unlink(used_path);
851                 return false;
852         }
853
854         fclose(f);
855
856         unlink(used_path);
857         return true;
858 }
859
860 /// Write an invitation file.
861 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
862         assert(conf_subdir);
863         assert(name);
864         assert(config);
865
866         if(!mesh->confbase) {
867                 return false;
868         }
869
870         char path[PATH_MAX];
871         make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
872
873         FILE *f = fopen(path, "w");
874
875         if(!f) {
876                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
877                 return false;
878         }
879
880         if(!config_write_file(mesh, f, config, key)) {
881                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
882                 fclose(f);
883                 return false;
884         }
885
886         if(fclose(f)) {
887                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
888                 return false;
889         }
890
891         return true;
892 }
893
894 /// Purge old invitation files
895 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
896         if(!mesh->confbase) {
897                 return true;
898         }
899
900         char path[PATH_MAX];
901         make_invitation_path(mesh, "current", "", path, sizeof(path));
902
903         DIR *dir = opendir(path);
904
905         if(!dir) {
906                 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
907                 meshlink_errno = MESHLINK_ESTORAGE;
908                 return 0;
909         }
910
911         errno = 0;
912         size_t count = 0;
913         struct dirent *ent;
914
915         while((ent = readdir(dir))) {
916                 if(strlen(ent->d_name) != 24) {
917                         continue;
918                 }
919
920                 char invname[PATH_MAX];
921                 struct stat st;
922
923                 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
924                         logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
925                         continue;
926                 }
927
928                 if(!stat(invname, &st)) {
929                         if(mesh->invitation_key && deadline < st.st_mtime) {
930                                 count++;
931                         } else {
932                                 unlink(invname);
933                         }
934                 } else {
935                         logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
936                         errno = 0;
937                 }
938         }
939
940         if(errno) {
941                 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
942                 closedir(dir);
943                 meshlink_errno = MESHLINK_ESTORAGE;
944                 return 0;
945         }
946
947         closedir(dir);
948
949         return count;
950 }