Exemplo n.º 1
0
		/// <exception cref="System.Exception"></exception>
		public virtual void Request(Session session, Channel channel, string subsystem, bool
			 want_reply)
		{
			SetReply(want_reply);
			this.subsystem = subsystem;
			this.DoRequest(session, channel);
		}
Exemplo n.º 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;
		}
Exemplo n.º 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;
		}
Exemplo n.º 4
0
		/// <exception cref="System.Exception"></exception>
		internal virtual void DoRequest(Session session, Channel channel)
		{
			this.session = session;
			this.channel = channel;
			if (channel.connectTimeout > 0)
			{
				SetReply(true);
			}
		}
		private static URIish CreateURI(Session session)
		{
			URIish uri = new URIish();
			uri = uri.SetScheme("ssh");
			uri = uri.SetUser(session.GetUserName());
			uri = uri.SetHost(session.GetHost());
			uri = uri.SetPort(session.GetPort());
			return uri;
		}
        protected override void Configure(OpenSshConfig.Host hc, Session session)
        {
            var config = new Properties();
            config["StrictHostKeyChecking"] = "no";
            config["PreferredAuthentications"] = "publickey";
            session.SetConfig(config);

            var jsch = this.GetJSch(hc, FS.DETECTED);
            jsch.AddIdentity("KeyPair", Encoding.UTF8.GetBytes(PrivateKey), Encoding.UTF8.GetBytes(PublicKey), null);
        }
Exemplo n.º 7
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("subsystem"));
			buf.PutByte(unchecked((byte)(WaitForReply() ? 1 : 0)));
			buf.PutString(Util.Str2byte(subsystem));
			Write(packet);
		}
Exemplo n.º 8
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);
		}
Exemplo n.º 9
0
		/// <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;
		}
Exemplo n.º 10
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);
		}
Exemplo n.º 11
0
		internal static string[] GetPortForwarding(Session session)
		{
			ArrayList foo = new ArrayList();
			lock (pool)
			{
				for (int i = 0; i < pool.Count; i++)
				{
					NSch.PortWatcher p = (NSch.PortWatcher)(pool[i]);
					if (p.session == session)
					{
						foo.Add(p.lport + ":" + p.host + ":" + p.rport);
					}
				}
			}
			string[] bar = new string[foo.Count];
			for (int i_1 = 0; i_1 < foo.Count; i_1++)
			{
				bar[i_1] = (string)(foo[i_1]);
			}
			return bar;
		}
Exemplo n.º 12
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
			//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);
		}
Exemplo n.º 13
0
			internal GlobalRequestReply(Session _enclosing)
			{
				this._enclosing = _enclosing;
			}
Exemplo n.º 14
0
		/// <exception cref="NSch.JSchException"></exception>
		internal static void DelPort(Session session, string address, int lport)
		{
			NSch.PortWatcher pw = GetPort(session, address, lport);
			if (pw == null)
			{
				throw new JSchException("PortForwardingL: local port " + address + ":" + lport + 
					" is not registered.");
			}
			pw.Delete();
			pool.RemoveElement(pw);
		}
