public ChannelForwardedTCPIP() : base()
		{
			//static private final int LOCAL_WINDOW_SIZE_MAX=0x100000;
			SetLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
			SetLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
			SetLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
			io = new IO();
			connected = true;
		}
예제 #2
0
		internal override void Init()
		{
			try
			{
				io = new IO();
			}
			catch (Exception e)
			{
				System.Console.Error.WriteLine(e);
			}
		}
예제 #3
0
		/// <exception cref="NSch.JSchException"></exception>
		public virtual void Connect(int connectTimeout)
		{
			if (isConnected)
			{
				throw new JSchException("session is already connected");
			}
			io = new IO();
			if (random == null)
			{
				try
				{
					Type c = Sharpen.Runtime.GetType(GetConfig("random"));
					random = (Random)(System.Activator.CreateInstance(c));
				}
				catch (Exception e)
				{
					throw new JSchException(e.ToString(), e);
				}
			}
			Packet.SetRandom(random);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "Connecting to " + host + " port " + port);
			}
			try
			{
				int i;
				int j;
				if (proxy == null)
				{
					InputStream @in;
					OutputStream @out;
					if (socket_factory == null)
					{
						socket = Util.CreateSocket(host, port, connectTimeout);
						@in = socket.GetInputStream();
						@out = socket.GetOutputStream();
					}
					else
					{
						socket = socket_factory.CreateSocket(host, port);
						@in = socket_factory.GetInputStream(socket);
						@out = socket_factory.GetOutputStream(socket);
					}
					//if(timeout>0){ socket.setSoTimeout(timeout); }
					socket.NoDelay = true;
					io.SetInputStream(@in);
					io.SetOutputStream(@out);
				}
				else
				{
					lock (proxy)
					{
						proxy.Connect(socket_factory, host, port, connectTimeout);
						io.SetInputStream(proxy.GetInputStream());
						io.SetOutputStream(proxy.GetOutputStream());
						socket = proxy.GetSocket();
					}
				}
				if (connectTimeout > 0 && socket != null)
				{
					socket.ReceiveTimeout = connectTimeout;
				}
				isConnected = true;
				if (JSch.GetLogger().IsEnabled(Logger.INFO))
				{
					JSch.GetLogger().Log(Logger.INFO, "Connection established");
				}
				jsch.AddSession(this);
				{
					// Some Cisco devices will miss to read '\n' if it is sent separately.
					byte[] foo = new byte[V_C.Length + 1];
					System.Array.Copy(V_C, 0, foo, 0, V_C.Length);
					foo[foo.Length - 1] = unchecked((byte)(byte)('\n'));
					io.Put(foo, 0, foo.Length);
				}
				while (true)
				{
					i = 0;
					j = 0;
					while (i < buf.buffer.Length)
					{
						j = io.GetByte();
						if (j < 0)
						{
							break;
						}
						buf.buffer[i] = unchecked((byte)j);
						i++;
						if (j == 10)
						{
							break;
						}
					}
					if (j < 0)
					{
						throw new JSchException("connection is closed by foreign host");
					}
					if (buf.buffer[i - 1] == 10)
					{
						// 0x0a
						i--;
						if (i > 0 && buf.buffer[i - 1] == 13)
						{
							// 0x0d
							i--;
						}
					}
					if (i <= 3 || ((i != buf.buffer.Length) && (buf.buffer[0] != 'S' || buf.buffer[1]
						 != 'S' || buf.buffer[2] != 'H' || buf.buffer[3] != '-')))
					{
						// It must not start with 'SSH-'
						//System.err.println(new String(buf.buffer, 0, i);
						continue;
					}
					if (i == buf.buffer.Length || i < 7 || (buf.buffer[4] == '1' && buf.buffer[6] != 
						'9'))
					{
						// SSH-1.99 or SSH-2.0
						// SSH-1.5
						throw new JSchException("invalid server's version string");
					}
					break;
				}
				V_S = new byte[i];
				System.Array.Copy(buf.buffer, 0, V_S, 0, i);
				//System.err.println("V_S: ("+i+") ["+new String(V_S)+"]");
				if (JSch.GetLogger().IsEnabled(Logger.INFO))
				{
					JSch.GetLogger().Log(Logger.INFO, "Remote version string: " + Util.Byte2str(V_S));
					JSch.GetLogger().Log(Logger.INFO, "Local version string: " + Util.Byte2str(V_C));
				}
				Send_kexinit();
				buf = Read(buf);
				if (buf.GetCommand() != SSH_MSG_KEXINIT)
				{
					in_kex = false;
					throw new JSchException("invalid protocol: " + buf.GetCommand());
				}
				if (JSch.GetLogger().IsEnabled(Logger.INFO))
				{
					JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEXINIT received");
				}
				KeyExchange kex = Receive_kexinit(buf);
				while (true)
				{
					buf = Read(buf);
					if (kex.GetState() == buf.GetCommand())
					{
						kex_start_time = Runtime.CurrentTimeMillis();
						bool result = kex.Next(buf);
						if (!result)
						{
							//System.err.println("verify: "+result);
							in_kex = false;
							throw new JSchException("verify: " + result);
						}
					}
					else
					{
						in_kex = false;
						throw new JSchException("invalid protocol(kex): " + buf.GetCommand());
					}
					if (kex.GetState() == KeyExchange.STATE_END)
					{
						break;
					}
				}
				try
				{
					CheckHost(host, port, kex);
				}
				catch (JSchException ee)
				{
					in_kex = false;
					throw;
				}
				Send_newkeys();
				// receive SSH_MSG_NEWKEYS(21)
				buf = Read(buf);
				//System.err.println("read: 21 ? "+buf.getCommand());
				if (buf.GetCommand() == SSH_MSG_NEWKEYS)
				{
					if (JSch.GetLogger().IsEnabled(Logger.INFO))
					{
						JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_NEWKEYS received");
					}
					Receive_newkeys(buf, kex);
				}
				else
				{
					in_kex = false;
					throw new JSchException("invalid protocol(newkyes): " + buf.GetCommand());
				}
				bool auth = false;
				bool auth_cancel = false;
				UserAuth ua = null;
				try
				{
					Type c = Sharpen.Runtime.GetType(GetConfig("userauth.none"));
					ua = (UserAuth)(System.Activator.CreateInstance(c));
				}
				catch (Exception e)
				{
					throw new JSchException(e.ToString(), e);
				}
				auth = ua.Start(this);
				string cmethods = GetConfig("PreferredAuthentications");
				string[] cmethoda = Util.Split(cmethods, ",");
				string smethods = null;
				if (!auth)
				{
					smethods = ((UserAuthNone)ua).GetMethods();
					if (smethods != null)
					{
						smethods = smethods.ToLower();
					}
					else
					{
						// methods: publickey,password,keyboard-interactive
						//smethods="publickey,password,keyboard-interactive";
						smethods = cmethods;
					}
				}
				string[] smethoda = Util.Split(smethods, ",");
				int methodi = 0;
				while (true)
				{
					//System.err.println("methods: "+methods);
					while (!auth && cmethoda != null && methodi < cmethoda.Length)
					{
						string method = cmethoda[methodi++];
						bool acceptable = false;
						for (int k = 0; k < smethoda.Length; k++)
						{
							if (smethoda[k].Equals(method))
							{
								acceptable = true;
								break;
							}
						}
						if (!acceptable)
						{
							continue;
						}
						//System.err.println("  method: "+method);
						if (JSch.GetLogger().IsEnabled(Logger.INFO))
						{
							string str = "Authentications that can continue: ";
							for (int k_1 = methodi - 1; k_1 < cmethoda.Length; k_1++)
							{
								str += cmethoda[k_1];
								if (k_1 + 1 < cmethoda.Length)
								{
									str += ",";
								}
							}
							JSch.GetLogger().Log(Logger.INFO, str);
							JSch.GetLogger().Log(Logger.INFO, "Next authentication method: " + method);
						}
						ua = null;
						try
						{
							Type c = null;
							if (GetConfig("userauth." + method) != null)
							{
								c = Sharpen.Runtime.GetType(GetConfig("userauth." + method));
								ua = (UserAuth)(System.Activator.CreateInstance(c));
							}
						}
						catch (Exception)
						{
							if (JSch.GetLogger().IsEnabled(Logger.WARN))
							{
								JSch.GetLogger().Log(Logger.WARN, "failed to load " + method + " method");
							}
						}
						if (ua != null)
						{
							auth_cancel = false;
							try
							{
								auth = ua.Start(this);
								if (auth && JSch.GetLogger().IsEnabled(Logger.INFO))
								{
									JSch.GetLogger().Log(Logger.INFO, "Authentication succeeded (" + method + ").");
								}
							}
							catch (JSchAuthCancelException)
							{
								auth_cancel = true;
							}
							catch (JSchPartialAuthException ee)
							{
								string tmp = smethods;
								smethods = ee.GetMethods();
								smethoda = Util.Split(smethods, ",");
								if (!tmp.Equals(smethods))
								{
									methodi = 0;
								}
								//System.err.println("PartialAuth: "+methods);
								auth_cancel = false;
								goto loop_continue;
							}
							catch (RuntimeException ee)
							{
								throw;
							}
							catch (Exception)
							{
								//System.err.println("ee: "+ee); // SSH_MSG_DISCONNECT: 2 Too many authentication failures
								goto loop_break;
							}
						}
					}
					break;
loop_continue: ;
				}
loop_break: ;
				if (!auth)
				{
					if (auth_cancel)
					{
						throw new JSchException("Auth cancel");
					}
					throw new JSchException("Auth fail");
				}
				if (connectTimeout > 0 || timeout > 0)
				{
					socket.ReceiveTimeout = timeout;
				}
				isAuthed = true;
				lock (Lock)
				{
					if (isConnected)
					{
						connectThread = new Sharpen.Thread(this);
						connectThread.SetName("Connect thread " + host + " session");
						if (daemon_thread)
						{
							connectThread.SetDaemon(daemon_thread);
						}
						connectThread.Start();
					}
				}
			}
			catch (Exception e)
			{
				// The session has been already down and
				// we don't have to start new thread.
				in_kex = false;
				if (isConnected)
				{
					try
					{
						packet.Reset();
						buf.PutByte(unchecked((byte)SSH_MSG_DISCONNECT));
						buf.PutInt(3);
						buf.PutString(Util.Str2byte(e.ToString()));
						buf.PutString(Util.Str2byte("en"));
						Write(packet);
						Disconnect();
					}
					catch (Exception)
					{
					}
				}
				isConnected = false;
				//e.printStackTrace();
				if (e is RuntimeException)
				{
					throw (RuntimeException)e;
				}
				if (e is JSchException)
				{
					throw (JSchException)e;
				}
				throw new JSchException("Session.connect: " + e);
			}
			finally
			{
				Util.Bzero(this.password);
				this.password = null;
			}
		}
