Пример #1
0
        public void request(Session session, Channel channel)
        {
            Buffer buf=new Buffer();
            Packet packet=new Packet(buf);

            bool reply=waitForReply();
            if(reply)
            {
                channel.reply=-1;
            }

            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(Util.getBytes("subsystem"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(Util.getBytes("sftp"));
            session.write(packet);

            if(reply)
            {
                while(channel.reply==-1)
                {
                    try{System.Threading.Thread.Sleep(10);}
                    catch//(Exception ee)
                    {
                    }
                }
                if(channel.reply==0)
                {
                    throw new JSchException("failed to send sftp request");
                }
            }
        }
Пример #2
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.getRecipient());
            buf.putString( Util.getBytes("signal"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(Util.getBytes(signal));
            session.write(packet);
        }
Пример #3
0
		public override void run()
		{
			//System.out.println(this+":run >");
			/*
				if(thread!=null){ return; }
				thread=Thread.currentThread();
			*/

			//    Buffer buf=new Buffer();
			Buffer buf=new Buffer(rmpsize);
			Packet packet=new Packet(buf);
			int i=-1;
			try
			{
				while(isConnected() &&
					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)continue;
					if(i==-1)
					{
						eof();
						break;
					}
					if(_close)break;
					packet.reset();
					buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
					buf.putInt(recipient);
					buf.putInt(i);
					buf.skip(i);
					session.write(packet, this, i);
				}
			}
			catch(Exception e)
			{
				Console.WriteLine("# ChannelExec.run");
				Console.WriteLine(e);
			}
			if(thread!=null)
			{
				//lock(thread){ System.Threading.Monitor.PulseAll(this);/*thread.notifyAll();*/ }
			}
			thread=null;
			//System.out.println(this+":run <");
		}
Пример #4
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.getRecipient());
            buf.putString(Util.getBytes("shell"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            session.write(packet);
        }
Пример #5
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.getRecipient());
            buf.putString(Util.getBytes("window-change"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putInt(width_columns);
            buf.putInt(height_rows);
            buf.putInt(width_pixels);
            buf.putInt(height_pixels);
            session.write(packet);
        }
Пример #6
0
		public void request(Session session, Channel channel) 
		{
			Buffer buf=new Buffer();
			Packet packet=new Packet(buf);

			// byte      SSH_MSG_CHANNEL_REQUEST(98)
			// uint32 recipient channel
			// string request type        // "x11-req"
			// boolean want reply         // 0
			// boolean   single connection
			// string    x11 authentication protocol // "MIT-MAGIC-COOKIE-1".
			// string    x11 authentication cookie
			// uint32    x11 screen number
			packet.reset();
			buf.putByte((byte) Session.SSH_MSG_CHANNEL_REQUEST);
			buf.putInt(channel.getRecipient());
			buf.putString(Util.getBytes("x11-req"));
			buf.putByte((byte)(waitForReply() ? 1 : 0));
			buf.putByte((byte)0);
			buf.putString(Util.getBytes("MIT-MAGIC-COOKIE-1"));
			buf.putString(ChannelX11.getFakedCookie(session));
			buf.putInt(0);
			session.write(packet);
		}
Пример #7
0
        internal static void delPort(Session session, int rport)
        {
            lock(pool)
            {
                Object[] foo=null;
                for(int i=0; i<pool.size(); i++)
                {
                    Object[] bar=(Object[])(pool.elementAt(i));
                    if(bar[0]!=session) continue;
                    if(((Integer)bar[1]).intValue()!=rport) continue;
                    foo=bar;
                    break;
                }
                if(foo==null)return;
                pool.removeElement(foo);
            }

            Buffer buf=new Buffer(100); // ??
            Packet packet=new Packet(buf);

            try
            {
                // byte SSH_MSG_GLOBAL_REQUEST 80
                // string "cancel-tcpip-forward"
                // boolean want_reply
                // string  address_to_bind (e.g. "127.0.0.1")
                // uint32  port number to bind
                packet.reset();
                buf.putByte((byte) 80/*SSH_MSG_GLOBAL_REQUEST*/);
                buf.putString(new Str("cancel-tcpip-forward").getBytes());
                buf.putByte((byte)0);
                buf.putString(new Str("0.0.0.0").getBytes());
                buf.putInt(rport);
                session.write(packet);
            }
            catch(Exception e)
            {
                //    throw new JSchException(e.toString());
            }
        }
Пример #8
0
 internal Session(JSch jsch)
 {
     ;
     this.jsch=jsch;
     buf=new Buffer();
     packet=new Packet(buf);
 }
Пример #9
0
Файл: IO.cs Проект: itsbth/GLuaR
 public void put(Packet p)
 {
     outs.Write(p.buffer.buffer, 0, p.buffer.index);
     outs.Flush();
 }
Пример #10
0
 /*
 public lockpublic void write(Packet packet) {
    encode(packet);
    if(io!=null){
      io.put(packet);
      seqo++;
    }
 }
 */
 public void write(Packet packet)
 {
     // System.Console.WriteLine("in_kex="+in_kex+" "+(packet.buffer.buffer[5]));
     while(in_kex)
     {
         byte command=packet.buffer.buffer[5];
         //System.Console.WriteLine("command: "+command);
         if(command==SSH_MSG_KEXINIT ||
             command==SSH_MSG_NEWKEYS ||
             command==SSH_MSG_KEXDH_INIT ||
             command==SSH_MSG_KEXDH_REPLY)
         {
             break;
         }
         try{Thread.Sleep(10);}
         catch(ThreadInterruptedException e){};
     }
     _write(packet);
 }
Пример #11
0
        private void setPortForwarding(int rport)
        {
            lock(grr)
            {
                Buffer buf=new Buffer(100); // ??
                Packet packet=new Packet(buf);

                try
                {
                    // byte SSH_MSG_GLOBAL_REQUEST 80
                    // String "tcpip-forward"
                    // bool want_reply
                    // String  address_to_bind
                    // uint32  port number to bind
                    packet.reset();
                    buf.putByte((byte) SSH_MSG_GLOBAL_REQUEST);
                    buf.putString( new String( "tcpip-forward" ).getBytes());
                    //      buf.putByte((byte)0);
                    buf.putByte((byte)1);
                    buf.putString(new String("0.0.0.0").getBytes());
                    buf.putInt(rport);
                    write(packet);
                }
                catch(Exception e)
                {
                    throw new JSchException(e.ToString());
                }

                grr.setThread(Thread.currentThread());
                try{ Thread.Sleep(10000);}
                catch(Exception e)
                {
                }
                int reply=grr.getReply();
                grr.setThread(null);
                if(reply==0)
                {
                    throw new JSchException("remote port forwarding failed for listen port "+rport);
                }
            }
        }
Пример #12
0
        //private byte[] f;
        public override void init(Session session,
			byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C)
        {
            this.session=session;
            this.V_S=V_S;
            this.V_C=V_C;
            this.I_S=I_S;
            this.I_C=I_C;

            //    sha=new SHA1();
            //    sha.init();

            try
            {
                Type t=Type.GetType(session.getConfig("sha-1"));
                sha=(HASH)(Activator.CreateInstance(t));
                sha.init();
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }

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

            try
            {
                Type t=Type.GetType(session.getConfig("dh"));
                dh=(DH)(Activator.CreateInstance(t));
                dh.init();
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }

            packet.reset();
            buf.putByte((byte)0x22);
            buf.putInt(min);
            buf.putInt(preferred);
            buf.putInt(max);
            session.write(packet);

            state=SSH_MSG_KEX_DH_GEX_GROUP;
        }
Пример #13
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 Thread(this);
                thread.start();
            }
            catch(Exception e)
            {
                io.close();
                io=null;
                Channel.del(this);
                if (e is JSchException)
                {
                    throw (JSchException) e;
                }
            }
        }
