virtual-config.c revision 075a53973bbdf15cc3bd2ba4872f96f3f2f00574
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "lib.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "array.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "crc32.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "istream.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "str.h"
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen#include "imap-parser.h"
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen#include "imap-match.h"
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen#include "mail-search-build.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "virtual-storage.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "virtual-plugin.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include <unistd.h>
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include <fcntl.h>
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainenstruct virtual_parse_context {
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen struct virtual_mailbox *mbox;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct istream *input;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen pool_t pool;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen string_t *rule;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen unsigned int rule_idx;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen char sep;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen bool have_wildcards;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen};
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomistatic struct mail_search_args *
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomivirtual_search_args_parse(const string_t *rule, const char **error_r)
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen{
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi struct istream *input;
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi struct imap_parser *parser;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen const struct imap_arg *args;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct mail_search_args *sargs;
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen bool fatal;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen int ret;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen input = i_stream_create_from_data(str_data(rule), str_len(rule));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen (void)i_stream_read(input);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen parser = imap_parser_create(input, NULL, (size_t)-1);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen ret = imap_parser_finish_line(parser, 0, 0, &args);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (ret < 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen sargs = NULL;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen *error_r = t_strdup(imap_parser_get_error(parser, &fatal));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen } else if (mail_search_build_from_imap_args(args, "UTF-8", &sargs,
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen error_r) < 0)
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen sargs = NULL;
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen imap_parser_destroy(&parser);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_stream_destroy(&input);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return sargs;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen}
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic int
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenvirtual_config_add_rule(struct virtual_parse_context *ctx, const char **error_r)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct virtual_backend_box *const *bboxes;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct mail_search_args *search_args;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen unsigned int i, count;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_assert(str_len(ctx->rule) == 0);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return 0;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen ctx->mbox->search_args_crc32 =
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen crc32_str_more(ctx->mbox->search_args_crc32, str_c(ctx->rule));
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen search_args = virtual_search_args_parse(ctx->rule, error_r);
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen str_truncate(ctx->rule, 0);
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi if (search_args == NULL) {
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen *error_r = t_strconcat("Previous search rule is invalid: ",
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi *error_r, NULL);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen return -1;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen }
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen /* update at all the mailboxes that were introduced since the previous
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen rule. */
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen bboxes = array_get(&ctx->mbox->backend_boxes, &count);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen i_assert(ctx->rule_idx < count);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen for (i = ctx->rule_idx; i < count; i++) {
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen i_assert(bboxes[i]->search_args == NULL);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen mail_search_args_ref(search_args);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen bboxes[i]->search_args = search_args;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen }
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen mail_search_args_unref(&search_args);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen ctx->rule_idx = array_count(&ctx->mbox->backend_boxes);
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi return 0;
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi}
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic int
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenvirtual_config_parse_line(struct virtual_parse_context *ctx, const char *line,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen const char **error_r)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen{
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct mail_user *user = ctx->mbox->storage->storage.ns->user;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct virtual_backend_box *bbox;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen const char *name;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen if (*line == ' ' || *line == '\t') {
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen /* continues the previous search rule */
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen if (ctx->rule_idx == array_count(&ctx->mbox->backend_boxes)) {
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen *error_r = "Search rule without a mailbox";
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen return -1;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen }
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen str_append(ctx->rule, line);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen return 0;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen }
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen /* if there is no rule yet, it means we want the previous mailboxes
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen to use the rule that comes later */
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen if (str_len(ctx->rule) > 0) {
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen if (virtual_config_add_rule(ctx, error_r) < 0)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen return -1;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen /* new mailbox. the search args are added to it later. */
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (strcasecmp(line, "INBOX") == 0)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen line = "INBOX";
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen bbox->name = p_strdup(ctx->pool, line);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (*line == '-') line++;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen bbox->ns = strcasecmp(line, "!INBOX") != 0 ?
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen mail_namespace_find(user->namespaces, &line) :
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen mail_namespace_find_inbox(user->namespaces);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (bbox->ns == NULL) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen *error_r = t_strdup_printf("Namespace not found for %s",
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen bbox->name);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return -1;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (strchr(bbox->name, '*') != NULL ||
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen strchr(bbox->name, '%') != NULL) {
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen name = bbox->name[0] == '-' ? bbox->name + 1 : bbox->name;
791fb70b3255a11a91ce0c2dc3ae1460d4cf8459Timo Sirainen bbox->glob = imap_match_init(ctx->pool, name, TRUE, ctx->sep);
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen ctx->have_wildcards = TRUE;
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen } else if (bbox->name[0] == '!') {
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen /* save messages here */
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen if (ctx->mbox->save_bbox != NULL) {
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen *error_r = "Multiple save mailboxes defined";
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen return -1;
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen }
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen bbox->name++;
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen ctx->mbox->save_bbox = bbox;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen }
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen array_append(&ctx->mbox->backend_boxes, &bbox, 1);
202b4674243a4a4826c35ed4d089831985c47256Timo Sirainen return 0;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen}
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainenstatic void
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainenvirtual_mailbox_get_list_patterns(struct virtual_parse_context *ctx)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen{
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct virtual_mailbox *mbox = ctx->mbox;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen ARRAY_TYPE(mailbox_virtual_patterns) *dest;
95f5b08fa73ddd9a9de40a97aa141e9c74e0645eAki Tuomi struct mailbox_virtual_pattern pattern;
95f5b08fa73ddd9a9de40a97aa141e9c74e0645eAki Tuomi struct virtual_backend_box *const *bboxes;
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi unsigned int i, count;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen memset(&pattern, 0, sizeof(pattern));
95f5b08fa73ddd9a9de40a97aa141e9c74e0645eAki Tuomi bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen p_array_init(&mbox->list_include_patterns, ctx->pool, count);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen p_array_init(&mbox->list_exclude_patterns, ctx->pool, count);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen for (i = 0; i < count; i++) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen pattern.ns = bboxes[i]->ns;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen pattern.pattern = bboxes[i]->name;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (*pattern.pattern != '-')
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen dest = &mbox->list_include_patterns;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen else {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen dest = &mbox->list_exclude_patterns;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen pattern.pattern++;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen }
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen array_append(dest, &pattern, 1);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen }
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen}
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainenstatic void
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainenseparate_wildcard_mailboxes(struct virtual_mailbox *mbox,
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen ARRAY_TYPE(virtual_backend_box) *wildcard_boxes,
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen ARRAY_TYPE(virtual_backend_box) *neg_boxes)
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen{
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen struct virtual_backend_box *const *bboxes;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen ARRAY_TYPE(virtual_backend_box) *dest;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen unsigned int i, count;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen t_array_init(wildcard_boxes, I_MIN(16, count));
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen t_array_init(neg_boxes, 4);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen for (i = 0; i < count;) {
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen if (*bboxes[i]->name == '-')
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen dest = neg_boxes;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen else if (bboxes[i]->glob != NULL)
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen dest = wildcard_boxes;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen else {
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen dest = NULL;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen i++;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen }
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen if (dest != NULL) {
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen array_append(dest, &bboxes[i], 1);
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen array_delete(&mbox->backend_boxes, i, 1);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes,
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen &count);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen }
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen }
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen}
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainenstatic void virtual_config_copy_expanded(struct virtual_parse_context *ctx,
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen struct virtual_backend_box *wbox,
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen const char *name)
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen{
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen struct virtual_backend_box *bbox;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen bbox = p_new(ctx->pool, struct virtual_backend_box, 1);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen *bbox = *wbox;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen bbox->name = p_strdup(ctx->pool, name);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen bbox->glob = NULL;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen mail_search_args_ref(bbox->search_args);
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen array_append(&ctx->mbox->backend_boxes, &bbox, 1);
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen}
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainenstatic bool virtual_config_match(const struct mailbox_info *info,
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen ARRAY_TYPE(virtual_backend_box) *boxes_arr,
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen unsigned int *idx_r)
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen{
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen struct virtual_backend_box *const *boxes;
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen unsigned int i, count;
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen boxes = array_get_modifiable(boxes_arr, &count);
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen for (i = 0; i < count; i++) {
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen if (boxes[i]->glob != NULL) {
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen /* we match only one namespace for each pattern. */
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen if (boxes[i]->ns == info->ns &&
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen imap_match(boxes[i]->glob,
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen info->name) == IMAP_MATCH_YES) {
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen *idx_r = i;
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen return TRUE;
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen }
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen } else {
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen i_assert(boxes[i]->name[0] == '-');
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen if (strcmp(boxes[i]->name + 1, info->name) == 0) {
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen *idx_r = i;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen return TRUE;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen }
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen }
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen }
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen return FALSE;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen}
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainenstatic int virtual_config_expand_wildcards(struct virtual_parse_context *ctx)
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen{
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen struct mail_user *user = ctx->mbox->storage->storage.ns->user;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen ARRAY_TYPE(virtual_backend_box) wildcard_boxes, neg_boxes;
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi struct mailbox_list_iterate_context *iter;
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi struct virtual_backend_box *const *wboxes;
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi const char **patterns;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen const struct mailbox_info *info;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen unsigned int i, j, count;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen separate_wildcard_mailboxes(ctx->mbox, &wildcard_boxes, &neg_boxes);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen /* get patterns we want to list */
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen wboxes = array_get_modifiable(&wildcard_boxes, &count);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen if (count == 0) {
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen /* only negative wildcards - doesn't really make sense.
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen just ignore. */
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen return 0;
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen }
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen patterns = t_new(const char *, count + 1);
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen for (i = 0; i < count; i++)
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi patterns[i] = wboxes[i]->name;
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen /* match listed mailboxes to wildcards */
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen iter = mailbox_list_iter_init_namespaces(user->namespaces, patterns,
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen MAILBOX_LIST_ITER_VIRTUAL_NAMES |
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen while ((info = mailbox_list_iter_next(iter)) != NULL) {
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen /* skip non-selectable mailboxes (especially mbox
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi directories) */
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen if ((info->flags & MAILBOX_NOSELECT) != 0)
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen continue;
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen if (virtual_config_match(info, &wildcard_boxes, &i) &&
32bd32dcc845cd0c00d5617aea1ffbe45522b413Aki Tuomi !virtual_config_match(info, &neg_boxes, &j) &&
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen virtual_backend_box_lookup_name(ctx->mbox,
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen info->name) == NULL) {
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainen virtual_config_copy_expanded(ctx, wboxes[i],
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen info->name);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen }
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen for (i = 0; i < count; i++)
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen mail_search_args_unref(&wboxes[i]->search_args);
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen return mailbox_list_iter_deinit(&iter);
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen}
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen
bb869cc24b24a8df84a43154c628785d6aee784cTimo Sirainenint virtual_config_read(struct virtual_mailbox *mbox)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
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_RDONLY);
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);
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->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) {
mail_storage_set_critical(mbox->ibox.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->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);
}
}