Beispiel #1
0
		/// <exception cref="System.Exception"></exception>
		internal virtual void Write(Packet packet)
		{
			if (reply)
			{
				channel.reply = -1;
			}
			session.Write(packet);
			if (reply)
			{
				long start = Runtime.CurrentTimeMillis();
				long timeout = channel.connectTimeout;
				while (channel.IsConnected() && channel.reply == -1)
				{
					try
					{
						Sharpen.Thread.Sleep(10);
					}
					catch (Exception)
					{
					}
					if (timeout > 0L && (Runtime.CurrentTimeMillis() - start) > timeout)
					{
						channel.reply = 0;
						throw new JSchException("channel request: timeout");
					}
				}
				if (channel.reply == 0)
				{
					throw new JSchException("failed to send channel request");
				}
			}
		}
Beispiel #2
0
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, 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(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("x11-req"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutByte(unchecked((byte)0));
			buf.PutString(Util.Str2byte("MIT-MAGIC-COOKIE-1"));
			buf.PutString(ChannelX11.GetFakedCookie(session));
			buf.PutInt(0);
			Write(packet);
			session.x11_forwarding = true;
		}
Beispiel #3
0
		/// <exception cref="System.Exception"></exception>
		public virtual bool Start(Session session)
		{
			this.userinfo = session.GetUserInfo();
			this.packet = session.packet;
			this.buf = packet.GetBuffer();
			this.username = session.GetUserName();
			return true;
		}
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("subsystem"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutString(Util.Str2byte(subsystem));
			Write(packet);
		}
Beispiel #5
0
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, 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(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("shell"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			Write(packet);
		}
Beispiel #6
0
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("pty-req"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutString(Util.Str2byte(ttype));
			buf.PutInt(tcol);
			buf.PutInt(trow);
			buf.PutInt(twp);
			buf.PutInt(thp);
			buf.PutString(terminal_mode);
			Write(packet);
		}
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, channel);
			SetReply(false);
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			// byte      SSH_MSG_CHANNEL_REQUEST(98)
			// uint32 recipient channel
			// string request type        // "*****@*****.**"
			// boolean want reply         // 0
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("*****@*****.**"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			Write(packet);
			session.agent_forwarding = true;
		}
		/// <exception cref="System.Exception"></exception>
		internal override void DoRequest(Session session, Channel channel)
		{
			base.DoRequest(session, 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(unchecked((byte)Session.SSH_MSG_CHANNEL_REQUEST));
			buf.PutInt(channel.GetRecipient());
			buf.PutString(Util.Str2byte("window-change"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutInt(width_columns);
			buf.PutInt(height_rows);
			buf.PutInt(width_pixels);
			buf.PutInt(height_pixels);
			Write(packet);
		}
Beispiel #9
0
		/// <exception cref="System.Exception"></exception>
		public virtual void Write(Packet packet)
		{
			// System.err.println("in_kex="+in_kex+" "+(packet.buffer.getCommand()));
			long t = GetTimeout();
			while (in_kex)
			{
				if (t > 0L && (Runtime.CurrentTimeMillis() - kex_start_time) > t)
				{
					throw new JSchException("timeout in wating for rekeying process.");
				}
				byte command = packet.buffer.GetCommand();
				//System.err.println("command: "+command);
				if (command == SSH_MSG_KEXINIT || command == SSH_MSG_NEWKEYS || command == SSH_MSG_KEXDH_INIT
					 || command == SSH_MSG_KEXDH_REPLY || command == SSH_MSG_KEX_DH_GEX_GROUP || command
					 == SSH_MSG_KEX_DH_GEX_INIT || command == SSH_MSG_KEX_DH_GEX_REPLY || command ==
					 SSH_MSG_KEX_DH_GEX_REQUEST || command == SSH_MSG_DISCONNECT)
				{
					break;
				}
				try
				{
					Sharpen.Thread.Sleep(10);
				}
				catch (Exception)
				{
				}
			}
			_write(packet);
		}
Beispiel #10
0
		//System.err.println("updatekeys: "+e); 
		/// <exception cref="System.Exception"></exception>
		internal virtual void Write(Packet packet, Channel c, int length)
		{
			long t = GetTimeout();
			while (true)
			{
				if (in_kex)
				{
					if (t > 0L && (Runtime.CurrentTimeMillis() - kex_start_time) > t)
					{
						throw new JSchException("timeout in wating for rekeying process.");
					}
					try
					{
						Sharpen.Thread.Sleep(10);
					}
					catch (Exception)
					{
					}
					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)
					{
						long len = c.rwsize;
						if (len > length)
						{
							len = length;
						}
						if (len != length)
						{
							s = packet.Shift((int)len, (c2smac != null ? c2smac.GetBlockSize() : 0));
						}
						command = packet.buffer.GetCommand();
						recipient = c.GetRecipient();
						length -= (int)len;
						c.rwsize -= len;
						sendit = true;
					}
				}
				if (sendit)
				{
					_write(packet);
					if (length == 0)
					{
						return;
					}
					packet.Unshift(command, recipient, s, length);
				}
				lock (c)
				{
					if (in_kex)
					{
						continue;
					}
					if (c.rwsize >= length)
					{
						c.rwsize -= length;
						break;
					}
					try
					{
						c.notifyme++;
						Sharpen.Runtime.Wait(c, 100);
					}
					catch (Exception)
					{
					}
					finally
					{
						c.notifyme--;
					}
				}
			}
			_write(packet);
		}
Beispiel #11
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();
		}
Beispiel #12
0
		//System.err.println("Channel.eof");
		//e.printStackTrace();
		internal virtual void Close()
		{
			if (close)
			{
				return;
			}
			close = true;
			eof_local = eof_remote = true;
			try
			{
				Buffer buf = new Buffer(100);
				Packet packet = new Packet(buf);
				packet.Reset();
				buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_CLOSE));
				buf.PutInt(GetRecipient());
				lock (this)
				{
					GetSession().Write(packet);
				}
			}
			catch (Exception)
			{
			}
		}
