Skip to content

Commit

Permalink
Merge pull request #627 from JacobBarthelmeh/term
Browse files Browse the repository at this point in the history
add terminal resize callback for unix like use
  • Loading branch information
ejohnstown authored Dec 1, 2023
2 parents e2ee49f + 7e300b5 commit 76c6c99
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 8 deletions.
5 changes: 3 additions & 2 deletions apps/wolfsshd/test/run_all_sshd_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ else
start_wolfsshd "sshd_config_test"
if [ -z "$PID" ]; then
echo "Issue starting up wolfSSHd"
exit -1
exit 1
fi
fi

Expand All @@ -46,11 +46,12 @@ run_test() {
printf "Shutting down test wolfSSHd\n"
stop_wolfsshd
fi
exit -1
exit 1
fi
}

run_test "sshd_exec_test.sh"
run_test "sshd_term_size_test.sh"

# add aditional tests here, check on var USING_LOCAL_HOST if can make sshd
# server start/restart with changes
Expand Down
98 changes: 98 additions & 0 deletions apps/wolfsshd/test/sshd_term_size_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash

# sshd local test

pushd ../../..

TEST_CLIENT="./examples/client/client"
USER=`whoami`
PRIVATE_KEY="./keys/hansel-key-ecc.der"
PUBLIC_KEY="./keys/hansel-key-ecc.pub"

if [ -z "$1" ] || [ -z "$2" ]; then
echo "expecting host and port as arguments"
echo "./sshd_exec_test.sh 127.0.0.1 22222"
exit 1
fi

set -e
echo "Creating tmux session at $PWD with command :"
tmux new-session -d -s test "$TEST_CLIENT -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\""

# give the command a second to establish SSH connection
sleep 0.5

COL=`tmux display -p -t test '#{pane_width}'`
ROW=`tmux display -p -t test '#{pane_height}'`

# get the terminals columns and lines
tmux send-keys -t test 'echo col=$COLUMNS row=$LINES'
tmux send-keys -t test 'ENTER'
tmux capture-pane -t test
RESULT=`tmux show-buffer | grep -v echo | grep -v rejecting | grep "col="`

echo "$RESULT"
echo ""
echo ""
ROW_FOUND=`echo "$RESULT" | sed -e 's/.*[^0-9]\([0-9]\+\)[^0-9]*$/\1/'`
COL_FOUND=`echo "$RESULT" | sed -r 's/^[^0-9]*([0-9]+).*$/\1/'`

if [ "$COL" != "$COL_FOUND" ]; then
echo "Col found was $COL_FOUND which does not match expected $COL"
exit 1
fi

if [ "$ROW" != "$ROW_FOUND" ]; then
echo "Row found was $ROW_FOUND which does not match expected $ROW"
exit 1
fi

# resize tmux after connection is open is not working @TODO
#tmux set-window-option -g aggressive-resize
#printf '\e[8;50;100t'
#tmux resize-pane -x 50 -y 10 -t test

# close down the SSH session
tmux send-keys -t test 'exit'
tmux send-keys -t test 'ENTER'
set +e

# kill off the session if it's still running, but don't error out if the session
# has already closed down
tmux kill-session -t test
set -e

tmux new-session -d -x 50 -y 10 -s test "$TEST_CLIENT -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\""

# give the command a second to establish SSH connection
sleep 0.5

echo "New COL=$COL ROW=$ROW"

tmux send-keys -t test 'echo col=$COLUMNS row=$LINES'
tmux send-keys -t test 'ENTER'
tmux capture-pane -t test
RESULT=`tmux show-buffer | grep -v echo | grep -v rejecting | grep "col="`

ROW_FOUND=`echo "$RESULT" | sed -e 's/.*[^0-9]\([0-9]\+\)[^0-9]*$/\1/'`
COL_FOUND=`echo "$RESULT" | sed -r 's/^[^0-9]*([0-9]+).*$/\1/'`

if [ "50" != "$COL_FOUND" ]; then
echo "Col found was $COL_FOUND which does not match expected 50"
exit 1
fi

if [ "10" != "$ROW_FOUND" ]; then
echo "Row found was $ROW_FOUND which does not match expected 10"
exit 1
fi

# close down the SSH session
tmux send-keys -t test 'exit'
tmux send-keys -t test 'ENTER'
set +e
tmux kill-session -t test

popd
exit 0

