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 /// Remove a directory recursively
52 static bool deltree(const char *dirname) {
55 DIR *d = opendir(dirname);
60 while((ent = readdir(d))) {
61 if(ent->d_name[0] == '.') {
62 if(!ent->d_name[1] || (ent->d_name[1] == '.' && !ent->d_name[2])) {
67 char filename[PATH_MAX];
68 snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
70 if(unlink(filename)) {
71 if(!deltree(filename)) {
79 return errno == ENOENT;
82 return rmdir(dirname) == 0;
85 bool sync_path(const char *pathname) {
88 int fd = open(pathname, O_RDONLY);
91 logger(NULL, MESHLINK_ERROR, "Failed to open %s: %s\n", pathname, strerror(errno));
92 meshlink_errno = MESHLINK_ESTORAGE;
97 logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
99 meshlink_errno = MESHLINK_ESTORAGE;
104 logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
106 meshlink_errno = MESHLINK_ESTORAGE;
113 /// Try decrypting the main configuration file from the given sub-directory.
114 static bool main_config_decrypt(meshlink_handle_t *mesh, const char *conf_subdir) {
115 assert(mesh->config_key);
116 assert(mesh->confbase);
121 if(!main_config_read(mesh, conf_subdir, &config, mesh->config_key)) {
122 logger(mesh, MESHLINK_ERROR, "Could not read main configuration file");
126 packmsg_input_t in = {config.buf, config.len};
128 uint32_t version = packmsg_get_uint32(&in);
129 config_free(&config);
131 return version == MESHLINK_CONFIG_VERSION;
134 /// Create a fresh configuration directory
135 bool config_init(meshlink_handle_t *mesh, const char *conf_subdir) {
138 if(!mesh->confbase) {
144 // Create "current" sub-directory in the confbase
145 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
148 logger(mesh, MESHLINK_DEBUG, "Could not delete directory %s: %s\n", path, strerror(errno));
152 if(mkdir(path, 0700)) {
153 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
157 make_host_path(mesh, conf_subdir, "", path, sizeof(path));
159 if(mkdir(path, 0700)) {
160 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
167 /// Wipe an existing configuration directory
168 bool config_destroy(const char *confbase, const char *conf_subdir) {
179 // Check the presence of configuration base sub directory.
180 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
182 if(stat(path, &st)) {
183 if(errno == ENOENT) {
186 logger(NULL, MESHLINK_ERROR, "Cannot stat %s: %s\n", path, strerror(errno));
187 meshlink_errno = MESHLINK_ESTORAGE;
192 // Remove meshlink.conf
193 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "meshlink.conf", confbase, conf_subdir);
196 if(errno != ENOENT) {
197 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
198 meshlink_errno = MESHLINK_ESTORAGE;
203 snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
206 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
207 meshlink_errno = MESHLINK_ESTORAGE;
211 return sync_path(confbase);
214 static bool copytree(const char *src_dir_name, const void *src_key, const char *dst_dir_name, const void *dst_key) {
215 assert(src_dir_name);
216 assert(dst_dir_name);
218 char src_filename[PATH_MAX];
219 char dst_filename[PATH_MAX];
222 DIR *src_dir = opendir(src_dir_name);
225 logger(NULL, MESHLINK_ERROR, "Could not open directory file %s\n", src_dir_name);
226 meshlink_errno = MESHLINK_ESTORAGE;
230 // Delete if already exists and create a new destination directory
231 if(!deltree(dst_dir_name)) {
232 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", dst_dir_name, strerror(errno));
233 meshlink_errno = MESHLINK_ESTORAGE;
237 if(mkdir(dst_dir_name, 0700)) {
238 logger(NULL, MESHLINK_ERROR, "Could not create directory %s\n", dst_filename);
239 meshlink_errno = MESHLINK_ESTORAGE;
243 while((ent = readdir(src_dir))) {
244 if(ent->d_name[0] == '.') {
248 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", dst_dir_name, ent->d_name);
249 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", src_dir_name, ent->d_name);
251 if(ent->d_type == DT_DIR) {
252 if(!copytree(src_filename, src_key, dst_filename, dst_key)) {
253 logger(NULL, MESHLINK_ERROR, "Copying %s to %s failed\n", src_filename, dst_filename);
254 meshlink_errno = MESHLINK_ESTORAGE;
258 if(!sync_path(dst_filename)) {
261 } else if(ent->d_type == DT_REG) {
265 if(stat(src_filename, &st)) {
266 logger(NULL, MESHLINK_ERROR, "Could not stat file `%s': %s\n", src_filename, strerror(errno));
267 meshlink_errno = MESHLINK_ESTORAGE;
271 FILE *f = fopen(src_filename, "r");
274 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s\n", src_filename, strerror(errno));
275 meshlink_errno = MESHLINK_ESTORAGE;
279 if(!config_read_file(NULL, f, &config, src_key)) {
280 logger(NULL, MESHLINK_ERROR, "Failed to read `%s': %s\n", src_filename, strerror(errno));
282 meshlink_errno = MESHLINK_ESTORAGE;
287 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s\n", src_filename, strerror(errno));
288 config_free(&config);
289 meshlink_errno = MESHLINK_ESTORAGE;
293 f = fopen(dst_filename, "w");
296 logger(NULL, MESHLINK_ERROR, "Failed to open `%s': %s", dst_filename, strerror(errno));
297 config_free(&config);
298 meshlink_errno = MESHLINK_ESTORAGE;
302 if(!config_write_file(NULL, f, &config, dst_key)) {
303 logger(NULL, MESHLINK_ERROR, "Failed to write `%s': %s", dst_filename, strerror(errno));
304 config_free(&config);
306 meshlink_errno = MESHLINK_ESTORAGE;
311 logger(NULL, MESHLINK_ERROR, "Failed to close `%s': %s", dst_filename, strerror(errno));
312 config_free(&config);
313 meshlink_errno = MESHLINK_ESTORAGE;
317 config_free(&config);
319 struct utimbuf times;
320 times.modtime = st.st_mtime;
321 times.actime = st.st_atime;
323 if(utime(dst_filename, ×)) {
324 logger(NULL, MESHLINK_ERROR, "Failed to utime `%s': %s", dst_filename, strerror(errno));
325 meshlink_errno = MESHLINK_ESTORAGE;
335 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) {
336 assert(src_dir_name);
337 assert(dst_dir_name);
339 char src_filename[PATH_MAX];
340 char dst_filename[PATH_MAX];
342 snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", mesh->confbase, dst_dir_name);
343 snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", mesh->confbase, src_dir_name);
345 return copytree(src_filename, src_key, dst_filename, dst_key);
348 /// Check the presence of the main configuration file.
349 bool main_config_exists(meshlink_handle_t *mesh, const char *conf_subdir) {
352 if(!mesh->confbase) {
357 make_main_path(mesh, conf_subdir, path, sizeof(path));
358 return access(path, F_OK) == 0;
361 bool config_rename(meshlink_handle_t *mesh, const char *old_conf_subdir, const char *new_conf_subdir) {
362 assert(old_conf_subdir);
363 assert(new_conf_subdir);
365 if(!mesh->confbase) {
369 char old_path[PATH_MAX];
370 char new_path[PATH_MAX];
372 snprintf(old_path, sizeof(old_path), "%s" SLASH "%s", mesh->confbase, old_conf_subdir);
373 snprintf(new_path, sizeof(new_path), "%s" SLASH "%s", mesh->confbase, new_conf_subdir);
375 return rename(old_path, new_path) == 0 && sync_path(mesh->confbase);
378 bool config_sync(meshlink_handle_t *mesh, const char *conf_subdir) {
381 if(!mesh->confbase || mesh->storage_policy == MESHLINK_STORAGE_DISABLED) {
386 snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "hosts", mesh->confbase, conf_subdir);
388 if(!sync_path(path)) {
392 snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
394 if(!sync_path(path)) {
401 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
402 if(!mesh->confbase) {
406 bool confbase_exists = false;
407 bool confbase_decryptable = false;
409 if(main_config_exists(mesh, "current")) {
410 confbase_exists = true;
412 if(mesh->config_key && main_config_decrypt(mesh, "current")) {
413 confbase_decryptable = true;
417 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "new")) {
418 confbase_exists = true;
420 if(main_config_decrypt(mesh, "new")) {
421 if(!config_destroy(mesh->confbase, "current")) {
425 if(!config_rename(mesh, "new", "current")) {
429 confbase_decryptable = true;
433 if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "old")) {
434 confbase_exists = true;
436 if(main_config_decrypt(mesh, "old")) {
437 if(!config_destroy(mesh->confbase, "current")) {
441 if(!config_rename(mesh, "old", "current")) {
445 confbase_decryptable = true;
449 // Cleanup if current is existing with old and new
450 if(confbase_exists && confbase_decryptable) {
451 if(!config_destroy(mesh->confbase, "old") || !config_destroy(mesh->confbase, "new")) {
456 return confbase_exists;
459 /// Lock the main configuration file. Creates confbase if necessary.
460 bool main_config_lock(meshlink_handle_t *mesh, const char *lock_filename) {
461 if(!mesh->confbase) {
465 assert(lock_filename);
467 if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
468 logger(NULL, MESHLINK_ERROR, "Cannot create configuration directory %s: %s", mesh->confbase, strerror(errno));
469 meshlink_close(mesh);
470 meshlink_errno = MESHLINK_ESTORAGE;
474 mesh->lockfile = fopen(lock_filename, "w+");
476 if(!mesh->lockfile) {
477 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", lock_filename, strerror(errno));
478 meshlink_errno = MESHLINK_ESTORAGE;
483 fcntl(fileno(mesh->lockfile), F_SETFD, FD_CLOEXEC);
487 // TODO: use _locking()?
490 if(flock(fileno(mesh->lockfile), LOCK_EX | LOCK_NB) != 0) {
491 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", lock_filename, strerror(errno));
492 fclose(mesh->lockfile);
493 mesh->lockfile = NULL;
494 meshlink_errno = MESHLINK_EBUSY;
503 /// Unlock the main configuration file.
504 void main_config_unlock(meshlink_handle_t *mesh) {
506 fclose(mesh->lockfile);
507 mesh->lockfile = NULL;
511 /// Read a configuration file from a FILE handle.
512 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
517 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
518 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
519 meshlink_errno = MESHLINK_ESTORAGE;
523 uint8_t *buf = xmalloc(len);
525 if(fread(buf, len, 1, f) != 1) {
526 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
527 meshlink_errno = MESHLINK_ESTORAGE;
532 uint8_t *decrypted = xmalloc(len);
533 size_t decrypted_len = len;
534 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
535 chacha_poly1305_set_key(ctx, key);
537 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
538 chacha_poly1305_exit(ctx);
540 config->buf = decrypted;
541 config->len = decrypted_len;
544 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
545 meshlink_errno = MESHLINK_ESTORAGE;
546 chacha_poly1305_exit(ctx);
559 /// Write a configuration file to a FILE handle.
560 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
564 uint8_t buf[config->len + 16];
565 size_t len = sizeof(buf);
567 randomize(&seqbuf, sizeof(seqbuf));
568 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
569 chacha_poly1305_set_key(ctx, key);
570 bool success = false;
572 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
573 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
576 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
579 meshlink_errno = MESHLINK_ESTORAGE;
581 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
582 meshlink_errno = MESHLINK_ESTORAGE;
585 chacha_poly1305_exit(ctx);
589 if(fwrite(config->buf, config->len, 1, f) != 1) {
590 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
591 meshlink_errno = MESHLINK_ESTORAGE;
596 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
597 meshlink_errno = MESHLINK_ESTORAGE;
601 if(fsync(fileno(f))) {
602 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
603 meshlink_errno = MESHLINK_ESTORAGE;
610 /// Free resources of a loaded configuration file.
611 void config_free(config_t *config) {
612 assert(!config->len || config->buf);
614 free((uint8_t *)config->buf);
619 /// Check the presence of a host configuration file.
620 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
623 if(!mesh->confbase) {
628 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
630 return access(path, F_OK) == 0;
633 /// Read a host configuration file.
634 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
637 if(!mesh->confbase) {
642 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
644 FILE *f = fopen(path, "r");
647 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
651 if(!config_read_file(mesh, f, config, key)) {
652 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
662 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
666 if(!mesh->confbase) {
672 char dname[PATH_MAX];
673 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
675 dir = opendir(dname);
678 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
679 meshlink_errno = MESHLINK_ESTORAGE;
683 while((ent = readdir(dir))) {
684 if(ent->d_name[0] == '.') {
688 if(!action(mesh, ent->d_name, arg)) {
698 /// Write a host configuration file.
699 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
704 if(!mesh->confbase) {
709 char tmp_path[PATH_MAX + 4];
710 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
711 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
713 FILE *f = fopen(tmp_path, "w");
716 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
717 meshlink_errno = MESHLINK_ESTORAGE;
721 if(!config_write_file(mesh, f, config, key)) {
722 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
728 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
729 meshlink_errno = MESHLINK_ESTORAGE;
733 if(rename(tmp_path, path)) {
734 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
735 meshlink_errno = MESHLINK_ESTORAGE;
742 /// Delete a host configuration file.
743 bool config_delete(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
747 if(!mesh->confbase) {
752 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
754 if(unlink(path) && errno != ENOENT) {
755 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
756 meshlink_errno = MESHLINK_ESTORAGE;
763 /// Read the main configuration file.
764 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
768 if(!mesh->confbase) {
773 make_main_path(mesh, conf_subdir, path, sizeof(path));
775 FILE *f = fopen(path, "r");
778 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
782 if(!config_read_file(mesh, f, config, key)) {
783 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
793 /// Write the main configuration file.
794 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
798 if(!mesh->confbase) {
803 char tmp_path[PATH_MAX + 4];
804 make_main_path(mesh, conf_subdir, path, sizeof(path));
805 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
807 FILE *f = fopen(tmp_path, "w");
810 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
811 meshlink_errno = MESHLINK_ESTORAGE;
815 if(!config_write_file(mesh, f, config, key)) {
816 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
821 if(rename(tmp_path, path)) {
822 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
823 meshlink_errno = MESHLINK_ESTORAGE;
829 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
830 meshlink_errno = MESHLINK_ESTORAGE;