Skip to content

Commit 6fe09bd

Browse files
do not treat shell as interactive until pty-req received
1 parent 4e087a0 commit 6fe09bd

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

apps/wolfsshd/wolfsshd.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
11931193
int wantWrite = 0;
11941194
int peerConnected = 1;
11951195
int stdoutEmpty = 0;
1196+
int ptyReq = 0;
11961197

11971198
childFd = -1;
11981199
stdoutPipe[0] = -1;
@@ -1203,6 +1204,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
12031204
stdinPipe[1] = -1;
12041205

12051206
forcedCmd = wolfSSHD_ConfigGetForcedCmd(usrConf);
1207+
ptyReq = wolfSSH_ReceivedPtyReq(ssh);
12061208

12071209
/* do not overwrite a forced command with 'exec' sub shell. Only set the
12081210
* 'exec' command when no forced command is set */
@@ -1223,8 +1225,9 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
12231225
return WS_FATAL_ERROR;
12241226
}
12251227

1228+
12261229
/* create pipes for stdout and stderr */
1227-
if (forcedCmd) {
1230+
if (ptyReq == 0 || forcedCmd) {
12281231
if (pipe(stdoutPipe) != 0) {
12291232
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Issue creating stdout pipe");
12301233
return WS_FATAL_ERROR;
@@ -1263,7 +1266,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
12631266
signal(SIGINT, SIG_DFL);
12641267
signal(SIGCHLD, SIG_DFL);
12651268

1266-
if (forcedCmd) {
1269+
if (ptyReq == 0 || forcedCmd) {
12671270
close(stdoutPipe[0]);
12681271
close(stderrPipe[0]);
12691272
close(stdinPipe[1]);
@@ -1390,7 +1393,13 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
13901393
close(stderrPipe[1]);
13911394
close(stdinPipe[1]);
13921395
}
1393-
else {
1396+
else if (ptyReq == 0) {
1397+
ret = execv(cmd, (char**)args);
1398+
close(stdoutPipe[1]);
1399+
close(stderrPipe[1]);
1400+
close(stdinPipe[1]);
1401+
}
1402+
else { /* open interactive shell */
13941403
ret = execv(cmd, (char**)args);
13951404
}
13961405
if (ret && errno) {
@@ -1443,7 +1452,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
14431452
#endif
14441453

14451454
wolfSSH_SetTerminalResizeCtx(ssh, (void*)&childFd);
1446-
if (forcedCmd) {
1455+
if (ptyReq == 0 || forcedCmd) {
14471456
close(stdoutPipe[1]);
14481457
close(stderrPipe[1]);
14491458
close(stdinPipe[0]);
@@ -1469,7 +1478,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
14691478

14701479
if (wolfSSH_stream_peek(ssh, tmp, 1) <= 0) {
14711480
/* select on stdout/stderr pipes with forced commands */
1472-
if (forcedCmd) {
1481+
if (ptyReq == 0 || forcedCmd) {
14731482
FD_SET(stdoutPipe[0], &readFds);
14741483
if (stdoutPipe[0] > maxFd)
14751484
maxFd = stdoutPipe[0];
@@ -1515,7 +1524,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15151524
if (cnt_r <= 0)
15161525
break;
15171526

1518-
if (forcedCmd) {
1527+
if (ptyReq == 0 || forcedCmd) {
15191528
cnt_w = (int)write(stdinPipe[1], channelBuffer,
15201529
cnt_r);
15211530
}
@@ -1555,7 +1564,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15551564
current = wolfSSH_ChannelFind(ssh, lastChannel,
15561565
WS_CHANNEL_ID_SELF);
15571566
eof = wolfSSH_ChannelGetEof(current);
1558-
if (eof && forcedCmd) {
1567+
if (eof && (ptyReq == 0 || forcedCmd)) {
15591568
/* SSH is done, close stdin pipe to child process */
15601569
close(stdinPipe[1]);
15611570
stdinPipe[1] = -1;
@@ -1585,7 +1594,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15851594
}
15861595
}
15871596

1588-
if (forcedCmd) {
1597+
if (ptyReq == 0 || forcedCmd) {
15891598
if (FD_ISSET(stderrPipe[0], &readFds)) {
15901599
cnt_r = (int)read(stderrPipe[0], shellBuffer,
15911600
sizeof shellBuffer);
@@ -1725,7 +1734,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
17251734
}
17261735

17271736
/* check for any left over data in pipes then close them */
1728-
if (forcedCmd) {
1737+
if (ptyReq == 0 || forcedCmd) {
17291738
int readSz;
17301739

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

src/internal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8951,6 +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 */
89548955
termSz = (word32)sizeof(term);
89558956
ret = GetString(term, &termSz, buf, len, &begin);
89568957
if (ret == WS_SUCCESS)

src/ssh.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,6 +2539,25 @@ 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+
25422561
const char* wolfSSH_GetSessionCommand(const WOLFSSH* ssh)
25432562
{
25442563
WLOG(WS_LOG_DEBUG, "Entering wolfSSH_GetSessionCommand()");

wolfssh/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ 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 */
916917
#endif
917918
#if defined(WOLFSSH_TERM) || defined(WOLFSSH_SHELL)
918919
word32 exitStatus;

wolfssh/ssh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +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);
236237

237238
/* Channel callbacks */
238239
typedef int (*WS_CallbackChannelOpen)(WOLFSSH_CHANNEL* channel, void* ctx);

0 commit comments

Comments
 (0)