3 # Copyright (c) 1995 Graham Barr <Graham.Barr@tiuk.ti.com>. All rights
4 # reserved. This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
11 Net::Netrc - OO interface to users netrc file
17 $mach = Net::Netrc->lookup('some.machine');
18 $login = $mach->login;
19 ($login, $password, $account) = $mach->lpa;
23 C<Net::Netrc> is a class implementing a simple interface to the .netrc file
24 used as by the ftp program.
26 C<Net::Netrc> also implements security checks just like the ftp program,
27 these checks are, first that the .netrc file must be owned by the user and
28 second the ownership permissions should be such that only the owner has
29 read and write access. If these conditions are not met then a warning is
30 output and the .netrc file is not read.
32 =head1 THE .netrc FILE
34 The .netrc file contains login and initialization information used by the
35 auto-login process. It resides in the user's home directory. The following
36 tokens are recognized; they may be separated by spaces, tabs, or new-lines:
42 Identify a remote machine name. The auto-login process searches
43 the .netrc file for a machine token that matches the remote machine
44 specified. Once a match is made, the subsequent .netrc tokens
45 are processed, stopping when the end of file is reached or an-
46 other machine or a default token is encountered.
50 This is the same as machine name except that default matches
51 any name. There can be only one default token, and it must be
52 after all machine tokens. This is normally used as:
54 default login anonymous password user@site
56 thereby giving the user automatic anonymous login to machines
57 not specified in .netrc.
61 Identify a user on the remote machine. If this token is present,
62 the auto-login process will initiate a login using the
67 Supply a password. If this token is present, the auto-login
68 process will supply the specified string if the remote server
69 requires a password as part of the login process.
73 Supply an additional account password. If this token is present,
74 the auto-login process will supply the specified string
75 if the remote server requires an additional account password.
79 Define a macro. C<Net::Netrc> only parses this field to be compatible
86 The constructor for a C<Net::Netrc> object is not called new as it does not
87 really create a new object. But instead is called C<lookup> as this is
88 essentially what it deos.
92 =item lookup ( MACHINE [, LOGIN ])
94 Lookup and return a reference to the entry for C<MACHINE>. If C<LOGIN> is given
95 then the entry returned will have the given login. If C<LOGIN> is not given then
96 the first entry in the .netrc file for C<MACHINE> will be returned.
98 If a matching entry cannot be found, and a default entry exists, then a
99 reference to the default entry is returned.
109 Return the login id for the netrc entry
113 Return the password for the netrc entry
117 Return the account information for the netrc entry
121 Return a list of login, password and account information fir the netrc entry
127 Graham Barr <Graham.Barr@tiuk.ti.com>
140 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
141 software; you can redistribute it and/or modify it under the same terms
149 use vars qw($VERSION);
151 $VERSION = sprintf("%d.%02d", q$Revision: 2.1 $ =~ /(\d+)\.(\d+)/);
159 # Some OS's don't have `getpwuid', so we default to $ENV{HOME}
160 my $home = eval { (getpwuid($>))[7] } || $ENV{HOME};
161 my $file = $home . "/.netrc";
163 my($login,$pass,$acct) = (undef,undef,undef);
167 $netrc{default} = undef;
169 # OS/2 does not handle stat in a way compatable with this check :-(
172 my @stat = stat($file);
178 carp "Bad permissions: $file";
183 carp "Not owner: $file";
189 if($fh = FileHandle->new($file,"r"))
191 my($mach,$macdef,$tok,@tok) = (0,0);
195 undef $macdef if /\A\n\Z/;
203 push(@tok, split(/[\s\n]+/, $_));
208 if($tok[0] eq "default")
212 $netrc{default} = [$mach];
222 if($tok eq "machine")
224 my $host = shift @tok;
225 $mach = bless {machine => $mach};
228 unless exists($netrc{$host});
229 push(@{$netrc{$host}}, $mach);
231 elsif($tok =~ /^(login|password|account)$/)
233 next TOKEN unless $mach;
234 my $value = shift @tok;
235 $mach->{$1} = $value;
237 elsif($tok eq "macdef")
239 next TOKEN unless $mach;
240 my $value = shift @tok;
242 unless exists $mach->{macdef};
243 $macdef = $mach->{machdef}{$value} = [];
253 my($pkg,$mach,$login) = @_;
256 unless exists $netrc{default};
260 if $mach eq 'default';
262 if(exists $netrc{$mach})
267 foreach $m (@{$netrc{$mach}})
270 if(exists $m->{login} && $m->{login} eq $login);
274 return $netrc{$mach}->[0]
277 return $netrc{default}
278 if defined $netrc{default};
296 exists $me->{account}
305 exists $me->{password}
313 ($me->login, $me->password, $me->account);