Skip to content

Commit 468a205

Browse files
authored
Merge pull request #719 from JacobBarthelmeh/cat
cat of large file with ssh shell
2 parents 40aabc2 + 2fbe010 commit 468a205

5 files changed

Lines changed: 123 additions & 29 deletions

File tree

apps/wolfsshd/test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To run all tests do:
1010
```
1111
$ cd apps/wolfsshd/test/
1212
13-
$ sudo ./run_all_sshd_tests.sh
13+
$ sudo ./run_all_sshd_tests.sh <user>
1414
Running all wolfSSHd tests
1515
Starting up local wolfSSHd for tests on 127.0.0.1:22222
1616
SSHD running on PID 7979

apps/wolfsshd/test/run_all_sshd_tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ fi
7676
# these tests require setting up an sshd
7777
if [ "$USING_LOCAL_HOST" == 1 ]; then
7878
run_test "sshd_forcedcmd_test.sh"
79+
run_test "sshd_window_full_test.sh"
7980
else
8081
printf "Skipping tests that need to setup local SSHD\n"
81-
SKIPPED=$((SKIPPED+1))
82+
SKIPPED=$((SKIPPED+2))
8283
fi
8384

8485
# these tests run with X509 sshd-config loaded
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
3+
# sshd local test
4+
5+
if [ -z "$1" ] || [ -z "$2" ]; then
6+
echo "expecting host and port as arguments"
7+
echo "./sshd_exec_test.sh 127.0.0.1 22222"
8+
exit 1
9+
fi
10+
11+
PWD=`pwd`
12+
13+
if [ ! -z "$3" ]; then
14+
USER="$3"
15+
else
16+
USER=`whoami`
17+
fi
18+
TEST_PORT="$2"
19+
TEST_HOST="$1"
20+
source ./start_sshd.sh
21+
cat <<EOF > sshd_config_test_window
22+
Port $TEST_PORT
23+
Protocol 2
24+
LoginGraceTime 600
25+
PermitRootLogin yes
26+
PasswordAuthentication yes
27+
PermitEmptyPasswords no
28+
UsePrivilegeSeparation no
29+
UseDNS no
30+
HostKey $PWD/../../../keys/server-key.pem
31+
AuthorizedKeysFile $PWD/authorized_keys_test
32+
EOF
33+
34+
start_wolfsshd "sshd_config_test_window"
35+
cd ../../..
36+
37+
TEST_CLIENT="./examples/client/client"
38+
TEST_SFTP="./examples/sftpclient/wolfsftp"
39+
PRIVATE_KEY="./keys/hansel-key-ecc.der"
40+
PUBLIC_KEY="./keys/hansel-key-ecc.pub"
41+
42+
head -c 1G /dev/urandom > random-test.txt
43+
44+
PWD=`pwd`
45+
$TEST_CLIENT -c "cd $PWD; $TEST_CLIENT -c \"cat $PWD/random-test.txt\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT > random-test-result.txt
46+
47+
diff random-test.txt random-test-result.txt
48+
RESULT=$?
49+
if [ "$RESULT" != 0 ]; then
50+
echo "cat did not pass through all expected data"
51+
ls -la random-test.txt
52+
ls -la random-test-result.txt
53+
exit 1
54+
fi
55+
56+
stop_wolfsshd
57+
exit 0
58+

apps/wolfsshd/wolfsshd.c

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,10 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
11731173
byte shellBuffer[EXAMPLE_BUFFER_SZ];
11741174
byte channelBuffer[EXAMPLE_BUFFER_SZ];
11751175
char* forcedCmd;
1176-
int windowFull = 0;
1176+
int windowFull = 0; /* Contains size of bytes from shellBuffer that did
1177+
* not get passed on to wolfSSH yet. This happens
1178+
* with window full errors or when rekeying. */
1179+
int wantWrite = 0;
11771180
int peerConnected = 1;
11781181
int stdoutEmpty = 0;
11791182

@@ -1423,7 +1426,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
14231426
maxFd = sshFd;
14241427

14251428
FD_ZERO(&writeFds);
1426-
if (windowFull) {
1429+
if (windowFull || wantWrite) {
14271430
FD_SET(sshFd, &writeFds);
14281431
}
14291432

@@ -1452,10 +1455,10 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
14521455
pending = 1; /* found some pending SSH data */
14531456
}
14541457

1455-
if (windowFull || pending || FD_ISSET(sshFd, &readFds)) {
1458+
if (wantWrite || windowFull || pending || FD_ISSET(sshFd, &readFds)) {
14561459
word32 lastChannel = 0;
14571460

1458-
windowFull = 0;
1461+
wantWrite = 0;
14591462
/* The following tries to read from the first channel inside
14601463
the stream. If the pending data in the socket is for
14611464
another channel, this will return an error with id
@@ -1466,24 +1469,31 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
14661469
if (cnt_r < 0) {
14671470
rc = wolfSSH_get_error(ssh);
14681471
if (rc == WS_CHAN_RXD) {
1469-
if (lastChannel == shellChannelId) {
1470-
cnt_r = wolfSSH_ChannelIdRead(ssh, shellChannelId,
1472+
if (!windowFull) { /* don't rewrite channeldBuffer if full
1473+
* of windowFull left overs */
1474+
if (lastChannel == shellChannelId) {
1475+
cnt_r = wolfSSH_ChannelIdRead(ssh, shellChannelId,
14711476
channelBuffer,
14721477
sizeof channelBuffer);
1473-
if (cnt_r <= 0)
1474-
break;
1475-
cnt_w = (int)write(childFd,
1476-
channelBuffer, cnt_r);
1477-
if (cnt_w <= 0)
1478-
break;
1478+
if (cnt_r <= 0)
1479+
break;
1480+
cnt_w = (int)write(childFd,
1481+
channelBuffer, cnt_r);
1482+
if (cnt_w <= 0)
1483+
break;
1484+
}
14791485
}
14801486
}
14811487
else if (rc == WS_CHANNEL_CLOSED) {
14821488
peerConnected = 0;
14831489
continue;
14841490
}
14851491
else if (rc == WS_WANT_WRITE) {
1486-
windowFull = 1;
1492+
wantWrite = 1;
1493+
continue;
1494+
}
1495+
else if (rc == WS_REKEYING) {
1496+
wantWrite = 1;
14871497
continue;
14881498
}
14891499
else if (rc != WS_WANT_READ) {
@@ -1495,17 +1505,22 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
14951505
/* if the window was previously full, try resending the data */
14961506
if (windowFull) {
14971507
cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId,
1498-
shellBuffer, cnt_r);
1499-
if (cnt_w == WS_WINDOW_FULL) {
1500-
windowFull = 1;
1508+
shellBuffer, windowFull);
1509+
if (cnt_w == WS_WINDOW_FULL || cnt_w == WS_REKEYING) {
15011510
continue;
15021511
}
15031512
else if (cnt_w == WS_WANT_WRITE) {
1504-
windowFull = 1;
1513+
wantWrite = 1;
15051514
continue;
15061515
}
15071516
else {
1508-
windowFull = 0;
1517+
windowFull -= cnt_w;
1518+
if (windowFull > 0) {
1519+
WMEMMOVE(shellBuffer, shellBuffer + cnt_w, windowFull);
1520+
continue;
1521+
}
1522+
if (windowFull < 0)
1523+
windowFull = 0;
15091524
}
15101525
}
15111526

@@ -1524,12 +1539,18 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15241539
if (cnt_r > 0) {
15251540
cnt_w = wolfSSH_extended_data_send(ssh, shellBuffer,
15261541
cnt_r);
1527-
if (cnt_w == WS_WINDOW_FULL) {
1528-
windowFull = 1;
1542+
if (cnt_w > 0 && cnt_w < cnt_r) { /* partial send */
1543+
windowFull = cnt_r - cnt_w;
1544+
WMEMMOVE(shellBuffer, shellBuffer + cnt_w,
1545+
windowFull);
1546+
}
1547+
else if (cnt_w == WS_WINDOW_FULL ||
1548+
cnt_w == WS_REKEYING) {
1549+
windowFull = cnt_r; /* save amount to be sent */
15291550
continue;
15301551
}
15311552
else if (cnt_w == WS_WANT_WRITE) {
1532-
windowFull = 1;
1553+
wantWrite = 1;
15331554
continue;
15341555
}
15351556
else if (cnt_w < 0)
@@ -1556,12 +1577,18 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15561577
if (cnt_r > 0) {
15571578
cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId,
15581579
shellBuffer, cnt_r);
1559-
if (cnt_w == WS_WINDOW_FULL) {
1560-
windowFull = 1;
1580+
if (cnt_w > 0 && cnt_w < cnt_r) { /* partial send */
1581+
windowFull = cnt_r - cnt_w;
1582+
WMEMMOVE(shellBuffer, shellBuffer + cnt_w,
1583+
windowFull);
1584+
}
1585+
else if (cnt_w == WS_WINDOW_FULL ||
1586+
cnt_w == WS_REKEYING) {
1587+
windowFull = cnt_r; /* save amount to be sent */
15611588
continue;
15621589
}
15631590
else if (cnt_w == WS_WANT_WRITE) {
1564-
windowFull = 1;
1591+
wantWrite = 1;
15651592
continue;
15661593
}
15671594
else if (cnt_w < 0) {
@@ -1586,12 +1613,18 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
15861613
if (cnt_r > 0) {
15871614
cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId,
15881615
shellBuffer, cnt_r);
1589-
if (cnt_w == WS_WINDOW_FULL) {
1590-
windowFull = 1;
1616+
if (cnt_w > 0 && cnt_w < cnt_r) { /* partial send */
1617+
windowFull = cnt_r - cnt_w;
1618+
WMEMMOVE(shellBuffer, shellBuffer + cnt_w,
1619+
windowFull);
1620+
}
1621+
else if (cnt_w == WS_WINDOW_FULL ||
1622+
cnt_w == WS_REKEYING) {
1623+
windowFull = cnt_r;
15911624
continue;
15921625
}
15931626
else if (cnt_w == WS_WANT_WRITE) {
1594-
windowFull = 1;
1627+
wantWrite = 1;
15951628
continue;
15961629
}
15971630
else if (cnt_w < 0) {

examples/client/common.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,10 @@ int ClientUserAuth(byte authType,
468468
* passed in a public key file, use public key auth */
469469
if (pubKeyLoaded == 1) {
470470
if (authType == WOLFSSH_USERAUTH_PASSWORD) {
471+
#ifdef WOLFSSH_DEBUG
471472
printf("rejecting password type with %s in favor of pub key\n",
472473
(char*)authData->username);
474+
#endif
473475
return WOLFSSH_USERAUTH_FAILURE;
474476
}
475477
}

0 commit comments

Comments
 (0)