Пример #14
0
        /*public*/
        /*lock*/
        public void write(Packet packet, Channel c, int length)
        {
            while(true)
                               {
                                   if(in_kex)
                                   {
                                       try{Thread.Sleep(10);}
                                       catch(ThreadInterruptedException e){};
                                       continue;
                                   }
                                   lock(c)
                                   {
                                       if(c.rwsize>=length)
                                       {
                                           c.rwsize-=length;
                                           break;
                                       }
                                   }
                                   if(c._close || !c.isConnected())
                                   {
                                       throw new IOException("channel is broken");
                                   }

                                   bool sendit=false;
                                   int s=0;
                                   byte command=0;
                                   int recipient=-1;
                                   lock(c)
                                   {
                                       if(c.rwsize>0)
                                       {
                                           int len=c.rwsize;
                                           s=packet.shift(len, (c2smac!=null ? c2smac.getBlockSize() : 0));
                                           command=packet.buffer.buffer[5];
                                           recipient=c.getRecipient();
                                           length-=len;
                                           c.rwsize-=len;
                                           sendit=true;
                                       }
                                   }
                                   if(sendit)
                                   {
                                       _write(packet);
                                       packet.unshift(command, recipient, s, length);
                                   }

                                   try{Thread.Sleep(10);}
                                   catch(ThreadInterruptedException e){};
                               }
                               _write(packet);
        }
