From a0997e4f1ebdc01332e99b232f66ad3146421239 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan.bosch@dovecot.fi>
Date: Fri, 22 Sep 2017 15:59:15 +0200
Subject: [PATCH] Obtain postmaster_address and hostname from
 mail_storage_settings.

---
 .../plugins/enotify/mailto/ntfy-mailto.c        |  4 ++--
 src/lib-sieve/plugins/notify/cmd-notify.c       | 10 ++++++----
 src/lib-sieve/plugins/vacation/cmd-vacation.c   |  6 ++++--
 .../plugins/vnd.dovecot/report/cmd-report.c     |  4 ++--
 src/lib-sieve/sieve-actions.c                   |  2 +-
 src/lib-sieve/sieve-address-source.c            |  2 +-
 src/lib-sieve/sieve-common.h                    |  7 +++++++
 src/lib-sieve/sieve-types.h                     |  1 -
 src/lib-sieve/sieve.c                           | 17 +++++++++++++++++
 src/plugins/imapsieve/imap-sieve.c              |  7 +++----
 src/plugins/lda-sieve/lda-sieve-plugin.c        |  5 +++--
 src/sieve-tools/sieve-filter.c                  |  1 -
 src/sieve-tools/sieve-test.c                    |  1 -
 src/testsuite/testsuite-mailstore.c             |  2 +-
 src/testsuite/testsuite-script.c                |  2 --
 src/testsuite/testsuite.c                       |  1 -
 16 files changed, 47 insertions(+), 25 deletions(-)

diff --git a/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c b/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
index 8bf564b73..5e61af72c 100644
--- a/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
+++ b/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
@@ -497,7 +497,7 @@ static int ntfy_mailto_send
 		else if ( svinst->user_email != NULL )
 			from_smtp = sieve_address_to_string(svinst->user_email);
 		else
-			from_smtp = senv->postmaster_address;
+			from_smtp = sieve_get_postmaster_address(senv);
 	}
 
 	/* Determine message from address */
@@ -668,7 +668,7 @@ static int ntfy_mailto_action_execute
 		(nenv->flags & SIEVE_EXECUTE_FLAG_NO_ENVELOPE) == 0 )
 		owner_email = sieve_message_get_final_recipient(nenv->msgctx);
 	if ( owner_email == NULL )
-		owner_email = senv->postmaster_address;
+		owner_email = sieve_get_postmaster_address(senv);
 	i_assert( owner_email != NULL );
 
 	/* Is the message an automatic reply ? */
diff --git a/src/lib-sieve/plugins/notify/cmd-notify.c b/src/lib-sieve/plugins/notify/cmd-notify.c
index 75878abfc..d261ddce5 100644
--- a/src/lib-sieve/plugins/notify/cmd-notify.c
+++ b/src/lib-sieve/plugins/notify/cmd-notify.c
@@ -724,7 +724,7 @@ static bool act_notify_send
 	}
 
 	rfc2822_header_printf(msg, "From",
-		"Postmaster <%s>", senv->postmaster_address);
+		"Postmaster <%s>", sieve_get_postmaster_address(senv));
 
 	rfc2822_header_write(msg, "Subject", "[SIEVE] New mail notification");
 
@@ -746,10 +746,12 @@ static bool act_notify_send
 	rfc2822_header_write(msg, "Message-ID", outmsgid);
 
 	if ( (aenv->flags & SIEVE_EXECUTE_FLAG_NO_ENVELOPE) == 0 &&
-		sieve_message_get_sender(aenv->msgctx) != NULL )
-		sctx = sieve_smtp_start(senv, senv->postmaster_address);
-	else
+		sieve_message_get_sender(aenv->msgctx) != NULL ) {
+		sctx = sieve_smtp_start(senv,
+			sieve_get_postmaster_address(senv));
+	} else {
 		sctx = sieve_smtp_start(senv, NULL);
+	}
 
 	/* Add all recipients (and compose To header field) */
 	to = t_str_new(128);
diff --git a/src/lib-sieve/plugins/vacation/cmd-vacation.c b/src/lib-sieve/plugins/vacation/cmd-vacation.c
index 3c033af94..2195e57a0 100644
--- a/src/lib-sieve/plugins/vacation/cmd-vacation.c
+++ b/src/lib-sieve/plugins/vacation/cmd-vacation.c
@@ -1031,8 +1031,10 @@ static int act_vacation_send
 		rfc2822_header_utf8_printf(msg, "From", "%s", ctx->from);
 	else if ( reply_from != NULL )
 		rfc2822_header_printf(msg, "From", "<%s>", reply_from);
