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.10"; # $Id: //depot/libnet/Net/Netrc.pm#4$
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 $file = $home . "/.netrc";
33 my($login,$pass,$acct) = (undef,undef,undef);
37 $netrc{default} = undef;
39 # OS/2 and Win32 do not handle stat in a way compatable with this check :-(
40 unless($^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'MacOS')
42 my @stat = stat($file);
48 carp "Bad permissions: $file";
53 carp "Not owner: $file";
59 if($fh = FileHandle->new($file,"r"))
61 my($mach,$macdef,$tok,@tok) = (0,0);
65 undef $macdef if /\A\n\Z/;
76 while(length && s/^("([^"]*)"|(\S+))\s*//);
81 if($tok[0] eq "default")
85 $netrc{default} = [$mach];
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)$/)
106 next TOKEN unless $mach;
107 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")
114 next TOKEN unless $mach;
115 my $value = shift @tok;
117 unless exists $mach->{macdef};
118 $macdef = $mach->{machdef}{$value} = [];
128 my($pkg,$mach,$login) = @_;
131 unless exists $netrc{default};
135 if $mach eq 'default';
137 if(exists $netrc{$mach})
142 foreach $m (@{$netrc{$mach}})
145 if(exists $m->{login} && $m->{login} eq $login);
149 return $netrc{$mach}->[0]
152 return $netrc{default}->[0]
153 if defined $netrc{default};
171 exists $me->{account}
180 exists $me->{password}
188 ($me->login, $me->password, $me->account);
197 Net::Netrc - OO interface to users netrc file
203 $mach = Net::Netrc->lookup('some.machine');
204 $login = $mach->login;
205 ($login, $password, $account) = $mach->lpa;
209 C<Net::Netrc> is a class implementing a simple interface to the .netrc file
210 used as by the ftp program.
212 C<Net::Netrc> also implements security checks just like the ftp program,
213 these checks are, first that the .netrc file must be owned by the user and
214 second the ownership permissions should be such that only the owner has
215 read and write access. If these conditions are not met then a warning is
216 output and the .netrc file is not read.
218 =head1 THE .netrc FILE
220 The .netrc file contains login and initialization information used by the
221 auto-login process. It resides in the user's home directory. The following
222 tokens are recognized; they may be separated by spaces, tabs, or new-lines:
228 Identify a remote machine name. The auto-login process searches
229 the .netrc file for a machine token that matches the remote machine
230 specified. Once a match is made, the subsequent .netrc tokens
231 are processed, stopping when the end of file is reached or an-
232 other machine or a default token is encountered.
236 This is the same as machine name except that default matches
237 any name. There can be only one default token, and it must be
238 after all machine tokens. This is normally used as:
240 default login anonymous password user@site
242 thereby giving the user automatic anonymous login to machines
243 not specified in .netrc.
247 Identify a user on the remote machine. If this token is present,
248 the auto-login process will initiate a login using the
251 =item password string
253 Supply a password. If this token is present, the auto-login
254 process will supply the specified string if the remote server
255 requires a password as part of the login process.
259 Supply an additional account password. If this token is present,
260 the auto-login process will supply the specified string
261 if the remote server requires an additional account password.
265 Define a macro. C<Net::Netrc> only parses this field to be compatible
272 The constructor for a C<Net::Netrc> object is not called new as it does not
273 really create a new object. But instead is called C<lookup> as this is
274 essentially what it does.
278 =item lookup ( MACHINE [, LOGIN ])
280 Lookup and return a reference to the entry for C<MACHINE>. If C<LOGIN> is given
281 then the entry returned will have the given login. If C<LOGIN> is not given then
282 the first entry in the .netrc file for C<MACHINE> will be returned.
284 If a matching entry cannot be found, and a default entry exists, then a
285 reference to the default entry is returned.
295 Return the login id for the netrc entry
299 Return the password for the netrc entry
303 Return the account information for the netrc entry
307 Return a list of login, password and account information fir the netrc entry
313 Graham Barr <gbarr@pobox.com>
322 Copyright (c) 1995-1998 Graham Barr. All rights reserved.
323 This program is free software; you can redistribute it and/or modify
324 it under the same terms as Perl itself.