9 |
use CouchDB; |
use CouchDB; |
10 |
|
|
11 |
use server; |
use server; |
12 |
|
use client; |
13 |
our $debug = server::debug; |
our $debug = server::debug; |
14 |
|
|
15 |
my $res = Net::DNS::Resolver->new( |
my $res = Net::DNS::Resolver->new( |
18 |
debug => $debug, |
debug => $debug, |
19 |
); |
); |
20 |
|
|
21 |
our $ptr_cache; |
our ( $ptr_cache, $a_cache ); |
22 |
sub name_ip { |
sub name_ip { |
23 |
my ( $name, $ip ) = @_; |
my ( $name, $ip ) = @_; |
24 |
$ptr_cache->{ join('.', reverse split(/\./, $ip)) } = $name; |
$ptr_cache->{ join('.', reverse split(/\./, $ip)) } = $name; |
25 |
|
$a_cache->{$name} = $ip; |
26 |
return $ip; |
return $ip; |
27 |
} |
} |
28 |
|
|
29 |
|
name_ip 'server' => $server::ip; |
30 |
|
|
31 |
|
foreach my $ip ( client::all_ips ) { |
32 |
|
if ( my $name = client::conf( $ip => 'hostname' ) ) { |
33 |
|
name_ip $name => $ip; |
34 |
|
} |
35 |
|
} |
36 |
|
|
37 |
sub reply_handler { |
sub reply_handler { |
38 |
my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_; |
my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_; |
39 |
my ($rcode, @ans, @auth, @add); |
my ($rcode, @ans, @auth, @add); |
40 |
|
|
|
server->refresh; |
|
41 |
$debug = server::debug; |
$debug = server::debug; |
42 |
|
|
43 |
CouchDB::audit( 'request', { |
my $audit = { |
44 |
qname => $qname, |
qname => $qname, |
45 |
qclass => $qclass, |
qclass => $qclass, |
46 |
qtype => $qtype, |
qtype => $qtype, |
47 |
peerhost => $peerhost, |
peerhost => $peerhost, |
48 |
sockhost => $conn->{"sockhost"} |
sockhost => $conn->{"sockhost"}, |
49 |
}); |
source => 'unknown', |
50 |
|
}; |
51 |
|
|
52 |
$query->print if $debug; |
$query->print if $debug; |
53 |
|
|
54 |
my $local = $1 if $qname =~ m{^(.+)\.\Q$server::domain_name\E$}; |
my $local = $1 if $qname =~ m{^(.+)\.\Q$server::domain\E$}; |
55 |
$local = $qname if $qname !~ m{\.}; |
$local = $qname if $qname !~ m{\.}; |
56 |
|
|
57 |
my $ttl = 3600; |
my $ttl = 3600; |
58 |
|
|
|
my $audit = { source => 'unknown' }; |
|
|
|
|
59 |
if ( $local ) { |
if ( $local ) { |
60 |
warn "local[$local] $qname $qtype"; |
warn "local[$local] $qname $qtype"; |
61 |
$rcode = "NOERROR"; |
$rcode = "NOERROR"; |
62 |
my $rdata; |
my $rdata; |
63 |
if ( $qtype eq "A" && $local eq "server" ) { |
if ( $qtype eq "A" ) { |
64 |
$rdata = name_ip( $local, $server::ip ); |
if ( $rdata = $a_cache->{$local} ) { |
65 |
$audit->{source} = 'local'; |
$audit->{source} = 'local'; |
66 |
} else { |
} else { |
67 |
$rcode = "NXDOMAIN"; |
$rcode = "NXDOMAIN"; |
68 |
} |
warn "## no $local in ",dump( $a_cache ); |
69 |
|
} |
70 |
push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata") if $ttl; |
} elsif ( $qtype eq 'PTR' ) { |
71 |
|
$qname =~ s{\.in-addr\.arpa$}{} || warn "W: can't strip suffix from $qtype $qname"; |
72 |
} elsif ( $qtype eq 'PTR' && $qname =~ m{^([0-9\.]*)\.in-addr\.arpa$} ) { |
if ( my $rdata = $ptr_cache->{$qname} ) { |
73 |
if ( my $rdata = $ptr_cache->{$1} ) { |
$rdata .= '.' . $server::domain; |
|
$rdata .= '.' . $server::domain_name; |
|
74 |
push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata"); |
push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata"); |
75 |
$audit->{source} = 'PTR'; |
$audit->{source} = 'PTR'; |
76 |
} else { |
} else { |
77 |
warn "## ",dump( $ptr_cache ); |
warn "## no $qname in ",dump( $ptr_cache ); |
78 |
$rcode = "NXDOMAIN"; |
$rcode = "NXDOMAIN"; |
79 |
} |
} |
80 |
|
} else { |
81 |
|
$audit->{warn} = "qtype $qtype not supported"; |
82 |
|
} |
83 |
|
|
84 |
|
push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata") if $ttl; |
85 |
|
|
86 |
} elsif ( my $packet = $res->query( $qname, $qtype ) ) { |
} elsif ( my $packet = $res->query( $qname, $qtype ) ) { |
87 |
|
|
88 |
$audit->{source} = 'upstream'; |
$audit->{source} = 'upstream'; |
115 |
sub start { |
sub start { |
116 |
my $ns = Net::DNS::Nameserver->new( |
my $ns = Net::DNS::Nameserver->new( |
117 |
LocalPort => 53, |
LocalPort => 53, |
118 |
ReplyHandler => \&reply_handler, |
ReplyHandler => sub { |
119 |
|
server->refresh; |
120 |
|
reply_handler(@_); |
121 |
|
}, |
122 |
Verbose => $debug, |
Verbose => $debug, |
123 |
) || die "couldn't create nameserver object\n"; |
) || die "couldn't create nameserver object\n"; |
124 |
|
|
125 |
CouchDB::audit('start', { listen => { port => 53, domain_name => $server::domain_name } }); |
CouchDB::audit('start', { port => 53, domain => $server::domain }); |
126 |
warn "DNS $server::domain_name"; |
warn "DNS $server::domain"; |
127 |
|
|
128 |
$ns->main_loop; |
$ns->main_loop; |
129 |
} |
} |