dns_diagnostics.php 12 KB

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