Skip to content

Commit 0f2e68b

Browse files
Jan Viktoringitster
authored andcommitted
send-email: provide whitelist of SMTP AUTH mechanisms
When sending an e-mail, the client and server must agree on an authentication mechanism. Some servers (due to misconfiguration or a bug) deny valid credentials for certain mechanisms. In this patch, a new option --smtp-auth and configuration entry smtpAuth are introduced. If smtp_auth is defined, it works as a whitelist of allowed mechanisms for authentication selected from the ones supported by the installed SASL perl library. Signed-off-by: Jan Viktorin <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a17c56c commit 0f2e68b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Documentation/git-send-email.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ Sending
171171
to determine your FQDN automatically. Default is the value of
172172
'sendemail.smtpDomain'.
173173

174+
--smtp-auth=<mechanisms>::
175+
Whitespace-separated list of allowed SMTP-AUTH mechanisms. This setting
176+
forces using only the listed mechanisms. Example:
177+
+
178+
------
179+
$ git send-email --smtp-auth="PLAIN LOGIN GSSAPI" ...
180+
------
181+
+
182+
If at least one of the specified mechanisms matches the ones advertised by the
183+
SMTP server and if it is supported by the utilized SASL library, the mechanism
184+
is used for authentication. If neither 'sendemail.smtpAuth' nor '--smtp-auth'
185+
is specified, all mechanisms supported by the SASL library can be used.
186+
174187
--smtp-pass[=<password>]::
175188
Password for SMTP-AUTH. The argument is optional: If no
176189
argument is specified, then the empty string is used as

git-send-email.perl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ sub usage {
7575
Pass an empty string to disable certificate
7676
verification.
7777
--smtp-domain <str> * The domain name sent to HELO/EHLO handshake
78+
--smtp-auth <str> * Space-separated list of allowed AUTH mechanisms.
79+
This setting forces to use one of the listed mechanisms.
7880
--smtp-debug <0|1> * Disable, enable Net::SMTP debug.
7981
8082
Automating:
@@ -208,7 +210,7 @@ sub do_edit {
208210
my ($to_cmd, $cc_cmd);
209211
my ($smtp_server, $smtp_server_port, @smtp_server_options);
210212
my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
211-
my ($identity, $aliasfiletype, @alias_files, $smtp_domain);
213+
my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
212214
my ($validate, $confirm);
213215
my (@suppress_cc);
214216
my ($auto_8bit_encoding);
@@ -239,6 +241,7 @@ sub do_edit {
239241
"smtppass" => \$smtp_authpass,
240242
"smtpsslcertpath" => \$smtp_ssl_cert_path,
241243
"smtpdomain" => \$smtp_domain,
244+
"smtpauth" => \$smtp_auth,
242245
"to" => \@initial_to,
243246
"tocmd" => \$to_cmd,
244247
"cc" => \@initial_cc,
@@ -310,6 +313,7 @@ sub signal_handler {
310313
"smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
311314
"smtp-debug:i" => \$debug_net_smtp,
312315
"smtp-domain:s" => \$smtp_domain,
316+
"smtp-auth=s" => \$smtp_auth,
313317
"identity=s" => \$identity,
314318
"annotate!" => \$annotate,
315319
"no-annotate" => sub {$annotate = 0},
@@ -1136,6 +1140,12 @@ sub smtp_auth_maybe {
11361140
Authen::SASL->import(qw(Perl));
11371141
};
11381142

1143+
# Check mechanism naming as defined in:
1144+
# https://tools.ietf.org/html/rfc4422#page-8
1145+
if ($smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
1146+
die "invalid smtp auth: '${smtp_auth}'";
1147+
}
1148+
11391149
# TODO: Authentication may fail not because credentials were
11401150
# invalid but due to other reasons, in which we should not
11411151
# reject credentials.
@@ -1148,6 +1158,20 @@ sub smtp_auth_maybe {
11481158
'password' => $smtp_authpass
11491159
}, sub {
11501160
my $cred = shift;
1161+
1162+
if ($smtp_auth) {
1163+
my $sasl = Authen::SASL->new(
1164+
mechanism => $smtp_auth,
1165+
callback => {
1166+
user => $cred->{'username'},
1167+
pass => $cred->{'password'},
1168+
authname => $cred->{'username'},
1169+
}
1170+
);
1171+
1172+
return !!$smtp->auth($sasl);
1173+
}
1174+
11511175
return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
11521176
});
11531177

0 commit comments

Comments
 (0)