/// <exception cref="NSch.JSchException"></exception> public virtual void Connect(int connectTimeout) { if (isConnected) { throw new JSchException("session is already connected"); } io = new IO(); if (random == null) { try { Type c = Sharpen.Runtime.GetType(GetConfig("random")); random = (Random)(System.Activator.CreateInstance(c)); } catch (Exception e) { throw new JSchException(e.ToString(), e); } } Packet.SetRandom(random); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Connecting to " + host + " port " + port); } try { int i; int j; if (proxy == null) { InputStream @in; OutputStream @out; if (socket_factory == null) { socket = Util.CreateSocket(host, port, connectTimeout); @in = socket.GetInputStream(); @out = socket.GetOutputStream(); } else { socket = socket_factory.CreateSocket(host, port); @in = socket_factory.GetInputStream(socket); @out = socket_factory.GetOutputStream(socket); } //if(timeout>0){ socket.setSoTimeout(timeout); } socket.NoDelay = true; io.SetInputStream(@in); io.SetOutputStream(@out); } else { lock (proxy) { proxy.Connect(socket_factory, host, port, connectTimeout); io.SetInputStream(proxy.GetInputStream()); io.SetOutputStream(proxy.GetOutputStream()); socket = proxy.GetSocket(); } } if (connectTimeout > 0 && socket != null) { socket.ReceiveTimeout = connectTimeout; } isConnected = true; if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Connection established"); } jsch.AddSession(this); { // Some Cisco devices will miss to read '\n' if it is sent separately. byte[] foo = new byte[V_C.Length + 1]; System.Array.Copy(V_C, 0, foo, 0, V_C.Length); foo[foo.Length - 1] = unchecked((byte)(byte)('\n')); io.Put(foo, 0, foo.Length); } while (true) { i = 0; j = 0; while (i < buf.buffer.Length) { j = io.GetByte(); if (j < 0) { break; } buf.buffer[i] = unchecked((byte)j); i++; if (j == 10) { break; } } if (j < 0) { throw new JSchException("connection is closed by foreign host"); } if (buf.buffer[i - 1] == 10) { // 0x0a i--; if (i > 0 && buf.buffer[i - 1] == 13) { // 0x0d i--; } } if (i <= 3 || ((i != buf.buffer.Length) && (buf.buffer[0] != 'S' || buf.buffer[1] != 'S' || buf.buffer[2] != 'H' || buf.buffer[3] != '-'))) { // It must not start with 'SSH-' //System.err.println(new String(buf.buffer, 0, i); continue; } if (i == buf.buffer.Length || i < 7 || (buf.buffer[4] == '1' && buf.buffer[6] != '9')) { // SSH-1.99 or SSH-2.0 // SSH-1.5 throw new JSchException("invalid server's version string"); } break; } V_S = new byte[i]; System.Array.Copy(buf.buffer, 0, V_S, 0, i); //System.err.println("V_S: ("+i+") ["+new String(V_S)+"]"); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Remote version string: " + Util.Byte2str(V_S)); JSch.GetLogger().Log(Logger.INFO, "Local version string: " + Util.Byte2str(V_C)); } Send_kexinit(); buf = Read(buf); if (buf.GetCommand() != SSH_MSG_KEXINIT) { in_kex = false; throw new JSchException("invalid protocol: " + buf.GetCommand()); } if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEXINIT received"); } KeyExchange kex = Receive_kexinit(buf); while (true) { buf = Read(buf); if (kex.GetState() == buf.GetCommand()) { kex_start_time = Runtime.CurrentTimeMillis(); bool result = kex.Next(buf); if (!result) { //System.err.println("verify: "+result); in_kex = false; throw new JSchException("verify: " + result); } } else { in_kex = false; throw new JSchException("invalid protocol(kex): " + buf.GetCommand()); } if (kex.GetState() == KeyExchange.STATE_END) { break; } } try { CheckHost(host, port, kex); } catch (JSchException ee) { in_kex = false; throw; } Send_newkeys(); // receive SSH_MSG_NEWKEYS(21) buf = Read(buf); //System.err.println("read: 21 ? "+buf.getCommand()); if (buf.GetCommand() == SSH_MSG_NEWKEYS) { if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_NEWKEYS received"); } Receive_newkeys(buf, kex); } else { in_kex = false; throw new JSchException("invalid protocol(newkyes): " + buf.GetCommand()); } bool auth = false; bool auth_cancel = false; UserAuth ua = null; try { Type c = Sharpen.Runtime.GetType(GetConfig("userauth.none")); ua = (UserAuth)(System.Activator.CreateInstance(c)); } catch (Exception e) { throw new JSchException(e.ToString(), e); } auth = ua.Start(this); string cmethods = GetConfig("PreferredAuthentications"); string[] cmethoda = Util.Split(cmethods, ","); string smethods = null; if (!auth) { smethods = ((UserAuthNone)ua).GetMethods(); if (smethods != null) { smethods = smethods.ToLower(); } else { // methods: publickey,password,keyboard-interactive //smethods="publickey,password,keyboard-interactive"; smethods = cmethods; } } string[] smethoda = Util.Split(smethods, ","); int methodi = 0; while (true) { //System.err.println("methods: "+methods); while (!auth && cmethoda != null && methodi < cmethoda.Length) { string method = cmethoda[methodi++]; bool acceptable = false; for (int k = 0; k < smethoda.Length; k++) { if (smethoda[k].Equals(method)) { acceptable = true; break; } } if (!acceptable) { continue; } //System.err.println(" method: "+method); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { string str = "Authentications that can continue: "; for (int k_1 = methodi - 1; k_1 < cmethoda.Length; k_1++) { str += cmethoda[k_1]; if (k_1 + 1 < cmethoda.Length) { str += ","; } } JSch.GetLogger().Log(Logger.INFO, str); JSch.GetLogger().Log(Logger.INFO, "Next authentication method: " + method); } ua = null; try { Type c = null; if (GetConfig("userauth." + method) != null) { c = Sharpen.Runtime.GetType(GetConfig("userauth." + method)); ua = (UserAuth)(System.Activator.CreateInstance(c)); } } catch (Exception) { if (JSch.GetLogger().IsEnabled(Logger.WARN)) { JSch.GetLogger().Log(Logger.WARN, "failed to load " + method + " method"); } } if (ua != null) { auth_cancel = false; try { auth = ua.Start(this); if (auth && JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Authentication succeeded (" + method + ")."); } } catch (JSchAuthCancelException) { auth_cancel = true; } catch (JSchPartialAuthException ee) { string tmp = smethods; smethods = ee.GetMethods(); smethoda = Util.Split(smethods, ","); if (!tmp.Equals(smethods)) { methodi = 0; } //System.err.println("PartialAuth: "+methods); auth_cancel = false; goto loop_continue; } catch (RuntimeException ee) { throw; } catch (Exception) { //System.err.println("ee: "+ee); // SSH_MSG_DISCONNECT: 2 Too many authentication failures goto loop_break; } } } break; loop_continue: ; } loop_break: ; if (!auth) { if (auth_cancel) { throw new JSchException("Auth cancel"); } throw new JSchException("Auth fail"); } if (connectTimeout > 0 || timeout > 0) { socket.ReceiveTimeout = timeout; } isAuthed = true; lock (Lock) { if (isConnected) { connectThread = new Sharpen.Thread(this); connectThread.SetName("Connect thread " + host + " session"); if (daemon_thread) { connectThread.SetDaemon(daemon_thread); } connectThread.Start(); } } } catch (Exception e) { // The session has been already down and // we don't have to start new thread. in_kex = false; if (isConnected) { try { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_DISCONNECT)); buf.PutInt(3); buf.PutString(Util.Str2byte(e.ToString())); buf.PutString(Util.Str2byte("en")); Write(packet); Disconnect(); } catch (Exception) { } } isConnected = false; //e.printStackTrace(); if (e is RuntimeException) { throw (RuntimeException)e; } if (e is JSchException) { throw (JSchException)e; } throw new JSchException("Session.connect: " + e); } finally { Util.Bzero(this.password); this.password = null; } }
/// <exception cref="System.Exception"></exception> public virtual Buffer Read(Buffer buf) { int j = 0; while (true) { buf.Reset(); io.GetByte(buf.buffer, buf.index, s2ccipher_size); buf.index += s2ccipher_size; if (s2ccipher != null) { s2ccipher.Update(buf.buffer, 0, s2ccipher_size, buf.buffer, 0); } j = ((buf.buffer[0] << 24) & unchecked((int)(0xff000000))) | ((buf.buffer[1] << 16 ) & unchecked((int)(0x00ff0000))) | ((buf.buffer[2] << 8) & unchecked((int)(0x0000ff00 ))) | ((buf.buffer[3]) & unchecked((int)(0x000000ff))); // RFC 4253 6.1. Maximum Packet Length if (j < 5 || j > PACKET_MAX_SIZE) { Start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE); } int need = j + 4 - s2ccipher_size; //if(need<0){ // throw new IOException("invalid data"); //} if ((buf.index + need) > buf.buffer.Length) { byte[] foo = new byte[buf.index + need]; System.Array.Copy(buf.buffer, 0, foo, 0, buf.index); buf.buffer = foo; } if ((need % s2ccipher_size) != 0) { string message = "Bad packet length " + need; if (JSch.GetLogger().IsEnabled(Logger.FATAL)) { JSch.GetLogger().Log(Logger.FATAL, message); } Start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE - s2ccipher_size); } if (need > 0) { io.GetByte(buf.buffer, buf.index, need); buf.index += (need); if (s2ccipher != null) { s2ccipher.Update(buf.buffer, s2ccipher_size, need, buf.buffer, s2ccipher_size); } } if (s2cmac != null) { s2cmac.Update(seqi); s2cmac.Update(buf.buffer, 0, buf.index); s2cmac.DoFinal(s2cmac_result1, 0); io.GetByte(s2cmac_result2, 0, s2cmac_result2.Length); if (!Arrays.Equals(s2cmac_result1, s2cmac_result2)) { if (need > PACKET_MAX_SIZE) { throw new IOException("MAC Error"); } Start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE - need); continue; } } seqi++; if (inflater != null) { //inflater.uncompress(buf); int pad = buf.buffer[4]; uncompress_len[0] = buf.index - 5 - pad; byte[] foo = inflater.Uncompress(buf.buffer, 5, uncompress_len); if (foo != null) { buf.buffer = foo; buf.index = 5 + uncompress_len[0]; } else { System.Console.Error.WriteLine("fail in inflater"); break; } } int type = buf.GetCommand() & unchecked((int)(0xff)); //System.err.println("read: "+type); if (type == SSH_MSG_DISCONNECT) { buf.Rewind(); buf.GetInt(); buf.GetShort(); int reason_code = buf.GetInt(); byte[] description = buf.GetString(); byte[] language_tag = buf.GetString(); throw new JSchException("SSH_MSG_DISCONNECT: " + reason_code + " " + Util.Byte2str (description) + " " + Util.Byte2str(language_tag)); } else { //break; if (type == SSH_MSG_IGNORE) { } else { if (type == SSH_MSG_UNIMPLEMENTED) { buf.Rewind(); buf.GetInt(); buf.GetShort(); int reason_id = buf.GetInt(); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Received SSH_MSG_UNIMPLEMENTED for " + reason_id ); } } else { if (type == SSH_MSG_DEBUG) { buf.Rewind(); buf.GetInt(); buf.GetShort(); } else { if (type == SSH_MSG_CHANNEL_WINDOW_ADJUST) { buf.Rewind(); buf.GetInt(); buf.GetShort(); Channel c = Channel.GetChannel(buf.GetInt(), this); if (c == null) { } else { c.AddRemoteWindowSize(buf.GetInt()); } } else { if (type == UserAuth.SSH_MSG_USERAUTH_SUCCESS) { isAuthed = true; if (inflater == null && deflater == null) { string method; method = guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS]; InitDeflater(method); method = guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC]; InitInflater(method); } break; } else { break; } } } } } } } buf.Rewind(); return buf; }
public virtual void Run() { thread = this; byte[] foo; Buffer buf = new Buffer(); Packet packet = new Packet(buf); int i = 0; Channel channel; int[] start = new int[1]; int[] length = new int[1]; KeyExchange kex = null; int stimeout = 0; try { while (isConnected && thread != null) { try { buf = Read(buf); stimeout = 0; } catch (ThreadInterruptedException ee) { if (!in_kex && stimeout < serverAliveCountMax) { SendKeepAliveMsg(); stimeout++; continue; } throw; } int msgType = buf.GetCommand() & unchecked((int)(0xff)); if (kex != null && kex.GetState() == msgType) { kex_start_time = Runtime.CurrentTimeMillis(); bool result = kex.Next(buf); if (!result) { throw new JSchException("verify: " + result); } continue; } switch (msgType) { case SSH_MSG_KEXINIT: { //System.err.println("KEXINIT"); kex = Receive_kexinit(buf); break; } case SSH_MSG_NEWKEYS: { //System.err.println("NEWKEYS"); Send_newkeys(); Receive_newkeys(buf, kex); kex = null; break; } case SSH_MSG_CHANNEL_DATA: { buf.GetInt(); buf.GetByte(); buf.GetByte(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); foo = buf.GetString(start, length); if (channel == null) { break; } if (length[0] == 0) { break; } try { channel.Write(foo, start[0], length[0]); } catch (Exception) { //System.err.println(e); try { channel.Disconnect(); } catch (Exception) { } break; } int len = length[0]; channel.SetLocalWindowSize(channel.lwsize - len); if (channel.lwsize < channel.lwsize_max / 2) { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST)); buf.PutInt(channel.GetRecipient()); buf.PutInt(channel.lwsize_max - channel.lwsize); Write(packet); channel.SetLocalWindowSize(channel.lwsize_max); } break; } case SSH_MSG_CHANNEL_EXTENDED_DATA: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); buf.GetInt(); // data_type_code == 1 foo = buf.GetString(start, length); //System.err.println("stderr: "+new String(foo,start[0],length[0])); if (channel == null) { break; } if (length[0] == 0) { break; } channel.Write_ext(foo, start[0], length[0]); int len = length[0]; channel.SetLocalWindowSize(channel.lwsize - len); if (channel.lwsize < channel.lwsize_max / 2) { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST)); buf.PutInt(channel.GetRecipient()); buf.PutInt(channel.lwsize_max - channel.lwsize); Write(packet); channel.SetLocalWindowSize(channel.lwsize_max); } break; } case SSH_MSG_CHANNEL_WINDOW_ADJUST: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { break; } channel.AddRemoteWindowSize(buf.GetInt()); break; } case SSH_MSG_CHANNEL_EOF: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel != null) { //channel.eof_remote=true; //channel.eof(); channel.Eof_remote(); } break; } case SSH_MSG_CHANNEL_CLOSE: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel != null) { // channel.close(); channel.Disconnect(); } break; } case SSH_MSG_CHANNEL_OPEN_CONFIRMATION: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { } //break; int r = buf.GetInt(); long rws = buf.GetUInt(); int rps = buf.GetInt(); channel.SetRemoteWindowSize(rws); channel.SetRemotePacketSize(rps); channel.SetRecipient(r); break; } case SSH_MSG_CHANNEL_OPEN_FAILURE: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { } //break; int reason_code = buf.GetInt(); //foo=buf.getString(); // additional textual information //foo=buf.getString(); // language tag channel.exitstatus = reason_code; channel.close = true; channel.eof_remote = true; channel.SetRecipient(0); break; } case SSH_MSG_CHANNEL_REQUEST: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); foo = buf.GetString(); bool reply = (buf.GetByte() != 0); channel = Channel.GetChannel(i, this); if (channel != null) { byte reply_type = unchecked((byte)SSH_MSG_CHANNEL_FAILURE); if ((Util.Byte2str(foo)).Equals("exit-status")) { i = buf.GetInt(); // exit-status channel.SetExitStatus(i); reply_type = unchecked((byte)SSH_MSG_CHANNEL_SUCCESS); } if (reply) { packet.Reset(); buf.PutByte(reply_type); buf.PutInt(channel.GetRecipient()); Write(packet); } } break; } case SSH_MSG_CHANNEL_OPEN: { buf.GetInt(); buf.GetShort(); foo = buf.GetString(); string ctyp = Util.Byte2str(foo); if (!"forwarded-tcpip".Equals(ctyp) && !("x11".Equals(ctyp) && x11_forwarding) && !("*****@*****.**".Equals(ctyp) && agent_forwarding)) { //System.err.println("Session.run: CHANNEL OPEN "+ctyp); //throw new IOException("Session.run: CHANNEL OPEN "+ctyp); packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_FAILURE)); buf.PutInt(buf.GetInt()); buf.PutInt(Channel.SSH_OPEN_ADMINISTRATIVELY_PROHIBITED); buf.PutString(Util.empty); buf.PutString(Util.empty); Write(packet); } else { channel = Channel.GetChannel(ctyp); AddChannel(channel); channel.GetData(buf); channel.Init(); Sharpen.Thread tmp = new Sharpen.Thread(channel); tmp.SetName("Channel " + ctyp + " " + host); if (daemon_thread) { tmp.SetDaemon(daemon_thread); } tmp.Start(); break; } goto case SSH_MSG_CHANNEL_SUCCESS; } case SSH_MSG_CHANNEL_SUCCESS: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { break; } channel.reply = 1; break; } case SSH_MSG_CHANNEL_FAILURE: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { break; } channel.reply = 0; break; } case SSH_MSG_GLOBAL_REQUEST: { buf.GetInt(); buf.GetShort(); foo = buf.GetString(); // request name bool reply = (buf.GetByte() != 0); if (reply) { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_REQUEST_FAILURE)); Write(packet); } break; } case SSH_MSG_REQUEST_FAILURE: case SSH_MSG_REQUEST_SUCCESS: { Sharpen.Thread t = grr.GetThread(); if (t != null) { grr.SetReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0); t.Interrupt(); } break; } default: { //System.err.println("Session.run: unsupported type "+msgType); throw new IOException("Unknown SSH message type " + msgType); } } } } catch (Exception e) { in_kex = false; if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Caught an exception, leaving main loop due to " + e.Message); } } //System.err.println("# Session.run"); //e.printStackTrace(); try { Disconnect(); } catch (ArgumentNullException) { } catch (Exception) { } //System.err.println("@1"); //e.printStackTrace(); //System.err.println("@2"); //e.printStackTrace(); isConnected = false; }