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) {
34 snprintf(path, len, "%s" SLASH "%s" SLASH "meshlink.conf", mesh->confbase, conf_subdir);
37 /// Generate a path to a host configuration file.
38 static void make_host_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
39 snprintf(path, len, "%s" SLASH "%s" SLASH "hosts" SLASH "%s", mesh->confbase, conf_subdir, name);
42 /// Generate a path to an unused invitation file.
43 static void make_invitation_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
44 snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s", mesh->confbase, conf_subdir, name);
47 /// Generate a path to a used invitation file.
48 static void make_used_invitation_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
49 snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, conf_subdir, name);
52 /// Remove a directory recursively
53 static void deltree(const char *dirname) {
54 DIR *d = opendir(dirname);
59 while((ent = readdir(d))) {
60 if(ent->d_name[0] == '.') {
64 char filename[PATH_MAX];
65 snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
67 if(unlink(filename)) {
78 static bool sync_path(const char *pathname) {
79 int fd = open(pathname, O_RDONLY);
82 logger(NULL, MESHLINK_ERROR, "Failed to open %s: %s\n", pathname, strerror(errno));
83 meshlink_errno = MESHLINK_ESTORAGE;
88 logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
90 meshlink_errno = MESHLINK_ESTORAGE;
95 logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
97 meshlink_errno = MESHLINK_ESTORAGE;
104 /// Try decrypting the main configuration file from the given sub-directory.
105 static bool main_config_decrypt(meshlink_handle_t *mesh, const char *conf_subdir) {
106 if(!mesh->config_key && !mesh->confbase && !conf_subdir) {
112 if(!main_config_read(mesh, conf_subdir, &config, mesh->config_key)) {
113 logger(mesh, MESHLINK_ERROR, "Could not read main configuration file");
117 packmsg_input_t in = {config.buf, config.len};
119 uint32_t version = packmsg_get_uint32(&in);
120 config_free(&config);
122 return version == MESHLINK_CONFIG_VERSION;
125 /// Create a fresh configuration directory
126 bool config_init(meshlink_handle_t *mesh, const char *conf_subdir) {
127 if(!mesh->confbase) {
135 if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
136 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno));
142 // Create "current" sub-directory in the confbase
143 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
146 if(mkdir(path, 0700)) {
147 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
151 make_host_path(mesh, conf_subdir, "", path, sizeof(path));
153 if(mkdir(path, 0700)) {
154 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
158 make_invitation_path(mesh, conf_subdir, "", path, sizeof(path));
160 if(mkdir(path, 0700)) {
161 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
168 /// Wipe an existing configuration directory
169 bool config_destroy(const char *confbase, const char *conf_subdir) {
170 if(!confbase && !conf_subdir) {
178 // Check the presence of configuration base sub directory.
179 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
181 if(stat(path, &st)) {
182 if(errno == ENOENT) {
185 logger(NULL, MESHLINK_ERROR, "Cannot stat %s: %s\n", path, strerror(errno));
186 meshlink_errno = MESHLINK_ESTORAGE;
191 // Remove meshlink.conf
192 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "meshlink.conf", confbase, conf_subdir);
195 if(errno != ENOENT) {
196 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
197 meshlink_errno = MESHLINK_ESTORAGE;
202 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
207 static bool copytree(const char *src_dir_name, const void *src_key, const char *dst_dir_name, const void *dst_key) {
208 if(!src_dir_name || !dst_dir_name) {
212 char src_filename[PATH_MAX];
213 char dst_filename[PATH_MAX];
216 DIR *src_dir = opendir(src_dir_name);
219 logger(NULL, MESHLINK_ERROR, "Could not open directory file %s\n", src_dir_name);
223 // Delete if already exists and create a new destination directory
224 deltree(dst_dir_name);
226 if(mkdir(dst_dir_name, 0700)) {
227 logger(NULL, MESHLINK_ERROR, "Could not create directory %s\n", dst_filename);
231 while((ent = readdir(src_dir))) {
232 if(ent->d_name[0] == '.') {
236 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", dst_dir_name, ent->d_name);
237 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", src_dir_name, ent->d_name);
239 if(ent->d_type == DT_DIR) {
240 if(!copytree(src_filename, src_key, dst_filename, dst_key)) {
241 logger(NULL, MESHLINK_ERROR, "Copying %s to %s failed\n", src_filename, dst_filename);
245 if(!sync_path(dst_filename)) {
248 } else if(ent->d_type == DT_REG) {
252 if(stat(src_filename, &st)) {
253 logger(NULL, MESHLINK_ERROR, "Could not stat file `%s': %s\n", src_filename, strerror(errno));
257 FILE *f = fopen(src_filename, "r");
260 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s\n", src_filename, strerror(errno));
264 if(!config_read_file(NULL, f, &config, src_key)) {
265 logger(NULL, MESHLINK_ERROR, "Failed to read `%s': %s\n", src_filename, strerror(errno));
271 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s\n", src_filename, strerror(errno));
272 config_free(&config);
276 f = fopen(dst_filename, "w");
279 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s", dst_filename, strerror(errno));
280 config_free(&config);
284 if(!config_write_file(NULL, f, &config, dst_key)) {
285 logger(NULL, MESHLINK_ERROR, "Failed to write `%s': %s", dst_filename, strerror(errno));
286 config_free(&config);
292 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s", dst_filename, strerror(errno));
293 config_free(&config);
297 config_free(&config);
299 struct utimbuf times;
300 times.modtime = st.st_mtime;
301 times.actime = st.st_atime;
303 if(utime(dst_filename, ×)) {
304 logger(NULL, MESHLINK_ERROR, "Failed to utime `%s': %s", dst_filename, strerror(errno));
314 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) {
315 char src_filename[PATH_MAX];
316 char dst_filename[PATH_MAX];
318 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", mesh->confbase, dst_dir_name);
319 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", mesh->confbase, src_dir_name);
321 return copytree(src_filename, src_key, dst_filename, dst_key);
324 /// Check the presence of the main configuration file.
325 bool main_config_exists(meshlink_handle_t *mesh, const char *conf_subdir) {
326 if(!mesh->confbase && !conf_subdir) {
331 make_main_path(mesh, conf_subdir, path, sizeof(path));
332 return access(path, F_OK) == 0;
335 bool config_rename(meshlink_handle_t *mesh, const char *old_conf_subdir, const char *new_conf_subdir) {
336 if(!mesh->confbase && !old_conf_subdir && !new_conf_subdir) {
340 char old_path[PATH_MAX];
341 char new_path[PATH_MAX];
343 snprintf(old_path, sizeof(old_path), "%s" SLASH "%s", mesh->confbase, old_conf_subdir);
344 snprintf(new_path, sizeof(new_path), "%s" SLASH "%s", mesh->confbase, new_conf_subdir);
346 return rename(old_path, new_path) == 0;
349 bool config_sync(meshlink_handle_t *mesh, const char *conf_subdir) {
350 if(!mesh->confbase) {
359 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "hosts", mesh->confbase, conf_subdir);
361 if(!sync_path(path)) {
365 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
367 if(!sync_path(path)) {
374 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
375 if(!mesh->confbase) {
379 bool confbase_exists = false;
380 bool confbase_decryptable = false;
382 if(main_config_exists(mesh, "current")) {
383 confbase_exists = true;
385 if(mesh->config_key && main_config_decrypt(mesh, "current")) {
386 confbase_decryptable = true;
390 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "new")) {
391 confbase_exists = true;
393 if(main_config_decrypt(mesh, "new")) {
394 if(!config_destroy(mesh->confbase, "current")) {
398 if(!config_rename(mesh, "new", "current")) {
402 confbase_decryptable = true;
406 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "old")) {
407 confbase_exists = true;
409 if(main_config_decrypt(mesh, "old")) {
410 if(!config_destroy(mesh->confbase, "current")) {
414 if(!config_rename(mesh, "old", "current")) {
418 confbase_decryptable = true;
422 // Cleanup if current is existing with old and new
423 if(confbase_exists && confbase_decryptable) {
424 config_destroy(mesh->confbase, "old");
425 config_destroy(mesh->confbase, "new");
428 return confbase_exists;
431 /// Lock the main configuration file.
432 bool main_config_lock(meshlink_handle_t *mesh) {
433 if(!mesh->confbase) {
438 make_main_path(mesh, "current", path, sizeof(path));
440 mesh->conffile = fopen(path, "r");
442 if(!mesh->conffile) {
443 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
444 meshlink_errno = MESHLINK_ESTORAGE;
449 fcntl(fileno(mesh->conffile), F_SETFD, FD_CLOEXEC);
453 // TODO: use _locking()?
456 if(flock(fileno(mesh->conffile), LOCK_EX | LOCK_NB) != 0) {
457 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
458 fclose(mesh->conffile);
459 mesh->conffile = NULL;
460 meshlink_errno = MESHLINK_EBUSY;
469 /// Unlock the main configuration file.
470 void main_config_unlock(meshlink_handle_t *mesh) {
472 fclose(mesh->conffile);
473 mesh->conffile = NULL;
477 /// Read a configuration file from a FILE handle.
478 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
481 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
482 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
483 meshlink_errno = MESHLINK_ESTORAGE;
488 uint8_t *buf = xmalloc(len);
490 if(fread(buf, len, 1, f) != 1) {
491 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
492 meshlink_errno = MESHLINK_ESTORAGE;
498 uint8_t *decrypted = xmalloc(len);
499 size_t decrypted_len = len;
500 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
501 chacha_poly1305_set_key(ctx, key);
503 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
504 chacha_poly1305_exit(ctx);
506 config->buf = decrypted;
507 config->len = decrypted_len;
510 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
511 meshlink_errno = MESHLINK_ESTORAGE;
512 chacha_poly1305_exit(ctx);
525 /// Write a configuration file to a FILE handle.
526 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
528 uint8_t buf[config->len + 16];
529 size_t len = sizeof(buf);
531 randomize(&seqbuf, sizeof(seqbuf));
532 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
533 chacha_poly1305_set_key(ctx, key);
534 bool success = false;
536 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
537 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
539 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
540 meshlink_errno = MESHLINK_ESTORAGE;
543 chacha_poly1305_exit(ctx);
547 if(fwrite(config->buf, config->len, 1, f) != 1) {
548 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
549 meshlink_errno = MESHLINK_ESTORAGE;
553 if(fsync(fileno(f))) {
554 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
561 /// Free resources of a loaded configuration file.
562 void config_free(config_t *config) {
563 free((uint8_t *)config->buf);
568 /// Check the presence of a host configuration file.
569 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
570 if(!mesh->confbase && !conf_subdir) {
575 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
577 return access(path, F_OK) == 0;
580 /// Read a host configuration file.
581 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
582 if(!mesh->confbase && !conf_subdir) {
587 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
589 FILE *f = fopen(path, "r");
592 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
596 if(!config_read_file(mesh, f, config, key)) {
597 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
607 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
608 if(!mesh->confbase && !conf_subdir && !conf_type) {
614 char dname[PATH_MAX];
615 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
617 dir = opendir(dname);
620 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
621 meshlink_errno = MESHLINK_ESTORAGE;
625 while((ent = readdir(dir))) {
626 if(ent->d_name[0] == '.') {
630 if(!action(mesh, ent->d_name, arg)) {
640 /// Write a host configuration file.
641 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
642 if(!mesh->confbase && !conf_subdir && !name) {
647 char tmp_path[PATH_MAX + 4];
648 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
649 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
651 FILE *f = fopen(tmp_path, "w");
654 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
658 if(!config_write_file(mesh, f, config, key)) {
659 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
664 if(fsync(fileno(f))) {
665 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno));
671 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
675 if(rename(tmp_path, path)) {
676 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
683 /// Read the main configuration file.
684 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
685 if(!mesh->confbase && !conf_subdir) {
690 make_main_path(mesh, conf_subdir, path, sizeof(path));
692 FILE *f = fopen(path, "r");
695 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
699 if(!config_read_file(mesh, f, config, key)) {
700 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
710 /// Write the main configuration file.
711 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
712 if(!mesh->confbase && !conf_subdir) {
717 char tmp_path[PATH_MAX + 4];
718 make_main_path(mesh, conf_subdir, path, sizeof(path));
719 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
721 FILE *f = fopen(tmp_path, "w");
724 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
728 if(!config_write_file(mesh, f, config, key)) {
729 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
734 if(fsync(fileno(f))) {
735 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno));
740 if(rename(tmp_path, path)) {
741 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
746 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
753 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
754 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
755 if(!mesh->confbase && !conf_subdir) {
760 char used_path[PATH_MAX];
761 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
762 make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
764 // Atomically rename the invitation file
765 if(rename(path, used_path)) {
766 if(errno == ENOENT) {
767 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
769 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
775 FILE *f = fopen(used_path, "r");
778 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
782 // Check the timestamp
785 if(fstat(fileno(f), &st)) {
786 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
792 if(mesh->loop.now.tv_sec > st.st_mtime + mesh->invitation_timeout) {
793 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
799 if(!config_read_file(mesh, f, config, key)) {
800 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
812 /// Write an invitation file.
813 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
814 if(!mesh->confbase && !conf_subdir) {
819 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
821 FILE *f = fopen(path, "w");
824 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
828 if(!config_write_file(mesh, f, config, key)) {
829 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
834 if(fsync(fileno(f))) {
835 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
840 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
847 /// Purge old invitation files
848 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
849 if(!mesh->confbase) {
854 make_invitation_path(mesh, "current", "", path, sizeof(path));
856 DIR *dir = opendir(path);
859 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
860 meshlink_errno = MESHLINK_ESTORAGE;
868 while((ent = readdir(dir))) {
869 if(strlen(ent->d_name) != 24) {
873 char invname[PATH_MAX];
876 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
877 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
881 if(!stat(invname, &st)) {
882 if(mesh->invitation_key && deadline < st.st_mtime) {
888 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
894 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
896 meshlink_errno = MESHLINK_ESTORAGE;