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