Exemple #1
0
 public override HostKey[] GetHostKey(string host, string type)
 {
     lock (pool)
     {
         int count = 0;
         for (int i = 0; i < pool.Count; i++)
         {
             HostKey hk = (HostKey)pool[i];
             if (hk.type == HostKey.UNKNOWN)
             {
                 continue;
             }
             if (host == null || (hk.IsMatched(host) && (type == null || hk.GetType().Equals(type
                                                                                             ))))
             {
                 count++;
             }
         }
         if (count == 0)
         {
             return(null);
         }
         HostKey[] foo = new HostKey[count];
         int       j   = 0;
         for (int i_1 = 0; i_1 < pool.Count; i_1++)
         {
             HostKey hk = (HostKey)pool[i_1];
             if (hk.type == HostKey.UNKNOWN)
             {
                 continue;
             }
             if (host == null || (hk.IsMatched(host) && (type == null || hk.GetType().Equals(type
                                                                                             ))))
             {
                 foo[j++] = hk;
             }
         }
         return(foo);
     }
 }
Exemple #2
0
        public override void Remove(string host, string type, byte[] key)
        {
            bool sync = false;

            lock (pool)
            {
                for (int i = 0; i < pool.Count; i++)
                {
                    HostKey hk = (HostKey)(pool[i]);
                    if (host == null || (hk.IsMatched(host) && (type == null || (hk.GetType().Equals(
                                                                                     type) && (key == null || Util.Array_equals(key, hk.key))))))
                    {
                        string hosts = hk.GetHost();
                        if (hosts.Equals(host) || ((hk is KnownHosts.HashedHostKey) && ((KnownHosts.HashedHostKey
                                                                                         )hk).IsHashed()))
                        {
                            pool.RemoveElement(hk);
                        }
                        else
                        {
                            hk.host = DeleteSubString(hosts, host);
                        }
                        sync = true;
                    }
                }
            }
            if (sync)
            {
                try
                {
                    Sync();
                }
                catch (Exception)
                {
                }
            }
        }
Exemple #3
0
		/// <exception cref="NSch.JSchException"></exception>
		private void CheckHost(string chost, int port, KeyExchange kex)
		{
			string shkc = GetConfig("StrictHostKeyChecking");
			if (hostKeyAlias != null)
			{
				chost = hostKeyAlias;
			}
			//System.err.println("shkc: "+shkc);
			byte[] K_S = kex.GetHostKey();
			string key_type = kex.GetKeyType();
			string key_fprint = kex.GetFingerPrint();
			if (hostKeyAlias == null && port != 22)
			{
				chost = ("[" + chost + "]:" + port);
			}
			//    hostkey=new HostKey(chost, K_S);
			HostKeyRepository hkr = jsch.GetHostKeyRepository();
			int i = 0;
			lock (hkr)
			{
				i = hkr.Check(chost, K_S);
			}
			bool insert = false;
			if ((shkc.Equals("ask") || shkc.Equals("yes")) && i == HostKeyRepository.CHANGED)
			{
				string file = null;
				lock (hkr)
				{
					file = hkr.GetKnownHostsRepositoryID();
				}
				if (file == null)
				{
					file = "known_hosts";
				}
				bool b = false;
				if (userinfo != null)
				{
					string message = "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!\n" + "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\n"
						 + "Someone could be eavesdropping on you right now (man-in-the-middle attack)!\n"
						 + "It is also possible that the " + key_type + " host key has just been changed.\n"
						 + "The fingerprint for the " + key_type + " key sent by the remote host is\n" +
						 key_fprint + ".\n" + "Please contact your system administrator.\n" + "Add correct host key in "
						 + file + " to get rid of this message.";
					if (shkc.Equals("ask"))
					{
						b = userinfo.PromptYesNo(message + "\nDo you want to delete the old key and insert the new key?"
							);
					}
					else
					{
						// shkc.equals("yes")
						userinfo.ShowMessage(message);
					}
				}
				if (!b)
				{
					throw new JSchException("HostKey has been changed: " + chost);
				}
				lock (hkr)
				{
					hkr.Remove(chost, (key_type.Equals("DSA") ? "ssh-dss" : "ssh-rsa"), null);
					insert = true;
				}
			}
			if ((shkc.Equals("ask") || shkc.Equals("yes")) && (i != HostKeyRepository.OK) && 
				!insert)
			{
				if (shkc.Equals("yes"))
				{
					throw new JSchException("reject HostKey: " + host);
				}
				//System.err.println("finger-print: "+key_fprint);
				if (userinfo != null)
				{
					bool foo = userinfo.PromptYesNo("The authenticity of host '" + host + "' can't be established.\n"
						 + key_type + " key fingerprint is " + key_fprint + ".\n" + "Are you sure you want to continue connecting?"
						);
					if (!foo)
					{
						throw new JSchException("reject HostKey: " + host);
					}
					insert = true;
				}
				else
				{
					if (i == HostKeyRepository.NOT_INCLUDED)
					{
						throw new JSchException("UnknownHostKey: " + host + ". " + key_type + " key fingerprint is "
							 + key_fprint);
					}
					else
					{
						throw new JSchException("HostKey has been changed: " + host);
					}
				}
			}
			if (shkc.Equals("no") && HostKeyRepository.NOT_INCLUDED == i)
			{
				insert = true;
			}
			if (i == HostKeyRepository.OK && JSch.GetLogger().IsEnabled(Logger.INFO))
			{
				JSch.GetLogger().Log(Logger.INFO, "Host '" + host + "' is known and mathces the "
					 + key_type + " host key");
			}
			if (insert && JSch.GetLogger().IsEnabled(Logger.WARN))
			{
				JSch.GetLogger().Log(Logger.WARN, "Permanently added '" + host + "' (" + key_type
					 + ") to the list of known hosts.");
			}
			string hkh = GetConfig("HashKnownHosts");
			if (hkh.Equals("yes") && (hkr is KnownHosts))
			{
				hostkey = ((KnownHosts)hkr).CreateHashedHostKey(chost, K_S);
			}
			else
			{
				hostkey = new HostKey(chost, K_S);
			}
			if (insert)
			{
				lock (hkr)
				{
					hkr.Add(hostkey, userinfo);
				}
			}
		}
 public abstract void Add(HostKey hostkey, UserInfo ui);
		public abstract void Add(HostKey hostkey, UserInfo ui);
