| [796] | 1 | #!/usr/bin/perl | 
|---|
|  | 2 | # Quickly bump the serial number on one or more zones | 
|---|
|  | 3 | ## | 
|---|
|  | 4 | # Copyright 2019 Kris Deugau <kdeugau@deepnet.cx> | 
|---|
|  | 5 | # | 
|---|
|  | 6 | #    This program is free software: you can redistribute it and/or modify | 
|---|
|  | 7 | #    it under the terms of the GNU General Public License as published by | 
|---|
|  | 8 | #    the Free Software Foundation, either version 3 of the License, or | 
|---|
|  | 9 | #    (at your option) any later version. | 
|---|
|  | 10 | # | 
|---|
|  | 11 | #    This program is distributed in the hope that it will be useful, | 
|---|
|  | 12 | #    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 13 | #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 14 | #    GNU General Public License for more details. | 
|---|
|  | 15 | # | 
|---|
|  | 16 | #    You should have received a copy of the GNU General Public License | 
|---|
|  | 17 | #    along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
|  | 18 | ## | 
|---|
|  | 19 |  | 
|---|
|  | 20 | use strict; | 
|---|
|  | 21 | use warnings; | 
|---|
|  | 22 | use NetAddr::IP; | 
|---|
|  | 23 | use Data::Dumper; | 
|---|
|  | 24 |  | 
|---|
|  | 25 | # push "the directory the script is in" into @INC | 
|---|
|  | 26 | use FindBin; | 
|---|
|  | 27 | use lib "$FindBin::RealBin/"; | 
|---|
|  | 28 |  | 
|---|
|  | 29 | use DNSDB; | 
|---|
|  | 30 |  | 
|---|
|  | 31 | usage() if !@ARGV; | 
|---|
|  | 32 |  | 
|---|
|  | 33 | sub usage { | 
|---|
|  | 34 | die "usage:  bumpserial zone1 [zone2] [zone3] ...\n"; | 
|---|
|  | 35 | } | 
|---|
|  | 36 |  | 
|---|
|  | 37 | my $dnsdb = new DNSDB or die "Couldn't create DNSDB object: ".$DNSDB::errstr."\n"; | 
|---|
|  | 38 | my $dbh = $dnsdb->{dbh}; | 
|---|
|  | 39 |  | 
|---|
|  | 40 | # revID() and domainID() need a location.  assuming '' likely fails.  zones SHOULD usually | 
|---|
|  | 41 | # be unique irrespective of locations - we aren't big enough to need to do that (yet...) | 
|---|
|  | 42 | my $didsth = $dbh->prepare("SELECT domain_id,domain,default_location FROM domains WHERE domain = ?"); | 
|---|
|  | 43 | my $ridsth = $dbh->prepare("SELECT rdns_id,revnet,default_location FROM revzones WHERE revnet = ?"); | 
|---|
|  | 44 |  | 
|---|
|  | 45 | # do the updates by way of the ID, that way we've confirmed we've got a zone that's actually present | 
|---|
|  | 46 | my $dser = $dbh->prepare("UPDATE domains SET zserial = ? WHERE domain_id = ?"); | 
|---|
|  | 47 | my $rser = $dbh->prepare("UPDATE revzones SET zserial = ? WHERE rdns_id = ?"); | 
|---|
|  | 48 |  | 
|---|
|  | 49 | while (my $zone = shift @ARGV) { | 
|---|
|  | 50 | $zone = lc $zone; | 
|---|
|  | 51 | if ($zone =~ m,^[\d\./]+$, || $zone =~ /\.in-addr\.arpa$/) { | 
|---|
|  | 52 | # reverse zone, CIDR format or .arpa format.  ignoring non-octet CIDR ranges and ipv6 for now | 
|---|
|  | 53 | my $z; | 
|---|
|  | 54 | if ($zone =~ /\.in-addr\.arpa$/) { | 
|---|
|  | 55 | # convert to CIDR for database lookups | 
|---|
|  | 56 | $z = DNSDB::_zone2cidr($zone); | 
|---|
|  | 57 | } else { | 
|---|
|  | 58 | $z = new NetAddr::IP $zone; | 
|---|
|  | 59 | } | 
|---|
|  | 60 | if (!$z) { | 
|---|
|  | 61 | warn "error: $zone does not appear to be a valid CIDR range\n"; | 
|---|
|  | 62 | next; | 
|---|
|  | 63 | } | 
|---|
|  | 64 | $ridsth->execute("$z"); | 
|---|
|  | 65 | my $rlist = $ridsth->fetchall_arrayref({}); | 
|---|
|  | 66 | my $newser = scalar(time); | 
|---|
|  | 67 | if (scalar(@{$rlist}) > 1) { | 
|---|
|  | 68 | print "warning: multiple zones matched!\n"; | 
|---|
|  | 69 | foreach (@{$rlist}) { | 
|---|
|  | 70 | print "  bump serial on $_->{revnet} (location $_->{default_location})? "; | 
|---|
|  | 71 | my $yn = <STDIN>; | 
|---|
|  | 72 | chomp $yn; | 
|---|
|  | 73 | if (lower($yn) eq 'y') { | 
|---|
|  | 74 | $rser->execute($newser, $_->{rdns_id}); | 
|---|
|  | 75 | print "    $_->{revnet} serial bumped to $newser\n"; | 
|---|
|  | 76 | } | 
|---|
|  | 77 | } | 
|---|
|  | 78 | } elsif (scalar(@{$rlist}) == 1) { | 
|---|
|  | 79 | $rser->execute($newser, $rlist->[0]->{rdns_id}); | 
|---|
|  | 80 | print "$zone serial bumped to $newser\n"; | 
|---|
|  | 81 | } else { | 
|---|
|  | 82 | warn "error:  unknown zone '$zone'\n"; | 
|---|
|  | 83 | } | 
|---|
|  | 84 |  | 
|---|
|  | 85 | } elsif ($zone =~ /^[a-z0-9\.-]+$/) { | 
|---|
|  | 86 | # domain | 
|---|
|  | 87 | $didsth->execute($zone); | 
|---|
|  | 88 | my $dlist = $didsth->fetchall_arrayref({}); | 
|---|
|  | 89 | my $newser = scalar(time); | 
|---|
|  | 90 | if (scalar(@{$dlist}) > 1) { | 
|---|
|  | 91 | print "warning: multiple zones matched!\n"; | 
|---|
|  | 92 | foreach (@{$dlist}) { | 
|---|
|  | 93 | print "  bump serial on $_->{domain} (location $_->{default_location})? "; | 
|---|
|  | 94 | my $yn = <STDIN>; | 
|---|
|  | 95 | chomp $yn; | 
|---|
|  | 96 | if (lower($yn) eq 'y') { | 
|---|
|  | 97 | $dser->execute($newser, $_->{domain_id}); | 
|---|
|  | 98 | print "    $_->{domain} serial bumped to $newser\n"; | 
|---|
|  | 99 | } | 
|---|
|  | 100 | } | 
|---|
|  | 101 | } elsif (scalar(@{$dlist}) == 1) { | 
|---|
|  | 102 | $dser->execute($newser, $dlist->[0]->{domain_id}); | 
|---|
|  | 103 | print "$zone serial bumped to $newser\n"; | 
|---|
|  | 104 | } else { | 
|---|
|  | 105 | warn "error:  unknown zone '$zone'\n"; | 
|---|
|  | 106 | } | 
|---|
|  | 107 |  | 
|---|
|  | 108 | } else { | 
|---|
|  | 109 | # Your llama is on fire | 
|---|
|  | 110 | print "Unknown zone name/format $zone\n"; | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 | } # shift @ARGV | 
|---|