From fcfe7469ceb2c282224db74b11657c783e5e110c Mon Sep 17 00:00:00 2001 From: mpet Date: Fri, 25 Oct 2024 08:26:25 +0200 Subject: [PATCH] Access to banner (#206) * Update TimeoutService.java interrupt hanging timeoutservice threads * Update TimeoutService.java * Update TimeoutService.java * Update Connection.java * Update Connection.java * Update TimeoutService.java * Update TimeoutService.java * Update Connection.java * JENKINS-74733: Not possible to access banner content * JENKINS-74733: Not possible to access banner content --------- Co-authored-by: mpet --- .gitignore | 5 ++++- src/com/trilead/ssh2/Connection.java | 13 +++++++++++++ .../ssh2/auth/AuthenticationManager.java | 17 +++++++++++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index bfb2730..e79ceb2 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,7 @@ nbbuild/ dist/ nbdist/ nbactions.xml -nb-configuration.xml \ No newline at end of file +nb-configuration.xml + +#vscode +.vscode/ \ No newline at end of file diff --git a/src/com/trilead/ssh2/Connection.java b/src/com/trilead/ssh2/Connection.java index 3149cb8..4c87e91 100644 --- a/src/com/trilead/ssh2/Connection.java +++ b/src/com/trilead/ssh2/Connection.java @@ -23,6 +23,8 @@ import java.net.InetSocketAddress; import java.net.SocketTimeoutException; import java.security.SecureRandom; +import java.util.Collections; +import java.util.List; import java.util.Vector; /** @@ -515,6 +517,17 @@ public synchronized boolean authenticateWithPublicKey(String user, File pemFile, return authenticateWithPublicKey(user, cw.toCharArray(), password); } + /** + * Method will return the banners provided by the server. + * {@link AuthenticationManager#getBanners()} + * @return a list of banner strings or empty if no banners is sent. + */ + public List getBanners(){ + if(am != null ){ + return am.getBanners(); + } + return Collections.emptyList(); + } /** * Add a {@link ConnectionMonitor} to this connection. Can be invoked at any diff --git a/src/com/trilead/ssh2/auth/AuthenticationManager.java b/src/com/trilead/ssh2/auth/AuthenticationManager.java index c294567..15ec822 100644 --- a/src/com/trilead/ssh2/auth/AuthenticationManager.java +++ b/src/com/trilead/ssh2/auth/AuthenticationManager.java @@ -14,6 +14,7 @@ import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Vector; @@ -34,7 +35,7 @@ public class AuthenticationManager implements MessageHandler Vector packets = new Vector(); boolean connectionClosed = false; - String banner; + List banners = new ArrayList<>(); String[] remainingMethods = new String[0]; boolean isPartialSuccess = false; @@ -104,9 +105,21 @@ byte[] getNextMessage() throws IOException PacketUserauthBanner sb = new PacketUserauthBanner(msg, 0, msg.length); - banner = sb.getBanner(); + banners.add(sb.getBanner()); } } + /** + * This method contains the SSH_MSG_USERAUTH_BANNER messages + * sent by the server. Messages can be sent at any time before + * SSH protocol starts and the authentication is complete. + * The purpose of the message is to display info to the user + * before authentication starts. + * Note: If there are messages sent make sure authentication + * is complete before using this method. + */ + public List getBanners(){ + return banners; + } public String[] getRemainingMethods(String user) throws IOException {