DnsHelper.php 10 KB

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