Apache MINA SSHD - Problems with authentication method when connecting to server
Asked Answered
H

1

2

I am trying to follow Apache MINA's guide for setting up a SSHD server but I run into problems when connecting to it with PuTTY. I get to enter a username but then get slapped with the following error message: enter image description here

Below is the code for my server. Do I have to manually set an authentication method for the server? Or am I doing something else wrong?

sshd = SshServer.setUpDefaultServer();
sshd.setPort(3333);

List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
sshd.setUserAuthFactories(userAuthFactories);

sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

sshd.start();
Haulm answered 9/9, 2013 at 8:17 Comment(0)
C
7

i just created a sample which works for me. i use Putty to connect to the Apache SSHD. Lib version i used was 'apache-sshd-0.9.0' (downloaded on:2014-02-04). JDK 1.7.

the main of my sample looks like:

public static void main(String[] args) throws IOException
{
  SshServer sshd = SshServer.setUpDefaultServer();
  sshd.setPort(22);
  sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("./keys/mysample", 
            SimpleGeneratorHostKeyProvider.SSH_RSA));

  List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
  userAuthFactories.add(new UserAuthPassword.Factory());
  sshd.setUserAuthFactories(userAuthFactories);

  sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
  public boolean authenticate(String username, String password, ServerSession session) {
    return "tomek".equals(username) && "123".equals(password);
    }
  });

  sshd.setShellFactory(new MyCommandFactory());

  sshd.start();
  while (true) ;
}

as you can see, i created my own command factory like this: (btw. this question on SO was very helpful for me)

public class MyCommandFactory  implements CommandFactory, Factory<Command>
{
  public Command createCommand(String s)
  {
    System.out.println("command requested: " + s);
    return new MyCommand(s);
  }

  public Command create()
  {
    return createCommand("none");
  }
}

for test purpose create a command like this:

public class MyCommand implements Command
{

  private String name;

  private InputStream inputStream;
  private OutputStream outputStream;
  private ExitCallback exitCallback;

  public MyCommand(String name)
  {
    this.name = name;
  }

  public void setInputStream(final InputStream inputStream)
  {
    System.out.println("input stream was set on command: " + name);
    this.inputStream = inputStream;
  }

  public void setOutputStream(final OutputStream outputStream)
  {
    System.out.println("output stream was set on command: " + name);
    this.outputStream = outputStream;
  }

  public void setErrorStream(OutputStream outputStream)
  {
    System.out.println("error stream was set on command: " + name);
  }

  public void setExitCallback(ExitCallback exitCallback)
  {
    System.out.println("exit callback was set on command: " + name);
    this.exitCallback = exitCallback;
  }

  public void start(Environment environment) throws IOException
  {
    try
    {

      final PrintWriter writer = new PrintWriter(outputStream, true);
      writer.println("Welcome to Java SSH echo server!");
      writer.flush();

      // ... here you gonna read input from remote client ...
      // use writer to write back responses
    }
    catch (final Exception e)
    {
      e.printStackTrace();
    }
  }

  public void destroy()
  {
    System.out.println("destroy was called on command: " + name);
    // client or server closed connection, clean up resources here
  }
}

as you can see, i used a key file './keys/mysample'. to create such a key file try this: (btw. this question on SO was helpful for that)

keytool -genkey -keystore ./keys/mysample" -keyalg RSA

and voila:

result of this sample

i hope this is helpful for someone.

[EDIT] take a look here for a even better echo server: http://svn.apache.org/repos/asf/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/EchoShellFactory.java

Capacity answered 4/2, 2014 at 13:30 Comment(1)
Hi, This is exactly what I am looking for. But can you tell me how do I run this? as in, do I need to run the java class which starts a thread on server and then try making a ssh connection with the user(tomek in your example) and password to trigger this thread? I need to implement two costume commands for my projectPint

© 2022 - 2024 — McMap. All rights reserved.