virtual-config.c revision ebc7480f2d7f235f2fd58f91b52f8a19f53ca26a
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "lib.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "array.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "crc32.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "istream.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "str.h"
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila#include "imap-parser.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "imap-match.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "mail-namespace.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "mail-search-build.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "mail-search-parser.h"
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include "virtual-storage.h"
07dabfd18e418c92b4de84c133f105835779bebeTimo Sirainen#include "virtual-plugin.h"
07dabfd18e418c92b4de84c133f105835779bebeTimo Sirainen
07dabfd18e418c92b4de84c133f105835779bebeTimo Sirainen#include <unistd.h>
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen#include <fcntl.h>
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstruct virtual_parse_context {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct virtual_mailbox *mbox;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct istream *input;
60ba197d17023594231d9805d889817782e41859Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen pool_t pool;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen string_t *rule;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int rule_idx;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen char sep;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bool have_wildcards;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen};
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainenstatic struct mail_search_args *
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainenvirtual_search_args_parse(const string_t *rule, const char **error_r)
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen{
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen struct istream *input;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen struct imap_parser *imap_parser;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen const struct imap_arg *args;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen struct mail_search_parser *parser;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen struct mail_search_args *sargs;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen bool fatal;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen int ret;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen if (str_len(rule) == 0) {
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen sargs = mail_search_build_init();
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen mail_search_build_add_all(sargs);
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen return sargs;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen }
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen input = i_stream_create_from_data(str_data(rule), str_len(rule));
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen (void)i_stream_read(input);
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen imap_parser = imap_parser_create(input, NULL, (size_t)-1);
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen ret = imap_parser_finish_line(imap_parser, 0, 0, &args);
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen if (ret < 0) {
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen sargs = NULL;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = t_strdup(imap_parser_get_error(imap_parser, &fatal));
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen } else {
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen parser = mail_search_parser_init_imap(args);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (mail_search_build(mail_search_register_get_imap(),
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen parser, "UTF-8", &sargs, error_r) < 0)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen sargs = NULL;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_search_parser_deinit(&parser);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen imap_parser_destroy(&imap_parser);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen i_stream_destroy(&input);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return sargs;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen}
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstatic int
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenvirtual_config_add_rule(struct virtual_parse_context *ctx, const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen{
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct virtual_backend_box *const *bboxes;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct mail_search_args *search_args;
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen unsigned int i, count;
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen i_assert(str_len(ctx->rule) == 0);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return 0;
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen ctx->mbox->search_args_crc32 =
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen crc32_str_more(ctx->mbox->search_args_crc32, str_c(ctx->rule));
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen search_args = virtual_search_args_parse(ctx->rule, error_r);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen str_truncate(ctx->rule, 0);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (search_args == NULL) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = t_strconcat("Previous search rule is invalid: ",
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r, NULL);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return -1;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
4c61d6b6ad6173f06563c2cee9bd813c59277dd2Timo Sirainen /* update at all the mailboxes that were introduced since the previous
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen rule. */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen i_assert(ctx->rule_idx < count);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen for (i = ctx->rule_idx; i < count; i++) {
57dc9669d34de7d08a44be9d7d3c9f6a0c34cc87Timo Sirainen i_assert(bboxes[i]->search_args == NULL);
57dc9669d34de7d08a44be9d7d3c9f6a0c34cc87Timo Sirainen mail_search_args_ref(search_args);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bboxes[i]->search_args = search_args;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
57dc9669d34de7d08a44be9d7d3c9f6a0c34cc87Timo Sirainen mail_search_args_unref(&search_args);
57dc9669d34de7d08a44be9d7d3c9f6a0c34cc87Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen ctx->rule_idx = array_count(&ctx->mbox->backend_boxes);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return 0;
07dabfd18e418c92b4de84c133f105835779bebeTimo Sirainen}
07dabfd18e418c92b4de84c133f105835779bebeTimo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstatic int
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenvirtual_config_parse_line(struct virtual_parse_context *ctx, const char *line,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen{
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct mail_user *user = ctx->mbox->storage->storage.user;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct virtual_backend_box *bbox;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen const char *name;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (*line == ' ' || *line == '\t') {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* continues the previous search rule */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = "Search rule without a mailbox";
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return -1;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
4c61d6b6ad6173f06563c2cee9bd813c59277dd2Timo Sirainen while (*line == ' ' || *line == '\t') line++;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen str_append_c(ctx->rule, ' ');
4c61d6b6ad6173f06563c2cee9bd813c59277dd2Timo Sirainen str_append(ctx->rule, line);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return 0;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
4c61d6b6ad6173f06563c2cee9bd813c59277dd2Timo Sirainen /* if there is no rule yet, it means we want the previous mailboxes
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen to use the rule that comes later */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (str_len(ctx->rule) > 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (virtual_config_add_rule(ctx, error_r) < 0)
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen return -1;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
fac865bad1ba10e85d80b63dedfd3493a65510d4Timo Sirainen /* new mailbox. the search args are added to it later. */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (strcasecmp(line, "INBOX") == 0)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen line = "INBOX";
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bbox->name = p_strdup(ctx->pool, line);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (*line == '-' || *line == '!') line++;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bbox->ns = strcasecmp(line, "INBOX") == 0 ?
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_namespace_find_inbox(user->namespaces) :
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_namespace_find(user->namespaces, &line);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (bbox->ns == NULL) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = t_strdup_printf("Namespace not found for %s",
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bbox->name);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return -1;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (strchr(bbox->name, '*') != NULL ||
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen strchr(bbox->name, '%') != NULL) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen name = bbox->name[0] == '-' ? bbox->name + 1 : bbox->name;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila bbox->glob = imap_match_init(ctx->pool, name, TRUE, ctx->sep);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila ctx->have_wildcards = TRUE;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila } else if (bbox->name[0] == '!') {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* save messages here */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (ctx->mbox->save_bbox != NULL) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila *error_r = "Multiple save mailboxes defined";
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila return -1;
957b0b4c9aeff7153bb9ebf91d8aea550bd07865Teemu Huovila }
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen bbox->name++;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila ctx->mbox->save_bbox = bbox;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila }
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila array_append(&ctx->mbox->backend_boxes, &bbox, 1);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila return 0;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila}
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila
07dabfd18e418c92b4de84c133f105835779bebeTimo Sirainenstatic void
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilavirtual_mailbox_get_list_patterns(struct virtual_parse_context *ctx)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila{
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct virtual_mailbox *mbox = ctx->mbox;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila ARRAY_TYPE(mailbox_virtual_patterns) *dest;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct mailbox_virtual_pattern pattern;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct virtual_backend_box *const *bboxes;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i, count;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila memset(&pattern, 0, sizeof(pattern));
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila p_array_init(&mbox->list_include_patterns, ctx->pool, count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila p_array_init(&mbox->list_exclude_patterns, ctx->pool, count);
957b0b4c9aeff7153bb9ebf91d8aea550bd07865Teemu Huovila for (i = 0; i < count; i++) {
957b0b4c9aeff7153bb9ebf91d8aea550bd07865Teemu Huovila pattern.ns = bboxes[i]->ns;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila pattern.pattern = bboxes[i]->name;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (*pattern.pattern != '-')
0c827d2094e80ede4c089fc00260d7ffcc764636Timo Sirainen dest = &mbox->list_include_patterns;
0c827d2094e80ede4c089fc00260d7ffcc764636Timo Sirainen else {
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen dest = &mbox->list_exclude_patterns;
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen pattern.pattern++;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila }
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen array_append(dest, &pattern, 1);
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen }
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen}
fac865bad1ba10e85d80b63dedfd3493a65510d4Timo Sirainen
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilastatic void
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilaseparate_wildcard_mailboxes(struct virtual_mailbox *mbox,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila ARRAY_TYPE(virtual_backend_box) *wildcard_boxes,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila ARRAY_TYPE(virtual_backend_box) *neg_boxes)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila{
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct virtual_backend_box *const *bboxes;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila ARRAY_TYPE(virtual_backend_box) *dest;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i, count;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila t_array_init(wildcard_boxes, I_MIN(16, count));
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila t_array_init(neg_boxes, 4);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila for (i = 0; i < count;) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (*bboxes[i]->name == '-')
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila dest = neg_boxes;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila else if (bboxes[i]->glob != NULL)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila dest = wildcard_boxes;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila else {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila dest = NULL;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila i++;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila }
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (dest != NULL) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila array_append(dest, &bboxes[i], 1);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila array_delete(&mbox->backend_boxes, i, 1);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila bboxes = array_get_modifiable(&mbox->backend_boxes,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila &count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila }
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila }
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila}
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovilastatic void virtual_config_copy_expanded(struct virtual_parse_context *ctx,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila struct virtual_backend_box *wbox,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen const char *name)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen{
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct virtual_backend_box *bbox;
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *bbox = *wbox;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bbox->name = p_strdup(ctx->pool, name);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bbox->glob = NULL;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bbox->wildcard = TRUE;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_search_args_ref(bbox->search_args);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen array_append(&ctx->mbox->backend_boxes, &bbox, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen}
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilastatic bool virtual_config_match(const struct mailbox_info *info,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila ARRAY_TYPE(virtual_backend_box) *boxes_arr,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int *idx_r)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila{
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct virtual_backend_box *const *boxes;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i, count;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila boxes = array_get_modifiable(boxes_arr, &count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila for (i = 0; i < count; i++) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (boxes[i]->glob != NULL) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* we match only one namespace for each pattern. */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (boxes[i]->ns == info->ns &&
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila imap_match(boxes[i]->glob,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila info->name) == IMAP_MATCH_YES) {
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila *idx_r = i;
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila return TRUE;
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen } else {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen i_assert(boxes[i]->name[0] == '-');
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila if (strcmp(boxes[i]->name + 1, info->name) == 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *idx_r = i;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return TRUE;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return FALSE;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen}
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstatic int virtual_config_expand_wildcards(struct virtual_parse_context *ctx)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen{
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen const enum namespace_type iter_ns_types =
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila NAMESPACE_PRIVATE | NAMESPACE_SHARED | NAMESPACE_PUBLIC;
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila const enum mailbox_list_iter_flags iter_flags =
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila MAILBOX_LIST_ITER_RETURN_NO_FLAGS;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct mail_user *user = ctx->mbox->storage->storage.user;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen ARRAY_TYPE(virtual_backend_box) wildcard_boxes, neg_boxes;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct mailbox_list_iterate_context *iter;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct virtual_backend_box *const *wboxes;
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila const char **patterns;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen const struct mailbox_info *info;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int i, j, count;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen separate_wildcard_mailboxes(ctx->mbox, &wildcard_boxes, &neg_boxes);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* get patterns we want to list */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen wboxes = array_get_modifiable(&wildcard_boxes, &count);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (count == 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* only negative wildcards - doesn't really make sense.
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen just ignore. */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen return 0;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen patterns = t_new(const char *, count + 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen for (i = 0; i < count; i++)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen patterns[i] = wboxes[i]->name;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* match listed mailboxes to wildcards */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen iter = mailbox_list_iter_init_namespaces(user->namespaces, patterns,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen iter_ns_types, iter_flags);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen while ((info = mailbox_list_iter_next(iter)) != NULL) {
60ba197d17023594231d9805d889817782e41859Timo Sirainen /* skip non-selectable mailboxes (especially mbox
60ba197d17023594231d9805d889817782e41859Timo Sirainen directories) */
60ba197d17023594231d9805d889817782e41859Timo Sirainen if ((info->flags & MAILBOX_NOSELECT) != 0)
60ba197d17023594231d9805d889817782e41859Timo Sirainen continue;
60ba197d17023594231d9805d889817782e41859Timo Sirainen
60ba197d17023594231d9805d889817782e41859Timo Sirainen if (virtual_config_match(info, &wildcard_boxes, &i) &&
60ba197d17023594231d9805d889817782e41859Timo Sirainen !virtual_config_match(info, &neg_boxes, &j) &&
60ba197d17023594231d9805d889817782e41859Timo Sirainen virtual_backend_box_lookup_name(ctx->mbox,
60ba197d17023594231d9805d889817782e41859Timo Sirainen info->name) == NULL) {
60ba197d17023594231d9805d889817782e41859Timo Sirainen virtual_config_copy_expanded(ctx, wboxes[i],
60ba197d17023594231d9805d889817782e41859Timo Sirainen info->name);
60ba197d17023594231d9805d889817782e41859Timo Sirainen }
60ba197d17023594231d9805d889817782e41859Timo Sirainen }
60ba197d17023594231d9805d889817782e41859Timo Sirainen for (i = 0; i < count; i++)
60ba197d17023594231d9805d889817782e41859Timo Sirainen mail_search_args_unref(&wboxes[i]->search_args);
60ba197d17023594231d9805d889817782e41859Timo Sirainen return mailbox_list_iter_deinit(&iter);
60ba197d17023594231d9805d889817782e41859Timo Sirainen}
60ba197d17023594231d9805d889817782e41859Timo Sirainen
60ba197d17023594231d9805d889817782e41859Timo Sirainenstatic void virtual_config_search_args_dup(struct virtual_mailbox *mbox)
60ba197d17023594231d9805d889817782e41859Timo Sirainen{
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct virtual_backend_box *const *bboxes;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct mail_search_args *old_args;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int i, count;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen for (i = 0; i < count; i++) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen old_args = bboxes[i]->search_args;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen bboxes[i]->search_args = mail_search_args_dup(old_args);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_search_args_unref(&old_args);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen }
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen}
60ba197d17023594231d9805d889817782e41859Timo Sirainen
60ba197d17023594231d9805d889817782e41859Timo Sirainenint virtual_config_read(struct virtual_mailbox *mbox)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila{
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct mail_storage *storage = mbox->box.storage;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct virtual_parse_context ctx;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct stat st;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila const char *path, *line, *error;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int linenum = 0;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen int fd, ret = 0;
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen i_array_init(&mbox->backend_boxes, 8);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mbox->search_args_crc32 = (uint32_t)-1;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen path = t_strconcat(mbox->box.path, "/"VIRTUAL_CONFIG_FNAME, NULL);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen fd = open(path, O_RDONLY);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (fd == -1) {
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen if (errno == EACCES) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_storage_set_critical(storage, "%s",
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen mail_error_eacces_msg("stat", mbox->box.path));
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen } else if (errno != ENOENT) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila mail_storage_set_critical(storage,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila "open(%s) failed: %m", path);
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila } else if (stat(mbox->box.path, &st) == 0) {
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila mail_storage_set_error(storage, MAIL_ERROR_NOTPOSSIBLE,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila "Virtual mailbox missing configuration file");
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila } else if (errno == ENOENT) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_storage_set_error(storage, MAIL_ERROR_NOTFOUND,
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen T_MAIL_ERR_MAILBOX_NOT_FOUND(mbox->box.name));
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen } else {
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen mail_storage_set_critical(storage,
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen "stat(%s) failed: %m", mbox->box.path);
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen }
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen return -1;
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen }
afbd17685101cc2bbd0614e85558bb167ab15096Timo Sirainen
afbd17685101cc2bbd0614e85558bb167ab15096Timo Sirainen memset(&ctx, 0, sizeof(ctx));
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen 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 && array_count(&mbox->backend_boxes) == 0) {
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);
(void)close(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);
}