dns.c 29 KB

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