diff --git a/src/internal.c b/src/internal.c index ea2326ed0..98367a901 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1980,6 +1980,9 @@ WOLFSSH* SshInit(WOLFSSH* ssh, WOLFSSH_CTX* ctx) ssh->scpFileHeaderSent = 0; ssh->scpNoProgress = 0; ssh->scpIsRecursive = 0; + ssh->scpTargetIsDir = 0; + ssh->scpRenamedRoot = 0; + ssh->scpRecvDepth = 0; ssh->scpDirection = WOLFSSH_SCP_DIR_NONE; ssh->scpDirDepth = 0; #if !defined(WOLFSSH_SCP_USER_CALLBACKS) && !defined(NO_FILESYSTEM) @@ -2097,6 +2100,10 @@ void SshResourceFree(WOLFSSH* ssh, void* heap) ssh->scpBasePathDynamic = NULL; ssh->scpBasePathSz = 0; } + if (ssh->scpRequestName) { + WFREE(ssh->scpRequestName, heap, DYNTYPE_STRING); + ssh->scpRequestName = NULL; + } #if !defined(WOLFSSH_SCP_USER_CALLBACKS) && !defined(NO_FILESYSTEM) /* free send-side dir stack from any aborted recursive transfer */ ScpSendCtxFreeDirs(ssh->fs, &ssh->scpSendCbCtx, heap); diff --git a/src/wolfscp.c b/src/wolfscp.c index 51747001d..9b26fdf1b 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -248,6 +248,16 @@ int DoScpSink(WOLFSSH* ssh) WOLFSSH_SCP_NEW_REQUEST, ssh->scpBasePath, NULL, 0, 0, 0, 0, NULL, 0, 0, wolfSSH_GetScpRecvCtx(ssh)); + + /* several files go to a directory ("scp -d -t"); refuse a + * target that is not one, as OpenSSH does, rather than write + * them all to that one name */ + if (ssh->scpTargetIsDir && ssh->scpFileReName != NULL && + ssh->scpConfirm != WS_SCP_ABORT) { + WLOG(WS_LOG_ERROR, "scp: target is not a directory"); + wolfSSH_SetScpErrorMsg(ssh, "target is not a directory"); + ssh->scpConfirm = WS_SCP_ABORT; + } continue; case SCP_RECEIVE_MESSAGE: @@ -735,6 +745,18 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpFileBufferSz - ssh->scpBufferedSz, wolfSSH_GetScpSendCtx(ssh)); + /* a newline ends the C or D record early, and the peer reads + * the rest of the name as further records */ + if (!ssh->scpFileHeaderSent && ssh->scpFileName != NULL && + (ssh->scpConfirm == WS_SCP_ENTER_DIR || + ssh->scpConfirm >= 0) && + WSTRCHR(ssh->scpFileName, '\n') != NULL) { + WLOG(WS_LOG_ERROR, "scp: file name contains a newline"); + wolfSSH_SetScpErrorMsg(ssh, + "file name contains a newline"); + ssh->scpConfirm = WS_SCP_ABORT; + } + if (ssh->scpConfirm == WS_SCP_ENTER_DIR) { ssh->scpState = SCP_SEND_ENTER_DIRECTORY; continue; @@ -1403,6 +1425,40 @@ static int GetScpFileSize(WOLFSSH* ssh, byte* buf, word32 bufSz, return ret; } +/* A record name is one path component: not empty, ".", "..", nor holding + * a '/'. Where it names the local file (local set), it also holds no + * separator of the local filesystem. */ +static int ScpCheckFileName(WOLFSSH* ssh, const char* fileName, word32 len, + byte local) +{ + word32 i; + + if (len == 0 || + (len == 1 && fileName[0] == '.') || + (len == 2 && fileName[0] == '.' && fileName[1] == '.')) { + WLOG(WS_LOG_ERROR, "scp: invalid file name component received"); + wolfSSH_SetScpErrorMsg(ssh, "invalid file name"); + return WS_SCP_BAD_MSG_E; + } + + for (i = 0; i < len; i++) { + char c = fileName[i]; + + if (c == '/' || (local && (c == '\\' +#if defined(USE_WINDOWS_API) || defined(WOLFSSL_NUCLEUS) + || c == ':' +#endif + ))) { + WLOG(WS_LOG_ERROR, "scp: invalid file name component received"); + wolfSSH_SetScpErrorMsg(ssh, "invalid file name"); + return WS_SCP_BAD_MSG_E; + } + } + + return WS_SUCCESS; +} + + /* Reads file name from beginning of string, expects string to be * null terminated. * @@ -1432,31 +1488,11 @@ static int GetScpFileName(WOLFSSH* ssh, byte* buf, word32 bufSz, ret = WS_SCP_CMD_E; if (ret == WS_SUCCESS) { - word32 i; - fileName = (const char*)(buf + idx); - if (len == 0 || - (len == 1 && fileName[0] == '.') || - (len == 2 && fileName[0] == '.' && fileName[1] == '.')) { - WLOG(WS_LOG_ERROR, "scp: invalid file name component received"); - wolfSSH_SetScpErrorMsg(ssh, "invalid file name"); - return WS_SCP_BAD_MSG_E; - } - - for (i = 0; i < len; i++) { - char c = fileName[i]; - - if (c == '/' || c == '\\' -#if defined(USE_WINDOWS_API) || defined(WOLFSSL_NUCLEUS) - || c == ':' -#endif - ) { - WLOG(WS_LOG_ERROR, "scp: invalid file name component received"); - wolfSSH_SetScpErrorMsg(ssh, "invalid file name"); - return WS_SCP_BAD_MSG_E; - } - } + ret = ScpCheckFileName(ssh, fileName, len, 1); + if (ret != WS_SUCCESS) + return ret; /* reuse the existing allocation when the name plus its terminator * fits; scpFileNameCap is the allocation size, so this is correct no @@ -1747,6 +1783,11 @@ static int ScpCheckForRename(WOLFSSH* ssh) static int ParseBasePathHelper(WOLFSSH* ssh) { int ret; + + /* a rename from an earlier transfer does not carry over */ + ssh->scpFileReName = NULL; + ssh->scpRenamedRoot = 0; + ssh->scpRecvDepth = 0; ret = ScpCheckForRename(ssh); #ifndef NO_FILESYSTEM if (ret == WS_SUCCESS) { @@ -1856,6 +1897,7 @@ int ParseScpCommand(WOLFSSH* ssh) if (ssh == NULL) return WS_BAD_ARGUMENT; + ssh->scpTargetIsDir = 0; cmd = wolfSSH_GetSessionCommand(ssh); if (cmd == NULL) ret = WS_SCP_CMD_E; @@ -1866,33 +1908,44 @@ int ParseScpCommand(WOLFSSH* ssh) while ((idx + 1 < cmdSz) && (ret == WS_SUCCESS) && !pathDone) { if (cmd[idx] == ' ' && cmd[idx + 1] == '-' && (idx + 2 < cmdSz)) { - idx = idx + 2; - - switch (cmd[idx]) { - case 'r': - ssh->scpIsRecursive = 1; - ssh->scpRequestType = WOLFSSH_SCP_RECURSIVE_REQUEST; - break; - - case 'p': - ssh->scpTimestamp = 1; - break; - - case 't': - case 'f': - ssh->scpDirection = (cmd[idx] == 't') ? - WOLFSSH_SCP_TO : WOLFSSH_SCP_FROM; - /* the rest of the command is the path, so stop - * scanning for options */ - pathDone = 1; - /* skip the option letter and the space */ - ret = ScpSetCommandPath(ssh, cmd, cmdSz, idx + 2); - if (ret == WS_SUCCESS && ssh->scpBasePath != NULL && - ssh->scpDirection == WOLFSSH_SCP_TO) { - ret = ParseBasePathHelper(ssh); - } - break; - } /* end switch */ + /* every letter of a group such as "-pr" counts */ + for (idx = idx + 2; idx < cmdSz && cmd[idx] != ' '; idx++) { + switch (cmd[idx]) { + case 'r': + ssh->scpIsRecursive = 1; + ssh->scpRequestType = + WOLFSSH_SCP_RECURSIVE_REQUEST; + break; + + case 'p': + ssh->scpTimestamp = 1; + break; + + case 'd': + ssh->scpTargetIsDir = 1; + break; + + case 't': + case 'f': + ssh->scpDirection = (cmd[idx] == 't') ? + WOLFSSH_SCP_TO : WOLFSSH_SCP_FROM; + /* the rest of the command is the path, so + * stop scanning for options */ + pathDone = 1; + break; + } /* end switch */ + } + if (pathDone) { + /* skip the space after the option group */ + ret = ScpSetCommandPath(ssh, cmd, cmdSz, idx + 1); + if (ret == WS_SUCCESS && ssh->scpBasePath != NULL && + ssh->scpDirection == WOLFSSH_SCP_TO) { + ret = ParseBasePathHelper(ssh); + } + } + /* idx is on the space ending the group, which may start + * the next one */ + continue; } idx++; } /* end while */ @@ -1938,6 +1991,7 @@ int ReceiveScpMessage(WOLFSSH* ssh) int nl; word32 idx = 0; byte* buf; + const char* name = NULL; if (ssh == NULL) return WS_BAD_ARGUMENT; @@ -1982,12 +2036,13 @@ int ReceiveScpMessage(WOLFSSH* ssh) if (wolfSSH_stream_peek(ssh, NULL, 1) > 0) { sz = wolfSSH_stream_read(ssh, buf + ssh->scpRecvMsgSz, DEFAULT_SCP_MSG_SZ - ssh->scpRecvMsgSz); - /* match the WS_CHAN_RXD branch below: return on a non-positive - * read so a hypothetical zero cannot re-loop this peek path */ - if (sz <= 0) + if (sz < 0) return sz; - ssh->scpRecvMsgSz += sz; - continue; + if (sz > 0) { + ssh->scpRecvMsgSz += sz; + continue; + } + /* a zero read is no record; poll below rather than re-peek */ } err = wolfSSH_worker(ssh, &lastChannel); @@ -1999,7 +2054,9 @@ int ReceiveScpMessage(WOLFSSH* ssh) sz = wolfSSH_ChannelIdRead(ssh, lastChannel, buf + ssh->scpRecvMsgSz, DEFAULT_SCP_MSG_SZ - ssh->scpRecvMsgSz); - if (sz <= 0) { + /* zero-length channel data reads 0, which is no record: + * keep reading until the newline */ + if (sz < 0) { return sz; } ssh->scpRecvMsgSz += sz; @@ -2021,14 +2078,12 @@ int ReceiveScpMessage(WOLFSSH* ssh) } } - /* Already at EOF, and the worker had nothing else to report. */ + /* Already at EOF, and the worker had nothing else to report. The + * id can name a channel already closed; that is no error here. */ if (err == 0) { WOLFSSH_CHANNEL* channel; channel = wolfSSH_ChannelFind(ssh, lastChannel, WS_CHANNEL_ID_SELF); - if (channel == NULL) { - ret = WS_INVALID_CHANID; - } - else if (wolfSSH_ChannelGetEof(channel)) { + if (channel != NULL && wolfSSH_ChannelGetEof(channel)) { return WS_EOF; } } @@ -2057,17 +2112,62 @@ int ReceiveScpMessage(WOLFSSH* ssh) if ((ret = GetScpFileSize(ssh, buf, sz, &idx)) != WS_SUCCESS) break; + /* a transfer not asked to recurse takes no directory */ + if (buf[0] == 'D' && !ssh->scpIsRecursive) { + WLOG(WS_LOG_ERROR, "scp: received directory without -r"); + wolfSSH_SetScpErrorMsg(ssh, "received directory without -r"); + ret = WS_SCP_BAD_MSG_E; + break; + } + + /* a root that took the local name is the whole transfer; a + * record after it would land beside the target */ + if (ssh->scpRenamedRoot && ssh->scpRecvDepth == 0) { + WLOG(WS_LOG_ERROR, "scp: record after the target"); + wolfSSH_SetScpErrorMsg(ssh, "unexpected record after target"); + ret = WS_SCP_BAD_MSG_E; + break; + } + if (ssh->scpFileReName == NULL) { ret = GetScpFileName(ssh, buf, sz, &idx); + name = ssh->scpFileName; } else { - ssh->scpFileReName = NULL; + /* the local name replaces it, but it must still be one; + * "scp -r . host:new" names the directory "." */ + name = (const char*)(buf + idx); + if (buf[0] == 'D' && WSTRCMP(name, ".") == 0) + ret = WS_SUCCESS; + else + ret = ScpCheckFileName(ssh, name, + (word32)WSTRLEN(name), 0); + /* a recursive root's entries keep their own names; without + * -r every file takes the local name, as with OpenSSH */ + if (ret == WS_SUCCESS && ssh->scpIsRecursive) { + ssh->scpFileReName = NULL; + ssh->scpRenamedRoot = 1; + } + } + + /* a single-file download takes only the file it named */ + if (ret == WS_SUCCESS && buf[0] == 'C' && !ssh->scpIsRecursive && + ssh->scpRequestName != NULL && + WSTRCMP(name, ssh->scpRequestName) != 0) { + WLOG(WS_LOG_ERROR, "scp: filename does not match request"); + wolfSSH_SetScpErrorMsg(ssh, "filename does not match request"); + ret = WS_SCP_BAD_MSG_E; } + + if (ret == WS_SUCCESS && buf[0] == 'D') + ssh->scpRecvDepth++; break; case 'E': ssh->scpMsgType = WOLFSSH_SCP_MSG_END_DIR; ssh->scpFileState = WOLFSSH_SCP_END_DIR; + if (ssh->scpRecvDepth > 0) + ssh->scpRecvDepth--; break; case SCP_CONFIRM_ERR: @@ -2411,6 +2511,107 @@ int wolfSSH_SCP_to(WOLFSSH* ssh, const char* src, const char* dst) } +/* Whether the exec command this client sent asks for recursion: an "r" in + * any word starting with "-" after the scp word (scp or .../scp) and ahead + * of a "--". A getopt server takes options after -f too, so a word is not + * known to be the path. Words before the scp word belong to a wrapper such as + * sudo. With no scp word the command is not understood, and it counts as + * recursive: a mistake that way only skips the checks, the other way it + * fails a transfer that was asked for. */ +static byte ScpCmdIsRecursive(const byte* cmd, word32 sz) +{ + word32 i = 0; + word32 end; + word32 base; + byte seenScp = 0; + + if (cmd == NULL) + return 1; + + while (i < sz) { + while (i < sz && (cmd[i] == ' ' || cmd[i] == '\t')) + i++; + if (i == sz) + break; + base = i; + for (end = i; end < sz && cmd[end] != ' ' && cmd[end] != '\t'; + end++) { + if (cmd[end] == '/') + base = end + 1; + } + + if (!seenScp) { + if (end - base == 3 && WMEMCMP(cmd + base, "scp", 3) == 0) + seenScp = 1; + } + else if (cmd[i] == '-') { + if (end - i == 2 && cmd[i + 1] == '-') + break; + for (; i < end; i++) { + if (cmd[i] == 'r') + return 1; + } + } + i = end; + } + + return seenScp ? 0 : 1; +} + + +/* Keeps the file name a single-file download from src expects: its last + * component, when src is a plain path the remote shell passes through + * unchanged. A glob, quote, variable or space can make the server send some + * other name, so then nothing is kept and the name goes unchecked. */ +static int ScpSetRequestName(WOLFSSH* ssh, const char* src) +{ + const char* base = src; + const char* p; + word32 sz; + + if (ssh->scpRequestName != NULL) { + WFREE(ssh->scpRequestName, ssh->ctx->heap, DYNTYPE_STRING); + ssh->scpRequestName = NULL; + } + + for (p = src; *p != '\0'; p++) { + byte c = (byte)*p; + + if (c == '/') + base = p + 1; + else if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || c >= 0x80 || + WSTRCHR("._+,@%=:-", c) != NULL)) + return WS_SUCCESS; + } + + sz = (word32)WSTRLEN(base); + if (sz == 0 || WSTRCMP(base, ".") == 0 || WSTRCMP(base, "..") == 0) + return WS_SUCCESS; + + ssh->scpRequestName = (char*)WMALLOC(sz + 1, ssh->ctx->heap, + DYNTYPE_STRING); + if (ssh->scpRequestName == NULL) + return WS_MEMORY_E; + WMEMCPY(ssh->scpRequestName, base, sz + 1); + + return WS_SUCCESS; +} + + +#ifdef WOLFSSH_TEST_INTERNAL +int wolfSSH_TestScpCmdIsRecursive(const char* cmd) +{ + return ScpCmdIsRecursive((const byte*)cmd, (word32)WSTRLEN(cmd)); +} + +int wolfSSH_TestScpSetRequestName(WOLFSSH* ssh, const char* src) +{ + return ScpSetRequestName(ssh, src); +} +#endif /* WOLFSSH_TEST_INTERNAL */ + + int wolfSSH_SCP_from(WOLFSSH* ssh, const char* src, const char* dst) { int ret = WS_SUCCESS; @@ -2455,6 +2656,11 @@ int wolfSSH_SCP_from(WOLFSSH* ssh, const char* src, const char* dst) if (ret == WS_SUCCESS) { ret = ParseBasePathHelper(ssh); } + if (ret == WS_SUCCESS) { + ssh->scpIsRecursive = ScpCmdIsRecursive(ssh->channelName, + ssh->channelNameSz); + ret = ScpSetRequestName(ssh, ssh->scpIsRecursive ? "" : src); + } if (ret == WS_SUCCESS) { ssh->scpState = SCP_SINK_BEGIN; ssh->scpRequestState = SCP_SINK; @@ -2893,6 +3099,24 @@ static int ScpRecvFileOpen(WOLFSSH* ssh, const char* name, WFILE** fp) } #endif /* !WOLFSSL_NUCLEUS */ +#ifdef WOLFSSL_NUCLEUS +/* Builds the cleaned basePath/fileName into out, WOLFSSH_MAX_FILENAME bytes. + * Fails rather than drop a name that does not fit. */ +static int ScpRecvNucleusPath(WOLFSSH* ssh, char* out, const char* basePath, + const char* fileName) +{ + out[0] = '\0'; + if (fileName == NULL || + WSTRNCAT(out, basePath, WOLFSSH_MAX_FILENAME) == NULL || + WSTRNCAT(out, "/", WOLFSSH_MAX_FILENAME) == NULL || + WSTRNCAT(out, fileName, WOLFSSH_MAX_FILENAME) == NULL || + wolfSSH_CleanPath(ssh, out, WOLFSSH_MAX_FILENAME) < 0) { + return WS_BUFFER_E; + } + return WS_SUCCESS; +} +#endif /* WOLFSSL_NUCLEUS */ + /* Default SCP receive callback, called by wolfSSH when application has called * wolfSSH_accept() and a new SCP request has been received for an incoming @@ -3044,11 +3268,9 @@ int wsScpRecvCallback(WOLFSSH* ssh, int state, const char* basePath, /* open file */ #ifdef WOLFSSL_NUCLEUS /* use absolute path */ - WSTRNCAT(abslut, (char*)basePath, WOLFSSH_MAX_FILENAME); - WSTRNCAT(abslut, "/", WOLFSSH_MAX_FILENAME); - WSTRNCAT(abslut, fileName, WOLFSSH_MAX_FILENAME); - wolfSSH_CleanPath(ssh, abslut, WOLFSSH_MAX_FILENAME); - if (WFOPEN(ssh->fs, &fp, abslut, "wb") != 0) { + if (ScpRecvNucleusPath(ssh, abslut, basePath, fileName) + != WS_SUCCESS || + WFOPEN(ssh->fs, &fp, abslut, "wb") != 0) { #else if (ScpRecvFileOpen(ssh, fileName, &fp) != WS_SUCCESS) { #endif @@ -3161,11 +3383,19 @@ int wsScpRecvCallback(WOLFSSH* ssh, int state, const char* basePath, if (WSTRLEN(fileName) > 0) { /* try to create new directory */ #ifdef WOLFSSL_NUCLEUS - /* get absolute path */ - WSTRNCAT(abslut, (char*)basePath, WOLFSSH_MAX_FILENAME); - WSTRNCAT(abslut, "/", WOLFSSH_MAX_FILENAME); - WSTRNCAT(abslut, fileName, WOLFSSH_MAX_FILENAME); - wolfSSH_CleanPath(ssh, abslut, WOLFSSH_MAX_FILENAME); + /* get absolute path; a name that does not fit must not + * leave scpDirDepth counting a level basePath lacks, and + * END_DIR needs room to append "/.." */ + if (ScpRecvNucleusPath(ssh, abslut, basePath, fileName) + != WS_SUCCESS || + WSTRLEN(abslut) + sizeof("/..") > + WOLFSSH_MAX_FILENAME - 1) { + WLOG(WS_LOG_ERROR, + "scp: directory path too long, abort"); + wolfSSH_SetScpErrorMsg(ssh, "unable to enter directory"); + ret = WS_SCP_ABORT; + break; + } if (WMKDIR(ssh->fs, abslut, fileMode) != 0) { /* check if directory already exists */ if (NU_Make_Dir(abslut) != NUF_EXIST) { @@ -3177,10 +3407,8 @@ int wsScpRecvCallback(WOLFSSH* ssh, int state, const char* basePath, } } - /* cd into directory */ - WSTRNCAT((char*)basePath, "/", WOLFSSH_MAX_FILENAME); - WSTRNCAT((char*)basePath, fileName, WOLFSSH_MAX_FILENAME); - wolfSSH_CleanPath(ssh, (char*)basePath, WOLFSSH_MAX_FILENAME); + /* cd into directory: abslut is the cleaned new basePath */ + WSTRNCPY((char*)basePath, abslut, WOLFSSH_MAX_FILENAME); ssh->scpDirDepth++; #else err = ScpRecvDirEnter(ssh, fileName, fileMode); @@ -3211,9 +3439,17 @@ int wsScpRecvCallback(WOLFSSH* ssh, int state, const char* basePath, /* cd out of directory */ #ifdef WOLFSSL_NUCLEUS - WSTRNCAT((char*)basePath, "/..", WOLFSSH_MAX_FILENAME - 1); - wolfSSH_CleanPath(ssh, (char*)basePath, WOLFSSH_MAX_FILENAME); + if (WSTRNCAT((char*)basePath, "/..", WOLFSSH_MAX_FILENAME - 1) + == NULL || + wolfSSH_CleanPath(ssh, (char*)basePath, + WOLFSSH_MAX_FILENAME) < 0) { + WLOG(WS_LOG_ERROR, "scp: unable to leave directory, abort"); + wolfSSH_SetScpErrorMsg(ssh, "unable to leave directory"); + ret = WS_SCP_ABORT; + } + else { ssh->scpDirDepth--; + } #else err = ScpRecvDirLeave(ssh); if (err != WS_SUCCESS) { @@ -3240,12 +3476,13 @@ int wsScpRecvCallback(WOLFSSH* ssh, int state, const char* basePath, return ret; } -/* Sets "scp: : " for the peer, path cut to fit. Returns - * WS_SCP_CONTINUE, or WS_SCP_ABORT on failure. */ +/* Sets "scp: : " for the peer, path cut to fit and control + * bytes shown as '?'. Returns WS_SCP_CONTINUE, or WS_SCP_ABORT on failure. */ static int ScpSetSkipMsg(WOLFSSH* ssh, const char* path, const char* reason) { char msg[DEFAULT_SCP_MSG_SZ - SCP_MIN_CONFIRM_SZ]; int pathSz; + int i; WLOG(WS_LOG_DEBUG, "scp: skipping %s, %s", path, reason); /* room left for the path */ @@ -3253,6 +3490,11 @@ static int ScpSetSkipMsg(WOLFSSH* ssh, const char* path, const char* reason) if (pathSz < 0) pathSz = 0; WSNPRINTF(msg, sizeof(msg), "scp: %.*s: %s", pathSz, path, reason); + /* the warning is a newline-ended record too */ + for (i = 0; msg[i] != '\0'; i++) { + if ((byte)msg[i] < 0x20 || (byte)msg[i] == 0x7F) + msg[i] = '?'; + } return (wolfSSH_SetScpErrorMsg(ssh, msg) == WS_SUCCESS) ? WS_SCP_CONTINUE : WS_SCP_ABORT; @@ -3690,7 +3932,8 @@ static int ScpFileIsFile(ScpSendCtx* ctx) * WS_NEXT_ERROR when next call to the function will cause an error * WS_SUCCESS when successfully opening a file * WS_SCP_CONTINUE when skipped with a warning: not a file or - * directory, or too large for the SCP header + * directory, too large for the SCP header, or a name holding a + * newline */ static int ScpProcessEntry(WOLFSSH* ssh, char* fileName, word64* mTime, word64* aTime, int* fileMode, word32* totalFileSz, byte* buf, @@ -3763,6 +4006,12 @@ static int ScpProcessEntry(WOLFSSH* ssh, char* fileName, word64* mTime, ret = WS_SCP_ABORT; } #endif /* WOLFSSH_HAVE_SYMLINK */ + /* a newline ends the C or D record early, and the peer reads the + * rest of the name as further records */ + if (ret == WS_SUCCESS && WSTRCHR(fileName, '\n') != NULL) { + ret = ScpSetSkipMsg(ssh, filePath, + "file name contains a newline"); + } if (ret == WS_SUCCESS) { ret = GetFileStats(ssh->fs, sendCtx, filePath, mTime, aTime, fileMode); @@ -3838,7 +4087,8 @@ static int ScpProcessEntry(WOLFSSH* ssh, char* fileName, word64* mTime, /* WS_SCP_ABORT entries (e.g. a rejected symlink) were already logged at * their source, so only the generic, unexpected-error case is noted * here to avoid a misleading second log line. */ - if (ret != WS_NEXT_ERROR && ret != WS_SCP_ABORT) { + if (ret != WS_NEXT_ERROR && ret != WS_SCP_ABORT && + ret != WS_SCP_CONTINUE) { WLOG(WS_LOG_ERROR, "scp: ret does not equal WS_NEXT_ERROR, abort"); ret = WS_SCP_ABORT; } @@ -3924,8 +4174,9 @@ static int ScpProcessEntry(WOLFSSH* ssh, char* fileName, word64* mTime, * is complete. * WS_SCP_CONTINUE - entry skipped with a warning (see * WS_CallbackScpSend); returned for - * special files and files of 4 GiB - * or more + * special files, files of 4 GiB + * or more, and names holding a + * newline * WS_SCP_ABORT - abort file transfer request * WS_BAD_FILE_E - local file open error hit * @@ -4032,6 +4283,11 @@ int wsScpSendCallback(WOLFSSH* ssh, int state, const char* peerRequest, if (ret == WS_SUCCESS) ret = ExtractFileName(peerRequest, fileName, fileNameSz); + if (ret == WS_SUCCESS && WSTRCHR(fileName, '\n') != NULL) { + ret = ScpSetSkipMsg(ssh, peerRequest, + "file name contains a newline"); + } + if (ret == WS_SUCCESS) { if (sendCtx != NULL && sendCtx->fp != NULL) { /* If it is an empty file, do not read. */ @@ -4062,9 +4318,7 @@ int wsScpSendCallback(WOLFSSH* ssh, int state, const char* peerRequest, case WOLFSSH_SCP_RECURSIVE_REQUEST: if (ScpDirStackIsEmpty(sendCtx)) { - #ifdef WOLFSSH_HAVE_SYMLINK - word32 rootLen; - #endif + word32 rootLen = 0; /* first request, keep track of request directory. Reject a * symlink root here (a trailing separator can still follow); @@ -4075,10 +4329,10 @@ int wsScpSendCallback(WOLFSSH* ssh, int state, const char* peerRequest, "scp: missing recursive root path, abort"); ret = WS_SCP_ABORT; } - #ifdef WOLFSSH_HAVE_SYMLINK - /* lstat() follows the link when the path ends in a separator, - * so check the root with any trailing separators removed */ else { + /* the root without trailing separators: lstat() follows + * a link through one, and the D record is named for the + * last component before them */ rootLen = (word32)WSTRLEN(peerRequest); while (rootLen > 1 && (peerRequest[rootLen - 1] == '/' || peerRequest[rootLen - 1] == '\\')) @@ -4093,32 +4347,42 @@ int wsScpSendCallback(WOLFSSH* ssh, int state, const char* peerRequest, else { WMEMCPY(filePath, peerRequest, rootLen); filePath[rootLen] = '\0'; - if (wIsSymlink(filePath)) { - WLOG(WS_LOG_ERROR, - "scp: refusing recursive root symlink, abort"); - wolfSSH_SetScpErrorMsg(ssh, - "unable to open file for reading"); - ret = WS_SCP_ABORT; - } } } + #ifdef WOLFSSH_HAVE_SYMLINK + if (ret == WS_SUCCESS && wIsSymlink(filePath)) { + WLOG(WS_LOG_ERROR, + "scp: refusing recursive root symlink, abort"); + wolfSSH_SetScpErrorMsg(ssh, + "unable to open file for reading"); + ret = WS_SCP_ABORT; + } #endif /* WOLFSSH_HAVE_SYMLINK */ if (ret == WS_SUCCESS) { - ret = ScpPushDir(ssh->fs, sendCtx, peerRequest, - ssh->ctx->heap); + /* get directory name from request; a root of "/" has + * none, and an empty D record name fails the peer */ + ret = ExtractFileName(filePath, fileName, fileNameSz); + if (ret == WS_SUCCESS && fileName[0] == '\0') + ret = WS_SCP_ABORT; if (ret != WS_SUCCESS) { WLOG(WS_LOG_ERROR, - "scp: error opening base directory, abort"); + "scp: error extracting directory name, abort"); + wolfSSH_SetScpErrorMsg(ssh, + "invalid directory name"); + } + else if (WSTRCHR(fileName, '\n') != NULL) { + ret = ScpSetSkipMsg(ssh, filePath, + "file name contains a newline"); } } if (ret == WS_SUCCESS) { - /* get file name from request */ - ret = ExtractFileName(peerRequest, fileName, fileNameSz); + ret = ScpPushDir(ssh->fs, sendCtx, peerRequest, + ssh->ctx->heap); if (ret != WS_SUCCESS) { WLOG(WS_LOG_ERROR, - "scp: error extracting directory name, abort"); + "scp: error opening base directory, abort"); } } @@ -4133,7 +4397,7 @@ int wsScpSendCallback(WOLFSSH* ssh, int state, const char* peerRequest, if (ret == WS_SUCCESS) { ret = WS_SCP_ENTER_DIR; - } else { + } else if (ret != WS_SCP_CONTINUE) { ret = WS_SCP_ABORT; } diff --git a/tests/api.c b/tests/api.c index b623318f1..64f15c07e 100644 --- a/tests/api.c +++ b/tests/api.c @@ -3512,8 +3512,61 @@ static void test_wolfSSH_SCP_SendRecursiveEntry(void) WREMOVE(NULL, filePath); WRMDIR(NULL, dirPath); } + +#ifndef WOLFSSH_ZEPHYR +/* A short recursive root ending in a separator, which wolfSSH_CleanPath() + * leaves alone, still names its D record; a root with no name aborts. */ +static void test_wolfSSH_SCP_SendRecursiveRootSlash(void) +{ + WOLFSSH_CTX* ctx = NULL; + WOLFSSH* ssh = NULL; + ScpSendCtx sendCtx; + char fileName[DEFAULT_SCP_FILE_NAME_SZ]; + char dirName[] = "sr"; + char dirPath[] = "sr/"; + char rootPath[] = "/"; + byte buf[256]; + word64 mTime = 0; + word64 aTime = 0; + word32 totalSz = 0; + int fileMode = 0; + + WRMDIR(NULL, dirName); + AssertIntEQ(WMKDIR(NULL, dirName, 0700), 0); + + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL)); + AssertNotNull(ssh = wolfSSH_new(ctx)); + WMEMSET(&sendCtx, 0, sizeof(sendCtx)); + WMEMSET(fileName, 0, sizeof(fileName)); + + AssertIntEQ(wsScpSendCallback(ssh, WOLFSSH_SCP_RECURSIVE_REQUEST, + dirPath, fileName, (word32)sizeof(fileName), &mTime, &aTime, + &fileMode, 0, &totalSz, buf, (word32)sizeof(buf), &sendCtx), + WS_SCP_ENTER_DIR); + AssertStrEQ(fileName, dirName); + AssertIntEQ(wsScpSendCallback(ssh, WOLFSSH_SCP_RECURSIVE_REQUEST, + dirPath, fileName, (word32)sizeof(fileName), &mTime, &aTime, + &fileMode, 0, &totalSz, buf, (word32)sizeof(buf), &sendCtx), + WS_SCP_EXIT_DIR_FINAL); + AssertNull(sendCtx.currentDir); + + AssertIntEQ(wsScpSendCallback(ssh, WOLFSSH_SCP_RECURSIVE_REQUEST, + rootPath, fileName, (word32)sizeof(fileName), &mTime, &aTime, + &fileMode, 0, &totalSz, buf, (word32)sizeof(buf), &sendCtx), + WS_SCP_ABORT); + AssertNull(sendCtx.currentDir); + + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + + WRMDIR(NULL, dirName); +} +#else +static void test_wolfSSH_SCP_SendRecursiveRootSlash(void) { ; } +#endif #else static void test_wolfSSH_SCP_SendRecursiveEntry(void) { ; } +static void test_wolfSSH_SCP_SendRecursiveRootSlash(void) { ; } #endif #if defined(WOLFSSH_SCP) && !defined(WOLFSSH_SCP_USER_CALLBACKS) && \ @@ -3605,6 +3658,126 @@ static void test_wolfSSH_SCP_SendRecursiveSkipFifo(void) WRMDIR(NULL, dirPath); } +/* A send skips a file or directory whose name holds a newline, which would + * split its C or D record, whether it is a recursive entry, a single file or + * a recursive root. A directory is not descended into, and the warning + * carries no newline of its own. */ +static void test_wolfSSH_SCP_SendRecursiveSkipNewline(void) +{ + WOLFSSH_CTX* ctx = NULL; + WOLFSSH* ssh = NULL; + ScpSendCtx sendCtx; + char fileName[DEFAULT_SCP_FILE_NAME_SZ]; + char filePath[DEFAULT_SCP_FILE_NAME_SZ]; + char badPath[DEFAULT_SCP_FILE_NAME_SZ]; + char badDir[DEFAULT_SCP_FILE_NAME_SZ]; + char fileWarn[DEFAULT_SCP_FILE_NAME_SZ + 40]; + char dirWarn[DEFAULT_SCP_FILE_NAME_SZ + 40]; + char entryName[] = "b.txt"; + char dirPath[] = "./scp_recur_nl"; + byte data[64]; + byte buf[256]; + word64 mTime = 0; + word64 aTime = 0; + word32 totalSz = 0; + word32 i; + int fileMode = 0; + int ret; + int fileWarned = 0; + int dirWarned = 0; + int sent = 0; + + for (i = 0; i < (word32)sizeof(data); i++) + data[i] = (byte)((i * 3 + 7) & 0xff); + + WSNPRINTF(filePath, sizeof(filePath), "%s/%s", dirPath, entryName); + WSNPRINTF(badPath, sizeof(badPath), "%s/a\nE\nE", dirPath); + WSNPRINTF(badDir, sizeof(badDir), "%s/d\nE", dirPath); + WSNPRINTF(fileWarn, sizeof(fileWarn), + "scp: %s/a?E?E: file name contains a newline\n", dirPath); + WSNPRINTF(dirWarn, sizeof(dirWarn), + "scp: %s/d?E: file name contains a newline\n", dirPath); + + WREMOVE(NULL, filePath); + WREMOVE(NULL, badPath); + WRMDIR(NULL, badDir); + WRMDIR(NULL, dirPath); + + AssertIntEQ(WMKDIR(NULL, dirPath, 0700), 0); + AssertIntEQ(WMKDIR(NULL, badDir, 0700), 0); + AssertIntEQ(scpStageRecurFile(badPath, data, sizeof(data)), 0); + AssertIntEQ(scpStageRecurFile(filePath, data, sizeof(data)), 0); + + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL)); + AssertNotNull(ssh = wolfSSH_new(ctx)); + WMEMSET(&sendCtx, 0, sizeof(sendCtx)); + WMEMSET(fileName, 0, sizeof(fileName)); + + AssertIntEQ(wsScpSendCallback(ssh, WOLFSSH_SCP_RECURSIVE_REQUEST, + dirPath, fileName, (word32)sizeof(fileName), &mTime, &aTime, + &fileMode, 0, &totalSz, buf, (word32)sizeof(buf), &sendCtx), + WS_SCP_ENTER_DIR); + + /* in listing order: both bad names warn, only the good file is sent, and + * the bad directory is never entered */ + for (i = 0; i < 3; i++) { + ret = wsScpSendCallback(ssh, WOLFSSH_SCP_RECURSIVE_REQUEST, + dirPath, fileName, (word32)sizeof(fileName), &mTime, &aTime, + &fileMode, 0, &totalSz, buf, (word32)sizeof(buf), &sendCtx); + if (ret == WS_SCP_CONTINUE) { + AssertNotNull(ssh->scpConfirmMsg); + if (WSTRCMP(ssh->scpConfirmMsg + 1, fileWarn) == 0) + fileWarned++; + else { + AssertStrEQ(ssh->scpConfirmMsg + 1, dirWarn); + dirWarned++; + } + } + else { + AssertIntEQ(ret, (int)sizeof(data)); + AssertStrEQ(fileName, entryName); + AssertIntEQ(totalSz, (word32)sizeof(data)); + sent++; + } + } + AssertIntEQ(fileWarned, 1); + AssertIntEQ(dirWarned, 1); + AssertIntEQ(sent, 1); + + AssertIntEQ(wsScpSendCallback(ssh, WOLFSSH_SCP_RECURSIVE_REQUEST, + dirPath, fileName, (word32)sizeof(fileName), &mTime, &aTime, + &fileMode, 0, &totalSz, buf, (word32)sizeof(buf), &sendCtx), + WS_SCP_EXIT_DIR_FINAL); + AssertNull(sendCtx.currentDir); + + /* the same names asked for as a single file or a recursive root */ + WMEMSET(&sendCtx, 0, sizeof(sendCtx)); + AssertIntEQ(wsScpSendCallback(ssh, WOLFSSH_SCP_SINGLE_FILE_REQUEST, + badPath, fileName, (word32)sizeof(fileName), &mTime, &aTime, + &fileMode, 0, &totalSz, buf, (word32)sizeof(buf), &sendCtx), + WS_SCP_CONTINUE); + AssertNull(sendCtx.fp); + AssertNotNull(ssh->scpConfirmMsg); + AssertStrEQ(ssh->scpConfirmMsg + 1, fileWarn); + + WMEMSET(&sendCtx, 0, sizeof(sendCtx)); + AssertIntEQ(wsScpSendCallback(ssh, WOLFSSH_SCP_RECURSIVE_REQUEST, + badDir, fileName, (word32)sizeof(fileName), &mTime, &aTime, + &fileMode, 0, &totalSz, buf, (word32)sizeof(buf), &sendCtx), + WS_SCP_CONTINUE); + AssertNull(sendCtx.currentDir); + AssertNotNull(ssh->scpConfirmMsg); + AssertStrEQ(ssh->scpConfirmMsg + 1, dirWarn); + + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + + WREMOVE(NULL, filePath); + WREMOVE(NULL, badPath); + WRMDIR(NULL, badDir); + WRMDIR(NULL, dirPath); +} + #ifdef WOLFSSH_HAVE_SYMLINK /* A single-file FIFO request is skipped with a warning, without blocking. */ static void test_wolfSSH_SCP_SendSingleFifo(void) @@ -3757,6 +3930,7 @@ static void test_wolfSSH_SCP_SendLargeFile(void) } #else static void test_wolfSSH_SCP_SendRecursiveSkipFifo(void) { ; } +static void test_wolfSSH_SCP_SendRecursiveSkipNewline(void) { ; } static void test_wolfSSH_SCP_SendSingleFifo(void) { ; } static void test_wolfSSH_SCP_SendLargeFile(void) { ; } #endif @@ -8623,7 +8797,7 @@ static int scpSendZeroFirst(WOLFSSH* ssh, int state, const char* peerRequest, case WOLFSSH_SCP_SINGLE_FILE_REQUEST: /* fill metadata, but hand back zero data bytes on this first call */ - WSTRNCPY(fileName, "scp_hdr_zero.txt", fileNameSz); + WSTRNCPY(fileName, "scp_hdr_src.txt", fileNameSz); if (totalFileSz != NULL) *totalFileSz = SCP_REKEY_FILE_SZ; if (mTime != NULL) *mTime = 0; if (aTime != NULL) *aTime = 0; @@ -9317,7 +9491,7 @@ static int scpSendSkipMidFile(WOLFSSH* ssh, int state, const char* peerRequest, case WOLFSSH_SCP_SINGLE_FILE_REQUEST: if (bufSz < SCP_REKEY_FILE_SZ / 2) return WS_SCP_ABORT; - WSTRNCPY(fileName, "scp_midfile.txt", fileNameSz); + WSTRNCPY(fileName, "scp_midskip_src.txt", fileNameSz); *totalFileSz = SCP_REKEY_FILE_SZ; *mTime = 0; *aTime = 0; @@ -11930,7 +12104,9 @@ int wolfSSH_ApiTest(int argc, char** argv) test_wolfSSH_SCP_CB(); test_wolfSSH_SCP_SendSymlinkReject(); test_wolfSSH_SCP_SendRecursiveEntry(); + test_wolfSSH_SCP_SendRecursiveRootSlash(); test_wolfSSH_SCP_SendRecursiveSkipFifo(); + test_wolfSSH_SCP_SendRecursiveSkipNewline(); test_wolfSSH_SCP_SendSingleFifo(); test_wolfSSH_SCP_SendLargeFile(); test_wolfSSH_SCP_ReKey(); diff --git a/tests/regress.c b/tests/regress.c index 923981c89..469d1b094 100644 --- a/tests/regress.c +++ b/tests/regress.c @@ -9977,6 +9977,517 @@ static void TestAcceptDivertMatchesScpCommandToken(void) } } + +/* Zero-length channel data is well-formed but carries no SCP record. The + * record read must wait for the newline rather than report the empty read + * as a record, which the sink would dispatch on a stale or zero (C) type. */ +static void TestScpZeroLengthDataIsNoRecord(void) +{ + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + byte in[128]; + word32 inSz; + + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + + harness.ssh->scpIsRecursive = 1; + + inSz = BuildChannelDataPacket(channel->channel, "", in, sizeof(in)); + inSz += BuildChannelDataPacket(channel->channel, "D0755 0 sub\n", + in + inSz, sizeof(in) - inSz); + RepointHarnessInput(&harness, in, inSz); + + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_DIR); + AssertNotNull(harness.ssh->scpFileName); + AssertStrEQ(harness.ssh->scpFileName, "sub"); + AssertIntEQ(harness.ssh->scpRecvMsgSz, 0); + + FreeChannelOpenHarness(&harness); +} + + +/* A worker pass that handles no channel data reports the last receive id, + * which can name a channel already closed. A record read after it must not + * return that as an error, as E and warning records set no status of their + * own. */ +static void TestScpStaleChannelIdIsNoError(void) +{ + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + byte payload[16]; + byte in[128]; + word32 idx; + word32 inSz; + + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->lastRxId = channel->channel + 1; + + idx = AppendString(payload, sizeof(payload), 0, ""); + inSz = WrapPacket(MSGID_IGNORE, payload, idx, in, sizeof(in)); + inSz += BuildChannelDataPacket(channel->channel, "E\n", + in + inSz, sizeof(in) - inSz); + RepointHarnessInput(&harness, in, inSz); + + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_END_DIR); + AssertIntEQ(harness.ssh->scpRecvMsgSz, 0); + + FreeChannelOpenHarness(&harness); +} + + +/* When the local name replaces the first record's, the record's own name + * still has to be a single path component, though not one the local + * filesystem could use. */ +static void TestScpRenamedRecordNameChecked(void) +{ + static char reName[] = "dst"; + static const char* bad[] = { + "C0644 5 ../x\n", "C0644 5 a/b\n", "C0644 5 ..\n", "C0644 5 \n" + }; + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + byte in[128]; + word32 inSz; + word32 i; + + for (i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) { + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpFileReName = reName; + + inSz = BuildChannelDataPacket(channel->channel, bad[i], in, + sizeof(in)); + RepointHarnessInput(&harness, in, inSz); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SCP_BAD_MSG_E); + + FreeChannelOpenHarness(&harness); + } + + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpFileReName = reName; + + inSz = BuildChannelDataPacket(channel->channel, "C0644 5 src\n", in, + sizeof(in)); + RepointHarnessInput(&harness, in, inSz); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_FILE); + AssertNotNull(harness.ssh->scpFileReName); + + FreeChannelOpenHarness(&harness); + + /* a '\\' only matters where the name becomes the local file */ + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpFileReName = reName; + + inSz = BuildChannelDataPacket(channel->channel, "C0644 5 a\\b\n", in, + sizeof(in)); + inSz += BuildChannelDataPacket(channel->channel, "C0644 5 a\\b\n", + in + inSz, sizeof(in) - inSz); + RepointHarnessInput(&harness, in, inSz); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + harness.ssh->scpFileReName = NULL; + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SCP_BAD_MSG_E); + + FreeChannelOpenHarness(&harness); + + /* "scp -r . host:new" sends its root as ".", but only a directory */ + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpFileReName = reName; + harness.ssh->scpIsRecursive = 1; + + inSz = BuildChannelDataPacket(channel->channel, "D0755 0 .\n", in, + sizeof(in)); + RepointHarnessInput(&harness, in, inSz); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_DIR); + AssertNull(harness.ssh->scpFileReName); + + FreeChannelOpenHarness(&harness); + + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpFileReName = reName; + + inSz = BuildChannelDataPacket(channel->channel, "C0644 5 .\n", in, + sizeof(in)); + RepointHarnessInput(&harness, in, inSz); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SCP_BAD_MSG_E); + + FreeChannelOpenHarness(&harness); +} + + +/* Without -r every file record takes the local name, as OpenSSH writes + * each to its target, so a second one cannot create a file of its own name + * beside it. A recursive root's entries keep theirs. */ +static void TestScpRenameKeptWithoutRecursion(void) +{ + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + byte in[128]; + word32 inSz; + int recursive; + + for (recursive = 0; recursive <= 1; recursive++) { + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpIsRecursive = (byte)recursive; + /* as ScpCheckForRename leaves it for a local name of "dst" */ + harness.ssh->scpFileName = (char*)WMALLOC(4, harness.ctx->heap, + DYNTYPE_STRING); + AssertNotNull(harness.ssh->scpFileName); + WMEMCPY(harness.ssh->scpFileName, "dst", 4); + harness.ssh->scpFileNameSz = 3; + harness.ssh->scpFileNameCap = 4; + harness.ssh->scpFileReName = harness.ssh->scpFileName; + + inSz = BuildChannelDataPacket(channel->channel, + recursive ? "D0755 0 want\n" : "C0644 5 want\n", + in, sizeof(in)); + inSz += BuildChannelDataPacket(channel->channel, "C0644 5 want\n", + in + inSz, sizeof(in) - inSz); + RepointHarnessInput(&harness, in, inSz); + + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertStrEQ(harness.ssh->scpFileName, "dst"); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_FILE); + if (recursive) { + AssertNull(harness.ssh->scpFileReName); + AssertStrEQ(harness.ssh->scpFileName, "want"); + } + else { + AssertNotNull(harness.ssh->scpFileReName); + AssertStrEQ(harness.ssh->scpFileName, "dst"); + } + + FreeChannelOpenHarness(&harness); + } +} + + +/* The server sink takes a D record only when the peer's scp -t asked for + * recursion, as OpenSSH's does. */ +static void TestScpServerRejectsDirWithoutRecursion(void) +{ + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + byte in[128]; + word32 inSz; + int recursive; + + for (recursive = 0; recursive <= 1; recursive++) { + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpIsRecursive = (byte)recursive; + + inSz = BuildChannelDataPacket(channel->channel, "D0755 0 sub\n", in, + sizeof(in)); + RepointHarnessInput(&harness, in, inSz); + if (recursive) { + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_DIR); + } + else { + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SCP_BAD_MSG_E); + } + + FreeChannelOpenHarness(&harness); + } +} + + +/* Where needle first appears in buf, or NULL. */ +static const byte* MemFind(const byte* buf, word32 bufSz, const byte* needle, + word32 needleSz) +{ + word32 i; + + for (i = 0; needleSz <= bufSz && i <= bufSz - needleSz; i++) { + if (WMEMCMP(buf + i, needle, needleSz) == 0) + return buf + i; + } + + return NULL; +} + +/* An application send callback that names a file with a newline. */ +static int ScpSendNewlineNameCb(WOLFSSH* ssh, int state, const char* peerRequest, + char* fileName, word32 fileNameSz, word64* mTime, word64* aTime, + int* fileMode, word32 fileOffset, word32* totalFileSz, byte* buf, + word32 bufSz, void* ctx) +{ + (void)ssh; (void)state; (void)peerRequest; (void)mTime; (void)aTime; + (void)fileOffset; (void)ctx; + + WSTRNCPY(fileName, "a\nC0644 1 x", fileNameSz); + *fileMode = 0644; + *totalFileSz = 1; + if (bufSz < 1) + return WS_SCP_ABORT; + buf[0] = 'x'; + return 1; +} + +/* Whatever the send callback, a name holding a newline never goes out in a + * C record: the transfer stops with an error record instead. */ +static void TestScpSourceRefusesNewlineName(void) +{ + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + const char* reason = "file name contains a newline"; + byte payload[16]; + byte in[64]; + word32 idx; + word32 inSz; + + InitChannelOpenHarness(&harness, NULL, 0); + wolfSSH_SetScpSend(harness.ctx, ScpSendNewlineNameCb); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpState = SCP_SOURCE_INIT; + harness.ssh->scpRequestType = WOLFSSH_SCP_SINGLE_FILE_REQUEST; + + /* the sink's ready byte */ + idx = AppendUint32(payload, sizeof(payload), 0, channel->channel); + idx = AppendStringSz(payload, sizeof(payload), idx, "", 1); + inSz = WrapPacket(MSGID_CHANNEL_DATA, payload, idx, in, sizeof(in)); + RepointHarnessInput(&harness, in, inSz); + + AssertIntEQ(wolfSSH_TestDoScpSource(harness.ssh), WS_FATAL_ERROR); + AssertIntEQ(harness.ssh->scpFileHeaderSent, 0); + AssertNotNull(MemFind(harness.io.out, harness.io.outSz, + (const byte*)reason, (word32)WSTRLEN(reason))); + AssertNull(MemFind(harness.io.out, harness.io.outSz, + (const byte*)"C0644", 5)); + + FreeChannelOpenHarness(&harness); +} + + +/* A receive callback that takes any request and writes nothing. */ +static int ScpRecvAcceptCb(WOLFSSH* ssh, int state, const char* basePath, + const char* fileName, int fileMode, word64 mTime, word64 aTime, + word32 totalFileSz, byte* buf, word32 bufSz, word32 fileOffset, + void* ctx) +{ + (void)ssh; (void)state; (void)basePath; (void)fileName; (void)fileMode; + (void)mTime; (void)aTime; (void)totalFileSz; (void)buf; (void)bufSz; + (void)fileOffset; (void)ctx; + + return WS_SCP_CONTINUE; +} + +/* "scp -d -t" sends several files to a directory. A target that is not one + * is refused before any file, as OpenSSH's sink does, rather than each file + * overwriting the last under the one name. Without -d it is a new name. */ +static void TestScpSinkRefusesNonDirTarget(void) +{ + static char reName[] = "newname"; + const char* reason = "target is not a directory"; + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + int targetIsDir; + int ret; + + for (targetIsDir = 0; targetIsDir <= 1; targetIsDir++) { + InitChannelOpenHarness(&harness, NULL, 0); + wolfSSH_SetScpRecv(harness.ctx, ScpRecvAcceptCb); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpState = SCP_SINK_BEGIN; + harness.ssh->scpTargetIsDir = (byte)targetIsDir; + harness.ssh->scpFileReName = reName; + + ret = wolfSSH_TestDoScpSink(harness.ssh); + if (targetIsDir) { + AssertIntEQ(ret, WS_FATAL_ERROR); + AssertNotNull(MemFind(harness.io.out, harness.io.outSz, + (const byte*)reason, (word32)WSTRLEN(reason))); + } + else { + /* the go-ahead is out, and the sink waits for a record */ + (void)ret; + AssertIntEQ(harness.ssh->error, WS_WANT_READ); + AssertNull(MemFind(harness.io.out, harness.io.outSz, + (const byte*)reason, (word32)WSTRLEN(reason))); + } + + FreeChannelOpenHarness(&harness); + } +} + + +/* A recursive root that took the local name is the whole transfer, as + * OpenSSH's sink fails a later top-level record; one written by its own + * name would land beside the target. Into a directory target it is taken. */ +static void TestScpRecordAfterRenamedRootRefused(void) +{ + static const char* recs[] = { + "D0755 0 d\n", "C0644 3 in\n", "E\n", "C0644 4 .bashrc\n" + }; + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + byte in[256]; + word32 inSz; + word32 i; + int renamed; + + for (renamed = 0; renamed <= 1; renamed++) { + InitChannelOpenHarness(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpIsRecursive = 1; + if (renamed) { + /* as ScpCheckForRename leaves it for a local name of "new" */ + harness.ssh->scpFileName = (char*)WMALLOC(4, harness.ctx->heap, + DYNTYPE_STRING); + AssertNotNull(harness.ssh->scpFileName); + WMEMCPY(harness.ssh->scpFileName, "new", 4); + harness.ssh->scpFileNameSz = 3; + harness.ssh->scpFileNameCap = 4; + harness.ssh->scpFileReName = harness.ssh->scpFileName; + } + + inSz = 0; + for (i = 0; i < sizeof(recs) / sizeof(recs[0]); i++) { + inSz += BuildChannelDataPacket(channel->channel, recs[i], + in + inSz, sizeof(in) - inSz); + } + RepointHarnessInput(&harness, in, inSz); + + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertStrEQ(harness.ssh->scpFileName, "in"); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_END_DIR); + AssertIntEQ(ReceiveScpMessage(harness.ssh), + renamed ? WS_SCP_BAD_MSG_E : WS_SUCCESS); + + FreeChannelOpenHarness(&harness); + } +} + + +#ifndef NO_WOLFSSH_CLIENT +/* A client whose exec command did not ask for recursion did not request a + * D record; a file record still passes. */ +static void TestScpClientRejectsDirWithoutRecursion(void) +{ + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + byte in[128]; + word32 inSz; + + InitChannelOpenHarnessClient(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + + inSz = BuildChannelDataPacket(channel->channel, "D0700 0 .ssh\n", in, + sizeof(in)); + inSz += BuildChannelDataPacket(channel->channel, "C0644 5 file\n", + in + inSz, sizeof(in) - inSz); + RepointHarnessInput(&harness, in, inSz); + + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SCP_BAD_MSG_E); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_FILE); + AssertStrEQ(harness.ssh->scpFileName, "file"); + + FreeChannelOpenHarness(&harness); + + /* a recursive download takes it */ + InitChannelOpenHarnessClient(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + harness.ssh->scpIsRecursive = 1; + + inSz = BuildChannelDataPacket(channel->channel, "D0700 0 sub\n", in, + sizeof(in)); + RepointHarnessInput(&harness, in, inSz); + AssertIntEQ(ReceiveScpMessage(harness.ssh), WS_SUCCESS); + AssertIntEQ(harness.ssh->scpMsgType, WOLFSSH_SCP_MSG_DIR); + AssertStrEQ(harness.ssh->scpFileName, "sub"); + + FreeChannelOpenHarness(&harness); +} + + +/* A single-file download takes only file records named as it asked, + * whether or not the local name replaces the record's. A recursive one + * takes any. */ +static void TestScpClientRejectsUnrequestedName(void) +{ + static char reName[] = "dst"; + static const struct { + const char* rec; + int reName; + int recursive; + int ret; + } cases[] = { + { "C0644 5 want\n", 0, 0, WS_SUCCESS }, + { "C0644 5 .bashrc\n", 0, 0, WS_SCP_BAD_MSG_E }, + { "C0644 5 want\n", 1, 0, WS_SUCCESS }, + { "C0644 5 .bashrc\n", 1, 0, WS_SCP_BAD_MSG_E }, + { "C0644 5 .bashrc\n", 0, 1, WS_SUCCESS } + }; + ChannelOpenHarness harness; + WOLFSSH_CHANNEL* channel; + byte in[128]; + word32 inSz; + word32 i; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + InitChannelOpenHarnessClient(&harness, NULL, 0); + channel = SeedUnconfirmedChannel(&harness); + AssertIntEQ(ChannelUpdatePeer(channel, 5, 1024, 1024), WS_SUCCESS); + channel->openConfirmed = 1; + AssertIntEQ(wolfSSH_TestScpSetRequestName(harness.ssh, "/r/want"), + WS_SUCCESS); + harness.ssh->scpIsRecursive = (byte)cases[i].recursive; + if (cases[i].reName) + harness.ssh->scpFileReName = reName; + + inSz = BuildChannelDataPacket(channel->channel, cases[i].rec, in, + sizeof(in)); + RepointHarnessInput(&harness, in, inSz); + AssertIntEQ(ReceiveScpMessage(harness.ssh), cases[i].ret); + + FreeChannelOpenHarness(&harness); + } +} +#endif /* !NO_WOLFSSH_CLIENT */ + #endif /* WOLFSSH_SCP */ /* The grant an application reads through the public accessor is the one the @@ -25465,6 +25976,18 @@ int main(int argc, char** argv) TestAcceptDivertMatchesScpCommandToken(); TestAcceptDivertNeedsScpGrant(); TestAcceptDivertNeedsScpGrantAlone(); + TestScpZeroLengthDataIsNoRecord(); + TestScpStaleChannelIdIsNoError(); + TestScpRenamedRecordNameChecked(); + TestScpRenameKeptWithoutRecursion(); + TestScpSinkRefusesNonDirTarget(); + TestScpRecordAfterRenamedRootRefused(); + TestScpServerRejectsDirWithoutRecursion(); + TestScpSourceRefusesNewlineName(); +#ifndef NO_WOLFSSH_CLIENT + TestScpClientRejectsDirWithoutRecursion(); + TestScpClientRejectsUnrequestedName(); +#endif #endif TestSecondSessionChannelRejected(); TestUsernameChangeDisconnects(); diff --git a/tests/unit.c b/tests/unit.c index 4cccbbac2..3f8489a84 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -5284,6 +5284,16 @@ static int test_ScpParseCommand(void) if (result == 0) result = ScpParseCommandCase("scp -f ./x -r", WS_SUCCESS, WOLFSSH_SCP_FROM, "./x -r", NULL, 0); + /* every letter of an option group counts */ + if (result == 0) + result = ScpParseCommandCase("scp -pr -f /tmp/a", WS_SUCCESS, + WOLFSSH_SCP_FROM, "/tmp/a", NULL, 1); + if (result == 0) + result = ScpParseCommandCase("scp -rf /tmp/a", WS_SUCCESS, + WOLFSSH_SCP_FROM, "/tmp/a", NULL, 1); + if (result == 0) + result = ScpParseCommandCase("scp -pt -- -file", WS_SUCCESS, + WOLFSSH_SCP_TO, ".", "-file", 0); /* "--" before a path starting with "-", as OpenSSH sends it */ if (result == 0) result = ScpParseCommandCase("scp -r -f -- -x", WS_SUCCESS, @@ -5533,6 +5543,108 @@ static int test_ScpMakeCmd(void) return result; } + + +/* The client sink takes D records only when its own exec command asked for + * recursion, so the scan has to find "r" among scp's options, never in the + * path or a wrapper's options. A command it cannot read counts as recursive, + * which only skips the checks. */ +static int test_ScpCmdIsRecursive(void) +{ + static const struct { + const char* cmd; + int recursive; + } cases[] = { + { "scp -f /tmp/a", 0 }, + { "scp -r -f /tmp/a", 1 }, + { "scp -p -r -f dir", 1 }, + { "scp -rf dir", 1 }, + { "scp -f -r dir", 1 }, /* getopt reads past -f */ + { "sudo scp -r -f dir", 1 }, + { "scp\t-r\t-f dir", 1 }, + { "scp -f -- -r", 0 }, + { "scp -- -r", 0 }, + { "scp -t /tmp/r", 0 }, + { "scp /tmp/-r", 0 }, + { "scp", 0 }, + { "/usr/bin/scp -r -f d", 1 }, + { "sudo --preserve-env scp -f /tmp/a", 0 }, + { "sudo -- scp -r -f dir", 1 }, + { "sudo -u root scp -f /tmp/a", 0 }, + { "sudo -r scp -f /tmp/a", 0 }, + { "scp -f /tmp/scp", 0 }, + { "nice -n 5 sh -c \"scp -f a\"", 1 }, /* not understood */ + { "", 1 } + }; + word32 i; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + if (wolfSSH_TestScpCmdIsRecursive(cases[i].cmd) != cases[i].recursive) + return -4883; + } + + return 0; +} + + +/* A download checks file names only against a plain source path, since the + * remote shell can rewrite anything else into names the client cannot + * predict. */ +static int test_ScpSetRequestName(void) +{ + static const struct { + const char* src; + const char* name; /* NULL: no check */ + } cases[] = { + { "/tmp/a.txt", "a.txt" }, + { "a.txt", "a.txt" }, + { "d-1/v2,x@y=z", "v2,x@y=z" }, + { "/tmp/*.txt", NULL }, + { "/tmp/a?", NULL }, + { "/tmp/{a,b}", NULL }, + { "/tmp/a b", NULL }, + { "~/a", NULL }, + { "$HOME/a", NULL }, + { "'a'", NULL }, + { "a\\b", NULL }, + { "dir/", NULL }, + { "/tmp/.", NULL }, + { "..", NULL }, + { "", NULL } + }; + WOLFSSH_CTX* ctx; + WOLFSSH* ssh; + int result = 0; + word32 i; + + ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL); + if (ctx == NULL) + return -4884; + ssh = wolfSSH_new(ctx); + if (ssh == NULL) { + wolfSSH_CTX_free(ctx); + return -4884; + } + + for (i = 0; result == 0 && i < sizeof(cases) / sizeof(cases[0]); i++) { + /* start from a kept name, so a NULL case shows it cleared */ + if (wolfSSH_TestScpSetRequestName(ssh, "keep") != WS_SUCCESS || + ssh->scpRequestName == NULL) + result = -4885; + else if (wolfSSH_TestScpSetRequestName(ssh, cases[i].src) + != WS_SUCCESS) + result = -4885; + else if (cases[i].name == NULL && ssh->scpRequestName != NULL) + result = -4886; + else if (cases[i].name != NULL && (ssh->scpRequestName == NULL || + WSTRCMP(ssh->scpRequestName, cases[i].name) != 0)) + result = -4887; + } + + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + return result; +} #endif /* !NO_WOLFSSH_CLIENT */ #endif /* WOLFSSH_TEST_INTERNAL && WOLFSSH_SCP */ @@ -31157,6 +31269,14 @@ int wolfSSH_UnitTest(int argc, char** argv) printf("ScpMakeCmd: %s (result=%d)\n", (unitResult == 0 ? "SUCCESS" : "FAILED"), unitResult); testResult = testResult || (unitResult != 0); + unitResult = test_ScpCmdIsRecursive(); + printf("ScpCmdIsRecursive: %s (result=%d)\n", + (unitResult == 0 ? "SUCCESS" : "FAILED"), unitResult); + testResult = testResult || (unitResult != 0); + unitResult = test_ScpSetRequestName(); + printf("ScpSetRequestName: %s (result=%d)\n", + (unitResult == 0 ? "SUCCESS" : "FAILED"), unitResult); + testResult = testResult || (unitResult != 0); #endif unitResult = test_ScpSinkEmptyFileAfterSource(); printf("ScpSinkEmptyFileAfterSource: %s\n", diff --git a/wolfssh/internal.h b/wolfssh/internal.h index 2bb9fa7a1..d3bd49e65 100644 --- a/wolfssh/internal.h +++ b/wolfssh/internal.h @@ -1361,7 +1361,12 @@ struct WOLFSSH { /* alter base path instead of using chdir */ char* scpBasePathDynamic; /* dynamic base path */ word32 scpBasePathSz; + char* scpRequestName; /* file name a client download expects, + * dynamic; NULL when not checked */ byte scpIsRecursive; /* recursive transfer requested */ + byte scpTargetIsDir; /* scp -d: the target must be a directory */ + byte scpRenamedRoot; /* sink: a recursive root took the local + * name */ byte scpRequestType; /* directory or single file */ byte scpMsgType; byte scpPeerWarned; /* sink read a warning record */ @@ -1388,6 +1393,7 @@ struct WOLFSSH { byte scpNoProgress; /* send callback already handed back 0 bytes * with file data still outstanding */ word32 scpDirDepth; /* SCP nested NEW_DIR depth below base path */ + word32 scpRecvDepth; /* sink: D records not yet closed by an E */ #ifdef WOLFSSL_NUCLEUS int scpFd; /* Nucleus fd for the default SCP receive cb */ #endif @@ -2290,6 +2296,9 @@ enum WS_MessageIdLimits { #ifndef NO_WOLFSSH_CLIENT WOLFSSH_API int wolfSSH_TestScpMakeCmd(const char* name, char dir, char** cmd, void* heap); + WOLFSSH_API int wolfSSH_TestScpCmdIsRecursive(const char* cmd); + WOLFSSH_API int wolfSSH_TestScpSetRequestName(WOLFSSH* ssh, + const char* src); #endif #endif /* WOLFSSH_SCP */ #ifndef WOLFSSH_NO_DH diff --git a/wolfssh/wolfscp.h b/wolfssh/wolfscp.h index b08caab47..582bab833 100644 --- a/wolfssh/wolfscp.h +++ b/wolfssh/wolfscp.h @@ -147,7 +147,11 @@ typedef int (*WS_CallbackScpRecv)(WOLFSSH* ssh, int state, * * WS_SCP_ABORT, or any other negative error, sends the message as an error * record, and that code becomes the session error. Mid-file the transfer is - * dropped instead. */ + * dropped instead. + * + * A name holding a newline would split its record, so the library ends the + * transfer with an error record rather than send it; the default callback + * skips such an entry instead. */ typedef int (*WS_CallbackScpSend)(WOLFSSH* ssh, int state, const char* peerRequest, char* fileName, word32 fileNameSz, word64* mTime, @@ -177,7 +181,18 @@ WOLFSSH_API int wolfSSH_SCP_to(WOLFSSH* ssh, const char* src, /* As the receiving side, here and in wolfSSH_SCP_accept(), a warning record * from the peer does not stop the transfer: the entries after it are still * written, then the transfer fails with WS_FATAL_ERROR, as OpenSSH scp exits - * non-zero. An error record fails it at once. */ + * non-zero. An error record fails it at once. A directory record fails the + * transfer unless the scp command carries -r: the peer's for + * wolfSSH_SCP_accept(), and for wolfSSH_SCP_from() the one it sends or one + * given earlier to wolfSSH_SCP_connect(). In that earlier command only + * options after the scp word count, and one with no scp word as its own + * word (scp or .../scp) is taken to carry -r. Without -r, when src is a plain + * path (no glob, quote, variable or space), a file record named other than + * its last component fails the download; give an earlier command the same + * src. Without -r, a target that is not a directory takes every file + * record, as with OpenSSH; with -r, a root record that takes its name is the + * only record at the top. A peer's scp -d -t, which sends several files, + * fails at once when its target is not an existing directory. */ WOLFSSH_API int wolfSSH_SCP_from(WOLFSSH* ssh, const char* src, const char* dst); /* Server side. Drives an SCP transfer on a channel whose "exec scp ..."