1 19 20 package org.netbeans.modules.versioning.system.cvss; 21 22 import org.netbeans.lib.cvsclient.connection.AbstractConnection; 23 import org.netbeans.lib.cvsclient.connection.ConnectionModifier; 24 import org.netbeans.lib.cvsclient.connection.AuthenticationException; 25 import org.netbeans.lib.cvsclient.command.CommandAbortedException; 26 import org.netbeans.lib.cvsclient.util.LoggedDataInputStream; 27 import org.netbeans.lib.cvsclient.util.LoggedDataOutputStream; 28 29 import javax.net.SocketFactory; 30 import java.io.*; 31 import java.util.*; 32 import java.net.Socket ; 33 import java.net.UnknownHostException ; 34 35 import com.jcraft.jsch.*; 36 import org.openide.util.NbBundle; 37 38 43 public class SSHConnection extends AbstractConnection { 44 45 private static final String CVS_SERVER_COMMAND = System.getenv("CVS_SERVER") != null? 46 System.getenv("CVS_SERVER") + " server": "cvs server"; 48 private final SocketFactory socketFactory; 49 private final String host; 50 private final int port; 51 private final String username; 52 private final String password; 53 54 private Session session; 55 private ChannelExec channel; 56 57 66 public SSHConnection(SocketFactory socketFactory, String host, int port, String username, String password) { 67 this.socketFactory = socketFactory; 68 this.host = host; 69 this.port = port; 70 this.username = username != null ? username : System.getProperty("user.name"); this.password = password; 72 } 73 74 public void open() throws AuthenticationException, CommandAbortedException { 75 76 Properties props = new Properties(); 77 props.put("StrictHostKeyChecking", "no"); 79 JSch jsch = new JSch(); 80 try { 81 session = jsch.getSession(username, host, port); 82 session.setSocketFactory(new SocketFactoryBridge()); 83 session.setConfig(props); 84 session.setUserInfo(new SSHUserInfo()); 85 session.connect(); 86 } catch (JSchException e) { 87 throw new AuthenticationException(e, NbBundle.getMessage(SSHConnection.class, "BK3001")); 88 } 89 90 try { 91 channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(CVS_SERVER_COMMAND); 93 setInputStream(new LoggedDataInputStream(new SshChannelInputStream(channel))); 94 setOutputStream(new LoggedDataOutputStream(channel.getOutputStream())); 95 channel.connect(); 96 } catch (JSchException e) { 97 IOException ioe = new IOException(NbBundle.getMessage(SSHConnection.class, "BK3002")); 98 ioe.initCause(e); 99 throw new AuthenticationException(ioe, NbBundle.getMessage(SSHConnection.class, "BK3003")); 100 } catch (IOException e) { 101 throw new AuthenticationException(e, NbBundle.getMessage(SSHConnection.class, "BK3003")); 102 } 103 } 104 105 110 public void verify() throws AuthenticationException { 111 try { 112 open(); 113 try { 114 Thread.sleep(1000); 115 } catch (InterruptedException e) { 116 } 118 if (channel.getExitStatus() != -1) { 119 throw new AuthenticationException(CVS_SERVER_COMMAND, NbBundle.getMessage(SSHConnection.class, "BK3004", CVS_SERVER_COMMAND)); 120 } 121 close(); 122 } catch (CommandAbortedException e) { 123 throw new AuthenticationException(e, NbBundle.getMessage(SSHConnection.class, "BK3005")); 124 } catch (IOException e) { 125 throw new AuthenticationException(e, NbBundle.getMessage(SSHConnection.class, "Bk3006")); 126 } finally { 127 reset(); 128 } 129 } 130 131 private void reset() { 132 session = null; 133 channel = null; 134 setInputStream(null); 135 setOutputStream(null); 136 } 137 138 public void close() throws IOException { 139 if (channel != null) channel.disconnect(); 140 if (session != null) session.disconnect(); 141 reset(); 142 } 143 144 public boolean isOpen() { 145 return channel != null && channel.isConnected(); 146 } 147 148 public int getPort() { 149 return port; 150 } 151 152 public void modifyInputStream(ConnectionModifier modifier) throws IOException { 153 modifier.modifyInputStream(getInputStream()); 154 } 155 156 public void modifyOutputStream(ConnectionModifier modifier) throws IOException { 157 modifier.modifyOutputStream(getOutputStream()); 158 } 159 160 163 private class SSHUserInfo implements UserInfo, UIKeyboardInteractive { 164 public String getPassphrase() { 165 return null; 166 } 167 168 public String getPassword() { 169 return password; 170 } 171 172 public boolean promptPassword(String message) { 173 return true; 174 } 175 176 public boolean promptPassphrase(String message) { 177 return true; 178 } 179 180 public boolean promptYesNo(String message) { 181 return false; 182 } 183 184 public void showMessage(String message) { 185 } 186 187 public String [] promptKeyboardInteractive(String destination, 188 String name, 189 String instruction, 190 String [] prompt, 191 boolean[] echo){ 192 String [] response=new String [prompt.length]; 193 if(prompt.length==1){ 194 response[0]=password; 195 } 196 return response; 197 } 198 } 199 200 203 private class SocketFactoryBridge implements com.jcraft.jsch.SocketFactory { 204 205 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 206 return socketFactory.createSocket(host, port); 207 } 208 209 public InputStream getInputStream(Socket socket) throws IOException { 210 return socket.getInputStream(); 211 } 212 213 public OutputStream getOutputStream(Socket socket) throws IOException { 214 return socket.getOutputStream(); 215 } 216 } 217 218 private static class SshChannelInputStream extends FilterInputStream { 219 220 private final Channel channel; 221 222 public SshChannelInputStream(Channel channel) throws IOException { 223 super(channel.getInputStream()); 224 this.channel = channel; 225 } 226 227 public int available() throws IOException { 228 checkChannelState(); 229 return super.available(); 230 } 231 232 private void checkChannelState() throws IOException { 233 int exitStatus = channel.getExitStatus(); 234 if (exitStatus > 0 || exitStatus < -1) throw new IOException(NbBundle.getMessage(SSHConnection.class, "BK3004", CVS_SERVER_COMMAND)); 235 if (exitStatus == 0 || channel.isEOF()) throw new EOFException(NbBundle.getMessage(SSHConnection.class, "BK3007")); 236 } 237 } 238 } 239 | Popular Tags |