xref: /freebsd/crypto/openssh/regress/cfgmatch.sh (revision bc5531debefeb54993d01d4f3c8b33ccbe0b4d95)
1*bc5531deSDag-Erling Smørgrav#	$OpenBSD: cfgmatch.sh,v 1.9 2015/03/03 22:35:19 markus Exp $
2ce3adf43SDag-Erling Smørgrav#	Placed in the Public Domain.
3ce3adf43SDag-Erling Smørgrav
4ce3adf43SDag-Erling Smørgravtid="sshd_config match"
5ce3adf43SDag-Erling Smørgrav
6ce3adf43SDag-Erling Smørgravpidfile=$OBJ/remote_pid
7ce3adf43SDag-Erling Smørgravfwdport=3301
8ce3adf43SDag-Erling Smørgravfwd="-L $fwdport:127.0.0.1:$PORT"
9ce3adf43SDag-Erling Smørgrav
10ce3adf43SDag-Erling Smørgravecho "ExitOnForwardFailure=yes" >> $OBJ/ssh_config
11ce3adf43SDag-Erling Smørgravecho "ExitOnForwardFailure=yes" >> $OBJ/ssh_proxy
12ce3adf43SDag-Erling Smørgrav
13ce3adf43SDag-Erling Smørgravstart_client()
14ce3adf43SDag-Erling Smørgrav{
15ce3adf43SDag-Erling Smørgrav	rm -f $pidfile
16ce3adf43SDag-Erling Smørgrav	${SSH} -q -$p $fwd "$@" somehost \
17ce3adf43SDag-Erling Smørgrav	    exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' \
18ce3adf43SDag-Erling Smørgrav	    >>$TEST_REGRESS_LOGFILE 2>&1 &
19ce3adf43SDag-Erling Smørgrav	client_pid=$!
20ce3adf43SDag-Erling Smørgrav	# Wait for remote end
21ce3adf43SDag-Erling Smørgrav	n=0
22ce3adf43SDag-Erling Smørgrav	while test ! -f $pidfile ; do
23ce3adf43SDag-Erling Smørgrav		sleep 1
24ce3adf43SDag-Erling Smørgrav		n=`expr $n + 1`
25ce3adf43SDag-Erling Smørgrav		if test $n -gt 60; then
26ce3adf43SDag-Erling Smørgrav			kill $client_pid
27ce3adf43SDag-Erling Smørgrav			fatal "timeout waiting for background ssh"
28ce3adf43SDag-Erling Smørgrav		fi
29ce3adf43SDag-Erling Smørgrav	done
30ce3adf43SDag-Erling Smørgrav}
31ce3adf43SDag-Erling Smørgrav
32ce3adf43SDag-Erling Smørgravstop_client()
33ce3adf43SDag-Erling Smørgrav{
34ce3adf43SDag-Erling Smørgrav	pid=`cat $pidfile`
35ce3adf43SDag-Erling Smørgrav	if [ ! -z "$pid" ]; then
36ce3adf43SDag-Erling Smørgrav		kill $pid
37ce3adf43SDag-Erling Smørgrav	fi
38ce3adf43SDag-Erling Smørgrav	wait
39ce3adf43SDag-Erling Smørgrav}
40ce3adf43SDag-Erling Smørgrav
41ce3adf43SDag-Erling Smørgravcp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
42ce3adf43SDag-Erling Smørgravecho "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_config
43ce3adf43SDag-Erling Smørgravecho "Match Address 127.0.0.1" >>$OBJ/sshd_config
44ce3adf43SDag-Erling Smørgravecho "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_config
45ce3adf43SDag-Erling Smørgrav
46ce3adf43SDag-Erling Smørgravgrep -v AuthorizedKeysFile $OBJ/sshd_proxy_bak > $OBJ/sshd_proxy
47ce3adf43SDag-Erling Smørgravecho "AuthorizedKeysFile /dev/null" >>$OBJ/sshd_proxy
48ce3adf43SDag-Erling Smørgravecho "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_proxy
49ce3adf43SDag-Erling Smørgravecho "Match user $USER" >>$OBJ/sshd_proxy
50ce3adf43SDag-Erling Smørgravecho "AuthorizedKeysFile /dev/null $OBJ/authorized_keys_%u" >>$OBJ/sshd_proxy
51ce3adf43SDag-Erling Smørgravecho "Match Address 127.0.0.1" >>$OBJ/sshd_proxy
52ce3adf43SDag-Erling Smørgravecho "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_proxy
53ce3adf43SDag-Erling Smørgrav
54ce3adf43SDag-Erling Smørgravstart_sshd
55ce3adf43SDag-Erling Smørgrav
56ce3adf43SDag-Erling Smørgrav#set -x
57ce3adf43SDag-Erling Smørgrav
58ce3adf43SDag-Erling Smørgrav# Test Match + PermitOpen in sshd_config.  This should be permitted
59*bc5531deSDag-Erling Smørgravfor p in ${SSH_PROTOCOLS}; do
60ce3adf43SDag-Erling Smørgrav	trace "match permitopen localhost proto $p"
61ce3adf43SDag-Erling Smørgrav	start_client -F $OBJ/ssh_config
62ce3adf43SDag-Erling Smørgrav	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
63ce3adf43SDag-Erling Smørgrav	    fail "match permitopen permit proto $p"
64ce3adf43SDag-Erling Smørgrav	stop_client
65ce3adf43SDag-Erling Smørgravdone
66ce3adf43SDag-Erling Smørgrav
67ce3adf43SDag-Erling Smørgrav# Same but from different source.  This should not be permitted
68*bc5531deSDag-Erling Smørgravfor p in ${SSH_PROTOCOLS}; do
69ce3adf43SDag-Erling Smørgrav	trace "match permitopen proxy proto $p"
70ce3adf43SDag-Erling Smørgrav	start_client -F $OBJ/ssh_proxy
71ce3adf43SDag-Erling Smørgrav	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
72ce3adf43SDag-Erling Smørgrav	    fail "match permitopen deny proto $p"
73ce3adf43SDag-Erling Smørgrav	stop_client
74ce3adf43SDag-Erling Smørgravdone
75ce3adf43SDag-Erling Smørgrav
76ce3adf43SDag-Erling Smørgrav# Retry previous with key option, should also be denied.
77*bc5531deSDag-Erling Smørgravcp /dev/null $OBJ/authorized_keys_$USER
78*bc5531deSDag-Erling Smørgravfor t in ${SSH_KEYTYPES}; do
79ce3adf43SDag-Erling Smørgrav	printf 'permitopen="127.0.0.1:'$PORT'" ' >> $OBJ/authorized_keys_$USER
80*bc5531deSDag-Erling Smørgrav	cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
81*bc5531deSDag-Erling Smørgravdone
82*bc5531deSDag-Erling Smørgravfor p in ${SSH_PROTOCOLS}; do
83ce3adf43SDag-Erling Smørgrav	trace "match permitopen proxy w/key opts proto $p"
84ce3adf43SDag-Erling Smørgrav	start_client -F $OBJ/ssh_proxy
85ce3adf43SDag-Erling Smørgrav	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
86ce3adf43SDag-Erling Smørgrav	    fail "match permitopen deny w/key opt proto $p"
87ce3adf43SDag-Erling Smørgrav	stop_client
88ce3adf43SDag-Erling Smørgravdone
89ce3adf43SDag-Erling Smørgrav
90ce3adf43SDag-Erling Smørgrav# Test both sshd_config and key options permitting the same dst/port pair.
91ce3adf43SDag-Erling Smørgrav# Should be permitted.
92*bc5531deSDag-Erling Smørgravfor p in ${SSH_PROTOCOLS}; do
93ce3adf43SDag-Erling Smørgrav	trace "match permitopen localhost proto $p"
94ce3adf43SDag-Erling Smørgrav	start_client -F $OBJ/ssh_config
95ce3adf43SDag-Erling Smørgrav	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
96ce3adf43SDag-Erling Smørgrav	    fail "match permitopen permit proto $p"
97ce3adf43SDag-Erling Smørgrav	stop_client
98ce3adf43SDag-Erling Smørgravdone
99ce3adf43SDag-Erling Smørgrav
100ce3adf43SDag-Erling Smørgravcp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
101ce3adf43SDag-Erling Smørgravecho "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
102ce3adf43SDag-Erling Smørgravecho "Match User $USER" >>$OBJ/sshd_proxy
103ce3adf43SDag-Erling Smørgravecho "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
104ce3adf43SDag-Erling Smørgrav
105ce3adf43SDag-Erling Smørgrav# Test that a Match overrides a PermitOpen in the global section
106*bc5531deSDag-Erling Smørgravfor p in ${SSH_PROTOCOLS}; do
107ce3adf43SDag-Erling Smørgrav	trace "match permitopen proxy w/key opts proto $p"
108ce3adf43SDag-Erling Smørgrav	start_client -F $OBJ/ssh_proxy
109ce3adf43SDag-Erling Smørgrav	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
110ce3adf43SDag-Erling Smørgrav	    fail "match override permitopen proto $p"
111ce3adf43SDag-Erling Smørgrav	stop_client
112ce3adf43SDag-Erling Smørgravdone
113ce3adf43SDag-Erling Smørgrav
114ce3adf43SDag-Erling Smørgravcp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
115ce3adf43SDag-Erling Smørgravecho "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
116ce3adf43SDag-Erling Smørgravecho "Match User NoSuchUser" >>$OBJ/sshd_proxy
117ce3adf43SDag-Erling Smørgravecho "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy
118ce3adf43SDag-Erling Smørgrav
119ce3adf43SDag-Erling Smørgrav# Test that a rule that doesn't match doesn't override, plus test a
120ce3adf43SDag-Erling Smørgrav# PermitOpen entry that's not at the start of the list
121*bc5531deSDag-Erling Smørgravfor p in ${SSH_PROTOCOLS}; do
122ce3adf43SDag-Erling Smørgrav	trace "nomatch permitopen proxy w/key opts proto $p"
123ce3adf43SDag-Erling Smørgrav	start_client -F $OBJ/ssh_proxy
124ce3adf43SDag-Erling Smørgrav	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
125ce3adf43SDag-Erling Smørgrav	    fail "nomatch override permitopen proto $p"
126ce3adf43SDag-Erling Smørgrav	stop_client
127ce3adf43SDag-Erling Smørgravdone
128