virtual-config.c revision de4d9f77d6378a416c9963963eac5ac18b75ec0b
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "lib.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "array.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "crc32.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "istream.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "str.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "imap-parser.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "imap-match.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "mail-search-build.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "mail-search-parser.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "virtual-storage.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "virtual-plugin.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include <unistd.h>
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include <fcntl.h>
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainenstruct virtual_parse_context {
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen struct virtual_mailbox *mbox;
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen struct istream *input;
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen pool_t pool;
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen string_t *rule;
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen unsigned int rule_idx;
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen char sep;
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen bool have_wildcards;
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen};
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainenstatic struct mail_search_args *
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainenvirtual_search_args_parse(const string_t *rule, const char **error_r)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen{
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen struct istream *input;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen struct imap_parser *imap_parser;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen const struct imap_arg *args;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen struct mail_search_parser *parser;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen struct mail_search_args *sargs;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen bool fatal;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen int ret;
5a8ee853d0b62692a6e624b125d08d87a79e001fTimo Sirainen
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen if (str_len(rule) == 0) {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen sargs = mail_search_build_init();
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen mail_search_build_add_all(sargs);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen return sargs;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen }
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen input = i_stream_create_from_data(str_data(rule), str_len(rule));
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen (void)i_stream_read(input);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen imap_parser = imap_parser_create(input, NULL, (size_t)-1);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen ret = imap_parser_finish_line(imap_parser, 0, 0, &args);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if (ret < 0) {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen sargs = NULL;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen *error_r = t_strdup(imap_parser_get_error(imap_parser, &fatal));
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen } else {
5a8ee853d0b62692a6e624b125d08d87a79e001fTimo Sirainen parser = mail_search_parser_init_imap(args);
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen if (mail_search_build(mail_search_register_imap, parser, "UTF-8",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen &sargs, error_r) < 0)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen sargs = NULL;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen mail_search_parser_deinit(&parser);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen }
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen imap_parser_destroy(&imap_parser);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen i_stream_destroy(&input);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen return sargs;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen}
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
58a770f1e0ab553a0dba9cad9d6f3a6cdf2dc855Timo Sirainenstatic int
58a770f1e0ab553a0dba9cad9d6f3a6cdf2dc855Timo Sirainenvirtual_config_add_rule(struct virtual_parse_context *ctx, const char **error_r)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen{
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen struct virtual_backend_box *const *bboxes;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen struct mail_search_args *search_args;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen unsigned int i, count;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen i_assert(str_len(ctx->rule) == 0);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen return 0;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen }
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen ctx->mbox->search_args_crc32 =
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen crc32_str_more(ctx->mbox->search_args_crc32, str_c(ctx->rule));
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen search_args = virtual_search_args_parse(ctx->rule, error_r);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen str_truncate(ctx->rule, 0);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if (search_args == NULL) {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen *error_r = t_strconcat("Previous search rule is invalid: ",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen *error_r, NULL);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen return -1;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen }
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* update at all the mailboxes that were introduced since the previous
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen rule. */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen i_assert(ctx->rule_idx < count);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen for (i = ctx->rule_idx; i < count; i++) {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen i_assert(bboxes[i]->search_args == NULL);
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen mail_search_args_ref(search_args);
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen bboxes[i]->search_args = search_args;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen }
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen mail_search_args_unref(&search_args);
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen ctx->rule_idx = array_count(&ctx->mbox->backend_boxes);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen return 0;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen}
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainenstatic int
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainenvirtual_config_parse_line(struct virtual_parse_context *ctx, const char *line,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen const char **error_r)
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen{
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen struct mail_user *user = ctx->mbox->storage->storage.user;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen struct virtual_backend_box *bbox;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen const char *name;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if (*line == ' ' || *line == '\t') {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* continues the previous search rule */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen *error_r = "Search rule without a mailbox";
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen return -1;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen }
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen str_append(ctx->rule, line);
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen return 0;
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen }
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* if there is no rule yet, it means we want the previous mailboxes
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen to use the rule that comes later */
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen if (str_len(ctx->rule) > 0) {
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen if (virtual_config_add_rule(ctx, error_r) < 0)
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen return -1;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen }
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen /* new mailbox. the search args are added to it later. */
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen if (strcasecmp(line, "INBOX") == 0)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen line = "INBOX";
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bbox->name = p_strdup(ctx->pool, line);
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen if (*line == '-') line++;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen bbox->ns = strcasecmp(line, "!INBOX") != 0 ?
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen mail_namespace_find(user->namespaces, &line) :
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen mail_namespace_find_inbox(user->namespaces);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen if (bbox->ns == NULL) {
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen *error_r = t_strdup_printf("Namespace not found for %s",
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen bbox->name);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen return -1;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen }
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen if (strchr(bbox->name, '*') != NULL ||
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen strchr(bbox->name, '%') != NULL) {
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen name = bbox->name[0] == '-' ? bbox->name + 1 : bbox->name;
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen bbox->glob = imap_match_init(ctx->pool, name, TRUE, ctx->sep);
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen ctx->have_wildcards = TRUE;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen } else if (bbox->name[0] == '!') {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen /* save messages here */
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen if (ctx->mbox->save_bbox != NULL) {
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen *error_r = "Multiple save mailboxes defined";
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen return -1;
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen }
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen bbox->name++;
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen ctx->mbox->save_bbox = bbox;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen }
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen array_append(&ctx->mbox->backend_boxes, &bbox, 1);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen return 0;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen}
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainenstatic void
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainenvirtual_mailbox_get_list_patterns(struct virtual_parse_context *ctx)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen{
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen struct virtual_mailbox *mbox = ctx->mbox;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen ARRAY_TYPE(mailbox_virtual_patterns) *dest;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen struct mailbox_virtual_pattern pattern;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen struct virtual_backend_box *const *bboxes;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen unsigned int i, count;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen memset(&pattern, 0, sizeof(pattern));
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen p_array_init(&mbox->list_include_patterns, ctx->pool, count);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen p_array_init(&mbox->list_exclude_patterns, ctx->pool, count);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen for (i = 0; i < count; i++) {
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen pattern.ns = bboxes[i]->ns;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen pattern.pattern = bboxes[i]->name;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen if (*pattern.pattern != '-')
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen dest = &mbox->list_include_patterns;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen else {
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen dest = &mbox->list_exclude_patterns;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen pattern.pattern++;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen }
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen array_append(dest, &pattern, 1);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen }
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen}
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainenstatic void
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainenseparate_wildcard_mailboxes(struct virtual_mailbox *mbox,
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen ARRAY_TYPE(virtual_backend_box) *wildcard_boxes,
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen ARRAY_TYPE(virtual_backend_box) *neg_boxes)
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen{
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen struct virtual_backend_box *const *bboxes;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen ARRAY_TYPE(virtual_backend_box) *dest;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen unsigned int i, count;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen t_array_init(wildcard_boxes, I_MIN(16, count));
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen t_array_init(neg_boxes, 4);
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen for (i = 0; i < count;) {
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen if (*bboxes[i]->name == '-')
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen dest = neg_boxes;
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen else if (bboxes[i]->glob != NULL)
f55b07916c82db8b915b28252e44ce6fb2bd3080Timo Sirainen dest = wildcard_boxes;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen else {
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen dest = NULL;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen i++;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen }
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen if (dest != NULL) {
4c9294fcb17fb12326ff5db29e2f882cf4bd7fedTimo Sirainen array_append(dest, &bboxes[i], 1);
99df8a838cd9c5257ea5a2554383a9a999191e38Pali Rohár array_delete(&mbox->backend_boxes, i, 1);
99df8a838cd9c5257ea5a2554383a9a999191e38Pali Rohár bboxes = array_get_modifiable(&mbox->backend_boxes,
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen &count);
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen }
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen }
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen}
99df8a838cd9c5257ea5a2554383a9a999191e38Pali Rohár
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainenstatic void virtual_config_copy_expanded(struct virtual_parse_context *ctx,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen struct virtual_backend_box *wbox,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen const char *name)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen{
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen struct virtual_backend_box *bbox;
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen *bbox = *wbox;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bbox->name = p_strdup(ctx->pool, name);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bbox->glob = NULL;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen bbox->wildcard = TRUE;
mail_search_args_ref(bbox->search_args);
array_append(&ctx->mbox->backend_boxes, &bbox, 1);
}
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) {
/* we match only one namespace for each pattern. */
if (boxes[i]->ns == info->ns &&
imap_match(boxes[i]->glob,
info->name) == IMAP_MATCH_YES) {
*idx_r = i;
return TRUE;
}
} else {
i_assert(boxes[i]->name[0] == '-');
if (strcmp(boxes[i]->name + 1, info->name) == 0) {
*idx_r = i;
return TRUE;
}
}
}
return FALSE;
}
static int virtual_config_expand_wildcards(struct virtual_parse_context *ctx)
{
const enum namespace_type iter_ns_types =
NAMESPACE_PRIVATE | NAMESPACE_SHARED | NAMESPACE_PUBLIC;
const enum mailbox_list_iter_flags iter_flags =
MAILBOX_LIST_ITER_VIRTUAL_NAMES |
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->name) == NULL) {
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);
}
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_user *user = mbox->storage->storage.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->box.path, "/"VIRTUAL_CONFIG_FNAME, NULL);
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
mail_storage_set_error(mbox->box.storage,
MAIL_ERROR_NOTPOSSIBLE,
"Virtual mailbox missing configuration file");
return -1;
}
mail_storage_set_critical(mbox->box.storage,
"open(%s) failed: %m", path);
return -1;
}
memset(&ctx, 0, sizeof(ctx));
ctx.sep = mail_namespaces_get_root_sep(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(mbox->box.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(mbox->box.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 && array_count(&mbox->backend_boxes) == 0) {
mail_storage_set_critical(mbox->box.storage,
"%s: No mailboxes defined", path);
ret = -1;
}
if (ret == 0)
virtual_config_search_args_dup(mbox);
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);
}
array_free(&mbox->backend_boxes);
}