#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# Rx3 NS Admin Board CGI
#
# Copyright (C) 2025-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; If not, see
# .
#
#-----------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------
# Includes
#-----------------------------------------------------------------------------------------------------------------------------------
: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/dns.bash"
. /etc/sysconfig/rx3-ns # To be removed?
#-----------------------------------------------------------------------------------------------------------------------------------
# Global Variables
#-----------------------------------------------------------------------------------------------------------------------------------
declare -g VERSION="1.2.0"
declare -g NAME="ns-admin_board.cgi"
declare -g DEBUG=""
#declare -g DEBUG="echo"
#declare -g DEBUG=":"
# No Log please
export LOG=""
declare -g CMD=""
declare -g FORMAT=""
declare -g IP=""
declare -g HOST=""
declare -g DOMAIN=""
declare -g REDIRECT=""
declare -g CMD_STATUS=""
declare -g REFRESH=""
declare -g TTL=""
declare -g STATUS=""
declare -g FILE_NAME=""
declare -g REMOTE_IP="${REMOTE_ADDR}"
declare -g HOST_INFO=""
#-----------------------------------------------------------------------------------------------------------------------------------
# Lookup Domain Owner
#-----------------------------------------------------------------------------------------------------------------------------------
nab_lookup_domain_owner()
{
local ldo_blk
local ldo_domain
local ldo_host
local ldo_owner
local OIFS
for ldo_blk in ${NS_LIST}
do
OIFS="${IFS}"
IFS=":"
set ${ldo_blk}
ldo_domain="$1"
ldo_host="$2"
ldo_owner="$3"
IFS="${OIFS}"
if [[ "${HOST}.${DOMAIN}" == "${ldo_host}.${ldo_domain}" ]]
then
echo "${ldo_owner}"
fi
done
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Header Print
#-----------------------------------------------------------------------------------------------------------------------------------
nab_header_print()
{
case "${FORMAT}"
in
"html")
echo "Content-type: text/html"
echo ""
echo ""
echo ""
echo "
"
echo " - cmd=address_get: Get the host IP address"
echo " + [format=html|txt]: Output request format (Default to html)"
echo " + host=<Host Name>: Host name to show"
echo " + domain=<Domain Name>: Domain name of the host name"
echo ""
echo " - cmd=address_set: Set the host IP address"
echo " + [format=html|txt]: Output request format (Default to html)"
echo " + host=<Host Name>: Host name to set"
echo " + domain=<Domain Name>: Domain name of the host name"
echo " + [ip=<IP Address>]: IP address to be set (Default to requestor address)"
echo " + [ttl=<TTL>]: TTL to be set (Default to 600)"
echo ""
echo " - Example: https://www.rx3.net/cgi-bin-private/ns-admin_board.cgi?cmd=address_get&host=vpn0&domain=vpn.rx3"
echo "
"
echo "
"
echo "
"
echo "
"
echo "
"
else
echo "Rx3 NS Service Usage:"
echo " - cmd=address_get: Get the host IP address"
echo " + [format=html|txt]: Output request format (Default to html)"
echo " + host=: Host name to show"
echo " + domain=: Domain name of the host name"
echo ""
echo " - cmd=address_set: Set the host IP address"
echo " + [format=html|txt]: Output request format (Default to html)"
echo " + host=: Host name to set"
echo " + domain=: Domain name of the host name"
echo " + [ip=]: IP address to be set (Default to requestor address)"
echo " + [ttl=]: TTL to be set (Default to 600)"
fi
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Result Print
#-----------------------------------------------------------------------------------------------------------------------------------
nab_result_print()
{
local message="$1"
if [[ "${FORMAT}" == "html" ]]
then
echo "
"
echo "
"
echo "
"
echo "
"
echo "
"
echo "${message}"
echo "
"
echo "
"
echo "
"
echo "
"
echo "
"
else
echo "${message}"
fi
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Main Board Print
#-----------------------------------------------------------------------------------------------------------------------------------
nab_main_board_print()
{
local blk
local domain
local host
local owner
local address
local ttl
local date
local class
local idx=0
local OIFS
if [[ "${FORMAT}" == "html" ]]
then
echo ""
echo "
"
else
echo "TABLE: RX3-NS_Status_Board"
echo "#;Domain;Host;Address;Owner;TTL;Date"
fi
for blk in ${NS_LIST}
do
OIFS="${IFS}"
IFS=":"
set ${blk}
domain="$1"
host="$2"
owner="$3"
IFS="${OIFS}"
address="$( dig -t A "${host}.${domain}." | grep "^${host}.${domain}" | awk '{print $5}')"
ttl="$( dig -t A "${host}.${domain}." | grep "^${host}.${domain}" | awk '{print $2}')"
date="$( dig -t TXT "${host}.${domain}." | grep "^${host}.${domain}" | awk '{print $5 " " $6}' | sed -e 's/\"//g')"
if [[ "${REMOTE_USER}" == "${owner}" ]]
then
class="default"
else
class="dark"
fi
if [[ "${FORMAT}" == "html" ]]
then
echo "
${idx}
${domain}
${host}
${address}
${owner}
${ttl}
${date}
"
else
echo "${domain};${host};${address};${owner};${ttl};${date}"
fi
idx=$(( idx + 1 ))
done
if [[ "${FORMAT}" == "html" ]]
then
echo "
"
echo "
"
echo " "
echo "
"
else
echo ""
fi
nab_usage_print
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Query String Parse
#-----------------------------------------------------------------------------------------------------------------------------------
nab_query_string_parse()
{
local var
local arg
local i
CMD=""
FORMAT=""
IP=""
HOST=""
DOMAIN=""
TTL=""
if [[ "${QUERY_STRING}" != "" ]]
then
local OIFS="${IFS}"
IFS="&"
set ${QUERY_STRING}
IFS="${OIFS}"
i=$#
while [[ "${i}" != "0" ]]
do
var="${1/=*/}"
arg="${1/*=/}"
case "${var}"
in
"cmd")
CMD="${arg}"
;;
"format")
FORMAT="${arg}"
;;
"host")
HOST="${arg}"
;;
"domain")
DOMAIN="${arg}"
;;
"ip")
IP="${arg}"
;;
"ttl")
TTL="${arg}"
;;
esac
shift
i=$(( i - 1 ))
done
fi
if [[ "${FORMAT}" == "" ]]
then
FORMAT="html"
fi
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Command Handler
#-----------------------------------------------------------------------------------------------------------------------------------
nab_command_handler()
{
local owner
local status
case "${CMD}"
in
"")
CMD_STATUS="OK"
REFRESH="yes"
nab_header_print
nab_main_board_print
nab_footer_print
;;
"address_set")
owner="$( nab_lookup_domain_owner)"
if [[ "${owner}" != "" ]]
then
if [[ "${REMOTE_USER}" == "${owner}" ]]
then
if [[ "${IP}" == "" ]]
then
IP="${REMOTE_IP}"
fi
if [[ "${TTL}" == "" ]]
then
TTL="600"
fi
dns_host_update "${HOST}" "${DOMAIN}" "${IP}" "${TTL}"
if [[ "$?" == "0" ]]
then
status="OK"
else
status="KO"
fi
else
status="KO - Not Authorized"
fi
else
status="KO - Domain not found"
fi
CMD_STATUS="${CMD}: ${status}"
REFRESH="no"
nab_header_print
nab_result_print "Set ${HOST}.${DOMAIN}. to ${IP} with TTL ${TTL}: ${status}"
nab_footer_print
;;
"address_get")
owner="$( nab_lookup_domain_owner)"
if [[ "${owner}" != "" ]]
then
HOST_INFO="$( dig -t ANY "${HOST}.${DOMAIN}.")"
if [[ "$?" == "0" ]]
then
status="OK"
else
status="KO"
fi
else
status="KO - Domain not found"
fi
CMD_STATUS="${CMD}: ${status}"
REFRESH="no"
nab_header_print
if [[ "${FORMAT}" == "html" ]]
then
nab_result_print "${HOST}.${DOMAIN}.: ${status}
${HOST_INFO}"
else
echo "${HOST}.${DOMAIN}.:"
echo ""
echo "${HOST_INFO}"
fi
nab_footer_print
;;
*)
CMD_STATUS="${CMD}: UNKNOWN_CMD"
nab_header_print
nab_footer_print
;;
esac
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------------------------------------------------------------------
nab_query_string_parse
dns_init
nab_command_handler
dns_deinit