From c38224099e0ddaaddd34c9feeb4fd25002183112 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Tue, 7 Nov 2023 02:31:10 -0800 Subject: [PATCH] increase type size for command name and improve handling read of pipes after command exits --- apps/wolfsshd/wolfsshd.c | 24 ++++++++++++++++++++---- wolfssh/internal.h | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index 738fde9ea..7cd4c49df 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -1166,11 +1166,15 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, #ifndef EXAMPLE_BUFFER_SZ #define EXAMPLE_BUFFER_SZ 4096 +#endif +#ifndef MAX_IDLE_COUNT + #define MAX_IDLE_COUNT 2 #endif byte shellBuffer[EXAMPLE_BUFFER_SZ]; byte channelBuffer[EXAMPLE_BUFFER_SZ]; char* forcedCmd; int windowFull = 0; + int idle = 0; forcedCmd = wolfSSHD_ConfigGetForcedCmd(usrConf); @@ -1381,7 +1385,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, close(stderrPipe[1]); } - while (ChildRunning) { + while (idle < MAX_IDLE_COUNT) { byte tmp[2]; fd_set readFds; WS_SOCKET_T maxFd; @@ -1389,6 +1393,8 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, int cnt_w; int pending = 0; + idle++; /* increment idle count, gets reset if not idle */ + FD_ZERO(&readFds); FD_SET(sshFd, &readFds); maxFd = sshFd; @@ -1416,6 +1422,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, } else { pending = 1; /* found some pending SSH data */ + idle = 0; } if (windowFull || pending || FD_ISSET(sshFd, &readFds)) { @@ -1431,6 +1438,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, if (cnt_r < 0) { rc = wolfSSH_get_error(ssh); if (rc == WS_CHAN_RXD) { + idle = 0; if (lastChannel == shellChannelId) { cnt_r = wolfSSH_ChannelIdRead(ssh, shellChannelId, channelBuffer, @@ -1458,6 +1466,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, shellBuffer, cnt_r); if (cnt_w == WS_WINDOW_FULL) { windowFull = 1; + idle = 0; continue; } else { @@ -1472,12 +1481,13 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, /* This read will return 0 on EOF */ if (cnt_r <= 0) { int err = errno; - if (err != EAGAIN) { + if (err != EAGAIN && err != 0) { break; } } else { if (cnt_r > 0) { + idle = 0; cnt_w = wolfSSH_extended_data_send(ssh, shellBuffer, cnt_r); if (cnt_w == WS_WINDOW_FULL) { @@ -1497,12 +1507,13 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, /* This read will return 0 on EOF */ if (cnt_r <= 0) { int err = errno; - if (err != EAGAIN) { + if (err != EAGAIN && err != 0) { break; } } else { if (cnt_r > 0) { + idle = 0; cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId, shellBuffer, cnt_r); if (cnt_w == WS_WINDOW_FULL) { @@ -1521,12 +1532,13 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, /* This read will return 0 on EOF */ if (cnt_r <= 0) { int err = errno; - if (err != EAGAIN) { + if (err != EAGAIN && err != 0) { break; } } else { if (cnt_r > 0) { + idle = 0; cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId, shellBuffer, cnt_r); if (cnt_w == WS_WINDOW_FULL) { @@ -1539,6 +1551,10 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, } } } + + if (ChildRunning && idle) { + idle = 0; /* waiting on child process */ + } } /* check for any left over data in pipes then close them */ diff --git a/wolfssh/internal.h b/wolfssh/internal.h index e06b515d1..827763e5d 100644 --- a/wolfssh/internal.h +++ b/wolfssh/internal.h @@ -715,7 +715,7 @@ struct WOLFSSH { word32 defaultPeerChannelId; word32 connectChannelId; byte channelName[WOLFSSH_MAX_CHN_NAMESZ]; - byte channelNameSz; + word32 channelNameSz; word32 lastRxId; WOLFSSH_BUFFER inputBuffer;