Exemplo n.º 15
0
		/// <exception cref="System.Exception"></exception>
		public override bool Start(Session session)
		{
			base.Start(session);
			byte[] password = session.password;
			string dest = username + "@" + session.host;
			if (session.port != 22)
			{
				dest += (":" + session.port);
			}
			try
			{
				while (true)
				{
					if (session.auth_failures >= session.max_auth_tries)
					{
						return false;
					}
					if (password == null)
					{
						if (userinfo == null)
						{
							//throw new JSchException("USERAUTH fail");
							return false;
						}
						if (!userinfo.PromptPassword("Password for " + dest))
						{
							throw new JSchAuthCancelException("password");
						}
						//break;
						string _password = userinfo.GetPassword();
						if (_password == null)
						{
							throw new JSchAuthCancelException("password");
						}
						//break;
						password = Util.Str2byte(_password);
					}
					byte[] _username = null;
					_username = Util.Str2byte(username);
					// send
					// byte      SSH_MSG_USERAUTH_REQUEST(50)
					// string    user name
					// string    service name ("ssh-connection")
					// string    "password"
					// boolen    FALSE
					// string    plaintext password (ISO-10646 UTF-8)
					packet.Reset();
					buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
					buf.PutString(_username);
					buf.PutString(Util.Str2byte("ssh-connection"));
					buf.PutString(Util.Str2byte("password"));
					buf.PutByte(unchecked((byte)0));
					buf.PutString(password);
					session.Write(packet);
					while (true)
					{
						buf = session.Read(buf);
						int command = buf.GetCommand() & unchecked((int)(0xff));
						if (command == SSH_MSG_USERAUTH_SUCCESS)
						{
							return true;
						}
						if (command == SSH_MSG_USERAUTH_BANNER)
						{
							buf.GetInt();
							buf.GetByte();
							buf.GetByte();
							byte[] _message = buf.GetString();
							byte[] lang = buf.GetString();
							string message = Util.Byte2str(_message);
							if (userinfo != null)
							{
								userinfo.ShowMessage(message);
							}
							goto loop_continue;
						}
						if (command == SSH_MSG_USERAUTH_PASSWD_CHANGEREQ)
						{
							buf.GetInt();
							buf.GetByte();
							buf.GetByte();
							byte[] instruction = buf.GetString();
							byte[] tag = buf.GetString();
							if (userinfo == null || !(userinfo is UIKeyboardInteractive))
							{
								if (userinfo != null)
								{
									userinfo.ShowMessage("Password must be changed.");
								}
								return false;
							}
							UIKeyboardInteractive kbi = (UIKeyboardInteractive)userinfo;
							string[] response;
							string name = "Password Change Required";
							string[] prompt = new string[] { "New Password: "******"password");
							}
							byte[] newpassword = Util.Str2byte(response[0]);
							// send
							// byte      SSH_MSG_USERAUTH_REQUEST(50)
							// string    user name
							// string    service name ("ssh-connection")
							// string    "password"
							// boolen    TRUE
							// string    plaintext old password (ISO-10646 UTF-8)
							// string    plaintext new password (ISO-10646 UTF-8)
							packet.Reset();
							buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
							buf.PutString(_username);
							buf.PutString(Util.Str2byte("ssh-connection"));
							buf.PutString(Util.Str2byte("password"));
							buf.PutByte(unchecked((byte)1));
							buf.PutString(password);
							buf.PutString(newpassword);
							Util.Bzero(newpassword);
							response = null;
							session.Write(packet);
							goto loop_continue;
						}
						if (command == SSH_MSG_USERAUTH_FAILURE)
						{
							buf.GetInt();
							buf.GetByte();
							buf.GetByte();
							byte[] foo = buf.GetString();
							int partial_success = buf.GetByte();
							//System.err.println(new String(foo)+
							//		 " partial_success:"+(partial_success!=0));
							if (partial_success != 0)
							{
								throw new JSchPartialAuthException(Util.Byte2str(foo));
							}
							session.auth_failures++;
							break;
						}
						else
						{
							//System.err.println("USERAUTH fail ("+buf.getCommand()+")");
							//	  throw new JSchException("USERAUTH fail ("+buf.getCommand()+")");
							return false;
						}
loop_continue: ;
					}
loop_break: ;
					if (password != null)
					{
						Util.Bzero(password);
						password = null;
					}
				}
			}
			finally
			{
				if (password != null)
				{
					Util.Bzero(password);
					password = null;
				}
			}
		}