예제 #4
0
		public virtual void Disconnect()
		{
			if (!isConnected)
			{
				return;
			}
			//System.err.println(this+": disconnect");
			//Thread.dumpStack();
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "Disconnecting from " + host + " port " + port);
			}
			Channel.Disconnect(this);
			isConnected = false;
			PortWatcher.DelPort(this);
			ChannelForwardedTCPIP.DelPort(this);
			lock (Lock)
			{
				if (connectThread != null)
				{
					Sharpen.Thread.Yield();
					connectThread.Interrupt();
					connectThread = null;
				}
			}
			thread = null;
			try
			{
				if (io != null)
				{
					if (io.@in != null)
					{
						[email protected]();
					}
					if (io.@out != null)
					{
						[email protected]();
					}
					if (io.out_ext != null)
					{
						io.out_ext.Close();
					}
				}
				if (proxy == null)
				{
					if (socket != null)
					{
						socket.Close();
					}
				}
				else
				{
					lock (proxy)
					{
						proxy.Close();
					}
					proxy = null;
				}
			}
			catch (Exception)
			{
			}
			//      e.printStackTrace();
			io = null;
			socket = null;
			//    synchronized(jsch.pool){
			//      jsch.pool.removeElement(this);
			//    }
			jsch.RemoveSession(this);
		}
예제 #5
0
		internal override void Init()
		{
			io = new IO();
		}
예제 #6
0
		public ChannelSession() : base()
		{
			type = _session;
			io = new IO();
		}
예제 #7
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();
		}