imap-quota-plugin.c revision bffaca7da2825c2a77aa46022358e2f8400a0f94
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2005-2017 Dovecot authors, see the included COPYING file */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenconst char *imap_quota_plugin_version = DOVECOT_ABI_VERSION;
f0d93763f210ecdb85a115fdd0210a16cfc5ff5cTimo Sirainenstatic imap_client_created_func_t *next_hook_client_created;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic const char *
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenimap_quota_root_get_name(struct mail_user *user, struct mail_user *owner,
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch return t_strdup_printf("%s%c%s", owner->username,
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Boschquota_reply_write(string_t *str, struct mail_user *user,
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch struct mail_user *owner, struct quota_root *root)
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch unsigned int i;
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch name = imap_quota_root_get_name(user, owner, root);
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch ret = quota_get_resource(root, "", *list, &value, &limit);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen (unsigned long long)value,
2e78f05b11df23ec2731afaf8f19d5b5240cb29fTimo Sirainen (unsigned long long)limit);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* this quota root doesn't have any quota actually enabled. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic bool cmd_getquotaroot(struct client_command_context *cmd)
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen struct quota_user *quser = QUOTA_USER_CONTEXT(client->user);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* <mailbox> */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen if (ns->owner != NULL && ns->owner != client->user) {
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen client_send_tagline(cmd, "NO Not showing other users' quota.");
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen box = mailbox_alloc(ns->list, mailbox, MAILBOX_FLAG_READONLY);
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen /* build QUOTAROOT reply and QUOTA reply for all quota roots */
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen imap_append_astring(quotaroot_reply, orig_mailbox);
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen while ((root = quota_root_iter_next(iter)) != NULL) {
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen name = imap_quota_root_get_name(client->user, ns->owner, root);
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen if (quota_reply_write(quota_reply, client->user, ns->owner, root) < 0)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* send replies */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen client_send_tagline(cmd, "NO Internal quota calculation error.");
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen client_send_line(client, str_c(quotaroot_reply));
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen o_stream_nsend(client->output, str_data(quota_reply),
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen client_send_tagline(cmd, "OK Getquotaroot completed.");
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenparse_quota_root(struct mail_user *user, const char *root_name,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mail_user **owner_r, struct quota_root **root_r)
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch const char *p;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we're an admin. see if there's a quota root for another user. */
e7dd5065d21c569e5e6ddd713bd345dadd1cf51dTimo Sirainen *owner_r = mail_user_find(user, t_strdup_until(root_name, p));
009217abb57a24a4076092e8e4e165545747839eStephan Boschstatic bool cmd_getquota(struct client_command_context *cmd)
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch /* <quota root> */
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch if (!client_read_string_args(cmd, 1, &root_name))
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch if (!parse_quota_root(cmd->client->user, root_name, &owner, &root)) {
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch client_send_tagline(cmd, "NO Quota root doesn't exist.");
1093de32efb2a231949566d4bd8aa55a8f43fb70Timo Sirainen if (quota_reply_write(quota_reply, cmd->client->user, owner, root) < 0)
ab281fc992907b6cf6c730f672dc3aa4c6685015Timo Sirainen client_send_tagline(cmd, "NO Internal quota calculation error.");
6d24551e169c0808695db35d7a228f1970a84c75Timo Sirainen o_stream_nsend(cmd->client->output, str_data(quota_reply),
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen client_send_tagline(cmd, "OK Getquota completed.");
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainenstatic bool cmd_setquota(struct client_command_context *cmd)
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainen const char *root_name, *name, *value_str, *error;
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch /* <quota root> <resource limits> */
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch if (!imap_arg_get_astring(&args[0], &root_name) ||
8ce3071e80b9973230048ecadfcb073fb82cc69fTimo Sirainen client_send_command_error(cmd, "Invalid arguments.");
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen client_send_tagline(cmd, "NO Quota can be changed only by admin.");
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk if (!parse_quota_root(cmd->client->user, root_name, &owner, &root)) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen client_send_tagline(cmd, "NO Quota root doesn't exist.");
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen for (; !IMAP_ARG_IS_EOL(list_args); list_args += 2) {
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen if (!imap_arg_get_atom(&list_args[0], &name) ||
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen !imap_arg_get_atom(&list_args[1], &value_str) ||
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen client_send_command_error(cmd, "Invalid arguments.");
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (quota_set_resource(root, name, value, &error) < 0) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen client_send_tagline(cmd, "OK Setquota completed.");
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic void imap_quota_client_created(struct client **client)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (mail_user_is_plugin_loaded((*client)->user, imap_quota_module))
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainenvoid imap_quota_plugin_init(struct module *module)
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen command_register("GETQUOTAROOT", cmd_getquotaroot, 0);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen command_register("GETQUOTA", cmd_getquota, 0);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen command_register("SETQUOTA", cmd_setquota, 0);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen imap_client_created_hook_set(imap_quota_client_created);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen imap_client_created_hook_set(next_hook_client_created);
ec23e16ed879e289d12c6e1a5f9745dd3979004aTimo Sirainenconst char *imap_quota_plugin_dependencies[] = { "quota", NULL };
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenconst char imap_quota_plugin_binary_dependency[] = "imap";