]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-master: master_service_import_environment_real() - Expand variables in import_env...
authorKarl Fleischmann <karl.fleischmann@open-xchange.com>
Tue, 23 Jan 2024 09:37:46 +0000 (10:37 +0100)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:12 +0000 (12:34 +0200)
src/lib-master/master-service.c
src/lib-master/master-service.h

index 5dc2d5292abbffaa3ef75bc2074d9f1089d8a17b..6b088508fc0ae30fed2003c1e7fe942b7ae81bf8 100644 (file)
@@ -26,6 +26,7 @@
 #include "master-service-ssl.h"
 #include "master-service-settings.h"
 #include "iostream-ssl.h"
+#include "var-expand.h"
 
 #include <getopt.h>
 #include <unistd.h>
@@ -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);
        }
index d4babfd90340bd043d3ecb0be069313145f9a24a..c4dccf761f8bb64725c4d273d0f2bf97dcce9c7a 100644 (file)
@@ -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. */