diff options
Diffstat (limited to 'regress/cfgmatchlisten.sh')
-rw-r--r-- | regress/cfgmatchlisten.sh | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/regress/cfgmatchlisten.sh b/regress/cfgmatchlisten.sh new file mode 100644 index 000000000..8155c56bc --- /dev/null +++ b/regress/cfgmatchlisten.sh | |||
@@ -0,0 +1,165 @@ | |||
1 | # $OpenBSD: cfgmatchlisten.sh,v 1.1 2018/06/07 04:46:34 djm Exp $ | ||
2 | # Placed in the Public Domain. | ||
3 | |||
4 | tid="sshd_config matchlisten" | ||
5 | |||
6 | pidfile=$OBJ/remote_pid | ||
7 | fwdport=3301 | ||
8 | fwdspec="localhost:${fwdport}" | ||
9 | fwd="-R $fwdport:127.0.0.1:$PORT" | ||
10 | |||
11 | echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_config | ||
12 | echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_proxy | ||
13 | |||
14 | start_client() | ||
15 | { | ||
16 | rm -f $pidfile | ||
17 | ${SSH} -vvv $fwd "$@" somehost true >>$TEST_REGRESS_LOGFILE 2>&1 | ||
18 | r=$? | ||
19 | if [ $r -ne 0 ]; then | ||
20 | return $r | ||
21 | fi | ||
22 | ${SSH} -vvv $fwd "$@" somehost \ | ||
23 | exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' & | ||
24 | >>$TEST_REGRESS_LOGFILE 2>&1 & | ||
25 | client_pid=$! | ||
26 | # Wait for remote end | ||
27 | n=0 | ||
28 | while test ! -f $pidfile ; do | ||
29 | sleep 1 | ||
30 | n=`expr $n + 1` | ||
31 | if test $n -gt 5; then | ||
32 | kill $client_pid | ||
33 | fatal "timeout waiting for background ssh" | ||
34 | fi | ||
35 | done | ||
36 | return $r | ||
37 | } | ||
38 | |||
39 | expect_client_ok() | ||
40 | { | ||
41 | start_client "$@" || | ||
42 | fail "client did not start" | ||
43 | } | ||
44 | |||
45 | expect_client_fail() | ||
46 | { | ||
47 | local failmsg="$1" | ||
48 | shift | ||
49 | start_client "$@" && | ||
50 | fail $failmsg | ||
51 | } | ||
52 | |||
53 | stop_client() | ||
54 | { | ||
55 | pid=`cat $pidfile` | ||
56 | if [ ! -z "$pid" ]; then | ||
57 | kill $pid | ||
58 | fi | ||
59 | wait | ||
60 | } | ||
61 | |||
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 | ||
66 | |||
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 | ||
74 | |||
75 | start_sshd | ||
76 | |||
77 | #set -x | ||
78 | |||
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" | ||
84 | stop_client | ||
85 | |||
86 | # Same but from different source. This should not be permitted | ||
87 | trace "match permitlisten proxy" | ||
88 | expect_client_fail "match permitlisten deny" \ | ||
89 | -F $OBJ/ssh_proxy | ||
90 | |||
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 | ||
96 | done | ||
97 | trace "match permitlisten proxy w/key opts" | ||
98 | expect_client_fail "match permitlisten deny w/key opt"\ | ||
99 | -F $OBJ/ssh_proxy | ||
100 | |||
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" | ||
107 | stop_client | ||
108 | |||
109 | cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy | ||
110 | echo "PermitListen 127.0.0.1:1 $fwdspec 127.0.0.2:2" >>$OBJ/sshd_proxy | ||
111 | echo "Match User $USER" >>$OBJ/sshd_proxy | ||
112 | echo "PermitListen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy | ||
113 | |||
114 | # Test that a Match overrides a PermitListen in the global section | ||
115 | trace "match permitlisten proxy w/key opts" | ||
116 | expect_client_fail "match override permitlisten" \ | ||
117 | -F $OBJ/ssh_proxy | ||
118 | |||
119 | cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy | ||
120 | echo "PermitListen 127.0.0.1:1 $fwdspec 127.0.0.2:2" >>$OBJ/sshd_proxy | ||
121 | echo "Match User NoSuchUser" >>$OBJ/sshd_proxy | ||
122 | echo "PermitListen 127.0.0.1:1 127.0.0.1:2" >>$OBJ/sshd_proxy | ||
123 | |||
124 | # Test that a rule that doesn't match doesn't override, plus test a | ||
125 | # PermitListen entry that's not at the start of the list | ||
126 | trace "nomatch permitlisten proxy w/key opts" | ||
127 | expect_client_ok -F $OBJ/ssh_proxy | ||
128 | ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true || \ | ||
129 | fail "nomatch override permitlisten" | ||
130 | stop_client | ||
131 | |||
132 | # bind to 127.0.0.1 instead of default localhost | ||
133 | fwdspec2="127.0.0.1:${fwdport}" | ||
134 | fwd="-R ${fwdspec2}:127.0.0.1:$PORT" | ||
135 | |||
136 | # first try w/ old fwdspec both in server config and key opts | ||
137 | cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy | ||
138 | echo "PermitListen 127.0.0.1:1 $fwdspec 127.0.0.2:2" >>$OBJ/sshd_proxy | ||
139 | cp /dev/null $OBJ/authorized_keys_$USER | ||
140 | for t in ${SSH_KEYTYPES}; do | ||
141 | printf 'permitlisten="'$fwdspec'" ' >> $OBJ/authorized_keys_$USER | ||
142 | cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER | ||
143 | done | ||
144 | trace "nomatch permitlisten 127.0.0.1 server config and userkey" | ||
145 | expect_client_fail "nomatch 127.0.0.1 server config and userkey" \ | ||
146 | -F $OBJ/ssh_config | ||
147 | |||
148 | # correct server config, denied by key opts | ||
149 | cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy | ||
150 | echo "PermitListen 127.0.0.1:1 ${fwdspec2} 127.0.0.2:2" >>$OBJ/sshd_proxy | ||
151 | trace "nomatch permitlisten 127.0.0.1 w/key opts" | ||
152 | expect_client_fail "nomatch 127.0.0.1 w/key otps" \ | ||
153 | -F $OBJ/ssh_config | ||
154 | |||
155 | # fix key opts | ||
156 | cp /dev/null $OBJ/authorized_keys_$USER | ||
157 | for t in ${SSH_KEYTYPES}; do | ||
158 | printf 'permitlisten="'$fwdspec2'" ' >> $OBJ/authorized_keys_$USER | ||
159 | cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER | ||
160 | done | ||
161 | trace "match permitlisten 127.0.0.1 server config w/key opts" | ||
162 | expect_client_ok -F $OBJ/ssh_proxy | ||
163 | ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true || \ | ||
164 | fail "match 127.0.0.1 server config w/key opts" | ||
165 | stop_client | ||