quota-imapc.c revision fde14422caabc3c4ac4a6c5e3e5cf176cedd90a6
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen unsigned int order;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenextern struct quota_backend quota_backend_imapc;
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainenstatic struct quota_root *imapc_quota_alloc(void)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic void handle_box_param(struct quota_root *_root, const char *param_value)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen ((struct imapc_quota_root *)_root)->box_name = p_strdup(_root->pool, param_value);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic void handle_root_param(struct quota_root *_root, const char *param_value)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen ((struct imapc_quota_root *)_root)->root_name = p_strdup(_root->pool, param_value);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic int imapc_quota_init(struct quota_root *_root, const char *args,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char **error_r)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct imapc_quota_root *root = (struct imapc_quota_root *)_root;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct quota_param_parser imapc_params[] = {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen {.param_name = "box=", .param_handler = handle_box_param},
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen {.param_name = "root=", .param_handler = handle_root_param},
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (quota_parse_parameters(_root, &args, error_r, imapc_params, TRUE) < 0)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (root->box_name == NULL && root->root_name == NULL)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen /* we'll never try to enforce the quota - it's just a lot of
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen unnecessary remote GETQUOTA calls. */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic void imapc_quota_deinit(struct quota_root *_root)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenimapc_quota_root_namespace_added(struct quota_root *_root,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen struct imapc_quota_root *root = (struct imapc_quota_root *)_root;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenimapc_quota_root_refresh_find(struct imapc_storage_client *client)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct imapc_storage *storage = client->_storage;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen quota = quota_get_mail_user_quota(storage->storage.user);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* find the quota root that is being refreshed */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if ((*rootp)->backend.name == quota_backend_imapc.name) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenimapc_quota_refresh_root_get(struct imapc_quota_refresh *refresh,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct imapc_quota_refresh_root *refresh_root;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen array_foreach_modifiable(&refresh->roots, refresh_root) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (strcmp(refresh_root->name, root_name) == 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen refresh_root = array_append_space(&refresh->roots);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen refresh_root->name = p_strdup(refresh->pool, root_name);
414b2d3b63412c4bef6a4caa7a7222f3abbdbabaTimo Sirainenstatic void imapc_untagged_quotaroot(const struct imapc_untagged_reply *reply,
414b2d3b63412c4bef6a4caa7a7222f3abbdbabaTimo Sirainen struct imapc_quota_refresh_root *refresh_root;
414b2d3b63412c4bef6a4caa7a7222f3abbdbabaTimo Sirainen unsigned int i;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (!imap_arg_get_astring(&reply->args[0], &mailbox_name))
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if ((refresh = imapc_quota_root_refresh_find(client)) == NULL ||
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen strcmp(refresh->box_name, mailbox_name) != 0) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* unsolicited QUOTAROOT reply - ignore */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* duplicate QUOTAROOT reply - ignore */
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen while (imap_arg_get_astring(&reply->args[i], &root_name)) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen refresh_root = imapc_quota_refresh_root_get(refresh, root_name);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic void imapc_untagged_quota(const struct imapc_untagged_reply *reply,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct imapc_quota_refresh_root *refresh_root;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char *root_name, *resource, *value_str, *limit_str;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen unsigned int i;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (!imap_arg_get_astring(&reply->args[0], &root_name) ||
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if ((refresh = imapc_quota_root_refresh_find(client)) == NULL) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* unsolicited QUOTA reply - ignore */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen refresh_root = imapc_quota_refresh_root_get(refresh, root_name);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen for (i = 0; list[i].type != IMAP_ARG_EOL; i += 3) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (!imap_arg_get_atom(&list[i], &resource) ||
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* RFC2087 uses 32bit number, but be ready for future */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (strcasecmp(resource, QUOTA_NAME_STORAGE_KILOBYTES) == 0) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen } else if (strcasecmp(resource, QUOTA_NAME_MESSAGES) == 0) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic bool imapc_quota_client_init(struct imapc_quota_root *root)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (mailbox_list_get_storage(&list, "", &storage) == 0 &&
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen strcmp(storage->name, IMAPC_STORAGE_NAME) != 0) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* non-imapc namespace, skip */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_warning("quota: Namespace '%s' is not imapc, "
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen "skipping for imapc quota",
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen root->client = ((struct imapc_storage *)storage)->client;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen imapc_storage_client_register_untagged(root->client, "QUOTAROOT",
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen imapc_storage_client_register_untagged(root->client, "QUOTA",
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic void imapc_quota_refresh_init(struct imapc_quota_refresh *refresh)
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen refresh->pool = pool_alloconly_create("imapc quota refresh", 256);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen p_array_init(&refresh->roots, refresh->pool, 4);
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainenimapc_quota_refresh_update(struct quota *quota,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct imapc_quota_refresh_root *refresh_root;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_error("quota: imapc didn't return any QUOTA results");
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* use the first quota root for everything */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if ((*rootp)->backend.name == quota_backend_imapc.name) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen root->root.bytes_limit = refresh_root->bytes_limit;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen root->root.count_limit = refresh_root->count_limit;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenimapc_quota_refresh_deinit(struct quota *quota,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct imapc_quota_refresh *refresh, bool success)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenimapc_quota_refresh_root_order_cmp(const struct imapc_quota_refresh_root *root1,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic int imapc_quota_refresh_mailbox(struct imapc_quota_root *root)
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen /* ask quotas for the configured mailbox */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen imapc_simple_context_init(&sctx, root->client);
1d40278513aafd35dcac239729fd3194833f9674Timo Sirainen imapc_command_sendf(cmd, "GETQUOTAROOT %s", root->box_name);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* if there are multiple quota roots, use the first one returned by
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen the QUOTAROOT */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen array_sort(&root->refresh.roots, imapc_quota_refresh_root_order_cmp);
e3df4d9063a06e0cd228a1713677ec105b0a4aa2Timo Sirainen imapc_quota_refresh_deinit(root->root.quota, &root->refresh,
e3df4d9063a06e0cd228a1713677ec105b0a4aa2Timo Sirainenstatic int imapc_quota_refresh_root(struct imapc_quota_root *root)
1d40278513aafd35dcac239729fd3194833f9674Timo Sirainen /* ask quotas for the configured quota root */
e3df4d9063a06e0cd228a1713677ec105b0a4aa2Timo Sirainen imapc_simple_context_init(&sctx, root->client);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen imapc_command_sendf(cmd, "GETQUOTA %s", root->root_name);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* there shouldn't be more than one QUOTA reply, but ignore anyway
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen anything we didn't expect. */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen while (array_count(&root->refresh.roots) > 0) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct imapc_quota_refresh_root *refresh_root =
c307328f59c963eba21091ecd36c9435d42b47d8Timo Sirainen if (strcmp(refresh_root->name, root->root_name) == 0)
c307328f59c963eba21091ecd36c9435d42b47d8Timo Sirainen imapc_quota_refresh_deinit(root->root.quota, &root->refresh,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic int imapc_quota_refresh(struct imapc_quota_root *root)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* imapc namespace is missing - disable this quota backend */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (root->last_refresh.tv_sec == ioloop_timeval.tv_sec &&
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen root->last_refresh.tv_usec == ioloop_timeval.tv_usec)
985acc0cfd9184b3f4f4cfd6b9e5686a65226147Timo Sirainen if (imapc_client_get_capabilities(root->client->client, &capa) < 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* no QUOTA capability - disable quota */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_warning("quota: Remote IMAP server doesn't support QUOTA - disabling");
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* set the last_refresh only after the refresh, because it changes
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen ioloop_timeval. */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic int imapc_quota_init_limits(struct quota_root *_root)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct imapc_quota_root *root = (struct imapc_quota_root *)_root;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenimapc_quota_namespace_added(struct quota *quota, struct mail_namespace *ns)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen unsigned int i, count;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen roots = array_get_modifiable("a->roots, &count);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen for (i = 0; i < count; i++) {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (roots[i]->backend.name == quota_backend_imapc.name &&
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen imapc_quota_root_namespace_added(roots[i], ns);
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenstatic const char *const *
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenimapc_quota_root_get_resources(struct quota_root *root ATTR_UNUSED)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen static const char *resources_both[] = {
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenimapc_quota_get_resource(struct quota_root *_root, const char *name,
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen struct imapc_quota_root *root = (struct imapc_quota_root *)_root;
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainen else if (strcmp(name, QUOTA_NAME_MESSAGES) == 0)
39ee82dad4d4fa61e3ed074d191afc6a9b82e249Timo Sirainenimapc_quota_update(struct quota_root *root ATTR_UNUSED,