public void request(Session session, Channel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); bool reply = waitForReply(); if (reply) { channel.Replay = -1; } packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.Recipient); buf.putString(Util.getBytes("subsystem")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(Util.getBytes("sftp")); session.write(packet); if (reply) { while (channel.Replay == -1) { try { System.Threading.Thread.Sleep(10); } catch//(Exception ee) { } } if (channel.Replay == 0) { throw new JSchException("failed to send sftp request"); } } }
public void request(Session session, Channel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); bool reply = waitForReply(); if (reply) channel.Replay = -1; packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.Recipient); buf.putString("subsystem"); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(m_subsystem); session.write(packet); if (reply) { while (channel.Replay == -1) { try { Thread.sleep(10); } catch { } } if (channel.Replay == 0) throw new JSchException("failed to send subsystem request"); } }
public void request(Session session, Channel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.Recipient); buf.putString(Util.getBytes("signal")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(Util.getBytes(m_signal)); session.write(packet); }
public override void init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) { m_session = session; m_V_S = V_S; m_V_C = V_C; m_I_S = I_S; m_I_C = I_C; try { Type t = Type.GetType(session.getConfig("sha-1")); m_sha = (HASH)(Activator.CreateInstance(t)); m_sha.init(); } catch (Exception ex) { throw ex; } m_buf = new Buffer(); m_packet = new Packet(m_buf); try { Type t = Type.GetType(session.getConfig("dh")); m_dh = (DH)(Activator.CreateInstance(t)); m_dh.init(); } catch (Exception ee) { throw ee; } m_dh.P = m_p; m_dh.G = m_g; // The client responds with: // byte SSH_MSG_KEXDH_INIT(30) // mpint e <- g^x mod p // x is a random number (1 < x < (p-1)/2) m_e = m_dh.E; m_packet.reset(); m_buf.putByte((byte)SSH_MSG_KEXDH_INIT); m_buf.putMPInt(m_e); session.write(m_packet); m_state = SSH_MSG_KEXDH_REPLY; }
public void request(Session session, Channel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); // send // byte SSH_MSG_CHANNEL_REQUEST(98) // uint32 recipient channel // string request type // "shell" // boolean want reply // 0 packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.Recipient); buf.putString(Util.getBytes("shell")); buf.putByte((byte)(waitForReply() ? 1 : 0)); session.write(packet); }
public void request(Session session, Channel channel) { Packet packet = session.m_packet; Buffer buf = session.m_buf; // send // byte SSH_MSG_CHANNEL_REQUEST(98) // uint32 recipient channel // string request type // "exec" // boolean want reply // 0 // string command packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.Recipient); buf.putString("exec"); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(m_command); session.write(packet); }
internal PortWatcher(Session session, string address, int lport, string host, int rport, ServerSocketFactory factory) { this.session = session; this.lport = lport; this.host = host; this.rport = rport; try { boundaddress = InetAddress.getByName(address); ss = (factory == null) ? new ServerSocket(lport, 0, boundaddress) : factory.createServerSocket(lport, 0, boundaddress); } catch (Exception e) { Console.WriteLine(e); throw new JSchException("PortForwardingL: local port " + address + ":" + lport + " cannot be bound."); } }
public override void init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) { m_session = session; m_V_S = V_S; m_V_C = V_C; m_I_S = I_S; m_I_C = I_C; try { Type t = Type.GetType(session.getConfig("sha-1")); m_sha = (HASH)(Activator.CreateInstance(t)); m_sha.init(); } catch (Exception) { } m_buf = new Buffer(); m_packet = new Packet(m_buf); try { Type t = Type.GetType(session.getConfig("dh")); m_dh = (DH)(Activator.CreateInstance(t)); m_dh.init(); } catch (Exception e) { throw e; } m_packet.reset(); m_buf.putByte((byte)0x22); m_buf.putInt(m_min); m_buf.putInt(m_preferred); m_buf.putInt(m_max); session.write(m_packet); m_state = SSH_MSG_KEX_DH_GEX_GROUP; }
public void request(Session session, Channel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); //byte SSH_MSG_CHANNEL_REQUEST //uint32 recipient_channel //string "window-change" //boolean FALSE //uint32 terminal width, columns //uint32 terminal height, rows //uint32 terminal width, pixels //uint32 terminal height, pixels packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.Recipient); buf.putString(Util.getBytes("window-change")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putInt(m_width_columns); buf.putInt(m_height_rows); buf.putInt(m_width_pixels); buf.putInt(m_height_pixels); session.write(packet); }
byte[] getSignature_rsa(Session session, byte[] data) { try { Type t = Type.GetType(m_jsch.getConfig("signature.rsa")); SignatureRSA rsa = (SignatureRSA)Activator.CreateInstance(t); rsa.init(); rsa.setPrvKey(m_e_array, m_n_array, m_d_array, m_p_array, m_q_array, m_dmp1_array, m_dmq1_array, m_iqmp_array); rsa.update(data); byte[] sig = rsa.sign(); Buffer buf = new Buffer(malgname_RSA.Length + 4 + sig.Length + 4); buf.putString(System.Text.Encoding.Default.GetBytes(malgname_RSA)); buf.putString(sig); return buf.m_buffer; } catch (Exception) { } return null; }
public void request(Session session, Channel channel, string subsystem, bool want_reply) { m_subsystem = subsystem; m_want_reply = want_reply; request(session, channel); }
public byte[] getSignature(Session session, byte[] data) { if (m_type == RSA) return getSignature_rsa(session, data); return getSignature_dss(session, data); }
byte[] getSignature_dss(Session session, byte[] data) { try { Type t = Type.GetType(m_jsch.getConfig("signature.dss")); SignatureDSA dsa = (SignatureDSA)(Activator.CreateInstance(t)); dsa.init(); dsa.setPrvKey(m_prv_array, m_P_array, m_Q_array, m_G_array); dsa.update(data); byte[] sig = dsa.sign(); Buffer buf = new Buffer(m_algname_DSS.Length + 4 + sig.Length + 4); buf.putString(System.Text.Encoding.Default.GetBytes(m_algname_DSS)); buf.putString(sig); return buf.m_buffer; } catch (Exception) { } return null; }
internal bool removeSession(Session session) { lock (m_pool) { m_pool.Remove(session); return true; } }
internal static string[] getPortForwarding(Session session) { ArrayList foo = new ArrayList(); lock (m_pool) { for (int i = 0; i < m_pool.Count; i++) { PortWatcher p = m_pool[i]; if (p.session == session) foo.Add(p.lport + ":" + p.host + ":" + p.rport); } } string[] bar = new String[foo.Count]; for (int i = 0; i < foo.Count; i++) { bar[i] = (String)(foo[i]); } return bar; }
internal static void delPort(Session session) { int[] rport = null; int count = 0; lock (m_pool) { rport = new int[m_pool.Count]; for (int i = 0; i < m_pool.Count; i++) { Object[] bar = (Object[])(m_pool[i]); if (bar[0] == session) rport[count++] = (int)bar[1]; } } for (int i = 0; i < count; i++) delPort(session, rport[i]); }
public override bool start(Session session) { List<Identity> identities = session.m_jsch.Identities; Packet packet = session.m_packet; Buffer buf = session.m_buf; string passphrase = null; string username = session.m_username; byte[] _username = null; try { _username = Util.getBytesUTF8(username); } catch { _username = Util.getBytes(username); } for (int i = 0; i < identities.Count; i++) { Identity identity = (Identity)(identities[i]); byte[] pubkeyblob = identity.PublicKeyBlob; if (pubkeyblob != null) { // send // byte SSH_MSG_USERAUTH_REQUEST(50) // string user name // string service name ("ssh-connection") // string "publickey" // boolen FALSE // string plaintext password (ISO-10646 UTF-8) packet.reset(); buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST); buf.putString(_username); buf.putString(Util.getBytes("ssh-connection")); buf.putString(Util.getBytes("publickey")); buf.putByte((byte)0); buf.putString(Util.getBytes(identity.AlgName)); buf.putString(pubkeyblob); session.write(packet); loop1: while (true) { // receive // byte SSH_MSG_USERAUTH_PK_OK(52) // string service name buf = session.read(buf); if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_PK_OK) break; else if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE) break; else if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_BANNER) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] _message = buf.getString(); byte[] lang = buf.getString(); string message = null; try { message = Util.getStringUTF8(_message); } catch { message = Util.getString(_message); } if (m_userinfo != null) m_userinfo.showMessage(message); goto loop1; } else break; } if (buf.m_buffer[5] != Session.SSH_MSG_USERAUTH_PK_OK) continue; } int count = 5; while (true) { if ((identity.isEncrypted && passphrase == null)) { if (m_userinfo == null) throw new JSchException("USERAUTH fail"); if (identity.isEncrypted && !m_userinfo.promptPassphrase("Passphrase for " + identity.Name)) throw new JSchAuthCancelException("publickey"); passphrase = m_userinfo.Passphrase; } if (!identity.isEncrypted || passphrase != null) { if (identity.setPassphrase(passphrase)) break; } passphrase = null; count--; if (count == 0) break; } if (identity.isEncrypted) continue; if (pubkeyblob == null) pubkeyblob = identity.PublicKeyBlob; if (pubkeyblob == null) continue; // send // byte SSH_MSG_USERAUTH_REQUEST(50) // string user name // string service name ("ssh-connection") // string "publickey" // boolen TRUE // string plaintext password (ISO-10646 UTF-8) packet.reset(); buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST); buf.putString(_username); buf.putString(Util.getBytes("ssh-connection")); buf.putString(Util.getBytes("publickey")); buf.putByte((byte)1); buf.putString(Util.getBytes(identity.AlgName)); buf.putString(pubkeyblob); byte[] sid = session.getSessionId(); uint sidlen = (uint)sid.Length; byte[] tmp = new byte[4 + sidlen + buf.m_index - 5]; tmp[0] = (byte)(sidlen >> 24); tmp[1] = (byte)(sidlen >> 16); tmp[2] = (byte)(sidlen >> 8); tmp[3] = (byte)(sidlen); Array.Copy(sid, 0, tmp, 4, sidlen); Array.Copy(buf.m_buffer, 5, tmp, 4 + sidlen, buf.m_index - 5); byte[] signature = identity.getSignature(session, tmp); if (signature == null) // for example, too long key length. break; buf.putString(signature); session.write(packet); loop2: while (true) { // receive // byte SSH_MSG_USERAUTH_SUCCESS(52) // string service name buf = session.read(buf); if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_SUCCESS) return true; else if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_BANNER) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] _message = buf.getString(); byte[] lang = buf.getString(); string message = null; try { message = Util.getStringUTF8(_message); } catch { message = Util.getString(_message); } if (m_userinfo != null) m_userinfo.showMessage(message); goto loop2; } else if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] foo = buf.getString(); int partial_success = buf.getByte(); if (partial_success != 0) throw new JSchPartialAuthException(Util.getString(foo)); break; } break; } } return false; }
internal static PortWatcher addPort(Session session, string address, int lport, string host, int rport, ServerSocketFactory ssf) { if (getPort(session, address, lport) != null) { throw new JSchException("PortForwardingL: local port " + address + ":" + lport + " is already registered."); } PortWatcher pw = new PortWatcher(session, address, lport, host, rport, ssf); m_pool.Add(pw); return pw; }
internal static void delPort(Session session) { lock (m_pool) { PortWatcher[] foo = new PortWatcher[m_pool.Count]; int count = 0; for (int i = 0; i < m_pool.Count; i++) { PortWatcher p = m_pool[i]; if (p.session == session) { p.delete(); foo[count++] = p; } } for (int i = 0; i < count; i++) { PortWatcher p = foo[i]; m_pool.Remove(p); } } }
internal static void addPort(Session session, int port, string target, int lport, SocketFactory factory) { lock (m_pool) { if (getPort(session, port) != null) throw new JSchException("PortForwardingR: remote port " + port + " is already registered."); Object[] foo = new Object[5]; foo[0] = session; foo[1] = port; foo[2] = target; foo[3] = lport; foo[4] = factory; m_pool.Add(foo); } }
internal static void disconnect(Session session) { Channel[] channels = null; int count = 0; lock (m_pool) { channels = new Channel[m_pool.Count]; for (int i = 0; i < m_pool.Count; i++) { try { Channel c = ((Channel)(m_pool[i])); if (c.m_session == session) channels[count++] = c; } catch (Exception) { } } } for (int i = 0; i < count; i++) channels[i].disconnect(); }
internal static Channel FindChannel(int id, Session session) { lock (m_pool) { for (int i = 0; i < m_pool.Count; i++) { Channel channel = m_pool[i]; if (channel.m_id == id && channel.m_session == session) return channel; } } return null; }
internal static PortWatcher getPort(Session session, string address, int lport) { InetAddress addr; try { addr = InetAddress.getByName(address); } catch (Exception) { throw new JSchException("PortForwardingL: invalid address " + address + " specified."); } lock (m_pool) { for (int i = 0; i < m_pool.Count; i++) { PortWatcher p = m_pool[i]; if (p.session == session && p.lport == lport) { if (p.boundaddress.isAnyLocalAddress() || p.boundaddress.equals(addr)) return p; } } return null; } }
internal static Object[] getPort(Session session, int rport) { lock (m_pool) { for (int i = 0; i < m_pool.Count; i++) { Object[] bar = (Object[])(m_pool[i]); if (bar[0] != session) continue; if ((int)bar[1] != rport) continue; return bar; } return null; } }
public override bool start(Session session) { base.start(session); Packet packet = session.m_packet; Buffer buf = session.m_buf; string username = session.m_username; byte[] _username = null; try { _username = Util.getBytesUTF8(username); } catch { _username = Util.getBytes(username); } // send // byte SSH_MSG_USERAUTH_REQUEST(50) // string user name // string service name ("ssh-connection") // string "none" packet.reset(); buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST); buf.putString(_username); buf.putString(Util.getBytes("ssh-connection")); buf.putString(Util.getBytes("none")); session.write(packet); loop: while (true) { // receive // byte SSH_MSG_USERAUTH_SUCCESS(52) // string service name buf = session.read(buf); if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_SUCCESS) return true; if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_BANNER) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] _message = buf.getString(); byte[] lang = buf.getString(); string message = null; try { message = Util.getStringUTF8(_message); } catch { message = Util.getString(_message); } if (m_userinfo != null) m_userinfo.showMessage(message); goto loop; } if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] foo = buf.getString(); int partial_success = buf.getByte(); m_methods = Util.getString(foo); break; } else throw new JSchException("USERAUTH fail (" + buf.m_buffer[5] + ")"); } return false; }
internal static string[] getPortForwarding(Session session) { ArrayList foo = new ArrayList(); lock (m_pool) { for (int i = 0; i < m_pool.Count; i++) { Object[] bar = (Object[])(m_pool[i]); if (bar[0] != session) continue; if (bar[3] == null) foo.Add(bar[1] + ":" + bar[2] + ":"); else foo.Add(bar[1] + ":" + bar[2] + ":" + bar[3]); } } string[] bar2 = new string[foo.Count]; for (int i = 0; i < foo.Count; i++) { bar2[i] = (string)(foo[i]); } return bar2; }
internal static void addPort(Session session, int port, string daemon, Object[] arg) { lock (m_pool) { if (getPort(session, port) != null) { throw new JSchException("PortForwardingR: remote port " + port + " is already registered."); } Object[] foo = new Object[4]; foo[0] = session; foo[1] = port; foo[2] = daemon; foo[3] = arg; m_pool.Add(foo); } }
public Session getSession(string username, string host, int port) { Session session = new Session(this); session.setUserName(username); session.setHost(host); session.setPort(port); m_pool.Add(session); return session; }
public override bool start(Session session) { Packet packet = session.m_packet; Buffer buf = session.m_buf; string username = session.m_username; string password = session.m_password; string dest = username + "@" + session.m_host; if (session.m_port != SharpSsh.SshBase.SSH_TCP_PORT) dest += (":" + session.m_port); while (true) { if (password == null) { if (m_userinfo == null) return false; if (!m_userinfo.promptPassword("Password for " + dest)) throw new JSchAuthCancelException("password"); password = m_userinfo.Password; if (password == null) throw new JSchAuthCancelException("password"); } byte[] _username = null; try { _username = Util.getBytesUTF8(username); } catch { _username = Util.getBytes(username); } byte[] _password = null; try { _password = Util.getBytesUTF8(password); } catch { _password = Util.getBytes(password); } // send // byte SSH_MSG_USERAUTH_REQUEST(50) // string user name // string service name ("ssh-connection") // string "password" // boolen FALSE // string plaintext password (ISO-10646 UTF-8) packet.reset(); buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST); buf.putString(_username); buf.putString(Util.getBytes("ssh-connection")); buf.putString(Util.getBytes("password")); buf.putByte((byte)0); buf.putString(_password); session.write(packet); loop: while (true) { // receive // byte SSH_MSG_USERAUTH_SUCCESS(52) // string service name buf = session.read(buf); if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_SUCCESS) return true; if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_BANNER) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] _message = buf.getString(); byte[] lang = buf.getString(); string message = null; try { message = Util.getStringUTF8(_message); } catch { message = Util.getString(_message); } if (m_userinfo != null) m_userinfo.showMessage(message); goto loop; } if (buf.m_buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE) { buf.getInt(); buf.getByte(); buf.getByte(); byte[] foo = buf.getString(); int partial_success = buf.getByte(); if (partial_success != 0) throw new JSchPartialAuthException(Util.getString(foo)); break; } else return false; } password = null; } }
internal static void delPort(Session session, int rport) { lock (m_pool) { Object[] foo = null; for (int i = 0; i < m_pool.Count; i++) { Object[] bar = (Object[])(m_pool[i]); if (bar[0] != session) continue; if ((int)bar[1] != rport) continue; foo = bar; break; } if (foo == null) return; m_pool.Remove(foo); } Buffer buf = new Buffer(100); Packet packet = new Packet(buf); try { packet.reset(); buf.putByte((byte)80 /*SSH_MSG_GLOBAL_REQUEST*/); buf.putString("cancel-tcpip-forward"); buf.putByte((byte)0); buf.putString("0.0.0.0"); buf.putInt(rport); session.write(packet); } catch (Exception) { } }