Beispiel #1
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;
                }
            }
        }
        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 (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((byte)SSH_MSG_USERAUTH_REQUEST);
                    buf.putString(_username);
                    buf.putString("ssh-connection".getBytes());
                    buf.putString("password".getBytes());
                    buf.putByte((byte)0);
                    buf.putString(password);
                    session.write(packet);

                    while (true)
                    {
                        buf = session.Read(buf);
                        int command = buf.getCommand() & 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;
                        }
                        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 Password: "******"password");
                            }

                            byte[] newpassword = response[0].getBytes();

                            // 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((byte)SSH_MSG_USERAUTH_REQUEST);
                            buf.putString(_username);
                            buf.putString("ssh-connection".getBytes());
                            buf.putString("password".getBytes());
                            buf.putByte((byte)1);
                            buf.putString(password);
                            buf.putString(newpassword);
                            Util.bzero(newpassword);
                            response = null;
                            session.write(packet);
                            goto loop;
                        }
                        if (command == SSH_MSG_USERAUTH_FAILURE)
                        {
                            buf.getInt(); buf.getByte(); buf.getByte();
                            byte[] foo             = buf.getString();
                            int    partial_success = buf.getByte();
                            //Console.Error.WriteLine(Encoding.UTF8.GetString(foo)+
                            //		 " partial_success:"+(partial_success!=0));
                            if (partial_success != 0)
                            {
                                throw new JSchPartialAuthException(Encoding.UTF8.GetString(foo));
                            }
                            break;
                        }
                        else
                        {
                            //Console.Error.WriteLine("USERAUTH fail ("+buf.getCommand()+")");
                            //	  throw new JSchException("USERAUTH fail ("+buf.getCommand()+")");
                            return(false);
                        }
