bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
bd63b5b860658b01b1f46f26d406e1e4a9dc019aTimo Sirainenconst char *imap_quota_plugin_version = DOVECOT_ABI_VERSION;
c1faff067b29fb48426cb84260adba563e93189aTimo Sirainenstatic imap_client_created_func_t *next_hook_client_created;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainenstatic const char *
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainenimap_quota_root_get_name(struct mail_user *user, struct mail_user *owner,
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen return t_strdup_printf("%s%c%s", owner->username,
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainenquota_reply_write(string_t *str, struct mail_user *user,
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen struct mail_user *owner, struct quota_root *root)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen unsigned int i;
2e07e3182f355cf04a1461dd7f893d0ebc818764Timo Sirainen enum quota_get_result ret = QUOTA_GET_RESULT_UNLIMITED;
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen name = imap_quota_root_get_name(user, owner, root);
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi ret = quota_get_resource(root, "", *list, &value, &limit, &error);
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi if (ret == QUOTA_GET_RESULT_INTERNAL_ERROR) {
cf41318871bd42358df3420e50614f5310b08c77Martti Rannanjärvi i_error("Failed to get quota resource %s: %s",
47a5a7e8296f3b8f2fac9a0659d4de3f2723ba4aMartti Rannanjärvi str_printfa(str, "%s %"PRIu64" %"PRIu64, *list,
f162f350e1eacce12288a565657e769d7976dd7fTimo Sirainen /* this quota root doesn't have any quota actually enabled. */
2e07e3182f355cf04a1461dd7f893d0ebc818764Timo Sirainen return ret == QUOTA_GET_RESULT_INTERNAL_ERROR ? -1 : 0;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenstatic bool cmd_getquotaroot(struct client_command_context *cmd)
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen struct quota_user *quser = QUOTA_USER_CONTEXT(client->user);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* <mailbox> */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
10ccd0e45768923d69be459e87ef6cd2574cec60Timo Sirainen if (ns->owner != NULL && ns->owner != client->user) {
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen client_send_tagline(cmd, "NO Not showing other users' quota.");
4145cbac82bfc0c8bfeceeca0ef841700117930cTimo Sirainen box = mailbox_alloc(ns->list, mailbox, MAILBOX_FLAG_READONLY);
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen /* build QUOTAROOT reply and QUOTA reply for all quota roots */
44f93baa7b8dca7d00bf187cd3db1c15eed384d2Timo Sirainen imap_append_astring(quotaroot_reply, orig_mailbox);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen while ((root = quota_root_iter_next(iter)) != NULL) {
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen name = imap_quota_root_get_name(client->user, ns->owner, root);
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen if (quota_reply_write(quota_reply, client->user, ns->owner, root) < 0)
5a37e34b1b5acf453372cd112c70bb4e46b4bee2Timo Sirainen /* send replies */
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen client_send_tagline(cmd, "NO Internal quota calculation error.");
d337f4291d9ea9cd1adee0f468bc0c3baeabb4feTimo Sirainen client_send_line(client, str_c(quotaroot_reply));
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_nsend(client->output, str_data(quota_reply),
d337f4291d9ea9cd1adee0f468bc0c3baeabb4feTimo Sirainen client_send_tagline(cmd, "OK Getquotaroot completed.");
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainenparse_quota_root(struct mail_user *user, const char *root_name,
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen struct mail_user **owner_r, struct quota_root **root_r)
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen const char *p;
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen /* we're an admin. see if there's a quota root for another user. */
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen *owner_r = mail_user_find(user, t_strdup_until(root_name, p));
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenstatic bool cmd_getquota(struct client_command_context *cmd)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* <quota root> */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen if (!client_read_string_args(cmd, 1, &root_name))
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen if (!parse_quota_root(cmd->client->user, root_name, &owner, &root)) {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen client_send_tagline(cmd, "NO Quota root doesn't exist.");
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen if (quota_reply_write(quota_reply, cmd->client->user, owner, root) < 0)
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen client_send_tagline(cmd, "NO Internal quota calculation error.");
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen o_stream_nsend(cmd->client->output, str_data(quota_reply),
f4e66312c54f8f21df984e3b17c0cc752e019ec5Timo Sirainen client_send_tagline(cmd, "OK Getquota completed.");
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenstatic bool cmd_setquota(struct client_command_context *cmd)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen const char *root_name, *name, *value_str, *error;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen /* <quota root> <resource limits> */
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (!imap_arg_get_astring(&args[0], &root_name) ||
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen client_send_tagline(cmd, "NO Quota can be changed only by admin.");
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen if (!parse_quota_root(cmd->client->user, root_name, &owner, &root)) {
e5dca7edde333c2759b0e6b1d0d00b94ea303322Timo Sirainen client_send_tagline(cmd, "NO Quota root doesn't exist.");
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen for (; !IMAP_ARG_IS_EOL(list_args); list_args += 2) {
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (!imap_arg_get_atom(&list_args[0], &name) ||
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen !imap_arg_get_atom(&list_args[1], &value_str) ||
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
bf9ea5404a0094a8fb8199b677d81f803512c44eTimo Sirainen if (quota_set_resource(root, name, value, &error) < 0) {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen client_send_tagline(cmd, "OK Setquota completed.");
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic void imap_quota_client_created(struct client **client)
cd2cd224d3216a243d55c71c298a5b7684de0ac4Timo Sirainen if (mail_user_is_plugin_loaded((*client)->user, imap_quota_module))
cd2cd224d3216a243d55c71c298a5b7684de0ac4Timo Sirainenvoid imap_quota_plugin_init(struct module *module)
66a872b4569c048e804f0731680d89c6042d8890Timo Sirainen command_register("GETQUOTAROOT", cmd_getquotaroot, 0);
66a872b4569c048e804f0731680d89c6042d8890Timo Sirainen command_register("GETQUOTA", cmd_getquota, 0);
66a872b4569c048e804f0731680d89c6042d8890Timo Sirainen command_register("SETQUOTA", cmd_setquota, 0);
ac0fed903142d28ae3a1d5d00d2097fdf161b138Timo Sirainen imap_client_created_hook_set(imap_quota_client_created);
b66d803de86bfb411165b3465b0d9ef64ecfe2a1Timo Sirainen imap_client_created_hook_set(next_hook_client_created);
23fdad6c7e2581921f511e24cd9371c9eaebcef9Timo Sirainenconst char *imap_quota_plugin_dependencies[] = { "quota", NULL };
8552b0cad8ffe9ccb8270577ba28b8010c89af11Timo Sirainenconst char imap_quota_plugin_binary_dependency[] = "imap";