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.

212 lines
5.2 KiB

  1. package Net::Config;
  2. # $Id: //depot/libnet/Net/Config.pm#6 $
  3. require Exporter;
  4. use vars qw(@ISA @EXPORT %NetConfig $VERSION $CONFIGURE $LIBNET_CFG);
  5. use Socket qw(inet_aton inet_ntoa);
  6. use strict;
  7. @EXPORT = qw(%NetConfig);
  8. @ISA = qw(Net::LocalCfg Exporter);
  9. $VERSION = "1.04";
  10. eval { local $SIG{__DIE__}; require Net::LocalCfg };
  11. %NetConfig = (
  12. nntp_hosts => [],
  13. snpp_hosts => [],
  14. pop3_hosts => [],
  15. smtp_hosts => [],
  16. ph_hosts => [],
  17. daytime_hosts => [],
  18. time_hosts => [],
  19. inet_domain => undef,
  20. ftp_firewall => undef,
  21. ftp_ext_passive => 0,
  22. ftp_int_passive => 0,
  23. test_hosts => 1,
  24. test_exist => 1,
  25. );
  26. my $file = __FILE__;
  27. my $ref;
  28. $file =~ s/Config.pm/libnet.cfg/;
  29. if ( -f $file ) {
  30. $ref = eval { do $file };
  31. if (ref($ref) eq 'HASH') {
  32. %NetConfig = (%NetConfig, %{ $ref });
  33. $LIBNET_CFG = $file;
  34. }
  35. }
  36. if ($< == $> and !$CONFIGURE) {
  37. my $home = eval { (getpwuid($>))[7] } || $ENV{HOME};
  38. $file = $home . "/.libnetrc";
  39. $ref = eval { do $file } if -f $file;
  40. %NetConfig = (%NetConfig, %{ $ref })
  41. if ref($ref) eq 'HASH';
  42. }
  43. my ($k,$v);
  44. while(($k,$v) = each %NetConfig) {
  45. $v = [ $v ]
  46. if($k =~ /_hosts$/ && !ref($v));
  47. }
  48. # Take a hostname and determine if it is inside te firewall
  49. sub requires_firewall {
  50. shift; # ignore package
  51. my $host = shift;
  52. return 0 unless defined $NetConfig{'ftp_firewall'};
  53. $host = inet_aton($host) or return -1;
  54. $host = inet_ntoa($host);
  55. if(exists $NetConfig{'local_netmask'}) {
  56. my $quad = unpack("N",pack("C*",split(/\./,$host)));
  57. my $list = $NetConfig{'local_netmask'};
  58. $list = [$list] unless ref($list);
  59. foreach (@$list) {
  60. my($net,$bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
  61. my $mask = ~0 << (32 - $bits);
  62. my $addr = unpack("N",pack("C*",split(/\./,$net)));
  63. return 0 if (($addr & $mask) == ($quad & $mask));
  64. }
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. use vars qw(*is_external);
  70. *is_external = \&requires_firewall;
  71. 1;
  72. __END__
  73. =head1 NAME
  74. Net::Config - Local configuration data for libnet
  75. =head1 SYNOPSYS
  76. use Net::Config qw(%NetConfig);
  77. =head1 DESCRIPTION
  78. C<Net::Config> holds configuration data for the modules in the libnet
  79. distribuion. During installation you will be asked for these values.
  80. The configuration data is held globally in a file in the perl installation
  81. tree, but a user may override any of these values by providing thier own. This
  82. can be done by having a C<.libnetrc> file in thier home directory. This file
  83. should return a reference to a HASH containing the keys described below.
  84. For example
  85. # .libnetrc
  86. {
  87. nntp_hosts => [ "my_prefered_host" ],
  88. ph_hosts => [ "my_ph_server" ],
  89. }
  90. __END__
  91. =head1 METHODS
  92. C<Net::Config> defines the following methods. They are methods as they are
  93. invoked as class methods. This is because C<Net::Config> inherits from
  94. C<Net::LocalCfg> so you can override these methods if you want.
  95. =over 4
  96. =item requires_firewall HOST
  97. Attempts to determine if a given host is outside your firewall. Possible
  98. return values are.
  99. -1 Cannot lookup hostname
  100. 0 Host is inside firewall (or there is no ftp_firewall entry)
  101. 1 Host is outside the firewall
  102. This is done by using hostname lookup and the C<local_netmask> entry in
  103. the configuration data.
  104. =back
  105. =head1 NetConfig VALUES
  106. =over 4
  107. =item nntp_hosts
  108. =item snpp_hosts
  109. =item pop3_hosts
  110. =item smtp_hosts
  111. =item ph_hosts
  112. =item daytime_hosts
  113. =item time_hosts
  114. Each is a reference to an array of hostnames (in order of preference),
  115. which should be used for the given protocol
  116. =item inet_domain
  117. Your internet domain name
  118. =item ftp_firewall
  119. If you have an FTP proxy firewall (B<NOT> a HTTP or SOCKS firewall)
  120. then this value should be set to the firewall hostname. If your firewall
  121. does not listen to port 21, then this value should be set to
  122. C<"hostname:port"> (eg C<"hostname:99">)
  123. =item ftp_ext_passive
  124. =item ftp_int_pasive
  125. FTP servers normally work on a non-passive mode. That is when you want to
  126. transfer data you have to tell the server the address and port to
  127. connect to.
  128. With some firewalls this does not work as te server cannot
  129. connect to your machine (because you are beind a firewall) and the firewall
  130. does not re-write te command. In this case you should set C<ftp_ext_passive>
  131. to a I<true> value.
  132. Some servers are configured to only work in passive mode. If you have
  133. one of these you can force C<Net::FTP> to always transfer in passive
  134. mode, when not going via a firewall, by cetting C<ftp_int_passive> to
  135. a I<true> value.
  136. =item local_netmask
  137. A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
  138. These are used by the C<requires_firewall> function to determine if a given
  139. host is inside or outside your firewall.
  140. =back
  141. The following entries are used during installation & testing on the
  142. libnet package
  143. =over 4
  144. =item test_hosts
  145. If true them C<make test> may attempt to connect to hosts given in the
  146. configuration.
  147. =item test_exists
  148. If true the C<Configure> will check each hostname given that it exists
  149. =back
  150. =cut