| 1 | package Net::DNS::DomainController::Discovery; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Net::DNS::DomainController::Discovery - Discover Microsoft Active Directory domain controllers via DNS queries | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.02 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Issues DNS requests to provide a list of hostnames and IP addresses of the Microsoft | ||||||||||||||||||||||||||||
| 24 | Active Directory domain controllers. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | use Net::DNS::DomainController::Discovery; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | my $foo = Net::DNS::DomainController::Discovery->domain_controllers('fabrikam.com'); | ||||||||||||||||||||||||||||
| 29 | ... | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 12 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 10 | our @EXPORT_OK = qw(domain_controllers srv_to_name srv_fqdn_list fqdn_to_ipaddr); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 12 | ▶ | use Net::DNS::Resolver; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 10 | our $TestResolver; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 srv_to_name | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Extract server name from the SRV response | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub srv_to_name { | ||||||||||||||||||||||||||||
| 49 | 10 | ▶ | my $rr = shift; | ||||||||||||||||||||||||||
|
Subroutine
srv_to_name: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 10 | ▶ | if ( ! $rr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | confess "Need Net::DNS::RR record"; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 9 | ▶ | if ( $rr->type ne 'SRV' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 2 | croak "Need Net::DNS::RR::SRV record (got \"". ${rr}->type . "\")"; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 7 | return $rr->target; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 srv_fqdn_list | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub srv_fqdn_list { | ||||||||||||||||||||||||||||
| 64 | 6 | ▶ | my ($resolver, $domain_name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
srv_fqdn_list: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 6 | my $resp = $resolver->query( $domain_name, 'SRV' ); | |||||||||||||||||||||||||||
| 66 | 6 | my @dc_name_list; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | if ( ! $resp ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 1 | croak "No SRV records in \"$domain_name\""; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 5 | @dc_name_list = map { srv_to_name($_) } $resp->answer; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 fqdn_to_ipaddr | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub fqdn_to_ipaddr { | ||||||||||||||||||||||||||||
| 79 | 10 | ▶ | my $rr = shift; | ||||||||||||||||||||||||||
|
Subroutine
fqdn_to_ipaddr: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 10 | ▶ | if ( ! $rr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | confess "Need Net::DNS::RR record"; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 9 | ▶ | if ( $rr->type ne 'A' && $rr->type ne 'AAAA' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rr->type ne "A" and $rr->type ne "AAAA"
MC/DC100%2/2
$rr->type ne "A" and $rr->type ne "AAAA"
$rr->type ne "A"
$rr->type ne "AAAA"
Truth table100%3/3
A and B
A= $rr->type ne "A"B= $rr->type ne "AAAA"
| |||||||||||||||||||||||||||||
| 84 | 3 | croak "Need Net::DNS::RR::A or AAAA record (got \"" . ${rr}->type . "\")"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 6 | return $rr->address; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 fqdn_ipaddr_list | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub fqdn_ipaddr_list { | ||||||||||||||||||||||||||||
| 94 | 5 | ▶ | my ($resolver, $fqdn) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fqdn_ipaddr_list: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 5 | my $resp = $resolver->query( $fqdn, 'A' ); | |||||||||||||||||||||||||||
| 96 | 5 | my @dc_ip_list; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 5 | ▶ | if ( ! $resp ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | croak "No A records in \"$fqdn\""; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 5 | @dc_ip_list= map { fqdn_to_ipaddr($_) } $resp->answer; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 dc_to_srv | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub dc_to_srv { | ||||||||||||||||||||||||||||
| 109 | 4 | ▶ | return '_ldap._tcp.dc._msdcs.' . $_[0] . '.' | ||||||||||||||||||||||||||
|
Subroutine
dc_to_srv: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 domain_controllers | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub domain_controllers { | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 5 | ▶ | croak "Active Directory domain name not provided ($#_)" if $#_ < 1; | ||||||||||||||||||||||||||
|
Subroutine
domain_controllers: called CC=5 SCAR=16.2
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 4 | shift; | |||||||||||||||||||||||||||
| 121 | 4 | my $domain_name = shift; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 4 | my $resolver; | |||||||||||||||||||||||||||
| 124 | 4 | ▶ | if (defined $TestResolver) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 4 | $resolver = $TestResolver; | |||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 0 | $resolver = Net::DNS::Resolver->new(); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 4 | my @dc; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | foreach my $fqdn (srv_fqdn_list( $resolver, dc_to_srv( $domain_name ))) { | |||||||||||||||||||||||||||
| 133 | 5 | foreach my $addr (fqdn_ipaddr_list( $resolver, $fqdn )) { | |||||||||||||||||||||||||||
| 134 | 4 | push @dc, [ $domain_name, $fqdn, $addr ]; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 2 | return @dc; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Marcin CIESLAK, C<< <saperski at cpan.org> >> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 BUGS | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Please report any bugs or feature requests to C<bug-net-dns-domaincontroller-discovery at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 147 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-DNS-DomainController-Discovery>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 148 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | perldoc Net::DNS::DomainController::Discovery | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | You can also look for information at: | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =over 4 | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-DNS-DomainController-Discovery> | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item * CPAN search engine | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | L<https://metacpan.org/release/Net-DNS-DomainController-Discovery> | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =back | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | This software is Copyright (c) 2020 by Marcin CIESLAK. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 10 | 1; # End of Net::DNS::DomainController::Discovery | |||||||||||||||||||||||||||