Exemplo n.º 16
0
		internal static void DelPort(Session session)
		{
			lock (pool)
			{
				NSch.PortWatcher[] foo = new NSch.PortWatcher[pool.Count];
				int count = 0;
				for (int i = 0; i < pool.Count; i++)
				{
					NSch.PortWatcher p = (NSch.PortWatcher)(pool[i]);
					if (p.session == session)
					{
						p.Delete();
						foo[count++] = p;
					}
				}
				for (int i_1 = 0; i_1 < count; i_1++)
				{
					NSch.PortWatcher p = foo[i_1];
					pool.RemoveElement(p);
				}
			}
		}
		/// <exception cref="System.Exception"></exception>
		public override bool Start(Session session)
		{
			base.Start(session);
			if (userinfo != null && !(userinfo is UIKeyboardInteractive))
			{
				return false;
			}
			string dest = username + "@" + session.host;
			if (session.port != 22)
			{
				dest += (":" + session.port);
			}
			byte[] password = session.password;
			bool cancel = false;
			byte[] _username = null;
			_username = Util.Str2byte(username);
			while (true)
			{
				// send
				// byte      SSH_MSG_USERAUTH_REQUEST(50)
				// string    user name (ISO-10646 UTF-8, as defined in [RFC-2279])
				// string    service name (US-ASCII) "ssh-userauth" ? "ssh-connection"
				// string    "keyboard-interactive" (US-ASCII)
				// string    language tag (as defined in [RFC-3066])
				// string    submethods (ISO-10646 UTF-8)
				packet.Reset();
				buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
				buf.PutString(_username);
				buf.PutString(Util.Str2byte("ssh-connection"));
				//buf.putString("ssh-userauth".getBytes());
				buf.PutString(Util.Str2byte("keyboard-interactive"));
				buf.PutString(Util.empty);
				buf.PutString(Util.empty);
				session.Write(packet);
				bool firsttime = true;
				while (true)
				{
					buf = session.Read(buf);
					int command = buf.GetCommand() & unchecked((int)(0xff));
					if (command == SSH_MSG_USERAUTH_SUCCESS)
					{
						return true;
					}
					if (command == SSH_MSG_USERAUTH_BANNER)
					{
						buf.GetInt();
						buf.GetByte();
						buf.GetByte();
						byte[] _message = buf.GetString();
						byte[] lang = buf.GetString();
						string message = Util.Byte2str(_message);
						if (userinfo != null)
						{
							userinfo.ShowMessage(message);
						}
						goto loop_continue;
					}
					if (command == SSH_MSG_USERAUTH_FAILURE)
					{
						buf.GetInt();
						buf.GetByte();
						buf.GetByte();
						byte[] foo = buf.GetString();
						int partial_success = buf.GetByte();
						//	  System.err.println(new String(foo)+
						//			     " partial_success:"+(partial_success!=0));
						if (partial_success != 0)
						{
							throw new JSchPartialAuthException(Util.Byte2str(foo));
						}
						if (firsttime)
						{
							return false;
						}
						//throw new JSchException("USERAUTH KI is not supported");
						//cancel=true;  // ??
						break;
					}
					if (command == SSH_MSG_USERAUTH_INFO_REQUEST)
					{
						firsttime = false;
						buf.GetInt();
						buf.GetByte();
						buf.GetByte();
						string name = Util.Byte2str(buf.GetString());
						string instruction = Util.Byte2str(buf.GetString());
						string languate_tag = Util.Byte2str(buf.GetString());
						int num = buf.GetInt();
						string[] prompt = new string[num];
						bool[] echo = new bool[num];
						for (int i = 0; i < num; i++)
						{
							prompt[i] = Util.Byte2str(buf.GetString());
							echo[i] = (buf.GetByte() != 0);
						}
						byte[][] response = null;
						if (password != null && prompt.Length == 1 && !echo[0] && prompt[0].ToLower().StartsWith
							("password:"******"USERAUTH fail ("+command+")");
					return false;
loop_continue: ;
				}
loop_break: ;
				if (cancel)
				{
					throw new JSchAuthCancelException("keyboard-interactive");
				}
			}
		}
Exemplo n.º 18
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;
		}
Exemplo n.º 19
0
		/// <exception cref="NSch.JSchException"></exception>
		internal PortWatcher(Session session, string address, int lport, string host, int
			 rport, ServerSocketFactory factory)
		{
			this.session = session;
			this.lport = lport;
			this.host = host;
			this.rport = rport;
			try
			{
				boundaddress = Sharpen.Extensions.GetAddressByName(address);
				ss = (factory == null) ? Sharpen.Extensions.CreateServerSocket(lport, 0, boundaddress
					) : factory.CreateServerSocket(lport, 0, boundaddress);
			}
			catch (Exception e)
			{
				//System.err.println(e);
				string message = "PortForwardingL: local port " + address + ":" + lport + " cannot be bound.";
				if (e is Exception)
				{
					throw new JSchException(message, (Exception)e);
				}
				throw new JSchException(message);
			}
			if (lport == 0)
			{
				int assigned = ss.GetLocalPort();
				if (assigned != -1)
				{
					this.lport = assigned;
				}
			}
		}
Exemplo n.º 20
0
		internal static bool CheckKex(Session s, string kex)
		{
			try
			{
				Type c = Sharpen.Runtime.GetType(kex);
				KeyExchange _c = (KeyExchange)(System.Activator.CreateInstance(c));
				_c.Init(s, null, null, null, null);
				return true;
			}
			catch (Exception)
			{
				return false;
			}
		}
Exemplo n.º 21
0
		/// <exception cref="NSch.JSchException"></exception>
		internal static NSch.PortWatcher GetPort(Session session, string address, int lport
			)
		{
			IPAddress addr;
			try
			{
				addr = Sharpen.Extensions.GetAddressByName(address);
			}
			catch (UnknownHostException uhe)
			{
				throw new JSchException("PortForwardingL: invalid address " + address + " specified."
					, uhe);
			}
			lock (pool)
			{
				for (int i = 0; i < pool.Count; i++)
				{
					NSch.PortWatcher p = (NSch.PortWatcher)(pool[i]);
					if (p.session == session && p.lport == lport)
					{
						if ((anyLocalAddress != null && p.boundaddress.Equals(anyLocalAddress)) || p.boundaddress
							.Equals(addr))
						{
							return p;
						}
					}
				}
				return null;
			}
		}
Exemplo n.º 22
0
		internal virtual void SetSession(Session session)
		{
			this.session = session;
		}
Exemplo n.º 23
0
		/// <exception cref="System.Exception"></exception>
		public override void Init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte
			[] I_C)
		{
			throw new NotSupportedException (); // The crypto for this method is unusably slow
			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 ex)
			{
				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 ex)
			{
				//System.err.println(e);
				throw;
			}
			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(unchecked((byte)SSH_MSG_KEXDH_INIT));
			buf.PutMPInt(e);
			if (V_S == null)
			{
				// This is a really ugly hack for Session.checkKexes ;-(
				return;
			}
			session.Write(packet);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_KEXDH_INIT sent");
				JSch.GetLogger().Log(Logger.INFO, "expecting SSH_MSG_KEXDH_REPLY");
			}
			state = SSH_MSG_KEXDH_REPLY;
		}
