-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfind-lamers.php
154 lines (137 loc) · 3.72 KB
/
find-lamers.php
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
<?php
require 'inc/lib.inc';
include 'header.php';
$html_top = '
<H1>Detect lame delegations</H1>
';
$html_bottom = '
</DIV>
</BODY>
</HTML>
';
$entrance_prologue = '
This tool detects lame delegations on our own DNS servers.
In order to do so, we need the hostname (or IP address) of
a DNS server <em>outside</em> of your network. The default
will usually be a sensible choice. Specifying a server
controlled through this database will accomplish exactly
nothing.<P>
<B>NB:</B> This will take a long time. Have patience.<P>
';
$entrance_body = '
<FORM action="find-lamers.php" method="post">
<INPUT type=text size=32 name=nameserver value="%s">
<INPUT type=submit value="Start">
</FORM>
';
$search_prologue = '
We manage these domains, but they do not appear to be
delegated to any of our DNS servers. Hence, some sort
of intervention is probably appropriate. <P>
';
$search_epilogue = '
<P><HR><P>Completed.<P>
Found %s lame delegations.<P>
';
function end_domain($begin, $domainform, $serverform, $end, $noservers, &$counter)
{
global $current_domain, $name_servers, $servers;
$matches = 0;
$row = "";
$result = "";
foreach($name_servers as $ns) {
$row .= sprintf($serverform, $ns);
if (isset($servers[$ns]))
$matches++;
}
if (!$matches) {
$counter++;
$dom = sprintf($domainform, $current_domain);
if (count($name_servers))
$result = $begin.$dom.$row.$end;
else
$result = $begin.$dom.$noservers.$end;
}
$current_domain = "";
return $result;
}
function begin_domain($domain)
{
global $current_domain, $name_servers;
$name_servers = array();
if ($current_domain)
end_domain();
$current_domain = $domain;
}
function domain_name_server($nameserver)
{
global $name_servers;
$name_servers[] = strtolower(ltrim(trim($nameserver)));
}
function entrance_page($text = "")
{
$db = new DB_probind;
global $html_top, $entrance_prologue, $entrance_body, $html_bottom;
$db->query("SELECT value FROM blackboard WHERE name = 'default_external_dns'");
$default_resolver = $db->next_record() ? $db->Record[0] : "8.8.8.8";
print $html_top;
print $entrance_prologue;
print $text;
print sprintf($entrance_body, $default_resolver);
print $html_bottom;
exit;
}
function initialize_servers()
{
$db = new DB_probind;
global $servers;
$db->query("SELECT hostname FROM servers");
while ($db->next_record()) {
$servers[$db->Record[0]] = 1;
}
}
#
# MAIN
#
get_input();
if (!$INPUT_VARS['nameserver']) {
entrance_page();
} else {
$tmp = strtolower(ltrim(rtrim($INPUT_VARS['nameserver'])));
if (ereg($DOMAIN_RE, $tmp)
&& ((gethostbyname($tmp) != $tmp) || valid_ip($tmp)))
$nameserver = $tmp;
else
entrance_page("Invalid hostname.<P>\n");
}
print sprintf($html_top, "#dcdcdc");
initialize_servers();
$db = new DB_probind;
$db->query("SELECT domain FROM zones WHERE (master IS NULL OR NOT master) AND domain != 'TEMPLATE' AND domain != '0.0.127.in-addr.arpa'".access()." ORDER BY domain");
$listfile = fopen("$TMP/domains", "w");
while ($db->next_record()) {
$domain = sprintf("%s\n", $db->Record[0]);
fwrite($listfile, $domain);
}
fclose($listfile);
print $search_prologue;
$pipe = popen("$BIN/nsrecs -h $nameserver < $TMP/domains", "r");
$lamecounter = 0;
while (!feof($pipe)) {
$result = fgets($pipe, 1000);
$hostnames = explode(" ", $result);
if (strlen($hostnames[0])) {
$zone = get_named_zone($hostnames[0]);
$domstr = "<B><A HREF=\"zones.php?zone=".$zone['id']."\">%s</A></B> ";
begin_domain($hostnames[0]);
for ($i=1; $i<count($hostnames); $i++) {
domain_name_server($hostnames[$i]);
}
print end_domain("", "$domstr is delegated to these servers:<UL>", "<LI>%s", "</UL><BR>\n", "<P>No NS records found", $lamecounter);
}
}
pclose($pipe);
print sprintf($search_epilogue, $lamecounter);
print $html_bottom;
?>