dns_diagnostics.php 12 KB

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