Commit | Line | Data |
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 | |
7 | package Net::Netrc; |
8 | |
9 | use Carp; |
10 | use strict; |
11 | use FileHandle; |
12 | use vars qw($VERSION); |
13 | |
14 | $VERSION = "2.10"; # $Id: //depot/libnet/Net/Netrc.pm#4$ |
15 | |
16 | my %netrc = (); |
17 | |
18 | sub _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 | |
78 | TOKEN: |
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 | |
126 | sub 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 | |
158 | sub login |
159 | { |
160 | my $me = shift; |
161 | |
162 | exists $me->{login} |
163 | ? $me->{login} |
164 | : undef; |
165 | } |
166 | |
167 | sub account |
168 | { |
169 | my $me = shift; |
170 | |
171 | exists $me->{account} |
172 | ? $me->{account} |
173 | : undef; |
174 | } |
175 | |
176 | sub password |
177 | { |
178 | my $me = shift; |
179 | |
180 | exists $me->{password} |
181 | ? $me->{password} |
182 | : undef; |
183 | } |
184 | |
185 | sub lpa |
186 | { |
187 | my $me = shift; |
188 | ($me->login, $me->password, $me->account); |
189 | } |
190 | |
191 | 1; |
192 | |
193 | __END__ |
194 | |
195 | =head1 NAME |
196 | |
197 | Net::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 | |
209 | C<Net::Netrc> is a class implementing a simple interface to the .netrc file |
210 | used as by the ftp program. |
211 | |
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. |
217 | |
218 | =head1 THE .netrc FILE |
219 | |
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: |
223 | |
224 | =over 4 |
225 | |
226 | =item machine name |
227 | |
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. |
233 | |
234 | =item default |
235 | |
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: |
239 | |
240 | default login anonymous password user@site |
241 | |
242 | thereby giving the user automatic anonymous login to machines |
243 | not specified in .netrc. |
244 | |
245 | =item login name |
246 | |
247 | Identify a user on the remote machine. If this token is present, |
248 | the auto-login process will initiate a login using the |
249 | specified name. |
250 | |
251 | =item password string |
252 | |
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. |
256 | |
257 | =item account string |
258 | |
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. |
262 | |
263 | =item macdef name |
264 | |
265 | Define a macro. C<Net::Netrc> only parses this field to be compatible |
266 | with I<ftp>. |
267 | |
268 | =back |
269 | |
270 | =head1 CONSTRUCTOR |
271 | |
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. |
275 | |
276 | =over 4 |
277 | |
278 | =item lookup ( MACHINE [, LOGIN ]) |
279 | |
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. |
283 | |
284 | If a matching entry cannot be found, and a default entry exists, then a |
285 | reference to the default entry is returned. |
286 | |
287 | =back |
288 | |
289 | =head1 METHODS |
290 | |
291 | =over 4 |
292 | |
293 | =item login () |
294 | |
295 | Return the login id for the netrc entry |
296 | |
297 | =item password () |
298 | |
299 | Return the password for the netrc entry |
300 | |
301 | =item account () |
302 | |
303 | Return the account information for the netrc entry |
304 | |
305 | =item lpa () |
306 | |
307 | Return a list of login, password and account information fir the netrc entry |
308 | |
309 | =back |
310 | |
311 | =head1 AUTHOR |
312 | |
313 | Graham Barr <gbarr@pobox.com> |
314 | |
315 | =head1 SEE ALSO |
316 | |
317 | L<Net::Netrc> |
318 | L<Net::Cmd> |
319 | |
320 | =head1 COPYRIGHT |
321 | |
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. |
325 | |
326 | =cut |