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