2021-09-30 06:22:50 +02:00
|
|
|
# $OpenBSD: keys-command.sh,v 1.8 2021/09/30 04:22:50 dtucker Exp $
|
2012-12-03 00:12:13 +01:00
|
|
|
# Placed in the Public Domain.
|
|
|
|
|
|
|
|
tid="authorized keys from command"
|
|
|
|
|
2016-09-26 23:34:38 +02:00
|
|
|
if [ -z "$SUDO" -a ! -w /var/run ]; then
|
2021-09-30 06:22:50 +02:00
|
|
|
skip "need SUDO to create file in /var/run, test won't work without"
|
2012-12-03 00:12:13 +01:00
|
|
|
fi
|
|
|
|
|
2015-05-21 08:40:02 +02:00
|
|
|
rm -f $OBJ/keys-command-args
|
|
|
|
|
|
|
|
touch $OBJ/keys-command-args
|
|
|
|
chmod a+rw $OBJ/keys-command-args
|
|
|
|
|
2019-07-25 10:48:11 +02:00
|
|
|
expected_key_text=`awk '{ print $2 }' < $OBJ/ssh-ed25519.pub`
|
|
|
|
expected_key_fp=`$SSHKEYGEN -lf $OBJ/ssh-ed25519.pub | awk '{ print $2 }'`
|
2015-05-21 08:40:02 +02:00
|
|
|
|
2012-12-03 00:12:13 +01:00
|
|
|
# Establish a AuthorizedKeysCommand in /var/run where it will have
|
|
|
|
# acceptable directory permissions.
|
2018-11-22 09:48:32 +01:00
|
|
|
KEY_COMMAND="/var/run/keycommand_${LOGNAME}.$$"
|
|
|
|
trap "${SUDO} rm -f ${KEY_COMMAND}" 0
|
2015-05-21 08:40:02 +02:00
|
|
|
cat << _EOF | $SUDO sh -c "rm -f '$KEY_COMMAND' ; cat > '$KEY_COMMAND'"
|
2012-12-03 00:12:13 +01:00
|
|
|
#!/bin/sh
|
2015-05-21 08:40:02 +02:00
|
|
|
echo args: "\$@" >> $OBJ/keys-command-args
|
|
|
|
echo "$PATH" | grep -q mekmitasdigoat && exit 7
|
2012-12-07 03:03:10 +01:00
|
|
|
test "x\$1" != "x${LOGNAME}" && exit 1
|
2015-05-21 08:40:02 +02:00
|
|
|
if test $# -eq 6 ; then
|
|
|
|
test "x\$2" != "xblah" && exit 2
|
|
|
|
test "x\$3" != "x${expected_key_text}" && exit 3
|
|
|
|
test "x\$4" != "xssh-rsa" && exit 4
|
|
|
|
test "x\$5" != "x${expected_key_fp}" && exit 5
|
|
|
|
test "x\$6" != "xblah" && exit 6
|
|
|
|
fi
|
2012-12-03 00:12:13 +01:00
|
|
|
exec cat "$OBJ/authorized_keys_${LOGNAME}"
|
|
|
|
_EOF
|
|
|
|
$SUDO chmod 0755 "$KEY_COMMAND"
|
|
|
|
|
2016-02-23 06:12:13 +01:00
|
|
|
if ! $OBJ/check-perm -m keys-command $KEY_COMMAND ; then
|
|
|
|
echo "skipping: $KEY_COMMAND is unsuitable as AuthorizedKeysCommand"
|
|
|
|
$SUDO rm -f $KEY_COMMAND
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2012-12-07 03:03:10 +01:00
|
|
|
if [ -x $KEY_COMMAND ]; then
|
2015-05-21 08:40:02 +02:00
|
|
|
cp $OBJ/sshd_proxy $OBJ/sshd_proxy.bak
|
|
|
|
|
|
|
|
verbose "AuthorizedKeysCommand with arguments"
|
|
|
|
(
|
|
|
|
grep -vi AuthorizedKeysFile $OBJ/sshd_proxy.bak
|
|
|
|
echo AuthorizedKeysFile none
|
|
|
|
echo AuthorizedKeysCommand $KEY_COMMAND %u blah %k %t %f blah
|
|
|
|
echo AuthorizedKeysCommandUser ${LOGNAME}
|
|
|
|
) > $OBJ/sshd_proxy
|
|
|
|
|
|
|
|
# Ensure that $PATH is sanitised in sshd
|
|
|
|
env PATH=$PATH:/sbin/mekmitasdigoat \
|
|
|
|
${SSH} -F $OBJ/ssh_proxy somehost true
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
fail "connect failed"
|
|
|
|
fi
|
|
|
|
|
|
|
|
verbose "AuthorizedKeysCommand without arguments"
|
|
|
|
# Check legacy behavior of no-args resulting in username being passed.
|
|
|
|
(
|
|
|
|
grep -vi AuthorizedKeysFile $OBJ/sshd_proxy.bak
|
|
|
|
echo AuthorizedKeysFile none
|
|
|
|
echo AuthorizedKeysCommand $KEY_COMMAND
|
|
|
|
echo AuthorizedKeysCommandUser ${LOGNAME}
|
|
|
|
) > $OBJ/sshd_proxy
|
|
|
|
|
|
|
|
# Ensure that $PATH is sanitised in sshd
|
|
|
|
env PATH=$PATH:/sbin/mekmitasdigoat \
|
|
|
|
${SSH} -F $OBJ/ssh_proxy somehost true
|
2012-12-07 03:03:10 +01:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
fail "connect failed"
|
|
|
|
fi
|
|
|
|
else
|
2021-09-01 02:50:27 +02:00
|
|
|
skip "$KEY_COMMAND not executable (/var/run mounted noexec?)"
|
2012-12-03 00:12:13 +01:00
|
|
|
fi
|