Exemplo n.º 24
0
		internal static void Disconnect(Session session)
		{
			Channel[] channels = null;
			int count = 0;
			lock (pool)
			{
				channels = new Channel[pool.Count];
				for (int i = 0; i < pool.Count; i++)
				{
					try
					{
						Channel c = ((Channel)(pool[i]));
						if (c.session == session)
						{
							channels[count++] = c;
						}
					}
					catch (Exception)
					{
					}
				}
			}
			for (int i_1 = 0; i_1 < count; i_1++)
			{
				channels[i_1].Disconnect();
			}
		}
Exemplo n.º 25
0
        /// <summary>
        /// Provide additional configuration for the session based on the host
        /// information.
        /// </summary>
        /// <remarks>
        /// Provide additional configuration for the session based on the host
        /// information. This method could be used to supply
        /// <see cref="NSch.UserInfo">NSch.UserInfo</see>
        /// .
        /// </remarks>
        /// <param name="hc">host configuration</param>
        /// <param name="session">session to configure</param>
        protected internal abstract void Configure(OpenSshConfig.Host hc, Session session
			);
Exemplo n.º 26
0
        protected internal override void Configure(OpenSshConfig.Host hc, Session session
			)
        {
        }
Exemplo n.º 27
0
		// OID 1.2.840.113554.1.2.2 in DER
		/// <exception cref="System.Exception"></exception>
		public override bool Start(Session session)
		{
			base.Start(session);
			byte[] _username = Util.Str2byte(username);
			packet.Reset();
			// byte            SSH_MSG_USERAUTH_REQUEST(50)
			// string          user name(in ISO-10646 UTF-8 encoding)
			// string          service name(in US-ASCII)
			// string          "gssapi"(US-ASCII)
			// uint32          n, the number of OIDs client supports
			// string[n]       mechanism OIDS
			buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
			buf.PutString(_username);
			buf.PutString(Util.Str2byte("ssh-connection"));
			buf.PutString(Util.Str2byte("gssapi-with-mic"));
			buf.PutInt(supported_oid.Length);
			for (int i = 0; i < supported_oid.Length; i++)
			{
				buf.PutString(supported_oid[i]);
			}
			session.Write(packet);
			string method = null;
			int command;
			while (true)
			{
				buf = session.Read(buf);
				command = buf.GetCommand() & unchecked((int)(0xff));
				if (command == SSH_MSG_USERAUTH_FAILURE)
				{
					return false;
				}
				if (command == SSH_MSG_USERAUTH_GSSAPI_RESPONSE)
				{
					buf.GetInt();
					buf.GetByte();
					buf.GetByte();
					byte[] message = buf.GetString();
					for (int i_1 = 0; i_1 < supported_oid.Length; i_1++)
					{
						if (Util.Array_equals(message, supported_oid[i_1]))
						{
							method = supported_method[i_1];
							break;
						}
					}
					if (method == null)
					{
						return false;
					}
					break;
				}
				// success
				if (command == SSH_MSG_USERAUTH_BANNER)
				{
					buf.GetInt();
					buf.GetByte();
					buf.GetByte();
					byte[] _message = buf.GetString();
					byte[] lang = buf.GetString();
					string message = Util.Byte2str(_message);
					if (userinfo != null)
					{
						userinfo.ShowMessage(message);
					}
					continue;
				}
				return false;
			}
			NSch.GSSContext context = null;
			try
			{
				Type c = Sharpen.Runtime.GetType(session.GetConfig(method));
				context = (NSch.GSSContext)(System.Activator.CreateInstance(c));
			}
			catch (Exception)
			{
				return false;
			}
			try
			{
				context.Create(username, session.host);
			}
			catch (JSchException)
			{
				return false;
			}
			byte[] token = new byte[0];
			while (!context.IsEstablished())
			{
				try
				{
					token = context.Init(token, 0, token.Length);
				}
				catch (JSchException)
				{
					// TODO
					// ERRTOK should be sent?
					// byte        SSH_MSG_USERAUTH_GSSAPI_ERRTOK
					// string      error token
					return false;
				}
				if (token != null)
				{
					packet.Reset();
					buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_GSSAPI_TOKEN));
					buf.PutString(token);
					session.Write(packet);
				}
				if (!context.IsEstablished())
				{
					buf = session.Read(buf);
					command = buf.GetCommand() & unchecked((int)(0xff));
					if (command == SSH_MSG_USERAUTH_GSSAPI_ERROR)
					{
						// uint32    major_status
						// uint32    minor_status
						// string    message
						// string    language tag
						buf = session.Read(buf);
						command = buf.GetCommand() & unchecked((int)(0xff));
					}
					else
					{
						//return false;
						if (command == SSH_MSG_USERAUTH_GSSAPI_ERRTOK)
						{
							// string error token
							buf = session.Read(buf);
							command = buf.GetCommand() & unchecked((int)(0xff));
						}
					}
					//return false;
					if (command == SSH_MSG_USERAUTH_FAILURE)
					{
						return false;
					}
					buf.GetInt();
					buf.GetByte();
					buf.GetByte();
					token = buf.GetString();
				}
			}
			Buffer mbuf = new Buffer();
			// string    session identifier
			// byte      SSH_MSG_USERAUTH_REQUEST
			// string    user name
			// string    service
			// string    "gssapi-with-mic"
			mbuf.PutString(session.GetSessionId());
			mbuf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
			mbuf.PutString(_username);
			mbuf.PutString(Util.Str2byte("ssh-connection"));
			mbuf.PutString(Util.Str2byte("gssapi-with-mic"));
			byte[] mic = context.GetMIC(mbuf.buffer, 0, mbuf.GetLength());
			if (mic == null)
			{
				return false;
			}
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_GSSAPI_MIC));
			buf.PutString(mic);
			session.Write(packet);
			context.Dispose();
			buf = session.Read(buf);
			command = buf.GetCommand() & unchecked((int)(0xff));
			if (command == SSH_MSG_USERAUTH_SUCCESS)
			{
				return true;
			}
			else
			{
				if (command == SSH_MSG_USERAUTH_FAILURE)
				{
					buf.GetInt();
					buf.GetByte();
					buf.GetByte();
					byte[] foo = buf.GetString();
					int partial_success = buf.GetByte();
					//System.err.println(new String(foo)+
					//		 " partial_success:"+(partial_success!=0));
					if (partial_success != 0)
					{
						throw new JSchPartialAuthException(Util.Byte2str(foo));
					}
				}
			}
			return false;
		}
