| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
- use \MGModule\DNSManager2\mgLibs\custom\dns;
- use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
- use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
- use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
- class SimpleDNSV8 extends dns\SubmoduleAbstract implements interfaces\SubmoduleRDNSInterface, interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface, interfaces\SubmoduleDNSSecInterface
- {
- CONST KEY_NAMESPACE = "MGModule\DNSManager2\mgLibs\custom\dns\dnssec\\";
- CONST ZSKID = 1;
- CONST KSKID = 2;
- public $configFields = [
- 'username' => [
- 'friendlyName' => 'Username',
- 'validators' => [
- 'required' => 'required',
- ]
- ],
- 'password' => [
- 'friendlyName' => 'Password',
- 'type' => 'password',
- 'validators' => [
- 'required' => 'required',
- ]
- ],
- 'hostname' => [
- 'friendlyName' => 'Hostname',
- 'validators' => [
- 'required' => 'required',
- ]
- ],
- 'ssl' => [
- 'friendlyName' => 'SSL',
- 'type' => 'yesno',
- 'validators' => [
- 'required' => 'required',
- ]
- ],
- 'port' => [
- 'friendlyName' => 'Port',
- 'type' => 'number',
- 'value' => '8053',
- 'validators' => [
- 'required' => 'required',
- ]
- ],
- 'httpauth' => array(
- 'friendlyName' => 'HTTP Auth Type',
- 'type' => 'select',
- 'options' => array(CURLAUTH_BASIC => 'BASIC', CURLAUTH_DIGEST => 'DIGEST'),
- ),
- 'ttl' => [
- 'friendlyName' => 'TTL',
- 'type' => 'number',
- 'value' => '360',
- 'validators' => [
- 'min' => 1,
- 'required' => 'required',
- ]
- ],
- ];
- private $primaryDNS = '';
- private $defaultTTL = '';
- public $availableTypes = [
- 'A',
- 'AAAA',
- 'NS',
- 'MX',
- 'CNAME',
- 'TXT',
- 'SRV',
- 'PTR',
- 'DNSKEY'
- ];
- /**
- * @var \MGModule\DNSManager2\mgLibs\custom\dns\submodules\SimpleDNSV8\Api
- */
- private $api;
- public function loadApiInstance()
- {
- if (!$this->api)
- {
- $params = [
- 'login' => $this->config['username'],
- 'password' => $this->config['password'],
- 'hostname' => $this->config['hostname'],
- 'port' => $this->config['port'],
- 'ssl' => $this->config['ssl'],
- 'httpauth' => $this->config['httpauth']
- ];
- $curl = new SimpleDNSV8\Curl($params);
- $this->api = new SimpleDNSV8\Api($curl);
- return $this->api;
- }
- return $this->api;
- }
- public function testConnection()
- {
- try
- {
- $this->loadApiInstance();
- $this->api->getZone('mgdomain.com');
- }
- catch (\Exception $ex)
- {
- if (strpos(strtolower($ex->getMessage()), 'not found') !== false || strpos(strtoupper($ex->getMessage()), 'IN SOA') !== false)
- {
- return true;
- }
- if (strlen($ex->getMessage()) === 0)
- {
- throw new exceptions\DNSSubmoduleException('Error: Invalid login details', dns\SubmoduleExceptionCodes::INVALID_PARAMETERS);
- }
- throw new exceptions\DNSSubmoduleException('Error: ' . $ex->getMessage(), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
- return true;
- }
- public function getZones()
- {
- $this->loadApiInstance();
- $zones = $this->api->getZones();
- $out = [];
- foreach ($zones as $zone)
- {
- $out[trim($zone->Name)] = '';
- }
- return $out;
- }
- public function zoneExists()
- {
- try
- {
- $this->loadApiInstance();
- $zones = $this->api->getZones();
- foreach ($zones as $zone)
- {
- if (trim(strtolower($zone->Name)) == strtolower($this->domain))
- {
- return true;
- }
- }
- return false;
- }
- catch (\Exception $e)
- {
- if ($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR)
- {
- return false;
- }
- throw new exceptions\DNSSubmoduleException($e->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- }
- public function getRecords($recordType = false)
- {
- $availableRecords = $recordType !== false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes();
-
- try
- {
- $this->loadApiInstance();
- $response = $this->api->getZoneRecords($this->domain);
- $records = [];
- foreach ($response as $line => $each)
- {
- if(!in_array(strtoupper($each->Type), $availableRecords))
- {
- continue;
- }
- $record = new dns\record\Record();
- $record->name = $each->Name;
- $record->ttl = empty($each->TTL) ? $this->defaultTTL : $each->TTL;
- $record->type = $each->Type;
- $record->line = $line;
- $record->createRDATAObject();
- $record->rdata->fromString(trim($each->Data));
- $record->customData = $each->Comment;
- $this->deleteDot($record);
- $records[] = $record;
- }
- return $records;
- }
- catch (\Exception $ex)
- {
- throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- }
- public function addRecord(dns\record\Record $record)
- {
- try
- {
- $this->loadApiInstance();
- $this->api->addRecord($this->domain, $this->recordToParamsArray($record));
- }
- catch (\Exception $ex)
- {
- throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- }
- public function editRecord(dns\record\Record $record)
- {
- try
- {
- $recordsList = $this->getRecords();
- foreach ($recordsList as $rec)
- {
- if ($record->line == $rec->line)
- {
- $this->deleteRecord($rec);
- $this->addRecord($record);
- break;
- }
- }
- }
- catch (\Exception $ex)
- {
- throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- }
- public function deleteRecord(dns\record\Record $record)
- {
- try
- {
- $this->loadApiInstance();
- if (substr($record->name, -1) == '.')
- {
- $record->name = substr($record->name, 0, -1);
- }
- $this->addDot($record);
- $data = [
- 'Name' => $record->name,
- 'Type' => $record->type,
- 'Data' => $record->rdata->toString()
- ];
- $this->api->removeRecord($this->domain, $data);
- }
- catch (\Exception $ex)
- {
- throw new exceptions\DNSSubmoduleException($ex->getMessage(), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- }
- public function activateZone()
- {
- $this->loadApiInstance();
- $data = [
- 'Type' => "Primary",
- 'DefaultTTL' => (int)$this->config['ttl'],
- ];
- $this->api->createZone($this->domain, $data);
- if (!$this->zoneExists())
- {
- throw new exceptions\DNSSubmoduleException('Cannot create zone', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- }
- public function terminateZone()
- {
- $this->loadApiInstance();
- $this->api->deleteZone($this->domain);
- if ($this->zoneExists())
- {
- throw new exceptions\DNSSubmoduleException('Cannot terminate zone', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- }
- private function addDot(dns\record\Record &$record)
- {
- if ($record->type == 'CNAME' && $record->rdata->toString() == '@')
- {
- return;
- }
- if ($record->type == 'SRV' || $record->type == 'MX' || $record->type == 'NS' || $record->type == 'CNAME' || $record->type == 'PTR')
- {
- $str = $record->rdata->toString();
- $str = rtrim($str, '.') . '.';
- $record->rdata->fromString($str);
- }
- }
- private function deleteDot(dns\record\Record &$record)
- {
- if ($record->type == 'SRV' || $record->type == 'MX' || $record->type == 'NS' || $record->type == 'CNAME' || $record->type == 'PTR')
- {
- $string = rtrim($record->rdata->toString(), '.');
- $record->rdata->fromString($string);
- }
- }
- private function recordToParamsArray(dns\record\Record $record)
- {
- $relativeName = $record->nameToRelative($this->domain);
- $this->addDot($record);
- if (!$relativeName || $relativeName == '@')
- {
- $relativeName = $this->domain;
- }
- else if (strpos($relativeName, $this->domain) === false)
- {
- if (substr($relativeName, -1) !== '.')
- {
- $relativeName .= '.' . $this->domain;
- }
- else
- {
- $relativeName .= $this->domain;
- }
- }
- return [
- 'Name' => $relativeName,
- 'Type' => $record->type,
- 'TTL' => (int)$record->ttl,
- 'Data' => $record->rdata->toString(),
- ];
- }
- public function getSignKeys()
- {
- $this->loadApiInstance();
- $result = $this->api->getDNSSecKeys($this->domain);
- $dnssec = new \MGModule\DNSManager2\mgLibs\custom\dns\dnssec\DnsSec();
- $dnsKeys = $this->getRecords('DNSKEY');
- $dnsKeys = $this->convertToIdAsKey($dnsKeys);
- foreach ($result as $DNSSECkey)
- {
- $class = SELF::KEY_NAMESPACE . $DNSSECkey->Type;
- if (!class_exists($class))
- {
- continue;
- }
- $key = new $class();
- $key->setId($DNSSECkey->ID);
- $key->setStarted($DNSSECkey->Created);
- $key->setBits($DNSSECkey->KeySize);
- $key->setDnsKey($dnsKeys[$DNSSECkey->ID]);
- $dnssec->addKey($key);
- }
- return $dnssec;
- }
- public function convertToIdAsKey($dnsKeys)
- {
- $out = [];
- foreach ($dnsKeys as $dnsKey)
- {
- $id = $this->findKeyId($dnsKey->customData);
- if(!$id)
- {
- continue;
- }
- $out[$id] = $dnsKey->rdata;
- }
- return $out;
- }
- public function findKeyId($string)
- {
- $result = explode('/', $string);
- foreach ($result as $piece)
- {
- if(stripos('Key:', $piece))
- {
- continue;
- }
- return preg_replace('/[^0-9]/', '', $piece);
- }
- return false;
- }
- public function sign()
- {
- $this->loadApiInstance();
- $data = [
- 'Type' => (new dns\dnssec\ZSK())->getType(),
- 'Algorithm' => dns\dnssec\Algorithms::getAlgorithm(15),
- 'KeySize' => 2048,
- ];
- $this->api->createOrUpdateDNSSECkey($this->domain, SELF::ZSKID, $data);
- $data['Type'] = (new dns\dnssec\KSK())->getType();
- $this->api->createOrUpdateDNSSECkey($this->domain, SELF::KSKID, $data);
- $this->api->dnsSecSign($this->domain, 365);
- }
- public function unsign()
- {
- $this->loadApiInstance();
- $this->api->deleteDNSSECkey($this->domain, SELF::ZSKID);
- $this->api->deleteDNSSECkey($this->domain, SELF::KSKID);
- }
- public function rectify()
- {
- $this->loadApiInstance();
- $this->unsign();
- $this->sign();
- }
- public function isSigned()
- {
- $this->loadApiInstance();
- $result = $this->api->getDNSSecKeys($this->domain);
- if (empty($result))
- {
- return false;
- }
- return true;
- }
- }
|