Handle hostnames with upper-case letters
[webmin.git] / init / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # Display a list of run-levels and the actions that are run at boot and
4 # shutdown time for each level
5
6 require './init-lib.pl';
7 require './hostconfig-lib.pl';
8 &ui_print_header(&text('index_mode', $text{'mode_'.$init_mode}),
9                  $text{'index_title'}, "", undef, 1, 1);
10
11 if ($init_mode eq "osx" && $access{'bootup'}) {
12         # This hostconfig if block written by Michael A Peters <mpeters@mac.com>
13         # for OSX/Darwin.
14         # build hostconfig table 
15         
16         @hconf_set = &hostconfig_settings();
17         %description_list = &hostconfig_gather(description);
18         
19         print &ui_columns_start([ &text('index_action'),
20                                   &text('index_setting'),
21                                   &text('index_desc') ], 100, 0);
22         $i = 0;
23         while (<@hconf_set>) {
24                 $action_description = $description_list{"$hconf_set[$i][0]"};
25                 print &hostconfig_table($hconf_set[$i][0], $hconf_set[$i][1], $action_description);
26                 $i++;
27                 }
28         print &ui_columns_end();
29         if ($access{'bootup'} == 1) {
30                 print &ui_links_row([
31                         "<a href='edit_hostconfig.cgi?1'>".
32                           "$text{'index_add_mac'}</a>",
33                         "<a href='edit_hostconfig.cgi?2'>".
34                           &text('index_editconfig',
35                                 "<tt>$config{'hostconfig'}</tt>")."</a>"
36                         ]);
37                 }
38         }
39 elsif ($init_mode eq "init" && $access{'bootup'}) {
40         # build list of normal and broken actions
41         @iacts = &list_actions();
42         foreach $a (@iacts) {
43                 @ac = split(/\s+/, $a);
44                 $nodemap{$ac[1]} = $ac[0];
45                 push(@acts, $ac[0]);
46                 push(@actsl, "0+$ac[0]");
47                 push(@actsf, $ac[0] =~ /^\// ? $ac[0]
48                                              : "$config{'init_dir'}/$ac[0]");
49                 }
50         @runlevels = &list_runlevels();
51         foreach $r (@runlevels) {
52                 foreach $w ("S", "K") {
53                         foreach $a (&runlevel_actions($r, $w)) {
54                                 @ac = split(/\s+/, $a);
55                                 if (!$nodemap{$ac[2]}) {
56                                         push(@acts, $ac[1]);
57                                         push(@actsl,
58                                              "1+$r+$ac[0]+$ac[1]+$ac[2]+$w");
59                                         push(@actsf, "$config{'init_base'}/rc$r.d/$w$ac[0]$ac[1]");
60                                         }
61                                 }
62                         }
63                 }
64
65         # For each action, look at /etc/rc*.d/* files to see if it is 
66         # started at boot
67         @boot = &get_inittab_runlevel();
68         for($i=0; $i<@acts; $i++) {
69                 foreach $s (&action_levels('S', $acts[$i])) {
70                         local ($l, $p) = split(/\s+/, $s);
71                         local ($lvl) = (&indexof($l, @boot) >= 0);
72                         local %daemon;
73                         if ($lvl && $config{'daemons_dir'} &&
74                             &read_env_file("$config{'daemons_dir'}/$acts[$i]",
75                                            \%daemon)) {
76                                 $lvl = lc($daemon{'ONBOOT'}) eq 'yes' ? 1 : 0;
77                                 }
78                         push(@{$actsb[$i]}, [ $l, $p, $lvl ]);
79                         }
80                 @{$actsb[$i]} = sort { $b->[2] <=> $a->[2] } @{$actsb[$i]};
81                 }
82
83         # Sort the actions if necessary
84         @order = ( 0 .. $#acts );
85         if ($config{'sort_mode'}) {
86                 @order = sort { local $aa = $actsb[$a]->[0];
87                                 local $bb = $actsb[$b]->[0];
88                                 $bb->[2] <=> $aa->[2] ||
89                                 $bb->[1] <=> $aa->[1] }
90                               @order;
91                 }
92         @acts = map { $acts[$_] } @order;
93         @actsl = map { $actsl[$_] } @order;
94         @actsf = map { $actsf[$_] } @order;
95         @actsb = map { $actsb[$_] } @order;
96
97         @links = ( );
98         if ($access{'bootup'} == 1) {
99                 push(@links,
100                         "<a href='edit_action.cgi?2'>$text{'index_add'}</a>");
101                 }
102         if (!$config{'desc'}) {
103                 # Display actions by name only
104                 print &ui_links_row(\@links);
105                 @grid = ( );
106                 for($i=0; $i<@acts; $i++) {
107                         if ($acts[$i]) {
108                                 push(@grid, "<a href=\"edit_action.cgi?".
109                                             "$actsl[$i]\">$acts[$i]</a>");
110                                 }
111                         }
112                 print &ui_grid_table(\@grid, 4, 100,
113                      [ "width=25%", "width=25%", "width=25%", "width=25%" ],
114                      undef, $text{'index_title'});
115                 print &ui_links_row(\@links);
116                 }
117         else {
118                 # Display actions and descriptions
119                 print &ui_form_start("mass_start_stop.cgi", "post");
120                 print &ui_links_row(\@links);
121                 print &ui_columns_start([
122                         "",
123                         $text{'index_action'},
124                         $config{'desc'} == 2 ? $text{'index_levels'}
125                                              : $text{'index_boot'},
126                         $config{'order'} ? ( $text{'index_order'} ) : ( ),
127                         $config{'status_check'} == 2 ? ( $text{'index_status'} ) : ( ),
128                         $text{'index_desc'} ],
129                         100, 0, [ "", "nowrap", "nowrap", "nowrap", "nowrap" ]);
130
131                 for($i=0; $i<@acts; $i++) {
132                         local ($boot, %daemon, @levels, $order);
133                         foreach $s (@{$actsb[$i]}) {
134                                 if ($s->[2]) {
135                                         $boot = 1;
136                                         push(@levels,
137                                           "<font color=#ff0000>$s->[0]</font>");
138                                         }
139                                 else {
140                                         push(@levels, $s->[0]);
141                                         }
142                                 }
143                         $order = $actsb[$i]->[0]->[1];
144                         local @cols;
145                         push(@cols, "<a href=\"edit_action.cgi?".
146                                     "$actsl[$i]\">$acts[$i]</a>");
147                         local %has;
148                         $d = &html_escape(&init_description($actsf[$i],
149                                  $config{'status_check'} == 2 ? \%has : undef));
150                         if ($config{'desc'} == 2) {
151                                 push(@cols, join(" ", @levels));
152                                 }
153                         else {
154                                 push(@cols,$boot ? $text{'yes'} :
155                                       "<font color=#ff0000>$text{'no'}</font>");
156                                 }
157                         if ($config{'order'}) {
158                                 push(@cols, $order);
159                                 }
160                         if ($config{'status_check'} == 2) {
161                                 if ($actsl[$i] =~ /^0/ && $has{'status'}) {
162                                         local $r = &action_running($actsf[$i]);
163                                         if ($r == 0) {
164                                                 push(@cols,
165                                                         "<font color=#ff0000>".
166                                                         "$text{'no'}</font>");
167                                                 }
168                                         elsif ($r == 1) {
169                                                 push(@cols, $text{'yes'});
170                                                 }
171                                         else {
172                                                 push(@cols, undef);
173                                                 }
174                                         }
175                                 else {
176                                         push(@cols, undef);
177                                         }
178                                 }
179                         push(@cols, $d);
180                         if ($actsl[$i] =~ /^0/) {
181                                 print &ui_checked_columns_row(
182                                         \@cols, undef, "idx", $order[$i]);
183                                 }
184                         else {
185                                 print &ui_columns_row([ undef, @cols ]);
186                                 }
187                         }
188                 print &ui_columns_end();
189                 print &ui_links_row(\@links);
190                 @buts = ( [ "start", $text{'index_start'} ],
191                           [ "stop", $text{'index_stop'} ],
192                           [ "restart", $text{'index_restart'} ] );
193                 if ($access{'bootup'} == 1) {
194                         # Show buttons to enable/disable at boot
195                         push(@buts, undef,
196                             [ "addboot", $text{'index_addboot'} ],
197                             [ "delboot", $text{'index_delboot'} ],
198                             undef,
199                             [ "addboot_start", $text{'index_addboot_start'} ],
200                             [ "delboot_stop", $text{'index_delboot_stop'} ],
201                             );
202                         }
203                 print &ui_form_end(\@buts);
204                 }
205         }
206 elsif ($init_mode eq "local" && $access{'bootup'} == 1) {
207         # Display local bootup script
208         if ($config{'hostconfig'}) {
209                 # This means a darwin system where
210                 # daemons are not started in the rc script
211                 print &text('index_script_mac',
212                         "<tt>$config{'local_script'}</tt>"),"<br>\n";
213                 }
214         else {
215                 print &text('index_script',
216                         "<tt>$config{'local_script'}</tt>"),"<br>\n";
217                 }
218         print &ui_form_start("save_local.cgi", "post");
219         print &ui_textarea("local",
220                 &read_file_contents($config{'local_script'}), 15, 80)."<br>\n";
221
222         # Show shutdown script too, if any
223         if ($config{'local_down'}) {
224                 print &text('index_downscript',
225                         "<tt>$config{'local_down'}</tt>"),"<br>\n";
226                 print &ui_textarea("down",
227                         &read_file_contents($config{'local_down'}), 15, 80).
228                         "<br>\n";
229                 }
230
231         print &ui_form_end([ [ undef, $text{'save'} ] ]);
232         }
233 elsif ($init_mode eq "win32" && $access{'bootup'}) {
234         # Show Windows services
235         print &ui_form_start("save_services.cgi", "post");
236         print &select_all_link("d"),"\n";
237         print &select_invert_link("d"),"<br>\n";
238         print &ui_columns_start([ "", $text{'index_sname'},
239                                   $text{'index_sdesc'},
240                                   $text{'index_sboot'},
241                                   $text{'index_sstate'} ]);
242         foreach $svc (&list_win32_services()) {
243                 print &ui_columns_row([
244                         &ui_checkbox("d", $svc->{'name'}, undef),
245                         $svc->{'name'},
246                         $svc->{'desc'},
247                         $text{'index_sboot'.$svc->{'boot'}} ||
248                           $svc->{'boot_desc'},
249                         $text{'index_sstate'.$svc->{'state'}} ||
250                           $svc->{'state_desc'},
251                         ]);
252                 }
253         print &ui_columns_end();
254         print &select_all_link("d"),"\n";
255         print &select_invert_link("d"),"<br>\n";
256         print &ui_form_end([ [ "start", $text{'index_start'} ],
257                              [ "stop", $text{'index_stop'} ],
258                              undef,
259                              [ "addboot", $text{'index_addboot'} ],
260                              [ "delboot", $text{'index_delboot'} ],
261                              undef,
262                              [ "addboot_start", $text{'index_addboot_start'} ],
263                              [ "delboot_stop", $text{'index_delboot_stop'} ],
264                             ]);
265         print &ui_hr();
266         }
267 elsif ($init_mode eq "rc" && $access{'bootup'}) {
268         # Show FreeBSD scripts
269         print &ui_form_start("mass_rcs.cgi", "post");
270         @links = ( &select_all_link("d"),
271                    &select_invert_link("d"),
272                    "<a href='edit_rc.cgi?new=1'>$text{'index_radd'}</a>" );
273         print &ui_links_row(\@links);
274         print &ui_columns_start([ "", $text{'index_rname'},
275                                   $text{'index_rdesc'},
276                                   $text{'index_rboot'} ]);
277         foreach $rc (&list_rc_scripts()) {
278                 print &ui_columns_row([
279                         &ui_checkbox("d", $rc->{'name'}, undef),
280                         "<a href='edit_rc.cgi?name=".
281                           &urlize($rc->{'name'})."'>$rc->{'name'}</a>",
282                         $rc->{'desc'},
283                         $rc->{'enabled'} == 1 ? $text{'yes'} :
284                         $rc->{'enabled'} == 2 ? "<i>$text{'index_unknown'}</i>":
285                                 "<font color=#ff0000>$text{'no'}</font>",
286                         ]);
287                 }
288         print &ui_columns_end();
289         print &ui_links_row(\@links);
290         print &ui_form_end([ [ "start", $text{'index_start'} ],
291                              [ "stop", $text{'index_stop'} ],
292                              undef,
293                              [ "addboot", $text{'index_addboot'} ],
294                              [ "delboot", $text{'index_delboot'} ],
295                              undef,
296                              [ "addboot_start", $text{'index_addboot_start'} ],
297                              [ "delboot_stop", $text{'index_delboot_stop'} ],
298                             ]);
299         }
300 elsif ($init_mode eq "upstart" && $access{'bootup'}) {
301         # Show upstart actions
302         print &ui_form_start("mass_upstarts.cgi", "post");
303         @links = ( &select_all_link("d"),
304                    &select_invert_link("d"),
305                    "<a href='edit_upstart.cgi?new=1'>$text{'index_uadd'}</a>" );
306         print &ui_links_row(\@links);
307         print &ui_columns_start([ "", $text{'index_uname'},
308                                   $text{'index_udesc'},
309                                   $text{'index_uboot'},
310                                   $text{'index_ustatus'}, ]);
311         foreach $u (&list_upstart_services()) {
312                 if ($u->{'legacy'}) {
313                         $l = "edit_action.cgi?0+".&urlize($u->{'name'});
314                         }
315                 else {
316                         $l = "edit_upstart.cgi?name=".&urlize($u->{'name'});
317                         }
318                 print &ui_columns_row([
319                         &ui_checkbox("d", $u->{'name'}, undef),
320                         "<a href='$l'>$u->{'name'}</a>",
321                         $u->{'desc'},
322                         $u->{'boot'} eq 'start' ? $text{'yes'} :
323                           $u->{'boot'} eq 'stop' ?
324                           "<font color=#ff0000>$text{'no'}</font>" :
325                           "<i>$text{'index_unknown'}</i>",
326                         $u->{'status'} eq 'running' ? $text{'yes'} :
327                           $u->{'status'} eq 'waiting' ?
328                           "<font color=#ff0000>$text{'no'}</font>" :
329                           "<i>$text{'index_unknown'}</i>",
330                         ]);
331                 }
332         print &ui_columns_end();
333         print &ui_links_row(\@links);
334         print &ui_form_end([ [ "start", $text{'index_start'} ],
335                              [ "stop", $text{'index_stop'} ],
336                              [ "restart", $text{'index_restart'} ],
337                              undef,
338                              [ "addboot", $text{'index_addboot'} ],
339                              [ "delboot", $text{'index_delboot'} ],
340                              undef,
341                              [ "addboot_start", $text{'index_addboot_start'} ],
342                              [ "delboot_stop", $text{'index_delboot_stop'} ],
343                             ]);
344
345         }
346
347 # reboot/shutdown buttons
348 print &ui_hr();
349 print &ui_buttons_start();
350 if ($init_mode eq 'init' && $access{'bootup'} == 1) {
351         print &ui_buttons_row("change_rl.cgi", $text{'index_rlchange'},
352                               $text{'index_rlchangedesc'}, undef,
353                               &ui_select("level", $boot[0], \@runlevels));
354         }
355 if ($access{'reboot'}) {
356         print &ui_buttons_row("reboot.cgi", $text{'index_reboot'},
357                               $text{'index_rebootmsg'});
358         }
359 if ($access{'shutdown'}) {
360         print &ui_buttons_row("shutdown.cgi", $text{'index_shutdown'},
361                               $text{'index_shutdownmsg'});
362         }
363 print &ui_buttons_end();
364
365 &ui_print_footer("/", $text{'index'});
366