-	else
-		rfc2822_header_printf(msg, "From", "Postmaster <%s>", senv->postmaster_address);
+	else {
+		rfc2822_header_printf(msg, "From", "Postmaster <%s>",
+			sieve_get_postmaster_address(senv));
+	}
 
 	rfc2822_header_printf(msg, "To", "%s", reply_to);
 
diff --git a/src/lib-sieve/plugins/vnd.dovecot/report/cmd-report.c b/src/lib-sieve/plugins/vnd.dovecot/report/cmd-report.c
index 18c93cc0c..38af36c9e 100644
--- a/src/lib-sieve/plugins/vnd.dovecot/report/cmd-report.c
+++ b/src/lib-sieve/plugins/vnd.dovecot/report/cmd-report.c
@@ -490,8 +490,8 @@ static int act_report_send
 	if ( (ret=sieve_address_source_get_address
 		(&report_from, svinst, senv, msgctx,
 			aenv->flags, &from)) <= 0 || from == NULL || *from == '\0') {
-		from = t_strdup_printf
-			("Postmaster <%s>", senv->postmaster_address);
+		from = t_strdup_printf("Postmaster <%s>",
+			sieve_get_postmaster_address(senv));
 	}
 
 	/* Start message */
diff --git a/src/lib-sieve/sieve-actions.c b/src/lib-sieve/sieve-actions.c
index f4a075902..1d05e2fa3 100644
--- a/src/lib-sieve/sieve-actions.c
+++ b/src/lib-sieve/sieve-actions.c
@@ -849,7 +849,7 @@ static int sieve_action_do_reject_mail
 	rfc2822_header_write(hdr, "Message-ID", new_msgid);
 	rfc2822_header_write(hdr, "Date", message_date_create(ioloop_time));
 	rfc2822_header_printf(hdr, "From", "Mail Delivery Subsystem <%s>",
-		senv->postmaster_address);
+		sieve_get_postmaster_address(senv));
 	rfc2822_header_printf(hdr, "To", "<%s>", sender);
 	rfc2822_header_write(hdr, "Subject", "Automatically rejected mail");
 	rfc2822_header_write(hdr, "Auto-Submitted", "auto-replied (rejected)");
diff --git a/src/lib-sieve/sieve-address-source.c b/src/lib-sieve/sieve-address-source.c
index 19a6bf786..e697bfffb 100644
--- a/src/lib-sieve/sieve-address-source.c
+++ b/src/lib-sieve/sieve-address-source.c
@@ -109,7 +109,7 @@ int sieve_address_source_get_address
 		*addr_r = sieve_address_to_string(svinst->user_email);
 		return 1;
 	case SIEVE_ADDRESS_SOURCE_POSTMASTER:
-		*addr_r = senv->postmaster_address;
+		*addr_r = sieve_get_postmaster_address(senv);
 		return 1;
 	case SIEVE_ADDRESS_SOURCE_EXPLICIT:
 		*addr_r = sieve_address_to_string(asrc->address);
diff --git a/src/lib-sieve/sieve-common.h b/src/lib-sieve/sieve-common.h
index 273f67121..401945f9d 100644
--- a/src/lib-sieve/sieve-common.h
+++ b/src/lib-sieve/sieve-common.h
@@ -221,4 +221,11 @@ void sieve_trace_log_write_line
 const char *sieve_get_user_email
 	(struct sieve_instance *svinst);
 
+/*
+ * Postmaster address 
+ */
+
+const char *sieve_get_postmaster_address
+(const struct sieve_script_env *senv);
+
 #endif /* __SIEVE_COMMON_H */
diff --git a/src/lib-sieve/sieve-types.h b/src/lib-sieve/sieve-types.h
index fdcc5823e..3c858b31e 100644
--- a/src/lib-sieve/sieve-types.h
+++ b/src/lib-sieve/sieve-types.h
@@ -183,7 +183,6 @@ struct sieve_script_env {
 	/* Mail-related */
 	struct mail_user *user;
 	const char *default_mailbox;
-	const char *postmaster_address;
 	bool mailbox_autocreate;
 	bool mailbox_autosubscribe;
 
diff --git a/src/lib-sieve/sieve.c b/src/lib-sieve/sieve.c
index 4273b8001..231ff28ad 100644
--- a/src/lib-sieve/sieve.c
+++ b/src/lib-sieve/sieve.c
@@ -10,6 +10,7 @@
 #include "eacces-error.h"
 #include "home-expand.h"
 #include "hostpid.h"
+#include "mail-user.h"
 
 #include "sieve-settings.h"
 #include "sieve-extensions.h"
@@ -1071,3 +1072,19 @@ const char *sieve_get_user_email
 	}
 	return NULL;
 }
