DnsHelper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Helper;
  3. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Server;
  4. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  5. use WHMCS\Database\Capsule;
  6. require_once '/usr/share/php/Net/DNS2.php';
  7. use \Net\DNS2\Net_DNS2_Resolver as Net_DNS2_Resolver;
  8. /**
  9. * Wrapper for WHMCS params passed to controler functions
  10. *
  11. * @autor ThurData <info@thurdata.ch>
  12. */
  13. class DnsHelper
  14. {
  15. use \ThurData\Servers\KerioEmail\Core\UI\Traits\WhmcsParams;
  16. public function __construct()
  17. {
  18. $this->params = $this->getWhmcsParamsByKeys(['domain', 'userid', 'serverhostname', 'domainid', 'serverid', 'pid']);
  19. $this->server = Server::select('id', 'nameserver1ip', 'nameserver2ip')->findOrFail($this->params['serverid']);
  20. // $this->nameserver = array(trim($this->server->nameserver1ip), trim($this->server->nameserver2ip));
  21. $this->nameserver = array('127.0.0.1', '127.0.0.2'); //test
  22. $this->clientDomains = localAPI('GetClientsDomains', array('clientid' => $this->params['userid']));
  23. $this->productManager = new ProductManager();
  24. $this->productManager->loadById($this->params['pid']);
  25. }
  26. public function KerioEmailCheckDNS()
  27. {
  28. $vars['maildomain'] = $this->params['domain'];
  29. $spfConfig = $this->productManager->get('spf_string');
  30. $dmarcConfig = $this->productManager->get('dmarc_string');
  31. if($this->clientDomains['totalresults'] == 0){
  32. $vars['selfdomain'] = FALSE;
  33. $vars['dmarcconfig'] = $dmarcConfig;
  34. $vars['spfconfig'] = $spfConfig;
  35. return $vars;
  36. } else {
  37. $vars['selfdomain'] = TRUE;
  38. }
  39. $resolver = new \Net_DNS2_Resolver(array('nameservers' => $this->nameserver));
  40. try {
  41. $result = $resolver->query($this->params['domain'], 'MX');
  42. } catch(\Net_DNS2_Exception $e) {
  43. echo "::query() failed: ", $e->getMessage(), "\n";
  44. }
  45. $domainMX = $result->answer;
  46. try {
  47. $result = $resolver->query($this->params['domain'], 'TXT');
  48. } catch(\Net_DNS2_Exception $e) {
  49. echo "::query() failed: ", $e->getMessage(), "\n";
  50. }
  51. $domainTXT = $result->answer;
  52. $domainSPF = array();
  53. $domainDKIM = array();
  54. $domainDMARC = array();
  55. foreach($domainTXT as $txtRecord) {
  56. foreach($txtRecord->text as $txtData) {
  57. if(strstr($txtData,'v=spf')) {
  58. array_push($domainSPF,$txtData);
  59. }
  60. if(strstr($txtData,'v=DKIM')) {
  61. array_push($domainDKIM,$txtData);
  62. }
  63. if(strstr($txtData,'v=DMARC')) {
  64. array_push($domainDMARC,$txtData);
  65. }
  66. }
  67. }
  68. # self hosted DNS
  69. $vars['selfDNS'] = FALSE;
  70. for($i=$this->clientDomains['startnumber'];$i<=$this->clientDomains['numreturned'];$i++) {
  71. if($this->params['domain'] == $this->clientDomains['domains']['domain'][$i]['domainname']) {
  72. $vars['selfDNS'] = TRUE;
  73. $vars['domainId'] = $this->clientDomains['domains']['domain'][$i]['id'];
  74. }
  75. }
  76. # SPF
  77. if (count($domainSPF) > 1) {
  78. $vars['multiSPF'] = TRUE;
  79. } else {
  80. $vars['multiSPF'] = FALSE;
  81. }
  82. $vars['spf'] = 'wrong';
  83. if (empty($domainSPF)) {
  84. $vars['spf'] = 'unset';
  85. } else {
  86. foreach($domainSPF as $spf) {
  87. if($spf === $spfConfig) {
  88. $vars['spf'] = 'set';
  89. }
  90. }
  91. }
  92. $vars['domainSPF'] = $domainSPF;
  93. # DKIM
  94. if (count($domainDKIM) > 1) {
  95. $vars['multiDKIM'] = TRUE;
  96. } else {
  97. $vars['multiDKIM'] = FALSE;
  98. }
  99. if (empty($domainDKIM)) {
  100. $vars['dkim'] = 'unset';
  101. } else {
  102. $vars['dkim'] = 'set';
  103. }
  104. $vars['domainDKIM'] = $domainDKIM;
  105. # DMARC
  106. if (count($domainDMARC) > 1) {
  107. $vars['multiDMARC'] = TRUE;
  108. } else {
  109. $vars['multiDMARC'] = FALSE;
  110. }
  111. $vars['dmarc'] = 'wrong';
  112. if (empty($domainDMARC)) {
  113. $vars['dmarc'] = 'unset';
  114. } else {
  115. foreach($domainDMARC as $dmarc) {
  116. if($dmarc === $dmarcConfig) {
  117. $vars['dmarc'] = 'set';
  118. }
  119. }
  120. }
  121. $vars['domainDMARC'] = $domainDMARC;
  122. # MX
  123. if(count($domainMX) > 1) {
  124. $vars['multiMX'] = TRUE;
  125. } else {
  126. $vars['multiMX'] = FALSE;
  127. }
  128. if(empty($domainMX)){
  129. $vars['mx'] = 'unset';
  130. $vars['mxtarget'] = $params['serverhostname'];
  131. } else {
  132. $vars['domainMX'] = $domainMX;
  133. $domainMXrecord = array_shift($domainMX);
  134. $vars['mxtarget'] = $domainMXrecord->exchange;
  135. if($domainMXrecord->exchange == $params['serverhostname']) {
  136. $vars['mx'] = 'set';
  137. } else {
  138. $var['mx'] = 'wrong';
  139. }
  140. }
  141. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  142. ->select('id')
  143. ->where('name', '=', $this->params['domain'])
  144. ->get();
  145. logModuleCall(
  146. 'kerioEmail',
  147. __FUNCTION__,
  148. $this->params,
  149. 'Debug',
  150. $zoneIDcollection
  151. );
  152. return $vars;
  153. }
  154. function KerioEmail_setMX()
  155. {
  156. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  157. ->select('id')
  158. ->where('name', '=', $this->params['domain'])
  159. ->get();
  160. $zoneIDobj = $zoneIDcollection[0];
  161. $zoneID = $zoneIDobj->{'id'};
  162. if(!isset($zoneID)) {
  163. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  164. }
  165. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  166. if($dnsZone['result'] != 'success') {
  167. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  168. }
  169. $zoneRecords = array();
  170. $mxRecord = array(
  171. 'line' => $this->params['domain'].'.|MX|0',
  172. 'name' => '@',
  173. 'type' => 'MX',
  174. 'class' => 'IN',
  175. 'data' => array(
  176. 'preference' => '10',
  177. 'exchange' => MX,
  178. ),
  179. );
  180. array_push($zoneRecords, $mxRecord);
  181. foreach($dnsZone['data']->records as $record) {
  182. if($record->type != 'MX') {
  183. array_push($zoneRecords, $record);
  184. };
  185. }
  186. $result = localAPI('dnsmanager' ,
  187. array(
  188. 'dnsaction' => 'updateZone',
  189. 'zone_id' => $zoneID,
  190. 'records' => $zoneRecords,
  191. )
  192. );
  193. if($result['result'] != 'success') {
  194. return 'Error: cloud not update zone for ID ' . $zoneID;
  195. }
  196. return 'success';
  197. }
  198. }