]> git.meshlink.io Git - meshlink/blob - src/conf.c
212b7c4ac7ae53d832bfe9e54e89344fa36b982e
[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                 if(!config_destroy(mesh->confbase, "old") || !config_destroy(mesh->confbase, "new")) {
448                         return false;
449                 }
450         }
451
452         return confbase_exists;
453 }
454
455 /// Lock the main configuration file. Creates confbase if necessary.
456 bool main_config_lock(meshlink_handle_t *mesh) {
457         if(!mesh->confbase) {
458                 return true;
459         }
460
461         if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
462                 logger(NULL, MESHLINK_ERROR, "Cannot create configuration directory %s: %s", mesh->confbase, strerror(errno));
463                 meshlink_close(mesh);
464                 meshlink_errno = MESHLINK_ESTORAGE;
465                 return NULL;
466         }
467
468         char path[PATH_MAX];
469         snprintf(path, sizeof(path), "%s" SLASH "meshlink.lock", mesh->confbase);
470
471         mesh->lockfile = fopen(path, "w+");
472
473         if(!mesh->lockfile) {
474                 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
475                 meshlink_errno = MESHLINK_ESTORAGE;
476                 return false;
477         }
478
479 #ifdef FD_CLOEXEC
480         fcntl(fileno(mesh->lockfile), F_SETFD, FD_CLOEXEC);
481 #endif
482
483 #ifdef HAVE_MINGW
484         // TODO: use _locking()?
485 #else
486
487         if(flock(fileno(mesh->lockfile), LOCK_EX | LOCK_NB) != 0) {
488                 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
489                 fclose(mesh->lockfile);
490                 mesh->lockfile = NULL;
491                 meshlink_errno = MESHLINK_EBUSY;
492                 return false;
493         }
494
495 #endif
496
497         return true;
498 }
499
500 /// Unlock the main configuration file.
501 void main_config_unlock(meshlink_handle_t *mesh) {
502         if(mesh->lockfile) {
503                 fclose(mesh->lockfile);
504                 mesh->lockfile = NULL;
505         }
506 }
507
508 /// Read a configuration file from a FILE handle.
509 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
510         assert(f);
511
512         long len;
513
514         if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
515                 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
516                 meshlink_errno = MESHLINK_ESTORAGE;
517                 return false;
518         }
519
520         uint8_t *buf = xmalloc(len);
521
522         if(fread(buf, len, 1, f) != 1) {
523                 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
524                 meshlink_errno = MESHLINK_ESTORAGE;
525                 return false;
526         }
527
528         if(key) {
529                 uint8_t *decrypted = xmalloc(len);
530                 size_t decrypted_len = len;
531                 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
532                 chacha_poly1305_set_key(ctx, key);
533
534                 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
535                         chacha_poly1305_exit(ctx);
536                         free(buf);
537                         config->buf = decrypted;
538                         config->len = decrypted_len;
539                         return true;
540                 } else {
541                         logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
542                         meshlink_errno = MESHLINK_ESTORAGE;
543                         chacha_poly1305_exit(ctx);
544                         free(decrypted);
545                         free(buf);
546                         return false;
547                 }
548         }
549
550         config->buf = buf;
551         config->len = len;
552
553         return true;
554 }
555
556 /// Write a configuration file to a FILE handle.
557 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
558         assert(f);
559
560         if(key) {
561                 uint8_t buf[config->len + 16];
562                 size_t len = sizeof(buf);
563                 uint8_t seqbuf[12];
564                 randomize(&seqbuf, sizeof(seqbuf));
565                 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
566                 chacha_poly1305_set_key(ctx, key);
567                 bool success = false;
568
569                 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
570                         success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
571
572                         if(!success) {
573                                 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
574                         }
575
576                         meshlink_errno = MESHLINK_ESTORAGE;
577                 } else {
578                         logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
579                         meshlink_errno = MESHLINK_ESTORAGE;
580                 }
581
582                 chacha_poly1305_exit(ctx);
583                 return success;
584         }
585
586         if(fwrite(config->buf, config->len, 1, f) != 1) {
587                 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
588                 meshlink_errno = MESHLINK_ESTORAGE;
589                 return false;
590         }
591
592         if(fflush(f)) {
593                 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
594                 meshlink_errno = MESHLINK_ESTORAGE;
595                 return false;
596         }
597
598         if(fsync(fileno(f))) {
599                 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
600                 meshlink_errno = MESHLINK_ESTORAGE;
601                 return false;
602         }
603
604         return true;
605 }
606
607 /// Free resources of a loaded configuration file.
608 void config_free(config_t *config) {
609         assert(!config->len || config->buf);
610
611         free((uint8_t *)config->buf);
612         config->buf = NULL;
613         config->len = 0;
614 }
615
616 /// Check the presence of a host configuration file.
617 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
618         assert(conf_subdir);
619
620         if(!mesh->confbase) {
621                 return false;
622         }
623
624         char path[PATH_MAX];
625         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
626
627         return access(path, F_OK) == 0;
628 }
629
630 /// Read a host configuration file.
631 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
632         assert(conf_subdir);
633
634         if(!mesh->confbase) {
635                 return false;
636         }
637
638         char path[PATH_MAX];
639         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
640
641         FILE *f = fopen(path, "r");
642
643         if(!f) {
644                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
645                 return false;
646         }
647
648         if(!config_read_file(mesh, f, config, key)) {
649                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
650                 fclose(f);
651                 return false;
652         }
653
654         fclose(f);
655
656         return true;
657 }
658
659 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
660         assert(conf_subdir);
661         assert(conf_type);
662
663         if(!mesh->confbase) {
664                 return true;
665         }
666
667         DIR *dir;
668         struct dirent *ent;
669         char dname[PATH_MAX];
670         snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
671
672         dir = opendir(dname);
673
674         if(!dir) {
675                 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
676                 meshlink_errno = MESHLINK_ESTORAGE;
677                 return false;
678         }
679
680         while((ent = readdir(dir))) {
681                 if(ent->d_name[0] == '.') {
682                         continue;
683                 }
684
685                 if(!action(mesh, ent->d_name, arg)) {
686                         closedir(dir);
687                         return false;
688                 }
689         }
690
691         closedir(dir);
692         return true;
693 }
694
695 /// Write a host configuration file.
696 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
697         assert(conf_subdir);
698         assert(name);
699         assert(config);
700
701         if(!mesh->confbase) {
702                 return true;
703         }
704
705         char path[PATH_MAX];
706         char tmp_path[PATH_MAX + 4];
707         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
708         snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
709
710         FILE *f = fopen(tmp_path, "w");
711
712         if(!f) {
713                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
714                 meshlink_errno = MESHLINK_ESTORAGE;
715                 return false;
716         }
717
718         if(!config_write_file(mesh, f, config, key)) {
719                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
720                 fclose(f);
721                 return false;
722         }
723
724         if(fclose(f)) {
725                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
726                 meshlink_errno = MESHLINK_ESTORAGE;
727                 return false;
728         }
729
730         if(rename(tmp_path, path)) {
731                 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
732                 meshlink_errno = MESHLINK_ESTORAGE;
733                 return false;
734         }
735
736         return true;
737 }
738
739 /// Delete a host configuration file.
740 bool config_delete(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
741         assert(conf_subdir);
742         assert(name);
743
744         if(!mesh->confbase) {
745                 return true;
746         }
747
748         char path[PATH_MAX];
749         make_host_path(mesh, conf_subdir, name, path, sizeof(path));
750
751         if(unlink(path) && errno != ENOENT) {
752                 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
753                 meshlink_errno = MESHLINK_ESTORAGE;
754                 return false;
755         }
756
757         return true;
758 }
759
760 /// Read the main configuration file.
761 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
762         assert(conf_subdir);
763         assert(config);
764
765         if(!mesh->confbase) {
766                 return false;
767         }
768
769         char path[PATH_MAX];
770         make_main_path(mesh, conf_subdir, path, sizeof(path));
771
772         FILE *f = fopen(path, "r");
773
774         if(!f) {
775                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
776                 return false;
777         }
778
779         if(!config_read_file(mesh, f, config, key)) {
780                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
781                 fclose(f);
782                 return false;
783         }
784
785         fclose(f);
786
787         return true;
788 }
789
790 /// Write the main configuration file.
791 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
792         assert(conf_subdir);
793         assert(config);
794
795         if(!mesh->confbase) {
796                 return true;
797         }
798
799         char path[PATH_MAX];
800         char tmp_path[PATH_MAX + 4];
801         make_main_path(mesh, conf_subdir, path, sizeof(path));
802         snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
803
804         FILE *f = fopen(tmp_path, "w");
805
806         if(!f) {
807                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
808                 meshlink_errno = MESHLINK_ESTORAGE;
809                 return false;
810         }
811
812         if(!config_write_file(mesh, f, config, key)) {
813                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
814                 fclose(f);
815                 return false;
816         }
817
818         if(rename(tmp_path, path)) {
819                 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
820                 meshlink_errno = MESHLINK_ESTORAGE;
821                 fclose(f);
822                 return false;
823         }
824
825         if(fclose(f)) {
826                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
827                 meshlink_errno = MESHLINK_ESTORAGE;
828                 return false;
829         }
830
831         return true;
832 }
833
834 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
835 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
836         assert(conf_subdir);
837         assert(name);
838         assert(config);
839
840         if(!mesh->confbase) {
841                 return false;
842         }
843
844         char path[PATH_MAX];
845         char used_path[PATH_MAX];
846         make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
847         make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
848
849         // Atomically rename the invitation file
850         if(rename(path, used_path)) {
851                 if(errno == ENOENT) {
852                         logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
853                 } else {
854                         logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
855                 }
856
857                 return false;
858         }
859
860         FILE *f = fopen(used_path, "r");
861
862         if(!f) {
863                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
864                 return false;
865         }
866
867         // Check the timestamp
868         struct stat st;
869
870         if(fstat(fileno(f), &st)) {
871                 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
872                 fclose(f);
873                 unlink(used_path);
874                 return false;
875         }
876
877         if(mesh->loop.now.tv_sec >= st.st_mtime + mesh->invitation_timeout) {
878                 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
879                 fclose(f);
880                 unlink(used_path);
881                 return false;
882         }
883
884         if(!config_read_file(mesh, f, config, key)) {
885                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
886                 fclose(f);
887                 unlink(used_path);
888                 return false;
889         }
890
891         fclose(f);
892
893         if(unlink(used_path)) {
894                 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
895                 return false;
896         }
897
898         snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
899
900         if(!sync_path(path)) {
901                 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
902                 meshlink_errno = MESHLINK_ESTORAGE;
903                 return false;
904         }
905
906         return true;
907 }
908
909 /// Write an invitation file.
910 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
911         assert(conf_subdir);
912         assert(name);
913         assert(config);
914
915         if(!mesh->confbase) {
916                 return false;
917         }
918
919         char path[PATH_MAX];
920         make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
921
922         FILE *f = fopen(path, "w");
923
924         if(!f) {
925                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
926                 meshlink_errno = MESHLINK_ESTORAGE;
927                 return false;
928         }
929
930         if(!config_write_file(mesh, f, config, key)) {
931                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
932                 fclose(f);
933                 return false;
934         }
935
936         if(fclose(f)) {
937                 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
938                 meshlink_errno = MESHLINK_ESTORAGE;
939                 return false;
940         }
941
942         snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
943
944         if(!sync_path(path)) {
945                 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
946                 meshlink_errno = MESHLINK_ESTORAGE;
947                 return false;
948         }
949
950         return true;
951 }
952
953 /// Purge old invitation files
954 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
955         if(!mesh->confbase) {
956                 return true;
957         }
958
959         char path[PATH_MAX];
960         make_invitation_path(mesh, "current", "", path, sizeof(path));
961
962         DIR *dir = opendir(path);
963
964         if(!dir) {
965                 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
966                 meshlink_errno = MESHLINK_ESTORAGE;
967                 return 0;
968         }
969
970         errno = 0;
971         size_t count = 0;
972         struct dirent *ent;
973
974         while((ent = readdir(dir))) {
975                 if(strlen(ent->d_name) != 24) {
976                         continue;
977                 }
978
979                 char invname[PATH_MAX];
980                 struct stat st;
981
982                 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
983                         logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
984                         continue;
985                 }
986
987                 if(!stat(invname, &st)) {
988                         if(mesh->invitation_key && deadline < st.st_mtime) {
989                                 count++;
990                         } else {
991                                 unlink(invname);
992                         }
993                 } else {
994                         logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
995                         errno = 0;
996                 }
997         }
998
999         if(errno) {
1000                 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
1001                 closedir(dir);
1002                 meshlink_errno = MESHLINK_ESTORAGE;
1003                 return 0;
1004         }
1005
1006         closedir(dir);
1007
1008         return count;
1009 }