Example #1
0
        public void remove(String host, String type, byte[] key)
        {
            bool _sync = false;

            for (int i = 0; i < pool.Count; i++)
            {
                HostKey hk = (HostKey)(pool[i]);
                if (host == null ||
                    (hk.getHost().Equals(host) &&
                     (type == null || (hk.getType().Equals(type) &&
                                       (key == null || key.SequenceEqual(hk.key))))))
                {
                    pool.Remove(hk);
                    _sync = true;
                }
            }
            if (_sync)
            {
                try {
                    sync();
                }
                catch {
                };
            }
        }
Example #2
0
 public 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 ||
                 (isIncluded(hk.host, 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 = (HostKey)pool[i];
             if (hk.type == HostKey.UNKNOWN)
             {
                 continue;
             }
             if (host == null ||
                 (isIncluded(hk.host, host) &&
                  (type == null || hk.getType().Equals(type))))
             {
                 foo[j++] = hk;
             }
         }
         return(foo);
     }
 }
Example #3
0
        internal void setKnownHosts(StreamReader foo)
        {
            pool.Clear();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            byte i;
            int  j;
            bool error = false;

            try {
                StreamReader fis = foo;
                String       host;
                String       key = null;
                int          type;
                byte[]       buf  = new byte[1024];
                int          bufl = 0;
loop:
                while (true)
                {
                    bufl = 0;
                    while (true)
                    {
                        j = fis.Read();
                        if (j == -1)
                        {
                            goto break_loop;
                        }
                        if (j == 0x0d)
                        {
                            continue;
                        }
                        if (j == 0x0a)
                        {
                            break;
                        }
                        buf[bufl++] = (byte)j;
                    }

                    j = 0;
                    while (j < bufl)
                    {
                        i = buf[j];
                        if (i == ' ' || i == '\t')
                        {
                            j++;
                            continue;
                        }
                        if (i == '#')
                        {
                            addInvalidLine(System.Text.Encoding.Default.GetString(buf, 0, bufl));
                            goto loop;
                        }
                        break;
                    }
                    if (j >= bufl)
                    {
                        addInvalidLine(System.Text.Encoding.Default.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(System.Text.Encoding.Default.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(StringAux.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(StringAux.getString(buf, 0, bufl));
                        goto loop;
                    }

                    //System.out.println(host);
                    //System.out.println("|"+key+"|");

                    HostKey hk = new HostKey(host, type,
                                             StringAux.fromBase64(StringAux.getBytes(key), 0,
                                                                  key.Length));
                    pool.Add(hk);
                }

break_loop:

                fis.Close();
                if (error)
                {
                    throw new SshClientException("KnownHosts: invalid format");
                }
            }
            catch (Exception e) {
                if (e is SshClientException)
                {
                    throw (SshClientException)e;
                }
                throw new SshClientException(e.ToString());
            }
        }
Example #4
0
        public void add(String host, byte[] key, IUserInfo userinfo)
        {
            HostKey hk;
            int     type = getType(key);

            for (int i = 0; i < pool.Count; i++)
            {
                hk = (HostKey)(pool[i]);
                if (isIncluded(hk.host, 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 = new HostKey(host, type, key);
            pool.Add(hk);

            String bar = getKnownHostsRepositoryID();

            if (userinfo != null &&
                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 dir = goo.Directory;
                        if (foo && dir != null && !dir.Exists)
                        {
                            foo = userinfo.promptYesNo(
                                "The parent directory " + dir.Name + " does not exist.\n" +
                                "Are you sure you want to create it?"
                                );
                            if (foo)
                            {
                                try {
                                    dir.Create();
                                    userinfo.showMessage(dir.Name + " has been succesfully created.\nPlease check its access permission.");
                                }
                                catch {
                                    userinfo.showMessage(dir.Name + " has not been created.");
                                    foo = false;
                                }
                            }
                        }
                        if (goo == null)
                        {
                            foo = false;
                        }
                    }
                }
                if (foo)
                {
                    try {
                        sync(bar);
                    }
                    catch (Exception e) {
                        Console.WriteLine("sync known_hosts: " + e);
                    }
                }
            }
        }
Example #5
0
        private void addInvalidLine(String line)
        {
            HostKey hk = new HostKey(line, HostKey.UNKNOWN, null);

            pool.Add(hk);
        }