[pmg-devel] [PATCH pmg-api v4 11/12] ldap: improve unicode support

Dominik Csapak d.csapak at proxmox.com
Thu Nov 24 13:21:11 CET 2022


when we receive mails with SMTPUTF8 encoded sender/recipient,
we have to encode these values for our ldapcache to work,
otherwise pmg-smtp-filter fails with when trying to insert
perl strings.

on read from the cache we have to decode these values again so
that the webui can show them correctly

also encode/decode dn and group names, since according to rfc4514[0]
utf-8 should be ok here

0: https://www.ietf.org/rfc/rfc4514.txt

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
openldap/freeipa did not let me add an email with unicode characters,
but active directory did. so tested with that

 src/PMG/LDAPCache.pm       | 31 ++++++++++++++++++-------------
 src/PMG/RuleDB/LDAP.pm     | 11 +++++++----
 src/PMG/RuleDB/LDAPUser.pm | 13 ++++++++-----
 3 files changed, 33 insertions(+), 22 deletions(-)

diff --git a/src/PMG/LDAPCache.pm b/src/PMG/LDAPCache.pm
index f0698da..6cc4383 100755
--- a/src/PMG/LDAPCache.pm
+++ b/src/PMG/LDAPCache.pm
@@ -6,6 +6,7 @@ use File::Path;
 use LockFile::Simple;
 use Data::Dumper;
 use DB_File;
+use Encode qw(encode decode);
 
 use PVE::SafeSyslog;
 use PVE::Tools qw(split_list);
