Handle hostnames with upper-case letters
[webmin.git] / init / edit_action.cgi
1 #!/usr/local/bin/perl
2 # edit_action.cgi
3 # Edit or create a bootup action. Existing actions can either be in the
4 # init.d directory (and linked to from the appropriate runlevels), or
5 # just plain runlevel files
6
7 require './init-lib.pl';
8 $access{'bootup'} || &error($text{'edit_ecannot'});
9
10 $ty = $ARGV[0];
11 if ($ty == 0) {
12         # Editing an action in init.d, linked to from various runlevels
13         $ac = $ARGV[1];
14         &ui_print_header(undef, $text{'edit_title'}, "");
15         $file = &action_filename($ac);
16         $data = &read_file_contents($file);
17         $hasarg = &get_action_args($file);
18         }
19 elsif ($ty == 1) {
20         # Editing an action in one of the runlevels
21         $rl = $ARGV[1];
22         $num = $ARGV[2];
23         $ac = $ARGV[3];
24         $inode = $ARGV[4];
25         $ss = $ARGV[5];
26         &ui_print_header(undef, $text{'edit_title'}, "");
27         $file = &runlevel_filename($rl, $ss, $num, $ac);
28         $data = &read_file_contents($file);
29         }
30 else {
31         # Creating a new action in init.d
32         &ui_print_header(undef, $text{'create_title'}, "");
33         }
34
35 print &ui_form_start("save_action.cgi", "form-data");
36 print &ui_table_start($text{'edit_details'}, "width=100%", 2);
37 print &ui_hidden("type", $ty);
38
39 if ($ty != 2) {
40         print &ui_hidden("old", $ac);
41         print &ui_hidden("file", $file);
42         if ($ty == 1) {
43                 print &ui_hidden("runlevel", $rl);
44                 print &ui_hidden("startstop", $ss);
45                 print &ui_hidden("number", $num);
46                 }
47         }
48
49 # Action name
50 if ($ac =~ /^\// || $access{'bootup'} == 2) {
51         print &ui_table_row($text{'edit_name'}, "<tt>$ac</tt>");
52         print &ui_hidden("name", $ac);
53         print &ui_hidden("extra", 1);
54         }
55 else {
56         print &ui_table_row($text{'edit_name'},
57                             &ui_textbox("name", $ac, 30));
58         }
59
60 if ($ty == 2) {
61         # Display fields for a template for a new action
62         print &ui_table_row($text{'edit_desc'},
63                 &ui_textarea("desc", undef, 2, 80));
64
65         if ($config{'start_stop_msg'}) {
66                 print &ui_table_row($text{'edit_startmsg'},
67                         &ui_textbox("start_msg", undef, 40));
68
69                 print &ui_table_row($text{'edit_stopmsg'},
70                         &ui_textbox("stop_msg", undef, 40));
71                 }
72
73         print &ui_table_row($text{'edit_start'},
74                 &ui_textarea("start", undef, 5, 80));
75
76         print &ui_table_row($text{'edit_stop'},
77                 &ui_textarea("stop", undef, 5, 80));
78         }
79 elsif ($access{'bootup'} == 2) {
80         # Just show current script
81         print &ui_table_row($text{'edit_script'},
82                 &ui_textarea("data", $data, 15, 80, undef, undef,
83                              "readonly=true"));
84         }
85 else {
86         # Allow direct editing of the script
87         print &ui_table_row($text{'edit_script'},
88                 &ui_textarea("data", $data, 15, 80));
89         }
90
91 if ($ty == 1 && $access{'bootup'} == 1) {
92         # Display a message about the script being bogus
93         print &ui_table_end();
94         print "<b>",&text("edit_bad$ss", $rl),"</b><br>\n";
95         print "<a href=\"fix_action.cgi?$rl+$ss+$num+$ac\">",
96               "$text{'edit_fix'}</a>. <p>\n";
97         }
98 elsif (!$config{'expert'} || $access{'bootup'} == 2) {
99         # Just tell the user if this action is started at boot time
100         local $boot = 0;
101         if ($ty == 0) {
102                 local @boot = &get_inittab_runlevel();
103                 foreach $s (&action_levels('S', $ac)) {
104                         local ($l, $p) = split(/\s+/, $s);
105                         $boot = 1 if (&indexof($l, @boot) >= 0);
106                         }
107                 if ($boot && $config{'daemons_dir'} &&
108                     &read_env_file("$config{'daemons_dir'}/$ac", \%daemon)) {
109                         $boot = lc($daemon{'ONBOOT'}) eq 'yes' ? 1 : 0;
110                         }
111                 print &ui_hidden("oldboot", $boot);
112                 }
113         if ($access{'bootup'} == 1) {
114                 print &ui_table_row($text{'edit_boot'},
115                         &ui_yesno_radio("boot", $boot || $ty == 2 ? 1 : 0));
116                 }
117         else {
118                 print &ui_table_row($text{'edit_boot'},
119                         $boot || $ty == 2 ? $text{'yes'} : $text{'no'});
120                 }
121
122         # Show if action is currently running
123         if ($hasarg->{'status'} && $config{'status_check'}) {
124                 $r = &action_running($file);
125                 if ($r == 0) {
126                         $status = "<font color=#ff0000>$text{'no'}</font>";
127                         }
128                 elsif ($r == 1) {
129                         $status = $text{'yes'};
130                         }
131                 else {
132                         $status = "<i>$text{'edit_unknown'}</i>";
133                         }
134                 print &ui_table_row($text{'edit_status'}, $status);
135                 }
136         print &ui_table_end();
137         }
138 else {
139         if ($config{'daemons_dir'} && $ac &&
140             &read_env_file("$config{'daemons_dir'}/$ac", \%daemon)) {
141                 # Display onboot flag from daemons file
142                 $boot = lc($daemon{'ONBOOT'}) eq 'yes';
143                 print &ui_table_row($text{'edit_boot'},
144                         &ui_yesno_radio("boot", $boot ? 1 : 0));
145                 }
146         print &ui_table_end();
147
148         # Display which runlevels the action is started/stopped in
149         print &ui_table_start($text{'edit_levels'}, "width=100%", 4);
150         if ($ac) {
151                 foreach $s (&action_levels('S', $ac)) {
152                         @s = split(/\s+/, $s);
153                         $spri{$s[0]} = $s[1];
154                         }
155                 foreach $k (&action_levels('K', $ac)) {
156                         @k = split(/\s+/, $k);
157                         $kpri{$k[0]} = $k[1];
158                         }
159                 }
160         @boot = &get_inittab_runlevel();
161         foreach $rl (&list_runlevels()) {
162                 if (&indexof($rl, @boot) == -1) {
163                         $label = &text('edit_rl', $rl);
164                         }
165                 else {
166                         $label = "<i>".&text('edit_rl', $rl)."</i>";
167                         }
168
169                 $od = $config{'order_digits'};
170                 $msg = &ui_checkbox("S$rl", 1, $text{'edit_startat'},
171                                     defined($spri{$rl}))." ".
172                        &ui_textbox("pri_S$rl", $spri{$rl}, $od)."\n".
173                        &ui_checkbox("K$rl", 1, $text{'edit_stopat'},
174                                     defined($kpri{$rl}))." ".
175                        &ui_textbox("pri_K$rl", $kpri{$rl}, $od);
176                 print &ui_table_row($label, $msg);
177                 }
178         print &ui_table_end();
179         }
180
181 if ($ty != 2) {
182         if ($access{'bootup'} == 1) {
183                 push(@buts, [ undef, $text{'save'} ]);
184                 }
185
186         # Buttons to start and stop
187         $args = join("+", @ARGV);
188         print &ui_hidden("back", "edit_action.cgi?$args");
189         foreach $a (@action_buttons) {
190                 if ($a eq 'start' || $a eq 'stop' || $hasarg->{$a}) {
191                         push(@buts, [ $a, $text{'edit_'.$a.'now'} ]);
192                         }
193                 }
194
195         # Button to delete
196         if ($access{'bootup'} == 1) {
197                 push(@buts, [ "delete", $text{'delete'} ]);
198                 }
199         print &ui_form_end(\@buts);
200         }
201 else {
202         print &ui_form_end([ [ undef, $text{'create'} ] ]);
203         }
204
205 &ui_print_footer("", $text{'index_return'});
206
207