Source for file GuiUpdateDns.php
Documentation is available at GuiUpdateDns.php
* Long description for file:
* Using ip.address and systems.name from the FreeNAC 'ip' DB, generate
* a list of dynamic DNS updates.
* The DNS update commands are written to $tmp_file, once the file has been
* written, the dns_update flag is reset for each field.
* TODO : aliases/CNAMES, then HINFO LOC SVR (separate table)
* Inputs: ip.address, systems.name
* $conf->: ddns_server, dns_domain, ddns_ttl
* $ddns_level: 0 = all hosts, 1 = hosts with dns_update bit 1 is set
* 1 = used: updated by Dyn dns [THIS is the only value we check]
* -1 = special address (network address, broadcast)
* LICENSE: This program 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.
* @author Sean Boran/Thomas Dagonnier (FreeNAC Core Team)
* @copyright 2008 FreeNAC
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License Version 2
* @link http://www.freenac.net
private $id, $action; // See also WebCommon and Common
function __construct($title=
"Updating DNS", $debug_level=
1)
parent::__construct(false); // See also WebCommon and Common
$this->logger->setDebugLevel($debug_level);
$this->debug("__construct debug=$debug_level", 2);
$this->module=
'GuiUpdateDns'; // identify module, in Webcommon
$this->table=
'ip'; // identify SQL table, in Webcommon
echo
"<hr><p class='text18'>$title</p>";
// make sure there are only DNS-ok characters
// currently ugly/basic - need to be improved
if ($position=
strpos($name, ".")) { // strip away a domain name, if there is one
$name =
substr($name, 0, $position);
$cmd=
"nslookup -type=axfr {$this->conf->dns_domain} {
$this->conf->ddns_server}";
echo
"<br>$cmd<pre class='logtext'>$res</pre>";
echo
"<hr><p>Go back to the <a href='{$this->calling_href}'>previous page</a></p>
";
public function UpdateDnsAll()
protected function call_process($cmd, $input)
// send the file to nsupdate
/* $fp = popen($nsupdate,'w');
print "<h3>Error while sending to: $nsupdate</h3>\n";
$des= array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr
$fp = proc_open($cmd, $des, $pipes);
fwrite($pipes[0], $input); // send dns updates
$retstdout= stream_get_contents($pipes[1]); // read the answer
#while (!feof($pipes[1])) // read the answer
# $ret.=fgets($pipes[1], 1024);
$retstderr= stream_get_contents($pipes[2]); // read the answer
#while (!feof($pipes[2])) // read the answer
# $ret.=fgets($pipes[2], 1024);
fclose($pipes[1]); fclose($pipes[2]);
#echo "<br>The stdout answer is:<pre class='logtext'>$retstdout</pre>";
#echo "<br>Process answer =$ret2 (0 is success)";
$this->debug("The stdout answer is:
$retstdout", 3);
$this->debug("The process answer is:
$ret2", 3);
echo "<br>The DNS server replied (look for 'status: NOERROR'):<pre class='logtext'>$retstderr</pre>";
public function UpdateDns($ddns_update_all=FALSE)
// ddns_update_all: True= all hosts, false= hosts with lastchange>lastupdate
// settings TBD: query from $conf later
#$nsupdate="nsupdate -v -t 10 "; // -d = debug, 10 sec timeout, use tcp
$nsupdate="nsupdate -d -v -t 10 "; // -d = debug, 10 sec timeout, use tcp
if ($_SESSION['nac_rights'] < 2) // TBD: change to 99 for production
throw new InsufficientRightsException('UpdateDns() ' .$_SESSION['nac_rights']);
$conn=$this->getConnection(); // make sure we have a DB connection
$query =
"SELECT ip.id as id, INET_NTOA(ip.address) as ip, systems.name as name FROM ip LEFT JOIN systems ON ip.system = systems.id WHERE ip.status=1 AND ip.system != 0 ";
if ($ddns_update_all===
FALSE) {
$query.=" AND (ip.lastupdate < ip.lastchange)";
$res =
$conn->query($query);
throw
new DatabaseErrorException($conn->error);
$dns_ina=
''; // collect the update commands
while (($host =
$res->fetch_assoc()) !==
NULL) {
if (($host['name'] == 'unknown') || ($host['ip'] == '') || ($host['name'] == '')) {
echo "Skipping invalid host={
$host['name']}, ip={
$host['ip']} <br>
";
echo "<p class='text15'>Analysing host={
$host['name']}, ip={
$host['ip']}";
$dns_ina .=
'update delete '.
$dns_name.
"\t A\r\n";
$dns_ina .=
'update add ' .
$dns_name .
"\t" .
$this->conf->ddns_ttl
.
' A ' .
$host['ip'] .
"\r\n";
$dns_ptr=
''; // reverse PTR record for this IP
$list=
explode(".", $host['ip']); // CReate reverse PTR records
#$dns_ptr.= "zone " .$list[2] ."." .$list[1] ."." .$list[0] .".in-addr.arpa \r\n";
$ptr=
$list[2] .
"." .
$list[1] .
"." .
$list[0] .
".in-addr.arpa";
$dns_ptr.=
"server {
$this->conf->ddns_server}\r\n
";
$dns_ptr.= "zone $ptr\r\n";
$dns_ptr.= "update delete {
$list[3]}.
$ptr PTR \r\n
";
$dns_ptr.= "update add {
$list[3]}.
$ptr {
$this->conf->ddns_ttl} PTR
$dns_name\r\n
";
$dns_ptr.= "answer\r\nsend \r\n";
echo "<br class='text15'>Create reverse PTR records:<pre class='logtext'>$dns_ptr</pre>";
// clear update flag: TBD: we don't really know yet if all updates will work..
$upd_clear =
"UPDATE ip SET lastupdate=NOW() WHERE id=".
$host['id'];
$res2 =
$conn->query($upd_clear);
throw
new DatabaseErrorException($conn->error);
echo "<br>There are no new changes, no DNS updates pending.";
$dns_update = "server {
$this->conf->ddns_server}\r\n
";
//$dns_update .= "zone $dns_domain\r\n"; Zone must be in name
// request verbose answer, i.e. NOERROR below:
# ;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 29828
# ;; flags: qr ra ; ZONE: 0, PREREQ: 0, UPDATE: 0, ADDITIONAL: 0
$dns_update .= "answer\n";
echo "<hr><p class='text15'>Updating the forward records, send request [$nsupdate]:<pre class='logtext'>$dns_update</pre>";
echo "<hr><p>Go back to the <a href='{
$this->calling_href}'>previous page</a></p>
";
/////////// main() should never get here .. ///////////////////////////////////////
if (isset($_POST['action']) && $_POST['action']=='Edit') {
$logger=Logger::getInstance();
$logger->debug("Edit__:action:".
$_POST['action'], 1);
if ( isset($_POST['submit']) ) { // form submit, check fields
## Initialise (standard header for all modules)
dir(dirname(__FILE__)); set_include_path("./:../");
require_once('webfuncs.inc');
$logger=Logger::getInstance();
$logger->setDebugLevel(1);
$logger->debug("Edit__ main -submit");
# Do nothing, we've been included.
Documentation generated on Mon, 17 Nov 2008 01:10:37 +0100 by phpDocumentor 1.4.0