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