Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
7.0 KiB

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