sid.go 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. package mstypes
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "fmt"
  6. )
  7. // RPCSID implements https://msdn.microsoft.com/en-us/library/cc230364.aspx
  8. type RPCSID struct {
  9. Revision uint8 // An 8-bit unsigned integer that specifies the revision level of the SID. This value MUST be set to 0x01.
  10. SubAuthorityCount uint8 // An 8-bit unsigned integer that specifies the number of elements in the SubAuthority array. The maximum number of elements allowed is 15.
  11. IdentifierAuthority [6]byte // An RPC_SID_IDENTIFIER_AUTHORITY structure that indicates the authority under which the SID was created. It describes the entity that created the SID. The Identifier Authority value {0,0,0,0,0,5} denotes SIDs created by the NT SID authority.
  12. SubAuthority []uint32 `ndr:"conformant"` // A variable length array of unsigned 32-bit integers that uniquely identifies a principal relative to the IdentifierAuthority. Its length is determined by SubAuthorityCount.
  13. }
  14. // String returns the string representation of the RPC_SID.
  15. func (s *RPCSID) String() string {
  16. var str string
  17. b := append(make([]byte, 2, 2), s.IdentifierAuthority[:]...)
  18. // For a strange reason this is read big endian: https://msdn.microsoft.com/en-us/library/dd302645.aspx
  19. i := binary.BigEndian.Uint64(b)
  20. if i >= 4294967296 {
  21. str = fmt.Sprintf("S-1-0x%s", hex.EncodeToString(s.IdentifierAuthority[:]))
  22. } else {
  23. str = fmt.Sprintf("S-1-%d", i)
  24. }
  25. for _, sub := range s.SubAuthority {
  26. str = fmt.Sprintf("%s-%d", str, sub)
  27. }
  28. return str
  29. }