| 1 | package Net::DHCP::DDNS::Lookup; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | use base 'Net::DNS::Resolver'; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use Net::IP; | ||
| 9 | |||
| 10 | # TODO: implement automatic authority chasing | ||
| 11 | # * is the reply authoritative | ||
| 12 | # * ask for SOA data inline? | ||
| 13 | # locating master name servers | ||
| 14 | |||
| 15 | # reply object cache structure | ||
| 16 | # | ||
| 17 | # $self->{DNSREPLY}->{$domain}->{$class} = { | ||
| 18 | # packet => Net::DNS::Packet, | ||
| 19 | # expires => last_valid_epoch, | ||
| 20 | # records => [ Net::DNS::RR, ... ] | ||
| 21 | # }; | ||
| 22 | # | ||
| 23 | |||
| 24 | my %DNSREPLY; | ||
| 25 | |||
| 26 | sub _populate { | ||
| 27 | my ( $self, $domain, $class ) = @_; | ||
| 28 | |||
| 29 | $self = __PACKAGE__->new unless ref( $self ); | ||
| 30 | |||
| 31 | my $packet = $self->SUPER::send( $domain, 'ANY', $class ) | ||
| 32 | or croak "DNS problem: ", $self->errorstring, "\n"; | ||
| 33 | |||
| 34 | unless ( $packet->header->ancount ) { | ||
| 35 | $self->evict( $domain, $class ); | ||
| 36 | return $packet; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $expires = 4000000000; # FIXME real upper limit? | ||
| 40 | |||
| 41 | my $now = time; | ||
| 42 | |||
| 43 | grep { my $e = $now + $_->ttl; | ||
| 44 | $expires = $e if $e < $expires; | ||
| 45 | } $packet->answer; | ||
| 46 | |||
| 47 | $DNSREPLY{$domain}->{$class} = { | ||
| 48 | packet => $packet, | ||
| 49 | expires => $expires | ||
| 50 | }; | ||
| 51 | |||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _fetch { | ||
| 56 | my ( $self, $domain, $class ) = @_; | ||
| 57 | |||
| 58 | return undef unless my $tmp1 = \%DNSREPLY; | ||
| 59 | return undef unless my $tmp2 = $tmp1->{$domain}; | ||
| 60 | return undef unless my $tmp3 = $tmp2->{$class}; | ||
| 61 | |||
| 62 | return $tmp3; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _is_valid { | ||
| 66 | my ( $self, $domain, $class ) = @_; | ||
| 67 | |||
| 68 | return 0 unless my $tmp = _fetch( $self, $domain, $class ); | ||
| 69 | return 0 unless my $exp = $tmp->{expires}; | ||
| 70 | |||
| 71 | return ( $exp < time ) ? 0 : 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _normalize_send_args(@) { | ||
| 75 | my ( $arg, $rrtype, $class ) = @_; | ||
| 76 | |||
| 77 | return if ref( $arg ); | ||
| 78 | |||
| 79 | my $ip = new Net::IP $arg; | ||
| 80 | my $domain = $ip ? $ip->reverse_ip : $arg; | ||
| 81 | |||
| 82 | $rrtype ||= 'PTR'; | ||
| 83 | $class ||= 'IN'; | ||
| 84 | |||
| 85 | return ( $domain, $rrtype, $class ); | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | # three call modes: | ||
| 90 | # ( packetobject ) | ||
| 91 | # ( domain [ type [ class ] ] ) | ||
| 92 | # ( address ) | ||
| 93 | # | ||
| 94 | # packetobject calls are not cached, all others are | ||
| 95 | # | ||
| 96 | sub send { | ||
| 97 | my $self = shift; | ||
| 98 | |||
| 99 | $self = __PACKAGE__->new unless ref( $self ); | ||
| 100 | |||
| 101 | my ( $domain, $rrtype, $class ) = _normalize_send_args( @_ ) | ||
| 102 | or return $self->SUPER::send( shift );; | ||
| 103 | |||
| 104 | _populate( $self, $domain, $class ) | ||
| 105 | unless _is_valid( $self, $domain, $class ); | ||
| 106 | |||
| 107 | return $self->cached_packet( $domain, $rrtype, $class ); | ||
| 108 | } | ||
| 109 | |||
| 110 | # | ||
| 111 | # arguments same as send() | ||
| 112 | # returns a copy of the packet object | ||
| 113 | # if rrtype != ANY, filters the answers | ||
| 114 | # | ||
| 115 | sub cached_packet { | ||
| 116 | my $self = shift; | ||
| 117 | |||
| 118 | my ( $domain, $rrtype, $class ) = _normalize_send_args( @_ ) | ||
| 119 | or return undef; | ||
| 120 | |||
| 121 | return undef unless my $tmp = _fetch( $self, $domain, $class ); | ||
| 122 | |||
| 123 | my $d = $tmp->{packet}->data; | ||
| 124 | my $r = new Net::DNS::Packet \$d; | ||
| 125 | |||
| 126 | my $i = 0; | ||
| 127 | foreach my $rr ( @{$r->{answer}} ) { | ||
| 128 | |||
| 129 | if ( $rr->type eq $rrtype ) { | ||
| 130 | $i++; | ||
| 131 | } | ||
| 132 | |||
| 133 | else { | ||
| 134 | splice @{$r->{answer}}, $i, 1; | ||
| 135 | } | ||
| 136 | |||
| 137 | } | ||
| 138 | |||
| 139 | unless ( $rrtype eq 'ANY' ) { | ||
| 140 | foreach my $q ( @{$r->{question}} ) { | ||
| 141 | $q->{qtype} = $rrtype; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | return $r; | ||
| 146 | } | ||
| 147 | |||
| 148 | # arguments same as send() | ||
| 149 | # returns a list of Net::DNS::RR | ||
| 150 | # if rrtype != ANY, filters the answers | ||
| 151 | sub cached_answer { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | my ( $domain, $rrtype, $class ) = _normalize_send_args( @_ ) | ||
| 155 | or return undef; | ||
| 156 | |||
| 157 | return undef unless my $tmp = _fetch( $self, $domain, $class ); | ||
| 158 | |||
| 159 | return @{$tmp->{packet}->{answer}} if $rrtype eq 'ANY'; | ||
| 160 | |||
| 161 | return grep { $_->type eq $rrtype } @{$tmp->{packet}->{answer}}; | ||
| 162 | } | ||
| 163 | |||
| 164 | # mark the domain cache record as expired | ||
| 165 | sub invalidate { | ||
| 166 | my ( $self, $domain, $class ) = @_; | ||
| 167 | |||
| 168 | return unless $domain; | ||
| 169 | $class ||= 'IN'; | ||
| 170 | |||
| 171 | return unless my $tmp = _fetch( $self, $domain, $class ); | ||
| 172 | return unless $tmp->{expires}; | ||
| 173 | |||
| 174 | $tmp->{expires} = 0; | ||
| 175 | |||
| 176 | return; | ||
| 177 | } | ||
| 178 | |||
| 179 | # delete the domain cache record entirely | ||
| 180 | sub evict { | ||
| 181 | my ( $self, $domain, $class ) = @_; | ||
| 182 | |||
| 183 | return unless $domain; | ||
| 184 | $class ||= 'IN'; | ||
| 185 | |||
| 186 | return unless my $tmp1 = \%DNSREPLY; | ||
| 187 | return unless my $tmp2 = $tmp1->{$domain}; | ||
| 188 | return unless my $tmp3 = $tmp2->{$class}; | ||
| 189 | |||
| 190 | delete $tmp2->{$class}; | ||
| 191 | delete $tmp1->{$domain} unless scalar( %$tmp2 ); | ||
| 192 | # delete $self->{DNSREPLY} unless scalar( %$tmp1 ); | ||
| 193 | |||
| 194 | return; | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | # locate the SOA | ||
| 199 | sub find_soa { | ||
| 200 | my ( $self, $dname ) = @_; | ||
| 201 | |||
| 202 | $self = __PACKAGE__->new unless ref( $self ); | ||
| 203 | |||
| 204 | $dname .= '.' unless $dname =~ /\.$/; | ||
| 205 | |||
| 206 | while ( $dname ) { | ||
| 207 | #warn "domain: $dname\n"; | ||
| 208 | my $rep = $self->send( $dname, 'SOA' ); | ||
| 209 | |||
| 210 | unless ( $rep and $rep->header->ancount ) { | ||
| 211 | continue unless $dname =~ /\./; | ||
| 212 | $dname =~ s/[^\.]+\.//; | ||
| 213 | next; | ||
| 214 | } | ||
| 215 | |||
| 216 | foreach my $rr ( ($rep->answer), ($rep->authority) ) { | ||
| 217 | next unless $rr->type eq 'SOA'; | ||
| 218 | return $rr; | ||
| 219 | } | ||
| 220 | |||
| 221 | continue unless $dname =~ /\./; | ||
| 222 | $dname =~ s/[^\.]+\.//; | ||
| 223 | |||
| 224 | } | ||
| 225 | |||
| 226 | croak "DNS error finding SOA for $dname: ", $self->errorstring; | ||
| 227 | |||
| 228 | } | ||
| 229 | |||
| 230 | # rfc1464 key=value pairs in dns | ||
| 231 | sub getattrbyname { | ||
| 232 | my ( $self, $dname, $attrname ) = @_; | ||
| 233 | my @rv; | ||
| 234 | |||
| 235 | $self = __PACKAGE__->new unless ref( $self ); | ||
| 236 | |||
| 237 | $dname .= '.' unless $dname =~ /\.$/; | ||
| 238 | |||
| 239 | $attrname = lc( $attrname ); | ||
| 240 | |||
| 241 | my $rep = $self->send( $dname, 'TXT' ) | ||
| 242 | or return undef; | ||
| 243 | |||
| 244 | foreach my $rr ( $rep->answer ) { | ||
| 245 | next unless $rr->type eq 'TXT'; | ||
| 246 | my $t = $rr->txtdata; | ||
| 247 | |||
| 248 | my ( $key, $value ) = $self->parse_attribute( $t ); | ||
| 249 | |||
| 250 | if ( $key eq $attrname ) { | ||
| 251 | push @rv, $value; | ||
| 252 | } | ||
| 253 | |||
| 254 | } | ||
| 255 | |||
| 256 | return @rv; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub parseattribute { | ||
| 260 | my ( $self, $t ) = @_; | ||
| 261 | my ( $key, $value, $have_key ); | ||
| 262 | |||
| 263 | $t =~ s/\\\\/\\/g; | ||
| 264 | $t =~ s/\\"/"/g; | ||
| 265 | |||
| 266 | next unless $t =~ /[^`]=/; | ||
| 267 | next if $t =~ /^=/; | ||
| 268 | |||
| 269 | foreach my $p ( split /=/, $t ) { | ||
| 270 | |||
| 271 | if ( $have_key ) { | ||
| 272 | $value .= '=' if $value; | ||
| 273 | $value .= $p; | ||
| 274 | next; | ||
| 275 | } | ||
| 276 | |||
| 277 | if ( $p =~ /`$/ ) { | ||
| 278 | $p =~ s/`$//; | ||
| 279 | $key .= '=' if $key; | ||
| 280 | $key .= $p; | ||
| 281 | next; | ||
| 282 | } | ||
| 283 | |||
| 284 | $key .= '=' if $key; | ||
| 285 | $key .= $p; | ||
| 286 | |||
| 287 | $have_key = 1; | ||
| 288 | |||
| 289 | } | ||
| 290 | |||
| 291 | $key = lc( $key ); | ||
| 292 | $key =~ s/^\s+//; | ||
| 293 | $key =~ s/\s+$//; | ||
| 294 | $key =~ s/``/`/g; | ||
| 295 | $key =~ s/`=/=/g; | ||
| 296 | $key =~ s/` / /g; | ||
| 297 | $key =~ s/` / /g; | ||
| 298 | $key = lc( $key ); | ||
| 299 | |||
| 300 | return ( $key, $value ); | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | 1; |