2019-01-04 16:38:20 +01:00
|
|
|
#!/bin/sh
|
2019-01-07 14:47:05 +01:00
|
|
|
#
|
|
|
|
# Set or get ssh port of <jailname>
|
|
|
|
# Usage: port <jailname> [<port>|auto]
|
|
|
|
#
|
2019-01-04 16:38:20 +01:00
|
|
|
|
2020-04-02 13:44:13 +02:00
|
|
|
# shellcheck source=./includes
|
2020-04-02 00:31:57 +02:00
|
|
|
LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
|
2019-01-04 16:38:20 +01:00
|
|
|
|
2020-04-02 13:44:13 +02:00
|
|
|
jail_name="${1:?}"
|
2019-01-04 16:38:20 +01:00
|
|
|
port="${2:-}"
|
2020-04-02 01:07:12 +02:00
|
|
|
|
|
|
|
if [ ! -n "${jail_name}" ]; then
|
2019-01-07 14:47:05 +01:00
|
|
|
"${LIBDIR}/bkctld-help" && exit 1
|
|
|
|
fi
|
2020-04-02 01:07:12 +02:00
|
|
|
jail_path=$(jail_path "${jail_name}")
|
|
|
|
|
|
|
|
test -d "${jail_path}" || error "${jail_name}: jail is missing."
|
2019-01-04 16:38:20 +01:00
|
|
|
|
2020-04-08 00:32:15 +02:00
|
|
|
jail_sshd_config="${jail_path}/${SSHD_CONFIG}"
|
|
|
|
|
2019-01-04 16:38:20 +01:00
|
|
|
if [ -z "${port}" ]; then
|
2020-04-08 00:32:15 +02:00
|
|
|
grep -E "Port [0-9]+" "${jail_sshd_config}"|grep -oE "[0-9]+"
|
2019-01-04 16:38:20 +01:00
|
|
|
else
|
|
|
|
if [ "${port}" = "auto" ]; then
|
|
|
|
port=$(grep -h Port "${JAILDIR}"/*/"${SSHD_CONFIG}" 2>/dev/null | grep -Eo "[0-9]+" | sort -n | tail -1)
|
|
|
|
port=$((port+1))
|
|
|
|
[ "${port}" -le 1 ] && port=2222
|
|
|
|
fi
|
2020-04-08 00:32:15 +02:00
|
|
|
sed -i "s/^Port .*/Port ${port}/" "${jail_sshd_config}"
|
2020-04-02 01:07:12 +02:00
|
|
|
|
2020-04-19 10:05:20 +02:00
|
|
|
notice "${jail_name}: port has been updated to \`${port}'"
|
2020-04-02 01:07:12 +02:00
|
|
|
|
|
|
|
"${LIBDIR}/bkctld-reload" "${jail_name}"
|
|
|
|
"${LIBDIR}/bkctld-firewall" "${jail_name}"
|
2019-01-04 16:38:20 +01:00
|
|
|
fi
|