Пример #15
0
 internal virtual void eof()
 {
     //System.Out.println("EOF!!!! "+this);
     //Thread.dumpStack();
     if(_close)return;
     if(eof_local)return;
     eof_local=true;
     //close=eof;
     try
     {
         Buffer buf=new Buffer(100);
         Packet packet=new Packet(buf);
         packet.reset();
         buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF);
         buf.putInt(getRecipient());
         session.write(packet);
     }
     catch(Exception e)
     {
         //System.Out.println("Channel.eof");
         //e.printStackTrace();
     }
     /*
     if(!isConnected()){ disconnect(); }
     */
 }
Пример #16
0
        public virtual void connect()
        {
            if(!session.isConnected())
            {
                throw new JSchException("session is down");
            }
            try
            {
                Buffer buf=new Buffer(100);
                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(this.type);
                buf.putInt(this.id);
                buf.putInt(this.lwsize);
                buf.putInt(this.lmpsize);
                session.write(packet);

                int retry=1000;
                while(this.getRecipient()==-1 &&
                    session.isConnected() &&
                    retry>0)
                {
                    try{Thread.sleep(50);}
                    catch(Exception ee){}
                    retry--;
                }
                if(!session.isConnected())
                {
                    throw new JSchException("session is down");
                }
                if(retry==0)
                {
                    throw new JSchException("channel is not opened.");
                }
                connected=true;
                start();
            }
            catch(Exception e)
            {
                connected=false;
                if(e is JSchException) throw (JSchException)e;
            }
        }
Пример #17
0
		public override void start() 
		{
			try
			{
				PipedOutputStream pos=new PipedOutputStream();
				io.setOutputStream(pos);
				//      PipedInputStream pis=new PipedInputStream(pos);
				PipedInputStream pis=new MyPipedInputStream(pos, 32*1024);
				io.setInputStream(pis);

				Request request=new RequestSftp();
				request.request(session, this);

				//    thread=Thread.currentThread();

				//      buf=new Buffer();
				buf=new Buffer(rmpsize);
				packet=new Packet(buf);
				int i=0;
				int j=0;
				int length;
				int type;
				byte[] str;

				// send SSH_FXP_INIT
				sendINIT();

				// receive SSH_FXP_VERSION
				buf.rewind();
				i=io.ins.Read(buf.buffer, 0, buf.buffer.Length);
				//System.out.println(io+" "+io.ins+" "+io.out);
				length=buf.getInt();
				type=buf.getByte();           // 2 -> SSH_FXP_VERSION
				server_version=buf.getInt();
				//System.out.println("SFTP protocol server-version="+server_version);

				// send SSH_FXP_REALPATH
				sendREALPATH(Util.getBytes("."));

				// receive SSH_FXP_NAME
				buf.rewind();
				i=io.ins.Read(buf.buffer, 0, buf.buffer.Length);
				length=buf.getInt();
				type=buf.getByte();          // 104 -> SSH_FXP_NAME
				buf.getInt();                //
				i=buf.getInt();              // count
				str=buf.getString();         // filename
				home=cwd=Util.getString(str);
				str=buf.getString();         // logname
				//    SftpATTRS.getATTR(buf);           // attrs

				lcwd=Path.GetFullPath(".");

				//thread=new Thread(this);
				//thread.setName("Sftp for "+session.host);
				//thread.start();
			}
			catch(Exception e)
			{
				//System.out.println(e);
				if(e is JSchException) throw (JSchException)e;
				throw new JSchException(e.ToString());
			}
		}
