1 package org.opendaylight.controller.netconf.ssh;
4 import ch.ethz.ssh2.AuthenticationResult;
5 import ch.ethz.ssh2.PtySettings;
6 import ch.ethz.ssh2.ServerAuthenticationCallback;
7 import ch.ethz.ssh2.ServerConnection;
8 import ch.ethz.ssh2.ServerConnectionCallback;
9 import ch.ethz.ssh2.ServerSession;
10 import ch.ethz.ssh2.ServerSessionCallback;
11 import ch.ethz.ssh2.SimpleServerSessionCallback;
12 import com.google.common.base.Optional;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import java.io.IOException;
15 import java.net.InetSocketAddress;
16 import java.net.Socket;
17 import java.nio.ByteBuffer;
18 import javax.net.ssl.SSLContext;
19 import org.opendaylight.controller.netconf.client.NetconfClient;
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
21 import org.opendaylight.controller.netconf.client.NetconfClientSession;
22 import org.opendaylight.controller.netconf.ssh.authentication.RSAKey;
23 import org.opendaylight.controller.netconf.ssh.handler.SSHChannelInboundHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 public class SocketThread implements Runnable, ServerAuthenticationCallback, ServerConnectionCallback
31 private Socket socket;
32 private static final String USER = "netconf";
33 private static final String PASSWORD = "netconf";
34 private NetconfClient netconfClient;
35 private static final InetSocketAddress clientAddress = new InetSocketAddress("127.0.0.1", 12023);
36 private static final Logger logger = LoggerFactory.getLogger(SocketThread.class);
39 private static ServerConnection conn = null;
41 public static void start(Socket socket) throws IOException{
42 new Thread(new SocketThread(socket)).start();
44 private SocketThread(Socket socket) throws IOException {
48 conn = new ServerConnection(socket);
49 RSAKey keyStore = new RSAKey();
50 conn.setRsaHostKey(keyStore.getPrivateKey());
51 conn.setAuthenticationCallback(this);
52 conn.setServerConnectionCallback(this);
60 public ServerSessionCallback acceptSession(final ServerSession session)
62 SimpleServerSessionCallback cb = new SimpleServerSessionCallback()
65 public Runnable requestSubsystem(ServerSession ss, final String subsystem) throws IOException
67 return new Runnable(){
70 if (subsystem.equals("netconf")){
71 logger.info("netconf subsystem received");
73 NetconfClientDispatcher clientDispatcher = null;
74 NioEventLoopGroup nioGrup = new NioEventLoopGroup(1);
75 clientDispatcher = new NetconfClientDispatcher(Optional.<SSLContext>absent(), nioGrup, nioGrup);
76 logger.info("dispatcher created");
77 netconfClient = new NetconfClient("ssh_" + clientAddress.toString(),clientAddress,5000,clientDispatcher);
78 logger.info("netconf client created");
79 } catch (Throwable t){
80 logger.error(t.getMessage(),t);
87 public Runnable requestPtyReq(final ServerSession ss, final PtySettings pty) throws IOException
93 System.out.println("Client requested " + pty.term + " pty");
99 public Runnable requestShell(final ServerSession ss) throws IOException
101 return new Runnable()
107 try (NetconfClientSession session = netconfClient.getClientSession())
109 session.getChannel().pipeline().addLast(new SSHChannelInboundHandler(ss));
110 byte[] bytes = new byte[1024];
113 int size = ss.getStdout().read(bytes);
116 System.err.println("SESSION EOF");
119 session.getChannel().write(ByteBuffer.wrap(bytes,0,size));
124 catch (IOException e)
126 System.err.println("SESSION DOWN");
137 public String initAuthentication(ServerConnection sc)
142 public String[] getRemainingAuthMethods(ServerConnection sc)
144 return new String[] { ServerAuthenticationCallback.METHOD_PASSWORD,
145 ServerAuthenticationCallback.METHOD_PUBLICKEY };
148 public AuthenticationResult authenticateWithNone(ServerConnection sc, String username)
150 return AuthenticationResult.FAILURE;
153 public AuthenticationResult authenticateWithPassword(ServerConnection sc, String username, String password)
155 if (USER.equals(username) && PASSWORD.equals(password))
156 return AuthenticationResult.SUCCESS;
158 return AuthenticationResult.FAILURE;
161 public AuthenticationResult authenticateWithPublicKey(ServerConnection sc, String username, String algorithm,
162 byte[] publickey, byte[] signature)
164 return AuthenticationResult.FAILURE;