2 econf.c -- configuration code
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
22 #include <sys/types.h>
28 #include "meshlink_internal.h"
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) {
38 snprintf(path, len, "%s" SLASH "%s" SLASH "meshlink.conf", mesh->confbase, conf_subdir);
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) {
48 snprintf(path, len, "%s" SLASH "%s" SLASH "hosts" SLASH "%s", mesh->confbase, conf_subdir, name);
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) {
58 snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s", mesh->confbase, conf_subdir, name);
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) {
68 snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, conf_subdir, name);
71 /// Remove a directory recursively
72 static bool deltree(const char *dirname) {
75 DIR *d = opendir(dirname);
80 while((ent = readdir(d))) {
81 if(ent->d_name[0] == '.') {
85 char filename[PATH_MAX];
86 snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
88 if(unlink(filename)) {
89 if(!deltree(filename)) {
97 return errno == ENOENT;
100 return rmdir(dirname) == 0;
103 bool sync_path(const char *pathname) {
106 int fd = open(pathname, O_RDONLY);
109 logger(NULL, MESHLINK_ERROR, "Failed to open %s: %s\n", pathname, strerror(errno));
110 meshlink_errno = MESHLINK_ESTORAGE;
115 logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
117 meshlink_errno = MESHLINK_ESTORAGE;
122 logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
124 meshlink_errno = MESHLINK_ESTORAGE;
131 /// Try decrypting the main configuration file from the given sub-directory.
132 static bool main_config_decrypt(meshlink_handle_t *mesh, const char *conf_subdir) {
133 assert(mesh->config_key);
134 assert(mesh->confbase);
139 if(!main_config_read(mesh, conf_subdir, &config, mesh->config_key)) {
140 logger(mesh, MESHLINK_ERROR, "Could not read main configuration file");
144 packmsg_input_t in = {config.buf, config.len};
146 uint32_t version = packmsg_get_uint32(&in);
147 config_free(&config);
149 return version == MESHLINK_CONFIG_VERSION;
152 /// Create a fresh configuration directory
153 bool config_init(meshlink_handle_t *mesh, const char *conf_subdir) {
156 if(!mesh->confbase) {
162 // Create "current" sub-directory in the confbase
163 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
166 logger(mesh, MESHLINK_DEBUG, "Could not delete directory %s: %s\n", path, strerror(errno));
170 if(mkdir(path, 0700)) {
171 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
175 make_host_path(mesh, conf_subdir, "", path, sizeof(path));
177 if(mkdir(path, 0700)) {
178 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
182 make_invitation_path(mesh, conf_subdir, "", path, sizeof(path));
184 if(mkdir(path, 0700)) {
185 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
192 /// Wipe an existing configuration directory
193 bool config_destroy(const char *confbase, const char *conf_subdir) {
204 // Check the presence of configuration base sub directory.
205 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
207 if(stat(path, &st)) {
208 if(errno == ENOENT) {
211 logger(NULL, MESHLINK_ERROR, "Cannot stat %s: %s\n", path, strerror(errno));
212 meshlink_errno = MESHLINK_ESTORAGE;
217 // Remove meshlink.conf
218 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "meshlink.conf", confbase, conf_subdir);
221 if(errno != ENOENT) {
222 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
223 meshlink_errno = MESHLINK_ESTORAGE;
228 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
231 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
232 meshlink_errno = MESHLINK_ESTORAGE;
236 return sync_path(confbase);
239 static bool copytree(const char *src_dir_name, const void *src_key, const char *dst_dir_name, const void *dst_key) {
240 assert(src_dir_name);
241 assert(dst_dir_name);
243 char src_filename[PATH_MAX];
244 char dst_filename[PATH_MAX];
247 DIR *src_dir = opendir(src_dir_name);
250 logger(NULL, MESHLINK_ERROR, "Could not open directory file %s\n", src_dir_name);
251 meshlink_errno = MESHLINK_ESTORAGE;
255 // Delete if already exists and create a new destination directory
256 if(!deltree(dst_dir_name)) {
257 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", dst_dir_name, strerror(errno));
258 meshlink_errno = MESHLINK_ESTORAGE;
262 if(mkdir(dst_dir_name, 0700)) {
263 logger(NULL, MESHLINK_ERROR, "Could not create directory %s\n", dst_filename);
264 meshlink_errno = MESHLINK_ESTORAGE;
268 while((ent = readdir(src_dir))) {
269 if(ent->d_name[0] == '.') {
273 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", dst_dir_name, ent->d_name);
274 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", src_dir_name, ent->d_name);
276 if(ent->d_type == DT_DIR) {
277 if(!copytree(src_filename, src_key, dst_filename, dst_key)) {
278 logger(NULL, MESHLINK_ERROR, "Copying %s to %s failed\n", src_filename, dst_filename);
279 meshlink_errno = MESHLINK_ESTORAGE;
283 if(!sync_path(dst_filename)) {
286 } else if(ent->d_type == DT_REG) {
290 if(stat(src_filename, &st)) {
291 logger(NULL, MESHLINK_ERROR, "Could not stat file `%s': %s\n", src_filename, strerror(errno));
292 meshlink_errno = MESHLINK_ESTORAGE;
296 FILE *f = fopen(src_filename, "r");
299 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s\n", src_filename, strerror(errno));
300 meshlink_errno = MESHLINK_ESTORAGE;
304 if(!config_read_file(NULL, f, &config, src_key)) {
305 logger(NULL, MESHLINK_ERROR, "Failed to read `%s': %s\n", src_filename, strerror(errno));
307 meshlink_errno = MESHLINK_ESTORAGE;
312 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s\n", src_filename, strerror(errno));
313 config_free(&config);
314 meshlink_errno = MESHLINK_ESTORAGE;
318 f = fopen(dst_filename, "w");
321 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s", dst_filename, strerror(errno));
322 config_free(&config);
323 meshlink_errno = MESHLINK_ESTORAGE;
327 if(!config_write_file(NULL, f, &config, dst_key)) {
328 logger(NULL, MESHLINK_ERROR, "Failed to write `%s': %s", dst_filename, strerror(errno));
329 config_free(&config);
331 meshlink_errno = MESHLINK_ESTORAGE;
336 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s", dst_filename, strerror(errno));
337 config_free(&config);
338 meshlink_errno = MESHLINK_ESTORAGE;
342 config_free(&config);
344 struct utimbuf times;
345 times.modtime = st.st_mtime;
346 times.actime = st.st_atime;
348 if(utime(dst_filename, ×)) {
349 logger(NULL, MESHLINK_ERROR, "Failed to utime `%s': %s", dst_filename, strerror(errno));
350 meshlink_errno = MESHLINK_ESTORAGE;
360 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) {
361 assert(src_dir_name);
362 assert(dst_dir_name);
364 char src_filename[PATH_MAX];
365 char dst_filename[PATH_MAX];
367 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", mesh->confbase, dst_dir_name);
368 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", mesh->confbase, src_dir_name);
370 return copytree(src_filename, src_key, dst_filename, dst_key);
373 /// Check the presence of the main configuration file.
374 bool main_config_exists(meshlink_handle_t *mesh, const char *conf_subdir) {
377 if(!mesh->confbase) {
382 make_main_path(mesh, conf_subdir, path, sizeof(path));
383 return access(path, F_OK) == 0;
386 bool config_rename(meshlink_handle_t *mesh, const char *old_conf_subdir, const char *new_conf_subdir) {
387 assert(old_conf_subdir);
388 assert(new_conf_subdir);
390 if(!mesh->confbase) {
394 char old_path[PATH_MAX];
395 char new_path[PATH_MAX];
397 snprintf(old_path, sizeof(old_path), "%s" SLASH "%s", mesh->confbase, old_conf_subdir);
398 snprintf(new_path, sizeof(new_path), "%s" SLASH "%s", mesh->confbase, new_conf_subdir);
400 return rename(old_path, new_path) == 0 && sync_path(mesh->confbase);
403 bool config_sync(meshlink_handle_t *mesh, const char *conf_subdir) {
406 if(!mesh->confbase) {
411 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "hosts", mesh->confbase, conf_subdir);
413 if(!sync_path(path)) {
417 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
419 if(!sync_path(path)) {
426 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
427 if(!mesh->confbase) {
431 bool confbase_exists = false;
432 bool confbase_decryptable = false;
434 if(main_config_exists(mesh, "current")) {
435 confbase_exists = true;
437 if(mesh->config_key && main_config_decrypt(mesh, "current")) {
438 confbase_decryptable = true;
442 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "new")) {
443 confbase_exists = true;
445 if(main_config_decrypt(mesh, "new")) {
446 if(!config_destroy(mesh->confbase, "current")) {
450 if(!config_rename(mesh, "new", "current")) {
454 confbase_decryptable = true;
458 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "old")) {
459 confbase_exists = true;
461 if(main_config_decrypt(mesh, "old")) {
462 if(!config_destroy(mesh->confbase, "current")) {
466 if(!config_rename(mesh, "old", "current")) {
470 confbase_decryptable = true;
474 // Cleanup if current is existing with old and new
475 if(confbase_exists && confbase_decryptable) {
476 if(!config_destroy(mesh->confbase, "old") || !config_destroy(mesh->confbase, "new")) {
481 return confbase_exists;
484 /// Lock the main configuration file. Creates confbase if necessary.
485 bool main_config_lock(meshlink_handle_t *mesh) {
486 if(!mesh->confbase) {
490 if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
491 logger(NULL, MESHLINK_ERROR, "Cannot create configuration directory %s: %s", mesh->confbase, strerror(errno));
492 meshlink_close(mesh);
493 meshlink_errno = MESHLINK_ESTORAGE;
498 snprintf(path, sizeof(path), "%s" SLASH "meshlink.lock", mesh->confbase);
500 mesh->lockfile = fopen(path, "w+");
502 if(!mesh->lockfile) {
503 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
504 meshlink_errno = MESHLINK_ESTORAGE;
509 fcntl(fileno(mesh->lockfile), F_SETFD, FD_CLOEXEC);
513 // TODO: use _locking()?
516 if(flock(fileno(mesh->lockfile), LOCK_EX | LOCK_NB) != 0) {
517 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
518 fclose(mesh->lockfile);
519 mesh->lockfile = NULL;
520 meshlink_errno = MESHLINK_EBUSY;
529 /// Unlock the main configuration file.
530 void main_config_unlock(meshlink_handle_t *mesh) {
532 fclose(mesh->lockfile);
533 mesh->lockfile = NULL;
537 /// Read a configuration file from a FILE handle.
538 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
543 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
544 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
545 meshlink_errno = MESHLINK_ESTORAGE;
549 uint8_t *buf = xmalloc(len);
551 if(fread(buf, len, 1, f) != 1) {
552 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
553 meshlink_errno = MESHLINK_ESTORAGE;
558 uint8_t *decrypted = xmalloc(len);
559 size_t decrypted_len = len;
560 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
561 chacha_poly1305_set_key(ctx, key);
563 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
564 chacha_poly1305_exit(ctx);
566 config->buf = decrypted;
567 config->len = decrypted_len;
570 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
571 meshlink_errno = MESHLINK_ESTORAGE;
572 chacha_poly1305_exit(ctx);
585 /// Write a configuration file to a FILE handle.
586 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
590 uint8_t buf[config->len + 16];
591 size_t len = sizeof(buf);
593 randomize(&seqbuf, sizeof(seqbuf));
594 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
595 chacha_poly1305_set_key(ctx, key);
596 bool success = false;
598 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
599 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
602 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
605 meshlink_errno = MESHLINK_ESTORAGE;
607 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
608 meshlink_errno = MESHLINK_ESTORAGE;
611 chacha_poly1305_exit(ctx);
615 if(fwrite(config->buf, config->len, 1, f) != 1) {
616 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
617 meshlink_errno = MESHLINK_ESTORAGE;
622 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
623 meshlink_errno = MESHLINK_ESTORAGE;
627 if(fsync(fileno(f))) {
628 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
629 meshlink_errno = MESHLINK_ESTORAGE;
636 /// Free resources of a loaded configuration file.
637 void config_free(config_t *config) {
638 assert(!config->len || config->buf);
640 free((uint8_t *)config->buf);
645 /// Check the presence of a host configuration file.
646 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
649 if(!mesh->confbase) {
654 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
656 return access(path, F_OK) == 0;
659 /// Read a host configuration file.
660 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
663 if(!mesh->confbase) {
668 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
670 FILE *f = fopen(path, "r");
673 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
677 if(!config_read_file(mesh, f, config, key)) {
678 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
688 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
692 if(!mesh->confbase) {
698 char dname[PATH_MAX];
699 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
701 dir = opendir(dname);
704 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
705 meshlink_errno = MESHLINK_ESTORAGE;
709 while((ent = readdir(dir))) {
710 if(ent->d_name[0] == '.') {
714 if(!action(mesh, ent->d_name, arg)) {
724 /// Write a host configuration file.
725 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
730 if(!mesh->confbase) {
735 char tmp_path[PATH_MAX + 4];
736 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
737 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
739 FILE *f = fopen(tmp_path, "w");
742 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
743 meshlink_errno = MESHLINK_ESTORAGE;
747 if(!config_write_file(mesh, f, config, key)) {
748 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
754 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
755 meshlink_errno = MESHLINK_ESTORAGE;
759 if(rename(tmp_path, path)) {
760 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
761 meshlink_errno = MESHLINK_ESTORAGE;
768 /// Delete a host configuration file.
769 bool config_delete(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
773 if(!mesh->confbase) {
778 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
780 if(unlink(path) && errno != ENOENT) {
781 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
782 meshlink_errno = MESHLINK_ESTORAGE;
789 /// Read the main configuration file.
790 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
794 if(!mesh->confbase) {
799 make_main_path(mesh, conf_subdir, path, sizeof(path));
801 FILE *f = fopen(path, "r");
804 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
808 if(!config_read_file(mesh, f, config, key)) {
809 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
819 /// Write the main configuration file.
820 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
824 if(!mesh->confbase) {
829 char tmp_path[PATH_MAX + 4];
830 make_main_path(mesh, conf_subdir, path, sizeof(path));
831 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
833 FILE *f = fopen(tmp_path, "w");
836 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
837 meshlink_errno = MESHLINK_ESTORAGE;
841 if(!config_write_file(mesh, f, config, key)) {
842 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
847 if(rename(tmp_path, path)) {
848 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
849 meshlink_errno = MESHLINK_ESTORAGE;
855 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
856 meshlink_errno = MESHLINK_ESTORAGE;
863 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
864 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
869 if(!mesh->confbase) {
874 char used_path[PATH_MAX];
875 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
876 make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
878 // Atomically rename the invitation file
879 if(rename(path, used_path)) {
880 if(errno == ENOENT) {
881 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
883 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
889 FILE *f = fopen(used_path, "r");
892 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
896 // Check the timestamp
899 if(fstat(fileno(f), &st)) {
900 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
906 if(time(NULL) >= st.st_mtime + mesh->invitation_timeout) {
907 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
913 if(!config_read_file(mesh, f, config, key)) {
914 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
922 if(unlink(used_path)) {
923 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
927 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
929 if(!sync_path(path)) {
930 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
931 meshlink_errno = MESHLINK_ESTORAGE;
938 /// Write an invitation file.
939 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
944 if(!mesh->confbase) {
949 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
951 FILE *f = fopen(path, "w");
954 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
955 meshlink_errno = MESHLINK_ESTORAGE;
959 if(!config_write_file(mesh, f, config, key)) {
960 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
966 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
967 meshlink_errno = MESHLINK_ESTORAGE;
971 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
973 if(!sync_path(path)) {
974 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
975 meshlink_errno = MESHLINK_ESTORAGE;
982 /// Purge old invitation files
983 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
984 if(!mesh->confbase) {
989 make_invitation_path(mesh, "current", "", path, sizeof(path));
991 DIR *dir = opendir(path);
994 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
995 meshlink_errno = MESHLINK_ESTORAGE;
1003 while((ent = readdir(dir))) {
1004 if(strlen(ent->d_name) != 24) {
1008 char invname[PATH_MAX];
1011 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
1012 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
1016 if(!stat(invname, &st)) {
1017 if(mesh->invitation_key && deadline < st.st_mtime) {
1023 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
1029 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
1031 meshlink_errno = MESHLINK_ESTORAGE;