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