dns_diagnostics.php 14 KB

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