doveadm.c revision 6d2b3ce2c6ef62334985ece4f0ab8b154e0e9560
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "lib.h"
d5abbb932a0a598f002da39a8b3326643b1b5efcTimo Sirainen#include "env-util.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "master-service.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "mail-user.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "mail-namespace.h"
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen#include "mail-storage.h"
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen#include "mail-storage-settings.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "mail-storage-service.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include <stdlib.h>
c5383a0ed56a188a7d5efaaa4c6f8243af432d65Timo Sirainen
c5383a0ed56a188a7d5efaaa4c6f8243af432d65Timo Sirainenstatic struct mail_user *mail_user;
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainenstatic void ATTR_NORETURN
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainenusage(void)
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen{
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen i_fatal(
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen"usage: doveadm \n"
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen" purge <user>\n"
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen" force-resync <user> <mailbox>\n"
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen}
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainenstatic void cmd_purge(struct mail_user *user)
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen{
3b94ff5951db4d4eddb7a80ed4e3f61207202635Timo Sirainen struct mail_namespace *ns;
66d2db642fe24d555d113ba463e446b038d476efTimo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen if (ns->type != NAMESPACE_PRIVATE || ns->alias_for != NULL)
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen continue;
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (mail_storage_purge(ns->storage) < 0) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_error("Purging namespace '%s' failed: %s", ns->prefix,
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen mail_storage_get_last_error(ns->storage, NULL));
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen }
3b94ff5951db4d4eddb7a80ed4e3f61207202635Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen}
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainenstatic struct mailbox *
22535a9e685e29214082878e37a267157044618eTimo Sirainenmailbox_find_and_open(struct mail_user *user, const char *mailbox)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct mail_namespace *ns;
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen struct mail_storage *storage;
137ea7ca34005345aa2304a940149b7f3774d727Timo Sirainen struct mailbox *box;
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen const char *orig_mailbox = mailbox;
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen ns = mail_namespace_find(user->namespaces, &mailbox);
312d3129573fc63ba0d39105b68daccfab9f0c8fTimo Sirainen if (ns == NULL)
312d3129573fc63ba0d39105b68daccfab9f0c8fTimo Sirainen i_fatal("Can't find namespace for mailbox %s", mailbox);
312d3129573fc63ba0d39105b68daccfab9f0c8fTimo Sirainen
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen storage = ns->storage;
312d3129573fc63ba0d39105b68daccfab9f0c8fTimo Sirainen box = mailbox_open(&storage, mailbox, NULL, MAILBOX_OPEN_KEEP_RECENT |
312d3129573fc63ba0d39105b68daccfab9f0c8fTimo Sirainen MAILBOX_OPEN_IGNORE_ACLS);
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen if (box == NULL) {
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen i_fatal("Opening mailbox %s failed: %s", orig_mailbox,
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen mail_storage_get_last_error(storage, NULL));
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen }
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen return box;
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen}
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainenstatic void cmd_force_resync(struct mail_user *user, const char *mailbox)
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen{
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen struct mail_storage *storage;
40eb305d9b12cf48400fe3806a8a15ad6d372952Timo Sirainen struct mailbox *box;
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen if (mailbox == NULL)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen usage();
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen box = mailbox_find_and_open(user, mailbox);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen storage = mailbox_get_storage(box);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FORCE_RESYNC |
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen MAILBOX_SYNC_FLAG_FIX_INCONSISTENT, 0, NULL) < 0) {
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen i_fatal("Forcing a resync on mailbox %s failed: %s", mailbox,
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen mail_storage_get_last_error(storage, NULL));
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen }
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen mailbox_close(&box);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen}
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainenint main(int argc, char *argv[])
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen{
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen enum mail_storage_service_flags service_flags = 0;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen struct master_service *service;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen const char *getopt_str, *user;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen int c;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen service = master_service_init("doveadm",
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen MASTER_SERVICE_FLAG_STANDALONE |
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen MASTER_SERVICE_FLAG_LOG_TO_STDERR,
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen argc, argv);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen user = getenv("USER");
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen getopt_str = t_strconcat("u:v", master_service_getopt_string(), NULL);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen while ((c = getopt(argc, argv, getopt_str)) > 0) {
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen switch (c) {
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen case 'u':
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen user = optarg;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen service_flags |= MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen break;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen case 'v':
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen service_flags |= MAIL_STORAGE_SERVICE_FLAG_DEBUG;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen break;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen default:
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen if (!master_service_parse_option(service, c, optarg))
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen usage();
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen }
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen }
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen if (optind == argc)
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen usage();
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen if (user == NULL)
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen i_fatal("USER environment is missing and -u option not used");
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen mail_user = mail_storage_service_init_user(service, user, NULL,
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen service_flags);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen i_set_failure_prefix(t_strdup_printf("doveadm(%s): ",
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen mail_user->username));
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen if (strcmp(argv[optind], "purge") == 0)
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen cmd_purge(mail_user);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen else if (strcmp(argv[optind], "force-resync") == 0)
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen cmd_force_resync(mail_user, argv[optind+2]);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen else
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen usage();
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen mail_user_unref(&mail_user);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen mail_storage_service_deinit_user();
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen master_service_deinit(&service);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen return 0;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen}
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen