doveadm-mail.c revision 554badea1f0259a505e1fd2c276c6caa7495090a
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch/* Copyright (c) 2009-2010 Dovecot authors, see the included COPYING file */
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan BoschARRAY_TYPE(doveadm_mail_cmd) doveadm_mail_cmds;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschvoid (*hook_doveadm_mail_init)(struct doveadm_mail_cmd_context *ctx);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_assert(size >= sizeof(struct doveadm_mail_cmd_context));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch pool = pool_alloconly_create("doveadm mail cmd", 1024);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschcmd_purge_run(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch for (ns = user->namespaces; ns != NULL; ns = ns->next) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (ns->type != NAMESPACE_PRIVATE || ns->alias_for != NULL)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_error("Purging namespace '%s' failed: %s", ns->prefix,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_get_last_error(ns->storage, NULL));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic struct doveadm_mail_cmd_context *cmd_purge_alloc(void)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic int mailbox_find_and_open(struct mail_user *user, const char *mailbox,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("Mailbox name not valid UTF-8: %s", mailbox);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ns = mail_namespace_find(user->namespaces, &mailbox);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("Can't find namespace for mailbox %s", mailbox);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch box = mailbox_alloc(ns->list, mailbox, MAILBOX_FLAG_KEEP_RECENT |
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_error("Opening mailbox %s failed: %s", orig_mailbox,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_get_last_error(mailbox_get_storage(box),
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschint doveadm_mailbox_find_and_sync(struct mail_user *user, const char *mailbox,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox_find_and_open(user, mailbox, box_r) < 0)
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 Boschdoveadm_mail_build_search_args(const char *const args[])
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch parser = mail_search_parser_init_cmdline(args);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mail_search_build(mail_search_register_get_human(),
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void cmd_force_resync_run(struct doveadm_mail_cmd_context *_ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox_find_and_open(user, ctx->mailbox, &box) < 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FORCE_RESYNC |
912e87d5be9dd8895e8cb7c6cb51d8a752edbe8cStephan Bosch i_error("Forcing a resync on mailbox %s failed: %s",
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void cmd_force_resync_init(struct doveadm_mail_cmd_context *_ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch const char *const args[])
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->mailbox = p_strdup(ctx->ctx.pool, mailbox);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic struct doveadm_mail_cmd_context *cmd_force_resync_alloc(void)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx = doveadm_mail_cmd_alloc(struct force_resync_cmd_context);
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 struct mail_storage_service_user *service_user;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_set_failure_prefix(t_strdup_printf("doveadm(%s): ", input->username));
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = mail_storage_service_lookup(ctx->storage_service, input,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch *error_r = t_strdup_printf("User lookup failed: %s",
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (doveadm_settings->doveadm_worker_count > 0 && !doveadm_server) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch /* execute this command via doveadm server */
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = doveadm_mail_server_user(ctx, service_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_user_free(&service_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = mail_storage_service_next(ctx->storage_service, service_user,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_user_free(&service_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_user_free(&service_user);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschvoid doveadm_mail_single_user(struct doveadm_mail_cmd_context *ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch enum mail_storage_service_flags service_flags)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_fatal("USER environment is missing and -u option not used");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->storage_service = mail_storage_service_init(master_service, NULL,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = doveadm_mail_next_user(ctx, &input, &error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch else if (ret == 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_deinit(&ctx->storage_service);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschstatic void sig_die(const siginfo_t *si, void *context ATTR_UNUSED)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_all_users(struct doveadm_mail_cmd_context *ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch enum mail_storage_service_flags service_flags)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch unsigned int user_idx, user_count, interval, n;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch service_flags |= MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ctx->storage_service = mail_storage_service_init(master_service, NULL,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch lib_signals_set_handler(SIGINT, FALSE, sig_die, NULL);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch lib_signals_set_handler(SIGTERM, FALSE, sig_die, NULL);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch user_count = mail_storage_service_all_init(ctx->storage_service);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch for (interval = 10; n > 0 && interval < 1000; interval *= 10)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch while ((ret = ctx->v.get_next_user(ctx, &user)) > 0) {
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch if (!wildcard_match_icase(user, wildcard_user))
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch ret = doveadm_mail_next_user(ctx, &input, &error);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch else if (ret == 0)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_warning("Killed with signal %d", killed_signo);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch i_error("Failed to iterate through some users");
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch mail_storage_service_deinit(&ctx->storage_service);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_init_noop(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_get_next_user(struct doveadm_mail_cmd_context *ctx,
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Bosch return mail_storage_service_all_next(ctx->storage_service, username_r);
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_deinit_noop(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED)
56dd928c164ec5c0d1158a1760154b58c5f1f6e7Stephan Boschdoveadm_mail_cmd_init(const struct doveadm_mail_cmd *cmd)
return ctx;
bool iter_single_user;
if (doveadm_debug)
if (iter_single_user) {
unsigned int len;
return FALSE;
return FALSE;
return FALSE;
return TRUE;
unsigned int cmd_name_len;
return TRUE;
return TRUE;
return FALSE;
return cmd;
return NULL;
return TRUE;
return FALSE;
void doveadm_mail_init(void)
&mod_set);
void doveadm_mail_deinit(void)