dns_diagnostics.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  3. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/spf.inc.php';
  4. define('state_good', '<i class="bi bi-check-lg text-success"></i>');
  5. define('state_missing', '<i class="bi bi-x-lg text-danger"></i>');
  6. define('state_nomatch', "?");
  7. define('state_optional', " <sup>2</sup>");
  8. if (isset($_SESSION['mailcow_cc_role']) && ($_SESSION['mailcow_cc_role'] == "admin"|| $_SESSION['mailcow_cc_role'] == "domainadmin")) {
  9. $alias_domains = array();
  10. if (isset($_GET['domain'])) {
  11. $domain_details = mailbox('get', 'domain_details', $_GET['domain']);
  12. if ($domain_details !== false) {
  13. $domain = $_GET['domain'];
  14. $alias_domains = array_merge($alias_domains, mailbox('get', 'alias_domains', $domain));
  15. }
  16. else {
  17. echo "No such domain in context";
  18. exit();
  19. }
  20. }
  21. $ch = curl_init('http://ip4.mailcow.email');
  22. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  23. curl_setopt($ch, CURLOPT_VERBOSE, false);
  24. curl_setopt($ch, CURLOPT_HEADER, false);
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  27. $ip = curl_exec($ch);
  28. curl_close($ch);
  29. $ch = curl_init('http://ip6.mailcow.email');
  30. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
  31. curl_setopt($ch, CURLOPT_VERBOSE, false);
  32. curl_setopt($ch, CURLOPT_HEADER, false);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  35. $ip6 = curl_exec($ch);
  36. curl_close($ch);
  37. $ptr = implode('.', array_reverse(explode('.', $ip))) . '.in-addr.arpa';
  38. if (!empty($ip6)) {
  39. $ip6_full = str_replace('::', str_repeat(':', 9-substr_count($ip6, ':')), $ip6);
  40. $ip6_full = str_replace('::', ':0:', $ip6_full);
  41. $ip6_full = str_replace('::', ':0:', $ip6_full);
  42. $ptr6 = '';
  43. foreach (explode(':', $ip6_full) as $part) {
  44. $ptr6 .= str_pad($part, 4, '0', STR_PAD_LEFT);
  45. }
  46. $ptr6 = implode('.', array_reverse(str_split($ptr6, 1))) . '.ip6.arpa';
  47. }
  48. $https_port = strpos($_SERVER['HTTP_HOST'], ':');
  49. if ($https_port === FALSE) {
  50. $https_port = 443;
  51. }
  52. else {
  53. $https_port = substr($_SERVER['HTTP_HOST'], $https_port+1);
  54. }
  55. if (!isset($autodiscover_config['sieve'])) {
  56. $autodiscover_config['sieve'] = array(
  57. 'server' => $mailcow_hostname,
  58. 'port' => array_pop(explode(':', getenv('SIEVE_PORT')))
  59. );
  60. }
  61. // Init records array
  62. $spf_link = '<a href="https://en.wikipedia.org/wiki/Sender_Policy_Framework" target="_blank">SPF Record Syntax</a><br />';
  63. $dmarc_link = '<a href="https://www.kitterman.com/dmarc/assistant.html" target="_blank">DMARC Assistant</a>';
  64. $records = array();
  65. if ($_SESSION['mailcow_cc_role'] == "admin") {
  66. $records[] = array(
  67. $mailcow_hostname,
  68. 'A',
  69. $ip
  70. );
  71. $records[] = array(
  72. $ptr,
  73. 'PTR',
  74. $mailcow_hostname
  75. );
  76. if (!empty($ip6)) {
  77. $records[] = array(
  78. $mailcow_hostname,
  79. 'AAAA',
  80. expand_ipv6($ip6)
  81. );
  82. $records[] = array(
  83. $ptr6,
  84. 'PTR',
  85. $mailcow_hostname
  86. );
  87. }
  88. $records[] = array(
  89. '_25._tcp.' . $autodiscover_config['smtp']['server'],
  90. 'TLSA',
  91. generate_tlsa_digest($autodiscover_config['smtp']['server'], 25, 1)
  92. );
  93. }
  94. $records[] = array(
  95. $domain,
  96. 'MX',
  97. $mailcow_hostname
  98. );
  99. if (!in_array($domain, $alias_domains)) {
  100. $records[] = array(
  101. 'autodiscover.' . $domain,
  102. 'CNAME',
  103. $mailcow_hostname
  104. );
  105. $records[] = array(
  106. '_autodiscover._tcp.' . $domain,
  107. 'SRV',
  108. $mailcow_hostname . ' ' . $https_port
  109. );
  110. $records[] = array(
  111. 'autoconfig.' . $domain,
  112. 'CNAME',
  113. $mailcow_hostname
  114. );
  115. if ($domain_details['xmpp'] === 1 && isset($domain_details['xmpp_prefix'])) {
  116. $records[] = array(
  117. $domain_details['xmpp_prefix'] . '.' . $domain,
  118. 'CNAME',
  119. $mailcow_hostname
  120. );
  121. $records[] = array(
  122. '*.' . $domain_details['xmpp_prefix'] . '.' . $domain,
  123. 'CNAME',
  124. $mailcow_hostname
  125. );
  126. $records[] = array(
  127. '_xmpp-client._tcp.' . $domain_details['xmpp_prefix'] . '.' . $domain,
  128. 'SRV',
  129. $mailcow_hostname . ' ' . array_pop(explode(':', getenv('XMPP_C2S_PORT')))
  130. );
  131. $records[] = array(
  132. '_xmpp-server._tcp.' . $domain_details['xmpp_prefix'] . '.' . $domain,
  133. 'SRV',
  134. $mailcow_hostname . ' ' . array_pop(explode(':', getenv('XMPP_S2S_PORT')))
  135. );
  136. }
  137. }
  138. $records[] = array(
  139. $domain,
  140. 'TXT',
  141. $spf_link,
  142. state_optional
  143. );
  144. $records[] = array(
  145. '_dmarc.' . $domain,
  146. 'TXT',
  147. $dmarc_link,
  148. state_optional
  149. );
  150. if (!empty($dkim = dkim('details', $domain))) {
  151. $records[] = array(
  152. $dkim['dkim_selector'] . '._domainkey.' . $domain,
  153. 'TXT',
  154. $dkim['dkim_txt']
  155. );
  156. }
  157. if (!in_array($domain, $alias_domains)) {
  158. $current_records = dns_get_record('_pop3._tcp.' . $domain, DNS_SRV);
  159. if (count($current_records) == 0 || $current_records[0]['target'] != '') {
  160. if ($autodiscover_config['pop3']['tlsport'] != '110') {
  161. $records[] = array(
  162. '_pop3._tcp.' . $domain,
  163. 'SRV',
  164. $autodiscover_config['pop3']['server'] . ' ' . $autodiscover_config['pop3']['tlsport']
  165. );
  166. }
  167. }
  168. else {
  169. $records[] = array(
  170. '_pop3._tcp.' . $domain,
  171. 'SRV',
  172. '. 0'
  173. );
  174. }
  175. $current_records = dns_get_record('_pop3s._tcp.' . $domain, DNS_SRV);
  176. if (count($current_records) == 0 || $current_records[0]['target'] != '') {
  177. if ($autodiscover_config['pop3']['port'] != '995') {
  178. $records[] = array(
  179. '_pop3s._tcp.' . $domain,
  180. 'SRV',
  181. $autodiscover_config['pop3']['server'] . ' ' . $autodiscover_config['pop3']['port']
  182. );
  183. }
  184. }
  185. else {
  186. $records[] = array(
  187. '_pop3s._tcp.' . $domain,
  188. 'SRV',
  189. '. 0'
  190. );
  191. }
  192. if ($autodiscover_config['imap']['tlsport'] != '143') {
  193. $records[] = array(
  194. '_imap._tcp.' . $domain,
  195. 'SRV',
  196. $autodiscover_config['imap']['server'] . ' ' . $autodiscover_config['imap']['tlsport']
  197. );
  198. }
  199. if ($autodiscover_config['imap']['port'] != '993') {
  200. $records[] = array(
  201. '_imaps._tcp.' . $domain,
  202. 'SRV',
  203. $autodiscover_config['imap']['server'] . ' ' . $autodiscover_config['imap']['port']
  204. );
  205. }
  206. if ($autodiscover_config['smtp']['tlsport'] != '587') {
  207. $records[] = array(
  208. '_submission._tcp.' . $domain,
  209. 'SRV',
  210. $autodiscover_config['smtp']['server'] . ' ' . $autodiscover_config['smtp']['tlsport']
  211. );
  212. }
  213. if ($autodiscover_config['smtp']['port'] != '465') {
  214. $records[] = array(
  215. '_smtps._tcp.' . $domain,
  216. 'SRV',
  217. $autodiscover_config['smtp']['server'] . ' ' . $autodiscover_config['smtp']['port']
  218. );
  219. }
  220. if ($autodiscover_config['sieve']['port'] != '4190') {
  221. $records[] = array(
  222. '_sieve._tcp.' . $domain,
  223. 'SRV',
  224. $autodiscover_config['sieve']['server'] . ' ' . $autodiscover_config['sieve']['port']
  225. );
  226. }
  227. }
  228. $record_types = array(
  229. 'A' => DNS_A,
  230. 'AAAA' => DNS_AAAA,
  231. 'CNAME' => DNS_CNAME,
  232. 'MX' => DNS_MX,
  233. 'PTR' => DNS_PTR,
  234. 'SRV' => DNS_SRV,
  235. 'TXT' => DNS_TXT,
  236. );
  237. $data_field = array(
  238. 'A' => 'ip',
  239. 'AAAA' => 'ipv6',
  240. 'CNAME' => 'target',
  241. 'MX' => 'target',
  242. 'PTR' => 'target',
  243. 'SRV' => 'data',
  244. 'TLSA' => 'data',
  245. 'TXT' => 'txt',
  246. );
  247. ?>
  248. <div class="table-responsive" id="dnstable">
  249. <table class="table table-striped">
  250. <tr>
  251. <th><?=$lang['diagnostics']['dns_records_name'];?></th>
  252. <th><?=$lang['diagnostics']['dns_records_type'];?></th>
  253. <th><?=$lang['diagnostics']['dns_records_data'];?></th>
  254. <th><?=$lang['diagnostics']['dns_records_status'];?></th>
  255. </tr>
  256. <?php
  257. foreach ($records as &$record) {
  258. $record[1] = strtoupper($record[1]);
  259. $state = state_optional;
  260. if ($record[1] == 'TLSA') {
  261. $currents = dns_get_record($record[0], 52, $_, $_, TRUE);
  262. foreach ($currents as &$current) {
  263. $current['type'] = 'TLSA';
  264. $current['cert_usage'] = hexdec(bin2hex($current['data']{0}));
  265. $current['selector'] = hexdec(bin2hex($current['data']{1}));
  266. $current['match_type'] = hexdec(bin2hex($current['data']{2}));
  267. $current['cert_data'] = bin2hex(substr($current['data'], 3));
  268. $current['data'] = $current['cert_usage'] . ' ' . $current['selector'] . ' ' . $current['match_type'] . ' ' . $current['cert_data'];
  269. }
  270. unset($current);
  271. }
  272. else {
  273. $currents = dns_get_record($record[0], $record_types[$record[1]]);
  274. if ($record[0] == $mailcow_hostname && ($record[1] == "A" || $record[1] == "AAAA")) {
  275. if (!empty(dns_get_record($record[0], DNS_CNAME))) {
  276. $currents[0]['ip'] = state_missing . ' <b>(CNAME)</b>';
  277. $currents[0]['ipv6'] = state_missing . ' <b>(CNAME)</b>';
  278. }
  279. }
  280. if ($record[1] == 'SRV') {
  281. foreach ($currents as &$current) {
  282. if ($current['target'] == '') {
  283. $current['target'] = '.';
  284. $current['port'] = '0';
  285. }
  286. $current['data'] = $current['target'] . ' ' . $current['port'];
  287. }
  288. unset($current);
  289. }
  290. elseif ($record[1] == 'TXT') {
  291. foreach ($currents as &$current) {
  292. unset($current);
  293. }
  294. unset($current);
  295. }
  296. elseif ($record[1] == 'AAAA') {
  297. foreach ($currents as &$current) {
  298. $current['ipv6'] = expand_ipv6($current['ipv6']);
  299. }
  300. }
  301. }
  302. if ($record[1] == 'CNAME' && count($currents) == 0) {
  303. // A and AAAA are also valid instead of CNAME
  304. $a = dns_get_record($record[0], DNS_A);
  305. $cname = dns_get_record($record[2], DNS_A);
  306. if (count($a) > 0 && count($cname) > 0) {
  307. if ($a[0]['ip'] == $cname[0]['ip']) {
  308. $currents = array(
  309. array(
  310. 'host' => $record[0],
  311. 'class' => 'IN',
  312. 'type' => 'CNAME',
  313. 'target' => $record[2]
  314. )
  315. );
  316. $aaaa = dns_get_record($record[0], DNS_AAAA);
  317. $cname = dns_get_record($record[2], DNS_AAAA);
  318. if (count($aaaa) == 0 || count($cname) == 0 || expand_ipv6($aaaa[0]['ipv6']) != expand_ipv6($cname[0]['ipv6'])) {
  319. $currents[0]['target'] = expand_ipv6($aaaa[0]['ipv6']) . ' <sup>1</sup>';
  320. }
  321. }
  322. else {
  323. $currents = array(
  324. array(
  325. 'host' => $record[0],
  326. 'class' => 'IN',
  327. 'type' => 'CNAME',
  328. 'target' => $a[0]['ip'] . ' <sup>1</sup>'
  329. )
  330. );
  331. }
  332. }
  333. }
  334. foreach ($currents as &$current) {
  335. if ($current['type'] == 'TXT' &&
  336. stripos($current['txt'], 'v=dmarc') === 0 &&
  337. $record[2] == $dmarc_link) {
  338. $current['txt'] = str_replace(' ', '', $current['txt']);
  339. $state = $current[$data_field[$current['type']]] . state_optional;
  340. }
  341. elseif ($current['type'] == 'TXT' &&
  342. stripos($current['txt'], 'v=spf') === 0 &&
  343. $record[2] == $spf_link) {
  344. $state = state_nomatch;
  345. $rslt = get_spf_allowed_hosts($record[0], true);
  346. if (in_array($ip, $rslt) && in_array(expand_ipv6($ip6), $rslt)) {
  347. $state = state_good;
  348. }
  349. $state .= '<br />' . $current[$data_field[$current['type']]] . state_optional;
  350. }
  351. elseif ($current['type'] == 'TXT' &&
  352. stripos($current['txt'], 'v=dkim') === 0 &&
  353. stripos($record[2], 'v=dkim') === 0) {
  354. preg_match('/v=DKIM1;.*k=rsa;.*p=([^;]*).*/i', $current[$data_field[$current['type']]], $dkim_matches_current);
  355. preg_match('/v=DKIM1;.*k=rsa;.*p=([^;]*).*/i', $record[2], $dkim_matches_good);
  356. if ($dkim_matches_current[1] == $dkim_matches_good[1]) {
  357. $state = state_good;
  358. }
  359. }
  360. elseif ($current['type'] != 'TXT' &&
  361. isset($data_field[$current['type']]) && $state != state_good) {
  362. $state = state_nomatch;
  363. if ($current[$data_field[$current['type']]] == $record[2]) {
  364. $state = state_good;
  365. }
  366. }
  367. }
  368. unset($current);
  369. if (isset($record[3]) &&
  370. $record[3] == state_optional &&
  371. ($state == state_missing || $state == state_nomatch)) {
  372. $state = state_optional;
  373. }
  374. if ($state == state_nomatch) {
  375. $state = array();
  376. foreach ($currents as $current) {
  377. $state[] = $current[$data_field[$current['type']]];
  378. }
  379. $state = implode('<br />', $state);
  380. }
  381. echo sprintf('
  382. <tr>
  383. <td>%s</td>
  384. <td>%s</td>
  385. <td class="dns-found">%s</td>
  386. <td class="dns-recommended">%s</td>
  387. </tr>', $record[0], $record[1], $record[2], $state);
  388. $record[3] = explode('<br />', $state);
  389. }
  390. unset($record);
  391. $dns_data = sprintf("\$ORIGIN %s.\n", $domain);
  392. foreach ($records as $record) {
  393. if ($domain == substr($record[0], -strlen($domain))) {
  394. $label = substr($record[0], 0, -strlen($domain)-1);
  395. $val = $record[2];
  396. if (strlen($label) == 0) {
  397. $label = "@";
  398. }
  399. $vals = array();
  400. if (strpos($val, "<a") !== FALSE) {
  401. if (is_array($record[3]) && count($record[3]) == 1 && $record[3][0] == state_optional) {
  402. $record[3][0] = "**TODO**";
  403. $label = ';' . $label;
  404. }
  405. foreach ($record[3] as $val) {
  406. $val = str_replace(state_optional, '', $val);
  407. $val = str_replace(state_good, '', $val);
  408. if (strlen($val) > 0) {
  409. $vals[] = sprintf("%s\tIN\t%s\t%s\n", $label, $record[1], $val);
  410. }
  411. }
  412. }
  413. else {
  414. $vals[] = sprintf("%s\tIN\t%s\t%s\n", $label, $record[1], $val);
  415. }
  416. foreach ($vals as $val) {
  417. $dns_data .= str_replace($domain, $domain . '.', $val);
  418. }
  419. }
  420. }
  421. ?>
  422. </table>
  423. <a id='download-zonefile' data-zonefile="<?=base64_encode($dns_data);?>" download='<?=$_GET['domain'];?>.txt' type='text/csv'>Download</a>
  424. <script>
  425. var zonefile_dl_link = document.getElementById('download-zonefile');
  426. var zonefile = atob(zonefile_dl_link.getAttribute('data-zonefile'));
  427. var data = new Blob([zonefile]);
  428. var download_zonefile_link = document.getElementById('download-zonefile');
  429. download_zonefile_link.href = URL.createObjectURL(data);
  430. </script>
  431. </div>
  432. <p class="help-block">
  433. <sup>1</sup> <?=$lang['diagnostics']['cname_from_a'];?><br />
  434. <sup>2</sup> <?=$lang['diagnostics']['optional'];?>
  435. </p>
  436. <?php
  437. }
  438. else {
  439. echo "Session invalid";
  440. exit();
  441. }
  442. ?>