Integrate maintperl changes #12268 and #12669;
[p5sagit/p5-mst-13.2.git] / lib / Net / Config.pm
CommitLineData
686337f3 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.
406c51ee 6
7package Net::Config;
406c51ee 8
9require Exporter;
10use vars qw(@ISA @EXPORT %NetConfig $VERSION $CONFIGURE $LIBNET_CFG);
11use Socket qw(inet_aton inet_ntoa);
12use strict;
13
14@EXPORT = qw(%NetConfig);
15@ISA = qw(Net::LocalCfg Exporter);
686337f3 16$VERSION = "1.05"; # $Id: //depot/libnet/Net/Config.pm#9 $
406c51ee 17
18eval { 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
36my $file = __FILE__;
37my $ref;
38$file =~ s/Config.pm/libnet.cfg/;
39if ( -f $file ) {
40 $ref = eval { do $file };
41 if (ref($ref) eq 'HASH') {
42 %NetConfig = (%NetConfig, %{ $ref });
43 $LIBNET_CFG = $file;
44 }
45}
46if ($< == $> and !$CONFIGURE) {
686337f3 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 }
406c51ee 54}
55my ($k,$v);
56while(($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
63sub 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
89use vars qw(*is_external);
90*is_external = \&requires_firewall;
91
921;
93
94__END__
95
96=head1 NAME
97
98Net::Config - Local configuration data for libnet
99
100=head1 SYNOPSYS
101
102 use Net::Config qw(%NetConfig);
103
104=head1 DESCRIPTION
105
106C<Net::Config> holds configuration data for the modules in the libnet
107distribuion. During installation you will be asked for these values.
108
109The configuration data is held globally in a file in the perl installation
ea5a7fad 110tree, but a user may override any of these values by providing their own. This
111can be done by having a C<.libnetrc> file in their home directory. This file
406c51ee 112should return a reference to a HASH containing the keys described below.
113For 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
124C<Net::Config> defines the following methods. They are methods as they are
125invoked as class methods. This is because C<Net::Config> inherits from
126C<Net::LocalCfg> so you can override these methods if you want.
127
128=over 4
129
130=item requires_firewall HOST
131
132Attempts to determine if a given host is outside your firewall. Possible
133return 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
139This is done by using hostname lookup and the C<local_netmask> entry in
140the 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
162Each is a reference to an array of hostnames (in order of preference),
163which should be used for the given protocol
164
165=item inet_domain
166
167Your internet domain name
168
169=item ftp_firewall
170
171If you have an FTP proxy firewall (B<NOT> a HTTP or SOCKS firewall)
172then this value should be set to the firewall hostname. If your firewall
173does not listen to port 21, then this value should be set to
174C<"hostname:port"> (eg C<"hostname:99">)
175
686337f3 176=item ftp_firewall_type
177
ea5a7fad 178There are many different ftp firewall products available. But unfortunately
179there is no standard for how to traverse a firewall. The list below shows the
686337f3 180sequence 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
192There 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
406c51ee 243=item ftp_ext_passive
244
245=item ftp_int_pasive
246
247FTP servers normally work on a non-passive mode. That is when you want to
248transfer data you have to tell the server the address and port to
249connect to.
250
ea5a7fad 251With some firewalls this does not work as the server cannot
252connect to your machine (because you are behind a firewall) and the firewall
253does not re-write the command. In this case you should set C<ftp_ext_passive>
406c51ee 254to a I<true> value.
255
256Some servers are configured to only work in passive mode. If you have
257one of these you can force C<Net::FTP> to always transfer in passive
ea5a7fad 258mode; when not going via a firewall, by setting C<ftp_int_passive> to
406c51ee 259a I<true> value.
260
261=item local_netmask
262
263A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
264These are used by the C<requires_firewall> function to determine if a given
265host is inside or outside your firewall.
266
267=back
268
269The following entries are used during installation & testing on the
270libnet package
271
272=over 4
273
274=item test_hosts
275
ea5a7fad 276If true then C<make test> may attempt to connect to hosts given in the
406c51ee 277configuration.
278
279=item test_exists
280
ea5a7fad 281If true then C<Configure> will check each hostname given that it exists
406c51ee 282
283=back
284
686337f3 285=for html <hr>
286
287I<$Id: //depot/libnet/Net/Config.pm#9 $>
288
406c51ee 289=cut