+
+/*
+ * Postmaster address
+ */
+
+const char *sieve_get_postmaster_address
+(const struct sieve_script_env *senv)
+{
+	const struct mail_storage_settings *mail_set =
+		mail_user_set_get_storage_set(senv->user);
+
+	i_assert(mail_set->postmaster_address != NULL &&
+		*mail_set->postmaster_address != '\0');
+	return mail_set->postmaster_address;
+}
+
diff --git a/src/plugins/imapsieve/imap-sieve.c b/src/plugins/imapsieve/imap-sieve.c
index 8df831b2c..9c7edf374 100644
--- a/src/plugins/imapsieve/imap-sieve.c
+++ b/src/plugins/imapsieve/imap-sieve.c
@@ -65,7 +65,8 @@ struct imap_sieve *imap_sieve_init(struct client *client)
 	struct sieve_environment svenv;
 	struct imap_sieve *isieve;
 	struct mail_user *user = client->user;
-	const struct lda_settings *lda_set = client->lda_set;
+	const struct mail_storage_settings *mail_set =
+		mail_user_set_get_storage_set(user);
 	bool debug = user->mail_debug;
 	pool_t pool;
 
@@ -79,7 +80,7 @@ struct imap_sieve *imap_sieve_init(struct client *client)
 	i_zero(&svenv);
 	svenv.username = user->username;
 	(void)mail_user_get_home(user, &svenv.home_dir);
-	svenv.hostname = lda_set->hostname;
+	svenv.hostname = mail_set->hostname;
 	svenv.base_dir = user->set->base_dir;
 	svenv.flags = SIEVE_FLAG_HOME_RELATIVE;
 	svenv.location = SIEVE_ENV_LOCATION_MS;
@@ -698,7 +699,6 @@ int imap_sieve_run_mail
 	struct imap_sieve *isieve = isrun->isieve;
 	struct sieve_instance *svinst = isieve->svinst;
 	struct mail_user *user = isieve->client->user;
-	const struct lda_settings *lda_set = isieve->client->lda_set;
 	struct sieve_message_data msgdata;
 	struct sieve_script_env scriptenv;
 	struct sieve_exec_status estatus;
@@ -740,7 +740,6 @@ int imap_sieve_run_mail
 		i_zero(&estatus);
 		scriptenv.default_mailbox = mailbox_get_vname(mail->box);
 		scriptenv.user = user;
-		scriptenv.postmaster_address = lda_set->postmaster_address;
 		scriptenv.smtp_start = imap_sieve_smtp_start;
 		scriptenv.smtp_add_rcpt = imap_sieve_smtp_add_rcpt;
 		scriptenv.smtp_send = imap_sieve_smtp_send;
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.c b/src/plugins/lda-sieve/lda-sieve-plugin.c
index 8acd6daa1..30d91b65a 100644
--- a/src/plugins/lda-sieve/lda-sieve-plugin.c
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.c
@@ -850,7 +850,6 @@ static int lda_sieve_execute
 		scriptenv.mailbox_autocreate = mdctx->set->lda_mailbox_autocreate;
 		scriptenv.mailbox_autosubscribe = mdctx->set->lda_mailbox_autosubscribe;
 		scriptenv.user = mdctx->dest_user;
-		scriptenv.postmaster_address = mdctx->set->postmaster_address;
 		scriptenv.smtp_start = lda_sieve_smtp_start;
 		scriptenv.smtp_add_rcpt = lda_sieve_smtp_add_rcpt;
 		scriptenv.smtp_send = lda_sieve_smtp_send;
