doveadm-mail.c revision ad58b50aef8125981ebdbc89513236558bcccf60
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2009-2011 Dovecot authors, see the included COPYING file */
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "lib.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "array.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "lib-signals.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "ioloop.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "str.h"
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen#include "unichar.h"
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen#include "module-dir.h"
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen#include "wildcard-match.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "master-service.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "mail-user.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "mail-namespace.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "mail-storage.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "mail-storage-settings.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "mail-storage-service.h"
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen#include "mail-search-build.h"
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen#include "mail-search-parser.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "doveadm.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "doveadm-settings.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "doveadm-print.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "dsync/doveadm-dsync.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "doveadm-mail.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include <stdio.h>
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include <stdlib.h>
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo SirainenARRAY_TYPE(doveadm_mail_cmd) doveadm_mail_cmds;
2d6c6f1d06d6d2e7b2ab0d46f7decdcf1cc80cd6Timo Sirainenvoid (*hook_doveadm_mail_init)(struct doveadm_mail_cmd_context *ctx);
2d6c6f1d06d6d2e7b2ab0d46f7decdcf1cc80cd6Timo Sirainenstruct doveadm_mail_cmd_module_register
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen doveadm_mail_cmd_module_register = { 0 };
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainenchar doveadm_mail_cmd_hide = '\0';
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic int killed_signo = 0;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstruct doveadm_mail_cmd_context *
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainendoveadm_mail_cmd_alloc_size(size_t size)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct doveadm_mail_cmd_context *ctx;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen pool_t pool;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_assert(size >= sizeof(struct doveadm_mail_cmd_context));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen pool = pool_alloconly_create("doveadm mail cmd", 1024);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen ctx = p_malloc(pool, size);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen ctx->pool = pool;
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen return ctx;
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen}
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainenstatic void
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainencmd_purge_run(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct mail_user *user)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct mail_namespace *ns;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (ns->type != NAMESPACE_PRIVATE || ns->alias_for != NULL)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen continue;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (mail_storage_purge(ns->storage) < 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_error("Purging namespace '%s' failed: %s", ns->prefix,
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen mail_storage_get_last_error(ns->storage, NULL));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen}
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_purge_alloc(void)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen struct doveadm_mail_cmd_context *ctx;
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen ctx->v.run = cmd_purge_run;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen return ctx;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen}
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainenstruct mailbox *
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainendoveadm_mailbox_find(struct mail_user *user, const char *mailbox)
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen{
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen struct mail_namespace *ns;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen if (!uni_utf8_str_is_valid(mailbox))
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen i_fatal("Mailbox name not valid UTF-8: %s", mailbox);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen ns = mail_namespace_find(user->namespaces, mailbox);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen if (ns == NULL)
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen i_fatal("Can't find namespace for mailbox %s", mailbox);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen return mailbox_alloc(ns->list, mailbox, MAILBOX_FLAG_IGNORE_ACLS);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen}
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic int
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainendoveadm_mailbox_find_and_open(struct mail_user *user, const char *mailbox,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct mailbox **box_r)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen{
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct mailbox *box;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen box = doveadm_mailbox_find(user, mailbox);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen if (mailbox_open(box) < 0) {
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen i_error("Opening mailbox %s failed: %s", mailbox,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen mailbox_get_last_error(box, NULL));
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen mailbox_free(&box);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen return -1;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen }
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen *box_r = box;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen return 0;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen}
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainenint doveadm_mailbox_find_and_sync(struct mail_user *user, const char *mailbox,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct mailbox **box_r)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen{
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen if (doveadm_mailbox_find_and_open(user, mailbox, box_r) < 0)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return -1;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (mailbox_sync(*box_r, MAILBOX_SYNC_FLAG_FULL_READ) < 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_error("Syncing mailbox %s failed: %s", mailbox,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen mailbox_get_last_error(*box_r, NULL));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen mailbox_free(box_r);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen mailbox_free(box_r);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return -1;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return 0;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen}
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstruct mail_search_args *
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainendoveadm_mail_build_search_args(const char *const args[])
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct mail_search_parser *parser;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct mail_search_args *sargs;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen const char *error, *charset = "UTF-8";
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen parser = mail_search_parser_init_cmdline(args);
791fb70b3255a11a91ce0c2dc3ae1460d4cf8459Timo Sirainen if (mail_search_build(mail_search_register_get_human(),
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen parser, &charset, &sargs, &error) < 0)
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen i_fatal("%s", error);
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen mail_search_parser_deinit(&parser);
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen return sargs;
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen}
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainen
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainenstruct force_resync_cmd_context {
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct doveadm_mail_cmd_context ctx;
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen const char *mailbox;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen};
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainenstatic void cmd_force_resync_run(struct doveadm_mail_cmd_context *_ctx,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct mail_user *user)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen{
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct force_resync_cmd_context *ctx =
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen (struct force_resync_cmd_context *)_ctx;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct mailbox *box;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (doveadm_mailbox_find_and_open(user, ctx->mailbox, &box) < 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen _ctx->failed = TRUE;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FORCE_RESYNC |
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen MAILBOX_SYNC_FLAG_FIX_INCONSISTENT) < 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_error("Forcing a resync on mailbox %s failed: %s",
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen ctx->mailbox, mailbox_get_last_error(box, NULL));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen _ctx->failed = TRUE;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen mailbox_free(&box);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen}
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic void cmd_force_resync_init(struct doveadm_mail_cmd_context *_ctx,
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen const char *const args[])
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen struct force_resync_cmd_context *ctx =
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen (struct force_resync_cmd_context *)_ctx;
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen const char *mailbox = args[0];
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen if (mailbox == NULL || args[1] != NULL)
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen doveadm_mail_help_name("force-resync");
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen ctx->mailbox = p_strdup(ctx->ctx.pool, mailbox);
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen}
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_force_resync_alloc(void)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen struct force_resync_cmd_context *ctx;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
10972f2a15f5538860fcc1d4adda227d59d2d757Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct force_resync_cmd_context);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen ctx->ctx.v.init = cmd_force_resync_init;
ctx->ctx.v.run = cmd_force_resync_run;
return &ctx->ctx;
}
static int
doveadm_mail_next_user(struct doveadm_mail_cmd_context *ctx,
const struct mail_storage_service_input *input,
const char **error_r)
{
const char *error;
int ret;
i_set_failure_prefix(t_strdup_printf("doveadm(%s): ", input->username));
/* see if we want to execute this command via (another)
doveadm server */
ret = doveadm_mail_server_user(ctx, input, error_r);
if (ret != 0)
return ret;
ret = mail_storage_service_lookup(ctx->storage_service, input,
&ctx->cur_service_user, &error);
if (ret <= 0) {
if (ret < 0) {
*error_r = t_strdup_printf("User lookup failed: %s",
error);
}
return ret;
}
ret = mail_storage_service_next(ctx->storage_service,
ctx->cur_service_user,
&ctx->cur_mail_user);
if (ret < 0) {
*error_r = "User init failed";
mail_storage_service_user_free(&ctx->cur_service_user);
return ret;
}
ctx->v.run(ctx, ctx->cur_mail_user);
mail_user_unref(&ctx->cur_mail_user);
mail_storage_service_user_free(&ctx->cur_service_user);
return 1;
}
void doveadm_mail_single_user(struct doveadm_mail_cmd_context *ctx,
const struct mail_storage_service_input *input)
{
const char *error;
int ret;
i_assert(input->username != NULL);
ctx->cur_username = input->username;
ctx->storage_service = mail_storage_service_init(master_service, NULL,
ctx->service_flags);
ctx->v.init(ctx, ctx->args);
if (hook_doveadm_mail_init != NULL)
hook_doveadm_mail_init(ctx);
ret = doveadm_mail_next_user(ctx, input, &error);
if (ret < 0)
i_fatal("%s", error);
else if (ret == 0)
i_fatal("User doesn't exist");
}
static void sig_die(const siginfo_t *si, void *context ATTR_UNUSED)
{
killed_signo = si->si_signo;
}
static void
doveadm_mail_all_users(struct doveadm_mail_cmd_context *ctx, char *argv[],
const char *wildcard_user)
{
struct mail_storage_service_input input;
unsigned int user_idx, user_count, interval, n;
const char *user, *error;
int ret;
ctx->service_flags |= MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
memset(&input, 0, sizeof(input));
input.service = "doveadm";
ctx->storage_service = mail_storage_service_init(master_service, NULL,
ctx->service_flags);
lib_signals_set_handler(SIGINT, 0, sig_die, NULL);
lib_signals_set_handler(SIGTERM, 0, sig_die, NULL);
ctx->v.init(ctx, (const void *)argv);
if (hook_doveadm_mail_init != NULL)
hook_doveadm_mail_init(ctx);
user_count = mail_storage_service_all_init(ctx->storage_service);
n = user_count / 10000;
for (interval = 10; n > 0 && interval < 1000; interval *= 10)
n /= 10;
user_idx = 0;
while ((ret = ctx->v.get_next_user(ctx, &user)) > 0) {
if (wildcard_user != NULL) {
if (!wildcard_match_icase(user, wildcard_user))
continue;
}
input.username = user;
doveadm_print_sticky("username", user);
T_BEGIN {
ret = doveadm_mail_next_user(ctx, &input, &error);
if (ret < 0)
i_error("%s", error);
else if (ret == 0)
i_info("User no longer exists, skipping");
} T_END;
if (ret == -1)
break;
if (doveadm_verbose) {
if (++user_idx % interval == 0) {
printf("\r%d / %d", user_idx, user_count);
fflush(stdout);
}
}
if (killed_signo != 0) {
i_warning("Killed with signal %d", killed_signo);
ret = -1;
break;
}
}
if (doveadm_verbose)
printf("\n");
i_set_failure_prefix("doveadm: ");
if (ret < 0)
i_error("Failed to iterate through some users");
}
static void
doveadm_mail_cmd_init_noop(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
const char *const args[] ATTR_UNUSED)
{
}
static int
doveadm_mail_cmd_get_next_user(struct doveadm_mail_cmd_context *ctx,
const char **username_r)
{
return mail_storage_service_all_next(ctx->storage_service, username_r);
}
static void
doveadm_mail_cmd_deinit_noop(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED)
{
}
struct doveadm_mail_cmd_context *
doveadm_mail_cmd_init(const struct doveadm_mail_cmd *cmd,
const struct doveadm_settings *set)
{
struct doveadm_mail_cmd_context *ctx;
ctx = cmd->alloc();
ctx->set = set;
ctx->cmd = cmd;
if (ctx->v.init == NULL)
ctx->v.init = doveadm_mail_cmd_init_noop;
if (ctx->v.get_next_user == NULL)
ctx->v.get_next_user = doveadm_mail_cmd_get_next_user;
if (ctx->v.deinit == NULL)
ctx->v.deinit = doveadm_mail_cmd_deinit_noop;
p_array_init(&ctx->module_contexts, ctx->pool, 5);
return ctx;
}
static void
doveadm_mail_cmd(const struct doveadm_mail_cmd *cmd, int argc, char *argv[])
{
struct doveadm_mail_cmd_context *ctx;
const char *getopt_args, *wildcard_user;
int c;
ctx = doveadm_mail_cmd_init(cmd, doveadm_settings);
ctx->full_args = (const void *)(argv + 1);
ctx->service_flags |= MAIL_STORAGE_SERVICE_FLAG_NO_LOG_INIT;
if (doveadm_debug)
ctx->service_flags |= MAIL_STORAGE_SERVICE_FLAG_DEBUG;
getopt_args = t_strconcat("AS:u:", ctx->getopt_args, NULL);
ctx->cur_username = getenv("USER");
wildcard_user = NULL;
while ((c = getopt(argc, argv, getopt_args)) > 0) {
switch (c) {
case 'A':
ctx->iterate_all_users = TRUE;
break;
case 'S':
doveadm_settings->doveadm_socket_path = optarg;
if (doveadm_settings->doveadm_worker_count == 0)
doveadm_settings->doveadm_worker_count = 1;
break;
case 'u':
ctx->service_flags |=
MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
ctx->cur_username = optarg;
if (strchr(ctx->cur_username, '*') != NULL ||
strchr(ctx->cur_username, '?') != NULL) {
wildcard_user = ctx->cur_username;
ctx->cur_username = NULL;
}
break;
default:
if (ctx->v.parse_arg == NULL ||
!ctx->v.parse_arg(ctx, c))
doveadm_mail_help(cmd);
}
}
argv += optind;
if (argv[0] != NULL && cmd->usage_args == NULL) {
i_fatal("doveadm %s: Unknown parameter: %s",
cmd->name, argv[0]);
}
ctx->args = (const void *)argv;
if (ctx->v.preinit != NULL)
ctx->v.preinit(ctx);
ctx->iterate_single_user =
!ctx->iterate_all_users && wildcard_user == NULL;
if (doveadm_print_is_initialized() && !ctx->iterate_single_user) {
doveadm_print_header("username", "Username",
DOVEADM_PRINT_HEADER_FLAG_STICKY |
DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
}
if (ctx->iterate_single_user) {
struct mail_storage_service_input input;
if (ctx->cur_username == NULL)
i_fatal("USER environment is missing and -u option not used");
memset(&input, 0, sizeof(input));
input.service = "doveadm";
input.username = ctx->cur_username;
doveadm_mail_single_user(ctx, &input);
} else {
ctx->service_flags |= MAIL_STORAGE_SERVICE_FLAG_TEMP_PRIV_DROP;
doveadm_mail_all_users(ctx, argv, wildcard_user);
}
if (ctx->search_args != NULL)
mail_search_args_unref(&ctx->search_args);
doveadm_mail_server_flush();
ctx->v.deinit(ctx);
doveadm_print_flush();
/* service deinit unloads mail plugins, so do it late */
mail_storage_service_deinit(&ctx->storage_service);
if (ctx->exit_code != 0)
exit(ctx->exit_code);
if (ctx->failed)
exit(FATAL_DEFAULT);
pool_unref(&ctx->pool);
}
static bool
doveadm_mail_try_run_multi_word(const struct doveadm_mail_cmd *cmd,
const char *cmdname, int argc, char *argv[])
{
unsigned int len;
if (argc < 2)
return FALSE;
len = strlen(argv[1]);
if (strncmp(cmdname, argv[1], len) != 0)
return FALSE;
if (cmdname[len] == ' ') {
/* more args */
return doveadm_mail_try_run_multi_word(cmd, cmdname + len + 1,
argc - 1, argv + 1);
}
if (cmdname[len] != '\0')
return FALSE;
/* match */
doveadm_mail_cmd(cmd, argc - 1, argv + 1);
return TRUE;
}
bool doveadm_mail_try_run(const char *cmd_name, int argc, char *argv[])
{
const struct doveadm_mail_cmd *cmd;
unsigned int cmd_name_len;
i_assert(argc > 0);
cmd_name_len = strlen(cmd_name);
array_foreach(&doveadm_mail_cmds, cmd) {
if (strcmp(cmd->name, cmd_name) == 0) {
doveadm_mail_cmd(cmd, argc, argv);
return TRUE;
}
/* see if it matches a multi-word command */
if (strncmp(cmd->name, cmd_name, cmd_name_len) == 0 &&
cmd->name[cmd_name_len] == ' ') {
const char *subcmd = cmd->name + cmd_name_len + 1;
if (doveadm_mail_try_run_multi_word(cmd, subcmd,
argc, argv))
return TRUE;
}
}
return FALSE;
}
void doveadm_mail_register_cmd(const struct doveadm_mail_cmd *cmd)
{
/* for now we'll just assume that cmd will be permanently in memory */
array_append(&doveadm_mail_cmds, cmd, 1);
}
const struct doveadm_mail_cmd *doveadm_mail_cmd_find(const char *cmd_name)
{
const struct doveadm_mail_cmd *cmd;
array_foreach(&doveadm_mail_cmds, cmd) {
if (strcmp(cmd->name, cmd_name) == 0)
return cmd;
}
return NULL;
}
void doveadm_mail_usage(string_t *out)
{
const struct doveadm_mail_cmd *cmd;
array_foreach(&doveadm_mail_cmds, cmd) {
if (cmd->usage_args == &doveadm_mail_cmd_hide)
continue;
str_printfa(out, "%s\t[-u <user>|-A] [-S <socket_path>]",
cmd->name);
if (cmd->usage_args != NULL)
str_printfa(out, " %s", cmd->usage_args);
str_append_c(out, '\n');
}
}
void doveadm_mail_help(const struct doveadm_mail_cmd *cmd)
{
fprintf(stderr, "doveadm %s [-u <user>|-A] [-S <socket_path>] %s\n",
cmd->name, cmd->usage_args == NULL ? "" : cmd->usage_args);
exit(1);
}
void doveadm_mail_try_help_name(const char *cmd_name)
{
const struct doveadm_mail_cmd *cmd;
cmd = doveadm_mail_cmd_find(cmd_name);
if (cmd != NULL)
doveadm_mail_help(cmd);
}
bool doveadm_mail_has_subcommands(const char *cmd_name)
{
const struct doveadm_mail_cmd *cmd;
unsigned int len = strlen(cmd_name);
array_foreach(&doveadm_mail_cmds, cmd) {
if (strncmp(cmd->name, cmd_name, len) == 0 &&
cmd->name[len] == ' ')
return TRUE;
}
return FALSE;
}
void doveadm_mail_help_name(const char *cmd_name)
{
doveadm_mail_try_help_name(cmd_name);
i_fatal("Missing help for command %s", cmd_name);
}
static struct doveadm_mail_cmd cmd_force_resync = {
cmd_force_resync_alloc, "force-resync", "<mailbox>"
};
static struct doveadm_mail_cmd cmd_purge = {
cmd_purge_alloc, "purge", NULL
};
static struct doveadm_mail_cmd *mail_commands[] = {
&cmd_force_resync,
&cmd_purge,
&cmd_expunge,
&cmd_search,
&cmd_fetch,
&cmd_import,
&cmd_index,
&cmd_altmove,
&cmd_move,
&cmd_mailbox_list,
&cmd_mailbox_create,
&cmd_mailbox_delete,
&cmd_mailbox_rename,
&cmd_mailbox_subscribe,
&cmd_mailbox_unsubscribe,
&cmd_mailbox_status,
&cmd_dsync_backup,
&cmd_dsync_mirror,
&cmd_dsync_server
};
void doveadm_mail_init(void)
{
struct module_dir_load_settings mod_set;
unsigned int i;
i_array_init(&doveadm_mail_cmds, 32);
for (i = 0; i < N_ELEMENTS(mail_commands); i++)
doveadm_mail_register_cmd(mail_commands[i]);
memset(&mod_set, 0, sizeof(mod_set));
mod_set.version = master_service_get_version_string(master_service);
mod_set.require_init_funcs = TRUE;
mod_set.debug = doveadm_debug;
mod_set.binary_name = "doveadm";
/* load all configured mail plugins */
mail_storage_service_modules =
module_dir_load_missing(mail_storage_service_modules,
doveadm_settings->mail_plugin_dir,
doveadm_settings->mail_plugins,
&mod_set);
}
void doveadm_mail_deinit(void)
{
array_free(&doveadm_mail_cmds);
}