static int mdbox_map_mkdir_storage(struct mdbox_map *map) { if (mailbox_list_mkdir_root(map->root_list, map->path, MAILBOX_LIST_PATH_TYPE_DIR) < 0) { mail_storage_copy_list_error(MAP_STORAGE(map), map->root_list); return -1; } if (strcmp(map->path, map->index_path) != 0 && mailbox_list_mkdir_root(map->root_list, map->index_path, MAILBOX_LIST_PATH_TYPE_INDEX) < 0) { mail_storage_copy_list_error(MAP_STORAGE(map), map->root_list); return -1; } return 0; }
int mdbox_file_create_fd(struct dbox_file *file, const char *path, bool parents) { struct mdbox_file *mfile = (struct mdbox_file *)file; struct mdbox_map *map = mfile->storage->map; struct mailbox_permissions perm; mode_t old_mask; const char *p, *dir; int fd; mailbox_list_get_root_permissions(map->root_list, &perm); old_mask = umask(0666 & ~perm.file_create_mode); fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); umask(old_mask); if (fd == -1 && errno == ENOENT && parents && (p = strrchr(path, '/')) != NULL) { dir = t_strdup_until(path, p); if (mailbox_list_mkdir_root(map->root_list, dir, path != file->alt_path ? MAILBOX_LIST_PATH_TYPE_DIR : MAILBOX_LIST_PATH_TYPE_ALT_DIR) < 0) { mail_storage_copy_list_error(&file->storage->storage, map->root_list); return -1; } /* try again */ old_mask = umask(0666 & ~perm.file_create_mode); fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); umask(old_mask); } if (fd == -1) { mail_storage_set_critical(&file->storage->storage, "open(%s, O_CREAT) failed: %m", path); } else if (perm.file_create_gid == (gid_t)-1) { /* no group change */ } else if (fchown(fd, (uid_t)-1, perm.file_create_gid) < 0) { if (errno == EPERM) { mail_storage_set_critical(&file->storage->storage, "%s", eperm_error_get_chgrp("fchown", path, perm.file_create_gid, perm.file_create_gid_origin)); } else { mail_storage_set_critical(&file->storage->storage, "fchown(%s, -1, %ld) failed: %m", path, (long)perm.file_create_gid); } /* continue anyway */ } return fd; }
int subsfile_set_subscribed(struct mailbox_list *list, const char *path, const char *temp_prefix, const char *name, bool set) { const struct mail_storage_settings *mail_set = list->mail_set; struct dotlock_settings dotlock_set; struct dotlock *dotlock; struct mailbox_permissions perm; const char *line, *dir, *fname, *escaped_name; struct istream *input = NULL; struct ostream *output; int fd_in, fd_out; enum mailbox_list_path_type type; bool found, changed = FALSE, failed = FALSE; unsigned int version = 2; if (strcasecmp(name, "INBOX") == 0) name = "INBOX"; memset(&dotlock_set, 0, sizeof(dotlock_set)); dotlock_set.use_excl_lock = mail_set->dotlock_use_excl; dotlock_set.nfs_flush = mail_set->mail_nfs_storage; dotlock_set.temp_prefix = temp_prefix; dotlock_set.timeout = SUBSCRIPTION_FILE_LOCK_TIMEOUT; dotlock_set.stale_timeout = SUBSCRIPTION_FILE_CHANGE_TIMEOUT; mailbox_list_get_root_permissions(list, &perm); fd_out = file_dotlock_open_group(&dotlock_set, path, 0, perm.file_create_mode, perm.file_create_gid, perm.file_create_gid_origin, &dotlock); if (fd_out == -1 && errno == ENOENT) { /* directory hasn't been created yet. */ type = list->set.control_dir != NULL ? MAILBOX_LIST_PATH_TYPE_CONTROL : MAILBOX_LIST_PATH_TYPE_DIR; fname = strrchr(path, '/'); if (fname != NULL) { dir = t_strdup_until(path, fname); if (mailbox_list_mkdir_root(list, dir, type) < 0) return -1; } fd_out = file_dotlock_open_group(&dotlock_set, path, 0, perm.file_create_mode, perm.file_create_gid, perm.file_create_gid_origin, &dotlock); } if (fd_out == -1) { if (errno == EAGAIN) { mailbox_list_set_error(list, MAIL_ERROR_TEMP, "Timeout waiting for subscription file lock"); } else { subswrite_set_syscall_error(list, "file_dotlock_open()", path); } return -1; } fd_in = nfs_safe_open(path, O_RDONLY); if (fd_in == -1 && errno != ENOENT) { subswrite_set_syscall_error(list, "open()", path); file_dotlock_delete(&dotlock); return -1; } if (fd_in != -1) { input = i_stream_create_fd_autoclose(&fd_in, list->mailbox_name_max_length+1); i_stream_set_return_partial_line(input, TRUE); subsfile_list_read_header(list, input, &version); } found = FALSE; output = o_stream_create_fd_file(fd_out, 0, FALSE); o_stream_cork(output); if (version >= 2) o_stream_send_str(output, version2_header); if (version < 2 || name[0] == '\0') escaped_name = name; else { const char *const *tmp; char separators[2]; string_t *str = t_str_new(64); separators[0] = mailbox_list_get_hierarchy_sep(list); separators[1] = '\0'; tmp = t_strsplit(name, separators); str_append_tabescaped(str, *tmp); for (tmp++; *tmp != NULL; tmp++) { str_append_c(str, '\t'); str_append_tabescaped(str, *tmp); } escaped_name = str_c(str); } if (input != NULL) { while ((line = next_line(list, path, input, &failed, FALSE)) != NULL) { if (strcmp(line, escaped_name) == 0) { found = TRUE; if (!set) { changed = TRUE; continue; } } o_stream_nsend_str(output, line); o_stream_nsend(output, "\n", 1); } i_stream_destroy(&input); } if (!failed && set && !found) { /* append subscription */ line = t_strconcat(escaped_name, "\n", NULL); o_stream_nsend_str(output, line); changed = TRUE; } if (changed && !failed) { if (o_stream_nfinish(output) < 0) { subswrite_set_syscall_error(list, "write()", path); failed = TRUE; } else if (mail_set->parsed_fsync_mode != FSYNC_MODE_NEVER) { if (fsync(fd_out) < 0) { subswrite_set_syscall_error(list, "fsync()", path); failed = TRUE; } } } else { o_stream_ignore_last_errors(output); } o_stream_destroy(&output); if (failed || !changed) { if (file_dotlock_delete(&dotlock) < 0) { subswrite_set_syscall_error(list, "file_dotlock_delete()", path); failed = TRUE; } } else { enum dotlock_replace_flags flags = DOTLOCK_REPLACE_FLAG_VERIFY_OWNER; if (file_dotlock_replace(&dotlock, flags) < 0) { subswrite_set_syscall_error(list, "file_dotlock_replace()", path); failed = TRUE; } } return failed ? -1 : (changed ? 1 : 0); }
static uint32_t mailbox_uidvalidity_next_rescan(struct mailbox_list *list, const char *path) { DIR *d; struct dirent *dp; const char *fname, *dir, *prefix, *tmp; unsigned int i, prefix_len; uint32_t cur_value, min_value, max_value; mode_t old_mask; int fd; fname = strrchr(path, '/'); if (fname == NULL) { dir = "."; fname = path; } else { dir = t_strdup_until(path, fname); fname++; } d = opendir(dir); if (d == NULL && errno == ENOENT) { /* FIXME: the PATH_TYPE_CONTROL should come as a parameter, but that's an API change, do it in v2.3. it's not really a problem though, since currently all backends use control dirs for the uidvalidity file. */ (void)mailbox_list_mkdir_root(list, dir, MAILBOX_LIST_PATH_TYPE_CONTROL); d = opendir(dir); } if (d == NULL) { i_error("opendir(%s) failed: %m", dir); return mailbox_uidvalidity_next_fallback(); } prefix = t_strconcat(fname, ".", NULL); prefix_len = strlen(prefix); /* just in case there happens to be multiple matching uidvalidity files, track the min/max values. use the max value and delete the min value file. */ max_value = 0; min_value = (uint32_t)-1; while ((dp = readdir(d)) != NULL) { if (strncmp(dp->d_name, prefix, prefix_len) == 0) { if (str_to_uint32_hex(dp->d_name + prefix_len, &cur_value) >= 0) { if (min_value > cur_value) min_value = cur_value; if (max_value < cur_value) max_value = cur_value; } } } if (closedir(d) < 0) i_error("closedir(%s) failed: %m", dir); if (max_value == 0) { /* no uidvalidity files. create one. */ for (i = 0; i < RETRY_COUNT; i++) { cur_value = mailbox_uidvalidity_next_fallback(); tmp = t_strdup_printf("%s.%08x", path, cur_value); /* the file is empty, don't bother with permissions */ old_mask = umask(0); fd = open(tmp, O_RDWR | O_CREAT | O_EXCL, 0444); umask(old_mask); if (fd != -1 || errno != EEXIST) break; /* already exists. although it's quite unlikely we'll hit this race condition. more likely we'll create a duplicate file.. */ } if (fd == -1) { i_error("creat(%s) failed: %m", tmp); return cur_value; } i_close_fd(&fd); mailbox_uidvalidity_write(list, path, cur_value); return cur_value; } if (min_value != max_value) { /* duplicate uidvalidity files, delete the oldest */ tmp = t_strdup_printf("%s.%08x", path, min_value); i_unlink_if_exists(tmp); } cur_value = max_value; if (mailbox_uidvalidity_rename(path, &cur_value, TRUE) < 0) return mailbox_uidvalidity_next_fallback(); mailbox_uidvalidity_write(list, path, cur_value); return cur_value; }