Пример #1
0
        public override void Run()
        {
            Buffer buf    = new Buffer(rmpsize);
            Packet packet = new Packet(buf);
            int    i      = 0;

            try
            {
                Session _session = GetSession();
                while (IsConnected() && thread != null && io != null && io.@in != null)
                {
                    i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
                    // padding and mac
                    if (i <= 0)
                    {
                        Eof();
                        break;
                    }
                    if (close)
                    {
                        break;
                    }
                    packet.Reset();
                    buf.PutByte(unchecked ((byte)Session.SSH_MSG_CHANNEL_DATA));
                    buf.PutInt(recipient);
                    buf.PutInt(i);
                    buf.Skip(i);
                    _session.Write(packet, this, i);
                }
            }
            catch (Exception)
            {
            }
            Disconnect();
        }
Пример #2
0
        public override void Run()
        {
            try
            {
                socket         = Util.CreateSocket(host, port, TIMEOUT);
                socket.NoDelay = true;
                io             = new IO();
                io.SetInputStream(socket.GetInputStream());
                io.SetOutputStream(socket.GetOutputStream());
                SendOpenConfirmation();
            }
            catch (Exception)
            {
                SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
                close = true;
                Disconnect();
                return;
            }
            thread = Sharpen.Thread.CurrentThread();
            Buffer buf    = new Buffer(rmpsize);
            Packet packet = new Packet(buf);
            int    i      = 0;

            try
            {
                while (thread != null && io != null && io.@in != null)
                {
                    i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
                    // padding and mac
                    if (i <= 0)
                    {
                        Eof();
                        break;
                    }
                    if (close)
                    {
                        break;
                    }
                    packet.Reset();
                    buf.PutByte(unchecked ((byte)Session.SSH_MSG_CHANNEL_DATA));
                    buf.PutInt(recipient);
                    buf.PutInt(i);
                    buf.Skip(i);
                    GetSession().Write(packet, this, i);
                }
            }
            catch (Exception)
            {
            }
            //System.err.println(e);
            Disconnect();
        }
Пример #3
0
        public override void Run()
        {
            //System.err.println(this+":run >");
            Buffer buf    = new Buffer(rmpsize);
            Packet packet = new Packet(buf);
            int    i      = -1;

            try
            {
                while (IsConnected() && thread != null && io != null && io.@in != null)
                {
                    i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
                    // padding and mac
                    if (i == 0)
                    {
                        continue;
                    }
                    if (i == -1)
                    {
                        Eof();
                        break;
                    }
                    if (close)
                    {
                        break;
                    }
                    //System.out.println("write: "+i);
                    packet.Reset();
                    buf.PutByte(unchecked ((byte)Session.SSH_MSG_CHANNEL_DATA));
                    buf.PutInt(recipient);
                    buf.PutInt(i);
                    buf.Skip(i);
                    GetSession().Write(packet, this, i);
                }
            }
            catch (Exception)
            {
            }
            //System.err.println("# ChannelExec.run");
            //e.printStackTrace();
            Sharpen.Thread _thread = thread;
            if (_thread != null)
            {
                lock (_thread)
                {
                    Sharpen.Runtime.NotifyAll(_thread);
                }
            }
            thread = null;
        }
