+bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
+ if(!mesh->confbase) {
+ return false;
+ }
+
+ bool confbase_exists = false;
+ bool confbase_decryptable = false;
+
+ if(main_config_exists(mesh, "current")) {
+ confbase_exists = true;
+
+ if(mesh->config_key && main_config_decrypt(mesh, "current")) {
+ confbase_decryptable = true;
+ }
+ }
+
+ if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "new")) {
+ confbase_exists = true;
+
+ if(main_config_decrypt(mesh, "new")) {
+ if(!config_destroy(mesh->confbase, "current")) {
+ return false;
+ }
+
+ if(!config_rename(mesh, "new", "current")) {
+ return false;
+ }
+
+ confbase_decryptable = true;
+ }
+ }
+
+ if(mesh->config_key && !confbase_decryptable && main_config_exists(mesh, "old")) {
+ confbase_exists = true;
+
+ if(main_config_decrypt(mesh, "old")) {
+ if(!config_destroy(mesh->confbase, "current")) {
+ return false;
+ }
+
+ if(!config_rename(mesh, "old", "current")) {
+ return false;
+ }
+
+ confbase_decryptable = true;
+ }
+ }
+
+ // Cleanup if current is existing with old and new
+ if(confbase_exists && confbase_decryptable) {
+ if(!config_destroy(mesh->confbase, "old") || !config_destroy(mesh->confbase, "new")) {
+ return false;
+ }
+ }
+
+ return confbase_exists;
+}
+
+/// Lock the main configuration file. Creates confbase if necessary.
+bool main_config_lock(meshlink_handle_t *mesh) {
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
+ logger(NULL, MESHLINK_ERROR, "Cannot create configuration directory %s: %s", mesh->confbase, strerror(errno));
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return NULL;
+ }
+
+ char path[PATH_MAX];
+ snprintf(path, sizeof(path), "%s" SLASH "meshlink.lock", mesh->confbase);
+
+ mesh->lockfile = fopen(path, "w+");
+
+ if(!mesh->lockfile) {
+ logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+#ifdef FD_CLOEXEC
+ fcntl(fileno(mesh->lockfile), F_SETFD, FD_CLOEXEC);
+#endif
+
+#ifdef HAVE_MINGW
+ // TODO: use _locking()?
+#else
+
+ if(flock(fileno(mesh->lockfile), LOCK_EX | LOCK_NB) != 0) {
+ logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
+ fclose(mesh->lockfile);
+ mesh->lockfile = NULL;
+ meshlink_errno = MESHLINK_EBUSY;
+ return false;
+ }
+
+#endif
+
+ return true;
+}
+
+/// Unlock the main configuration file.
+void main_config_unlock(meshlink_handle_t *mesh) {
+ if(mesh->lockfile) {
+ fclose(mesh->lockfile);
+ mesh->lockfile = NULL;
+ }
+}
+
+/// Read a configuration file from a FILE handle.
+bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const void *key) {
+ assert(f);
+
+ long len;
+
+ if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
+ logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ uint8_t *buf = xmalloc(len);
+
+ if(fread(buf, len, 1, f) != 1) {
+ logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(key) {
+ uint8_t *decrypted = xmalloc(len);
+ size_t decrypted_len = len;
+ chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
+ chacha_poly1305_set_key(ctx, key);
+
+ if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
+ chacha_poly1305_exit(ctx);
+ free(buf);
+ config->buf = decrypted;
+ config->len = decrypted_len;
+ return true;
+ } else {
+ logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
+ meshlink_errno = MESHLINK_ESTORAGE;
+ chacha_poly1305_exit(ctx);
+ free(decrypted);
+ free(buf);
+ return false;
+ }
+ }
+
+ config->buf = buf;
+ config->len = len;
+
+ return true;
+}
+
+/// Write a configuration file to a FILE handle.
+bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, const void *key) {
+ assert(f);
+
+ if(key) {
+ uint8_t buf[config->len + 16];
+ size_t len = sizeof(buf);
+ uint8_t seqbuf[12];
+ randomize(&seqbuf, sizeof(seqbuf));
+ chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
+ chacha_poly1305_set_key(ctx, key);
+ bool success = false;
+
+ if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
+ success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
+
+ if(!success) {
+ logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
+ }
+
+ meshlink_errno = MESHLINK_ESTORAGE;
+ } else {
+ logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
+ meshlink_errno = MESHLINK_ESTORAGE;
+ }
+
+ chacha_poly1305_exit(ctx);
+ return success;
+ }
+
+ if(fwrite(config->buf, config->len, 1, f) != 1) {
+ logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(fflush(f)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(fsync(fileno(f))) {
+ logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ return true;
+}
+
+/// Free resources of a loaded configuration file.
+void config_free(config_t *config) {
+ assert(!config->len || config->buf);
+
+ free((uint8_t *)config->buf);
+ config->buf = NULL;
+ config->len = 0;
+}
+
+/// Check the presence of a host configuration file.
+bool config_exists(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
+ assert(conf_subdir);
+
+ if(!mesh->confbase) {
+ return false;
+ }
+
+ char path[PATH_MAX];
+ make_host_path(mesh, conf_subdir, name, path, sizeof(path));
+
+ return access(path, F_OK) == 0;
+}
+
+/// Read a host configuration file.
+bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
+ assert(conf_subdir);
+
+ if(!mesh->confbase) {
+ return false;
+ }
+
+ char path[PATH_MAX];
+ make_host_path(mesh, conf_subdir, name, path, sizeof(path));
+
+ FILE *f = fopen(path, "r");
+
+ if(!f) {
+ logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
+ return false;
+ }
+
+ if(!config_read_file(mesh, f, config, key)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
+ fclose(f);
+ return false;
+ }
+
+ fclose(f);
+
+ return true;
+}
+
+bool config_scan_all(meshlink_handle_t *mesh, const char *conf_subdir, const char *conf_type, config_scan_action_t action, void *arg) {
+ assert(conf_subdir);
+ assert(conf_type);
+
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ DIR *dir;
+ struct dirent *ent;
+ char dname[PATH_MAX];
+ snprintf(dname, sizeof(dname), "%s" SLASH "%s" SLASH "%s", mesh->confbase, conf_subdir, conf_type);
+
+ dir = opendir(dname);
+
+ if(!dir) {
+ logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ while((ent = readdir(dir))) {
+ if(ent->d_name[0] == '.') {
+ continue;
+ }
+
+ if(!action(mesh, ent->d_name, arg)) {
+ closedir(dir);
+ return false;
+ }
+ }
+
+ closedir(dir);
+ return true;
+}
+
+/// Write a host configuration file.
+bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
+ assert(conf_subdir);
+ assert(name);
+ assert(config);
+
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ char path[PATH_MAX];
+ char tmp_path[PATH_MAX + 4];
+ make_host_path(mesh, conf_subdir, name, path, sizeof(path));
+ snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
+
+ FILE *f = fopen(tmp_path, "w");
+
+ if(!f) {
+ logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(!config_write_file(mesh, f, config, key)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
+ fclose(f);
+ return false;
+ }
+
+ if(fclose(f)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(rename(tmp_path, path)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ return true;
+}
+
+/// Delete a host configuration file.
+bool config_delete(meshlink_handle_t *mesh, const char *conf_subdir, const char *name) {
+ assert(conf_subdir);
+ assert(name);
+
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ char path[PATH_MAX];
+ make_host_path(mesh, conf_subdir, name, path, sizeof(path));
+
+ if(unlink(path) && errno != ENOENT) {
+ logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ return true;
+}
+
+/// Read the main configuration file.
+bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t *config, void *key) {
+ assert(conf_subdir);
+ assert(config);
+
+ if(!mesh->confbase) {
+ return false;
+ }
+
+ char path[PATH_MAX];
+ make_main_path(mesh, conf_subdir, path, sizeof(path));
+
+ FILE *f = fopen(path, "r");
+
+ if(!f) {
+ logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
+ return false;
+ }
+
+ if(!config_read_file(mesh, f, config, key)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
+ fclose(f);
+ return false;
+ }
+
+ fclose(f);
+
+ return true;
+}
+
+/// Write the main configuration file.
+bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const config_t *config, void *key) {
+ assert(conf_subdir);
+ assert(config);
+
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ char path[PATH_MAX];
+ char tmp_path[PATH_MAX + 4];
+ make_main_path(mesh, conf_subdir, path, sizeof(path));
+ snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
+
+ FILE *f = fopen(tmp_path, "w");
+
+ if(!f) {
+ logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(!config_write_file(mesh, f, config, key)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
+ fclose(f);
+ return false;
+ }
+
+ if(rename(tmp_path, path)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ fclose(f);
+ return false;
+ }
+
+ if(fclose(f)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ return true;
+}
+
+/// Read an invitation file from the confbase sub-directory, and immediately delete it.
+bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, config_t *config, void *key) {
+ assert(conf_subdir);
+ assert(name);
+ assert(config);
+
+ if(!mesh->confbase) {
+ return false;
+ }
+
+ char path[PATH_MAX];
+ char used_path[PATH_MAX];
+ make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
+ make_used_invitation_path(mesh, conf_subdir, name, used_path, sizeof(used_path));
+
+ // Atomically rename the invitation file
+ if(rename(path, used_path)) {
+ if(errno == ENOENT) {
+ logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
+ } else {
+ logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
+ }
+
+ return false;
+ }
+
+ FILE *f = fopen(used_path, "r");
+
+ if(!f) {
+ logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
+ return false;
+ }
+
+ // Check the timestamp
+ struct stat st;
+
+ if(fstat(fileno(f), &st)) {
+ logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
+ fclose(f);
+ unlink(used_path);
+ return false;
+ }
+
+ if(mesh->loop.now.tv_sec >= st.st_mtime + mesh->invitation_timeout) {
+ logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
+ fclose(f);
+ unlink(used_path);
+ return false;
+ }
+
+ if(!config_read_file(mesh, f, config, key)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
+ fclose(f);
+ unlink(used_path);
+ return false;
+ }
+
+ fclose(f);
+
+ if(unlink(used_path)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to unlink `%s': %s", path, strerror(errno));
+ return false;
+ }
+
+ snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
+
+ if(!sync_path(path)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ return true;
+}
+
+/// Write an invitation file.
+bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, const config_t *config, void *key) {
+ assert(conf_subdir);
+ assert(name);
+ assert(config);
+
+ if(!mesh->confbase) {
+ return false;
+ }
+
+ char path[PATH_MAX];
+ make_invitation_path(mesh, conf_subdir, name, path, sizeof(path));
+
+ FILE *f = fopen(path, "w");
+
+ if(!f) {
+ logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(!config_write_file(mesh, f, config, key)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
+ fclose(f);
+ return false;
+ }
+
+ if(fclose(f)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "invitations", mesh->confbase, conf_subdir);
+
+ if(!sync_path(path)) {
+ logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ return true;
+}
+
+/// Purge old invitation files
+size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ char path[PATH_MAX];
+ make_invitation_path(mesh, "current", "", path, sizeof(path));
+
+ DIR *dir = opendir(path);
+
+ if(!dir) {
+ logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return 0;
+ }
+
+ errno = 0;
+ size_t count = 0;
+ struct dirent *ent;
+
+ while((ent = readdir(dir))) {
+ if(strlen(ent->d_name) != 24) {
+ continue;
+ }
+
+ char invname[PATH_MAX];
+ struct stat st;
+
+ if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
+ logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
+ continue;
+ }
+
+ if(!stat(invname, &st)) {
+ if(mesh->invitation_key && deadline < st.st_mtime) {
+ count++;
+ } else {
+ unlink(invname);
+ }
+ } else {
+ logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
+ errno = 0;
+ }
+ }
+
+ if(errno) {
+ logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
+ closedir(dir);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return 0;
+ }
+
+ closedir(dir);
+
+ return count;