wolfSCP: allow overriding GETHDR, disable functions that call snprinf

according to compile options
pull/324/head
Daniele Lacamera 2021-02-16 13:03:16 +01:00
parent 4e76f3dd72
commit d7cd61d926
1 changed files with 34 additions and 18 deletions

View File

@ -235,6 +235,8 @@ static int ScpSourceInit(WOLFSSH* ssh)
return WS_SUCCESS;
}
#ifndef NO_SCP_TIMESTAMP
/* Sends timestamp information (access, modification) to peer.
*
* T<modification_secs> 0 <access_secs> 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)