Example #1
0
        /* Change the password */
        private bool DoChangePassword( string oldpassword, string newpassword)
        {
            m_connection.WriteLine("CAPABILITIES");
            byte[] fln = m_connection.ReadLine();
            if (fln == null)
            {
                SetState(State.Terminated, "server closed connection");
                return false;
            }
            Console.WriteLine( "CAPABILITIES {0}", Encoding.UTF8.GetString(fln));

            // 1. The client asks to open the password change dialog:
		    m_connection.WriteLine( "PASSWD");
		    // 2. The server accepts or not:
            byte[] ln = m_connection.ReadLine();
            if (ln == null)
            {
                SetState(State.Terminated, "server closed connection");
                return false;
            }
            if (Protocol.IsCommand("ERR", ln))
            {
                SetState(State.Terminated, "password change refused: " + Protocol.CommandArg("ERR", ln));
                return false;
            }
            else if (!Protocol.IsCommand("OK", ln))
            {
                SetState(State.Terminated, "protocol error initiating password change");
                return false;
            }
		    // 3. The server sends a challenge:
            byte[] challenge = m_connection.ReadContent();
            if (challenge == null)
            {
                SetState(State.Terminated, "server closed connection");
                return false;
            }
		    // 4. The client returns a message with the password 
		    //	pair (old, new) encrypted with the challenge:
		    m_connection.WriteContent(
                Encoding.ASCII.GetBytes( 
                    PasswordChange.Message( oldpassword, Encoding.ASCII.GetString(challenge), newpassword)));

		    // 5. The server accepts the password change or not:
            ln = m_connection.ReadLine();
            if (ln == null)
            {
                SetState(State.Terminated, "server closed connection");
                return false;
            }
            if (Protocol.IsCommand("OK", ln))
            {
                return true;
            }
            else if (Protocol.IsCommand("ERR", ln))
            {
                SetState(State.Terminated, "password change error: " + Protocol.CommandArg("ERR", ln));
                return false;
            }
            else
            {
                SetState(State.Terminated, "protocol error in password change");
                return false;
            }
        }