Handle hostnames with upper-case letters
[webmin.git] / dnsadmin / create_master.cgi
1 #!/usr/local/bin/perl
2 # create_master.cgi
3 # Create a new master zone
4
5 require './dns-lib.pl';
6 &ReadParse();
7 $whatfailed = "Failed to create zone";
8 %access = &get_module_acl();
9 $access{'master'} || &error("You cannot create master zones");
10 &lock_file($config{'named_boot_file'});
11 $conf = &get_config();
12
13 # validate inputs
14 if ($in{'rev'}) {
15         $in{'zone'} =~ /^[\d\.]+$/ ||
16                 &error("'$in{'zone'}' is not a valid network");
17         $in{'zone'} = &ip_to_arpa($in{'zone'});
18         }
19 else {
20         $in{'zone'} =~ /^[A-Za-z0-9\-\.]+$/ ||
21                 &error("'$in{'zone'}' is not a valid domain name");
22         $in{'zone'} !~ /^[0-9\.]+$/ ||
23                 &error("'$in{'zone'}' must be a domain, not a network");
24         }
25 $in{'zone'} =~ s/\.$//g;
26 $in{'master'} =~ /^[A-Za-z0-9\-\.]+$/ ||
27         &error("'$in{'master'}' is not a valid master server");
28 if ($in{'master'} !~ /\.$/) { $in{'master'} .= "."; }
29 $in{'email'} =~ /^\S+\@\S+$/ ||
30         &error("'$in{'email'}' is not a valid email address");
31 $in{'email'} =~ s/\@/\./g;
32 if ($in{'email'} !~ /\.$/) { $in{'email'} .= "."; }
33 $in{'refresh'} =~ /^\S+$/ ||
34         &error("'$in{'refresh'}' is not a valid refresh time");
35 $in{'retry'} =~ /^\S+$/ ||
36         &error("'$in{'retry'}' is not a valid transfer retry time");
37 $in{'expiry'} =~ /^\S+$/ ||
38         &error("'$in{'expiry'}' is not a valid expiry time");
39 $in{'minimum'} =~ /^\S+$/ ||
40         &error("'$in{'minimum'}' is not a valid default TTL");
41 $base = $access{'dir'} eq '/' ? &base_directory($conf) : $access{'dir'};
42 if (!$in{'file_def'}) {
43         $in{'file'} =~ /^\S+$/ ||
44                 &error("'$in{'file'}' is not a valid filename");
45         if ($in{'file'} !~ /^\//) {
46                 $in{'file'} = $base."/".$in{'file'};
47                 }
48         &allowed_zone_file(\%access, $in{'file'}) ||
49                 &error("'$in{'file'}' is not an allowable zone file");
50         }
51 elsif ($in{'rev'}) {
52         # create filename for reverse zone
53         $in{'file'} = $base."/".&arpa_to_ip($in{'zone'}).".rev";
54         }
55 else {
56         # create filename for forward zone
57         $in{'file'} = $base."/$in{'zone'}.hosts";
58         }
59 &lock_file($in{'file'});
60 open(ZONE, ">$in{'file'}") || &error("Failed to create '$in{'file'}' : $?");
61 close(ZONE);
62
63 # create the SOA and NS records
64 if ($config{'soa_style'} == 1) {
65         $serial = &date_serial()."00";
66         }
67 else {
68         $serial = time();
69         }
70 $vals = "$in{'master'} $in{'email'} (\n".
71         "\t\t\t$serial\n".
72         "\t\t\t$in{'refresh'}\n".
73         "\t\t\t$in{'retry'}\n".
74         "\t\t\t$in{'expiry'}\n".
75         "\t\t\t$in{'minimum'} )";
76 &create_record($in{'file'}, "$in{'zone'}.", undef, "IN", "SOA", $vals);
77 &create_record($in{'file'}, "$in{'zone'}.", undef, "IN", "NS", $in{'master'});
78 &unlock_file($in{'file'});
79
80 # create the zone directive
81 &create_zone({ 'name' => 'primary', 'values' => [ $in{'zone'}, $in{'file'} ]});
82 &unlock_file($config{'named_boot_file'});
83 &webmin_log("create", "master", $in{'zone'}, \%in);
84
85 # Add the new zone to the access list
86 if ($access{'zones'} ne '*') {
87         $access{'zones'} .= " ".$in{'zone'};
88         &save_module_acl(\%access);
89         }
90 &redirect("");
91