source: trunk/compact-recs.pl@ 782

Last change on this file since 782 was 770, checked in by Kris Deugau, 7 years ago

/trunk

Tweak some log-filling bits in a few of the command line tools

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 4.5 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 770 2018-02-13 22:19:14Z kdeugau $
6# Copyright 2013,2014 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
25use lib '.'; ##uselib##
26use DNSDB;
27
28usage() if !$ARGV[1];
29
30sub usage {
31 die qq(usage: compact-recs.pl netblock pattern
32 netblock the CIDR block to define the A+PTR template on
33 pattern the pattern to define the new A+PTR template with, and
34 to match A+PTR records within the netblock for deletion
35 OR
36 compact-recs.pl --batch patternfile
37 patternfile should be a file containing a list of netblock-pattern
38 pairs, whitespace separated
39
40 A PTR template record will be created instead of an A+PTR template
41 if the forward zone specified in the template is not present in
42 the database.
43
44 WARNING: Multiple runs will result in duplicate template records.
45);
46}
47
48my $dnsdb = new DNSDB or die "Couldn't create DNSDB object: ".$DNSDB::errstr."\n";
49my $dbh = $dnsdb->{dbh};
50
51my $code;
52
53# get userdata for log
54($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
55$dnsdb->{logfullname} =~ s/,//g;
56$dnsdb->{loguserid} = 0; # not worth setting up a pseudouser the way the RPC system does
57$dnsdb->{logusername} = $dnsdb->{logusername}."/compact-recs.pl";
58$dnsdb->{logfullname} = $dnsdb->{logusername} if !$dnsdb->{logfullname};
59
60if ($ARGV[0] eq '--batch') {
61 open NBLIST, "<$ARGV[1]";
62 while (<NBLIST>) {
63 next if /^#/;
64 next if /^\s*$/;
65 s/^\s*//;
66 squashem(split(/\s+/));
67 }
68} else {
69 my $cidr = new NetAddr::IP $ARGV[0];
70 usage() if !$cidr;
71 squashem($cidr, $ARGV[1]);
72}
73
74exit 0;
75
76
77sub squashem {
78 my $cidr = shift;
79 my $patt = shift;
80
81 $dbh->{AutoCommit} = 0;
82 $dbh->{RaiseError} = 1;
83
84 my ($zone,$ploc) = $dbh->selectrow_array("SELECT rdns_id,default_location FROM revzones WHERE revnet >>= ?",
85 undef, ($cidr) );
86 if (!$zone) {
87 warn "$cidr is not within a zone currently managed here.\n";
88 return;
89 }
90 my $soa = $dnsdb->getSOA('n', 'y', $zone);
91 my $dparent = $dnsdb->_hostparent($patt) || 0;
92 my $newtype = ($dparent ? 65283 : 65282);
93
94 my ($istmpl) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = ? AND ".
95 "(type=65282 OR type=65283) AND val = ?", undef, ($zone, $cidr) );
96 if ($istmpl) {
97 print "Template already exists for $cidr, manual cleanup required\n";
98 return;
99 }
100
101 print "Converting PTR and A+PTR records in $cidr matching $patt to single $typemap{$newtype} record\n";
102 my $delcnt = 0;
103
104 eval {
105 my $getsth = $dbh->prepare("SELECT record_id,host,val FROM records ".
106 "WHERE (type = 12 OR type > 65000) AND inetlazy(val) << ?");
107 my $delsth = $dbh->prepare("DELETE FROM records WHERE record_id = ?");
108 $getsth->execute($cidr);
109 my $i = 0;
110 while (my ($id,$host,$val) = $getsth->fetchrow_array) {
111 my $cmp = $patt;
112 DNSDB::_template4_expand(\$cmp, $val);
113 $delsth->execute($id) if $cmp eq $host;
114 $delcnt++ if $cmp eq $host;
115# print "got $id, '$host', '$val'; compare '$cmp'\t";
116# print " delete\n" if $cmp eq $host;
117# print " keep\n" if $cmp ne $host;
118# last if $i++ >8;
119 }
120
121 $dbh->do("INSERT INTO records (domain_id, rdns_id, host, type, val, ttl, location) VALUES (?,?,?,?,?,?,?)",
122 undef, ($dparent, $zone, $patt, $newtype, $cidr, $soa->{minttl}, $ploc) );
123 $dbh->do("UPDATE revzones SET changed='y' WHERE rdns_id = ?", undef, ($zone));
124 $dbh->do("UPDATE domains SET changed='y' WHERE domain_id = ?", undef, ($dparent)) if $dparent;
125 $dnsdb->_log(rdns_id => $zone, domain_id => $dparent, group_id => 1,
126 entry => "A+PTR and/or PTR records in $cidr matching $patt replaced by $typemap{$newtype} record for $cidr");
127
128# $dbh->rollback;
129 $dbh->commit;
130 };
131 if ($@) {
132 print "barf: $@\n";
133 $dbh->rollback;
134 return;
135 }
136 print " complete ($delcnt records)\n";
137} # squashem ()
Note: See TracBrowser for help on using the repository browser.