virtual-config.c revision 4a6f18b8c03dbb81b1e4f90242fe7788589d7876
/* Copyright (c) 2008 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "crc32.h"
#include "istream.h"
#include "str.h"
#include "imap-parser.h"
#include "imap-match.h"
#include "mail-search-build.h"
#include "virtual-storage.h"
#include "virtual-plugin.h"
#include <unistd.h>
#include <fcntl.h>
struct virtual_parse_context {
struct virtual_mailbox *mbox;
struct istream *input;
pool_t pool;
string_t *rule;
unsigned int rule_idx;
char sep;
bool have_wildcards;
};
static struct mail_search_args *
virtual_search_args_parse(const string_t *rule, const char **error_r)
{
struct istream *input;
struct imap_parser *parser;
const struct imap_arg *args;
struct mail_search_args *sargs;
bool fatal;
int ret;
input = i_stream_create_from_data(str_data(rule), str_len(rule));
(void)i_stream_read(input);
parser = imap_parser_create(input, NULL, (size_t)-1);
ret = imap_parser_finish_line(parser, 0, 0, &args);
if (ret < 0) {
sargs = NULL;
*error_r = t_strdup(imap_parser_get_error(parser, &fatal));
} else if (mail_search_build_from_imap_args(args, "UTF-8", &sargs,
error_r) < 0)
sargs = NULL;
imap_parser_destroy(&parser);
i_stream_destroy(&input);
return sargs;
}
static int
virtual_config_add_rule(struct virtual_parse_context *ctx, const char **error_r)
{
struct virtual_backend_box *const *bboxes;
struct mail_search_args *search_args;
unsigned int i, count;
if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
i_assert(str_len(ctx->rule) == 0);
return 0;
}
ctx->mbox->search_args_crc32 =
crc32_str_more(ctx->mbox->search_args_crc32, str_c(ctx->rule));
search_args = virtual_search_args_parse(ctx->rule, error_r);
str_truncate(ctx->rule, 0);
if (search_args == NULL) {
*error_r = t_strconcat("Previous search rule is invalid: ",
*error_r, NULL);
return -1;
}
/* update at all the mailboxes that were introduced since the previous
rule. */
bboxes = array_get(&ctx->mbox->backend_boxes, &count);
i_assert(ctx->rule_idx < count);
for (i = ctx->rule_idx; i < count; i++) {
i_assert(bboxes[i]->search_args == NULL);
mail_search_args_ref(search_args);
bboxes[i]->search_args = search_args;
}
mail_search_args_unref(&search_args);
ctx->rule_idx = array_count(&ctx->mbox->backend_boxes);
return 0;
}
static int
virtual_config_parse_line(struct virtual_parse_context *ctx, const char *line,
const char **error_r)
{
struct mail_user *user = ctx->mbox->storage->storage.ns->user;
struct virtual_backend_box *bbox;
if (*line == ' ') {
/* continues the previous search rule */
if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
*error_r = "Search rule without a mailbox";
return -1;
}
str_append(ctx->rule, line);
return 0;
}
/* if there is no rule yet, it means we want the previous mailboxes
to use the rule that comes later */
if (str_len(ctx->rule) > 0) {
if (virtual_config_add_rule(ctx, error_r) < 0)
return -1;
}
/* new mailbox. the search args are added to it later. */
bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
bbox->name = p_strdup(ctx->pool, line);
if (strchr(bbox->name, '*') != NULL ||
strchr(bbox->name, '%') != NULL) {
bbox->glob = imap_match_init(ctx->pool, bbox->name,
TRUE, ctx->sep);
bbox->ns = mail_namespace_find(user->namespaces, &line);
ctx->have_wildcards = TRUE;
}
array_append(&ctx->mbox->backend_boxes, &bbox, 1);
return 0;
}
static void
separate_wildcard_mailboxes(struct virtual_mailbox *mbox,
ARRAY_TYPE(virtual_backend_box) *wildcard_boxes)
{
struct virtual_backend_box *const *bboxes;
unsigned int i, count;
bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
t_array_init(wildcard_boxes, I_MIN(16, count));
for (i = 0; i < count;) {
if (bboxes[i]->glob == NULL)
i++;
else {
array_append(wildcard_boxes, &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;
mail_search_args_ref(bbox->search_args);
array_append(&ctx->mbox->backend_boxes, &bbox, 1);
}
static int virtual_config_expand_wildcards(struct virtual_parse_context *ctx)
{
struct mail_user *user = ctx->mbox->storage->storage.ns->user;
ARRAY_TYPE(virtual_backend_box) wildcard_boxes;
struct mailbox_list_iterate_context *iter;
struct virtual_backend_box *const *wboxes;
const char **patterns;
const struct mailbox_info *info;
unsigned int i, count;
separate_wildcard_mailboxes(ctx->mbox, &wildcard_boxes);
/* get patterns we want to list */
wboxes = array_get_modifiable(&wildcard_boxes, &count);
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,
MAILBOX_LIST_ITER_VIRTUAL_NAMES |
MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
while ((info = mailbox_list_iter_next(iter)) != NULL) {
for (i = 0; i < count; i++) {
/* we match only one namespace for each pattern.
skip non-selectable mailboxes (especially mbox
directories) */
if (wboxes[i]->ns == info->ns &&
(info->flags & MAILBOX_NOSELECT) == 0 &&
imap_match(wboxes[i]->glob,
info->name) == IMAP_MATCH_YES) {
virtual_config_copy_expanded(ctx, wboxes[i],
info->name);
}
}
}
for (i = 0; i < count; i++)
mail_search_args_unref(&wboxes[i]->search_args);
return mailbox_list_iter_deinit(&iter);
}
int virtual_config_read(struct virtual_mailbox *mbox)
{
struct mail_user *user = mbox->storage->storage.ns->user;
struct virtual_parse_context ctx;
const char *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;
path = t_strconcat(mbox->path, "/"VIRTUAL_CONFIG_FNAME, NULL);
fd = open(path, O_RDWR);
if (fd == -1) {
if (errno == ENOENT) {
mail_storage_set_error(mbox->ibox.storage,
MAIL_ERROR_NOTPOSSIBLE,
"Virtual mailbox missing configuration file");
return -1;
}
mail_storage_set_critical(mbox->ibox.storage,
"open(%s) failed: %m", path);
return -1;
}
memset(&ctx, 0, sizeof(ctx));
ctx.sep = mail_namespace_get_root_sep(user->namespaces);
ctx.mbox = mbox;
ctx.pool = mbox->ibox.box.pool;
ctx.rule = t_str_new(256);
ctx.input = i_stream_create_fd(fd, (size_t)-1, FALSE);
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(mbox->ibox.storage,
"%s: Error at line %u: %s",
path, linenum, error);
break;
}
}
if (ret == 0)
ret = virtual_config_add_rule(&ctx, &error);
if (ret == 0 && ctx.have_wildcards)
ret = virtual_config_expand_wildcards(&ctx);
if (ret == 0 && array_count(&mbox->backend_boxes) == 0) {
mail_storage_set_critical(mbox->ibox.storage,
"%s: No mailboxes defined", path);
ret = -1;
}
i_stream_unref(&ctx.input);
(void)close(fd);
return ret;
}
void virtual_config_free(struct virtual_mailbox *mbox)
{
struct virtual_backend_box *const *bboxes;
unsigned int i, count;
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);
}
}