]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-settings, global: Change SETTINGS_EVENT_VAR_EXPAND_* APIs
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Thu, 7 Mar 2024 07:57:16 +0000 (09:57 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:12 +0000 (12:34 +0200)
These now work with struct var_expand_params, which allows more flexibility.

src/auth/auth-request-var-expand.c
src/auth/auth-request.c
src/lib-settings/settings.c
src/lib-settings/settings.h
src/lib-storage/index/shared/shared-storage.c
src/lib-storage/mail-storage-service.c
src/lib-storage/mail-user.c
src/lmtp/lmtp-client.c
src/login-common/client-common.c

index 7dbf439fd9fe3cdf7a1f2739c63911241de905eb..32d654ed9373ecc66ebfcaa53d52907d0aaba95a 100644 (file)
@@ -311,16 +311,15 @@ int t_auth_request_var_expand(const char *str,
 }
 
 static void
-auth_request_event_var_expand_callback(struct event *event,
-       const struct var_expand_table **tab_r,
-       const struct var_expand_func_table **func_tab_r)
+auth_request_event_var_expand_callback(void *context,
+                                      struct var_expand_params *params_r)
 {
-       struct auth_request_var_expand_ctx *ctx =
-               event_get_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT);
+       struct auth_request_var_expand_ctx *ctx = context;
 
-       *tab_r = auth_request_get_var_expand_table(ctx->auth_request,
-                                                  ctx->escape_func);
-       *func_tab_r = auth_request_var_funcs_table;
+       params_r->table = auth_request_get_var_expand_table(ctx->auth_request,
+                                                           ctx->escape_func);
+       params_r->func_table = auth_request_var_funcs_table;
+       params_r->func_context = ctx;
 }
 
 void auth_request_event_set_var_expand(struct auth_request *auth_request)
@@ -333,5 +332,5 @@ void auth_request_event_set_var_expand(struct auth_request *auth_request)
        event_set_ptr(auth_request->event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK,
                      auth_request_event_var_expand_callback);
        event_set_ptr(auth_request->event,
-                     SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT, ctx);
+                     SETTINGS_EVENT_VAR_EXPAND_CALLBACK_CONTEXT, ctx);
 }
index 4fa80365d04b4a3402b3bd7ecb4c8a9a85487994..02d1462ad1a991845f3f38f23a9cfea08e08bcdc 100644 (file)
@@ -842,10 +842,11 @@ int auth_request_set_passdb_fields(struct auth_request *request,
                { driver_name, auth_request_fields_var_expand_lookup },
                { NULL, NULL }
        };
