1 # $OpenBSD: cfgmatchlisten.sh,v 1.3 2018/07/02 14:13:30 dtucker Exp $
2 # Placed in the Public Domain.
4 tid="sshd_config matchlisten"
6 pidfile=$OBJ/remote_pid
8 fwdspec="localhost:${fwdport}"
9 fwd="-R $fwdport:127.0.0.1:$PORT"
11 echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_config
12 echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_proxy
17 ${SSH} -vvv $fwd "$@" somehost true >>$TEST_REGRESS_LOGFILE 2>&1
22 ${SSH} -vvv $fwd "$@" somehost \
23 exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' \
24 >>$TEST_REGRESS_LOGFILE 2>&1 &
28 while test ! -f $pidfile ; do
31 if test $n -gt 60; then
33 fatal "timeout waiting for background ssh"
42 fail "client did not start"
56 if [ ! -z "$pid" ]; then
62 cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
63 echo "PermitListen 127.0.0.1:1" >>$OBJ/sshd_config
64 echo "Match Address 127.0.0.1" >>$OBJ/sshd_config
65 echo "PermitListen 127.0.0.1:2 127.0.0.1:3 $fwdspec" >>$OBJ/sshd_config
67 grep -v AuthorizedKeysFile $OBJ/sshd_proxy_bak > $OBJ/sshd_proxy
68 echo "AuthorizedKeysFile /dev/null" >>$OBJ/sshd_proxy
69 echo "PermitListen 127.0.0.1:1" >>$OBJ/sshd_proxy
70 echo "Match user $USER" >>$OBJ/sshd_proxy
71 echo "AuthorizedKeysFile /dev/null $OBJ/authorized_keys_%u" >>$OBJ/sshd_proxy
72 echo "Match Address 127.0.0.1" >>$OBJ/sshd_proxy
73 echo "PermitListen 127.0.0.1:2 127.0.0.1:3 $fwdspec" >>$OBJ/sshd_proxy
79 # Test Match + PermitListen in sshd_config. This should be permitted
80 trace "match permitlisten localhost"
81 expect_client_ok -F $OBJ/ssh_config
82 ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true || \
83 fail "match permitlisten permit"
86 # Same but from different source. This should not be permitted
87 trace "match permitlisten proxy"
88 expect_client_fail "match permitlisten deny" \
91 # Retry previous with key option, should also be denied.
92 cp /dev/null $OBJ/authorized_keys_$USER
93 for t in ${SSH_KEYTYPES}; do
94 printf 'permitlisten="'$fwdspec'" ' >> $OBJ/authorized_keys_$USER
95 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
97 trace "match permitlisten proxy w/key opts"
98 expect_client_fail "match permitlisten deny w/key opt"\
101 # Test both sshd_config and key options permitting the same dst/port pair.
102 # Should be permitted.
103 trace "match permitlisten localhost"
104 expect_client_ok -F $OBJ/ssh_config
105 ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true || \
106 fail "match permitlisten permit"
109 # Test that a bare port number is accepted in PermitListen
110 cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
111 echo "PermitListen 127.0.0.1:1 $fwdport 127.0.0.2:2" >>$OBJ/sshd_proxy
112 trace "match permitlisten bare"
113 expect_client_ok -F $OBJ/ssh_config
114 ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true || \
115 fail "match permitlisten bare"
118 # Test that an incorrect bare port number is denied as expected
119 cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
120 echo "PermitListen 1 2 99" >>$OBJ/sshd_proxy
121 trace "match permitlisten bare"
122 expect_client_fail -F $OBJ/ssh_config
124 cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
125 echo "PermitListen 127.0.0.1:1 $fwdspec 127.0.0.2:2" >>$OBJ/sshd_proxy
126 echo "Match User $USER" >>$OBJ/sshd_proxy
127 echo "PermitListen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
129 # Test that a Match overrides a PermitListen in the global section
130 trace "match permitlisten proxy w/key opts"
131 expect_client_fail "match override permitlisten" \
134 cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
135 echo "PermitListen 127.0.0.1:1 $fwdspec 127.0.0.2:2" >>$OBJ/sshd_proxy
136 echo "Match User NoSuchUser" >>$OBJ/sshd_proxy
137 echo "PermitListen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
139 # Test that a rule that doesn't match doesn't override, plus test a
140 # PermitListen entry that's not at the start of the list
141 trace "nomatch permitlisten proxy w/key opts"
142 expect_client_ok -F $OBJ/ssh_proxy
143 ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true || \
144 fail "nomatch override permitlisten"
147 # bind to 127.0.0.1 instead of default localhost
148 fwdspec2="127.0.0.1:${fwdport}"
149 fwd="-R ${fwdspec2}:127.0.0.1:$PORT"
151 # first try w/ old fwdspec both in server config and key opts
152 cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
153 echo "PermitListen 127.0.0.1:1 $fwdspec 127.0.0.2:2" >>$OBJ/sshd_proxy
154 cp /dev/null $OBJ/authorized_keys_$USER
155 for t in ${SSH_KEYTYPES}; do
156 printf 'permitlisten="'$fwdspec'" ' >> $OBJ/authorized_keys_$USER
157 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
159 trace "nomatch permitlisten 127.0.0.1 server config and userkey"
160 expect_client_fail "nomatch 127.0.0.1 server config and userkey" \
163 # correct server config, denied by key opts
164 cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
165 echo "PermitListen 127.0.0.1:1 ${fwdspec2} 127.0.0.2:2" >>$OBJ/sshd_proxy
166 trace "nomatch permitlisten 127.0.0.1 w/key opts"
167 expect_client_fail "nomatch 127.0.0.1 w/key opts" \
171 cp /dev/null $OBJ/authorized_keys_$USER
172 for t in ${SSH_KEYTYPES}; do
173 printf 'permitlisten="'$fwdspec2'" ' >> $OBJ/authorized_keys_$USER
174 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
176 trace "match permitlisten 127.0.0.1 server config w/key opts"
177 expect_client_ok -F $OBJ/ssh_proxy
178 ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true || \
179 fail "match 127.0.0.1 server config w/key opts"
182 # key opts with bare port number
183 cp /dev/null $OBJ/authorized_keys_$USER
184 for t in ${SSH_KEYTYPES}; do
185 printf 'permitlisten="'$fwdport'" ' >> $OBJ/authorized_keys_$USER
186 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
188 trace "match permitlisten 127.0.0.1 server config w/key opts (bare)"
189 expect_client_ok -F $OBJ/ssh_proxy
190 ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true || \
191 fail "match 127.0.0.1 server config w/key opts (bare)"
194 # key opts with incorrect bare port number
195 cp /dev/null $OBJ/authorized_keys_$USER
196 for t in ${SSH_KEYTYPES}; do
197 printf 'permitlisten="99" ' >> $OBJ/authorized_keys_$USER
198 cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
200 trace "match permitlisten 127.0.0.1 server config w/key opts (wrong bare)"
201 expect_client_fail "nomatch 127.0.0.1 w/key opts (wrong bare)" \