virtual-config.c revision 8536cd46be13bc18aa538b279d7fdba7d54e268f
5e0ce63bb65db34d7f48b34bbb5545fa791781c4Timo Sirainen/* Copyright (c) 2008-2013 Dovecot authors, see the included COPYING file */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "lib.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "array.h"
e1e5b1f4ada9d9b4d36edeaf1c5229be90b12815Timo Sirainen#include "crc32.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "istream.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "str.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "unichar.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "imap-parser.h"
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen#include "imap-match.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "mail-namespace.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "mail-search-build.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "mail-search-parser.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "mailbox-list-iter.h"
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen#include "virtual-storage.h"
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include "virtual-plugin.h"
cd466fe7b84b0223735a6469c7f7bc225f65996dTimo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include <unistd.h>
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen#include <fcntl.h>
d56384d5226c8860079d0d0b08b83404e8c42986Timo Sirainen
d56384d5226c8860079d0d0b08b83404e8c42986Timo Sirainenstruct virtual_parse_context {
d56384d5226c8860079d0d0b08b83404e8c42986Timo Sirainen struct virtual_mailbox *mbox;
d56384d5226c8860079d0d0b08b83404e8c42986Timo Sirainen struct istream *input;
d56384d5226c8860079d0d0b08b83404e8c42986Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen pool_t pool;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen string_t *rule;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen unsigned int rule_idx;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen char sep;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen bool have_wildcards;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen bool have_mailbox_defines;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen};
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenstatic struct mail_search_args *
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenvirtual_search_args_parse(const string_t *rule, const char **error_r)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen{
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen struct istream *input;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen struct imap_parser *imap_parser;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen const struct imap_arg *args;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen struct mail_search_parser *parser;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen struct mail_search_args *sargs;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen const char *charset = "UTF-8";
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen bool fatal;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen int ret;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen if (str_len(rule) == 0) {
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen sargs = mail_search_build_init();
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen mail_search_build_add_all(sargs);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen return sargs;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen }
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen input = i_stream_create_from_data(str_data(rule), str_len(rule));
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen (void)i_stream_read(input);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
a3ea111cfdbfd4f32baeb0bd7f1d72568c60a023Timo Sirainen imap_parser = imap_parser_create(input, NULL, (size_t)-1);
a3ea111cfdbfd4f32baeb0bd7f1d72568c60a023Timo Sirainen ret = imap_parser_finish_line(imap_parser, 0, 0, &args);
a3ea111cfdbfd4f32baeb0bd7f1d72568c60a023Timo Sirainen if (ret < 0) {
a3ea111cfdbfd4f32baeb0bd7f1d72568c60a023Timo Sirainen sargs = NULL;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen *error_r = t_strdup(imap_parser_get_error(imap_parser, &fatal));
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen } else {
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen parser = mail_search_parser_init_imap(args);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen if (mail_search_build(mail_search_register_get_imap(),
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen parser, &charset, &sargs, error_r) < 0)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen sargs = NULL;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen mail_search_parser_deinit(&parser);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen }
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen imap_parser_unref(&imap_parser);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen i_stream_destroy(&input);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen return sargs;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen}
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenstatic int
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenvirtual_config_add_rule(struct virtual_parse_context *ctx, const char **error_r)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen{
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen struct virtual_backend_box *const *bboxes;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mail_search_args *search_args;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen unsigned int i, count;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen i_assert(str_len(ctx->rule) == 0);
a3ea111cfdbfd4f32baeb0bd7f1d72568c60a023Timo Sirainen return 0;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen }
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen ctx->mbox->search_args_crc32 =
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen crc32_str_more(ctx->mbox->search_args_crc32, str_c(ctx->rule));
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen search_args = virtual_search_args_parse(ctx->rule, error_r);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen str_truncate(ctx->rule, 0);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen if (search_args == NULL) {
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen *error_r = t_strconcat("Previous search rule is invalid: ",
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen *error_r, NULL);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen return -1;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen }
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen /* update at all the mailboxes that were introduced since the previous
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen rule. */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen i_assert(ctx->rule_idx < count);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen for (i = ctx->rule_idx; i < count; i++) {
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen i_assert(bboxes[i]->search_args == NULL);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen mail_search_args_ref(search_args);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen bboxes[i]->search_args = search_args;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen }
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen mail_search_args_unref(&search_args);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen ctx->rule_idx = array_count(&ctx->mbox->backend_boxes);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen return 0;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen}
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainenstatic int
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenvirtual_config_parse_line(struct virtual_parse_context *ctx, const char *line,
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen const char **error_r)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen{
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen struct mail_user *user = ctx->mbox->storage->storage.user;
6defed8fef1781cb1d34353784232e87c19f173bTimo Sirainen struct virtual_backend_box *bbox;
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen const char *name;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
6defed8fef1781cb1d34353784232e87c19f173bTimo Sirainen if (*line == ' ' || *line == '\t') {
6defed8fef1781cb1d34353784232e87c19f173bTimo Sirainen /* continues the previous search rule */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen *error_r = "Search rule without a mailbox";
fc34e919907845ce01ad04aa8213033596a1ae70Timo Sirainen return -1;
38499bb33c74acc6d725204e893cfc02a5890ec7Timo Sirainen }
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen while (*line == ' ' || *line == '\t') line++;
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen str_append_c(ctx->rule, ' ');
6defed8fef1781cb1d34353784232e87c19f173bTimo Sirainen str_append(ctx->rule, line);
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen return 0;
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen }
6defed8fef1781cb1d34353784232e87c19f173bTimo Sirainen /* if there is no rule yet, it means we want the previous mailboxes
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen to use the rule that comes later */
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen if (str_len(ctx->rule) > 0) {
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen if (virtual_config_add_rule(ctx, error_r) < 0)
0c9754d1cf2ba0ff7873b6baa614ca10051a08fdTimo Sirainen return -1;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen }
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen /* new mailbox. the search args are added to it later. */
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen if (strcasecmp(line, "INBOX") == 0)
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen line = "INBOX";
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen bbox->name = p_strdup(ctx->pool, line);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen if (*line == '-' || *line == '+' || *line == '!') line++;
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen bbox->ns = strcasecmp(line, "INBOX") == 0 ?
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen mail_namespace_find_inbox(user->namespaces) :
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen mail_namespace_find(user->namespaces, line);
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen if (!uni_utf8_str_is_valid(bbox->name)) {
33ca6b017b6ebbd048651b5e3d16915001dbc291Timo Sirainen *error_r = t_strdup_printf("Mailbox name not UTF-8: %s",
bbox->name);
return -1;
}
if (bbox->ns == NULL) {
*error_r = t_strdup_printf("Namespace not found for %s",
bbox->name);
return -1;
}
if (bbox->name[0] == '+') {
bbox->name++;
bbox->clear_recent = TRUE;
}
if (strchr(bbox->name, '*') != NULL ||
strchr(bbox->name, '%') != NULL) {
name = bbox->name[0] == '-' ? bbox->name + 1 : bbox->name;
bbox->glob = imap_match_init(ctx->pool, name, TRUE, ctx->sep);
ctx->have_wildcards = TRUE;
} else if (bbox->name[0] == '!') {
/* save messages here */
if (ctx->mbox->save_bbox != NULL) {
*error_r = "Multiple save mailboxes defined";
return -1;
}
bbox->name++;
ctx->mbox->save_bbox = bbox;
}
ctx->have_mailbox_defines = TRUE;
array_append(&ctx->mbox->backend_boxes, &bbox, 1);
return 0;
}
static void
virtual_mailbox_get_list_patterns(struct virtual_parse_context *ctx)
{
struct virtual_mailbox *mbox = ctx->mbox;
ARRAY_TYPE(mailbox_virtual_patterns) *dest;
struct mailbox_virtual_pattern pattern;
struct virtual_backend_box *const *bboxes;
unsigned int i, count;
memset(&pattern, 0, sizeof(pattern));
bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
p_array_init(&mbox->list_include_patterns, ctx->pool, count);
p_array_init(&mbox->list_exclude_patterns, ctx->pool, count);
for (i = 0; i < count; i++) {
pattern.ns = bboxes[i]->ns;
pattern.pattern = bboxes[i]->name;
if (*pattern.pattern != '-')
dest = &mbox->list_include_patterns;
else {
dest = &mbox->list_exclude_patterns;
pattern.pattern++;
}
array_append(dest, &pattern, 1);
}
}
static void
separate_wildcard_mailboxes(struct virtual_mailbox *mbox,
ARRAY_TYPE(virtual_backend_box) *wildcard_boxes,
ARRAY_TYPE(virtual_backend_box) *neg_boxes)
{
struct virtual_backend_box *const *bboxes;
ARRAY_TYPE(virtual_backend_box) *dest;
unsigned int i, count;
bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
t_array_init(wildcard_boxes, I_MIN(16, count));
t_array_init(neg_boxes, 4);
for (i = 0; i < count;) {
if (*bboxes[i]->name == '-')
dest = neg_boxes;
else if (bboxes[i]->glob != NULL)
dest = wildcard_boxes;
else {
dest = NULL;
i++;
}
if (dest != NULL) {
array_append(dest, &bboxes[i], 1);
array_delete(&mbox->backend_boxes, i, 1);
bboxes = array_get_modifiable(&mbox->backend_boxes,
&count);
}
}
}
static void virtual_config_copy_expanded(struct virtual_parse_context *ctx,
struct virtual_backend_box *wbox,
const char *name)
{
struct virtual_backend_box *bbox;
bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
*bbox = *wbox;
bbox->name = p_strdup(ctx->pool, name);
bbox->glob = NULL;
bbox->wildcard = TRUE;
mail_search_args_ref(bbox->search_args);
array_append(&ctx->mbox->backend_boxes, &bbox, 1);
}
static bool virtual_ns_match(struct mail_namespace *config_ns,
struct mail_namespace *iter_ns)
{
/* we match only one namespace for each pattern, except with shared
namespaces match also autocreated children */
if (config_ns == iter_ns)
return TRUE;
if (config_ns->type == iter_ns->type &&
(config_ns->flags & NAMESPACE_FLAG_AUTOCREATED) == 0 &&
(iter_ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0)
return TRUE;
return FALSE;
}
static bool virtual_config_match(const struct mailbox_info *info,
ARRAY_TYPE(virtual_backend_box) *boxes_arr,
unsigned int *idx_r)
{
struct virtual_backend_box *const *boxes;
unsigned int i, count;
boxes = array_get_modifiable(boxes_arr, &count);
for (i = 0; i < count; i++) {
if (boxes[i]->glob != NULL) {
if (virtual_ns_match(boxes[i]->ns, info->ns) &&
imap_match(boxes[i]->glob,
info->vname) == IMAP_MATCH_YES) {
*idx_r = i;
return TRUE;
}
} else {
i_assert(boxes[i]->name[0] == '-');
if (strcmp(boxes[i]->name + 1, info->vname) == 0) {
*idx_r = i;
return TRUE;
}
}
}
return FALSE;
}
static int virtual_config_expand_wildcards(struct virtual_parse_context *ctx)
{
const enum mail_namespace_type iter_ns_types =
MAIL_NAMESPACE_TYPE_MASK_ALL;
const enum mailbox_list_iter_flags iter_flags =
MAILBOX_LIST_ITER_RETURN_NO_FLAGS;
struct mail_user *user = ctx->mbox->storage->storage.user;
ARRAY_TYPE(virtual_backend_box) wildcard_boxes, neg_boxes;
struct mailbox_list_iterate_context *iter;
struct virtual_backend_box *const *wboxes;
const char **patterns;
const struct mailbox_info *info;
unsigned int i, j, count;
separate_wildcard_mailboxes(ctx->mbox, &wildcard_boxes, &neg_boxes);
/* get patterns we want to list */
wboxes = array_get_modifiable(&wildcard_boxes, &count);
if (count == 0) {
/* only negative wildcards - doesn't really make sense.
just ignore. */
return 0;
}
patterns = t_new(const char *, count + 1);
for (i = 0; i < count; i++)
patterns[i] = wboxes[i]->name;
/* match listed mailboxes to wildcards */
iter = mailbox_list_iter_init_namespaces(user->namespaces, patterns,
iter_ns_types, iter_flags);
while ((info = mailbox_list_iter_next(iter)) != NULL) {
/* skip non-selectable mailboxes (especially mbox
directories) */
if ((info->flags & MAILBOX_NOSELECT) != 0)
continue;
if (virtual_config_match(info, &wildcard_boxes, &i) &&
!virtual_config_match(info, &neg_boxes, &j) &&
virtual_backend_box_lookup_name(ctx->mbox,
info->vname) == NULL) {
virtual_config_copy_expanded(ctx, wboxes[i],
info->vname);
}
}
for (i = 0; i < count; i++)
mail_search_args_unref(&wboxes[i]->search_args);
return mailbox_list_iter_deinit(&iter);
}
static void virtual_config_search_args_dup(struct virtual_mailbox *mbox)
{
struct virtual_backend_box *const *bboxes;
struct mail_search_args *old_args;
unsigned int i, count;
bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
for (i = 0; i < count; i++) {
old_args = bboxes[i]->search_args;
bboxes[i]->search_args = mail_search_args_dup(old_args);
mail_search_args_unref(&old_args);
}
}
int virtual_config_read(struct virtual_mailbox *mbox)
{
struct mail_storage *storage = mbox->box.storage;
struct virtual_parse_context ctx;
const char *box_path, *path, *line, *error;
unsigned int linenum = 0;
int fd, ret = 0;
i_array_init(&mbox->backend_boxes, 8);
mbox->search_args_crc32 = (uint32_t)-1;
box_path = mailbox_get_path(&mbox->box);
path = t_strconcat(box_path, "/"VIRTUAL_CONFIG_FNAME, NULL);
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno == EACCES) {
mail_storage_set_critical(storage, "%s",
mail_error_eacces_msg("open", path));
} else if (errno != ENOENT) {
mail_storage_set_critical(storage,
"open(%s) failed: %m", path);
} else if (errno == ENOENT) {
mail_storage_set_error(storage, MAIL_ERROR_NOTFOUND,
T_MAIL_ERR_MAILBOX_NOT_FOUND(mbox->box.vname));
} else {
mail_storage_set_critical(storage,
"stat(%s) failed: %m", box_path);
}
return -1;
}
memset(&ctx, 0, sizeof(ctx));
ctx.sep = mail_namespaces_get_root_sep(storage->user->namespaces);
ctx.mbox = mbox;
ctx.pool = mbox->box.pool;
ctx.rule = t_str_new(256);
ctx.input = i_stream_create_fd(fd, (size_t)-1, FALSE);
i_stream_set_return_partial_line(ctx.input, TRUE);
while ((line = i_stream_read_next_line(ctx.input)) != NULL) {
linenum++;
if (*line == '#')
continue;
if (*line == '\0')
ret = virtual_config_add_rule(&ctx, &error);
else
ret = virtual_config_parse_line(&ctx, line, &error);
if (ret < 0) {
mail_storage_set_critical(storage,
"%s: Error at line %u: %s",
path, linenum, error);
break;
}
}
if (ret == 0) {
ret = virtual_config_add_rule(&ctx, &error);
if (ret < 0) {
mail_storage_set_critical(storage,
"%s: Error at line %u: %s",
path, linenum, error);
}
}
virtual_mailbox_get_list_patterns(&ctx);
if (ret == 0 && ctx.have_wildcards)
ret = virtual_config_expand_wildcards(&ctx);
if (ret == 0 && !ctx.have_mailbox_defines) {
mail_storage_set_critical(storage,
"%s: No mailboxes defined", path);
ret = -1;
}
if (ret == 0)
virtual_config_search_args_dup(mbox);
i_stream_unref(&ctx.input);
i_close_fd(&fd);
return ret;
}
void virtual_config_free(struct virtual_mailbox *mbox)
{
struct virtual_backend_box *const *bboxes;
unsigned int i, count;
if (!array_is_created(&mbox->backend_boxes)) {
/* mailbox wasn't opened */
return;
}
bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
for (i = 0; i < count; i++) {
if (bboxes[i]->search_args != NULL)
mail_search_args_unref(&bboxes[i]->search_args);
}
array_free(&mbox->backend_boxes);
}