From f65f54b3f697fcd9bfa761ed19b0f5d37465ea94 Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Tue, 23 Jan 2024 10:37:46 +0100 Subject: [PATCH] lib-master: master_service_import_environment_real() - Expand variables in import_environment setting --- src/lib-master/master-service.c | 16 +++++++++++++++- src/lib-master/master-service.h | 7 ++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/lib-master/master-service.c b/src/lib-master/master-service.c index 5dc2d5292a..6b088508fc 100644 --- a/src/lib-master/master-service.c +++ b/src/lib-master/master-service.c @@ -26,6 +26,7 @@ #include "master-service-ssl.h" #include "master-service-settings.h" #include "iostream-ssl.h" +#include "var-expand.h" #include #include @@ -1016,6 +1017,12 @@ static void master_service_import_environment_real(const char *import_environmen { const char *const *envs, *key, *value; ARRAY_TYPE(const_string) keys; + const char *error; + string_t *expanded; + + static const struct var_expand_table table[] = { + { '\0', NULL, NULL }, + }; if (*import_environment == '\0') return; @@ -1033,13 +1040,20 @@ static void master_service_import_environment_real(const char *import_environmen #endif /* add new environments */ envs = t_strsplit_spaces(import_environment, " "); + expanded = t_str_new(64); for (; *envs != NULL; envs++) { value = strchr(*envs, '='); if (value == NULL) key = *envs; else { key = t_strdup_until(*envs, value++); - env_put(key, value); + if (var_expand(expanded, value, table, &error) <= 0) + i_fatal("Cannot expand variable %s", value); + if (str_len(expanded) > 0) { + value = str_c(expanded); + env_put(key, value); + str_clear(expanded); + } } array_push_back(&keys, &key); } diff --git a/src/lib-master/master-service.h b/src/lib-master/master-service.h index d4babfd903..c4dccf761f 100644 --- a/src/lib-master/master-service.h +++ b/src/lib-master/master-service.h @@ -144,9 +144,10 @@ bool master_service_parse_option(struct master_service *service, void master_service_init_finish(struct master_service *service); /* import_environment is a space-separated list of environment keys or - key=values. The key=values are immediately added to the environment. - All the keys are added to DOVECOT_PRESERVE_ENVS environment so they're - preserved by master_service_env_clean(). */ + key=values. If the values contain %variables they are expanded and + immediately added to the environment, this can i_fatal() if the %variables + are invalid. All the keys are added to DOVECOT_PRESERVE_ENVS environment so + they're preserved by master_service_env_clean(). */ void master_service_import_environment(const char *import_environment); /* Clean environment from everything except the ones listed in DOVECOT_PRESERVE_ENVS environment. */ -- 2.47.3