Handle hostnames with upper-case letters
[webmin.git] / net / save_dns.cgi
1 #!/usr/local/bin/perl
2 # save_dns.cgi
3 # Save DNS client configuration
4
5 require './net-lib.pl';
6 $access{'dns'} == 2 || &error($text{'dns_ecannot'});
7 &error_setup($text{'dns_err'});
8 &ReadParse();
9 $old_hostname = &get_system_hostname();
10
11 $in{'hostname'} =~ /^[A-z0-9\.\-]+$/ ||
12         &error(&text('dns_ehost', $in{'hostname'}));
13 $dns = { };
14 for($i=0; defined($ns = $in{"nameserver_$i"}); $i++) {
15         $ns = $in{"nameserver_$i"};
16         $ns =~ s/^\s+//; $ns =~ s/\s+$//;
17         if ($ns) {
18                 &check_ipaddress_any($ns) ||
19                         &error(&text('dns_ens', $ns));
20                 push(@{$dns->{'nameserver'}}, $ns);
21                 }
22         }
23 if ($in{'name0'}) {
24     my $i = 0 ;
25     my $namekey="name$i";
26     while ($in{$namekey}) {
27         $dns->{'name'}[$i] = $in{$namekey};
28         my $nskey = "nameserver$i";
29         my $j = -1;
30         while (++$j < $max_dns_servers) {
31             $ns = $in{"${nskey}_$j"};
32             $ns =~ s/^\s+//; $ns =~ s/\s+$//;
33             if ($ns) {
34                 &check_ipaddress_any($ns) ||
35                     &error(&text('dns_ens', $ns));
36                 push(@{$dns->{$nskey}}, $ns);
37             }
38         }
39         $i++;
40         $namekey="name$i";
41     }
42 }
43 if (!$in{'domain_def'}) {
44         @dlist = split(/\s+/, $in{'domain'});
45         foreach $d (@dlist) {
46                 $d =~ /^[A-z0-9\.\-]+$/ ||
47                         &error(&text('dns_edomain', $d));
48                 push(@{$dns->{'domain'}}, $d);
49                 }
50         @dlist>0 || &error($text{'dns_esearch'});
51         }
52 &parse_order($dns);
53 &save_dns_config($dns);
54 &save_hostname($in{'hostname'});
55
56 if ($in{'hosts'} && $in{'hostname'} ne $old_hostname) {
57         # Update hostname in /etc/hosts too
58         @hosts = &list_hosts();
59         foreach $h (@hosts) {
60                 local $found = 0;
61                 foreach $n (@{$h->{'hosts'}}) {
62                         if (lc($n) eq lc($old_hostname)) {
63                                 $n = $in{'hostname'};
64                                 $found++;
65                                 }
66                         }
67                 &modify_host($h) if ($found);
68                 }
69
70         # Update in ipnodes too
71         @ipnodes = &list_ipnodes();
72         foreach $h (@ipnodes) {
73                 local $found = 0;
74                 foreach $n (@{$h->{'ipnodes'}}) {
75                         if (lc($n) eq lc($old_hostname)) {
76                                 $n = $in{'hostname'};
77                                 $found++;
78                                 }
79                         }
80                 &modify_ipnode($h) if ($found);
81                 }
82         }
83
84 &webmin_log("dns", undef, undef, \%in);
85 &redirect("");
86