Handle hostnames with upper-case letters
[webmin.git] / init / save_rc.cgi
1 #!/usr/local/bin/perl
2 # Create, update or delete a BSD RC script
3
4 require './init-lib.pl';
5 &ReadParse();
6 $access{'bootup'} || &error($text{'edit_ecannot'});
7 &foreign_require("proc", "proc-lib.pl");
8
9 @rcs = &list_rc_scripts();
10 if (!$in{'new'}) {
11         ($rc) = grep { $_->{'name'} eq $in{'name'} } @rcs;
12         $rc || &error($text{'edit_egone'});
13         }
14
15 if ($in{'delete'}) {
16         # Delete the action script
17         &delete_rc_script($in{'name'});
18         &webmin_log("delete", "action", $in{'name'});
19         &redirect("");
20         }
21 elsif ($in{'start'} || $in{'stop'} || $in{'status'}) {
22         # Run now
23         $mode = $in{'start'} ? "start" :
24                 $in{'stop'} ? "stop" : "status";
25         &ui_print_header(undef, $text{'ss_'.$mode}, "");
26         print &text('ss_doing'.$mode, "<tt>$in{'name'}</tt>"),"<br>\n";
27         $cmd = "$rc->{'file'} $mode";
28         print "<pre>";
29         &foreign_call("proc", "safe_process_exec_logged", $cmd, 0, 0, STDOUT, undef, 1);
30         print "</pre>\n";
31         &webmin_log($mode, 'action', $in{'name'});
32         &ui_print_footer("edit_rc.cgi?name=".&urlize($in{'name'}),
33                          $text{'edit_return'});
34         }
35 else {
36         # Validate inputs
37         if ($in{'new'}) {
38                 $in{'name'} =~ /^[A-z0-9\_\-\.]+$/ ||
39                         &error($text{'save_ename'});
40                 ($clash) = grep { $_->{'name'} eq $in{'name'} } @rcs;
41                 $clash && &error($text{'save_eclash'});
42                 $in{'start_cmd'} =~ /\S/ || &error($text{'save_estartcmd'});
43
44                 @dirs = split(/\s+/, $config{'rc_dir'});
45                 $file = $dirs[$#dir]."/".$in{'name'};
46                 $data =  "#!/bin/sh\n";
47                 $data .= "#\n";
48                 $data .= "# PROVIDE: $in{'name'}\n";
49                 $data .= "# REQUIRE: LOGIN\n";
50                 $data .= "\n";
51                 $data .= ". /etc/rc.subr\n";
52                 $data .= "\n";
53                 $data .= "name=$in{'name'}\n";
54                 $data .= "rcvar=`set_rcvar`\n";
55                 $data .= "start_cmd=\"$in{'start_cmd'}\"\n";
56                 if ($in{'stop_cmd'}) {
57                         $data .= "stop_cmd=\"$in{'stop_cmd'}\"\n";
58                         }
59                 if ($in{'status_cmd'}) {
60                         $data .= "status_cmd=\"$in{'status_cmd'}\"\n";
61                         }
62                 $data .= "\n";
63                 $data .= "load_rc_config \${name}\n";
64                 $data .= "run_rc_command \"\$1\"\n";
65                 }
66         else {
67                 $data = $in{'script'};
68                 $data =~ s/\r//g;
69                 $data =~ /\S/ || &error($text{'save_escript'});
70                 $file = $rc->{'file'};
71                 }
72
73         # Write out the file
74         &open_lock_tempfile(SCRIPT, ">$file");
75         &print_tempfile(SCRIPT, $data);
76         &close_tempfile(SCRIPT);
77         &set_ownership_permissions(undef, undef, 0755, $file);
78
79         if ($rc->{'enabled'} != 2) {
80                 # Enable or disable
81                 &lock_rc_files();
82                 if ($in{'enabled'}) {
83                         &enable_rc_script($in{'name'});
84                         }
85                 else {
86                         &disable_rc_script($in{'name'});
87                         }
88                 &unlock_rc_files();
89                 }
90
91         &webmin_log($in{'new'} ? "create" : "modify", "action", $in{'name'});
92         &redirect("");
93         }
94