From d7cd61d926bff2802310b94652693a7146b56e7f Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 16 Feb 2021 13:03:16 +0100 Subject: [PATCH] wolfSCP: allow overriding GETHDR, disable functions that call snprinf according to compile options --- src/wolfscp.c | 52 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/wolfscp.c b/src/wolfscp.c index d022230..5c2f3ab 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -235,6 +235,8 @@ static int ScpSourceInit(WOLFSSH* ssh) return WS_SUCCESS; } + +#ifndef NO_SCP_TIMESTAMP /* Sends timestamp information (access, modification) to peer. * * T 0 0 @@ -271,6 +273,10 @@ static int SendScpTimestamp(WOLFSSH* ssh) return ret; } +#endif + + + /* Sends file header (mode, file name) to peer. * @@ -282,28 +288,33 @@ static int SendScpFileHeader(WOLFSSH* ssh) { int ret = WS_SUCCESS, bufSz; char buf[DEFAULT_SCP_MSG_SZ]; + char *filehdr; if (ssh == NULL) return WS_BAD_ARGUMENT; +#ifndef WSCPFILEHDR WMEMSET(buf, 0, sizeof(buf)); - WSNPRINTF(buf, sizeof(buf), "C%04o %u %s\n", ssh->scpFileMode, ssh->scpFileSz, ssh->scpFileName); - - bufSz = (int)WSTRLEN(buf); - - ret = wolfSSH_stream_send(ssh, (byte*)buf, bufSz); + filehdr = buf; +#else + filehdr = WSCPFILEHDR(ssh); + if (!filehdr) + return WS_BAD_ARGUMENT; +#endif + bufSz = (int)WSTRLEN(filehdr); + ret = wolfSSH_stream_send(ssh, (byte*)filehdr, bufSz); if (ret != bufSz) { ret = WS_FATAL_ERROR; } else { - WLOG(WS_LOG_DEBUG, "scp: sent file header: %s", buf); + WLOG(WS_LOG_DEBUG, "scp: sent file header: %s", filehdr); ret = WS_SUCCESS; } - return ret; } +#ifndef NO_SCP_ENTER_DIRECTORY /* Sends directory message to peer, length is ignored but must * be present in message format (set to 0). * @@ -336,6 +347,7 @@ static int SendScpEnterDirectory(WOLFSSH* ssh) return ret; } +#endif /* Sends end directory message to peer. * @@ -507,6 +519,7 @@ int DoScpSource(WOLFSSH* ssh) continue; +#ifndef NO_SCP_TIMESTAMP case SCP_SEND_TIMESTAMP: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_TIMESTAMP"); @@ -518,7 +531,9 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpNextState = SCP_SEND_FILE_HEADER; continue; +#endif +#ifndef NO_SCP_ENTER_DIRECTORY case SCP_SEND_ENTER_DIRECTORY: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_ENTER_DIRECTORY"); @@ -530,6 +545,7 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpNextState = SCP_TRANSFER; continue; +#endif case SCP_SEND_EXIT_DIRECTORY: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_EXIT_DIRECTORY"); @@ -554,7 +570,6 @@ int DoScpSource(WOLFSSH* ssh) ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpNextState = SCP_DONE; continue; - case SCP_SEND_FILE_HEADER: WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_FILE_HEADER"); @@ -879,7 +894,7 @@ static int GetScpFileSize(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpFileSz = atoi((char*)(buf + idx)); + ssh->scpFileSz = strtol((char *)(buf + idx), buf + spaceIdx, 10); /* restore space, increment idx to space */ buf[spaceIdx] = ' '; @@ -985,7 +1000,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpMTime = atoi((char*)(buf + idx)); + ssh->scpMTime = strtol((char*)(buf + idx), buf + spaceIdx, 10); /* restore space, increment idx past it */ buf[spaceIdx] = ' '; @@ -1017,7 +1032,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, if (ret == WS_SUCCESS) { /* replace space with newline for atoi */ buf[spaceIdx] = '\n'; - ssh->scpATime = atoi((char*)(buf + idx)); + ssh->scpATime = strtol((char*)(buf + idx), buf + spaceIdx, 10); /* restore space, increment idx past it */ buf[spaceIdx] = ' '; @@ -1035,7 +1050,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, return ret; } - +#ifndef NO_FILESYSTEM /* checks for if directory is being renamed in command * * returns WS_SUCCESS on success @@ -1043,7 +1058,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz, static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) { /* case of file, not directory */ - char buf[cmdSz + 4]; + char buf[DEFAULT_SCP_MSG_SZ]; int sz = (int)WSTRLEN(ssh->scpBasePath); int idx; @@ -1054,6 +1069,7 @@ static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) WSTRNCPY(buf, ssh->scpBasePath, cmdSz); buf[sz] = '\0'; WSTRNCAT(buf, "/..", sizeof("/..")); + idx = wolfSSH_CleanPath(ssh, buf); if (idx < 0) { return WS_FATAL_ERROR; @@ -1110,16 +1126,15 @@ static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) return WS_SUCCESS; } +#endif /* helps with checking if the base path is a directory or file * returns WS_SUCCESS on success */ static int ParseBasePathHelper(WOLFSSH* ssh, int cmdSz) { - int ret; - - ret = ScpCheckForRename(ssh, cmdSz); - + int ret = 0; #ifndef NO_FILESYSTEM + ret = ScpCheckForRename(ssh, cmdSz); { ScpSendCtx ctx; @@ -1569,7 +1584,7 @@ int wolfSSH_SCP_connect(WOLFSSH* ssh, byte* cmd) return ret; } - +#ifndef NO_WOLFSSH_CLIENT static int wolfSSH_SCP_cmd(WOLFSSH* ssh, const char* localName, const char* remoteName, byte dir) { @@ -1635,6 +1650,7 @@ int wolfSSH_SCP_from(WOLFSSH* ssh, const char* src, const char* dst) /* src is passed to the server in the scp -f command */ /* dst is used locally to fopen and write for copy from */ } +#endif /* ! NO_WOLFSSH_CLIENT */ #if !defined(WOLFSSH_SCP_USER_CALLBACKS)