Exemplo n.º 28
0
		/// <exception cref="System.Exception"></exception>
		public override bool Start(Session session)
		{
			base.Start(session);
			// send
			// byte      SSH_MSG_SERVICE_REQUEST(5)
			// string    service name "ssh-userauth"
			packet.Reset();
			buf.PutByte(unchecked((byte)Session.SSH_MSG_SERVICE_REQUEST));
			buf.PutString(Util.Str2byte("ssh-userauth"));
			session.Write(packet);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_SERVICE_REQUEST sent");
			}
			// receive
			// byte      SSH_MSG_SERVICE_ACCEPT(6)
			// string    service name
			buf = session.Read(buf);
			int command = buf.GetCommand();
			bool result = (command == SSH_MSG_SERVICE_ACCEPT);
			if (JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "SSH_MSG_SERVICE_ACCEPT received");
			}
			if (!result)
			{
				return false;
			}
			byte[] _username = null;
			_username = Util.Str2byte(username);
			// send
			// byte      SSH_MSG_USERAUTH_REQUEST(50)
			// string    user name
			// string    service name ("ssh-connection")
			// string    "none"
			packet.Reset();
			buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
			buf.PutString(_username);
			buf.PutString(Util.Str2byte("ssh-connection"));
			buf.PutString(Util.Str2byte("none"));
			session.Write(packet);
			while (true)
			{
				buf = session.Read(buf);
				command = buf.GetCommand() & unchecked((int)(0xff));
				if (command == SSH_MSG_USERAUTH_SUCCESS)
				{
					return true;
				}
				if (command == SSH_MSG_USERAUTH_BANNER)
				{
					buf.GetInt();
					buf.GetByte();
					buf.GetByte();
					byte[] _message = buf.GetString();
					byte[] lang = buf.GetString();
					string message = Util.Byte2str(_message);
					if (userinfo != null)
					{
						try
						{
							userinfo.ShowMessage(message);
						}
						catch (RuntimeException)
						{
						}
					}
					goto loop_continue;
				}
				if (command == SSH_MSG_USERAUTH_FAILURE)
				{
					buf.GetInt();
					buf.GetByte();
					buf.GetByte();
					byte[] foo = buf.GetString();
					int partial_success = buf.GetByte();
					methods = Util.Byte2str(foo);
					//System.err.println("UserAuthNONE: "+methods+
					//		   " partial_success:"+(partial_success!=0));
					//	if(partial_success!=0){
					//	  throw new JSchPartialAuthException(new String(foo));
					//	}
					break;
				}
				else
				{
					//      System.err.println("USERAUTH fail ("+command+")");
					throw new JSchException("USERAUTH fail (" + command + ")");
				}
loop_continue: ;
			}
