From: Timo Sirainen Date: Mon, 26 Aug 2024 09:19:42 +0000 (+0300) Subject: quota: quota-status - Convert plugin settings to global settings X-Git-Tag: 2.4.1~651 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f007b97f2dc077aaeb81dcf7e29477d1ba0b197c;p=thirdparty%2Fdovecot%2Fcore.git quota: quota-status - Convert plugin settings to global settings --- diff --git a/src/plugins/quota/quota-status-settings.c b/src/plugins/quota/quota-status-settings.c index c26517038a..6820df3ed1 100644 --- a/src/plugins/quota/quota-status-settings.c +++ b/src/plugins/quota/quota-status-settings.c @@ -12,12 +12,14 @@ static const struct setting_define quota_status_setting_defines[] = { DEF(STR, recipient_delimiter), + DEF(STR, quota_status_nouser), SETTING_DEFINE_LIST_END }; static const struct quota_status_settings quota_status_default_settings = { .recipient_delimiter = "+", + .quota_status_nouser = "REJECT Unknown user", }; const struct setting_parser_info quota_status_setting_parser_info = { diff --git a/src/plugins/quota/quota-status-settings.h b/src/plugins/quota/quota-status-settings.h index 4782ff639c..cb399fa5c0 100644 --- a/src/plugins/quota/quota-status-settings.h +++ b/src/plugins/quota/quota-status-settings.h @@ -4,6 +4,7 @@ struct quota_status_settings { pool_t pool; const char *recipient_delimiter; + const char *quota_status_nouser; }; extern const struct setting_parser_info quota_status_setting_parser_info; diff --git a/src/plugins/quota/quota-status.c b/src/plugins/quota/quota-status.c index 47a0d84cdb..74c75dd057 100644 --- a/src/plugins/quota/quota-status.c +++ b/src/plugins/quota/quota-status.c @@ -35,6 +35,39 @@ struct quota_client { bool warned_bad_state:1; }; +struct quota_status_result_settings { + pool_t pool; + + const char *quota_status_success; + const char *quota_status_toolarge; + const char *quota_status_overquota; +}; + +#undef DEF +#define DEF(type, name) \ + SETTING_DEFINE_STRUCT_##type(#name, name, struct quota_status_result_settings) +static const struct setting_define quota_status_result_setting_defines[] = { + DEF(STR, quota_status_success), + DEF(STR, quota_status_toolarge), + DEF(STR, quota_status_overquota), + + SETTING_DEFINE_LIST_END +}; + +static const struct quota_status_result_settings quota_status_result_default_settings = { + .quota_status_success = "OK", + .quota_status_toolarge = "", + .quota_status_overquota = "554 5.2.2 %{error}", +}; + +const struct setting_parser_info quota_status_result_setting_parser_info = { + .name = "quota_status_result", + .defines = quota_status_result_setting_defines, + .defaults = "a_status_result_default_settings, + .struct_size = sizeof(struct quota_status_result_settings), + .pool_offset1 = 1 + offsetof(struct quota_status_result_settings, pool), +}; + static struct event_category event_category_quota_status = { .name = "quota-status" }; @@ -43,7 +76,6 @@ static const struct quota_status_settings *quota_status_settings; static enum quota_protocol protocol; static struct mail_storage_service_ctx *storage_service; static struct connection_list *clients; -static char *nouser_reply; static void client_connected(struct master_service_connection *conn) { @@ -164,7 +196,7 @@ static void client_handle_request(struct quota_client *client) restrict_access_allow_coredumps(TRUE); if (ret == 0) { e_debug(client->event, "User `%s' not found", input.username); - value = nouser_reply; + value = quota_status_settings->quota_status_nouser; } else if (ret > 0) { enum quota_alloc_result qret = quota_check(user, client->size, &error); @@ -176,33 +208,48 @@ static void client_handle_request(struct quota_client *client) "Quota check failed: %s", error); } - switch (qret) { - case QUOTA_ALLOC_RESULT_OK: /* under quota */ - value = mail_user_plugin_getenv(user, - "quota_status_success"); - if (value == NULL) - value = "OK"; - break; - case QUOTA_ALLOC_RESULT_OVER_MAXSIZE: - /* even over maximum quota */ - case QUOTA_ALLOC_RESULT_OVER_QUOTA_LIMIT: - value = mail_user_plugin_getenv(user, - "quota_status_toolarge"); - /* fall through */ - case QUOTA_ALLOC_RESULT_OVER_QUOTA: - case QUOTA_ALLOC_RESULT_OVER_QUOTA_MAILBOX_LIMIT: - if (value == NULL) - value = mail_user_plugin_getenv(user, - "quota_status_overquota"); - if (value == NULL) - value = t_strdup_printf("554 5.2.2 %s", error); - break; - case QUOTA_ALLOC_RESULT_TEMPFAIL: - case QUOTA_ALLOC_RESULT_BACKGROUND_CALC: + struct event *event = event_create(client->event); + const struct var_expand_table table[] = { + { '\0', error, "error" }, + { '\0', NULL, NULL } + }; + struct var_expand_params params = { + .table = table, + }; + const struct quota_status_result_settings *set; + const char *set_error; + event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_PARAMS, ¶ms); + + if (settings_get(event, "a_status_result_setting_parser_info, + 0, &set, &set_error) < 0) { + e_error(client->event, "%s", set_error); + error = "Temporary internal error"; ret = -1; - break; + } else { + switch (qret) { + case QUOTA_ALLOC_RESULT_OK: /* under quota */ + value = set->quota_status_success; + break; + case QUOTA_ALLOC_RESULT_OVER_MAXSIZE: + /* even over maximum quota */ + case QUOTA_ALLOC_RESULT_OVER_QUOTA_LIMIT: + value = set->quota_status_toolarge; + if (value[0] == '\0') + break; + /* fall through */ + case QUOTA_ALLOC_RESULT_OVER_QUOTA: + case QUOTA_ALLOC_RESULT_OVER_QUOTA_MAILBOX_LIMIT: + value = set->quota_status_overquota; + break; + case QUOTA_ALLOC_RESULT_TEMPFAIL: + case QUOTA_ALLOC_RESULT_BACKGROUND_CALC: + ret = -1; + break; + } + value = t_strdup(value); + settings_free(set); } - value = t_strdup(value); /* user's pool is being freed */ + event_unref(&event); mail_user_deinit(&user); } else { e_error(client->event, @@ -291,8 +338,6 @@ static void main_preinit(void) static void main_init(void) { struct mail_storage_service_input input; - const struct mail_storage_settings *mail_set; - const char *value, *error; clients = connection_list_init(&client_set, &client_vfuncs); storage_service = mail_storage_service_init(master_service, @@ -306,23 +351,14 @@ static void main_init(void) input.service = "quota-status"; input.username = ""; - if (settings_get(master_service_get_event(master_service), - &mail_storage_setting_parser_info, - SETTINGS_GET_FLAG_NO_EXPAND, &mail_set, &error) < 0) - i_fatal("%s", error); quota_status_settings = settings_get_or_fatal( master_service_get_event(master_service), "a_status_setting_parser_info); - - value = mail_user_set_plugin_getenv(mail_set, "quota_status_nouser"); - nouser_reply = i_strdup(value != NULL ? value : "REJECT Unknown user"); - settings_free(mail_set); } static void main_deinit(void) { settings_free(quota_status_settings); - i_free(nouser_reply); connection_list_deinit(&clients); mail_storage_service_deinit(&storage_service); }