ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FreeBSDAdmin/DNS/reverse.pl
Revision: 896
Committed: 2007-04-28T01:28:37-07:00 (18 years, 2 months ago) by douglas
Content type: text/x-perl
File size: 2276 byte(s)
Log Message:
New server, better arrangement...

File Contents

# User Rev Content
1 douglas 846 #!/usr/local/bin/perl
2     # Reverse
3     #
4 douglas 845 # Douglas Thrift
5     #
6     # $Id$
7    
8 douglas 846 use strict;
9     use warnings;
10     use Net::hostent;
11     use POSIX qw(:time_h :unistd_h);
12     use Socket;
13    
14     my @hostnames;
15    
16     open DAT, "$ENV{HOME}/reverse.dat" or die "$0: $!";
17    
18     while (<DAT>)
19 douglas 845 {
20 douglas 853 $hostnames[$#hostnames + 1] = $& if (/^[^#].*$/);
21 douglas 845 }
22    
23 douglas 846 close DAT;
24 douglas 845
25 douglas 846 my $namedb = geteuid == 0 ? '/etc/namedb' : '.';
26    
27     open CONF, "$namedb/named.conf" or die "$0: $!";
28    
29     my $conf;
30    
31     while (<CONF>)
32 douglas 845 {
33 douglas 846 $conf .= $_;
34 douglas 845 }
35    
36 douglas 846 my $updated = 0;
37 douglas 845
38 douglas 846 for my $hostname (@hostnames)
39     {
40     my $host = gethostbyname $hostname;
41    
42     if (defined $host)
43     {
44     my $address = inet_ntoa($host->addr);
45     my $zone = join ('.', reverse unpack('C4', $host->addr)) . '.in-addr.arpa';
46    
47     $host = gethostbyaddr ($host->addr, AF_INET);
48    
49     if (!defined ($host) || $host->name ne $hostname)
50     {
51 douglas 896 open ZONE, '>', "$namedb/reverse/$zone" or die "$0: $!";
52 douglas 846
53     my $serialnum = strftime('%Y%m%d01', localtime);
54    
55 douglas 896 if ($conf =~ /\n# $hostname\nzone "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\.in-addr\.arpa)" {\n\ttype master;\n\tfile "reverse\/\1";\n};\n/)
56 douglas 846 {
57     next if ($1 eq $zone);
58    
59     my $oldzone = $1;
60 douglas 896 my $oldzonefile = "$namedb/reverse/$oldzone";
61 douglas 846
62     open OLDZONE, $oldzonefile;
63    
64     $oldzone =~ s/\./\\./g;
65    
66     while (<OLDZONE>)
67     {
68     s/$oldzone/$zone/;
69     s/\d{10} ; serial$/$serialnum ; serial/;
70    
71     print ZONE;
72     }
73    
74     close OLDZONE;
75     unlink $oldzonefile;
76    
77     $conf =~ s/$oldzone/$zone/g;
78     }
79     else
80     {
81     print ZONE "\$ORIGIN .
82 douglas 845 \$TTL 3600 ; 1 hour
83 douglas 846 $zone IN SOA ns.douglasthrift.net. admin.douglasthrift.net. (
84     $serialnum ; serial
85 douglas 845 7200 ; refresh (2 hours)
86     3600 ; retry (1 hour)
87     604800 ; expire (1 week)
88     3600 ; minimum (1 hour)
89     )
90     NS ns.douglasthrift.net.
91     PTR $hostname.
92 douglas 846 ";
93 douglas 845
94 douglas 846 $conf .= "
95 douglas 845 # $hostname
96 douglas 846 zone \"$zone\" {
97 douglas 845 type master;
98 douglas 896 file \"reverse/$zone\";
99 douglas 845 };
100 douglas 846 ";
101     }
102    
103     close ZONE;
104    
105     $updated = 1;
106     }
107     }
108 douglas 896 elsif ($conf =~ s/\n# $hostname\nzone "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\.in-addr\.arpa)" {\n\ttype master;\n\tfile "reverse\/\1";\n};\n//)
109 douglas 846 {
110 douglas 896 unlink "$namedb/reverse/$1";
111 douglas 846
112     $updated = 1;
113     }
114     }
115    
116     if ($updated)
117     {
118     open CONF, '>', "$namedb/named.conf" or die "$0: $!";
119     print CONF $conf;
120     }
121    
122     close CONF;
123 douglas 896 exec {'/usr/sbin/rndc'} 'rndc', 'reconfig' or die "$0: $!" if ($updated && geteuid == 0);

Properties

Name Value
svn:executable *
svn:keywords Id