3 # Versions up to 2.13 Copyright (c) 1995-1998 Graham Barr <gbarr@pobox.com>.
5 # Changes in Version 2.13_01 onwards Copyright (C) 2013-2014 Steve Hay. All
7 # This module is free software; you can redistribute it and/or modify it under
8 # the same terms as Perl itself, i.e. under the terms of either the GNU General
9 # Public License or the Artistic License, as specified in the F<LICENCE> file.
21 our $VERSION = "3.08";
28 my($class, $host) = @_;
32 $home = $ENV{HOME} || `pwd`;
34 $file = ($home =~ /:$/ ? $home . "netrc" : $home . ":netrc");
38 # Some OS's don't have "getpwuid", so we default to $ENV{HOME}
39 $home = eval { (getpwuid($>))[7] } || $ENV{HOME};
40 $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH} || '') if defined $ENV{HOMEDRIVE};
41 if (-e $home . "/.netrc") {
42 $file = $home . "/.netrc";
44 elsif (-e $home . "/_netrc") {
45 $file = $home . "/_netrc";
48 return unless $TESTING;
52 my ($login, $pass, $acct) = (undef, undef, undef);
56 $netrc{default} = undef;
58 # OS/2 and Win32 do not handle stat in a way compatible with this check :-(
64 my @stat = stat($file);
67 if ($stat[2] & 077) { ## no critic (ValuesAndExpressions::ProhibitLeadingZeros)
68 carp "Bad permissions: $file";
72 carp "Not owner: $file";
78 if ($fh = FileHandle->new($file, "r")) {
79 my ($mach, $macdef, $tok, @tok) = (0, 0);
82 undef $macdef if /\A\n\Z/;
92 while (length && s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//) {
93 (my $tok = $+) =~ s/\\(.)/$1/g;
99 if ($tok[0] eq "default") {
101 $mach = bless {}, $class;
102 $netrc{default} = [$mach];
112 if ($tok eq "machine") {
113 my $host = shift @tok;
114 $mach = bless {machine => $host}, $class;
117 unless exists($netrc{$host});
118 push(@{$netrc{$host}}, $mach);
120 elsif ($tok =~ /^(login|password|account)$/) {
121 next TOKEN unless $mach;
122 my $value = shift @tok;
124 # Following line added by rmerrell to remove '/' escape char in .netrc
125 $value =~ s/\/\\/\\/g;
126 $mach->{$1} = $value;
128 elsif ($tok eq "macdef") {
129 next TOKEN unless $mach;
130 my $value = shift @tok;
132 unless exists $mach->{macdef};
133 $macdef = $mach->{machdef}{$value} = [];
143 my ($class, $mach, $login) = @_;
146 unless exists $netrc{default};
150 if $mach eq 'default';
152 if (exists $netrc{$mach}) {
153 if (defined $login) {
154 foreach my $m (@{$netrc{$mach}}) {
156 if (exists $m->{login} && $m->{login} eq $login);
160 return $netrc{$mach}->[0];
163 return $netrc{default}->[0]
164 if defined $netrc{default};
182 exists $me->{account}
191 exists $me->{password}
199 ($me->login, $me->password, $me->account);
208 Net::Netrc - OO interface to users netrc file
214 $mach = Net::Netrc->lookup('some.machine');
215 $login = $mach->login;
216 ($login, $password, $account) = $mach->lpa;
220 C<Net::Netrc> is a class implementing a simple interface to the .netrc file
221 used as by the ftp program.
223 C<Net::Netrc> also implements security checks just like the ftp program,
224 these checks are, first that the .netrc file must be owned by the user and
225 second the ownership permissions should be such that only the owner has
226 read and write access. If these conditions are not met then a warning is
227 output and the .netrc file is not read.
229 =head1 THE .netrc FILE
231 The .netrc file contains login and initialization information used by the
232 auto-login process. It resides in the user's home directory. The following
233 tokens are recognized; they may be separated by spaces, tabs, or new-lines:
239 Identify a remote machine name. The auto-login process searches
240 the .netrc file for a machine token that matches the remote machine
241 specified. Once a match is made, the subsequent .netrc tokens
242 are processed, stopping when the end of file is reached or an-
243 other machine or a default token is encountered.
247 This is the same as machine name except that default matches
248 any name. There can be only one default token, and it must be
249 after all machine tokens. This is normally used as:
251 default login anonymous password user@site
253 thereby giving the user automatic anonymous login to machines
254 not specified in .netrc.
258 Identify a user on the remote machine. If this token is present,
259 the auto-login process will initiate a login using the
262 =item password string
264 Supply a password. If this token is present, the auto-login
265 process will supply the specified string if the remote server
266 requires a password as part of the login process.
270 Supply an additional account password. If this token is present,
271 the auto-login process will supply the specified string
272 if the remote server requires an additional account password.
276 Define a macro. C<Net::Netrc> only parses this field to be compatible
283 The constructor for a C<Net::Netrc> object is not called new as it does not
284 really create a new object. But instead is called C<lookup> as this is
285 essentially what it does.
289 =item lookup ( MACHINE [, LOGIN ])
291 Lookup and return a reference to the entry for C<MACHINE>. If C<LOGIN> is given
292 then the entry returned will have the given login. If C<LOGIN> is not given then
293 the first entry in the .netrc file for C<MACHINE> will be returned.
295 If a matching entry cannot be found, and a default entry exists, then a
296 reference to the default entry is returned.
298 If there is no matching entry found and there is no default defined, or
299 no .netrc file is found, then C<undef> is returned.
309 Return the login id for the netrc entry
313 Return the password for the netrc entry
317 Return the account information for the netrc entry
321 Return a list of login, password and account information for the netrc entry
327 Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
329 Steve Hay E<lt>F<shay@cpan.org>E<gt> is now maintaining libnet as of version
339 Versions up to 2.13 Copyright (c) 1995-1998 Graham Barr. All rights reserved.
340 Changes in Version 2.13_01 onwards Copyright (C) 2013-2014 Steve Hay. All
343 This module is free software; you can redistribute it and/or modify it under the
344 same terms as Perl itself, i.e. under the terms of either the GNU General Public
345 License or the Artistic License, as specified in the F<LICENCE> file.