DnsHelper.php 8.4 KB

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