2 # save_hostconfig_action.cgi
4 # Creates a new StartupItems directory containing a script and
5 # plist, and adds the appropriate entry into the hostconfig file.
7 # Written by Michael A. Peters <mpeters@mac.com> for the webmin init
8 # module for Darwin and OS X.
10 require './init-lib.pl';
11 $access{'bootup'} == 1 || &error("You are not allowed to edit the bootup script");
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");
18 elsif ( $in{'script_name'} eq "" ) {
19 &error("The $text{'edit_hostconfig_scriptname'} field may not be left blank");
21 elsif ( $in{'execute'} eq "" ) {
22 &error("The $text{'edit_start'} field may not be left blank");
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");
30 #make sure action name is not in use
31 open(LOCAL, $config{'hostconfig'});
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'}");
40 if (not $in{'script_name'}=~ /^[A-Za-z0-9_-]*$/ ) {
41 &error("The $text{'edit_hostconfig_scriptname'} should contain only letters, numbers, _, and -");
44 if ( -d "$config{'darwin_setup'}/$in{'script_name'}" ) {
45 # Perl does know about hfs+ preservation but not sensitivity-
47 &error("$in{'script_name'} is already in use for another action. Please choose another value for $text{'edit_hostconfig_scriptname'}");
52 $in{'execute'} =~ s/\r//g;
53 if ( $in{'start'} eq "" ) {
54 $in{'start'} = "Starting $in{'script_name'}";
56 if ( $in{'stop'} eq "" ) {
57 $in{'stop'} = "Stopping $in{'script_name'}";
59 if ( $in{'description'} eq "" ) {
60 $in{'description'} = "No description available";
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\""
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\""
84 if ( $in{'uses'} ne "" ) {
85 @uses_array = split (/:/, $in{'uses'});
86 $uses = "\"$uses_array[0]\"";
87 if ( $uses_array[1] ne "" ) {
89 foreach $element (@uses_array) {
90 $uses = "$uses, \"$element\""
97 if ( $provides ne "" ) {
98 $array = "\tProvides\t= \($provides\);\n";
100 if ( $requires ne "" ) {
101 $array = "$array\tRequires\t= \($requires\);\n";
104 $array = "$array\tUses\t= \($uses\);\n";
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";
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";
113 # So write the files already!
114 if (not -d "$config{'darwin_setup'}") {
115 mkdir ("$config{'darwin_setup'}", 0755);
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'}");
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'}");
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'}");
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'}");
138 #print "Content-type: text/plain", "\n\n";
142 #print "$startscript";