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