line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Net::Netrc.pm |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (C) 1995-1998 Graham Barr. All rights reserved. |
4
|
|
|
|
|
|
|
# Copyright (C) 2013-2014, 2020 Steve Hay. All rights reserved. |
5
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or modify it under |
6
|
|
|
|
|
|
|
# the same terms as Perl itself, i.e. under the terms of either the GNU General |
7
|
|
|
|
|
|
|
# Public License or the Artistic License, as specified in the F file. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Net::Netrc; |
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
4539
|
use 5.008001; |
|
2
|
|
|
|
|
9
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
17
|
use strict; |
|
2
|
|
|
|
|
18
|
|
|
2
|
|
|
|
|
109
|
|
14
|
2
|
|
|
2
|
|
17
|
use warnings; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
97
|
|
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
2
|
|
17
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
216
|
|
17
|
2
|
|
|
2
|
|
506
|
use FileHandle; |
|
2
|
|
|
|
|
8839
|
|
|
2
|
|
|
|
|
1307
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = "3.15"; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $TESTING; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my %netrc = (); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _readrc { |
26
|
2
|
|
|
2
|
|
2938
|
my($class, $host) = @_; |
27
|
2
|
|
|
|
|
5
|
my ($home, $file); |
28
|
|
|
|
|
|
|
|
29
|
2
|
50
|
|
|
|
11
|
if ($^O eq "MacOS") { |
30
|
0
|
|
0
|
|
|
0
|
$home = $ENV{HOME} || `pwd`; |
31
|
0
|
|
|
|
|
0
|
chomp($home); |
32
|
0
|
0
|
|
|
|
0
|
$file = ($home =~ /:$/ ? $home . "netrc" : $home . ":netrc"); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
else { |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Some OS's don't have "getpwuid", so we default to $ENV{HOME} |
37
|
2
|
|
33
|
|
|
5
|
$home = eval { (getpwuid($>))[7] } || $ENV{HOME}; |
38
|
2
|
50
|
0
|
|
|
5823
|
$home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH} || '') if defined $ENV{HOMEDRIVE}; |
|
|
|
0
|
|
|
|
|
39
|
2
|
50
|
|
|
|
95
|
if (-e $home . "/.netrc") { |
|
|
50
|
|
|
|
|
|
40
|
0
|
|
|
|
|
0
|
$file = $home . "/.netrc"; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
elsif (-e $home . "/_netrc") { |
43
|
0
|
|
|
|
|
0
|
$file = $home . "/_netrc"; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
else { |
46
|
2
|
50
|
|
|
|
18
|
return unless $TESTING; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
2
|
|
|
|
|
9
|
my ($login, $pass, $acct) = (undef, undef, undef); |
51
|
2
|
|
|
|
|
4
|
my $fh; |
52
|
2
|
|
|
|
|
13
|
local $_; |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
16
|
$netrc{default} = undef; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# OS/2 and Win32 do not handle stat in a way compatible with this check :-( |
57
|
2
|
50
|
33
|
|
|
84
|
unless ($^O eq 'os2' |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
58
|
|
|
|
|
|
|
|| $^O eq 'MSWin32' |
59
|
|
|
|
|
|
|
|| $^O eq 'MacOS' |
60
|
|
|
|
|
|
|
|| $^O =~ /^cygwin/) |
61
|
|
|
|
|
|
|
{ |
62
|
2
|
|
|
|
|
34
|
my @stat = stat($file); |
63
|
|
|
|
|
|
|
|
64
|
2
|
50
|
|
|
|
24
|
if (@stat) { |
65
|
2
|
100
|
|
|
|
13
|
if ($stat[2] & 077) { ## no critic (ValuesAndExpressions::ProhibitLeadingZeros) |
66
|
1
|
|
|
|
|
60
|
carp "Bad permissions: $file"; |
67
|
1
|
|
|
|
|
413
|
return; |
68
|
|
|
|
|
|
|
} |
69
|
1
|
50
|
|
|
|
40
|
if ($stat[4] != $<) { |
70
|
0
|
|
|
|
|
0
|
carp "Not owner: $file"; |
71
|
0
|
|
|
|
|
0
|
return; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
1
|
50
|
|
|
|
23
|
if ($fh = FileHandle->new($file, "r")) { |
77
|
1
|
|
|
|
|
156
|
my ($mach, $macdef, $tok, @tok) = (0, 0); |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
11
|
while (<$fh>) { |
80
|
10
|
50
|
|
|
|
88
|
undef $macdef if /\A\n\Z/; |
81
|
|
|
|
|
|
|
|
82
|
10
|
50
|
|
|
|
19
|
if ($macdef) { |
83
|
0
|
|
|
|
|
0
|
push(@$macdef, $_); |
84
|
0
|
|
|
|
|
0
|
next; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
10
|
|
|
|
|
39
|
s/^\s*//; |
88
|
10
|
|
|
|
|
17
|
chomp; |
89
|
|
|
|
|
|
|
|
90
|
10
|
|
66
|
|
|
74
|
while (length && s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//) { |
91
|
29
|
|
|
|
|
76
|
(my $tok = $+) =~ s/\\(.)/$1/g; |
92
|
29
|
|
|
|
|
137
|
push(@tok, $tok); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
TOKEN: |
96
|
10
|
|
|
|
|
26
|
while (@tok) { |
97
|
20
|
100
|
|
|
|
41
|
if ($tok[0] eq "default") { |
98
|
2
|
|
|
|
|
2
|
shift(@tok); |
99
|
2
|
|
|
|
|
10
|
$mach = bless {}, $class; |
100
|
2
|
|
|
|
|
23
|
$netrc{default} = [$mach]; |
101
|
|
|
|
|
|
|
|
102
|
2
|
|
|
|
|
6
|
next TOKEN; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
last TOKEN |
106
|
18
|
100
|
|
|
|
39
|
unless @tok > 1; |
107
|
|
|
|
|
|
|
|
108
|
15
|
|
|
|
|
19
|
$tok = shift(@tok); |
109
|
|
|
|
|
|
|
|
110
|
15
|
100
|
|
|
|
73
|
if ($tok eq "machine") { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
111
|
2
|
|
|
|
|
3
|
my $host = shift @tok; |
112
|
2
|
|
|
|
|
11
|
$mach = bless {machine => $host}, $class; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$netrc{$host} = [] |
115
|
2
|
100
|
|
|
|
11
|
unless exists($netrc{$host}); |
116
|
2
|
|
|
|
|
3
|
push(@{$netrc{$host}}, $mach); |
|
2
|
|
|
|
|
14
|
|
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
elsif ($tok =~ /^(login|password|account)$/) { |
119
|
10
|
100
|
|
|
|
26
|
next TOKEN unless $mach; |
120
|
9
|
|
|
|
|
11
|
my $value = shift @tok; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Following line added by rmerrell to remove '/' escape char in .netrc |
123
|
9
|
|
|
|
|
19
|
$value =~ s/\/\\/\\/g; |
124
|
9
|
|
|
|
|
43
|
$mach->{$1} = $value; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
elsif ($tok eq "macdef") { |
127
|
1
|
50
|
|
|
|
14
|
next TOKEN unless $mach; |
128
|
0
|
|
|
|
|
0
|
my $value = shift @tok; |
129
|
|
|
|
|
|
|
$mach->{macdef} = {} |
130
|
0
|
0
|
|
|
|
0
|
unless exists $mach->{macdef}; |
131
|
0
|
|
|
|
|
0
|
$macdef = $mach->{machdef}{$value} = []; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
1
|
|
|
|
|
6
|
$fh->close(); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub lookup { |
141
|
5
|
|
|
5
|
1
|
1415
|
my ($class, $mach, $login) = @_; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$class->_readrc() |
144
|
5
|
50
|
|
|
|
17
|
unless exists $netrc{default}; |
145
|
|
|
|
|
|
|
|
146
|
5
|
|
100
|
|
|
27
|
$mach ||= 'default'; |
147
|
5
|
100
|
|
|
|
56
|
undef $login |
148
|
|
|
|
|
|
|
if $mach eq 'default'; |
149
|
|
|
|
|
|
|
|
150
|
5
|
100
|
|
|
|
13
|
if (exists $netrc{$mach}) { |
151
|
4
|
100
|
|
|
|
7
|
if (defined $login) { |
152
|
1
|
|
|
|
|
1
|
foreach my $m (@{$netrc{$mach}}) { |
|
1
|
|
|
|
|
10
|
|
153
|
|
|
|
|
|
|
return $m |
154
|
2
|
100
|
66
|
|
|
15
|
if (exists $m->{login} && $m->{login} eq $login); |
155
|
|
|
|
|
|
|
} |
156
|
0
|
|
|
|
|
0
|
return; |
157
|
|
|
|
|
|
|
} |
158
|
3
|
|
|
|
|
19
|
return $netrc{$mach}->[0]; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
return $netrc{default}->[0] |
162
|
1
|
50
|
|
|
|
10
|
if defined $netrc{default}; |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
0
|
return; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub login { |
169
|
4
|
|
|
4
|
1
|
6
|
my $me = shift; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
exists $me->{login} |
172
|
|
|
|
|
|
|
? $me->{login} |
173
|
4
|
100
|
|
|
|
22
|
: undef; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub account { |
178
|
4
|
|
|
4
|
1
|
530
|
my $me = shift; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
exists $me->{account} |
181
|
|
|
|
|
|
|
? $me->{account} |
182
|
4
|
100
|
|
|
|
30
|
: undef; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub password { |
187
|
4
|
|
|
4
|
1
|
515
|
my $me = shift; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
exists $me->{password} |
190
|
|
|
|
|
|
|
? $me->{password} |
191
|
4
|
100
|
|
|
|
16
|
: undef; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub lpa { |
196
|
2
|
|
|
2
|
1
|
525
|
my $me = shift; |
197
|
2
|
|
|
|
|
6
|
($me->login, $me->password, $me->account); |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
__END__ |