dovecot-1.0: %variables in plugin {} settings need to be expanded.

dovecot at dovecot.org dovecot at dovecot.org
Thu Jul 5 18:16:32 EEST 2007


details:   http://hg.dovecot.org/dovecot-1.0/rev/f0bf2439bc09
changeset: 5337:f0bf2439bc09
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Jul 05 18:16:26 2007 +0300
description:
%variables in plugin {} settings need to be expanded.

diffstat:

1 file changed, 66 insertions(+), 23 deletions(-)
src/deliver/deliver.c |   89 ++++++++++++++++++++++++++++++++++++-------------

diffs (160 lines):

diff -r de5d9ffa2a45 -r f0bf2439bc09 src/deliver/deliver.c
--- a/src/deliver/deliver.c	Wed Jul 04 17:48:42 2007 +0300
+++ b/src/deliver/deliver.c	Thu Jul 05 18:16:26 2007 +0300
@@ -3,6 +3,7 @@
 #include "lib.h"
 #include "lib-signals.h"
 #include "ioloop.h"
+#include "array.h"
 #include "hostpid.h"
 #include "home-expand.h"
 #include "env-util.h"
@@ -55,6 +56,9 @@ static struct module *modules;
 static struct module *modules;
 static struct ioloop *ioloop;
 
+static pool_t plugin_pool;
+static array_t ARRAY_DEFINE(plugin_envs, const char *);
+
 static void sig_die(int signo, void *context __attr_unused__)
 {
 	/* warn about being killed because of some signal, except SIGINT (^C)
@@ -205,8 +209,12 @@ static void config_file_init(const char 
 	struct istream *input;
 	const char *key, *value;
 	char *line, *p, quote;
-	int fd, sections = 0, lda_section = FALSE, pop3_section = FALSE;
+	int fd, sections = 0;
+	bool lda_section = FALSE, pop3_section = FALSE, plugin_section = FALSE;
 	size_t len;
+
+	plugin_pool = pool_alloconly_create("Plugin strings", 512);
+	ARRAY_CREATE(&plugin_envs, default_pool, const char *, 16);
 
 	fd = open(path, O_RDONLY);
 	if (fd < 0)
@@ -250,16 +258,19 @@ static void config_file_init(const char 
 		value = p = strchr(line, '=');
 		if (value == NULL) {
 			if (strchr(line, '{') != NULL) {
-				if (strcmp(line, "protocol lda {") == 0 ||
-				    strcmp(line, "plugin {") == 0)
+				if (strcmp(line, "protocol lda {") == 0)
 					lda_section = TRUE;
-				if (strcmp(line, "protocol pop3 {") == 0)
+				else if (strcmp(line, "plugin {") == 0) {
+					plugin_section = TRUE;
+					lda_section = TRUE;
+				} else if (strcmp(line, "protocol pop3 {") == 0)
 					pop3_section = TRUE;
 				sections++;
 			}
 			if (*line == '}') {
 				sections--;
 				lda_section = FALSE;
+				plugin_section = FALSE;
 				pop3_section = FALSE;
 			}
 			continue;
@@ -297,7 +308,16 @@ static void config_file_init(const char 
 			continue;
 		}
 
-		env_put(t_strconcat(t_str_ucase(key), "=", value, NULL));
+		if (!plugin_section) {
+			env_put(t_strconcat(t_str_ucase(key), "=",
+					    value, NULL));
+		} else {
+			/* %variables need to be expanded.
+			   store these for later. */
+			value = p_strconcat(plugin_pool,
+					    t_str_ucase(key), "=", value, NULL);
+			array_append(&plugin_envs, &value, 1);
+		}
 	}
 	i_stream_unref(&input);
 	t_pop();
@@ -465,6 +485,44 @@ static void open_logfile(const char *use
 	i_set_failure_timestamp_format(stamp);
 }
 
+static const char *expand_envs(const char *destination)
+{
+        const struct var_expand_table *table;
+	const char *mail_env, *const *envs;
+	unsigned int i, count;
+	string_t *str;
+
+	str = t_str_new(256);
+	table = get_var_expand_table(destination, getenv("HOME"));
+	envs = array_get(&plugin_envs, &count);
+	for (i = 0; i < count; i++) {
+		str_truncate(str, 0);
+		var_expand(str, envs[i], table);
+		env_put(str_c(str));
+	}
+
+	/* get the table again in case plugin provided the home directory
+	   (yea, kludgy) */
+	table = get_var_expand_table(destination, getenv("HOME"));
+
+	/* MAIL comes from userdb, MAIL_LOCATION from dovecot.conf.
+	   We don't want to expand settings coming from userdb. */
+	mail_env = getenv("MAIL");
+	if (mail_env == NULL)  {
+		mail_env = getenv("MAIL_LOCATION");
+		if (mail_env == NULL)  {
+			/* Keep this for backwards compatibility */
+			mail_env = getenv("DEFAULT_MAIL_ENV");
+		}
+		if (mail_env != NULL) {
+			table = get_var_expand_table(destination,
+						     getenv("HOME"));
+			mail_env = expand_mail_env(mail_env, table);
+		}
+	}
+	return mail_env;
+}
+
 static void print_help(void)
 {
 	printf(
@@ -478,8 +536,7 @@ int main(int argc, char *argv[])
 	const char *envelope_sender = DEFAULT_ENVELOPE_SENDER;
 	const char *mailbox = "INBOX";
 	const char *auth_socket, *env_tz;
-	const char *home, *destination, *user, *mail_env, *value;
-        const struct var_expand_table *table;
+	const char *home, *destination, *user, *value, *mail_env;
         enum mail_storage_flags flags;
         enum mail_storage_lock_method lock_method;
 	struct mail_storage *storage, *mbox_storage;
@@ -646,26 +703,12 @@ int main(int argc, char *argv[])
 	if (deliver_set->sendmail_path == NULL)
 		deliver_set->sendmail_path = DEFAULT_SENDMAIL_PATH;
 
+	mail_env = expand_envs(destination);
+
 	dict_client_register();
         duplicate_init();
         mail_storage_init();
 	mail_storage_register_all();
-
-	/* MAIL comes from userdb, MAIL_LOCATION from dovecot.conf.
-	   We don't want to expand settings coming from userdb. */
-	mail_env = getenv("MAIL");
-	if (mail_env == NULL)  {
-		mail_env = getenv("MAIL_LOCATION");
-		if (mail_env == NULL)  {
-			/* Keep this for backwards compatibility */
-			mail_env = getenv("DEFAULT_MAIL_ENV");
-		}
-		if (mail_env != NULL) {
-			table = get_var_expand_table(destination,
-						     getenv("HOME"));
-			mail_env = expand_mail_env(mail_env, table);
-		}
-	}
 
 	module_dir_init(modules);
 


More information about the dovecot-cvs mailing list