Skip to content

Commit

Permalink
NAT enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
phpipam committed Apr 21, 2019
1 parent 2d0cbd5 commit 7aab047
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 145 deletions.
1 change: 1 addition & 0 deletions app/admin/custom-fields/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"circuitProviders" => "Circuit providers",
"circuits" => "Circuits",
"customers" => "Customers",
"nat" => "NAT",
"routing_bgp" => "BGP Routing",
//"routing_ospf" => "OSPF Routing"
);
Expand Down
37 changes: 29 additions & 8 deletions app/admin/nat/edit-result.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
$Database = new Database_PDO;
$User = new User ($Database);
$Admin = new Admin ($Database, false);
$Tools = new Tools ($Database);
$Result = new Result ();

# verify that user is logged in
Expand All @@ -22,6 +23,9 @@
$User->check_module_permissions ("nat", 3, true, false);
}

# fetch custom fields
$custom = $Tools->fetch_custom_fields('nat');

# strip input tags
$_POST = $Admin->strip_input_tags($_POST);

Expand All @@ -45,19 +49,36 @@

// set values
$values = array(
"id" =>@$_POST['id'],
"name" =>$_POST['name'],
"type" =>$_POST['type'],
"src_port" =>$_POST['src_port'],
"dst_port" =>$_POST['dst_port'],
"device" =>$_POST['device'],
"description" =>$_POST['description']
);
"id" => @$_POST['id'],
"name" => $_POST['name'],
"type" => $_POST['type'],
"src_port" => $_POST['src_port'],
"dst_port" => $_POST['dst_port'],
"device" => $_POST['device'],
"description" => $_POST['description'],
"policy" => "No",
"policy_dst" => ""
);

if ($User->get_module_permissions ("devices")<1) {
unset ($values['device']);
}

// policy NAT override
if($_POST['action']=="edit" && $_POST['policy']=="Yes" && strlen($_POST['policy_dst'])>0) {
$values['policy'] = $_POST['policy'];
$values['policy_dst'] = $Tools->strip_input_tags($_POST['policy_dst']);
}

// append custom
if(sizeof($custom) > 0) {
foreach($custom as $myField) {
# replace possible ___ back to spaces!
$myField['nameTest'] = str_replace(" ", "___", $myField['name']);
if(isset($_POST[$myField['nameTest']])) { $values[$myField['name']] = @$_POST[$myField['nameTest']];}
}
}

# execute update
if(!$Admin->object_modify ("nat", $_POST['action'], "id", $values)) { $Result->show("danger", _("NAT $_POST[action] failed"), false); }
else { $Result->show("success", _("NAT $_POST[action] successful"), false); }
Expand Down
71 changes: 65 additions & 6 deletions app/admin/nat/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
# disable edit on delete
$readonly = $_POST['action']=="delete" ? "readonly" : "";
$link = $readonly ? false : true;

# fetch custom fields
$custom = $Tools->fetch_custom_fields('nat');
?>


Expand Down Expand Up @@ -113,8 +116,43 @@
</tr>
<?php } ?>

<tr>
<th><?php print _('Description'); ?></th>
<td colspan="2">
<textarea class="form-control input-sm" name="description" placeholder='<?php print _('Port'); ?>' <?php print $readonly; ?>><?php print $nat->description; ?></textarea>
</td>
</tr>

<!-- Source -->
<?php if($_POST['action']!=="add") { ?>

<!-- Policy nat -->
<tr class='port'>
<th><?php print _('Policy NAT'); ?></th>
<td>
<select name="policy" class="form-control input-sm input-w-auto" <?php print $readonly; ?>>
<?php
foreach (["No", "Yes"] as $d) {
$selected = $nat->policy==$d ? "selected" : "";
print "<option value='$d' $selected>$d</option>";
}
?>
</td>
<td>
<span class="text-muted"><?php print _("Use destination policy NAT"); ?></span>
</td>
</tr>

<tr class='port'>
<th><?php print _('Destination address'); ?></th>
<td>
<input type="text" class="form-control input-sm" name="policy_dst" value="<?php print $nat->policy_dst; ?>" placeholder='<?php print _('IP'); ?>' <?php print $readonly; ?>>
</td>
<td>
<span class="text-muted"><?php print _("Destination address for policy NAT"); ?></span>
</td>
</tr>

<tr>
<td colspan="3"><hr></td>
</tr>
Expand Down Expand Up @@ -210,14 +248,35 @@
<span class="text-muted"><?php print _("Destination port"); ?></span>
</td>
</tr>

<?php } ?>

<tr>
<th><?php print _('Description'); ?></th>
<td colspan="2">
<textarea class="form-control input-sm" name="description" placeholder='<?php print _('Port'); ?>' <?php print $readonly; ?>><?php print $nat->description; ?></textarea>
</td>
</tr>
<!-- Custom -->
<?php
if(sizeof($custom) > 0) {

print '<tr>';
print ' <td colspan="3"><hr></td>';
print '</tr>';

# count datepickers
$timepicker_index = 0;

# all my fields
foreach($custom as $field) {
// create input > result is array (required, input(html), timepicker_index)
$custom_input = $Tools->create_custom_field_input ($field, $nat, $_POST['action'], $timepicker_index);
// add datepicker index
$timepicker_index = $timepicker_index + $custom_input['timepicker_index'];
// print
print "<tr>";
print " <th>".ucwords($Tools->print_custom_field_name ($field['name']))." ".$custom_input['required']."</th>";
print " <td>".$custom_input['field']."</td>";
print " <td><span class='muted'>".$field['Comment']."</span></td>";
print "</tr>";
}
}
?>

</tbody>

Expand Down
165 changes: 165 additions & 0 deletions app/tools/nat/all_nats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

/**
* Script to print nats
***************************/