@@ -887,6 +886,8 @@ static int lda_sieve_deliver_mail
 (struct mail_deliver_context *mdctx, struct mail_storage **storage_r)
 {
 	struct lda_sieve_run_context srctx;
+	const struct mail_storage_settings *mail_set =
+		mail_user_set_get_storage_set(mdctx->dest_user);
 	bool debug = mdctx->dest_user->mail_debug;
 	struct sieve_environment svenv;
 	int ret = 0;
@@ -902,7 +903,7 @@ static int lda_sieve_deliver_mail
 	memset((void*)&svenv, 0, sizeof(svenv));
 	svenv.username = mdctx->dest_user->username;
 	svenv.home_dir = srctx.home_dir;
-	svenv.hostname = mdctx->set->hostname;
+	svenv.hostname = mail_set->hostname;
 	svenv.base_dir = mdctx->dest_user->set->base_dir;
 	svenv.temp_dir = mdctx->dest_user->set->mail_temp_dir;
 	svenv.flags = SIEVE_FLAG_HOME_RELATIVE;
diff --git a/src/sieve-tools/sieve-filter.c b/src/sieve-tools/sieve-filter.c
index fcc475292..9ea3305fb 100644
--- a/src/sieve-tools/sieve-filter.c
+++ b/src/sieve-tools/sieve-filter.c
@@ -548,7 +548,6 @@ int main(int argc, char **argv)
 	scriptenv.mailbox_autocreate = FALSE;
 	scriptenv.default_mailbox = dst_mailbox;
 	scriptenv.user = mail_user;
-	scriptenv.postmaster_address = "postmaster@example.com";
 
 	/* Compose filter context */
 	i_zero(&sfdata);
diff --git a/src/sieve-tools/sieve-test.c b/src/sieve-tools/sieve-test.c
index 8d37fbfeb..eaae59b0f 100644
--- a/src/sieve-tools/sieve-test.c
+++ b/src/sieve-tools/sieve-test.c
@@ -309,7 +309,6 @@ int main(int argc, char **argv)
 		i_zero(&scriptenv);
 		scriptenv.default_mailbox = mailbox;
 		scriptenv.user = sieve_tool_get_mail_user(sieve_tool);
-		scriptenv.postmaster_address = "postmaster@example.com";
 		scriptenv.smtp_start = sieve_smtp_start;
 		scriptenv.smtp_add_rcpt = sieve_smtp_add_rcpt;
 		scriptenv.smtp_send = sieve_smtp_send;
diff --git a/src/testsuite/testsuite-mailstore.c b/src/testsuite/testsuite-mailstore.c
index 534b87390..3035a5559 100644
--- a/src/testsuite/testsuite-mailstore.c
+++ b/src/testsuite/testsuite-mailstore.c
@@ -71,7 +71,7 @@ void testsuite_mailstore_init(void)
 	}
 	
 	mail_user_dovecot = sieve_tool_get_mail_user(sieve_tool);
-	mail_user = mail_user_alloc("testsuite mail user",
+	mail_user = mail_user_alloc("testsuite-mail-user@example.org",
 		mail_user_dovecot->set_info, mail_user_dovecot->unexpanded_set);
 	mail_user->autocreated = TRUE;
 	if (t_get_working_dir(&cwd, &error) < 0)
diff --git a/src/testsuite/testsuite-script.c b/src/testsuite/testsuite-script.c
index 40b1102a6..6d3a1cc67 100644
--- a/src/testsuite/testsuite-script.c
+++ b/src/testsuite/testsuite-script.c
@@ -108,7 +108,6 @@ bool testsuite_script_run(const struct sieve_runtime_env *renv)
 	/* Compose script execution environment */
 	i_zero(&scriptenv);
 	scriptenv.default_mailbox = "INBOX";
-	scriptenv.postmaster_address = "postmaster@example.com";
 	scriptenv.smtp_start = NULL;
 	scriptenv.smtp_add_rcpt = NULL;
 	scriptenv.smtp_send = NULL;
@@ -181,7 +180,6 @@ bool testsuite_script_multiscript
 	/* Compose script execution environment */
 	i_zero(&scriptenv);
 	scriptenv.default_mailbox = "INBOX";
-	scriptenv.postmaster_address = "postmaster@example.com";
 	scriptenv.smtp_start = NULL;
 	scriptenv.smtp_add_rcpt = NULL;
 	scriptenv.smtp_send = NULL;
diff --git a/src/testsuite/testsuite.c b/src/testsuite/testsuite.c
index 315d01fa9..94c60f7c4 100644
--- a/src/testsuite/testsuite.c
+++ b/src/testsuite/testsuite.c
@@ -182,7 +182,6 @@ int main(int argc, char **argv)
 		i_zero(&scriptenv);
 		scriptenv.user = testsuite_mailstore_get_user();
 		scriptenv.default_mailbox = "INBOX";
-		scriptenv.postmaster_address = "postmaster@example.com";
 		scriptenv.smtp_start = testsuite_smtp_start;
 		scriptenv.smtp_add_rcpt = testsuite_smtp_add_rcpt;
 		scriptenv.smtp_send = testsuite_smtp_send;
-- 
GitLab