Handle hostnames with upper-case letters
[webmin.git] / zones / save_net.cgi
1 #!/usr/local/bin/perl
2 # Update, add or delete a network interface
3
4 require './zones-lib.pl';
5 do 'forms-lib.pl';
6 &ReadParse();
7 $zinfo = &get_zone($in{'zone'});
8 $zinfo || &error($text{'edit_egone'});
9 if (!$in{'new'}) {
10         # Find the network object
11         ($net) = grep { $_->{'address'} eq $in{'old'} } @{$zinfo->{'net'}};
12         $net || &error($text{'net_egone'});
13         $active = &get_active_interface($zinfo, $net);
14         }
15 $net ||= { 'keytype' => 'net' };
16
17 if ($in{'delete'}) {
18         # Just remove this network
19         &delete_zone_object($zinfo, $net);
20         &net::deactivate_interface($active) if ($active);
21         }
22 else {
23         # Validate inputs
24         $form = &get_net_form(\%in, $zinfo, $net);
25         $form->validate_redirect("edit_net.cgi");
26         if ($form->get_value("netmask")) {
27                 $cidr = &net::mask_to_prefix($form->get_value("netmask"));
28                 $net->{'address'} = $form->get_value("address")."/".$cidr;
29                 }
30         else {
31                 $net->{'address'} = $form->get_value("address");
32                 }
33         $net->{'physical'} = $form->get_value("physical");
34         &find_clash($zinfo, $net) &&
35                 $form->validate_redirect("edit_net.cgi",
36                                         [ [ "address", $text{'net_eclash'} ] ]);
37
38         # Create or update the real interface
39         if ($in{'new'}) {
40                 local $vmax = int($net::min_virtual_number);
41                 local $a;
42                 foreach $a (&net::active_interfaces()) {
43                         $vmax = $a->{'virtual'}
44                                 if ($a->{'name'} eq $in{'physical'} &&
45                                     $a->{'virtual'} > $vmax);
46                         }
47                 $active = { 'name' => $in{'physical'},
48                             'virtual' => $vmax+1,
49                             'fullname' => $in{'physical'}.":".($vmax+1),
50                             'zone' => $in{'zone'},
51                             'up' => 1 };
52                 }
53         if ($active) {
54                 $active->{'address'} = $in{'address'};
55                 if ($in{'netmask_def'}) {
56                         $active->{'netmask'} =
57                                 &net::automatic_netmask($in{'address'});
58                         }
59                 else {
60                         $active->{'netmask'} = $in{'netmask'};
61                         }
62                 $active->{'broadcast'} = &net::compute_broadcast(
63                         $active->{'address'}, $active->{'netmask'});
64                 $active->{'zone'} = $in{'zone'};
65                 }
66
67         # Save the zone settings
68         if ($in{'new'}) {
69                 &create_zone_object($zinfo, $net);
70                 if ($zinfo->{'status'} eq 'running') {
71                         &net::activate_interface($active);
72                         }
73                 }
74         else {
75                 &modify_zone_object($zinfo, $net);
76                 if ($active) {
77                         &net::activate_interface($active);
78                         }
79                 }
80         }
81
82 &webmin_log($in{'new'} ? "create" : $in{'delete'} ? "delete" : "modify",
83             "net", $in{'old'} || $net->{'address'}, $net);
84 &redirect("edit_zone.cgi?zone=$in{'zone'}");
85