dns_diagnostics.php 15 KB

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