addrmatch.sh 2.4 KB

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