geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

289 line
5.9 KiB

  1. # $FreeBSD$
  2. PIDFILE=ggatessh.pid
  3. TESTIMG="test.img"
  4. TEMPFILE="random.data"
  5. SFTPSERVER="/usr/libexec/sftp-server"
  6. PORT=2222
  7. atf_test_case ggatessh cleanup
  8. ggatessh_head()
  9. {
  10. atf_set "descr" "ggatessh can proxy to sftp"
  11. atf_set "require.progs" "ggatessh"
  12. atf_set "require.user" "root"
  13. atf_set "timeout" 20
  14. }
  15. ggatessh_body()
  16. {
  17. load_ggate
  18. us=$(alloc_ggate_dev)
  19. n1mchunks=3
  20. secsize=4096
  21. atf_check -e ignore -o ignore \
  22. dd if=/dev/random of="$TEMPFILE" bs=1m count=$n1mchunks conv=notrunc
  23. startup_sshd
  24. truncate -s ${n1mchunks}m "$TESTIMG"
  25. # sshd authenticates and switches to USER
  26. chown "$USER" "$TESTIMG"
  27. echo 'WARNING: ggatessh error messages goes to syslog (aka /var/log/messages)'
  28. atf_check ggatessh create -i "$(pwd)/id_rsa" -p "$PORT" -F "$PIDFILE" -u $us -l "$USER" 127.0.0.1 "$(pwd)/$TESTIMG"
  29. ggate_dev=/dev/ggate${us}
  30. wait_for_ggate_device ${ggate_dev}
  31. # make sure it has correct size and sector sizekj
  32. read _dev _secsize _size _nsecs _stripesize _stripeoff <<EOF
  33. $(diskinfo /dev/ggate$us)
  34. EOF
  35. atf_check_equal "$_secsize" $secsize
  36. atf_check_equal "$_size" $(($n1mchunks * 1024 * 1024))
  37. atf_check_equal "$_nsecs" $(($n1mchunks * 1024 * 1024 / $secsize))
  38. # Test writing
  39. atf_check -e ignore -o ignore \
  40. dd if="$TEMPFILE" of=${ggate_dev} bs=1m count=$n1mchunks conv=notrunc
  41. # Test reading
  42. atf_check -e ignore -o ignore \
  43. dd of="$TEMPFILE"2 if=${ggate_dev} bs=1m count=$n1mchunks conv=notrunc
  44. # Verify that we read what we wrote
  45. atf_check cmp "$TEMPFILE" "$TEMPFILE"2
  46. # Verify that the image matches
  47. atf_check cmp "$TEMPFILE" "$TESTIMG"
  48. rm "$TEMPFILE" "$TEMPFILE"2
  49. }
  50. ggatessh_cleanup()
  51. {
  52. common_cleanup
  53. }
  54. atf_test_case ggatessh_resize cleanup
  55. ggatessh_resize_head()
  56. {
  57. atf_set "descr" "ggatessh will resize the devices"
  58. atf_set "require.progs" "ggatessh"
  59. atf_set "require.user" "root"
  60. atf_set "timeout" 20
  61. }
  62. ggatessh_resize_body()
  63. {
  64. n1mchunks=4
  65. secsize=4096
  66. us=$(alloc_ggate_dev)
  67. startup_sshd
  68. truncate -s ${n1mchunks}m "$TESTIMG"
  69. # sshd authenticates and switches to USER
  70. chown "$USER" "$TESTIMG"
  71. echo 'WARNING: ggatessh error messages goes to syslog (aka /var/log/messages)'
  72. atf_check ggatessh create -i "$(pwd)/id_rsa" -p "$PORT" -F "$PIDFILE" -u $us -l "$USER" 127.0.0.1 "$(pwd)/$TESTIMG"
  73. ggate_dev=/dev/ggate${us}
  74. wait_for_ggate_device ${ggate_dev}
  75. # make sure it has correct size and sector sizekj
  76. read _dev _secsize _size _nsecs _stripesize _stripeoff <<EOF
  77. $(diskinfo /dev/ggate$us)
  78. EOF
  79. atf_check_equal "$_secsize" $secsize
  80. atf_check_equal "$_size" $(($n1mchunks * 1024 * 1024))
  81. atf_check_equal "$_nsecs" $(($n1mchunks * 1024 * 1024 / $secsize))
  82. # kill off old ggate
  83. pkill -F "$PIDFILE"
  84. # Test resizing
  85. n1mchunks=6
  86. truncate -s ${n1mchunks}m "$TESTIMG"
  87. ps auxwww | grep ggatessh
  88. cat "$PIDFILE"
  89. sleep 1
  90. # restart ggate
  91. atf_check ggatessh rescue -v -i "$(pwd)/id_rsa" -p "$PORT" -F "$PIDFILE" -u $us -l "$USER" 127.0.0.1 "$(pwd)/$TESTIMG" &
  92. sleep 1
  93. # make sure it has correct size and sector size
  94. read _dev _secsize _size _nsecs _stripesize _stripeoff <<EOF
  95. $(diskinfo /dev/ggate$us)
  96. EOF
  97. atf_check_equal "$_secsize" $secsize
  98. atf_check_equal "$_size" $(($n1mchunks * 1024 * 1024))
  99. atf_check_equal "$_nsecs" $(($n1mchunks * 1024 * 1024 / $secsize))
  100. dd if=/dev/ggate$us of=/dev/null bs=1m
  101. }
  102. ggatessh_resize_cleanup()
  103. {
  104. common_cleanup
  105. }
  106. atf_init_test_cases()
  107. {
  108. atf_add_test_case ggatessh
  109. atf_add_test_case ggatessh_resize
  110. }
  111. alloc_ggate_dev()
  112. {
  113. local us
  114. us=0
  115. while [ -c /dev/ggate${us} ]; do
  116. : $(( us += 1 ))
  117. done
  118. echo ${us} > ggate.devs
  119. echo ${us}
  120. }
  121. alloc_md()
  122. {
  123. local md
  124. md=$(mdconfig -a -t malloc -s 1M) || \
  125. atf_fail "failed to allocate md device"
  126. echo ${md} >> md.devs
  127. echo ${md}
  128. }
  129. # slightly modified from:
  130. # https://serverfault.com/questions/344295/is-it-possible-to-run-sshd-as-a-normal-user
  131. startup_sshd()
  132. {
  133. # Host keys
  134. ssh-keygen -f ssh_host_rsa_key -N '' -t rsa > /dev/null
  135. # user key
  136. ssh-keygen -f id_rsa -N '' -t rsa > /dev/null
  137. (echo -n 'command="/usr/libexec/sftp-server" '; cat id_rsa.pub) > authorized_keys
  138. cat > sshd_config <<EOF
  139. ListenAddress 127.0.0.1:$PORT
  140. HostKey /home/freebsd/custom_ssh/ssh_host_rsa_key
  141. AuthorizedKeysFile $(pwd)/authorized_keys
  142. ChallengeResponseAuthentication no
  143. PasswordAuthentication no
  144. # to allow writable tmp w/ sticky bit
  145. StrictModes no
  146. UsePAM no
  147. Subsystem sftp ${SFTPSERVER}
  148. PidFile $(pwd)/sshd.pid
  149. EOF
  150. if ! :; then
  151. /usr/sbin/sshd -dD -f $(pwd)/sshd_config &
  152. sleep .2
  153. else
  154. /usr/sbin/sshd -f $(pwd)/sshd_config
  155. while ! [ -f sshd.pid ]; do
  156. sleep .2
  157. done
  158. fi
  159. }
  160. checksum()
  161. {
  162. local src work
  163. src=$1
  164. work=$2
  165. src_checksum=$(md5 -q $src)
  166. work_checksum=$(md5 -q $work)
  167. if [ "$work_checksum" != "$src_checksum" ]; then
  168. atf_fail "work md5 checksum didn't match"
  169. fi
  170. ggate_checksum=$(md5 -q /dev/ggate${us})
  171. if [ "$ggate_checksum" != "$src_checksum" ]; then
  172. atf_fail "ggate md5 checksum didn't match"
  173. fi
  174. }
  175. common_cleanup()
  176. {
  177. if [ -f "ggate.devs" ]; then
  178. while read test_ggate; do
  179. ggatessh destroy -f -u $test_ggate >/dev/null
  180. done < ggate.devs
  181. rm ggate.devs
  182. fi
  183. if [ -f "sshd.pid" ]; then
  184. pkill -F sshd.pid
  185. # clean up after startup_sshd
  186. rm ssh_host_rsa_key
  187. rm id_rsa id_rsa.pub
  188. rm authorized_keys
  189. fi
  190. if [ -f "$PIDFILE" ]; then
  191. pkill -F "$PIDFILE"
  192. rm $PIDFILE
  193. fi
  194. if [ -f "PLAINFILES" ]; then
  195. while read f; do
  196. rm -f ${f}
  197. done < ${PLAINFILES}
  198. rm ${PLAINFILES}
  199. fi
  200. if [ -f "md.devs" ]; then
  201. while read test_md; do
  202. mdconfig -d -u $test_md 2>/dev/null
  203. done < md.devs
  204. rm md.devs
  205. fi
  206. true
  207. }
  208. load_ggate()
  209. {
  210. local class=gate
  211. # If the geom class isn't already loaded, try loading it.
  212. if ! kldstat -q -m g_${class}; then
  213. if ! geom ${class} load; then
  214. atf_skip "could not load module for geom class=${class}"
  215. fi
  216. fi
  217. }
  218. # Bug 204616: ggatel(8) creates /dev/ggate* asynchronously if `ggatel create`
  219. # isn't called with `-v`.
  220. wait_for_ggate_device()
  221. {
  222. ggate_device=$1
  223. while [ ! -c $ggate_device ]; do
  224. sleep 0.5
  225. done
  226. }