X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FNet%2FNetrc.pm;h=02ebc82840b61f4d6d2ce9bf53674e76888730c5;hb=53273a086103cdbbf7ebdd5f1a18b2c0777cbc1b;hp=429982186548d8a094b2415e2aecd30ec9ff663d;hpb=7e1af8bca57f405a8444b575a870918a6d88fc5c;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/Net/Netrc.pm b/lib/Net/Netrc.pm index 4299821..02ebc82 100644 --- a/lib/Net/Netrc.pm +++ b/lib/Net/Netrc.pm @@ -1,164 +1,35 @@ # Net::Netrc.pm # -# Copyright (c) 1995 Graham Barr . All rights -# reserved. This program is free software; you can redistribute it and/or +# Copyright (c) 1995-1998 Graham Barr . All rights reserved. +# This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. package Net::Netrc; -=head1 NAME - -Net::Netrc - OO interface to users netrc file - -=head1 SYNOPSIS - - use Net::Netrc; - - $mach = Net::Netrc->lookup('some.machine'); - $login = $mach->login; - ($login, $password, $account) = $mach->lpa; - -=head1 DESCRIPTION - -C is a class implementing a simple interface to the .netrc file -used as by the ftp program. - -C also implements security checks just like the ftp program, -these checks are, first that the .netrc file must be owned by the user and -second the ownership permissions should be such that only the owner has -read and write access. If these conditions are not met then a warning is -output and the .netrc file is not read. - -=head1 THE .netrc FILE - -The .netrc file contains login and initialization information used by the -auto-login process. It resides in the user's home directory. The following -tokens are recognized; they may be separated by spaces, tabs, or new-lines: - -=over 4 - -=item machine name - -Identify a remote machine name. The auto-login process searches -the .netrc file for a machine token that matches the remote machine -specified. Once a match is made, the subsequent .netrc tokens -are processed, stopping when the end of file is reached or an- -other machine or a default token is encountered. - -=item default - -This is the same as machine name except that default matches -any name. There can be only one default token, and it must be -after all machine tokens. This is normally used as: - - default login anonymous password user@site - -thereby giving the user automatic anonymous login to machines -not specified in .netrc. - -=item login name - -Identify a user on the remote machine. If this token is present, -the auto-login process will initiate a login using the -specified name. - -=item password string - -Supply a password. If this token is present, the auto-login -process will supply the specified string if the remote server -requires a password as part of the login process. - -=item account string - -Supply an additional account password. If this token is present, -the auto-login process will supply the specified string -if the remote server requires an additional account password. - -=item macdef name - -Define a macro. C only parses this field to be compatible -with I. - -=back - -=head1 CONSTRUCTOR - -The constructor for a C object is not called new as it does not -really create a new object. But instead is called C as this is -essentially what it deos. - -=over 4 - -=item lookup ( MACHINE [, LOGIN ]) - -Lookup and return a reference to the entry for C. If C is given -then the entry returned will have the given login. If C is not given then -the first entry in the .netrc file for C will be returned. - -If a matching entry cannot be found, and a default entry exists, then a -reference to the default entry is returned. - -=back - -=head1 METHODS - -=over 4 - -=item login () - -Return the login id for the netrc entry - -=item password () - -Return the password for the netrc entry - -=item account () - -Return the account information for the netrc entry - -=item lpa () - -Return a list of login, password and account information fir the netrc entry - -=back - -=head1 AUTHOR - -Graham Barr - -=head1 REVISION - -$Revision: 2.1 $ - -=head1 SEE ALSO - -L -L - -=head1 COPYRIGHT - -Copyright (c) 1995 Graham Barr. All rights reserved. This program is free -software; you can redistribute it and/or modify it under the same terms -as Perl itself. - -=cut - use Carp; use strict; use FileHandle; use vars qw($VERSION); -$VERSION = sprintf("%d.%02d", q$Revision: 2.1 $ =~ /(\d+)\.(\d+)/); +$VERSION = "2.12"; # $Id: //depot/libnet/Net/Netrc.pm#13 $ my %netrc = (); sub _readrc { my $host = shift; - - # Some OS's don't have `getpwuid', so we default to $ENV{HOME} - my $home = eval { (getpwuid($>))[7] } || $ENV{HOME}; - my $file = $home . "/.netrc"; + my($home,$file); + + if($^O eq "MacOS") { + $home = $ENV{HOME} || `pwd`; + chomp($home); + $file = ($home =~ /:$/ ? $home . "netrc" : $home . ":netrc"); + } else { + # Some OS's don't have `getpwuid', so we default to $ENV{HOME} + $home = eval { (getpwuid($>))[7] } || $ENV{HOME}; + $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH}||'') if defined $ENV{HOMEDRIVE}; + $file = $home . "/.netrc"; + } my($login,$pass,$acct) = (undef,undef,undef); my $fh; @@ -166,8 +37,11 @@ sub _readrc $netrc{default} = undef; - # OS/2 does not handle stat in a way compatable with this check :-( - unless($^O eq 'os2') + # OS/2 and Win32 do not handle stat in a way compatable with this check :-( + unless($^O eq 'os2' + || $^O eq 'MSWin32' + || $^O eq 'MacOS' + || $^O =~ /^cygwin/) { my @stat = stat($file); @@ -200,7 +74,13 @@ sub _readrc next; } - push(@tok, split(/[\s\n]+/, $_)); + s/^\s*//; + chomp; + + while(length && s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//) { + (my $tok = $+) =~ s/\\(.)/$1/g; + push(@tok, $tok); + } TOKEN: while(@tok) @@ -222,7 +102,7 @@ TOKEN: if($tok eq "machine") { my $host = shift @tok; - $mach = bless {machine => $mach}; + $mach = bless {machine => $host}; $netrc{$host} = [] unless exists($netrc{$host}); @@ -232,6 +112,8 @@ TOKEN: { next TOKEN unless $mach; my $value = shift @tok; + # Following line added by rmerrell to remove '/' escape char in .netrc + $value =~ s/\/\\/\\/g; $mach->{$1} = $value; } elsif($tok eq "macdef") @@ -274,7 +156,7 @@ sub lookup return $netrc{$mach}->[0] } - return $netrc{default} + return $netrc{default}->[0] if defined $netrc{default}; return undef; @@ -314,3 +196,145 @@ sub lpa } 1; + +__END__ + +=head1 NAME + +Net::Netrc - OO interface to users netrc file + +=head1 SYNOPSIS + + use Net::Netrc; + + $mach = Net::Netrc->lookup('some.machine'); + $login = $mach->login; + ($login, $password, $account) = $mach->lpa; + +=head1 DESCRIPTION + +C is a class implementing a simple interface to the .netrc file +used as by the ftp program. + +C also implements security checks just like the ftp program, +these checks are, first that the .netrc file must be owned by the user and +second the ownership permissions should be such that only the owner has +read and write access. If these conditions are not met then a warning is +output and the .netrc file is not read. + +=head1 THE .netrc FILE + +The .netrc file contains login and initialization information used by the +auto-login process. It resides in the user's home directory. The following +tokens are recognized; they may be separated by spaces, tabs, or new-lines: + +=over 4 + +=item machine name + +Identify a remote machine name. The auto-login process searches +the .netrc file for a machine token that matches the remote machine +specified. Once a match is made, the subsequent .netrc tokens +are processed, stopping when the end of file is reached or an- +other machine or a default token is encountered. + +=item default + +This is the same as machine name except that default matches +any name. There can be only one default token, and it must be +after all machine tokens. This is normally used as: + + default login anonymous password user@site + +thereby giving the user automatic anonymous login to machines +not specified in .netrc. + +=item login name + +Identify a user on the remote machine. If this token is present, +the auto-login process will initiate a login using the +specified name. + +=item password string + +Supply a password. If this token is present, the auto-login +process will supply the specified string if the remote server +requires a password as part of the login process. + +=item account string + +Supply an additional account password. If this token is present, +the auto-login process will supply the specified string +if the remote server requires an additional account password. + +=item macdef name + +Define a macro. C only parses this field to be compatible +with I. + +=back + +=head1 CONSTRUCTOR + +The constructor for a C object is not called new as it does not +really create a new object. But instead is called C as this is +essentially what it does. + +=over 4 + +=item lookup ( MACHINE [, LOGIN ]) + +Lookup and return a reference to the entry for C. If C is given +then the entry returned will have the given login. If C is not given then +the first entry in the .netrc file for C will be returned. + +If a matching entry cannot be found, and a default entry exists, then a +reference to the default entry is returned. + +If there is no matching entry found and there is no default defined, or +no .netrc file is found, then C is returned. + +=back + +=head1 METHODS + +=over 4 + +=item login () + +Return the login id for the netrc entry + +=item password () + +Return the password for the netrc entry + +=item account () + +Return the account information for the netrc entry + +=item lpa () + +Return a list of login, password and account information fir the netrc entry + +=back + +=head1 AUTHOR + +Graham Barr + +=head1 SEE ALSO + +L +L + +=head1 COPYRIGHT + +Copyright (c) 1995-1998 Graham Barr. All rights reserved. +This program is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=for html
+ +$Id: //depot/libnet/Net/Netrc.pm#13 $ + +=cut