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) {
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) {
488 if(!mesh->confbase) {
492 if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
493 logger(NULL, MESHLINK_ERROR, "Cannot create configuration directory %s: %s", mesh->confbase, strerror(errno));
494 meshlink_close(mesh);
495 meshlink_errno = MESHLINK_ESTORAGE;
500 snprintf(path, sizeof(path), "%s" SLASH "meshlink.lock", mesh->confbase);
502 mesh->lockfile = fopen(path, "w+");
504 if(!mesh->lockfile) {
505 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
506 meshlink_errno = MESHLINK_ESTORAGE;
511 fcntl(fileno(mesh->lockfile), F_SETFD, FD_CLOEXEC);
515 // TODO: use _locking()?
518 if(flock(fileno(mesh->lockfile), LOCK_EX | LOCK_NB) != 0) {
519 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
520 fclose(mesh->lockfile);
521 mesh->lockfile = NULL;
522 meshlink_errno = MESHLINK_EBUSY;
531 /// Unlock the main configuration file.
532 void main_config_unlock(meshlink_handle_t *mesh) {
534 fclose(mesh->lockfile);
535 mesh->lockfile = NULL;
539 /// Read a configuration file from a FILE handle.
540 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
545 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
546 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
547 meshlink_errno = MESHLINK_ESTORAGE;
551 uint8_t *buf = xmalloc(len);
553 if(fread(buf, len, 1, f) != 1) {
554 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
555 meshlink_errno = MESHLINK_ESTORAGE;
560 uint8_t *decrypted = xmalloc(len);
561 size_t decrypted_len = len;
562 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
563 chacha_poly1305_set_key(ctx, key);
565 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
566 chacha_poly1305_exit(ctx);
568 config->buf = decrypted;
569 config->len = decrypted_len;
572 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
573 meshlink_errno = MESHLINK_ESTORAGE;
574 chacha_poly1305_exit(ctx);
587 /// Write a configuration file to a FILE handle.
588 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
592 uint8_t buf[config->len + 16];
593 size_t len = sizeof(buf);
595 randomize(&seqbuf, sizeof(seqbuf));
596 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
597 chacha_poly1305_set_key(ctx, key);
598 bool success = false;
600 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
601 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
604 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
607 meshlink_errno = MESHLINK_ESTORAGE;
609 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
610 meshlink_errno = MESHLINK_ESTORAGE;
613 chacha_poly1305_exit(ctx);
617 if(fwrite(config->buf, config->len, 1, f) != 1) {
618 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
619 meshlink_errno = MESHLINK_ESTORAGE;
624 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
625 meshlink_errno = MESHLINK_ESTORAGE;
629 if(fsync(fileno(f))) {
630 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
631 meshlink_errno = MESHLINK_ESTORAGE;
638 /// Free resources of a loaded configuration file.
639 void config_free(config_t *config) {
640 assert(!config->len || config->buf);
642 free((uint8_t *)config->buf);
647 /// Check the presence of a host configuration file.
648 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
651 if(!mesh->confbase) {
656 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
658 return access(path, F_OK) == 0;
661 /// Read a host configuration file.
662 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
665 if(!mesh->confbase) {
670 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
672 FILE *f = fopen(path, "r");
675 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
679 if(!config_read_file(mesh, f, config, key)) {
680 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
690 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
694 if(!mesh->confbase) {
700 char dname[PATH_MAX];
701 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
703 dir = opendir(dname);
706 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
707 meshlink_errno = MESHLINK_ESTORAGE;
711 while((ent = readdir(dir))) {
712 if(ent->d_name[0] == '.') {
716 if(!action(mesh, ent->d_name, arg)) {
726 /// Write a host configuration file.
727 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
732 if(!mesh->confbase) {
737 char tmp_path[PATH_MAX + 4];
738 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
739 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
741 FILE *f = fopen(tmp_path, "w");
744 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
745 meshlink_errno = MESHLINK_ESTORAGE;
749 if(!config_write_file(mesh, f, config, key)) {
750 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
756 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
757 meshlink_errno = MESHLINK_ESTORAGE;
761 if(rename(tmp_path, path)) {
762 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
763 meshlink_errno = MESHLINK_ESTORAGE;
770 /// Delete a host configuration file.
771 bool config_delete(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
775 if(!mesh->confbase) {
780 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
782 if(unlink(path) && errno != ENOENT) {
783 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
784 meshlink_errno = MESHLINK_ESTORAGE;
791 /// Read the main configuration file.
792 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
796 if(!mesh->confbase) {
801 make_main_path(mesh, conf_subdir, path, sizeof(path));
803 FILE *f = fopen(path, "r");
806 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
810 if(!config_read_file(mesh, f, config, key)) {
811 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
821 /// Write the main configuration file.
822 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
826 if(!mesh->confbase) {
831 char tmp_path[PATH_MAX + 4];
832 make_main_path(mesh, conf_subdir, path, sizeof(path));
833 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
835 FILE *f = fopen(tmp_path, "w");
838 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
839 meshlink_errno = MESHLINK_ESTORAGE;
843 if(!config_write_file(mesh, f, config, key)) {
844 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
849 if(rename(tmp_path, path)) {
850 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
851 meshlink_errno = MESHLINK_ESTORAGE;
857 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
858 meshlink_errno = MESHLINK_ESTORAGE;
865 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
866 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
871 if(!mesh->confbase) {
876 char used_path[PATH_MAX];
877 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
878 make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
880 // Atomically rename the invitation file
881 if(rename(path, used_path)) {
882 if(errno == ENOENT) {
883 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
885 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
891 FILE *f = fopen(used_path, "r");
894 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
898 // Check the timestamp
901 if(fstat(fileno(f), &st)) {
902 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
908 if(time(NULL) >= st.st_mtime + mesh->invitation_timeout) {
909 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
915 if(!config_read_file(mesh, f, config, key)) {
916 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
924 if(unlink(used_path)) {
925 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
929 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
931 if(!sync_path(path)) {
932 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
933 meshlink_errno = MESHLINK_ESTORAGE;
940 /// Write an invitation file.
941 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
946 if(!mesh->confbase) {
951 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
953 FILE *f = fopen(path, "w");
956 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
957 meshlink_errno = MESHLINK_ESTORAGE;
961 if(!config_write_file(mesh, f, config, key)) {
962 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
968 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
969 meshlink_errno = MESHLINK_ESTORAGE;
973 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
975 if(!sync_path(path)) {
976 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
977 meshlink_errno = MESHLINK_ESTORAGE;
984 /// Purge old invitation files
985 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
986 if(!mesh->confbase) {
991 make_invitation_path(mesh, "current", "", path, sizeof(path));
993 DIR *dir = opendir(path);
996 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
997 meshlink_errno = MESHLINK_ESTORAGE;
1005 while((ent = readdir(dir))) {
1006 if(strlen(ent->d_name) != 24) {
1010 char invname[PATH_MAX];
1013 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
1014 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
1018 if(!stat(invname, &st)) {
1019 if(mesh->invitation_key && deadline < st.st_mtime) {
1025 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
1031 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
1033 meshlink_errno = MESHLINK_ESTORAGE;
1042 /// Purge invitations for the given node
1043 size_t invitation_purge_node(meshlink_handle_t *mesh, const char *node_name) {
1044 if(!mesh->confbase) {
1048 char path[PATH_MAX];
1049 make_invitation_path(mesh, "current", "", path, sizeof(path));
1051 DIR *dir = opendir(path);
1054 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
1055 meshlink_errno = MESHLINK_ESTORAGE;
1063 while((ent = readdir(dir))) {
1064 if(strlen(ent->d_name) != 24) {
1068 char invname[PATH_MAX];
1070 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
1071 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
1075 FILE *f = fopen(invname, "r");
1084 if(!config_read_file(mesh, f, &config, mesh->config_key)) {
1085 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", invname, strerror(errno));
1086 config_free(&config);
1092 packmsg_input_t in = {config.buf, config.len};
1093 packmsg_get_uint32(&in); // skip version
1094 char *name = packmsg_get_str_dup(&in);
1096 if(name && !strcmp(name, node_name)) {
1097 logger(mesh, MESHLINK_DEBUG, "Removing invitation for %s", node_name);
1102 config_free(&config);
1107 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
1109 meshlink_errno = MESHLINK_ESTORAGE;