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;
487 uint8_t *buf = xmalloc(len);
489 if(fread(buf, len, 1, f) != 1) {
490 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
491 meshlink_errno = MESHLINK_ESTORAGE;
496 uint8_t *decrypted = xmalloc(len);
497 size_t decrypted_len = len;
498 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
499 chacha_poly1305_set_key(ctx, key);
501 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
502 chacha_poly1305_exit(ctx);
504 config->buf = decrypted;
505 config->len = decrypted_len;
508 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
509 meshlink_errno = MESHLINK_ESTORAGE;
510 chacha_poly1305_exit(ctx);
523 /// Write a configuration file to a FILE handle.
524 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
526 uint8_t buf[config->len + 16];
527 size_t len = sizeof(buf);
529 randomize(&seqbuf, sizeof(seqbuf));
530 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
531 chacha_poly1305_set_key(ctx, key);
532 bool success = false;
534 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
535 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
537 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
538 meshlink_errno = MESHLINK_ESTORAGE;
541 chacha_poly1305_exit(ctx);
545 if(fwrite(config->buf, config->len, 1, f) != 1) {
546 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
547 meshlink_errno = MESHLINK_ESTORAGE;
551 if(fsync(fileno(f))) {
552 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
559 /// Free resources of a loaded configuration file.
560 void config_free(config_t *config) {
561 free((uint8_t *)config->buf);
566 /// Check the presence of a host configuration file.
567 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
568 if(!mesh->confbase && !conf_subdir) {
573 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
575 return access(path, F_OK) == 0;
578 /// Read a host configuration file.
579 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
580 if(!mesh->confbase && !conf_subdir) {
585 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
587 FILE *f = fopen(path, "r");
590 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
594 if(!config_read_file(mesh, f, config, key)) {
595 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
605 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
606 if(!mesh->confbase && !conf_subdir && !conf_type) {
612 char dname[PATH_MAX];
613 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
615 dir = opendir(dname);
618 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
619 meshlink_errno = MESHLINK_ESTORAGE;
623 while((ent = readdir(dir))) {
624 if(ent->d_name[0] == '.') {
628 if(!action(mesh, ent->d_name, arg)) {
638 /// Write a host configuration file.
639 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
640 if(!mesh->confbase && !conf_subdir && !name) {
645 char tmp_path[PATH_MAX + 4];
646 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
647 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
649 FILE *f = fopen(tmp_path, "w");
652 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
656 if(!config_write_file(mesh, f, config, key)) {
657 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
662 if(fsync(fileno(f))) {
663 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno));
669 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
673 if(rename(tmp_path, path)) {
674 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
681 /// Read the main configuration file.
682 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
683 if(!mesh->confbase && !conf_subdir) {
688 make_main_path(mesh, conf_subdir, path, sizeof(path));
690 FILE *f = fopen(path, "r");
693 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
697 if(!config_read_file(mesh, f, config, key)) {
698 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
708 /// Write the main configuration file.
709 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
710 if(!mesh->confbase && !conf_subdir) {
715 char tmp_path[PATH_MAX + 4];
716 make_main_path(mesh, conf_subdir, path, sizeof(path));
717 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
719 FILE *f = fopen(tmp_path, "w");
722 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
726 if(!config_write_file(mesh, f, config, key)) {
727 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
732 if(fsync(fileno(f))) {
733 logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno));
738 if(rename(tmp_path, path)) {
739 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
745 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
752 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
753 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
754 if(!mesh->confbase && !conf_subdir) {
759 char used_path[PATH_MAX];
760 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
761 make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
763 // Atomically rename the invitation file
764 if(rename(path, used_path)) {
765 if(errno == ENOENT) {
766 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
768 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
774 FILE *f = fopen(used_path, "r");
777 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
781 // Check the timestamp
784 if(fstat(fileno(f), &st)) {
785 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
791 if(mesh->loop.now.tv_sec > st.st_mtime + mesh->invitation_timeout) {
792 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
798 if(!config_read_file(mesh, f, config, key)) {
799 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
811 /// Write an invitation file.
812 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
813 if(!mesh->confbase && !conf_subdir) {
818 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
820 FILE *f = fopen(path, "w");
823 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
827 if(!config_write_file(mesh, f, config, key)) {
828 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
833 if(fsync(fileno(f))) {
834 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;