cfgmatch.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # $OpenBSD: cfgmatch.sh,v 1.12 2019/04/18 18:57:16 dtucker Exp $
  2. # Placed in the Public Domain.
  3. tid="sshd_config match"
  4. pidfile=$OBJ/remote_pid
  5. fwdport=3301
  6. fwd="-L $fwdport:127.0.0.1:$PORT"
  7. echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_config
  8. echo "ExitOnForwardFailure=yes" >> $OBJ/ssh_proxy
  9. start_client()
  10. {
  11. rm -f $pidfile
  12. ${SSH} -q $fwd "$@" somehost \
  13. exec sh -c \'"echo \$\$ > $pidfile; exec sleep 100"\' \
  14. >> $TEST_REGRESS_LOGFILE 2>&1 &
  15. client_pid=$!
  16. # Wait for remote end
  17. n=0
  18. while test ! -f $pidfile; do
  19. sleep 1
  20. n=$(expr $n + 1)
  21. if test $n -gt 60; then
  22. kill $client_pid
  23. fatal "timeout waiting for background ssh"
  24. fi
  25. done
  26. }
  27. stop_client()
  28. {
  29. pid=$(cat $pidfile)
  30. if [ ! -z "$pid" ]; then
  31. kill $pid
  32. fi
  33. wait
  34. }
  35. cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
  36. echo "PermitOpen 127.0.0.1:1" >> $OBJ/sshd_config
  37. echo "Match Address 127.0.0.1" >> $OBJ/sshd_config
  38. echo "PermitOpen 127.0.0.1:2 127.0.0.1:3 127.0.0.1:$PORT" >> $OBJ/sshd_config
  39. grep -v AuthorizedKeysFile $OBJ/sshd_proxy_bak > $OBJ/sshd_proxy
  40. echo "AuthorizedKeysFile /dev/null" >> $OBJ/sshd_proxy
  41. echo "PermitOpen 127.0.0.1:1" >> $OBJ/sshd_proxy
  42. echo "Match user $USER" >> $OBJ/sshd_proxy
  43. echo "AuthorizedKeysFile /dev/null $OBJ/authorized_keys_%u" >> $OBJ/sshd_proxy
  44. echo "Match Address 127.0.0.1" >> $OBJ/sshd_proxy
  45. echo "PermitOpen 127.0.0.1:2 127.0.0.1:3 127.0.0.1:$PORT" >> $OBJ/sshd_proxy
  46. ${SUDO} ${SSHD} -f $OBJ/sshd_config -T > /dev/null ||
  47. fail "config w/match fails config test"
  48. start_sshd
  49. # Test Match + PermitOpen in sshd_config. This should be permitted
  50. trace "match permitopen localhost"
  51. start_client -F $OBJ/ssh_config
  52. ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true ||
  53. fail "match permitopen permit"
  54. stop_client
  55. # Same but from different source. This should not be permitted
  56. trace "match permitopen proxy"
  57. start_client -F $OBJ/ssh_proxy
  58. ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true &&
  59. fail "match permitopen deny"
  60. stop_client
  61. # Retry previous with key option, should also be denied.
  62. cp /dev/null $OBJ/authorized_keys_$USER
  63. for t in ${SSH_KEYTYPES}; do
  64. printf 'permitopen="127.0.0.1:'$PORT'" ' >> $OBJ/authorized_keys_$USER
  65. cat $OBJ/$t.pub >> $OBJ/authorized_keys_$USER
  66. done
  67. trace "match permitopen proxy w/key opts"
  68. start_client -F $OBJ/ssh_proxy
  69. ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true &&
  70. fail "match permitopen deny w/key opt"
  71. stop_client
  72. # Test both sshd_config and key options permitting the same dst/port pair.
  73. # Should be permitted.
  74. trace "match permitopen localhost"
  75. start_client -F $OBJ/ssh_config
  76. ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true ||
  77. fail "match permitopen permit"
  78. stop_client
  79. cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
  80. echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >> $OBJ/sshd_proxy
  81. echo "Match User $USER" >> $OBJ/sshd_proxy
  82. echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >> $OBJ/sshd_proxy
  83. # Test that a Match overrides a PermitOpen in the global section
  84. trace "match permitopen proxy w/key opts"
  85. start_client -F $OBJ/ssh_proxy
  86. ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true &&
  87. fail "match override permitopen"
  88. stop_client
  89. cp $OBJ/sshd_proxy_bak $OBJ/sshd_proxy
  90. echo "PermitOpen 127.0.0.1:1 127.0.0.1:$PORT 127.0.0.2:2" >> $OBJ/sshd_proxy
  91. echo "Match User NoSuchUser" >> $OBJ/sshd_proxy
  92. echo "PermitOpen 127.0.0.1:1 127.0.0.1:2" >> $OBJ/sshd_proxy
  93. # Test that a rule that doesn't match doesn't override, plus test a
  94. # PermitOpen entry that's not at the start of the list
  95. trace "nomatch permitopen proxy w/key opts"
  96. start_client -F $OBJ/ssh_proxy
  97. ${SSH} -q -p $fwdport -F $OBJ/ssh_config somehost true ||
  98. fail "nomatch override permitopen"
  99. stop_client
  100. # Test parsing of available Match criteria (with the exception of Group which
  101. # requires knowledge of actual group memberships user running the test).
  102. params="user:user:u1 host:host:h1 address:addr:1.2.3.4 \
  103. localaddress:laddr:5.6.7.8 rdomain:rdomain:rdom1"
  104. cp $OBJ/sshd_proxy_bak $OBJ/sshd_config
  105. echo 'Banner /nomatch' >> $OBJ/sshd_config
  106. for i in $params; do
  107. config=$(echo $i | cut -f1 -d:)
  108. criteria=$(echo $i | cut -f2 -d:)
  109. value=$(echo $i | cut -f3 -d:)
  110. cat >> $OBJ/sshd_config << EOD
  111. Match $config $value
  112. Banner /$value
  113. EOD
  114. done
  115. ${SUDO} ${SSHD} -f $OBJ/sshd_config -T > /dev/null ||
  116. fail "validate config for w/out spec"
  117. # Test matching each criteria.
  118. for i in $params; do
  119. testcriteria=$(echo $i | cut -f2 -d:)
  120. expected=/$(echo $i | cut -f3 -d:)
  121. spec=""
  122. for j in $params; do
  123. config=$(echo $j | cut -f1 -d:)
  124. criteria=$(echo $j | cut -f2 -d:)
  125. value=$(echo $j | cut -f3 -d:)
  126. if [ "$criteria" = "$testcriteria" ]; then
  127. spec="$criteria=$value,$spec"
  128. else
  129. spec="$criteria=1$value,$spec"
  130. fi
  131. done
  132. trace "test spec $spec"
  133. result=$(${SUDO} ${SSHD} -f $OBJ/sshd_config -T -C "$spec" |
  134. awk '$1=="banner"{print $2}')
  135. if [ "$result" != "$expected" ]; then
  136. fail "match $config expected $expected got $result"
  137. fi
  138. done