dns_win32.c 15 KB

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