blob: 80cf22930ce3bf5cc6fcbc4ca237ab50f24c23bd [file] [log] [blame]
Darren Tucker56347ef2013-05-17 13:28:36 +10001# $OpenBSD: cfgmatch.sh,v 1.8 2013/05/17 00:37:40 dtucker 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"\' \
Darren Tucker75129022013-05-17 09:19:10 +100018 >>$TEST_REGRESS_LOGFILE 2>&1 &
Damien Miller8cb35872011-05-29 21:59:10 +100019 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
Damien Miller7b1877c2006-07-24 15:31:41 +100037 fi
Damien Miller8cb35872011-05-29 21:59:10 +100038 wait
Damien Miller7b1877c2006-07-24 15:31:41 +100039}
40
41cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
Damien Miller7b1877c2006-07-24 15:31:41 +100042echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_config
43echo "Match Address 127.0.0.1" >>$OBJ/sshd_config
44echo "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_config
45
Darren Tucker7f193232013-05-17 19:02:28 +100046grep -v AuthorizedKeysFile $OBJ/sshd_proxy_bak > $OBJ/sshd_proxy
47echo "AuthorizedKeysFile /dev/null" >>$OBJ/sshd_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +100048echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_proxy
Darren Tucker7f193232013-05-17 19:02:28 +100049echo "Match user $USER" >>$OBJ/sshd_proxy
50echo "AuthorizedKeysFile /dev/null $OBJ/authorized_keys_%u" >>$OBJ/sshd_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +100051echo "Match Address 127.0.0.1" >>$OBJ/sshd_proxy
52echo "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_proxy
53
54start_sshd
55
56#set -x
57
58# Test Match + PermitOpen in sshd_config. This should be permitted
59for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +100060 trace "match permitopen localhost proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +100061 start_client -F $OBJ/ssh_config
Damien Miller7b1877c2006-07-24 15:31:41 +100062 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
63 fail "match permitopen permit proto $p"
64 stop_client
65done
66
67# Same but from different source. This should not be permitted
68for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +100069 trace "match permitopen proxy proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +100070 start_client -F $OBJ/ssh_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +100071 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
72 fail "match permitopen deny proto $p"
73 stop_client
74done
75
76# Retry previous with key option, should also be denied.
Darren Tucker56347ef2013-05-17 13:28:36 +100077printf 'permitopen="127.0.0.1:'$PORT'" ' >$OBJ/authorized_keys_$USER
Damien Miller7b1877c2006-07-24 15:31:41 +100078cat $OBJ/rsa.pub >> $OBJ/authorized_keys_$USER
Darren Tucker56347ef2013-05-17 13:28:36 +100079printf 'permitopen="127.0.0.1:'$PORT'" ' >>$OBJ/authorized_keys_$USER
Damien Miller7b1877c2006-07-24 15:31:41 +100080cat $OBJ/rsa1.pub >> $OBJ/authorized_keys_$USER
81for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +100082 trace "match permitopen proxy w/key opts proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +100083 start_client -F $OBJ/ssh_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +100084 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
85 fail "match permitopen deny w/key opt proto $p"
86 stop_client
87done
88
89# Test both sshd_config and key options permitting the same dst/port pair.
90# Should be permitted.
91for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +100092 trace "match permitopen localhost proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +100093 start_client -F $OBJ/ssh_config
Damien Miller7b1877c2006-07-24 15:31:41 +100094 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
95 fail "match permitopen permit proto $p"
96 stop_client
97done
98
99cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
100echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
101echo "Match User $USER" >>$OBJ/sshd_proxy
102echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
103
104# Test that a Match overrides a PermitOpen in the global section
105for p in 1 2; do
Damien Miller7b1877c2006-07-24 15:31:41 +1000106 trace "match permitopen proxy w/key opts proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +1000107 start_client -F $OBJ/ssh_proxy
Damien Miller7b1877c2006-07-24 15:31:41 +1000108 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
109 fail "match override permitopen proto $p"
110 stop_client
111done
Damien Miller80ba1302007-10-26 14:45:13 +1000112
113cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
114echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
115echo "Match User NoSuchUser" >>$OBJ/sshd_proxy
116echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
117
118# Test that a rule that doesn't match doesn't override, plus test a
119# PermitOpen entry that's not at the start of the list
120for p in 1 2; do
Damien Miller80ba1302007-10-26 14:45:13 +1000121 trace "nomatch permitopen proxy w/key opts proto $p"
Damien Miller8cb35872011-05-29 21:59:10 +1000122 start_client -F $OBJ/ssh_proxy
Damien Miller80ba1302007-10-26 14:45:13 +1000123 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
124 fail "nomatch override permitopen proto $p"
125 stop_client
126done