servcfginclude.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # Placed in the Public Domain.
  2. tid="server config include"
  3. cat > $OBJ/sshd_config.i << _EOF
  4. HostKey $OBJ/host.ssh-ed25519
  5. Match host a
  6. Banner /aa
  7. Match host b
  8. Banner /bb
  9. Include $OBJ/sshd_config.i.*
  10. Match host c
  11. Include $OBJ/sshd_config.i.*
  12. Banner /cc
  13. Match host m
  14. Include $OBJ/sshd_config.i.*
  15. Match Host d
  16. Banner /dd
  17. Match Host e
  18. Banner /ee
  19. Include $OBJ/sshd_config.i.*
  20. Match Host f
  21. Include $OBJ/sshd_config.i.*
  22. Banner /ff
  23. Match Host n
  24. Include $OBJ/sshd_config.i.*
  25. _EOF
  26. cat > $OBJ/sshd_config.i.0 << _EOF
  27. Match host xxxxxx
  28. _EOF
  29. cat > $OBJ/sshd_config.i.1 << _EOF
  30. Match host a
  31. Banner /aaa
  32. Match host b
  33. Banner /bbb
  34. Match host c
  35. Banner /ccc
  36. Match Host d
  37. Banner /ddd
  38. Match Host e
  39. Banner /eee
  40. Match Host f
  41. Banner /fff
  42. _EOF
  43. cat > $OBJ/sshd_config.i.2 << _EOF
  44. Match host a
  45. Banner /aaaa
  46. Match host b
  47. Banner /bbbb
  48. Match host c
  49. Banner /cccc
  50. Match Host d
  51. Banner /dddd
  52. Match Host e
  53. Banner /eeee
  54. Match Host f
  55. Banner /ffff
  56. Match all
  57. Banner /xxxx
  58. _EOF
  59. trial()
  60. {
  61. _host="$1"
  62. _exp="$2"
  63. _desc="$3"
  64. test -z "$_desc" && _desc="test match"
  65. trace "$_desc host=$_host expect=$_exp"
  66. ${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i -T \
  67. -C "host=$_host,user=test,addr=127.0.0.1" > $OBJ/sshd_config.out ||
  68. fatal "ssh config parse failed: $_desc host=$_host expect=$_exp"
  69. _got=$(grep -i '^banner ' $OBJ/sshd_config.out | awk '{print $2}')
  70. if test "x$_exp" != "x$_got"; then
  71. fail "$desc_ host $_host include fail: expected $_exp got $_got"
  72. fi
  73. }
  74. trial a /aa
  75. trial b /bb
  76. trial c /ccc
  77. trial d /dd
  78. trial e /ee
  79. trial f /fff
  80. trial m /xxxx
  81. trial n /xxxx
  82. trial x none
  83. # Prepare an included config with an error.
  84. cat > $OBJ/sshd_config.i.3 << _EOF
  85. Banner xxxx
  86. Junk
  87. _EOF
  88. trace "disallow invalid config host=a"
  89. ${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i \
  90. -C "host=a,user=test,addr=127.0.0.1" 2> /dev/null &&
  91. fail "sshd include allowed invalid config"
  92. trace "disallow invalid config host=x"
  93. ${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i \
  94. -C "host=x,user=test,addr=127.0.0.1" 2> /dev/null &&
  95. fail "sshd include allowed invalid config"
  96. rm -f $OBJ/sshd_config.i.*
  97. # Ensure that a missing include is not fatal.
  98. cat > $OBJ/sshd_config.i << _EOF
  99. HostKey $OBJ/host.ssh-ed25519
  100. Include $OBJ/sshd_config.i.*
  101. Banner /aa
  102. _EOF
  103. trial a /aa "missing include non-fatal"
  104. # Ensure that Match/Host in an included config does not affect parent.
  105. cat > $OBJ/sshd_config.i.x << _EOF
  106. Match host x
  107. _EOF
  108. trial a /aa "included file does not affect match state"
  109. # Ensure the empty include directive is not accepted
  110. cat > $OBJ/sshd_config.i.x << _EOF
  111. Include
  112. _EOF
  113. trace "disallow invalid with no argument"
  114. ${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i.x -T \
  115. -C "host=x,user=test,addr=127.0.0.1" 2> /dev/null &&
  116. fail "sshd allowed Include with no argument"
  117. # Ensure the Include before any Match block works as expected (bug #3122)
  118. cat > $OBJ/sshd_config.i << _EOF
  119. Banner /xx
  120. HostKey $OBJ/host.ssh-ed25519
  121. Include $OBJ/sshd_config.i.2
  122. Match host a
  123. Banner /aaaa
  124. _EOF
  125. cat > $OBJ/sshd_config.i.2 << _EOF
  126. Match host a
  127. Banner /aa
  128. _EOF
  129. trace "Include before match blocks"
  130. trial a /aa "included file before match blocks is properly evaluated"
  131. # Port in included file is correctly interpretted (bug #3169)
  132. cat > $OBJ/sshd_config.i << _EOF
  133. Include $OBJ/sshd_config.i.2
  134. Port 7722
  135. _EOF
  136. cat > $OBJ/sshd_config.i.2 << _EOF
  137. HostKey $OBJ/host.ssh-ed25519
  138. _EOF
  139. trace "Port after included files"
  140. ${SUDO} ${REAL_SSHD} -f $OBJ/sshd_config.i -T \
  141. -C "host=x,user=test,addr=127.0.0.1" > $OBJ/sshd_config.out ||
  142. fail "failed to parse Port after included files"
  143. _port=$(grep -i '^port ' $OBJ/sshd_config.out | awk '{print $2}')
  144. if test "x7722" != "x$_port"; then
  145. fail "The Port in included file was intertepretted wrongly. Expected 7722, got $_port"
  146. fi
  147. # cleanup
  148. rm -f $OBJ/sshd_config.i $OBJ/sshd_config.i.* $OBJ/sshd_config.out