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 void 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)) {
99 bool sync_path(const char *pathname) {
102 int fd = open(pathname, O_RDONLY);
105 logger(NULL, MESHLINK_ERROR, "Failed to open %s: %s\n", pathname, strerror(errno));
106 meshlink_errno = MESHLINK_ESTORAGE;
111 logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
113 meshlink_errno = MESHLINK_ESTORAGE;
118 logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
120 meshlink_errno = MESHLINK_ESTORAGE;
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);
135 if(!main_config_read(mesh, conf_subdir, &config, mesh->config_key)) {
136 logger(mesh, MESHLINK_ERROR, "Could not read main configuration file");
140 packmsg_input_t in = {config.buf, config.len};
142 uint32_t version = packmsg_get_uint32(&in);
143 config_free(&config);
145 return version == MESHLINK_CONFIG_VERSION;
148 /// Create a fresh configuration directory
149 bool config_init(meshlink_handle_t *mesh, const char *conf_subdir) {
152 if(!mesh->confbase) {
158 // Create "current" sub-directory in the confbase
159 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
162 if(mkdir(path, 0700)) {
163 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
167 make_host_path(mesh, conf_subdir, "", path, sizeof(path));
169 if(mkdir(path, 0700)) {
170 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
174 make_invitation_path(mesh, conf_subdir, "", path, sizeof(path));
176 if(mkdir(path, 0700)) {
177 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
184 /// Wipe an existing configuration directory
185 bool config_destroy(const char *confbase, const char *conf_subdir) {
196 // Check the presence of configuration base sub directory.
197 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
199 if(stat(path, &st)) {
200 if(errno == ENOENT) {
203 logger(NULL, MESHLINK_ERROR, "Cannot stat %s: %s\n", path, strerror(errno));
204 meshlink_errno = MESHLINK_ESTORAGE;
209 // Remove meshlink.conf
210 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "meshlink.conf", confbase, conf_subdir);
213 if(errno != ENOENT) {
214 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
215 meshlink_errno = MESHLINK_ESTORAGE;
220 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
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);
229 char src_filename[PATH_MAX];
230 char dst_filename[PATH_MAX];
233 DIR *src_dir = opendir(src_dir_name);
236 logger(NULL, MESHLINK_ERROR, "Could not open directory file %s\n", src_dir_name);
240 // Delete if already exists and create a new destination directory
241 deltree(dst_dir_name);
243 if(mkdir(dst_dir_name, 0700)) {
244 logger(NULL, MESHLINK_ERROR, "Could not create directory %s\n", dst_filename);
248 while((ent = readdir(src_dir))) {
249 if(ent->d_name[0] == '.') {
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);
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);
262 if(!sync_path(dst_filename)) {
265 } else if(ent->d_type == DT_REG) {
269 if(stat(src_filename, &st)) {
270 logger(NULL, MESHLINK_ERROR, "Could not stat file `%s': %s\n", src_filename, strerror(errno));
274 FILE *f = fopen(src_filename, "r");
277 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s\n", src_filename, strerror(errno));
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));
288 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s\n", src_filename, strerror(errno));
289 config_free(&config);
293 f = fopen(dst_filename, "w");
296 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s", dst_filename, strerror(errno));
297 config_free(&config);
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);
309 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s", dst_filename, strerror(errno));
310 config_free(&config);
314 config_free(&config);
316 struct utimbuf times;
317 times.modtime = st.st_mtime;
318 times.actime = st.st_atime;
320 if(utime(dst_filename, ×)) {
321 logger(NULL, MESHLINK_ERROR, "Failed to utime `%s': %s", dst_filename, strerror(errno));
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);
335 char src_filename[PATH_MAX];
336 char dst_filename[PATH_MAX];
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);
341 return copytree(src_filename, src_key, dst_filename, dst_key);
344 /// Check the presence of the main configuration file.
345 bool main_config_exists(meshlink_handle_t *mesh, const char *conf_subdir) {
348 if(!mesh->confbase) {
353 make_main_path(mesh, conf_subdir, path, sizeof(path));
354 return access(path, F_OK) == 0;
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);
361 if(!mesh->confbase) {
365 char old_path[PATH_MAX];
366 char new_path[PATH_MAX];
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);
371 return rename(old_path, new_path) == 0;
374 bool config_sync(meshlink_handle_t *mesh, const char *conf_subdir) {
377 if(!mesh->confbase) {
382 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "hosts", mesh->confbase, conf_subdir);
384 if(!sync_path(path)) {
388 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
390 if(!sync_path(path)) {
397 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
398 if(!mesh->confbase) {
402 bool confbase_exists = false;
403 bool confbase_decryptable = false;
405 if(main_config_exists(mesh, "current")) {
406 confbase_exists = true;
408 if(mesh->config_key && main_config_decrypt(mesh, "current")) {
409 confbase_decryptable = true;
413 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "new")) {
414 confbase_exists = true;
416 if(main_config_decrypt(mesh, "new")) {
417 if(!config_destroy(mesh->confbase, "current")) {
421 if(!config_rename(mesh, "new", "current")) {
425 confbase_decryptable = true;
429 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "old")) {
430 confbase_exists = true;
432 if(main_config_decrypt(mesh, "old")) {
433 if(!config_destroy(mesh->confbase, "current")) {
437 if(!config_rename(mesh, "old", "current")) {
441 confbase_decryptable = true;
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")) {
452 return confbase_exists;
455 /// Lock the main configuration file. Creates confbase if necessary.
456 bool main_config_lock(meshlink_handle_t *mesh) {
457 if(!mesh->confbase) {
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;
469 snprintf(path, sizeof(path), "%s" SLASH "meshlink.lock", mesh->confbase);
471 mesh->lockfile = fopen(path, "w+");
473 if(!mesh->lockfile) {
474 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
475 meshlink_errno = MESHLINK_ESTORAGE;
480 fcntl(fileno(mesh->lockfile), F_SETFD, FD_CLOEXEC);
484 // TODO: use _locking()?
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;
500 /// Unlock the main configuration file.
501 void main_config_unlock(meshlink_handle_t *mesh) {
503 fclose(mesh->lockfile);
504 mesh->lockfile = NULL;
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) {
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;
520 uint8_t *buf = xmalloc(len);
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;
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);
534 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
535 chacha_poly1305_exit(ctx);
537 config->buf = decrypted;
538 config->len = decrypted_len;
541 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
542 meshlink_errno = MESHLINK_ESTORAGE;
543 chacha_poly1305_exit(ctx);
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) {
561 uint8_t buf[config->len + 16];
562 size_t len = sizeof(buf);
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;
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;
573 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
576 meshlink_errno = MESHLINK_ESTORAGE;
578 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
579 meshlink_errno = MESHLINK_ESTORAGE;
582 chacha_poly1305_exit(ctx);
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;
593 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
594 meshlink_errno = MESHLINK_ESTORAGE;
598 if(fsync(fileno(f))) {
599 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
600 meshlink_errno = MESHLINK_ESTORAGE;
607 /// Free resources of a loaded configuration file.
608 void config_free(config_t *config) {
609 assert(!config->len || config->buf);
611 free((uint8_t *)config->buf);
616 /// Check the presence of a host configuration file.
617 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
620 if(!mesh->confbase) {
625 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
627 return access(path, F_OK) == 0;
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) {
634 if(!mesh->confbase) {
639 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
641 FILE *f = fopen(path, "r");
644 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
648 if(!config_read_file(mesh, f, config, key)) {
649 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
659 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
663 if(!mesh->confbase) {
669 char dname[PATH_MAX];
670 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
672 dir = opendir(dname);
675 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
676 meshlink_errno = MESHLINK_ESTORAGE;
680 while((ent = readdir(dir))) {
681 if(ent->d_name[0] == '.') {
685 if(!action(mesh, ent->d_name, arg)) {
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) {
701 if(!mesh->confbase) {
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);
710 FILE *f = fopen(tmp_path, "w");
713 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
714 meshlink_errno = MESHLINK_ESTORAGE;
718 if(!config_write_file(mesh, f, config, key)) {
719 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
725 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
726 meshlink_errno = MESHLINK_ESTORAGE;
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;
739 /// Delete a host configuration file.
740 bool config_delete(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
744 if(!mesh->confbase) {
749 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
751 if(unlink(path) && errno != ENOENT) {
752 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
753 meshlink_errno = MESHLINK_ESTORAGE;
760 /// Read the main configuration file.
761 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
765 if(!mesh->confbase) {
770 make_main_path(mesh, conf_subdir, path, sizeof(path));
772 FILE *f = fopen(path, "r");
775 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
779 if(!config_read_file(mesh, f, config, key)) {
780 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
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) {
795 if(!mesh->confbase) {
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);
804 FILE *f = fopen(tmp_path, "w");
807 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
808 meshlink_errno = MESHLINK_ESTORAGE;
812 if(!config_write_file(mesh, f, config, key)) {
813 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
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;
826 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
827 meshlink_errno = MESHLINK_ESTORAGE;
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) {
840 if(!mesh->confbase) {
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));
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);
854 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
860 FILE *f = fopen(used_path, "r");
863 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
867 // Check the timestamp
870 if(fstat(fileno(f), &st)) {
871 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
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);
884 if(!config_read_file(mesh, f, config, key)) {
885 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
893 if(unlink(used_path)) {
894 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
898 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
900 if(!sync_path(path)) {
901 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
902 meshlink_errno = MESHLINK_ESTORAGE;
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) {
915 if(!mesh->confbase) {
920 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
922 FILE *f = fopen(path, "w");
925 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
926 meshlink_errno = MESHLINK_ESTORAGE;
930 if(!config_write_file(mesh, f, config, key)) {
931 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
937 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
938 meshlink_errno = MESHLINK_ESTORAGE;
942 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
944 if(!sync_path(path)) {
945 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
946 meshlink_errno = MESHLINK_ESTORAGE;
953 /// Purge old invitation files
954 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
955 if(!mesh->confbase) {
960 make_invitation_path(mesh, "current", "", path, sizeof(path));
962 DIR *dir = opendir(path);
965 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
966 meshlink_errno = MESHLINK_ESTORAGE;
974 while((ent = readdir(dir))) {
975 if(strlen(ent->d_name) != 24) {
979 char invname[PATH_MAX];
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);
987 if(!stat(invname, &st)) {
988 if(mesh->invitation_key && deadline < st.st_mtime) {
994 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
1000 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
1002 meshlink_errno = MESHLINK_ESTORAGE;