Skip to content

Commit b816ac5

Browse files
committed
SFTP List
Updated from peer review comments.
1 parent a48926b commit b816ac5

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

examples/echoserver/echoserver.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,16 @@ static int wsUserAuth(byte authType,
20312031

20322032

20332033
#ifdef WOLFSSH_SFTP
2034+
/*
2035+
* Sets the WOLFSSH object's default SFTP path to the value provided by
2036+
* defaultSftpPath, or uses the current working directory from where the
2037+
* echoserver is run. The new default path is cleaned up with the real
2038+
* path function.
2039+
*
2040+
* @param ssh WOLFSSH object to update
2041+
* @param defaultSftpPath command line provided default SFTP path
2042+
* @return 0 for success or error code
2043+
*/
20342044
static int SetDefaultSftpPath(WOLFSSH* ssh, const char* defaultSftpPath)
20352045
{
20362046
char path[WOLFSSH_MAX_FILENAME];
@@ -2039,26 +2049,31 @@ static int SetDefaultSftpPath(WOLFSSH* ssh, const char* defaultSftpPath)
20392049

20402050
if (defaultSftpPath == NULL) {
20412051
#ifdef USE_WINDOWS_API
2042-
if (GetCurrentDirectoryA(sizeof(path), path) == 0) {
2043-
ret = -1;
2052+
if (GetCurrentDirectoryA(sizeof(path)-1, path) == 0) {
2053+
ret = WS_INVALID_PATH_E;
20442054
}
20452055
#else
2046-
if (getcwd(path, sizeof(path)) == NULL) {
2047-
ret = -1;
2056+
if (getcwd(path, sizeof(path)-1) == NULL) {
2057+
ret = WS_INVALID_PATH_E;
20482058
}
20492059
#endif
20502060
}
20512061
else {
2052-
WSTRNCPY(path, defaultSftpPath, sizeof(path));
2053-
path[sizeof(path) - 1] = 0;
2062+
if (WSTRLEN(defaultSftpPath) >= sizeof(path)) {
2063+
ret = WS_INVALID_PATH_E;
2064+
}
2065+
else {
2066+
WSTRNCPY(path, defaultSftpPath, sizeof(path));
2067+
}
20542068
}
20552069

20562070
if (ret == 0) {
20572071
path[sizeof(path) - 1] = 0;
2058-
wolfSSH_RealPath(NULL, path, realPath, sizeof(realPath));
2059-
if (wolfSSH_SFTP_SetDefaultPath(ssh, realPath) != WS_SUCCESS) {
2060-
ret = -1;
2061-
}
2072+
ret = wolfSSH_RealPath(NULL, path, realPath, sizeof(realPath));
2073+
}
2074+
2075+
if (ret == WS_SUCCESS) {
2076+
ret = wolfSSH_SFTP_SetDefaultPath(ssh, realPath);
20622077
}
20632078

20642079
return ret;

src/port.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ int wfopen(WFILE** f, const char* filename, const char* mode)
166166

167167
#if defined(USE_WINDOWS_API) && (defined(WOLFSSH_SFTP) || defined(WOLFSSH_SCP))
168168

169+
/*
170+
* SFTP paths all start with a leading root "/". Most Windows file routines
171+
* expect a drive letter when dealing with an absolute path. If the provided
172+
* path, f, is of the form "/C:...", adjust the pointer f to point to the "C",
173+
* and decrement the file path size, fSz, by one.
174+
*
175+
* @param f pointer to a file name
176+
* @param fSz size of f in bytes
177+
* @return pointer to somewhere in f
178+
*/
169179
static const char* TrimFileName(const char* f, size_t* fSz)
170180
{
171181
if (f != NULL && fSz != NULL && *fSz >= 3 && f[0] == '/' && f[2] == ':') {

src/wolfsftp.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,16 +1553,28 @@ int wolfSSH_SFTP_CreateStatus(WOLFSSH* ssh, word32 status, word32 reqId,
15531553
}
15541554

15551555

1556-
static int GetAndCleanPath(const char* dP, const byte* data, word32 sz,
1557-
char* s, word32 sSz)
1556+
/*
1557+
* This is a wrapper around the function wolfSSH_RealPath. Since it modifies
1558+
* the source path value, copy the path from the data stream into a local
1559+
* array and use that as the source.
1560+
*
1561+
* @param defaultPath pointer to the defaultPath
1562+
* @param data input data stream at the location of the path name
1563+
* @param sz size of the path name in bytes
1564+
* @param s destination buffer for the Real Path
1565+
* @param sSz size of s in bytes
1566+
* @return 0 for success or negative error code
1567+
*/
1568+
static int GetAndCleanPath(const char* defaultPath,
1569+
const byte* data, word32 sz, char* s, word32 sSz)
15581570
{
15591571
char r[WOLFSSH_MAX_FILENAME];
15601572

15611573
if (sz > sizeof r) sz = sizeof r;
15621574
WMEMCPY(r, data, sz);
15631575
r[sz] = '\0';
15641576

1565-
return wolfSSH_RealPath(dP, r, s, sSz);
1577+
return wolfSSH_RealPath(defaultPath, r, s, sSz);
15661578
}
15671579

15681580

@@ -1673,7 +1685,7 @@ int wolfSSH_SFTP_RecvMKDIR(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
16731685
ret = GetAndCleanPath(ssh->sftpDefaultPath,
16741686
data + idx, sz, dir, sizeof(dir));
16751687
if (ret != WS_SUCCESS) {
1676-
return WS_BUFFER_E;
1688+
return ret;
16771689
}
16781690

16791691
idx += sz;

tests/api.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,12 +1077,12 @@ static void DoRealPathTestCase(const char* path, struct RealPathTestCase* tc)
10771077
err = wolfSSH_RealPath(path, testPath,
10781078
checkPath, sizeof checkPath);
10791079
if (err || WSTRCMP(tc->exp, checkPath) != 0) {
1080-
printf("RealPath failure (%d)\n"
1081-
" defaultPath: %s\n"
1082-
" input: %s\n"
1083-
" expected: %s\n"
1084-
" output: %s\n", err,
1085-
path, tc->in, tc->exp, checkPath);
1080+
fprintf(stderr, "RealPath failure (%d)\n"
1081+
" defaultPath: %s\n"
1082+
" input: %s\n"
1083+
" expected: %s\n"
1084+
" output: %s\n", err,
1085+
path, tc->in, tc->exp, checkPath);
10861086
}
10871087
}
10881088

0 commit comments

Comments
 (0)