1*19261079SEd Maste# $OpenBSD: addrmatch.sh,v 1.6 2020/08/28 03:17:13 dtucker Exp $ 2ce3adf43SDag-Erling Smørgrav# Placed in the Public Domain. 3ce3adf43SDag-Erling Smørgrav 4ce3adf43SDag-Erling Smørgravtid="address match" 5ce3adf43SDag-Erling Smørgrav 6ce3adf43SDag-Erling Smørgravmv $OBJ/sshd_proxy $OBJ/sshd_proxy_bak 7ce3adf43SDag-Erling Smørgrav 8ce3adf43SDag-Erling Smørgravrun_trial() 9ce3adf43SDag-Erling Smørgrav{ 10ce3adf43SDag-Erling Smørgrav user="$1"; addr="$2"; host="$3"; laddr="$4"; lport="$5" 11ce3adf43SDag-Erling Smørgrav expected="$6"; descr="$7" 12ce3adf43SDag-Erling Smørgrav 13ce3adf43SDag-Erling Smørgrav verbose "test $descr for $user $addr $host" 14ce3adf43SDag-Erling Smørgrav result=`${SSHD} -f $OBJ/sshd_proxy -T \ 15ce3adf43SDag-Erling Smørgrav -C user=${user},addr=${addr},host=${host},laddr=${laddr},lport=${lport} | \ 16ce3adf43SDag-Erling Smørgrav awk '/^forcecommand/ {print $2}'` 17ce3adf43SDag-Erling Smørgrav if [ "$result" != "$expected" ]; then 18ce3adf43SDag-Erling Smørgrav fail "failed '$descr' expected $expected got $result" 19ce3adf43SDag-Erling Smørgrav fi 20ce3adf43SDag-Erling Smørgrav} 21ce3adf43SDag-Erling Smørgrav 22ce3adf43SDag-Erling Smørgravcp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy 23ce3adf43SDag-Erling Smørgravcat >>$OBJ/sshd_proxy <<EOD 24ce3adf43SDag-Erling SmørgravForceCommand nomatch 25ce3adf43SDag-Erling SmørgravMatch Address 192.168.0.0/16,!192.168.30.0/24,10.0.0.0/8,host.example.com 26ce3adf43SDag-Erling Smørgrav ForceCommand match1 27ce3adf43SDag-Erling SmørgravMatch Address 1.1.1.1,::1,!::3,2000::/16 28ce3adf43SDag-Erling Smørgrav ForceCommand match2 29ce3adf43SDag-Erling SmørgravMatch LocalAddress 127.0.0.1,::1 30ce3adf43SDag-Erling Smørgrav ForceCommand match3 31ce3adf43SDag-Erling SmørgravMatch LocalPort 5678 32ce3adf43SDag-Erling Smørgrav ForceCommand match4 33ce3adf43SDag-Erling SmørgravEOD 34ce3adf43SDag-Erling Smørgrav 35ce3adf43SDag-Erling Smørgravrun_trial user 192.168.0.1 somehost 1.2.3.4 1234 match1 "first entry" 36ce3adf43SDag-Erling Smørgravrun_trial user 192.168.30.1 somehost 1.2.3.4 1234 nomatch "negative match" 37ce3adf43SDag-Erling Smørgravrun_trial user 19.0.0.1 somehost 1.2.3.4 1234 nomatch "no match" 38ce3adf43SDag-Erling Smørgravrun_trial user 10.255.255.254 somehost 1.2.3.4 1234 match1 "list middle" 39ce3adf43SDag-Erling Smørgravrun_trial user 192.168.30.1 192.168.0.1 1.2.3.4 1234 nomatch "faked IP in hostname" 40ce3adf43SDag-Erling Smørgravrun_trial user 1.1.1.1 somehost.example.com 1.2.3.4 1234 match2 "bare IP4 address" 41ce3adf43SDag-Erling Smørgravrun_trial user 19.0.0.1 somehost 127.0.0.1 1234 match3 "localaddress" 42ce3adf43SDag-Erling Smørgravrun_trial user 19.0.0.1 somehost 1.2.3.4 5678 match4 "localport" 43ce3adf43SDag-Erling Smørgrav 44ce3adf43SDag-Erling Smørgravif test "$TEST_SSH_IPV6" != "no"; then 45ce3adf43SDag-Erling Smørgravrun_trial user ::1 somehost.example.com ::2 1234 match2 "bare IP6 address" 46*19261079SEd Masterun_trial user ::2 somehost.example.com ::2 1234 nomatch "deny IPv6" 47ce3adf43SDag-Erling Smørgravrun_trial user ::3 somehost ::2 1234 nomatch "IP6 negated" 48ce3adf43SDag-Erling Smørgravrun_trial user ::4 somehost ::2 1234 nomatch "IP6 no match" 49ce3adf43SDag-Erling Smørgravrun_trial user 2000::1 somehost ::2 1234 match2 "IP6 network" 50ce3adf43SDag-Erling Smørgravrun_trial user 2001::1 somehost ::2 1234 nomatch "IP6 network" 51ce3adf43SDag-Erling Smørgravrun_trial user ::5 somehost ::1 1234 match3 "IP6 localaddress" 52ce3adf43SDag-Erling Smørgravrun_trial user ::5 somehost ::2 5678 match4 "IP6 localport" 53ce3adf43SDag-Erling Smørgravfi 54ce3adf43SDag-Erling Smørgrav 55*19261079SEd Maste# 56*19261079SEd Maste# Check that we catch invalid address/mask in Match Address/Localaddress 57*19261079SEd Maste# 58*19261079SEd Mastefor i in 10.0.1.0/8 10.0.0.1/24 2000:aa:bb:01::/56; do 59*19261079SEd Maste for a in address localaddress; do 60*19261079SEd Maste verbose "test invalid Match $a $i" 61*19261079SEd Maste echo "Match $a $i" > $OBJ/sshd_proxy 62*19261079SEd Maste ${SUDO} ${SSHD} -f $OBJ/sshd_proxy -t >/dev/null 2>&1 && \ 63*19261079SEd Maste fail "accepted invalid match $a $i" 64*19261079SEd Maste done 65*19261079SEd Mastedone 66*19261079SEd Maste 67ce3adf43SDag-Erling Smørgravcp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy 68ce3adf43SDag-Erling Smørgravrm $OBJ/sshd_proxy_bak 69