Commit | Line | Data |
---|---|---|
686337f3 JH |
1 | # Net::Config.pm |
2 | # | |
2e173144 CBW |
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. | |
a4f8ff46 SH |
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. | |
406c51ee JH |
10 | |
11 | package Net::Config; | |
406c51ee | 12 | |
2e173144 CBW |
13 | use 5.008001; |
14 | ||
406c51ee | 15 | use strict; |
2e173144 CBW |
16 | use warnings; |
17 | ||
18 | use Exporter; | |
19 | use Socket qw(inet_aton inet_ntoa); | |
406c51ee | 20 | |
2e173144 CBW |
21 | our @EXPORT = qw(%NetConfig); |
22 | our @ISA = qw(Net::LocalCfg Exporter); | |
bfdb5bfe | 23 | our $VERSION = "3.08"; |
2e173144 CBW |
24 | |
25 | our($CONFIGURE, $LIBNET_CFG); | |
406c51ee JH |
26 | |
27 | eval { local $SIG{__DIE__}; require Net::LocalCfg }; | |
28 | ||
2e173144 | 29 | our %NetConfig = ( |
b3f6f6a6 RGS |
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, | |
406c51ee JH |
43 | ); |
44 | ||
c8570720 GB |
45 | # |
46 | # Try to get as much configuration info as possible from InternetConfig | |
47 | # | |
2e173144 CBW |
48 | { |
49 | ## no critic (BuiltinFunctions::ProhibitStringyEval) | |
12df23ee | 50 | $^O eq 'MacOS' and eval <<TRY_INTERNET_CONFIG; |
c8570720 GB |
51 | use Mac::InternetConfig; |
52 | ||
53 | { | |
54 | my %nc = ( | |
12df23ee GB |
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, | |
c8570720 | 62 | socks_hosts => |
5abafd4c | 63 | \$InternetConfig{ kICUseSocks() } ? [ \$InternetConfig{ kICSocksHost() } ] : [], |
c8570720 | 64 | ftp_firewall => |
5abafd4c | 65 | \$InternetConfig{ kICUseFTPProxy() } ? [ \$InternetConfig{ kICFTPProxyHost() } ] : [], |
c8570720 | 66 | ); |
12df23ee | 67 | \@NetConfig{keys %nc} = values %nc; |
c8570720 GB |
68 | } |
69 | TRY_INTERNET_CONFIG | |
2e173144 | 70 | } |
c8570720 | 71 | |
406c51ee JH |
72 | my $file = __FILE__; |
73 | my $ref; | |
74 | $file =~ s/Config.pm/libnet.cfg/; | |
b3f6f6a6 RGS |
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 | } | |
406c51ee | 81 | } |
b3f6f6a6 RGS |
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 | } | |
406c51ee | 91 | } |
b3f6f6a6 RGS |
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)); | |
406c51ee JH |
96 | } |
97 | ||
302c2e6b | 98 | # Take a hostname and determine if it is inside the firewall |
406c51ee | 99 | |
b3f6f6a6 | 100 | |
406c51ee | 101 | sub requires_firewall { |
b3f6f6a6 RGS |
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)); | |
406c51ee | 120 | } |
b3f6f6a6 RGS |
121 | return 1; |
122 | } | |
406c51ee | 123 | |
b3f6f6a6 | 124 | return 0; |
406c51ee JH |
125 | } |
126 | ||
406c51ee JH |
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 | ||
db956464 | 137 | =head1 SYNOPSIS |
406c51ee JH |
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 | |
3c4b39be | 144 | distribution. During installation you will be asked for these values. |
406c51ee JH |
145 | |
146 | The configuration data is held globally in a file in the perl installation | |
ea5a7fad | 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 | |
406c51ee JH |
149 | should return a reference to a HASH containing the keys described below. |
150 | For example | |
151 | ||
152 | # .libnetrc | |
153 | { | |
3c4b39be | 154 | nntp_hosts => [ "my_preferred_host" ], |
5abafd4c | 155 | ph_hosts => [ "my_ph_server" ], |
406c51ee JH |
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 | ||
2e173144 | 167 | =item requires_firewall ( HOST ) |
406c51ee JH |
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 | ||
d1be9408 | 208 | If you have an FTP proxy firewall (B<NOT> an HTTP or SOCKS firewall) |
406c51ee JH |
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 | ||
686337f3 JH |
213 | =item ftp_firewall_type |
214 | ||
ea5a7fad | 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 | |
686337f3 JH |
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 | ||
0d0bc230 | 227 | =item 0Z<> |
686337f3 JH |
228 | |
229 | There is no firewall | |
230 | ||
0d0bc230 | 231 | =item 1Z<> |
686337f3 JH |
232 | |
233 | USER user@remote.host | |
234 | PASS pass | |
235 | ||
0d0bc230 | 236 | =item 2Z<> |
686337f3 JH |
237 | |
238 | USER fwuser | |
239 | PASS fwpass | |
240 | USER user@remote.host | |
241 | PASS pass | |
242 | ||
0d0bc230 | 243 | =item 3Z<> |
686337f3 JH |
244 | |
245 | USER fwuser | |
246 | PASS fwpass | |
247 | SITE remote.site | |
248 | USER user | |
249 | PASS pass | |
250 | ||
0d0bc230 | 251 | =item 4Z<> |
686337f3 JH |
252 | |
253 | USER fwuser | |
254 | PASS fwpass | |
255 | OPEN remote.site | |
256 | USER user | |
257 | PASS pass | |
258 | ||
0d0bc230 | 259 | =item 5Z<> |
686337f3 JH |
260 | |
261 | USER user@fwuser@remote.site | |
262 | PASS pass@fwpass | |
263 | ||
0d0bc230 | 264 | =item 6Z<> |
686337f3 JH |
265 | |
266 | USER fwuser@remote.site | |
267 | PASS fwpass | |
268 | USER user | |
269 | PASS pass | |
270 | ||
0d0bc230 | 271 | =item 7Z<> |
686337f3 JH |
272 | |
273 | USER user@remote.host | |
274 | PASS pass | |
275 | AUTH fwuser | |
276 | RESP fwpass | |
277 | ||
278 | =back | |
279 | ||
406c51ee JH |
280 | =item ftp_ext_passive |
281 | ||
3c4b39be | 282 | =item ftp_int_passive |
406c51ee | 283 | |
8460ac15 GA |
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. | |
8723f121 | 288 | |
8460ac15 | 289 | With some firewalls active mode does not work as the server cannot |
ea5a7fad | 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> | |
406c51ee JH |
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 | |
ea5a7fad | 296 | mode; when not going via a firewall, by setting C<ftp_int_passive> to |
406c51ee JH |
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 | ||
ea5a7fad | 314 | If true then C<make test> may attempt to connect to hosts given in the |
406c51ee JH |
315 | configuration. |
316 | ||
317 | =item test_exists | |
318 | ||
ea5a7fad | 319 | If true then C<Configure> will check each hostname given that it exists |
406c51ee JH |
320 | |
321 | =back | |
322 | ||
a4f8ff46 SH |
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 | ||
406c51ee | 340 | =cut |