DnsHelper.php 11 KB

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