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