Example #1
0
        public override void connect()
        {
            try
            {
                if (!session.isConnected())
                {
                    throw new JSchException("session is down");
                }
                Buffer buf = new Buffer(150);
                Packet packet = new Packet(buf);
                // send
                // byte   SSH_MSG_CHANNEL_OPEN(90)
                // string channel type         //
                // uint32 sender channel       // 0
                // uint32 initial window size  // 0x100000(65536)
                // uint32 maxmum packet size   // 0x4000(16384)

                packet.reset();
                buf.putByte((byte) 90);
                buf.putString(Util.getBytes("direct-tcpip"));
                buf.putInt(id);
                buf.putInt(lwsize);
                buf.putInt(lmpsize);
                buf.putString(Util.getBytes(host));
                buf.putInt(port);
                buf.putString(Util.getBytes(originator_IP_address));
                buf.putInt(originator_port);
                session.write(packet);

                int retry = 1000;
                try
                {
                    while (this.getRecipient() == -1 &&
                           session.isConnected() &&
                           retry > 0 &&
                           !_eof_remote)
                    {
                        //Thread.sleep(500);
                        Thread.Sleep(50);
                        retry--;
                    }
                }
                catch
                {
                }

                if (!session.isConnected())
                {
                    throw new JSchException("session is down");
                }
                if (retry == 0 || this._eof_remote)
                {
                    throw new JSchException("channel is not opened.");
                }
                /*
                if(this.eof_remote){      // failed to open
                  disconnect();
                  return;
                }
                */

                connected = true;

                thread = new JavaThread(this);
                thread.Start();
            }
            catch (Exception e)
            {
                io.close();
                io = null;
                del(this);
                if (e is JSchException)
                {
                    throw (JSchException) e;
                }
            }
        }
        public override void run()
        {
            thread = new JavaThread(Thread.CurrentThread);
            Buffer buf = new Buffer(rmpsize);
            Packet packet = new Packet(buf);
            int i = 0;
            try
            {
                while (thread != null && io != null && io.ins != null)
                {
                    i = io.ins.Read(buf.buffer,
                                    14,
                                    buf.buffer.Length - 14
                                    - 32 - 20 // padding and mac
                        );
                    if (i <= 0)
                    {
                        eof();
                        break;
                    }
                    packet.reset();
                    if (_close) break;
                    buf.putByte((byte) Session.SSH_MSG_CHANNEL_DATA);
                    buf.putInt(recipient);
                    buf.putInt(i);
                    buf.skip(i);
                    session.write(packet, this, i);
                }
            }
            catch (Exception)
            {
                //System.out.println(e);
            }

            //thread=null;
            //eof();
            disconnect();
        }
Example #3
0
        public override void start()
        {
            try
            {
                Request request;

                if (xforwading)
                {
                    request = new RequestX11();
                    request.request(session, this);
                }

                if (pty)
                {
                    request = new RequestPtyReq();
                    request.request(session, this);
                }

                request = new RequestExec(command);
                request.request(session, this);
            }
            catch (Exception)
            {
                throw new JSchException("ChannelExec");
            }
            thread = new JavaThread(this);
            thread.Name("Exec thread " + session.getHost());
            thread.Start();
        }
Example #4
0
        public virtual void disconnect()
        {
            //System.Out.println(this+":disconnect "+io+" "+io.in);
            if (!connected)
            {
                return;
            }
            connected = false;

            close();

            _eof_remote = eof_local = true;

            thread = null;

            try
            {
                if (io != null)
                {
                    io.close();
                }
            }
            catch (Exception)
            {
                //e.printStackTrace();
            }
            io = null;
            del(this);
        }
Example #5
0
 public override void start()
 {
     try
     {
         Request request;
         if (xforwading)
         {
             request = new RequestX11();
             request.request(session, this);
         }
         if (pty)
         {
             request = new RequestPtyReq();
             request.request(session, this);
         }
         request = new RequestSubsystem();
         ((RequestSubsystem) request).request(session, this, subsystem, want_reply);
     }
     catch (Exception e)
     {
         if (e is JSchException)
         {
             throw (JSchException) e;
         }
         throw new JSchException("ChannelSubsystem");
     }
     JavaThread thread = new JavaThread(this);
     thread.Name("Subsystem for " + session.host);
     thread.Start();
 }
