Handle hostnames with upper-case letters
[webmin.git] / dhcpd / edit_host.cgi
1 #!/usr/local/bin/perl
2 # edit_host.cgi
3 # Edit or create a host
4
5 require './dhcpd-lib.pl';
6 require './params-lib.pl';
7 &ReadParse();
8 $conf = &get_config();
9 ($par, $host) = &get_branch('hst');
10 $hconf = $host->{'members'};
11
12 # check acls
13 %access = &get_module_acl();
14 &error_setup($text{'eacl_aviol'});
15 if ($in{'new'}  ) {
16         &error("$text{'eacl_np'} $text{'eacl_pih'}")
17                 unless &can('c', \%access, $host) && &can('rw', \%access, $par);
18         }
19 else {
20         &error("$text{'eacl_np'} $text{'eacl_psh'}")
21                 if !&can('r', \%access, $host);
22         }
23
24 # display
25 if ($in{'uidx'} ne '') {
26         local $s = $in{'sidx'} eq '' ? $conf->[$in{'uidx'}] :
27                    $conf->[$in{'sidx'}]->{'members'}->[$in{'uidx'}];
28         $desc = &text('ehost_subnet', $s->{'values'}->[0], $s->{'values'}->[2]);
29         }
30 elsif ($in{'sidx'} ne '') {
31         local $s = $conf->[$in{'sidx'}];
32         $desc = &text('ehost_shared', $s->{'values'}->[0]);
33         }
34 &ui_print_header($desc, $in{'new'} ? $text{'ehost_crheader'} : $text{'ehost_eheader'}, "");
35
36 @shar = &find("shared-network", $conf);
37 @subn = &find("subnet", $conf);
38 @group = &find("group", $conf);
39 $iu = 0; $is = 0; $ig = 0;
40 foreach $g (@group) {
41         $gm = 0;
42         foreach $h (&find("host", $g->{'members'})) { $gm++; };
43         push(@group_desc, &group_name($gm, $g));
44         $group_desc[$#group_desc-1] =~ s/ / /g;
45         $script3 .= "parent.options[$ig] = "
46                 ."new Option(\"$group_desc[$ig]\", $ig)\n"
47                 ."parent.options[$ig].value = "
48                 ."new String(\"$g->{'index'}\")\n"
49                         if &can('rw', \%access, $g);
50         if ($in{'sidx'} eq '' && $in{'uidx'} eq '' && $in{'gidx'} eq $g->{'index'}) {
51                 $sel_parent = $ig;
52                 $currpar = "$g->{'index'}";
53                 }
54         $ig ++;
55         }
56 foreach $u (@subn) {
57         push(@subn_desc, $u->{'values'}->[0]);
58         $script2 .= "parent.options[$iu] = "
59                 ."new Option(\"$subn_desc[$iu]\", $iu)\n"
60                 ."parent.options[$iu].value = "
61                 ."new String(\"$u->{'index'}\")\n"
62                         if &can('rw', \%access, $u);
63         if ($in{'sidx'} eq '' && $in{'uidx'} eq $u->{'index'} && $in{'gidx'} eq '') {
64                 $sel_parent = $iu;
65                 $currpar = "$u->{'index'}";
66                 }
67         foreach $g (&find("group", $u->{'members'})) {
68                 push(@group, $g);
69                 $gm = 0;
70                 foreach $h (&find("host", $g->{'members'})) { $gm ++; };
71                 push(@group_desc, &group_name($gm, $g).
72                                   " $text{'ehost_in'} $u->{'values'}->[0]");
73                 $group_desc[$#group_desc-1] =~ s/ / /g;
74                 $subnet{$g} = $u->{'index'};
75                 $script3 .= "parent.options[$ig] = "
76                         ."new Option(\"$group_desc[$ig]\", $ig)\n"
77                         ."parent.options[$ig].value = "
78                         ."new String(\"$u->{'index'},$g->{'index'}\")\n"
79                                 if &can('rw', \%access, $g);
80                 if ($in{'sidx'} eq '' && $in{'uidx'} eq $u->{'index'} && $in{'gidx'} eq $g->{'index'}) {
81                         $sel_parent = $ig;
82                         $currpar = "$u->{'index'},$g->{'index'}";
83                         }
84                 $ig ++;
85                 }
86         $iu ++;
87         }
88 foreach $s (@shar) {
89         push(@shar_desc, $s->{'values'}->[0]);
90         $script1 .= "parent.options[$is] = "
91                 ."new Option(\"$shar_desc[$is]\", $is)\n"
92                 ."parent.options[$is].value = "
93                 ."new String(\"$s->{'index'}\")\n"
94                         if &can('rw', \%access, $s);
95         if ($in{'sidx'} eq $s->{'index'} && $in{'uidx'} eq '' && $in{'gidx'} eq '') {
96                 $sel_parent = $is;
97                 $currpar = "$s->{'index'}";
98                 }
99         foreach $g (&find("group", $s->{'members'})) {
100                 push(@group, $g);
101                 $gm = 0;
102                 foreach $h (&find("host", $g->{'members'})) { $gm ++; };
103                 push(@group_desc, &group_name($gm, $g).
104                                   " $text{'ehost_in'} $s->{'values'}->[0]");
105                 $group_desc[$#group_desc-1] =~ s/ / /g;
106                 $shared{$g} = $s->{'index'};
107                 $script3 .= "parent.options[$ig] = "
108                         ."new Option(\"$group_desc[$ig]\", $ig)\n"
109                         ."parent.options[$ig].value = "
110                         ."new String(\"$s->{'index'},$g->{'index'}\")\n"
111                                 if &can('rw', \%access, $g);
112                 if ($in{'sidx'} eq $s->{'index'} && $in{'uidx'} eq '' && $in{'gidx'} eq $g->{'index'}) {
113                         $sel_parent = $ig;
114                         $currpar = "$s->{'index'},$g->{'index'}";
115                         }
116                 $ig ++;
117                 }
118         foreach $u (&find("subnet", $s->{'members'})) {
119                 push(@subn, $u);
120                 push(@subn_desc, "$u->{'values'}->[0] $text{'ehost_in'} $s->{'values'}->[0]");
121                 $shared{$u} = $s->{'index'};
122                 $script2 .= "parent.options[$iu] = "
123                         ."new Option(\"$subn_desc[$iu]\", $iu)\n"
124                         ."parent.options[$iu].value = "
125                         ."new String(\"$s->{'index'},$u->{'index'}\")\n"
126                                 if &can('rw', \%access, $u);
127                 if ($in{'sidx'} eq $s->{'index'} && $in{'uidx'} eq $u->{'index'} && $in{'gidx'} eq '') {
128                         $sel_parent = $iu;
129                         $currpar = "$s->{'index'},$u->{'index'}";
130                         }
131                 foreach $g (&find("group", $u->{'members'})) {
132                         push(@group, $g);
133                         $gm = 0;
134                         foreach $h (&find("host", $g->{'members'})) { $gm ++; };
135                         push(@group_desc, &group_name($gm, $g).
136                           " $text{'ehost_in'} $u->{'values'}->[0] ".
137                           "$text{'ehost_in'} $s->{'values'}->[0]");
138                         $group_desc[$#group_desc-1] =~ s/ / /g;
139                         $shared{$g} = $s->{'index'};
140                         $subnet{$g} = $u->{'index'};
141                         $script3 .= "parent.options[$ig] = "
142                                 ."new Option(\"$group_desc[$ig]\", $ig)\n"
143                                 ."parent.options[$ig].value = "
144                                 ."new String(\"$s->{'index'},$u->{'index'},$g->{'index'}\")\n"
145                                         if &can('rw', \%access, $g);
146                         if ($in{'sidx'} eq $s->{'index'} && $in{'uidx'} eq $u->{'index'} && $in{'gidx'} eq $g->{'index'}) {
147                                 $sel_parent = $ig;
148                                 $currpar = "$s->{'index'},$u->{'index'},$g->{'index'}";
149                                 }
150                         $ig ++;
151                         }
152                 $iu ++;
153                 }
154         $is ++;
155         }
156
157 print "<form action=save_host.cgi method=post>\n";
158 print "<input name=ret value=\"$in{'ret'}\" type=hidden>\n";
159 print "<table border width=100%>\n";
160 print "<tr $tb> <td><b>$text{'ehost_tabhdr'}</b></td> </tr>\n";
161 print "<tr $cb> <td><table width=100%>\n";
162
163 print "<tr> <td><b>$text{'ehost_desc'}</b></td>\n";
164 printf "<td colspan=3><input name=desc size=60 value='%s'></td> </tr>\n",
165         &html_escape($host->{'comment'});
166
167 print "<tr> <td><b>$text{'ehost_hname'}</b></td>\n";
168 printf "<td><input name=name size=20 value=\"%s\"></td>\n",
169         $host ? $host->{'values'}->[0] : "";
170 $assign = $in{'gidx'} ne "" ? "3" :
171         $in{'uidx'} ne "" ? "2" :
172         $in{'sidx'} ne "" ? "1" : "0";
173 if (!defined($in{'ret'})) {
174         local @labels = ( $text{'ehost_toplevel'}, $text{'ehost_inshared'},
175                           $text{'ehost_insubnet'}, $text{'ehost_ingroup'} );
176         print "<td colspan=2 rowspan=2><table><tr>";
177         print "<td colspan=2>$text{'ehost_nojavascr'}</td></tr>\n<tr>" if ($in{'assign'});
178         print "<td valign=top><b>$text{'ehost_assign'}</b><br>\n";
179         if ($in{'assign'}) {
180                 $assign = $in{'assign'};
181                 print "$labels[$assign]</td>\n";
182                 print "<input name=assign type=hidden value=$assign>\n";
183                 print "<input name=jsquirk type=hidden value=1>\n";
184                 }
185         else {
186                 print "<select name=assign onChange='setparent(0)'>\n";
187                 for ($i = 0; $i <= 3; $i++) {
188                         printf "<option value=$i %s>%s\n",
189                                 $assign == $i ? "selected" : "",
190                                 $labels[$i];
191                         }
192                 print "</select></td>\n";
193                 }
194         print "<td><select name=parent size=5 width=120>\n";
195         if ($assign == 3) {
196                 $ig = 0;
197                 foreach $g (@group) {
198                         printf "<option value=\"%s\" %s>%s\n",
199                                 (defined($shared{$g}) ? "$shared{$g}," : "").
200                                 (defined($subnet{$g}) ? "$subnet{$g}," : "").
201                                 $g->{'index'},
202                                 $ig == $sel_parent ? "selected" : "",
203                                 $group_desc[$ig]
204                                         if &can('rw', \%access, $g);
205                         $ig ++;
206                         }
207                 }
208         elsif ($assign == 2) {
209                 $iu = 0;
210                 foreach $u (@subn) {
211                         printf "<option value=\"%s\" %s>%s\n",
212                                 defined($shared{$u}) ? "$shared{$u},$u->{'index'}" : $u->{'index'},
213                                 $iu == $sel_parent ? "selected" : "",
214                                 $subn_desc[$iu]
215                                         if &can('rw', \%access, $u);
216                         $iu ++;
217                         }
218                 }
219         elsif ($assign == 1) {
220                 $is = 0;
221                 foreach $s (@shar) {
222                         printf "<option value=\"%s\" %s>%s\n",
223                                 $s->{'index'},
224                                 $is == $sel_parent ? "selected" : "",
225                                 $shar_desc[$is]
226                                         if &can('rw', \%access, $s);
227                         $is ++;
228                         }
229                 }
230         print "</select></td></tr>\n";
231         print "</table></td>\n";
232
233         print "</tr> <tr>\n";
234         }
235 else {
236         print "<input name=assign type=hidden value=$assign>\n";
237         print "<input name=parent type=hidden value=$currpar>\n";
238         }
239
240 $hard = $hconf ? &find("hardware", $hconf) : undef;
241 print "<td><b>$text{'ehost_hwaddr'}</b></td>\n";
242 print "<td nowrap><select name=hardware_type>\n";
243 printf "<option %s>ethernet\n",
244         $hard && $hard->{'values'}->[0] eq "ethernet" ? "selected" : "";
245 printf "<option %s>token-ring\n",
246         $hard && $hard->{'values'}->[0] eq "token-ring" ? "selected" : "";
247 printf "<option %s>fddi\n",
248         $hard && $hard->{'values'}->[0] eq "fddi" ? "selected" : "";
249 print "</select>";
250 printf "<input name=hardware size=18 value=\"%s\"></td> </tr>\n",
251         $hard ? $hard->{'values'}->[1] : "";
252
253 $fixed = $host ? &find("fixed-address", $hconf) : "";
254 print "<tr> <td><b>$text{'ehost_fixedip'}</b></td> <td>\n";
255 printf "<input name=fixed-address size=20 value=\"%s\"></td>\n",
256         $fixed ? join(" ", grep { $_ ne "," } @{$fixed->{'values'}}) : "";
257
258 &display_params($hconf, "host");
259
260 print "</table></td></tr></table>\n";
261 print "<input type=hidden name=gidx value=\"$in{'gidx'}\">\n";
262 print "<input type=hidden name=uidx value=\"$in{'uidx'}\">\n";
263 print "<input type=hidden name=sidx value=\"$in{'sidx'}\">\n";
264 if (!$in{'new'}) {
265         print "<input type=hidden name=idx value=\"$in{'idx'}\">\n";
266         print "<table width=100%><tr>\n";
267         print "<td><input type=submit value=\"$text{'save'}\"></td>\n"
268                 if &can('rw', \%access, $host);
269         print "<td align=center><input type=submit name=options value=\"",
270           &can('rw', \%access, $host) ? $text{'butt_eco'} : $text{'butt_vco'},
271               "\"></td>\n";               
272         print "<td align=right><input type=submit name=delete ",
273               "value=\"$text{'delete'}\"></td>\n" 
274                   if &can('rw', \%access, $host, 1);
275         print "</tr></table>\n";
276         }
277 else {
278         print "<input type=hidden name=new value=1>\n";
279         print "<input type=submit value=\"$text{'butt_create'}\">\n";
280         }
281 print "</form>\n";
282 print &script_fn() if (!defined($in{'ret'}));
283 if ($in{'ret'} eq "group") {
284         &ui_print_footer("edit_group.cgi?sidx=$in{'sidx'}&uidx=$in{'uidx'}&idx=$in{'gidx'}",
285                 $text{'ehost_retgroup'});
286         }
287 elsif ($in{'ret'} eq "subnet") {
288         &ui_print_footer("edit_subnet.cgi?sidx=$in{'sidx'}&idx=$in{'uidx'}",
289                 $text{'ehost_retsubn'});
290         }
291 elsif ($in{'ret'} eq "shared") {
292         &ui_print_footer("edit_shared.cgi?idx=$in{'sidx'}", $text{'ehost_retshar'});
293         }
294 else {
295         &ui_print_footer($ret, $text{'ehost_return'});
296         }
297
298 sub script_fn
299 {
300 return <<EOF
301 <script>
302 function setparent(sel)
303 {
304 var idx = document.forms[0].assign.selectedIndex;
305 var v = document.forms[0].assign.options[idx].value;
306 var vv = v.split(";");
307 var parent = document.forms[0].parent;
308 parent.length = 0;
309
310 if (v==1) {
311 $script1
312 }
313 if (v==2) {
314 $script2
315 }
316 if (v==3) {
317 $script3
318 }
319 if (parent.length > 0) {
320         parent.options[sel].selected = true;
321         }
322 }
323 setparent($sel_parent);
324 </script>
325 EOF
326
327 }