Damien Miller | 4cd5fba | 2008-03-12 23:59:43 +1100 | [diff] [blame] | 1 | # $OpenBSD: sftp-cmds.sh,v 1.9 2007/12/12 05:04:03 djm Exp $ |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 2 | # Placed in the Public Domain. |
| 3 | |
| 4 | # XXX - TODO: |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 5 | # - chmod / chown / chgrp |
| 6 | # - -p flag for get & put |
| 7 | |
| 8 | tid="sftp commands" |
| 9 | |
Darren Tucker | c2c4223 | 2003-09-04 14:54:39 +1000 | [diff] [blame] | 10 | DATA=/bin/ls${EXEEXT} |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 11 | COPY=${OBJ}/copy |
Darren Tucker | 933e4f1 | 2003-09-04 14:26:31 +1000 | [diff] [blame] | 12 | # test that these files are readable! |
| 13 | for i in `(cd /bin;echo l*)` |
| 14 | do |
| 15 | if [ -r $i ]; then |
| 16 | GLOBFILES="$GLOBFILES $i" |
| 17 | fi |
| 18 | done |
| 19 | |
Tim Rice | 23ee359 | 2003-09-11 22:32:17 -0700 | [diff] [blame] | 20 | if have_prog uname |
Darren Tucker | e7f7580 | 2003-09-07 09:32:58 +1000 | [diff] [blame] | 21 | then |
| 22 | case `uname` in |
| 23 | CYGWIN*) |
| 24 | os=cygwin |
| 25 | ;; |
| 26 | *) |
| 27 | os=`uname` |
| 28 | ;; |
| 29 | esac |
| 30 | else |
| 31 | os="unknown" |
| 32 | fi |
| 33 | |
Darren Tucker | cca1bdb | 2003-07-19 20:11:54 +1000 | [diff] [blame] | 34 | # Path with embedded quote |
| 35 | QUOTECOPY=${COPY}".\"blah\"" |
| 36 | QUOTECOPY_ARG=${COPY}'.\"blah\"' |
Damien Miller | ce0e60e | 2007-10-26 14:54:12 +1000 | [diff] [blame] | 37 | # File with spaces |
| 38 | SPACECOPY="${COPY} this has spaces.txt" |
| 39 | SPACECOPY_ARG="${COPY}\ this\ has\ spaces.txt" |
| 40 | # File with glob metacharacters |
| 41 | GLOBMETACOPY="${COPY} [metachar].txt" |
Darren Tucker | cca1bdb | 2003-07-19 20:11:54 +1000 | [diff] [blame] | 42 | |
Tim Rice | 1eb40cf | 2003-09-09 08:35:20 -0700 | [diff] [blame] | 43 | rm -rf ${COPY} ${COPY}.1 ${COPY}.2 ${COPY}.dd ${COPY}.dd2 |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 44 | mkdir ${COPY}.dd |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 45 | |
| 46 | verbose "$tid: lls" |
Tim Rice | 5143043 | 2008-03-14 10:39:17 -0700 | [diff] [blame] | 47 | (echo "lcd ${OBJ}" ; echo "lls") | ${SFTP} -P ${SFTPSERVER} 2>&1 | \ |
Tim Rice | dce65cf | 2008-03-13 18:21:06 -0700 | [diff] [blame] | 48 | grep copy.dd >/dev/null 2>&1 || fail "lls failed" |
Damien Miller | 4cd5fba | 2008-03-12 23:59:43 +1100 | [diff] [blame] | 49 | |
| 50 | verbose "$tid: lls w/path" |
| 51 | echo "lls ${OBJ}" | ${SFTP} -P ${SFTPSERVER} 2>&1 | \ |
Tim Rice | dce65cf | 2008-03-13 18:21:06 -0700 | [diff] [blame] | 52 | grep copy.dd >/dev/null 2>&1 || fail "lls w/path failed" |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 53 | |
| 54 | verbose "$tid: ls" |
| 55 | echo "ls ${OBJ}" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 56 | || fail "ls failed" |
| 57 | # XXX always successful |
| 58 | |
| 59 | verbose "$tid: shell" |
| 60 | echo "!echo hi there" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 61 | || fail "shell failed" |
| 62 | # XXX always successful |
| 63 | |
| 64 | verbose "$tid: pwd" |
| 65 | echo "pwd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 66 | || fail "pwd failed" |
| 67 | # XXX always successful |
| 68 | |
| 69 | verbose "$tid: lpwd" |
| 70 | echo "lpwd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 71 | || fail "lpwd failed" |
| 72 | # XXX always successful |
| 73 | |
| 74 | verbose "$tid: quit" |
| 75 | echo "quit" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 76 | || fail "quit failed" |
| 77 | # XXX always successful |
| 78 | |
| 79 | verbose "$tid: help" |
| 80 | echo "help" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 81 | || fail "help failed" |
| 82 | # XXX always successful |
| 83 | |
| 84 | rm -f ${COPY} |
| 85 | verbose "$tid: get" |
| 86 | echo "get $DATA $COPY" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 87 | || fail "get failed" |
| 88 | cmp $DATA ${COPY} || fail "corrupted copy after get" |
| 89 | |
Darren Tucker | 0240ff7 | 2003-10-08 17:52:10 +1000 | [diff] [blame] | 90 | rm -f ${COPY} |
| 91 | verbose "$tid: get quoted" |
| 92 | echo "get \"$DATA\" $COPY" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 93 | || fail "get failed" |
| 94 | cmp $DATA ${COPY} || fail "corrupted copy after get" |
| 95 | |
Darren Tucker | c99a19b | 2004-04-16 17:58:28 +1000 | [diff] [blame] | 96 | if [ "$os" != "cygwin" ]; then |
Darren Tucker | 0240ff7 | 2003-10-08 17:52:10 +1000 | [diff] [blame] | 97 | rm -f ${QUOTECOPY} |
| 98 | cp $DATA ${QUOTECOPY} |
| 99 | verbose "$tid: get filename with quotes" |
Damien Miller | ce0e60e | 2007-10-26 14:54:12 +1000 | [diff] [blame] | 100 | echo "get \"$QUOTECOPY_ARG\" ${COPY}" | \ |
| 101 | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ || fail "get failed" |
Darren Tucker | 0240ff7 | 2003-10-08 17:52:10 +1000 | [diff] [blame] | 102 | cmp ${COPY} ${QUOTECOPY} || fail "corrupted copy after get with quotes" |
| 103 | rm -f ${QUOTECOPY} ${COPY} |
Darren Tucker | c99a19b | 2004-04-16 17:58:28 +1000 | [diff] [blame] | 104 | fi |
Darren Tucker | 0240ff7 | 2003-10-08 17:52:10 +1000 | [diff] [blame] | 105 | |
Damien Miller | ce0e60e | 2007-10-26 14:54:12 +1000 | [diff] [blame] | 106 | rm -f "$SPACECOPY" ${COPY} |
| 107 | cp $DATA "$SPACECOPY" |
| 108 | verbose "$tid: get filename with spaces" |
| 109 | echo "get ${SPACECOPY_ARG} ${COPY}" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 110 | || fail "get failed" |
| 111 | cmp ${COPY} "$SPACECOPY" || fail "corrupted copy after get with spaces" |
| 112 | |
| 113 | rm -f "$GLOBMETACOPY" ${COPY} |
| 114 | cp $DATA "$GLOBMETACOPY" |
| 115 | verbose "$tid: get filename with glob metacharacters" |
| 116 | echo "get \"${GLOBMETACOPY}\" ${COPY}" | \ |
| 117 | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "get failed" |
| 118 | cmp ${COPY} "$GLOBMETACOPY" || \ |
| 119 | fail "corrupted copy after get with glob metacharacters" |
| 120 | |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 121 | rm -f ${COPY}.dd/* |
| 122 | verbose "$tid: get to directory" |
| 123 | echo "get $DATA ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 124 | || fail "get failed" |
Darren Tucker | c2c4223 | 2003-09-04 14:54:39 +1000 | [diff] [blame] | 125 | cmp $DATA ${COPY}.dd/`basename $DATA` || fail "corrupted copy after get" |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 126 | |
| 127 | rm -f ${COPY}.dd/* |
| 128 | verbose "$tid: glob get to directory" |
| 129 | echo "get /bin/l* ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 130 | || fail "get failed" |
| 131 | for x in $GLOBFILES; do |
| 132 | cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after get" |
| 133 | done |
| 134 | |
| 135 | rm -f ${COPY}.dd/* |
| 136 | verbose "$tid: get to local dir" |
Darren Tucker | c2c4223 | 2003-09-04 14:54:39 +1000 | [diff] [blame] | 137 | (echo "lcd ${COPY}.dd"; echo "get $DATA" ) | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 138 | || fail "get failed" |
Darren Tucker | c2c4223 | 2003-09-04 14:54:39 +1000 | [diff] [blame] | 139 | cmp $DATA ${COPY}.dd/`basename $DATA` || fail "corrupted copy after get" |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 140 | |
| 141 | rm -f ${COPY}.dd/* |
| 142 | verbose "$tid: glob get to local dir" |
Darren Tucker | c2c4223 | 2003-09-04 14:54:39 +1000 | [diff] [blame] | 143 | (echo "lcd ${COPY}.dd"; echo "get /bin/l*") | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 144 | || fail "get failed" |
| 145 | for x in $GLOBFILES; do |
| 146 | cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after get" |
| 147 | done |
| 148 | |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 149 | rm -f ${COPY} |
| 150 | verbose "$tid: put" |
Damien Miller | ce0e60e | 2007-10-26 14:54:12 +1000 | [diff] [blame] | 151 | echo "put $DATA $COPY" | \ |
| 152 | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed" |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 153 | cmp $DATA ${COPY} || fail "corrupted copy after put" |
| 154 | |
Darren Tucker | e7f7580 | 2003-09-07 09:32:58 +1000 | [diff] [blame] | 155 | if [ "$os" != "cygwin" ]; then |
Darren Tucker | cca1bdb | 2003-07-19 20:11:54 +1000 | [diff] [blame] | 156 | rm -f ${QUOTECOPY} |
| 157 | verbose "$tid: put filename with quotes" |
Damien Miller | ce0e60e | 2007-10-26 14:54:12 +1000 | [diff] [blame] | 158 | echo "put $DATA \"$QUOTECOPY_ARG\"" | \ |
| 159 | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed" |
Darren Tucker | cca1bdb | 2003-07-19 20:11:54 +1000 | [diff] [blame] | 160 | cmp $DATA ${QUOTECOPY} || fail "corrupted copy after put with quotes" |
Darren Tucker | e7f7580 | 2003-09-07 09:32:58 +1000 | [diff] [blame] | 161 | fi |
Darren Tucker | cca1bdb | 2003-07-19 20:11:54 +1000 | [diff] [blame] | 162 | |
Damien Miller | ce0e60e | 2007-10-26 14:54:12 +1000 | [diff] [blame] | 163 | rm -f "$SPACECOPY" |
| 164 | verbose "$tid: put filename with spaces" |
| 165 | echo "put $DATA ${SPACECOPY_ARG}" | \ |
| 166 | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed" |
| 167 | cmp $DATA "$SPACECOPY" || fail "corrupted copy after put with spaces" |
| 168 | |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 169 | rm -f ${COPY}.dd/* |
| 170 | verbose "$tid: put to directory" |
| 171 | echo "put $DATA ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 172 | || fail "put failed" |
Darren Tucker | c2c4223 | 2003-09-04 14:54:39 +1000 | [diff] [blame] | 173 | cmp $DATA ${COPY}.dd/`basename $DATA` || fail "corrupted copy after put" |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 174 | |
| 175 | rm -f ${COPY}.dd/* |
| 176 | verbose "$tid: glob put to directory" |
Damien Miller | 89437ed | 2007-10-26 15:37:50 +1000 | [diff] [blame] | 177 | echo "put /bin/l? ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 178 | || fail "put failed" |
| 179 | for x in $GLOBFILES; do |
| 180 | cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after put" |
| 181 | done |
| 182 | |
| 183 | rm -f ${COPY}.dd/* |
| 184 | verbose "$tid: put to local dir" |
Darren Tucker | c2c4223 | 2003-09-04 14:54:39 +1000 | [diff] [blame] | 185 | (echo "cd ${COPY}.dd"; echo "put $DATA") | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 186 | || fail "put failed" |
Darren Tucker | c2c4223 | 2003-09-04 14:54:39 +1000 | [diff] [blame] | 187 | cmp $DATA ${COPY}.dd/`basename $DATA` || fail "corrupted copy after put" |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 188 | |
| 189 | rm -f ${COPY}.dd/* |
| 190 | verbose "$tid: glob put to local dir" |
Damien Miller | 89437ed | 2007-10-26 15:37:50 +1000 | [diff] [blame] | 191 | (echo "cd ${COPY}.dd"; echo "put /bin/l?") | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
Darren Tucker | c43362c | 2003-06-18 22:41:41 +1000 | [diff] [blame] | 192 | || fail "put failed" |
| 193 | for x in $GLOBFILES; do |
| 194 | cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after put" |
| 195 | done |
| 196 | |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 197 | verbose "$tid: rename" |
| 198 | echo "rename $COPY ${COPY}.1" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 199 | || fail "rename failed" |
| 200 | test -f ${COPY}.1 || fail "missing file after rename" |
| 201 | cmp $DATA ${COPY}.1 >/dev/null 2>&1 || fail "corrupted copy after rename" |
| 202 | |
Darren Tucker | a4040f5 | 2003-06-18 22:35:11 +1000 | [diff] [blame] | 203 | verbose "$tid: rename directory" |
Damien Miller | ce0e60e | 2007-10-26 14:54:12 +1000 | [diff] [blame] | 204 | echo "rename ${COPY}.dd ${COPY}.dd2" | \ |
| 205 | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || \ |
| 206 | fail "rename directory failed" |
Darren Tucker | a4040f5 | 2003-06-18 22:35:11 +1000 | [diff] [blame] | 207 | test -d ${COPY}.dd && fail "oldname exists after rename directory" |
| 208 | test -d ${COPY}.dd2 || fail "missing newname after rename directory" |
| 209 | |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 210 | verbose "$tid: ln" |
| 211 | echo "ln ${COPY}.1 ${COPY}.2" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "ln failed" |
Darren Tucker | 4735115 | 2003-09-04 15:24:50 +1000 | [diff] [blame] | 212 | test -h ${COPY}.2 || fail "missing file after ln" |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 213 | |
| 214 | verbose "$tid: mkdir" |
| 215 | echo "mkdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 216 | || fail "mkdir failed" |
| 217 | test -d ${COPY}.dd || fail "missing directory after mkdir" |
| 218 | |
| 219 | # XXX do more here |
| 220 | verbose "$tid: chdir" |
| 221 | echo "chdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 222 | || fail "chdir failed" |
| 223 | |
| 224 | verbose "$tid: rmdir" |
| 225 | echo "rmdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 226 | || fail "rmdir failed" |
| 227 | test -d ${COPY}.1 && fail "present directory after rmdir" |
| 228 | |
| 229 | verbose "$tid: lmkdir" |
| 230 | echo "lmkdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 231 | || fail "lmkdir failed" |
| 232 | test -d ${COPY}.dd || fail "missing directory after lmkdir" |
| 233 | |
| 234 | # XXX do more here |
| 235 | verbose "$tid: lchdir" |
| 236 | echo "lchdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ |
| 237 | || fail "lchdir failed" |
| 238 | |
Tim Rice | 1eb40cf | 2003-09-09 08:35:20 -0700 | [diff] [blame] | 239 | rm -rf ${COPY} ${COPY}.1 ${COPY}.2 ${COPY}.dd ${COPY}.dd2 |
Damien Miller | ce0e60e | 2007-10-26 14:54:12 +1000 | [diff] [blame] | 240 | rm -rf ${QUOTECOPY} "$SPACECOPY" "$GLOBMETACOPY" |
Damien Miller | 8b9cde7 | 2003-01-22 17:53:16 +1100 | [diff] [blame] | 241 | |