DnsHelper.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Helper;
  3. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Server;
  4. use \WHMCS\Database\Capsule;
  5. require_once '/usr/share/php/Net/DNS2.php';
  6. use \Net\DNS2\Net_DNS2_Resolver as Net_DNS2_Resolver;
  7. /**
  8. * Wrapper for WHMCS params passed to controler functions
  9. *
  10. * @autor ThurData <info@thurdata.ch>
  11. */
  12. class DnsHelper
  13. {
  14. use \ThurData\Servers\KerioEmail\Core\UI\Traits\WhmcsParams;
  15. public function __construct()
  16. {
  17. $this->params = $this->getWhmcsParamsByKeys(['domain', 'userid', 'serverhostname', 'serverusername', 'serverpassword', 'domainid', 'serverid', 'pid']);
  18. $this->server = Server::select('id', 'nameserver1ip', 'nameserver2ip')->findOrFail($this->params['serverid']);
  19. $this->nameserver = array(trim($this->server->nameserver1ip), trim($this->server->nameserver2ip));
  20. // $this->nameserver = array('127.0.0.1', '127.0.0.2'); //test
  21. $this->resolver = new \Net_DNS2_Resolver(array('nameservers' => $this->nameserver));
  22. }
  23. public function getRecords($domain) {
  24. $zoneID = $this->selfDns($domain);
  25. if($zoneID) {
  26. return $this->getLocalRecords($zoneID);
  27. }
  28. return $this->getResolverRecords($domain);
  29. }
  30. private function selfDns($domain){
  31. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  32. ->select('id')
  33. ->where('name', '=', $domain)
  34. ->get();
  35. $zoneIDobj = $zoneIDcollection[0];
  36. if(!isset($zoneIDobj->{'id'})) {
  37. return false;
  38. }
  39. return $zoneIDobj->{'id'};
  40. }
  41. private function getResolverRecords($domain)
  42. {
  43. $vars['mx'] = array();
  44. $vars['spf'] = array();
  45. $vars['dmarc'] = array();
  46. $vars['dkim'] = array();
  47. $vars['selfDNS'] = false;
  48. try {
  49. $responseMX = $this->resolver->query($domain, 'MX');
  50. $responseTXT = $this->resolver->query($domain, 'TXT');
  51. } catch(\Net_DNS2_Exception $e) {
  52. echo "::query() failed: ", $e->getMessage(), "\n";
  53. }
  54. $domainMX = $responseMX->answer;
  55. $domainTXT = $responseTXT->answer;
  56. foreach($domainMX as $mxRecord) {
  57. array_push($vars['mx'], $mxRecord->exchange);
  58. }
  59. foreach($domainTXT as $txtRecord) {
  60. foreach($txtRecord->text as $txtData) {
  61. if(strstr($txtData,'v=spf')) {
  62. array_push($vars['spf'],$txtData);
  63. }
  64. if(strstr($txtData,'v=DMARC')) {
  65. array_push($vars['dmarc'],$txtData);
  66. }
  67. if(strstr($txtData,'v=DKIM')) {
  68. array_push($vars['dkim'],$txtData);
  69. }
  70. }
  71. }
  72. return $vars;
  73. }
  74. private function getLocalRecords($zoneID) {
  75. $vars['mx'] = array();
  76. $vars['spf'] = array();
  77. $vars['dmarc'] = array();
  78. $vars['dkim'] = array();
  79. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  80. if($dnsZone['result'] != 'success') {
  81. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  82. }
  83. foreach($dnsZone['data']->records as $localRecord) {
  84. if($localRecord->type == 'MX'){
  85. array_push($vars['mx'], $localRecord->rdata->exchange);
  86. }
  87. if($localRecord->type == 'TXT'){
  88. if(strstr($localRecord->rdata->txtdata,'v=spf')) {
  89. array_push($vars['spf'], $localRecord->rdata->txtdata);
  90. }
  91. if(strstr($localRecord->rdata->txtdata,'v=DMARC')) {
  92. array_push($vars['dmarc'], $localRecord->rdata->txtdata);
  93. }
  94. if(strstr($localRecord->rdata->txtdata,'v=DKIM')) {
  95. array_push($vars['dkim'], $localRecord->rdata->txtdata);
  96. }
  97. }
  98. }
  99. return $vars;
  100. }
  101. function KerioEmailsetDNS()
  102. {
  103. return 'success';
  104. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  105. ->select('id')
  106. ->where('name', '=', $this->params['domain'])
  107. ->get();
  108. $zoneIDobj = $zoneIDcollection[0];
  109. $zoneID = $zoneIDobj->{'id'};
  110. if(!isset($zoneID)) {
  111. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  112. }
  113. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  114. logModuleCall(
  115. 'kerioEmail',
  116. __FUNCTION__,
  117. $this->params,
  118. 'DEbug',
  119. $dnsZone['result']
  120. );
  121. if($dnsZone['result'] != 'success') {
  122. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  123. }
  124. $zoneRecords = array();
  125. $mxRecord = array(
  126. 'line' => $this->params['domain'].'.|MX|0',
  127. 'name' => '@',
  128. 'type' => 'MX',
  129. 'class' => 'IN',
  130. 'data' => array(
  131. 'preference' => '10',
  132. 'exchange' => $this->params['serverhostname'],
  133. ),
  134. );
  135. array_push($zoneRecords, $mxRecord);
  136. $spfRecord = array(
  137. 'line' => $this->params['domain'].'.|TXT|0',
  138. 'name' => '@',
  139. 'type' => 'TXT',
  140. 'class' => 'IN',
  141. 'data' => $this->spfConfig
  142. );
  143. array_push($zoneRecords, $spfRecord);
  144. $dmarcRecord = array(
  145. 'line' => $this->params['domain'].'.|TXT|0',
  146. 'name' => '@',
  147. 'type' => 'TXT',
  148. 'class' => 'IN',
  149. 'data' => $this->dmarcConfig
  150. );
  151. array_push($zoneRecords, $dmarcRecord);
  152. foreach($dnsZone['data']->records as $record) {
  153. if($record->type == 'MX') continue;
  154. if(!$record->type === 'TXT') {
  155. // skip dmarc
  156. if(preg_match('/^v=DMARC1(.*)$/i', trim($record->rdata->txtdata,'"'))) continue;
  157. // skip spf
  158. if(preg_match('/^v=spf(.*)$/i', trim($record->rdata->txtdata,'"'))) continue;
  159. // skip own dkim
  160. if(($this->dkimName == $record->name) && ($this->domainKey == trim($record->rdata->txtdata,'"'))) continue;
  161. };
  162. array_push($zoneRecords, $record);
  163. }
  164. logModuleCall(
  165. 'kerioEmail',
  166. __FUNCTION__,
  167. $this->params,
  168. 'DEbug',
  169. $zoneRecords
  170. );
  171. /* $result = localAPI('dnsmanager' ,
  172. array(
  173. 'dnsaction' => 'updateZone',
  174. 'zone_id' => $zoneID,
  175. 'records' => $zoneRecords,
  176. )
  177. );
  178. if($result['result'] != 'success') {
  179. return 'Error: cloud not update zone for ID ' . $zoneID;
  180. } */
  181. return 'success';
  182. }
  183. function KerioEmailunsetMX()
  184. {
  185. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  186. ->select('id')
  187. ->where('name', '=', $this->params['domain'])
  188. ->get();
  189. $zoneIDobj = $zoneIDcollection[0];
  190. $zoneID = $zoneIDobj->{'id'};
  191. if(!isset($zoneID)) {
  192. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  193. }
  194. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  195. if($dnsZone['result'] != 'success') {
  196. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  197. }
  198. $zoneRecords = array();
  199. foreach($dnsZone['data']->records as $record) {
  200. if($record->type == 'MX') continue;
  201. array_push($zoneRecords, $record);
  202. }
  203. logModuleCall(
  204. 'kerioEmail',
  205. __FUNCTION__,
  206. $this->params,
  207. 'DEbug',
  208. $zoneRecords
  209. );
  210. /* $result = localAPI('dnsmanager' ,
  211. array(
  212. 'dnsaction' => 'updateZone',
  213. 'zone_id' => $zoneID,
  214. 'records' => $zoneRecords,
  215. )
  216. );
  217. if($result['result'] != 'success') {
  218. return 'Error: cloud not update zone for ID ' . $zoneID;
  219. } */
  220. return 'success';
  221. }
  222. }