Exemple #6
0
        /// <exception cref="NSch.JSchException"></exception>
        internal virtual void SetKnownHosts(InputStream foo)
        {
            pool.Clear();
            StringBuilder sb = new StringBuilder();
            byte          i;
            int           j;
            bool          error = false;

            try
            {
                InputStream fis = foo;
                string      host;
                string      key = null;
                int         type;
                byte[]      buf  = new byte[1024];
                int         bufl = 0;
                while (true)
                {
                    bufl = 0;
                    while (true)
                    {
                        j = fis.Read();
                        if (j == -1)
                        {
                            if (bufl == 0)
                            {
                                goto loop_break;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (j == unchecked ((int)(0x0d)))
                        {
                            continue;
                        }
                        if (j == unchecked ((int)(0x0a)))
                        {
                            break;
                        }
                        if (buf.Length <= bufl)
                        {
                            if (bufl > 1024 * 10)
                            {
                                break;
                            }
                            // too long...
                            byte[] newbuf = new byte[buf.Length * 2];
                            System.Array.Copy(buf, 0, newbuf, 0, buf.Length);
                            buf = newbuf;
                        }
                        buf[bufl++] = unchecked ((byte)j);
                    }
                    j = 0;
                    while (j < bufl)
                    {
                        i = buf[j];
                        if (i == ' ' || i == '\t')
                        {
                            j++;
                            continue;
                        }
                        if (i == '#')
                        {
                            AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                            goto loop_continue;
                        }
                        break;
                    }
                    if (j >= bufl)
                    {
                        AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                        goto loop_continue;
                    }
                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == unchecked ((int)(0x20)) || i == '\t')
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    host = sb.ToString();
                    if (j >= bufl || host.Length == 0)
                    {
                        AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                        goto loop_continue;
                    }
                    sb.Length = 0;
                    type      = -1;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == unchecked ((int)(0x20)) || i == '\t')
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    if (sb.ToString().Equals("ssh-dss"))
                    {
                        type = HostKey.SSHDSS;
                    }
                    else
                    {
                        if (sb.ToString().Equals("ssh-rsa"))
                        {
                            type = HostKey.SSHRSA;
                        }
                        else
                        {
                            j = bufl;
                        }
                    }
                    if (j >= bufl)
                    {
                        AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                        goto loop_continue;
                    }
                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == unchecked ((int)(0x0d)))
                        {
                            continue;
                        }
                        if (i == unchecked ((int)(0x0a)))
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    key = sb.ToString();
                    if (key.Length == 0)
                    {
                        AddInvalidLine(Util.Byte2str(buf, 0, bufl));
                        goto loop_continue;
                    }
                    //System.err.println(host);
                    //System.err.println("|"+key+"|");
                    HostKey hk = null;
                    hk = new KnownHosts.HashedHostKey(this, host, type, Util.FromBase64(Util.Str2byte
                                                                                            (key), 0, key.Length));
                    pool.Add(hk);
                    loop_continue :;
                }
                loop_break :;
                fis.Close();
                if (error)
                {
                    throw new JSchException("KnownHosts: invalid format");
                }
            }
            catch (Exception e)
            {
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                if (e is Exception)
                {
                    throw new JSchException(e.ToString(), (Exception)e);
                }
                throw new JSchException(e.ToString());
            }
        }
