2010-08-17 01:23:57 +02:00
|
|
|
<?php
|
2013-04-23 16:18:48 +02:00
|
|
|
/**
|
|
|
|
* Nameserver class
|
|
|
|
*
|
|
|
|
* @copyright 2013 Steffen Vogel
|
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
|
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* @link http://www.steffenvogel.de
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This file is part of sddns
|
|
|
|
*
|
|
|
|
* sddns is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* sddns is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with sddns. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2010-08-17 01:23:57 +02:00
|
|
|
|
|
|
|
class NameServer implements Object {
|
2017-08-05 17:29:32 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
protected $process;
|
|
|
|
protected $pipes;
|
|
|
|
|
|
|
|
private $queue;
|
|
|
|
|
|
|
|
public $hostname;
|
|
|
|
public $port;
|
|
|
|
|
|
|
|
public function __construct($hostname, $port = 53) {
|
2017-08-05 17:29:32 +02:00
|
|
|
global $config;
|
2010-08-17 01:23:57 +02:00
|
|
|
|
|
|
|
$this->hostname = $hostname;
|
|
|
|
$this->port = $port;
|
|
|
|
}
|
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
protected function open() {
|
|
|
|
global $output;
|
2010-08-17 01:23:57 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
$descriptorspec = array(
|
|
|
|
0 => array('pipe', 'r'), // stdin
|
|
|
|
1 => array('pipe', 'w'), // stdout
|
|
|
|
2 => array('pipe', 'w') // stderr
|
|
|
|
);
|
2010-08-17 01:23:57 +02:00
|
|
|
|
|
|
|
if ($this->isRunning()) {
|
2017-08-05 17:29:32 +02:00
|
|
|
throw new NameserverException('ns connection' , 'already established');
|
2010-08-17 01:23:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->process = proc_open('nsupdate -d -v', $descriptorspec, $this->pipes);
|
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
if ($this->isRunning()) {
|
|
|
|
$output->add('ns connection', 'debug', 3, 'established');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new NameserverException('ns connection', 'failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-08-17 01:23:57 +02:00
|
|
|
}
|
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
protected function close() {
|
|
|
|
global $output;
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
if (!$this->isRunning()) {
|
|
|
|
throw new NameserverException('there is no running process to close');
|
|
|
|
}
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
fclose($this->pipes[0]);
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
$result['stdout'] = stream_get_contents($this->pipes[1]);
|
|
|
|
$result['stderr'] = stream_get_contents($this->pipes[2]);
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
fclose($this->pipes[1]);
|
|
|
|
fclose($this->pipes[2]);
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
$result['code'] = proc_close($this->process);
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
$output->add('ns connection', 'debug', 3, 'closed');
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
$this->process = null;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function initQueue() {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$this->queue = array();
|
|
|
|
|
|
|
|
$this->queueCommand('server ' . $this->hostname . ' ' . $this->port);
|
|
|
|
$this->queueCommand('class ' . $config['sddns']['std']['class']);
|
|
|
|
$this->queueCommand('ttl ' . $config['sddns']['std']['ttl']);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sendQueue() {
|
|
|
|
while ($command = array_shift($this->queue)) {
|
|
|
|
$this->sendCommand($command);
|
2010-08-17 01:23:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
protected function commitQueue() {
|
|
|
|
$this->open();
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
$this->queueCommand('show');
|
2010-08-17 01:23:57 +02:00
|
|
|
$this->queueCommand('send');
|
2017-08-05 17:29:32 +02:00
|
|
|
$this->queueCommand('answer');
|
|
|
|
|
|
|
|
$this->sendQueue();
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
return $this->close();
|
|
|
|
}
|
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
private function sendCommand($command) {
|
|
|
|
global $output;
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
fwrite($this->pipes[0], $command . "\n");
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
if (substr($command, 0, 3) != 'key') {
|
2017-08-05 17:29:32 +02:00
|
|
|
$output->add('ns command', 'debug', 3, $command);
|
2010-08-17 01:23:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
protected function queueCommand($command) {
|
|
|
|
$this->queue[] = $command;
|
|
|
|
}
|
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
protected function add(Record $record) {
|
|
|
|
$this->queueCommand('update add ' . $record);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function delete(Record $record) {
|
|
|
|
$this->queueCommand('update delete ' . $record);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isRunning() {
|
|
|
|
return is_resource($this->process);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function query($host, $type = 'A', $class = 'IN') {
|
2017-08-05 17:29:32 +02:00
|
|
|
global $output;
|
|
|
|
global $config;
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
$cli = 'dig -c ' . $class . ' -t ' . $type . ' ' . $host . ' @' . $this->hostname . ' +noall +answer';
|
|
|
|
$output->add('execute dig', 'debug', 2, $cli);
|
2010-10-07 12:45:05 +02:00
|
|
|
exec(escapeshellcmd($cli), $return, $returnCode);
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2017-08-05 17:29:32 +02:00
|
|
|
if ($returnCode != 0) {
|
|
|
|
throw new NameServerException('dig query', 'failed', $returnCode);
|
|
|
|
}
|
2010-08-17 01:23:57 +02:00
|
|
|
|
|
|
|
$results = array();
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-10-07 12:45:05 +02:00
|
|
|
foreach ($return as $line) {
|
2010-08-17 01:23:57 +02:00
|
|
|
if (substr($line, 0, 1) != ';' && !empty($line)) {
|
|
|
|
$result = array();
|
2010-10-07 13:03:50 +02:00
|
|
|
foreach (preg_split("/\s+/", $line) as $column) {
|
2010-08-17 01:23:57 +02:00
|
|
|
if (!empty($column))
|
|
|
|
$result[] = $column;
|
|
|
|
}
|
|
|
|
$results[] = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
/*
|
|
|
|
* Output
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return $this->hostname . ':' . $this->port;
|
|
|
|
}
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
public function toXml(DOMDocument $doc, $tagName = 'nameserver') {
|
|
|
|
$xmlNs = $doc->createElement($tagName);
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
$xmlNs->appendChild($doc->createElement('hostname', $this->hostname));
|
|
|
|
$xmlNs->appendChild($doc->createElement('port', $this->port));
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
return $xmlNs;
|
|
|
|
}
|
2010-10-07 12:34:19 +02:00
|
|
|
|
2010-08-17 01:23:57 +02:00
|
|
|
public function toHtml() {
|
|
|
|
return 'nameserver: ' . $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|