DnsHelper.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  17. public function __construct()
  18. {
  19. $this->params = $this->getWhmcsParamsByKeys(['domain', 'userid', 'serverhostname', 'username', 'password', 'domainid', 'serverid', 'pid']);
  20. $this->server = Server::select('id', 'nameserver1ip', 'nameserver2ip')->findOrFail($this->params['serverid']);
  21. // $this->nameserver = array(trim($this->server->nameserver1ip), trim($this->server->nameserver2ip));
  22. $this->nameserver = array('127.0.0.1', '127.0.0.2'); //test
  23. $this->clientDomains = localAPI('GetClientsDomains', array('clientid' => $this->params['userid']));
  24. $productManager = new ProductManager();
  25. $productManager->loadById($this->params['pid']);
  26. $this->spfConfig = $productManager->get('spf_string');
  27. $this->dmarcConfig = $productManager->get('dmarc_string');
  28. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  29. try {
  30. $api->login($this->params['serverhostname'], $this->params['username'], $this->params['password']);
  31. } catch (KerioApiException $error) {
  32. logModuleCall(
  33. 'kerioEmail',
  34. __FUNCTION__,
  35. $accessData,
  36. 'Error: cannot login to ' . $accessData['kerioServer'],
  37. $error->getMessage()
  38. );
  39. exit();
  40. }
  41. }
  42. public function KerioEmailCheckDNS()
  43. {
  44. $vars['maildomain'] = $this->params['domain'];
  45. if($this->clientDomains['totalresults'] == 0){
  46. $vars['selfdomain'] = FALSE;
  47. $vars['dmarcconfig'] = $dmarcConfig;
  48. $vars['spfconfig'] = $spfConfig;
  49. return $vars;
  50. } else {
  51. $vars['selfdomain'] = TRUE;
  52. }
  53. $resolver = new \Net_DNS2_Resolver(array('nameservers' => $this->nameserver));
  54. try {
  55. $result = $resolver->query($this->params['domain'], 'MX');
  56. } catch(\Net_DNS2_Exception $e) {
  57. echo "::query() failed: ", $e->getMessage(), "\n";
  58. }
  59. $domainMX = $result->answer;
  60. try {
  61. $result = $resolver->query($this->params['domain'], 'TXT');
  62. } catch(\Net_DNS2_Exception $e) {
  63. echo "::query() failed: ", $e->getMessage(), "\n";
  64. }
  65. $domainTXT = $result->answer;
  66. $domainSPF = array();
  67. $domainDKIM = array();
  68. $domainDMARC = array();
  69. foreach($domainTXT as $txtRecord) {
  70. foreach($txtRecord->text as $txtData) {
  71. if(strstr($txtData,'v=spf')) {
  72. array_push($domainSPF,$txtData);
  73. }
  74. if(strstr($txtData,'v=DKIM')) {
  75. array_push($domainDKIM,$txtData);
  76. }
  77. if(strstr($txtData,'v=DMARC')) {
  78. array_push($domainDMARC,$txtData);
  79. }
  80. }
  81. }
  82. # self hosted DNS
  83. $vars['selfDNS'] = FALSE;
  84. for($i=$this->clientDomains['startnumber'];$i<=$this->clientDomains['numreturned'];$i++) {
  85. if($this->params['domain'] == $this->clientDomains['domains']['domain'][$i]['domainname']) {
  86. $vars['selfDNS'] = TRUE;
  87. $vars['domainId'] = $this->clientDomains['domains']['domain'][$i]['id'];
  88. }
  89. }
  90. # SPF
  91. if (count($domainSPF) > 1) {
  92. $vars['multiSPF'] = TRUE;
  93. } else {
  94. $vars['multiSPF'] = FALSE;
  95. }
  96. $vars['spf'] = 'wrong';
  97. if (empty($domainSPF)) {
  98. $vars['spf'] = 'unset';
  99. } else {
  100. foreach($domainSPF as $spf) {
  101. if($spf === $spfConfig) {
  102. $vars['spf'] = 'set';
  103. }
  104. }
  105. }
  106. $vars['domainSPF'] = $domainSPF;
  107. # DKIM
  108. if (count($domainDKIM) > 1) {
  109. $vars['multiDKIM'] = TRUE;
  110. } else {
  111. $vars['multiDKIM'] = FALSE;
  112. }
  113. if (empty($domainDKIM)) {
  114. $vars['dkim'] = 'unset';
  115. } else {
  116. $vars['dkim'] = 'set';
  117. }
  118. $vars['domainDKIM'] = $domainDKIM;
  119. # DMARC
  120. if (count($domainDMARC) > 1) {
  121. $vars['multiDMARC'] = TRUE;
  122. } else {
  123. $vars['multiDMARC'] = FALSE;
  124. }
  125. $vars['dmarc'] = 'wrong';
  126. if (empty($domainDMARC)) {
  127. $vars['dmarc'] = 'unset';
  128. } else {
  129. foreach($domainDMARC as $dmarc) {
  130. if($dmarc === $dmarcConfig) {
  131. $vars['dmarc'] = 'set';
  132. }
  133. }
  134. }
  135. $vars['domainDMARC'] = $domainDMARC;
  136. # MX
  137. if(count($domainMX) > 1) {
  138. $vars['multiMX'] = TRUE;
  139. } else {
  140. $vars['multiMX'] = FALSE;
  141. }
  142. if(empty($domainMX)){
  143. $vars['mx'] = 'unset';
  144. $vars['mxtarget'] = $this->params['serverhostname'];
  145. } else {
  146. $vars['domainMX'] = $domainMX;
  147. $domainMXrecord = array_shift($domainMX);
  148. $vars['mxtarget'] = $domainMXrecord->exchange;
  149. if($domainMXrecord->exchange == $this->params['serverhostname']) {
  150. $vars['mx'] = 'set';
  151. } else {
  152. $var['mx'] = 'wrong';
  153. }
  154. }
  155. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  156. ->select('id')
  157. ->where('name', '=', $this->params['domain'])
  158. ->get();
  159. logModuleCall(
  160. 'kerioEmail',
  161. __FUNCTION__,
  162. $this->params,
  163. 'Debug',
  164. $zoneIDcollection
  165. );
  166. return $vars;
  167. }
  168. function KerioEmail_setDNS()
  169. {
  170. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  171. ->select('id')
  172. ->where('name', '=', $this->params['domain'])
  173. ->get();
  174. $zoneIDobj = $zoneIDcollection[0];
  175. $zoneID = $zoneIDobj->{'id'};
  176. if(!isset($zoneID)) {
  177. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  178. }
  179. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  180. if($dnsZone['result'] != 'success') {
  181. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  182. }
  183. $zoneRecords = array();
  184. $mxRecord = array(
  185. 'line' => $this->params['domain'].'.|MX|0',
  186. 'name' => '@',
  187. 'type' => 'MX',
  188. 'class' => 'IN',
  189. 'data' => array(
  190. 'preference' => '10',
  191. 'exchange' => $this->params['serverhostname'],
  192. ),
  193. );
  194. array_push($zoneRecords, $mxRecord);
  195. $spfRecord = array(
  196. 'line' => $params['domain'].'.|TXT|0',
  197. 'name' => '@',
  198. 'type' => 'TXT',
  199. 'class' => 'IN',
  200. 'data' => $this->spfConfig
  201. );
  202. array_push($zoneRecords, $spfRecord);
  203. $dmarcRecord = array(
  204. 'line' => $params['domain'].'.|TXT|0',
  205. 'name' => '@',
  206. 'type' => 'TXT',
  207. 'class' => 'IN',
  208. 'data' => $this->dmarcConfig
  209. );
  210. array_push($zoneRecords, $dmarcRecord);
  211. foreach($dnsZone['data']->records as $record) {
  212. if($record->type != 'MX') {
  213. if(!$record->type === 'TXT'){
  214. array_push($zoneRecords, $record);
  215. } elseif (!preg_match('/^v=spf(.*)$/i', trim($record->rdata->txtdata,'"'))) {
  216. array_push($zoneRecords, $record);
  217. };
  218. };
  219. }
  220. $result = localAPI('dnsmanager' ,
  221. array(
  222. 'dnsaction' => 'updateZone',
  223. 'zone_id' => $zoneID,
  224. 'records' => $zoneRecords,
  225. )
  226. );
  227. if($result['result'] != 'success') {
  228. return 'Error: cloud not update zone for ID ' . $zoneID;
  229. }
  230. return 'success';
  231. }
  232. }