source: trunk/mergerecs@ 824

Last change on this file since 824 was 797, checked in by Kris Deugau, 4 years ago

/trunk

Clean up a lingering nuisance with Perl's default include path; all scripts
should now run correctly no matter what the caller's current directory.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 7.6 KB
RevLine 
[643]1#!/usr/bin/perl
2# Merge matching forward and reverse A/PTR or AAAA/PTR pairs
3##
4# $Id: mergerecs 797 2020-11-03 20:38:37Z kdeugau $
[797]5# Copyright 2014,2016,2018,2020 Kris Deugau <kdeugau@deepnet.cx>
[643]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
[797]29# push "the directory the script is in" into @INC
30use FindBin;
31use lib "$FindBin::RealBin/";
[643]32
33use DNSDB;
34
35usage() if !$ARGV[0];
36
37sub usage {
[645]38 die qq(usage: mergerecs zone [domain] [--detail]
[744]39 zone The primary zone to walk for records to try to merge. May be either
40 a reverse zone or a domain.
41 domain Optionally restrict record merges in a reverse zone to a specific
42 domain.
[645]43 --detail Optional argument to add one log entry for each A+PTR pair merged
44 instead of a single generic entry.
[643]45);
46}
47
[645]48my $logdetail = 0;
49my $matchdom = '';
[643]50
[645]51my $pzone = shift @ARGV;
52if (@ARGV) {
53 if ($ARGV[0] eq '--detail') {
54 $logdetail = 1;
55 } else {
56 $matchdom = shift @ARGV;
57 $logdetail = 1 if @ARGV && $ARGV[0] eq '--detail';
58 }
59}
60
[643]61my $dnsdb = new DNSDB or die "Couldn't create DNSDB object: ".$DNSDB::errstr."\n";
62my $dbh = $dnsdb->{dbh};
63
64# get userdata for log
65($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
[770]66$dnsdb->{logfullname} =~ s/,//g;
[643]67$dnsdb->{loguserid} = 0; # not worth setting up a pseudouser the way the RPC system does
68$dnsdb->{logusername} = $dnsdb->{logusername}."/mergerecs";
69$dnsdb->{logfullname} = $dnsdb->{logusername} if !$dnsdb->{logfullname};
70
71# and now the meat
72
73# get zone ID
74my $rev;
75my $zid;
76if ($pzone =~ /\.arpa$/ || $pzone =~ m{^[0-9./a-fA-F:]+$}) {
77 # first zone is a reverse zone
78 $rev = 'y';
79 my $npzone;
80 if ($pzone =~ /\.arpa$/) {
81 my $msg;
82 ($msg, $npzone) = $dnsdb->_zone2cidr($pzone);
83 die "$pzone is not a valid reverse zone specification\n" if $msg eq 'FAIL';
84 } else {
85 $npzone = new NetAddr::IP $pzone;
86 }
87 die "$pzone is not a valid reverse zone specification\n" if !$npzone;
[744]88 $zid = $dnsdb->revID($npzone, '');
[643]89} else {
90 $rev = 'n';
[744]91 $zid = $dnsdb->domainID($pzone, '');
[643]92}
[744]93die "$pzone is not a zone in the database (".$dnsdb->errstr.")\n" if !$zid;
[643]94
95# check the second arg.
[744]96my $fzid = $dnsdb->domainID($matchdom, '');
[643]97die "$matchdom is not a domain in the database\n" if $matchdom && !$fzid;
98
99if ($rev eq 'n' && $matchdom) {
100 $fzid = 0;
101 $matchdom = '';
102}
103
104# group may or may not in fact be
105my $group = $dnsdb->parentID(revrec => $rev, id => $zid, type => ($rev eq 'n' ? 'domain' : 'revzone') );
106
107local $dbh->{AutoCommit} = 0;
108local $dbh->{RaiseError} = 1;
109
110eval {
111 if ($rev eq 'n') {
112 # merge records in a forward zone
113 my $reclist = $dbh->prepare("SELECT host,val,type,record_id,ttl,location FROM records ".
114 "WHERE (type=1 OR type=28) AND domain_id = ?");
115 my $findsth = $dbh->prepare("SELECT rdns_id,record_id,ttl FROM records ".
116 "WHERE host = ? AND val = ? AND type=12");
117 my $mergesth = $dbh->prepare("UPDATE records SET rdns_id = ?, ttl = ?, type = ? WHERE record_id = ?");
118 my $delsth = $dbh->prepare("DELETE FROM records WHERE record_id = ?");
119
120 $reclist->execute($zid);
121 my $nrecs = 0;
[744]122 my @cloglist;
[643]123 while (my ($host,$val,$type,$id,$ttl,$loc) = $reclist->fetchrow_array) {
124 my $etype = 12;
125 my $logentry;
126 $findsth->execute($host, $val);
127 my ($erdns,$erid,$ettl) = $findsth->fetchrow_array;
128 if ($erid) {
129 if ($type == 1) { # PTR -> A+PTR
130 $etype = 65280;
131 $logentry = "Merged A record with PTR record '$host A+PTR $val', TTL $ettl";
132 }
133 if ($type == 28) { # PTR -> AAAA+PTR
134 $etype = 65281;
135 $logentry = "Merged AAAA record with PTR record '$host AAAA+PTR $val', TTL $ettl";
136 }
137 $ettl = ($ettl < $ttl ? $ettl : $ttl); # use lower TTL
138 $mergesth->execute($erdns, $ettl, $etype, $id);
139 $delsth->execute($erid);
[645]140 if ($logdetail) {
[744]141 my $lid = $dnsdb->_log(group_id => $group, domain_id => $zid, rdns_id => $erdns, entry => $logentry);
142 push @cloglist, $lid;
[645]143 print "$logentry\n";
144 }
[662]145 $nrecs++;
[643]146 }
[645]147 } # while
[744]148 my $lpid = $dnsdb->_log(group_id => $group, domain_id => $zid,
[645]149 entry => "Merged $nrecs A and AAAA records in $pzone with matching PTRs");
[744]150 # since unlike in most other operations, we only "know" the parent log entry *after*
151 # we're done entering all the child entries, we have to update the children with the parent ID
152 my $assoc = $dbh->prepare("UPDATE log SET logparent = ? WHERE log_id = ?");
153 for my $lcid (@cloglist) {
154 $assoc->execute($lpid, $lcid);
[643]155 }
[645]156 print "Merged $nrecs A and AAAA records in $pzone with matching PTRs\n";
[643]157
158 } else {
[645]159 # merge records in a reverse zone
[643]160 my $reclist = $dbh->prepare("SELECT host,val,type,record_id,ttl,location FROM records ".
161 "WHERE type=12 AND rdns_id = ?");
162 my $findsth = $dbh->prepare("SELECT domain_id,type,record_id,ttl FROM records ".
163 "WHERE host = ? AND val = ? AND (type=1 OR type=28)");
164 my $mergesth = $dbh->prepare("UPDATE records SET domain_id = ?, ttl = ?, type = ? WHERE record_id = ?");
165 my $delsth = $dbh->prepare("DELETE FROM records WHERE record_id = ?");
166
167 $reclist->execute($zid);
168 my $nrecs = 0;
[744]169 my @cloglist;
[643]170 while (my ($host,$val,$type,$id,$ttl,$loc) = $reclist->fetchrow_array) {
171 if ($matchdom) {
172 next unless $host =~ /$matchdom$/;
173 }
174 my $ntype = 12;
175 my $logentry;
176 $findsth->execute($host, $val);
177 my ($edid,$etype,$erid,$ettl) = $findsth->fetchrow_array;
178 if ($erid) {
179 if ($etype == 1) { # PTR -> A+PTR
180 $ntype = 65280;
181 $logentry = "Merged PTR record with A record '$host A+PTR $val', TTL $ettl";
182 }
183 if ($etype == 28) { # PTR -> AAAA+PTR
184 $ntype = 65281;
185 $logentry = "Merged PTR record with A record '$host AAAA+PTR $val', TTL $ettl";
186 }
187 $ettl = ($ettl < $ttl ? $ettl : $ttl); # use lower TTL
188 $mergesth->execute($edid, $ettl, $ntype, $id);
189 $delsth->execute($erid);
[645]190 if ($logdetail) {
[744]191 my $lid = $dnsdb->_log(group_id => $group, domain_id => $edid, rdns_id => $zid, entry => $logentry);
192 push @cloglist, $lid;
193 print "$lid: $logentry\n";
[645]194 }
[662]195 $nrecs++;
[643]196 }
197 } # while
[645]198 my $entry = "Merged $nrecs PTR records in $pzone with matching A or AAAA records".($fzid ? " in $matchdom" : '');
[744]199 my $lpid;
200 if ($fzid) {
201 $lpid = $dnsdb->_log(group_id => $group, domain_id => $fzid, rdns_id => $zid, entry => $entry);
202 } else {
203 $lpid = $dnsdb->_log(group_id => $group, rdns_id => $zid, entry => $entry);
[645]204 }
[744]205 # since unlike in most other operations, we only "know" the parent log entry *after*
206 # we're done entering all the child entries, we have to update the children with the parent ID
207 my $assoc = $dbh->prepare("UPDATE log SET logparent = ? WHERE log_id = ?");
208 for my $lcid (@cloglist) {
209 $assoc->execute($lpid, $lcid);
210 }
[645]211 print "$entry\n";
[744]212 }
213
[643]214 $dbh->commit;
215};
216if ($@) {
217 $dbh->rollback;
218 die "Failure on record update/delete: $@\n";
219}
Note: See TracBrowser for help on using the repository browser.