IP Reg is a IPAM tool to keep track of assets, nodes (IP addresses, MAC addresses, DNS aliases) within different subnets, over different locations or even VLAN's. Written in PHP, used with a MySQL-database to have a unique insight in your local network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
ipreg/nat.php

186 lines
5.3 KiB

<?php
/*****************************************************************************
IP Reg, a PHP/MySQL IPAM tool
Copyright (C) 2007-2009 Wietse Warendorff (up to v0.5)
Copyright (C) 2011-2023 Thomas Hooge
SPDX-License-Identifier: GPL-3.0-or-later
*****************************************************************************/
include("includes.php");
if (isset($_REQUEST['id'])) {
$id = (int) $_REQUEST['id'] or $id = 0;
}
// ========== ACTIONS START ===================================================
switch ($submit = form_get_action()) {
case NULL: break;
case 'add': $action = ACT_ADD; break;
case 'view': $action = ACT_VIEW; break;
case 'edit': $action = ACT_EDIT; break;
case 'del': $action = ACT_DELETE; break;
case 'insert':
$node_id_ext = sanitize($_POST['node_id_ext']);
$node_id_int = sanitize($_POST['node_id_int']);
$nat_type = sanitize($_POST['nat_type']);
$sql = "INSERT INTO nat (nat_ext, nat_int, nat_type)
VALUE (?, ?, ?)";
$sth = $dbh->prepare($sql);
$sth->execute([$node_id_ext, $node_id_int, $nat_type]);
header_location("node.php?f=view&id=$node_id_ext");
break;
case 'delete':
$node_id_ext = sanitize($_POST['node_id_ext']);
$sth = $dbh->prepare("DELETE FROM nat WHERE nat_id=?");
$sth->execute([$id]);
// TODO
// header_location("node.php?f=view&id=" . $node_id_ext);
$action = ACT_DEFAULT;
break;
default:
$g_error->Add(submit_error($submit));
$valid = FALSE;
}
// ========== ACTIONS END =====================================================
include("header.php");
$sql = "SELECT n.nat_id AS id, n.nat_type, n.nat_ext, n.nat_int,
n.nat_ext_port AS port_ext, n.nat_int_port AS port_int,
n.nat_description AS description,
n1.node_ip AS node_ip_int, n2.node_ip AS node_ip_ext
FROM nat AS n
LEFT JOIN node AS n1 ON (n.nat_int=n1.node_id)
LEFT JOIN node AS n2 ON (n.nat_ext=n2.node_id)
ORDER BY INET_ATON(nat_ext)";
$sth = $dbh->query($sql);
$smarty->assign("nats", $sth->fetchAll());
$smarty->display("nat.tpl");
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
$node_id = sanitize($_GET['node_id']);
// node_ext
$sql = "SELECT node_ip AS node_ip_ext
FROM node
WHERE node_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$node_id]);
$node = $sth->fetch(PDO::FETCH_OBJ);
$smarty->assign("node_id_ext", $node_id);
$smarty->assign("node_ip_ext", $node->node_ip_ext);
// node_int
$sql = "SELECT
a.asset_name,
n.node_id AS node_id_int,
n.node_ip AS node_ip_int
FROM
asset AS a LEFT JOIN node AS n USING (asset_id)
WHERE
n.node_id NOT IN (
SELECT
nat_int
FROM
nat
WHERE
nat_ext=?
)
AND n.node_id!=?
ORDER BY
INET_ATON(n.node_ip)";
$sth = $dbh->prepare($sql);
$sth->execute([$node_id, $node_id]);
$nodes = $sth->fetchAll();
foreach ($nodes as $rec) {
$node_options[$rec['node_id_int']] = $rec['node_ip_int'] . '/' . $rec['asset_name'];
}
$smarty->assign("node_options", $node_options);
$nat_type_options[1] = $lang['lang_nat_type_1'];
$nat_type_options[2] = $lang['lang_nat_type_2'];
$nat_type_options[3] = $lang['lang_nat_type_3'];
$smarty->assign("nat_type_options", $nat_type_options);
$smarty->display("natadd.tpl");
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
$sql = "SELECT nat_id AS id, nat_type AS type, nat_ext, nat_int FROM nat WHERE nat_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("nat", $sth->fetch(PDO::FETCH_OBJ));
$smarty->display("natview.tpl");
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$node_id = sanitize($_GET['node_id']);
$sql = "SELECT node_id AS id, node_ip AS ip FROM node WHERE node.node_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$node_id]);
$smarty->assign("node", $sth->fetch(PDO::FETCH_OBJ));
$smarty->display("natedit.tpl");
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
$node_id = sanitize($_GET['node_id']);
// node_ext
$sth = $dbh->prepare("SELECT node_id AS id_ext, node_ip AS ip_ext FROM node WHERE node_id=?");
$sth->execute([$node_id]);
$smarty->assign("node", $sth->fetch(PDO::FETCH_OBJ));
// options
$sql = "SELECT x.nat_id, n.node_ip, a.asset_name
FROM nat AS x
LEFT JOIN node AS n ON (x.nat_int=n.node_id)
LEFT JOIN asset AS a USING (asset_id)
WHERE x.nat_ext=?
ORDER BY INET_ATON(n.node_ip)";
$sth = $dbh->prepare($sql);
$sth->execute([$node_id]);
$nats = $sth->fetchAll();
$options = array();
foreach ($nats as $rec) {
$options[$rec['nat_id']] = $rec['node_ip'] . '/' . $rec['asset_name'];
}
$smarty->assign("nat_options", $options);
$smarty->display("natdel.tpl");
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo "<p>Unknown function call: Please report to system development!</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
$smarty->display('footer.tpl');