Пример #1
0
        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");
            }
        }
Пример #2
0
        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");
                }
            }
        }
Пример #3
0
        public override void connect()
        {
            try
            {
                if (!m_session.IsConnected())
                    throw new JSchException("session is down");

                Buffer buf = new Buffer(150);
                Packet packet = new Packet(buf);

                packet.reset();
                buf.putByte((byte)90);
                buf.putString(Util.getBytes("direct-tcpip"));
                buf.putInt(m_id);
                buf.putInt(m_lwsize);
                buf.putInt(m_lmpsize);
                buf.putString(Util.getBytes(m_host));
                buf.putInt(m_port);
                buf.putString(Util.getBytes(m_originator_IP_address));
                buf.putInt(m_originator_port);
                m_session.write(packet);

                int retry = 1000;
                try
                {
                    while (Recipient == -1
                        && m_session.IsConnected()
                        && retry > 0
                        && !m_eof_remote
                        )
                    {
                        Thread.Sleep(50);
                        retry--;
                    }
                }
                catch { }

                if (!m_session.IsConnected())
                    throw new JSchException("session is down");
                if (retry == 0 || this.m_eof_remote)
                    throw new JSchException("channel is not opened.");

                m_connected = true;
                m_thread = new Thread(this);
                m_thread.Start();
            }
            catch (Exception e)
            {
                m_io.close();
                m_io = null;
                Channel.Remove(this);
                if (e is JSchException)
                    throw (JSchException)e;
            }
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }
Пример #6
0
        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;
        }
Пример #7
0
 public override void Run()
 {
     Buffer buf = new Buffer(m_rmpsize);
     Packet packet = new Packet(buf);
     int i = -1;
     try
     {
         while (isConnected() &&
             m_thread != null &&
             m_io != null &&
             m_io.m_ins != null)
         {
             i = m_io.m_ins.Read(buf.m_buffer,
                             14,
                             buf.m_buffer.Length - 14
                             - 32 - 20 // padding and mac
                             );
             if (i == 0)
                 continue;
             if (i == -1)
             {
                 eof();
                 break;
             }
             if (m_close)
                 break;
             packet.reset();
             buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
             buf.putInt(m_recipient);
             buf.putInt(i);
             buf.skip(i);
             m_session.write(packet, this, i);
         }
     }
     catch (Exception) { }
     m_thread = null;
 }
Пример #8
0
        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);
        }
Пример #9
0
 public void sendKeepAliveMsg()
 {
     Buffer buf = new Buffer();
     Packet packet = new Packet(buf);
     packet.reset();
     buf.putByte((byte)SSH_MSG_GLOBAL_REQUEST);
     buf.putString(m_keepalivemsg);
     buf.putByte((byte)1);
     write(packet);
 }
Пример #10
0
 public void sendIgnore()
 {
     Buffer buf = new Buffer();
     Packet packet = new Packet(buf);
     packet.reset();
     buf.putByte((byte)SSH_MSG_IGNORE);
     write(packet);
 }
