From 796423bfac63bc098a2fd98b19cb849190cedf96 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 26 Jan 2022 15:05:34 -0800 Subject: [PATCH 1/3] Update tests and examples to include config.h 1. All the sources for the tests and examples did not have an include for config.h. This fixed some inconsistent builds with an older version of gcc. 2. Moved config.h from the src directory to the top level of the repo directory. --- configure.ac | 2 +- examples/client/client.c | 4 ++++ examples/echoserver/echoserver.c | 6 +++--- examples/portfwd/portfwd.c | 4 ++++ examples/scpclient/scpclient.c | 4 ++++ examples/server/server.c | 4 ++++ examples/sftpclient/sftpclient.c | 4 ++++ tests/api.c | 3 +++ tests/sftp.c | 4 ++++ tests/testsuite.c | 4 ++++ tests/unit.c | 3 +++ 11 files changed, 38 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index a9dd7a0f..f386e6f5 100644 --- a/configure.ac +++ b/configure.ac @@ -16,7 +16,7 @@ AM_INIT_AUTOMAKE([1.11 -Wall -Werror -Wno-portability foreign tar-ustar subdir-o AC_ARG_PROGRAM AC_CONFIG_MACRO_DIR([m4]) -AC_CONFIG_HEADERS([src/config.h]) +AC_CONFIG_HEADERS([config.h]) WOLFSSH_LIBRARY_VERSION=12:3:3 # | | | diff --git a/examples/client/client.c b/examples/client/client.c index acb57171..ab505e4e 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -18,6 +18,10 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif + #define WOLFSSH_TEST_CLIENT #include diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index 906b89f7..8a396e0c 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -18,13 +18,13 @@ * along with wolfSSH. If not, see . */ -#define WOLFSSH_TEST_SERVER -#define WOLFSSH_TEST_ECHOSERVER - #ifdef HAVE_CONFIG_H #include #endif +#define WOLFSSH_TEST_SERVER +#define WOLFSSH_TEST_ECHOSERVER + #ifdef WOLFSSL_USER_SETTINGS #include #else diff --git a/examples/portfwd/portfwd.c b/examples/portfwd/portfwd.c index bb146c9f..02de901f 100644 --- a/examples/portfwd/portfwd.c +++ b/examples/portfwd/portfwd.c @@ -18,6 +18,10 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif + #define WOLFSSH_TEST_CLIENT #define WOLFSSH_TEST_SERVER diff --git a/examples/scpclient/scpclient.c b/examples/scpclient/scpclient.c index 1dc65bea..7ae750f5 100644 --- a/examples/scpclient/scpclient.c +++ b/examples/scpclient/scpclient.c @@ -18,6 +18,10 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif + #define WOLFSSH_TEST_CLIENT #include diff --git a/examples/server/server.c b/examples/server/server.c index f28fd1f5..1d24c283 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -18,6 +18,10 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif + #define WOLFSSH_TEST_SERVER #define WOLFSSH_TEST_THREADING diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index 42eb1f6e..57cf952e 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -18,6 +18,10 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif + #define WOLFSSH_TEST_CLIENT #include diff --git a/tests/api.c b/tests/api.c index 19021fd2..270f0e55 100644 --- a/tests/api.c +++ b/tests/api.c @@ -18,6 +18,9 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif #include #include diff --git a/tests/sftp.c b/tests/sftp.c index 66b7698d..fae7b61d 100644 --- a/tests/sftp.c +++ b/tests/sftp.c @@ -18,6 +18,10 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif + #include #include #include diff --git a/tests/testsuite.c b/tests/testsuite.c index ab69c2eb..f5ac942b 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -18,6 +18,10 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif + #define WOLFSSH_TEST_CLIENT #define WOLFSSH_TEST_SERVER #define WOLFSSH_TEST_THREADING diff --git a/tests/unit.c b/tests/unit.c index 1c6e9312..b3e1d19c 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -18,6 +18,9 @@ * along with wolfSSH. If not, see . */ +#ifdef HAVE_CONFIG_H + #include +#endif #include #include From ba42d6f732d73cc27e10d51b3098ec16a3b4fe80 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 26 Jan 2022 15:18:16 -0800 Subject: [PATCH 2/3] Fix missing pread() and pwrite() 1. configure.ac: Check that the DECLs for `pread()` and `pwrite()` exist in unistd.h. 2. port.c: If `pread()` or `pwrite()` aren't available, use the local versions that are wrappers around `seek()` and `read()` and `write()`. --- configure.ac | 1 + src/port.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index f386e6f5..6470db81 100644 --- a/configure.ac +++ b/configure.ac @@ -84,6 +84,7 @@ CPPFLAGS="$CPPFLAGS -I${wcpath}/include" AC_CHECK_LIB([wolfssl],[wolfCrypt_Init],,[AC_MSG_ERROR([libwolfssl is required for ${PACKAGE}. It can be obtained from https://www.wolfssl.com/download.html/ .])]) AC_CHECK_FUNCS([gethostbyname getaddrinfo gettimeofday inet_ntoa memset socket wc_ecc_set_rng]) +AC_CHECK_DECLS([[pread],[pwrite]],,[unistd.h]) # DEBUG DEBUG_CFLAGS="-g -O0" diff --git a/src/port.c b/src/port.c index f21ba153..cdb0463e 100644 --- a/src/port.c +++ b/src/port.c @@ -37,6 +37,13 @@ #include #endif +/* +Flags: + WOLFSSH_LOCAL_PREAD_PWRITE + Defined to use local implementations of pread() and pwrite(). Switched + on automatically if pread() or pwrite() aren't found by configure. +*/ + #if !defined(NO_FILESYSTEM) && !defined(WOLFSSH_USER_FILESYSTEM) int wfopen(WFILE** f, const char* filename, const char* mode) @@ -82,6 +89,16 @@ int wfopen(WFILE** f, const char* filename, const char* mode) #endif } + +/* If either pread() or pwrite() are missing, use the local versions. */ +#if (defined(USE_OSE_API) || \ + !defined(HAVE_DECL_PREAD) || (HAVE_DECL_PREAD == 0) || \ + !defined(HAVE_DECL_PWRITE) || (HAVE_DECL_PWRITE == 0)) + #undef WOLFSSH_LOCAL_PREAD_PWRITE + #define WOLFSSH_LOCAL_PREAD_PWRITE +#endif + + #if (defined(WOLFSSH_SFTP) || defined(WOLFSSH_SCP)) && \ !defined(NO_WOLFSSH_SERVER) @@ -90,7 +107,7 @@ int wfopen(WFILE** f, const char* filename, const char* mode) /* This is current inline in the source. */ - #elif defined(USE_OSE_API) + #elif defined(WOLFSSH_LOCAL_PREAD_PWRITE) int wPwrite(WFD fd, unsigned char* buf, unsigned int sz, const unsigned int* shortOffset) @@ -104,7 +121,6 @@ int wfopen(WFILE** f, const char* filename, const char* mode) return ret; } - int wPread(WFD fd, unsigned char* buf, unsigned int sz, const unsigned int* shortOffset) { @@ -117,7 +133,7 @@ int wfopen(WFILE** f, const char* filename, const char* mode) return ret; } - #else /* USE_WINDOWS_API USE_OSE_API */ + #else /* USE_WINDOWS_API WOLFSSH_LOCAL_PREAD_PWRITE */ int wPwrite(WFD fd, unsigned char* buf, unsigned int sz, const unsigned int* shortOffset) From bd312c34f24004de52ad763bcad8ed1244d60642 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 26 Jan 2022 16:24:41 -0800 Subject: [PATCH 3/3] Old Compiler Warning Cleanup (GCC 4.0.2) 1. client.c: It was warning a recommendation to tag the function `readInput()` as non-return. Changed the err_sys() calls to `fprintf(stderr, ...)` and then return from the function. 2. internal.c: In functions `DoKexInit()` and `DoUserAuthRequestPublicKey()`, initialized a couple variables that could be used uninitialized. 3. wolfsftp.c: Fixed a variable that was shadowing a global label. 4. wolfsftp.c: In function `wolfSSH_SFTP_RecvFSTAT()`, initialized a variable that could be used uninitialized. 5. scpclient.c: The command line argument was read into a char value assuming it was signed, which is usually true. On the PowerPC this value is unsigned. Promoted it to int to match the other tools. --- examples/client/client.c | 9 ++++++--- examples/scpclient/scpclient.c | 2 +- src/internal.c | 14 ++++++++++---- src/wolfsftp.c | 20 ++++++++++---------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index ab505e4e..5906f8de 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -541,14 +541,17 @@ static THREAD_RET readInput(void* in) sz = (word32)ret; #endif if (ret <= 0) { - err_sys("Error reading stdin"); + fprintf(stderr, "Error reading stdin\n"); + return THREAD_RET_SUCCESS; } /* lock SSH structure access */ wc_LockMutex(&args->lock); ret = wolfSSH_stream_send(args->ssh, buf, sz); wc_UnLockMutex(&args->lock); - if (ret <= 0) - err_sys("Couldn't send data"); + if (ret <= 0) { + fprintf(stderr, "Couldn't send data\n"); + return THREAD_RET_SUCCESS; + } } #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) wc_ecc_fp_free(); /* free per thread cache */ diff --git a/examples/scpclient/scpclient.c b/examples/scpclient/scpclient.c index 7ae750f5..18150772 100644 --- a/examples/scpclient/scpclient.c +++ b/examples/scpclient/scpclient.c @@ -235,7 +235,7 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) word16 port = wolfSshPort; byte nonBlock = 0; enum copyDir dir = copyNone; - char ch; + int ch; ((func_args*)args)->return_code = 0; diff --git a/src/internal.c b/src/internal.c index bd9cac65..9c2e67c7 100644 --- a/src/internal.c +++ b/src/internal.c @@ -2356,7 +2356,7 @@ static INLINE byte AeadModeForId(byte id) static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx) { int ret = WS_SUCCESS; - int side; + int side = WOLFSSH_ENDPOINT_SERVER; byte algoId; byte list[16] = {ID_NONE}; word32 listSz; @@ -4475,7 +4475,7 @@ static int DoUserAuthRequestPublicKey(WOLFSSH* ssh, WS_UserAuthData* authData, else { wc_HashAlg hash; byte digest[WC_MAX_DIGEST_SIZE]; - word32 digestSz; + word32 digestSz = 0; enum wc_HashType hashId = WC_HASH_TYPE_SHA; byte pkTypeId; @@ -4489,10 +4489,16 @@ static int DoUserAuthRequestPublicKey(WOLFSSH* ssh, WS_UserAuthData* authData, if (ret == WS_SUCCESS) { hashId = HashForId(pkTypeId); WMEMSET(digest, 0, sizeof(digest)); - digestSz = wc_HashGetDigestSize(hashId); - ret = wc_HashInit(&hash, hashId); + ret = wc_HashGetDigestSize(hashId); + if (ret > 0) { + digestSz = ret; + ret = 0; + } } + if (ret == 0) + ret = wc_HashInit(&hash, hashId); + if (ret == 0) { c32toa(ssh->sessionIdSz, digest); ret = wc_HashUpdate(&hash, hashId, digest, UINT32_SZ); diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 331474f4..4cbf2bfb 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -954,7 +954,7 @@ static INLINE int SFTP_GetSz(byte* buf, word32* sz, #ifndef WOLFSSH_USER_FILESYSTEM static int SFTP_GetAttributes(void* fs, const char* fileName, - WS_SFTP_FILEATRB* atr, byte link, void* heap); + WS_SFTP_FILEATRB* atr, byte noFollow, void* heap); static int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz, WS_SFTP_FILEATRB* atr); #endif @@ -4098,7 +4098,7 @@ static word32 TimeTo32(word16 d, word16 t) * returns WS_SUCCESS on success */ int SFTP_GetAttributes(void* fs, const char* fileName, WS_SFTP_FILEATRB* atr, - byte link, void* heap) + byte noFollow, void* heap) { DSTAT stats; int sz = (int)WSTRLEN(fileName); @@ -4107,7 +4107,7 @@ int SFTP_GetAttributes(void* fs, const char* fileName, WS_SFTP_FILEATRB* atr, (void)heap; (void)fs; - if (link) { + if (noFollow) { ret = WLSTAT(fileName, &stats); } else { @@ -4238,13 +4238,13 @@ int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz, * returns WS_SUCCESS on success */ int SFTP_GetAttributes(void* fs, const char* fileName, WS_SFTP_FILEATRB* atr, - byte link, void* heap) + byte noFollow, void* heap) { BOOL error; WIN32_FILE_ATTRIBUTE_DATA stats; WLOG(WS_LOG_SFTP, "Entering SFTP_GetAttributes()"); - (void)link; + (void)noFollow; (void)fs; /* @TODO add proper Windows link support */ @@ -4283,7 +4283,7 @@ int SFTP_GetAttributes(void* fs, const char* fileName, WS_SFTP_FILEATRB* atr, * Fills out a WS_SFTP_FILEATRB structure * returns WS_SUCCESS on success */ int SFTP_GetAttributes(void* fs, const char* fileName, WS_SFTP_FILEATRB* atr, - byte link, void* heap) + byte noFollow, void* heap) { int err, sz; MQX_FILE_PTR mfs_ptr; @@ -4401,7 +4401,7 @@ int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz, /* FatFs has its own structure for file attributes */ static int SFTP_GetAttributes(void* fs, const char* fileName, - WS_SFTP_FILEATRB* atr, byte link, void* heap) + WS_SFTP_FILEATRB* atr, byte noFollow, void* heap) { FILINFO info; FRESULT ret; @@ -4522,14 +4522,14 @@ static int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz, * returns WS_SUCCESS on success */ int SFTP_GetAttributes(void* fs, const char* fileName, WS_SFTP_FILEATRB* atr, - byte link, void* heap) + byte noFollow, void* heap) { WSTAT_T stats; (void)heap; (void)fs; - if (link) { + if (noFollow) { /* Note, for windows, we treat WSTAT and WLSTAT the same. */ if (WLSTAT(fileName, &stats) != 0) { return WS_BAD_FILE_E; @@ -4620,7 +4620,7 @@ int wolfSSH_SFTP_RecvFSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz) { WS_SFTP_FILEATRB atr; word32 handleSz; - word32 sz; + word32 sz = 0; byte* handle; word32 idx = 0; int ret = WS_SUCCESS;