home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / Net / Netrc.pm < prev    next >
Text File  |  1997-09-26  |  7KB  |  315 lines

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