blib/lib/Net/DNS/DomainController/Discovery.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 95.4% 20 / 22 (1 uncoverable) Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 98.1% 106 / 109 (1 uncoverable) CC 17 SCAR 13.1
CC
17
Cov
98%
domain_controllers
7.0
dc_to_srv
4.0
fqdn_to_ipaddr
4.0
CRAP 3.7
↑ Summary
1 package Net::DNS::DomainController::Discovery;
2  
3 13 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶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 1.00
15  
16 =cut
17  
18 13 our $VERSION = '1.00';
19  
20 =head1 SYNOPSIS
21  
22 Issues DNS requests to provide a list of hostnames and IP addresses of the Microsoft
23 Active Directory domain controllers.
24  
25 use Net::DNS::DomainController::Discovery;
26  
27 my $foo = Net::DNS::DomainController::Discovery::domain_controllers('fabrikam.com');
28 ...
29  
30 Multiple domain names can be specified:
31  
32 my $foo = Net::DNS::DomainController::Discovery::domain_controllers('fabrikam.com', 'contoso.com');
33  
34 This module works only if the Active Directory domain controllers are registed with the domain name system (DNS).
35  
36 =cut
37  
38 13 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 13 our @EXPORT_OK = qw(domain_controllers srv_to_name srv_fqdn_list fqdn_to_ipaddr);
40  
41 13 ▶use Net::DNS::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 13 our $TestResolver;
44  
45 =head2 domain_controllers
46  
47 Use this function to obtain a list of Active Domain controllers registered in the DNS
48 for the domain names given as arguments.
49  
50 Returns a nested array of (domain name, hostname, ip address) tuples that contain all
51 the Active Directory domain controllers serving the domain name if registered in the DNS.
52  
53 If the domain does not contain any Active Domain domain controller service records,
54 no entries for the domain are returned.
55  
56 No records are returned for the domain controller names which do resolve neither
57 to an IPv4 nor IPv6 address.
58  
59 =cut
60  
61 18 ▶sub domain_controllers {
Subroutine domain_controllers: called CC=7 SCAR=19.5
Pod documented
62  
63 18 ▶ croak "Active Directory domain name not provided" unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 17 18
64  
65 17 my $resolver;
66  
67 # uncoverable branch false
68 17 ▶ if (defined $TestResolver) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Net::DNS::DomainController::Discovery::TestResolver) { } 17 0 17
69 17 $resolver = $TestResolver;
70 } else {
71 0 $resolver = Net::DNS::Resolver->new();
72 }
73  
74 17 my @dc;
75 17 foreach my $domain_name (@_) {
76 25 foreach my $fqdn (srv_fqdn_list( $resolver, dc_to_srv( $domain_name ))) {
77 25 foreach my $addr (fqdn_ipaddr_list( $resolver, 'AAAA', $fqdn )) {
78 4 push @dc, [ $domain_name, $fqdn, $addr ];
79 }
80 24 foreach my $addr (fqdn_ipaddr_list( $resolver, 'A', $fqdn )) {
81 20 push @dc, [ $domain_name, $fqdn, $addr ];
82 }
83 }
84 }
85 9 return @dc;
86 }
87 =head1 INTERNAL SUBROUTINES
88  
89 =head2 srv_to_name
90  
91 Extract server name from the SRV response.
92  
93 =cut
94  
95 30 ▶sub srv_to_name {
Subroutine srv_to_name: called CC=3 SCAR=11.0
Pod documented
96 30 my $rr = shift;
97 30 ▶ if ( ! $rr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rr) 1 29 30
98 1 confess "Need Net::DNS::RR record";
99 }
100 29 ▶ if ( $rr->type ne 'SRV' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type ne "SRV") 2 27 29
101 2 croak "Need Net::DNS::RR::SRV record (got \"". ${rr}->type . "\")";
102 }
103 27 return $rr->target;
104 }
105  
106 =head2 srv_fqdn_list
107  
108 Query SRV records and return server names if any.
109  
110 =cut
111  
112 21 ▶sub srv_fqdn_list {
Subroutine srv_fqdn_list: called CC=2 SCAR=6.9
Pod documented
113 21 my ($resolver, $domain_name) = @_;
114 21 my $resp = $resolver->query( $domain_name, 'SRV' );
115 21 my @dc_name_list;
116  
117 21 ▶ if ( $resp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 19 2 21
118 19 return map { srv_to_name($_) } $resp->answer;
119 } else {
120 2 return ();
121 }
122 }
123  
124 =head2 fqdn_to_ipaddr
125  
126 Extract IP addresses from the resolver response.
127  
128 =cut
129  
130 30 ▶sub fqdn_to_ipaddr {
Subroutine fqdn_to_ipaddr: called CC=4 SCAR=13.9
Pod documented
131 30 my $rr = shift;
132 30 ▶ if ( ! $rr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rr) 1 29 30
133 1 confess "Need Net::DNS::RR record";
134 }
135 29 ▶ if ( $rr->type ne 'A' && $rr->type ne 'AAAA' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type ne "A" and $rr->type ne "AAAA") 3 26 29
Condition100%3/3
$rr->type ne "A" and $rr->type ne "AAAA"
!ll&&!rl&&r
2153
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"
ABresult
0X0
100
111
136 3 croak "Need Net::DNS::RR::A or AAAA record (got \"" . ${rr}->type . "\")";
137 }
138 26 return $rr->address;
139 }
140  
141 =head2 fqdn_ipaddr_list
142  
143 Resolver server names using the appropriate record for the address family requested.
144 C<$type> parameter should be set C<A> for IPv4, C<AAAA> for IPv6).
145  
146 =cut
147  
148 49 ▶sub fqdn_ipaddr_list {
Subroutine fqdn_ipaddr_list: called CC=2 SCAR=6.9
Pod documented
149 49 my ($resolver, $type, $fqdn) = @_;
150 49 my $resp = $resolver->query( $fqdn, $type );
151 49 my @dc_ip_list;
152
153 49 ▶ if ( $resp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 25 24 49
154 25 return map { fqdn_to_ipaddr($_) } $resp->answer;
155 } else {
156 return ()
157 24 }
158 }
159  
160 =head2 dc_to_srv
161  
162 Validate the domain name and add the magic string for the Active Directory domain controllers.
163  
164 =cut
165  
166 25 ▶sub dc_to_srv {
Subroutine dc_to_srv: called CC=4 SCAR=13.9
Pod documented
167 25 ▶ croak "Active Directory domain name not provided" unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 25 25
168 25 ▶ croak "Active Directory domain name not defined" unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 2 23 25
169 23 ▶ croak "Invalid domain name: \"$_[0]\"" unless $_[0] =~ /\A\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\Z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\A\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\Z/ 4 19 23
170 19 return '_ldap._tcp.dc._msdcs.' . $_[0] . '.'
171 }
172  
173 =head1 AUTHOR
174  
175 Marcin CIESLAK, C<< <saperski at cpan.org> >>
176  
177 =head1 BUGS
178  
179 Please report any bugs or feature requests to C<bug-net-dns-domaincontroller-discovery at rt.cpan.org>, or through
180 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
181 automatically be notified of progress on your bug as I make changes.
182  
183 =head1 SUPPORT
184  
185 Microsoft has a documentation how the Active Directory domain controllers should register themselves in the DNS:
186  
187 Microsoft Active Directory Technical Specifications [MS-ADTS]
188 Section 6.3.2.3 SRV Records
189 Published 14 February 2019 at L<https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/c1987d42-1847-4cc9-acf7-aab2136d6952>
190  
191 Archived on 23 March 2020: L<http://archive.today/6NUSR>
192  
193 You can find documentation for this module with the perldoc command.
194  
195 perldoc Net::DNS::DomainController::Discovery
196  
197 You can also look for information at:
198  
199 =over 4
200  
201 =item * Source code repository
202  
203 L<https://repo.or.cz/Net-DNS-DomainController-Discovery.git>
204  
205 =item * RT: CPAN's request tracker (report bugs here)
206  
207 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-DNS-DomainController-Discovery>
208  
209 =item * CPAN search engine
210  
211 L<https://metacpan.org/release/Net-DNS-DomainController-Discovery>
212  
213 =back
214  
215  
216 =head1 LICENSE AND COPYRIGHT
217  
218 This software is Copyright (c) 2020 by Marcin CIESLAK.
219  
220 This is free software, licensed under:
221  
222 The Artistic License 2.0 (GPL Compatible)
223  
224  
225 =cut
226  
227 13 1; # End of Net::DNS::DomainController::Discovery
↑ Summary