dns_win32.c 15 KB

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