Handle hostnames with upper-case letters
[webmin.git] / net / net-lib.pl
1 # net-lib.pl
2 # Common local networking functions
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 %access = &get_module_acl();
8 $access{'ipnodes'} = $access{'hosts'};
9
10 if (-r "$module_root_directory/$gconfig{'os_type'}-$gconfig{'os_version'}-lib.pl") {
11         do "$gconfig{'os_type'}-$gconfig{'os_version'}-lib.pl";
12         }
13 elsif ($gconfig{'os_type'} eq 'suse-linux' &&
14        $gconfig{'os_version'} >= 9.2) {
15         # Special case for SuSE 9.2+
16         do "$gconfig{'os_type'}-9.2-ALL-lib.pl";
17         }
18 elsif ($gconfig{'os_type'} eq 'slackware-linux' &&
19        $gconfig{'os_version'} >= 9.1) {
20         # Special case for Slackware 9.1+
21         do "$gconfig{'os_type'}-9.1-ALL-lib.pl";
22         }
23 else {
24         do "$gconfig{'os_type'}-lib.pl";
25         }
26
27 # list_hosts()
28 # Parse hosts from /etc/hosts into a data structure
29 sub list_hosts
30 {
31 local @rv;
32 local $lnum = 0;
33 local $line="";
34
35 &open_readfile(HOSTS, $config{'hosts_file'});
36 while($line=<HOSTS>) {
37         $line =~ s/\r|\n//g;
38         $line =~ s/#.*$//g;
39         $line =~ s/\s+$//g;
40         local(@f)=split(/\s+/, $line);
41         local($ipaddr)=shift(@f);
42         if (check_ipaddress_any($ipaddr)) {
43                 push(@rv, { 'address' => $ipaddr,
44                             'hosts' => [ @f ],
45                             'line', $lnum,
46                             'index', scalar(@rv) });
47                 }
48         $lnum++;
49         }
50 close(HOSTS);
51 return @rv;
52 }
53
54 # create_host(&host)
55 # Add a new host to /etc/hosts
56 sub create_host
57 {
58 &open_tempfile(HOSTS, ">>$config{'hosts_file'}");
59 &print_tempfile(HOSTS, $_[0]->{'address'},"\t",join(" ",@{$_[0]->{'hosts'}}),"\n");
60 &close_tempfile(HOSTS);
61 }
62
63 # modify_host(&host)
64 # Update the address and hosts of a line in /etc/hosts
65 sub modify_host
66 {
67 &replace_file_line($config{'hosts_file'},
68                    $_[0]->{'line'},
69                    $_[0]->{'address'}."\t".join(" ",@{$_[0]->{'hosts'}})."\n");
70 }
71
72 # delete_host(&host)
73 # Delete a host from /etc/hosts
74 sub delete_host
75 {
76 &replace_file_line($config{'hosts_file'}, $_[0]->{'line'});
77 }
78
79 # list_ipnodes()
80 # Parse ipnodes from /etc/ipnodes into a data structure
81 sub list_ipnodes
82 {
83 local @rv;
84 local $lnum = 0;
85 &open_readfile(HOSTS, $config{'ipnodes_file'});
86 while(<HOSTS>) {
87         s/\r|\n//g;
88         s/#.*$//g;
89         s/\s+$//g;
90         if (/([0-9a-f:]+|[0-9\.]+)\s+(.*)$/) {
91                 push(@rv, { 'address' => $1,
92                             'ipnodes' => [ split(/\s+/, $2) ],
93                             'line', $lnum,
94                             'index', scalar(@rv) });
95                 }
96         $lnum++;
97         }
98 close(HOSTS);
99 return @rv;
100 }
101
102 # create_ipnode(&ipnode)
103 # Add a new ipnode to /etc/ipnodes
104 sub create_ipnode
105 {
106 &open_tempfile(HOSTS, ">>$config{'ipnodes_file'}");
107 &print_tempfile(HOSTS, $_[0]->{'address'},"\t",join(" ",@{$_[0]->{'ipnodes'}}),"\n");
108 &close_tempfile(HOSTS);
109 }
110
111 # modify_ipnode(&ipnode)
112 # Update the address and ipnodes of a line in /etc/ipnodes
113 sub modify_ipnode
114 {
115 &replace_file_line($config{'ipnodes_file'},
116                    $_[0]->{'line'},
117                    $_[0]->{'address'}."\t".join(" ",@{$_[0]->{'ipnodes'}})."\n");
118 }
119
120 # delete_ipnode(&ipnode)
121 # Delete a ipnode from /etc/ipnodes
122 sub delete_ipnode
123 {
124 &replace_file_line($config{'ipnodes_file'}, $_[0]->{'line'});
125 }
126
127 # parse_hex(hex)
128 # Convert an address like ff000000 into 255.0.0.0
129 sub parse_hex
130 {
131 $_[0] =~ /(..)(..)(..)(..)$/;
132 return join(".", (hex($1), hex($2), hex($3), hex($4)));
133 }
134
135 # interfaces_chooser_button(field, multiple, [form])
136 # Returns HTML for a javascript button for choosing an interface or interfaces
137 sub interfaces_chooser_button
138 {
139   local $form = @_ > 2 ? $_[2] : 0;
140   local $w = $_[1] ? 500 : 300;
141   return "<input type=button onClick='ifield = document.forms[$form].$_[0]; chooser = window.open(\"$gconfig{'webprefix'}/net/interface_chooser.cgi?multi=$_[1]&interface=\"+escape(ifield.value), \"chooser\", \"toolbar=no,menubar=no,scrollbars=yes,width=$w,height=200\"); chooser.ifield = ifield' value=\"...\">\n";
142 }
143
144 # prefix_to_mask(prefix)
145 # Converts a number like 24 to a mask like 255.255.255.0
146 sub prefix_to_mask
147 {
148 return $_[0] >= 24 ? "255.255.255.".(256-(2 ** (32-$_[0]))) :
149        $_[0] >= 16 ? "255.255.".(256-(2 ** (24-$_[0]))).".0" :
150        $_[0] >= 8 ? "255.".(256-(2 ** (16-$_[0]))).".0.0" :
151                      (256-(2 ** (8-$_[0]))).".0.0.0";
152 }
153
154 # mask_to_prefix(mask)
155 # Converts a mask like 255.255.255.0 to a prefix like 24
156 sub mask_to_prefix
157 {
158 return $_[0] =~ /^255\.255\.255\.(\d+)$/ ? 32-&log2(256-$1) :
159        $_[0] =~ /^255\.255\.(\d+)\.0$/ ? 24-&log2(256-$1) :
160        $_[0] =~ /^255\.(\d+)\.0\.0$/ ? 16-&log2(256-$1) :
161        $_[0] =~ /^(\d+)\.0\.0\.0$/ ? 8-&log2(256-$1) : 32;
162 }
163
164 sub log2
165 {
166 return int(log($_[0])/log(2));
167 }
168
169 # module_for_interface(&interface)
170 # Returns a structure containing details of some other module that manages
171 # some active interface
172 sub module_for_interface
173 {
174 if (&foreign_check("zones") && $_[0]->{'zone'}) {
175         # Zones virtual interface
176         return { 'module' => 'zones',
177                  'desc' => &text('mod_zones', $_[0]->{'zone'}) };
178         }
179 if (&foreign_check("virtual-server") && $_[0]->{'virtual'} ne '') {
180         # Check for a Virtualmin interface
181         &foreign_require("virtual-server", "virtual-server-lib.pl");
182         local ($d) = &virtual_server::get_domain_by("ip", $_[0]->{'address'});
183         if ($d) {
184                 return { 'module' => 'virtual-server',
185                          'desc' => &text('mod_virtualmin', $d->{'dom'}) };
186                 }
187         if (defined(&virtual_server::list_resellers)) {
188                 ($resel) = grep { $_->{'acl'}->{'defip'} eq $_[0]->{'address'} }
189                                 &virtual_server::list_resellers();
190                 if ($resel) {
191                         return { 'module' => 'virtual-server',
192                                  'desc' => &text('mod_reseller',
193                                                  $resel->{'name'}) };
194                         }
195                 }
196         }
197 return undef if ($_[0]->{'name'} !~ /^ppp/);    # only for PPP
198 if (&foreign_check("ppp-client")) {
199         # Dialup PPP connection
200         &foreign_require("ppp-client", "ppp-client-lib.pl");
201         local ($ip, $pid, $sect) = &ppp_client::get_connect_details();
202         if ($ip eq $_[0]->{'address'}) {
203                 return { 'module' => 'ppp-client',
204                          'desc' => &text('mod_ppp', $sect) };
205                 }
206         }
207 if (&foreign_check("adsl-client")) {
208         # ADSL PPP connection
209         &foreign_require("adsl-client", "adsl-client-lib.pl");
210         local ($dev, $ip) = &adsl_client::get_adsl_ip();
211         if ("ppp$dev" eq $_[0]->{'fullname'}) {
212                 return { 'module' => 'adsl-client',
213                          'desc' => &text('mod_adsl') };
214                 }
215         }
216 if (&foreign_check("pap")) {
217         # Dialin PPP connection
218         # XXX not handled yet
219         }
220 if (&foreign_check("pptp-client")) {
221         # PPTP client connection
222         &foreign_require("pptp-client", "pptp-client-lib.pl");
223         local @tunnels = &pptp_client::list_tunnels();
224         local %tunnels = map { $_->{'name'}, 1 } @tunnels;
225         local @conns = &pptp_client::list_connected();
226         foreach $c (@conns) {
227                 if ($c->[2] eq $_[0]->{'fullname'}) {
228                         return { 'module' => 'pptp-client',
229                                  'desc' => &text('mod_pptpc', "<i>$c->[0]</i>") };
230                         }
231                 }
232         }
233 if (&foreign_check("pptp-server")) {
234         # PPTP server connection
235         &foreign_require("pptp-server", "pptp-server-lib.pl");
236         local @conns = &pptp_server::list_connections();
237         local $c;
238         foreach $c (@conns) {
239                 if ($c->[3] eq $_[0]->{'fullname'} ||
240                     $c->[4] eq $_[0]->{'address'}) {
241                         return { 'module' => 'pptp-server',
242                                  'desc' => &text('mod_pptps', $c->[2]) };
243                         }
244                 }
245         }
246 return undef;
247 }
248
249 # can_iface(name)
250 sub can_iface
251 {
252 local $name = ref($_[0]) && $_[0]->{'fullname'} ? $_[0]->{'fullname'} :
253               ref($_[0]) ? $_[0]->{'name'}.
254                    ($_[0]->{'virtual'} ne "" ? ":$_[0]->{'virtual'}" : "") :
255                    $_[0];
256 return 0 if ($access{'ifcs'} == 0 || $access{'ifcs'} == 1);
257 return 1 if ($access{'ifcs'} == 2);
258 local %can = map { $_, 1 } split(/\s+/, $access{'interfaces'});
259 if ($access{'ifcs'} == 3) {
260         return $can{$name};
261         }
262 else {
263         return !$can{$name};
264         }
265 }
266
267 sub can_create_iface
268 {
269 return $access{'ifcs'} == 2;
270 }
271
272 # interface_choice(name, value, blankmode-text, [disabled?], [non-virt-only])
273 # Returns HTML for an interface chooser menu
274 sub interface_choice
275 {
276 my ($name, $value, $blanktext, $disabled, $nonvirt) = @_;
277 my @ifacestrs = grep { $_->{'fullname'} }
278                      ( &active_interfaces(), &boot_interfaces() );
279 if ($nonvirt) {
280         @ifacestrs = grep { $_->{'virtual'} eq '' } @ifacestrs;
281         }
282 my @ifaces = map { $_->{'fullname'} } @ifacestrs;
283 @ifaces = sort { $a cmp $b } &unique(@ifaces);
284 my @opts;
285 my $found;
286 if ($blanktext) {
287         push(@opts, [ '', $blanktext ]);
288         }
289 $found++ if ($value eq "");
290 foreach my $i (@ifaces) {
291         push(@opts, [ $i, $i ]);
292         $found++ if ($value eq $i);
293         }
294 push(@opts, [ 'other', $text{'chooser_other'} ]);
295 return &ui_select($name, !$found && $value ? 'other' : $value,
296                   \@opts, 1, 0, 0, $disabled)." ".
297        &ui_textbox($name."_other", $found ? "" : $value, $disabled);
298 }
299
300 # compute_broadcast(ip, netmask)
301 # Returns a computed broadcast address (ip ^ ~netmask)
302 sub compute_broadcast
303 {
304 local $ipnum = &ip_to_integer($_[0]);
305 local $nmnum = &ip_to_integer($_[1]);
306 return &integer_to_ip($ipnum | (~$nmnum));
307 }
308
309 # compute_network(ip, netmask)
310 # Returns a computed network address (ip & netmask)
311 sub compute_network
312 {
313 local $ipnum = &ip_to_integer($_[0]);
314 local $nmnum = &ip_to_integer($_[1]);
315 return &integer_to_ip($ipnum & $nmnum);
316 }
317
318 # ip_to_integer(ip)
319 # Given an IP address, returns a 32-bit number
320 sub ip_to_integer
321 {
322 local @ip = split(/\./, $_[0]);
323 return ($ip[0]<<24) + ($ip[1]<<16) + ($ip[2]<<8) + ($ip[3]<<0);
324 }
325
326 # integer_to_ip(integer)
327 # Given a 32-bit number, converts it to an IP
328 sub integer_to_ip
329 {
330 return sprintf "%d.%d.%d.%d",
331                 ($_[0]>>24)&0xff,
332                 ($_[0]>>16)&0xff,
333                 ($_[0]>>8)&0xff,
334                 ($_[0]>>0)&0xff;
335 }
336
337 # all_interfaces()
338 # Returns a list of all active and boot-time interfaces
339 sub all_interfaces
340 {
341 local @rv;
342 foreach my $a (&active_interfaces()) {
343         $a->{'active'} = 1;
344         push(@rv, $a);
345         }
346 foreach my $a (&boot_interfaces()) {
347         $a->{'boot'} = 1;
348         push(@rv, $a);
349         }
350 return @rv;
351 }
352
353 # check_netmask(netmask,ipaddress_associated)
354 # check if some netmask is properly formatted accordingly
355 # the associated address format (IPv4 or IPv6)
356 sub check_netmask
357 {
358   local($netmask,$address)= @_;
359   local($ret);
360   
361   # Detect IP address type (V4, V6) and check syntax accordingly
362   if ( &check_ip6address($address)  ) {
363     $ret=&check_ip6netmask($netmask);
364   }
365   
366   else {
367     $ret=&check_ipaddress($netmask);
368   }
369   return $ret;
370 }
371
372 # check_ip6netmask(netmask)
373 # check if some netmask has IPv6 format: its value is between 0 and 128.
374 sub check_ip6netmask
375 {   
376   return 0 if ( @_[0] <0 || @_[0] >128 );
377   return 1;
378 }
379
380 sub check_ipaddress_any
381 {
382 return &check_ipaddress($_[0]) || &check_ip6address($_[0]);
383 }
384
385 # common_order_input(name, value, &opts)
386 # Returns a field for a standard DNS resolution order input
387 sub common_order_input
388 {
389 my ($name, $value, $opts) = @_;
390 if ($value =~ /\[/) {
391         # Using a complex resolve list
392         return &ui_textbox($name, $value, 60);
393         }
394 else {
395         # Can select by menus
396         my $rv;
397         my @o = split(/\s+/, $value);
398         for(my $i = 0; $i<scalar(@o)+2; $i++) {
399                 $rv .= &ui_select($name."_".$i, $o[$i],
400                                   [ [ "", "&nbsp;" ], @$opts ], 1, 0, 1)."<br>";
401                 }
402         return $rv;
403         }
404 }
405
406 # supports_address6([&iface])
407 # Returns 1 if managing IPv6 interfaces is supported
408 sub supports_address6
409 {
410 local ($iface) = @_;
411 return 0;
412 }
413
414 1;
415