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);
488 if(flock(fileno(mesh->lockfile), LOCK_EX | LOCK_NB) != 0) {
489 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", lock_filename, strerror(errno));
490 fclose(mesh->lockfile);
491 mesh->lockfile = NULL;
492 meshlink_errno = MESHLINK_EBUSY;
501 /// Unlock the main configuration file.
502 void main_config_unlock(meshlink_handle_t *mesh) {
504 fclose(mesh->lockfile);
505 mesh->lockfile = NULL;
509 /// Read a configuration file from a FILE handle.
510 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
515 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
516 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
517 meshlink_errno = MESHLINK_ESTORAGE;
521 uint8_t *buf = xmalloc(len);
523 if(fread(buf, len, 1, f) != 1) {
524 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
525 meshlink_errno = MESHLINK_ESTORAGE;
530 uint8_t *decrypted = xmalloc(len);
531 size_t decrypted_len = len;
532 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
533 chacha_poly1305_set_key(ctx, key);
535 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
536 chacha_poly1305_exit(ctx);
538 config->buf = decrypted;
539 config->len = decrypted_len;
542 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
543 meshlink_errno = MESHLINK_ESTORAGE;
544 chacha_poly1305_exit(ctx);
557 /// Write a configuration file to a FILE handle.
558 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
562 uint8_t buf[config->len + 16];
563 size_t len = sizeof(buf);
565 randomize(&seqbuf, sizeof(seqbuf));
566 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
567 chacha_poly1305_set_key(ctx, key);
568 bool success = false;
570 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
571 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
574 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
577 meshlink_errno = MESHLINK_ESTORAGE;
579 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
580 meshlink_errno = MESHLINK_ESTORAGE;
583 chacha_poly1305_exit(ctx);
587 if(fwrite(config->buf, config->len, 1, f) != 1) {
588 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
589 meshlink_errno = MESHLINK_ESTORAGE;
594 logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
595 meshlink_errno = MESHLINK_ESTORAGE;
599 if(fsync(fileno(f))) {
600 logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
601 meshlink_errno = MESHLINK_ESTORAGE;
608 /// Free resources of a loaded configuration file.
609 void config_free(config_t *config) {
610 assert(!config->len || config->buf);
612 free((uint8_t *)config->buf);
617 /// Check the presence of a host configuration file.
618 bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
621 if(!mesh->confbase) {
626 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
628 return access(path, F_OK) == 0;
631 /// Read a host configuration file.
632 bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
635 if(!mesh->confbase) {
640 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
642 FILE *f = fopen(path, "r");
645 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
649 if(!config_read_file(mesh, f, config, key)) {
650 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
660 bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
664 if(!mesh->confbase) {
670 char dname[PATH_MAX];
671 snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
673 dir = opendir(dname);
676 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
677 meshlink_errno = MESHLINK_ESTORAGE;
681 while((ent = readdir(dir))) {
682 if(ent->d_name[0] == '.') {
686 if(!action(mesh, ent->d_name, arg)) {
696 /// Write a host configuration file.
697 bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
702 if(!mesh->confbase) {
707 char tmp_path[PATH_MAX + 4];
708 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
709 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
711 FILE *f = fopen(tmp_path, "w");
714 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
715 meshlink_errno = MESHLINK_ESTORAGE;
719 if(!config_write_file(mesh, f, config, key)) {
720 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
726 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
727 meshlink_errno = MESHLINK_ESTORAGE;
731 if(rename(tmp_path, path)) {
732 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
733 meshlink_errno = MESHLINK_ESTORAGE;
740 /// Delete a host configuration file.
741 bool config_delete(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
745 if(!mesh->confbase) {
750 make_host_path(mesh, conf_subdir, name, path, sizeof(path));
752 if(unlink(path) && errno != ENOENT) {
753 logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
754 meshlink_errno = MESHLINK_ESTORAGE;
761 /// Read the main configuration file.
762 bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
766 if(!mesh->confbase) {
771 make_main_path(mesh, conf_subdir, path, sizeof(path));
773 FILE *f = fopen(path, "r");
776 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
780 if(!config_read_file(mesh, f, config, key)) {
781 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
791 /// Write the main configuration file.
792 bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
796 if(!mesh->confbase) {
801 char tmp_path[PATH_MAX + 4];
802 make_main_path(mesh, conf_subdir, path, sizeof(path));
803 snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
805 FILE *f = fopen(tmp_path, "w");
808 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
809 meshlink_errno = MESHLINK_ESTORAGE;
813 if(!config_write_file(mesh, f, config, key)) {
814 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
819 if(rename(tmp_path, path)) {
820 logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
821 meshlink_errno = MESHLINK_ESTORAGE;
827 logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
828 meshlink_errno = MESHLINK_ESTORAGE;