SimpleDNSV8.php 13 KB

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