sd.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //go:build windows
  2. // +build windows
  3. package winio
  4. import (
  5. "errors"
  6. "syscall"
  7. "unsafe"
  8. "golang.org/x/sys/windows"
  9. )
  10. //sys lookupAccountName(systemName *uint16, accountName string, sid *byte, sidSize *uint32, refDomain *uint16, refDomainSize *uint32, sidNameUse *uint32) (err error) = advapi32.LookupAccountNameW
  11. //sys lookupAccountSid(systemName *uint16, sid *byte, name *uint16, nameSize *uint32, refDomain *uint16, refDomainSize *uint32, sidNameUse *uint32) (err error) = advapi32.LookupAccountSidW
  12. //sys convertSidToStringSid(sid *byte, str **uint16) (err error) = advapi32.ConvertSidToStringSidW
  13. //sys convertStringSidToSid(str *uint16, sid **byte) (err error) = advapi32.ConvertStringSidToSidW
  14. //sys convertStringSecurityDescriptorToSecurityDescriptor(str string, revision uint32, sd *uintptr, size *uint32) (err error) = advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW
  15. //sys convertSecurityDescriptorToStringSecurityDescriptor(sd *byte, revision uint32, secInfo uint32, sddl **uint16, sddlSize *uint32) (err error) = advapi32.ConvertSecurityDescriptorToStringSecurityDescriptorW
  16. //sys localFree(mem uintptr) = LocalFree
  17. //sys getSecurityDescriptorLength(sd uintptr) (len uint32) = advapi32.GetSecurityDescriptorLength
  18. type AccountLookupError struct {
  19. Name string
  20. Err error
  21. }
  22. func (e *AccountLookupError) Error() string {
  23. if e.Name == "" {
  24. return "lookup account: empty account name specified"
  25. }
  26. var s string
  27. switch {
  28. case errors.Is(e.Err, windows.ERROR_INVALID_SID):
  29. s = "the security ID structure is invalid"
  30. case errors.Is(e.Err, windows.ERROR_NONE_MAPPED):
  31. s = "not found"
  32. default:
  33. s = e.Err.Error()
  34. }
  35. return "lookup account " + e.Name + ": " + s
  36. }
  37. func (e *AccountLookupError) Unwrap() error { return e.Err }
  38. type SddlConversionError struct {
  39. Sddl string
  40. Err error
  41. }
  42. func (e *SddlConversionError) Error() string {
  43. return "convert " + e.Sddl + ": " + e.Err.Error()
  44. }
  45. func (e *SddlConversionError) Unwrap() error { return e.Err }
  46. // LookupSidByName looks up the SID of an account by name
  47. //
  48. //revive:disable-next-line:var-naming SID, not Sid
  49. func LookupSidByName(name string) (sid string, err error) {
  50. if name == "" {
  51. return "", &AccountLookupError{name, windows.ERROR_NONE_MAPPED}
  52. }
  53. var sidSize, sidNameUse, refDomainSize uint32
  54. err = lookupAccountName(nil, name, nil, &sidSize, nil, &refDomainSize, &sidNameUse)
  55. if err != nil && err != syscall.ERROR_INSUFFICIENT_BUFFER { //nolint:errorlint // err is Errno
  56. return "", &AccountLookupError{name, err}
  57. }
  58. sidBuffer := make([]byte, sidSize)
  59. refDomainBuffer := make([]uint16, refDomainSize)
  60. err = lookupAccountName(nil, name, &sidBuffer[0], &sidSize, &refDomainBuffer[0], &refDomainSize, &sidNameUse)
  61. if err != nil {
  62. return "", &AccountLookupError{name, err}
  63. }
  64. var strBuffer *uint16
  65. err = convertSidToStringSid(&sidBuffer[0], &strBuffer)
  66. if err != nil {
  67. return "", &AccountLookupError{name, err}
  68. }
  69. sid = syscall.UTF16ToString((*[0xffff]uint16)(unsafe.Pointer(strBuffer))[:])
  70. localFree(uintptr(unsafe.Pointer(strBuffer)))
  71. return sid, nil
  72. }
  73. // LookupNameBySid looks up the name of an account by SID
  74. //
  75. //revive:disable-next-line:var-naming SID, not Sid
  76. func LookupNameBySid(sid string) (name string, err error) {
  77. if sid == "" {
  78. return "", &AccountLookupError{sid, windows.ERROR_NONE_MAPPED}
  79. }
  80. sidBuffer, err := windows.UTF16PtrFromString(sid)
  81. if err != nil {
  82. return "", &AccountLookupError{sid, err}
  83. }
  84. var sidPtr *byte
  85. if err = convertStringSidToSid(sidBuffer, &sidPtr); err != nil {
  86. return "", &AccountLookupError{sid, err}
  87. }
  88. defer localFree(uintptr(unsafe.Pointer(sidPtr)))
  89. var nameSize, refDomainSize, sidNameUse uint32
  90. err = lookupAccountSid(nil, sidPtr, nil, &nameSize, nil, &refDomainSize, &sidNameUse)
  91. if err != nil && err != windows.ERROR_INSUFFICIENT_BUFFER { //nolint:errorlint // err is Errno
  92. return "", &AccountLookupError{sid, err}
  93. }
  94. nameBuffer := make([]uint16, nameSize)
  95. refDomainBuffer := make([]uint16, refDomainSize)
  96. err = lookupAccountSid(nil, sidPtr, &nameBuffer[0], &nameSize, &refDomainBuffer[0], &refDomainSize, &sidNameUse)
  97. if err != nil {
  98. return "", &AccountLookupError{sid, err}
  99. }
  100. name = windows.UTF16ToString(nameBuffer)
  101. return name, nil
  102. }
  103. func SddlToSecurityDescriptor(sddl string) ([]byte, error) {
  104. var sdBuffer uintptr
  105. err := convertStringSecurityDescriptorToSecurityDescriptor(sddl, 1, &sdBuffer, nil)
  106. if err != nil {
  107. return nil, &SddlConversionError{sddl, err}
  108. }
  109. defer localFree(sdBuffer)
  110. sd := make([]byte, getSecurityDescriptorLength(sdBuffer))
  111. copy(sd, (*[0xffff]byte)(unsafe.Pointer(sdBuffer))[:len(sd)])
  112. return sd, nil
  113. }
  114. func SecurityDescriptorToSddl(sd []byte) (string, error) {
  115. var sddl *uint16
  116. // The returned string length seems to include an arbitrary number of terminating NULs.
  117. // Don't use it.
  118. err := convertSecurityDescriptorToStringSecurityDescriptor(&sd[0], 1, 0xff, &sddl, nil)
  119. if err != nil {
  120. return "", err
  121. }
  122. defer localFree(uintptr(unsafe.Pointer(sddl)))
  123. return syscall.UTF16ToString((*[0xffff]uint16)(unsafe.Pointer(sddl))[:]), nil
  124. }