Beispiel #13
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");
			}
		}
Beispiel #14
0
		/// <exception cref="System.Exception"></exception>
		public virtual void SendIgnore()
		{
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_IGNORE));
			Write(packet);
		}
Beispiel #15
0
		/// <exception cref="NSch.JSchException"></exception>
		private void SetPortForwarding(string bind_address, int rport)
		{
			lock (grr)
			{
				Buffer buf = new Buffer(100);
				// ??
				Packet packet = new Packet(buf);
				string address_to_bind = ChannelForwardedTCPIP.Normalize(bind_address);
				try
				{
					// byte SSH_MSG_GLOBAL_REQUEST 80
					// string "tcpip-forward"
					// boolean want_reply
					// string  address_to_bind
					// uint32  port number to bind
					packet.Reset();
					buf.PutByte(unchecked((byte)SSH_MSG_GLOBAL_REQUEST));
					buf.PutString(Util.Str2byte("tcpip-forward"));
					//      buf.putByte((byte)0);
					buf.PutByte(unchecked((byte)1));
					buf.PutString(Util.Str2byte(address_to_bind));
					buf.PutInt(rport);
					Write(packet);
				}
				catch (Exception e)
				{
					if (e is Exception)
					{
						throw new JSchException(e.ToString(), (Exception)e);
					}
					throw new JSchException(e.ToString());
				}
				grr.SetThread(Sharpen.Thread.CurrentThread());
				try
				{
					Sharpen.Thread.Sleep(10000);
				}
				catch (Exception)
				{
				}
				int reply = grr.GetReply();
				grr.SetThread(null);
				if (reply == 0)
				{
					throw new JSchException("remote port forwarding failed for listen port " + rport);
				}
			}
		}
Beispiel #16
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();
        }
Beispiel #17
0
		public ChannelSftp()
		{
			version = client_version.ToString();
			packet = new Packet(buf);
		}
Beispiel #18
0
		/// <exception cref="NSch.JSchException"></exception>
		public override void Start()
		{
			try
			{
				PipedOutputStream pos = new PipedOutputStream();
				io.SetOutputStream(pos);
				PipedInputStream pis = new Channel.MyPipedInputStream(this, pos, 32 * 1024);
				io.SetInputStream(pis);
				io_in = io.@in;
				if (io_in == null)
				{
					throw new JSchException("channel is down");
				}
				Request request = new RequestSftp();
				request.DoRequest(GetSession(), this);
				buf = new Buffer(rmpsize);
				packet = new Packet(buf);
				int i = 0;
				int length;
				int type;
				byte[] str;
				// send SSH_FXP_INIT
				SendINIT();
				// receive SSH_FXP_VERSION
				ChannelHeader header = new ChannelHeader(this);
				header = Header(buf, header);
				length = header.length;
				if (length > MAX_MSG_LENGTH)
				{
					throw new SftpException(SSH_FX_FAILURE, "Received message is too long: " + length
						);
				}
				type = header.type;
				// 2 -> SSH_FXP_VERSION
				server_version = header.rid;
				//System.err.println("SFTP protocol server-version="+server_version);
				if (length > 0)
				{
					extensions = new Hashtable();
					// extension data
					Fill(buf, length);
					byte[] extension_name = null;
					byte[] extension_data = null;
					while (length > 0)
					{
						extension_name = buf.GetString();
						length -= (4 + extension_name.Length);
						extension_data = buf.GetString();
						length -= (4 + extension_data.Length);
						extensions.Put(Util.Byte2str(extension_name), Util.Byte2str(extension_data));
					}
				}
				lcwd = new FilePath(".").GetCanonicalPath();
			}
			catch (Exception e)
			{
				//System.err.println(e);
				if (e is JSchException)
				{
					throw (JSchException)e;
				}
				if (e is Exception)
				{
					throw new JSchException(e.ToString(), (Exception)e);
				}
				throw new JSchException(e.ToString());
			}
		}
