home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Net / Netrc.pm < prev    next >
Encoding:
Perl POD Document  |  1997-08-10  |  6.7 KB  |  316 lines

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