Skip to content

Commit

Permalink
Refactoring Channels
Browse files Browse the repository at this point in the history
1. Allow some of the shell code in the echoserver for handling echo.
2. Add check for echo to the shell start cb to skip forking a child
   process to handle the shell.
3. Always all the echo test in the testsuite.
4. Check a return value on execv of the shell in the echoserver.
5. Whitespace.
  • Loading branch information
ejohnstown committed Nov 4, 2024
1 parent 1fb0328 commit d5bd4e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
63 changes: 33 additions & 30 deletions examples/echoserver/echoserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,11 @@ typedef struct WS_FwdCbActionCtx {
#endif


#ifdef WOLFSSH_SHELL
typedef struct WS_ShellCtx {
int isConnected;
WS_SOCKET_T childFd;
word32 channelId;
} WS_ShellCtx;
#endif


typedef struct {
Expand All @@ -202,10 +200,8 @@ typedef struct {
WS_FwdCbActionCtx fwdCbCtx;
byte fwdBuffer[EXAMPLE_BUFFER_SZ];
#endif
#ifdef WOLFSSH_SHELL
WS_ShellCtx shellCtx;
byte shellBuffer[EXAMPLE_BUFFER_SZ];
#endif
#ifdef WOLFSSH_SFTP
int doSftp;
#endif
Expand All @@ -217,8 +213,6 @@ typedef struct {
} thread_ctx_t;


#ifdef WOLFSSH_SHELL

static byte find_char(const byte* str, const byte* buf, word32 bufSz)
{
const byte* cur;
Expand Down Expand Up @@ -281,8 +275,6 @@ static int process_bytes(thread_ctx_t* threadCtx,
return stop;
}

#endif /* WOLFSSH_SHELL */


#if defined(WOLFSSL_PTHREADS) && defined(WOLFSSL_TEST_GLOBAL_REQ)

Expand Down Expand Up @@ -640,23 +632,33 @@ static int termios_show(int fd)
}
return 0;
}
#endif
#endif /* SHELL_DEBUG */
#endif /* WOLFSSH_SHELL */


static int wsShellStartCb(WOLFSSH_CHANNEL* channel, void* ctx)
{
if (ctx != NULL) {
thread_ctx_t* threadCtx;
#ifdef WOLFSSH_SHELL
WOLFSSH* ssh;
const char *userName;
struct passwd *p_passwd;
struct termios tios;
pid_t childPid;
int rc;
#endif /* WOLFSSH_SHELL */

threadCtx = (thread_ctx_t*)ctx;
ssh = threadCtx->ssh;
threadCtx->shellCtx.isConnected = 1;
if (threadCtx->echo) {
/* Running as the echoserver. Don't check the user with
* the system. One of the canned users is OK here. */
return 0;
}

#ifdef WOLFSSH_SHELL
ssh = threadCtx->ssh;
wolfSSH_ChannelGetId(channel,
&threadCtx->shellCtx.channelId, WS_CHANNEL_ID_PEER);
userName = wolfSSH_GetUsername(ssh);
Expand Down Expand Up @@ -695,6 +697,10 @@ static int wsShellStartCb(WOLFSSH_CHANNEL* channel, void* ctx)
}

rc = execv("/bin/sh", (char **)args);
if (rc < 0) {
printf("execv failed: rc =%d,errno=%x\n", rc, errno);
return WS_FATAL_ERROR;
}
}
#ifdef SHELL_DEBUG
printf("In childPid > 0; getpid=%d\n", (int)getpid());
Expand Down Expand Up @@ -732,11 +738,13 @@ static int wsShellStartCb(WOLFSSH_CHANNEL* channel, void* ctx)
#endif /* HAVE_SYS_IOCTL_H */

wolfSSH_SetTerminalResizeCtx(ssh, (void*)&threadCtx->shellCtx.childFd);
#else /* WOLFSSH_SHELL */
(void)channel;
#endif /* WOLFSSH_SHELL */
}

return 0;
}
#endif /* WOLFSSH_SHELL */


