From 734c05f75f074db6af75572677b4b298af1930c8 Mon Sep 17 00:00:00 2001 From: Billy Keyes Date: Fri, 13 Mar 2015 15:03:36 -0700 Subject: [PATCH] Detect end-of-stream in TransportImpl#init OpenSSH will drop connections based on the value of MaxStartups when there are too many unauthenticated connection. When this happens, reads on the client socket return -1, which was previously inserted into the identification buffer, leading to the error in #118. --- src/main/java/net/schmizz/sshj/transport/TransportImpl.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/schmizz/sshj/transport/TransportImpl.java b/src/main/java/net/schmizz/sshj/transport/TransportImpl.java index acccaaed0..cdf3382ed 100644 --- a/src/main/java/net/schmizz/sshj/transport/TransportImpl.java +++ b/src/main/java/net/schmizz/sshj/transport/TransportImpl.java @@ -136,7 +136,10 @@ public void init(String remoteHost, int remotePort, InputStream in, OutputStream // Read server's ID final Buffer.PlainBuffer buf = new Buffer.PlainBuffer(); while ((serverID = readIdentification(buf)).isEmpty()) { - buf.putByte((byte) connInfo.in.read()); + int b = connInfo.in.read(); + if (b == -1) + throw new TransportException("Server closed connection during identification exchange"); + buf.putByte((byte) b); } log.info("Server identity string: {}", serverID);