home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / perl / 5.8.8 / Net / Config.pm < prev    next >
Encoding:
Perl POD Document  |  2006-07-07  |  7.4 KB  |  314 lines

  1. # Net::Config.pm
  2. #
  3. # Copyright (c) 2000 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::Config;
  8.  
  9. require Exporter;
  10. use vars qw(@ISA @EXPORT %NetConfig $VERSION $CONFIGURE $LIBNET_CFG);
  11. use Socket qw(inet_aton inet_ntoa);
  12. use strict;
  13.  
  14. @EXPORT  = qw(%NetConfig);
  15. @ISA     = qw(Net::LocalCfg Exporter);
  16. $VERSION = "1.10"; # $Id: //depot/libnet/Net/Config.pm#17 $
  17.  
  18. eval { local $SIG{__DIE__}; require Net::LocalCfg };
  19.  
  20. %NetConfig = (
  21.     nntp_hosts => [],
  22.     snpp_hosts => [],
  23.     pop3_hosts => [],
  24.     smtp_hosts => [],
  25.     ph_hosts => [],
  26.     daytime_hosts => [],
  27.     time_hosts => [],
  28.     inet_domain => undef,
  29.     ftp_firewall => undef,
  30.     ftp_ext_passive => 0,
  31.     ftp_int_passive => 0,
  32.     test_hosts => 1,
  33.     test_exist => 1,
  34. );
  35.  
  36. #
  37. # Try to get as much configuration info as possible from InternetConfig
  38. #
  39. $^O eq 'MacOS' and eval <<TRY_INTERNET_CONFIG;
  40. use Mac::InternetConfig;
  41.  
  42. {
  43. my %nc = (
  44.     nntp_hosts      => [ \$InternetConfig{ kICNNTPHost() } ],
  45.     pop3_hosts      => [ \$InternetConfig{ kICMailAccount() } =~ /\@(.*)/ ],
  46.     smtp_hosts      => [ \$InternetConfig{ kICSMTPHost() } ],
  47.     ftp_testhost    => \$InternetConfig{ kICFTPHost() } ? \$InternetConfig{ kICFTPHost()} : undef,
  48.     ph_hosts        => [ \$InternetConfig{ kICPhHost() }   ],
  49.     ftp_ext_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
  50.     ftp_int_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
  51.     socks_hosts     => 
  52.         \$InternetConfig{ kICUseSocks() }    ? [ \$InternetConfig{ kICSocksHost() }    ] : [],
  53.     ftp_firewall    => 
  54.         \$InternetConfig{ kICUseFTPProxy() } ? [ \$InternetConfig{ kICFTPProxyHost() } ] : [],
  55. );
  56. \@NetConfig{keys %nc} = values %nc;
  57. }
  58. TRY_INTERNET_CONFIG
  59.  
  60. my $file = '/etc/perl/Net/libnet.cfg';
  61. my $ref;
  62. if ( -f $file ) {
  63.     $ref = eval { local $SIG{__DIE__}; do $file };
  64.     if (ref($ref) eq 'HASH') {
  65.     %NetConfig = (%NetConfig, %{ $ref });
  66.     $LIBNET_CFG = $file;
  67.     }
  68. }
  69. if ($< == $> and !$CONFIGURE)  {
  70.     my $home = eval { local $SIG{__DIE__}; (getpwuid($>))[7] } || $ENV{HOME};
  71.     $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH}||'') if defined $ENV{HOMEDRIVE};
  72.     if (defined $home) {
  73.     $file = $home . "/.libnetrc";
  74.     $ref = eval { local $SIG{__DIE__}; do $file } if -f $file;
  75.     %NetConfig = (%NetConfig, %{ $ref })
  76.         if ref($ref) eq 'HASH';    
  77.     }
  78. }
  79. my ($k,$v);
  80. while(($k,$v) = each %NetConfig) {
  81.     $NetConfig{$k} = [ $v ]
  82.         if($k =~ /_hosts$/ and $k ne "test_hosts" and defined($v) and !ref($v));
  83. }
  84.  
  85. # Take a hostname and determine if it is inside the firewall
  86.  
  87. sub requires_firewall {
  88.     shift; # ignore package
  89.     my $host = shift;
  90.  
  91.     return 0 unless defined $NetConfig{'ftp_firewall'};
  92.  
  93.     $host = inet_aton($host) or return -1;
  94.     $host = inet_ntoa($host);
  95.  
  96.     if(exists $NetConfig{'local_netmask'}) {
  97.     my $quad = unpack("N",pack("C*",split(/\./,$host)));
  98.     my $list = $NetConfig{'local_netmask'};
  99.     $list = [$list] unless ref($list);
  100.     foreach (@$list) {
  101.         my($net,$bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
  102.         my $mask = ~0 << (32 - $bits);
  103.         my $addr = unpack("N",pack("C*",split(/\./,$net)));
  104.  
  105.         return 0 if (($addr & $mask) == ($quad & $mask));
  106.     }
  107.     return 1;
  108.     }
  109.  
  110.     return 0;
  111. }
  112.  
  113. use vars qw(*is_external);
  114. *is_external = \&requires_firewall;
  115.  
  116. 1;
  117.  
  118. __END__
  119.  
  120. =head1 NAME
  121.  
  122. Net::Config - Local configuration data for libnet
  123.  
  124. =head1 SYNOPSYS
  125.  
  126.     use Net::Config qw(%NetConfig);
  127.  
  128. =head1 DESCRIPTION
  129.  
  130. C<Net::Config> holds configuration data for the modules in the libnet
  131. distribuion. During installation you will be asked for these values.
  132.  
  133. The configuration data is held globally in a file in the perl installation
  134. tree, but a user may override any of these values by providing their own. This
  135. can be done by having a C<.libnetrc> file in their home directory. This file
  136. should return a reference to a HASH containing the keys described below.
  137. For example
  138.  
  139.     # .libnetrc
  140.     {
  141.         nntp_hosts => [ "my_prefered_host" ],
  142.     ph_hosts   => [ "my_ph_server" ],
  143.     }
  144.     __END__
  145.  
  146. =head1 METHODS
  147.  
  148. C<Net::Config> defines the following methods. They are methods as they are
  149. invoked as class methods. This is because C<Net::Config> inherits from
  150. C<Net::LocalCfg> so you can override these methods if you want.
  151.  
  152. =over 4
  153.  
  154. =item requires_firewall HOST
  155.  
  156. Attempts to determine if a given host is outside your firewall. Possible
  157. return values are.
  158.  
  159.   -1  Cannot lookup hostname
  160.    0  Host is inside firewall (or there is no ftp_firewall entry)
  161.    1  Host is outside the firewall
  162.  
  163. This is done by using hostname lookup and the C<local_netmask> entry in
  164. the configuration data.
  165.  
  166. =back
  167.  
  168. =head1 NetConfig VALUES
  169.  
  170. =over 4
  171.  
  172. =item nntp_hosts
  173.  
  174. =item snpp_hosts
  175.  
  176. =item pop3_hosts
  177.  
  178. =item smtp_hosts
  179.  
  180. =item ph_hosts
  181.  
  182. =item daytime_hosts
  183.  
  184. =item time_hosts
  185.  
  186. Each is a reference to an array of hostnames (in order of preference),
  187. which should be used for the given protocol
  188.  
  189. =item inet_domain
  190.  
  191. Your internet domain name
  192.  
  193. =item ftp_firewall
  194.  
  195. If you have an FTP proxy firewall (B<NOT> an HTTP or SOCKS firewall)
  196. then this value should be set to the firewall hostname. If your firewall
  197. does not listen to port 21, then this value should be set to
  198. C<"hostname:port"> (eg C<"hostname:99">)
  199.  
  200. =item ftp_firewall_type
  201.  
  202. There are many different ftp firewall products available. But unfortunately
  203. there is no standard for how to traverse a firewall.  The list below shows the
  204. sequence of commands that Net::FTP will use
  205.  
  206.   user        Username for remote host
  207.   pass        Password for remote host
  208.   fwuser      Username for firewall
  209.   fwpass      Password for firewall
  210.   remote.host The hostname of the remote ftp server
  211.  
  212. =over 4
  213.  
  214. =item 0
  215.  
  216. There is no firewall
  217.  
  218. =item 1
  219.  
  220.      USER user@remote.host
  221.      PASS pass
  222.  
  223. =item 2
  224.  
  225.      USER fwuser
  226.      PASS fwpass
  227.      USER user@remote.host
  228.      PASS pass
  229.  
  230. =item 3
  231.  
  232.      USER fwuser
  233.      PASS fwpass
  234.      SITE remote.site
  235.      USER user
  236.      PASS pass
  237.  
  238. =item 4
  239.  
  240.      USER fwuser
  241.      PASS fwpass
  242.      OPEN remote.site
  243.      USER user
  244.      PASS pass
  245.  
  246. =item 5
  247.  
  248.      USER user@fwuser@remote.site
  249.      PASS pass@fwpass
  250.  
  251. =item 6
  252.  
  253.      USER fwuser@remote.site
  254.      PASS fwpass
  255.      USER user
  256.      PASS pass
  257.  
  258. =item 7
  259.  
  260.      USER user@remote.host
  261.      PASS pass
  262.      AUTH fwuser
  263.      RESP fwpass
  264.  
  265. =back
  266.  
  267. =item ftp_ext_passive
  268.  
  269. =item ftp_int_pasive
  270.  
  271. FTP servers normally work on a non-passive mode. That is when you want to
  272. transfer data you have to tell the server the address and port to
  273. connect to.
  274.  
  275. With some firewalls this does not work as the server cannot
  276. connect to your machine (because you are behind a firewall) and the firewall
  277. does not re-write the command. In this case you should set C<ftp_ext_passive>
  278. to a I<true> value.
  279.  
  280. Some servers are configured to only work in passive mode. If you have
  281. one of these you can force C<Net::FTP> to always transfer in passive
  282. mode; when not going via a firewall, by setting C<ftp_int_passive> to
  283. a I<true> value.
  284.  
  285. =item local_netmask
  286.  
  287. A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
  288. These are used by the C<requires_firewall> function to determine if a given
  289. host is inside or outside your firewall.
  290.  
  291. =back
  292.  
  293. The following entries are used during installation & testing on the
  294. libnet package
  295.  
  296. =over 4
  297.  
  298. =item test_hosts
  299.  
  300. If true then C<make test> may attempt to connect to hosts given in the
  301. configuration.
  302.  
  303. =item test_exists
  304.  
  305. If true then C<Configure> will check each hostname given that it exists
  306.  
  307. =back
  308.  
  309. =for html <hr>
  310.  
  311. I<$Id: //depot/libnet/Net/Config.pm#17 $>
  312.  
  313. =cut
  314.