Skip to content

Commit db4a912

Browse files
committed
Merge branch 'mc/netrc-service-names'
"netrc" credential helper has been improved to understand textual service names (like smtp) in addition to the numeric port numbers (like 25). * mc/netrc-service-names: contrib: better support symbolic port names in git-credential-netrc contrib: warn for invalid netrc file ports in git-credential-netrc contrib: use a more portable shebang for git-credential-netrc
2 parents 0d046cb + 1926d9b commit db4a912

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

contrib/credential/netrc/git-credential-netrc.perl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/perl
1+
#!/usr/bin/env perl
22

33
use strict;
44
use warnings;
@@ -267,8 +267,16 @@ sub load_netrc {
267267
if (!defined $nentry->{machine}) {
268268
next;
269269
}
270-
if (defined $nentry->{port} && $nentry->{port} =~ m/^\d+$/) {
271-
$num_port = $nentry->{port};
270+
if (defined $nentry->{port}) {
271+
$num_port = Git::port_num($nentry->{port});
272+
unless ($num_port) {
273+
printf(STDERR "ignoring invalid port `%s' " .
274+
"from netrc file\n", $nentry->{port});
275+
}
276+
# Since we've already validated and converted
277+
# the port to its numerical value, do not
278+
# capture it as the `protocol' value, as used
279+
# to be the case for symbolic port names.
272280
delete $nentry->{port};
273281
}
274282

contrib/credential/netrc/test.pl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ BEGIN
4545
diag "Testing with invalid data\n";
4646
$cred = run_credential(['-f', $netrc, 'get'],
4747
"bad data");
48-
ok(scalar keys %$cred == 4, "Got first found keys with bad data");
48+
ok(scalar keys %$cred == 3, "Got first found keys with bad data");
4949

5050
diag "Testing netrc file for a missing corovamilkbar entry\n";
5151
$cred = run_credential(['-f', $netrc, 'get'],
@@ -64,12 +64,12 @@ BEGIN
6464

6565
diag "Testing netrc file for a username-specific entry\n";
6666
$cred = run_credential(['-f', $netrc, 'get'],
67-
{ host => 'imap', username => 'bob' });
67+
{ host => 'imap:993', username => 'bob' });
6868

69-
ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
69+
# Only the password field gets returned.
70+
ok(scalar keys %$cred == 1, "Got 1 username-specific keys");
7071

7172
is($cred->{password}, 'bobwillknow', "Got correct user-specific password");
72-
is($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
7373

7474
diag "Testing netrc file for a host:port-specific entry\n";
7575
$cred = run_credential(['-f', $netrc, 'get'],

git-send-email.perl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,17 @@ sub initialize_modified_loop_vars {
21122112
}
21132113
}
21142114

2115+
# Validate the SMTP server port, if provided.
2116+
if (defined $smtp_server_port) {
2117+
my $port = Git::port_num($smtp_server_port);
2118+
if ($port) {
2119+
$smtp_server_port = $port;
2120+
} else {
2121+
die sprintf(__("error: invalid SMTP port '%s'\n"),
2122+
$smtp_server_port);
2123+
}
2124+
}
2125+
21152126
# Run the loop once again to avoid gaps in the counter due to FIFO
21162127
# arguments provided by the user.
21172128
my $num = 1;

perl/Git.pm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,19 @@ sub _close_cat_blob {
10611061
delete @$self{@vars};
10621062
}
10631063

1064+
# Given PORT, a port number or service name, return its numerical
1065+
# value else undef.
1066+
sub port_num {
1067+
my ($port) = @_;
1068+
1069+
# Port can be either a positive integer within the 16-bit range...
1070+
if ($port =~ /^\d+$/ && $port > 0 && $port <= (2**16 - 1)) {
1071+
return $port;
1072+
}
1073+
1074+
# ... or a symbolic port (service name).
1075+
return scalar getservbyname($port, '');
1076+
}
10641077

10651078
=item credential_read( FILEHANDLE )
10661079

t/t9001-send-email.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ test_expect_success $PREREQ 'cc trailer with get_maintainer.pl output' '
201201
test_cmp expected-cc commandline1
202202
'
203203

204+
test_expect_failure $PREREQ 'invalid smtp server port value' '
205+
clean_fake_sendmail &&
206+
git send-email -1 [email protected] \
207+
--smtp-server-port=bogus-symbolic-name \
208+
--smtp-server="$(pwd)/fake.sendmail"
209+
'
210+
204211
test_expect_success $PREREQ 'setup expect' "
205212
cat >expected-show-all-headers <<\EOF
206213
0001-Second.patch

0 commit comments

Comments
 (0)