1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
#!/usr/bin/perl -w
#
# Author: Petter Reinholdtsen <mail_redacted_for_web>
# Date: 2002-07-08
#
# Check /etc/hosts, and make sure the content matches the information
# in DNS. Lookup IP, and check if the names listed in /etc/hosts
# maches the one in DNS. It will ignore entries with '# NAGIOSIGNORE'
# at the end.
use vars qw($use_perl_resolver $debug $returnvalue $nagiosmsg);
$debug = 0;
$returnvalue = 0; # all ok
$nagiosmsg = "";
# Report missing reverse lookup. This will ignore CNAME entries
# pointing to the IP address, and report error in these cases.
$use_reverse = 0;
# Report missing forward-lookup. This will complain on private
# network IP addresses using the same name as a DNS entry.
$use_forward = 1;
eval 'use Net::DNS;';
if ($@) {
print "Using /etc/hosts\n" if $debug;
$use_perl_resolver = 0;
} else {
print "Using Net::DNS\n" if $debug;
$use_perl_resolver = 1;
}
$host = '/usr/bin/host';
# Look up ip address, and return ($error status, @DNS names, @DNS addresses)
sub dns_lookup_ext {
local $address = shift;
# Stupid Tru64 Unix give me two copies of the error messages from
# host. Throw away one of them.
close(STDERR);
print "Looking up $address using $host\n" if $debug;
my @names = ();
my @addresses = ();
my $lookup = "";
# Some versions need -i to make sure it uses reverse DNS lookup,
# and not /etc/hosts.
# This option will confuse 'host' on Irix and HP/UX, and make the
# program loop forever. Avoiding it for now [pere 2002-08-06]
for $options ("") {
open(HOST, "$host $options $address 2>&1 |")
|| die "Unable to execute host";
while (<HOST>) {
$lookup .= $_;
chomp;
print "host: $_\n" if $debug;
push(@names, lc($1)) if (/^Name: (.+)$/);
push(@names, lc($1)) if (/^Aliases: (.+)$/);
# 10.6.240.129.in-addr.arpa PTR perleporten.uio.no
push(@names, lc($1)) if (/\s+PTR\s+(.+)$/);
# spheniscus.uio.no has address 129.240.148.19
if (/^\S+ has address (\S+)$/) {
print "Match addr $1\n" if $debug;
push(@addresses, lc($1))
}
# 10.6.240.129.IN-ADDR.ARPA domain name pointer perleporten.uio.no
if (/IN-ADDR.ARPA domain name pointer\s+(.+)$/) {
print "Match name $1\n" if $debug;
push(@names, lc($1))
}
push(@addresses, $1) if (/^Address: (.+)$/);
push(@addresses, $1) if (/\s+A\s+(\d+.+)$/)
}
close(HOST);
if ($lookup =~ /Usage: /) {
# Probably unknown parameter, try again without -i
$lookup = "";
} else {
last;
}
}
return ("no/bad reply from DNS server", undef, undef)
if ($lookup !~ /domain name pointer/
&& $lookup !~ /\shas address\s/
&& $lookup !~ /\sPTR\s/
&& $lookup !~ /Name:/);
if ( $address =~ m/^\d+\.\d+\.\d+\.\d+/
&& ! grep /$address/, @addresses ) {
print "Adding $address to list of addresses\n" if $debug;
unshift(@addresses, $address) ;
}
return (undef, \@names, \@addresses);
}
sub dns_lookup_int {
my $address = shift;
print "Looking up $address using Net::DNS\n" if $debug;
my @names = ();
my @addresses = ();
my $res = new Net::DNS::Resolver;
my $query;
if ($address =~ m/\d+\.\d+\.\d+\.\d+/) {
$query = $res->query($address);
} else {
$query = $res->search($address);
}
if ($query) {
foreach $rr ($query->answer) {
print "Type: $rr->type\n" if $debug;
if ($rr->type eq "A") {
print $rr->address, " - A\n" if $debug;
push(@addresses, $rr->address);
}
if ($rr->type eq "CNAME") {
print $rr->cname, " - CNAME\n" if $debug;
push(@addresses, $rr->cname);
}
if ($rr->type eq "PTR") {
print $rr->ptrdname, " - PTR\n" if $debug;
push(@names, lc($rr->ptrdname));
}
}
return (undef, \@names, \@addresses);
}
else {
print "query failed: ", $res->errorstring, "\n" if $debug;
return ($res->errorstring, (), ());
}
}
sub dns_lookup {
my $entry = shift;
if ($use_perl_resolver) {
return dns_lookup_int($entry);
} else {
return dns_lookup_ext($entry);
}
}
sub error {
local ($level, $error) = @_;
$returnvalue = 1 if ($level =~ /^W$/ && $returnvalue <= 1);
$returnvalue = 2 if ($level =~ /^C$/);
$nagiosmsg = $nagiosmsg . "<br>" unless ($nagiosmsg =~ /^$/);
$nagiosmsg = $nagiosmsg . "$error";
}
sub is_ip_private {
my $ip = shift;
return 1 if ($ip =~ m/^10\./);
return 1 if ($ip =~ m/^192\.168\./);
return 0;
}
sub is_names_ip_matching {
local ($ip, @names) = @_;
my $level = "W";
# Ignore IPv6 addresses for now.
return if ($ip =~ m/:/);
# Ignore private network
return if (is_ip_private($ip));
my $name;
for $name (sort @names) {
if ($use_reverse) {
# Check reverse
my ($retval, $revnames) = dns_lookup($ip);
return if ( $retval ); # Ignore unknown IP addresses
if ( ! $retval && ! grep /$name/, @{$revnames} ) {
error $level, "Incorrect /etc/hosts for $ip: ".
"$name not in reverse DNS list";
}
}
if ($use_forward) {
# Check forward
my ($retval, $revnames, $forwip);
($retval, $revnames) = dns_lookup($ip);
($retval, undef, $forwip) = dns_lookup($name);
print "Forward DNS $name/$ip: ", join(" ", @{$forwip}), "\n"
if $debug;
# Ignore entry if both IP and hostname fail to resolve in DNS
return if ( ! defined $revnames && ! defined $forwip );
if ( ! grep /$ip/, @{$forwip} ) {
error $level, "Incorrect /etc/hosts for $ip: ".
"IP not in forward DNS list for '$name'";
}
}
}
}
sub check_etc_hosts {
open(HOSTS, "< /etc/hosts") || die "Unable to open /etc/hosts";
while (<HOSTS>) {
chomp;
next if (/# NAGIOSIGNORE$/); # Skip lines marked to be ignored
s/\#.+//; # Skip comments
next if (/^\s*$/); # Skip empty lines
print "Testing $_\n" if $debug;
$_ = lc($_);
local ($ip, @names) = split(/\s+/);
# Skip localhost, it is different on some platforms.
next if ($ip eq '127.0.0.1');
is_names_ip_matching($ip, @names);
}
close(HOSTS);
}
check_etc_hosts() if ( -f "/etc/hosts" );
if ($nagiosmsg =~ /^$/) {
print "/etc/hosts OK\n";
} else {
print $nagiosmsg . "\n";
}
exit $returnvalue;
|