Merge pull request #324 from danielinux/nosyscall-fixes

SCP: improvements to run on embedded RTOS
pull/329/head
JacobBarthelmeh 2021-03-26 16:37:13 +07:00 committed by GitHub
commit a77bc90b75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 19 deletions

View File

@ -57,8 +57,8 @@
#endif /* WOLFSSH_NO_DEFAULT_LOGGING_CB */ #endif /* WOLFSSH_NO_DEFAULT_LOGGING_CB */
static enum wolfSSH_LogLevel logLevel = WS_LOG_DEFAULT;
#ifdef DEBUG_WOLFSSH #ifdef DEBUG_WOLFSSH
static enum wolfSSH_LogLevel logLevel = WS_LOG_DEFAULT;
static int logEnable = 0; static int logEnable = 0;
#endif #endif
@ -99,6 +99,7 @@ int wolfSSH_LogEnabled(void)
} }
#ifdef DEBUG_WOLFSSH
#ifndef WOLFSSH_NO_DEFAULT_LOGGING_CB #ifndef WOLFSSH_NO_DEFAULT_LOGGING_CB
/* log level string */ /* log level string */
static const char* GetLogStr(enum wolfSSH_LogLevel level) static const char* GetLogStr(enum wolfSSH_LogLevel level)
@ -133,7 +134,6 @@ static const char* GetLogStr(enum wolfSSH_LogLevel level)
} }
} }
void DefaultLoggingCb(enum wolfSSH_LogLevel level, const char *const msgStr) void DefaultLoggingCb(enum wolfSSH_LogLevel level, const char *const msgStr)
{ {
char timeStr[24]; char timeStr[24];
@ -176,3 +176,18 @@ void wolfSSH_Log(enum wolfSSH_LogLevel level, const char *const fmt, ...)
if (logFunction) if (logFunction)
logFunction(level, msgStr); logFunction(level, msgStr);
} }
#else
void DefaultLoggingCb(enum wolfSSH_LogLevel level, const char *const msgStr)
{
(void)level;
(void)msgStr;
}
void wolfSSH_Log(enum wolfSSH_LogLevel level, const char *const fmt, ...)
{
(void)level;
(void)fmt;
}
#endif /* DEBUG_WOLFSSH */

View File

