SimpleDNSV8.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
  6. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
  7. use MGModule\DNSManager2\mgLibs\custom\dns\utils\ReverseDNSHelper;
  8. class SimpleDNSV8 extends dns\SubmoduleAbstract implements interfaces\SubmoduleRDNSInterface, interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface, interfaces\SubmoduleDNSSecInterface
  9. {
  10. CONST KEY_NAMESPACE = "MGModule\DNSManager2\mgLibs\custom\dns\dnssec\\";
  11. CONST ZSKID = 1;
  12. CONST KSKID = 2;
  13. public $configFields = [
  14. 'username' => [
  15. 'friendlyName' => 'Username',
  16. 'validators' => [
  17. 'required' => 'required',
  18. ]
  19. ],
  20. 'password' => [
  21. 'friendlyName' => 'Password',
  22. 'type' => 'password',
  23. 'validators' => [
  24. 'required' => 'required',
  25. ]
  26. ],
  27. 'hostname' => [
  28. 'friendlyName' => 'Hostname',
  29. 'validators' => [
  30. 'required' => 'required',
  31. ]
  32. ],
  33. 'ssl' => [
  34. 'friendlyName' => 'SSL',
  35. 'type' => 'yesno',
  36. 'validators' => [
  37. 'required' => 'required',
  38. ]
  39. ],
  40. 'port' => [
  41. 'friendlyName' => 'Port',
  42. 'type' => 'number',
  43. 'value' => '8053',
  44. 'validators' => [
  45. 'required' => 'required',
  46. ]
  47. ],
  48. 'httpauth' => array(
  49. 'friendlyName' => 'HTTP Auth Type',
  50. 'type' => 'select',
  51. 'options' => array(CURLAUTH_BASIC => 'BASIC', CURLAUTH_DIGEST => 'DIGEST'),
  52. ),
  53. 'ttl' => [
  54. 'friendlyName' => 'TTL',
  55. 'type' => 'number',
  56. 'value' => '360',
  57. 'validators' => [
  58. 'min' => 1,
  59. 'required' => 'required',
  60. ]
  61. ],
  62. ];
  63. private $primaryDNS = '';
  64. private $defaultTTL = '';
  65. public $availableTypes = [
  66. 'A',
  67. 'AAAA',
  68. 'NS',
  69. 'MX',
  70. 'CNAME',
  71. 'TXT',
  72. 'SRV',
  73. 'PTR',
  74. 'DNSKEY'
  75. ];
  76. /**
  77. * @var \MGModule\DNSManager2\mgLibs\custom\dns\submodules\SimpleDNSV8\Api
  78. */
  79. private $api;
  80. public function loadApiInstance()
  81. {
  82. if (!$this->api)
  83. {
  84. $params = [
  85. 'login' => $this->config['username'],
  86. 'password' => $this->config['password'],
  87. 'hostname' => $this->config['hostname'],
  88. 'port' => $this->config['port'],
  89. 'ssl' => $this->config['ssl'],
  90. 'httpauth' => $this->config['httpauth']
  91. ];
  92. $curl = new SimpleDNSV8\Curl($params);
  93. $this->api = new SimpleDNSV8\Api($curl);
  94. return $this->api;
  95. }
  96. return $this->api;
  97. }
  98. public function testConnection()
  99. {
  100. try
  101. {
  102. $this->loadApiInstance();
  103. $this->api->getZone('mgdomain.com');
  104. }
  105. catch (\Exception $ex)
  106. {
  107. if (strpos(strtolower($ex->getMessage()), 'not found') !== false || strpos(strtoupper($ex->getMessage()), 'IN SOA') !== false)
  108. {
  109. return true;
  110. }
  111. if (strlen($ex->getMessage()) === 0)
  112. {
  113. throw new exceptions\DNSSubmoduleException('Error: Invalid login details', dns\SubmoduleExceptionCodes::INVALID_PARAMETERS);
  114. }
  115. throw new exceptions\DNSSubmoduleException('Error: ' . $ex->getMessage(), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  116. }
  117. return true;
  118. }
  119. public function getZones()
  120. {
  121. $this->loadApiInstance();
  122. $zones = $this->api->getZones();
  123. $out = [];
  124. foreach ($zones as $zone)
  125. {
  126. $out[trim($zone->Name)] = '';
  127. }
  128. return $out;
  129. }
  130. public function zoneExists()
  131. {
  132. try
  133. {
  134. $this->loadApiInstance();
  135. $zones = $this->api->getZones();
  136. foreach ($zones as $zone)
  137. {
  138. if (trim(strtolower($zone->Name)) == strtolower($this->domain))
  139. {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. catch (\Exception $e)
  146. {
  147. if ($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR)
  148. {
  149. return false;
  150. }
  151. throw new exceptions\DNSSubmoduleException($e->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  152. }
  153. }
  154. public function getRecords($recordType = false)
  155. {
  156. $availableRecords = $recordType !== false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes();
  157. try
  158. {
  159. $this->loadApiInstance();
  160. $response = $this->api->getZoneRecords($this->domain);
  161. $records = [];
  162. foreach ($response as $line => $each)
  163. {
  164. if(!in_array(strtoupper($each->Type), $availableRecords))
  165. {
  166. continue;
  167. }
  168. $record = new dns\record\Record();
  169. $record->name = $each->Name;
  170. $record->ttl = empty($each->TTL) ? $this->defaultTTL : $each->TTL;
  171. $record->type = $each->Type;
  172. $record->line = $line;
  173. $record->createRDATAObject();
  174. $record->rdata->fromString(trim($each->Data));
  175. $record->customData = $each->Comment;
  176. $this->deleteDot($record);
  177. $records[] = $record;
  178. }
  179. return $records;
  180. }
  181. catch (\Exception $ex)
  182. {
  183. throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  184. }
  185. }
  186. public function addRecord(dns\record\Record $record)
  187. {
  188. try
  189. {
  190. $this->loadApiInstance();
  191. $this->api->addRecord($this->domain, $this->recordToParamsArray($record));
  192. }
  193. catch (\Exception $ex)
  194. {
  195. throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  196. }
  197. }
  198. public function editRecord(dns\record\Record $record)
  199. {
  200. try
  201. {
  202. $recordsList = $this->getRecords();
  203. foreach ($recordsList as $rec)
  204. {
  205. if ($record->line == $rec->line)
  206. {
  207. $this->deleteRecord($rec);
  208. $this->addRecord($record);
  209. break;
  210. }
  211. }
  212. }
  213. catch (\Exception $ex)
  214. {
  215. throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  216. }
  217. }
  218. public function deleteRecord(dns\record\Record $record)
  219. {
  220. try
  221. {
  222. $this->loadApiInstance();
  223. if (substr($record->name, -1) == '.')
  224. {
  225. $record->name = substr($record->name, 0, -1);
  226. }
  227. $this->addDot($record);
  228. $data = [
  229. 'Name' => $record->name,
  230. 'Type' => $record->type,
  231. 'Data' => $record->rdata->toString()
  232. ];
  233. $this->api->removeRecord($this->domain, $data);
  234. }
  235. catch (\Exception $ex)
  236. {
  237. throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  238. }
  239. }
  240. public function activateZone()
  241. {
  242. $this->loadApiInstance();
  243. $data = [
  244. 'Type' => "Primary",
  245. 'DefaultTTL' => (int)$this->config['ttl'],
  246. ];
  247. $this->api->createZone($this->domain, $data);
  248. if (!$this->zoneExists())
  249. {
  250. throw new exceptions\DNSSubmoduleException('Cannot create zone', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  251. }
  252. }
  253. public function terminateZone()
  254. {
  255. $this->loadApiInstance();
  256. $this->api->deleteZone($this->domain);
  257. if ($this->zoneExists())
  258. {
  259. throw new exceptions\DNSSubmoduleException('Cannot terminate zone', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  260. }
  261. }
  262. private function addDot(dns\record\Record &$record)
  263. {
  264. if ($record->type == 'CNAME' && $record->rdata->toString() == '@')
  265. {
  266. return;
  267. }
  268. if ($record->type == 'SRV' || $record->type == 'MX' || $record->type == 'NS' || $record->type == 'CNAME' || $record->type == 'PTR')
  269. {
  270. $str = $record->rdata->toString();
  271. $str = rtrim($str, '.') . '.';
  272. $record->rdata->fromString($str);
  273. }
  274. }
  275. private function deleteDot(dns\record\Record &$record)
  276. {
  277. if ($record->type == 'SRV' || $record->type == 'MX' || $record->type == 'NS' || $record->type == 'CNAME' || $record->type == 'PTR')
  278. {
  279. $string = rtrim($record->rdata->toString(), '.');
  280. $record->rdata->fromString($string);
  281. }
  282. }
  283. public function getRDNSRecord( $ip )
  284. {
  285. $clone = $this->getClone($ip);
  286. $reverseRecordName = ReverseDNSHelper::reverseRecordName($ip);
  287. foreach( $clone->getRecords('PTR') as $record )
  288. {
  289. $record->name = rtrim($record->name, '.') . '.';
  290. if( $record->name === $reverseRecordName || $record->nameToRelative($clone->domain) === $reverseRecordName )
  291. {
  292. return $record;
  293. }
  294. }
  295. return false;
  296. }
  297. private function recordToParamsArray(dns\record\Record $record)
  298. {
  299. $relativeName = $record->nameToRelative($this->domain);
  300. $this->addDot($record);
  301. if (!$relativeName || $relativeName == '@')
  302. {
  303. $relativeName = $this->domain;
  304. }
  305. else if (strpos($relativeName, $this->domain) === false)
  306. {
  307. if (substr($relativeName, -1) !== '.')
  308. {
  309. $relativeName .= '.' . $this->domain;
  310. }
  311. else
  312. {
  313. $relativeName .= $this->domain;
  314. }
  315. }
  316. return [
  317. 'Name' => $relativeName,
  318. 'Type' => $record->type,
  319. 'TTL' => (int)$record->ttl,
  320. 'Data' => $record->rdata->toString(),
  321. ];
  322. }
  323. public function getSignKeys()
  324. {
  325. $this->loadApiInstance();
  326. $result = $this->api->getDNSSecKeys($this->domain);
  327. $dnssec = new \MGModule\DNSManager2\mgLibs\custom\dns\dnssec\DnsSec();
  328. $dnsKeys = $this->getRecords('DNSKEY');
  329. $dnsKeys = $this->convertToIdAsKey($dnsKeys);
  330. foreach ($result as $DNSSECkey)
  331. {
  332. $class = SELF::KEY_NAMESPACE . $DNSSECkey->Type;
  333. if (!class_exists($class))
  334. {
  335. continue;
  336. }
  337. $key = new $class();
  338. $key->setId($DNSSECkey->ID);
  339. $key->setStarted($DNSSECkey->Created);
  340. $key->setBits($DNSSECkey->KeySize);
  341. $key->setDnsKey($dnsKeys[$DNSSECkey->ID]);
  342. $dnssec->addKey($key);
  343. }
  344. return $dnssec;
  345. }
  346. public function convertToIdAsKey($dnsKeys)
  347. {
  348. $out = [];
  349. foreach ($dnsKeys as $dnsKey)
  350. {
  351. $id = $this->findKeyId($dnsKey->customData);
  352. if(!$id)
  353. {
  354. continue;
  355. }
  356. $out[$id] = $dnsKey->rdata;
  357. }
  358. return $out;
  359. }
  360. public function findKeyId($string)
  361. {
  362. $result = explode('/', $string);
  363. foreach ($result as $piece)
  364. {
  365. if(stripos('Key:', $piece))
  366. {
  367. continue;
  368. }
  369. return preg_replace('/[^0-9]/', '', $piece);
  370. }
  371. return false;
  372. }
  373. public function sign()
  374. {
  375. $this->loadApiInstance();
  376. $data = [
  377. 'Type' => (new dns\dnssec\ZSK())->getType(),
  378. 'Algorithm' => dns\dnssec\Algorithms::getAlgorithm(15),
  379. 'KeySize' => 2048,
  380. ];
  381. $this->api->createOrUpdateDNSSECkey($this->domain, SELF::ZSKID, $data);
  382. $data['Type'] = (new dns\dnssec\KSK())->getType();
  383. $this->api->createOrUpdateDNSSECkey($this->domain, SELF::KSKID, $data);
  384. $this->api->dnsSecSign($this->domain, 365);
  385. }
  386. public function unsign()
  387. {
  388. $this->loadApiInstance();
  389. $this->api->deleteDNSSECkey($this->domain, SELF::ZSKID);
  390. $this->api->deleteDNSSECkey($this->domain, SELF::KSKID);
  391. }
  392. public function rectify()
  393. {
  394. $this->loadApiInstance();
  395. $this->unsign();
  396. $this->sign();
  397. }
  398. public function isSigned()
  399. {
  400. $this->loadApiInstance();
  401. $result = $this->api->getDNSSecKeys($this->domain);
  402. if (empty($result))
  403. {
  404. return false;
  405. }
  406. return true;
  407. }
  408. }