xref: /freebsd/crypto/openssh/regress/cfgmatch.sh (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1#	$OpenBSD: cfgmatch.sh,v 1.9 2015/03/03 22:35:19 markus Exp $
2#	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
10echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_config
11echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_proxy
12
13start_client()
14{
15	rm -f $pidfile
16	${SSH} -q -$p $fwd "$@" somehost \
17	    exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' \
18	    >>$TEST_REGRESS_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
32stop_client()
33{
34	pid=`cat $pidfile`
35	if [ ! -z "$pid" ]; then
36		kill $pid
37	fi
38	wait
39}
40
41cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
42echo "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
46grep -v AuthorizedKeysFile $OBJ/sshd_proxy_bak > $OBJ/sshd_proxy
47echo "AuthorizedKeysFile /dev/null" >>$OBJ/sshd_proxy
48echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_proxy
49echo "Match user $USER" >>$OBJ/sshd_proxy
50echo "AuthorizedKeysFile /dev/null $OBJ/authorized_keys_%u" >>$OBJ/sshd_proxy
51echo "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 ${SSH_PROTOCOLS}; do
60	trace "match permitopen localhost proto $p"
61	start_client -F $OBJ/ssh_config
62	${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 ${SSH_PROTOCOLS}; do
69	trace "match permitopen proxy proto $p"
70	start_client -F $OBJ/ssh_proxy
71	${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.
77cp /dev/null $OBJ/authorized_keys_$USER
78for t in ${SSH_KEYTYPES}; do
79	printf 'permitopen="127.0.0.1:'$PORT'" ' >> $OBJ/authorized_keys_$USER
80	cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
81done
82for p in ${SSH_PROTOCOLS}; do
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"
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 ${SSH_PROTOCOLS}; do
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"
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 ${SSH_PROTOCOLS}; do
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"
111	stop_client
112done
113
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 ${SSH_PROTOCOLS}; do
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"
126	stop_client
127done
128