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