Exemplo n.º 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 ||
                 (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);
     }
 }
Exemplo n.º 2
0
        public override 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 || Util.array_equals(key, hk.key))))))
                {
                    pool.Remove(hk);
                    _sync = true;
                }
            }
            if (_sync)
            {
                try{ sync(); }
                catch {};
            }
        }
Exemplo n.º 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(Util.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(Util.getString(buf, 0, bufl));
                        goto loop;
                    }

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

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

break_loop:

                fis.Close();
                if (error)
                {
                    throw new JSchException("KnownHosts: invalid format");
                }
            }
            catch (Exception e)
            {
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.ToString());
            }
        }
Exemplo n.º 4
0
        private void addInvalidLine(String line)
        {
            HostKey hk = new HostKey(line, HostKey.UNKNOWN, null);

            pool.Add(hk);
        }
Exemplo n.º 5
0
        public override void add(String host, byte[] key, UserInfo 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); }
                }
            }
        }
Exemplo n.º 6
0
        public override void add(String host, byte[] key, UserInfo 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); }
                }
            }
        }
Exemplo n.º 7
0
 private void addInvalidLine(String line)
 {
     HostKey hk = new HostKey(line, HostKey.UNKNOWN, null);
     pool.Add(hk);
 }
Exemplo n.º 8
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(Util.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(Util.getString(buf, 0, bufl));
                        goto loop;
                    }

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

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

            break_loop:

                fis.Close();
                if(error)
                {
                    throw new JSchException("KnownHosts: invalid format");
                }
            }
            catch(Exception e)
            {
                if(e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.ToString());
            }
        }
Exemplo n.º 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 ||
                 (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;
     }
 }
Exemplo n.º 10
0
        private void checkHost(String host, KeyExchange kex)
        {
            String shkc=getConfig("StrictHostKeyChecking");

            //System.Console.WriteLine("shkc: "+shkc);

            byte[] K_S=kex.getHostKey();
            String key_type=kex.getKeyType();
            String key_fprint=kex.getFingerPrint();

            hostkey=new HostKey(host, K_S);

            HostKeyRepository hkr=jsch.getHostKeyRepository();
            int i=0;
            lock(hkr)
            {
                i=hkr.check(host, 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";}
                String message=
                    "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"+
                    "@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @\n"+
                    "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\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.";

                bool b=false;

                if(userinfo!=null)
                {
                    //userinfo.showMessage(message);
                    b=userinfo.promptYesNo(message+
                        "\nDo you want to delete the old key and insert the new key?");
                }
                //throw new JSchException("HostKey has been changed: "+host);
                if(!b)
                {
                    throw new JSchException("HostKey has been changed: "+host);
                }
                else
                {
                    lock(hkr)
                    {
                        hkr.remove(host, null);
                        insert=true;
                    }
                }
            }

            //    bool insert=false;

            if((shkc.equals("ask") || shkc.equals("yes")) &&
                (i!=HostKeyRepository.OK) && !insert)
            {
                if(shkc.equals("yes"))
                {
                    throw new JSchException("reject HostKey: "+host);
                }
                //System.Console.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);
                    else throw new JSchException("HostKey has been changed: "+host);
                }
            }

            if(shkc.equals("no") &&
                HostKeyRepository.NOT_INCLUDED==i)
            {
                insert=true;
            }

            if(insert)
            {
                lock(hkr)
                {
                    hkr.add(host, K_S, userinfo);
                }
            }
        }