@ -1416,8 +1416,8 @@ int wolfSSH_ReadKey_buffer(const byte* in, word32 inSz, int format,
if (format == WOLFSSH_FORMAT_SSH) { if (format == WOLFSSH_FORMAT_SSH) {
char* c; char* c;
char* last; char* last;
char* type; char* type = NULL;
char* key; char* key = NULL;
/* /*
SSH format is: SSH format is:

View File

@ -235,6 +235,7 @@ static int ScpSourceInit(WOLFSSH* ssh)
return WS_SUCCESS; return WS_SUCCESS;
} }
/* Sends timestamp information (access, modification) to peer. /* Sends timestamp information (access, modification) to peer.
* *
* T<modification_secs> 0 <access_secs> 0 * T<modification_secs> 0 <access_secs> 0
@ -272,6 +273,9 @@ static int SendScpTimestamp(WOLFSSH* ssh)
return ret; return ret;
} }
/* Sends file header (mode, file name) to peer. /* Sends file header (mode, file name) to peer.
* *
* C<mode> <length> <filename> * C<mode> <length> <filename>
@ -282,25 +286,29 @@ static int SendScpFileHeader(WOLFSSH* ssh)
{ {
int ret = WS_SUCCESS, bufSz; int ret = WS_SUCCESS, bufSz;
char buf[DEFAULT_SCP_MSG_SZ]; char buf[DEFAULT_SCP_MSG_SZ];
char *filehdr;
if (ssh == NULL) if (ssh == NULL)
return WS_BAD_ARGUMENT; return WS_BAD_ARGUMENT;
#ifndef WSCPFILEHDR
WMEMSET(buf, 0, sizeof(buf)); WMEMSET(buf, 0, sizeof(buf));
WSNPRINTF(buf, sizeof(buf), "C%04o %u %s\n", WSNPRINTF(buf, sizeof(buf), "C%04o %u %s\n",
ssh->scpFileMode, ssh->scpFileSz, ssh->scpFileName); ssh->scpFileMode, ssh->scpFileSz, ssh->scpFileName);
filehdr = buf;
bufSz = (int)WSTRLEN(buf); #else
filehdr = WSCPFILEHDR(ssh);
ret = wolfSSH_stream_send(ssh, (byte*)buf, bufSz); if (!filehdr)
return WS_BAD_ARGUMENT;
#endif
bufSz = (int)WSTRLEN(filehdr);
ret = wolfSSH_stream_send(ssh, (byte*)filehdr, bufSz);
if (ret != bufSz) { if (ret != bufSz) {
ret = WS_FATAL_ERROR; ret = WS_FATAL_ERROR;
} else { } else {
WLOG(WS_LOG_DEBUG, "scp: sent file header: %s", buf); WLOG(WS_LOG_DEBUG, "scp: sent file header: %s", filehdr);
ret = WS_SUCCESS; ret = WS_SUCCESS;
} }
return ret; return ret;
} }
@ -554,7 +562,6 @@ int DoScpSource(WOLFSSH* ssh)
ssh->scpState = SCP_RECEIVE_CONFIRMATION; ssh->scpState = SCP_RECEIVE_CONFIRMATION;
ssh->scpNextState = SCP_DONE; ssh->scpNextState = SCP_DONE;
continue; continue;
case SCP_SEND_FILE_HEADER: case SCP_SEND_FILE_HEADER:
WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_FILE_HEADER"); WLOG(WS_LOG_DEBUG, scpState, "SCP_SEND_FILE_HEADER");
@ -879,7 +886,7 @@ static int GetScpFileSize(WOLFSSH* ssh, byte* buf, word32 bufSz,
if (ret == WS_SUCCESS) { if (ret == WS_SUCCESS) {
/* replace space with newline for atoi */ /* replace space with newline for atoi */
buf[spaceIdx] = '\n'; buf[spaceIdx] = '\n';
ssh->scpFileSz = atoi((char*)(buf + idx)); ssh->scpFileSz = atoi((char *)(buf + idx));
/* restore space, increment idx to space */ /* restore space, increment idx to space */
buf[spaceIdx] = ' '; buf[spaceIdx] = ' ';
@ -1035,7 +1042,6 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz,
return ret; return ret;
} }
/* checks for if directory is being renamed in command /* checks for if directory is being renamed in command
* *
* returns WS_SUCCESS on success * returns WS_SUCCESS on success
@ -1043,7 +1049,7 @@ static int GetScpTimestamp(WOLFSSH* ssh, byte* buf, word32 bufSz,
static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz) static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz)
{ {
/* case of file, not directory */ /* case of file, not directory */
char buf[cmdSz + 4]; char buf[DEFAULT_SCP_MSG_SZ];
int sz = (int)WSTRLEN(ssh->scpBasePath); int sz = (int)WSTRLEN(ssh->scpBasePath);
int idx; int idx;
@ -1051,9 +1057,14 @@ static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz)
return WS_BUFFER_E; return WS_BUFFER_E;
} }
if (cmdSz + 4 > DEFAULT_SCP_MSG_SZ) {
return WS_BUFFER_E;
}
WSTRNCPY(buf, ssh->scpBasePath, cmdSz); WSTRNCPY(buf, ssh->scpBasePath, cmdSz);
buf[sz] = '\0'; buf[sz] = '\0';
WSTRNCAT(buf, "/..", sizeof("/..")); WSTRNCAT(buf, "/..", sizeof("/.."));
idx = wolfSSH_CleanPath(ssh, buf); idx = wolfSSH_CleanPath(ssh, buf);
if (idx < 0) { if (idx < 0) {
return WS_FATAL_ERROR; return WS_FATAL_ERROR;
@ -1115,10 +1126,8 @@ static int ScpCheckForRename(WOLFSSH* ssh, int cmdSz)
* returns WS_SUCCESS on success */ * returns WS_SUCCESS on success */
static int ParseBasePathHelper(WOLFSSH* ssh, int cmdSz) static int ParseBasePathHelper(WOLFSSH* ssh, int cmdSz)
{ {
int ret; int ret = 0;
ret = ScpCheckForRename(ssh, cmdSz); ret = ScpCheckForRename(ssh, cmdSz);
#ifndef NO_FILESYSTEM #ifndef NO_FILESYSTEM
{ {
ScpSendCtx ctx; ScpSendCtx ctx;
@ -1541,6 +1550,7 @@ void* wolfSSH_GetScpSendCtx(WOLFSSH* ssh)
} }
#ifndef NO_WOLFSSH_CLIENT
int wolfSSH_SCP_connect(WOLFSSH* ssh, byte* cmd) int wolfSSH_SCP_connect(WOLFSSH* ssh, byte* cmd)
{ {
int ret = WS_SUCCESS; int ret = WS_SUCCESS;
@ -1569,7 +1579,6 @@ int wolfSSH_SCP_connect(WOLFSSH* ssh, byte* cmd)
return ret; return ret;
} }
static int wolfSSH_SCP_cmd(WOLFSSH* ssh, const char* localName, static int wolfSSH_SCP_cmd(WOLFSSH* ssh, const char* localName,
const char* remoteName, byte dir) const char* remoteName, byte dir)
{ {
@ -1635,6 +1644,7 @@ int wolfSSH_SCP_from(WOLFSSH* ssh, const char* src, const char* dst)
/* src is passed to the server in the scp -f command */ /* src is passed to the server in the scp -f command */
/* dst is used locally to fopen and write for copy from */ /* dst is used locally to fopen and write for copy from */
} }
#endif /* ! NO_WOLFSSH_CLIENT */
#if !defined(WOLFSSH_SCP_USER_CALLBACKS) #if !defined(WOLFSSH_SCP_USER_CALLBACKS)

View File

@ -1010,6 +1010,8 @@ extern "C" {
#define WDIR HANDLE #define WDIR HANDLE
#endif /* NO_WOLFSSH_DIR */ #endif /* NO_WOLFSSH_DIR */
#elif defined(WOLFSSH_USER_IO)
/* User-defined I/O support */
#else #else
#include <unistd.h> /* used for rmdir */ #include <unistd.h> /* used for rmdir */
#include <sys/stat.h> /* used for mkdir, stat, and lstat */ #include <sys/stat.h> /* used for mkdir, stat, and lstat */