source: trunk/mergerecs@ 867

Last change on this file since 867 was 837, checked in by Kris Deugau, 3 years ago

/trunk

Commit some lurking tweaks to mergerecs

  • user info for log
  • use location of ':ANY:' to find and match domains or reverse zones that may have default locations set

Update include path finder - see #80

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 7.9 KB
Line 
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 $
5# Copyright 2014-2022 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# 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.
31use File::Spec ();
32use File::Basename ();
33my $path;
34BEGIN {
35 $path = File::Basename::dirname(File::Spec->rel2abs($0));
36 if ($path =~ /(.*)/) {
37 $path = $1;
38 }
39}
40use lib $path;
41
42use DNSDB;
43
44usage() if !$ARGV[0];
45
46sub usage {
47 die qq(usage: mergerecs zone [domain] [--detail]
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.
52 --detail Optional argument to add one log entry for each A+PTR pair merged
53 instead of a single generic entry.
54);
55}
56
57my $logdetail = 0;
58my $matchdom = '';
59
60my $pzone = shift @ARGV;
61if (@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
70my $dnsdb = new DNSDB or die "Couldn't create DNSDB object: ".$DNSDB::errstr."\n";
71my $dbh = $dnsdb->{dbh};
72
73# get userdata for log
74($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
75$dnsdb->{logfullname} =~ s/,//g;
76$dnsdb->{loguserid} = 0; # not worth setting up a pseudouser the way the RPC system does
77$dnsdb->{logusername} = $dnsdb->{logusername}."/mergerecs";
78$dnsdb->{logfullname} = ($dnsdb->{logfullname} ? $dnsdb->{logfullname}."/mergerecs" : $dnsdb->{logusername});
79
80# and now the meat
81
82# get zone ID
83my $rev;
84my $zid;
85if ($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;
97 $zid = $dnsdb->revID($npzone, ':ANY:');
98} else {
99 $rev = 'n';
100 $zid = $dnsdb->domainID($pzone, ':ANY:');
101}
102die "$pzone is not a zone in the database (".$dnsdb->errstr.")\n" if !$zid;
103
104# check the second arg.
105my $fzid = $dnsdb->domainID($matchdom, '');
106die "$matchdom is not a domain in the database\n" if $matchdom && !$fzid;
107
108if ($rev eq 'n' && $matchdom) {
109 $fzid = 0;
110 $matchdom = '';
111}
112
113# group may or may not in fact be
114my $group = $dnsdb->parentID(revrec => $rev, id => $zid, type => ($rev eq 'n' ? 'domain' : 'revzone') );
115
116local $dbh->{AutoCommit} = 0;
117local $dbh->{RaiseError} = 1;
118
119eval {
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;
131 my @cloglist;
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);
149 if ($logdetail) {
150 my $lid = $dnsdb->_log(group_id => $group, domain_id => $zid, rdns_id => $erdns, entry => $logentry);
151 push @cloglist, $lid;
152 print "$logentry\n";
153 }
154 $nrecs++;
155 }
156 } # while
157 my $lpid = $dnsdb->_log(group_id => $group, domain_id => $zid,
158 entry => "Merged $nrecs A and AAAA records in $pzone with matching PTRs");
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);
164 }
165 print "Merged $nrecs A and AAAA records in $pzone with matching PTRs\n";
166
167 } else {
168 # merge records in a reverse zone
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;
178 my @cloglist;
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);
199 if ($logdetail) {
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";
203 }
204 $nrecs++;
205 }
206 } # while
207 my $entry = "Merged $nrecs PTR records in $pzone with matching A or AAAA records".($fzid ? " in $matchdom" : '');
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);
213 }
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 }
220 print "$entry\n";
221 }
222
223 $dbh->commit;
224};
225if ($@) {
226 $dbh->rollback;
227 die "Failure on record update/delete: $@\n";
228}
Note: See TracBrowser for help on using the repository browser.