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] == '.') {
82 if(!ent->d_name[1] || (ent->d_name[1] == '.' && !ent->d_name[2])) {
87 char filename[PATH_MAX];
88 snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
90 if(unlink(filename)) {
91 if(!deltree(filename)) {
99 return errno == ENOENT;
102 return rmdir(dirname) == 0;
105 bool sync_path(const char *pathname) {
108 int fd = open(pathname, O_RDONLY);
111 logger(NULL, MESHLINK_ERROR, "Failed to open %s: %s\n", pathname, strerror(errno));
112 meshlink_errno = MESHLINK_ESTORAGE;
117 logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
119 meshlink_errno = MESHLINK_ESTORAGE;
124 logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
126 meshlink_errno = MESHLINK_ESTORAGE;
133 /// Try decrypting the main configuration file from the given sub-directory.
134 static bool main_config_decrypt(meshlink_handle_t *mesh, const char *conf_subdir) {
135 assert(mesh->config_key);
136 assert(mesh->confbase);
141 if(!main_config_read(mesh, conf_subdir, &config, mesh->config_key)) {
142 logger(mesh, MESHLINK_ERROR, "Could not read main configuration file");
146 packmsg_input_t in = {config.buf, config.len};
148 uint32_t version = packmsg_get_uint32(&in);
149 config_free(&config);
151 return version == MESHLINK_CONFIG_VERSION;
154 /// Create a fresh configuration directory
155 bool config_init(meshlink_handle_t *mesh, const char *conf_subdir) {
158 if(!mesh->confbase) {
164 // Create "current" sub-directory in the confbase
165 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
168 logger(mesh, MESHLINK_DEBUG, "Could not delete directory %s: %s\n", path, strerror(errno));
172 if(mkdir(path, 0700)) {
173 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
177 make_host_path(mesh, conf_subdir, "", path, sizeof(path));
179 if(mkdir(path, 0700)) {
180 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
184 make_invitation_path(mesh, conf_subdir, "", path, sizeof(path));
186 if(mkdir(path, 0700)) {
187 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
194 /// Wipe an existing configuration directory
195 bool config_destroy(const char *confbase, const char *conf_subdir) {
206 // Check the presence of configuration base sub directory.
207 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
209 if(stat(path, &st)) {
210 if(errno == ENOENT) {
213 logger(NULL, MESHLINK_ERROR, "Cannot stat %s: %s\n", path, strerror(errno));
214 meshlink_errno = MESHLINK_ESTORAGE;
219 // Remove meshlink.conf
220 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "meshlink.conf", confbase, conf_subdir);
223 if(errno != ENOENT) {
224 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
225 meshlink_errno = MESHLINK_ESTORAGE;
230 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
233 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
234 meshlink_errno = MESHLINK_ESTORAGE;
238 return sync_path(confbase);
241 static bool copytree(const char *src_dir_name, const void *src_key, const char *dst_dir_name, const void *dst_key) {
242 assert(src_dir_name);
243 assert(dst_dir_name);
245 char src_filename[PATH_MAX];
246 char dst_filename[PATH_MAX];
249 DIR *src_dir = opendir(src_dir_name);
252 logger(NULL, MESHLINK_ERROR, "Could not open directory file %s\n", src_dir_name);
253 meshlink_errno = MESHLINK_ESTORAGE;
257 // Delete if already exists and create a new destination directory
258 if(!deltree(dst_dir_name)) {
259 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", dst_dir_name, strerror(errno));
260 meshlink_errno = MESHLINK_ESTORAGE;
264 if(mkdir(dst_dir_name, 0700)) {
265 logger(NULL, MESHLINK_ERROR, "Could not create directory %s\n", dst_filename);
266 meshlink_errno = MESHLINK_ESTORAGE;
270 while((ent = readdir(src_dir))) {
271 if(ent->d_name[0] == '.') {
275 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", dst_dir_name, ent->d_name);
276 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", src_dir_name, ent->d_name);
278 if(ent->d_type == DT_DIR) {
279 if(!copytree(src_filename, src_key, dst_filename, dst_key)) {
280 logger(NULL, MESHLINK_ERROR, "Copying %s to %s failed\n", src_filename, dst_filename);
281 meshlink_errno = MESHLINK_ESTORAGE;
285 if(!sync_path(dst_filename)) {
288 } else if(ent->d_type == DT_REG) {
292 if(stat(src_filename, &st)) {
293 logger(NULL, MESHLINK_ERROR, "Could not stat file `%s': %s\n", src_filename, strerror(errno));
294 meshlink_errno = MESHLINK_ESTORAGE;
298 FILE *f = fopen(src_filename, "r");
301 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s\n", src_filename, strerror(errno));
302 meshlink_errno = MESHLINK_ESTORAGE;
306 if(!config_read_file(NULL, f, &config, src_key)) {
307 logger(NULL, MESHLINK_ERROR, "Failed to read `%s': %s\n", src_filename, strerror(errno));
309 meshlink_errno = MESHLINK_ESTORAGE;
314 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s\n", src_filename, strerror(errno));
315 config_free(&config);
316 meshlink_errno = MESHLINK_ESTORAGE;
320 f = fopen(dst_filename, "w");
323 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s", dst_filename, strerror(errno));
324 config_free(&config);
325 meshlink_errno = MESHLINK_ESTORAGE;
329 if(!config_write_file(NULL, f, &config, dst_key)) {
330 logger(NULL, MESHLINK_ERROR, "Failed to write `%s': %s", dst_filename, strerror(errno));
331 config_free(&config);
333 meshlink_errno = MESHLINK_ESTORAGE;
338 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s", dst_filename, strerror(errno));
339 config_free(&config);
340 meshlink_errno = MESHLINK_ESTORAGE;
344 config_free(&config);
346 struct utimbuf times;
347 times.modtime = st.st_mtime;
348 times.actime = st.st_atime;
350 if(utime(dst_filename, ×)) {
351 logger(NULL, MESHLINK_ERROR, "Failed to utime `%s': %s", dst_filename, strerror(errno));
352 meshlink_errno = MESHLINK_ESTORAGE;
362 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) {
363 assert(src_dir_name);
364 assert(dst_dir_name);
366 char src_filename[PATH_MAX];
367 char dst_filename[PATH_MAX];
369 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", mesh->confbase, dst_dir_name);
370 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", mesh->confbase, src_dir_name);
372 return copytree(src_filename, src_key, dst_filename, dst_key);
375 /// Check the presence of the main configuration file.
376 bool main_config_exists(meshlink_handle_t *mesh, const char *conf_subdir) {
379 if(!mesh->confbase) {
384 make_main_path(mesh, conf_subdir, path, sizeof(path));
385 return access(path, F_OK) == 0;
388 bool config_rename(meshlink_handle_t *mesh, const char *old_conf_subdir, const char *new_conf_subdir) {
389 assert(old_conf_subdir);
390 assert(new_conf_subdir);
392 if(!mesh->confbase) {
396 char old_path[PATH_MAX];
397 char new_path[PATH_MAX];
399 snprintf(old_path, sizeof(old_path), "%s" SLASH "%s", mesh->confbase, old_conf_subdir);
400 snprintf(new_path, sizeof(new_path), "%s" SLASH "%s", mesh->confbase, new_conf_subdir);
402 return rename(old_path, new_path) == 0 && sync_path(mesh->confbase);
405 bool config_sync(meshlink_handle_t *mesh, const char *conf_subdir) {
408 if(!mesh->confbase || mesh->storage_policy == MESHLINK_STORAGE_DISABLED) {
413 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "hosts", mesh->confbase, conf_subdir);
415 if(!sync_path(path)) {
419 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
421 if(!sync_path(path)) {
428 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
429 if(!mesh->confbase) {
433 bool confbase_exists = false;
434 bool confbase_decryptable = false;
436 if(main_config_exists(mesh, "current")) {
437 confbase_exists = true;
439 if(mesh->config_key && main_config_decrypt(mesh, "current")) {
440 confbase_decryptable = true;
444 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "new")) {
445 confbase_exists = true;
447 if(main_config_decrypt(mesh, "new")) {
448 if(!config_destroy(mesh->confbase, "current")) {
452 if(!config_rename(mesh, "new", "current")) {
456 confbase_decryptable = true;
460 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "old")) {
461 confbase_exists = true;
463 if(main_config_decrypt(mesh, "old")) {
464 if(!config_destroy(mesh->confbase, "current")) {
468 if(!config_rename(mesh, "old", "current")) {
472 confbase_decryptable = true;
476 // Cleanup if current is existing with old and new
477 if(confbase_exists && confbase_decryptable) {
478 if(!config_destroy(mesh->confbase, "old") || !config_destroy(mesh->confbase, "new")) {
483 return confbase_exists;
486 /// Lock the main configuration file. Creates confbase if necessary.
487 bool main_config_lock(meshlink_handle_t *mesh, const char *lock_filename) {
488 if(!mesh->confbase) {
492 assert(lock_filename);
494 if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
495 logger(NULL, MESHLINK_ERROR, "Cannot create configuration directory %s: %s", mesh->confbase, strerror(errno));
496 meshlink_close(mesh);
497 meshlink_errno = MESHLINK_ESTORAGE;
501 mesh->lockfile = fopen(lock_filename, "w+");
503 if(!mesh->lockfile) {
504 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", lock_filename, strerror(errno));
505 meshlink_errno = MESHLINK_ESTORAGE;
510 fcntl(fileno(mesh->lockfile), F_SETFD, FD_CLOEXEC);
514 // TODO: use _locking()?
517 if(flock(fileno(mesh->lockfile), LOCK_EX | LOCK_NB) != 0) {
518 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", lock_filename, strerror(errno));
519 fclose(mesh->lockfile);
520 mesh->lockfile = NULL;
521 meshlink_errno = MESHLINK_EBUSY;
530 /// Unlock the main configuration file.
531 void main_config_unlock(meshlink_handle_t *mesh) {
533 fclose(mesh->lockfile);
534 mesh->lockfile = NULL;
538 /// Read a configuration file from a FILE handle.
539 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
544 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
545 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
546 meshlink_errno = MESHLINK_ESTORAGE;
550 uint8_t *buf = xmalloc(len);
552 if(fread(buf, len, 1, f) != 1) {
553 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
554 meshlink_errno = MESHLINK_ESTORAGE;
559 uint8_t *decrypted = xmalloc(len);
560 size_t decrypted_len = len;
561 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
562 chacha_poly1305_set_key(ctx, key);
564 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
565 chacha_poly1305_exit(ctx);
567 config->buf = decrypted;
568 config->len = decrypted_len;
571 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
572 meshlink_errno = MESHLINK_ESTORAGE;
573 chacha_poly1305_exit(ctx);
586 /// Write a configuration file to a FILE handle.
587 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
591 uint8_t buf[config->len + 16];
592 size_t len = sizeof(buf);
594 randomize(&seqbuf, sizeof(seqbuf));
595 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
596 chacha_poly1305_set_key(ctx, key);
597 bool success = false;
599 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
600 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
603 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
606 meshlink_errno = MESHLINK_ESTORAGE;
608 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
609 meshlink_errno = MESHLINK_ESTORAGE;
612 chacha_poly1305_exit(ctx);
616 if(fwrite(config->buf, config->len, 1, f) != 1) {
617 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
618 meshlink_errno = MESHLINK_ESTORAGE;
623 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
624 meshlink_errno = MESHLINK_ESTORAGE;
628 if(fsync(fileno(f))) {
629 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
630 meshlink_errno = MESHLINK_ESTORAGE;
637 /// Free resources of a loaded configuration file.
638 void config_free(config_t *config) {
639 assert(!config->len || config->buf);
641 free((uint8_t *)config->buf);
646 /// Check the presence of a host configuration file.
647 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
650 if(!mesh->confbase) {
655 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
657 return access(path, F_OK) == 0;
660 /// Read a host configuration file.
661 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
664 if(!mesh->confbase) {
669 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
671 FILE *f = fopen(path, "r");
674 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
678 if(!config_read_file(mesh, f, config, key)) {
679 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
689 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
693 if(!mesh->confbase) {
699 char dname[PATH_MAX];
700 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
702 dir = opendir(dname);
705 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
706 meshlink_errno = MESHLINK_ESTORAGE;
710 while((ent = readdir(dir))) {
711 if(ent->d_name[0] == '.') {
715 if(!action(mesh, ent->d_name, arg)) {
725 /// Write a host configuration file.
726 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
731 if(!mesh->confbase) {
736 char tmp_path[PATH_MAX + 4];
737 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
738 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
740 FILE *f = fopen(tmp_path, "w");
743 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
744 meshlink_errno = MESHLINK_ESTORAGE;
748 if(!config_write_file(mesh, f, config, key)) {
749 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
755 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
756 meshlink_errno = MESHLINK_ESTORAGE;
760 if(rename(tmp_path, path)) {
761 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
762 meshlink_errno = MESHLINK_ESTORAGE;
769 /// Delete a host configuration file.
770 bool config_delete(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
774 if(!mesh->confbase) {
779 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
781 if(unlink(path) && errno != ENOENT) {
782 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
783 meshlink_errno = MESHLINK_ESTORAGE;
790 /// Read the main configuration file.
791 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
795 if(!mesh->confbase) {
800 make_main_path(mesh, conf_subdir, path, sizeof(path));
802 FILE *f = fopen(path, "r");
805 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
809 if(!config_read_file(mesh, f, config, key)) {
810 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
820 /// Write the main configuration file.
821 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
825 if(!mesh->confbase) {
830 char tmp_path[PATH_MAX + 4];
831 make_main_path(mesh, conf_subdir, path, sizeof(path));
832 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
834 FILE *f = fopen(tmp_path, "w");
837 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
838 meshlink_errno = MESHLINK_ESTORAGE;
842 if(!config_write_file(mesh, f, config, key)) {
843 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
848 if(rename(tmp_path, path)) {
849 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
850 meshlink_errno = MESHLINK_ESTORAGE;
856 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
857 meshlink_errno = MESHLINK_ESTORAGE;
864 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
865 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
870 if(!mesh->confbase) {
875 char used_path[PATH_MAX];
876 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
877 make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
879 // Atomically rename the invitation file
880 if(rename(path, used_path)) {
881 if(errno == ENOENT) {
882 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
884 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
890 FILE *f = fopen(used_path, "r");
893 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
897 // Check the timestamp
900 if(fstat(fileno(f), &st)) {
901 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
907 if(time(NULL) >= st.st_mtime + mesh->invitation_timeout) {
908 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
914 if(!config_read_file(mesh, f, config, key)) {
915 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
923 if(unlink(used_path)) {
924 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
928 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
930 if(!sync_path(path)) {
931 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
932 meshlink_errno = MESHLINK_ESTORAGE;
939 /// Write an invitation file.
940 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
945 if(!mesh->confbase) {
950 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
952 FILE *f = fopen(path, "w");
955 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
956 meshlink_errno = MESHLINK_ESTORAGE;
960 if(!config_write_file(mesh, f, config, key)) {
961 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
967 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
968 meshlink_errno = MESHLINK_ESTORAGE;
972 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
974 if(!sync_path(path)) {
975 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
976 meshlink_errno = MESHLINK_ESTORAGE;
983 /// Purge old invitation files
984 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
985 if(!mesh->confbase) {
990 make_invitation_path(mesh, "current", "", path, sizeof(path));
992 DIR *dir = opendir(path);
995 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
996 meshlink_errno = MESHLINK_ESTORAGE;
1004 while((ent = readdir(dir))) {
1005 if(strlen(ent->d_name) != 24) {
1009 char invname[PATH_MAX];
1012 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
1013 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
1017 if(!stat(invname, &st)) {
1018 if(mesh->invitation_key && deadline < st.st_mtime) {
1024 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
1030 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
1032 meshlink_errno = MESHLINK_ESTORAGE;
1041 /// Purge invitations for the given node
1042 size_t invitation_purge_node(meshlink_handle_t *mesh, const char *node_name) {
1043 if(!mesh->confbase) {
1047 char path[PATH_MAX];
1048 make_invitation_path(mesh, "current", "", path, sizeof(path));
1050 DIR *dir = opendir(path);
1053 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
1054 meshlink_errno = MESHLINK_ESTORAGE;
1062 while((ent = readdir(dir))) {
1063 if(strlen(ent->d_name) != 24) {
1067 char invname[PATH_MAX];
1069 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
1070 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
1074 FILE *f = fopen(invname, "r");
1083 if(!config_read_file(mesh, f, &config, mesh->config_key)) {
1084 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", invname, strerror(errno));
1085 config_free(&config);
1091 packmsg_input_t in = {config.buf, config.len};
1092 packmsg_get_uint32(&in); // skip version
1093 char *name = packmsg_get_str_dup(&in);
1095 if(name && !strcmp(name, node_name)) {
1096 logger(mesh, MESHLINK_DEBUG, "Removing invitation for %s", node_name);
1101 config_free(&config);
1106 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
1108 meshlink_errno = MESHLINK_ESTORAGE;