1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Pierre A. Joye <pierre@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include "php.h"
20
21 #include <windows.h>
22 #include <Winbase.h >
23 #include <Windns.h>
24
25 #include "php_dns.h"
26
27 #define PHP_DNS_NUM_TYPES 12 /* Number of DNS Types Supported by PHP currently */
28
29 #define PHP_DNS_A 0x00000001
30 #define PHP_DNS_NS 0x00000002
31 #define PHP_DNS_CNAME 0x00000010
32 #define PHP_DNS_SOA 0x00000020
33 #define PHP_DNS_PTR 0x00000800
34 #define PHP_DNS_HINFO 0x00001000
35 #define PHP_DNS_MX 0x00004000
36 #define PHP_DNS_TXT 0x00008000
37 #define PHP_DNS_A6 0x01000000
38 #define PHP_DNS_SRV 0x02000000
39 #define PHP_DNS_NAPTR 0x04000000
40 #define PHP_DNS_AAAA 0x08000000
41 #define PHP_DNS_ANY 0x10000000
42 #define PHP_DNS_ALL (PHP_DNS_A|PHP_DNS_NS|PHP_DNS_CNAME|PHP_DNS_SOA|PHP_DNS_PTR|PHP_DNS_HINFO|PHP_DNS_MX|PHP_DNS_TXT|PHP_DNS_A6|PHP_DNS_SRV|PHP_DNS_NAPTR|PHP_DNS_AAAA)
43
PHP_FUNCTION(dns_get_mx)44 PHP_FUNCTION(dns_get_mx) /* {{{ */
45 {
46 char *hostname;
47 size_t hostname_len;
48 zval *mx_list, *weight_list = NULL;
49
50 DNS_STATUS status; /* Return value of DnsQuery_A() function */
51 PDNS_RECORD pResult, pRec; /* Pointer to DNS_RECORD structure */
52
53 if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|z", &hostname, &hostname_len, &mx_list, &weight_list) == FAILURE) {
54 return;
55 }
56
57 status = DnsQuery_A(hostname, DNS_TYPE_MX, DNS_QUERY_STANDARD, NULL, &pResult, NULL);
58
59 if (status) {
60 RETURN_FALSE;
61 }
62
63 mx_list = zend_try_array_init(mx_list);
64 if (!mx_list) {
65 goto cleanup;
66 }
67
68 if (weight_list) {
69 weight_list = zend_try_array_init(weight_list);
70 if (!weight_list) {
71 goto cleanup;
72 }
73 }
74
75 for (pRec = pResult; pRec; pRec = pRec->pNext) {
76 DNS_SRV_DATA *srv = &pRec->Data.Srv;
77
78 if (pRec->wType != DNS_TYPE_MX) {
79 continue;
80 }
81
82 add_next_index_string(mx_list, pRec->Data.MX.pNameExchange);
83 if (weight_list) {
84 add_next_index_long(weight_list, srv->wPriority);
85 }
86 }
87
88 cleanup:
89 /* Free memory allocated for DNS records. */
90 DnsRecordListFree(pResult, DnsFreeRecordListDeep);
91
92 RETURN_TRUE;
93 }
94 /* }}} */
95
96 /* {{{ proto bool dns_check_record(string host [, string type])
97 Check DNS records corresponding to a given Internet host name or IP address */
PHP_FUNCTION(dns_check_record)98 PHP_FUNCTION(dns_check_record)
99 {
100 char *hostname, *rectype = NULL;
101 size_t hostname_len, rectype_len = 0;
102 int type = DNS_TYPE_MX;
103
104 DNS_STATUS status; /* Return value of DnsQuery_A() function */
105 PDNS_RECORD pResult; /* Pointer to DNS_RECORD structure */
106
107 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) {
108 return;
109 }
110
111 if (hostname_len == 0) {
112 php_error_docref(NULL, E_WARNING, "Host cannot be empty");
113 RETURN_FALSE;
114 }
115
116 if (rectype) {
117 if (!strcasecmp("A", rectype)) type = DNS_TYPE_A;
118 else if (!strcasecmp("NS", rectype)) type = DNS_TYPE_NS;
119 else if (!strcasecmp("MX", rectype)) type = DNS_TYPE_MX;
120 else if (!strcasecmp("PTR", rectype)) type = DNS_TYPE_PTR;
121 else if (!strcasecmp("ANY", rectype)) type = DNS_TYPE_ANY;
122 else if (!strcasecmp("SOA", rectype)) type = DNS_TYPE_SOA;
123 else if (!strcasecmp("TXT", rectype)) type = DNS_TYPE_TEXT;
124 else if (!strcasecmp("CNAME", rectype)) type = DNS_TYPE_CNAME;
125 else if (!strcasecmp("AAAA", rectype)) type = DNS_TYPE_AAAA;
126 else if (!strcasecmp("SRV", rectype)) type = DNS_TYPE_SRV;
127 else if (!strcasecmp("NAPTR", rectype)) type = DNS_TYPE_NAPTR;
128 else if (!strcasecmp("A6", rectype)) type = DNS_TYPE_A6;
129 else {
130 php_error_docref(NULL, E_WARNING, "Type '%s' not supported", rectype);
131 RETURN_FALSE;
132 }
133 }
134
135 status = DnsQuery_A(hostname, type, DNS_QUERY_STANDARD, NULL, &pResult, NULL);
136
137 if (status) {
138 RETURN_FALSE;
139 }
140
141 RETURN_TRUE;
142 }
143 /* }}} */
144
145 /* {{{ php_parserr */
php_parserr(PDNS_RECORD pRec,int type_to_fetch,int store,int raw,zval * subarray)146 static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, int raw, zval *subarray)
147 {
148 int type;
149 u_long ttl;
150
151 type = pRec->wType;
152 ttl = pRec->dwTtl;
153
154 ZVAL_UNDEF(subarray);
155
156 if (type_to_fetch != DNS_TYPE_ANY && type != type_to_fetch) {
157 return;
158 }
159
160 if (!store) {
161 return;
162 }
163
164 array_init(subarray);
165
166 add_assoc_string(subarray, "host", pRec->pName);
167 add_assoc_string(subarray, "class", "IN");
168 add_assoc_long(subarray, "ttl", ttl);
169
170 if (raw) {
171 add_assoc_long(subarray, "type", type);
172 add_assoc_stringl(subarray, "data", (char*) &pRec->Data, (uint32_t) pRec->wDataLength);
173 return;
174 }
175
176 switch (type) {
177 case DNS_TYPE_A: {
178 IN_ADDR ipaddr;
179 ipaddr.S_un.S_addr = (pRec->Data.A.IpAddress);
180 add_assoc_string(subarray, "type", "A");
181 add_assoc_string(subarray, "ip", inet_ntoa(ipaddr));
182 break;
183 }
184
185 case DNS_TYPE_MX:
186 add_assoc_string(subarray, "type", "MX");
187 add_assoc_long(subarray, "pri", pRec->Data.Srv.wPriority);
188 /* no break; */
189
190 case DNS_TYPE_CNAME:
191 if (type == DNS_TYPE_CNAME) {
192 add_assoc_string(subarray, "type", "CNAME");
193 }
194 /* no break; */
195
196 case DNS_TYPE_NS:
197 if (type == DNS_TYPE_NS) {
198 add_assoc_string(subarray, "type", "NS");
199 }
200 /* no break; */
201
202 case DNS_TYPE_PTR:
203 if (type == DNS_TYPE_PTR) {
204 add_assoc_string(subarray, "type", "PTR");
205 }
206 add_assoc_string(subarray, "target", pRec->Data.MX.pNameExchange);
207 break;
208
209 /* Not available on windows, the query is possible but there is no DNS_HINFO_DATA structure */
210 case DNS_TYPE_HINFO:
211 case DNS_TYPE_TEXT:
212 {
213 DWORD i = 0;
214 DNS_TXT_DATA *data_txt = &pRec->Data.TXT;
215 DWORD count = data_txt->dwStringCount;
216 zend_string *txt;
217 char *txt_dst;
218 size_t txt_len = 0;
219 zval entries;
220
221 add_assoc_string(subarray, "type", "TXT");
222
223 array_init(&entries);
224
225 for (i = 0; i < count; i++) {
226 txt_len += strlen(data_txt->pStringArray[i]);
227 }
228
229 txt = zend_string_alloc(txt_len, 0);
230 txt_dst = ZSTR_VAL(txt);
231 for (i = 0; i < count; i++) {
232 size_t len = strlen(data_txt->pStringArray[i]);
233 memcpy(txt_dst, data_txt->pStringArray[i], len);
234 add_next_index_stringl(&entries, data_txt->pStringArray[i], len);
235 txt_dst += len;
236 }
237 *txt_dst = '\0';
238 add_assoc_str(subarray, "txt", txt);
239 add_assoc_zval(subarray, "entries", &entries);
240 }
241 break;
242
243 case DNS_TYPE_SOA:
244 {
245 DNS_SOA_DATA *data_soa = &pRec->Data.Soa;
246
247 add_assoc_string(subarray, "type", "SOA");
248
249 add_assoc_string(subarray, "mname", data_soa->pNamePrimaryServer);
250 add_assoc_string(subarray, "rname", data_soa->pNameAdministrator);
251 add_assoc_long(subarray, "serial", data_soa->dwSerialNo);
252 add_assoc_long(subarray, "refresh", data_soa->dwRefresh);
253 add_assoc_long(subarray, "retry", data_soa->dwRetry);
254 add_assoc_long(subarray, "expire", data_soa->dwExpire);
255 add_assoc_long(subarray, "minimum-ttl", data_soa->dwDefaultTtl);
256 }
257 break;
258
259 case DNS_TYPE_AAAA:
260 {
261 DNS_AAAA_DATA *data_aaaa = &pRec->Data.AAAA;
262 char buf[sizeof("AAAA:AAAA:AAAA:AAAA:AAAA:AAAA:AAAA:AAAA")];
263 char *tp = buf;
264 int i;
265 unsigned short out[8];
266 int have_v6_break = 0, in_v6_break = 0;
267
268 for (i = 0; i < 4; ++i) {
269 DWORD chunk = data_aaaa->Ip6Address.IP6Dword[i];
270 out[i * 2] = htons(LOWORD(chunk));
271 out[i * 2 + 1] = htons(HIWORD(chunk));
272 }
273
274 for(i=0; i < 8; i++) {
275 if (out[i] != 0) {
276 if (tp > (u_char *)buf) {
277 in_v6_break = 0;
278 tp[0] = ':';
279 tp++;
280 }
281 tp += sprintf((char*)tp,"%x", out[i]);
282 } else {
283 if (!have_v6_break) {
284 have_v6_break = 1;
285 in_v6_break = 1;
286 tp[0] = ':';
287 tp++;
288 } else if (!in_v6_break) {
289 tp[0] = ':';
290 tp++;
291 tp[0] = '0';
292 tp++;
293 }
294 }
295 }
296
297 if (have_v6_break && in_v6_break) {
298 tp[0] = ':';
299 tp++;
300 }
301 tp[0] = '\0';
302
303 add_assoc_string(subarray, "type", "AAAA");
304 add_assoc_string(subarray, "ipv6", buf);
305 }
306 break;
307
308 #if 0
309 /* Won't be implemented. A6 is deprecated. (Pierre) */
310 case DNS_TYPE_A6:
311 break;
312 #endif
313
314 case DNS_TYPE_SRV:
315 {
316 DNS_SRV_DATA *data_srv = &pRec->Data.Srv;
317
318 add_assoc_string(subarray, "type", "SRV");
319 add_assoc_long(subarray, "pri", data_srv->wPriority);
320 add_assoc_long(subarray, "weight", data_srv->wWeight);
321 add_assoc_long(subarray, "port", data_srv->wPort);
322 add_assoc_string(subarray, "target", data_srv->pNameTarget);
323 }
324 break;
325
326 case DNS_TYPE_NAPTR:
327 {
328 DNS_NAPTR_DATA * data_naptr = &pRec->Data.Naptr;
329
330 add_assoc_string(subarray, "type", "NAPTR");
331 add_assoc_long(subarray, "order", data_naptr->wOrder);
332 add_assoc_long(subarray, "pref", data_naptr->wPreference);
333 add_assoc_string(subarray, "flags", data_naptr->pFlags);
334 add_assoc_string(subarray, "services", data_naptr->pService);
335 add_assoc_string(subarray, "regex", data_naptr->pRegularExpression);
336 add_assoc_string(subarray, "replacement", data_naptr->pReplacement);
337 }
338 break;
339
340 default:
341 /* unknown type */
342 ZVAL_UNDEF(subarray);
343 return;
344 }
345
346 }
347 /* }}} */
348
349 /* {{{ proto array|false dns_get_record(string hostname [, int type[, array &authns[, array &addtl[, bool raw]]]])
350 Get any Resource Record corresponding to a given Internet host name */
PHP_FUNCTION(dns_get_record)351 PHP_FUNCTION(dns_get_record)
352 {
353 char *hostname;
354 size_t hostname_len;
355 zend_long type_param = PHP_DNS_ANY;
356 zval *authns = NULL, *addtl = NULL;
357 int type, type_to_fetch, first_query = 1, store_results = 1;
358 zend_bool raw = 0;
359
360 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lz!z!b",
361 &hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) {
362 return;
363 }
364
365 if (authns) {
366 authns = zend_try_array_init(authns);
367 if (!authns) {
368 return;
369 }
370 }
371 if (addtl) {
372 addtl = zend_try_array_init(addtl);
373 if (!addtl) {
374 return;
375 }
376 }
377
378 if (!raw) {
379 if ((type_param & ~PHP_DNS_ALL) && (type_param != PHP_DNS_ANY)) {
380 php_error_docref(NULL, E_WARNING, "Type '%ld' not supported", type_param);
381 RETURN_FALSE;
382 }
383 } else {
384 if ((type_param < 1) || (type_param > 0xFFFF)) {
385 php_error_docref(NULL, E_WARNING,
386 "Numeric DNS record type must be between 1 and 65535, '%ld' given", type_param);
387 RETURN_FALSE;
388 }
389 }
390
391 /* Initialize the return array */
392 array_init(return_value);
393
394 if (raw) {
395 type = -1;
396 } else if (type_param == PHP_DNS_ANY) {
397 type = PHP_DNS_NUM_TYPES + 1;
398 } else {
399 type = 0;
400 }
401
402 for ( ;
403 type < (addtl ? (PHP_DNS_NUM_TYPES + 2) : PHP_DNS_NUM_TYPES) || first_query;
404 type++
405 ) {
406 DNS_STATUS status; /* Return value of DnsQuery_A() function */
407 PDNS_RECORD pResult, pRec; /* Pointer to DNS_RECORD structure */
408
409 first_query = 0;
410 switch (type) {
411 case -1: /* raw */
412 type_to_fetch = type_param;
413 /* skip over the rest and go directly to additional records */
414 type = PHP_DNS_NUM_TYPES - 1;
415 break;
416 case 0:
417 type_to_fetch = type_param&PHP_DNS_A ? DNS_TYPE_A : 0;
418 break;
419 case 1:
420 type_to_fetch = type_param&PHP_DNS_NS ? DNS_TYPE_NS : 0;
421 break;
422 case 2:
423 type_to_fetch = type_param&PHP_DNS_CNAME ? DNS_TYPE_CNAME : 0;
424 break;
425 case 3:
426 type_to_fetch = type_param&PHP_DNS_SOA ? DNS_TYPE_SOA : 0;
427 break;
428 case 4:
429 type_to_fetch = type_param&PHP_DNS_PTR ? DNS_TYPE_PTR : 0;
430 break;
431 case 5:
432 type_to_fetch = type_param&PHP_DNS_HINFO ? DNS_TYPE_HINFO : 0;
433 break;
434 case 6:
435 type_to_fetch = type_param&PHP_DNS_MX ? DNS_TYPE_MX : 0;
436 break;
437 case 7:
438 type_to_fetch = type_param&PHP_DNS_TXT ? DNS_TYPE_TEXT : 0;
439 break;
440 case 8:
441 type_to_fetch = type_param&PHP_DNS_AAAA ? DNS_TYPE_AAAA : 0;
442 break;
443 case 9:
444 type_to_fetch = type_param&PHP_DNS_SRV ? DNS_TYPE_SRV : 0;
445 break;
446 case 10:
447 type_to_fetch = type_param&PHP_DNS_NAPTR ? DNS_TYPE_NAPTR : 0;
448 break;
449 case 11:
450 type_to_fetch = type_param&PHP_DNS_A6 ? DNS_TYPE_A6 : 0;
451 break;
452 case PHP_DNS_NUM_TYPES:
453 store_results = 0;
454 continue;
455 default:
456 case (PHP_DNS_NUM_TYPES + 1):
457 type_to_fetch = DNS_TYPE_ANY;
458 break;
459 }
460
461 if (type_to_fetch) {
462 status = DnsQuery_A(hostname, type_to_fetch, DNS_QUERY_STANDARD, NULL, &pResult, NULL);
463
464 if (status) {
465 if (status == DNS_INFO_NO_RECORDS || status == DNS_ERROR_RCODE_NAME_ERROR) {
466 continue;
467 } else {
468 php_error_docref(NULL, E_WARNING, "DNS Query failed");
469 zend_array_destroy(Z_ARR_P(return_value));
470 RETURN_FALSE;
471 }
472 }
473
474 for (pRec = pResult; pRec; pRec = pRec->pNext) {
475 zval retval;
476
477 if (pRec->Flags.S.Section == DnsSectionAnswer) {
478 php_parserr(pRec, type_to_fetch, store_results, raw, &retval);
479 if (!Z_ISUNDEF(retval) && store_results) {
480 add_next_index_zval(return_value, &retval);
481 }
482 }
483
484 if (authns && pRec->Flags.S.Section == DnsSectionAuthority) {
485
486 php_parserr(pRec, type_to_fetch, 1, raw, &retval);
487 if (!Z_ISUNDEF(retval)) {
488 add_next_index_zval(authns, &retval);
489 }
490 }
491
492 /* Stupid typo in PSDK 6.1, WinDNS.h(1258)... */
493 #ifndef DnsSectionAdditional
494 # ifdef DnsSectionAddtional
495 # define DnsSectionAdditional DnsSectionAddtional
496 # else
497 # define DnsSectionAdditional 3
498 # endif
499 #endif
500 if (addtl && pRec->Flags.S.Section == DnsSectionAdditional) {
501 php_parserr(pRec, type_to_fetch, 1, raw, &retval);
502 if (!Z_ISUNDEF(retval)) {
503 add_next_index_zval(addtl, &retval);
504 }
505 }
506 }
507 /* Free memory allocated for DNS records. */
508 DnsRecordListFree(pResult, DnsFreeRecordListDeep);
509 }
510 }
511 }
512 /* }}} */
513
PHP_MINIT_FUNCTION(dns)514 PHP_MINIT_FUNCTION(dns) {
515 REGISTER_LONG_CONSTANT("DNS_A", PHP_DNS_A, CONST_CS | CONST_PERSISTENT);
516 REGISTER_LONG_CONSTANT("DNS_NS", PHP_DNS_NS, CONST_CS | CONST_PERSISTENT);
517 REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT);
518 REGISTER_LONG_CONSTANT("DNS_SOA", PHP_DNS_SOA, CONST_CS | CONST_PERSISTENT);
519 REGISTER_LONG_CONSTANT("DNS_PTR", PHP_DNS_PTR, CONST_CS | CONST_PERSISTENT);
520 REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT);
521 REGISTER_LONG_CONSTANT("DNS_MX", PHP_DNS_MX, CONST_CS | CONST_PERSISTENT);
522 REGISTER_LONG_CONSTANT("DNS_TXT", PHP_DNS_TXT, CONST_CS | CONST_PERSISTENT);
523 REGISTER_LONG_CONSTANT("DNS_SRV", PHP_DNS_SRV, CONST_CS | CONST_PERSISTENT);
524 REGISTER_LONG_CONSTANT("DNS_NAPTR", PHP_DNS_NAPTR, CONST_CS | CONST_PERSISTENT);
525 REGISTER_LONG_CONSTANT("DNS_AAAA", PHP_DNS_AAAA, CONST_CS | CONST_PERSISTENT);
526 REGISTER_LONG_CONSTANT("DNS_A6", PHP_DNS_A6, CONST_CS | CONST_PERSISTENT);
527 REGISTER_LONG_CONSTANT("DNS_ANY", PHP_DNS_ANY, CONST_CS | CONST_PERSISTENT);
528 REGISTER_LONG_CONSTANT("DNS_ALL", PHP_DNS_ALL, CONST_CS | CONST_PERSISTENT);
529 return SUCCESS;
530 }
531