dns.c 29 KB

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