Пример #4
0
        internal virtual void Padding(int bsize)
        {
            int len = buffer.index;
            int pad = (-len) & (bsize - 1);

            if (pad < bsize)
            {
                pad += bsize;
            }
            len    = len + pad - 4;
            ba4[0] = unchecked ((byte)((int)(((uint)len) >> 24)));
            ba4[1] = unchecked ((byte)((int)(((uint)len) >> 16)));
            ba4[2] = unchecked ((byte)((int)(((uint)len) >> 8)));
            ba4[3] = unchecked ((byte)(len));
            System.Array.Copy(ba4, 0, buffer.buffer, 0, 4);
            buffer.buffer[4] = unchecked ((byte)pad);
            lock (random)
            {
                random.Fill(buffer.buffer, buffer.index, pad);
            }
            buffer.Skip(pad);
        }
 public override void Run()
 {
     try
     {
         SendChannelOpen();
         Buffer  buf      = new Buffer(rmpsize);
         Packet  packet   = new Packet(buf);
         Session _session = GetSession();
         int     i        = 0;
         while (IsConnected() && thread != null && io != null && io.@in != null)
         {
             i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - Session.buffer_margin);
             if (i <= 0)
             {
                 Eof();
                 break;
             }
             packet.Reset();
             buf.PutByte(unchecked ((byte)Session.SSH_MSG_CHANNEL_DATA));
             buf.PutInt(recipient);
             buf.PutInt(i);
             buf.Skip(i);
             lock (this)
             {
                 if (close)
                 {
                     break;
                 }
                 _session.Write(packet, this, i);
             }
         }
     }
     catch (Exception)
     {
     }
     Disconnect();
 }
Пример #6
0
		/// <exception cref="System.Exception"></exception>
		private void Send_kexinit()
		{
			if (in_kex)
			{
				return;
			}
			string cipherc2s = GetConfig("cipher.c2s");
			string ciphers2c = GetConfig("cipher.s2c");
			string[] not_available = CheckCiphers(GetConfig("CheckCiphers"));
			if (not_available != null && not_available.Length > 0)
			{
				cipherc2s = Util.DiffString(cipherc2s, not_available);
				ciphers2c = Util.DiffString(ciphers2c, not_available);
				if (cipherc2s == null || ciphers2c == null)
				{
					throw new JSchException("There are not any available ciphers.");
				}
			}
			in_kex = true;
			kex_start_time = Runtime.CurrentTimeMillis();
			// byte      SSH_MSG_KEXINIT(20)
			// byte[16]  cookie (random bytes)
			// string    kex_algorithms
			// string    server_host_key_algorithms
			// string    encryption_algorithms_client_to_server
			// string    encryption_algorithms_server_to_client
			// string    mac_algorithms_client_to_server
			// string    mac_algorithms_server_to_client
			// string    compression_algorithms_client_to_server
			// string    compression_algorithms_server_to_client
			// string    languages_client_to_server
			// string    languages_server_to_client
			Buffer buf = new Buffer();
			// send_kexinit may be invoked
			Packet packet = new Packet(buf);
			// by user thread.
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_KEXINIT));
			lock (random)
			{
				random.Fill(buf.buffer, buf.index, 16);
				buf.Skip(16);
			}
			buf.PutString(Util.Str2byte(GetConfig("kex")));
			buf.PutString(Util.Str2byte(GetConfig("server_host_key")));
			buf.PutString(Util.Str2byte(cipherc2s));
			buf.PutString(Util.Str2byte(ciphers2c));
			buf.PutString(Util.Str2byte(GetConfig("mac.c2s")));
			buf.PutString(Util.Str2byte(GetConfig("mac.s2c")));
			buf.PutString(Util.Str2byte(GetConfig("compression.c2s")));
			buf.PutString(Util.Str2byte(GetConfig("compression.s2c")));
			buf.PutString(Util.Str2byte(GetConfig("lang.c2s")));
			buf.PutString(Util.Str2byte(GetConfig("lang.s2c")));
			buf.PutByte(unchecked((byte)0));
			buf.PutInt(0);
			buf.SetOffSet(5);
			I_C = new byte[buf.GetLength()];
			buf.GetByte(I_C);
			Write(packet);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEXINIT sent");
			}
		}
Пример #7
0
		public override void Run()
		{
			try
			{
				SendChannelOpen();
				Buffer buf = new Buffer(rmpsize);
				Packet packet = new Packet(buf);
				Session _session = GetSession();
				int i = 0;
				while (IsConnected() && thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - Session.buffer_margin);
					if (i <= 0)
					{
						Eof();
						break;
					}
					packet.Reset();
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					lock (this)
					{
						if (close)
						{
							break;
						}
						_session.Write(packet, this, i);
					}
				}
			}
			catch (Exception)
			{
			}
			Disconnect();
		}
