2022-04-21 03:36:46 +02:00
|
|
|
# $OpenBSD: forward-control.sh,v 1.11 2022/04/21 01:36:46 dtucker Exp $
|
2012-12-03 00:13:39 +01:00
|
|
|
# Placed in the Public Domain.
|
|
|
|
|
|
|
|
tid="sshd control of local and remote forwarding"
|
|
|
|
|
|
|
|
LFWD_PORT=3320
|
|
|
|
RFWD_PORT=3321
|
|
|
|
CTL=$OBJ/ctl-sock
|
2022-04-20 07:24:13 +02:00
|
|
|
WAIT_SECONDS=20
|
2012-12-03 00:13:39 +01:00
|
|
|
|
|
|
|
wait_for_process_to_exit() {
|
|
|
|
_pid=$1
|
|
|
|
_n=0
|
|
|
|
while kill -0 $_pid 2>/dev/null ; do
|
|
|
|
test $_n -eq 1 && trace "waiting for $_pid to exit"
|
|
|
|
_n=`expr $_n + 1`
|
2022-04-20 07:24:13 +02:00
|
|
|
test $_n -ge $WAIT_SECONDS && return 1
|
2012-12-03 00:13:39 +01:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-04-21 03:36:46 +02:00
|
|
|
mux_cmd() {
|
|
|
|
${SSH} -F $OBJ/ssh_proxy -S $CTL -O $1 host 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
controlmaster_pid() {
|
|
|
|
mux_cmd check | cut -f2 -d= | cut -f1 -d')'
|
|
|
|
}
|
|
|
|
|
2017-05-01 01:34:55 +02:00
|
|
|
# usage: check_lfwd Y|N message
|
2012-12-03 00:13:39 +01:00
|
|
|
check_lfwd() {
|
2017-05-01 01:34:55 +02:00
|
|
|
_expected=$1
|
|
|
|
_message=$2
|
|
|
|
${SSH} -F $OBJ/ssh_proxy \
|
2012-12-03 00:13:39 +01:00
|
|
|
-L$LFWD_PORT:127.0.0.1:$PORT \
|
|
|
|
-o ExitOnForwardFailure=yes \
|
2022-04-20 15:25:55 +02:00
|
|
|
-MS $CTL -o ControlPersist=yes \
|
|
|
|
-f host true
|
2022-04-21 03:36:46 +02:00
|
|
|
mux_cmd check >/dev/null || fatal "check_lfwd ssh fail: $_message"
|
2012-12-03 00:13:39 +01:00
|
|
|
${SSH} -F $OBJ/ssh_config -p $LFWD_PORT \
|
2021-05-07 11:23:40 +02:00
|
|
|
-oConnectionAttempts=10 host true >/dev/null 2>&1
|
2012-12-03 00:13:39 +01:00
|
|
|
_result=$?
|
2022-04-21 03:36:46 +02:00
|
|
|
_sshpid=`controlmaster_pid`
|
|
|
|
mux_cmd exit >/dev/null
|
|
|
|
wait_for_process_to_exit $_sshpid
|
2012-12-03 00:13:39 +01:00
|
|
|
if test "x$_expected" = "xY" -a $_result -ne 0 ; then
|
|
|
|
fail "check_lfwd failed (expecting success): $_message"
|
|
|
|
elif test "x$_expected" = "xN" -a $_result -eq 0 ; then
|
|
|
|
fail "check_lfwd succeeded (expecting failure): $_message"
|
|
|
|
elif test "x$_expected" != "xY" -a "x$_expected" != "xN" ; then
|
|
|
|
fatal "check_lfwd invalid argument \"$_expected\""
|
|
|
|
else
|
|
|
|
verbose "check_lfwd done (expecting $_expected): $_message"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-05-01 01:34:55 +02:00
|
|
|
# usage: check_rfwd Y|N message
|
2012-12-03 00:13:39 +01:00
|
|
|
check_rfwd() {
|
2017-05-01 01:34:55 +02:00
|
|
|
_expected=$1
|
|
|
|
_message=$2
|
|
|
|
${SSH} -F $OBJ/ssh_proxy \
|
2018-06-06 20:25:33 +02:00
|
|
|
-R127.0.0.1:$RFWD_PORT:127.0.0.1:$PORT \
|
2012-12-03 00:13:39 +01:00
|
|
|
-o ExitOnForwardFailure=yes \
|
2022-04-20 15:25:55 +02:00
|
|
|
-MS $CTL -o ControlPersist=yes \
|
|
|
|
-f host true
|
2022-04-21 03:36:46 +02:00
|
|
|
mux_cmd check >/dev/null
|
2012-12-03 00:13:39 +01:00
|
|
|
_result=$?
|
2022-04-21 03:36:46 +02:00
|
|
|
_sshpid=`controlmaster_pid`
|
|
|
|
if test $_result -eq 0; then
|
2012-12-03 00:13:39 +01:00
|
|
|
${SSH} -F $OBJ/ssh_config -p $RFWD_PORT \
|
2021-05-07 11:23:40 +02:00
|
|
|
-oConnectionAttempts=10 host true >/dev/null 2>&1
|
2012-12-03 00:13:39 +01:00
|
|
|
_result=$?
|
2022-04-21 03:36:46 +02:00
|
|
|
mux_cmd exit >/dev/null
|
|
|
|
wait_for_process_to_exit $_sshpid
|
2012-12-03 00:13:39 +01:00
|
|
|
fi
|
|
|
|
if test "x$_expected" = "xY" -a $_result -ne 0 ; then
|
|
|
|
fail "check_rfwd failed (expecting success): $_message"
|
|
|
|
elif test "x$_expected" = "xN" -a $_result -eq 0 ; then
|
|
|
|
fail "check_rfwd succeeded (expecting failure): $_message"
|
|
|
|
elif test "x$_expected" != "xY" -a "x$_expected" != "xN" ; then
|
|
|
|
fatal "check_rfwd invalid argument \"$_expected\""
|
|
|
|
else
|
|
|
|
verbose "check_rfwd done (expecting $_expected): $_message"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
start_sshd
|
|
|
|
cp ${OBJ}/sshd_proxy ${OBJ}/sshd_proxy.bak
|
|
|
|
cp ${OBJ}/authorized_keys_${USER} ${OBJ}/authorized_keys_${USER}.bak
|
|
|
|
|
|
|
|
# Sanity check: ensure the default config allows forwarding
|
2017-05-01 01:34:55 +02:00
|
|
|
check_lfwd Y "default configuration"
|
|
|
|
check_rfwd Y "default configuration"
|
2012-12-03 00:13:39 +01:00
|
|
|
|
2018-06-06 20:25:33 +02:00
|
|
|
# Usage: lperm_tests yes|local|remote|no Y|N Y|N Y|N Y|N Y|N Y|N
|
|
|
|
lperm_tests() {
|
2012-12-03 00:13:39 +01:00
|
|
|
_tcpfwd=$1
|
|
|
|
_plain_lfwd=$2
|
|
|
|
_plain_rfwd=$3
|
|
|
|
_nopermit_lfwd=$4
|
|
|
|
_nopermit_rfwd=$5
|
|
|
|
_permit_lfwd=$6
|
|
|
|
_permit_rfwd=$7
|
2018-06-06 20:25:33 +02:00
|
|
|
_badfwd1=127.0.0.1:22
|
|
|
|
_badfwd2=127.0.0.2:22
|
2012-12-03 00:13:39 +01:00
|
|
|
_goodfwd=127.0.0.1:${PORT}
|
2017-05-01 01:34:55 +02:00
|
|
|
cp ${OBJ}/authorized_keys_${USER}.bak ${OBJ}/authorized_keys_${USER}
|
|
|
|
_prefix="AllowTcpForwarding=$_tcpfwd"
|
2018-06-06 20:25:33 +02:00
|
|
|
|
2017-05-01 01:34:55 +02:00
|
|
|
# No PermitOpen
|
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ) \
|
|
|
|
> ${OBJ}/sshd_proxy
|
|
|
|
check_lfwd $_plain_lfwd "$_prefix"
|
|
|
|
check_rfwd $_plain_rfwd "$_prefix"
|
2018-06-06 20:25:33 +02:00
|
|
|
|
2017-05-01 01:34:55 +02:00
|
|
|
# PermitOpen via sshd_config that doesn't match
|
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ;
|
2018-06-06 20:25:33 +02:00
|
|
|
echo "PermitOpen $_badfwd1 $_badfwd2" ) \
|
2017-05-01 01:34:55 +02:00
|
|
|
> ${OBJ}/sshd_proxy
|
|
|
|
check_lfwd $_nopermit_lfwd "$_prefix, !PermitOpen"
|
|
|
|
check_rfwd $_nopermit_rfwd "$_prefix, !PermitOpen"
|
|
|
|
# PermitOpen via sshd_config that does match
|
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ;
|
2018-06-06 20:25:33 +02:00
|
|
|
echo "PermitOpen $_badfwd1 $_goodfwd $_badfwd2" ) \
|
2017-05-01 01:34:55 +02:00
|
|
|
> ${OBJ}/sshd_proxy
|
2018-06-06 20:25:33 +02:00
|
|
|
check_lfwd $_plain_lfwd "$_prefix, PermitOpen"
|
|
|
|
check_rfwd $_plain_rfwd "$_prefix, PermitOpen"
|
|
|
|
|
|
|
|
# permitopen keys option.
|
2017-05-01 01:34:55 +02:00
|
|
|
# NB. permitopen via authorized_keys should have same
|
|
|
|
# success/fail as via sshd_config
|
|
|
|
# permitopen via authorized_keys that doesn't match
|
2018-06-06 20:25:33 +02:00
|
|
|
sed "s/^/permitopen=\"$_badfwd1\",permitopen=\"$_badfwd2\" /" \
|
2017-05-01 01:34:55 +02:00
|
|
|
< ${OBJ}/authorized_keys_${USER}.bak \
|
|
|
|
> ${OBJ}/authorized_keys_${USER} || fatal "sed 1 fail"
|
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ) \
|
|
|
|
> ${OBJ}/sshd_proxy
|
|
|
|
check_lfwd $_nopermit_lfwd "$_prefix, !permitopen"
|
|
|
|
check_rfwd $_nopermit_rfwd "$_prefix, !permitopen"
|
|
|
|
# permitopen via authorized_keys that does match
|
2018-06-06 20:25:33 +02:00
|
|
|
sed "s/^/permitopen=\"$_badfwd1\",permitopen=\"$_goodfwd\" /" \
|
2017-05-01 01:34:55 +02:00
|
|
|
< ${OBJ}/authorized_keys_${USER}.bak \
|
|
|
|
> ${OBJ}/authorized_keys_${USER} || fatal "sed 2 fail"
|
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ) \
|
|
|
|
> ${OBJ}/sshd_proxy
|
|
|
|
check_lfwd $_permit_lfwd "$_prefix, permitopen"
|
|
|
|
check_rfwd $_permit_rfwd "$_prefix, permitopen"
|
2018-06-06 20:25:33 +02:00
|
|
|
|
2018-03-02 03:51:55 +01:00
|
|
|
# Check port-forwarding flags in authorized_keys.
|
|
|
|
# These two should refuse all.
|
|
|
|
sed "s/^/no-port-forwarding /" \
|
|
|
|
< ${OBJ}/authorized_keys_${USER}.bak \
|
|
|
|
> ${OBJ}/authorized_keys_${USER} || fatal "sed 3 fail"
|
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ) \
|
|
|
|
> ${OBJ}/sshd_proxy
|
|
|
|
check_lfwd N "$_prefix, no-port-forwarding"
|
|
|
|
check_rfwd N "$_prefix, no-port-forwarding"
|
|
|
|
sed "s/^/restrict /" \
|
|
|
|
< ${OBJ}/authorized_keys_${USER}.bak \
|
|
|
|
> ${OBJ}/authorized_keys_${USER} || fatal "sed 4 fail"
|
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ) \
|
|
|
|
> ${OBJ}/sshd_proxy
|
|
|
|
check_lfwd N "$_prefix, restrict"
|
|
|
|
check_rfwd N "$_prefix, restrict"
|
|
|
|
# This should pass the same cases as _nopermit*
|
|
|
|
sed "s/^/restrict,port-forwarding /" \
|
|
|
|
< ${OBJ}/authorized_keys_${USER}.bak \
|
|
|
|
> ${OBJ}/authorized_keys_${USER} || fatal "sed 5 fail"
|
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ) \
|
|
|
|
> ${OBJ}/sshd_proxy
|
|
|
|
check_lfwd $_plain_lfwd "$_prefix, restrict,port-forwarding"
|
|
|
|
check_rfwd $_plain_rfwd "$_prefix, restrict,port-forwarding"
|
2012-12-03 00:13:39 +01:00
|
|
|
}
|
|
|
|
|
2018-06-06 20:25:33 +02:00
|
|
|
# permit-open none mismatch match
|
|
|
|
# AllowTcpForwarding local remote local remote local remote
|
|
|
|
lperm_tests yes Y Y N Y Y Y
|
|
|
|
lperm_tests local Y N N N Y N
|
|
|
|
lperm_tests remote N Y N Y N Y
|
|
|
|
lperm_tests no N N N N N N
|
|
|
|
|
|
|
|
# Usage: rperm_tests yes|local|remote|no Y|N Y|N Y|N Y|N Y|N Y|N
|
|
|
|
rperm_tests() {
|
|
|
|
_tcpfwd=$1
|
|
|
|
_plain_lfwd=$2
|
|
|
|
_plain_rfwd=$3
|
|
|
|
_nopermit_lfwd=$4
|
|
|
|
_nopermit_rfwd=$5
|
|
|
|
_permit_lfwd=$6
|
|
|
|
_permit_rfwd=$7
|
|
|
|
_badfwd1=127.0.0.1:22
|
|
|
|
_badfwd2=127.0.0.2:${RFWD_PORT}
|
|
|
|
_goodfwd=127.0.0.1:${RFWD_PORT}
|
|
|
|
cp ${OBJ}/authorized_keys_${USER}.bak ${OBJ}/authorized_keys_${USER}
|
|
|
|
_prefix="AllowTcpForwarding=$_tcpfwd"
|
|
|
|
|
2018-06-07 16:29:43 +02:00
|
|
|
# PermitListen via sshd_config that doesn't match
|
2018-06-06 20:25:33 +02:00
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ;
|
2018-06-07 16:29:43 +02:00
|
|
|
echo "PermitListen $_badfwd1 $_badfwd2" ) \
|
2018-06-06 20:25:33 +02:00
|
|
|
> ${OBJ}/sshd_proxy
|
2018-06-07 16:29:43 +02:00
|
|
|
check_lfwd $_nopermit_lfwd "$_prefix, !PermitListen"
|
|
|
|
check_rfwd $_nopermit_rfwd "$_prefix, !PermitListen"
|
|
|
|
# PermitListen via sshd_config that does match
|
2018-06-06 20:25:33 +02:00
|
|
|
( cat ${OBJ}/sshd_proxy.bak ;
|
|
|
|
echo "AllowTcpForwarding $_tcpfwd" ;
|
2018-06-07 16:29:43 +02:00
|
|
|
echo "PermitListen $_badfwd1 $_goodfwd $_badfwd2" ) \
|
2018-06-06 20:25:33 +02:00
|
|
|
> ${OBJ}/sshd_proxy
|
2018-06-07 16:29:43 +02:00
|
|
|
check_lfwd $_plain_lfwd "$_prefix, PermitListen"
|
|
|
|
check_rfwd $_plain_rfwd "$_prefix, PermitListen"
|
2018-06-06 20:25:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# permit-remote-open none mismatch match
|
|
|
|
# AllowTcpForwarding local remote local remote local remote
|
|
|
|
rperm_tests yes Y Y Y N Y Y
|
|
|
|
rperm_tests local Y N Y N Y N
|
|
|
|
rperm_tests remote N Y N N N Y
|
|
|
|
rperm_tests no N N N N N N
|
|
|
|
|