[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 |
|
---|
| 21 | use strict;
|
---|
| 22 | use warnings;
|
---|
| 23 |
|
---|
| 24 | #use Net::DNS;
|
---|
| 25 | use DBI;
|
---|
| 26 |
|
---|
| 27 | use Data::Dumper;
|
---|
| 28 |
|
---|
[797] | 29 | # push "the directory the script is in" into @INC
|
---|
| 30 | use FindBin;
|
---|
| 31 | use lib "$FindBin::RealBin/";
|
---|
[643] | 32 |
|
---|
| 33 | use DNSDB;
|
---|
| 34 |
|
---|
| 35 | usage() if !$ARGV[0];
|
---|
| 36 |
|
---|
| 37 | sub 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] | 48 | my $logdetail = 0;
|
---|
| 49 | my $matchdom = '';
|
---|
[643] | 50 |
|
---|
[645] | 51 | my $pzone = shift @ARGV;
|
---|
| 52 | if (@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] | 61 | my $dnsdb = new DNSDB or die "Couldn't create DNSDB object: ".$DNSDB::errstr."\n";
|
---|
| 62 | my $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
|
---|
| 74 | my $rev;
|
---|
| 75 | my $zid;
|
---|
| 76 | if ($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] | 93 | die "$pzone is not a zone in the database (".$dnsdb->errstr.")\n" if !$zid;
|
---|
[643] | 94 |
|
---|
| 95 | # check the second arg.
|
---|
[744] | 96 | my $fzid = $dnsdb->domainID($matchdom, '');
|
---|
[643] | 97 | die "$matchdom is not a domain in the database\n" if $matchdom && !$fzid;
|
---|
| 98 |
|
---|
| 99 | if ($rev eq 'n' && $matchdom) {
|
---|
| 100 | $fzid = 0;
|
---|
| 101 | $matchdom = '';
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | # group may or may not in fact be
|
---|
| 105 | my $group = $dnsdb->parentID(revrec => $rev, id => $zid, type => ($rev eq 'n' ? 'domain' : 'revzone') );
|
---|
| 106 |
|
---|
| 107 | local $dbh->{AutoCommit} = 0;
|
---|
| 108 | local $dbh->{RaiseError} = 1;
|
---|
| 109 |
|
---|
| 110 | eval {
|
---|
| 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 | };
|
---|
| 216 | if ($@) {
|
---|
| 217 | $dbh->rollback;
|
---|
| 218 | die "Failure on record update/delete: $@\n";
|
---|
| 219 | }
|
---|