diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index cbf2662b4..7272a551c 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -382,7 +382,7 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx) if (action == WOLFSSH_AGENT_LOCAL_SETUP) { struct sockaddr_un* name = &ctx->name; - size_t size; + int envSet = 0; WMEMSET(name, 0, sizeof(struct sockaddr_un)); ctx->pid = getpid(); @@ -391,19 +391,15 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx) ret = snprintf(name->sun_path, sizeof(name->sun_path), "/tmp/wolfserver.%d", ctx->pid); - if (ret == 0) { + if (ret > 0) { name->sun_path[sizeof(name->sun_path) - 1] = '\0'; - size = WSTRLEN(name->sun_path) + - offsetof(struct sockaddr_un, sun_path); ctx->listenFd = socket(AF_UNIX, SOCK_STREAM, 0); - if (ctx->listenFd == -1) { - ret = -1; - } + ret = (ctx->listenFd == -1) ? -1 : 0; } if (ret == 0) { - ret = bind(ctx->listenFd, - (struct sockaddr *)name, (socklen_t)size); + ret = bind(ctx->listenFd, (struct sockaddr *)name, + (socklen_t)sizeof(struct sockaddr_un)); } if (ret == 0) { @@ -411,6 +407,7 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx) } if (ret == 0) { + envSet = 1; ret = listen(ctx->listenFd, 5); } @@ -418,6 +415,13 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx) ctx->state = AGENT_STATE_LISTEN; } else { + if (envSet) { + unsetenv(EnvNameAuthPort); + } + if (ctx->listenFd >= 0) { + close(ctx->listenFd); + ctx->listenFd = -1; + } ret = WS_AGENT_SETUP_E; } } @@ -2749,7 +2753,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args) #ifdef WOLFSSH_TEST_BLOCK if (!nonBlock) { - ES_ERROR("Use -N when testing forced non blocking"); + ES_ERROR("Use -N when testing forced non blocking\n"); } #endif diff --git a/examples/portfwd/portfwd.c b/examples/portfwd/portfwd.c index 723d1ba2c..0d5b7329e 100644 --- a/examples/portfwd/portfwd.c +++ b/examples/portfwd/portfwd.c @@ -235,6 +235,7 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) const char* fwdToHost = NULL; const char* username = NULL; const char* password = NULL; + const char* readyFile = NULL; SOCKADDR_IN_T hostAddr; socklen_t hostAddrSz = sizeof(hostAddr); SOCKET_T sshFd; @@ -266,7 +267,7 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) ((func_args*)args)->return_code = 0; - while ((ch = mygetopt(argc, argv, "?f:h:p:t:u:F:P:T:")) != -1) { + while ((ch = mygetopt(argc, argv, "?f:h:p:t:u:F:P:R:T:")) != -1) { switch (ch) { case 'h': host = myoptarg; @@ -306,6 +307,10 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) password = myoptarg; break; + case 'R': + readyFile = myoptarg; + break; + case 'T': fwdToHost = myoptarg; break; @@ -404,6 +409,21 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) if (ret != WS_SUCCESS) err_sys("Couldn't connect SFTP"); + if (readyFile != NULL) { + #ifndef NO_FILESYSTEM + WFILE* f = NULL; + ret = WFOPEN(NULL, &f, readyFile, "w"); + if (f != NULL && ret == 0) { + char portStr[10]; + int l = WSNPRINTF(portStr, sizeof(portStr), "%d\n", (int)port); + WFWRITE(NULL, portStr, MIN((size_t)l, sizeof(portStr)), 1, f); + WFCLOSE(NULL, f); + } + #else + err_sys("cannot create readyFile with no file system.\r\n"); + #endif + } + FD_ZERO(&templateFds); FD_SET(sshFd, &templateFds); FD_SET(listenFd, &templateFds); diff --git a/scripts/fwd.test b/scripts/fwd.test new file mode 100755 index 000000000..e42709602 --- /dev/null +++ b/scripts/fwd.test @@ -0,0 +1,109 @@ +#!/usr/bin/env bash + +NCSVRPORT=11111 +NCCLIPORT=12345 +ECHO_READY=ready.es.$$ +FWD_READY=ready.fwd.$$ +ESPID=0 +NCSVRPID=0 +FWDPID=0 + +do_cleanup() { + rm -f "$ECHO_READY" "$FWD_READY" + if [ "$ESPID" -ne 0 ] + then + kill "$ESPID" >/dev/null 2>&1 || true + fi + if [ "$NCSVRPID" -ne 0 ] + then + kill "$NCSVRPID" >/dev/null 2>&1 || true + fi + if [ "$FWDPID" -ne 0 ] + then + kill "$FWDPID" >/dev/null 2>&1 || true + fi +} + +wait_for_ready_file() { + READY_COUNTER=0 + while [ ! -s "$1" ] && [ "$READY_COUNTER" -lt 20 ] + do + sleep 0.1 + READY_COUNTER=$((READY_COUNTER + 1)) + done + + if [ ! -e "$1" ] + then + echo "$2 never started." + do_cleanup + exit 1 + fi +} + +# Check for prerequisites: build for forwarding, nc present + +# libtool can leave behind a script that runs the actual executable. +if [ ! -x ./examples/echoserver/echoserver ] || \ + ./examples/echoserver/echoserver "-?" 2>&1 | grep -q "does not exist" +then + echo "This test requires the echoserver." + exit 1 +fi + +# libtool can leave behind a script that runs the actual executable. +if [ ! -x ./examples/portfwd/portfwd ] || \ + ./examples/portfwd/portfwd "-?" 2>&1 | grep -q "does not exist" +then + echo "Port forwarding not enabled. Skipping." + exit 77 +fi + +# test for nonblocking only +./examples/client/client -h | grep WOLFSSH_TEST_BLOCK +if [ $? -eq 0 ] +then + echo "macro WOLFSSH_TEST_BLOCK was used" + exit 77 +fi + +if [ ! -x "$(which nc)" ] +then + echo "Tool nc not installed. Skipping." + exit 77 +fi + +nc -l $NCSVRPORT & +NCSVRPID=$! +./examples/echoserver/echoserver -1 -f -R "$ECHO_READY" & +ESPID=$! +wait_for_ready_file "$ECHO_READY" "Echoserver" +./examples/portfwd/portfwd -p "$(cat $ECHO_READY)" -R "$FWD_READY" \ + -u jill -P upthehill -f "$NCCLIPORT" -t "$NCSVRPORT" & +FWDPID=$! +wait_for_ready_file "$FWD_READY" "Port forwarding" + +nc -w 2 -4 localhost $NCCLIPORT </dev/null + then + kill "$NCCLIPID" + fi +} & +MONPID=$! + +wait "$NCCLIPID" +CLIENTRESULT=$? + +kill "$MONPID" 2>/dev/null || true + +do_cleanup +exit "$CLIENTRESULT" diff --git a/scripts/include.am b/scripts/include.am index 02f4d943f..ce3fdc63b 100644 --- a/scripts/include.am +++ b/scripts/include.am @@ -11,4 +11,4 @@ if BUILD_SCP dist_noinst_SCRIPTS+= scripts/scp.test endif -dist_noinst_SCRIPTS+= scripts/external.test +dist_noinst_SCRIPTS+= scripts/external.test scripts/fwd.test diff --git a/src/agent.c b/src/agent.c index de918769c..bed5bd836 100644 --- a/src/agent.c +++ b/src/agent.c @@ -374,6 +374,7 @@ static int PostLock(WOLFSSH_AGENT_CTX* agent, word32 ppSz; WLOG(WS_LOG_AGENT, "Posting lock to agent %p", agent); + WOLFSSH_UNUSED(agent); ppSz = sizeof(pp) - 1; if (passphraseSz < ppSz) @@ -395,6 +396,7 @@ static int PostUnlock(WOLFSSH_AGENT_CTX* agent, word32 ppSz; WLOG(WS_LOG_AGENT, "Posting unlock to agent %p", agent); + WOLFSSH_UNUSED(agent); ppSz = sizeof(pp) - 1; if (passphraseSz < ppSz) diff --git a/src/ssh.c b/src/ssh.c index c30d5fd4a..c6d7508c0 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -2605,7 +2605,7 @@ int wolfSSH_worker(WOLFSSH* ssh, word32* channelId) } #endif /* WOLFSSH_TEST_BLOCK */ - if (ret == WS_SUCCESS) { + if (ret == WS_SUCCESS || ret == WS_CHAN_RXD) { if (channelId != NULL) { *channelId = ssh->lastRxId; }