Пример #8
0
        /// <exception cref="System.IO.IOException"></exception>
        internal override void Write(byte[] foo, int s, int l)
        {
            if (packet == null)
            {
                wbuf   = new Buffer(rmpsize);
                packet = new Packet(wbuf);
            }
            rbuf.Shift();
            if (rbuf.buffer.Length < rbuf.index + l)
            {
                byte[] newbuf = new byte[rbuf.s + l];
                System.Array.Copy(rbuf.buffer, 0, newbuf, 0, rbuf.buffer.Length);
                rbuf.buffer = newbuf;
            }
            rbuf.PutByte(foo, s, l);
            int mlen = rbuf.GetInt();

            if (mlen > rbuf.GetLength())
            {
                rbuf.s -= 4;
                return;
            }
            int     typ      = rbuf.GetByte();
            Session _session = null;

            try
            {
                _session = GetSession();
            }
            catch (JSchException e)
            {
                throw new IOException(e.ToString());
            }
            IdentityRepository irepo    = _session.jsch.GetIdentityRepository();
            UserInfo           userinfo = _session.GetUserInfo();

            mbuf.Reset();
            if (typ == SSH2_AGENTC_REQUEST_IDENTITIES)
            {
                mbuf.PutByte(SSH2_AGENT_IDENTITIES_ANSWER);
                ArrayList identities = irepo.GetIdentities();
                lock (identities)
                {
                    int count = 0;
                    for (int i = 0; i < identities.Count; i++)
                    {
                        Identity identity = (Identity)(identities[i]);
                        if (identity.GetPublicKeyBlob() != null)
                        {
                            count++;
                        }
                    }
                    mbuf.PutInt(count);
                    for (int i_1 = 0; i_1 < identities.Count; i_1++)
                    {
                        Identity identity   = (Identity)(identities[i_1]);
                        byte[]   pubkeyblob = identity.GetPublicKeyBlob();
                        if (pubkeyblob == null)
                        {
                            continue;
                        }
                        mbuf.PutString(pubkeyblob);
                        mbuf.PutString(Util.empty);
                    }
                }
            }
            else
            {
                if (typ == SSH_AGENTC_REQUEST_RSA_IDENTITIES)
                {
                    mbuf.PutByte(SSH_AGENT_RSA_IDENTITIES_ANSWER);
                    mbuf.PutInt(0);
                }
                else
                {
                    if (typ == SSH2_AGENTC_SIGN_REQUEST)
                    {
                        byte[] blob  = rbuf.GetString();
                        byte[] data  = rbuf.GetString();
                        int    flags = rbuf.GetInt();
                        //      if((flags & 1)!=0){ //SSH_AGENT_OLD_SIGNATURE // old OpenSSH 2.0, 2.1
                        //        datafellows = SSH_BUG_SIGBLOB;
                        //      }
                        ArrayList identities = irepo.GetIdentities();
                        Identity  identity   = null;
                        lock (identities)
                        {
                            for (int i = 0; i < identities.Count; i++)
                            {
                                Identity _identity = (Identity)(identities[i]);
                                if (_identity.GetPublicKeyBlob() == null)
                                {
                                    continue;
                                }
                                if (!Util.Array_equals(blob, _identity.GetPublicKeyBlob()))
                                {
                                    continue;
                                }
                                if (_identity.IsEncrypted())
                                {
                                    if (userinfo == null)
                                    {
                                        continue;
                                    }
                                    while (_identity.IsEncrypted())
                                    {
                                        if (!userinfo.PromptPassphrase("Passphrase for " + _identity.GetName()))
                                        {
                                            break;
                                        }
                                        string _passphrase = userinfo.GetPassphrase();
                                        if (_passphrase == null)
                                        {
                                            break;
                                        }
                                        byte[] passphrase = Util.Str2byte(_passphrase);
                                        try
                                        {
                                            if (_identity.SetPassphrase(passphrase))
                                            {
                                                break;
                                            }
                                        }
                                        catch (JSchException)
                                        {
                                            break;
                                        }
                                    }
                                }
                                if (!_identity.IsEncrypted())
                                {
                                    identity = _identity;
                                    break;
                                }
                            }
                        }
                        byte[] signature = null;
                        if (identity != null)
                        {
                            signature = identity.GetSignature(data);
                        }
                        if (signature == null)
                        {
                            mbuf.PutByte(SSH2_AGENT_FAILURE);
                        }
                        else
                        {
                            mbuf.PutByte(SSH2_AGENT_SIGN_RESPONSE);
                            mbuf.PutString(signature);
                        }
                    }
                    else
                    {
                        if (typ == SSH2_AGENTC_REMOVE_IDENTITY)
                        {
                            byte[] blob = rbuf.GetString();
                            irepo.Remove(blob);
                            mbuf.PutByte(SSH_AGENT_SUCCESS);
                        }
                        else
                        {
                            if (typ == SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES)
                            {
                                mbuf.PutByte(SSH_AGENT_SUCCESS);
                            }
                            else
                            {
                                if (typ == SSH2_AGENTC_REMOVE_ALL_IDENTITIES)
                                {
                                    irepo.RemoveAll();
                                    mbuf.PutByte(SSH_AGENT_SUCCESS);
                                }
                                else
                                {
                                    if (typ == SSH2_AGENTC_ADD_IDENTITY)
                                    {
                                        int    fooo = rbuf.GetLength();
                                        byte[] tmp  = new byte[fooo];
                                        rbuf.GetByte(tmp);
                                        bool result = irepo.Add(tmp);
                                        mbuf.PutByte(result ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
                                    }
                                    else
                                    {
                                        rbuf.Skip(rbuf.GetLength() - 1);
                                        mbuf.PutByte(SSH_AGENT_FAILURE);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            byte[] response = new byte[mbuf.GetLength()];
            mbuf.GetByte(response);
            Send(response);
        }
Пример #9
0
		/// <exception cref="System.IO.IOException"></exception>
		private void Fill(Buffer buf, int len)
		{
			buf.Reset();
			Fill(buf.buffer, 0, len);
			buf.Skip(len);
		}
Пример #10
0
		public override void Run()
		{
			Buffer buf = new Buffer(rmpsize);
			Packet packet = new Packet(buf);
			int i = 0;
			try
			{
				Session _session = GetSession();
				while (IsConnected() && thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
					// padding and mac
					if (i <= 0)
					{
						Eof();
						break;
					}
					if (close)
					{
						break;
					}
					packet.Reset();
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					_session.Write(packet, this, i);
				}
			}
			catch (Exception)
			{
			}
			Disconnect();
		}
Пример #11
0
		public override void Run()
		{
			//System.err.println(this+":run >");
			Buffer buf = new Buffer(rmpsize);
			Packet packet = new Packet(buf);
			int i = -1;
			try
			{
				while (IsConnected() && thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - Session.buffer_margin);
					if (i == 0)
					{
						continue;
					}
					if (i == -1)
					{
						Eof();
						break;
					}
					if (close)
					{
						break;
					}
					//System.out.println("write: "+i);
					packet.Reset();
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					GetSession().Write(packet, this, i);
				}
			}
			catch (Exception)
			{
			}
			//System.err.println("# ChannelExec.run");
			//e.printStackTrace();
			Sharpen.Thread _thread = thread;
			if (_thread != null)
			{
				lock (_thread)
				{
					Sharpen.Runtime.NotifyAll(_thread);
				}
			}
			thread = null;
		}
Пример #12
0
		public override void Run()
		{
			try
			{
				if (lport == -1)
				{
					Type c = Sharpen.Runtime.GetType(target);
					daemon = (ForwardedTCPIPDaemon)System.Activator.CreateInstance(c);
					PipedOutputStream @out = new PipedOutputStream();
					io.SetInputStream(new Channel.PassiveInputStream(this, @out, 32 * 1024), false);
					daemon.SetChannel(this, GetInputStream(), @out);
					object[] foo = GetPort(GetSession(), rport);
					daemon.SetArg((object[])foo[3]);
					new Sharpen.Thread(daemon).Start();
				}
				else
				{
					socket = (factory == null) ? Util.CreateSocket(target, lport, TIMEOUT) : factory.
						CreateSocket(target, lport);
					socket.NoDelay = true;
					io.SetInputStream(socket.GetInputStream());
					io.SetOutputStream(socket.GetOutputStream());
				}
				SendOpenConfirmation();
			}
			catch (Exception)
			{
				SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
				close = true;
				Disconnect();
				return;
			}
			thread = Sharpen.Thread.CurrentThread();
			Buffer buf = new Buffer(rmpsize);
			Packet packet = new Packet(buf);
			int i = 0;
			try
			{
				while (thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
					// padding and mac
					if (i <= 0)
					{
						Eof();
						break;
					}
					packet.Reset();
					if (close)
					{
						break;
					}
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					GetSession().Write(packet, this, i);
				}
			}
			catch (Exception)
			{
			}
			//System.err.println(e);
			//thread=null;
			//eof();
			Disconnect();
		}
Пример #13
0
		public override void Run()
		{
			try
			{
				socket = Util.CreateSocket(host, port, TIMEOUT);
				socket.NoDelay = true;
				io = new IO();
				io.SetInputStream(socket.GetInputStream());
				io.SetOutputStream(socket.GetOutputStream());
				SendOpenConfirmation();
			}
			catch (Exception)
			{
				SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
				close = true;
				Disconnect();
				return;
			}
			thread = Sharpen.Thread.CurrentThread();
			Buffer buf = new Buffer(rmpsize);
			Packet packet = new Packet(buf);
			int i = 0;
			try
			{
				while (thread != null && io != null && io.@in != null)
				{
					i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
					// padding and mac
					if (i <= 0)
					{
						Eof();
						break;
					}
					if (close)
					{
						break;
					}
					packet.Reset();
					buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
					buf.PutInt(recipient);
					buf.PutInt(i);
					buf.Skip(i);
					GetSession().Write(packet, this, i);
				}
			}
			catch (Exception)
			{
			}
			//System.err.println(e);
			Disconnect();
		}
Пример #14
0
        public override void Run()
        {
            try
            {
                if (lport == -1)
                {
                    Type c = Sharpen.Runtime.GetType(target);
                    daemon = (ForwardedTCPIPDaemon)System.Activator.CreateInstance(c);
                    PipedOutputStream @out = new PipedOutputStream();
                    io.SetInputStream(new Channel.PassiveInputStream(this, @out, 32 * 1024), false);
                    daemon.SetChannel(this, GetInputStream(), @out);
                    object[] foo = GetPort(GetSession(), rport);
                    daemon.SetArg((object[])foo[3]);
                    new Sharpen.Thread(daemon).Start();
                }
                else
                {
                    socket = (factory == null) ? Util.CreateSocket(target, lport, TIMEOUT) : factory.
                             CreateSocket(target, lport);
                    socket.NoDelay = true;
                    io.SetInputStream(socket.GetInputStream());
                    io.SetOutputStream(socket.GetOutputStream());
                }
                SendOpenConfirmation();
            }
            catch (Exception)
            {
                SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
                close = true;
                Disconnect();
                return;
            }
            thread = Sharpen.Thread.CurrentThread();
            Buffer buf    = new Buffer(rmpsize);
            Packet packet = new Packet(buf);
            int    i      = 0;

            try
            {
                Session _session = GetSession();
                while (thread != null && io != null && io.@in != null)
                {
                    i = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - Session.buffer_margin);
                    if (i <= 0)
                    {
                        Eof();
                        break;
                    }
                    packet.Reset();
                    buf.PutByte(unchecked ((byte)Session.SSH_MSG_CHANNEL_DATA));
                    buf.PutInt(recipient);
                    buf.PutInt(i);
                    buf.Skip(i);
                    lock (this)
                    {
                        if (close)
                        {
                            break;
                        }
                        _session.Write(packet, this, i);
                    }
                }
            }
            catch (Exception)
            {
            }
            //System.err.println(e);
            //thread=null;
            //eof();
            Disconnect();
        }