loop_break: ;
			//throw new JSchException("USERAUTH fail");
			return false;
		}
Exemplo n.º 29
0
		/// <exception cref="System.Exception"></exception>
		public override bool Start(Session session)
		{
			base.Start(session);
			ArrayList identities = session.jsch.GetIdentityRepository().GetIdentities();
			byte[] passphrase = null;
			byte[] _username = null;
			int command;
			lock (identities)
			{
				if (identities.Count <= 0)
				{
					return false;
				}
				_username = Util.Str2byte(username);
				for (int i = 0; i < identities.Count; i++)
				{
					if (session.auth_failures >= session.max_auth_tries)
					{
						return false;
					}
					Identity identity = (Identity)(identities[i]);
					byte[] pubkeyblob = identity.GetPublicKeyBlob();
					//System.err.println("UserAuthPublicKey: "+identity+" "+pubkeyblob);
					if (pubkeyblob != null)
					{
						// send
						// byte      SSH_MSG_USERAUTH_REQUEST(50)
						// string    user name
						// string    service name ("ssh-connection")
						// string    "publickey"
						// boolen    FALSE
						// string    plaintext password (ISO-10646 UTF-8)
						packet.Reset();
						buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
						buf.PutString(_username);
						buf.PutString(Util.Str2byte("ssh-connection"));
						buf.PutString(Util.Str2byte("publickey"));
						buf.PutByte(unchecked((byte)0));
						buf.PutString(Util.Str2byte(identity.GetAlgName()));
						buf.PutString(pubkeyblob);
						session.Write(packet);
						while (true)
						{
							buf = session.Read(buf);
							command = buf.GetCommand() & unchecked((int)(0xff));
							if (command == SSH_MSG_USERAUTH_PK_OK)
							{
								break;
							}
							else
							{
								if (command == SSH_MSG_USERAUTH_FAILURE)
								{
									break;
								}
								else
								{
									if (command == SSH_MSG_USERAUTH_BANNER)
									{
										buf.GetInt();
										buf.GetByte();
										buf.GetByte();
										byte[] _message = buf.GetString();
										byte[] lang = buf.GetString();
										string message = Util.Byte2str(_message);
										if (userinfo != null)
										{
											userinfo.ShowMessage(message);
										}
										goto loop1_continue;
									}
									else
									{
										//System.err.println("USERAUTH fail ("+command+")");
										//throw new JSchException("USERAUTH fail ("+command+")");
										break;
									}
								}
							}
loop1_continue: ;
						}
loop1_break: ;
						if (command != SSH_MSG_USERAUTH_PK_OK)
						{
							continue;
						}
					}
					//System.err.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted());
					int count = 5;
					while (true)
					{
						if ((identity.IsEncrypted() && passphrase == null))
						{
							if (userinfo == null)
							{
								throw new JSchException("USERAUTH fail");
							}
							if (identity.IsEncrypted() && !userinfo.PromptPassphrase("Passphrase for " + identity
								.GetName()))
							{
								throw new JSchAuthCancelException("publickey");
							}
							//throw new JSchException("USERAUTH cancel");
							//break;
							string _passphrase = userinfo.GetPassphrase();
							if (_passphrase != null)
							{
								passphrase = Util.Str2byte(_passphrase);
							}
						}
						if (!identity.IsEncrypted() || passphrase != null)
						{
							if (identity.SetPassphrase(passphrase))
							{
								break;
							}
							else
							{
								throw new System.Exception ("Invalid passphrase supplied for the ssh key");
							}
						}
						Util.Bzero(passphrase);
						passphrase = null;
						count--;
						if (count == 0)
						{
							break;
						}
					}
					Util.Bzero(passphrase);
					passphrase = null;
					//System.err.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted());
					if (identity.IsEncrypted())
					{
						continue;
					}
					if (pubkeyblob == null)
					{
						pubkeyblob = identity.GetPublicKeyBlob();
					}
					//System.err.println("UserAuthPublicKey: pubkeyblob="+pubkeyblob);
					if (pubkeyblob == null)
					{
						continue;
					}
					// send
					// byte      SSH_MSG_USERAUTH_REQUEST(50)
					// string    user name
					// string    service name ("ssh-connection")
					// string    "publickey"
					// boolen    TRUE
					// string    plaintext password (ISO-10646 UTF-8)
					packet.Reset();
					buf.PutByte(unchecked((byte)SSH_MSG_USERAUTH_REQUEST));
					buf.PutString(_username);
					buf.PutString(Util.Str2byte("ssh-connection"));
					buf.PutString(Util.Str2byte("publickey"));
					buf.PutByte(unchecked((byte)1));
					buf.PutString(Util.Str2byte(identity.GetAlgName()));
					buf.PutString(pubkeyblob);
					//      byte[] tmp=new byte[buf.index-5];
					//      System.arraycopy(buf.buffer, 5, tmp, 0, tmp.length);
					//      buf.putString(signature);
					byte[] sid = session.GetSessionId();
					int sidlen = sid.Length;
					byte[] tmp = new byte[4 + sidlen + buf.index - 5];
					tmp[0] = unchecked((byte)((int)(((uint)sidlen) >> 24)));
					tmp[1] = unchecked((byte)((int)(((uint)sidlen) >> 16)));
					tmp[2] = unchecked((byte)((int)(((uint)sidlen) >> 8)));
					tmp[3] = unchecked((byte)(sidlen));
					System.Array.Copy(sid, 0, tmp, 4, sidlen);
					System.Array.Copy(buf.buffer, 5, tmp, 4 + sidlen, buf.index - 5);
					byte[] signature = identity.GetSignature(tmp);
					if (signature == null)
					{
						// for example, too long key length.
						break;
					}
					buf.PutString(signature);
					session.Write(packet);
					while (true)
					{
						buf = session.Read(buf);
						command = buf.GetCommand() & unchecked((int)(0xff));
						if (command == SSH_MSG_USERAUTH_SUCCESS)
						{
							return true;
						}
						else
						{
							if (command == SSH_MSG_USERAUTH_BANNER)
							{
								buf.GetInt();
								buf.GetByte();
								buf.GetByte();
								byte[] _message = buf.GetString();
								byte[] lang = buf.GetString();
								string message = Util.Byte2str(_message);
								if (userinfo != null)
								{
									userinfo.ShowMessage(message);
								}
								goto loop2_continue;
							}
							else
							{
								if (command == SSH_MSG_USERAUTH_FAILURE)
								{
									buf.GetInt();
									buf.GetByte();
									buf.GetByte();
									byte[] foo = buf.GetString();
									int partial_success = buf.GetByte();
									//System.err.println(new String(foo)+
									//                   " partial_success:"+(partial_success!=0));
									if (partial_success != 0)
									{
										throw new JSchPartialAuthException(Util.Byte2str(foo));
									}
									session.auth_failures++;
									break;
								}
							}
						}
						//System.err.println("USERAUTH fail ("+command+")");
						//throw new JSchException("USERAUTH fail ("+command+")");
						break;
loop2_continue: ;
					}
loop2_break: ;
				}
			}
			return false;
		}
Exemplo n.º 30
0
		internal static NSch.Channel GetChannel(int id, Session session)
		{
			lock (pool)
			{
				for (int i = 0; i < pool.Count; i++)
				{
					NSch.Channel c = (NSch.Channel)(pool[i]);
					if (c.id == id && c.session == session)
					{
						return c;
					}
				}
			}
			return null;
		}