#!/bin/sh

# Copyright (c) 2014-2018 Franco Fichtner <franco@opnsense.org>
# Copyright (c) 2004-2011 Scott Ullrich <sullrich@gmail.com>
# Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>
# All rights reserved.

# make sure the user can't kill us
trap : 2
trap : 3

CMD_BANNER="/usr/local/opnsense/scripts/shell/banner.php"
CMD_DEFAULTS="/usr/local/opnsense/scripts/shell/defaults.php"
CMD_FIRMWARE="/usr/local/opnsense/scripts/shell/firmware.sh"
CMD_HALT="/usr/local/opnsense/scripts/shell/halt.php"
CMD_PASSWORD="/usr/local/opnsense/scripts/shell/password.php"
CMD_PFLOG="/usr/sbin/tcpdump -s 256 -v -S -l -n -e -ttt -i pflog0"
CMD_PFTOP="/usr/local/sbin/pftop"
CMD_PING="/usr/local/opnsense/scripts/shell/ping.php"
CMD_REBOOT="/usr/local/opnsense/scripts/shell/reboot.php"
CMD_RELOAD="/usr/local/etc/rc.reload_all"
CMD_RESTORE="/usr/local/opnsense/scripts/shell/restore.sh"
CMD_SETADDR="/usr/local/opnsense/scripts/shell/setaddr.php"
CMD_SETPORTS="/usr/local/opnsense/scripts/shell/setports.php"
CMD_SHELL="/bin/csh"

# rc.d may call this while being root using `su -m user -c ...'
# and it has arguments to pass through to the shell.  It creates
# a problem for us as su(1) assumes a full root shell and has no
# way to override it because the target user most likely does
# not have a shell set.  Let us therefore parse the command line
# arguments to find `-c' to be able to pass this on to a shell that
# will handle it properly.
while getopts c: OPT; do
	case ${OPT} in
	c)
		exec ${CMD_SHELL} -${OPT} "${OPTARG}"
		;;
	*)
		echo "Usage: man ${0##*/}" >&2
		exit 1
		;;
	esac
done

shift $((OPTIND - 1))

# Beyond this point the console menu yields no value to scripting,
# so we can check for root to avoid permission errors later on.
if [ "$(id -u)" != "0" ]; then
	echo "Must be root."
	exit 1
fi

# The invoke using "opnsense-shell cmd" will launch the
# utility without presenting and looping the menu.
case "${1}" in
banner)
	shift
	exec ${CMD_BANNER} "${@}"
	;;
defaults)
	shift
	exec ${CMD_DEFAULTS} "${@}"
	;;
firmware)
	shift
	exec ${CMD_FIRMWARE} "${@}"
	;;
halt)
	shift
	exec ${CMD_HALT} "${@}"
	;;
password)
	shift
	exec ${CMD_PASSWORD} "${@}"
	;;
ping)
	shift
	exec ${CMD_PING} "${@}"
	;;
pflog)
	shift
	exec ${CMD_PFLOG} "${@}"
	;;
pftop)
	shift
	exec ${CMD_PFTOP} "${@}"
	;;
reboot)
	shift
	exec ${CMD_REBOOT} "${@}"
	;;
reload)
	shift
	exec ${CMD_RELOAD} "${@}"
	;;
restore)
	shift
	exec ${CMD_RESTORE} "${@}"
	;;
shell)
	shift
	exec ${CMD_SHELL} "${@}"
	;;
esac

# endless loop
while : ; do

${CMD_BANNER}

# We `set -e' to force exit if we encounter an error.
# This is mainly useful in case we lose our tty (happens when
# an ssh connection breaks, for example), in which case our stdout
# is closed and the `echo' commands in the while loop will silently fail.
# Failure to exit at that moment would lead to an infinite loop.
set -e

echo
echo "  0) Logout                              7) Ping host"
echo "  1) Assign interfaces                   8) Shell"
echo "  2) Set interface IP address            9) pfTop"
echo "  3) Reset the root password            10) Firewall log"
echo "  4) Reset to factory defaults          11) Reload all services"
echo "  5) Power off system                   12) Update from console"
echo "  6) Reboot system                      13) Restore a backup"
echo
read -p "Enter an option: " OPCODE
echo

# The scripts we'll call below may return non-zero, don't exit if they do
set +e

# see what the user has chosen
case ${OPCODE} in
0|exit|logout|quit)
	exit
	;;
1)
	${CMD_SETPORTS}
	;;
2)
	${CMD_SETADDR}
	;;
3)
	${CMD_PASSWORD}
	;;
4)
	${CMD_DEFAULTS}
	;;
5)
	${CMD_HALT}
	;;
6)
	${CMD_REBOOT}
	;;
7)
	${CMD_PING}
	;;
8)
	${CMD_SHELL}
	;;
9)
	${CMD_PFTOP}
	;;
10)
	${CMD_PFLOG}
	;;
11)
	${CMD_RELOAD}
	;;
12)
	${CMD_FIRMWARE}
	;;
13)
	${CMD_RESTORE}
	;;
*)
	;;
esac

done
