« blib/lib/App/Netdisco/Web/Plugin/Device/Ports.pm
↑ Summary
blib/lib/App/Netdisco/Transport/SNMP.pm »
| 1 | package App::Netdisco::Util::DNS; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Dancer ':script'; | ||
| 6 | |||
| 7 | use Net::DNS; | ||
| 8 | use Scalar::Util qw/blessed reftype/; | ||
| 9 | use NetAddr::IP::Lite ':lower'; | ||
| 10 | |||
| 11 | use base 'Exporter'; | ||
| 12 | our @EXPORT = (); | ||
| 13 | our @EXPORT_OK = qw/hostname_from_ip ipv4_from_hostname/; | ||
| 14 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | App::Netdisco::Util::DNS | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | A set of helper subroutines to support parts of the Netdisco application. | ||
| 23 | |||
| 24 | There are no default exports, however the C<:all> tag will export all | ||
| 25 | subroutines. | ||
| 26 | |||
| 27 | =head1 EXPORT_OK | ||
| 28 | |||
| 29 | =head2 hostname_from_ip( $ip, \%opts? ) | ||
| 30 | |||
| 31 | Given an IP address (either IPv4 or IPv6), return the canonical hostname. | ||
| 32 | |||
| 33 | C<< %opts >> can override the various timeouts available in | ||
| 34 | L<Net::DNS::Resolver>: | ||
| 35 | |||
| 36 | =over 4 | ||
| 37 | |||
| 38 | =item C<tcp_timeout>: 120 (seconds) | ||
| 39 | |||
| 40 | =item C<udp_timeout>: 30 (seconds) | ||
| 41 | |||
| 42 | =item C<retry>: 4 (attempts) | ||
| 43 | |||
| 44 | =item C<retrans>: 5 (timeout) | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | Returns C<undef> if no PTR record exists for the IP. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub hostname_from_ip { | ||
| 53 | my ($ip, $opts) = @_; | ||
| 54 | return unless $ip; | ||
| 55 | my $skip = setting('dns')->{'no'}; | ||
| 56 | my $ETCHOSTS = setting('dns')->{'ETCHOSTS'}; | ||
| 57 | |||
| 58 | return if check_acl_no_ipaddr_only($ip, $skip); | ||
| 59 | |||
| 60 | # check /etc/hosts file and short-circuit if found | ||
| 61 | foreach my $name (reverse sort keys %$ETCHOSTS) { | ||
| 62 | if ($ETCHOSTS->{$name}->[0]->[0] eq $ip) { | ||
| 63 | return $name; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | my $res = Net::DNS::Resolver->new; | ||
| 68 | $res->tcp_timeout($opts->{tcp_timeout} || 120); | ||
| 69 | $res->udp_timeout($opts->{udp_timeout} || 30); | ||
| 70 | $res->retry($opts->{retry} || 4); | ||
| 71 | $res->retrans($opts->{retrans} || 5); | ||
| 72 | my $query = $res->search($ip); | ||
| 73 | |||
| 74 | if ($query) { | ||
| 75 | foreach my $rr ($query->answer) { | ||
| 76 | next unless $rr->type eq "PTR"; | ||
| 77 | return $rr->ptrdname; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | return undef; | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 ipv4_from_hostname( $name ) | ||
| 85 | |||
| 86 | Given a host name will return the first IPv4 address. | ||
| 87 | |||
| 88 | Returns C<undef> if no A record exists for the name. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub ipv4_from_hostname { | ||
| 93 | my $name = shift; | ||
| 94 | return undef unless $name; | ||
| 95 | my $ETCHOSTS = setting('dns')->{'ETCHOSTS'}; | ||
| 96 | |||
| 97 | # check /etc/hosts file and short-circuit if found | ||
| 98 | if (exists $ETCHOSTS->{$name} and $ETCHOSTS->{$name}->[0]->[0]) { | ||
| 99 | my $ip = NetAddr::IP::Lite->new($ETCHOSTS->{$name}->[0]->[0]); | ||
| 100 | return $ip->addr if $ip and $ip->bits == 32; | ||
| 101 | } | ||
| 102 | |||
| 103 | my $res = Net::DNS::Resolver->new; | ||
| 104 | my $query = $res->search($name); | ||
| 105 | |||
| 106 | if ($query) { | ||
| 107 | foreach my $rr ($query->answer) { | ||
| 108 | next unless $rr->type eq "A"; | ||
| 109 | return $rr->address; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | return undef; | ||
| 114 | } | ||
| 115 | |||
| 116 | # to avoid circular dependency with App::Netdisco::Util::Permission | ||
| 117 | # supports IP addresses and CIDR blocks only | ||
| 118 | |||
| 119 | # as %IN_GROUP in Util::Permission: a group that refers back to itself | ||
| 120 | # matches nothing where it recurs | ||
| 121 | our %IN_GROUP; | ||
| 122 | |||
| 123 | sub check_acl_no_ipaddr_only { | ||
| 124 | my ($thing, $config) = @_; | ||
| 125 | return 0 unless defined $thing and defined $config; | ||
| 126 | |||
| 127 | my $real_ip = $thing; | ||
| 128 | if (blessed $thing) { | ||
| 129 | $real_ip = ($thing->can('alias') ? $thing->alias : ( | ||
| 130 | $thing->can('ip') ? $thing->ip : ( | ||
| 131 | $thing->can('addr') ? $thing->addr : $thing ))); | ||
| 132 | } | ||
| 133 | return 0 if !defined $real_ip | ||
| 134 | or blessed $real_ip; # class we do not understand | ||
| 135 | |||
| 136 | $config = [$config] if ref '' eq ref $config; | ||
| 137 | if (ref [] ne ref $config) { | ||
| 138 | error "error: acl is not a single item or list (cannot compare to $real_ip)"; | ||
| 139 | return 0; | ||
| 140 | } | ||
| 141 | my $all = (scalar grep {$_ eq 'op:and'} @$config); | ||
| 142 | |||
| 143 | # common case of using plain IP in ACL, so string compare for speed | ||
| 144 | my $find = (scalar grep {not reftype $_ and $_ eq $real_ip} @$config); | ||
| 145 | return 1 if $find and not $all; | ||
| 146 | |||
| 147 | my $addr = NetAddr::IP::Lite->new($real_ip) or return 0; | ||
| 148 | |||
| 149 | INLIST: foreach (@$config) { | ||
| 150 | my $item = $_; # must copy so that we can modify safely | ||
| 151 | next INLIST if !defined $item or $item eq 'op:and'; | ||
| 152 | |||
| 153 | my $neg = ($item =~ s/^!//); | ||
| 154 | |||
| 155 | if ($item =~ m/^group:(.+)$/) { | ||
| 156 | my $group = $1; | ||
| 157 | # an unknown group matches nothing; reading must not write into config | ||
| 158 | my $rules = (setting('host_groups') || {})->{$group} || []; | ||
| 159 | |||
| 160 | my $match = 0; | ||
| 161 | if (not $IN_GROUP{$group}) { | ||
| 162 | local $IN_GROUP{$group} = 1; | ||
| 163 | $match = check_acl_no_ipaddr_only($thing, $rules); | ||
| 164 | } | ||
| 165 | |||
| 166 | if ($neg xor $match) { | ||
| 167 | return 1 if not $all; | ||
| 168 | } | ||
| 169 | else { | ||
| 170 | return 0 if $all; | ||
| 171 | } | ||
| 172 | next INLIST; | ||
| 173 | } | ||
| 174 | |||
| 175 | if ($item =~ m/[:.]([a-f0-9]+)-([a-f0-9]+)$/i) { | ||
| 176 | my $first = $1; | ||
| 177 | my $last = $2; | ||
| 178 | |||
| 179 | if ($item =~ m/:/) { | ||
| 180 | next INLIST if $addr->bits != 128 and not $all; | ||
| 181 | |||
| 182 | $first = hex $first; | ||
| 183 | $last = hex $last; | ||
| 184 | |||
| 185 | (my $header = $item) =~ s/:[^:]+$/:/; | ||
| 186 | foreach my $part ($first .. $last) { | ||
| 187 | my $ip = NetAddr::IP::Lite->new($header . sprintf('%x',$part) . '/128') | ||
| 188 | or next; | ||
| 189 | if ($neg xor ($ip == $addr)) { | ||
| 190 | return 1 if not $all; | ||
| 191 | next INLIST; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | return 0 if (not $neg and $all); | ||
| 195 | return 1 if ($neg and not $all); | ||
| 196 | } | ||
| 197 | else { | ||
| 198 | next INLIST if $addr->bits != 32 and not $all; | ||
| 199 | |||
| 200 | (my $header = $item) =~ s/\.[^.]+$/./; | ||
| 201 | foreach my $part ($first .. $last) { | ||
| 202 | my $ip = NetAddr::IP::Lite->new($header . $part . '/32') | ||
| 203 | or next; | ||
| 204 | if ($neg xor ($ip == $addr)) { | ||
| 205 | return 1 if not $all; | ||
| 206 | next INLIST; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | return 0 if (not $neg and $all); | ||
| 210 | return 1 if ($neg and not $all); | ||
| 211 | } | ||
| 212 | next INLIST; | ||
| 213 | } | ||
| 214 | |||
| 215 | # could be something in error, and IP/host is only option left | ||
| 216 | next INLIST if ref $item; | ||
| 217 | |||
| 218 | my $ip = NetAddr::IP::Lite->new($item) | ||
| 219 | or next INLIST; | ||
| 220 | next INLIST if $ip->bits != $addr->bits and not $all; | ||
| 221 | |||
| 222 | if ($neg xor ($ip->contains($addr))) { | ||
| 223 | return 1 if not $all; | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | return 0 if $all; | ||
| 227 | } | ||
| 228 | next INLIST; | ||
| 229 | } | ||
| 230 | |||
| 231 | return ($all ? 1 : 0); | ||
| 232 | } | ||
| 233 | |||
| 234 | 1; |