Handle hostnames with upper-case letters
[webmin.git] / init / save_hostconfig_action.cgi
1 #!/usr/local/bin/perl
2 # save_hostconfig_action.cgi
3 #
4 # Creates a new StartupItems directory containing a script and
5 # plist, and adds the appropriate entry into the hostconfig file.
6 #
7 # Written by Michael A. Peters <mpeters@mac.com> for the webmin init 
8 # module for Darwin and OS X.
9
10 require './init-lib.pl';
11 $access{'bootup'} == 1 || &error("You are not allowed to edit the bootup script");
12 &ReadParse();
13
14 #make sure required fields have been filled out
15 if ( $in{'action_name'} eq "" ) {
16         &error("The $text{'edit_hostconfig_actionname'} field may not be left blank");
17         }
18 elsif ( $in{'script_name'} eq "" ) {
19         &error("The $text{'edit_hostconfig_scriptname'} field may not be left blank");
20         }
21 elsif ( $in{'execute'} eq "" ) {
22         &error("The $text{'edit_start'} field may not be left blank");
23         }
24         
25 #check for legality of action name and script name
26 if (not $in{'action_name'}=~ /^[A-Z][A-Z0-9_]*$/ ) {
27         &error("The $text{'edit_hostconfig_actionname'} should contain only upper case letters, numbers, and _ and should start with an upper case letter");
28         }
29 else {
30         #make sure action name is not in use
31         open(LOCAL, $config{'hostconfig'});
32         @temp = <LOCAL>;
33         close(LOCAL);
34         foreach $element (@temp) {
35                 if ($element =~ /^$in{'action_name'}=(.*)$/ ) {
36                         &error("$in{'action_name'} is already in use. Please choose another value for $text{'edit_hostconfig_actionname'}");
37                         }
38                 }
39         }
40 if (not $in{'script_name'}=~ /^[A-Za-z0-9_-]*$/ ) {
41         &error("The $text{'edit_hostconfig_scriptname'} should contain only letters, numbers, _, and -");
42         }
43 else {
44         if ( -d "$config{'darwin_setup'}/$in{'script_name'}" ) {
45         # Perl does know about hfs+ preservation but not sensitivity-
46         # I checked :)
47                 &error("$in{'script_name'} is already in use for another action. Please choose another value for $text{'edit_hostconfig_scriptname'}");
48                 }
49         }
50
51 #assign values  
52 $in{'execute'} =~ s/\r//g;
53 if ( $in{'start'} eq "" ) {
54         $in{'start'} = "Starting $in{'script_name'}";
55         }
56 if ( $in{'stop'} eq "" ) {
57         $in{'stop'} = "Stopping $in{'script_name'}";
58         }
59 if ( $in{'description'} eq "" ) {
60         $in{'description'} = "No description available";
61         }
62         
63 #assign array values
64 if ( $in{'provides'} ne "" ) {
65         @provides_array = split (/:/, $in{'provides'});
66         $provides = "\"$provides_array[0]\"";
67         if ( $provides_array[1] ne "" ) {
68                 shift @provides_array;
69                 foreach $element (@provides_array) {
70                         $provides = "$provides, \"$element\""
71                         }
72                 }
73         }
74 if ( $in{'requires'} ne "" ) {
75         @requires_array = split (/:/, $in{'requires'});
76         $requires = "\"$requires_array[0]\"";
77         if ( $requires_array[1] ne "" ) {
78                 shift @requires_array;
79                 foreach $element (@requires_array) {
80                         $requires = "$requires, \"$element\""
81                         }
82                 }
83         }
84 if ( $in{'uses'} ne "" ) {
85         @uses_array = split (/:/, $in{'uses'});
86         $uses = "\"$uses_array[0]\"";
87         if ( $uses_array[1] ne "" ) {
88                 shift @uses_array;
89                 foreach $element (@uses_array) {
90                         $uses = "$uses, \"$element\""
91                         }
92                 }
93         }
94
95 # make array string
96 $array="";
97 if ( $provides ne "" ) {
98         $array = "\tProvides\t= \($provides\);\n";
99         }
100 if ( $requires ne "" ) {
101         $array = "$array\tRequires\t= \($requires\);\n";
102         }
103 if ( $uses ne "" ) {
104         $array = "$array\tUses\t= \($uses\);\n";
105         }
106         
107 # make plist
108 $plist = "\{\n\tDescription\t= \"$in{'description'}\";\n$array\tOrderPreference\t= \"$in{'order'}\";\n\tMessages =\n\t\{\n\t\tstart\t= \"$in{'start'}\";\n\t\tstop\t=\"$in{'stop'}\";\n\t\};\n\}\n";
109
110 # make startscript
111 $startscript = "#!/bin/sh\n\n. /etc/rc.common\n\nif \[ \"\$\{$in{'action_name'}:=-NO-\}\" = \"-YES-\" \]; then\n\tConsoleMessage \"$in{'start'}\"\n$in{'execute'}\nfi\n";
112
113 # So write the files already!
114 if (not -d "$config{'darwin_setup'}") {
115         mkdir ("$config{'darwin_setup'}", 0755);
116         }
117 # startup dir for this action should not yet exist
118 mkdir ("$config{'darwin_setup'}/$in{'script_name'}", 0755) || &error("Could not create $config{'darwin_setup'}/$in{'script_name'}");
119
120 &open_tempfile(LOCAL, ">$config{'darwin_setup'}/$in{'script_name'}/$config{'plist'}");
121 &print_tempfile(LOCAL, $plist);
122 &close_tempfile(LOCAL);
123 chmod(0644, "$config{'darwin_setup'}/$in{'script_name'}/$config{'plist'}");
124
125 &open_tempfile(LOCAL, ">$config{'darwin_setup'}/$in{'script_name'}/$in{'script_name'}");
126 &print_tempfile(LOCAL, $startscript);
127 &close_tempfile(LOCAL);
128 chmod(0750, "$config{'darwin_setup'}/$in{'script_name'}/$in{'script_name'}");
129
130 &lock_file($config{'hostconfig'});
131 &open_tempfile(LOCAL, ">>$config{'hostconfig'}");
132 &print_tempfile(LOCAL, "$in{'action_name'}=$in{'boot'}\n");
133 &close_tempfile(LOCAL);
134 &unlock_file($config{'hostconfig'});
135 &webmin_log("new_action", undef, undef, \%in);
136 &redirect("edit_hostconfig.cgi?0+$in{'action_name'}");
137
138 #print "Content-type: text/plain", "\n\n";
139 #print "Debug\n\n";
140 #print "$plist";
141 #print "\n\n";
142 #print "$startscript";
143 #print "\n";