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