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