ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FreeBSDAdmin/DNS/reverse.pl
Revision: 846
Committed: 2006-07-28T02:37:29-07:00 (18 years, 11 months ago) by douglas
Content type: text/x-perl
File size: 2247 byte(s)
Log Message:
Woo!

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

Properties

Name Value
svn:executable *
svn:keywords Id