@@ -491,7 +492,7 @@ sub get_groups {
     my $status = $dbh->seq($key, $value, R_FIRST());
 
     while ($status == 0) {
-	$res->{$value} = $key;
+	$res->{$value} = PMG::Utils::try_decode_utf8($key);
         $status = $dbh->seq($key, $value, R_NEXT());
     }
 
@@ -515,9 +516,9 @@ sub get_users {
     while ($status == 0) {
 	my ($pmail, $account, $dn) = unpack('n/a* n/a* n/a*', $value);
 	$res->{$key} = {
-	    pmail => $pmail,
-	    account => $account,
-	    dn => $dn,
+	    pmail => PMG::Utils::try_decode_utf8($pmail),
+	    account => PMG::Utils::try_decode_utf8($account),
+	    dn => PMG::Utils::try_decode_utf8($dn),
 	};
         $status = $dbh->seq($key, $value, R_NEXT());
     }
@@ -595,7 +596,7 @@ sub list_addresses {
 
     return undef if !$dbhmails || !$dbhusers;
 
-    $mail = lc($mail);
+    $mail = encode('UTF-8', lc($mail));
 
     my $res = [];
 
@@ -609,7 +610,7 @@ sub list_addresses {
 
     my ($pmail, $account, $dn) = unpack('n/a* n/a* n/a*', $rdata);
 
-    push @$res, { primary => 1, email => $pmail };
+    push @$res, { primary => 1, email => PMG::Utils::try_decode_utf8($pmail) };
 
     my $key = 0 ;
     my $value = "" ;
@@ -617,7 +618,7 @@ sub list_addresses {
 
     while ($status == 0) {
 	if ($value == $cuid && $key ne $pmail) {
-	    push @$res, { primary => 0, email => $key };
+	    push @$res, { primary => 0, email => PMG::Utils::try_decode_utf8($key) };
 	}
 	$status = $dbhmails->seq($key, $value, R_NEXT());
     }
@@ -631,7 +632,7 @@ sub mail_exists {
     my $dbh = $self->{dbstat}->{mails}->{dbh};
     return 0 if !$dbh;
 
-    $mail = lc($mail);
+    $mail = encode('UTF-8', lc($mail));
 
     my $res;
     $dbh->get($mail, $res);
@@ -644,7 +645,7 @@ sub account_exists {
     my $dbh = $self->{dbstat}->{accounts}->{dbh};
     return 0 if !$dbh;
 
-    $account = lc($account);
+    $account = encode('UTF-8', lc($account));
 
     my $res;
     $dbh->get($account, $res);
@@ -657,6 +658,8 @@ sub group_exists {
     my $dbh = $self->{dbstat}->{groups}->{dbh};
     return 0 if !$dbh;
 
+    $group = encode('UTF-8', $group);
+
     my $res;
     $dbh->get($group, $res);
     return $res;
@@ -669,8 +672,8 @@ sub account_has_address {
     my $dbhaccounts = $self->{dbstat}->{accounts}->{dbh};
     return 0 if !$dbhmails || !$dbhaccounts;
 
-    $account = lc($account);
-    $mail = lc($mail);
+    $account = encode('UTF-8', lc($account));
+    $mail = encode('UTF-8', lc($mail));
 
     my $accid;
     $dbhaccounts->get($account, $accid);
@@ -692,12 +695,14 @@ sub user_in_group {
 
     return 0 if !$dbhmails || !$dbhgroups || !$dbhmemberof;
 
-    $mail = lc($mail);
+    $mail = encode('UTF-8', lc($mail));
 
     my $cuid;
     $dbhmails->get($mail, $cuid);
     return 0 if !$cuid;
 
+    $group = encode('UTF-8', $group);
+
     my $groupid;
     $dbhgroups->get($group, $groupid);
     return 0 if !$groupid;
@@ -715,7 +720,7 @@ sub account_info {
 
     return undef if !$dbhmails || !$dbhusers;
 
-    $mail = lc($mail);
+    $mail = encode('UTF-8', lc($mail));
 
     my $res = {};
 
diff --git a/src/PMG/RuleDB/LDAP.pm b/src/PMG/RuleDB/LDAP.pm
index a132499..3fcf5f0 100644
--- a/src/PMG/RuleDB/LDAP.pm
+++ b/src/PMG/RuleDB/LDAP.pm
@@ -3,6 +3,7 @@ package PMG::RuleDB::LDAP;
 use strict;
 use warnings;
 use DBI;
+use Encode qw(encode);
 
 use PVE::Exception qw(raise_param_exc);
 
@@ -45,12 +46,14 @@ sub load_attr {
 
     defined($value) || die "undefined value: ERROR";
 
+    my $decoded = PMG::Utils::try_decode_utf8($value);
+
     my $obj;
-    if ($value =~ m/^([^:]*):(.*)$/) {
+    if ($decoded =~ m/^([^:]*):(.*)$/) {
 	$obj = $class->new($2, $1, $ogroup);
-	$obj->{digest} = Digest::SHA::sha1_hex($id, $2, $1, $ogroup);
+	$obj->{digest} = Digest::SHA::sha1_hex($id, encode('UTF-8', $2), encode('UTF-8', $1), $ogroup);
     } else {
-	$obj = $class->new($value, '', $ogroup);
+	$obj = $class->new($decoded, '', $ogroup);
 	$obj->{digest} = Digest::SHA::sha1_hex($id, $value, '#', $ogroup);
     }
 
@@ -69,7 +72,7 @@ sub save {
     my $grp = $self->{ldapgroup};
     my $profile = $self->{profile};
 
-    my $confdata = "$profile:$grp";
+    my $confdata = encode('UTF-8', "$profile:$grp");
 
     if (defined ($self->{id})) {
 	# update
diff --git a/src/PMG/RuleDB/LDAPUser.pm b/src/PMG/RuleDB/LDAPUser.pm
index 022d784..345decb 100644
--- a/src/PMG/RuleDB/LDAPUser.pm
+++ b/src/PMG/RuleDB/LDAPUser.pm
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use DBI;
 use Digest::SHA;
+use Encode qw(encode);
 
 use PVE::INotify;
 
@@ -46,13 +47,15 @@ sub load_attr {
     my $class = ref($type) || $type;
 
     defined($value) || die "undefined value: ERROR";
-    
+
+    my $decoded = PMG::Utils::try_decode_utf8($value);
+
     my $obj;
-    if ($value =~ m/^([^:]*):(.*)$/) {
+    if ($decoded =~ m/^([^:]*):(.*)$/) {
 	$obj = $class->new($2, $1, $ogroup);
-	$obj->{digest} = Digest::SHA::sha1_hex($id, $2, $1, $ogroup);
+	$obj->{digest} = Digest::SHA::sha1_hex($id, encode('UTF-8', $2), encode('UTF-8', $1), $ogroup);
    } else {
-	$obj = $class->new($value, '', $ogroup);
+	$obj = $class->new($decoded, '', $ogroup);
 	$obj->{digest} = Digest::SHA::sha1_hex ($id, $value, '#', $ogroup);
     }
 
@@ -71,7 +74,7 @@ sub save {
     my $user = $self->{ldapuser};
     my $profile = $self->{profile};
  
-    my $confdata = "$profile:$user";
+    my $confdata = encode('UTF-8', "$profile:$user");
     
     if (defined($self->{id})) {
 	# update
-- 
2.30.2





More information about the pmg-devel mailing list