Skip to content

Commit

Permalink
mwiede#293 allow UserAuthNone to be extended.
Browse files Browse the repository at this point in the history
  • Loading branch information
norrisjeremy committed Mar 25, 2023
1 parent 692bb9c commit 83b28b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* [0.2.9](https://github.com/mwiede/jsch/releases/tag/jsch-0.2.9)
* [#293](https://github.com/mwiede/jsch/issues/293) allow UserAuthNone to be extended.
* [0.2.8](https://github.com/mwiede/jsch/releases/tag/jsch-0.2.8)
* [#287](https://github.com/mwiede/jsch/issues/287) add algorithm type information to algorithm negotiation logs.
* [#289](https://github.com/mwiede/jsch/issues/289) wrap NoClassDefFoundError's for invalid private keys.
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/jcraft/jsch/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,29 +379,29 @@ public void connect(int connectTimeout) throws JSchException {
boolean auth = false;
boolean auth_cancel = false;

UserAuth ua = null;
UserAuthNone uan = null;
try {
Class<? extends UserAuth> c =
Class.forName(getConfig("userauth.none")).asSubclass(UserAuth.class);
ua = c.getDeclaredConstructor().newInstance();
Class<? extends UserAuthNone> c =
Class.forName(getConfig("userauth.none")).asSubclass(UserAuthNone.class);
uan = c.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new JSchException(e.toString(), e);
}

auth = ua.start(this);
auth = uan.start(this);

String cmethods = getConfig("PreferredAuthentications");

String[] cmethoda = Util.split(cmethods, ",");

String smethods = null;
if (!auth) {
smethods = ((UserAuthNone) ua).getMethods();
smethods = uan.getMethods();
if (smethods != null) {
smethods = smethods.toLowerCase();
} else {
// methods: publickey,password,keyboard-interactive
// smethods="publickey,password,keyboard-interactive";
// smethods = "publickey,password,keyboard-interactive";
smethods = cmethods;
}
}
Expand All @@ -426,7 +426,7 @@ public void connect(int connectTimeout) throws JSchException {
continue;
}

// System.err.println(" method: "+method);
// System.err.println(" method: " + method);

if (getLogger().isEnabled(Logger.INFO)) {
String str = "Authentications that can continue: ";
Expand All @@ -439,7 +439,7 @@ public void connect(int connectTimeout) throws JSchException {
getLogger().log(Logger.INFO, "Next authentication method: " + method);
}

ua = null;
UserAuth ua = null;
try {
Class<? extends UserAuth> c = null;
if (getConfig("userauth." + method) != null) {
Expand Down Expand Up @@ -468,16 +468,16 @@ public void connect(int connectTimeout) throws JSchException {
if (!tmp.equals(smethods)) {
methodi = 0;
}
// System.err.println("PartialAuth: "+methods);
// System.err.println("PartialAuth: " + methods);
auth_cancel = false;
continue loop;
} catch (RuntimeException ee) {
throw ee;
} catch (JSchException ee) {
throw ee;
} catch (Exception ee) {
// System.err.println("ee: "+ee); // SSH_MSG_DISCONNECT: 2 Too many authentication
// failures
// SSH_MSG_DISCONNECT: Too many authentication failures
// System.err.println("ee: " + ee);
if (getLogger().isEnabled(Logger.WARN)) {
getLogger().log(Logger.WARN,
"an exception during authentication\n" + ee.toString());
Expand Down
35 changes: 20 additions & 15 deletions src/main/java/com/jcraft/jsch/UserAuthNone.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@

package com.jcraft.jsch;

class UserAuthNone extends UserAuth {
private static final int SSH_MSG_SERVICE_ACCEPT = 6;
private String methods = null;
public class UserAuthNone extends UserAuth {
protected static final int SSH_MSG_SERVICE_REQUEST = 5;
protected static final int SSH_MSG_SERVICE_ACCEPT = 6;

private String methods;

@Override
public boolean start(Session session) throws Exception {
super.start(session);


// send
// byte SSH_MSG_SERVICE_REQUEST(5)
// string service name "ssh-userauth"
packet.reset();
buf.putByte((byte) Session.SSH_MSG_SERVICE_REQUEST);
buf.putByte((byte) SSH_MSG_SERVICE_REQUEST);
buf.putString(Util.str2byte("ssh-userauth"));
session.write(packet);

Expand Down Expand Up @@ -73,7 +74,7 @@ public boolean start(Session session) throws Exception {
// string service name ("ssh-connection")
// string "none"
packet.reset();
buf.putByte((byte) SSH_MSG_USERAUTH_REQUEST);
buf.putByte((byte) UserAuth.SSH_MSG_USERAUTH_REQUEST);
buf.putString(_username);
buf.putString(Util.str2byte("ssh-connection"));
buf.putString(Util.str2byte("none"));
Expand All @@ -83,10 +84,10 @@ public boolean start(Session session) throws Exception {
buf = session.read(buf);
command = buf.getCommand() & 0xff;

if (command == SSH_MSG_USERAUTH_SUCCESS) {
if (command == UserAuth.SSH_MSG_USERAUTH_SUCCESS) {
return true;
}
if (command == SSH_MSG_USERAUTH_BANNER) {
if (command == UserAuth.SSH_MSG_USERAUTH_BANNER) {
buf.getInt();
buf.getByte();
buf.getByte();
Expand All @@ -101,30 +102,34 @@ public boolean start(Session session) throws Exception {
}
continue loop;
}
if (command == SSH_MSG_USERAUTH_FAILURE) {
if (command == UserAuth.SSH_MSG_USERAUTH_FAILURE) {
buf.getInt();
buf.getByte();
buf.getByte();
byte[] foo = buf.getString();
int partial_success = buf.getByte();
methods = Util.byte2str(foo);
// System.err.println("UserAuthNONE: "+methods+
// " partial_success:"+(partial_success!=0));
// if(partial_success!=0){
setMethods(Util.byte2str(foo));
// System.err.println("UserAuthNone: " + methods + " partial_success:" + (partial_success !=
// 0));
// if (partial_success != 0) {
// throw new JSchPartialAuthException(new String(foo));
// }

break;
} else {
// System.err.println("USERAUTH fail ("+command+")");
// System.err.println("USERAUTH fail (" + command + ")");
throw new JSchException("USERAUTH fail (" + command + ")");
}
}
// throw new JSchException("USERAUTH fail");
return false;
}

String getMethods() {
protected String getMethods() {
return methods;
}

protected void setMethods(String methods) {
this.methods = methods;
}
}

0 comments on commit 83b28b1

Please sign in to comment.