Example #6
0
        public 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;

            try
            {
                while (_isConnected &&
                       thread != null)
                {
                    buf = read(buf);
                    int msgType = buf.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:
                            //System.Console.WriteLine("KEXINIT");
                            kex = receive_kexinit(buf);
                            break;

                        case SSH_MSG_NEWKEYS:
                            //System.Console.WriteLine("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;
                            }
                            try
                            {
                                channel.write(foo, start[0], length[0]);
                            }
                            catch (Exception)
                            {
                                //System.Console.WriteLine(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((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.Console.WriteLine("stderr: "+new String(foo,start[0],length[0]));
                            if (channel == null)
                            {
                                break;
                            }
                            //channel.write(foo, start[0], length[0]);
                            channel.write_ext(foo, start[0], length[0]);

                            len = length[0];
                            channel.setLocalWindowSize(channel.lwsize - len);
                            if (channel.lwsize < channel.lwsize_max / 2)
                            {
                                packet.reset();
                                buf.putByte((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();
                            }
                            /*
                            packet.reset();
                            buf.putByte((byte)SSH_MSG_CHANNEL_EOF);
                            buf.putInt(channel.getRecipient());
                            write(packet);
                            */
                            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();
                            }
                            /*
                                if(Channel.pool.Count==0){
                              thread=null;
                            }
                            */
                            break;
                        case SSH_MSG_CHANNEL_OPEN_CONFIRMATION:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.getChannel(i, this);
                            if (channel == null)
                            {
                                //break;
                            }
                            channel.setRecipient(buf.getInt());
                            channel.setRemoteWindowSize(buf.getInt());
                            channel.setRemotePacketSize(buf.getInt());
                            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 = (byte)SSH_MSG_CHANNEL_FAILURE;
                                if ((new JavaString(foo)).Equals("exit-status"))
                                {
                                    i = buf.getInt(); // exit-status
                                    channel.setExitStatus(i);
                                    //	    System.Console.WriteLine("exit-stauts: "+i);
                                    //          channel.close();
                                    reply_type = (byte)SSH_MSG_CHANNEL_SUCCESS;
                                }
                                if (reply)
                                {
                                    packet.reset();
                                    buf.putByte(reply_type);
                                    buf.putInt(channel.getRecipient());
                                    write(packet);
                                }
                            }
                            else
                            {
                            }
                            break;
                        case SSH_MSG_CHANNEL_OPEN:
                            buf.getInt();
                            buf.getShort();
                            foo = buf.getString();
                            JavaString ctyp = new JavaString(foo);
                            //System.Console.WriteLine("type="+ctyp);
                            if (!new JavaString("forwarded-tcpip").Equals(ctyp) &&
                                !(new JavaString("x11").Equals(ctyp) && 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.getRecipient());
                                buf.putInt(channel.id);
                                buf.putInt(channel.lwsize);
                                buf.putInt(channel.lmpsize);
                                write(packet);
                                JavaThread tmp = new JavaThread(channel);
                                tmp.Name("Channel " + ctyp + " " + host);
                                tmp.Start();
                                break;
                            }
                        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
                            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:
                            JavaThread t = grr.getThread();
                            if (t != null)
                            {
                                grr.setReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0);
                                t.Interrupt();
                            }
                            break;
                        default:
                            Console.WriteLine("Session.run: unsupported type " + msgType);
                            throw new IOException("Unknown SSH message type " + msgType);
                    }
                }
            }
            catch (Exception)
            {
                //System.Console.WriteLine("# Session.run");
                //e.printStackTrace();
            }
            try
            {
                disconnect();
            }
            catch (NullReferenceException)
            {
                //System.Console.WriteLine("@1");
                //e.printStackTrace();
            }
            catch (Exception)
            {
                //System.Console.WriteLine("@2");
                //e.printStackTrace();
            }
            _isConnected = false;
        }
Example #7
0
 public override void start()
 {
     try
     {
         Request request;
         if (xforwading)
         {
             request = new RequestX11();
             request.request(session, this);
         }
         if (pty)
         {
             request = new RequestPtyReq();
             request.request(session, this);
         }
         request = new RequestShell();
         request.request(session, this);
     }
     catch //(System.Exception e)
     {
         throw new JSchException("ChannelShell");
     }
     thread = new JavaThread(this);
     thread.Name("Shell for " + session.host);
     thread.Start();
 }
