# HG changeset patch # User Heiko Schlittermann (JUMPER) # Date 1422485050 -3600 # Node ID 0d9901b561f724296fd76ab4f2f69e7c9b2b65e2 # Parent 0c4905e5d01af226e98062d530aac3a73a3ba36f Now we can edit reverse zones! diff -r 0c4905e5d01a -r 0d9901b561f7 bin/dnsvi --- a/bin/dnsvi Wed Jan 28 12:51:17 2015 +0100 +++ b/bin/dnsvi Wed Jan 28 23:44:10 2015 +0100 @@ -39,10 +39,11 @@ && @ARGV >= 1 or pod2usage(); - my $zone = shift @ARGV; + my %auth = get_auth_info shift @ARGV; + my $zone = $auth{name}; $o{server} = - $o{local} ? 'localhost' : (split ' ', `dig +short soa $zone`)[0] + $o{local} ? 'localhost' : $auth{mname} if not defined $o{server}; my @dig = ( diff -r 0c4905e5d01a -r 0d9901b561f7 lib/DNS/Vi.pm --- a/lib/DNS/Vi.pm Wed Jan 28 12:51:17 2015 +0100 +++ b/lib/DNS/Vi.pm Wed Jan 28 23:44:10 2015 +0100 @@ -5,14 +5,14 @@ use if $ENV{DEBUG} // '' eq 'dnsvi' => 'Smart::Comments'; use Digest::SHA qw(sha512_hex); use File::Temp; +use Net::DNS; use Term::ReadKey; use base 'Exporter'; use if $] >= 5.020, experimental => 'smartmatch'; -#no if $warnings::Offset{'experimental'} => (warnings => 'experimental'); - -our @EXPORT = qw(ttl2h h2ttl parse delta nice edit update show get_key); +our @EXPORT = qw(ttl2h h2ttl parse delta nice edit update show get_key + get_auth_info); our @EXPORT_OK = (); # the sort order for the records of the same label @@ -245,7 +245,7 @@ 'answer', ); if ($arg{-dry}) { - return say join "\n", '', @cmds, '' if $arg{-dry}; + return say join "\n", '', @cmds, '' if $arg{-dry}; } my @nsupdate = ( 'nsupdate', @@ -281,4 +281,26 @@ return $x; } +sub get_auth_info { + my $name = shift; + my %auth = (zone => undef, master => undef); + state $resolver = Net::DNS::Resolver->new; + my $response = $resolver->send($name, 'SOA') + or die $resolver->errorstring, "\n"; + +# use Data::Dumper; +# die Dumper $response; + + if (my @soa = grep { $_->type eq 'SOA' } $response->answer, $response->authority) { + die "got multiple soa records\n" if @soa > 1; + my $soa = $soa[0]; + return ( + name => $soa->name, + mname => $soa->mname, + ) + } + + return $response->authority; +} + 1; diff -r 0c4905e5d01a -r 0d9901b561f7 t/20-reverse-dns.t --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/t/20-reverse-dns.t Wed Jan 28 23:44:10 2015 +0100 @@ -0,0 +1,61 @@ +#! perl + +use 5.010; +use Test::More; +use strict; +use warnings; +use File::Basename; +use Data::Dumper; + +use_ok 'DNS::Vi' or BAIL_OUT 'DNS::Vi not found!'; +can_ok 'DNS::Vi', qw(get_auth_info); + +is_deeply { get_auth_info('schlittermann.de') }, + { + name => 'schlittermann.de', + mname => 'pu.schlittermann.de' + } => 'schlittermann.de'; + +is_deeply { get_auth_info('kx.schlittermann.de') }, + { + name => 'kx.schlittermann.de', + mname => 'pu.schlittermann.de' + } => 'kx.schlittermann.de'; + +is_deeply { get_auth_info('ssl.schlittermann.de') }, + { + name => 'schlittermann.de', + mname => 'pu.schlittermann.de' + } => 'ssl.schlittermann.de'; + +is_deeply { get_auth_info('www.schlittermann.de') }, + { + name => 'schlittermann.de', + mname => 'pu.schlittermann.de' + } => 'www.schlittermann.de'; + +is_deeply { get_auth_info('no-such-domain.de') }, + { + name => 'de', + mname => 'f.nic.de', + } => 'no-such-domain.de'; + +is_deeply { get_auth_info('212.80.235.130') }, + { + name => '128-191.235.80.212.in-addr.arpa', + mname => 'pu.schlittermann.de' + } => '212.80.235.130'; + +is_deeply { get_auth_info('212.80.235.135') }, + { + name => '128-191.235.80.212.in-addr.arpa', + mname => 'pu.schlittermann.de' + } => '212.80.235.135'; + +is_deeply { get_auth_info('2001:470:72aa:8::12') }, + { + name => 'a.a.2.7.0.7.4.0.1.0.0.2.ip6.arpa', + mname => 'pu.schlittermann.de' + } => '2001:470:72aa:8::12'; + +done_testing;