Handle hostnames with upper-case letters
[webmin.git] / bind8 / index.cgi
1 #!/usr/local/bin/perl
2 # Display a list of domains, views, and icons for global options.
3
4 require './bind8-lib.pl';
5 &ReadParse();
6
7 $need_create = !-r &make_chroot($config{'named_conf'}) ||
8                $in{'create'};
9
10 # Check if bind is installed
11 if (!-x $config{'named_path'}) {
12         &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0,
13                 &help_search_link("bind", "doc", "google"));
14         print "<p>",&text('index_enamed', "<tt>$config{'named_path'}</tt>",
15                           "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
16
17         &foreign_require("software", "software-lib.pl");
18         $lnk = &software::missing_install_link("bind", $text{'index_bind'},
19                         "../$module_name/", $text{'index_title'});
20         print $lnk,"<p>\n" if ($lnk);
21
22         &ui_print_footer("/", $text{"index"});
23         exit;
24         }
25
26 # Check if BIND is the right version.. Only BIND 8/9 offers the -f option
27 # Is there a better way to do this?
28 if ($out = &check_bind_8()) {
29         &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0,
30                 &help_search_link("bind", "doc", "google"));
31         print "<p>",&text('index_eversion', "<tt>$config{'named_path'}</tt>",
32                           "/dnsadmin/", "<tt>$config{'named_path'} -help</tt>",
33                           "<pre>$out</pre>"),"<p>\n";
34         &ui_print_footer("/", $text{"index"});
35         exit;
36         }
37
38 # Try to get the version number, and save for later calls
39 $bind_version = &get_bind_version();
40 &open_tempfile(VERSION, ">$module_config_directory/version");
41 &print_tempfile(VERSION, "$bind_version\n");
42 &close_tempfile(VERSION);
43
44 # Get the list of zones
45 @allzones = &list_zone_names();
46 @zones = grep { $_->{'type'} ne 'view' &&
47                 &can_edit_zone($_) &&
48                 (!$access{'ro'} || $_->{'name'} ne '.') } @allzones;
49 @views = grep { $_->{'type'} eq 'view' } @allzones;
50 @hashint = grep { $_->{'type'} ne 'view' &&
51                   $_->{'name'} eq '.' } @allzones;
52
53 if (@zones == 1 && $access{'zones'} ne '*' && !$access{'defaults'} &&
54     !$access{'views'} && $access{'apply'} != 1 && !$access{'master'} &&
55     !$access{'slave'} && !$access{'forward'} && $access{'noconfig'}) {
56         # Only one zone, so go direct to it
57         $z = $zones[0];
58         &redirect("edit_master.cgi?index=$z->{'index'}".
59                   ($z->{'viewindex'} eq '' ? '' : '&view='.$z->{'viewindex'}));
60         exit;
61         }
62
63 $chroot = &get_chroot();
64 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0,
65         &restart_links().'<br>'.
66         &help_search_link("bind", "doc", "google"), undef, undef,
67         &text($chroot eq "/" || !$chroot ? 'index_version' : 'index_chroot',
68               $bind_version, "<tt>$chroot</tt>"));
69
70 # If the named.conf file does not exist, offer to create it
71 if ($need_create) {
72         print &text('index_eempty',
73                     "<tt>".&make_chroot($config{'named_conf'})."</tt>"),"<p>\n";
74
75         print &ui_form_start("dns_boot.cgi");
76         print &ui_radio("real", 1,
77                 [ [ 0, $text{'index_local'}."<br>" ],
78                   [ 1, $text{'index_download'}."<br>" ],
79                   [ 2, $text{'index_webmin'}."<br>" ] ]);
80         print &ui_form_end([ [ undef, $text{'index_create'} ] ]);
81
82         &ui_print_footer("/", $text{"index"});
83         exit;
84         }
85
86 if ($access{'defaults'}) {
87         # display global options
88         print &ui_subheading($text{'index_opts'});
89         @olinks = ("conf_servers.cgi", "conf_logging.cgi", "conf_acls.cgi",
90                    "conf_files.cgi", "conf_forwarding.cgi", "conf_net.cgi",
91                    "conf_misc.cgi", "conf_controls.cgi", "conf_keys.cgi",
92                    "conf_zonedef.cgi", "list_slaves.cgi",
93                    $bind_version >= 9 ? ( "conf_rndc.cgi" ) : ( ),
94                    &supports_dnssec_client() ? ( "conf_trusted.cgi" ) : ( ),
95                    &supports_dnssec() ? ( "conf_dnssec.cgi" ) : ( ),
96                    &supports_check_conf() ? ( "conf_ncheck.cgi" ) : ( ),
97                    "conf_manual.cgi" );
98         @otitles = map { /(conf|list)_(\S+).cgi/; $text{$2."_title"} } @olinks;
99         @oicons = map { /^(conf|list)_(\S+).cgi/; "images/$2.gif"; } @olinks;
100         &icons_table(\@olinks, \@otitles, \@oicons, 6);
101         print &ui_hr();
102         }
103
104 # Work out what creation links we have
105 @crlinks = ( );
106 if ($access{'master'} && !$access{'ro'}) {
107         push(@crlinks,
108              "<a href=\"master_form.cgi\">$text{'index_addmaster'}</a>");
109         }
110 if ($access{'slave'} && !$access{'ro'}) {
111         push(@crlinks,
112              "<a href=\"slave_form.cgi\">$text{'index_addslave'}</a>");
113         push(@crlinks,
114              "<a href=\"stub_form.cgi\">$text{'index_addstub'}</a>");
115         }
116 if ($access{'forward'} && !$access{'ro'}) {
117         push(@crlinks,
118              "<a href=\"forward_form.cgi\">$text{'index_addfwd'}</a>");
119         }
120 if ($access{'delegation'} && !$access{'ro'} && &version_atleast(9, 2, 1)) {
121         push(@crlinks,
122              "<a href=\"delegation_form.cgi\">$text{'index_adddele'}</a>");
123         }
124 if ($access{'master'} && !$access{'ro'} &&
125     scalar(@hashint) < (@views ? scalar(@views) : 1)) {
126         push(@crlinks,
127              "<a href=\"hint_form.cgi\">$text{'index_addhint'}</a>");
128         }
129 if (@crlinks) {
130         push(@crlinks,
131              "<a href=\"mass_form.cgi\">$text{'index_addmass'}</a>");
132         }
133
134 if (@zones > $config{'max_zones'}) {
135         # Too many zones, show search form
136         print &ui_subheading($text{'index_zones'});
137         print "$text{'index_toomany'}<p>\n";
138         print &ui_form_start("find_zones.cgi");
139         print "<b>$text{'index_find'}</b>\n";
140         print &ui_textbox("search", undef, 20);
141         print &ui_form_end([ [ undef, $text{'index_search'} ] ]);
142         print &ui_links_row(\@crlinks);
143         }
144 elsif (@zones && (!@views || !$config{'by_view'})) {
145         # Show all zones
146         print &ui_subheading($text{'index_zones'});
147         foreach $z (@zones) {
148                 $v = $z->{'name'};
149                 $t = $z->{'type'};
150                 next if (!$t);
151                 $t = "delegation" if ($t eq "delegation-only");
152                 local $zn = $v eq "." ? "<i>$text{'index_root'}</i>"
153                                       : &ip6int_to_net(&arpa_to_ip($v));
154                 if ($z->{'view'}) {
155                         local $vw = $z->{'viewindex'};
156                         push(@zlinks, "edit_$t.cgi?index=$z->{'index'}".
157                                       "&view=$vw");
158                         push(@ztitles, $zn." ".
159                                &text('index_view', "<tt>$z->{'view'}</tt>"));
160                         push(@zdels, &can_edit_zone($z, $vw) ?
161                                 $z->{'index'}." ".$z->{'viewindex'} : undef);
162                         }
163                 else {
164                         push(@zlinks, "edit_$t.cgi?index=$z->{'index'}");
165                         push(@ztitles, $zn);
166                         push(@zdels, &can_edit_zone($z) ?
167                                 $z->{'index'} : undef);
168                         }
169                 push(@zsort, $t eq 'hint' ? undef : $ztitles[$#ztitles]);
170                 push(@zicons, "images/$t.gif");
171                 push(@ztypes, $text{"index_$t"});
172                 $zhash{$zn} = $z;
173                 $ztitlehash{$zn} = $ztitles[$#ztitles];
174                 $zlinkhash{$zn} = $zlinks[$#zlinks];
175                 $ztypeshash{$zn} = $ztypes[$#ztypes];
176                 $zdelhash{$zn} = $zdels[$#ztypes];
177                 $len++;
178                 }
179
180         # sort list of zones
181         @zorder = sort { &compare_zones($zsort[$a], $zsort[$b]) } (0 .. $len-1);
182         @zlinks = map { $zlinks[$_] } @zorder;
183         @ztitles = map { $ztitles[$_] } @zorder;
184         @zicons = map { $zicons[$_] } @zorder;
185         @ztypes = map { $ztypes[$_] } @zorder;
186         @zdels = map { $zdels[$_] } @zorder;
187
188         print &ui_form_start("mass_delete.cgi", "post");
189         @links = ( &select_all_link("d", 0),
190                    &select_invert_link("d", 0),
191                    @crlinks );
192         print &ui_links_row(\@links);
193
194         if ($config{'show_list'} == 1) {
195                 # display as list
196                 $mid = int((@zlinks+1)/2);
197                 @grid = ( );
198                 push(@grid, &zones_table([ @zlinks[0 .. $mid-1] ],
199                                       [ @ztitles[0 .. $mid-1] ],
200                                       [ @ztypes[0 .. $mid-1] ],
201                                       [ @zdels[0 .. $mid-1] ] ));
202                 if ($mid < @zlinks) {
203                         push(@grid, &zones_table([ @zlinks[$mid .. $#zlinks] ],
204                                              [ @ztitles[$mid .. $#ztitles] ],
205                                              [ @ztypes[$mid .. $#ztypes] ],
206                                              [ @zdels[$mid .. $#zdels] ]));
207                         }
208                 print &ui_grid_table(\@grid, 2, 100,
209                                      [ "width=50%", "width=50%" ]);
210                 }
211         elsif ($config{'show_list'} == 2) {
212                 # Show as collapsible tree, broken down by domain parts
213                 %heiropen = map { $_, 1 } &get_heiropen();
214                 $heiropen{""} = 1;
215                 foreach $z (grep { $_->{'type'} } @zones) {
216                         local $v = $z->{'name'};
217                         local @p = split(/\./, &ip6int_to_net(&arpa_to_ip($v)));
218                         for($i=1; $i<=@p; $i++) {
219                                 local $ch = join(".", @p[$i-1 .. $#p]);
220                                 local $par = $i == @p ?
221                                         "" : join(".", @p[$i .. $#p]);
222                                 @{$ztree{$par}} = &unique(@{$ztree{$par}}, $ch);
223                                 }
224                         }
225                 print "<table>\n";
226                 &recursive_tree("");
227                 print "</table>\n";
228                 }
229         else {
230                 # display as icons
231                 @befores = map { $_ ? &ui_checkbox("d", $_, "", 0) : "" }
232                                @zdels;
233                 &icons_table(\@zlinks, \@ztitles, \@zicons, 5, undef, 
234                              undef, undef, \@befores);
235                 }
236         print &ui_links_row(\@links);
237         print &ui_form_end([ $access{'delete'} ?
238                               ( [ "delete", $text{'index_massdelete'} ] ) : ( ),
239                              [ "update", $text{'index_massupdate'} ],
240                              [ "create", $text{'index_masscreate'} ],
241                              [ "rdelete", $text{'index_massrdelete'} ] ]);
242         }
243 elsif (@zones) {
244         # Show zones under views
245         print &ui_subheading($text{'index_zones'});
246         foreach $vw (@views) {
247                 local (@zorder, @zlinks, @ztitles, @zicons, @ztypes, @zsort, @zdels, $len);
248                 local @zv = grep { $_->{'view'} eq $vw->{'name'} } @zones;
249                 next if (!@zv);
250                 print "<b>",&text('index_inview',
251                                   "<tt>$vw->{'name'}</tt>"),"</b><br>\n";
252                 foreach $z (@zv) {
253                         $v = $z->{'name'};
254                         $t = $z->{'type'};
255                         local $zn = $v eq "." ? "<i>$text{'index_root'}</i>"
256                                               : &ip6int_to_net(&arpa_to_ip($v));
257                         push(@zlinks, "edit_$t.cgi?index=$z->{'index'}".
258                                       "&view=$z->{'viewindex'}");
259                         push(@ztitles, $zn);
260                         push(@zsort, $t eq 'hint' ? undef : $ztitles[$#ztitles]);
261                         push(@zicons, "images/$t.gif");
262                         push(@ztypes, $text{"index_$t"});
263                         push(@zdels, $z->{'index'}." ".$z->{'viewindex'});
264                         $len++;
265                         }
266
267                 # sort list of zones
268                 @zorder = sort { &compare_zones($zsort[$a], $zsort[$b]) }
269                                (0 .. $len-1);
270                 @zlinks = map { $zlinks[$_] } @zorder;
271                 @ztitles = map { $ztitles[$_] } @zorder;
272                 @zicons = map { $zicons[$_] } @zorder;
273                 @ztypes = map { $ztypes[$_] } @zorder;
274                 @zdels = map { $zdels[$_] } @zorder;
275
276                 print &ui_form_start("mass_delete.cgi", "post");
277                 print &ui_links_row(\@crlinks);
278                 if ($config{'show_list'}) {
279                         # display as list
280                         $mid = int((@zlinks+1)/2);
281                         @grid = ( );
282                         push(@grid, &zones_table([ @zlinks[0 .. $mid-1] ],
283                                              [ @ztitles[0 .. $mid-1] ],
284                                              [ @ztypes[0 .. $mid-1] ],
285                                              [ @zdels[0 .. $mid-1] ]));
286                         if ($mid < @zlinks) {
287                                 push(@grid, &zones_table(
288                                              [ @zlinks[$mid .. $#zlinks] ],
289                                              [ @ztitles[$mid .. $#ztitles] ],
290                                              [ @ztypes[$mid .. $#ztypes] ],
291                                              [ @zdels[$mid .. $#zdels] ]));
292                                 }
293                         print &ui_grid_table(\@grid, 2, 100,
294                                              [ "width=50%", "width=50%" ]);
295                         }
296                 else {
297                         # display as icons
298                         @befores = map { $_ ? &ui_checkbox("d", $_, "", 0) : "" }
299                                        @zdels;
300                         &icons_table(\@zlinks, \@ztitles, \@zicons, 5, undef,
301                                      undef, undef, \@befores);
302                         }
303                 print &ui_links_row(\@crlinks);
304                 print &ui_form_end([
305                         $access{'delete'} ?
306                           ( [ "delete", $text{'index_massdelete'} ] ) : ( ),
307                         [ "update", $text{'index_massupdate'} ],
308                         [ "create", $text{'index_masscreate'} ],
309                         [ "rdelete", $text{'index_massrdelete'} ], ]);
310                 }
311         }
312 else {
313         print "<b>$text{'index_none'}</b><p>\n";
314         print &ui_links_row(\@crlinks);
315         }
316
317 if ($access{'views'} && $bind_version >= 9) {
318         # Display list of views
319         print &ui_hr();
320         print &ui_subheading($text{'index_views'});
321
322         # Show a warning if any zones are not in a view
323         @notinview = grep { $_->{'viewindex'} eq '' } @zones;
324         if (@notinview && @views) {
325                 print "<b>",&text('index_viewwarn',
326                   join(" , ", map { "<tt>".&ip6int_to_net(
327                                           &arpa_to_ip($_->{'name'}))."</tt>" }
328                                 @notinview)),"</b><p>\n";
329                 print "<b>$text{'index_viewwarn2'}</b><p>\n";
330                 }
331
332         @views = grep { &can_edit_view($_) } @views;
333         foreach $v (@views) {
334                 push(@vlinks, "edit_view.cgi?index=$v->{'index'}");
335                 push(@vtitles, $v->{'name'});
336                 push(@vicons, "images/view.gif");
337                 }
338         @links = ( );
339         push(@links, "<a href=\"view_form.cgi\">$text{'index_addview'}</a>")
340                 if (!$access{'ro'} && $access{'views'} != 2);
341         if (@views) {
342                 print &ui_links_row(\@links);
343                 &icons_table(\@vlinks, \@vtitles, \@vicons, 5);
344                 }
345         else {
346                 print "<b>$text{'index_vnone'}</b><p>\n";
347                 }
348         print &ui_links_row(\@links);
349         }
350
351 &ui_print_footer("/", $text{"index"});
352
353 sub dump_config
354 {
355 local($c);
356 foreach $c (@{$_[0]}) {
357         print "$_[1]$c->{'name'} ",
358                 join(',', @{$c->{'values'}});
359         if ($c->{'type'}) {
360                 print " {\n";
361                 &dump_config($c->{'members'}, "$_[1]\t");
362                 print "$_[1]}\n";
363                 }
364         else { print "\n"; }
365         }
366 }
367
368 sub compare_zones
369 {
370 local @sp0 = split(/\./, lc($_[0]));
371 local @sp1 = split(/\./, lc($_[1]));
372 for($i=0; $i<@sp0 || $i<@sp1; $i++) {
373         if ($sp0[$i] =~ /^\d+$/ && $sp1[$i] =~ /^\d+$/) {
374                 return -1 if ($sp0[$i] < $sp1[$i]);
375                 return 1 if ($sp0[$i] > $sp1[$i]);
376                 }
377         else {
378                 local $c = $sp0[$i] cmp $sp1[$i];
379                 return $c if ($c);
380                 }
381         }
382 return 0;
383 }
384
385 sub recursive_tree
386 {
387 local ($name, $depth) = @_;
388 print "<tr> <td>", "&nbsp;&nbsp;" x $depth;
389 if ($_[0] ne "") {
390         print "<a name=\"$name\"></a>\n";
391         $name =~ /^([^\.]+)/;
392         if (!$ztree{$name}) {
393                 # Has no children
394                 print "<img border=0 src=images/smallicon.gif>&nbsp; $1</td>\n",
395                 }
396         else {
397                 # Has children
398                 local $act = $heiropen{$name} ? "close" : "open";
399                 print "<a href=\"$act.cgi?what=",&urlize($name),"\">";
400                 print "<img border=0 src=images/$act.gif></a>&nbsp; $1</td>\n",
401                 }
402         }
403 else {
404         # Is the root
405         print "<img src=images/close.gif> <i>$text{'index_all'}</i></td>\n";
406         }
407 if ($zhash{$name}) {
408         local $cb = $zdelhash{$name} ?
409                 &ui_checkbox("d", $zdelhash{$name}, "", 0)." " : "";
410         print "<td>$cb<a href='$zlinkhash{$name}'>$ztitlehash{$name} ($ztypeshash{$name})</a></td> </tr>\n";
411         }
412 else {
413         print "<td><br></td> </tr>\n";
414         }
415 if ($heiropen{$name}) {
416         foreach $sz (@{$ztree{$name}}) {
417                 &recursive_tree($sz, $depth+1);
418                 }
419         }
420 }
421