Handle hostnames with upper-case letters
[webmin.git] / net / edit_bifc.cgi
1 #!/usr/local/bin/perl
2 # edit_bifc.cgi
3 # Edit or create a bootup interface
4
5 require './net-lib.pl';
6 &ReadParse();
7 !$in{'new'} || &can_create_iface() || &error($text{'ifcs_ecannot'});
8 @boot = &boot_interfaces();
9
10 # Show page title and get interface
11 if ($in{'new'} && $in{'bond'}) {
12         # New bonding interface
13         &ui_print_header(undef, $text{'bonding_create'}, "");
14         }
15 elsif ($in{'new'} && $in{'vlan'}) {
16         # New VLAN
17         &ui_print_header(undef, $text{'vlan_create'}, "");
18         }
19 elsif ($in{'new'} && $in{'bridge'}) {
20         # New Bridge
21         &ui_print_header(undef, $text{'bridge_create'}, "");
22         $bmax = -1;
23         foreach $b (@boot) {
24                 if ($b->{'fullname'} =~ /^br(\d+)$/) {
25                         $bmax = $1;
26                         }
27                 }
28         }
29 elsif ($in{'new'}) {
30         # New real or virtual interface
31         &ui_print_header(undef, $text{'bifc_create'}, "");
32         if ($in{'virtual'}) {
33                 # Pick a virtual number
34                 $vmax = int($net::min_virtual_number);
35                 foreach my $e (@boot) {
36                         $vmax = $e->{'virtual'}
37                                 if ($e->{'name'} eq $in{'virtual'} &&
38                                     $e->{'virtual'} > $vmax);
39                         }
40                 }
41         }
42 else {
43         # Editing existing
44         $b = $boot[$in{'idx'}];
45         &can_iface($b) || &error($text{'ifcs_ecannot_this'});
46         &ui_print_header(undef, $text{'bifc_edit'}, "");
47         if (!$b->{'dhcp'} && !$b->{'bootp'} && !$b->{'broadcast'}) {
48                 # Fill in broadcast if missing
49                 $b->{'broadcast'} = &compute_broadcast(
50                         $b->{'address'}, $b->{'netmask'});
51                 }
52         }
53
54 # Start of the form
55 print &ui_form_start("save_bifc.cgi");
56 print &ui_hidden("new", $in{'new'});
57 print &ui_hidden("idx", $in{'idx'});
58 print &ui_hidden("vlan", $in{'vlan'});
59 print &ui_hidden("bond", $in{'bond'});
60 print &ui_hidden("bridge", $in{'bridge'});
61 print &ui_table_start($in{'virtual'} || $b && $b->{'virtual'} ne "" ?
62                         $text{'bifc_desc2'} : $text{'bifc_desc1'}, undef, 2);
63
64 # Comment, if allowed
65 if (defined(&can_iface_desc) && &can_iface_desc($b)) {
66         print &ui_table_row($text{'ifcs_desc'},
67                 &ui_textbox("desc", $b ? $b->{'desc'} : undef, 60), 3);
68         }
69
70 # Interface name
71 if ($in{'new'} && $in{'virtual'}) {
72         # New virtual interface
73         $namefield = $in{'virtual'}.":".
74                      &ui_textbox("virtual", $vmax+1, 3).
75                      &ui_hidden("name", $in{'virtual'});
76         }
77 elsif ($in{'new'}) {
78         # New real interface
79         if ($in{'vlan'} == 1) {
80                 $namefield = "auto".&ui_hidden("name", "auto");
81                 }
82         elsif ($in{'bridge'}) {
83                 $namefield = "br ".&ui_textbox("name", ($bmax+1), 3);
84                 }
85         else {
86                 $namefield = &ui_textbox("name", undef, 6);
87                 }
88         }
89 else {
90         # Existing interface
91         $namefield = "<tt>$b->{'fullname'}</tt>";
92         }
93 print &ui_table_row($text{'ifcs_name'}, $namefield);
94
95 # Activate at boot?
96 if (&can_edit("up", $b) && $access{'up'}) {
97         $upfield = &ui_yesno_radio("up", !$b || $b->{'up'});
98         }
99 else {
100         $upfield = !$b ? $text{'yes'} :
101                    $b->{'up'} ? $text{'yes'} : $text{'no'};
102         }
103 print &ui_table_row($text{'bifc_act'}, $upfield);
104
105 # IP address source. This can either be DHCP, BootP or a fixed IP,
106 # netmask and broadcast
107 $virtual = (!$b && $in{'virtual'}) || ($b && $b->{'virtual'} ne "");
108 $dhcp = &can_edit("dhcp") && !$virtual;
109 $bootp = &can_edit("bootp") && !$virtual;
110 if (defined(&supports_no_address) && &supports_no_address()) {
111         # Having no address is allowed
112         $canno = 1;
113         }
114 elsif ($b && !$b->{'address'} && !$b->{'dhcp'} && !$b->{'bootp'}) {
115         # Has no address
116         $canno = 1;
117         }
118 @opts = ( );
119 if ($canno) {
120         push(@opts, [ "none", $text{'ifcs_noaddress'} ]);
121         }
122 if ($dhcp) {
123         push(@opts, [ "dhcp", $text{'ifcs_dhcp'} ]);
124         }
125 if ($bootp) {
126         push(@opts, [ "bootp", $text{'ifcs_bootp'} ]);
127         }
128 if ($canno) {
129         }
130 @grid = ( $text{'ifcs_ip'},
131           &ui_textbox("address", $b ? $b->{'address'} : "", 15) );
132 if ($in{'virtual'} && $in{'new'} && $virtual_netmask) {
133         # Netmask is fixed
134         push(@grid, $text{'ifcs_mask'}, "<tt>$virtual_netmask</tt>");
135         }
136 elsif (&can_edit("netmask", $b) && $access{'netmask'}) {
137         # Can edit netmask
138         push(@grid, $text{'ifcs_mask'},
139                     &ui_textbox("netmask", $b ? $b->{'netmask'}
140                                               : $config{'def_netmask'}, 15));
141         }
142 elsif ($b && $b->{'netmask'}) {
143         # Cannot edit
144         push(@grid, $text{'ifcs_mask'}, "<tt>$b->{'netmask'}</tt>");
145         }
146 if (&can_edit("broadcast", $b) && $access{'broadcast'}) {
147         # Can edit broadcast address
148         push(@grid, $text{'ifcs_broad'},
149             &ui_opt_textbox("broadcast",
150                 $b ? $b->{'broadcast'} : $config{'def_broadcast'},
151                 15, $text{'ifcs_auto'}));
152         }
153 elsif ($b && $b->{'broadcast'}) {
154         # Broadcast is fixed
155         push(@grid, $text{'ifcs_broad'}, "<tt>$b->{'broadcast'}</tt>");
156         }
157 push(@opts, [ "address", $text{'ifcs_static2'}, &ui_grid_table(\@grid, 2) ]);
158
159 # Show the IP field
160 if (@opts > 1) {
161         print &ui_table_row($text{'ifcs_mode'},
162                 &ui_radio_table("mode", $b && $b->{'dhcp'} ? "dhcp" :
163                                         $b && $b->{'bootp'} ? "bootp" :
164                                         $b && !$b->{'address'} ? "none" :
165                                                               "address",
166                                 \@opts), 3);
167         }
168 else {
169         print &ui_table_row($opts[0]->[1], $opts[0]->[2]);
170         }
171
172 # Show the IPv6 field
173 if (&supports_address6($b)) {
174         # Multiple IPs allowed
175         $table6 = &ui_columns_start([ $text{'ifcs_address6'},
176                                       $text{'ifcs_netmask6'} ], 50);
177         for($i=0; $i<=($b ? scalar(@{$b->{'address6'}}) : 0); $i++) {
178                 $table6 .= &ui_columns_row([
179                     &ui_textbox("address6_$i",
180                                 $b->{'address6'}->[$i], 40),
181                     &ui_textbox("netmask6_$i",
182                                 $b->{'netmask6'}->[$i] || 64, 10) ]);
183                 }
184         $table6 .= &ui_columns_end();
185         print &ui_table_row($text{'ifcs_mode6'},
186                 &ui_radio_table("mode6",
187                         !$b ? "none" :
188                         $b->{'auto6'} ? "auto" :
189                         @{$b->{'address6'}} ? "address" : "none",
190                         [ [ "none", $text{'ifcs_none6'} ],
191                           [ "auto", $text{'ifcs_auto6'} ],
192                           [ "address", $text{'ifcs_static2'}, $table6 ] ]), 2);
193         }
194
195 # MTU
196 if (&can_edit("mtu", $b) && $access{'mtu'}) {
197         $mtufield = &ui_opt_textbox(
198                 "mtu", $b ? $b->{'mtu'} : $config{'def_mtu'}, 8,
199                 $text{'default'});
200         }
201 else {
202         $mtufield = $b && $b->{'mtu'} ? $b->{'mtu'} : undef;
203         }
204 if ($mtufield) {
205         print &ui_table_row($text{'ifcs_mtu'}, $mtufield);
206         }
207
208 # Virtual sub-interfaces
209 if ($b && $b->{'virtual'} eq "") {
210         $vcount = 0;
211         foreach $vb (@boot) {
212                 if ($vb->{'virtual'} ne "" && $vb->{'name'} eq $b->{'name'}) {
213                         $vcount++;
214                         }
215                 }
216         $vlink = "";
217         if ($access{'virt'} && !$noos_support_add_virtifcs) {
218                 $vlink = "(<a href='edit_bifc.cgi?new=1&virtual=$b->{'name'}'>".
219                          "$text{'ifcs_addvirt'}</a>)\n";
220                 }
221         print &ui_table_row($text{'ifcs_virts'}, $vcount." ".$vlink);
222         }
223
224 # Special parameters for teaming
225 if ($in{'bond'} || &iface_type($b->{'name'}) eq 'Bonded') {
226         # Select bonding teampartner
227         print &ui_table_row($text{'bonding_teamparts'},
228                 &ui_textbox("partner", $b->{'partner'}, 10));
229         
230         # Select teaming mode
231         @mode = ("balance-rr", "activebackup", "balance-xor", "broadcast", "802.3ad", "balance-tlb", "balance-alb");
232         print &ui_table_row($text{'bonding_teammode'},
233                 &ui_select("bondmode", int($b->{'mode'}),
234                            [ map { [ $_, $mode[$_] ] } (0 .. $#mode) ]));
235
236         # Select mii Monitoring Interval
237         print &ui_table_row($text{'bonding_miimon'},
238                 &ui_textbox("miimon", $b->{'miimon'}, 5)." ms");
239
240         # Select updelay
241         print &ui_table_row($text{'bonding_updelay'},
242                 &ui_textbox("updelay", $b->{'updelay'}, 5)." ms");
243
244         # Select downdelay
245         print &ui_table_row($text{'bonding_downdelay'},
246                 &ui_textbox("downdelay", $b->{'downdelay'}, 5)." ms");
247         }
248
249 # Special Parameter for vlan tagging
250 if(($in{'vlan'}) or (&iface_type($b->{'name'}) =~ /^(.*) (VLAN)$/)) {
251         $b->{'name'} =~ /(\S+)\.(\d+)/;
252         $physical = $1;
253         $vlanid = $2;
254
255         # Phyical device
256         @phys = grep { $_->{'virtual'} eq '' } &active_interfaces();
257         print &ui_table_row($text{'vlan_physical'},
258                 $in{'new'} ? &ui_select("physical", $physical,
259                                         [ map { $_->{'fullname'} } @phys ])
260                            : $physical.&ui_hidden("physical", $physical));
261         
262         # VLAN ID
263         print &ui_table_row($text{'vlan_id'},
264                 $in{'new'} ? &ui_textbox("vlanid", $vlanid, 10)
265                            : $vlanid.&ui_hidden("vlanid", $vlanid));
266         }
267
268 # Hardware address, if non-virtual
269 if (($in{'new'} && $in{'virtual'} eq "" && !$in{'bridge'}) ||
270     (!$in{'new'} && $b->{'virtual'} eq "" &&
271      defined(&boot_iface_hardware) &&
272      &boot_iface_hardware($b->{'name'}))) {
273         $hardfield = &ui_opt_textbox("ether", $b->{'ether'}, 30,
274                                      $text{'aifc_default'});
275         print &ui_table_row($text{'aifc_hard'}, $hardfield);
276         }
277
278 # Real interface for bridge
279 if ($in{'bridge'} || $b && $b->{'bridge'}) {
280         @ethboot = sort { $a cmp $b }
281                      map { $_->{'fullname'} }
282                        grep { $_->{'fullname'} =~ /^eth(\d+)$/ } @boot;
283         print &ui_table_row($text{'bifc_bridgeto'},
284                 &ui_select("bridgeto", $b->{'bridgeto'}, \@ethboot, 1, 0,
285                            $in{'new'} ? 0 : 1));
286         }
287
288 print &ui_table_end();
289      
290 # Generate and show buttons at end of the form
291 @buts = ( );
292 if ($access{'bootonly'}) {
293         # Can only save both boot-time and active
294         if ($in{'new'}) {
295                 push(@buts, [ "activate", $text{'bifc_capply'} ]);
296                 }
297         else {
298                 push(@buts, [ "activate", $text{'bifc_apply'} ]);
299                 if ($access{'delete'}) {
300                         push(@buts, [ "unapply", $text{'bifc_dapply'} ]);
301                         }
302                 }
303         }
304 else {
305         # Show buttons to save both boot-time and/or active
306         if ($in{'new'}) {
307                 push(@buts, [ undef, $text{'create'} ]);
308                 push(@buts, [ "activate", $text{'bifc_capply'} ]);
309                 }
310         else {
311                 push(@buts, [ undef, $text{'save'} ])
312                         unless $always_apply_ifcs;
313                 if (!($b->{'bootp'} || $b->{'dhcp'}) ||
314                     defined(&apply_interface)) {
315                         push(@buts, [ "activate", $text{'bifc_apply'} ]);
316                         }
317                 if ($access{'delete'}) {
318                         push(@buts, [ "unapply", $text{'bifc_dapply'} ]);
319                         push(@buts, [ "delete", $text{'delete'} ])
320                                 unless $noos_support_delete_ifcs;
321                         }
322                 }
323         }
324 print &ui_form_end(\@buts);
325
326 &ui_print_footer("list_ifcs.cgi?mode=boot", $text{'ifcs_return'});
327