1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-05 14:06:15 +02:00

git-send-email: use git credential to obtain password

If smtp_user is provided but smtp_pass is not, instead of
prompting for password, make git-send-email use git
credential command instead.

Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Michal Nazarewicz 2013-02-12 15:02:33 +01:00 committed by Junio C Hamano
parent 52dce6d036
commit 4d31a44a08
2 changed files with 45 additions and 30 deletions

View File

@ -164,8 +164,8 @@ Sending
Furthermore, passwords need not be specified in configuration files
or on the command line. If a username has been specified (with
'--smtp-user' or a 'sendemail.smtpuser'), but no password has been
specified (with '--smtp-pass' or 'sendemail.smtppass'), then the
user is prompted for a password while the input is masked for privacy.
specified (with '--smtp-pass' or 'sendemail.smtppass'), then
a password is obtained using 'git-credential'.
--smtp-server=<host>::
If set, specifies the outgoing SMTP server to use (e.g.

View File

@ -1045,6 +1045,47 @@ sub maildomain {
return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
}
sub smtp_host_string {
if (defined $smtp_server_port) {
return "$smtp_server:$smtp_server_port";
} else {
return $smtp_server;
}
}
# Returns 1 if authentication succeeded or was not necessary
# (smtp_user was not specified), and 0 otherwise.
sub smtp_auth_maybe {
if (!defined $smtp_authuser || $auth) {
return 1;
}
# Workaround AUTH PLAIN/LOGIN interaction defect
# with Authen::SASL::Cyrus
eval {
require Authen::SASL;
Authen::SASL->import(qw(Perl));
};
# TODO: Authentication may fail not because credentials were
# invalid but due to other reasons, in which we should not
# reject credentials.
$auth = Git::credential({
'protocol' => 'smtp',
'host' => smtp_host_string(),
'username' => $smtp_authuser,
# if there's no password, "git credential fill" will
# give us one, otherwise it'll just pass this one.
'password' => $smtp_authpass
}, sub {
my $cred = shift;
return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
});
return $auth;
}
# Returns 1 if the message was sent, and 0 otherwise.
# In actuality, the whole program dies when there
# is an error sending a message.
@ -1155,9 +1196,7 @@ sub send_message {
else {
require Net::SMTP;
$smtp_domain ||= maildomain();
$smtp ||= Net::SMTP->new((defined $smtp_server_port)
? "$smtp_server:$smtp_server_port"
: $smtp_server,
$smtp ||= Net::SMTP->new(smtp_host_string(),
Hello => $smtp_domain,
Debug => $debug_net_smtp);
if ($smtp_encryption eq 'tls' && $smtp) {
@ -1185,31 +1224,7 @@ sub send_message {
defined $smtp_server_port ? " port=$smtp_server_port" : "";
}
if (defined $smtp_authuser) {
# Workaround AUTH PLAIN/LOGIN interaction defect
# with Authen::SASL::Cyrus
eval {
require Authen::SASL;
Authen::SASL->import(qw(Perl));
};
if (!defined $smtp_authpass) {
system "stty -echo";
do {
print "Password: ";
$_ = <STDIN>;
print "\n";
} while (!defined $_);
chomp($smtp_authpass = $_);
system "stty echo";
}
$auth ||= $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
}
smtp_auth_maybe or die $smtp->message;
$smtp->mail( $raw_from ) or die $smtp->message;
$smtp->to( @recipients ) or die $smtp->message;