//----------------------------------------------------------------------- // Class constructors //----------------------------------------------------------------------- /// <summary>Initializes a new instance of the <see cref="UserProfile"/> class.</summary> /// <param name="binaryData">Byte stream input</param> internal UserProfile(byte[] binaryData) { // Check whether the table is at least 712B long (V7R2 minimum) if (binaryData.Length < 712) { throw new System.InvalidOperationException("The received user data is too short for USRI0300 format."); } // Check whether the returned bytes value makes sense if (Converters.BigEndianToUInt32(binaryData) > binaryData.Length) { throw new System.InvalidOperationException("The received data length is bigger than the buffer size. Exiting."); } this.UserName = Converters.EbcdicToAsciiString(binaryData, 8, 10).ToUpper().Trim(); this.AccountingCode = Converters.EbcdicToAsciiString(binaryData, 309, 15).ToUpper().Trim(); this.LastPasswordChangeDate = Converters.DTSTimeStampToDateTime(binaryData, 46); this.PasswordExpirationDate = Converters.DTSTimeStampToDateTime(binaryData, 60); this.UserExpirationDate = Converters.DTSTimeStampToDateTime(binaryData, 676); this.Status = Converters.EbcdicToAsciiString(binaryData, 36, 10).ToUpper().Trim(); this.LastSignonDateTime = Converters.TimestampToDateTime(Converters.EbcdicToAsciiString(binaryData, 18, 13).ToUpper().Trim()); this.BlockPasswordChange = Converters.EbcdicToAsciiString(binaryData, 661, 10).ToUpper().Trim(); this.Owner = Converters.EbcdicToAsciiString(binaryData, 108, 10).ToUpper().Trim(); this.UserClass = Converters.EbcdicToAsciiString(binaryData, 73, 10).ToUpper().Trim(); this.GroupAuthority = Converters.EbcdicToAsciiString(binaryData, 118, 10).ToUpper().Trim(); this.GroupProfile = Converters.EbcdicToAsciiString(binaryData, 98, 10).ToUpper().Trim(); string specialAuthorities = Converters.EbcdicToAsciiString(binaryData, 83, 15).ToUpper().PadRight(8); this.SpecialAuthorities = (specialAuthorities[0] == '1' ? "*ALLOBJ " : "") + (specialAuthorities[1] == '1' ? "*SECADM " : "") + (specialAuthorities[2] == '1' ? "*JOBCTL " : "") + (specialAuthorities[3] == '1' ? "*SPLCTL " : "") + (specialAuthorities[4] == '1' ? "*SAVSYS " : "") + (specialAuthorities[5] == '1' ? "*SERVICE " : "") + (specialAuthorities[6] == '1' ? "*AUDIT " : "") + (specialAuthorities[7] == '1' ? "*IOSYSCFG" : ""); this.PasswordExpirationInterval = Converters.BigEndianToUInt32(binaryData, 56); this.PasswordExpirationDays = Converters.BigEndianToUInt32(binaryData, 68); this.LastPasswordChangeDate = Converters.DTSTimeStampToDateTime(binaryData, 46); this.PasswordExpirationDate = Converters.DTSTimeStampToDateTime(binaryData, 60); //TODO: Implementation of other fields }
//----------------------------------------------------------------------- // Private methods //----------------------------------------------------------------------- /// <summary>Connects to the Sign-on Verify server</summary> private void ConnectToSignonVerifyServer() { // Establish authentication channel this.socketConnectorSignonVerify = new SocketConnector(this.serverName, this.useSSL ? TcpPortSignonVerifySSL : TcpPortSignonVerify, this.useSSL, this.ignoreSelfSignedCertificates); // Default current seed information this.clientSeed = 0; this.serverSeed = 0; // Exchange random seeds ulong clientSeed = (ulong)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; BigEndianMemoryStream outputStream = new BigEndianMemoryStream(); //// outputStream.WriteInt(52); // length outputStream.WriteShort(0); // Header ID (0) outputStream.WriteShort(ServerIDSignonVerify); // Server ID outputStream.WriteInt(0); // CS instance outputStream.WriteInt(0); // Correlation ID outputStream.WriteShort(0); // Template length outputStream.WriteShort(0x7003); // ReqReP ID outputStream.WriteInt(10); // Client version LL outputStream.WriteShort(0x1101); // Client version CP outputStream.WriteInt(1); // Client version outputStream.WriteInt(8); // Client data stream level LL outputStream.WriteShort(0x1102); // Client datastream level CP outputStream.WriteShort(2); // Client datastream level outputStream.WriteInt(14); // Client seed LL outputStream.WriteShort(0x1103); // Client seed CP outputStream.WriteLong(clientSeed); // Client seed this.socketConnectorSignonVerify.Write(outputStream.ToArray()); // Retrieve server response byte[] response = this.socketConnectorSignonVerify.Read(); BigEndianMemoryStream inputStream = new BigEndianMemoryStream(); inputStream.Write(response, 0, response.Length); /* * The response comes in the following format: * * Offset (byte) Information Length (byte) * ----------------------------------------------------------- * Fixed header * ----------------------------------------------------------- * 0 Response length 4 * 4 (Reserved) 16 * 20 Result code 4 * 24 (Dynamic fields) see below * ----------------------------------------------------------- * Dynamic fields (Offset is dynamic) * ----------------------------------------------------------- * 0 Field length 4 * 4 Field code 2 * 6 Field data (Field length - 6) */ // Read fixed header inputStream.Position = 0; uint responseLength = inputStream.ReadInt(); if (responseLength < 20) { throw new System.InvalidOperationException("Seeds exchange failed. Bad response length."); } inputStream.Position += 16; uint resultCode = inputStream.ReadInt(); if (resultCode != 0) { throw new System.InvalidOperationException("Seeds exchange failed. Bad return code."); } while (inputStream.Position < responseLength) { uint dynamicFieldLength = inputStream.ReadInt(); ushort dynamicFieldCode = inputStream.ReadShort(); byte[] dynamicFieldData = new byte[dynamicFieldLength - 6]; inputStream.Read(dynamicFieldData, 0, (int)dynamicFieldLength - 6); switch (dynamicFieldCode) { case 0x1101: // Server Version this.serverVersion = Converters.BigEndianToUInt32(dynamicFieldData); break; case 0x1102: // Server Level this.serverLevel = Converters.BigEndianToUInt16(dynamicFieldData); break; case 0x1103: // Server Seed this.serverSeed = Converters.BigEndianToUInt64(dynamicFieldData); break; case 0x1119: // Password Level this.passwordLevel = Converters.BigEndianToUInt8(dynamicFieldData); break; case 0x111F: // Job Name this.jobName = Converters.EbcdicToAsciiString(dynamicFieldData, 4); break; default: break; } } Debug.WriteLine("Seeds were exchanged, return code is 0x" + resultCode.ToString("X8")); this.clientSeed = clientSeed; }
/// <summary>Authenticates the user to the remote command server</summary> private void AuthenticateToRemoteCommandServer() { // Establish command channel if (this.socketConnectorRemoteCommand == null) { throw new System.InvalidOperationException("Operation failed, connection not established."); } BigEndianMemoryStream outputStream = new BigEndianMemoryStream(); byte[] encryptedPassword; byte[] userID = Converters.AsciiToEbcdic(this.userName.ToUpper().PadRight(10)); encryptedPassword = this.passwordLevel < 2 ? Encryption.EncryptPasswordDES(this.userName, this.password, this.serverSeed, this.clientSeed) : Encryption.EncryptPasswordSHA1(this.userName, this.password, this.serverSeed, this.clientSeed); outputStream.WriteByte(2); // Client attributes (2: return job information) outputStream.WriteByte(0); // Server attributes outputStream.WriteShort(ServerIDRemoteCommand); // Server ID outputStream.WriteInt(0); // CS instance outputStream.WriteInt(0); // Correlation ID outputStream.WriteShort(2); // Template length outputStream.WriteShort(0x7002); // ReqReP ID outputStream.WriteByte((byte)(this.passwordLevel < 2 ? 1 : 3)); // Password encryption type outputStream.WriteByte(1); // Send reply outputStream.WriteInt(6 + (uint)encryptedPassword.Length); // Password LL outputStream.WriteShort(0x1105); // Password CP. 0x1115 is other. outputStream.Write(encryptedPassword, 0, encryptedPassword.Length); // Password outputStream.WriteInt(16); // User ID LL outputStream.WriteShort(0x1104); // User ID CP outputStream.Write(userID, 0, userID.Length); // UserID this.socketConnectorRemoteCommand.Write(outputStream.ToArray()); // Retrieve server response byte[] response = this.socketConnectorRemoteCommand.Read(); BigEndianMemoryStream inputStream = new BigEndianMemoryStream(); inputStream.Write(response, 0, response.Length); /* * The response comes in the following format: * * Offset (byte) Information Length (byte) * ----------------------------------------------------------- * Fixed header * ----------------------------------------------------------- * 0 Response length 4 * 4 (Reserved) 16 * 20 Result code 4 * 24 (Dynamic fields) see below * ----------------------------------------------------------- * Dynamic fields (Offset is dynamic) * ----------------------------------------------------------- * 0 Field length 4 * 4 Field code 2 * 6 Field data (Field length - 6) */ // Read fixed header inputStream.Position = 0; uint responseLength = inputStream.ReadInt(); if (responseLength < 20) { throw new System.InvalidOperationException("Authentication failed. Bad response length."); } inputStream.Position += 16; uint resultCode = inputStream.ReadInt(); if (resultCode != 0) { if ((resultCode & 0xFFFF0000) == 0x00010000) { throw new System.InvalidOperationException("Authentication failed. Error on request data."); } if ((resultCode & 0xFFFF0000) == 0x00040000) { throw new System.InvalidOperationException("Authentication failed. General security error, function not performed."); } if ((resultCode & 0xFFFF0000) == 0x00060000) { throw new System.InvalidOperationException("Authentication failed. Authentication token error."); } switch (resultCode) { case 0x00020001: throw new System.InvalidOperationException("Authentication failed. User ID unknown."); case 0x00020002: throw new System.InvalidOperationException("Authentication failed. User ID locked."); case 0x00020003: throw new System.InvalidOperationException("Authentication failed. User ID doesn't match the authentication token."); case 0x0003000B: throw new System.InvalidOperationException("Authentication failed. Password incorrect."); case 0x0003000C: throw new System.InvalidOperationException("Authentication failed. Password incorrect. User profile will be revoked on next invalid password or passphrase."); case 0x0003000D: throw new System.InvalidOperationException("Authentication failed. Password is expired."); case 0x0003000E: throw new System.InvalidOperationException("Authentication failed. Pre-V2R2 encrypted password."); case 0x00030010: throw new System.InvalidOperationException("Authentication failed. Password is *NONE."); default: throw new System.InvalidOperationException("Authentication failed. Return code 0x" + resultCode.ToString("X8")); } } while (inputStream.Position < responseLength) { uint dynamicFieldLength = inputStream.ReadInt(); ushort dynamicFieldCode = inputStream.ReadShort(); byte[] dynamicFieldData = new byte[dynamicFieldLength - 6]; inputStream.Read(dynamicFieldData, 0, (int)dynamicFieldLength - 6); switch (dynamicFieldCode) { case 0x111F: // Server Version this.jobName = Converters.EbcdicToAsciiString(dynamicFieldData, 4); break; default: break; } } Debug.WriteLine("User authenticated to remote command server."); }
/// <summary>Retrieves server information</summary> private void RetrieveRemoteCommandServerInformation() { // Establish command channel if (this.socketConnectorRemoteCommand == null) { throw new System.InvalidOperationException("Operation failed, connection not established."); } BigEndianMemoryStream outputStream = new BigEndianMemoryStream(); outputStream.WriteShort(0); // Header ID outputStream.WriteShort(ServerIDRemoteCommand); // Server ID outputStream.WriteInt(0); // CS instance outputStream.WriteInt(0); // Correlation ID outputStream.WriteShort(14); // Template length outputStream.WriteShort(0x1001); // ReqReP ID outputStream.WriteInt(1200); // Operation is CCSID outputStream.Write(Converters.AsciiToEbcdic("2924"), 0, 4); // NLV value (default = 2924 = English) outputStream.WriteInt(1); // Client version outputStream.WriteShort(0); // Client datastream level this.socketConnectorRemoteCommand.Write(outputStream.ToArray()); // Retrieve server response byte[] response = this.socketConnectorRemoteCommand.Read(); BigEndianMemoryStream inputStream = new BigEndianMemoryStream(); inputStream.Write(response, 0, response.Length); /* * The response comes in the following format: * * Offset (byte) Information Length (byte) * ----------------------------------------------------------- * Fixed header * ----------------------------------------------------------- * 0 Response length 4 * 4 (Reserved) 16 * 20 Result code 2 * 24 CCSID 4 * 28 NLV (in EBCDIC) 4 * 32 (reserved) 4 * 36 Datastream level 2 */ // Read fixed header inputStream.Position = 0; uint responseLength = inputStream.ReadInt(); if (responseLength < 20) { throw new System.InvalidOperationException("Seeds exchange failed. Bad response length."); } inputStream.Position += 16; uint resultCode = inputStream.ReadShort(); // We ignore the same return codes that JTOPEN/JT400 ignores if (resultCode != 0x0100 && // User with *LMTCPB = *YES resultCode != 0x0104 && // Invalid CCSID. resultCode != 0x0105 && // Invalid NLV, default to primary NLV: resultCode != 0x0106 && // NLV not installed, default to primary NLV: // The NLV may not be supported or it may not be installed on the system. resultCode != 0x0107 && // Error retrieving product information. Can't validate NLV. resultCode != 0x0108 && // Error trying to add NLV library to system library list: // One possible reason for failure is the user may not be authorized to CHGSYSLIBL command. resultCode != 0) { throw new System.InvalidOperationException("Invalid operation, failed to retrieve Remote Command server attributes."); } this.serverCCSID = inputStream.ReadInt(); byte[] nlv = new byte[4]; inputStream.Read(nlv, 0, 4); this.serverNLV = Converters.EbcdicToAsciiString(nlv); inputStream.Position += 4; this.serverDatastreamLevel = inputStream.ReadShort(); Debug.WriteLine("Remote Command server attributes retrieved, return code is 0x" + resultCode.ToString("X8")); }
//----------------------------------------------------------------------- // Class methods //----------------------------------------------------------------------- public string[] GetUsersList() { /* * https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/apis/qgyolaus.htm * * Required Parameter Group: * * 1 Receiver variable Output Char(*) - 120000B * 2 Length of receiver variable Input Binary(4) - 120000 * 3 List information Output Char(80) * 4 Number of records to return Input Binary(4) - "9999" * 5 Format name Input Char(8) - "AUTU0100" * 6 Selection criteria Input Char(10) - "*ALL" * 7 Group profile name Input Char(10) - "*NONE" * 8 Error code I/O Char(*) */ ProgramCallParameters qgyolausCallParameters = new ProgramCallParameters(8) { [0] = new ProgramCallParameter( ProgramCallParameter.ParameterTypeOutput, null, 120000), [1] = new ProgramCallParameter( ProgramCallParameter.ParameterTypeInput, Converters.UInt32ToBigEndian(120000)), [2] = new ProgramCallParameter( ProgramCallParameter.ParameterTypeOutput, null, 80), [3] = new ProgramCallParameter( ProgramCallParameter.ParameterTypeInput, Converters.UInt32ToBigEndian(9999)), [4] = new ProgramCallParameter( ProgramCallParameter.ParameterTypeInput, Converters.AsciiToEbcdic("AUTU0100")), [5] = new ProgramCallParameter( ProgramCallParameter.ParameterTypeInput, Converters.AsciiToEbcdic("*ALL ")), [6] = new ProgramCallParameter( ProgramCallParameter.ParameterTypeInput, Converters.AsciiToEbcdic("*NONE ")), [7] = new ProgramCallParameter( ProgramCallParameter.ParameterTypeInputOutput, null, 500) }; CallMessages qgyolausCallMessages = new CallMessages(); if (CallProgram("QGYOLAUS", "QSYS", ref qgyolausCallParameters, ref qgyolausCallMessages) != 0) { foreach (CallMessage outputMessage in qgyolausCallMessages) { Debug.WriteLine(outputMessage.MessageText); } throw new System.InvalidOperationException("The method GetUserList failed. Check debug information."); } uint numEntries = Converters.BigEndianToUInt32(qgyolausCallParameters[2].ParameterValue, 0, 4); if (numEntries <= 0) { return(null); } string[] userList = new string[numEntries]; for (int i = 0; i < numEntries; i++) { userList[i] = Converters.EbcdicToAsciiString(qgyolausCallParameters[0].ParameterValue, (uint)i * 12, 10); } return(userList); }