summaryrefslogtreecommitdiff
path: root/regress/usr.bin/ssh/cfgmatch.sh
blob: 2819b4f62b48144e2b64ebf3e1cfe417caa90fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#	$OpenBSD: cfgmatch.sh,v 1.4 2006/12/13 08:36:36 dtucker Exp $
#	Placed in the Public Domain.

tid="sshd_config match"

pidfile=$OBJ/remote_pid
fwdport=3301
fwd="-L $fwdport:127.0.0.1:$PORT"

stop_client()
{
	pid=`cat $pidfile`
	if [ ! -z "$pid" ]; then
		kill $pid
	fi
}

cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak

echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_config
echo "Match Address 127.0.0.1" >>$OBJ/sshd_config
echo "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_config

echo "PermitOpen 127.0.0.1:1" >>$OBJ/sshd_proxy
echo "Match Address 127.0.0.1" >>$OBJ/sshd_proxy
echo "PermitOpen 127.0.0.1:$PORT" >>$OBJ/sshd_proxy

start_sshd

#set -x

# Test Match + PermitOpen in sshd_config.  This should be permitted
for p in 1 2; do
	rm -f $pidfile
	trace "match permitopen localhost proto $p"
	${SSH} -$p $fwd -F $OBJ/ssh_config -f somehost \
	    exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' >>$TEST_SSH_LOGFILE 2>&1 ||\
	    fail "match permitopen proto $p sshd failed"
	sleep 1;
	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
	    fail "match permitopen permit proto $p"
	stop_client
done

# Same but from different source.  This should not be permitted
for p in 1 2; do
	rm -f $pidfile
	trace "match permitopen proxy proto $p"
	${SSH} -q -$p $fwd -F $OBJ/ssh_proxy -f somehost \
	    exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' >>$TEST_SSH_LOGFILE 2>&1 ||\
	    fail "match permitopen proxy proto $p sshd failed"
	sleep 1;
	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
	    fail "match permitopen deny proto $p"
	stop_client
done

# Retry previous with key option, should also be denied.
echo -n 'permitopen="127.0.0.1:'$PORT'" ' >$OBJ/authorized_keys_$USER
cat $OBJ/rsa.pub >> $OBJ/authorized_keys_$USER
echo -n 'permitopen="127.0.0.1:'$PORT'" ' >>$OBJ/authorized_keys_$USER
cat $OBJ/rsa1.pub >> $OBJ/authorized_keys_$USER
for p in 1 2; do
	rm -f $pidfile
	trace "match permitopen proxy w/key opts proto $p"
	${SSH} -q -$p $fwd -F $OBJ/ssh_proxy -f somehost \
	    exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' >>$TEST_SSH_LOGFILE 2>&1 ||\
	    fail "match permitopen w/key opt proto $p sshd failed"
	sleep 1;
	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
	    fail "match permitopen deny w/key opt proto $p"
	stop_client
done

# Test both sshd_config and key options permitting the same dst/port pair.
# Should be permitted.
for p in 1 2; do
	rm -f $pidfile
	trace "match permitopen localhost proto $p"
	${SSH} -$p $fwd -F $OBJ/ssh_config -f somehost \
	    exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' >>$TEST_SSH_LOGFILE 2>&1 ||\
	    fail "match permitopen proto $p sshd failed"
	sleep 1;
	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
	    fail "match permitopen permit proto $p"
	stop_client
done

cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
echo "Match User $USER" >>$OBJ/sshd_proxy
echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy

# Test that a Match overrides a PermitOpen in the global section
for p in 1 2; do
	rm -f $pidfile
	trace "match permitopen proxy w/key opts proto $p"
	${SSH} -q -$p $fwd -F $OBJ/ssh_proxy -f somehost \
	    exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' >>$TEST_SSH_LOGFILE 2>&1 ||\
	    fail "match override permitopen proto $p sshd failed"
	sleep 1;
	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true && \
	    fail "match override permitopen proto $p"
	stop_client
done

cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >>$OBJ/sshd_proxy
echo "Match User NoSuchUser" >>$OBJ/sshd_proxy
echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy

# Test that a rule that doesn't match doesn't override, plus test a
# PermitOpen entry that's not at the start of the list
for p in 1 2; do
	rm -f $pidfile
	trace "nomatch permitopen proxy w/key opts proto $p"
	${SSH} -q -$p $fwd -F $OBJ/ssh_proxy -f somehost \
	    exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' >>$TEST_SSH_LOGFILE 2>&1 ||\
	    fail "nomatch override permitopen proto $p sshd failed"
	sleep 1;
	${SSH} -q -$p -p $fwdport -F $OBJ/ssh_config somehost true || \
	    fail "nomatch override permitopen proto $p"
	stop_client
done