Exemplo n.º 1
0
        //-----------------------------------------------------------------------
        // Class methods
        //-----------------------------------------------------------------------

        /// <summary>Returns password hash of the specified user, in specified format</summary>
        /// <param name="system">System to be connected to</param>
        /// <param name="userName">User name</param>
        /// <param name="hashType">Predefined password hash type (see documentation for details)</param>
        /// <returns>Password hash as a hex string</returns>
        public string GetEncryptedPassword(string userName, int hashType)
        {
            /*
             *  http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Fapis%2Fqsyrupwd.htm
             *
             *  Required Parameter Group:
             *
             *   1   Receiver variable              Output   Char(*) - 2000B
             *   2   Length of receiver variable    Input    Binary(4)
             *   3   Format                         Input    Char(8) - "UPWD0100"
             *   4   User profile name              Input    Char(10) - userName
             *   5   Error code                     I/O      Char(*)
             */

            ProgramCallParameters qsyrupwdCallParameters =
                new ProgramCallParameters(5)
            {
                [0] = new ProgramCallParameter(
                    ProgramCallParameter.ParameterTypeOutput,
                    null,
                    2000),
                [1] = new ProgramCallParameter(
                    ProgramCallParameter.ParameterTypeInput,
                    Converters.UInt32ToBigEndian(2000)),
                [2] = new ProgramCallParameter(
                    ProgramCallParameter.ParameterTypeInput,
                    Converters.AsciiToEbcdic("UPWD0100")),
                [3] = new ProgramCallParameter(
                    ProgramCallParameter.ParameterTypeInput,
                    Converters.AsciiToEbcdic(userName.ToUpper().PadRight(10))),
                [4] = new ProgramCallParameter(
                    ProgramCallParameter.ParameterTypeInputOutput,
                    null,
                    500)
            };
            CallMessages qsyrupwdCallMessages = new CallMessages();

            if (CallProgram("QSYRUPWD", "QSYS", ref qsyrupwdCallParameters, ref qsyrupwdCallMessages) != 0)
            {
                foreach (CallMessage outputMessage in qsyrupwdCallMessages)
                {
                    Debug.WriteLine(outputMessage.MessageText);
                }
                throw new System.InvalidOperationException("The method GetEncryptedPassword failed. Check debug information.");
            }


            switch (hashType)
            {
            case PASSWORD_HASH_ALLDATA:     // All data
                return(Converters.BigEndianToHexString(qsyrupwdCallParameters[0].ParameterValue, 1, 269));

            case PASSWORD_HASH_UNKNOWNHASH:     // Unknown (hash?) data
                return(Converters.BigEndianToHexString(qsyrupwdCallParameters[0].ParameterValue, 78, 192));

            case PASSWORD_HASH_HMACSHA1MC:     // HMAC-SHA1 password (mixed case)
                return(Converters.BigEndianToHexString(qsyrupwdCallParameters[0].ParameterValue, 35, 20));

            case PASSWORD_HASH_HMACSHA1UC:     // HMAC-SHA1 password (uppercase)
                return(Converters.BigEndianToHexString(qsyrupwdCallParameters[0].ParameterValue, 55, 20));

            case PASSWORD_HASH_LMHASH:     // LM hash
                return(Converters.BigEndianToHexString(qsyrupwdCallParameters[0].ParameterValue, 17, 16));

            case PASSWORD_HASH_DES:     // Composed DES hash (PW_TOKENa XOR PW_TOKENb):
                return(Converters.BigEndianToHexString(Converters.UInt64ToBigEndian(Converters.BigEndianToUInt64(qsyrupwdCallParameters[0].ParameterValue, 1, 8) ^ Converters.BigEndianToUInt64(qsyrupwdCallParameters[0].ParameterValue, 9, 8))));

            case PASSWORD_HASH_SECONDDES:     // Second DES password token (PW_TOKENb)
                return(Converters.BigEndianToHexString(qsyrupwdCallParameters[0].ParameterValue, 9, 8));

            case PASSWORD_HASH_FIRSTDES:     // First DES password (PW_TOKENa)
            default:
                return(Converters.BigEndianToHexString(qsyrupwdCallParameters[0].ParameterValue, 1, 8));
            }
        }