]> git.meshlink.io Git - meshlink/blob - src/conf.c
eea9c3dd6245b8e8432af30f511d41c6f0f0ace6
[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
569                         if(!success) {
570                                 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
571                         }
572
573                         meshlink_errno = MESHLINK_ESTORAGE;
574                 } else {
575                         logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
576                         meshlink_errno = MESHLINK_ESTORAGE;
577                 }
578
579                 chacha_poly1305_exit(ctx);
580                 return success;
581         }
582
583         if(fwrite(config->buf, config->len, 1, f) != 1) {
584                 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
585                 meshlink_errno = MESHLINK_ESTORAGE;
586                 return false;
587         }
588
589         if(fflush(f)) {
590                 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
591                 meshlink_errno = MESHLINK_ESTORAGE;
592                 return false;
593         }
594
595         if(fsync(fileno(f))) {
596                 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
597                 meshlink_errno = MESHLINK_ESTORAGE;
598                 return false;
599         }
600
601         return true;
602 }
603
604 /// Free resources of a loaded configuration file.
605 void config_free(config_t *config) {
606         assert(!config->len || config->buf);
607
608         free((uint8_t *)config->buf);
609         config->buf = NULL;
610         config->len = 0;
611 }
612
613 /// Check the presence of a host configuration file.
614 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
615         assert(conf_subdir);
616
617         if(!mesh->confbase) {
618                 return false;
619         }
620
621         char path[PATH_MAX];
622         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
623
624         return access(path, F_OK) == 0;
625 }
626
627 /// Read a host configuration file.
628 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
629         assert(conf_subdir);
630
631         if(!mesh->confbase) {
632                 return false;
633         }
634
635         char path[PATH_MAX];
636         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
637
638         FILE *f = fopen(path, "r");
639
640         if(!f) {
641                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
642                 return false;
643         }
644
645         if(!config_read_file(mesh, f, config, key)) {
646                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
647                 fclose(f);
648                 return false;
649         }
650
651         fclose(f);
652
653         return true;
654 }
655
656 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
657         assert(conf_subdir);
658         assert(conf_type);
659
660         if(!mesh->confbase) {
661                 return true;
662         }
663
664         DIR *dir;
665         struct dirent *ent;
666         char dname[PATH_MAX];
667         snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
668
669         dir = opendir(dname);
670
671         if(!dir) {
672                 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
673                 meshlink_errno = MESHLINK_ESTORAGE;
674                 return false;
675         }
676
677         while((ent = readdir(dir))) {
678                 if(ent->d_name[0] == '.') {
679                         continue;
680                 }
681
682                 if(!action(mesh, ent->d_name, arg)) {
683                         closedir(dir);
684                         return false;
685                 }
686         }
687
688         closedir(dir);
689         return true;
690 }
691
692 /// Write a host configuration file.
693 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
694         assert(conf_subdir);
695         assert(name);
696         assert(config);
697
698         if(!mesh->confbase) {
699                 return true;
700         }
701
702         char path[PATH_MAX];
703         char tmp_path[PATH_MAX + 4];
704         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
705         snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
706
707         FILE *f = fopen(tmp_path, "w");
708
709         if(!f) {
710                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
711                 meshlink_errno = MESHLINK_ESTORAGE;
712                 return false;
713         }
714
715         if(!config_write_file(mesh, f, config, key)) {
716                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
717                 fclose(f);
718                 return false;
719         }
720
721         if(fclose(f)) {
722                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
723                 meshlink_errno = MESHLINK_ESTORAGE;
724                 return false;
725         }
726
727         if(rename(tmp_path, path)) {
728                 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
729                 meshlink_errno = MESHLINK_ESTORAGE;
730                 return false;
731         }
732
733         return true;
734 }
735
736 /// Read the main configuration file.
737 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
738         assert(conf_subdir);
739         assert(config);
740
741         if(!mesh->confbase) {
742                 return false;
743         }
744
745         char path[PATH_MAX];
746         make_main_path(mesh, conf_subdir, path, sizeof(path));
747
748         FILE *f = fopen(path, "r");
749
750         if(!f) {
751                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
752                 return false;
753         }
754
755         if(!config_read_file(mesh, f, config, key)) {
756                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
757                 fclose(f);
758                 return false;
759         }
760
761         fclose(f);
762
763         return true;
764 }
765
766 /// Write the main configuration file.
767 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
768         assert(conf_subdir);
769         assert(config);
770
771         if(!mesh->confbase) {
772                 return true;
773         }
774
775         char path[PATH_MAX];
776         char tmp_path[PATH_MAX + 4];
777         make_main_path(mesh, conf_subdir, path, sizeof(path));
778         snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
779
780         FILE *f = fopen(tmp_path, "w");
781
782         if(!f) {
783                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
784                 meshlink_errno = MESHLINK_ESTORAGE;
785                 return false;
786         }
787
788         if(!config_write_file(mesh, f, config, key)) {
789                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
790                 fclose(f);
791                 return false;
792         }
793
794         if(rename(tmp_path, path)) {
795                 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
796                 meshlink_errno = MESHLINK_ESTORAGE;
797                 fclose(f);
798                 return false;
799         }
800
801         if(fclose(f)) {
802                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
803                 meshlink_errno = MESHLINK_ESTORAGE;
804                 return false;
805         }
806
807         return true;
808 }
809
810 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
811 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
812         assert(conf_subdir);
813         assert(name);
814         assert(config);
815
816         if(!mesh->confbase) {
817                 return false;
818         }
819
820         char path[PATH_MAX];
821         char used_path[PATH_MAX];
822         make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
823         make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
824
825         // Atomically rename the invitation file
826         if(rename(path, used_path)) {
827                 if(errno == ENOENT) {
828                         logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
829                 } else {
830                         logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
831                 }
832
833                 return false;
834         }
835
836         FILE *f = fopen(used_path, "r");
837
838         if(!f) {
839                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
840                 return false;
841         }
842
843         // Check the timestamp
844         struct stat st;
845
846         if(fstat(fileno(f), &st)) {
847                 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
848                 fclose(f);
849                 unlink(used_path);
850                 return false;
851         }
852
853         if(mesh->loop.now.tv_sec >= st.st_mtime + mesh->invitation_timeout) {
854                 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
855                 fclose(f);
856                 unlink(used_path);
857                 return false;
858         }
859
860         if(!config_read_file(mesh, f, config, key)) {
861                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
862                 fclose(f);
863                 unlink(used_path);
864                 return false;
865         }
866
867         fclose(f);
868
869         unlink(used_path);
870         return true;
871 }
872
873 /// Write an invitation file.
874 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
875         assert(conf_subdir);
876         assert(name);
877         assert(config);
878
879         if(!mesh->confbase) {
880                 return false;
881         }
882
883         char path[PATH_MAX];
884         make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
885
886         FILE *f = fopen(path, "w");
887
888         if(!f) {
889                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
890                 meshlink_errno = MESHLINK_ESTORAGE;
891                 return false;
892         }
893
894         if(!config_write_file(mesh, f, config, key)) {
895                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
896                 fclose(f);
897                 return false;
898         }
899
900         if(fclose(f)) {
901                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
902                 meshlink_errno = MESHLINK_ESTORAGE;
903                 return false;
904         }
905
906         snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
907
908         if(!sync_path(path)) {
909                 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
910                 meshlink_errno = MESHLINK_ESTORAGE;
911                 return false;
912         }
913
914         return true;
915 }
916
917 /// Purge old invitation files
918 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
919         if(!mesh->confbase) {
920                 return true;
921         }
922
923         char path[PATH_MAX];
924         make_invitation_path(mesh, "current", "", path, sizeof(path));
925
926         DIR *dir = opendir(path);
927
928         if(!dir) {
929                 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
930                 meshlink_errno = MESHLINK_ESTORAGE;
931                 return 0;
932         }
933
934         errno = 0;
935         size_t count = 0;
936         struct dirent *ent;
937
938         while((ent = readdir(dir))) {
939                 if(strlen(ent->d_name) != 24) {
940                         continue;
941                 }
942
943                 char invname[PATH_MAX];
944                 struct stat st;
945
946                 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
947                         logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
948                         continue;
949                 }
950
951                 if(!stat(invname, &st)) {
952                         if(mesh->invitation_key && deadline < st.st_mtime) {
953                                 count++;
954                         } else {
955                                 unlink(invname);
956                         }
957                 } else {
958                         logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
959                         errno = 0;
960                 }
961         }
962
963         if(errno) {
964                 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
965                 closedir(dir);
966                 meshlink_errno = MESHLINK_ESTORAGE;
967                 return 0;
968         }
969
970         closedir(dir);
971
972         return count;
973 }