doveadm-mail.c revision 554badea1f0259a505e1fd2c276c6caa7495090a
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch/* Copyright (c) 2009-2010 Dovecot authors, see the included COPYING file */
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "lib.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "array.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "lib-signals.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "ioloop.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "str.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "module-dir.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "wildcard-match.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "master-service.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "imap-utf7.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "mail-user.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "mail-namespace.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "mail-storage.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "mail-storage-settings.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "mail-storage-service.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "mail-search-build.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "mail-search-parser.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "doveadm.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "doveadm-settings.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "doveadm-print.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include "doveadm-mail.h"
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include <stdio.h>
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch#include <stdlib.h>
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan BoschARRAY_TYPE(doveadm_mail_cmd) doveadm_mail_cmds;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschvoid (*hook_doveadm_mail_init)(struct doveadm_mail_cmd_context *ctx);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstruct doveadm_mail_cmd_module_register
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch doveadm_mail_cmd_module_register = { 0 };
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic int killed_signo = 0;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstruct doveadm_mail_cmd_context *
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_alloc_size(size_t size)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct doveadm_mail_cmd_context *ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch pool_t pool;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_assert(size >= sizeof(struct doveadm_mail_cmd_context));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch pool = pool_alloconly_create("doveadm mail cmd", 1024);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx = p_malloc(pool, size);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->pool = pool;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschcmd_purge_run(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_user *user)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_namespace *ns;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch for (ns = user->namespaces; ns != NULL; ns = ns->next) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ns->type != NAMESPACE_PRIVATE || ns->alias_for != NULL)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch continue;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mail_storage_purge(ns->storage) < 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_error("Purging namespace '%s' failed: %s", ns->prefix,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_get_last_error(ns->storage, NULL));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic struct doveadm_mail_cmd_context *cmd_purge_alloc(void)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct doveadm_mail_cmd_context *ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->v.run = cmd_purge_run;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic int mailbox_find_and_open(struct mail_user *user, const char *mailbox,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mailbox **box_r)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_namespace *ns;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mailbox *box;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch string_t *str;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *orig_mailbox = mailbox;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch str = t_str_new(128);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (imap_utf8_to_utf7(mailbox, str) < 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("Mailbox name not valid UTF-8: %s", mailbox);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mailbox = str_c(str);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ns = mail_namespace_find(user->namespaces, &mailbox);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ns == NULL)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("Can't find namespace for mailbox %s", mailbox);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch box = mailbox_alloc(ns->list, mailbox, MAILBOX_FLAG_KEEP_RECENT |
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch MAILBOX_FLAG_IGNORE_ACLS);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox_open(box) < 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_error("Opening mailbox %s failed: %s", orig_mailbox,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_get_last_error(mailbox_get_storage(box),
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch NULL));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mailbox_free(&box);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return -1;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch *box_r = box;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return 0;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschint doveadm_mailbox_find_and_sync(struct mail_user *user, const char *mailbox,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mailbox **box_r)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox_find_and_open(user, mailbox, box_r) < 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return -1;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox_sync(*box_r, MAILBOX_SYNC_FLAG_FULL_READ) < 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_error("Syncing mailbox %s failed: %s", mailbox,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_get_last_error(mailbox_get_storage(*box_r),
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch NULL));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return -1;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return 0;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstruct mail_search_args *
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_build_search_args(const char *const args[])
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_search_parser *parser;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_search_args *sargs;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *error;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch parser = mail_search_parser_init_cmdline(args);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mail_search_build(mail_search_register_get_human(),
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch parser, "UTF-8", &sargs, &error) < 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("%s", error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_search_parser_deinit(&parser);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return sargs;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstruct force_resync_cmd_context {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct doveadm_mail_cmd_context ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *mailbox;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch};
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void cmd_force_resync_run(struct doveadm_mail_cmd_context *_ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_user *user)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct force_resync_cmd_context *ctx =
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch (struct force_resync_cmd_context *)_ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_storage *storage;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mailbox *box;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox_find_and_open(user, ctx->mailbox, &box) < 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch _ctx->failed = TRUE;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch storage = mailbox_get_storage(box);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FORCE_RESYNC |
f091dd12d19df22e5403855f93dedee437bd7d87Stephan Bosch MAILBOX_SYNC_FLAG_FIX_INCONSISTENT) < 0) {
912e87d5be9dd8895e8cb7c6cb51d8a752edbe8cStephan Bosch i_error("Forcing a resync on mailbox %s failed: %s",
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->mailbox,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_get_last_error(storage, NULL));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch _ctx->failed = TRUE;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mailbox_free(&box);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void cmd_force_resync_init(struct doveadm_mail_cmd_context *_ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *const args[])
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct force_resync_cmd_context *ctx =
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch (struct force_resync_cmd_context *)_ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *mailbox = args[0];
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox == NULL || args[1] != NULL)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch doveadm_mail_help_name("force-resync");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->mailbox = p_strdup(ctx->ctx.pool, mailbox);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic struct doveadm_mail_cmd_context *cmd_force_resync_alloc(void)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct force_resync_cmd_context *ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx = doveadm_mail_cmd_alloc(struct force_resync_cmd_context);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->ctx.v.init = cmd_force_resync_init;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->ctx.v.run = cmd_force_resync_run;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return &ctx->ctx;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic int
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_next_user(struct doveadm_mail_cmd_context *ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const struct mail_storage_service_input *input,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char **error_r)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_storage_service_user *service_user;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *error;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch int ret;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_set_failure_prefix(t_strdup_printf("doveadm(%s): ", input->username));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = mail_storage_service_lookup(ctx->storage_service, input,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch &service_user, &error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ret <= 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ret < 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch *error_r = t_strdup_printf("User lookup failed: %s",
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return ret;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (doveadm_settings->doveadm_worker_count > 0 && !doveadm_server) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch /* execute this command via doveadm server */
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch T_BEGIN {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = doveadm_mail_server_user(ctx, service_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_user_free(&service_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch } T_END;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return ret < 0 ? -1 : 1;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = mail_storage_service_next(ctx->storage_service, service_user,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch &ctx->cur_mail_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ret < 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch *error_r = "User init failed";
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_user_free(&service_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return ret;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->v.run(ctx, ctx->cur_mail_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_user_unref(&ctx->cur_mail_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_user_free(&service_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return 1;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschvoid doveadm_mail_single_user(struct doveadm_mail_cmd_context *ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *username,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch enum mail_storage_service_flags service_flags)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_storage_service_input input;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *error;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch int ret;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (username == NULL)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("USER environment is missing and -u option not used");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch memset(&input, 0, sizeof(input));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch input.username = username;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->storage_service = mail_storage_service_init(master_service, NULL,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch service_flags);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (hook_doveadm_mail_init != NULL)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch hook_doveadm_mail_init(ctx);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = doveadm_mail_next_user(ctx, &input, &error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ret < 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("%s", error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch else if (ret == 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("User doesn't exist");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_deinit(&ctx->storage_service);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void sig_die(const siginfo_t *si, void *context ATTR_UNUSED)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch killed_signo = si->si_signo;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_all_users(struct doveadm_mail_cmd_context *ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *wildcard_user,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch enum mail_storage_service_flags service_flags)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch struct mail_storage_service_input input;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch unsigned int user_idx, user_count, interval, n;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *user, *error;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch int ret;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch service_flags |= MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch memset(&input, 0, sizeof(input));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch input.service = "doveadm";
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->storage_service = mail_storage_service_init(master_service, NULL,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch service_flags);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch lib_signals_set_handler(SIGINT, FALSE, sig_die, NULL);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch lib_signals_set_handler(SIGTERM, FALSE, sig_die, NULL);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (hook_doveadm_mail_init != NULL)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch hook_doveadm_mail_init(ctx);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch user_count = mail_storage_service_all_init(ctx->storage_service);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch n = user_count / 10000;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch for (interval = 10; n > 0 && interval < 1000; interval *= 10)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch n /= 10;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch user_idx = 0;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch while ((ret = ctx->v.get_next_user(ctx, &user)) > 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (wildcard_user != NULL) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (!wildcard_match_icase(user, wildcard_user))
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch continue;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch input.username = user;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch doveadm_print_sticky("username", user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch T_BEGIN {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = doveadm_mail_next_user(ctx, &input, &error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ret < 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_error("%s", error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch else if (ret == 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_info("User no longer exists, skipping");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch } T_END;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ret == -1)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch break;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (doveadm_verbose) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (++user_idx % interval == 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch printf("\r%d / %d", user_idx, user_count);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch fflush(stdout);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (killed_signo != 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_warning("Killed with signal %d", killed_signo);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = -1;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch break;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch }
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (doveadm_verbose)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch printf("\n");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_set_failure_prefix("doveadm: ");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ret < 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_error("Failed to iterate through some users");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_deinit(&ctx->storage_service);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch doveadm_mail_server_flush();
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_init_noop(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *const args[] ATTR_UNUSED)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic int
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_get_next_user(struct doveadm_mail_cmd_context *ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char **username_r)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return mail_storage_service_all_next(ctx->storage_service, username_r);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_deinit_noop(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch}
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstruct doveadm_mail_cmd_context *
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_init(const struct doveadm_mail_cmd *cmd)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch{
5d5ad796fc34170333bb52c05614425a0dd7e182Stephan Bosch struct doveadm_mail_cmd_context *ctx;
5d5ad796fc34170333bb52c05614425a0dd7e182Stephan Bosch
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx = cmd->alloc();
6d76b99eb61df1cbe13cf76d0aac81507cbd1d21Stephan Bosch ctx->cmd = cmd;
6d76b99eb61df1cbe13cf76d0aac81507cbd1d21Stephan Bosch if (ctx->v.init == NULL)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->v.init = doveadm_mail_cmd_init_noop;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch 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[])
{
enum mail_storage_service_flags service_flags =
MAIL_STORAGE_SERVICE_FLAG_NO_LOG_INIT;
struct doveadm_mail_cmd_context *ctx;
const char *getopt_args, *username, *wildcard_user;
bool iter_single_user;
int c;
if (doveadm_debug)
service_flags |= MAIL_STORAGE_SERVICE_FLAG_DEBUG;
ctx = doveadm_mail_cmd_init(cmd);
ctx->args = (const void *)argv;
getopt_args = t_strconcat("Au:", ctx->getopt_args, NULL);
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 'u':
service_flags |=
MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
username = optarg;
if (strchr(username, '*') != NULL ||
strchr(username, '?') != NULL)
wildcard_user = username;
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]);
}
iter_single_user = !ctx->iterate_all_users && wildcard_user == NULL;
if (doveadm_print_is_initialized() && !iter_single_user) {
doveadm_print_header("username", "Username",
DOVEADM_PRINT_HEADER_FLAG_STICKY |
DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
}
ctx->v.init(ctx, (const void *)argv);
if (iter_single_user) {
doveadm_mail_single_user(ctx, username, service_flags);
} else {
service_flags |= MAIL_STORAGE_SERVICE_FLAG_TEMP_PRIV_DROP;
doveadm_mail_all_users(ctx, wildcard_user, service_flags);
}
ctx->v.deinit(ctx);
doveadm_print_flush();
if (ctx->failed)
exit(FATAL_DEFAULT);
}
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) {
str_printfa(out, "%s\t[-u <user>|-A]", 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\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_altmove,
&cmd_mailbox_list,
&cmd_mailbox_create,
&cmd_mailbox_delete,
&cmd_mailbox_rename,
&cmd_mailbox_subscribe,
&cmd_mailbox_unsubscribe,
&cmd_mailbox_status
};
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);
}