dns_diagnostics.php 12 KB

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