Skip to content

Commit 7c9f8f1

Browse files
refactor pty flag to Channel level and treat result as boolean in wolfsshd checks
1 parent 6fe09bd commit 7c9f8f1

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

apps/wolfsshd/wolfsshd.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,12 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
12041204
stdinPipe[1] = -1;
12051205

12061206
forcedCmd = wolfSSHD_ConfigGetForcedCmd(usrConf);
1207-
ptyReq = wolfSSH_ReceivedPtyReq(ssh);
1207+
1208+
ptyReq = wolfSSH_ChannelIsPty(ssh, NULL);
1209+
if (ptyReq < 0) {
1210+
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Failure get channel PTY state");
1211+
return WS_FATAL_ERROR;
1212+
}
12081213

12091214
/* do not overwrite a forced command with 'exec' sub shell. Only set the
12101215
* 'exec' command when no forced command is set */
@@ -1227,7 +1232,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
12271232

12281233

12291234
/* create pipes for stdout and stderr */
1230-
if (ptyReq == 0 || forcedCmd) {
1235+
if (!ptyReq || forcedCmd) {
12311236
if (pipe(stdoutPipe) != 0) {
12321237
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Issue creating stdout pipe");
12331238
return WS_FATAL_ERROR;
@@ -1266,7 +1271,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
12661271
signal(SIGINT, SIG_DFL);
12671272
signal(SIGCHLD, SIG_DFL);
12681273

1269-
if (ptyReq == 0 || forcedCmd) {
1274+
if (!ptyReq || forcedCmd) {
12701275
close(stdoutPipe[0]);
12711276
close(stderrPipe[0]);
12721277
close(stdinPipe[1]);
@@ -1393,7 +1398,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
13931398
close(stderrPipe[1]);
13941399
close(stdinPipe[1]);
13951400
}
1396-
else if (ptyReq == 0) {
1401+
else if (!ptyReq) {
13971402
ret = execv(cmd, (char**)args);
13981403
close(stdoutPipe[1]);
13991404
close(stderrPipe[1]);
@@ -1452,7 +1457,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
14521457
#endif
14531458

14541459
wolfSSH_SetTerminalResizeCtx(ssh, (void*)&childFd);
1455-
if (ptyReq == 0 || forcedCmd) {
1460+
if (!ptyReq || forcedCmd) {
14561461
close(stdoutPipe[1]);
14571462
close(stderrPipe[1]);
14581463
close(stdinPipe[0]);
@@ -1478,7 +1483,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
14781483

14791484
if (wolfSSH_stream_peek(ssh, tmp, 1) <= 0) {
14801485
/* select on stdout/stderr pipes with forced commands */
1481-
if (ptyReq == 0 || forcedCmd) {
1486+
if (!ptyReq || forcedCmd) {
14821487
FD_SET(stdoutPipe[0], &readFds);
14831488
if (stdoutPipe[0] > maxFd)
14841489
maxFd = stdoutPipe[0];
@@ -1524,7 +1529,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15241529
if (cnt_r <= 0)
15251530
break;
15261531

1527-
if (ptyReq == 0 || forcedCmd) {
1532+
if (!ptyReq || forcedCmd) {
15281533
cnt_w = (int)write(stdinPipe[1], channelBuffer,
15291534
cnt_r);
15301535
}
@@ -1564,7 +1569,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15641569
current = wolfSSH_ChannelFind(ssh, lastChannel,
15651570
WS_CHANNEL_ID_SELF);
15661571
eof = wolfSSH_ChannelGetEof(current);
1567-
if (eof && (ptyReq == 0 || forcedCmd)) {
1572+
if (eof && (!ptyReq || forcedCmd)) {
15681573
/* SSH is done, close stdin pipe to child process */
15691574
close(stdinPipe[1]);
15701575
stdinPipe[1] = -1;
@@ -1594,7 +1599,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15941599
}
15951600
}
15961601

1597-
if (ptyReq == 0 || forcedCmd) {
1602+
if (!ptyReq || forcedCmd) {
15981603
if (FD_ISSET(stderrPipe[0], &readFds)) {
15991604
cnt_r = (int)read(stderrPipe[0], shellBuffer,
16001605
sizeof shellBuffer);
@@ -1734,7 +1739,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
17341739
}
17351740

17361741
/* check for any left over data in pipes then close them */
1737-
if (ptyReq == 0 || forcedCmd) {
1742+
if (!ptyReq || forcedCmd) {
17381743
int readSz;
17391744

17401745
fcntl(stdoutPipe[0], F_SETFL, fcntl(stdoutPipe[0], F_GETFL)

src/internal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8951,7 +8951,7 @@ static int DoChannelRequest(WOLFSSH* ssh,
89518951
word32 termSz, modesSz = 0;
89528952
word32 widthChar, heightRows, widthPixels, heightPixels;
89538953

8954-
ssh->ptyReq = 1; /* recieved a pty request */
8954+
channel->ptyReq = 1; /* recieved a pty request */
89558955
termSz = (word32)sizeof(term);
89568956
ret = GetString(term, &termSz, buf, len, &begin);
89578957
if (ret == WS_SUCCESS)

src/ssh.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,25 +2539,6 @@ WS_SessionType wolfSSH_GetSessionType(const WOLFSSH* ssh)
25392539
}
25402540

25412541

2542-
#if defined(WOLFSSH_TERM)
2543-
int wolfSSH_ReceivedPtyReq(const WOLFSSH* ssh)
2544-
{
2545-
WLOG(WS_LOG_DEBUG, "Entering wolfSSH_ReceivedPtyReq");
2546-
2547-
if (ssh == NULL) {
2548-
return WS_BAD_ARGUMENT;
2549-
}
2550-
2551-
if (ssh->ptyReq) {
2552-
return 1;
2553-
}
2554-
else {
2555-
return 0;
2556-
}
2557-
}
2558-
#endif
2559-
2560-
25612542
const char* wolfSSH_GetSessionCommand(const WOLFSSH* ssh)
25622543
{
25632544
WLOG(WS_LOG_DEBUG, "Entering wolfSSH_GetSessionCommand()");
@@ -3252,6 +3233,33 @@ WS_SessionType wolfSSH_ChannelGetSessionType(const WOLFSSH_CHANNEL* channel)
32523233
}
32533234

32543235

3236+
#if defined(WOLFSSH_TERM)
3237+
/* returns 1 if a PTY was requested, 0 if not, and negative on failure */
3238+
int wolfSSH_ChannelIsPty(WOLFSSH* ssh, WOLFSSH_CHANNEL* channel)
3239+
{
3240+
WOLFSSH_CHANNEL* channelPtr;
3241+
int ret = WS_SUCCESS;
3242+
3243+
channelPtr = channel;
3244+
3245+
/* if channel is NULL than use the last rxtx channel */
3246+
if (channelPtr == NULL) {
3247+
word32 channelId = 0;
3248+
3249+
ret = wolfSSH_GetLastRxId(ssh, &channelId);
3250+
if (ret == WS_SUCCESS) {
3251+
channelPtr = ChannelFind(ssh, channelId, WS_CHANNEL_ID_SELF);
3252+
}
3253+
}
3254+
3255+
if (ret == WS_SUCCESS) {
3256+
ret = channelPtr->ptyReq;
3257+
}
3258+
return ret;
3259+
}
3260+
#endif
3261+
3262+
32553263
const char* wolfSSH_ChannelGetSessionCommand(const WOLFSSH_CHANNEL* channel)
32563264
{
32573265
const char* cmd = NULL;

wolfssh/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,6 @@ struct WOLFSSH {
913913
word32 heightPixels; /* pixel height */
914914
byte* modes;
915915
word32 modesSz;
916-
byte ptyReq:1; /* flag for if interactive pty request was received */
917916
#endif
918917
#if defined(WOLFSSH_TERM) || defined(WOLFSSH_SHELL)
919918
word32 exitStatus;
@@ -934,6 +933,7 @@ struct WOLFSSH_CHANNEL {
934933
byte eofRxd : 1;
935934
byte eofTxd : 1;
936935
byte openConfirmed : 1;
936+
byte ptyReq : 1; /* flag for if interactive pty request was received */
937937
word32 channel;
938938
word32 windowSz;
939939
word32 maxPacketSz;

wolfssh/ssh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ WOLFSSH_API WS_SessionType wolfSSH_ChannelGetSessionType(
233233
const WOLFSSH_CHANNEL* channel);
234234
WOLFSSH_API const char* wolfSSH_ChannelGetSessionCommand(
235235
const WOLFSSH_CHANNEL* channel);
236-
WOLFSSH_API int wolfSSH_ReceivedPtyReq(const WOLFSSH* ssh);
236+
WOLFSSH_API int wolfSSH_ChannelIsPty(WOLFSSH* ssh, WOLFSSH_CHANNEL* channel);
237237

238238
/* Channel callbacks */
239239
typedef int (*WS_CallbackChannelOpen)(WOLFSSH_CHANNEL* channel, void* ctx);

0 commit comments

Comments
 (0)