source: trunk/compact-recs.pl@ 835

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

/trunk

Logging tweak for compact-recs.pl

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 4.8 KB
Line 
1#!/usr/bin/perl
2# Quick utility to use post-import to convert great huge piles of
3# A+PTR records to single A+PTR template records
4##
5# $Id: compact-recs.pl 835 2022-04-19 21:02:06Z kdeugau $
6# Copyright 2013-2022 Kris Deugau <kdeugau@deepnet.cx>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20##
21
22use strict;
23use warnings;
24
25# Taint-safe (ish) voodoo to push "the directory the script is in" into @INC.
26# See https://secure.deepnet.cx/trac/dnsadmin/ticket/80 for more gory details on how we got here.
27use File::Spec ();
28use File::Basename ();
29my $path;
30BEGIN {
31 $path = File::Basename::dirname(File::Spec->rel2abs($0));
32 if ($path =~ /(.*)/) {
33 $path = $1;
34 }
35}
36use lib $path;
37
38use DNSDB;
39
40usage() if !$ARGV[1];
41
42sub usage {
43 die qq(usage: compact-recs.pl netblock pattern
44 netblock the CIDR block to define the A+PTR template on
45 pattern the pattern to define the new A+PTR template with, and
46 to match A+PTR records within the netblock for deletion
47 OR
48 compact-recs.pl --batch patternfile
49 patternfile should be a file containing a list of netblock-pattern
50 pairs, whitespace separated
51
52 A PTR template record will be created instead of an A+PTR template
53 if the forward zone specified in the template is not present in
54 the database.
55
56 WARNING: Multiple runs will result in duplicate template records.
57);
58}
59
60my $dnsdb = new DNSDB or die "Couldn't create DNSDB object: ".$DNSDB::errstr."\n";
61my $dbh = $dnsdb->{dbh};
62
63my $code;
64
65# get userdata for log
66($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
67$dnsdb->{logfullname} =~ s/,//g;
68$dnsdb->{loguserid} = 0; # not worth setting up a pseudouser the way the RPC system does
69$dnsdb->{logusername} = $dnsdb->{logusername}."/compact-recs.pl";
70$dnsdb->{logfullname} = ($dnsdb->{logfullname} ? $dnsdb->{logfullname}."/compact-recs.pl" : $dnsdb->{logusername});
71
72if ($ARGV[0] eq '--batch') {
73 open NBLIST, "<$ARGV[1]";
74 while (<NBLIST>) {
75 next if /^#/;
76 next if /^\s*$/;
77 s/^\s*//;
78 squashem(split(/\s+/));
79 }
80} else {
81 my $cidr = new NetAddr::IP $ARGV[0];
82 usage() if !$cidr;
83 squashem($cidr, $ARGV[1]);
84}
85
86exit 0;
87
88
89sub squashem {
90 my $cidr = shift;
91 my $patt = shift;
92
93 $dbh->{AutoCommit} = 0;
94 $dbh->{RaiseError} = 1;
95
96 my ($zonecidr,$zone,$ploc) = $dbh->selectrow_array("SELECT revnet,rdns_id,default_location FROM revzones WHERE revnet >>= ?",
97 undef, ($cidr) );
98 if (!$zone) {
99 warn "$cidr is not within a zone currently managed here.\n";
100 return;
101 }
102 my $soa = $dnsdb->getSOA('n', 'y', $zone);
103 my $dparent = $dnsdb->_hostparent($patt) || 0;
104 my $newtype = ($dparent ? 65283 : 65282);
105
106 my ($istmpl) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = ? AND ".
107 "(type=65282 OR type=65283) AND val = ?", undef, ($zone, $cidr) );
108 if ($istmpl) {
109 print "Template already exists for $cidr, manual cleanup required\n";
110 return;
111 }
112
113 print "Converting PTR and A+PTR records in $cidr matching $patt to single $typemap{$newtype} record\n";
114 my $delcnt = 0;
115
116 eval {
117 # First, clean up the records that match the template.
118 my $getsth = $dbh->prepare("SELECT record_id,host,val FROM records ".
119 "WHERE (type = 12 OR type > 65000) AND inetlazy(val) << ? AND rdns_id = ?");
120 my $delsth = $dbh->prepare("DELETE FROM records WHERE record_id = ?");
121 $getsth->execute($cidr, $zone);
122 my $i = 0;
123 while (my ($id,$host,$val) = $getsth->fetchrow_array) {
124 my $cmp = $patt;
125 DNSDB::_template4_expand(\$cmp, $val, \$cidr);
126 $delsth->execute($id) if $cmp eq $host;
127 $delcnt++ if $cmp eq $host;
128 }
129
130 $dbh->do("INSERT INTO records (domain_id, rdns_id, host, type, val, ttl, location) VALUES (?,?,?,?,?,?,?)",
131 undef, ($dparent, $zone, $patt, $newtype, $cidr, $soa->{minttl}, $ploc) );
132 $dbh->do("UPDATE revzones SET changed='y' WHERE rdns_id = ?", undef, ($zone));
133 $dbh->do("UPDATE domains SET changed='y' WHERE domain_id = ?", undef, ($dparent)) if $dparent;
134 $dnsdb->_log(rdns_id => $zone, domain_id => $dparent, group_id => 1,
135 entry => "A+PTR and/or PTR records in $cidr matching $patt replaced by $typemap{$newtype} record for $cidr");
136
137 $dbh->commit;
138 };
139 if ($@) {
140 print "barf: $@\n";
141 $dbh->rollback;
142 return;
143 }
144 print " complete ($delcnt records)\n";
145} # squashem ()
Note: See TracBrowser for help on using the repository browser.