#ifdef WOLFSSH_SCP
Expand Down Expand Up @@ -952,23 +960,18 @@ static int ssh_worker(thread_ctx_t* threadCtx)
while (ChildRunning) {
fd_set readFds, exFds;
WS_SOCKET_T maxFd;
int cnt_r;
#if defined(WOLFSSH_SHELL) || defined(WOLFSSH_AGENT) || defined(WOLFSSH_FWD)
int cnt_w;
#endif
int cnt_r, cnt_w = 0;

FD_ZERO(&readFds);
FD_ZERO(&exFds);
FD_SET(sshFd, &readFds);
maxFd = sshFd;

#ifdef WOLFSSH_SHELL
if (threadCtx->shellCtx.isConnected) {
FD_SET(threadCtx->shellCtx.childFd, &readFds);
if (threadCtx->shellCtx.childFd > maxFd)
maxFd = threadCtx->shellCtx.childFd;
}
#endif /* WOLFSSH_SHELL */
#ifdef WOLFSSH_AGENT
if (threadCtx->agentCbCtx.state == AGENT_STATE_LISTEN) {
FD_SET(agentListenFd, &readFds);
Expand Down Expand Up @@ -1028,7 +1031,6 @@ static int ssh_worker(thread_ctx_t* threadCtx)
if (cnt_r < 0) {
rc = wolfSSH_get_error(ssh);
if (rc == WS_CHAN_RXD) {
#ifdef WOLFSSH_SHELL
if (threadCtx->shellCtx.isConnected &&
lastChannel == threadCtx->shellCtx.channelId) {
cnt_r = wolfSSH_ChannelIdRead(ssh,
Expand All @@ -1040,11 +1042,7 @@ static int ssh_worker(thread_ctx_t* threadCtx)
#ifdef SHELL_DEBUG
buf_dump(threadCtx->channelBuffer, cnt_r);
#endif
if (!threadCtx->echo) {
cnt_w = (int)write(threadCtx->shellCtx.childFd,
threadCtx->channelBuffer, cnt_r);
}
else {
if (threadCtx->echo) {
cnt_w = wolfSSH_ChannelIdSend(ssh,
threadCtx->shellCtx.channelId,
threadCtx->channelBuffer, cnt_r);
Expand All @@ -1055,10 +1053,15 @@ static int ssh_worker(thread_ctx_t* threadCtx)
ChildRunning = !doStop;
}
}
#ifdef WOLFSSH_SHELL
else {
cnt_w = (int)write(threadCtx->shellCtx.childFd,
threadCtx->channelBuffer, cnt_r);
}
#endif /* WOLFSSH_SHELL */
if (cnt_w <= 0)
break;
}
#endif /* WOLFSSH_SHELL */
#ifdef WOLFSSH_AGENT
if (lastChannel == agentChannelId) {
cnt_r = wolfSSH_ChannelIdRead(ssh, agentChannelId,
Expand Down Expand Up @@ -1144,7 +1147,7 @@ static int ssh_worker(thread_ctx_t* threadCtx)
}
}
#ifdef WOLFSSH_SHELL
if (threadCtx->shellCtx.isConnected) {
if (threadCtx->shellCtx.isConnected && !threadCtx->echo) {
if (FD_ISSET(threadCtx->shellCtx.childFd, &readFds)) {
cnt_r = (int)read(threadCtx->shellCtx.childFd,
threadCtx->shellBuffer,
Expand Down Expand Up @@ -2493,6 +2496,10 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
char** argv = serverArgs->argv;
serverArgs->return_code = EXIT_SUCCESS;

#ifndef WOLFSSH_SHELL
echo = 1;
#endif

if (argc > 0) {
const char* optlist = "?1a:d:efEp:R:Ni:j:I:J:K:P:k:b:";
myoptind = 0;
Expand Down Expand Up @@ -2525,9 +2532,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
break;

case 'f':
#ifdef WOLFSSH_SHELL
echo = 1;
#endif
echo = 1;
break;

case 'p':
Expand Down Expand Up @@ -2647,9 +2652,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
wolfSSH_SetUserAuth(ctx, ((func_args*)args)->user_auth);
wolfSSH_SetUserAuthResult(ctx, wsUserAuthResult);
wolfSSH_CTX_SetBanner(ctx, echoserverBanner);
#ifdef WOLFSSH_SHELL
wolfSSH_CTX_SetChannelReqShellCb(ctx, wsShellStartCb);
#endif
#ifdef WOLFSSH_AGENT
wolfSSH_CTX_set_agent_cb(ctx, wolfSSH_AGENT_DefaultActions, NULL);
#endif
Expand Down
5 changes: 0 additions & 5 deletions tests/testsuite.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ char* myoptarg = NULL;
#if !defined(NO_WOLFSSH_SERVER) && !defined(NO_WOLFSSH_CLIENT) && \
!defined(SINGLE_THREADED) && !defined(WOLFSSH_TEST_BLOCK)

#ifdef WOLFSSH_SHELL

static int tsClientUserAuth(byte authType, WS_UserAuthData* authData, void* ctx)
{
static char password[] = "upthehill";
Expand Down Expand Up @@ -150,7 +148,6 @@ static void wolfSSH_EchoTest(void)

FreeTcpReady(&ready);
}
#endif /* WOLFSSH_SHELL */


int wolfSSH_TestsuiteTest(int argc, char** argv)
Expand Down Expand Up @@ -179,9 +176,7 @@ int wolfSSH_TestsuiteTest(int argc, char** argv)
ChangeToWolfSshRoot();
#endif

#ifdef WOLFSSH_SHELL
wolfSSH_EchoTest();
#endif

wolfSSH_Cleanup();

Expand Down

0 comments on commit d5bd4e9

Please sign in to comment.