Example #8
0
        /*
        public void finalize() throws Throwable{
          disconnect();
          jsch=null;
        }
        */
        public void disconnect()
        {
            if (!_isConnected) return;

            //System.Console.WriteLine(this+": disconnect");
            //Thread.dumpStack();
            /*
            for(int i=0; i<Channel.pool.Count; i++){
              try{
                Channel c=((Channel)(Channel.pool[i]));
            if(c.session==this) c.eof();
              }
              catch(System.Exception e){
              }
            }
            */

            Channel.disconnect(this);

            _isConnected = false;

            PortWatcher.delPort(this);
            ChannelForwardedTCPIP.delPort(this);

            lock (connectThread)
            {
                connectThread.Interrupt();
                connectThread = null;
            }
            thread = null;
            try
            {
                if (io != null)
                {
                    if (io.ins != null) io.ins.Close();
                    if (io.outs != null) io.outs.Close();
                    if (io.outs_ext != null) io.outs_ext.Close();
                }
                if (proxy == null)
                {
                    if (socket != null)
                        socket.Close();
                }
                else
                {
                    lock (proxy)
                    {
                        proxy.Close();
                    }
                    proxy = null;
                }
            }
            catch (Exception)
            {
                //      e.printStackTrace();
            }
            io = null;
            socket = null;

            //    lock(jsch.pool){
            //      jsch.pool.removeElement(this);
            //    }

            jsch.removeSession(this);

            //System.gc();
        }
Example #9
0
 internal void setThread(JavaThread thread)
 {
     this.thread = thread;
     this.reply = -1;
 }
