DnsHelper.php 8.7 KB

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