doveadm-mail.c revision 09304bdc1e5e416aa343802732a15064f49495cc
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2009-2010 Dovecot authors, see the included COPYING file */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo SirainenARRAY_TYPE(doveadm_mail_cmd) doveadm_mail_cmds;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenvoid (*hook_doveadm_mail_init)(struct doveadm_mail_cmd_context *ctx);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen i_assert(size >= sizeof(struct doveadm_mail_cmd_context));
4462bd7b4c7ef3de006f060e155a90e5de7cae21Timo Sirainen pool = pool_alloconly_create("doveadm mail cmd", 1024);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainencmd_purge_run(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (ns->type != NAMESPACE_PRIVATE || ns->alias_for != NULL)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen i_error("Purging namespace '%s' failed: %s", ns->prefix,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen mail_storage_get_last_error(ns->storage, NULL));
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_purge_alloc(void)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
cbc61fcb33b370d049c16a3c44568b4deb4e2b33Timo Sirainenstatic int mailbox_find_and_open(struct mail_user *user, const char *mailbox,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen i_fatal("Mailbox name not valid UTF-8: %s", mailbox);
e07bf3772a2bc075de4915ad0961beb8d083c22dTimo Sirainen ns = mail_namespace_find(user->namespaces, &mailbox);
e07bf3772a2bc075de4915ad0961beb8d083c22dTimo Sirainen i_fatal("Can't find namespace for mailbox %s", mailbox);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen box = mailbox_alloc(ns->list, mailbox, MAILBOX_FLAG_KEEP_RECENT |
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen i_error("Opening mailbox %s failed: %s", orig_mailbox,
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen mail_storage_get_last_error(mailbox_get_storage(box),
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainenint doveadm_mailbox_find_and_sync(struct mail_user *user, const char *mailbox,
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen if (mailbox_find_and_open(user, mailbox, box_r) < 0)
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen if (mailbox_sync(*box_r, MAILBOX_SYNC_FLAG_FULL_READ) < 0) {
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen i_error("Syncing mailbox %s failed: %s", mailbox,
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen mail_storage_get_last_error(mailbox_get_storage(*box_r),
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainendoveadm_mail_build_search_args(const char *const args[])
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen parser = mail_search_parser_init_cmdline(args);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (mail_search_build(mail_search_register_get_human(),
2e263a9d901483a902720a30c474761bd3324fe8Timo Sirainenstatic void cmd_force_resync_run(struct doveadm_mail_cmd_context *_ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (mailbox_find_and_open(user, ctx->mailbox, &box) < 0) {
cbc61fcb33b370d049c16a3c44568b4deb4e2b33Timo Sirainen if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FORCE_RESYNC |
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen i_error("Forcing a resync on mailbox %s failed: %s",
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainenstatic void cmd_force_resync_init(struct doveadm_mail_cmd_context *_ctx,
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen const char *const args[])
3e7565a7b39694bcdf448d8eb2a7f0774733297bTimo Sirainen ctx->mailbox = p_strdup(ctx->ctx.pool, mailbox);
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_force_resync_alloc(void)
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen ctx = doveadm_mail_cmd_alloc(struct force_resync_cmd_context);
2e263a9d901483a902720a30c474761bd3324fe8Timo Sirainendoveadm_mail_next_user(struct doveadm_mail_cmd_context *ctx,
2e263a9d901483a902720a30c474761bd3324fe8Timo Sirainen const struct mail_storage_service_input *input,
2e263a9d901483a902720a30c474761bd3324fe8Timo Sirainen const char **error_r)
2e263a9d901483a902720a30c474761bd3324fe8Timo Sirainen struct mail_storage_service_user *service_user;
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen i_set_failure_prefix(t_strdup_printf("doveadm(%s): ", input->username));
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen ret = mail_storage_service_lookup(ctx->storage_service, input,
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen *error_r = t_strdup_printf("User lookup failed: %s",
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen ret = mail_storage_service_next(ctx->storage_service, service_user,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen mail_storage_service_user_free(&service_user);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen mail_storage_service_user_free(&service_user);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainendoveadm_mail_single_user(struct doveadm_mail_cmd_context *ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen enum mail_storage_service_flags service_flags)
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen i_fatal("USER environment is missing and -u option not used");
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen ctx->storage_service = mail_storage_service_init(master_service, NULL,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen ret = doveadm_mail_next_user(ctx, &input, &error);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen else if (ret == 0)
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen mail_storage_service_deinit(&ctx->storage_service);
cbc61fcb33b370d049c16a3c44568b4deb4e2b33Timo Sirainenstatic void sig_die(const siginfo_t *si, void *context ATTR_UNUSED)
cbc61fcb33b370d049c16a3c44568b4deb4e2b33Timo Sirainendoveadm_mail_all_users(struct doveadm_mail_cmd_context *ctx,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen enum mail_storage_service_flags service_flags)
bd4d0a1a7c0626452b8d82f37e3ec07267ac9896Timo Sirainen unsigned int user_idx, user_count, interval, n;
bd4d0a1a7c0626452b8d82f37e3ec07267ac9896Timo Sirainen service_flags |= MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen ctx->storage_service = mail_storage_service_init(master_service, NULL,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen lib_signals_set_handler(SIGINT, FALSE, sig_die, NULL);
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen lib_signals_set_handler(SIGTERM, FALSE, sig_die, NULL);
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen user_count = mail_storage_service_all_init(ctx->storage_service);
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen for (interval = 10; n > 0 && interval < 1000; interval *= 10)
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen while ((ret = ctx->v.get_next_user(ctx, &user)) > 0) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen if (!wildcard_match_icase(user, wildcard_user))
56963ffad65b860c827553dfaf09fb766cb7e20eTimo Sirainen ret = doveadm_mail_next_user(ctx, &input, &error);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen else if (ret == 0)
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen i_warning("Killed with signal %d", killed_signo);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen i_error("Failed to iterate through some users");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mail_storage_service_deinit(&ctx->storage_service);
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainendoveadm_mail_cmd_init_noop(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainendoveadm_mail_cmd_get_next_user(struct doveadm_mail_cmd_context *ctx,
9514dbd41faad453406fc3a10aca791417a9ce75Timo Sirainen return mail_storage_service_all_next(ctx->storage_service, username_r);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainendoveadm_mail_cmd_deinit_noop(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED)
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainendoveadm_mail_cmd(const struct doveadm_mail_cmd *cmd, int argc, char *argv[])
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen enum mail_storage_service_flags service_flags =
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen const char *getopt_args, *username, *wildcard_user;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen service_flags |= MAIL_STORAGE_SERVICE_FLAG_DEBUG;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen ctx->v.get_next_user = doveadm_mail_cmd_get_next_user;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen p_array_init(&ctx->module_contexts, ctx->pool, 5);
5666a3d6a7ea89362b8d9e8b39b15424cd9d6388Timo Sirainen getopt_args = t_strconcat("Au:", ctx->getopt_args, NULL);
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen while ((c = getopt(argc, argv, getopt_args)) > 0) {
unsigned int len;
return FALSE;
return FALSE;
return FALSE;
return TRUE;
unsigned int cmd_name_len;
return TRUE;
return TRUE;
return FALSE;
exit(0);
return TRUE;
return FALSE;
void doveadm_mail_init(void)
&mod_set);
void doveadm_mail_deinit(void)