Skip to content

Commit

Permalink
add internal get path function for SFTP
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Oct 4, 2024
1 parent 7ec124c commit 48570ac
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
51 changes: 51 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -15686,6 +15686,57 @@ int SendChannelSuccess(WOLFSSH* ssh, word32 channelId, int success)

#if (defined(WOLFSSH_SFTP) || defined(WOLFSSH_SCP)) && \
!defined(NO_WOLFSSH_SERVER)
/* Checks if 'in' is absolute path, if not the returns the concat. of
* 'defaultPath' | 'in'. This leaves 'in' as-is and does not handle
* simplification of the path, such as removing ../
*
* Sanity checks outSz and then adjusts it for the size used
* returns WS_SUCCESS on success
*/
int wolfSSH_GetPath(const char* defaultPath, byte* in, word32 inSz,
char* out, word32* outSz)
{
word32 curSz = 0;

if (out != NULL) {
WMEMSET(out, 0, *outSz);
}

if (inSz == 0 || (!WOLFSSH_SFTP_IS_DELIM(in[0]) &&
!WOLFSSH_SFTP_IS_WINPATH(inSz, in))) {
if (defaultPath != NULL) {
curSz = (word32)WSTRLEN(defaultPath);
if (out != NULL && curSz >= *outSz) {
return WS_INVALID_PATH_E;
}
if (out != NULL) {
WSTRNCPY(out, defaultPath, *outSz);
if (out[curSz] != '/') {
out[curSz] = '/';
curSz++;
}
}
}
}

if (out != NULL) {
WMEMCPY(out + curSz, in, inSz);
}
curSz += inSz;
if (out != NULL) {
if (!WOLFSSH_SFTP_IS_DELIM(out[0])) {
WMEMMOVE(out+1, out, curSz);
out[0] = '/';
curSz++;
}
out[curSz] = 0;
}
*outSz = curSz;

return WS_SUCCESS;
}


/* cleans up absolute path
* returns size of new path on success (strlen sz) and negative values on fail*/
int wolfSSH_CleanPath(WOLFSSH* ssh, char* in)
Expand Down
30 changes: 14 additions & 16 deletions src/wolfsftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1977,8 +1977,8 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
{
WS_SFTP_FILEATRB atr;
WFD fd;
word32 sz;
char dir[WOLFSSH_MAX_FILENAME+1]; /* +1 for null terminator */
word32 sz, dirSz;
char dir[WOLFSSH_MAX_FILENAME];
word32 reason;
word32 idx = 0;
int m = 0;
Expand Down Expand Up @@ -2013,13 +2013,12 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
return WS_BUFFER_E;
}

/* copy file name directly for attempt to open */
if (sz >= WOLFSSH_MAX_FILENAME) {
WLOG(WS_LOG_SFTP, "File name is too large");
return WS_BUFFER_E;
dirSz = sizeof(dir);
if (wolfSSH_GetPath(ssh->sftpDefaultPath, data + idx, sz, dir, &dirSz)
!= WS_SUCCESS) {
WLOG(WS_LOG_SFTP, "Creating path for file to open failed");
return WS_FATAL_ERROR;
}
WMEMCPY(dir, data + idx, sz);
dir[sz] = '\0';
idx += sz;

/* get reason for opening file */
Expand Down Expand Up @@ -2133,8 +2132,8 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
{
/* WS_SFTP_FILEATRB atr;*/
HANDLE fileHandle;
word32 sz;
char dir[WOLFSSH_MAX_FILENAME+1]; /* +1 for null terminator */
word32 sz, dirSz;
char dir[WOLFSSH_MAX_FILENAME];
word32 reason;
word32 idx = 0;
DWORD desiredAccess = 0;
Expand Down Expand Up @@ -2170,13 +2169,12 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
return WS_BUFFER_E;
}

/* copy file name directly for attempt to open */
if (sz >= WOLFSSH_MAX_FILENAME) {
WLOG(WS_LOG_SFTP, "File name is too large");
return WS_BUFFER_E;
dirSz = sizeof(dir);
if (wolfSSH_GetPath(ssh->sftpDefaultPath, data + idx, sz, dir, &dirSz)
!= WS_SUCCESS) {
WLOG(WS_LOG_SFTP, "Creating path for file to open failed");
return WS_FATAL_ERROR;
}
WMEMCPY(dir, data + idx, sz);
dir[sz] = '\0';
idx += sz;

/* get reason for opening file */
Expand Down
6 changes: 6 additions & 0 deletions wolfssh/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,12 @@ typedef struct HandshakeInfo {
} privKey;
} HandshakeInfo;

#if (defined(WOLFSSH_SFTP) || defined(WOLFSSH_SCP)) && \
!defined(NO_WOLFSSH_SERVER)
WOLFSSH_LOCAL int wolfSSH_GetPath(const char* defaultPath, byte* in,
word32 inSz, char* out, word32* outSz);
#endif

#ifdef WOLFSSH_SFTP
#define WOLFSSH_MAX_SFTPOFST 3

Expand Down

0 comments on commit 48570ac

Please sign in to comment.