source: trunk/mergerecs@ 643

Last change on this file since 643 was 643, checked in by Kris Deugau, 11 years ago

/trunk

Add a new segment in UPGRADE for 1.2.3 -> 1.2.4
Add mergerecs command-line utility

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 5.8 KB
Line 
1#!/usr/bin/perl
2# Merge matching forward and reverse A/PTR or AAAA/PTR pairs
3##
4# $Id: mergerecs 643 2014-06-10 17:49:14Z kdeugau $
5# Copyright 2014 Kris Deugau <kdeugau@deepnet.cx>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
20
21use strict;
22use warnings;
23
24#use Net::DNS;
25use DBI;
26
27use Data::Dumper;
28
29# don't remove! required for GNU/FHS-ish install from tarball
30use lib '.'; ##uselib##
31
32use DNSDB;
33
34usage() if !$ARGV[0];
35
36sub usage {
37 die qq(usage: mergerecs zone [domain]
38 zone The zone to walk for PTR or A/AAAA records to try to merge.
39 Using a reverse zone (either CIDR or in-addr.arpa) will
40 likely behave more consistently.
41 domain Optionally restrict record merges to a specified domain as well.
42 Ignored if zone is a domain.
43);
44}
45
46my $pzone = $ARGV[0];
47my $matchdom = $ARGV[1] || '';
48
49my $dnsdb = new DNSDB or die "Couldn't create DNSDB object: ".$DNSDB::errstr."\n";
50my $dbh = $dnsdb->{dbh};
51
52# get userdata for log
53($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
54$dnsdb->{loguserid} = 0; # not worth setting up a pseudouser the way the RPC system does
55$dnsdb->{logusername} = $dnsdb->{logusername}."/mergerecs";
56$dnsdb->{logfullname} = $dnsdb->{logusername} if !$dnsdb->{logfullname};
57
58# and now the meat
59
60# get zone ID
61my $rev;
62my $zid;
63if ($pzone =~ /\.arpa$/ || $pzone =~ m{^[0-9./a-fA-F:]+$}) {
64 # first zone is a reverse zone
65 $rev = 'y';
66 my $npzone;
67 if ($pzone =~ /\.arpa$/) {
68 my $msg;
69 ($msg, $npzone) = $dnsdb->_zone2cidr($pzone);
70 die "$pzone is not a valid reverse zone specification\n" if $msg eq 'FAIL';
71 } else {
72 $npzone = new NetAddr::IP $pzone;
73 }
74 die "$pzone is not a valid reverse zone specification\n" if !$npzone;
75 $zid = $dnsdb->revID($npzone);
76} else {
77 $rev = 'n';
78 $zid = $dnsdb->domainID($pzone);
79}
80die "$pzone is not a zone in the database\n" if !$zid;
81
82# check the second arg.
83my $fzid = $dnsdb->domainID($matchdom);
84die "$matchdom is not a domain in the database\n" if $matchdom && !$fzid;
85
86if ($rev eq 'n' && $matchdom) {
87 $fzid = 0;
88 $matchdom = '';
89}
90
91# group may or may not in fact be
92my $group = $dnsdb->parentID(revrec => $rev, id => $zid, type => ($rev eq 'n' ? 'domain' : 'revzone') );
93
94local $dbh->{AutoCommit} = 0;
95local $dbh->{RaiseError} = 1;
96
97eval {
98 if ($rev eq 'n') {
99 # merge records in a forward zone
100 my $reclist = $dbh->prepare("SELECT host,val,type,record_id,ttl,location FROM records ".
101 "WHERE (type=1 OR type=28) AND domain_id = ?");
102 my $findsth = $dbh->prepare("SELECT rdns_id,record_id,ttl FROM records ".
103 "WHERE host = ? AND val = ? AND type=12");
104 my $mergesth = $dbh->prepare("UPDATE records SET rdns_id = ?, ttl = ?, type = ? WHERE record_id = ?");
105 my $delsth = $dbh->prepare("DELETE FROM records WHERE record_id = ?");
106
107 $reclist->execute($zid);
108 my $nrecs = 0;
109 while (my ($host,$val,$type,$id,$ttl,$loc) = $reclist->fetchrow_array) {
110 my $etype = 12;
111 my $logentry;
112 $findsth->execute($host, $val);
113 my ($erdns,$erid,$ettl) = $findsth->fetchrow_array;
114 if ($erid) {
115 if ($type == 1) { # PTR -> A+PTR
116 $etype = 65280;
117 $logentry = "Merged A record with PTR record '$host A+PTR $val', TTL $ettl";
118 }
119 if ($type == 28) { # PTR -> AAAA+PTR
120 $etype = 65281;
121 $logentry = "Merged AAAA record with PTR record '$host AAAA+PTR $val', TTL $ettl";
122 }
123 $ettl = ($ettl < $ttl ? $ettl : $ttl); # use lower TTL
124 $mergesth->execute($erdns, $ettl, $etype, $id);
125 $delsth->execute($erid);
126 $dnsdb->_log(group_id => $group, domain_id => $zid, rdns_id => $erdns, entry => $logentry);
127 }
128 }
129
130 } else {
131 # merge records in a forward zone
132 my $reclist = $dbh->prepare("SELECT host,val,type,record_id,ttl,location FROM records ".
133 "WHERE type=12 AND rdns_id = ?");
134 my $findsth = $dbh->prepare("SELECT domain_id,type,record_id,ttl FROM records ".
135 "WHERE host = ? AND val = ? AND (type=1 OR type=28)");
136 my $mergesth = $dbh->prepare("UPDATE records SET domain_id = ?, ttl = ?, type = ? WHERE record_id = ?");
137 my $delsth = $dbh->prepare("DELETE FROM records WHERE record_id = ?");
138
139 $reclist->execute($zid);
140 my $nrecs = 0;
141 while (my ($host,$val,$type,$id,$ttl,$loc) = $reclist->fetchrow_array) {
142 if ($matchdom) {
143 next unless $host =~ /$matchdom$/;
144 }
145 my $ntype = 12;
146 my $logentry;
147 $findsth->execute($host, $val);
148 my ($edid,$etype,$erid,$ettl) = $findsth->fetchrow_array;
149 if ($erid) {
150 if ($etype == 1) { # PTR -> A+PTR
151 $ntype = 65280;
152 $logentry = "Merged PTR record with A record '$host A+PTR $val', TTL $ettl";
153 }
154 if ($etype == 28) { # PTR -> AAAA+PTR
155 $ntype = 65281;
156 $logentry = "Merged PTR record with A record '$host AAAA+PTR $val', TTL $ettl";
157 }
158 $ettl = ($ettl < $ttl ? $ettl : $ttl); # use lower TTL
159 $mergesth->execute($edid, $ettl, $ntype, $id);
160 $delsth->execute($erid);
161 $dnsdb->_log(group_id => $group, domain_id => $edid, rdns_id => $zid, entry => $logentry);
162 }
163 } # while
164 } # else
165 $dbh->commit;
166};
167if ($@) {
168 $dbh->rollback;
169 die "Failure on record update/delete: $@\n";
170}
Note: See TracBrowser for help on using the repository browser.