# verify that user is logged in
$User->check_user_session();
?>

<h4><?php print _('NAT translations'); ?></h4>
<hr>


<?php if($User->settings->enableNAT=="1" && $User->get_module_permissions ("nat")==3) { ?>
<div class="btn-group">
<a href="" class='btn btn-sm btn-default open_popup' data-script='app/admin/nat/edit.php' data-class='700' data-action='add' data-id='' style='margin-bottom:10px;'><i class='fa fa-plus'></i> <?php print _('Add nat'); ?></a>
<a class='btn btn-sm btn-default open_popup' data-script='app/admin/nat/cleanup.php' data-class='700'><i class="fa fa-legal"></i> <?php print _('Cleanup'); ?></a>
</div>
<br>
<?php } ?>

<?php
# check that nat support isenabled
if ($User->settings->enableNAT!="1") {
$Result->show("danger", _("NAT module disabled."), false);
}
# no access
elseif ($User->check_module_permissions ("nat", 1, false, false)===false) {
$Result->show("danger", _("You do not have permissions to access this module"), false);
}
else {
# fetch all nats
$all_nats = $Tools->fetch_all_objects("nat", "name");

// check if we have any policy nat !
$policy_nat_found = false;
foreach ($all_nats as $n) {
if ($n->policy=="Yes") {
$policy_nat_found = true;
break;
}
}

// table
print "<table class='table sorted table-striped table-top table-td-top' data-cookie-id-table='nat_table'>";
// headers
print "<thead>";
print "<tr>";
print " <th>"._('Name')."</th>";
print " <th>"._('Type')."</th>";
if($policy_nat_found)
print " <th>"._('Policy IP')."</th>";
print " <th>"._('Translation')."</th>";
print " <th></th>";
print " <th></th>";
print " <th>"._('Device')."</th>";
print " <th>"._('Src Port')."</th>";
print " <th>"._('Dst Port')."</th>";
print " <th>"._('Description')."</th>";
if($User->get_module_permissions ("nat")>1)
print " <th style='width:80px'></th>";
print "</tr>";
print "</thead>";

print "<tbody>";

// init array
$nats_reordered = array("source"=>array(), "static"=>array(), "destination"=>array());

# rearrange based on type
if($all_nats !== false) {
foreach ($all_nats as $n) {
$nats_reordered[$n->type][] = $n;
}
}

# loop
foreach ($nats_reordered as $k=>$nats) {
# header
$colspan = $policy_nat_found ? 11 :10;
print "<tr>";
print " <td colspan='$colspan' class='th'><i class='fa fa-exchange'></i> "._(ucwords($k)." NAT")."</td>";
print "</tr>";

# if none than print
if(sizeof($nats)==0) {
print "<tr>";
print " <td colspan='$colspan'>".$Result->show("info","No $k NAT configured", false, false, true)."</td>";
print "</tr>";
}
else {
foreach ($nats as $n) {
// translate json to array, links etc
$sources = $Tools->translate_nat_objects_for_display ($n->src);
$destinations = $Tools->translate_nat_objects_for_display ($n->dst);

// no src/dst
if ($sources===false)
$sources = array("<span class='badge badge1 badge5 alert-danger'>"._("None")."</span>");
if ($destinations===false)
$destinations = array("<span class='badge badge1 badge5 alert-danger'>"._("None")."</span>");

// device
if (strlen($n->device)) {
if($n->device !== 0) {
$device = $Tools->fetch_object ("devices", "id", $n->device);
$n->device = $device===false ? "/" : "<a href='".create_link("tools", "devices", $device->id)."'>$device->hostname</a>";
}
}
else {
$n->device = "/";
}

// icon
$icon = $n->type=="static" ? "fa-arrows-h" : "fa-long-arrow-right";

// append policy
if($n->policy=="Yes") {
$n->type .= " - Policy";
$policy_dst = $n->policy_dst;
}
else {
$policy_dst = "/";
}

// description
$n->description = str_replace("\n", "<br>", $n->description);

// port
if(strlen($n->src_port)==0) $n->src_port = "/";
if(strlen($n->dst_port)==0) $n->dst_port = "/";

// print
print "<tr>";
print " <td><strong><a href='".create_link($_GET['page'], "nat", $n->id)."'>$n->name</a></strong></td>";
print " <td><span class='badge badge1 badge5'>".ucwords($n->type)."</span></td>";
if($policy_nat_found)
print " <td>$policy_dst</td>";
print " <td>".implode("<br>", $sources)."</td>";
print " <td style='width:10px;'><i class='fa $icon'></i></td>";
print " <td>".implode("<br>", $destinations)."</td>";
print " <td>$n->device</td>";
print " <td>$n->src_port</td>";
print " <td>$n->dst_port</td>";
print " <td><span class='text-muted'>$n->description</span></td>";
// actions
if($User->get_module_permissions ("nat")>1) {
print " <td class='actions'>";
$links = [];
$links[] = ["type"=>"header", "text"=>"Manage NAT"];
$links[] = ["type"=>"link", "text"=>"Edit NAT", "href"=>"", "class"=>"open_popup", "dataparams"=>" data-script='app/admin/nat/edit.php' data-class='700' data-action='edit' data-id='$n->id'", "icon"=>"pencil"];
$links[] = ["type"=>"link", "text"=>"Delete NAT", "href"=>"", "class"=>"open_popup", "dataparams"=>" data-script='app/admin/nat/edit.php' data-class='700' data-action='delete' data-id='$n->id'", "icon"=>"times"];
// print links
print $User->print_actions($User->user->compress_actions, $links);
print " </td>";
}

print "</tr>";
}
}
}
print "</tbody>";
print "</table>";
}
Loading

0 comments on commit 7aab047

Please sign in to comment.