scp.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # $OpenBSD: scp.sh,v 1.11 2019/07/19 03:45:44 djm Exp $
  2. # Placed in the Public Domain.
  3. tid="scp"
  4. #set -x
  5. # Figure out if diff understands "-N"
  6. if diff -N ${SRC}/scp.sh ${SRC}/scp.sh 2> /dev/null; then
  7. DIFFOPT="-rN"
  8. else
  9. DIFFOPT="-r"
  10. fi
  11. COPY2=${OBJ}/copy2
  12. DIR=${COPY}.dd
  13. DIR2=${COPY}.dd2
  14. SRC=$(dirname ${SCRIPT})
  15. cp ${SRC}/scp-ssh-wrapper.sh ${OBJ}/scp-ssh-wrapper.scp
  16. chmod 755 ${OBJ}/scp-ssh-wrapper.scp
  17. scpopts="-q -S ${OBJ}/scp-ssh-wrapper.scp"
  18. export SCP # used in scp-ssh-wrapper.scp
  19. scpclean()
  20. {
  21. rm -rf ${COPY} ${COPY2} ${DIR} ${DIR2}
  22. mkdir ${DIR} ${DIR2}
  23. chmod 755 ${DIR} ${DIR2}
  24. }
  25. verbose "$tid: simple copy local file to local file"
  26. scpclean
  27. $SCP $scpopts ${DATA} ${COPY} || fail "copy failed"
  28. cmp ${DATA} ${COPY} || fail "corrupted copy"
  29. verbose "$tid: simple copy local file to remote file"
  30. scpclean
  31. $SCP $scpopts ${DATA} somehost:${COPY} || fail "copy failed"
  32. cmp ${DATA} ${COPY} || fail "corrupted copy"
  33. verbose "$tid: simple copy remote file to local file"
  34. scpclean
  35. $SCP $scpopts somehost:${DATA} ${COPY} || fail "copy failed"
  36. cmp ${DATA} ${COPY} || fail "corrupted copy"
  37. verbose "$tid: simple copy local file to remote dir"
  38. scpclean
  39. cp ${DATA} ${COPY}
  40. $SCP $scpopts ${COPY} somehost:${DIR} || fail "copy failed"
  41. cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
  42. verbose "$tid: simple copy local file to local dir"
  43. scpclean
  44. cp ${DATA} ${COPY}
  45. $SCP $scpopts ${COPY} ${DIR} || fail "copy failed"
  46. cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
  47. verbose "$tid: simple copy remote file to local dir"
  48. scpclean
  49. cp ${DATA} ${COPY}
  50. $SCP $scpopts somehost:${COPY} ${DIR} || fail "copy failed"
  51. cmp ${COPY} ${DIR}/copy || fail "corrupted copy"
  52. verbose "$tid: recursive local dir to remote dir"
  53. scpclean
  54. rm -rf ${DIR2}
  55. cp ${DATA} ${DIR}/copy
  56. $SCP $scpopts -r ${DIR} somehost:${DIR2} || fail "copy failed"
  57. diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
  58. verbose "$tid: recursive local dir to local dir"
  59. scpclean
  60. rm -rf ${DIR2}
  61. cp ${DATA} ${DIR}/copy
  62. $SCP $scpopts -r ${DIR} ${DIR2} || fail "copy failed"
  63. diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
  64. verbose "$tid: recursive remote dir to local dir"
  65. scpclean
  66. rm -rf ${DIR2}
  67. cp ${DATA} ${DIR}/copy
  68. $SCP $scpopts -r somehost:${DIR} ${DIR2} || fail "copy failed"
  69. diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
  70. verbose "$tid: shell metacharacters"
  71. scpclean
  72. (
  73. cd ${DIR} &&
  74. touch '`touch metachartest`' &&
  75. $SCP $scpopts *metachar* ${DIR2} 2> /dev/null
  76. [ ! -f metachartest ]
  77. ) || fail "shell metacharacters"
  78. if [ ! -z "$SUDO" ]; then
  79. verbose "$tid: skipped file after scp -p with failed chown+utimes"
  80. scpclean
  81. cp -p ${DATA} ${DIR}/copy
  82. cp -p ${DATA} ${DIR}/copy2
  83. cp ${DATA} ${DIR2}/copy
  84. chmod 660 ${DIR2}/copy
  85. $SUDO chown root ${DIR2}/copy
  86. $SCP -p $scpopts somehost:${DIR}/\* ${DIR2} > /dev/null 2>&1
  87. $SUDO diff ${DIFFOPT} ${DIR} ${DIR2} || fail "corrupted copy"
  88. $SUDO rm ${DIR2}/copy
  89. fi
  90. for i in 0 1 2 3 4 5 6 7; do
  91. verbose "$tid: disallow bad server #$i"
  92. SCPTESTMODE=badserver_$i
  93. export DIR SCPTESTMODE
  94. scpclean
  95. $SCP $scpopts somehost:${DATA} ${DIR} > /dev/null 2> /dev/null
  96. [ -d {$DIR}/rootpathdir ] && fail "allows dir relative to root dir"
  97. [ -d ${DIR}/dotpathdir ] && fail "allows dir creation in non-recursive mode"
  98. scpclean
  99. $SCP -r $scpopts somehost:${DATA} ${DIR2} > /dev/null 2> /dev/null
  100. [ -d ${DIR}/dotpathdir ] && fail "allows dir creation outside of subdir"
  101. scpclean
  102. $SCP -pr $scpopts somehost:${DATA} ${DIR2} > /dev/null 2> /dev/null
  103. [ ! -w ${DIR2} ] && fail "allows target root attribute change"
  104. scpclean
  105. $SCP $scpopts somehost:${DATA} ${DIR2} > /dev/null 2> /dev/null
  106. [ -e ${DIR2}/extrafile ] && fail "allows unauth object creation"
  107. rm -f ${DIR2}/extrafile
  108. done
  109. verbose "$tid: detect non-directory target"
  110. scpclean
  111. echo a > ${COPY}
  112. echo b > ${COPY2}
  113. $SCP $scpopts ${DATA} ${COPY} ${COPY2}
  114. cmp ${COPY} ${COPY2} > /dev/null && fail "corrupt target"
  115. scpclean
  116. rm -f ${OBJ}/scp-ssh-wrapper.scp