Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.2.4 changes #200

Merged
merged 4 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* [0.2.4](https://github.com/mwiede/jsch/releases/tag/jsch-0.2.4)
* When connections fail due to an algorithm negotiation failure, throw a `JSchAlgoNegoFailException` that extends `JSchException`.
* The new `JSchAlgoNegoFailException` details which specific algorithm negotiation failed, along with what both JSch and the server proposed.
* [0.2.3](https://github.com/mwiede/jsch/releases/tag/jsch-0.2.3)
* #188 fix private key length checks for ssh-ed25519 & ssh-ed448. by @norrisjeremy in https://github.com/mwiede/jsch/pull/189
* [0.2.2](https://github.com/mwiede/jsch/releases/tag/jsch-0.2.2)
Expand Down
168 changes: 168 additions & 0 deletions examples/JumpHosts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/**
* This program will demonstrate SSH through jump hosts.
* Suppose that you don't have direct accesses to host2 and host3.
* $ CLASSPATH=.:../build javac JumpHosts.java
* $ CLASSPATH=.:../build java JumpHosts usr1@host1 usr2@host2 usr3@host3
* You will be asked passwords for those destinations,
* and if everything works fine, you will get file lists of your home-directory
* at host3.
*
*/
import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;

public class JumpHosts {
public static void main(String[] arg){

try{
JSch jsch = new JSch();

if(arg.length <= 1){
System.out.println("This program expects more arguments.");
System.exit(-1);
}

Session session = null;
Session[] sessions = new Session[arg.length];

String host = arg[0];
String user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@')+1);

sessions[0] = session = jsch.getSession(user, host, 22);
session.setUserInfo(new MyUserInfo());
session.connect();
System.out.println("The session has been established to "+user+"@"+host);

for(int i = 1; i < arg.length; i++){
host = arg[i];
user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@')+1);

int assinged_port = session.setPortForwardingL(0, host, 22);
System.out.println("portforwarding: "+
"localhost:"+assinged_port+" -> "+host+":"+22);
sessions[i] = session =
jsch.getSession(user, "127.0.0.1", assinged_port);

session.setUserInfo(new MyUserInfo());
session.setHostKeyAlias(host);
session.connect();
System.out.println("The session has been established to "+
user+"@"+host);
}

ChannelSftp sftp = (ChannelSftp)session.openChannel("sftp");

sftp.connect();
sftp.ls(".",
new ChannelSftp.LsEntrySelector() {
public int select(ChannelSftp.LsEntry le) {
System.out.println(le);
return ChannelSftp.LsEntrySelector.CONTINUE;
}
});
sftp.disconnect();

for(int i = sessions.length-1; i >= 0; i--){
sessions[i].disconnect();
}
}
catch(Exception e){
System.out.println(e);
}
}

public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
public String getPassword(){ return passwd; }
public boolean promptYesNo(String str){
Object[] options={ "yes", "no" };
int foo=JOptionPane.showOptionDialog(null,
str,
"Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
return foo==0;
}

String passwd;
JTextField passwordField=(JTextField)new JPasswordField(20);

public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return true; }
public boolean promptPassword(String message){
Object[] ob={passwordField};
int result=
JOptionPane.showConfirmDialog(null, ob, message,
JOptionPane.OK_CANCEL_OPTION);
if(result==JOptionPane.OK_OPTION){
passwd=passwordField.getText();
return true;
}
else{ return false; }
}
public void showMessage(String message){
JOptionPane.showMessageDialog(null, message);
}
final GridBagConstraints gbc =
new GridBagConstraints(0,0,1,1,1,1,
GridBagConstraints.NORTHWEST,
GridBagConstraints.NONE,
new Insets(0,0,0,0),0,0);
private Container panel;
public String[] promptKeyboardInteractive(String destination,
String name,
String instruction,
String[] prompt,
boolean[] echo){
panel = new JPanel();
panel.setLayout(new GridBagLayout());

gbc.weightx = 1.0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridx = 0;
panel.add(new JLabel(instruction), gbc);
gbc.gridy++;

gbc.gridwidth = GridBagConstraints.RELATIVE;

JTextField[] texts=new JTextField[prompt.length];
for(int i=0; i<prompt.length; i++){
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.weightx = 1;
panel.add(new JLabel(prompt[i]),gbc);

gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 1;
if(echo[i]){
texts[i]=new JTextField(20);
}
else{
texts[i]=new JPasswordField(20);
}
panel.add(texts[i], gbc);
gbc.gridy++;
}

if(JOptionPane.showConfirmDialog(null, panel,
destination+": "+name,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE)
==JOptionPane.OK_OPTION){
String[] response=new String[prompt.length];
for(int i=0; i<prompt.length; i++){
response[i]=texts[i].getText();
}
return response;
}
else{
return null; // cancel
}
}
}
}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-beta0</version>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -177,7 +177,7 @@
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.13</artifactId>
<version>3.2.12</version>
<version>3.2.13</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -610,7 +610,7 @@
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.13.1</version>
<version>2.15.0</version>
</path>
</annotationProcessorPaths>
</configuration>
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/jcraft/jsch/JSchAlgoNegoFailException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.jcraft.jsch;

/**
* Extension of {@link JSchException} to indicate when a connection fails during algorithm
* negotiation.
*/
public class JSchAlgoNegoFailException extends JSchException {

private static final long serialVersionUID = -1L;

private final String algorithmName;
private final String jschProposal;
private final String serverProposal;

JSchAlgoNegoFailException(int algorithmIndex, String jschProposal, String serverProposal) {
super(failString(algorithmIndex, jschProposal, serverProposal));
algorithmName = algorithmNameFromIndex(algorithmIndex);
this.jschProposal = jschProposal;
this.serverProposal = serverProposal;
}

/** Get the algorithm name. */
public String getAlgorithmName() {
return algorithmName;
}

/** Get the JSch algorithm proposal. */
public String getJSchProposal() {
return jschProposal;
}

/** Get the server algorithm proposal. */
public String getServerProposal() {
return serverProposal;
}

private static String failString(int algorithmIndex, String jschProposal, String serverProposal) {
return String.format(
"Algorithm negotiation fail: algorithmName=\"%s\" jschProposal=\"%s\" serverProposal=\"%s\"",
algorithmNameFromIndex(algorithmIndex), jschProposal, serverProposal);
}

private static String algorithmNameFromIndex(int algorithmIndex) {
switch (algorithmIndex) {
case KeyExchange.PROPOSAL_KEX_ALGS:
return "kex";
case KeyExchange.PROPOSAL_SERVER_HOST_KEY_ALGS:
return "server_host_key";
case KeyExchange.PROPOSAL_ENC_ALGS_CTOS:
return "cipher.c2s";
case KeyExchange.PROPOSAL_ENC_ALGS_STOC:
return "cipher.s2c";
case KeyExchange.PROPOSAL_MAC_ALGS_CTOS:
return "mac.c2s";
case KeyExchange.PROPOSAL_MAC_ALGS_STOC:
return "mac.s2c";
case KeyExchange.PROPOSAL_COMP_ALGS_CTOS:
return "compression.c2s";
case KeyExchange.PROPOSAL_COMP_ALGS_STOC:
return "compression.s2c";
case KeyExchange.PROPOSAL_LANG_CTOS:
return "lang.c2s";
case KeyExchange.PROPOSAL_LANG_STOC:
return "lang.s2c";
default:
return "";
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/jcraft/jsch/KeyExchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ protected static String[] guess(Session session, byte[]I_S, byte[]I_C) throws Ex
loop:
while(j<cp.length){
while(j<cp.length && cp[j]!=',')j++;
if(k==j) return null;
if(k==j) throw new JSchAlgoNegoFailException(i, Util.byte2str(cp), Util.byte2str(sp));
String algorithm=Util.byte2str(cp, k, j-k);
int l=0;
int m=0;
while(l<sp.length){
while(l<sp.length && sp[l]!=',')l++;
if(m==l) return null;
if(m==l) throw new JSchAlgoNegoFailException(i, Util.byte2str(cp), Util.byte2str(sp));
if(algorithm.equals(Util.byte2str(sp, m, l-m))){
guess[i]=algorithm;
break loop;
Expand All @@ -144,7 +144,7 @@ protected static String[] guess(Session session, byte[]I_S, byte[]I_C) throws Ex
guess[i]="";
}
else if(guess[i]==null){
return null;
throw new JSchAlgoNegoFailException(i, Util.byte2str(cp), Util.byte2str(sp));
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/jcraft/jsch/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,6 @@ private KeyExchange receive_kexinit(Buffer buf) throws Exception {
}

guess=KeyExchange.guess(this, I_S, I_C);
if(guess==null){
throw new JSchException("Algorithm negotiation fail");
}

if(guess[KeyExchange.PROPOSAL_KEX_ALGS].equals("ext-info-c") ||
guess[KeyExchange.PROPOSAL_KEX_ALGS].equals("ext-info-s")){
Expand Down
Loading