Handle hostnames with upper-case letters
[webmin.git] / bind8 / free_chooser.cgi
1 #!/usr/local/bin/perl
2 # Show a list of free IP addresses, within the configured ranges
3
4 require './bind8-lib.pl';
5
6 # Go through all zones to find IPs in use, and networks
7 $conf = &get_config();
8 @views = &find("view", $conf);
9 foreach $v (@views) {
10         @vz = &find("zone", $v->{'members'});
11         map { $view{$_} = $v } @vz;
12         push(@zones, @vz);
13         }
14 push(@zones, &find("zone", $conf));
15 foreach $z (@zones) {
16         $type = &find_value("type", $z->{'members'});
17         next if ($type ne "master");
18         $file = &find_value("file", $z->{'members'});
19         @recs = &read_zone_file($file, $z->{'value'});
20         foreach $r (@recs) {
21                 if ($r->{'type'} eq 'A') {
22                         $taken{$r->{'values'}->[0]}++;
23                         $net = $r->{'values'}->[0];
24                         $net =~ s/\d+$/0/;
25                         if ($net ne "127.0.0.0") {
26                                 $nets{$net}++;
27                                 }
28                         }
29                 elsif ($r->{'type'} eq 'PTR') {
30                         $taken{&arpa_to_ip($r->{'values'}->[0])}++;
31                         }
32                 }
33         }
34
35 # Use configured networks, if any
36 if ($config{'free_nets'}) {
37         @nets = split(/\s+/, $config{'free_nets'});
38         }
39 else {
40         @nets = keys %nets;
41         }
42 @nets = sort { $a cmp $b } @nets;
43
44 # display list of free IPs in the nets
45 &popup_header($text{'free_title'});
46 print "<script>\n";
47 print "function select(f)\n";
48 print "{\n";
49 print "top.opener.ifield.value = f;\n";
50 print "top.close();\n";
51 print "return false;\n";
52 print "}\n";
53 print "</script>\n";
54 print &ui_columns_start([ $text{'free_ip'} ], 100);
55 foreach $net (@nets) {
56         @netip = split(/\./, $net);
57         if ($netip[3] eq "0") {
58                 $start = 1;
59                 $end = 255;
60                 }
61         elsif ($netip[3] =~ /^(\d+)\-(\d+)$/) {
62                 $start = $1;
63                 $end = $2;
64                 }
65         else {
66                 $start = $end = $netip[3];
67                 }
68         for($d=$start; $d<=$end; $d++) {
69                 $ip = "$netip[0].$netip[1].$netip[2].$d";
70                 if (!$taken{$ip}) {
71                         print &ui_columns_row([ "<a href=\"\" onClick='return select(\"$ip\")'>$ip</a>" ]);
72                         }
73                 }
74         }
75 print &ui_columns_end();
76 &popup_footer();
77