1 16 package org.apache.commons.net.bsd; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.ServerSocket ; 21 import java.net.Socket ; 22 import org.apache.commons.net.io.SocketInputStream; 23 import org.apache.commons.net.SocketClient; 24 import java.io.OutputStream ; 25 26 63 64 public class RExecClient extends SocketClient 65 { 66 69 public static final int DEFAULT_PORT = 512; 70 71 private boolean __remoteVerificationEnabled; 72 73 79 protected InputStream _errorStream_; 80 81 InputStream _createErrorStream() throws IOException 84 { 85 ServerSocket server; 86 Socket socket; 87 88 server = _socketFactory_.createServerSocket(0, 1, getLocalAddress()); 89 90 _output_.write(Integer.toString(server.getLocalPort()).getBytes()); 91 _output_.write('\0'); 92 _output_.flush(); 93 94 socket = server.accept(); 95 server.close(); 96 97 if (__remoteVerificationEnabled && !verifyRemote(socket)) 98 { 99 socket.close(); 100 throw new IOException ( 101 "Security violation: unexpected connection attempt by " + 102 socket.getInetAddress().getHostAddress()); 103 } 104 105 return (new SocketInputStream(socket, socket.getInputStream())); 106 } 107 108 109 113 public RExecClient() 114 { 115 _errorStream_ = null; 116 setDefaultPort(DEFAULT_PORT); 117 } 118 119 120 128 public InputStream getInputStream() 129 { 130 return _input_; 131 } 132 133 134 142 public OutputStream getOutputStream() 143 { 144 return _output_; 145 } 146 147 148 158 public InputStream getErrorStream() 159 { 160 return _errorStream_; 161 } 162 163 164 196 public void rexec(String username, String password, 197 String command, boolean separateErrorStream) 198 throws IOException 199 { 200 int ch; 201 202 if (separateErrorStream) 203 { 204 _errorStream_ = _createErrorStream(); 205 } 206 else 207 { 208 _output_.write('\0'); 209 } 210 211 _output_.write(username.getBytes()); 212 _output_.write('\0'); 213 _output_.write(password.getBytes()); 214 _output_.write('\0'); 215 _output_.write(command.getBytes()); 216 _output_.write('\0'); 217 _output_.flush(); 218 219 ch = _input_.read(); 220 if (ch > 0) 221 { 222 StringBuffer buffer = new StringBuffer (); 223 224 while ((ch = _input_.read()) != -1 && ch != '\n') 225 buffer.append((char)ch); 226 227 throw new IOException (buffer.toString()); 228 } 229 else if (ch < 0) 230 { 231 throw new IOException ("Server closed connection."); 232 } 233 } 234 235 236 239 public void rexec(String username, String password, 240 String command) 241 throws IOException 242 { 243 rexec(username, password, command, false); 244 } 245 246 252 public void disconnect() throws IOException 253 { 254 if (_errorStream_ != null) 255 _errorStream_.close(); 256 _errorStream_ = null; 257 super.disconnect(); 258 } 259 260 261 270 public final void setRemoteVerificationEnabled(boolean enable) 271 { 272 __remoteVerificationEnabled = enable; 273 } 274 275 282 public final boolean isRemoteVerificationEnabled() 283 { 284 return __remoteVerificationEnabled; 285 } 286 287 } 288 289 | Popular Tags |