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 = 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 HashedHostKey) &&
                             ((HashedHostKey)hk).isHashed()))
                        {
                            pool.Remove(hk);
                        }
                        else
                        {
                            hk.host = deleteSubString(hosts, host);
                        }
                        sync = true;
                    }
                }
            }
            if (sync)
            {
                try { Sync(); }
                catch /*(Exception e)*/ { };
            }
        }
 public override HostKey[] getHostKey(string host, string type)
 {
     lock (pool)
     {
         int count = 0;
         for (int i = 0; i < pool.Count; i++)
         {
             HostKey hk = 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 = 0; i < pool.Count; i++)
         {
             HostKey hk = pool[i];
             if (hk.type == HostKey.UNKNOWN)
             {
                 continue;
             }
             if (host == null ||
                 (hk.isMatched(host) &&
                  (type == null || hk.getType().Equals(type))))
             {
                 foo[j++] = hk;
             }
         }
         return(foo);
     }
 }
Exemple #3
0
        private void checkHost(string chost, int port, KeyExchange kex)
        {
            string shkc = getConfig("StrictHostKeyChecking");

            if (hostKeyAlias != null)
            {
                chost = hostKeyAlias;
            }

            //Console.Error.WriteLine("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);
                }
                //Console.Error.WriteLine("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);
                }

            }
        }
        internal void setKnownHosts(Stream foo)
        {
            pool.Clear();
            StringBuilder sb = new StringBuilder();
            byte          i;
            int           j;
            bool          error = false;

            try
            {
                Stream fis = foo;
                string host;
                string key = null;
                int    type;
                byte[] buf  = new byte[1024];
                int    bufl = 0;

                while (true)
                {
loop:
                    bufl = 0;
                    while (true)
                    {
                        j = fis.Read();
                        if (j == -1)
                        {
                            if (bufl == 0)
                            {
                                goto outloop;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (j == 0x0d)
                        {
                            continue;
                        }
                        if (j == 0x0a)
                        {
                            break;
                        }
                        if (buf.Length <= bufl)
                        {
                            if (bufl > 1024 * 10)
                            {
                                break;                     // too long...
                            }
                            byte[] newbuf = new byte[buf.Length * 2];
                            Array.Copy(buf, 0, newbuf, 0, buf.Length);
                            buf = newbuf;
                        }
                        buf[bufl++] = (byte)j;
                    }

                    j = 0;
                    while (j < bufl)
                    {
                        i = buf[j];
                        if (i == ' ' || i == '\t')
                        {
                            j++; continue;
                        }
                        if (i == '#')
                        {
                            addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                            goto loop;
                        }
                        break;
                    }
                    if (j >= bufl)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x20 || i == '\t')
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    host = sb.ToString();
                    if (j >= bufl || host.Length == 0)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    type      = -1;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 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(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x0d)
                        {
                            continue;
                        }
                        if (i == 0x0a)
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    key = sb.ToString();
                    if (key.Length == 0)
                    {
                        addInvalidLine(Encoding.UTF8.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    //Console.Error.WriteLine(host);
                    //Console.Error.WriteLine("|"+key+"|");

                    HostKey hk = null;
                    hk = new HashedHostKey(this, host, type,
                                           Util.fromBase64(key.getBytes(), 0,
                                                           key.Length));
                    pool.Add(hk);
                }
outloop:
                fis.Close();
                if (error)
                {
                    throw new JSchException("KnownHosts: invalid format");
                }
            }
            catch (Exception e)
            {
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.Message, e);
            }
        }
        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 = pool[i];
                    if (hk.isMatched(host) && hk.type == type)
                    {
                        /*
                         *    if(Util.array_equals(hk.key, key)){ return; }
                         *    if(hk.host.Equals(host)){
                         *      hk.key=key;
                         *      return;
                         *    }
                         *    else{
                         *      hk.host=deleteSubString(hk.host, host);
                         *      break;
                         *    }
                         */
                    }
                }
            }

            hk = hostkey;

            pool.Add(hk);

            string bar = getKnownHostsRepositoryID();

            if (bar != null)
            {
                bool     foo = true;
                FileInfo goo = new FileInfo(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?"
                                                   );
                        DirectoryInfo dgoo = Directory.GetParent(bar);
                        if (foo && dgoo != null && !dgoo.Exists)
                        {
                            foo = userinfo.promptYesNo("The parent directory " + goo + " does not exist.\n" +
                                                       "Are you sure you want to create it?"
                                                       );
                            if (foo)
                            {
                                if (!dgoo.mkdirs())
                                {
                                    userinfo.showMessage(goo + " has not been created.");
                                    foo = false;
                                }
                                else
                                {
                                    userinfo.showMessage(goo + " has been succesfully created.\nPlease check its access permission.");
                                }
                            }
                        }
                        if (dgoo == null)
                        {
                            foo = false;
                        }
                    }
                }
                if (foo)
                {
                    try
                    {
                        Sync(bar);
                    }
                    catch (Exception e) { Console.Error.WriteLine("sync known_hosts: " + e); }
                }
            }
        }
        private void addInvalidLine(string line)
        {
            HostKey hk = new HostKey(line, HostKey.UNKNOWN, null);

            pool.Add(hk);
        }
        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 = pool[i];
                    if (hk.isMatched(host) && hk.type == type)
                    {
                        /*
                              if(Util.array_equals(hk.key, key)){ return; }
                              if(hk.host.Equals(host)){
                                hk.key=key;
                                return;
                              }
                              else{
                                hk.host=deleteSubString(hk.host, host);
                                break;
                              }
                        */
                    }
                }
            }

            hk = hostkey;

            pool.Add(hk);

            string bar = getKnownHostsRepositoryID();
            if (bar != null)
            {
                bool foo = true;
                FileInfo goo = new FileInfo(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?"
                                                 );
                        DirectoryInfo dgoo = Directory.GetParent(bar);
                        if (foo && dgoo != null && !dgoo.Exists)
                        {
                            foo = userinfo.promptYesNo("The parent directory " + goo + " does not exist.\n" +
                                                     "Are you sure you want to create it?"
                                                     );
                            if (foo)
                            {
                                if (!dgoo.mkdirs())
                                {
                                    userinfo.showMessage(goo + " has not been created.");
                                    foo = false;
                                }
                                else
                                {
                                    userinfo.showMessage(goo + " has been succesfully created.\nPlease check its access permission.");
                                }
                            }
                        }
                        if (dgoo == null) foo = false;
                    }
                }
                if (foo)
                {
                    try
                    {
                        Sync(bar);
                    }
                    catch (Exception e) { Console.Error.WriteLine("sync known_hosts: " + e); }
                }
            }
        }
 private void addInvalidLine(string line)
 {
     HostKey hk = new HostKey(line, HostKey.UNKNOWN, null);
     pool.Add(hk);
 }
 public override HostKey[] getHostKey(string host, string type)
 {
     lock (pool)
     {
         int count = 0;
         for (int i = 0; i < pool.Count; i++)
         {
             HostKey hk = 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 = 0; i < pool.Count; i++)
         {
             HostKey hk = pool[i];
             if (hk.type == HostKey.UNKNOWN) continue;
             if (host == null ||
                (hk.isMatched(host) &&
                 (type == null || hk.getType().Equals(type))))
             {
                 foo[j++] = hk;
             }
         }
         return foo;
     }
 }
 public abstract void add(HostKey hostkey, UserInfo ui);