Beispiel #19
0
		protected internal virtual Packet GenChannelOpenPacket()
		{
			Buffer buf = new Buffer(100);
			Packet packet = new Packet(buf);
			// 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(unchecked((byte)90));
			buf.PutString(this.type);
			buf.PutInt(this.id);
			buf.PutInt(this.lwsize);
			buf.PutInt(this.lmpsize);
			return packet;
		}
Beispiel #20
0
		protected internal virtual void SendOpenFailure(int reasoncode)
		{
			try
			{
				Buffer buf = new Buffer(100);
				Packet packet = new Packet(buf);
				packet.Reset();
				buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_FAILURE));
				buf.PutInt(GetRecipient());
				buf.PutInt(reasoncode);
				buf.PutString(Util.Str2byte("open failed"));
				buf.PutString(Util.empty);
				GetSession().Write(packet);
			}
			catch (Exception)
			{
			}
		}
Beispiel #21
0
		/// <exception cref="System.Exception"></exception>
		protected internal virtual void SendOpenConfirmation()
		{
			Buffer buf = new Buffer(100);
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_CONFIRMATION));
			buf.PutInt(GetRecipient());
			buf.PutInt(id);
			buf.PutInt(lwsize);
			buf.PutInt(lmpsize);
			GetSession().Write(packet);
		}
Beispiel #22
0
		/// <exception cref="System.Exception"></exception>
		private void _write(Packet packet)
		{
			lock (Lock)
			{
				Encode(packet);
				if (io != null)
				{
					io.Put(packet);
					seqo++;
				}
			}
		}
Beispiel #23
0
		//  static int min=512;
		//  static int preferred=1024;
		//  static int max=2000;
		//  com.jcraft.jsch.DH dh;
		//private byte[] f;
		/// <exception cref="System.Exception"></exception>
		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;
			try
			{
				Type c = Sharpen.Runtime.GetType(session.GetConfig("sha-1"));
				sha = (HASH)(System.Activator.CreateInstance(c));
				sha.Init();
			}
			catch (Exception e)
			{
				System.Console.Error.WriteLine(e);
			}
			buf = new Buffer();
			packet = new Packet(buf);
			try
			{
				Type c = Sharpen.Runtime.GetType(session.GetConfig("dh"));
				dh = (NSch.DH)(System.Activator.CreateInstance(c));
				dh.Init();
			}
			catch (Exception e)
			{
				//      System.err.println(e);
				throw;
			}
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_KEX_DH_GEX_REQUEST));
			buf.PutInt(min);
			buf.PutInt(preferred);
			buf.PutInt(max);
			session.Write(packet);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEX_DH_GEX_REQUEST(" + min + "<" + preferred
					 + "<" + max + ") sent");
				JSch.GetLogger().Log(Logger.INFO, "expecting SSH_MSG_KEX_DH_GEX_GROUP");
			}
			state = SSH_MSG_KEX_DH_GEX_GROUP;
		}
