dns_diagnostics.php 12 KB

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