DnsHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 getMX($domain){
  25. try {
  26. $result = $this->resolver->query($domain, 'MX');
  27. } catch(\Net_DNS2_Exception $e) {
  28. echo "::query() failed: ", $e->getMessage(), "\n";
  29. }
  30. $domainMX = $result->answer;
  31. if(empty($domainMX)){
  32. $vars['mx'] = 'unset';
  33. $vars['mxtarget'] = $this->params['serverhostname'];
  34. } else {
  35. if(count($domainMX) > 1) {
  36. $vars['multiMX'] = TRUE;
  37. } else {
  38. $vars['multiMX'] = FALSE;
  39. }
  40. $vars['domainMX'] = $domainMX;
  41. $domainMXrecord = array_shift($domainMX);
  42. $vars['mxtarget'] = $domainMXrecord->exchange;
  43. if($domainMXrecord->exchange == $this->params['serverhostname']) {
  44. $vars['mx'] = 'set';
  45. } else {
  46. $var['mx'] = 'wrong';
  47. }
  48. }
  49. return $vars;
  50. }
  51. public function getSPF($domain){
  52. try {
  53. $result = $this->resolver->query($domain, 'MX');
  54. } catch(\Net_DNS2_Exception $e) {
  55. echo "::query() failed: ", $e->getMessage(), "\n";
  56. }
  57. $domainMX = $result->answer;
  58. if(count($domainMX) > 1) {
  59. $vars['multiMX'] = TRUE;
  60. } else {
  61. $vars['multiMX'] = FALSE;
  62. }
  63. if(empty($domainMX)){
  64. $vars['mx'] = 'unset';
  65. $vars['mxtarget'] = $this->params['serverhostname'];
  66. } else {
  67. $vars['domainMX'] = $domainMX;
  68. $domainMXrecord = array_shift($domainMX);
  69. $vars['mxtarget'] = $domainMXrecord->exchange;
  70. if($domainMXrecord->exchange == $this->params['serverhostname']) {
  71. $vars['mx'] = 'set';
  72. } else {
  73. $var['mx'] = 'wrong';
  74. }
  75. }
  76. return $vars;
  77. }
  78. public function getRecords($domain) {
  79. $zoneID = $this->selfDns($domain);
  80. if($zoneID) {
  81. return $this->getLocalRecords($zoneID);
  82. }
  83. return $this->getResolverRecords($domain);
  84. }
  85. public function getResolverRecords($domain)
  86. {
  87. try {
  88. $responseMX = $this->resolver->query($domain, 'MX');
  89. $responseTXT = $this->resolver->query($domain, 'TXT');
  90. } catch(\Net_DNS2_Exception $e) {
  91. echo "::query() failed: ", $e->getMessage(), "\n";
  92. }
  93. $domainMX = $responseMX->answer;
  94. $domainTXT = $responseTXT->answer;
  95. if(empty($domainMX)){
  96. $vars['mx'] = [ 'status' => 'unset', 'target' => 'none'];
  97. } else {
  98. foreach($domainMX as $mxRecord) {
  99. if($mxRecord->exchange == $this->params['serverhostname']) {
  100. array_push($vars['mx'], [ 'status' => 'set','target' => $mxRecord->exchange]);
  101. }
  102. }
  103. if(empty($vars['mx'])){
  104. $vars['mx'] = [ 'status' => 'wrong', 'target' => $mxRrecord->exchange];
  105. }
  106. }
  107. logModuleCall(
  108. 'kerioEmail',
  109. __FUNCTION__,
  110. $vars['mx'],
  111. 'DEbug',
  112. $domain
  113. );
  114. $domainSPF = array();
  115. $domainDMARC = array();
  116. $domainDKIM = array();
  117. if(empty($domainTXT)){
  118. $vars['spf'] = 'unset';
  119. $vars['dmarc'] = 'unset';
  120. $vars['dkim'] = 'unset';
  121. } else {
  122. foreach($domainTXT as $txtRecord) {
  123. foreach($txtRecord->text as $txtData) {
  124. if(strstr($txtData,'v=spf')) {
  125. array_push($domainSPF,$txtData);
  126. }
  127. if(strstr($txtData,'v=DKIM')) {
  128. array_push($domainDKIM,$txtData);
  129. }
  130. if(strstr($txtData,'v=DMARC')) {
  131. array_push($domainDMARC,$txtData);
  132. }
  133. }
  134. }
  135. # SPF, multi verboten
  136. if (count($domainSPF) > 1) {
  137. $vars['multiSPF'] = TRUE;
  138. $vars['spf'] = 'wrong';
  139. } else {
  140. $vars['multiSPF'] = FALSE;
  141. if (empty($domainSPF)) {
  142. $vars['spf'] = 'unset';
  143. } else {
  144. if($domainSPF[0] === $spfConfig) {
  145. $vars['spf'] = 'set';
  146. } else {
  147. $vars['spf'] = 'wrong';
  148. }
  149. }
  150. }
  151. # DKIM
  152. if (count($domainDKIM) > 1) {
  153. $vars['multiDKIM'] = TRUE;
  154. } else {
  155. $vars['multiDKIM'] = FALSE;
  156. }
  157. if (empty($domainDKIM)) {
  158. $vars['dkim'] = 'unset';
  159. } else {
  160. $vars['dkim'] = 'set';
  161. }
  162. $vars['domainDKIM'] = $domainDKIM;
  163. # DMARC
  164. if (count($domainDMARC) > 1) {
  165. $vars['multiDMARC'] = TRUE;
  166. } else {
  167. $vars['multiDMARC'] = FALSE;
  168. }
  169. $vars['dmarc'] = 'wrong';
  170. if (empty($domainDMARC)) {
  171. $vars['dmarc'] = 'unset';
  172. } else {
  173. foreach($domainDMARC as $dmarc) {
  174. if($dmarc === $dmarcConfig) {
  175. $vars['dmarc'] = 'set';
  176. }
  177. }
  178. }
  179. $vars['domainDMARC'] = $domainDMARC;
  180. }
  181. return $vars;
  182. }
  183. public function selfDns($domain){
  184. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  185. ->select('id')
  186. ->where('name', '=', $domain)
  187. ->get();
  188. $zoneIDobj = $zoneIDcollection[0];
  189. if(!isset($zoneIDobj->{'id'})) {
  190. return false;
  191. }
  192. return $zoneIDobj->{'id'};
  193. }
  194. public function getLocalRecords($zoneID) {
  195. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  196. if($dnsZone['result'] != 'success') {
  197. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  198. }
  199. logModuleCall(
  200. 'kerioEmail',
  201. __FUNCTION__,
  202. $zoneID,
  203. 'DEbug',
  204. $dnsZone
  205. );
  206. $zoneRecords = array();
  207. foreach($dnsZone['data']->records as $record) {
  208. if(in_array($record->type, ['MX', 'TXT'])){
  209. array_push($zoneRecords, $record);
  210. }
  211. }
  212. return $zoneRecords;
  213. }
  214. function KerioEmailsetDNS()
  215. {
  216. return 'success';
  217. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  218. ->select('id')
  219. ->where('name', '=', $this->params['domain'])
  220. ->get();
  221. $zoneIDobj = $zoneIDcollection[0];
  222. $zoneID = $zoneIDobj->{'id'};
  223. if(!isset($zoneID)) {
  224. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  225. }
  226. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  227. logModuleCall(
  228. 'kerioEmail',
  229. __FUNCTION__,
  230. $this->params,
  231. 'DEbug',
  232. $dnsZone['result']
  233. );
  234. if($dnsZone['result'] != 'success') {
  235. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  236. }
  237. $zoneRecords = array();
  238. $mxRecord = array(
  239. 'line' => $this->params['domain'].'.|MX|0',
  240. 'name' => '@',
  241. 'type' => 'MX',
  242. 'class' => 'IN',
  243. 'data' => array(
  244. 'preference' => '10',
  245. 'exchange' => $this->params['serverhostname'],
  246. ),
  247. );
  248. array_push($zoneRecords, $mxRecord);
  249. $spfRecord = array(
  250. 'line' => $this->params['domain'].'.|TXT|0',
  251. 'name' => '@',
  252. 'type' => 'TXT',
  253. 'class' => 'IN',
  254. 'data' => $this->spfConfig
  255. );
  256. array_push($zoneRecords, $spfRecord);
  257. $dmarcRecord = array(
  258. 'line' => $this->params['domain'].'.|TXT|0',
  259. 'name' => '@',
  260. 'type' => 'TXT',
  261. 'class' => 'IN',
  262. 'data' => $this->dmarcConfig
  263. );
  264. array_push($zoneRecords, $dmarcRecord);
  265. foreach($dnsZone['data']->records as $record) {
  266. if($record->type == 'MX') continue;
  267. if(!$record->type === 'TXT') {
  268. // skip dmarc
  269. if(preg_match('/^v=DMARC1(.*)$/i', trim($record->rdata->txtdata,'"'))) continue;
  270. // skip spf
  271. if(preg_match('/^v=spf(.*)$/i', trim($record->rdata->txtdata,'"'))) continue;
  272. // skip own dkim
  273. if(($this->dkimName == $record->name) && ($this->domainKey == trim($record->rdata->txtdata,'"'))) continue;
  274. };
  275. array_push($zoneRecords, $record);
  276. }
  277. logModuleCall(
  278. 'kerioEmail',
  279. __FUNCTION__,
  280. $this->params,
  281. 'DEbug',
  282. $zoneRecords
  283. );
  284. /* $result = localAPI('dnsmanager' ,
  285. array(
  286. 'dnsaction' => 'updateZone',
  287. 'zone_id' => $zoneID,
  288. 'records' => $zoneRecords,
  289. )
  290. );
  291. if($result['result'] != 'success') {
  292. return 'Error: cloud not update zone for ID ' . $zoneID;
  293. } */
  294. return 'success';
  295. }
  296. function KerioEmailunsetMX()
  297. {
  298. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  299. ->select('id')
  300. ->where('name', '=', $this->params['domain'])
  301. ->get();
  302. $zoneIDobj = $zoneIDcollection[0];
  303. $zoneID = $zoneIDobj->{'id'};
  304. if(!isset($zoneID)) {
  305. return 'Error: zone ID not found for domain ' . $this->params['domain'];
  306. }
  307. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  308. if($dnsZone['result'] != 'success') {
  309. return 'Error: cloud not fetch zone for ID ' . $zoneID;
  310. }
  311. $zoneRecords = array();
  312. foreach($dnsZone['data']->records as $record) {
  313. if($record->type == 'MX') continue;
  314. array_push($zoneRecords, $record);
  315. }
  316. logModuleCall(
  317. 'kerioEmail',
  318. __FUNCTION__,
  319. $this->params,
  320. 'DEbug',
  321. $zoneRecords
  322. );
  323. /* $result = localAPI('dnsmanager' ,
  324. array(
  325. 'dnsaction' => 'updateZone',
  326. 'zone_id' => $zoneID,
  327. 'records' => $zoneRecords,
  328. )
  329. );
  330. if($result['result'] != 'success') {
  331. return 'Error: cloud not update zone for ID ' . $zoneID;
  332. } */
  333. return 'success';
  334. }
  335. }