mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-05-29 18:53:02 +00:00
Fix sync-jobs with legacy-encoded passwords by implementing UTF-8/Latin-1 retry logic
Co-authored-by: DerLinkman <62480600+DerLinkman@users.noreply.github.com>
This commit is contained in:
@@ -117,51 +117,77 @@ while ($row = $sth->fetchrow_arrayref()) {
|
|||||||
if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; }
|
if ($enc1 eq "TLS") { $enc1 = "--tls1"; } elsif ($enc1 eq "SSL") { $enc1 = "--ssl1"; } else { undef $enc1; }
|
||||||
|
|
||||||
my $template = $run_dir . '/imapsync.XXXXXXX';
|
my $template = $run_dir . '/imapsync.XXXXXXX';
|
||||||
my $passfile1 = File::Temp->new(TEMPLATE => $template);
|
|
||||||
my $passfile2 = File::Temp->new(TEMPLATE => $template);
|
my $passfile2 = File::Temp->new(TEMPLATE => $template);
|
||||||
|
|
||||||
binmode( $passfile1, ":utf8" );
|
|
||||||
|
|
||||||
print $passfile1 "$password1\n";
|
|
||||||
print $passfile2 trim($master_pass) . "\n";
|
print $passfile2 trim($master_pass) . "\n";
|
||||||
|
|
||||||
my @custom_params_a = qqw($custom_params);
|
my @custom_params_a = qqw($custom_params);
|
||||||
my $custom_params_ref = \@custom_params_a;
|
my $custom_params_ref = \@custom_params_a;
|
||||||
|
|
||||||
my $generated_cmds = [ "/usr/local/bin/imapsync",
|
# Helper function to run imapsync with a specific encoding for password1
|
||||||
"--tmpdir", "/tmp",
|
my $run_imapsync = sub {
|
||||||
"--nofoldersizes",
|
my ($encoding) = @_;
|
||||||
"--addheader",
|
|
||||||
($timeout1 le "0" ? () : ('--timeout1', $timeout1)),
|
my $passfile1 = File::Temp->new(TEMPLATE => $template);
|
||||||
($timeout2 le "0" ? () : ('--timeout2', $timeout2)),
|
|
||||||
($exclude eq "" ? () : ("--exclude", $exclude)),
|
if ($encoding eq 'utf8') {
|
||||||
($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
|
binmode( $passfile1, ":utf8" );
|
||||||
($maxage eq "0" ? () : ('--maxage', $maxage)),
|
} elsif ($encoding eq 'latin1') {
|
||||||
($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxbytespersecond)),
|
binmode( $passfile1, ":encoding(iso-8859-1)" );
|
||||||
($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
|
}
|
||||||
($subscribeall ne "1" ? () : ('--subscribeall')),
|
|
||||||
($delete1 ne "1" ? () : ('--delete')),
|
print $passfile1 "$password1\n";
|
||||||
($delete2 ne "1" ? () : ('--delete2')),
|
$passfile1->flush();
|
||||||
($automap ne "1" ? () : ('--automap')),
|
|
||||||
($skipcrossduplicates ne "1" ? () : ('--skipcrossduplicates')),
|
my $generated_cmds = [ "/usr/local/bin/imapsync",
|
||||||
(!defined($enc1) ? () : ($enc1)),
|
"--tmpdir", "/tmp",
|
||||||
"--host1", $host1,
|
"--nofoldersizes",
|
||||||
"--user1", $user1,
|
"--addheader",
|
||||||
"--passfile1", $passfile1->filename,
|
($timeout1 le "0" ? () : ('--timeout1', $timeout1)),
|
||||||
"--port1", $port1,
|
($timeout2 le "0" ? () : ('--timeout2', $timeout2)),
|
||||||
"--host2", "localhost",
|
($exclude eq "" ? () : ("--exclude", $exclude)),
|
||||||
"--user2", $user2 . '*' . trim($master_user),
|
($subfolder2 eq "" ? () : ('--subfolder2', $subfolder2)),
|
||||||
"--passfile2", $passfile2->filename,
|
($maxage eq "0" ? () : ('--maxage', $maxage)),
|
||||||
($dry eq "1" ? ('--dry') : ()),
|
($maxbytespersecond eq "0" ? () : ('--maxbytespersecond', $maxbytespersecond)),
|
||||||
'--no-modulesversion',
|
($delete2duplicates ne "1" ? () : ('--delete2duplicates')),
|
||||||
'--noreleasecheck'];
|
($subscribeall ne "1" ? () : ('--subscribeall')),
|
||||||
|
($delete1 ne "1" ? () : ('--delete')),
|
||||||
|
($delete2 ne "1" ? () : ('--delete2')),
|
||||||
|
($automap ne "1" ? () : ('--automap')),
|
||||||
|
($skipcrossduplicates ne "1" ? () : ('--skipcrossduplicates')),
|
||||||
|
(!defined($enc1) ? () : ($enc1)),
|
||||||
|
"--host1", $host1,
|
||||||
|
"--user1", $user1,
|
||||||
|
"--passfile1", $passfile1->filename,
|
||||||
|
"--port1", $port1,
|
||||||
|
"--host2", "localhost",
|
||||||
|
"--user2", $user2 . '*' . trim($master_user),
|
||||||
|
"--passfile2", $passfile2->filename,
|
||||||
|
($dry eq "1" ? ('--dry') : ()),
|
||||||
|
'--no-modulesversion',
|
||||||
|
'--noreleasecheck'];
|
||||||
|
|
||||||
|
my $stdout;
|
||||||
|
run [@$generated_cmds, @$custom_params_ref], '&>', \$stdout;
|
||||||
|
|
||||||
|
return $stdout;
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1, success = NULL, exit_status = NULL WHERE id = ?");
|
$is_running = $dbh->prepare("UPDATE imapsync SET is_running = 1, success = NULL, exit_status = NULL WHERE id = ?");
|
||||||
$is_running->bind_param( 1, ${id} );
|
$is_running->bind_param( 1, ${id} );
|
||||||
$is_running->execute();
|
$is_running->execute();
|
||||||
|
|
||||||
run [@$generated_cmds, @$custom_params_ref], '&>', \my $stdout;
|
# First attempt with UTF-8 encoding (default behavior)
|
||||||
|
my $stdout = $run_imapsync->('utf8');
|
||||||
|
|
||||||
|
# Check if authentication failed
|
||||||
|
my $auth_failed = ($stdout =~ /LOGIN failed|authentication failed|AUTHENTICATIONFAILED/i);
|
||||||
|
|
||||||
|
# If authentication failed with UTF-8, retry with Latin-1 encoding for legacy passwords
|
||||||
|
if ($auth_failed) {
|
||||||
|
$stdout = $run_imapsync->('latin1');
|
||||||
|
}
|
||||||
|
|
||||||
# check exit code and status
|
# check exit code and status
|
||||||
($exit_code, $exit_status) = ($stdout =~ m/Exiting\swith\sreturn\svalue\s(\d+)\s\(([^:)]+)/);
|
($exit_code, $exit_status) = ($stdout =~ m/Exiting\swith\sreturn\svalue\s(\d+)\s\(([^:)]+)/);
|
||||||
|
|||||||
Reference in New Issue
Block a user