dns_diagnostics.php 16 KB

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