Пример #18
0
		public ChannelSftp()
		{
			packet=new Packet(buf);
		}
Пример #19
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(msgType!=94)
                    //System.Console.WriteLine("read: 94 ? "+msgType);

                    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 e)
                            {
                                //System.Console.WriteLine(e);
                                try{channel.disconnect();}
                                catch(Exception ee){}
                                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.size()==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 String(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();
                            String ctyp=new String(foo);
                            //System.Console.WriteLine("type="+ctyp);
                            if(!new String("forwarded-tcpip").equals(ctyp) &&
                                !(new String("x11").equals(ctyp) && x11_forwarding))
                            {
                                System.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);
                                Thread tmp=new Thread(channel);
                                tmp.setName("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:
                            Thread t=grr.getThread();
                            if(t!=null)
                            {
                                grr.setReply(msgType==SSH_MSG_REQUEST_SUCCESS? 1 : 0);
                                t.interrupt();
                            }
                            break;
                        default:
                            System.Console.WriteLine("Session.run: unsupported type "+msgType);
                            throw new IOException("Unknown SSH message type "+msgType);
                    }
                }
            }
            catch(Exception e)
            {
                //System.Console.WriteLine("# Session.run");
                //e.printStackTrace();
            }
            try
            {
                disconnect();
            }
            catch(NullReferenceException e)
            {
                //System.Console.WriteLine("@1");
                //e.printStackTrace();
            }
            catch(Exception e)
            {
                //System.Console.WriteLine("@2");
                //e.printStackTrace();
            }
            _isConnected=false;
        }
Пример #20
0
 // encode will bin invoked in write with synchronization.
 public void encode(Packet packet)
 {
     //System.Console.WriteLine("encode: "+packet.buffer.buffer[5]);
     //System.Console.WriteLine("        "+packet.buffer.index);
     //if(packet.buffer.buffer[5]==96){
     //Thread.dumpStack();
     //}
     if(deflater!=null)
     {
         packet.buffer.index=deflater.compress(packet.buffer.buffer,
             5, packet.buffer.index);
     }
     if(c2scipher!=null)
     {
         packet.padding(c2scipher.getIVSize());
         int pad=packet.buffer.buffer[4];
         lock(random)
         {
             random.fill(packet.buffer.buffer, packet.buffer.index-pad, pad);
         }
     }
     else
     {
         packet.padding(8);
     }
     byte[] mac=null;
     if(c2smac!=null)
     {
         c2smac.update(seqo);
         c2smac.update(packet.buffer.buffer, 0, packet.buffer.index);
         mac=c2smac.doFinal();
     }
     if(c2scipher!=null)
     {
         byte[] buf=packet.buffer.buffer;
         c2scipher.update(buf, 0, packet.buffer.index, buf, 0);
     }
     if(mac!=null)
     {
         packet.buffer.putByte(mac);
     }
 }
Пример #21
0
 private void _write(Packet packet)
 {
     encode(packet);
     if(io!=null)
     {
         io.put(packet);
         seqo++;
     }
 }
