Handle hostnames with upper-case letters
[webmin.git] / init / hostconfig-lib.pl
1 #!/usr/local/bin/perl
2 # hostconfig-lib.pl
3
4 # These are functions specific to the hostconfig file
5 # used by darwin
6
7 # written by Michael A. Peters <mpeters@mac.com>
8
9 sub hostconfig_settings
10 {
11 ####
12 #
13 # This subroutine reads the hostconfig file into an array
14 # and outputs a second array containing matched pairs of
15 # the startup action and what the startup action is set to
16 #
17 ####
18 local($conffile, @hconf);
19 $conffile = "$config{'hostconfig'}";
20 open(LOCAL, $conffile);
21 @conf = <LOCAL>;
22 close(LOCAL);
23 @conf = grep /^\w/, @conf;
24 while (<@conf>) {
25         push @hconf, [ split /=/ ];
26         }
27 return @hconf;
28 }
29
30 sub hostconfig_gather
31 {
32 ####
33 #
34 # Gathers information about an action item that is set in the hostconfig
35 # file.
36 #
37 # It takes one arguement- the type of info wanted (description or 
38 # scriptname)
39 #
40 # It outputs a hash where the action item is the key, and what was 
41 # requested is the value.
42 #
43 # Thus, we can use the hash to find out what startup script is 
44 # associated with the MAILSERVER action or what description goes with 
45 # that action.
46 #
47 # Originally I wanted to output an array with two elements where the 
48 # first was one type of hash, and the second element was the second type # of hash, but I couldn't get that to work (array of hashes)
49 #
50 ####
51 local ($hash_type, @startupdir, $plist, @action_to_description, @action_to_script, @sec_action_to_description, @sec_action_to_script, $element, @ls, @script, $a, $action_name, @param, $description);
52
53 my($hash_type) = @_;
54 @startupdir = ();
55 @startupdir = split (/:/, $config{'startup_dirs'});
56 $plist = $config{'plist'};
57 @action_to_description = ();
58 @action_to_script = ();
59 @sec_action_to_description = ();
60 @sec_action_to_script = ();
61
62 foreach $element (@startupdir) {
63         if ( -d "$element" ) {
64                 opendir (LOCAL, $element);
65                 @ls = readdir LOCAL;
66                 closedir LOCAL;
67                 shift @ls; shift @ls;
68                 foreach $a (@ls) {
69                         #we need BOTH an executable and a plist- or its useless.
70                         #executable script has to be in a directory of the same
71                         #name for some reason
72                         if (( -x "$element/$a/$a") && ( -e "$element/$a/$plist")) {
73                                 #Get the startup action associated with script
74                                 open (SCRIPT, "$element/$a/$a");
75                                 @script = <SCRIPT>;
76                                 close SCRIPT;
77                                 @script = grep /:=/, @script;
78                                 #  we are looking at a line in the script that looks like:
79                                 #if [ "${WEBMIN:=-NO-}" = "-YES-" ]; then
80                                 #  and we want to extract the WEBMIN part as the action_name
81                                 if ( $script[0] =~ /\$\{(.*):/ ) {
82                                         $action_name = $1;
83                                         }
84                                 else {
85                                         #shouldn't happen
86                                         $action_name = "";
87                                         }
88                                 open (PLIST, "$element/$a/$plist");
89                                 @param = <PLIST>;
90                                 close PLIST;
91                                 @param = grep /Description[ \t]*=/, @param;
92                                 #  we are looking at a line in the plist that looks like:
93                                 #\t\tDescription\t\t= "Webmin System Administration Daemon";
94                                 #  and we want to extract the contents of the quotes
95                                 if ( $param[0] =~ /\"(.*)\"/ ) {
96                                         $description = $1;
97                                         }
98                                 else {
99                                         $description = "";
100                                         }
101                                 # make the primary hash
102                                 if ( $action_name ne "" ) {
103                                         $action_to_description{$action_name} = "$description";
104                                         $action_to_script{$action_name} = "$element/$a/$a";
105                                         }
106                                 # make the secondary hash
107                                 shift @script;
108                                 if ( $script[0] ne "" ) {
109                                         foreach $secondary (@script) {
110                                                 if ( $secondary =~ /\$\{(.*):/ ) {
111                                                         $action_name = $1;
112                                                         $sec_action_to_description{$action_name} = "$description";
113                                                         $sec_action_to_script{$action_name} = "$element/$a/$a";
114                                                         }
115                                                 }
116                                         }
117                                 } #ends the: if (( -x "$element/$a/$a") && ( -e "$element/$a/$plist")
118                         } #ends the: foreach $a (@ls)
119                 } #ends the: if ( -d "$element" )
120         } #ends the: foreach $element (@startupdir)
121         
122 # now we have two sets of each hash
123 # elements in sec_blah that are not already in blah
124 # need to be integrated into blah
125 #
126 # We have to do this because some scripts use several action_name just 
127 # for that particular script, and sometimes what one action is set to is 
128 # used in how another script acts even though that is not the action for # that script.
129 #
130 # Thus, if a action_item is in the secondary array and is not in the
131 # primary, we know that it is a case where more than one action belongs
132 # to a script- but if its in both, the action probably belongs to the
133 # script in the primary.
134
135 while (($key,$value) = each %action_to_description) {
136         $sec_action_to_description{$key} = "$value";
137         }
138 while (($key,$value) = each %action_to_script) {
139         $sec_action_to_script{$key} = "$value";
140         }
141         
142 if ( $hash_type eq "description" ) {
143         return %sec_action_to_description;
144         }
145 elsif ( $hash_type eq "startscript" ) {
146         return %sec_action_to_script;
147         }
148
149 }
150
151 sub hostconfig_table
152 {
153 ####
154 #
155 # This sub writes a table row in html for index.cgi.
156 # It takes the startup action, setting, and provides
157 # as its arguements- and outputs a string.
158 #
159 ####
160 local($ahref, $setting, $link, $description);
161 my($ahref, $setting, $description) = @_;
162 local @cols;
163 if ($access{'bootup'} == 1) {
164         push(@cols, "<a href=\"edit_hostconfig.cgi?0+$ahref\">$ahref</a>");
165         }
166 else {
167         push(@cols, $ahref);
168         }
169 if ( $setting eq "-NO-" ) {
170         push(@cols, "<FONT color=#ff0000>$setting</font>");
171         }
172 elsif ( $setting ne "" ) {
173         push(@cols, $setting);
174         }
175 else {
176         push(@cols, "");
177         }
178 push(@cols, $description);
179 if ( $ahref ne "" ) {
180         return &ui_columns_row(\@cols);
181         }
182 else {
183         return "<!-- this is annoying- I'll have to track it down.. -->";
184         }
185 }
186
187 sub hostconfig_editaction
188 {
189 ####
190 #
191 # This sub takes either one or two arguements- the first (action name)
192 # is required, the second is the StartupItems script affected by
193 # the setting of the action. 
194 #
195 # If there is no script, the current setting is used as the default in
196 # a text field for editing.
197 #
198 # If there is a script, but the setting is something like automatic in 
199 # the Network script where there isn't an alternative, then radio button # choice between the defined setting and a custom one is offered.
200 #
201 # If, as is the case with most scripts, there is a -NO- and -YES- 
202 # option, then those two choices are offered as a radio button option 
203 # and a text box option just in case a custom answer is needed.
204 #
205 # Returns a string
206 #
207 ####
208 local(@sconf, @sfile, @possible_settings, $current, $setting, $line, $option_selected, $buttons, $pre);
209 my($action_item, $startupfile) = @_;
210
211 # get current setting
212 $line = "$config{'hostconfig'}";
213 open(HCONF, $line);
214 @sconf = <HCONF>;
215 close(HCONF);
216 @sconf = grep /^$action_item=/, @sconf;
217 ($dontcare, $current) = split(/=/, $sconf[0]);
218 if ( $current eq "" ) {
219         $current = "udefined";
220         }
221 #get rid of quotes
222 $current =~ s/\"//g;
223 $current =~ s/\n//;
224
225 @possible_settings = ();
226 $option_selected = "";
227 $buttons = "";
228 $option_selected = "";
229
230 # get possible settings
231 if ( $startupfile ne "" ) {
232         open(LOCAL, $startupfile);
233         @sfile = <LOCAL>;
234         close(LOCAL);
235         #
236         # I really need to write a parser to get
237         # this done- so that I can deal with
238         # case options.
239         #
240         # But this is better than nothing...
241         #
242         @sfile = grep /\{$action_item:=/, @sfile;
243         for $element (@sfile) {
244                 # We are looking at a line that looks like
245                 #if [ "${WEBMIN:=-NO-}" = "-YES-" ]; then
246                 # We want the -NO- and -YES-
247                 if ( $element =~ /\"\$\{$action_item:=(.*)\}\"[ \t]*=[ \t]*\"(.*)\"/ ) {
248                         push @possible_settings, ($1, $2);
249                         }
250                 }
251         # get rid of duplicate entries
252         %unique = map { $_ => 1 } @possible_settings;
253         @possible_settings = keys %unique;
254         } # end of :if ( $startupfile ne "" )
255         
256 if ( $possible_settings[0] eq "" ) {
257         $buttons = "<input type=text name=choice value=\"$current\">";
258         }
259 else {
260         foreach $setting (@possible_settings) {
261                 if ( $setting ne $current ) {
262                         if ( $buttons eq "" ) {
263                                 $pre = "";
264                                 }
265                         else {
266                                 $pre = "$buttons<br>\n";
267                                 }
268                         $buttons = "$pre<input type=radio name=choice value=\"$setting\">$setting";
269                         }
270                 else {
271                         $option_selected = "yes";
272                         if ( $buttons eq "" ) {
273                                 $pre = "";
274                                 }
275                         else {
276                                 $pre = "$buttons<br>\n";
277                                 }
278                         $buttons = "$pre<input type=radio name=choice value=\"$setting\" checked>$setting";
279                         }
280                 } #end foreach
281         # add custom text option
282         if ( $option_selected eq "yes" ) {
283                 $buttons = "$buttons<br>\n<input type=radio name=choice value=custom> <input type=text name=custom size=60 value=\"\">";
284                 }
285         else {
286                 $buttons = "$buttons<br>\n<input type=radio name=choice value=custom checked> <input type=text name=custom size=60 value=\"$current\">";
287                 }
288         }
289 # add hidden value
290 $buttons = "$buttons\n<input type=hidden name=action value=$action_item>\n";
291         
292 return $buttons;
293
294 }
295
296 sub hostconfig_createtext
297 {
298 # simply outputs the text in the create new action table
299 my($text_line,$required_field) = @_;
300 if ( $required_field ne "" ) {
301         $output="<td><font size=-1 color=#ff0000>*</font><b>$text_line</b></td>\n";
302         }
303 else {
304         $output="<td><b>$text_line</b></td>\n";
305         }
306 return $output;
307 }
308 1;