doveadm-quota.c revision de88de2e8c616af3cf6f5ab20035791a71ce7746
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen/* Copyright (c) 2005-2010 Dovecot authors, see the included COPYING file */
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen#include "lib.h"
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen#include "module-dir.h"
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen#include "quota-plugin.h"
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen#include "quota-private.h"
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen#include "doveadm-mail.h"
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen#include <stdio.h>
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainenconst char *doveadm_quota_plugin_version = DOVECOT_VERSION;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainenvoid doveadm_quota_plugin_init(struct module *module);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainenvoid doveadm_quota_plugin_deinit(void);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainenstatic void cmd_quota_get_root(struct mail_user *user, struct quota_root *root)
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen{
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen const char *const *res;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen uint64_t value, limit;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen int ret;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen printf("%s(%s): ", user->username, root->set->name);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen res = quota_root_get_resources(root);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen for (; *res != NULL; res++) {
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen ret = quota_get_resource(root, "", *res, &value, &limit);
18792f1110ecd1fca5306f3be96fcbe887f6dbccTimo Sirainen printf("%s ", *res);
18792f1110ecd1fca5306f3be96fcbe887f6dbccTimo Sirainen if (ret > 0) {
18792f1110ecd1fca5306f3be96fcbe887f6dbccTimo Sirainen printf("%llu/%llu",
18792f1110ecd1fca5306f3be96fcbe887f6dbccTimo Sirainen (unsigned long long)value,
18792f1110ecd1fca5306f3be96fcbe887f6dbccTimo Sirainen (unsigned long long)limit);
de88de2e8c616af3cf6f5ab20035791a71ce7746Timo Sirainen } else if (ret == 0) {
de88de2e8c616af3cf6f5ab20035791a71ce7746Timo Sirainen printf("%llu/unlimited",
de88de2e8c616af3cf6f5ab20035791a71ce7746Timo Sirainen (unsigned long long)value);
de88de2e8c616af3cf6f5ab20035791a71ce7746Timo Sirainen } else
18792f1110ecd1fca5306f3be96fcbe887f6dbccTimo Sirainen printf("error");
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen if (res[1] != NULL)
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen printf(", ");
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen }
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen printf("\n");
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen}
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainenstatic void
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainencmd_quota_get_run(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen struct mail_user *user)
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen{
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen struct quota_user *quser = QUOTA_USER_CONTEXT(user);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen struct quota_root *const *root;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen array_foreach(&quser->quota->roots, root)
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen cmd_quota_get_root(user, *root);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen}
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainenstatic struct doveadm_mail_cmd_context *
bc10373fc050eb9dd23f6ed5ee8207d0e4d142eeTimo Sirainencmd_quota_get_alloc(void)
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen{
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen struct doveadm_mail_cmd_context *ctx;
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen
bc10373fc050eb9dd23f6ed5ee8207d0e4d142eeTimo Sirainen ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen ctx->run = cmd_quota_get_run;
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen return ctx;
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen}
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainenstatic void
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainencmd_quota_recalc_run(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen struct mail_user *user)
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen{
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen struct quota_user *quser = QUOTA_USER_CONTEXT(user);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen struct quota_root *const *root;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen struct quota_transaction_context trans;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen memset(&trans, 0, sizeof(trans));
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen trans.quota = quser->quota;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen trans.recalculate = TRUE;
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen array_foreach(&quser->quota->roots, root)
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen (void)(*root)->backend.v.update(*root, &trans);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen}
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainenstatic struct doveadm_mail_cmd_context *
bc10373fc050eb9dd23f6ed5ee8207d0e4d142eeTimo Sirainencmd_quota_recalc_alloc(void)
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen{
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen struct doveadm_mail_cmd_context *ctx;
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen
bc10373fc050eb9dd23f6ed5ee8207d0e4d142eeTimo Sirainen ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen ctx->run = cmd_quota_recalc_run;
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen return ctx;
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen}
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainenstatic struct doveadm_mail_cmd quota_commands[] = {
bc10373fc050eb9dd23f6ed5ee8207d0e4d142eeTimo Sirainen { cmd_quota_get_alloc, "quota get", NULL },
bc10373fc050eb9dd23f6ed5ee8207d0e4d142eeTimo Sirainen { cmd_quota_recalc_alloc, "quota recalc", NULL }
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen};
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainenvoid doveadm_quota_plugin_init(struct module *module ATTR_UNUSED)
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen{
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen unsigned int i;
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen for (i = 0; i < N_ELEMENTS(quota_commands); i++)
bc85bdcdebb2224e45e6c2bbc288e9ce59f651d9Timo Sirainen doveadm_mail_register_cmd(&quota_commands[i]);
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen}
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainenvoid doveadm_quota_plugin_deinit(void)
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen{
9563e6542b7a8a13b13a0ef62ec68b0383b99293Timo Sirainen}