[nsd-users] Generating PTR records?

Guus Zijlstra, Zinias B.V. guus.zijlstra at zinias.nl
Tue Jul 28 12:19:05 UTC 2020


Hello Mark,

Also coming from djbdns I wrote a simple perl script years ago.
It is probably not without issues, but it does the trick for me.

Regards,
Guus.


On 7/26/20 8:56 PM, Mark Raynsford via nsd-users wrote:
> Hello!
>
> In years past, I used djbdns [0]. I eventually migrated to nsd after
> over a decade of djbdns as djbdns was no longer being maintained. One
> thing I did like about djbdns was that I never had to write my own PTR
> records; creating an A or an AAAA record automatically generated a
> corresponding PTR record.
>
> We obviously don't have this functionality in nsd (at least as far as
> I'm aware!), but has anyone put together a tool that can generate a
> *.in-addr.arpa.zone file given a zone file as input? I could write one
> myself, but I'd prefer not to if someone else already has.
>
> [0] http://cr.yp.to/djbdns.html
>
>
> _______________________________________________
> nsd-users mailing list
> nsd-users at lists.nlnetlabs.nl
> https://lists.nlnetlabs.nl/mailman/listinfo/nsd-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nlnetlabs.nl/pipermail/nsd-users/attachments/20200728/d7f0e5bb/attachment.htm>
-------------- next part --------------
#!/usr/bin/perl -ws

use vars qw/ $h $q $t $u $v /;
use Data::Dumper;

$v ||= 0;
my $marker = 'end-of-header';

my $usage = "Usage: $0 [ -h ] [ -u ] <forward zone file> <reverse zone file>";

if ( $h ) {
  die "
$usage

#
# This is a crude attempt at converting an nsd forward zone to a reverse equivalent.
#
# The script accepts two arguments: a forward zone file and a reverse zone file.
# Both files must exist.
#
# The reverse zone file must contain the normal header, followed
# by a line containing the marker string '$marker'.
#
# The reverse records will replace whatever comes after the marker line.
#
# Aactions
#  -u   replace the reverse file with the new contents
#
# Options
#  -h   print this help
#  -q   be quiet
#  -v   print revFile on stderr
#
# Files without marker string will not be updated.
#
#


"
}

die "$usage\n" unless @ARGV;
my $undefined = "<undefined>\n";

my ( $zoneFile, $revFile ) = @ARGV;

my @rev;
my @revs = ("\n");
my @revHeader;
my %ipSeen = ();
my $bailywick;
my $bailywickPattern;

{
  open(REV, $revFile) || die "cannot open '$revFile': $@";
  my $markerSeen = 0;
  while ($_=<REV>) {
    if ( /^\s*(\S+)\s+IN\s+SOA\s+/ ) {
      ( $bailywick ) = ( $1 );
      $bailywickPattern = ".".$bailywick."\$";
      $bailywickPattern =~ s/\./\\./g;
      warn "Zone bailywick: $bailywick (/$bailywickPattern/)\n";
    }

    if ( !$markerSeen ) {
      push(@revHeader, $_);
      $markerSeen++ if /end-of-header/;
      $markerSeen++ if /$marker/;
    }
  }
  close(REV);
  die "no marker line (/$marker/) seen in revFile $revFile\n" unless $markerSeen;
}
print @revHeader, @revs if !$u || $v;

open(IN, $zoneFile) || die "cannot open '$zoneFile': $@";
while ($_=<IN>) {
  s/;.*$//; # strip comments

  if ( /^\s*(\S+)\s+IN\s+A\s+(\S+)\s+/ ) {
    ( $name, $ip ) = ( $1, $2 );

    # double check ip usage
    warn "warning: $name uses $ip, which is already used by ".$ipSeen{$ip}."\n" if $ipSeen{$ip};
    $ipSeen{$ip} = $name;

    my $reversed_ip = reverse_ip($ip);
    my $arpa = "$reversed_ip.in-addr.arpa.";
    if ( $arpa =~ /$bailywickPattern/ ) {
      push @revs, "$arpa PTR $name\n" unless $name eq '*';
    }
    else {
      warn "SKIP: $arpa -- is outside bailywick $bailywick\n";
    }
  }
}

if ( $u ) {
  open(OUT, ">".$revFile) || die "cannot open > '$revFile': $@";
  print OUT @revHeader, sort @revs;
  warn "updated $revFile\n" unless $q;
}
else {
  warn "test run: $revFile is not updated\n" unless $q;
}

exit;

sub reverse_ip {
  my ( $ip ) = @_;
  my $reversed = "";
  while ( $ip =~ s/.?\b(\d+)$// ) {
    $reversed .= $1 . ".";
  }
  $reversed =~ s/\.$//;
  return $reversed
}



More information about the nsd-users mailing list