DnsHelper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. logModuleCall(
  164. 'kerioEmail',
  165. __FUNCTION__,
  166. $this->params,
  167. 'DEbug',
  168. $vars
  169. );
  170. return $vars;
  171. }
  172. function KerioEmail_setDNS()
  173. {
  174. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  175. ->select('id')
  176. ->where('name', '=', $this->params['domain'])
  177. ->get();
  178. $zoneIDobj = $zoneIDcollection[0];
  179. $zoneID = $zoneIDobj->{'id'};
  180. if(!isset($zoneID)) {
  181. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  182. }
  183. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  184. if($dnsZone['result'] != 'success') {
  185. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  186. }
  187. $zoneRecords = array();
  188. $mxRecord = array(
  189. 'line' => $this->params['domain'].'.|MX|0',
  190. 'name' => '@',
  191. 'type' => 'MX',
  192. 'class' => 'IN',
  193. 'data' => array(
  194. 'preference' => '10',
  195. 'exchange' => $this->params['serverhostname'],
  196. ),
  197. );
  198. array_push($zoneRecords, $mxRecord);
  199. $spfRecord = array(
  200. 'line' => $this->params['domain'].'.|TXT|0',
  201. 'name' => '@',
  202. 'type' => 'TXT',
  203. 'class' => 'IN',
  204. 'data' => $this->spfConfig
  205. );
  206. array_push($zoneRecords, $spfRecord);
  207. $dmarcRecord = array(
  208. 'line' => $this->params['domain'].'.|TXT|0',
  209. 'name' => '@',
  210. 'type' => 'TXT',
  211. 'class' => 'IN',
  212. 'data' => $this->dmarcConfig
  213. );
  214. array_push($zoneRecords, $dmarcRecord);
  215. foreach($dnsZone['data']->records as $record) {
  216. if($record->type == 'MX') continue;
  217. if(!$record->type === 'TXT') {
  218. // skip dmarc
  219. if(preg_match('/^v=DMARC1(.*)$/i', trim($record->rdata->txtdata,'"'))) continue;
  220. // skip spf
  221. if(preg_match('/^v=spf(.*)$/i', trim($record->rdata->txtdata,'"'))) continue;
  222. // skip own dkim
  223. if(($this->dkimName == $record->name) && ($this->domainKey == trim($record->rdata->txtdata,'"'))) continue;
  224. };
  225. array_push($zoneRecords, $record);
  226. }
  227. logModuleCall(
  228. 'kerioEmail',
  229. __FUNCTION__,
  230. $this->params,
  231. 'DEbug',
  232. $zoneRecords
  233. );
  234. /* $result = localAPI('dnsmanager' ,
  235. array(
  236. 'dnsaction' => 'updateZone',
  237. 'zone_id' => $zoneID,
  238. 'records' => $zoneRecords,
  239. )
  240. );
  241. if($result['result'] != 'success') {
  242. return 'Error: cloud not update zone for ID ' . $zoneID;
  243. } */
  244. return 'success';
  245. }
  246. function KerioEmail_unsetMX()
  247. {
  248. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  249. ->select('id')
  250. ->where('name', '=', $this->params['domain'])
  251. ->get();
  252. $zoneIDobj = $zoneIDcollection[0];
  253. $zoneID = $zoneIDobj->{'id'};
  254. if(!isset($zoneID)) {
  255. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  256. }
  257. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  258. if($dnsZone['result'] != 'success') {
  259. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  260. }
  261. $zoneRecords = array();
  262. foreach($dnsZone['data']->records as $record) {
  263. if($record->type == 'MX') continue;
  264. array_push($zoneRecords, $record);
  265. }
  266. logModuleCall(
  267. 'kerioEmail',
  268. __FUNCTION__,
  269. $this->params,
  270. 'DEbug',
  271. $zoneRecords
  272. );
  273. /* $result = localAPI('dnsmanager' ,
  274. array(
  275. 'dnsaction' => 'updateZone',
  276. 'zone_id' => $zoneID,
  277. 'records' => $zoneRecords,
  278. )
  279. );
  280. if($result['result'] != 'success') {
  281. return 'Error: cloud not update zone for ID ' . $zoneID;
  282. } */
  283. return 'success';
  284. }
  285. }