Beispiel #24
0
		public virtual 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;
			int stimeout = 0;
			try
			{
				while (isConnected && thread != null)
				{
					try
					{
						buf = Read(buf);
						stimeout = 0;
					}
					catch (ThreadInterruptedException ee)
					{
						if (!in_kex && stimeout < serverAliveCountMax)
						{
							SendKeepAliveMsg();
							stimeout++;
							continue;
						}
						throw;
					}
					int msgType = buf.GetCommand() & unchecked((int)(0xff));
					if (kex != null && kex.GetState() == msgType)
					{
						kex_start_time = Runtime.CurrentTimeMillis();
						bool result = kex.Next(buf);
						if (!result)
						{
							throw new JSchException("verify: " + result);
						}
						continue;
					}
					switch (msgType)
					{
						case SSH_MSG_KEXINIT:
						{
							//System.err.println("KEXINIT");
							kex = Receive_kexinit(buf);
							break;
						}

						case SSH_MSG_NEWKEYS:
						{
							//System.err.println("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;
							}
							if (length[0] == 0)
							{
								break;
							}
							try
							{
								channel.Write(foo, start[0], length[0]);
							}
							catch (Exception)
							{
								//System.err.println(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(unchecked((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.err.println("stderr: "+new String(foo,start[0],length[0]));
							if (channel == null)
							{
								break;
							}
							if (length[0] == 0)
							{
								break;
							}
							channel.Write_ext(foo, start[0], length[0]);
							int len = length[0];
							channel.SetLocalWindowSize(channel.lwsize - len);
							if (channel.lwsize < channel.lwsize_max / 2)
							{
								packet.Reset();
								buf.PutByte(unchecked((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();
							}
							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();
							}
							break;
						}

						case SSH_MSG_CHANNEL_OPEN_CONFIRMATION:
						{
							buf.GetInt();
							buf.GetShort();
							i = buf.GetInt();
							channel = Channel.GetChannel(i, this);
							if (channel == null)
							{
							}
							//break;
							int r = buf.GetInt();
							long rws = buf.GetUInt();
							int rps = buf.GetInt();
							channel.SetRemoteWindowSize(rws);
							channel.SetRemotePacketSize(rps);
							channel.SetRecipient(r);
							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 = unchecked((byte)SSH_MSG_CHANNEL_FAILURE);
								if ((Util.Byte2str(foo)).Equals("exit-status"))
								{
									i = buf.GetInt();
									// exit-status
									channel.SetExitStatus(i);
									reply_type = unchecked((byte)SSH_MSG_CHANNEL_SUCCESS);
								}
								if (reply)
								{
									packet.Reset();
									buf.PutByte(reply_type);
									buf.PutInt(channel.GetRecipient());
									Write(packet);
								}
							}
							break;
						}

						case SSH_MSG_CHANNEL_OPEN:
						{
							buf.GetInt();
							buf.GetShort();
							foo = buf.GetString();
							string ctyp = Util.Byte2str(foo);
							if (!"forwarded-tcpip".Equals(ctyp) && !("x11".Equals(ctyp) && x11_forwarding) &&
								 !("*****@*****.**".Equals(ctyp) && agent_forwarding))
							{
								//System.err.println("Session.run: CHANNEL OPEN "+ctyp); 
								//throw new IOException("Session.run: CHANNEL OPEN "+ctyp);
								packet.Reset();
								buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_FAILURE));
								buf.PutInt(buf.GetInt());
								buf.PutInt(Channel.SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
								buf.PutString(Util.empty);
								buf.PutString(Util.empty);
								Write(packet);
							}
							else
							{
								channel = Channel.GetChannel(ctyp);
								AddChannel(channel);
								channel.GetData(buf);
								channel.Init();
								Sharpen.Thread tmp = new Sharpen.Thread(channel);
								tmp.SetName("Channel " + ctyp + " " + host);
								if (daemon_thread)
								{
									tmp.SetDaemon(daemon_thread);
								}
								tmp.Start();
								break;
							}
							goto case SSH_MSG_CHANNEL_SUCCESS;
						}

						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
							bool reply = (buf.GetByte() != 0);
							if (reply)
							{
								packet.Reset();
								buf.PutByte(unchecked((byte)SSH_MSG_REQUEST_FAILURE));
								Write(packet);
							}
							break;
						}

						case SSH_MSG_REQUEST_FAILURE:
						case SSH_MSG_REQUEST_SUCCESS:
						{
							Sharpen.Thread t = grr.GetThread();
							if (t != null)
							{
								grr.SetReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0);
								t.Interrupt();
							}
							break;
						}

						default:
						{
							//System.err.println("Session.run: unsupported type "+msgType); 
							throw new IOException("Unknown SSH message type " + msgType);
						}
					}
				}
			}
			catch (Exception e)
			{
				in_kex = false;
				if (JSch.GetLogger().IsEnabled(Logger.INFO))
				{
					JSch.GetLogger().Log(Logger.INFO, "Caught an exception, leaving main loop due to "
						 + e.Message);
				}
			}
			//System.err.println("# Session.run");
			//e.printStackTrace();
			try
			{
				Disconnect();
			}
			catch (ArgumentNullException)
			{
			}
			catch (Exception)
			{
			}
			//System.err.println("@1");
			//e.printStackTrace();
			//System.err.println("@2");
			//e.printStackTrace();
			isConnected = false;
		}
		/// <exception cref="NSch.JSchException"></exception>
		public override void Connect()
		{
			try
			{
				Session _session = GetSession();
				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(unchecked((byte)90));
				buf.PutString(Util.Str2byte("direct-tcpip"));
				buf.PutInt(id);
				buf.PutInt(lwsize);
				buf.PutInt(lmpsize);
				buf.PutString(Util.Str2byte(host));
				buf.PutInt(port);
				buf.PutString(Util.Str2byte(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);
						Sharpen.Thread.Sleep(50);
						retry--;
					}
				}
				catch (Exception)
				{
				}
				if (!_session.IsConnected())
				{
					throw new JSchException("session is down");
				}
				if (retry == 0 || this.eof_remote)
				{
					throw new JSchException("channel is not opened.");
				}
				connected = true;
				if (io.@in != null)
				{
					thread = new Sharpen.Thread(this);
					thread.SetName("DirectTCPIP thread " + _session.GetHost());
					if (_session.daemon_thread)
					{
						thread.SetDaemon(_session.daemon_thread);
					}
					thread.Start();
				}
			}
			catch (Exception e)
			{
				io.Close();
				io = null;
				Channel.Del(this);
				if (e is JSchException)
				{
					throw (JSchException)e;
				}
			}
		}