Exemple #7
0
        public override void Add(HostKey hostkey, UserInfo userinfo)
        {
            int    type = hostkey.type;
            string host = hostkey.GetHost();

            byte[]  key = hostkey.key;
            HostKey hk  = null;

            lock (pool)
            {
                for (int i = 0; i < pool.Count; i++)
                {
                    hk = (HostKey)(pool[i]);
                    if (hk.IsMatched(host) && hk.type == type)
                    {
                    }
                }
            }
            hk = hostkey;
            pool.Add(hk);
            string bar = GetKnownHostsRepositoryID();

            if (bar != null)
            {
                bool     foo = true;
                FilePath goo = new FilePath(bar);
                if (!goo.Exists())
                {
                    foo = false;
                    if (userinfo != null)
                    {
                        foo = userinfo.PromptYesNo(bar + " does not exist.\n" + "Are you sure you want to create it?"
                                                   );
                        goo = goo.GetParentFile();
                        if (foo && goo != null && !goo.Exists())
                        {
                            foo = userinfo.PromptYesNo("The parent directory " + goo + " does not exist.\n" +
                                                       "Are you sure you want to create it?");
                            if (foo)
                            {
                                if (!goo.Mkdirs())
                                {
                                    userinfo.ShowMessage(goo + " has not been created.");
                                    foo = false;
                                }
                                else
                                {
                                    userinfo.ShowMessage(goo + " has been succesfully created.\nPlease check its access permission."
                                                         );
                                }
                            }
                        }
                        if (goo == null)
                        {
                            foo = false;
                        }
                    }
                }
                if (foo)
                {
                    try
                    {
                        Sync(bar);
                    }
                    catch (Exception e)
                    {
                        System.Console.Error.WriteLine("sync known_hosts: " + e);
                    }
                }
            }
        }
Exemple #8
0
        /// <exception cref="NSch.JSchException"></exception>
        private void AddInvalidLine(string line)
        {
            HostKey hk = new HostKey(line, HostKey.UNKNOWN, null);

            pool.Add(hk);
        }
Exemple #9
0
		public override HostKey[] GetHostKey(string host, string type)
		{
			lock (pool)
			{
				int count = 0;
				for (int i = 0; i < pool.Count; i++)
				{
					HostKey hk = (HostKey)pool[i];
					if (hk.type == HostKey.UNKNOWN)
					{
						continue;
					}
					if (host == null || (hk.IsMatched(host) && (type == null || hk.GetType().Equals(type
						))))
					{
						count++;
					}
				}
				if (count == 0)
				{
					return null;
				}
				HostKey[] foo = new HostKey[count];
				int j = 0;
				for (int i_1 = 0; i_1 < pool.Count; i_1++)
				{
					HostKey hk = (HostKey)pool[i_1];
					if (hk.type == HostKey.UNKNOWN)
					{
						continue;
					}
					if (host == null || (hk.IsMatched(host) && (type == null || hk.GetType().Equals(type
						))))
					{
						foo[j++] = hk;
					}
				}
				return foo;
			}
		}
Exemple #10
0
		public override void Add(HostKey hostkey, UserInfo userinfo)
		{
			int type = hostkey.type;
			string host = hostkey.GetHost();
			byte[] key = hostkey.key;
			HostKey hk = null;
			lock (pool)
			{
				for (int i = 0; i < pool.Count; i++)
				{
					hk = (HostKey)(pool[i]);
					if (hk.IsMatched(host) && hk.type == type)
					{
					}
				}
			}
			hk = hostkey;
			pool.Add(hk);
			string bar = GetKnownHostsRepositoryID();
			if (bar != null)
			{
				bool foo = true;
				FilePath goo = new FilePath(bar);
				if (!goo.Exists())
				{
					foo = false;
					if (userinfo != null)
					{
						foo = userinfo.PromptYesNo(bar + " does not exist.\n" + "Are you sure you want to create it?"
							);
						goo = goo.GetParentFile();
						if (foo && goo != null && !goo.Exists())
						{
							foo = userinfo.PromptYesNo("The parent directory " + goo + " does not exist.\n" +
								 "Are you sure you want to create it?");
							if (foo)
							{
								if (!goo.Mkdirs())
								{
									userinfo.ShowMessage(goo + " has not been created.");
									foo = false;
								}
								else
								{
									userinfo.ShowMessage(goo + " has been succesfully created.\nPlease check its access permission."
										);
								}
							}
						}
						if (goo == null)
						{
							foo = false;
						}
					}
				}
				if (foo)
				{
					try
					{
						Sync(bar);
					}
					catch (Exception e)
					{
						System.Console.Error.WriteLine("sync known_hosts: " + e);
					}
				}
			}
		}
Exemple #11
0
		/// <exception cref="NSch.JSchException"></exception>
		private void AddInvalidLine(string line)
		{
			HostKey hk = new HostKey(line, HostKey.UNKNOWN, null);
			pool.Add(hk);
		}