Пример #22
0
 public override void run()
 {
     thread=Thread.currentThread();
     Buffer buf=new Buffer(rmpsize);
     Packet packet=new Packet(buf);
     int i=0;
     try
     {
         while(thread!=null)
         {
             i=io.ins.Read(buf.buffer,
                 14,
                 buf.buffer.Length-14
                 -16 -20 // padding and mac
                 );
             if(i<=0)
             {
                 eof();
                 break;
             }
             if(_close)break;
             packet.reset();
             buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
             buf.putInt(recipient);
             buf.putInt(i);
             buf.skip(i);
             session.write(packet, this, i);
         }
     }
     catch
     {
         //System.out.println(e);
     }
     thread=null;
 }
Пример #23
0
		public void run()
		{
			Buffer buf=new Buffer(300); // ??
			Packet packet=new Packet(buf);
			thread=this;
			try
			{
				while(thread!=null)
				{
					Socket socket=ss.accept();
					socket.setTcpNoDelay(true);
					Stream In=socket.getInputStream();
					Stream Out=socket.getOutputStream();
					ChannelDirectTCPIP channel=new ChannelDirectTCPIP();
					channel.init();
					channel.setInputStream(In);
					channel.setOutputStream(Out);
					session.addChannel(channel);
					((ChannelDirectTCPIP)channel).setHost(host);
					((ChannelDirectTCPIP)channel).setPort(rport);
					((ChannelDirectTCPIP)channel).setOrgIPAddress(socket.getInetAddress().getHostAddress());
					((ChannelDirectTCPIP)channel).setOrgPort(socket.getPort());
					channel.connect();
					if(channel.exitstatus!=-1)
					{
					}
				}
			}
			catch(Exception e)
			{
				//System.out.println("! "+e);
			}

			delete();
		}
Пример #24
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()
        {
            //System.Out.println("close!!!!");
            if(_close)return;
            _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(getRecipient());
                session.write(packet);
            }
            catch(Exception e)
            {
                //e.printStackTrace();
            }
        }
Пример #25
0
        public override void run()
        {
            //    thread=Thread.currentThread();
            //System.out.println("rmpsize: "+rmpsize+", lmpsize: "+lmpsize);
            Buffer buf=new Buffer(rmpsize);
            //    Buffer buf=new Buffer(lmpsize);
            Packet packet=new Packet(buf);
            int i=0;
            try
            {
                while(isConnected() &&
                    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;
                    }
                    if(_close)break;
                    packet.reset();
                    buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
                    buf.putInt(recipient);
                    buf.putInt(i);
                    buf.skip(i);
                    session.write(packet, this, i);
                }
            }
            catch
            {
            }
            disconnect();
            //System.out.println("connect end");

            /*
                try{
                  packet.reset();
                  buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF);
                  buf.putInt(recipient);
                  session.write(packet);
                }
                catch(Exception e){
                }
            */
            //    close();
        }
Пример #26
0
        public override void init(Session session,
			byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C)
        {
            this.session=session;
            this.V_S=V_S;
            this.V_C=V_C;
            this.I_S=I_S;
            this.I_C=I_C;

            //    sha=new SHA1();
            //    sha.init();
            try
            {
                Type t=Type.GetType(session.getConfig("sha-1"));
                sha=(HASH)(Activator.CreateInstance(t));
                sha.init();
            }
            catch(Exception ee)
            {
                Console.WriteLine(ee);
            }

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

            try
            {
                Type t=Type.GetType(session.getConfig("dh"));
                dh=(DH)(Activator.CreateInstance(t));
                dh.init();
            }
            catch(Exception ee)
            {
                Console.WriteLine(ee);
            }

            dh.setP(p);
            dh.setG(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)

            e=dh.getE();

            packet.reset();
            buf.putByte((byte)SSH_MSG_KEXDH_INIT);
            buf.putMPInt(e);
            session.write(packet);

            state=SSH_MSG_KEXDH_REPLY;
        }
Пример #27
0
 public void sendIgnore()
 {
     Buffer buf=new Buffer();
     Packet packet=new Packet(buf);
     packet.reset();
     buf.putByte((byte)SSH_MSG_IGNORE);
     write(packet);
 }