dns_diagnostics.php 12 KB

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