dns_diagnostics.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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[1] == 'SRV') {
  310. foreach ($currents as &$current) {
  311. if ($current['target'] == '') {
  312. $current['target'] = '.';
  313. $current['port'] = '0';
  314. }
  315. $current['data'] = $current['target'] . ' ' . $current['port'];
  316. }
  317. unset($current);
  318. }
  319. elseif ($record[1] == 'TXT') {
  320. foreach ($currents as &$current) {
  321. unset($current);
  322. }
  323. unset($current);
  324. }
  325. elseif ($record[1] == 'AAAA') {
  326. foreach ($currents as &$current) {
  327. $current['ipv6'] = expand_ipv6($current['ipv6']);
  328. }
  329. }
  330. }
  331. if ($record[1] == 'CNAME' && count($currents) == 0) {
  332. // A and AAAA are also valid instead of CNAME
  333. $a = dns_get_record($record[0], DNS_A);
  334. $cname = dns_get_record($record[2], DNS_A);
  335. if (count($a) > 0 && count($cname) > 0) {
  336. if ($a[0]['ip'] == $cname[0]['ip']) {
  337. $currents = array(array('host' => $record[0], 'class' => 'IN', 'type' => 'CNAME', 'target' => $record[2]));
  338. $aaaa = dns_get_record($record[0], DNS_AAAA);
  339. $cname = dns_get_record($record[2], DNS_AAAA);
  340. if (count($aaaa) == 0 || count($cname) == 0 || expand_ipv6($aaaa[0]['ipv6']) != expand_ipv6($cname[0]['ipv6'])) {
  341. $currents[0]['target'] = expand_ipv6($aaaa[0]['ipv6']) . ' <sup>1</sup>';
  342. }
  343. }
  344. else {
  345. $currents = array(array('host' => $record[0], 'class' => 'IN', 'type' => 'CNAME', 'target' => $a[0]['ip'] . ' <sup>1</sup>'));
  346. }
  347. }
  348. }
  349. foreach ($currents as &$current) {
  350. if ($current['type'] == 'TXT' &&
  351. stripos($current['txt'], 'v=dmarc') === 0 &&
  352. $record[2] == $dmarc_link) {
  353. $current['txt'] = str_replace(' ', '', $current['txt']);
  354. $state = $current[$data_field[$current['type']]] . state_optional;
  355. }
  356. elseif ($current['type'] == 'TXT' &&
  357. stripos($current['txt'], 'v=spf') === 0 &&
  358. $record[2] == $spf_link) {
  359. $state = state_nomatch;
  360. $rslt = get_spf_allowed_hosts($record[0]);
  361. if(in_array($ip, $rslt) && in_array(expand_ipv6($ip6), $rslt)){
  362. $state = state_good;
  363. }
  364. $state .= '<br />' . $current[$data_field[$current['type']]].state_optional;
  365. }
  366. elseif ($current['type'] == 'TXT' &&
  367. stripos($current['txt'], 'v=dkim') === 0 &&
  368. stripos($record[2], 'v=dkim') === 0) {
  369. preg_match('/v=DKIM1;.*k=rsa;.*p=([^;]*).*/i', $current[$data_field[$current['type']]], $dkim_matches_current);
  370. preg_match('/v=DKIM1;.*k=rsa;.*p=([^;]*).*/i', $record[2], $dkim_matches_good);
  371. if ($dkim_matches_current[1] == $dkim_matches_good[1]) {
  372. $state = state_good;
  373. }
  374. }
  375. elseif ($current['type'] != 'TXT' &&
  376. isset($data_field[$current['type']]) && $state != state_good) {
  377. $state = state_nomatch;
  378. if ($current[$data_field[$current['type']]] == $record[2]) {
  379. $state = state_good;
  380. }
  381. }
  382. }
  383. unset($current);
  384. if (isset($record[3]) &&
  385. $record[3] == state_optional &&
  386. ($state == state_missing || $state == state_nomatch)) {
  387. $state = state_optional;
  388. }
  389. if ($state == state_nomatch) {
  390. $state = array();
  391. foreach ($currents as $current) {
  392. $state[] = $current[$data_field[$current['type']]];
  393. }
  394. $state = implode('<br />', $state);
  395. }
  396. echo sprintf('<tr>
  397. <td>%s</td>
  398. <td>%s</td>
  399. <td class="dns-found">%s</td>
  400. <td class="dns-recommended">%s</td>
  401. </tr>', $record[0], $record[1], $record[2], $state);
  402. }
  403. ?>
  404. </table>
  405. </div>
  406. <p class="help-block">
  407. <sup>1</sup> <?=$lang['diagnostics']['cname_from_a'];?><br />
  408. <sup>2</sup> <?=$lang['diagnostics']['optional'];?>
  409. </p>
  410. <?php
  411. } else {
  412. echo "Session invalid";
  413. exit();
  414. }
  415. ?>