DynECT.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 DynECT extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface, interfaces\SubmoduleRDNSInterface/*, interfaces\SubmoduleDNSSecInterface*/ {
  8. CONST URL = 'https://api.dynect.net/REST/';
  9. CONST WSDL = 'https://api2.dynect.net/wsdl/current/Dynect.wsdl'; //'https://api2.dynect.net/wsdl/3.0.0/Dynect.wsdl'
  10. public $configFields = array(
  11. 'username' =>array (
  12. 'friendlyName' => 'Username',
  13. 'validators' => array(
  14. 'required' => 'required',
  15. )
  16. ),
  17. 'password' =>array (
  18. 'friendlyName' => 'User Password',
  19. 'type'=> 'password',
  20. 'validators' => array(
  21. 'required' => 'required',
  22. )
  23. ),
  24. 'customer_name' =>array (
  25. 'friendlyName' => 'Customer Name',
  26. ),
  27. 'hostname' =>array (
  28. 'friendlyName' => 'Hostname/IP',
  29. 'validators' => array(
  30. 'required' => 'required',
  31. )
  32. ),
  33. 'ssl' =>array (
  34. 'friendlyName' => 'Enable SSL',
  35. 'type'=> 'yesno',
  36. ),
  37. 'default_ip' =>array (
  38. 'friendlyName' => 'Default IP',
  39. 'validators' => array(
  40. 'pattern' => Patterns::IP4_OR_IP6,
  41. )
  42. ),
  43. 'adminemail' =>array (
  44. 'friendlyName' => 'Admin Email',
  45. 'type'=> 'email',
  46. 'validators' => array(
  47. 'required' => 'required',
  48. )
  49. ),
  50. 'default_ttl' =>array ( //??
  51. 'friendlyName' => 'Default TTL',
  52. 'type' => 'number',
  53. 'help' => 'Default TTL (in seconds) for records in the zone',
  54. 'validators' => array(
  55. 'required' => 'required',
  56. ),
  57. ),
  58. /*'dnssec_contact_nickname' => array(
  59. 'friendlyName' => 'DNSSEC Contact Nickname',
  60. 'type' => 'text',
  61. )*/
  62. );
  63. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'DNAME', 'LOC', 'TXT', 'SRV', 'PTR', 'DS');
  64. private $token;
  65. public function getRecords($recordType = false){
  66. $return = $this->execute('GET', 'AllRecord/'.$this->domain);
  67. $out = array();
  68. foreach($return['data'] as $url)
  69. {
  70. $url = substr($url, 6);
  71. $type = substr($url, 0, strpos($url, 'Record'));
  72. if(!in_array(strtoupper($type), $this->getAvailableRecordTypes()))
  73. {
  74. continue;
  75. }
  76. $data = $this->execute('GET', $url);
  77. $record = new dns\record\Record();
  78. $record->line = $data['data']['record_id'];
  79. $record->name = $data['data']['fqdn'];
  80. $record->type = $data['data']['record_type'];
  81. $record->ttl = $data['data']['ttl'];
  82. $record->createRDATAObject();
  83. $record->rdata->setDataFromArray($data['data']['rdata']);
  84. if($type == 'DS')
  85. {
  86. $record->rdata->digesttype = $data['data']['rdata']['digtype'];
  87. }
  88. $out[] = $record;
  89. }
  90. return $out;
  91. }
  92. private function recordToParamsArray(dns\record\Record $record) {
  93. $params = array(
  94. 'rdata' => $record->rdata->toArray(false),
  95. 'ttl' => $record->ttl,
  96. );
  97. return $params;
  98. }
  99. public function editRecord(dns\record\Record $record) {
  100. $params = $this->recordToParamsArray($record);
  101. $this->execute('PUT', $record->type.'Record/'.$this->domain.'/'.$record->nameToAbsolute($this->domain, false) . '/' . $record->line, $params);
  102. $this->publishZone();
  103. }
  104. public function deleteRecord(dns\record\Record $record) {
  105. $ret = $this->execute('DELETE', $record->type.'Record/'.$this->domain.'/'.$record->nameToAbsolute($this->domain, false) . '/' . $record->line);
  106. $this->publishZone();
  107. }
  108. public function addRecord(dns\record\Record $record) {
  109. $params = $this->recordToParamsArray($record);
  110. $this->execute('POST', $record->type.'Record/'.$this->domain.'/'.$record->nameToAbsolute($this->domain, false), $params);
  111. $this->publishZone();
  112. }
  113. public function zoneExists() {
  114. try {
  115. $this->execute('GET', 'Zone/' . $this->domain);
  116. } catch (exceptions\DNSSubmoduleException $e) {
  117. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  118. return false;
  119. }
  120. throw $e;
  121. }
  122. return true;
  123. }
  124. public function terminateZone() {
  125. $this->execute('DELETE','Zone/'.$this->domain);
  126. }
  127. public function activateZone() {
  128. $params = array(
  129. 'ttl' => $this->config['default_ttl'],
  130. 'rname' => $this->config['adminemail'],
  131. 'serial_style' => 'epoch'
  132. );
  133. if(!$this->zoneExists()) {
  134. $this->execute('POST','Zone/'.$this->domain, $params);
  135. }
  136. $this->execute('PUT','Zone/'.$this->domain, array('publish' => 'true'));
  137. }
  138. protected function publishZone(){
  139. $this->execute('PUT','Zone/'.$this->domain, array(
  140. 'publish' => 1
  141. ));
  142. }
  143. private function execute($method, $function, $params = array()){
  144. if(empty($this->token) && $function != 'Session') {
  145. $this->login();
  146. }
  147. $header = array();
  148. $header[] = 'Content-Type: application/json';
  149. if ($function != 'Session'){
  150. $header[] = 'Auth-Token: '. $this->token;
  151. }
  152. $url = self::URL . $function . '/';
  153. if (!empty($params) && strtoupper($method) == 'GET') {
  154. $params = $this->getParams($params);
  155. $url .= '?'.$params;
  156. }
  157. $ch = curl_init();
  158. curl_setopt($ch, CURLOPT_URL, $url);
  159. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  160. curl_setopt($ch, CURLOPT_HEADER, false);
  161. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  162. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
  163. if (!empty($params)) {
  164. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  165. }
  166. curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  167. $curl_result = curl_exec($ch);
  168. //$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  169. if (curl_errno($ch)) {
  170. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($ch) . " - " . curl_error($ch), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  171. }
  172. curl_close($ch);
  173. $json = json_decode($curl_result, true);
  174. if(empty($json)) {
  175. throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  176. }
  177. if($json['status'] != 'success') {
  178. if(!empty($json['msgs'])) {
  179. $errors = array();
  180. foreach($json['msgs'] as $message) {
  181. $errors[] = $message['INFO'];
  182. }
  183. throw new exceptions\DNSSubmoduleException(implode('; ', $errors), dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  184. }
  185. throw new exceptions\DNSSubmoduleException('Unknown Error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  186. }
  187. return $json;
  188. }
  189. private function getParams($params){
  190. $string = '';
  191. foreach ($params as $key => $value){
  192. $string .= $key.'='.$value.'&';
  193. }
  194. $string = substr($string, 0, -1); //erase last &
  195. return $string;
  196. }
  197. private function login(){
  198. $result = $this->execute('POST', 'Session',
  199. array(
  200. 'customer_name' => $this->config['customer_name'],
  201. 'user_name' => $this->config['username'],
  202. 'password' => $this->config['password']
  203. )
  204. );
  205. $this->token = trim($result['data']['token']);
  206. }
  207. public function testConnection() {
  208. $this->login();
  209. }
  210. public function getZones() {
  211. $result = $this->execute('GET', 'Zone' . $this->domain);
  212. $out = array();
  213. foreach($result as $zone) {
  214. $out[$zone->zone] = '';
  215. }
  216. return $out;
  217. }
  218. /**
  219. *
  220. * @return boolean
  221. */
  222. public function isSigned()
  223. {
  224. return $this->getSignKeys() ? true : false;
  225. }
  226. public function generateSignKeys() {
  227. }
  228. /**
  229. *
  230. * @return Array of \MGModule\DNSManager2\mgLibs\custom\dns\submodules\dnssec\Key
  231. */
  232. public function getSignKeys()
  233. {
  234. try
  235. {
  236. $ret = $this->execute('GET', 'DNSSEC/'.$this->domain);
  237. $dnssec = new dns\dnssec\DnsSec();
  238. foreach($ret['data']['keys'] as $k)
  239. {
  240. $key = null;
  241. if($k['type'] == 'ZSK')
  242. {
  243. $key = new dns\dnssec\ZSK();
  244. }
  245. else
  246. {
  247. $key = new dns\dnssec\KSK();
  248. }
  249. $key->setId($k['dnssec_key_id']);
  250. $key->setExpires($k['expire_ts']);
  251. $key->setLifetime($k['lifetime']);
  252. $key->setBits($k['bits']);
  253. //DNSKEY
  254. $dnskey = new dns\record\type\DNSKEY();
  255. $dnskey->setProtocol($k['dnskey']['protocol']);
  256. $dnskey->setFlags($k['dnskey']['flags']);
  257. $dnskey->setAlgorithm($k['dnskey']['algorithm']);
  258. $dnskey->setPublicKey($k['dnskey']['public_key']);
  259. $key->setDnsKey($dnskey);
  260. //DS
  261. if($k['ds']['keytag'])
  262. {
  263. $ds = new dns\record\type\DS();
  264. $ds->setAlgorithm($k['ds']['algorithm']);
  265. $ds->setKeytag($k['ds']['keytag']);
  266. $ds->setDigestType($k['ds']['digtype']);
  267. $ds->setDigest($k['ds']['digest']);
  268. $key->setDs($ds);
  269. }
  270. $dnssec->addKey($key);
  271. }
  272. return $dnssec;
  273. }
  274. catch(exceptions\DNSSubmoduleException $ex)
  275. {
  276. if(strpos($ex->getMessage(), 'No such service in your account'))
  277. {
  278. return null;
  279. }
  280. throw $ex;
  281. }
  282. }
  283. public function sign() {
  284. $this->execute('POST', 'DNSSEC/'.$this->domain, array(
  285. 'keys' => array
  286. (
  287. array
  288. (
  289. 'type' => 'KSK',
  290. 'algorithm' => 'RSA/SHA-1',
  291. 'bits' => 2048,
  292. 'start_ts' => time(),
  293. 'lifetime' => 60*60*24*356,
  294. 'overlap' => 60*60*24*7,
  295. // use default value
  296. //'expire_ts' => ''
  297. ),
  298. array
  299. (
  300. 'type' => 'ZSK',
  301. 'algorithm' => 'RSA/SHA-1',
  302. 'bits' => 2048,
  303. 'start_ts' => time(),
  304. 'lifetime' => 60*60*24*356,
  305. 'overlap' => 60*60*24*7,
  306. // use default value
  307. //'expire_ts' => ''
  308. )
  309. ),
  310. 'contact_nickname' => $this->config['dnssec_contact_nickname']
  311. ));
  312. }
  313. public function unsign() {
  314. $this->execute('DELETE', 'DNSSEC/'.$this->domain);
  315. }
  316. }