blob: 4029e2c786376bc9192c832e2c30932a610c8718 [file] [log] [blame]
Damien Millerd27bff22017-10-20 13:22:00 +11001# $OpenBSD: keys-command.sh,v 1.4 2016/09/26 21:34:38 bluhm Exp $
Damien Miller771c43c2012-12-03 10:12:13 +11002# Placed in the Public Domain.
3
4tid="authorized keys from command"
5
bluhm@openbsd.orgce44c972016-09-26 21:34:38 +00006if [ -z "$SUDO" -a ! -w /var/run ]; then
Damien Miller771c43c2012-12-03 10:12:13 +11007 echo "skipped (SUDO not set)"
8 echo "need SUDO to create file in /var/run, test won't work without"
9 exit 0
10fi
11
djm@openbsd.org84452c52015-05-21 06:40:02 +000012rm -f $OBJ/keys-command-args
13
14touch $OBJ/keys-command-args
15chmod a+rw $OBJ/keys-command-args
16
17expected_key_text=`awk '{ print $2 }' < $OBJ/rsa.pub`
18expected_key_fp=`$SSHKEYGEN -lf $OBJ/rsa.pub | awk '{ print $2 }'`
19
Damien Miller771c43c2012-12-03 10:12:13 +110020# Establish a AuthorizedKeysCommand in /var/run where it will have
21# acceptable directory permissions.
22KEY_COMMAND="/var/run/keycommand_${LOGNAME}"
djm@openbsd.org84452c52015-05-21 06:40:02 +000023cat << _EOF | $SUDO sh -c "rm -f '$KEY_COMMAND' ; cat > '$KEY_COMMAND'"
Damien Miller771c43c2012-12-03 10:12:13 +110024#!/bin/sh
djm@openbsd.org84452c52015-05-21 06:40:02 +000025echo args: "\$@" >> $OBJ/keys-command-args
26echo "$PATH" | grep -q mekmitasdigoat && exit 7
Darren Tucker3dfb8772012-12-07 13:03:10 +110027test "x\$1" != "x${LOGNAME}" && exit 1
djm@openbsd.org84452c52015-05-21 06:40:02 +000028if test $# -eq 6 ; then
29 test "x\$2" != "xblah" && exit 2
30 test "x\$3" != "x${expected_key_text}" && exit 3
31 test "x\$4" != "xssh-rsa" && exit 4
32 test "x\$5" != "x${expected_key_fp}" && exit 5
33 test "x\$6" != "xblah" && exit 6
34fi
Damien Miller771c43c2012-12-03 10:12:13 +110035exec cat "$OBJ/authorized_keys_${LOGNAME}"
36_EOF
37$SUDO chmod 0755 "$KEY_COMMAND"
38
Damien Miller1acc0582016-02-23 16:12:13 +110039if ! $OBJ/check-perm -m keys-command $KEY_COMMAND ; then
40 echo "skipping: $KEY_COMMAND is unsuitable as AuthorizedKeysCommand"
41 $SUDO rm -f $KEY_COMMAND
42 exit 0
43fi
44
Darren Tucker3dfb8772012-12-07 13:03:10 +110045if [ -x $KEY_COMMAND ]; then
djm@openbsd.org84452c52015-05-21 06:40:02 +000046 cp $OBJ/sshd_proxy $OBJ/sshd_proxy.bak
47
48 verbose "AuthorizedKeysCommand with arguments"
49 (
50 grep -vi AuthorizedKeysFile $OBJ/sshd_proxy.bak
51 echo AuthorizedKeysFile none
52 echo AuthorizedKeysCommand $KEY_COMMAND %u blah %k %t %f blah
53 echo AuthorizedKeysCommandUser ${LOGNAME}
54 ) > $OBJ/sshd_proxy
55
56 # Ensure that $PATH is sanitised in sshd
57 env PATH=$PATH:/sbin/mekmitasdigoat \
58 ${SSH} -F $OBJ/ssh_proxy somehost true
59 if [ $? -ne 0 ]; then
60 fail "connect failed"
61 fi
62
63 verbose "AuthorizedKeysCommand without arguments"
64 # Check legacy behavior of no-args resulting in username being passed.
65 (
66 grep -vi AuthorizedKeysFile $OBJ/sshd_proxy.bak
67 echo AuthorizedKeysFile none
68 echo AuthorizedKeysCommand $KEY_COMMAND
69 echo AuthorizedKeysCommandUser ${LOGNAME}
70 ) > $OBJ/sshd_proxy
71
72 # Ensure that $PATH is sanitised in sshd
73 env PATH=$PATH:/sbin/mekmitasdigoat \
74 ${SSH} -F $OBJ/ssh_proxy somehost true
Darren Tucker3dfb8772012-12-07 13:03:10 +110075 if [ $? -ne 0 ]; then
76 fail "connect failed"
77 fi
78else
79 echo "SKIPPED: $KEY_COMMAND not executable (/var/run mounted noexec?)"
Damien Miller771c43c2012-12-03 10:12:13 +110080fi
Darren Tucker3dfb8772012-12-07 13:03:10 +110081
82$SUDO rm -f $KEY_COMMAND