18 changes: 18 additions & 0 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,9 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
return ret;
}
#else
#if defined(HAVE_SYS_IOCTL_H)
#include <sys/ioctl.h>
#endif

/* handles creating a new shell env. and maintains SSH connection for incoming
* user input as well as output of the shell.
Expand Down Expand Up @@ -1314,6 +1317,21 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
return WS_FATAL_ERROR;
}

/* set initial size of terminal based on saved size */
#if defined(HAVE_SYS_IOCTL_H)
{
struct winsize s;

WMEMSET(&s, 0, sizeof s);
s.ws_col = ssh->curX;
s.ws_row = ssh->curY;
s.ws_xpixel = ssh->curXP;
s.ws_ypixel = ssh->curYP;
ioctl(childFd, TIOCSWINSZ, &s);
}
#endif

wolfSSH_SetTerminalResizeCtx(ssh, (void*)&childFd);
while (ChildRunning) {
byte tmp[2];
fd_set readFds;
Expand Down
41 changes: 35 additions & 6 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,9 @@ void CtxResourceFree(WOLFSSH_CTX* ctx)
#ifdef WOLFSSH_TERM
/* default terminal resize handling callbacks */

#if defined(USE_WINDOWS_API) && defined(WOLFSSH_SSHD)
static int WS_WindowsTermResize(WOLFSSH* ssh, word32 col, word32 row, word32 colP,
#if defined(WOLFSSH_SSHD) && !defined(WOLFSSH_RESIZE_NO_DEFUALT)
#if defined(USE_WINDOWS_API)
static int WS_TermResize(WOLFSSH* ssh, word32 col, word32 row, word32 colP,
word32 rowP, void* usrCtx)
{
HPCON* term = (HPCON*)usrCtx;
Expand All @@ -667,7 +668,33 @@ static int WS_WindowsTermResize(WOLFSSH* ssh, word32 col, word32 row, word32 col

return ret;
}
#elif defined(HAVE_SYS_IOCTL_H)

#include <sys/ioctl.h>
static int WS_TermResize(WOLFSSH* ssh, word32 col, word32 row, word32 colP,
word32 rowP, void* usrCtx)
{
struct winsize s;
int ret = WS_SUCCESS;
int* fd = (int*)usrCtx;

if (fd != NULL) {
WMEMSET(&s, 0, sizeof s);
s.ws_row = row;
s.ws_col = col;
s.ws_xpixel = colP;
s.ws_ypixel = rowP;

ioctl(*fd, TIOCSWINSZ, &s);
}

(void)ssh;
return ret;
}
#else
#define WOLFSSH_RESIZE_NO_DEFUALT
#endif
#endif /* WOLFSSH_SSHD */

#endif /* WOLFSSH_TERM */

Expand Down Expand Up @@ -763,10 +790,10 @@ WOLFSSH* SshInit(WOLFSSH* ssh, WOLFSSH_CTX* ctx)
ssh->agentEnabled = ctx->agentEnabled;
#endif

#ifdef WOLFSSH_TERM
#if defined(USE_WINDOWS_API) && defined(WOLFSSH_SSHD)
ssh->termResizeCb = WS_WindowsTermResize;
#endif
#if defined(WOLFSSH_TERM) && defined(WOLFSSH_SSHD)
#ifndef WOLFSSH_RESIZE_NO_DEFUALT
ssh->termResizeCb = WS_TermResize;
#endif
#endif

if (BufferInit(&ssh->inputBuffer, 0, ctx->heap) != WS_SUCCESS ||
Expand Down Expand Up @@ -7171,6 +7198,8 @@ static int DoChannelRequest(WOLFSSH* ssh,
WLOG(WS_LOG_DEBUG, " heightPixels = %u", heightPixels);
ssh->curX = widthChar;
ssh->curY = heightRows;
ssh->curXP = widthPixels;
ssh->curYP = heightPixels;
if (ssh->termResizeCb) {
if (ssh->termResizeCb(ssh, widthChar, heightRows,
widthPixels, heightPixels, ssh->termCtx)
Expand Down
2 changes: 2 additions & 0 deletions wolfssh/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,8 @@ struct WOLFSSH {
void* termCtx;
word32 curX; /* current terminal width */
word32 curY; /* current terminal height */
word32 curXP; /* pixel width */
word32 curYP; /* pixel height */
#endif
};

Expand Down

0 comments on commit 76c6c99

Please sign in to comment.