#!/usr/bin/perl
# Quickly bump the serial number on one or more zones
##
# Copyright 2019 Kris Deugau <kdeugau@deepnet.cx>
# 
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version. 
# 
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
# 
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

use strict;
use warnings;
use NetAddr::IP;
use Data::Dumper;

# push "the directory the script is in" into @INC
use FindBin;
use lib "$FindBin::RealBin/";

use DNSDB;

usage() if !@ARGV;

sub usage {
  die "usage:  bumpserial zone1 [zone2] [zone3] ...\n";
}

my $dnsdb = new DNSDB or die "Couldn't create DNSDB object: ".$DNSDB::errstr."\n";
my $dbh = $dnsdb->{dbh};

# revID() and domainID() need a location.  assuming '' likely fails.  zones SHOULD usually
# be unique irrespective of locations - we aren't big enough to need to do that (yet...)
my $didsth = $dbh->prepare("SELECT domain_id,domain,default_location FROM domains WHERE domain = ?");
my $ridsth = $dbh->prepare("SELECT rdns_id,revnet,default_location FROM revzones WHERE revnet = ?");

# do the updates by way of the ID, that way we've confirmed we've got a zone that's actually present
my $dser = $dbh->prepare("UPDATE domains SET zserial = ? WHERE domain_id = ?");
my $rser = $dbh->prepare("UPDATE revzones SET zserial = ? WHERE rdns_id = ?");

while (my $zone = shift @ARGV) {
  $zone = lc $zone;
  if ($zone =~ m,^[\d\./]+$, || $zone =~ /\.in-addr\.arpa$/) {
    # reverse zone, CIDR format or .arpa format.  ignoring non-octet CIDR ranges and ipv6 for now
    my $z;
    if ($zone =~ /\.in-addr\.arpa$/) {
      # convert to CIDR for database lookups
      $z = DNSDB::_zone2cidr($zone);
    } else {
      $z = new NetAddr::IP $zone;
    }
    if (!$z) {
      warn "error: $zone does not appear to be a valid CIDR range\n";
      next;
    }
    $ridsth->execute("$z");
    my $rlist = $ridsth->fetchall_arrayref({});
    my $newser = scalar(time);
    if (scalar(@{$rlist}) > 1) {
      print "warning: multiple zones matched!\n";
      foreach (@{$rlist}) {
        print "  bump serial on $_->{revnet} (location $_->{default_location})? ";
        my $yn = <STDIN>;
        chomp $yn;
        if (lower($yn) eq 'y') {
          $rser->execute($newser, $_->{rdns_id});
          print "    $_->{revnet} serial bumped to $newser\n";
        }
      }
    } elsif (scalar(@{$rlist}) == 1) {
      $rser->execute($newser, $rlist->[0]->{rdns_id});
      print "$zone serial bumped to $newser\n";
    } else {
      warn "error:  unknown zone '$zone'\n";
    }

  } elsif ($zone =~ /^[a-z0-9\.-]+$/) {
    # domain
    $didsth->execute($zone);
    my $dlist = $didsth->fetchall_arrayref({});
    my $newser = scalar(time);  
    if (scalar(@{$dlist}) > 1) {
      print "warning: multiple zones matched!\n";
      foreach (@{$dlist}) {
        print "  bump serial on $_->{domain} (location $_->{default_location})? ";
        my $yn = <STDIN>;
        chomp $yn;
        if (lower($yn) eq 'y') {
          $dser->execute($newser, $_->{domain_id});
          print "    $_->{domain} serial bumped to $newser\n";
        }
      }
    } elsif (scalar(@{$dlist}) == 1) {
      $dser->execute($newser, $dlist->[0]->{domain_id});
      print "$zone serial bumped to $newser\n";
    } else {
      warn "error:  unknown zone '$zone'\n";
    }

  } else {
    # Your llama is on fire
    print "Unknown zone name/format $zone\n";
  }

} # shift @ARGV
