dns_diagnostics.php 15 KB

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