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);
14 $VERSION = "2.12"; # $Id: //depot/libnet/Net/Netrc.pm#12 $
24 $home = $ENV{HOME} || `pwd`;
26 $file = ($home =~ /:$/ ? $home . "netrc" : $home . ":netrc");
28 # Some OS's don't have `getpwuid', so we default to $ENV{HOME}
29 $home = eval { (getpwuid($>))[7] } || $ENV{HOME};
30 $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH}||'') if defined $ENV{HOMEDRIVE};
31 $file = $home . "/.netrc";
34 my($login,$pass,$acct) = (undef,undef,undef);
38 $netrc{default} = undef;
40 # OS/2 and Win32 do not handle stat in a way compatable with this check :-(
46 my @stat = stat($file);
52 carp "Bad permissions: $file";
57 carp "Not owner: $file";
63 if($fh = FileHandle->new($file,"r"))
65 my($mach,$macdef,$tok,@tok) = (0,0);
69 undef $macdef if /\A\n\Z/;
80 while(length && s/^("([^"]*)"|(\S+))\s*//);
85 if($tok[0] eq "default")
89 $netrc{default} = [$mach];
101 my $host = shift @tok;
102 $mach = bless {machine => $host};
105 unless exists($netrc{$host});
106 push(@{$netrc{$host}}, $mach);
108 elsif($tok =~ /^(login|password|account)$/)
110 next TOKEN unless $mach;
111 my $value = shift @tok;
112 # Following line added by rmerrell to remove '/' escape char in .netrc
113 $value =~ s/\/\\/\\/g;
114 $mach->{$1} = $value;
116 elsif($tok eq "macdef")
118 next TOKEN unless $mach;
119 my $value = shift @tok;
121 unless exists $mach->{macdef};
122 $macdef = $mach->{machdef}{$value} = [];
132 my($pkg,$mach,$login) = @_;
135 unless exists $netrc{default};
139 if $mach eq 'default';
141 if(exists $netrc{$mach})
146 foreach $m (@{$netrc{$mach}})
149 if(exists $m->{login} && $m->{login} eq $login);
153 return $netrc{$mach}->[0]
156 return $netrc{default}->[0]
157 if defined $netrc{default};
175 exists $me->{account}
184 exists $me->{password}
192 ($me->login, $me->password, $me->account);
201 Net::Netrc - OO interface to users netrc file
207 $mach = Net::Netrc->lookup('some.machine');
208 $login = $mach->login;
209 ($login, $password, $account) = $mach->lpa;
213 C<Net::Netrc> is a class implementing a simple interface to the .netrc file
214 used as by the ftp program.
216 C<Net::Netrc> also implements security checks just like the ftp program,
217 these checks are, first that the .netrc file must be owned by the user and
218 second the ownership permissions should be such that only the owner has
219 read and write access. If these conditions are not met then a warning is
220 output and the .netrc file is not read.
222 =head1 THE .netrc FILE
224 The .netrc file contains login and initialization information used by the
225 auto-login process. It resides in the user's home directory. The following
226 tokens are recognized; they may be separated by spaces, tabs, or new-lines:
232 Identify a remote machine name. The auto-login process searches
233 the .netrc file for a machine token that matches the remote machine
234 specified. Once a match is made, the subsequent .netrc tokens
235 are processed, stopping when the end of file is reached or an-
236 other machine or a default token is encountered.
240 This is the same as machine name except that default matches
241 any name. There can be only one default token, and it must be
242 after all machine tokens. This is normally used as:
244 default login anonymous password user@site
246 thereby giving the user automatic anonymous login to machines
247 not specified in .netrc.
251 Identify a user on the remote machine. If this token is present,
252 the auto-login process will initiate a login using the
255 =item password string
257 Supply a password. If this token is present, the auto-login
258 process will supply the specified string if the remote server
259 requires a password as part of the login process.
263 Supply an additional account password. If this token is present,
264 the auto-login process will supply the specified string
265 if the remote server requires an additional account password.
269 Define a macro. C<Net::Netrc> only parses this field to be compatible
276 The constructor for a C<Net::Netrc> object is not called new as it does not
277 really create a new object. But instead is called C<lookup> as this is
278 essentially what it does.
282 =item lookup ( MACHINE [, LOGIN ])
284 Lookup and return a reference to the entry for C<MACHINE>. If C<LOGIN> is given
285 then the entry returned will have the given login. If C<LOGIN> is not given then
286 the first entry in the .netrc file for C<MACHINE> will be returned.
288 If a matching entry cannot be found, and a default entry exists, then a
289 reference to the default entry is returned.
291 If there is no matching entry found and there is no default defined, or
292 no .netrc file is found, then C<undef> is returned.
302 Return the login id for the netrc entry
306 Return the password for the netrc entry
310 Return the account information for the netrc entry
314 Return a list of login, password and account information fir the netrc entry
320 Graham Barr <gbarr@pobox.com>
329 Copyright (c) 1995-1998 Graham Barr. All rights reserved.
330 This program is free software; you can redistribute it and/or modify
331 it under the same terms as Perl itself.
335 $Id: //depot/libnet/Net/Netrc.pm#12 $