Handle hostnames with upper-case letters
[webmin.git] / dhcpd / edit_subnet.cgi
1 #!/usr/local/bin/perl
2 # edit_subnet.cgi
3 # Edit or create a subnet
4
5 require './dhcpd-lib.pl';
6 require './params-lib.pl';
7 &ReadParse();
8 $conf = &get_config();
9 ($par, $sub) = &get_branch('sub');
10 $mems = $par->{'members'};
11 $sconf = $sub->{'members'};
12
13 # check acls
14 %access = &get_module_acl();
15 &error_setup($text{'eacl_aviol'});
16 if ($in{'new'}) {
17         &error("$text{'eacl_np'} $text{'eacl_pis'}")
18                 unless &can('c', \%access, $sub) && &can('rw', \%access, $par);
19         }
20 else {
21         &error("$text{'eacl_np'} $text{'eacl_pss'}")
22                 if !&can('r', \%access, $sub);
23         }
24
25 # display
26 if ($in{'sidx'} ne "") {
27         local $s = $conf->[$in{'sidx'}];
28         $desc = &text('ehost_shared', $s->{'values'}->[0]);
29         }
30 &ui_print_header($desc, $in{'new'} ? $text{'esub_crheader'} : $text{'esub_edheader'}, "");
31
32 foreach $s (&find("shared-network", $conf)) {
33         if ($in{'sidx'} eq $s->{'index'}) {
34                 $s_parent = $s;
35                 }
36         }
37
38 print "<form action=save_subnet.cgi method=post>\n";
39 print "<input name=ret value=\"$in{'ret'}\" type=hidden>\n";
40 print "<table border width=100%>\n";
41 print "<tr $tb> <td><b>$text{'esub_tabhdr'}</b></td> </tr>\n";
42 print "<tr $cb> <td><table width=100%>\n";
43
44 print "<tr> <td><b>$text{'esub_desc'}</b></td>\n";
45 printf "<td colspan=3><input name=desc size=60 value='%s'></td> </tr>\n",
46         $sub ? &html_escape($sub->{'comment'}) : "";
47
48 print "<tr> <td><b>$text{'esub_naddr'}</b></td>\n";
49 printf "<td><input name=network size=25 value=\"%s\"></td>\n",
50         $sub ? $sub->{'values'}->[0] : "";
51
52 print "<td><b>$text{'esub_nmask'}</b></td>\n";
53 printf "<td><input name=netmask size=15 value=\"%s\"></td> </tr>\n",
54         $sub ? $sub->{'values'}->[2] : "";
55
56 @range = $sub ? &find("range", $sub->{'members'}) : ();
57 print "<tr> <td valign=top><b>$text{'esub_arange'}</b></td> <td colspan=3>\n";
58 for($i=0; $i<=@range; $i++) {
59         $r = $range[$i];
60         local $dyn = ($r->{'values'}->[0] eq "dynamic-bootp");
61         printf "<input name=range_low_$i size=15 value=\"%s\"> - \n",
62                 $r->{'values'}->[$dyn];
63         printf "<input name=range_hi_$i size=15 value=\"%s\">&nbsp;\n",
64                 $r->{'values'}->[$dyn+1];
65         printf "<input type=checkbox name=range_dyn_$i value=1 %s>\n",
66                 $dyn ? "checked" : "";
67         print "$text{'esub_dbooptpq'}<br>\n";
68         }
69 print "</td> </tr>\n";
70
71 if (!defined($in{'ret'})) {
72         print "<tr> <td><b>$text{'esub_shnet'}</b></td>\n";
73         print "<td><select name=parent>\n";
74         printf "<option value=\"\" '%s'>&lt;%s&gt;\n",
75                 $s_parent ? "" : "checked", $text{'esub_none'};
76         foreach $s (&find("shared-network", $conf)) {
77                 printf "<option value=\"%s\" %s>%s\n",
78                         $s->{'index'},
79                         $s eq $s_parent ? "selected" : "",
80                         $s->{'values'}->[0]
81                                 if &can('rw', \%access, $s);
82                 }
83         print "</select></td>\n";
84         }
85 else {
86         print "<input name=parent type=hidden value=$s_parent->{'index'}>\n";
87         print "<tr> <td>&nbsp;</td> <td>&nbsp;</td>\n";
88         }
89
90 &display_params($sconf, "subnet");
91
92 foreach $h (&find("host", $mems)) {
93         push(@host, $h);
94 # if &can('r', \%access, $h);
95         }
96 foreach $g (&find("group", $mems)) {
97         push(@group, $g);
98 # if &can('r', \%access, $g);
99         }
100 foreach $s (&find("subnet", $mems)) {
101         foreach $h (&find("host", $s->{'members'})) {
102                 push(@host, $h);
103 # if &can('r', \%access, $h);
104                 $insubn{$h} = $s->{'index'};
105                 }
106         foreach $g (&find("group", $s->{'members'})) {
107                 push(@group, $g);
108 # if &can('r', \%access, $g);
109                 $insubn{$g} = $s->{'index'};
110                 }
111         }
112 @host = sort { $a->{'values'}->[0] cmp $b->{'values'}->[0] } @host;
113 # @group = sort { @{$a->{'members'}} <=> @{$b->{'members'}} } @group;
114
115 print "<tr> <td valign=top><b>$text{'esub_hosts'}</b></td>\n";
116 print "<td><select name=hosts size=3 multiple>\n";
117 foreach $h (@host) {
118         next if !&can('r', \%access, $h);
119         printf "<option value=\"%s,%s\" %s>%s\n",
120                 $h->{'index'}, $insubn{$h},
121                 (!$in{'new'}) && $insubn{$h} eq $sub->{'index'} ? "selected" : "",
122                 $h->{'values'}->[0];
123         }
124 print "</select></td>\n";
125
126 print "<td valign=top><b>$text{'esub_groups'}</b></td>\n";
127 print "<td><select name=groups size=3 multiple>\n";
128 foreach $g (@group) {
129         local $gm = 0;
130         next if !&can('r', \%access, $g);
131         foreach $h (@{$g->{'members'}}) {
132                 if ($h->{'name'} eq "host") { $gm++; }
133                 }
134         printf "<option value=\"%s,%s\" %s>%s\n",
135                 $g->{'index'}, $insubn{$g},
136                 (!$in{'new'}) && $insubn{$g} eq $sub->{'index'} ? "selected" : "",
137                 &group_name($gm, $g);
138         }
139 print "</select></td>\n";
140
141 if (!$in{'new'}) {
142         # inaccessible hosts in this subnet
143         foreach $h (@host) {
144                 if (!&can('r', \%access, $h) && $insubn{$h} eq $sub->{'index'}) {
145                         print "<input name=hosts value=\"$h->{'index'},$sub->{'index'}\" type=hidden>\n";
146                         }
147                 }
148         # inaccessible groups in this subnet
149         foreach $g (@group) {
150                 if (!&can('r', \%access, $g) && $insubn{$g} eq $sub->{'index'}) {
151                         print "<input name=groups value=\"$g->{'index'},$sub->{'index'}\" type=hidden>\n";
152                         }
153                 }
154         }
155
156 print "</table></td></tr></table>\n";
157 print "<input type=hidden name=sidx value=\"$in{'sidx'}\">\n";
158 if (!$in{'new'}) {
159         # Show buttons for existing subnet
160         print "<input type=hidden name=idx value=\"$in{'idx'}\">\n";
161         print "<table width=100%><tr>\n";
162         print "<td><input type=submit value=\"$text{'save'}\"></td>\n"
163                 if &can('rw', \%access, $sub);
164         print "<td align=center><input type=submit name=options value=\"", 
165                   &can('rw', \%access, $sub) ? $text{'butt_eco'} : $text{'butt_vco'},
166                   "\"></td>\n";
167         if ($access{'r_leases'}) {
168                 print "<td align=center><input type=submit name=leases ",
169                       "value=\"$text{'butt_leases'}\"></td>\n";
170                 }
171         print "<td align=right><input type=submit name=delete ",
172               "value=\"$text{'delete'}\"></td>\n" if &can('rw', \%access, $sub, 1);
173         print "</tr></table>\n";
174         print "<a href=\"edit_host.cgi?new=1&sidx=$in{'sidx'}&uidx=$in{'idx'}"
175                 ."&ret=subnet\">$text{'index_addhst'}</a>&nbsp;&nbsp;\n"
176                         if &can('rw', \%access, $sub);
177         print "<a href=\"edit_group.cgi?new=1&sidx=$in{'sidx'}&uidx=$in{'idx'}"
178                 ."&ret=subnet\">$text{'index_addhstg'}</a><p>\n"
179                         if &can('rw', \%access, $sub);
180         }
181 else {
182         # Show create button for new subnet
183         print "<input type=hidden name=new value=1>\n";
184         print "<input type=submit value=\"$text{'create'}\">\n";
185         }
186
187 if ($config{'dhcpd_version'} >= 3 && !$in{'new'}) {
188         # Display address pools
189         print &ui_hr();
190         print &ui_subheading($text{'esub_pools'});
191         local $pn = 1;
192         foreach $p (&find('pool', $sconf)) {
193                 push(@links, "edit_pool.cgi?uidx=$in{'idx'}&sidx=$in{'sidx'}&idx=$p->{'index'}");
194                 push(@titles, &text('esub_pool', $pn));
195                 push(@icons, "images/pool.gif");
196                 $pn++;
197                 }
198         if ($pn == 1) {
199                 print "$text{'esub_poolnone'}<p>\n";
200                 }
201         else {
202                 &icons_table(\@links, \@titles, \@icons, 5);
203                 }
204         print "<a href='edit_pool.cgi?uidx=$in{'idx'}&sidx=$in{'sidx'}&new=1'>",
205               "$text{'esub_pooladd'}</a><br>\n";
206         }
207
208 print "</form>\n";
209 if ($in{'ret'} eq "shared") {
210         &ui_print_footer("edit_shared.cgi?idx=$in{'sidx'}", $text{'esub_retshar'});
211         }
212 else {
213         &ui_print_footer("", $text{'esub_return'});
214         }
215