Пример #11
0
        public void Run()
        {
            m_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;

            try
            {
                while (m_isConnected &&
                    m_thread != null)
                {
                    buf = read(buf);
                    int msgType = buf.m_buffer[5] & 0xff;

                    if (kex != null && kex.getState() == msgType)
                    {
                        bool result = kex.next(buf);
                        if (!result)
                            throw new JSchException("verify: " + result);
                        continue;
                    }

                    switch (msgType)
                    {
                        case SSH_MSG_KEXINIT:
                            kex = receive_kexinit(buf);
                            break;

                        case SSH_MSG_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.FindChannel(i, this);
                            foo = buf.getString(start, length);
                            if (channel == null)
                                break;

                            try
                            {
                                channel.write(foo, start[0], length[0]);
                            }
                            catch (Exception)
                            {
                                try
                                {
                                    channel.disconnect();
                                }
                                catch (Exception)
                                { }
                                break;
                            }
                            int len = length[0];
                            channel.LocalWindowSize = channel.LocalWindowSize - len;
                            if (channel.LocalWindowSize < channel.LocalWindowSizeMax / 2)
                            {
                                packet.reset();
                                buf.putByte((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST);
                                buf.putInt(channel.Recipient);
                                buf.putInt(channel.LocalWindowSizeMax - channel.LocalWindowSize);
                                write(packet);
                                channel.LocalWindowSize = channel.LocalWindowSizeMax;
                            }
                            break;

                        case SSH_MSG_CHANNEL_EXTENDED_DATA:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);
                            buf.getInt();   // data_type_code == 1
                            foo = buf.getString(start, length);

                            if (channel == null)
                                break;

                            channel.write_ext(foo, start[0], length[0]);

                            len = length[0];
                            channel.LocalWindowSize = channel.LocalWindowSize - len;
                            if (channel.LocalWindowSize < channel.LocalWindowSizeMax / 2)
                            {
                                packet.reset();
                                buf.putByte((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST);
                                buf.putInt(channel.Recipient);
                                buf.putInt(channel.LocalWindowSizeMax - channel.LocalWindowSize);
                                write(packet);
                                channel.LocalWindowSize = channel.LocalWindowSizeMax;
                            }
                            break;

                        case SSH_MSG_CHANNEL_WINDOW_ADJUST:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(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.FindChannel(i, this);
                            if (channel != null)
                                channel.eof_remote();
                            break;
                        case SSH_MSG_CHANNEL_CLOSE:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);
                            if (channel != null)
                                channel.disconnect();
                            break;
                        case SSH_MSG_CHANNEL_OPEN_CONFIRMATION:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);

                            channel.Recipient = buf.getInt();
                            channel.RemoteWindowSize = buf.getInt();
                            channel.RemotePacketSize = buf.getInt();
                            break;
                        case SSH_MSG_CHANNEL_OPEN_FAILURE:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);

                            int reason_code = buf.getInt();
                            channel.ExitStatus = reason_code;
                            channel.IsClosed = true;
                            channel.IsEofRemote = true;
                            channel.Recipient = 0;
                            break;
                        case SSH_MSG_CHANNEL_REQUEST:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            foo = buf.getString();
                            bool reply = (buf.getByte() != 0);
                            channel = Channel.FindChannel(i, this);
                            if (channel != null)
                            {
                                byte reply_type = (byte)SSH_MSG_CHANNEL_FAILURE;
                                if (Util.getStringUTF8(foo) == "exit-status")
                                {
                                    i = buf.getInt();             // exit-status
                                    channel.ExitStatus = i;
                                    reply_type = (byte)SSH_MSG_CHANNEL_SUCCESS;
                                }
                                if (reply)
                                {
                                    packet.reset();
                                    buf.putByte(reply_type);
                                    buf.putInt(channel.Recipient);
                                    write(packet);
                                }
                            }
                            break;
                        case SSH_MSG_CHANNEL_OPEN:
                            buf.getInt();
                            buf.getShort();
                            string ctyp = Util.getStringUTF8(buf.getString());

                            if ("forwarded-tcpip" != ctyp && !("x11" == ctyp && m_x11_forwarding))
                            {
                                Console.WriteLine("Session.run: CHANNEL OPEN " + ctyp);
                                throw new IOException("Session.run: CHANNEL OPEN " + ctyp);
                            }
                            else
                            {
                                channel = Channel.getChannel(ctyp);
                                addChannel(channel);
                                channel.getData(buf);
                                channel.Init();

                                packet.reset();
                                buf.putByte((byte)SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                                buf.putInt(channel.Recipient);
                                buf.putInt(channel.Id);
                                buf.putInt(channel.LocalWindowSize);
                                buf.putInt(channel.LocalPacketSize);
                                write(packet);
                                Thread tmp = new Thread(channel);
                                tmp.Name = "Channel " + ctyp + " " + m_host;
                                tmp.Start();
                                break;
                            }
                        case SSH_MSG_CHANNEL_SUCCESS:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);
                            if (channel == null)
                                break;

                            channel.Replay = 1;
                            break;
                        case SSH_MSG_CHANNEL_FAILURE:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);
                            if (channel == null)
                                break;

                            channel.Replay = 0;
                            break;
                        case SSH_MSG_GLOBAL_REQUEST:
                            buf.getInt();
                            buf.getShort();
                            foo = buf.getString();       // request name
                            reply = (buf.getByte() != 0);
                            if (reply)
                            {
                                packet.reset();
                                buf.putByte((byte)SSH_MSG_REQUEST_FAILURE);
                                write(packet);
                            }
                            break;
                        case SSH_MSG_REQUEST_FAILURE:
                        case SSH_MSG_REQUEST_SUCCESS:
                            Thread t = m_grr.getThread();
                            if (t != null)
                            {
                                m_grr.setReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0);
                                t.Interrupt();
                            }
                            break;
                        default:
                            throw new IOException("Unknown SSH message type " + msgType);
                    }
                }
            }
            catch (Exception)
            { }

            try
            {
                disconnect();
            }
            catch (NullReferenceException)
            { }
            catch (Exception)
            { }

            m_isConnected = false;
        }