-
-       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_TABLE,
-                     auth_request_fields_funcs);
-       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT, fields);
+       struct var_expand_params params = {
+               .func_table = auth_request_fields_funcs,
+               .func_context = fields,
+       };
+       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_PARAMS, &params);
 
        if (settings_get(event, &auth_passdb_post_setting_parser_info, 0,
                         &post_set, &error) < 0) {
@@ -873,10 +874,11 @@ int auth_request_set_userdb_fields(struct auth_request *request,
                { driver_name, auth_request_fields_var_expand_lookup },
                { NULL, NULL }
        };
-
-       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_TABLE,
-                     auth_request_fields_funcs);
-       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT, fields);
+       struct var_expand_params params = {
+               .func_table = auth_request_fields_funcs,
+               .func_context = fields,
+       };
+       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_PARAMS, &params);
 
        if (settings_get(event, &auth_userdb_post_setting_parser_info, 0,
                         &post_set, &error) < 0) {
index 902b7b5c327ee7bf84fb4a913dd841d5738be228..90c689fc3cb95445966ab98a0c4f47510fbf5d1f 100644 (file)
@@ -142,8 +142,7 @@ struct settings_apply_ctx {
        ARRAY_TYPE(settings_override_p) overrides;
 
        string_t *str;
-       const struct var_expand_table *const *tables;
-       const struct var_expand_params_func *funcs;
+       struct var_expand_params var_params;
 };
 
 static ARRAY(const struct setting_parser_info *) set_registered_infos;
@@ -531,8 +530,7 @@ settings_mmap_apply_key(struct settings_apply_ctx *ctx, unsigned int key_idx,
                        /* Make sure only the file path is var-expanded. */
                        value = file.path;
                }
-               if (var_expand_with_arrays(ctx->str, value, ctx->tables,
-                                          ctx->funcs, &error) <= 0 &&
+               if (var_expand(ctx->str, value, &ctx->var_params, &error) <= 0 &&
                    (ctx->flags & SETTINGS_GET_FLAG_FAKE_EXPAND) == 0) {
                        *error_r = t_strdup_printf(
                                "Failed to expand %s setting variables: %s",
@@ -598,8 +596,7 @@ settings_mmap_apply_defaults(struct settings_apply_ctx *ctx,
                if ((ctx->flags & SETTINGS_GET_FLAG_NO_EXPAND) == 0) {
                        const char *error;
                        str_truncate(ctx->str, 0);
-                       if (var_expand_with_arrays(ctx->str, value, ctx->tables,
-                                                  ctx->funcs, &error) <= 0 &&
+                       if (var_expand(ctx->str, value, &ctx->var_params, &error) <= 0 &&
                            (ctx->flags & SETTINGS_GET_FLAG_FAKE_EXPAND) == 0) {
                                *error_r = t_strdup_printf(
                                        "Failed to expand default setting %s=%s variables: %s",
@@ -1043,53 +1040,68 @@ settings_mmap_pool_create(struct settings_root *root,
        return mpool;
 }
 
+struct settings_var_expand_init_ctx {
+       ARRAY(const struct var_expand_table *) tables;
+       ARRAY(struct var_expand_params_func) funcs;
+};
+
+static void
+settings_var_expand_init_add(struct settings_var_expand_init_ctx *init_ctx,
+                            const struct var_expand_params *params)
+{
+       if (params->table != NULL)
+               array_push_back(&init_ctx->tables, &params->table);
+       if (params->func_table != NULL) {
+               struct var_expand_params_func *func =
+                       array_append_space(&init_ctx->funcs);
+               func->table = params->func_table;
+               func->context = params->func_context;
+       }
+       if (params->tables_arr != NULL) {
+               for (unsigned int i = 0; params->tables_arr[i] != NULL; i++)
+                       array_push_back(&init_ctx->tables, &params->tables_arr[i]);
+       }
+       if (params->funcs_arr != NULL) {
+               for (unsigned int i = 0; params->funcs_arr[i].table != NULL; i++)
+                       array_push_back(&init_ctx->funcs, &params->funcs_arr[i]);
+       }
+}
+
 static void
 settings_var_expand_init(struct settings_apply_ctx *ctx)
 {
        struct event *event = ctx->event;
-       ARRAY(const struct var_expand_table *) tables;
-       ARRAY(struct var_expand_params_func) funcs;
-       const struct var_expand_table *table;
-       const struct var_expand_func_table *func_table;
+       struct settings_var_expand_init_ctx init_ctx;
+       struct var_expand_params tmp_params;
 
-       t_array_init(&tables, 4);
-       t_array_init(&funcs, 4);
+       i_zero(&init_ctx);
+       t_array_init(&init_ctx.tables, 4);
+       t_array_init(&init_ctx.funcs, 4);
 
        while (event != NULL) {
                settings_var_expand_t *callback =
                        event_get_ptr(event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK);
                if (callback != NULL) {
-                       callback(event, &table, &func_table);
-                       if (table != NULL)
-                               array_push_back(&tables, &table);
-                       if (func_table != NULL) {
-                               struct var_expand_params_func *func =
-                                       array_append_space(&funcs);
-                               func->table = func_table;
-                               func->context = event_get_ptr(event,
-                                       SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT);
-                       }
+                       void *callback_context = event_get_ptr(event,
+                               SETTINGS_EVENT_VAR_EXPAND_CALLBACK_CONTEXT);
+                       i_zero(&tmp_params);
+                       callback(callback_context, &tmp_params);
+                       settings_var_expand_init_add(&init_ctx, &tmp_params);
                }
 
-               table = event_get_ptr(event, SETTINGS_EVENT_VAR_EXPAND_TABLE);
-               if (table != NULL)
-                       array_push_back(&tables, &table);
-
-               func_table = event_get_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_TABLE);
-               if (func_table != NULL) {
-                       struct var_expand_params_func *func =
-                               array_append_space(&funcs);
-                       func->table = func_table;
-                       func->context = event_get_ptr(event,
-                               SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT);
-               }
+               struct var_expand_params *params =
+                       event_get_ptr(event, SETTINGS_EVENT_VAR_EXPAND_PARAMS);
+               if (params != NULL)
+                       settings_var_expand_init_add(&init_ctx, params);
 
                event = event_get_parent(event);
        }
-       array_append_zero(&funcs);
 
-       ctx->tables = array_front(&tables);
-       ctx->funcs = array_front(&funcs);
+       array_append_zero(&init_ctx.tables);
+       array_append_zero(&init_ctx.funcs);
+
+       ctx->var_params.tables_arr = array_front(&init_ctx.tables);
+       ctx->var_params.funcs_arr = array_front(&init_ctx.funcs);
 }
 
 static int settings_override_cmp(struct settings_override *const *set1,
@@ -1514,8 +1526,7 @@ settings_instance_override(struct settings_apply_ctx *ctx,
                           or with SETTINGS_OVERRIDE_TYPE_2ND_DEFAULT. */
                        const char *error;
                        str_truncate(ctx->str, 0);
-                       if (var_expand_with_arrays(ctx->str, value, ctx->tables,
-                                                  ctx->funcs, &error) <= 0 &&
+                       if (var_expand(ctx->str, value, &ctx->var_params, &error) <= 0 &&
                            (ctx->flags & SETTINGS_GET_FLAG_FAKE_EXPAND) == 0) {
                                *error_r = t_strdup_printf(
                                        "Failed to expand default setting %s=%s variables: %s",
index ea21fc3e3438825a6849d58105171dd7b45f8c30..bbf38b7478c4bd86854eb5f207b267268e4f82fb 100644 (file)
@@ -3,8 +3,7 @@
 
 #include "settings-parser.h"
 
-struct var_expand_table;
-struct var_expand_func_table;
+struct var_expand_params;
 
 struct settings_root;
 struct settings_mmap;
@@ -81,47 +80,33 @@ enum settings_get_flags {
 #define SETTINGS_EVENT_MAILBOX_NAME_WITH_PREFIX "mailbox"
 #define SETTINGS_EVENT_MAILBOX_NAME_WITHOUT_PREFIX "mailbox_subname"
 
-/* Set struct var_expand_table to be used for settings expansion. The table is
-   expected to be accessible until the event is freed or the table is cleared
+/* Set struct var_expand_params to be used for settings expansion. The struct is
+   expected to be accessible until the event is freed or the params is removed
    from the event. Usage:
 
-   event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_TABLE, var_expand_table);
-*/
-#define SETTINGS_EVENT_VAR_EXPAND_TABLE \
-       "settings_var_expand_table"
-/* Set struct var_expand_func_table and its function context pointer to be used
-   for settings expansion. The table is expected to be accessible until the
-   event is freed or the table is cleared from the event. Usage:
-
-   event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_TABLE, func_table);
-   event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT, func_context);
-
-   You can set any combination of SETTINGS_EVENT_VAR_EXPAND_TABLE,
-   SETTINGS_EVENT_VAR_EXPAND_FUNC_TABLE and SETTINGS_EVENT_VAR_EXPAND_CALLBACK
-   to the same event or parent events. They are all merged while expanding
-   the variables. */
-#define SETTINGS_EVENT_VAR_EXPAND_FUNC_TABLE \
-       "settings_var_expand_func_table"
-#define SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT \
-       "settings_var_expand_func_context"
-
-/* Set a settings_var_expand_t callback that returns
-   var_expand_[func_]table for settings expansion. This can be used instead of
-   SETTINGS_EVENT_VAR_EXPAND_[FUNC_]TABLE to dynamically generate the table
+   event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_PARAMS, var_expand_params);
+
+   You can set any combination of SETTINGS_EVENT_VAR_EXPAND_PARAMS
+   and SETTINGS_EVENT_VAR_EXPAND_CALLBACK to the same event or parent events.
+   They are all merged while expanding the variables. */
+#define SETTINGS_EVENT_VAR_EXPAND_PARAMS \
+       "settings_var_expand_params"
+
+/* Set a settings_var_expand_t callback that returns var_expand_params for
+   settings expansion. This can be used instead of
+   SETTINGS_EVENT_VAR_EXPAND_PARAMS to dynamically generate the tables
    on-demand. Usage:
 
    event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK, callback);
-   event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT, func_context);
+   event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK_CONTEXT, context);
 */
 #define SETTINGS_EVENT_VAR_EXPAND_CALLBACK \
        "settings_var_expand_callback"
-/* Callback function used with SETTINGS_EVENT_VAR_EXPAND_CALLBACK. The function
-   can return either or both of tab_r and func_tab_r, using NULL for the field
-   that isn't needed. */
+#define SETTINGS_EVENT_VAR_EXPAND_CALLBACK_CONTEXT \
+       "settings_var_expand_callback_context"
+/* Callback function used with SETTINGS_EVENT_VAR_EXPAND_CALLBACK. */
 typedef void
-settings_var_expand_t(struct event *event,
-                     const struct var_expand_table **tab_r,
-                     const struct var_expand_func_table **func_tab_r);
+settings_var_expand_t(void *context, struct var_expand_params *params_r);
 
 /* Get the wanted settings and check that the settings are valid.
    The settings struct must have pool_t (info->pool_offset1), which the caller
index e4697051ce43f4715adf05b9edf139d580f005fb..af32e7322810f425b8444e8631aa80ca5d5f12d8 100644 (file)
@@ -342,14 +342,15 @@ shared_mail_user_init(struct mail_storage *_storage,
                { "owner_home", shared_mail_user_var_home },
                { NULL, NULL }
        };
+       struct var_expand_params *params =
+               p_new(user->pool, struct var_expand_params, 1);
+       params->table = tab;
+       params->func_table = func_tab;
+       params->func_context = &var_expand_ctx;
 
        struct event *set_event = event_create(user->event);
        event_add_str(set_event, SETTINGS_EVENT_NAMESPACE_NAME, ns->set->name);
-       event_set_ptr(set_event, SETTINGS_EVENT_VAR_EXPAND_TABLE, tab);
-       event_set_ptr(set_event, SETTINGS_EVENT_VAR_EXPAND_FUNC_TABLE,
-                     func_tab);
-       event_set_ptr(set_event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT,
-                     &var_expand_ctx);
+       event_set_ptr(set_event, SETTINGS_EVENT_VAR_EXPAND_PARAMS, params);
 
        /* Expanding mail_path may verify whether the user exists by
           trying to access %{owner_home}. This sets
index 727ea21c5b3e12ac5dc0a4cd0e2d2e4245bddd49..fad0619d617e59da544b1042ddf3708ade84e36d 100644 (file)
@@ -799,18 +799,16 @@ mail_storage_service_var_expand_func_table[] = {
 };
 
 static void
-mail_storage_service_var_expand_callback(struct event *event,
-                                        const struct var_expand_table **tab_r,
-                                        const struct var_expand_func_table **func_tab_r)
+mail_storage_service_var_expand_callback(void *context,
+                                        struct var_expand_params *params_r)
 {
-       struct mail_storage_service_init_var_expand_ctx *var_expand_ctx =
-               event_get_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT);
-       i_assert(var_expand_ctx != NULL);
-
-       *tab_r = get_var_expand_table(var_expand_ctx->ctx->service,
-                                     var_expand_ctx->user,
-                                     var_expand_ctx->input);
-       *func_tab_r = mail_storage_service_var_expand_func_table;
+       struct mail_storage_service_init_var_expand_ctx *var_expand_ctx = context;
+
+       params_r->table = get_var_expand_table(var_expand_ctx->ctx->service,
+                                              var_expand_ctx->user,
+                                              var_expand_ctx->input);
+       params_r->func_table = mail_storage_service_var_expand_func_table;
+       params_r->func_context = var_expand_ctx;
 }
 
 const char *
@@ -1145,7 +1143,7 @@ mail_storage_service_lookup_real(struct mail_storage_service_ctx *ctx,
           settings lookups are expected to be using mail_user.event. */
        event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK,
                      mail_storage_service_var_expand_callback);
-       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT,
+       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK_CONTEXT,
                      &var_expand_ctx);
        if (settings_get(event, &mail_user_setting_parser_info,
                         0, &user_set, error_r) < 0) {
@@ -1304,7 +1302,7 @@ mail_storage_service_lookup_real(struct mail_storage_service_ctx *ctx,
                /* The context points to a variable in stack, so it can't be
                   used anymore. */
                event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK, NULL);
-               event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT, NULL);
+               event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK_CONTEXT, NULL);
        }
        *user_r = user;
        return ret;
index 393f7a1e9d7ecd3b304b38474815ba04a4e1027e..e46c80b1307b032d58e47cb7d1d26e8eac78c9b7 100644 (file)
@@ -60,16 +60,13 @@ void mail_user_add_event_fields(struct mail_user *user)
 }
 
 static void
-mail_user_var_expand_callback(struct event *event,
-                             const struct var_expand_table **tab_r,
-                             const struct var_expand_func_table **func_tab_r)
+mail_user_var_expand_callback(void *context, struct var_expand_params *params_r)
 {
-       struct mail_user *user =
-               event_get_ptr(event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT);
-       i_assert(user != NULL);
+       struct mail_user *user = context;
 
-       *tab_r = mail_user_var_expand_table(user);
-       *func_tab_r = mail_user_var_expand_func_table;
+       params_r->table = mail_user_var_expand_table(user);
+       params_r->func_table = mail_user_var_expand_func_table;
+       params_r->func_context = user;
 }
 
 struct mail_user *
@@ -102,7 +99,8 @@ mail_user_alloc(struct mail_storage_service_user *service_user)
           lookups. */
        event_set_ptr(user->event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK,
                      mail_user_var_expand_callback);
-       event_set_ptr(user->event, SETTINGS_EVENT_VAR_EXPAND_FUNC_CONTEXT, user);
+       event_set_ptr(user->event,
+                     SETTINGS_EVENT_VAR_EXPAND_CALLBACK_CONTEXT, user);
 
        user->v.deinit = mail_user_deinit_base;
        user->v.deinit_pre = mail_user_deinit_pre_base;
index 7d0f7e3e4189403ecabf43d931e82e673f2d201b..9e5e0fcd8118fe288f4d772e170a27bb827c5258 100644 (file)
@@ -123,11 +123,12 @@ static void client_read_settings(struct client *client, bool ssl)
        client->raw_mail_user =
                raw_storage_create_from_set(storage_service, client->set_instance);
 
-       const struct var_expand_table *tab =
-               mail_storage_service_get_var_expand_table(storage_service, &input);
+       struct var_expand_params params = {
+               .table = mail_storage_service_get_var_expand_table(storage_service, &input),
+       };
 
        struct event *event = event_create(client->event);
-       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_TABLE, (void *)tab);
+       event_set_ptr(event, SETTINGS_EVENT_VAR_EXPAND_PARAMS, &params);
        if (settings_get(event, &lda_setting_parser_info, 0,
                         &client->lda_set, &error) < 0 ||
            settings_get(event, &lmtp_setting_parser_info, 0,
index d5782b6de063f9439fd076885f3216bcee7c840b..ef4b5d79daddaf84d4da44d1aed2d3c96ca16ae4 100644 (file)
@@ -199,13 +199,10 @@ static bool client_is_trusted(struct client *client)
 }
 
 static void
-client_var_expand_callback(struct event *event,
-                          const struct var_expand_table **tab_r,
-                          const struct var_expand_func_table **func_tab_r ATTR_UNUSED)
+client_var_expand_callback(void *context, struct var_expand_params *params_r)
 {
-       struct client *client = event_get_ptr(event, "client");
-
-       *tab_r = get_var_expand_table(client);
+       struct client *client = context;
+       params_r->table = get_var_expand_table(client);
 }
 
 static int client_settings_get(struct client *client, const char **error_r)
@@ -270,7 +267,8 @@ int client_alloc(int fd, const struct master_service_connection *conn,
        /* Get settings before using log callback */
        event_set_ptr(client->event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK,
                      client_var_expand_callback);
-       event_set_ptr(client->event, "client", client);
+       event_set_ptr(client->event, SETTINGS_EVENT_VAR_EXPAND_CALLBACK_CONTEXT,
+                     client);
        if (client_settings_get(client, &error) < 0) {
                e_error(client->event, "%s", error);
                event_unref(&client->event);