Example #10
0
        public void connect(int connectTimeout)
        {
            if (_isConnected)
            {
                throw new JSchException("session is already connected");
            }
            io = new IO();
            if (random == null)
            {
                try
                {
                    var c = Type.GetType(getConfig("random"));
                    random = (Random)Activator.CreateInstance(c);

                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("connect: random " + e);
                }
            }
            Packet.setRandom(random);

            try
            {
                int i, j;
                // int pad = 0;

                if (proxy == null)
                {
                    proxy = jsch.getProxy(host);
                    if (proxy != null)
                    {
                        lock (proxy)
                        {
                            proxy.Close();
                        }
                    }
                }

                if (proxy == null)
                {
                    Stream In;
                    Stream 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.setTcpNoDelay(true);
                    io.setInputStream(In);
                    io.setOutputStream(Out);
                }
                else
                {
                    lock (proxy)
                    {
                        proxy.Connect(socket_factory, host, port, connectTimeout);
                        io.setInputStream(proxy.InputStream);
                        io.setOutputStream(proxy.OutputStream);
                        socket = proxy.Socket;
                    }
                }

                if (connectTimeout > 0 && socket != null)
                {
                    socket.setSoTimeout(connectTimeout);
                }

                _isConnected = true;

                while (true)
                {
                    i = 0;
                    j = 0;
                    while (i < buf.buffer.Length)
                    {
                        j = io.getByte();
                        if (j < 0) break;
                        buf.buffer[i] = (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 (buf.buffer[i - 1] == 13)
                        {
                            // 0x0d
                            i--;
                        }
                    }

                    if (i > 4 && (i != buf.buffer.Length) &&
                        (buf.buffer[0] != 'S' || buf.buffer[1] != 'S' ||
                         buf.buffer[2] != 'H' || buf.buffer[3] != '-'))
                    {
                        //System.err.println(new String(buf.buffer, 0, i);
                        continue;
                    }

                    if (i == buf.buffer.Length ||
                        i < 7 || // SSH-1.99 or SSH-2.0
                        (buf.buffer[4] == '1' && buf.buffer[6] != '9') // SSH-1.5
                        )
                    {
                        throw new JSchException("invalid server's version String");
                    }
                    break;
                }

                V_S = new byte[i];
                Array.Copy(buf.buffer, 0, V_S, 0, i);
                {
                    // Some Cisco devices will miss to read '\n' if it is sent separately.
                    byte[] foo = new byte[V_C.Length + 1];
                    Array.Copy(V_C, 0, foo, 0, V_C.Length);
                    foo[foo.Length - 1] = (byte)'\n';
                    io.put(foo, 0, foo.Length);
                }

                buf = read(buf);
                //System.Console.WriteLine("read: 20 ? "+buf.buffer[5]);
                if (buf.buffer[5] != SSH_MSG_KEXINIT)
                {
                    throw new JSchException("invalid protocol: " + buf.buffer[5]);
                }
                KeyExchange kex = receive_kexinit(buf);

                while (true)
                {
                    buf = read(buf);
                    if (kex.getState() == buf.buffer[5])
                    {
                        bool result = kex.next(buf);
                        if (!result)
                        {
                            //System.Console.WriteLine("verify: "+result);
                            in_kex = false;
                            throw new JSchException("verify: " + result);
                        }
                    }
                    else
                    {
                        in_kex = false;
                        throw new JSchException("invalid protocol(kex): " + buf.buffer[5]);
                    }
                    if (kex.getState() == KeyExchange.STATE_END)
                    {
                        break;
                    }
                }

                try
                {
                    checkHost(host, kex);
                }
                catch (JSchException ee)
                {
                    in_kex = false;
                    throw ee;
                }

                send_newkeys();

                // receive SSH_MSG_NEWKEYS(21)
                buf = read(buf);
                //System.Console.WriteLine("read: 21 ? "+buf.buffer[5]);
                if (buf.buffer[5] == SSH_MSG_NEWKEYS)
                {
                    receive_newkeys(buf, kex);
                }
                else
                {
                    in_kex = false;
                    throw new JSchException("invalid protocol(newkyes): " + buf.buffer[5]);
                }

                bool auth = false;
                bool auth_cancel = false;

                UserAuthNone usn = new UserAuthNone(userinfo);
                auth = usn.start(this);

                String methods = null;
                if (!auth)
                {
                    methods = usn.getMethods();
                    if (methods != null)
                    {
                        methods = methods.ToLower();
                    }
                    else
                    {
                        // methods: publickey,password,keyboard-interactive
                        methods = "publickey,password,keyboard-interactive";
                    }
                }

                while (true)
                {
                    while (!auth && methods != null && methods.Length > 0)
                    {
                        UserAuth us = null;
                        if (methods.StartsWith("publickey"))
                        {
                            lock (jsch.identities)
                            {
                                if (jsch.identities.Count > 0)
                                {
                                    us = new UserAuthPublicKey(userinfo);
                                }
                            }
                        }
                        else if (methods.StartsWith("keyboard-interactive"))
                        {
                            if (userinfo is UIKeyboardInteractive)
                            {
                                us = new UserAuthKeyboardInteractive(userinfo);
                            }
                        }
                        else if (methods.StartsWith("password"))
                        {
                            us = new UserAuthPassword(userinfo);
                        }
                        if (us != null)
                        {
                            try
                            {
                                auth = us.start(this);
                                auth_cancel = false;
                            }
                            catch (JSchAuthCancelException)
                            {
                                auth_cancel = true;
                            }
                            catch (JSchPartialAuthException ee)
                            {
                                methods = ee.getMethods();
                                auth_cancel = false;
                                continue;
                            }
                            catch (Exception ee)
                            {
                                Console.WriteLine("ee: " + ee);
                            }
                        }
                        if (!auth)
                        {
                            int comma = methods.IndexOf(",");
                            if (comma == -1) break;
                            methods = methods.Substring(comma + 1);
                        }
                    }
                    break;
                }

                if (connectTimeout > 0 || timeout > 0)
                {
                    socket.setSoTimeout(timeout);
                }

                if (auth)
                {
                    isAuthed = true;
                    connectThread = new JavaThread(this);
                    connectThread.Name("Connect thread " + host + " session");
                    connectThread.Start();
                    return;
                }
                if (auth_cancel)
                    throw new JSchException("Auth cancel");
                throw new JSchException("Auth fail");
            }
            catch (Exception e)
            {
                in_kex = false;
                if (_isConnected)
                {
                    try
                    {
                        packet.reset();
                        buf.putByte((byte)SSH_MSG_DISCONNECT);
                        buf.putInt(3);
                        buf.putString(new JavaString(e.ToString()).GetBytes());
                        buf.putString(new JavaString("en").GetBytes());
                        write(packet);
                        disconnect();
                    }
                    catch (Exception) { }
                }
                _isConnected = false;
                if (e is JSchException) throw (JSchException)e;
                throw new JSchException("Session.connect: " + e);
            }
        }
Example #11
0
 public void setPortForwardingL(JavaString boundaddress, int lport, JavaString host, int rport, ServerSocketFactory ssf)
 {
     PortWatcher pw = PortWatcher.addPort(this, boundaddress, lport, host, rport, ssf);
     JavaThread tmp = new JavaThread(pw);
     tmp.Name("PortWatcher Thread for " + host);
     tmp.Start();
 }
Example #12
0
 protected void addRunningThread(JavaThread thread)
 {
     if (threadList == null) threadList = new ArrayList();
     threadList.Add(thread);
 }