All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] git-send-email: add ~/.authinfo parsing
@ 2013-01-29 19:13 Michal Nazarewicz
  2013-01-29 19:53 ` Junio C Hamano
  0 siblings, 1 reply; 78+ messages in thread
From: Michal Nazarewicz @ 2013-01-29 19:13 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Krzysztof Mazur, Michal Nazarewicz

From: Michal Nazarewicz <mina86@mina86.com>

Make git-send-email read password from a ~/.authinfo file instead of
requiring it to be stored in git configuration, passed as command line
argument or typed in.

There are various other applications that use this file for
authentication information so letting users use it for git-send-email
is convinient.  Furthermore, some users store their ~/.gitconfig file
in a public repository and having to store password there makes it
easy to publish the password.

Not to introduce any new dependencies, ~/.authinfo file is parsed only
if Text::CSV Perl module is installed.  If it's not, a notification is
printed and the file is ignored.

Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
---
 Documentation/git-send-email.txt | 15 +++++++--
 git-send-email.perl              | 69 +++++++++++++++++++++++++++++++++-------
 2 files changed, 70 insertions(+), 14 deletions(-)

diff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt
index eeb561c..b83576e 100644
--- a/Documentation/git-send-email.txt
+++ b/Documentation/git-send-email.txt
@@ -158,14 +158,25 @@ Sending
 --smtp-pass[=<password>]::
 	Password for SMTP-AUTH. The argument is optional: If no
 	argument is specified, then the empty string is used as
-	the password. Default is the value of 'sendemail.smtppass',
-	however '--smtp-pass' always overrides this value.
+	the password. Default is the value of 'sendemail.smtppass'
+	or value read from '~/.authinfo' file, however '--smtp-pass'
+	always overrides this value.
 +
 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.
++
+The '~/.authinfo' file is read if Text::CSV Perl module is installed
+on the system; if it's missing, a notification message will be printed
+and the file ignored altogether.  The file should contain a line with
+the following format:
++
+  machine <domain> port <port> login <user> password <pass>
++
+Contrary to other tools, 'git-send-email' does not support symbolic
+port names like 'imap' thus `<port>` must be a number.
 
 --smtp-server=<host>::
 	If set, specifies the outgoing SMTP server to use (e.g.
diff --git a/git-send-email.perl b/git-send-email.perl
index be809e5..d824098 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -1045,6 +1045,62 @@ sub maildomain {
 	return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
 }
 
+
+sub read_password_from_stdin {
+	my $line;
+
+	system "stty -echo";
+
+	do {
+		print "Password: ";
+		$line = <STDIN>;
+		print "\n";
+	} while (!defined $line);
+
+	system "stty echo";
+
+	chomp $line;
+	return $line;
+}
+
+sub read_password_from_authinfo {
+	my $fd;
+	if (!open $fd, '<', $ENV{'HOME'} . '/.authinfo') {
+		return;
+	}
+
+	if (!eval { require Text::CSV; 1 }) {
+		print STDERR "Text::CSV missing, won't read ~/.authinfo\n";
+		close $fd;
+		return;
+	}
+
+	my $csv = Text::CSV->new( { sep_char => ' ' } );
+	my $password;
+	while (my $line = <$fd>) {
+		chomp $line;
+		$csv->parse($line);
+		my %row = $csv->fields();
+		if (defined $row{'machine'} &&
+		    defined $row{'login'} &&
+		    defined $row{'port'} &&
+		    defined $row{'password'} &&
+		    $row{'machine'} eq $smtp_server &&
+		    $row{'login'} eq $smtp_authuser &&
+		    $row{'port'} eq $smtp_server_port) {
+			$password = $row{'password'};
+			last;
+		}
+	}
+
+	close $fd;
+	return $password;
+}
+
+sub read_password {
+	return read_password_from_authinfo || read_password_from_stdin;
+}
+
 # Returns 1 if the message was sent, and 0 otherwise.
 # In actuality, the whole program dies when there
 # is an error sending a message.
@@ -1194,18 +1250,7 @@ X-Mailer: git-send-email $gitversion
 			};
 
 			if (!defined $smtp_authpass) {
-
-				system "stty -echo";
-
-				do {
-					print "Password: ";
-					$_ = <STDIN>;
-					print "\n";
-				} while (!defined $_);
-
-				chomp($smtp_authpass = $_);
-
-				system "stty echo";
+				$smtp_authpass = read_password
 			}
 
 			$auth ||= $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message;
-- 
1.8.1

^ permalink raw reply related	[flat|nested] 78+ messages in thread

end of thread, other threads:[~2013-02-08 18:16 UTC | newest]

Thread overview: 78+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-29 19:13 [PATCH] git-send-email: add ~/.authinfo parsing Michal Nazarewicz
2013-01-29 19:53 ` Junio C Hamano
2013-01-29 21:08   ` [PATCHv2] " Michal Nazarewicz
2013-01-29 22:38     ` Junio C Hamano
2013-01-30  0:03       ` [PATCHv3] " Michal Nazarewicz
2013-01-30  0:34         ` Junio C Hamano
2013-01-30  7:43   ` [PATCH] " Jeff King
2013-01-30 15:57     ` Junio C Hamano
2013-01-31 15:23       ` Ted Zlatanov
2013-01-31 19:38         ` Jeff King
2013-02-02 11:57           ` Ted Zlatanov
2013-02-03 19:41             ` Jeff King
2013-02-04 16:40               ` Ted Zlatanov
2013-02-04 16:42               ` [PATCH 1/3] Add contrib/credentials/netrc with GPG support Ted Zlatanov
2013-02-04 16:57                 ` Ted Zlatanov
2013-02-04 17:24                 ` Junio C Hamano
2013-02-04 18:33                   ` Ted Zlatanov
2013-02-04 19:06                     ` Junio C Hamano
2013-02-04 19:40                       ` Ted Zlatanov
2013-02-04 23:10                         ` Junio C Hamano
2013-02-06 15:10                           ` CodingGuidelines Perl amendment (was: [PATCH 1/3] Add contrib/credentials/netrc with GPG support) Ted Zlatanov
2013-02-06 16:29                             ` CodingGuidelines Perl amendment Junio C Hamano
2013-02-06 17:45                               ` demerphq
2013-02-06 18:08                                 ` Ted Zlatanov
2013-02-06 18:14                                 ` Junio C Hamano
2013-02-06 18:18                                   ` demerphq
2013-02-06 17:55                               ` [PATCH] Update CodingGuidelines for Perl 5 Ted Zlatanov
2013-02-06 18:05                               ` CodingGuidelines Perl amendment Ted Zlatanov
2013-02-06 18:16                                 ` Junio C Hamano
2013-02-06 18:27                                   ` Ted Zlatanov
2013-02-06 18:25                                 ` demerphq
2013-02-06 18:35                                   ` Ted Zlatanov
2013-02-06 18:44                                     ` demerphq
2013-02-06 18:54                                       ` Ted Zlatanov
2013-02-06 19:37                                         ` Junio C Hamano
2013-02-06 19:49                                           ` [PATCH v2] Update CodingGuidelines for Perl 5 Ted Zlatanov
2013-02-04 16:42               ` [PATCH 2/3] Skip blank and commented lines in contrib/credentials/netrc Ted Zlatanov
2013-02-04 16:43               ` [PATCH 3/3] Fix contrib/credentials/netrc minor issues: exit quietly; use 3-parameter open; etc Ted Zlatanov
2013-02-04 17:27                 ` Junio C Hamano
2013-02-04 18:41                   ` Ted Zlatanov
2013-02-04 16:33     ` [PATCH] git-send-email: add ~/.authinfo parsing Michal Nazarewicz
2013-02-04 17:00       ` Ted Zlatanov
2013-02-04 20:10         ` Jeff King
2013-02-04 20:28           ` Ted Zlatanov
2013-02-04 20:59             ` Jeff King
2013-02-04 21:08               ` Ted Zlatanov
2013-02-04 21:22                 ` Jeff King
2013-02-04 21:41                   ` Ted Zlatanov
2013-02-05 23:10     ` Junio C Hamano
2013-02-06  8:11       ` Matthieu Moy
2013-02-06 14:53         ` Ted Zlatanov
2013-02-06 15:10           ` Matthieu Moy
2013-02-06 15:58             ` Ted Zlatanov
2013-02-06 16:41               ` Matthieu Moy
2013-02-06 17:40                 ` Ted Zlatanov
2013-02-06 18:11                 ` [PATCH 0/4] Allow contrib/ to use Git's Makefile for perl code Matthieu Moy
2013-02-06 18:11                   ` [PATCH 1/4] Makefile: extract perl-related rules to make them available from other dirs Matthieu Moy
2013-02-07 19:16                     ` Junio C Hamano
2013-02-06 18:11                   ` [PATCH 2/4] perl.mak: introduce $(GIT_ROOT_DIR) to allow inclusion from other directories Matthieu Moy
2013-02-06 18:11                   ` [PATCH 3/4] Makefile: factor common configuration in git-default-config.mak Matthieu Moy
2013-02-07 19:28                     ` Junio C Hamano
2013-02-08 17:05                       ` Matthieu Moy
2013-02-08 17:31                         ` [PATCH 1/2] Makefile: make script-related rules usable from subdirectories Matthieu Moy
2013-02-08 17:31                           ` [PATCH 2/2] git-remote-mediawiki: use toplevel's Makefile Matthieu Moy
2013-02-06 18:11                   ` [PATCH 4/4] git-remote-mediawiki: use Git's Makefile to build the script Matthieu Moy
2013-02-07 19:28                     ` Junio C Hamano
2013-02-08  4:28                       ` Jeff King
2013-02-08 17:34                         ` Matthieu Moy
2013-02-08 17:43                           ` Jeff King
2013-02-08 18:13                             ` Junio C Hamano
2013-02-08 18:15                               ` Jeff King
2013-02-06 21:57               ` [PATCH] git-send-email: add ~/.authinfo parsing Jeff King
2013-02-06 23:12                 ` Ted Zlatanov
2013-02-07  7:08                   ` Matthieu Moy
2013-02-07 14:30                     ` Ted Zlatanov
2013-02-06 13:26       ` Michal Nazarewicz
2013-02-06 14:47         ` Ted Zlatanov
2013-01-30 15:03   ` Ted Zlatanov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.