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