device_info.go 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package pac
  2. import (
  3. "fmt"
  4. "gopkg.in/jcmturner/gokrb5.v2/mstypes"
  5. "gopkg.in/jcmturner/gokrb5.v2/ndr"
  6. )
  7. // DeviceInfo implements https://msdn.microsoft.com/en-us/library/hh536402.aspx
  8. type DeviceInfo struct {
  9. UserID uint32 // A 32-bit unsigned integer that contains the RID of the account. If the UserId member equals 0x00000000, the first group SID in this member is the SID for this account.
  10. PrimaryGroupID uint32 // A 32-bit unsigned integer that contains the RID for the primary group to which this account belongs.
  11. AccountDomainID mstypes.RPCSID // A SID structure that contains the SID for the domain of the account.This member is used in conjunction with the UserId, and GroupIds members to create the user and group SIDs for the client.
  12. AccountGroupCount uint32 // A 32-bit unsigned integer that contains the number of groups within the account domain to which the account belongs
  13. AccountGroupIDs []mstypes.GroupMembership // A pointer to a list of GROUP_MEMBERSHIP (section 2.2.2) structures that contains the groups to which the account belongs in the account domain. The number of groups in this list MUST be equal to GroupCount.
  14. SIDCount uint32 // A 32-bit unsigned integer that contains the total number of SIDs present in the ExtraSids member.
  15. ExtraSIDs []mstypes.KerbSidAndAttributes // A pointer to a list of KERB_SID_AND_ATTRIBUTES structures that contain a list of SIDs corresponding to groups not in domains. If the UserId member equals 0x00000000, the first group SID in this member is the SID for this account.
  16. DomainGroupCount uint32 // A 32-bit unsigned integer that contains the number of domains with groups to which the account belongs.
  17. DomainGroup []mstypes.DomainGroupMembership // A pointer to a list of DOMAIN_GROUP_MEMBERSHIP structures (section 2.2.3) that contains the domains to which the account belongs to a group. The number of sets in this list MUST be equal to DomainCount.
  18. }
  19. // Unmarshal bytes into the DeviceInfo struct
  20. func (k *DeviceInfo) Unmarshal(b []byte) error {
  21. ch, _, p, err := ndr.ReadHeaders(&b)
  22. if err != nil {
  23. return fmt.Errorf("Error parsing byte stream headers: %v", err)
  24. }
  25. e := &ch.Endianness
  26. //The next 4 bytes are an RPC unique pointer referent. We just skip these
  27. p += 4
  28. k.UserID = ndr.ReadUint32(&b, &p, e)
  29. k.PrimaryGroupID = ndr.ReadUint32(&b, &p, e)
  30. k.AccountDomainID, err = mstypes.ReadRPCSID(&b, &p, e)
  31. if err != nil {
  32. return err
  33. }
  34. k.AccountGroupCount = ndr.ReadUint32(&b, &p, e)
  35. if k.AccountGroupCount > 0 {
  36. ag := make([]mstypes.GroupMembership, k.AccountGroupCount, k.AccountGroupCount)
  37. for i := range ag {
  38. ag[i] = mstypes.ReadGroupMembership(&b, &p, e)
  39. }
  40. k.AccountGroupIDs = ag
  41. }
  42. k.SIDCount = ndr.ReadUint32(&b, &p, e)
  43. if k.SIDCount > 0 {
  44. ac := ndr.ReadUniDimensionalConformantArrayHeader(&b, &p, e)
  45. if ac != int(k.SIDCount) {
  46. return fmt.Errorf("Error with size of ExtraSIDs list. Expected: %d, Actual: %d", k.SIDCount, ac)
  47. }
  48. es := make([]mstypes.KerbSidAndAttributes, k.SIDCount, k.SIDCount)
  49. attr := make([]uint32, k.SIDCount, k.SIDCount)
  50. ptr := make([]uint32, k.SIDCount, k.SIDCount)
  51. for i := range attr {
  52. ptr[i] = ndr.ReadUint32(&b, &p, e)
  53. attr[i] = ndr.ReadUint32(&b, &p, e)
  54. }
  55. for i := range es {
  56. if ptr[i] != 0 {
  57. s, err := mstypes.ReadRPCSID(&b, &p, e)
  58. es[i] = mstypes.KerbSidAndAttributes{SID: s, Attributes: attr[i]}
  59. if err != nil {
  60. return ndr.Malformed{EText: fmt.Sprintf("Could not read ExtraSIDs: %v", err)}
  61. }
  62. }
  63. }
  64. k.ExtraSIDs = es
  65. }
  66. k.DomainGroupCount = ndr.ReadUint32(&b, &p, e)
  67. if k.DomainGroupCount > 0 {
  68. dg := make([]mstypes.DomainGroupMembership, k.DomainGroupCount, k.DomainGroupCount)
  69. for i := range dg {
  70. dg[i], _ = mstypes.ReadDomainGroupMembership(&b, &p, e)
  71. }
  72. k.DomainGroup = dg
  73. }
  74. //Check that there is only zero padding left
  75. if len(b) >= p {
  76. for _, v := range b[p:] {
  77. if v != 0 {
  78. return ndr.Malformed{EText: "Non-zero padding left over at end of data stream"}
  79. }
  80. }
  81. }
  82. return nil
  83. }