line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Checker::DNS; |
2
|
|
|
|
|
|
|
# Copyright (c) 2013-2016 Sullivan Beck. All rights reserved. |
3
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify it |
4
|
|
|
|
|
|
|
# under the same terms as Perl itself. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
############################################################################### |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require 5.008; |
9
|
1
|
|
|
1
|
|
4
|
use warnings 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
10
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
11
|
1
|
|
|
1
|
|
3
|
use Net::DNS; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
783
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our($VERSION); |
14
|
|
|
|
|
|
|
$VERSION='1.07'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
############################################################################### |
17
|
|
|
|
|
|
|
############################################################################### |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub check { |
20
|
23
|
|
|
23
|
1
|
42
|
my($obj,$element,$desc,$check_opts) = @_; |
21
|
23
|
|
|
|
|
42
|
my $err = []; |
22
|
23
|
|
|
|
|
33
|
my $warn = []; |
23
|
23
|
|
|
|
|
55
|
my $info = []; |
24
|
|
|
|
|
|
|
# 0 - 255 |
25
|
23
|
|
|
|
|
130
|
my $oct_rx = qr/([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/; |
26
|
|
|
|
|
|
|
|
27
|
23
|
100
|
|
|
|
65
|
if (! defined $check_opts) { |
28
|
2
|
|
|
|
|
10
|
$check_opts = { 'dns' => undef }; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Check to see if it's an IP |
32
|
|
|
|
|
|
|
|
33
|
23
|
|
|
|
|
301
|
my $is_hostname = ($element !~ /^$oct_rx\.$oct_rx\.$oct_rx\.$oct_rx$/); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Do the qualified check |
36
|
|
|
|
|
|
|
|
37
|
23
|
|
|
|
|
95
|
$obj->check_value($check_opts,'qualified',$element,$is_hostname, |
38
|
|
|
|
|
|
|
'Only hostnames can be check with qualified',undef, |
39
|
|
|
|
|
|
|
$err,$warn,$info); |
40
|
23
|
100
|
|
|
|
66
|
return ($element,$err,$warn,$info) if (@$err); |
41
|
|
|
|
|
|
|
|
42
|
22
|
100
|
|
|
|
60
|
if ($is_hostname) { |
43
|
21
|
|
|
|
|
86
|
my @host = split(/\./,$element); |
44
|
21
|
|
|
|
|
24
|
my($fqhost,$uqhost,$domain); |
45
|
21
|
100
|
|
|
|
63
|
if (@host == 1) { |
46
|
4
|
|
|
|
|
6
|
$uqhost = $element; |
47
|
|
|
|
|
|
|
} else { |
48
|
17
|
|
|
|
|
50
|
$fqhost = $element; |
49
|
17
|
|
|
|
|
31
|
$uqhost = shift(@host); |
50
|
17
|
|
|
|
|
46
|
$domain = join('.',@host); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
21
|
|
|
|
|
79
|
$obj->check_value($check_opts,'qualified',$element,$fqhost, |
54
|
|
|
|
|
|
|
'Host is not fully qualified', |
55
|
|
|
|
|
|
|
'Host is fully qualified', |
56
|
|
|
|
|
|
|
$err,$warn,$info); |
57
|
21
|
100
|
|
|
|
87
|
return ($element,$err,$warn,$info) if (@$err); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Set up the resolver |
61
|
|
|
|
|
|
|
|
62
|
18
|
|
|
|
|
23
|
my $res; |
63
|
18
|
|
|
|
|
60
|
my $nameservers = $obj->check_option($check_opts,'nameservers'); |
64
|
18
|
100
|
|
|
|
42
|
if ($nameservers) { |
65
|
1
|
|
|
|
|
4
|
my @nameservers = split(/\s+/,$nameservers); |
66
|
1
|
|
|
|
|
9
|
$res = Net::DNS::Resolver->new(nameservers => [@nameservers]); |
67
|
|
|
|
|
|
|
} else { |
68
|
17
|
|
|
|
|
149
|
$res = Net::DNS::Resolver->new(); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Do the dns check |
72
|
|
|
|
|
|
|
|
73
|
18
|
|
|
|
|
1227
|
my $q = $res->search($element); |
74
|
18
|
100
|
|
|
|
1504561
|
my $in_dns = ($q ? 1 : 0); |
75
|
|
|
|
|
|
|
|
76
|
18
|
|
|
|
|
147
|
$obj->check_value($check_opts,'dns',$element,$in_dns, |
77
|
|
|
|
|
|
|
'Host is not defined in DNS', |
78
|
|
|
|
|
|
|
'Host is already in DNS', |
79
|
|
|
|
|
|
|
$err,$warn,$info); |
80
|
18
|
100
|
|
|
|
112
|
return ($element,$err,$warn,$info) if (@$err); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Do the expected_* checks |
83
|
|
|
|
|
|
|
|
84
|
15
|
|
|
|
|
45
|
foreach my $check ('ip','domain','hostname') { |
85
|
41
|
|
|
|
|
97
|
my $label = "expected_$check"; |
86
|
41
|
100
|
|
|
|
119
|
next if (! $obj->check_performed($check_opts,$label)); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Get the expected value(s) |
89
|
|
|
|
|
|
|
|
90
|
7
|
|
|
|
|
14
|
my $vals; |
91
|
7
|
100
|
66
|
|
|
64
|
if (defined($desc) && |
|
|
|
66
|
|
|
|
|
92
|
|
|
|
|
|
|
ref($desc) eq 'HASH' && |
93
|
|
|
|
|
|
|
exists $$desc{$check}) { |
94
|
5
|
|
|
|
|
15
|
$vals = $$desc{$check}; |
95
|
|
|
|
|
|
|
} else { |
96
|
2
|
|
|
|
|
9
|
$vals = $obj->check_option($check_opts,'value',undef,$label); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
7
|
|
|
|
|
18
|
my %vals = (); |
100
|
7
|
50
|
|
|
|
22
|
if (defined($vals)) { |
101
|
7
|
100
|
|
|
|
31
|
if (ref($vals) eq 'ARRAY') { |
|
|
50
|
|
|
|
|
|
102
|
3
|
|
|
|
|
8
|
%vals = map { $_,1 } @$vals; |
|
4
|
|
|
|
|
24
|
|
103
|
|
|
|
|
|
|
} elsif (! ref($vals)) { |
104
|
4
|
|
|
|
|
11
|
%vals = ( $vals => 1 ); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
7
|
|
|
|
|
28
|
my @vals = keys %vals; |
109
|
7
|
50
|
|
|
|
23
|
if (! @vals) { |
110
|
0
|
|
|
|
|
0
|
die "ERROR: No value provided for expected_$check DNS check.\n"; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Test each value in DNS |
114
|
|
|
|
|
|
|
|
115
|
7
|
|
|
|
|
29
|
my @a = $q->answer(); |
116
|
7
|
|
|
|
|
53
|
foreach my $rr (@a) { |
117
|
9
|
100
|
|
|
|
59
|
next if ($rr->type ne 'A'); |
118
|
|
|
|
|
|
|
|
119
|
7
|
|
|
|
|
82
|
my $value; |
120
|
7
|
100
|
|
|
|
32
|
if ($check eq 'ip') { |
|
|
50
|
|
|
|
|
|
121
|
2
|
|
|
|
|
11
|
$value = $rr->address; |
122
|
|
|
|
|
|
|
} elsif ($check eq 'domain') { |
123
|
5
|
|
|
|
|
20
|
$value = $rr->name; |
124
|
5
|
|
|
|
|
448
|
$value =~ s/^.*?\.//; |
125
|
|
|
|
|
|
|
} else { |
126
|
0
|
|
|
|
|
0
|
$value = $rr->name; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
7
|
|
|
|
|
96
|
$obj->check_value($check_opts,$label,$element,exists $vals{$value}, |
130
|
|
|
|
|
|
|
"DNS $check value does not match expected value", |
131
|
|
|
|
|
|
|
"DNS $check value is a restricted value", |
132
|
|
|
|
|
|
|
$err,$warn,$info); |
133
|
7
|
100
|
|
|
|
109
|
return ($element,$err,$warn,$info) if (@$err); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
12
|
|
|
|
|
311
|
return ($element,$err,$warn,$info); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1; |
142
|
|
|
|
|
|
|
# Local Variables: |
143
|
|
|
|
|
|
|
# mode: cperl |
144
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
145
|
|
|
|
|
|
|
# cperl-indent-level: 3 |
146
|
|
|
|
|
|
|
# cperl-continued-statement-offset: 2 |
147
|
|
|
|
|
|
|
# cperl-continued-brace-offset: 0 |
148
|
|
|
|
|
|
|
# cperl-brace-offset: 0 |
149
|
|
|
|
|
|
|
# cperl-brace-imaginary-offset: 0 |
150
|
|
|
|
|
|
|
# cperl-label-offset: 0 |
151
|
|
|
|
|
|
|
# End: |