/
prototype.php
339 lines (289 loc) · 9.81 KB
/
prototype.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
require('vendor/autoload.php');
require('config.php');
require('functions.php');
// Database
require('db_setup.php');
// Process HTTP Request
$request = file_get_contents("php://input");
$query = json_decode($request, true);
// PowerDNS 'Lookup' Method
if($query['method'] == 'lookup') {
$qtype = $query['parameters']['qtype'];
$qname = $query['parameters']['qname'];
$prefix_supported = false;
$prefix_soa_qname = false;
// Autoreverse IPv4 Suffixes
if(substr($qname, -12, 12) == 'in-addr.arpa') {
foreach($suffix_ipv4 as $prefix => $suffix) {
// Check if this is a valid IPv4 Prefix
if(preg_match("/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\/([0-9]{1,2})$/", $prefix, $prefix_arr)) {
// Convert $qname to IP
$ip_arr = explode(".", substr($qname, 0, strlen($qname) - 13));
$ip_arr = array_reverse($ip_arr);
if(sizeof($ip_arr) < 4) {
for($i = (4 - (4 - sizeof($ip_arr))); $i < 4; $i++) {
$ip_arr[] = 0;
}
}
$qname_ip = implode(".", $ip_arr);
// Check if IP is in this prefix
if(ip_in_network($qname_ip, $prefix)) {
switch($query['parameters']['qtype']) {
case 'SOA':
// Get closest IPv4 Octet Boundary and generate SOA qname
$octets = floor($prefix_arr[5] / 8);
for($i = $octets; $i > 0; $i--) {
$prefix_soa_qname .= $prefix_arr[$i] . '.';
}
$prefix_soa_qname .= 'in-addr.arpa';
if(ip_in_network($qname_ip, $prefix) || $prefix_soa_qname == $qname) {
$result = array(
'result' =>array(
array(
'qtype' => 'SOA',
'qname' => $qname,
'content' => implode(" ", $soa),
'ttl' => $soa[6],
),
),
);
}
break;
case 'NS':
break;
default:
$content = implode("-", $ip_arr) . '.' . $suffix['suffix'];
$result = array('result' => array(array(
'qtype' => 'PTR',
'qname' => $qname,
'content' => $content,
'ttl' => $soa[6],
)));
break;
}
break;
}
}
}
}
// Autoreverse IPv6 Suffixes
if(substr($qname, -8, 8) == 'ip6.arpa') {
foreach($suffix_ipv6 as $prefix => $suffix) {
// Ensure this is a valid IPv6 prefix
$prefix_split_arr = explode("/", $prefix);
if (!filter_var($prefix_split_arr[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
$ip_arr = explode(".", substr($qname, 0, strlen($qname) - 9));
$ip_arr = array_reverse($ip_arr);
for($i = 0; $i < 32; $i++) {
$qname_ip .= $ip_arr[$i] ? $ip_arr[$i] : '0';
if($i % 4 == 3 && $i != 31) $qname_ip .= ":";
}
if(ip_in_network6($qname_ip, $prefix)) {
switch($query['parameters']['qtype']) {
case 'SOA':
// Generate SOA name from prefix
$prefix_expanded = inet6_expand($prefix_split_arr[0]);
$prefix_expanded = str_replace(':', '', $prefix_expanded);
$prefix_arr = str_split($prefix_expanded);
$octets = floor($prefix_split_arr[1] / 4);
for($i = $octets - 1; $i >= 0; $i--) {
$prefix_soa_qname .= $prefix_arr[$i] . '.';
}
$prefix_soa_qname .= 'ip6.arpa';
if(ip_in_network6($qname_ip, $prefix) || $prefix_soa_qname == $qname) {
$result = array(
'result' =>array(
array(
'qtype' => 'SOA',
'qname' => $query['parameters']['qname'],
'content' => implode(" ", $soa),
'ttl' => $soa[6],
),
),
);
}
break;
case 'NS':
break;
default:
$ip_arr = explode(".", substr($qname, 0, strlen($qname) - 9));
$ip_arr = array_reverse($ip_arr);
for($i = 0; $i < 32; $i++) {
$content .= $ip_arr[$i];
if($i % 4 == 3 && $i != 31) $content .= "-";
}
$content .= '.' . $suffix['suffix'];
$result = array('result' => array(array(
'qtype' => 'PTR',
'qname' => $qname,
'content' => $content,
'ttl' => $soa[6],
)));
break;
}
break;
}
};
}
}
// A and AAAA records from Autoreverse addresses
// Confirm that this is a PTR record generated here
if(preg_match("/^([-0-9]+)\.(.*)$/", $qname, $qname_parts) || in_array_recursive($qname, $suffix_ipv4) || in_array_recursive($qname, $suffix_ipv6)) {
// Create $qname_parts array if we matched $suffix_* but not the string (Probably an SOA or NS request)
if(!$qname_parts) {
$qname_parts = array(
$qname,
false,
$qname,
);
}
// Check that we support this suffix
// IPv4
if(in_array_recursive($qname_parts[2], $suffix_ipv4)) {
// Get IP from $qname_parts if possible
if($qname_parts[1]) {
$qname_ip = str_replace('-', '.', $qname_parts[1]);
}
foreach($suffix_ipv4 as $prefix => $suffix) {
if($suffix['suffix'] == $qname_parts[2]) {
switch($qtype) {
case 'SOA':
if(ip_in_network($qname_ip, $prefix) || $qname == $suffix['suffix']) {
$result = array(
'result' =>array(
array(
'qtype' => 'SOA',
'qname' => $qname,
'content' => implode(" ", $soa),
'ttl' => $soa[6],
),
),
);
}
break;
case 'NS':
break;
default:
// Convert to IP and confirm it's in this prefix
if(ip_in_network($qname_ip, $prefix)) {
$result = array(
'result' =>array(
array(
'qtype' => 'A',
'qname' => $qname,
'content' => $qname_ip,
'ttl' => $soa[6],
),
),
);
}
break;
}
}
}
}
// IPv6
elseif(in_array_recursive($qname_parts[2], $suffix_ipv6)) {
// Get IP from $qname_parts if possible
if($qname_parts[1]) {
$qname_ip = str_replace('-', ':', $qname_parts[1]);
}
foreach($suffix_ipv6 as $prefix => $suffix) {
if($suffix['suffix'] == $qname_parts[2]) {
switch($qtype) {
case 'SOA':
if(ip_in_network6($qname_ip, $prefix) || $qname == $suffix['suffix']) {
$result = array(
'result' =>array(
array(
'qtype' => 'SOA',
'qname' => $qname,
'content' => implode(" ", $soa),
'ttl' => $soa[6],
),
),
);
}
break;
case 'NS':
break;
default:
// Convert to IP and confirm it's in this prefix
if(ip_in_network6($qname_ip, $prefix)) {
$result = array(
'result' =>array(
array(
'qtype' => 'AAAA',
'qname' => $qname,
'content' => $qname_ip,
'ttl' => $soa[6],
),
),
);
}
break;
}
}
}
}
}
// PHPIPAM Hostname Records
$prefix_supported = false;
foreach($phpipam_suffixes as $suffix) {
if(strpos($qname, $suffix) !== false) {
$prefix_supported = true;
}
}
if($prefix_supported) {
switch($qtype) {
case 'SOA':
$result = array(
'result' =>array(
array(
'qtype' => 'SOA',
'qname' => $qname,
'content' => implode(" ", $soa),
'ttl' => $soa[6],
),
),
);
break;
default:
$ips = $db->table('ipaddresses')->where('dns_name', '=', $qname)->lists('ip_addr');
// Generate $results
$results = array();
foreach($ips as $ip) {
// IPv4
if($ip <= 4294967294) {
$results[] = array(
'qtype' => 'A',
'qname' => $qname,
'content' => long2ip($ip),
'ttl' => $soa[6],
);
}
// IPv6
else {
$results[] = array(
'qtype' => 'AAAA',
'qname' => $qname,
'content' => long2ip6($ip),
'ttl' => $soa[6],
);
}
}
if(sizeof($results)) {
$result = array('result' => $results);
}
break;
}
}
}
if(!$result) {
$result = array('result' => false);
}
$result['log'] = array("query=$request response=" . json_encode($result) . " " . date('U'));
$response = json_encode($result);
echo $response;
?>