1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-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: The typical suspects |
16 | Pollita <pollita@php.net> |
17 | Marcus Boerger <helly@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 /* {{{ includes */
22 #include "php.h"
23 #include "php_network.h"
24
25 #if HAVE_SYS_SOCKET_H
26 #include <sys/socket.h>
27 #endif
28
29 #ifdef PHP_WIN32
30 # include "win32/inet.h"
31 # include <winsock2.h>
32 # include <windows.h>
33 # include <Ws2tcpip.h>
34 #else
35 #include <netinet/in.h>
36 #if HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
38 #endif
39 #include <netdb.h>
40 #ifdef _OSD_POSIX
41 #undef STATUS
42 #undef T_UNSPEC
43 #endif
44 #if HAVE_ARPA_NAMESER_H
45 #ifdef DARWIN
46 # define BIND_8_COMPAT 1
47 #endif
48 #include <arpa/nameser.h>
49 #endif
50 #if HAVE_RESOLV_H
51 #include <resolv.h>
52 #endif
53 #ifdef HAVE_DNS_H
54 #include <dns.h>
55 #endif
56 #endif
57
58 #ifndef MAXHOSTNAMELEN
59 #define MAXHOSTNAMELEN 255
60 #endif
61
62 /* For the local hostname obtained via gethostname which is different from the
63 dns-related MAXHOSTNAMELEN constant above */
64 #ifndef HOST_NAME_MAX
65 #define HOST_NAME_MAX 255
66 #endif
67
68 #include "php_dns.h"
69
70 /* type compat */
71 #ifndef DNS_T_A
72 #define DNS_T_A 1
73 #endif
74 #ifndef DNS_T_NS
75 #define DNS_T_NS 2
76 #endif
77 #ifndef DNS_T_CNAME
78 #define DNS_T_CNAME 5
79 #endif
80 #ifndef DNS_T_SOA
81 #define DNS_T_SOA 6
82 #endif
83 #ifndef DNS_T_PTR
84 #define DNS_T_PTR 12
85 #endif
86 #ifndef DNS_T_HINFO
87 #define DNS_T_HINFO 13
88 #endif
89 #ifndef DNS_T_MINFO
90 #define DNS_T_MINFO 14
91 #endif
92 #ifndef DNS_T_MX
93 #define DNS_T_MX 15
94 #endif
95 #ifndef DNS_T_TXT
96 #define DNS_T_TXT 16
97 #endif
98 #ifndef DNS_T_AAAA
99 #define DNS_T_AAAA 28
100 #endif
101 #ifndef DNS_T_SRV
102 #define DNS_T_SRV 33
103 #endif
104 #ifndef DNS_T_NAPTR
105 #define DNS_T_NAPTR 35
106 #endif
107 #ifndef DNS_T_A6
108 #define DNS_T_A6 38
109 #endif
110 #ifndef DNS_T_CAA
111 #define DNS_T_CAA 257
112 #endif
113
114 #ifndef DNS_T_ANY
115 #define DNS_T_ANY 255
116 #endif
117 /* }}} */
118
119 static zend_string *php_gethostbyaddr(char *ip);
120 static zend_string *php_gethostbyname(char *name);
121
122 #ifdef HAVE_GETHOSTNAME
123 /* {{{ proto string gethostname()
124 Get the host name of the current machine */
PHP_FUNCTION(gethostname)125 PHP_FUNCTION(gethostname)
126 {
127 char buf[HOST_NAME_MAX + 1];
128
129 if (zend_parse_parameters_none() == FAILURE) {
130 return;
131 }
132
133 if (gethostname(buf, sizeof(buf))) {
134 php_error_docref(NULL, E_WARNING, "unable to fetch host [%d]: %s", errno, strerror(errno));
135 RETURN_FALSE;
136 }
137
138 RETURN_STRING(buf);
139 }
140 /* }}} */
141 #endif
142
143 /* TODO: Reimplement the gethostby* functions using the new winxp+ API, in dns_win32.c, then
144 we can have a dns.c, dns_unix.c and dns_win32.c instead of a messy dns.c full of #ifdef
145 */
146
147 /* {{{ proto string gethostbyaddr(string ip_address)
148 Get the Internet host name corresponding to a given IP address */
PHP_FUNCTION(gethostbyaddr)149 PHP_FUNCTION(gethostbyaddr)
150 {
151 char *addr;
152 size_t addr_len;
153 zend_string *hostname;
154
155 ZEND_PARSE_PARAMETERS_START(1, 1)
156 Z_PARAM_STRING(addr, addr_len)
157 ZEND_PARSE_PARAMETERS_END();
158
159 hostname = php_gethostbyaddr(addr);
160
161 if (hostname == NULL) {
162 #if HAVE_IPV6 && HAVE_INET_PTON
163 php_error_docref(NULL, E_WARNING, "Address is not a valid IPv4 or IPv6 address");
164 #else
165 php_error_docref(NULL, E_WARNING, "Address is not in a.b.c.d form");
166 #endif
167 RETVAL_FALSE;
168 } else {
169 RETVAL_STR(hostname);
170 }
171 }
172 /* }}} */
173
174 /* {{{ php_gethostbyaddr */
php_gethostbyaddr(char * ip)175 static zend_string *php_gethostbyaddr(char *ip)
176 {
177 #if HAVE_IPV6 && HAVE_INET_PTON
178 struct in6_addr addr6;
179 #endif
180 struct in_addr addr;
181 struct hostent *hp;
182
183 #if HAVE_IPV6 && HAVE_INET_PTON
184 if (inet_pton(AF_INET6, ip, &addr6)) {
185 hp = gethostbyaddr((char *) &addr6, sizeof(addr6), AF_INET6);
186 } else if (inet_pton(AF_INET, ip, &addr)) {
187 hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
188 } else {
189 return NULL;
190 }
191 #else
192 addr.s_addr = inet_addr(ip);
193
194 if (addr.s_addr == -1) {
195 return NULL;
196 }
197
198 hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
199 #endif
200
201 if (!hp || hp->h_name == NULL || hp->h_name[0] == '\0') {
202 return zend_string_init(ip, strlen(ip), 0);
203 }
204
205 return zend_string_init(hp->h_name, strlen(hp->h_name), 0);
206 }
207 /* }}} */
208
209 /* {{{ proto string gethostbyname(string hostname)
210 Get the IP address corresponding to a given Internet host name */
PHP_FUNCTION(gethostbyname)211 PHP_FUNCTION(gethostbyname)
212 {
213 char *hostname;
214 size_t hostname_len;
215
216 ZEND_PARSE_PARAMETERS_START(1, 1)
217 Z_PARAM_STRING(hostname, hostname_len)
218 ZEND_PARSE_PARAMETERS_END();
219
220 if(hostname_len > MAXFQDNLEN) {
221 /* name too long, protect from CVE-2015-0235 */
222 php_error_docref(NULL, E_WARNING, "Host name is too long, the limit is %d characters", MAXFQDNLEN);
223 RETURN_STRINGL(hostname, hostname_len);
224 }
225
226 RETURN_STR(php_gethostbyname(hostname));
227 }
228 /* }}} */
229
230 /* {{{ proto array gethostbynamel(string hostname)
231 Return a list of IP addresses that a given hostname resolves to. */
PHP_FUNCTION(gethostbynamel)232 PHP_FUNCTION(gethostbynamel)
233 {
234 char *hostname;
235 size_t hostname_len;
236 struct hostent *hp;
237 struct in_addr in;
238 int i;
239
240 ZEND_PARSE_PARAMETERS_START(1, 1)
241 Z_PARAM_STRING(hostname, hostname_len)
242 ZEND_PARSE_PARAMETERS_END();
243
244 if(hostname_len > MAXFQDNLEN) {
245 /* name too long, protect from CVE-2015-0235 */
246 php_error_docref(NULL, E_WARNING, "Host name is too long, the limit is %d characters", MAXFQDNLEN);
247 RETURN_FALSE;
248 }
249
250 hp = php_network_gethostbyname(hostname);
251 if (hp == NULL || hp->h_addr_list == NULL) {
252 RETURN_FALSE;
253 }
254
255 array_init(return_value);
256
257 for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
258 in = *(struct in_addr *) hp->h_addr_list[i];
259 add_next_index_string(return_value, inet_ntoa(in));
260 }
261 }
262 /* }}} */
263
264 /* {{{ php_gethostbyname */
php_gethostbyname(char * name)265 static zend_string *php_gethostbyname(char *name)
266 {
267 struct hostent *hp;
268 struct in_addr in;
269 char *address;
270
271 hp = php_network_gethostbyname(name);
272
273 if (!hp || !*(hp->h_addr_list)) {
274 return zend_string_init(name, strlen(name), 0);
275 }
276
277 memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
278
279 address = inet_ntoa(in);
280 return zend_string_init(address, strlen(address), 0);
281 }
282 /* }}} */
283
284 #if HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32)
285 # define PHP_DNS_NUM_TYPES 13 /* Number of DNS Types Supported by PHP currently */
286
287 # define PHP_DNS_A 0x00000001
288 # define PHP_DNS_NS 0x00000002
289 # define PHP_DNS_CNAME 0x00000010
290 # define PHP_DNS_SOA 0x00000020
291 # define PHP_DNS_PTR 0x00000800
292 # define PHP_DNS_HINFO 0x00001000
293 # define PHP_DNS_CAA 0x00002000
294 # define PHP_DNS_MX 0x00004000
295 # define PHP_DNS_TXT 0x00008000
296 # define PHP_DNS_A6 0x01000000
297 # define PHP_DNS_SRV 0x02000000
298 # define PHP_DNS_NAPTR 0x04000000
299 # define PHP_DNS_AAAA 0x08000000
300 # define PHP_DNS_ANY 0x10000000
301 # define PHP_DNS_ALL (PHP_DNS_A|PHP_DNS_NS|PHP_DNS_CNAME|PHP_DNS_SOA|PHP_DNS_PTR|PHP_DNS_HINFO|PHP_DNS_CAA|PHP_DNS_MX|PHP_DNS_TXT|PHP_DNS_A6|PHP_DNS_SRV|PHP_DNS_NAPTR|PHP_DNS_AAAA)
302 #endif /* HAVE_FULL_DNS_FUNCS || defined(PHP_WIN32) */
303
304 /* Note: These functions are defined in ext/standard/dns_win32.c for Windows! */
305 #if !defined(PHP_WIN32) && HAVE_DNS_SEARCH_FUNC
306
307 #ifndef HFIXEDSZ
308 #define HFIXEDSZ 12 /* fixed data in header <arpa/nameser.h> */
309 #endif /* HFIXEDSZ */
310
311 #ifndef QFIXEDSZ
312 #define QFIXEDSZ 4 /* fixed data in query <arpa/nameser.h> */
313 #endif /* QFIXEDSZ */
314
315 #undef MAXHOSTNAMELEN
316 #define MAXHOSTNAMELEN 1024
317
318 #ifndef MAXRESOURCERECORDS
319 #define MAXRESOURCERECORDS 64
320 #endif /* MAXRESOURCERECORDS */
321
322 typedef union {
323 HEADER qb1;
324 u_char qb2[65536];
325 } querybuf;
326
327 /* just a hack to free resources allocated by glibc in __res_nsend()
328 * See also:
329 * res_thread_freeres() in glibc/resolv/res_init.c
330 * __libc_res_nsend() in resolv/res_send.c
331 * */
332
333 #if defined(__GLIBC__) && !defined(HAVE_DEPRECATED_DNS_FUNCS)
334 #define php_dns_free_res(__res__) _php_dns_free_res(__res__)
_php_dns_free_res(struct __res_state * res)335 static void _php_dns_free_res(struct __res_state *res) { /* {{{ */
336 int ns;
337 for (ns = 0; ns < MAXNS; ns++) {
338 if (res->_u._ext.nsaddrs[ns] != NULL) {
339 free (res->_u._ext.nsaddrs[ns]);
340 res->_u._ext.nsaddrs[ns] = NULL;
341 }
342 }
343 } /* }}} */
344 #else
345 #define php_dns_free_res(__res__)
346 #endif
347
348 /* {{{ proto bool dns_check_record(string host [, string type])
349 Check DNS records corresponding to a given Internet host name or IP address */
PHP_FUNCTION(dns_check_record)350 PHP_FUNCTION(dns_check_record)
351 {
352 HEADER *hp;
353 querybuf answer;
354 char *hostname, *rectype = NULL;
355 size_t hostname_len, rectype_len = 0;
356 int type = T_MX, i;
357 #if defined(HAVE_DNS_SEARCH)
358 struct sockaddr_storage from;
359 uint32_t fromsize = sizeof(from);
360 dns_handle_t handle;
361 #elif defined(HAVE_RES_NSEARCH)
362 struct __res_state state;
363 struct __res_state *handle = &state;
364 #endif
365
366 ZEND_PARSE_PARAMETERS_START(1, 2)
367 Z_PARAM_STRING(hostname, hostname_len)
368 Z_PARAM_OPTIONAL
369 Z_PARAM_STRING(rectype, rectype_len)
370 ZEND_PARSE_PARAMETERS_END();
371
372 if (hostname_len == 0) {
373 php_error_docref(NULL, E_WARNING, "Host cannot be empty");
374 RETURN_FALSE;
375 }
376
377 if (rectype) {
378 if (!strcasecmp("A", rectype)) type = T_A;
379 else if (!strcasecmp("NS", rectype)) type = DNS_T_NS;
380 else if (!strcasecmp("MX", rectype)) type = DNS_T_MX;
381 else if (!strcasecmp("PTR", rectype)) type = DNS_T_PTR;
382 else if (!strcasecmp("ANY", rectype)) type = DNS_T_ANY;
383 else if (!strcasecmp("SOA", rectype)) type = DNS_T_SOA;
384 else if (!strcasecmp("CAA", rectype)) type = DNS_T_CAA;
385 else if (!strcasecmp("TXT", rectype)) type = DNS_T_TXT;
386 else if (!strcasecmp("CNAME", rectype)) type = DNS_T_CNAME;
387 else if (!strcasecmp("AAAA", rectype)) type = DNS_T_AAAA;
388 else if (!strcasecmp("SRV", rectype)) type = DNS_T_SRV;
389 else if (!strcasecmp("NAPTR", rectype)) type = DNS_T_NAPTR;
390 else if (!strcasecmp("A6", rectype)) type = DNS_T_A6;
391 else {
392 php_error_docref(NULL, E_WARNING, "Type '%s' not supported", rectype);
393 RETURN_FALSE;
394 }
395 }
396
397 #if defined(HAVE_DNS_SEARCH)
398 handle = dns_open(NULL);
399 if (handle == NULL) {
400 RETURN_FALSE;
401 }
402 #elif defined(HAVE_RES_NSEARCH)
403 memset(&state, 0, sizeof(state));
404 if (res_ninit(handle)) {
405 RETURN_FALSE;
406 }
407 #else
408 res_init();
409 #endif
410
411 i = php_dns_search(handle, hostname, C_IN, type, answer.qb2, sizeof answer);
412 php_dns_free_handle(handle);
413
414 if (i < 0) {
415 RETURN_FALSE;
416 }
417 hp = (HEADER *)&answer;
418 RETURN_BOOL(ntohs(hp->ancount) != 0);
419 }
420 /* }}} */
421
422 #if HAVE_FULL_DNS_FUNCS
423
424 #define CHECKCP(n) do { \
425 if (cp + n > end) { \
426 return NULL; \
427 } \
428 } while (0)
429
430 /* {{{ php_parserr */
php_parserr(u_char * cp,u_char * end,querybuf * answer,int type_to_fetch,int store,int raw,zval * subarray)431 static u_char *php_parserr(u_char *cp, u_char *end, querybuf *answer, int type_to_fetch, int store, int raw, zval *subarray)
432 {
433 u_short type, class, dlen;
434 u_long ttl;
435 long n, i;
436 u_short s;
437 u_char *tp, *p;
438 char name[MAXHOSTNAMELEN];
439 int have_v6_break = 0, in_v6_break = 0;
440
441 ZVAL_UNDEF(subarray);
442
443 n = dn_expand(answer->qb2, end, cp, name, sizeof(name) - 2);
444 if (n < 0) {
445 return NULL;
446 }
447 cp += n;
448
449 CHECKCP(10);
450 GETSHORT(type, cp);
451 GETSHORT(class, cp);
452 GETLONG(ttl, cp);
453 GETSHORT(dlen, cp);
454 CHECKCP(dlen);
455 if (dlen == 0) {
456 /* No data in the response - nothing to do */
457 return NULL;
458 }
459 if (type_to_fetch != T_ANY && type != type_to_fetch) {
460 cp += dlen;
461 return cp;
462 }
463
464 if (!store) {
465 cp += dlen;
466 return cp;
467 }
468
469 array_init(subarray);
470
471 add_assoc_string(subarray, "host", name);
472 add_assoc_string(subarray, "class", "IN");
473 add_assoc_long(subarray, "ttl", ttl);
474 (void) class;
475
476 if (raw) {
477 add_assoc_long(subarray, "type", type);
478 add_assoc_stringl(subarray, "data", (char*) cp, (uint32_t) dlen);
479 cp += dlen;
480 return cp;
481 }
482
483 switch (type) {
484 case DNS_T_A:
485 CHECKCP(4);
486 add_assoc_string(subarray, "type", "A");
487 snprintf(name, sizeof(name), "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
488 add_assoc_string(subarray, "ip", name);
489 cp += dlen;
490 break;
491 case DNS_T_MX:
492 CHECKCP(2);
493 add_assoc_string(subarray, "type", "MX");
494 GETSHORT(n, cp);
495 add_assoc_long(subarray, "pri", n);
496 /* no break; */
497 case DNS_T_CNAME:
498 if (type == DNS_T_CNAME) {
499 add_assoc_string(subarray, "type", "CNAME");
500 }
501 /* no break; */
502 case DNS_T_NS:
503 if (type == DNS_T_NS) {
504 add_assoc_string(subarray, "type", "NS");
505 }
506 /* no break; */
507 case DNS_T_PTR:
508 if (type == DNS_T_PTR) {
509 add_assoc_string(subarray, "type", "PTR");
510 }
511 n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
512 if (n < 0) {
513 return NULL;
514 }
515 cp += n;
516 add_assoc_string(subarray, "target", name);
517 break;
518 case DNS_T_HINFO:
519 /* See RFC 1010 for values */
520 add_assoc_string(subarray, "type", "HINFO");
521 CHECKCP(1);
522 n = *cp & 0xFF;
523 cp++;
524 CHECKCP(n);
525 add_assoc_stringl(subarray, "cpu", (char*)cp, n);
526 cp += n;
527 CHECKCP(1);
528 n = *cp & 0xFF;
529 cp++;
530 CHECKCP(n);
531 add_assoc_stringl(subarray, "os", (char*)cp, n);
532 cp += n;
533 break;
534 case DNS_T_CAA:
535 /* See RFC 6844 for values https://tools.ietf.org/html/rfc6844 */
536 add_assoc_string(subarray, "type", "CAA");
537 // 1 flag byte
538 CHECKCP(1);
539 n = *cp & 0xFF;
540 add_assoc_long(subarray, "flags", n);
541 cp++;
542 // Tag length (1 byte)
543 CHECKCP(1);
544 n = *cp & 0xFF;
545 cp++;
546 CHECKCP(n);
547 add_assoc_stringl(subarray, "tag", (char*)cp, n);
548 cp += n;
549 if ( (size_t) dlen < ((size_t)n) + 2 ) {
550 return NULL;
551 }
552 n = dlen - n - 2;
553 CHECKCP(n);
554 add_assoc_stringl(subarray, "value", (char*)cp, n);
555 cp += n;
556 break;
557 case DNS_T_TXT:
558 {
559 int l1 = 0, l2 = 0;
560 zval entries;
561 zend_string *tp;
562
563 add_assoc_string(subarray, "type", "TXT");
564 tp = zend_string_alloc(dlen, 0);
565
566 array_init(&entries);
567
568 while (l1 < dlen) {
569 n = cp[l1];
570 if ((l1 + n) >= dlen) {
571 // Invalid chunk length, truncate
572 n = dlen - (l1 + 1);
573 }
574 if (n) {
575 memcpy(ZSTR_VAL(tp) + l2 , cp + l1 + 1, n);
576 add_next_index_stringl(&entries, (char *) cp + l1 + 1, n);
577 }
578 l1 = l1 + n + 1;
579 l2 = l2 + n;
580 }
581 ZSTR_VAL(tp)[l2] = '\0';
582 ZSTR_LEN(tp) = l2;
583 cp += dlen;
584
585 add_assoc_str(subarray, "txt", tp);
586 add_assoc_zval(subarray, "entries", &entries);
587 }
588 break;
589 case DNS_T_SOA:
590 add_assoc_string(subarray, "type", "SOA");
591 n = dn_expand(answer->qb2, end, cp, name, (sizeof name) -2);
592 if (n < 0) {
593 return NULL;
594 }
595 cp += n;
596 add_assoc_string(subarray, "mname", name);
597 n = dn_expand(answer->qb2, end, cp, name, (sizeof name) -2);
598 if (n < 0) {
599 return NULL;
600 }
601 cp += n;
602 add_assoc_string(subarray, "rname", name);
603 CHECKCP(5*4);
604 GETLONG(n, cp);
605 add_assoc_long(subarray, "serial", n);
606 GETLONG(n, cp);
607 add_assoc_long(subarray, "refresh", n);
608 GETLONG(n, cp);
609 add_assoc_long(subarray, "retry", n);
610 GETLONG(n, cp);
611 add_assoc_long(subarray, "expire", n);
612 GETLONG(n, cp);
613 add_assoc_long(subarray, "minimum-ttl", n);
614 break;
615 case DNS_T_AAAA:
616 tp = (u_char*)name;
617 CHECKCP(8*2);
618 for(i=0; i < 8; i++) {
619 GETSHORT(s, cp);
620 if (s != 0) {
621 if (tp > (u_char *)name) {
622 in_v6_break = 0;
623 tp[0] = ':';
624 tp++;
625 }
626 tp += sprintf((char*)tp,"%x",s);
627 } else {
628 if (!have_v6_break) {
629 have_v6_break = 1;
630 in_v6_break = 1;
631 tp[0] = ':';
632 tp++;
633 } else if (!in_v6_break) {
634 tp[0] = ':';
635 tp++;
636 tp[0] = '0';
637 tp++;
638 }
639 }
640 }
641 if (have_v6_break && in_v6_break) {
642 tp[0] = ':';
643 tp++;
644 }
645 tp[0] = '\0';
646 add_assoc_string(subarray, "type", "AAAA");
647 add_assoc_string(subarray, "ipv6", name);
648 break;
649 case DNS_T_A6:
650 p = cp;
651 add_assoc_string(subarray, "type", "A6");
652 CHECKCP(1);
653 n = ((int)cp[0]) & 0xFF;
654 cp++;
655 add_assoc_long(subarray, "masklen", n);
656 tp = (u_char*)name;
657 if (n > 15) {
658 have_v6_break = 1;
659 in_v6_break = 1;
660 tp[0] = ':';
661 tp++;
662 }
663 if (n % 16 > 8) {
664 /* Partial short */
665 if (cp[0] != 0) {
666 if (tp > (u_char *)name) {
667 in_v6_break = 0;
668 tp[0] = ':';
669 tp++;
670 }
671 sprintf((char*)tp, "%x", cp[0] & 0xFF);
672 } else {
673 if (!have_v6_break) {
674 have_v6_break = 1;
675 in_v6_break = 1;
676 tp[0] = ':';
677 tp++;
678 } else if (!in_v6_break) {
679 tp[0] = ':';
680 tp++;
681 tp[0] = '0';
682 tp++;
683 }
684 }
685 cp++;
686 }
687 for (i = (n + 8) / 16; i < 8; i++) {
688 CHECKCP(2);
689 GETSHORT(s, cp);
690 if (s != 0) {
691 if (tp > (u_char *)name) {
692 in_v6_break = 0;
693 tp[0] = ':';
694 tp++;
695 }
696 tp += sprintf((char*)tp,"%x",s);
697 } else {
698 if (!have_v6_break) {
699 have_v6_break = 1;
700 in_v6_break = 1;
701 tp[0] = ':';
702 tp++;
703 } else if (!in_v6_break) {
704 tp[0] = ':';
705 tp++;
706 tp[0] = '0';
707 tp++;
708 }
709 }
710 }
711 if (have_v6_break && in_v6_break) {
712 tp[0] = ':';
713 tp++;
714 }
715 tp[0] = '\0';
716 add_assoc_string(subarray, "ipv6", name);
717 if (cp < p + dlen) {
718 n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
719 if (n < 0) {
720 return NULL;
721 }
722 cp += n;
723 add_assoc_string(subarray, "chain", name);
724 }
725 break;
726 case DNS_T_SRV:
727 CHECKCP(3*2);
728 add_assoc_string(subarray, "type", "SRV");
729 GETSHORT(n, cp);
730 add_assoc_long(subarray, "pri", n);
731 GETSHORT(n, cp);
732 add_assoc_long(subarray, "weight", n);
733 GETSHORT(n, cp);
734 add_assoc_long(subarray, "port", n);
735 n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
736 if (n < 0) {
737 return NULL;
738 }
739 cp += n;
740 add_assoc_string(subarray, "target", name);
741 break;
742 case DNS_T_NAPTR:
743 CHECKCP(2*2);
744 add_assoc_string(subarray, "type", "NAPTR");
745 GETSHORT(n, cp);
746 add_assoc_long(subarray, "order", n);
747 GETSHORT(n, cp);
748 add_assoc_long(subarray, "pref", n);
749
750 CHECKCP(1);
751 n = (cp[0] & 0xFF);
752 cp++;
753 CHECKCP(n);
754 add_assoc_stringl(subarray, "flags", (char*)cp, n);
755 cp += n;
756
757 CHECKCP(1);
758 n = (cp[0] & 0xFF);
759 cp++;
760 CHECKCP(n);
761 add_assoc_stringl(subarray, "services", (char*)cp, n);
762 cp += n;
763
764 CHECKCP(1);
765 n = (cp[0] & 0xFF);
766 cp++;
767 CHECKCP(n);
768 add_assoc_stringl(subarray, "regex", (char*)cp, n);
769 cp += n;
770
771 n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
772 if (n < 0) {
773 return NULL;
774 }
775 cp += n;
776 add_assoc_string(subarray, "replacement", name);
777 break;
778 default:
779 zval_ptr_dtor(subarray);
780 ZVAL_UNDEF(subarray);
781 cp += dlen;
782 break;
783 }
784
785 return cp;
786 }
787 /* }}} */
788
789 /* {{{ proto array|false dns_get_record(string hostname [, int type[, array &authns[, array &addtl[, bool raw]]]])
790 Get any Resource Record corresponding to a given Internet host name */
PHP_FUNCTION(dns_get_record)791 PHP_FUNCTION(dns_get_record)
792 {
793 char *hostname;
794 size_t hostname_len;
795 zend_long type_param = PHP_DNS_ANY;
796 zval *authns = NULL, *addtl = NULL;
797 int type_to_fetch;
798 #if defined(HAVE_DNS_SEARCH)
799 struct sockaddr_storage from;
800 uint32_t fromsize = sizeof(from);
801 dns_handle_t handle;
802 #elif defined(HAVE_RES_NSEARCH)
803 struct __res_state state;
804 struct __res_state *handle = &state;
805 #endif
806 HEADER *hp;
807 querybuf answer;
808 u_char *cp = NULL, *end = NULL;
809 int n, qd, an, ns = 0, ar = 0;
810 int type, first_query = 1, store_results = 1;
811 zend_bool raw = 0;
812
813 ZEND_PARSE_PARAMETERS_START(1, 5)
814 Z_PARAM_STRING(hostname, hostname_len)
815 Z_PARAM_OPTIONAL
816 Z_PARAM_LONG(type_param)
817 Z_PARAM_ZVAL_DEREF_EX(authns, 1, 0)
818 Z_PARAM_ZVAL_DEREF_EX(addtl, 1, 0)
819 Z_PARAM_BOOL(raw)
820 ZEND_PARSE_PARAMETERS_END();
821
822 if (authns) {
823 zval_ptr_dtor(authns);
824 array_init(authns);
825 }
826 if (addtl) {
827 zval_ptr_dtor(addtl);
828 array_init(addtl);
829 }
830
831 if (!raw) {
832 if ((type_param & ~PHP_DNS_ALL) && (type_param != PHP_DNS_ANY)) {
833 php_error_docref(NULL, E_WARNING, "Type '" ZEND_LONG_FMT "' not supported", type_param);
834 RETURN_FALSE;
835 }
836 } else {
837 if ((type_param < 1) || (type_param > 0xFFFF)) {
838 php_error_docref(NULL, E_WARNING,
839 "Numeric DNS record type must be between 1 and 65535, '" ZEND_LONG_FMT "' given", type_param);
840 RETURN_FALSE;
841 }
842 }
843
844 /* Initialize the return array */
845 array_init(return_value);
846
847 /* - We emulate an or'ed type mask by querying type by type. (Steps 0 - NUMTYPES-1 )
848 * If additional info is wanted we check again with DNS_T_ANY (step NUMTYPES / NUMTYPES+1 )
849 * store_results is used to skip storing the results retrieved in step
850 * NUMTYPES+1 when results were already fetched.
851 * - In case of PHP_DNS_ANY we use the directly fetch DNS_T_ANY. (step NUMTYPES+1 )
852 * - In case of raw mode, we query only the requestd type instead of looping type by type
853 * before going with the additional info stuff.
854 */
855
856 if (raw) {
857 type = -1;
858 } else if (type_param == PHP_DNS_ANY) {
859 type = PHP_DNS_NUM_TYPES + 1;
860 } else {
861 type = 0;
862 }
863
864 for ( ;
865 type < (addtl ? (PHP_DNS_NUM_TYPES + 2) : PHP_DNS_NUM_TYPES) || first_query;
866 type++
867 ) {
868 first_query = 0;
869 switch (type) {
870 case -1: /* raw */
871 type_to_fetch = type_param;
872 /* skip over the rest and go directly to additional records */
873 type = PHP_DNS_NUM_TYPES - 1;
874 break;
875 case 0:
876 type_to_fetch = type_param&PHP_DNS_A ? DNS_T_A : 0;
877 break;
878 case 1:
879 type_to_fetch = type_param&PHP_DNS_NS ? DNS_T_NS : 0;
880 break;
881 case 2:
882 type_to_fetch = type_param&PHP_DNS_CNAME ? DNS_T_CNAME : 0;
883 break;
884 case 3:
885 type_to_fetch = type_param&PHP_DNS_SOA ? DNS_T_SOA : 0;
886 break;
887 case 4:
888 type_to_fetch = type_param&PHP_DNS_PTR ? DNS_T_PTR : 0;
889 break;
890 case 5:
891 type_to_fetch = type_param&PHP_DNS_HINFO ? DNS_T_HINFO : 0;
892 break;
893 case 6:
894 type_to_fetch = type_param&PHP_DNS_MX ? DNS_T_MX : 0;
895 break;
896 case 7:
897 type_to_fetch = type_param&PHP_DNS_TXT ? DNS_T_TXT : 0;
898 break;
899 case 8:
900 type_to_fetch = type_param&PHP_DNS_AAAA ? DNS_T_AAAA : 0;
901 break;
902 case 9:
903 type_to_fetch = type_param&PHP_DNS_SRV ? DNS_T_SRV : 0;
904 break;
905 case 10:
906 type_to_fetch = type_param&PHP_DNS_NAPTR ? DNS_T_NAPTR : 0;
907 break;
908 case 11:
909 type_to_fetch = type_param&PHP_DNS_A6 ? DNS_T_A6 : 0;
910 break;
911 case 12:
912 type_to_fetch = type_param&PHP_DNS_CAA ? DNS_T_CAA : 0;
913 break;
914 case PHP_DNS_NUM_TYPES:
915 store_results = 0;
916 continue;
917 default:
918 case (PHP_DNS_NUM_TYPES + 1):
919 type_to_fetch = DNS_T_ANY;
920 break;
921 }
922
923 if (type_to_fetch) {
924 #if defined(HAVE_DNS_SEARCH)
925 handle = dns_open(NULL);
926 if (handle == NULL) {
927 zend_array_destroy(Z_ARR_P(return_value));
928 RETURN_FALSE;
929 }
930 #elif defined(HAVE_RES_NSEARCH)
931 memset(&state, 0, sizeof(state));
932 if (res_ninit(handle)) {
933 zend_array_destroy(Z_ARR_P(return_value));
934 RETURN_FALSE;
935 }
936 #else
937 res_init();
938 #endif
939
940 n = php_dns_search(handle, hostname, C_IN, type_to_fetch, answer.qb2, sizeof answer);
941
942 if (n < 0) {
943 php_dns_free_handle(handle);
944 switch (h_errno) {
945 case NO_DATA:
946 case HOST_NOT_FOUND:
947 continue;
948
949 case NO_RECOVERY:
950 php_error_docref(NULL, E_WARNING, "An unexpected server failure occurred.");
951 break;
952
953 case TRY_AGAIN:
954 php_error_docref(NULL, E_WARNING, "A temporary server error occurred.");
955 break;
956
957 default:
958 php_error_docref(NULL, E_WARNING, "DNS Query failed");
959 }
960 zend_array_destroy(Z_ARR_P(return_value));
961 RETURN_FALSE;
962 }
963
964 cp = answer.qb2 + HFIXEDSZ;
965 end = answer.qb2 + n;
966 hp = (HEADER *)&answer;
967 qd = ntohs(hp->qdcount);
968 an = ntohs(hp->ancount);
969 ns = ntohs(hp->nscount);
970 ar = ntohs(hp->arcount);
971
972 /* Skip QD entries, they're only used by dn_expand later on */
973 while (qd-- > 0) {
974 n = dn_skipname(cp, end);
975 if (n < 0) {
976 php_error_docref(NULL, E_WARNING, "Unable to parse DNS data received");
977 zend_array_destroy(Z_ARR_P(return_value));
978 php_dns_free_handle(handle);
979 RETURN_FALSE;
980 }
981 cp += n + QFIXEDSZ;
982 }
983
984 /* YAY! Our real answers! */
985 while (an-- && cp && cp < end) {
986 zval retval;
987
988 cp = php_parserr(cp, end, &answer, type_to_fetch, store_results, raw, &retval);
989 if (Z_TYPE(retval) != IS_UNDEF && store_results) {
990 add_next_index_zval(return_value, &retval);
991 }
992 }
993
994 if (authns || addtl) {
995 /* List of Authoritative Name Servers
996 * Process when only requesting addtl so that we can skip through the section
997 */
998 while (ns-- > 0 && cp && cp < end) {
999 zval retval;
1000
1001 cp = php_parserr(cp, end, &answer, DNS_T_ANY, authns != NULL, raw, &retval);
1002 if (Z_TYPE(retval) != IS_UNDEF) {
1003 add_next_index_zval(authns, &retval);
1004 }
1005 }
1006 }
1007
1008 if (addtl) {
1009 /* Additional records associated with authoritative name servers */
1010 while (ar-- > 0 && cp && cp < end) {
1011 zval retval;
1012
1013 cp = php_parserr(cp, end, &answer, DNS_T_ANY, 1, raw, &retval);
1014 if (Z_TYPE(retval) != IS_UNDEF) {
1015 add_next_index_zval(addtl, &retval);
1016 }
1017 }
1018 }
1019 php_dns_free_handle(handle);
1020 }
1021 }
1022 }
1023 /* }}} */
1024
1025 /* {{{ proto bool dns_get_mx(string hostname, array mxhosts [, array weight])
1026 Get MX records corresponding to a given Internet host name */
PHP_FUNCTION(dns_get_mx)1027 PHP_FUNCTION(dns_get_mx)
1028 {
1029 char *hostname;
1030 size_t hostname_len;
1031 zval *mx_list, *weight_list = NULL;
1032 int count, qdc;
1033 u_short type, weight;
1034 querybuf answer;
1035 char buf[MAXHOSTNAMELEN];
1036 HEADER *hp;
1037 u_char *cp, *end;
1038 int i;
1039 #if defined(HAVE_DNS_SEARCH)
1040 struct sockaddr_storage from;
1041 uint32_t fromsize = sizeof(from);
1042 dns_handle_t handle;
1043 #elif defined(HAVE_RES_NSEARCH)
1044 struct __res_state state;
1045 struct __res_state *handle = &state;
1046 #endif
1047
1048 ZEND_PARSE_PARAMETERS_START(2, 3)
1049 Z_PARAM_STRING(hostname, hostname_len)
1050 Z_PARAM_ZVAL_DEREF(mx_list)
1051 Z_PARAM_OPTIONAL
1052 Z_PARAM_ZVAL_DEREF(weight_list)
1053 ZEND_PARSE_PARAMETERS_END();
1054
1055 zval_ptr_dtor(mx_list);
1056 array_init(mx_list);
1057
1058 if (weight_list) {
1059 zval_ptr_dtor(weight_list);
1060 array_init(weight_list);
1061 }
1062
1063 #if defined(HAVE_DNS_SEARCH)
1064 handle = dns_open(NULL);
1065 if (handle == NULL) {
1066 RETURN_FALSE;
1067 }
1068 #elif defined(HAVE_RES_NSEARCH)
1069 memset(&state, 0, sizeof(state));
1070 if (res_ninit(handle)) {
1071 RETURN_FALSE;
1072 }
1073 #else
1074 res_init();
1075 #endif
1076
1077 i = php_dns_search(handle, hostname, C_IN, DNS_T_MX, answer.qb2, sizeof answer);
1078 if (i < 0) {
1079 php_dns_free_handle(handle);
1080 RETURN_FALSE;
1081 }
1082 hp = (HEADER *)&answer;
1083 cp = answer.qb2 + HFIXEDSZ;
1084 end = answer.qb2 + i;
1085 for (qdc = ntohs((unsigned short)hp->qdcount); qdc--; cp += i + QFIXEDSZ) {
1086 if ((i = dn_skipname(cp, end)) < 0 ) {
1087 php_dns_free_handle(handle);
1088 RETURN_FALSE;
1089 }
1090 }
1091 count = ntohs((unsigned short)hp->ancount);
1092 while (--count >= 0 && cp < end) {
1093 if ((i = dn_skipname(cp, end)) < 0 ) {
1094 php_dns_free_handle(handle);
1095 RETURN_FALSE;
1096 }
1097 cp += i;
1098 GETSHORT(type, cp);
1099 cp += INT16SZ + INT32SZ;
1100 GETSHORT(i, cp);
1101 if (type != DNS_T_MX) {
1102 cp += i;
1103 continue;
1104 }
1105 GETSHORT(weight, cp);
1106 if ((i = dn_expand(answer.qb2, end, cp, buf, sizeof(buf)-1)) < 0) {
1107 php_dns_free_handle(handle);
1108 RETURN_FALSE;
1109 }
1110 cp += i;
1111 add_next_index_string(mx_list, buf);
1112 if (weight_list) {
1113 add_next_index_long(weight_list, weight);
1114 }
1115 }
1116 php_dns_free_handle(handle);
1117 RETURN_BOOL(zend_hash_num_elements(Z_ARRVAL_P(mx_list)) != 0);
1118 }
1119 /* }}} */
1120 #endif /* HAVE_FULL_DNS_FUNCS */
1121 #endif /* !defined(PHP_WIN32) && HAVE_DNS_SEARCH_FUNC */
1122
1123 #if HAVE_FULL_DNS_FUNCS && !defined(PHP_WIN32)
PHP_MINIT_FUNCTION(dns)1124 PHP_MINIT_FUNCTION(dns) {
1125 REGISTER_LONG_CONSTANT("DNS_A", PHP_DNS_A, CONST_CS | CONST_PERSISTENT);
1126 REGISTER_LONG_CONSTANT("DNS_NS", PHP_DNS_NS, CONST_CS | CONST_PERSISTENT);
1127 REGISTER_LONG_CONSTANT("DNS_CNAME", PHP_DNS_CNAME, CONST_CS | CONST_PERSISTENT);
1128 REGISTER_LONG_CONSTANT("DNS_SOA", PHP_DNS_SOA, CONST_CS | CONST_PERSISTENT);
1129 REGISTER_LONG_CONSTANT("DNS_PTR", PHP_DNS_PTR, CONST_CS | CONST_PERSISTENT);
1130 REGISTER_LONG_CONSTANT("DNS_HINFO", PHP_DNS_HINFO, CONST_CS | CONST_PERSISTENT);
1131 REGISTER_LONG_CONSTANT("DNS_CAA", PHP_DNS_CAA, CONST_CS | CONST_PERSISTENT);
1132 REGISTER_LONG_CONSTANT("DNS_MX", PHP_DNS_MX, CONST_CS | CONST_PERSISTENT);
1133 REGISTER_LONG_CONSTANT("DNS_TXT", PHP_DNS_TXT, CONST_CS | CONST_PERSISTENT);
1134 REGISTER_LONG_CONSTANT("DNS_SRV", PHP_DNS_SRV, CONST_CS | CONST_PERSISTENT);
1135 REGISTER_LONG_CONSTANT("DNS_NAPTR", PHP_DNS_NAPTR, CONST_CS | CONST_PERSISTENT);
1136 REGISTER_LONG_CONSTANT("DNS_AAAA", PHP_DNS_AAAA, CONST_CS | CONST_PERSISTENT);
1137 REGISTER_LONG_CONSTANT("DNS_A6", PHP_DNS_A6, CONST_CS | CONST_PERSISTENT);
1138 REGISTER_LONG_CONSTANT("DNS_ANY", PHP_DNS_ANY, CONST_CS | CONST_PERSISTENT);
1139 REGISTER_LONG_CONSTANT("DNS_ALL", PHP_DNS_ALL, CONST_CS | CONST_PERSISTENT);
1140 return SUCCESS;
1141 }
1142 #endif /* HAVE_FULL_DNS_FUNCS */
1143
1144 /*
1145 * Local variables:
1146 * tab-width: 4
1147 * c-basic-offset: 4
1148 * End:
1149 * vim600: sw=4 ts=4 fdm=marker
1150 * vim<600: sw=4 ts=4
1151 */
1152