Пример #12
0
        private void setPortForwarding(int rport)
        {
            lock (m_grr)
            {
                Buffer buf = new Buffer(100); // ??
                Packet packet = new Packet(buf);

                try
                {
                    packet.reset();
                    buf.putByte((byte)SSH_MSG_GLOBAL_REQUEST);
                    buf.putString("tcpip-forward");
                    buf.putByte((byte)1);
                    buf.putString("0.0.0.0");
                    buf.putInt(rport);
                    write(packet);
                }
                catch (Exception e)
                {
                    throw new JSchException(e.ToString());
                }

                m_grr.setThread(Thread.currentThread());
                try
                {
                    Thread.Sleep(10000);
                }
                catch { }

                int reply = m_grr.getReply();
                m_grr.setThread(null);
                if (reply == 0)
                    throw new JSchException("remote port forwarding failed for listen port " + rport);
            }
        }
Пример #13
0
        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;
        }
Пример #14
0
        internal virtual void eof()
        {
            // Thread.dumpStack();
            if (m_close || m_eof_local)
                return;
            m_eof_local = true;

            try
            {
                Buffer buf = new Buffer(100);
                Packet packet = new Packet(buf);
                packet.reset();
                buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF);
                buf.putInt(Recipient);
                m_session.write(packet);
            }
            catch (Exception)
            { }
        }
Пример #15
0
        /*
        http://www1.ietf.org/internet-drafts/draft-ietf-secsh-connect-24.txt

          5.3  Closing a Channel
        When a party will no longer send more data to a channel, it SHOULD
         send SSH_MSG_CHANNEL_EOF.

                  byte      SSH_MSG_CHANNEL_EOF
                  uint32    recipient_channel

        No explicit response is sent to this message.  However, the
         application may send EOF to whatever is at the other end of the
        channel.  Note that the channel remains open after this message, and
         more data may still be sent In the other direction.  This message
         does not consume window space and can be sent even if no window space
         is available.

           When either party wishes to terminate the channel, it sends
           SSH_MSG_CHANNEL_CLOSE.  Upon receiving this message, a party MUST
         send back a SSH_MSG_CHANNEL_CLOSE unless it has already sent this
         message for the channel.  The channel is considered closed for a
           party when it has both sent and received SSH_MSG_CHANNEL_CLOSE, and
         the party may then reuse the channel number.  A party MAY send
         SSH_MSG_CHANNEL_CLOSE without having sent or received
         SSH_MSG_CHANNEL_EOF.

                  byte      SSH_MSG_CHANNEL_CLOSE
                  uint32    recipient_channel

         This message does not consume window space and can be sent even if no
         window space is available.

         It is recommended that any data sent before this message is delivered
           to the actual destination, if possible.
        */
        internal virtual void close()
        {
            if (m_close) return;
            m_close = true;
            try
            {
                Buffer buf = new Buffer(100);
                Packet packet = new Packet(buf);
                packet.reset();
                buf.putByte((byte)Session.SSH_MSG_CHANNEL_CLOSE);
                buf.putInt(Recipient);
                m_session.write(packet);
            }
            catch (Exception)
            { }
        }
Пример #16
0
        public virtual void connect()
        {
            if (!m_session.IsConnected())
                throw new JSchException("session is down");
            try
            {
                Buffer buf = new Buffer(100);
                Packet packet = new Packet(buf);

                packet.reset();
                buf.putByte((byte)90);
                buf.putString(m_type);
                buf.putInt(m_id);
                buf.putInt(m_lwsize);
                buf.putInt(m_lmpsize);
                m_session.write(packet);

                int retry = 1000;
                while (Recipient == -1
                    && m_session.IsConnected()
                    && retry > 0
                    )
                {
                    try
                    {
                        Thread.sleep(50);
                    }
                    catch (Exception) { }
                    retry--;
                }

                if (!m_session.IsConnected())
                    throw new JSchException("session is down");
                if (retry == 0)
                    throw new JSchException("channel is not opened.");
                m_connected = true;
                start();
            }
            catch (Exception e)
            {
                m_connected = false;
                if (e is JSchException)
                    throw (JSchException)e;
            }
        }
Пример #17
0
        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)
            { }
        }
Пример #18
0
 public override void Run()
 {
     m_thread = Thread.currentThread();
     Buffer buf = new Buffer(m_rmpsize);
     Packet packet = new Packet(buf);
     int i = 0;
     try
     {
         while (m_thread != null)
         {
             i = m_io.m_ins.Read(buf.m_buffer,
                 14,
                 buf.m_buffer.Length - 14 - 16 - 20 // padding and mac
                 );
             if (i <= 0)
             {
                 eof();
                 break;
             }
             if (m_close) break;
             packet.reset();
             buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
             buf.putInt(m_recipient);
             buf.putInt(i);
             buf.skip(i);
             m_session.write(packet, this, i);
         }
     }
     catch { }
     m_thread = null;
 }