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