/// <summary>Receives data from an open stream.</summary> /// <returns>Retrieved data</returns> public byte[] Read() { byte[] responseLength = new byte[4]; using (BigEndianMemoryStream readStream = new BigEndianMemoryStream()) { if (!this.bufferedStream.CanRead) { return(null); } this.bufferedStream.Read(responseLength, 0, 4); readStream.Write(responseLength, 0, 4); Debug.WriteLine("Packet length: " + BitConverter.ToString(responseLength)); long bytesToRead = Converters.BigEndianToUInt32(responseLength); if (bytesToRead == 0x00000000 || bytesToRead == 0x40404040) { return(null); } bytesToRead -= 4; while (bytesToRead > 0) { byte[] buffer = new byte[(long)byte.MaxValue < bytesToRead ? byte.MaxValue : (int)bytesToRead]; if (!this.bufferedStream.CanRead) { Thread.Sleep(100); } this.bufferedStream.Read(buffer, 0, buffer.Length); readStream.Write(buffer, 0, buffer.Length); bytesToRead -= buffer.Length; } return(readStream.ToArray()); } }
//----------------------------------------------------------------------- // 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 signon server</summary> private void AuthenticateToSignonVerifyServer() { 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); if (this.serverLevel >= 5) { outputStream.WriteShort(0); // Header ID (0) } outputStream.WriteShort(ServerIDSignonVerify); // Server ID outputStream.WriteInt(0); // CS instance outputStream.WriteInt(0); // Correlation ID outputStream.WriteShort(0x0001); // Template length outputStream.WriteShort(0x7004); // ReqReP ID outputStream.WriteByte((byte)(this.passwordLevel < 2 ? 1 : 3)); // Password encryption type outputStream.WriteInt(10); // Client CCSID LL outputStream.WriteShort(0x1113); // Client CCSID CP outputStream.WriteInt(1200); // Client CCSID (big endian UTF-16) 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 if (this.serverLevel >= 5) { outputStream.WriteInt(7); // Return error messages LL outputStream.WriteShort(0x1128); // Return error messages CP outputStream.WriteByte(1); // Return error messages } 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("Authentication failed. Bad response length."); } inputStream.Position += 16; uint resultCode = inputStream.ReadInt(); if (resultCode != 0) { switch (resultCode) { case 0x00020001: throw new System.InvalidOperationException("Authentication failed. Unknown username."); case 0x0003000B: throw new System.InvalidOperationException("Authentication failed. Wrong password."); case 0x0003000C: throw new System.InvalidOperationException("Authentication failed. Wrong password. Profile will be disabled on the next invalid password."); case 0x0003000D: throw new System.InvalidOperationException("Authentication failed. Password is expired."); 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 0x1114: // Server Version this.serverCCSID = Converters.BigEndianToUInt32(dynamicFieldData); break; default: break; } } Debug.WriteLine("User authenticated to signon server."); }
/// <summary>Reads an int value from the stream</summary> /// <returns>A int value</returns> public uint ReadInt() { byte[] intArray = new byte[4]; this.Read(intArray, 0, 4); return(Converters.BigEndianToUInt32(intArray)); }
//----------------------------------------------------------------------- // 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); }