Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new functionality to autogenerate forward (A) PowerDNS records #2705

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions api/controllers/Prefix.php
Expand Up @@ -158,6 +158,7 @@ class Prefix_controller extends Common_api_functions {
"pingSubnet",
"discoverSubnet",
"DNSrecursive",
"DNSforward",
"DNSrecords",
"nameserverId",
"scanAgent",
Expand Down
14 changes: 10 additions & 4 deletions app/admin/powerDNS/refresh-ptr-records-submit.php
Expand Up @@ -53,7 +53,7 @@
$ptr_indexes = $Addresses->ptr_get_subnet_indexes ($subnet->id);

// remove existing records and links
$PowerDNS->remove_all_ptr_records ($domain->id, $ptr_indexes);
$PowerDNS->remove_all_ptr_records ($domain->id, $ptr_indexes, $delete_reverse = $subnet->DNSforward);
$Addresses->ptr_unlink_subnet_addresses ($subnet->id);

// fetch all hosts
Expand All @@ -75,13 +75,19 @@
// validate hostname, we only add valid hostnames
elseif ($Result->validate_hostname ($h->hostname) !== false) {
// formulate new record
$record = $PowerDNS->formulate_new_record ($domain->id, $PowerDNS->get_ip_ptr_name ($h->ip), "PTR", $h->hostname, $values['ttl']);
$PTR_record = $PowerDNS->formulate_new_record ($domain->id, $PowerDNS->get_ip_ptr_name ($h->ip), "PTR", $h->hostname, $values['ttl']);
// insert record
$PowerDNS->add_domain_record ($record, false);
$PowerDNS->add_domain_record ($PTR_record, false);

// link
$Addresses->ptr_link ($h->id, $PowerDNS->lastId);

// formulate and insert forward record if option enabled
if ($subnet->DNSforward == 1) {
$A_record = $PowerDNS->formulate_new_record ($domain->id, $h->hostname, "A", $h->ip, $values['ttl']);
$PowerDNS->add_domain_record ($A_record, false);
}

// ok
$success[] = $h;
}
Expand Down Expand Up @@ -122,4 +128,4 @@


print "<p class='hidden alert-danger'></p>";
print implode("<br>", $print);
print implode("<br>", $print);
20 changes: 11 additions & 9 deletions app/admin/subnets/edit-result.php
Expand Up @@ -312,6 +312,7 @@
"resolveDNS" => $Admin->verify_checkbox(@$_POST['resolveDNS']),
"scanAgent" => @$_POST['scanAgent'],
"DNSrecursive" => $Admin->verify_checkbox(@$_POST['DNSrecursive']),
"DNSforward" => $Admin->verify_checkbox(@$_POST['DNSforward']),
"DNSrecords" => $Admin->verify_checkbox(@$_POST['DNSrecords']),
"nameserverId" => $_POST['nameserverId'],
"device" => $_POST['device'],
Expand Down Expand Up @@ -450,11 +451,12 @@
// try to fetch domain
$domain = $PowerDNS->fetch_domain_by_name ($zone);

// POST DNSrecursive not set, fake it if old is also 0
// POST DNSrecursive or DNSforward not set, fake it if old is also 0
if (!isset($_POST['DNSrecursive']) && @$old_subnet_details->DNSrecursive==0) { $_POST['DNSrecursive'] = 0; }
if (!isset($_POST['DNSforward']) && @$old_subnet_details->DNSforward==0) { $_POST['DNSforward'] = 0; }

// recreate csrf cookie
$csrf = $User->Crypto->csrf_cookie ("create", "domain");
$csrf = $User->Crypto->csrf_cookie ("create", "domain");

//delete
if ($_POST['action']=="delete") {
Expand All @@ -476,7 +478,7 @@
}
}
//create
elseif ($_POST['action']=="add" && @$_POST['DNSrecursive']=="1") {
elseif ($_POST['action']=="add" && @$_POST['DNSrecursive']) {
// if zone exists do nothing, otherwise create zone
if ($domain===false) {
// use default values
Expand All @@ -489,7 +491,7 @@
}
}
// update
elseif ($_POST['action']=="edit" && $_POST['DNSrecursive']!=$old_subnet_details->DNSrecursive) {
elseif ($_POST['action']=="edit" && ($_POST['DNSrecursive']!=$old_subnet_details->DNSrecursive || $_POST['DNSforward']!=$old_subnet_details->DNSforward)) {
// remove domain
if (!isset($_POST['DNSrecursive']) && $domain!==false) {
print "<hr><p class='hidden alert-danger'></p>";
Expand All @@ -499,9 +501,8 @@
print " <a class='btn btn-danger btn-xs' id='editDomainSubmit'>"._('Yes')."</a>";
print " <a class='btn btn-default btn-xs hidePopupsReload'>"._('No')."</a>";
print " </div>";

print _('Do you wish to delete DNS zone and all records')."?<br>";
print " &nbsp;&nbsp; DNS zone <strong>$domain->name</strong></li>";
print " &nbsp;&nbsp; DNS zone <strong>$domain->name</strong></li>";
print " <form name='domainEdit' id='domainEdit'><input type='hidden' name='action' value='delete'><input type='hidden' name='id' value='$domain->id'><input type='hidden' name='csrf_cookie' value='$csrf'></form>";
print " <div class='domain-edit-result'></div>";
print "</div>";
Expand Down Expand Up @@ -531,22 +532,23 @@
if ($h->PTRignore=="1") {
$ski++;
}
elseif ($Addresses->ptr_add ($h, false, $h->id) !== false) {
elseif ($Addresses->ptr_add ($h, false, $h->id, !empty($_POST['DNSforward'])) !== false) {
$cnt++;
}
else {
$err++;
}
}
// print
$Result->show ("success", "$cnt PTR records created");
$a_records = empty($_POST['DNSforward']) ? ' ' : ' and A ';
$Result->show ("success", "$cnt PTR{$a_records}records created");
// error
if ($err!=0) {
$Result->show ("warning", "$err invalid hostnames");
}
}
}
}
}
}
}
?>
48 changes: 36 additions & 12 deletions app/admin/subnets/edit.php
Expand Up @@ -76,10 +76,13 @@
$subnet_old_details['pingSubnet'] = @$subnet_old_temp['pingSubnet']; // inherit pingSubnet
$subnet_old_details['discoverSubnet'] = @$subnet_old_temp['discoverSubnet']; // inherit discovery
$subnet_old_details['nameserverId'] = @$subnet_old_temp['nameserverId']; // inherit nameserver
if($User->settings->enableLocations=="1")
$subnet_old_details['location'] = @$subnet_old_temp['location']; // inherit location
if($User->settings->enableCustomers=="1")
$subnet_old_details['customer_id'] = @$subnet_old_temp['customer_id']; // inherit location
if($User->settings->enableLocations)
$subnet_old_details['location'] = @$subnet_old_temp['location']; // inherit location
if($User->settings->enableCustomers)
$subnet_old_details['customer_id'] = @$subnet_old_temp['customer_id']; // inherit location
if($User->settings->enablePowerDNS)
$subnet_old_details['DNSrecursive'] = @$subnet_old_temp['DNSrecursive'];
$subnet_old_details['DNSforward'] = @$subnet_old_temp['DNSforward'];
}
# set master if it came from free space!
if(isset($_POST['freespaceMSID'])) {
Expand Down Expand Up @@ -126,6 +129,7 @@
$(".input-switch").bootstrapSwitch(switch_options);
$(".input-switch-agents-ping").bootstrapSwitch(switch_options);
$(".input-switch-agents-scan").bootstrapSwitch(switch_options);
$(".input-switch-dns-recursive").bootstrapSwitch(switch_options);

// change - agent selector
$('.input-switch-agents-ping, .input-switch-agents-scan').on('switchChange.bootstrapSwitch', function (e, data) {
Expand All @@ -137,7 +141,16 @@
if (ping==true || scan==true) { $("tr#scanAgentDropdown").removeClass("hidden"); }
else if (ping==false && scan==false) { $("tr#scanAgentDropdown").addClass("hidden"); }
});
<?php if ($User->settings->enableThreshold=="1") { ?>
// change - allow dns forward
$('.input-switch-dns-recursive').on('switchChange.bootstrapSwitch', function (e, data) {
// get state
var DNSrecursive = ($(".input-switch-dns-recursive").bootstrapSwitch('state'));

// change
if (DNSrecursive) { $("tr#dnsForwardSwitch").removeClass("hidden"); }
else { $("tr#dnsForwardSwitch").addClass("hidden"); }
});
<?php if ($User->settings->enableThreshold) { ?>
$('.slider').slider().on('slide', function(ev){
$('.slider-text span').html(ev.value);
});
Expand Down Expand Up @@ -472,12 +485,12 @@
}
print "</select>";
print "</td>";
print ' <td class="info2">'._('Select which scanagent to use').'</td>' . "\n";
print ' <td class="info2">'._('Select which scan agent to use').'</td>' . "\n";
print "</tr>";
}

//check host status
$checked = @$subnet_old_details['pingSubnet']==1 ? "checked": "";
$checked = @$subnet_old_details['pingSubnet'] ? "checked": "";
print '<tr>' . "\n";
print ' <td>'._('Check hosts status').'</td>' . "\n";
print ' <td>' . "\n";
Expand All @@ -487,7 +500,7 @@
print '</tr>';

//Discover new hosts
$checked = @$subnet_old_details['discoverSubnet']==1 ? "checked": "";
$checked = @$subnet_old_details['discoverSubnet'] ? "checked": "";
print '<tr>' . "\n";
print ' <td>'._('Discover new hosts').'</td>' . "\n";
print ' <td>' . "\n";
Expand All @@ -497,7 +510,7 @@
print '</tr>';

//resolve hostname
$checked = @$subnet_old_details['resolveDNS']==1 ? "checked": "";
$checked = @$subnet_old_details['resolveDNS'] ? "checked": "";
print '<tr>' . "\n";
print ' <td>'._('Resolve DNS names').'</td>' . "\n";
print ' <td>' . "\n";
Expand Down Expand Up @@ -558,17 +571,28 @@

//autocreate reverse records
if($User->settings->enablePowerDNS==1) {
$checked = @$subnet_old_details['DNSrecursive']==1 ? "checked": "";
$checked = @$subnet_old_details['DNSrecursive'] ? "checked": "";
print '<tr>' . "\n";
print ' <td>'._('Autocreate reverse records').'</td>' . "\n";
print ' <td>' . "\n";
print ' <input type="checkbox" name="DNSrecursive" class="input-switch" value="1" '.$checked.'>'. "\n";
print ' <input type="checkbox" name="DNSrecursive" class="input-switch-dns-recursive" value="1" '.$checked.'>'. "\n";
print ' </td>' . "\n";
print ' <td class="info2">'._('Auto create reverse (PTR) records for this subnet').'</td>' . "\n";
print '</tr>';

// autocreate forward records
$hidden = @$subnet_old_details['DNSrecursive'] ? "" : "hidden";
$checked = @$subnet_old_details['DNSforward'] ? "checked": "";
print "<tr id='dnsForwardSwitch' class='$hidden'>" . "\n";
print ' <td>'._('Autocreate forward records').'</td>' . "\n";
print ' <td>' . "\n";
print ' <input type="checkbox" name="DNSforward" class="input-switch" value="1" '.$checked.'>'. "\n";
print ' </td>' . "\n";
print ' <td class="info2">'._('Auto create forward (A) records for this subnet').'</td>' . "\n";
print '</tr>';

// show records
$checked = @$subnet_old_details['DNSrecords']==1 ? "checked": "";
$checked = @$subnet_old_details['DNSrecords'] ? "checked": "";
print '<tr>' . "\n";
print ' <td>'._('Show DNS records').'</td>' . "\n";
print ' <td>' . "\n";
Expand Down
6 changes: 3 additions & 3 deletions app/subnets/subnet-details/subnet-details.php
Expand Up @@ -475,13 +475,13 @@
print "</tr>";

print "<tr>";
print " <th>"._('Autocreate reverse records')."</th>";
if($subnet['DNSrecursive'] == 1) { print " <td><span class='badge badge1 badge5 alert-success'>"._('enabled')."</span> $btns $zone</td>"; } # yes
print " <th>"._('Autocreate ' . ($subnet['DNSforward'] ? 'DNS' : 'reverse') .' records')."</th>";
if($subnet['DNSrecursive']) { print " <td><span class='badge badge1 badge5 alert-success'>"._('enabled')."</span> $btns $zone</td>"; } # yes
else { print " <td><span class='badge badge1 badge5'>"._('disabled')."</span></td>";} # no
print "</tr>";
print "<tr>";
print " <th>"._('Show DNS records')."</th>";
if($subnet['DNSrecords'] == 1) { print " <td><span class='badge badge1 badge5 alert-success'>"._('enabled')."</span></td>"; } # yes
if($subnet['DNSrecords']) { print " <td><span class='badge badge1 badge5 alert-success'>"._('enabled')."</span></td>"; } # yes
else { print " <td><span class='badge badge1 badge5'>"._('disabled')."</span></td>";} # no
print "</tr>";
}
Expand Down
1 change: 1 addition & 0 deletions db/SCHEMA.sql
Expand Up @@ -275,6 +275,7 @@ CREATE TABLE `subnets` (
`discoverSubnet` BOOL NOT NULL DEFAULT '0',
`resolveDNS` BOOL NOT NULL DEFAULT '0',
`DNSrecursive` BOOL NOT NULL DEFAULT '0',
`DNSforward` BOOL NOT NULL DEFAULT '0',
`DNSrecords` BOOL NOT NULL DEFAULT '0',
`nameserverId` INT(11) NULL DEFAULT '0',
`scanAgent` INT(11) DEFAULT NULL,
Expand Down
22 changes: 15 additions & 7 deletions functions/classes/class.Addresses.php
Expand Up @@ -895,7 +895,7 @@ public function ptr_modify ($action, $address, $print_error = true) {
// to object
$address = (object) $address;
# execute based on action
if($action=="add") { return $this->ptr_add ($address, $print_error); } //create new PTR
if($action=="add") { return $this->ptr_add ($address, $print_error, null, $subnet->DNSforward); } //create new PTR
elseif($action=="edit") { return $this->ptr_edit ($address, $print_error); } //modify existing PTR
elseif($action=="delete") { return $this->ptr_delete ($address, $print_error); } //delete PTR
else { return $this->Result->show("danger", _("Invalid PDNS action"), true); }
Expand Down Expand Up @@ -971,7 +971,7 @@ private function pdns_fetch_domain ($subnet_id) {
* @param mixed $id (default: NULL)
* @return void
*/
public function ptr_add ($address, $print_error = true, $id = null) {
public function ptr_add ($address, $print_error = true, $id = null, $add_forward_record = false) {
// decode values
$values = json_decode($this->settings->powerDNS);

Expand All @@ -987,15 +987,23 @@ public function ptr_add ($address, $print_error = true, $id = null) {
$domain = $this->pdns_fetch_domain ($address->subnetId);

// formulate new record
$record = $this->PowerDNS->formulate_new_record ($domain->id, $this->PowerDNS->get_ip_ptr_name ($this->transform_address ($address->ip_addr, "dotted")), "PTR", $address->hostname, $values->ttl);
$ptr_record = $this->PowerDNS->formulate_new_record ($domain->id, $this->PowerDNS->get_ip_ptr_name ($this->transform_address ($address->ip_addr, "dotted")), "PTR", $address->hostname, $values->ttl);
// insert record
$this->PowerDNS->add_domain_record ($record, false);
$this->PowerDNS->add_domain_record ($ptr_record, false);
// link to address
$id = $id===null ? $this->lastId : $id;
$this->ptr_link ($id, $this->PowerDNS->lastId);

// formulate and insert backward_record if specified
if ($add_forward_record) {
$A_record = $this->PowerDNS->formulate_new_record ($domain->id, $address->hostname, "A", $this->transform_address ($address->ip_addr, "dotted"), $values->ttl);
$this->PowerDNS->add_domain_record ($A_record, false);
}

// ok
if ($print_error && php_sapi_name()!="cli")
$this->Result->show("success", "PTR record created", false);
if ($print_error && php_sapi_name()!="cli") {
$this->Result->show("success", "PTR record created", false);
}

return true;
}
Expand All @@ -1021,7 +1029,7 @@ public function ptr_edit ($address, $print_error = true) {
// fake lastid
$this->lastId = $address->id;
// new ptr record
$this->ptr_add ($address, true);
$this->ptr_add ($address, true, null, $subnet->DNSforward);
}
// update PTR
else {
Expand Down
5 changes: 4 additions & 1 deletion functions/classes/class.Log.php
Expand Up @@ -190,6 +190,7 @@ class Logging extends Common_functions {
"discoverSubnet" => "Discover new hosts for this subnet",
"allowRequests" => "Allow IP requests for subnet",
"DNSrecursive" => "Create recursive PowerDNS records",
"DNSforward" => "Create forward PowerDNS records",
"DNSrecords" => "Show PowerDNS records",
"nameserverId" => "Nameserver",
"scanAgent" => "Scan agent index",
Expand Down Expand Up @@ -1074,6 +1075,7 @@ private function changelog_unset_unneeded_values () {
$this->object_new['pingSubnet'],
$this->object_new['discoverSubnet'],
$this->object_new['DNSrecursive'],
$this->object_new['DNSforward'],
$this->object_new['DNSrecords'],
$this->object_new['nameserverId'],
$this->object_new['scanAgent'],
Expand All @@ -1093,6 +1095,7 @@ private function changelog_unset_unneeded_values () {
$this->object_old['pingSubnet'],
$this->object_old['discoverSubnet'],
$this->object_old['DNSrecursive'],
$this->object_old['DNSforward'],
$this->object_old['DNSrecords'],
$this->object_old['nameserverId'],
$this->object_old['scanAgent'],
Expand Down Expand Up @@ -1420,7 +1423,7 @@ private function changelog_make_booleans ($k, $v) {
$keys = array();
// list of keys to be changed per object
$keys['section'] = array("strictMode", "showVLAN", "showVRF");
$keys['subnet'] = array("allowRequests", "showName", "pingSubnet", "discoverSubnet", "DNSrecursive", "DNSrecords", "isFull");
$keys['subnet'] = array("allowRequests", "showName", "pingSubnet", "discoverSubnet", "DNSrecursive", "DNSforward", "DNSrecords", "isFull");
$keys['ip_addr'] = array("is_gateway", "excludePing", "PTRignore");

// check
Expand Down