snprintf(path, len, "%s" SLASH "%s" SLASH "hosts" SLASH "%s", mesh->confbase, conf_subdir, name);
}
-/// Generate a path to an unused invitation file.
-static void make_invitation_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
- assert(conf_subdir);
- assert(name);
- assert(path);
- assert(len);
-
- snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s", mesh->confbase, conf_subdir, name);
-}
-
-/// Generate a path to a used invitation file.
-static void make_used_invitation_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
- assert(conf_subdir);
- assert(name);
- assert(path);
- assert(len);
-
- snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, conf_subdir, name);
-}
-
/// Remove a directory recursively
static bool deltree(const char *dirname) {
assert(dirname);
return false;
}
- make_invitation_path(mesh, conf_subdir, "", path, sizeof(path));
-
- if(mkdir(path, 0700)) {
- logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
- return false;
- }
-
return true;
}
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(time(NULL) >= 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;
-}
-
-/// Purge invitations for the given node
-size_t invitation_purge_node(meshlink_handle_t *mesh, const char *node_name) {
- 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];
-
- 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;
- }
-
- FILE *f = fopen(invname, "r");
-
- if(!f) {
- errno = 0;
- continue;
- }
-
- config_t config;
-
- if(!config_read_file(mesh, f, &config, mesh->config_key)) {
- logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", invname, strerror(errno));
- config_free(&config);
- fclose(f);
- errno = 0;
- continue;
- }
-
- packmsg_input_t in = {config.buf, config.len};
- packmsg_get_uint32(&in); // skip version
- char *name = packmsg_get_str_dup(&in);
-
- if(name && !strcmp(name, node_name)) {
- logger(mesh, MESHLINK_DEBUG, "Removing invitation for %s", node_name);
- unlink(invname);
- }
-
- free(name);
- config_free(&config);
- fclose(f);
- }
-
- 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;
-}