1 # $OpenBSD: cfgmatch.sh,v 1.6 2011/06/03 05:35:10 dtucker Exp $
2 # Placed in the Public Domain.
4 tid="sshd_config match"
6 pidfile=$OBJ/remote_pid
8 fwd="-L $fwdport:127.0.0.1:$PORT"
10 echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_config
11 echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_proxy
16 ${SSH} -q -$p $fwd "$@" somehost \
17 exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' \
18 >>$TEST_SSH_LOGFILE 2>&1 &
22 while test ! -f $pidfile ; do
25 if test $n -gt 60; then
27 fatal "timeout waiting for background ssh"
35 if [ ! -z "$pid" ]; then
42 cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
43 grep -v AuthorizedKeysFile $OBJ/sshd_proxy_bak > $OBJ/sshd_proxy
44 echo "AuthorizedKeysFile /dev/null" >>$OBJ/sshd_proxy
45 echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_config
46 echo "Match user $USER" >>$OBJ/sshd_proxy
47 echo "AuthorizedKeysFile /dev/null $OBJ/authorized_keys_%u" >>$OBJ/sshd_proxy
48 echo "Match Address 127.0.0.1" >>$OBJ/sshd_config
49 echo "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_config
51 echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_proxy
52 echo "Match Address 127.0.0.1" >>$OBJ/sshd_proxy
53 echo "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_proxy
59 # Test Match + PermitOpen in sshd_config. This should be permitted
61 trace "match permitopen localhost proto $p"
62 start_client -F $OBJ/ssh_config
63 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
64 fail "match permitopen permit proto $p"
68 # Same but from different source. This should not be permitted
70 trace "match permitopen proxy proto $p"
71 start_client -F $OBJ/ssh_proxy
72 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
73 fail "match permitopen deny proto $p"
77 # Retry previous with key option, should also be denied.
78 echon 'permitopen="127.0.0.1:'$PORT'" ' >$OBJ/authorized_keys_$USER
79 cat $OBJ/rsa.pub >> $OBJ/authorized_keys_$USER
80 echon 'permitopen="127.0.0.1:'$PORT'" ' >>$OBJ/authorized_keys_$USER
81 cat $OBJ/rsa1.pub >> $OBJ/authorized_keys_$USER
83 trace "match permitopen proxy w/key opts proto $p"
84 start_client -F $OBJ/ssh_proxy
85 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
86 fail "match permitopen deny w/key opt proto $p"
90 # Test both sshd_config and key options permitting the same dst/port pair.
91 # Should be permitted.
93 trace "match permitopen localhost proto $p"
94 start_client -F $OBJ/ssh_config
95 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
96 fail "match permitopen permit proto $p"
100 cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
101 echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
102 echo "Match User $USER" >>$OBJ/sshd_proxy
103 echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
105 # Test that a Match overrides a PermitOpen in the global section
107 trace "match permitopen proxy w/key opts proto $p"
108 start_client -F $OBJ/ssh_proxy
109 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
110 fail "match override permitopen proto $p"
114 cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
115 echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
116 echo "Match User NoSuchUser" >>$OBJ/sshd_proxy
117 echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
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
122 trace "nomatch permitopen proxy w/key opts proto $p"
123 start_client -F $OBJ/ssh_proxy
124 ${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
125 fail "nomatch override permitopen proto $p"