loop:
                        new object();
                    }

                    if (password != null)
                    {
                        Util.bzero(password);
                        password = null;
                    }
                }
            }
            finally
            {
                if (password != null)
                {
                    Util.bzero(password);
                    password = null;
                }
            }

            //throw new JSchException("USERAUTH fail");
            //return false;
        }
        public override bool start(Session session)
        {
//System.out.println("UserAuthKeyboardInteractive: start");
            Packet packet   = session.packet;
            Buffer buf      = session.buf;
            String username = session.username;
            String dest     = username + "@" + session.host;

            if (session.port != 22)
            {
                dest += (":" + session.port);
            }

            bool cancel = false;

            byte[] _username = null;
            try
            {
                _username = Encoding.UTF8.GetBytes(username);
            }
            catch
            {
                _username = Util.getBytes(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((byte)Session.SSH_MSG_USERAUTH_REQUEST);
                buf.putString(_username);
                buf.putString(Util.getBytes("ssh-connection"));
                //buf.putString("ssh-userauth".getBytes());
                buf.putString(Util.getBytes("keyboard-interactive"));
                buf.putString(Util.getBytes(""));
                buf.putString(Util.getBytes(""));
                session.write(packet);

                bool firsttime = true;
loop:
                while (true)
                {
                    // receive
                    // byte      SSH_MSG_USERAUTH_SUCCESS(52)
                    // string    service name
                    try
                    {
                        buf = session.read(buf);
                    }
                    catch (JSchException e)
                    {
                        e.GetType();
                        return(false);
                    }
                    catch (IOException e)
                    {
                        e.GetType();
                        return(false);
                    }
                    //System.out.println("read: 52 ? "+    buf.buffer[5]);
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_SUCCESS)
                    {
                        return(true);
                    }
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_BANNER)
                    {
                        buf.getInt();
                        buf.getByte();
                        buf.getByte();
                        byte[] _message = buf.getString();
                        byte[] lang     = buf.getString();
                        String message  = null;
                        try
                        {
                            message = Util.getStringUTF8(_message);
                        }
                        catch
                        {
                            message = Util.getString(_message);
                        }
                        if (userinfo != null)
                        {
                            userinfo.showMessage(message);
                        }
                        goto loop;
                    }
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE)
                    {
                        buf.getInt();
                        buf.getByte();
                        buf.getByte();
                        byte[] foo             = buf.getString();
                        int    partial_success = buf.getByte();
//	  System.out.println(new String(foo)+
//			     " partial_success:"+(partial_success!=0));

                        if (partial_success != 0)
                        {
                            throw new JSchPartialAuthException(Util.getString(foo));
                        }

                        if (firsttime)
                        {
                            throw new JSchException("USERAUTH KI is not supported");
                            //return false;
                            //cancel=true;  // ??
                        }
                        break;
                    }
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_INFO_REQUEST)
                    {
                        firsttime = false;
                        buf.getInt();
                        buf.getByte();
                        buf.getByte();
                        String name         = Util.getString(buf.getString());
                        String instruction  = Util.getString(buf.getString());
                        String languate_tag = Util.getString(buf.getString());
                        int    num          = buf.getInt();
//System.out.println("name: "+name);
//System.out.println("instruction: "+instruction);
//System.out.println("lang: "+languate_tag);
//System.out.println("num: "+num);
                        String[] prompt = new String[num];
                        bool[]   echo   = new bool[num];
                        for (int i = 0; i < num; i++)
                        {
                            prompt[i] = Util.getString(buf.getString());
                            echo[i]   = (buf.getByte() != 0);
//System.out.println("  "+prompt[i]+","+echo[i]);
                        }

                        String[] response = null;
                        if (num > 0 ||
                            (name.Length > 0 || instruction.Length > 0)
                            )
                        {
                            UIKeyboardInteractive kbi = (UIKeyboardInteractive)userinfo;
                            if (userinfo != null)
                            {
                                response = kbi.promptKeyboardInteractive(dest,
                                                                         name,
                                                                         instruction,
                                                                         prompt,
                                                                         echo);
                            }
                        }
                        // byte      SSH_MSG_USERAUTH_INFO_RESPONSE(61)
                        // int       num-responses
                        // string    response[1] (ISO-10646 UTF-8)
                        // ...
                        // string    response[num-responses] (ISO-10646 UTF-8)
//if(response!=null)
//System.out.println("response.length="+response.length);
//else
//System.out.println("response is null");
                        packet.reset();
                        buf.putByte((byte)Session.SSH_MSG_USERAUTH_INFO_RESPONSE);
                        if (num > 0 &&
                            (response == null || // cancel
                             num != response.Length))
                        {
                            buf.putInt(0);
                            if (response == null)
                            {
                                cancel = true;
                            }
                        }
                        else
                        {
                            buf.putInt(num);
                            for (int i = 0; i < num; i++)
                            {
//System.out.println("response: |"+response[i]+"| <- replace here with **** if you need");
                                buf.putString(Util.getBytes(response[i]));
                            }
                        }
                        session.write(packet);
                        if (cancel)
                        {
                            break;
                        }
//System.out.println("continue loop");
                        goto loop;
                    }
                    //throw new JSchException("USERAUTH fail ("+buf.buffer[5]+")");
                    return(false);
                }
                if (cancel)
                {
                    throw new JSchAuthCancelException("keyboard-interactive");
                    //break;
                }
            }
            //return false;
        }
        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((byte)SSH_MSG_USERAUTH_REQUEST);
                buf.putString(_username);
                buf.putString("ssh-connection".getBytes());
                //buf.putString("ssh-userauth".getBytes());
                buf.putString("keyboard-interactive".getBytes());
                buf.putString("".getBytes());
                buf.putString("".getBytes());
                session.write(packet);

                bool firsttime = true;
                while (true)
                {
                    buf = session.Read(buf);
                    int command = buf.getCommand() & 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  = null;
                        try { message = Encoding.UTF8.GetString(_message); }
                        catch //(Exception e)
                        {
                            message = Encoding.UTF8.GetString(_message);
                        }
                        if (userinfo != null)
                        {
                            userinfo.showMessage(message);
                        }
                        goto loop;
                    }
                    if (command == SSH_MSG_USERAUTH_FAILURE)
                    {
                        buf.getInt(); buf.getByte(); buf.getByte();
                        byte[] foo             = buf.getString();
                        int    partial_success = buf.getByte();
                        //	  Console.Error.WriteLine(Encoding.UTF8.GetString(foo)+
                        //			     " partial_success:"+(partial_success!=0));

                        if (partial_success != 0)
                        {
                            throw new JSchPartialAuthException(Encoding.UTF8.GetString(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         = Encoding.UTF8.GetString(buf.getString());
                        string   instruction  = Encoding.UTF8.GetString(buf.getString());
                        string   languate_tag = Encoding.UTF8.GetString(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] = Encoding.UTF8.GetString(buf.getString());
                            echo[i]   = (buf.getByte() != 0);
                        }

                        byte[][] response = null;
                        if (num > 0 ||
                            (name.Length > 0 || instruction.Length > 0)
                            )
                        {
                            if (userinfo != null)
                            {
                                UIKeyboardInteractive kbi = (UIKeyboardInteractive)userinfo;
                                string[] _response        = kbi.promptKeyboardInteractive(dest,
                                                                                          name,
                                                                                          instruction,
                                                                                          prompt,
                                                                                          echo);
                                if (_response != null)
                                {
                                    response = new byte[_response.Length][];
                                    for (int i = 0; i < _response.Length; i++)
                                    {
                                        response[i] = Util.str2byte(_response[i]);
                                    }
                                }
                            }
                            else if (password != null &&
                                     prompt.Length == 1 &&
                                     !echo[0] &&
                                     prompt[0].ToLower().StartsWith("password:"******"response.Length="+response.Length);
                        //else
                        //Console.Error.WriteLine("response is null");
                        packet.reset();
                        buf.putByte((byte)SSH_MSG_USERAUTH_INFO_RESPONSE);
                        if (num > 0 &&
                            (response == null || // cancel
                             num != response.Length))
                        {
                            if (response == null)
                            {
                                // working around the bug in OpenSSH ;-<
                                buf.putInt(num);
                                for (int i = 0; i < num; i++)
                                {
                                    buf.putString("".getBytes());
                                }
                            }
                            else
                            {
                                buf.putInt(0);
                            }

                            if (response == null)
                            {
                                cancel = true;
                            }
                        }
                        else
                        {
                            buf.putInt(num);
                            for (int i = 0; i < num; i++)
                            {
                                //Console.Error.WriteLine("response: |"+Encoding.UTF8.GetString(response[i])+"| <- replace here with **** if you need");
                                buf.putString(response[i]);
                            }
                        }
                        session.write(packet);

                        /*
                         * if(cancel)
                         * break;
                         */
                        goto loop;
                    }
                    //throw new JSchException("USERAUTH fail ("+command+")");
                    return(false);

loop:
                    new object();
                }
                if (cancel)
                {
                    throw new JSchAuthCancelException("keyboard-interactive");
                    //break;
                }
            }
            //return false;
        }
        /// <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");
                }
            }
        }