Let perl_clone copy PL_exit_flags
[p5sagit/p5-mst-13.2.git] / lib / Net / Netrc.pm
CommitLineData
406c51ee 1# Net::Netrc.pm
2#
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.
6
7package Net::Netrc;
8
9use Carp;
10use strict;
11use FileHandle;
12use vars qw($VERSION);
13
14$VERSION = "2.10"; # $Id: //depot/libnet/Net/Netrc.pm#4$
15
16my %netrc = ();
17
18sub _readrc
19{
20 my $host = shift;
21 my($home,$file);
22
23 if($^O eq "MacOS") {
24 $home = $ENV{HOME} || `pwd`;
25 chomp($home);
26 $file = ($home =~ /:$/ ? $home . "netrc" : $home . ":netrc");
27 } else {
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";
31 }
32
33 my($login,$pass,$acct) = (undef,undef,undef);
34 my $fh;
35 local $_;
36
37 $netrc{default} = undef;
38
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')
41 {
42 my @stat = stat($file);
43
44 if(@stat)
45 {
46 if($stat[2] & 077)
47 {
48 carp "Bad permissions: $file";
49 return;
50 }
51 if($stat[4] != $<)
52 {
53 carp "Not owner: $file";
54 return;
55 }
56 }
57 }
58
59 if($fh = FileHandle->new($file,"r"))
60 {
61 my($mach,$macdef,$tok,@tok) = (0,0);
62
63 while(<$fh>)
64 {
65 undef $macdef if /\A\n\Z/;
66
67 if($macdef)
68 {
69 push(@$macdef,$_);
70 next;
71 }
72
73 s/^\s*//;
74 chomp;
75 push(@tok, $+)
76 while(length && s/^("([^"]*)"|(\S+))\s*//);
77
78TOKEN:
79 while(@tok)
80 {
81 if($tok[0] eq "default")
82 {
83 shift(@tok);
84 $mach = bless {};
85 $netrc{default} = [$mach];
86
87 next TOKEN;
88 }
89
90 last TOKEN
91 unless @tok > 1;
92
93 $tok = shift(@tok);
94
95 if($tok eq "machine")
96 {
97 my $host = shift @tok;
98 $mach = bless {machine => $host};
99
100 $netrc{$host} = []
101 unless exists($netrc{$host});
102 push(@{$netrc{$host}}, $mach);
103 }
104 elsif($tok =~ /^(login|password|account)$/)
105 {
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;
111 }
112 elsif($tok eq "macdef")
113 {
114 next TOKEN unless $mach;
115 my $value = shift @tok;
116 $mach->{macdef} = {}
117 unless exists $mach->{macdef};
118 $macdef = $mach->{machdef}{$value} = [];
119 }
120 }
121 }
122 $fh->close();
123 }
124}
125
126sub lookup
127{
128 my($pkg,$mach,$login) = @_;
129
130 _readrc()
131 unless exists $netrc{default};
132
133 $mach ||= 'default';
134 undef $login
135 if $mach eq 'default';
136
137 if(exists $netrc{$mach})
138 {
139 if(defined $login)
140 {
141 my $m;
142 foreach $m (@{$netrc{$mach}})
143 {
144 return $m
145 if(exists $m->{login} && $m->{login} eq $login);
146 }
147 return undef;
148 }
149 return $netrc{$mach}->[0]
150 }
151
152 return $netrc{default}->[0]
153 if defined $netrc{default};
154
155 return undef;
156}
157
158sub login
159{
160 my $me = shift;
161
162 exists $me->{login}
163 ? $me->{login}
164 : undef;
165}
166
167sub account
168{
169 my $me = shift;
170
171 exists $me->{account}
172 ? $me->{account}
173 : undef;
174}
175
176sub password
177{
178 my $me = shift;
179
180 exists $me->{password}
181 ? $me->{password}
182 : undef;
183}
184
185sub lpa
186{
187 my $me = shift;
188 ($me->login, $me->password, $me->account);
189}
190
1911;
192
193__END__
194
195=head1 NAME
196
197Net::Netrc - OO interface to users netrc file
198
199=head1 SYNOPSIS
200
201 use Net::Netrc;
202
203 $mach = Net::Netrc->lookup('some.machine');
204 $login = $mach->login;
205 ($login, $password, $account) = $mach->lpa;
206
207=head1 DESCRIPTION
208
209C<Net::Netrc> is a class implementing a simple interface to the .netrc file
210used as by the ftp program.
211
212C<Net::Netrc> also implements security checks just like the ftp program,
213these checks are, first that the .netrc file must be owned by the user and
214second the ownership permissions should be such that only the owner has
215read and write access. If these conditions are not met then a warning is
216output and the .netrc file is not read.
217
218=head1 THE .netrc FILE
219
220The .netrc file contains login and initialization information used by the
221auto-login process. It resides in the user's home directory. The following
222tokens are recognized; they may be separated by spaces, tabs, or new-lines:
223
224=over 4
225
226=item machine name
227
228Identify a remote machine name. The auto-login process searches
229the .netrc file for a machine token that matches the remote machine
230specified. Once a match is made, the subsequent .netrc tokens
231are processed, stopping when the end of file is reached or an-
232other machine or a default token is encountered.
233
234=item default
235
236This is the same as machine name except that default matches
237any name. There can be only one default token, and it must be
238after all machine tokens. This is normally used as:
239
240 default login anonymous password user@site
241
242thereby giving the user automatic anonymous login to machines
243not specified in .netrc.
244
245=item login name
246
247Identify a user on the remote machine. If this token is present,
248the auto-login process will initiate a login using the
249specified name.
250
251=item password string
252
253Supply a password. If this token is present, the auto-login
254process will supply the specified string if the remote server
255requires a password as part of the login process.
256
257=item account string
258
259Supply an additional account password. If this token is present,
260the auto-login process will supply the specified string
261if the remote server requires an additional account password.
262
263=item macdef name
264
265Define a macro. C<Net::Netrc> only parses this field to be compatible
266with I<ftp>.
267
268=back
269
270=head1 CONSTRUCTOR
271
272The constructor for a C<Net::Netrc> object is not called new as it does not
273really create a new object. But instead is called C<lookup> as this is
274essentially what it does.
275
276=over 4
277
278=item lookup ( MACHINE [, LOGIN ])
279
280Lookup and return a reference to the entry for C<MACHINE>. If C<LOGIN> is given
281then the entry returned will have the given login. If C<LOGIN> is not given then
282the first entry in the .netrc file for C<MACHINE> will be returned.
283
284If a matching entry cannot be found, and a default entry exists, then a
285reference to the default entry is returned.
286
287=back
288
289=head1 METHODS
290
291=over 4
292
293=item login ()
294
295Return the login id for the netrc entry
296
297=item password ()
298
299Return the password for the netrc entry
300
301=item account ()
302
303Return the account information for the netrc entry
304
305=item lpa ()
306
307Return a list of login, password and account information fir the netrc entry
308
309=back
310
311=head1 AUTHOR
312
313Graham Barr <gbarr@pobox.com>
314
315=head1 SEE ALSO
316
317L<Net::Netrc>
318L<Net::Cmd>
319
320=head1 COPYRIGHT
321
322Copyright (c) 1995-1998 Graham Barr. All rights reserved.
323This program is free software; you can redistribute it and/or modify
324it under the same terms as Perl itself.
325
326=cut