Beispiel #26
0
		/// <exception cref="NSch.JSchException"></exception>
		internal Session(JSch jsch) : base()
		{
			grr = new Session.GlobalRequestReply(this);
			// http://ietf.org/internet-drafts/draft-ietf-secsh-assignednumbers-01.txt
			// server version
			// client version
			// the payload of the client's SSH_MSG_KEXINIT
			// the payload of the server's SSH_MSG_KEXINIT
			// the host key
			//private byte[] mac_buf;
			this.jsch = jsch;
			buf = new Buffer();
			packet = new Packet(buf);
		}
		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();
		}
Beispiel #28
0
		/// <exception cref="System.Exception"></exception>
		public virtual void SendKeepAliveMsg()
		{
			Buffer buf = new Buffer();
			Packet packet = new Packet(buf);
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_GLOBAL_REQUEST));
			buf.PutString(keepalivemsg);
			buf.PutByte(unchecked((byte)1));
			Write(packet);
		}
Beispiel #29
0
		protected internal override Packet GenChannelOpenPacket()
		{
			Buffer buf = new Buffer(150);
			Packet packet = new Packet(buf);
			// 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(unchecked((byte)90));
			buf.PutString(this.type);
			buf.PutInt(id);
			buf.PutInt(lwsize);
			buf.PutInt(lmpsize);
			buf.PutString(Util.Str2byte(host));
			buf.PutInt(port);
			buf.PutString(Util.Str2byte(originator_IP_address));
			buf.PutInt(originator_port);
			return packet;
		}
Beispiel #30
0
		// encode will bin invoked in write with synchronization.
		/// <exception cref="System.Exception"></exception>
		public virtual void Encode(Packet packet)
		{
			//System.err.println("encode: "+packet.buffer.getCommand());
			//System.err.println("        "+packet.buffer.index);
			//if(packet.buffer.getCommand()==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());
				packet.Padding(c2scipher_size);
				int pad = packet.buffer.buffer[4];
				lock (random)
				{
					random.Fill(packet.buffer.buffer, packet.buffer.index - pad, pad);
				}
			}
			else
			{
				packet.Padding(8);
			}
			if (c2smac != null)
			{
				c2smac.Update(seqo);
				c2smac.Update(packet.buffer.buffer, 0, packet.buffer.index);
				c2smac.DoFinal(packet.buffer.buffer, packet.buffer.index);
			}
			if (c2scipher != null)
			{
				byte[] buf = packet.buffer.buffer;
				c2scipher.Update(buf, 0, packet.buffer.index, buf, 0);
			}
			if (c2smac != null)
			{
				packet.buffer.Skip(c2smac.GetBlockSize());
			}
		}
Beispiel #31
0
			/// <exception cref="System.IO.IOException"></exception>
			private void Init()
			{
				lock (this)
				{
					this.buffer = new Buffer(this._enclosing.rmpsize);
					this.packet = new Packet(this.buffer);
					byte[] _buf = this.buffer.buffer;
					if (_buf.Length - (14 + 0) - Session.buffer_margin <= 0)
					{
						this.buffer = null;
						this.packet = null;
						throw new IOException("failed to initialize the channel.");
					}
				}
			}