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));
87 logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
93 logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
101 /// Try decrypting the main configuration file from the given sub-directory.
102 static bool main_config_decrypt(meshlink_handle_t *mesh, const char *conf_subdir) {
103 if(!mesh->config_key && !mesh->confbase && !conf_subdir) {
109 if(!main_config_read(mesh, conf_subdir, &config, mesh->config_key)) {
110 logger(mesh, MESHLINK_ERROR, "Could not read main configuration file");
114 packmsg_input_t in = {config.buf, config.len};
116 uint32_t version = packmsg_get_uint32(&in);
117 config_free(&config);
119 return version == MESHLINK_CONFIG_VERSION;
122 /// Create a fresh configuration directory
123 bool config_init(meshlink_handle_t *mesh, const char *conf_subdir) {
124 if(!mesh->confbase) {
132 if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
133 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno));
139 // Create "current" sub-directory in the confbase
140 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
143 if(mkdir(path, 0700)) {
144 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
148 make_host_path(mesh, conf_subdir, "", path, sizeof(path));
150 if(mkdir(path, 0700)) {
151 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
155 make_invitation_path(mesh, conf_subdir, "", path, sizeof(path));
157 if(mkdir(path, 0700)) {
158 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
165 /// Wipe an existing configuration directory
166 bool config_destroy(const char *confbase, const char *conf_subdir) {
167 if(!confbase && !conf_subdir) {
175 // Check the presence of configuration base sub directory.
176 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
178 if(stat(path, &st)) {
179 if(errno == ENOENT) {
182 logger(NULL, MESHLINK_ERROR, "Cannot stat %s: %s\n", path, strerror(errno));
183 meshlink_errno = MESHLINK_ESTORAGE;
188 // Remove meshlink.conf
189 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "meshlink.conf", confbase, conf_subdir);
192 if(errno != ENOENT) {
193 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
194 meshlink_errno = MESHLINK_ESTORAGE;
199 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
204 static bool copytree(const char *src_dir_name, const void *src_key, const char *dst_dir_name, const void *dst_key) {
205 if(!src_dir_name || !dst_dir_name) {
209 char src_filename[PATH_MAX];
210 char dst_filename[PATH_MAX];
213 DIR *src_dir = opendir(src_dir_name);
216 logger(NULL, MESHLINK_ERROR, "Could not open directory file %s\n", src_dir_name);
220 // Delete if already exists and create a new destination directory
221 deltree(dst_dir_name);
223 if(mkdir(dst_dir_name, 0700)) {
224 logger(NULL, MESHLINK_ERROR, "Could not create directory %s\n", dst_filename);
228 while((ent = readdir(src_dir))) {
229 if(ent->d_name[0] == '.') {
233 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", dst_dir_name, ent->d_name);
234 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", src_dir_name, ent->d_name);
236 if(ent->d_type == DT_DIR) {
237 if(!copytree(src_filename, src_key, dst_filename, dst_key)) {
238 logger(NULL, MESHLINK_ERROR, "Copying %s to %s failed\n", src_filename, dst_filename);
242 if(!sync_path(dst_filename)) {
245 } else if(ent->d_type == DT_REG) {
249 if(stat(src_filename, &st)) {
250 logger(NULL, MESHLINK_ERROR, "Could not stat file `%s': %s\n", src_filename, strerror(errno));
254 FILE *f = fopen(src_filename, "r");
257 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s\n", src_filename, strerror(errno));
261 if(!config_read_file(NULL, f, &config, src_key)) {
262 logger(NULL, MESHLINK_ERROR, "Failed to read `%s': %s\n", src_filename, strerror(errno));
268 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s\n", src_filename, strerror(errno));
269 config_free(&config);
273 f = fopen(dst_filename, "w");
276 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s", dst_filename, strerror(errno));
277 config_free(&config);
281 if(!config_write_file(NULL, f, &config, dst_key)) {
282 logger(NULL, MESHLINK_ERROR, "Failed to write `%s': %s", dst_filename, strerror(errno));
283 config_free(&config);
289 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s", dst_filename, strerror(errno));
290 config_free(&config);
294 config_free(&config);
296 struct utimbuf times;
297 times.modtime = st.st_mtime;
298 times.actime = st.st_atime;
300 if(utime(dst_filename, ×)) {
301 logger(NULL, MESHLINK_ERROR, "Failed to utime `%s': %s", dst_filename, strerror(errno));
311 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) {
312 char src_filename[PATH_MAX];
313 char dst_filename[PATH_MAX];
315 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", mesh->confbase, dst_dir_name);
316 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", mesh->confbase, src_dir_name);
318 return copytree(src_filename, src_key, dst_filename, dst_key);
321 /// Check the presence of the main configuration file.
322 bool main_config_exists(meshlink_handle_t *mesh, const char *conf_subdir) {
323 if(!mesh->confbase && !conf_subdir) {
328 make_main_path(mesh, conf_subdir, path, sizeof(path));
329 return access(path, F_OK) == 0;
332 bool config_rename(meshlink_handle_t *mesh, const char *old_conf_subdir, const char *new_conf_subdir) {
333 if(!mesh->confbase && !old_conf_subdir && !new_conf_subdir) {
337 char old_path[PATH_MAX];
338 char new_path[PATH_MAX];
340 snprintf(old_path, sizeof(old_path), "%s" SLASH "%s", mesh->confbase, old_conf_subdir);
341 snprintf(new_path, sizeof(new_path), "%s" SLASH "%s", mesh->confbase, new_conf_subdir);
343 return rename(old_path, new_path) == 0;
346 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
347 if(!mesh->confbase) {
351 bool confbase_exists = false;
352 bool confbase_decryptable = false;
354 if(main_config_exists(mesh, "current")) {
355 confbase_exists = true;
357 if(mesh->config_key && main_config_decrypt(mesh, "current")) {
358 confbase_decryptable = true;
362 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "new")) {
363 confbase_exists = true;
365 if(main_config_decrypt(mesh, "new")) {
366 if(!config_destroy(mesh->confbase, "current")) {
370 if(!config_rename(mesh, "new", "current")) {
374 confbase_decryptable = true;
378 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "old")) {
379 confbase_exists = true;
381 if(main_config_decrypt(mesh, "old")) {
382 if(!config_destroy(mesh->confbase, "current")) {
386 if(!config_rename(mesh, "old", "current")) {
390 confbase_decryptable = true;
394 // Cleanup if current is existing with old and new
395 if(confbase_exists && confbase_decryptable) {
396 config_destroy(mesh->confbase, "old");
397 config_destroy(mesh->confbase, "new");
400 return confbase_exists;
403 /// Lock the main configuration file.
404 bool main_config_lock(meshlink_handle_t *mesh) {
405 if(!mesh->confbase) {
410 make_main_path(mesh, "current", path, sizeof(path));
412 mesh->conffile = fopen(path, "r");
414 if(!mesh->conffile) {
415 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
416 meshlink_errno = MESHLINK_ESTORAGE;
421 fcntl(fileno(mesh->conffile), F_SETFD, FD_CLOEXEC);
425 // TODO: use _locking()?
428 if(flock(fileno(mesh->conffile), LOCK_EX | LOCK_NB) != 0) {
429 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
430 fclose(mesh->conffile);
431 mesh->conffile = NULL;
432 meshlink_errno = MESHLINK_EBUSY;
441 /// Unlock the main configuration file.
442 void main_config_unlock(meshlink_handle_t *mesh) {
444 fclose(mesh->conffile);
445 mesh->conffile = NULL;
449 /// Read a configuration file from a FILE handle.
450 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
453 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
454 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
455 meshlink_errno = MESHLINK_ESTORAGE;
460 uint8_t *buf = xmalloc(len);
462 if(fread(buf, len, 1, f) != 1) {
463 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
464 meshlink_errno = MESHLINK_ESTORAGE;
470 uint8_t *decrypted = xmalloc(len);
471 size_t decrypted_len = len;
472 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
473 chacha_poly1305_set_key(ctx, key);
475 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
476 chacha_poly1305_exit(ctx);
478 config->buf = decrypted;
479 config->len = decrypted_len;
482 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
483 meshlink_errno = MESHLINK_ESTORAGE;
484 chacha_poly1305_exit(ctx);
497 /// Write a configuration file to a FILE handle.
498 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
500 uint8_t buf[config->len + 16];
501 size_t len = sizeof(buf);
503 randomize(&seqbuf, sizeof(seqbuf));
504 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
505 chacha_poly1305_set_key(ctx, key);
506 bool success = false;
508 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
509 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
511 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
512 meshlink_errno = MESHLINK_ESTORAGE;
515 chacha_poly1305_exit(ctx);
519 if(fwrite(config->buf, config->len, 1, f) != 1) {
520 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
521 meshlink_errno = MESHLINK_ESTORAGE;
525 if(fsync(fileno(f))) {
526 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
533 /// Free resources of a loaded configuration file.
534 void config_free(config_t *config) {
535 free((uint8_t *)config->buf);
540 /// Check the presence of a host configuration file.
541 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
542 if(!mesh->confbase && !conf_subdir) {
547 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
549 return access(path, F_OK) == 0;
552 /// Read a host configuration file.
553 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
554 if(!mesh->confbase && !conf_subdir) {
559 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
561 FILE *f = fopen(path, "r");
564 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
568 if(!config_read_file(mesh, f, config, key)) {
569 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
579 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
580 if(!mesh->confbase && !conf_subdir && !conf_type) {
586 char dname[PATH_MAX];
587 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
589 dir = opendir(dname);
592 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
593 meshlink_errno = MESHLINK_ESTORAGE;
597 while((ent = readdir(dir))) {
598 if(ent->d_name[0] == '.') {
602 if(!action(mesh, ent->d_name, arg)) {
612 /// Write a host configuration file.
613 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
614 if(!mesh->confbase && !conf_subdir && !name) {
619 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
621 FILE *f = fopen(path, "w");
624 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
628 if(!config_write_file(mesh, f, config, key)) {
629 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
635 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
642 /// Read the main configuration file.
643 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
644 if(!mesh->confbase && !conf_subdir) {
649 make_main_path(mesh, conf_subdir, path, sizeof(path));
651 FILE *f = fopen(path, "r");
654 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
658 if(!config_read_file(mesh, f, config, key)) {
659 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
669 /// Write the main configuration file.
670 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
671 if(!mesh->confbase && !conf_subdir) {
676 make_main_path(mesh, conf_subdir, path, sizeof(path));
678 FILE *f = fopen(path, "w");
681 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
685 if(!config_write_file(mesh, f, config, key)) {
686 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
692 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
699 /// Read an invitation file from the confbase sub-directory, and immediately delete it.
700 bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
701 if(!mesh->confbase && !conf_subdir) {
706 char used_path[PATH_MAX];
707 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
708 make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
710 // Atomically rename the invitation file
711 if(rename(path, used_path)) {
712 if(errno == ENOENT) {
713 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
715 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
721 FILE *f = fopen(used_path, "r");
724 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
728 // Check the timestamp
731 if(fstat(fileno(f), &st)) {
732 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
738 if(mesh->loop.now.tv_sec > st.st_mtime + mesh->invitation_timeout) {
739 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
745 if(!config_read_file(mesh, f, config, key)) {
746 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
758 /// Write an invitation file.
759 bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
760 if(!mesh->confbase && !conf_subdir) {
765 make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
767 FILE *f = fopen(path, "w");
770 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
774 if(!config_write_file(mesh, f, config, key)) {
775 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
781 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
788 /// Purge old invitation files
789 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
790 if(!mesh->confbase) {
795 make_invitation_path(mesh, "current", "", path, sizeof(path));
797 DIR *dir = opendir(path);
800 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
801 meshlink_errno = MESHLINK_ESTORAGE;
809 while((ent = readdir(dir))) {
810 if(strlen(ent->d_name) != 24) {
814 char invname[PATH_MAX];
817 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
818 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
822 if(!stat(invname, &st)) {
823 if(mesh->invitation_key && deadline < st.st_mtime) {
829 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
835 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
837 meshlink_errno = MESHLINK_ESTORAGE;