dns_diagnostics.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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="http://www.open-spf.org/SPF_Record_Syntax/" 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. $mtasts_report_link = '<a href="https://mxtoolbox.com/dmarc/smtp-tls/how-to-setup-smtp-tls-reports" target="_blank">TLS Report Record Syntax</a>';
  65. $records = array();
  66. if ($_SESSION['mailcow_cc_role'] == "admin") {
  67. $records[] = array(
  68. $mailcow_hostname,
  69. 'A',
  70. $ip
  71. );
  72. $records[] = array(
  73. $ptr,
  74. 'PTR',
  75. $mailcow_hostname
  76. );
  77. if (!empty($ip6)) {
  78. $records[] = array(
  79. $mailcow_hostname,
  80. 'AAAA',
  81. expand_ipv6($ip6)
  82. );
  83. $records[] = array(
  84. $ptr6,
  85. 'PTR',
  86. $mailcow_hostname
  87. );
  88. }
  89. $records[] = array(
  90. '_25._tcp.' . $autodiscover_config['smtp']['server'],
  91. 'TLSA',
  92. generate_tlsa_digest($autodiscover_config['smtp']['server'], 25, 1)
  93. );
  94. }
  95. $records[] = array(
  96. $domain,
  97. 'MX',
  98. $mailcow_hostname
  99. );
  100. if (!in_array($domain, $alias_domains)) {
  101. $records[] = array(
  102. 'autodiscover.' . $domain,
  103. 'CNAME',
  104. $mailcow_hostname
  105. );
  106. $records[] = array(
  107. '_autodiscover._tcp.' . $domain,
  108. 'SRV',
  109. $mailcow_hostname . ' ' . $https_port
  110. );
  111. $records[] = array(
  112. 'autoconfig.' . $domain,
  113. 'CNAME',
  114. $mailcow_hostname
  115. );
  116. }
  117. $mta_sts = mailbox('get', 'mta_sts', $domain);
  118. if (count($mta_sts) > 0 && $mta_sts['active'] == 1) {
  119. if (!in_array($domain, $alias_domains)) {
  120. $records[] = array(
  121. 'mta-sts.' . $domain,
  122. 'CNAME',
  123. $mailcow_hostname
  124. );
  125. }
  126. $records[] = array(
  127. '_mta-sts.' . $domain,
  128. 'TXT',
  129. "v={$mta_sts['version']};id={$mta_sts['id']};",
  130. );
  131. $records[] = array(
  132. '_smtp._tls.' . $domain,
  133. 'TXT',
  134. $mtasts_report_link,
  135. );
  136. }
  137. $records[] = array(
  138. $domain,
  139. 'TXT',
  140. $spf_link,
  141. state_optional
  142. );
  143. $records[] = array(
  144. '_dmarc.' . $domain,
  145. 'TXT',
  146. $dmarc_link,
  147. state_optional
  148. );
  149. if (!empty($dkim = dkim('details', $domain))) {
  150. $records[] = array(
  151. $dkim['dkim_selector'] . '._domainkey.' . $domain,
  152. 'TXT',
  153. $dkim['dkim_txt']
  154. );
  155. }
  156. if (!in_array($domain, $alias_domains)) {
  157. $current_records = (array)dns_get_record('_pop3._tcp.' . $domain, DNS_SRV);
  158. if (count($current_records) == 0 || $current_records[0]['target'] != '') {
  159. if ($autodiscover_config['pop3']['tlsport'] != '110') {
  160. $records[] = array(
  161. '_pop3._tcp.' . $domain,
  162. 'SRV',
  163. $autodiscover_config['pop3']['server'] . ' ' . $autodiscover_config['pop3']['tlsport']
  164. );
  165. }
  166. }
  167. else {
  168. $records[] = array(
  169. '_pop3._tcp.' . $domain,
  170. 'SRV',
  171. '. 0'
  172. );
  173. }
  174. $current_records = (array)dns_get_record('_pop3s._tcp.' . $domain, DNS_SRV);
  175. if (count($current_records) == 0 || $current_records[0]['target'] != '') {
  176. if ($autodiscover_config['pop3']['port'] != '995') {
  177. $records[] = array(
  178. '_pop3s._tcp.' . $domain,
  179. 'SRV',
  180. $autodiscover_config['pop3']['server'] . ' ' . $autodiscover_config['pop3']['port']
  181. );
  182. }
  183. }
  184. else {
  185. $records[] = array(
  186. '_pop3s._tcp.' . $domain,
  187. 'SRV',
  188. '. 0'
  189. );
  190. }
  191. if ($autodiscover_config['imap']['tlsport'] != '143') {
  192. $records[] = array(
  193. '_imap._tcp.' . $domain,
  194. 'SRV',
  195. $autodiscover_config['imap']['server'] . ' ' . $autodiscover_config['imap']['tlsport']
  196. );
  197. }
  198. if ($autodiscover_config['imap']['port'] != '993') {
  199. $records[] = array(
  200. '_imaps._tcp.' . $domain,
  201. 'SRV',
  202. $autodiscover_config['imap']['server'] . ' ' . $autodiscover_config['imap']['port']
  203. );
  204. }
  205. if ($autodiscover_config['smtp']['tlsport'] != '587') {
  206. $records[] = array(
  207. '_submission._tcp.' . $domain,
  208. 'SRV',
  209. $autodiscover_config['smtp']['server'] . ' ' . $autodiscover_config['smtp']['tlsport']
  210. );
  211. }
  212. if ($autodiscover_config['smtp']['port'] != '465') {
  213. $records[] = array(
  214. '_smtps._tcp.' . $domain,
  215. 'SRV',
  216. $autodiscover_config['smtp']['server'] . ' ' . $autodiscover_config['smtp']['port']
  217. );
  218. }
  219. if ($autodiscover_config['sieve']['port'] != '4190') {
  220. $records[] = array(
  221. '_sieve._tcp.' . $domain,
  222. 'SRV',
  223. $autodiscover_config['sieve']['server'] . ' ' . $autodiscover_config['sieve']['port']
  224. );
  225. }
  226. }
  227. $record_types = array(
  228. 'A' => DNS_A,
  229. 'AAAA' => DNS_AAAA,
  230. 'CNAME' => DNS_CNAME,
  231. 'MX' => DNS_MX,
  232. 'PTR' => DNS_PTR,
  233. 'SRV' => DNS_SRV,
  234. 'TXT' => DNS_TXT,
  235. );
  236. $data_field = array(
  237. 'A' => 'ip',
  238. 'AAAA' => 'ipv6',
  239. 'CNAME' => 'target',
  240. 'MX' => 'target',
  241. 'PTR' => 'target',
  242. 'SRV' => 'data',
  243. 'TLSA' => 'data',
  244. 'TXT' => 'txt',
  245. );
  246. ?>
  247. <div class="table-responsive" id="dnstable">
  248. <table class="table table-striped">
  249. <tr>
  250. <th><?=$lang['diagnostics']['dns_records_name'];?></th>
  251. <th><?=$lang['diagnostics']['dns_records_type'];?></th>
  252. <th><?=$lang['diagnostics']['dns_records_data'];?></th>
  253. <th><?=$lang['diagnostics']['dns_records_status'];?></th>
  254. </tr>
  255. <?php
  256. foreach ($records as &$record) {
  257. $record[1] = strtoupper($record[1]);
  258. $state = state_optional;
  259. if ($record[1] == 'TLSA') {
  260. $currents = (array)dns_get_record($record[0], 52, $_, $_, TRUE);
  261. foreach ($currents as &$current) {
  262. $current['type'] = 'TLSA';
  263. $current['cert_usage'] = hexdec(bin2hex($current['data'][0]));
  264. $current['selector'] = hexdec(bin2hex($current['data'][1]));
  265. $current['match_type'] = hexdec(bin2hex($current['data'][2]));
  266. $current['cert_data'] = bin2hex(substr($current['data'], 3));
  267. $current['data'] = $current['cert_usage'] . ' ' . $current['selector'] . ' ' . $current['match_type'] . ' ' . $current['cert_data'];
  268. }
  269. unset($current);
  270. }
  271. else {
  272. $currents = (array)dns_get_record($record[0], $record_types[$record[1]]);
  273. if ($record[0] == $mailcow_hostname && ($record[1] == "A" || $record[1] == "AAAA")) {
  274. if (!empty((array)dns_get_record($record[0], DNS_CNAME))) {
  275. $currents[0]['ip'] = state_missing . ' <b>(CNAME)</b>';
  276. $currents[0]['ipv6'] = state_missing . ' <b>(CNAME)</b>';
  277. }
  278. }
  279. if ($record[1] == 'SRV') {
  280. foreach ($currents as &$current) {
  281. if ($current['target'] == '') {
  282. $current['target'] = '.';
  283. $current['port'] = '0';
  284. }
  285. $current['data'] = $current['target'] . ' ' . $current['port'];
  286. }
  287. unset($current);
  288. }
  289. elseif ($record[1] == 'TXT') {
  290. foreach ($currents as &$current) {
  291. unset($current);
  292. }
  293. unset($current);
  294. }
  295. elseif ($record[1] == 'AAAA') {
  296. foreach ($currents as &$current) {
  297. $current['ipv6'] = expand_ipv6($current['ipv6']);
  298. }
  299. }
  300. }
  301. if ($record[1] == 'CNAME' && count($currents) == 0) {
  302. // A and AAAA are also valid instead of CNAME
  303. $a = (array)dns_get_record($record[0], DNS_A);
  304. $cname = (array)dns_get_record($record[2], DNS_A);
  305. if (count($a) > 0 && count($cname) > 0) {
  306. if ($a[0]['ip'] == $cname[0]['ip']) {
  307. $currents = array(
  308. array(
  309. 'host' => $record[0],
  310. 'class' => 'IN',
  311. 'type' => 'CNAME',
  312. 'target' => $record[2]
  313. )
  314. );
  315. $aaaa = (array)dns_get_record($record[0], DNS_AAAA);
  316. $cname = (array)dns_get_record($record[2], DNS_AAAA);
  317. if (count($aaaa) == 0 || count($cname) == 0 || expand_ipv6($aaaa[0]['ipv6']) != expand_ipv6($cname[0]['ipv6'])) {
  318. $currents[0]['target'] = expand_ipv6($aaaa[0]['ipv6']) . ' <sup>1</sup>';
  319. }
  320. }
  321. else {
  322. $currents = array(
  323. array(
  324. 'host' => $record[0],
  325. 'class' => 'IN',
  326. 'type' => 'CNAME',
  327. 'target' => $a[0]['ip'] . ' <sup>1</sup>'
  328. )
  329. );
  330. }
  331. }
  332. }
  333. foreach ($currents as &$current) {
  334. if ($current['type'] == "TXT" &&
  335. stripos(strtolower($current['txt']), 'v=sts') === 0) {
  336. if (strtolower($current[$data_field[$current['type']]]) == strtolower($record[2])) {
  337. $state = state_good;
  338. }
  339. else {
  340. $state = state_nomatch;
  341. }
  342. $state .= '<br />' . $current[$data_field[$current['type']]];
  343. }
  344. if ($current['type'] == 'TXT' &&
  345. stripos($current['txt'], 'v=dmarc') === 0 &&
  346. $record[2] == $dmarc_link) {
  347. $current['txt'] = str_replace(' ', '', $current['txt']);
  348. $state = $current[$data_field[$current['type']]] . state_optional;
  349. }
  350. elseif ($current['type'] == 'TXT' &&
  351. stripos($current['txt'], 'v=spf') === 0 &&
  352. $record[2] == $spf_link) {
  353. $state = state_nomatch;
  354. $rslt = get_spf_allowed_hosts($record[0], true);
  355. if (in_array($ip, $rslt) && in_array(expand_ipv6($ip6), $rslt)) {
  356. $state = state_good;
  357. }
  358. $state .= '<br />' . $current[$data_field[$current['type']]] . state_optional;
  359. }
  360. elseif ($current['type'] == 'TXT' &&
  361. stripos($current['txt'], 'v=dkim') === 0 &&
  362. stripos($record[2], 'v=dkim') === 0) {
  363. preg_match('/v=DKIM1;.*k=rsa;.*p=([^;]*).*/i', $current[$data_field[$current['type']]], $dkim_matches_current);
  364. preg_match('/v=DKIM1;.*k=rsa;.*p=([^;]*).*/i', $record[2], $dkim_matches_good);
  365. if ($dkim_matches_current[1] == $dkim_matches_good[1]) {
  366. $state = state_good;
  367. }
  368. }
  369. elseif ($current['type'] != 'TXT' &&
  370. isset($data_field[$current['type']]) && $state != state_good) {
  371. $state = state_nomatch;
  372. if ($current[$data_field[$current['type']]] == $record[2]) {
  373. $state = state_good;
  374. }
  375. }
  376. }
  377. unset($current);
  378. if (isset($record[3]) &&
  379. $record[3] == state_optional &&
  380. ($state == state_missing || $state == state_nomatch)) {
  381. $state = state_optional;
  382. }
  383. if ($state == state_nomatch) {
  384. $state = array();
  385. foreach ($currents as $current) {
  386. $state[] = $current[$data_field[$current['type']]];
  387. }
  388. $state = implode('<br />', $state);
  389. }
  390. echo sprintf('
  391. <tr>
  392. <td>%s</td>
  393. <td>%s</td>
  394. <td class="dns-found">%s</td>
  395. <td class="dns-recommended">%s</td>
  396. </tr>', $record[0], $record[1], $record[2], $state);
  397. $record[3] = explode('<br />', $state);
  398. }
  399. unset($record);
  400. $dns_data = sprintf("\$ORIGIN %s.\n", $domain);
  401. foreach ($records as $record) {
  402. if ($domain == substr($record[0], -strlen($domain))) {
  403. $label = substr($record[0], 0, -strlen($domain)-1);
  404. $val = $record[2];
  405. if (strlen($label) == 0) {
  406. $label = "@";
  407. }
  408. $vals = array();
  409. if (strpos($val, "<a") !== FALSE) {
  410. if (is_array($record[3]) && count($record[3]) == 1 && $record[3][0] == state_optional) {
  411. $record[3][0] = "**TODO**";
  412. $label = ';' . $label;
  413. }
  414. foreach ($record[3] as $val) {
  415. $val = str_replace(state_optional, '', $val);
  416. $val = str_replace(state_good, '', $val);
  417. if (strlen($val) > 0) {
  418. $vals[] = sprintf("%s\tIN\t%s\t%s\n", $label, $record[1], $val);
  419. }
  420. }
  421. }
  422. else {
  423. $vals[] = sprintf("%s\tIN\t%s\t%s\n", $label, $record[1], $val);
  424. }
  425. foreach ($vals as $val) {
  426. $dns_data .= str_replace($domain, $domain . '.', $val);
  427. }
  428. }
  429. }
  430. ?>
  431. </table>
  432. <a id='download-zonefile' class="btn btn-sm btn-secondary visible-xs-block visible-sm-inline visible-md-inline visible-lg-inline mb-4" style="margin-top:10px" data-zonefile="<?=base64_encode($dns_data);?>" download='<?=$_GET['domain'];?>.txt' type='text/csv'>Download</a>
  433. <script>
  434. var zonefile_dl_link = document.getElementById('download-zonefile');
  435. var zonefile = atob(zonefile_dl_link.getAttribute('data-zonefile'));
  436. var data = new Blob([zonefile]);
  437. var download_zonefile_link = document.getElementById('download-zonefile');
  438. download_zonefile_link.href = URL.createObjectURL(data);
  439. </script>
  440. </div>
  441. <p class="help-block">
  442. <sup>1</sup> <?=$lang['diagnostics']['cname_from_a'];?><br />
  443. <sup>2</sup> <?=$lang['diagnostics']['optional'];?>
  444. </p>
  445. <?php
  446. }
  447. else {
  448. echo "Session invalid";
  449. exit();
  450. }
  451. ?>