3 # Copyright (c) 1995-1998 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
12 use vars qw($VERSION);
24 $home = $ENV{HOME} || `pwd`;
26 $file = ($home =~ /:$/ ? $home . "netrc" : $home . ":netrc");
30 # Some OS's don't have `getpwuid', so we default to $ENV{HOME}
31 $home = eval { (getpwuid($>))[7] } || $ENV{HOME};
32 $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH} || '') if defined $ENV{HOMEDRIVE};
33 $file = $home . "/.netrc";
36 my ($login, $pass, $acct) = (undef, undef, undef);
40 $netrc{default} = undef;
42 # OS/2 and Win32 do not handle stat in a way compatable with this check :-(
48 my @stat = stat($file);
52 carp "Bad permissions: $file";
56 carp "Not owner: $file";
62 if ($fh = FileHandle->new($file, "r")) {
63 my ($mach, $macdef, $tok, @tok) = (0, 0);
66 undef $macdef if /\A\n\Z/;
76 while (length && s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//) {
77 (my $tok = $+) =~ s/\\(.)/$1/g;
83 if ($tok[0] eq "default") {
86 $netrc{default} = [$mach];
96 if ($tok eq "machine") {
97 my $host = shift @tok;
98 $mach = bless {machine => $host};
101 unless exists($netrc{$host});
102 push(@{$netrc{$host}}, $mach);
104 elsif ($tok =~ /^(login|password|account)$/) {
105 next TOKEN unless $mach;
106 my $value = shift @tok;
108 # Following line added by rmerrell to remove '/' escape char in .netrc
109 $value =~ s/\/\\/\\/g;
110 $mach->{$1} = $value;
112 elsif ($tok eq "macdef") {
113 next TOKEN unless $mach;
114 my $value = shift @tok;
116 unless exists $mach->{macdef};
117 $macdef = $mach->{machdef}{$value} = [];
127 my ($pkg, $mach, $login) = @_;
130 unless exists $netrc{default};
134 if $mach eq 'default';
136 if (exists $netrc{$mach}) {
137 if (defined $login) {
139 foreach $m (@{$netrc{$mach}}) {
141 if (exists $m->{login} && $m->{login} eq $login);
145 return $netrc{$mach}->[0];
148 return $netrc{default}->[0]
149 if defined $netrc{default};
167 exists $me->{account}
176 exists $me->{password}
184 ($me->login, $me->password, $me->account);
193 Net::Netrc - OO interface to users netrc file
199 $mach = Net::Netrc->lookup('some.machine');
200 $login = $mach->login;
201 ($login, $password, $account) = $mach->lpa;
205 C<Net::Netrc> is a class implementing a simple interface to the .netrc file
206 used as by the ftp program.
208 C<Net::Netrc> also implements security checks just like the ftp program,
209 these checks are, first that the .netrc file must be owned by the user and
210 second the ownership permissions should be such that only the owner has
211 read and write access. If these conditions are not met then a warning is
212 output and the .netrc file is not read.
214 =head1 THE .netrc FILE
216 The .netrc file contains login and initialization information used by the
217 auto-login process. It resides in the user's home directory. The following
218 tokens are recognized; they may be separated by spaces, tabs, or new-lines:
224 Identify a remote machine name. The auto-login process searches
225 the .netrc file for a machine token that matches the remote machine
226 specified. Once a match is made, the subsequent .netrc tokens
227 are processed, stopping when the end of file is reached or an-
228 other machine or a default token is encountered.
232 This is the same as machine name except that default matches
233 any name. There can be only one default token, and it must be
234 after all machine tokens. This is normally used as:
236 default login anonymous password user@site
238 thereby giving the user automatic anonymous login to machines
239 not specified in .netrc.
243 Identify a user on the remote machine. If this token is present,
244 the auto-login process will initiate a login using the
247 =item password string
249 Supply a password. If this token is present, the auto-login
250 process will supply the specified string if the remote server
251 requires a password as part of the login process.
255 Supply an additional account password. If this token is present,
256 the auto-login process will supply the specified string
257 if the remote server requires an additional account password.
261 Define a macro. C<Net::Netrc> only parses this field to be compatible
268 The constructor for a C<Net::Netrc> object is not called new as it does not
269 really create a new object. But instead is called C<lookup> as this is
270 essentially what it does.
274 =item lookup ( MACHINE [, LOGIN ])
276 Lookup and return a reference to the entry for C<MACHINE>. If C<LOGIN> is given
277 then the entry returned will have the given login. If C<LOGIN> is not given then
278 the first entry in the .netrc file for C<MACHINE> will be returned.
280 If a matching entry cannot be found, and a default entry exists, then a
281 reference to the default entry is returned.
283 If there is no matching entry found and there is no default defined, or
284 no .netrc file is found, then C<undef> is returned.
294 Return the login id for the netrc entry
298 Return the password for the netrc entry
302 Return the account information for the netrc entry
306 Return a list of login, password and account information fir the netrc entry
312 Graham Barr <gbarr@pobox.com>
321 Copyright (c) 1995-1998 Graham Barr. All rights reserved.
322 This program is free software; you can redistribute it and/or modify
323 it under the same terms as Perl itself.