blob: a16a5c13a4056966c4b6b9acea2bba4b96be6a86 [file] [log] [blame]
Damien Miller8cb35872011-05-29 21:59:10 +10001# $OpenBSD: cfgmatch.sh,v 1.5 2011/05/23 03:31:31 djm Exp $
Damien Miller7b1877c2006-07-24 15:31:41 +10002# Placed in the Public Domain.
3
4tid="sshd_config match"
5
6pidfile=$OBJ/remote_pid
7fwdport=3301
8fwd="-L $fwdport:127.0.0.1:$PORT"
9
Tim Ricebc481572011-06-02 22:26:19 -070010echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_config
11echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_proxy
Damien Miller8cb35872011-05-29 21:59:10 +100012
13start_client()
14{
15 rm -f $pidfile
16 ${SSH} -q -$p $fwd "$@" somehost \
17 exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' \
18 >>$TEST_SSH_LOGFILE 2>&1 &
19 client_pid=$!
20 # Wait for remote end
21 n=0
22 while test ! -f $pidfile ; do
23 sleep 1
24 n=`expr $n + 1`
25 if test $n -gt 60; then
26 kill $client_pid
27 fatal "timeout waiting for background ssh"
28 fi
29 done
30}
31
Damien Miller7b1877c2006-07-24 15:31:41 +100032stop_client()
33{
34 pid=`cat $pidfile`
35 if [ ! -z "$pid" ]; then
36 kill $pid
Darren Tucker89f59ce2006-09-08 00:03:05 +100037 sleep 1
Damien Miller7b1877c2006-07-24 15:31:41 +100038 fi
Damien Miller8cb35872011-05-29 21:59:10 +100039 wait
Damien Miller7b1877c2006-07-24 15:31:41 +100040}
41
42cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
Damien Miller8cb35872011-05-29 21:59:10 +100043grep -v AuthorizedKeysFile $OBJ/sshd_proxy_bak > $OBJ/sshd_proxy
44echo "AuthorizedKeysFile /dev/null" >>$OBJ/sshd_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +100045echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_config
Damien Miller8cb35872011-05-29 21:59:10 +100046echo "Match user $USER" >>$OBJ/sshd_proxy
47echo "AuthorizedKeysFile /dev/null $OBJ/authorized_keys_%u" >>$OBJ/sshd_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +100048echo "Match Address 127.0.0.1" >>$OBJ/sshd_config
49echo "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_config
50
51echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_proxy
52echo "Match Address 127.0.0.1" >>$OBJ/sshd_proxy
53echo "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_proxy
54
55start_sshd
56
57#set -x
58
59# Test Match + PermitOpen in sshd_config. This should be permitted
60for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +100061 trace "match permitopen localhost proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +100062 start_client -F $OBJ/ssh_config
Damien Miller7b1877c2006-07-24 15:31:41 +100063 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
64 fail "match permitopen permit proto $p"
65 stop_client
66done
67
68# Same but from different source. This should not be permitted
69for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +100070 trace "match permitopen proxy proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +100071 start_client -F $OBJ/ssh_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +100072 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
73 fail "match permitopen deny proto $p"
74 stop_client
75done
76
77# Retry previous with key option, should also be denied.
Darren Tuckerc614c782010-03-01 12:49:05 +110078echon 'permitopen="127.0.0.1:'$PORT'" ' >$OBJ/authorized_keys_$USER
Damien Miller7b1877c2006-07-24 15:31:41 +100079cat $OBJ/rsa.pub >> $OBJ/authorized_keys_$USER
Darren Tuckerc614c782010-03-01 12:49:05 +110080echon 'permitopen="127.0.0.1:'$PORT'" ' >>$OBJ/authorized_keys_$USER
Damien Miller7b1877c2006-07-24 15:31:41 +100081cat $OBJ/rsa1.pub >> $OBJ/authorized_keys_$USER
82for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +100083 trace "match permitopen proxy w/key opts proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +100084 start_client -F $OBJ/ssh_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +100085 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
86 fail "match permitopen deny w/key opt proto $p"
87 stop_client
88done
89
90# Test both sshd_config and key options permitting the same dst/port pair.
91# Should be permitted.
92for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +100093 trace "match permitopen localhost proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +100094 start_client -F $OBJ/ssh_config
Damien Miller7b1877c2006-07-24 15:31:41 +100095 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
96 fail "match permitopen permit proto $p"
97 stop_client
98done
99
100cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
101echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
102echo "Match User $USER" >>$OBJ/sshd_proxy
103echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
104
105# Test that a Match overrides a PermitOpen in the global section
106for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +1000107 trace "match permitopen proxy w/key opts proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +1000108 start_client -F $OBJ/ssh_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +1000109 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
110 fail "match override permitopen proto $p"
111 stop_client
112done
Damien Miller80ba1302007-10-26 14:45:13 +1000113
114cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
115echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
116echo "Match User NoSuchUser" >>$OBJ/sshd_proxy
117echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
118
119# Test that a rule that doesn't match doesn't override, plus test a
120# PermitOpen entry that's not at the start of the list
121for p in 1 2; do
Damien Miller80ba1302007-10-26 14:45:13 +1000122 trace "nomatch permitopen proxy w/key opts proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +1000123 start_client -F $OBJ/ssh_proxy
Damien Miller80ba1302007-10-26 14:45:13 +1000124 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
125 fail "nomatch override permitopen proto $p"
126 stop_client
127done