public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api v3 1/5] smtputf8: keep smtputf8 from incoming postfix, detect for local mail
Date: Fri, 17 Mar 2023 19:44:50 +0100	[thread overview]
Message-ID: <20230317184456.26465-2-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20230317184456.26465-1-s.ivanov@proxmox.com>

This patch changes the detection if smtputf8 is needed as option to
the 'MAIL' command:
* for mail arriving through postfix it is only added if the mail
  originally was received with it (Accept and BCC actions)
* for locally generated mail (Notify, reports, quarantine-link and
  ndrs) it is decided based on utf8 characters in the mail-addresses
  or headers - this is done by `reinject_local_mail`, as a new helper

This should match postfix own behavior in those cases quite
closely:
https://www.postfix.org/SMTPUTF8_README.html#using

Notable difference is that we check the complete e-mail address and
not only the domain part, but I assume non-ascii local-parts to be a
very fringe edge-case in environments where smtputf8 is not supported.

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
 src/PMG/API2/Quarantine.pm |  2 +-
 src/PMG/RuleDB/Notify.pm   |  2 +-
 src/PMG/SMTP.pm            |  3 ++-
 src/PMG/Utils.pm           | 28 ++++++++++++++++++++--------
 4 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/src/PMG/API2/Quarantine.pm b/src/PMG/API2/Quarantine.pm
index fbb302a..6318082 100644
--- a/src/PMG/API2/Quarantine.pm
+++ b/src/PMG/API2/Quarantine.pm
@@ -1239,7 +1239,7 @@ my sub send_link_mail {
     );
 
     # we use an empty envelope sender (we don't want to receive NDRs)
-    PMG::Utils::reinject_mail ($mail, '', [$receiver], undef, $fqdn);
+    PMG::Utils::reinject_local_mail ($mail, '', [$receiver], undef, $fqdn);
 }
 
 __PACKAGE__->register_method ({
diff --git a/src/PMG/RuleDB/Notify.pm b/src/PMG/RuleDB/Notify.pm
index 68f9b4e..c024840 100644
--- a/src/PMG/RuleDB/Notify.pm
+++ b/src/PMG/RuleDB/Notify.pm
@@ -256,7 +256,7 @@ sub execute {
 	print $fh "notify end\n";
     } else {
 	my @targets = split(/\s*,\s*/, $to);
-	my $qid = PMG::Utils::reinject_mail(
+	my $qid = PMG::Utils::reinject_local_mail(
 	    $top, $from, \@targets, undef, $msginfo->{fqdn});
 	foreach (@targets) {
 	    my $target = encode('UTF-8', $_);
diff --git a/src/PMG/SMTP.pm b/src/PMG/SMTP.pm
index fbf5c95..b7bc5d3 100644
--- a/src/PMG/SMTP.pm
+++ b/src/PMG/SMTP.pm
@@ -111,6 +111,7 @@ sub loop {
 			$self->{param}->{mail}->{$1} = $2;
 		    } elsif ($opt =~ m/smtputf8/i) {
 			$self->{smtputf8} = 1;
+			$self->{param}->{mail}->{smtputf8} = 1;
 			$from = decode('UTF-8', $from);
 		    } else {
 			#ignore everything else
@@ -314,7 +315,7 @@ EOF
 	Encoding => '7bit',
 	Description => 'Delivery report');
 
-    my $qid = PMG::Utils::reinject_mail($ndr, '', [$sender], undef, $hostname);
+    my $qid = PMG::Utils::reinject_local_mail($ndr, '', [$sender], undef, $hostname);
     if ($qid) {
 	syslog('info', "sent NDR for rejecting recipients - $qid");
     } else {
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 9c6f841..49939c4 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -221,18 +221,28 @@ sub subst_values_for_header {
     return $res;
 }
 
-sub mail_needs_smtputf8 {
-    my ($entity, $sender, $targets) = @_;
+# detects the need for setting smtputf8 based on addresses and headers
+sub reinject_local_mail {
+    my ($entity, $sender, $targets, $xforward, $me, $params) = @_;
+
+    my $needs_smtputf8 = 0;
 
-    return 1 if ($sender =~ /[^\p{PosixPrint}]/);
+    $needs_smtputf8 = 1 if ($sender =~ /[^\p{PosixPrint}]/);
 
     foreach my $target (@$targets) {
 	if ($target =~ /[^\p{PosixPrint}]/) {
-	    return 1;
+	    $needs_smtputf8 = 1;
+	    last;
 	}
     }
 
-    return 0;
+    if (!$needs_smtputf8 && $entity->head()->as_string() =~ /([^\p{PosixPrint}\n\r\t])/) {
+	$needs_smtputf8 = 1;
+    }
+
+    $params->{mail}->{smtputf8} = $needs_smtputf8;
+
+    return reinject_mail($entity, $sender, $targets, $xforward, $me, $params);
 }
 
 sub reinject_mail {
@@ -260,10 +270,12 @@ sub reinject_mail {
 	}
 
 	my $mail_opts = " BODY=8BITMIME";
-	$mail_opts .= " SMTPUTF8" if mail_needs_smtputf8($entity, $sender, $targets);
 	my $sender_addr = encode('UTF-8', $smtp->_addr($sender));
-
 	if (defined($params->{mail})) {
+	    if (delete $params->{mail}->{smtputf8}) {
+		$mail_opts .= " SMTPUTF8";
+	    }
+
 	    my $mailparams = $params->{mail};
 	    for my $p (keys %$mailparams) {
 		$mail_opts .= " $p=$mailparams->{$p}";
@@ -1258,7 +1270,7 @@ sub finalize_report {
 	return;
     }
     # we use an empty envelope sender (we don't want to receive NDRs)
-    PMG::Utils::reinject_mail ($top, '', [$receiver], undef, $data->{fqdn});
+    PMG::Utils::reinject_local_mail ($top, '', [$receiver], undef, $data->{fqdn});
 }
 
 sub lookup_timespan {
-- 
2.30.2





  reply	other threads:[~2023-03-17 18:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-17 18:44 [pmg-devel] [PATCH pmg-api v3 0/5] improve local mail injection and add smtputf8 support Stoiko Ivanov
2023-03-17 18:44 ` Stoiko Ivanov [this message]
2023-03-17 18:44 ` [pmg-devel] [PATCH pmg-api v3 2/5] config: make smtputf8 configurable through the API Stoiko Ivanov
2023-03-17 18:44 ` [pmg-devel] [PATCH pmg-api v3 3/5] reinject mail: improve error logging Stoiko Ivanov
2023-03-17 18:44 ` [pmg-devel] [PATCH pmg-api v3 4/5] quarantine: use reinject_local_mail to deliver quarantined mail Stoiko Ivanov
2023-03-17 18:44 ` [pmg-devel] [PATCH pmg-api v3 5/5] api: quarantine: decode addresses before delivery/userlisting Stoiko Ivanov
2023-03-17 18:44 ` [pmg-devel] [PATCH pmg-gui v3 1/1] mail proxy options: add smtputf8 checkbox Stoiko Ivanov
2023-03-17 18:44 ` [pmg-devel] [PATCH pmg-docs v3 1/1] doc-generator: add new option smtputf8 Stoiko Ivanov
2023-03-23 11:18 ` [pmg-devel] [PATCH pmg-api v3 0/5] improve local mail injection and add smtputf8 support Dominik Csapak
2023-03-27 10:33 ` [pmg-devel] applied-series: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230317184456.26465-2-s.ivanov@proxmox.com \
    --to=s.ivanov@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal