DnsHelper.php 8.4 KB

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