sftp-perm.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #!/usr/bin/env sh
  2. # $OpenBSD: sftp-perm.sh,v 1.2 2013/10/17 22:00:18 djm Exp $
  3. # Placed in the Public Domain.
  4. tid="sftp permissions"
  5. SERVER_LOG=${OBJ}/sftp-server.log
  6. CLIENT_LOG=${OBJ}/sftp.log
  7. TEST_SFTP_SERVER=${OBJ}/sftp-server.sh
  8. prepare_server()
  9. {
  10. printf "#!/bin/sh\nexec $SFTPSERVER -el debug3 $* 2>$SERVER_LOG\n" \
  11. > $TEST_SFTP_SERVER
  12. chmod a+x $TEST_SFTP_SERVER
  13. }
  14. run_client()
  15. {
  16. echo "$@" | ${SFTP} -D ${TEST_SFTP_SERVER} -vvvb - > $CLIENT_LOG 2>&1
  17. }
  18. prepare_files()
  19. {
  20. _prep="$1"
  21. rm -f ${COPY} ${COPY}.1
  22. test -d ${COPY}.dd && {
  23. rmdir ${COPY}.dd || fatal "rmdir ${COPY}.dd"
  24. }
  25. test -z "$_prep" && return
  26. sh -c "$_prep" || fail "preparation failed: \"$_prep\""
  27. }
  28. postcondition()
  29. {
  30. _title="$1"
  31. _check="$2"
  32. test -z "$_check" && return
  33. ${TEST_SHELL} -c "$_check" || fail "postcondition check failed: $_title"
  34. }
  35. ro_test()
  36. {
  37. _desc=$1
  38. _cmd="$2"
  39. _prep="$3"
  40. _expect_success_post="$4"
  41. _expect_fail_post="$5"
  42. verbose "$tid: read-only $_desc"
  43. # Plain (no options, mostly to test that _cmd is good)
  44. prepare_files "$_prep"
  45. prepare_server
  46. run_client "$_cmd" || fail "plain $_desc failed"
  47. postcondition "$_desc no-readonly" "$_expect_success_post"
  48. # Read-only enabled
  49. prepare_files "$_prep"
  50. prepare_server -R
  51. run_client "$_cmd" && fail "read-only $_desc succeeded"
  52. postcondition "$_desc readonly" "$_expect_fail_post"
  53. }
  54. perm_test()
  55. {
  56. _op=$1
  57. _whitelist_ops=$2
  58. _cmd="$3"
  59. _prep="$4"
  60. _expect_success_post="$5"
  61. _expect_fail_post="$6"
  62. verbose "$tid: explicit $_op"
  63. # Plain (no options, mostly to test that _cmd is good)
  64. prepare_files "$_prep"
  65. prepare_server
  66. run_client "$_cmd" || fail "plain $_op failed"
  67. postcondition "$_op no white/blacklists" "$_expect_success_post"
  68. # Whitelist
  69. prepare_files "$_prep"
  70. prepare_server -p $_op,$_whitelist_ops
  71. run_client "$_cmd" || fail "whitelisted $_op failed"
  72. postcondition "$_op whitelisted" "$_expect_success_post"
  73. # Blacklist
  74. prepare_files "$_prep"
  75. prepare_server -P $_op
  76. run_client "$_cmd" && fail "blacklisted $_op succeeded"
  77. postcondition "$_op blacklisted" "$_expect_fail_post"
  78. # Whitelist with op missing.
  79. prepare_files "$_prep"
  80. prepare_server -p $_whitelist_ops
  81. run_client "$_cmd" && fail "no whitelist $_op succeeded"
  82. postcondition "$_op not in whitelist" "$_expect_fail_post"
  83. }
  84. ro_test \
  85. "upload" \
  86. "put $DATA $COPY" \
  87. "" \
  88. "cmp $DATA $COPY" \
  89. "test ! -f $COPY"
  90. ro_test \
  91. "setstat" \
  92. "chmod 0700 $COPY" \
  93. "touch $COPY; chmod 0400 $COPY" \
  94. "test -x $COPY" \
  95. "test ! -x $COPY"
  96. ro_test \
  97. "rm" \
  98. "rm $COPY" \
  99. "touch $COPY" \
  100. "test ! -f $COPY" \
  101. "test -f $COPY"
  102. ro_test \
  103. "mkdir" \
  104. "mkdir ${COPY}.dd" \
  105. "" \
  106. "test -d ${COPY}.dd" \
  107. "test ! -d ${COPY}.dd"
  108. ro_test \
  109. "rmdir" \
  110. "rmdir ${COPY}.dd" \
  111. "mkdir ${COPY}.dd" \
  112. "test ! -d ${COPY}.dd" \
  113. "test -d ${COPY}.dd"
  114. ro_test \
  115. "posix-rename" \
  116. "rename $COPY ${COPY}.1" \
  117. "touch $COPY" \
  118. "test -f ${COPY}.1 -a ! -f $COPY" \
  119. "test -f $COPY -a ! -f ${COPY}.1"
  120. ro_test \
  121. "oldrename" \
  122. "rename -l $COPY ${COPY}.1" \
  123. "touch $COPY" \
  124. "test -f ${COPY}.1 -a ! -f $COPY" \
  125. "test -f $COPY -a ! -f ${COPY}.1"
  126. ro_test \
  127. "symlink" \
  128. "ln -s $COPY ${COPY}.1" \
  129. "touch $COPY" \
  130. "test -h ${COPY}.1" \
  131. "test ! -h ${COPY}.1"
  132. ro_test \
  133. "hardlink" \
  134. "ln $COPY ${COPY}.1" \
  135. "touch $COPY" \
  136. "test -f ${COPY}.1" \
  137. "test ! -f ${COPY}.1"
  138. # Test explicit permissions
  139. perm_test \
  140. "open" \
  141. "realpath,stat,lstat,read,close" \
  142. "get $DATA $COPY" \
  143. "" \
  144. "cmp $DATA $COPY" \
  145. "! cmp $DATA $COPY 2>/dev/null"
  146. perm_test \
  147. "read" \
  148. "realpath,stat,lstat,open,close" \
  149. "get $DATA $COPY" \
  150. "" \
  151. "cmp $DATA $COPY" \
  152. "! cmp $DATA $COPY 2>/dev/null"
  153. perm_test \
  154. "write" \
  155. "realpath,stat,lstat,open,close" \
  156. "put $DATA $COPY" \
  157. "" \
  158. "cmp $DATA $COPY" \
  159. "! cmp $DATA $COPY 2>/dev/null"
  160. perm_test \
  161. "lstat" \
  162. "realpath,stat,open,read,close" \
  163. "get $DATA $COPY" \
  164. "" \
  165. "cmp $DATA $COPY" \
  166. "! cmp $DATA $COPY 2>/dev/null"
  167. perm_test \
  168. "opendir" \
  169. "realpath,readdir,stat,lstat" \
  170. "ls -ln $OBJ"
  171. perm_test \
  172. "readdir" \
  173. "realpath,opendir,stat,lstat" \
  174. "ls -ln $OBJ"
  175. perm_test \
  176. "setstat" \
  177. "realpath,stat,lstat" \
  178. "chmod 0700 $COPY" \
  179. "touch $COPY; chmod 0400 $COPY" \
  180. "test -x $COPY" \
  181. "test ! -x $COPY"
  182. perm_test \
  183. "remove" \
  184. "realpath,stat,lstat" \
  185. "rm $COPY" \
  186. "touch $COPY" \
  187. "test ! -f $COPY" \
  188. "test -f $COPY"
  189. perm_test \
  190. "mkdir" \
  191. "realpath,stat,lstat" \
  192. "mkdir ${COPY}.dd" \
  193. "" \
  194. "test -d ${COPY}.dd" \
  195. "test ! -d ${COPY}.dd"
  196. perm_test \
  197. "rmdir" \
  198. "realpath,stat,lstat" \
  199. "rmdir ${COPY}.dd" \
  200. "mkdir ${COPY}.dd" \
  201. "test ! -d ${COPY}.dd" \
  202. "test -d ${COPY}.dd"
  203. perm_test \
  204. "posix-rename" \
  205. "realpath,stat,lstat" \
  206. "rename $COPY ${COPY}.1" \
  207. "touch $COPY" \
  208. "test -f ${COPY}.1 -a ! -f $COPY" \
  209. "test -f $COPY -a ! -f ${COPY}.1"
  210. perm_test \
  211. "rename" \
  212. "realpath,stat,lstat" \
  213. "rename -l $COPY ${COPY}.1" \
  214. "touch $COPY" \
  215. "test -f ${COPY}.1 -a ! -f $COPY" \
  216. "test -f $COPY -a ! -f ${COPY}.1"
  217. perm_test \
  218. "symlink" \
  219. "realpath,stat,lstat" \
  220. "ln -s $COPY ${COPY}.1" \
  221. "touch $COPY" \
  222. "test -h ${COPY}.1" \
  223. "test ! -h ${COPY}.1"
  224. perm_test \
  225. "hardlink" \
  226. "realpath,stat,lstat" \
  227. "ln $COPY ${COPY}.1" \
  228. "touch $COPY" \
  229. "test -f ${COPY}.1" \
  230. "test ! -f ${COPY}.1"
  231. perm_test \
  232. "statvfs" \
  233. "realpath,stat,lstat" \
  234. "df /"
  235. # XXX need good tests for:
  236. # fstat
  237. # fsetstat
  238. # realpath
  239. # stat
  240. # readlink
  241. # fstatvfs
  242. rm -rf ${COPY} ${COPY}.1 ${COPY}.dd