You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sd.go 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // +build windows
  2. package winio
  3. import (
  4. "syscall"
  5. "unsafe"
  6. )
  7. //sys lookupAccountName(systemName *uint16, accountName string, sid *byte, sidSize *uint32, refDomain *uint16, refDomainSize *uint32, sidNameUse *uint32) (err error) = advapi32.LookupAccountNameW
  8. //sys convertSidToStringSid(sid *byte, str **uint16) (err error) = advapi32.ConvertSidToStringSidW
  9. //sys convertStringSecurityDescriptorToSecurityDescriptor(str string, revision uint32, sd *uintptr, size *uint32) (err error) = advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW
  10. //sys convertSecurityDescriptorToStringSecurityDescriptor(sd *byte, revision uint32, secInfo uint32, sddl **uint16, sddlSize *uint32) (err error) = advapi32.ConvertSecurityDescriptorToStringSecurityDescriptorW
  11. //sys localFree(mem uintptr) = LocalFree
  12. //sys getSecurityDescriptorLength(sd uintptr) (len uint32) = advapi32.GetSecurityDescriptorLength
  13. const (
  14. cERROR_NONE_MAPPED = syscall.Errno(1332)
  15. )
  16. type AccountLookupError struct {
  17. Name string
  18. Err error
  19. }
  20. func (e *AccountLookupError) Error() string {
  21. if e.Name == "" {
  22. return "lookup account: empty account name specified"
  23. }
  24. var s string
  25. switch e.Err {
  26. case cERROR_NONE_MAPPED:
  27. s = "not found"
  28. default:
  29. s = e.Err.Error()
  30. }
  31. return "lookup account " + e.Name + ": " + s
  32. }
  33. type SddlConversionError struct {
  34. Sddl string
  35. Err error
  36. }
  37. func (e *SddlConversionError) Error() string {
  38. return "convert " + e.Sddl + ": " + e.Err.Error()
  39. }
  40. // LookupSidByName looks up the SID of an account by name
  41. func LookupSidByName(name string) (sid string, err error) {
  42. if name == "" {
  43. return "", &AccountLookupError{name, cERROR_NONE_MAPPED}
  44. }
  45. var sidSize, sidNameUse, refDomainSize uint32
  46. err = lookupAccountName(nil, name, nil, &sidSize, nil, &refDomainSize, &sidNameUse)
  47. if err != nil && err != syscall.ERROR_INSUFFICIENT_BUFFER {
  48. return "", &AccountLookupError{name, err}
  49. }
  50. sidBuffer := make([]byte, sidSize)
  51. refDomainBuffer := make([]uint16, refDomainSize)
  52. err = lookupAccountName(nil, name, &sidBuffer[0], &sidSize, &refDomainBuffer[0], &refDomainSize, &sidNameUse)
  53. if err != nil {
  54. return "", &AccountLookupError{name, err}
  55. }
  56. var strBuffer *uint16
  57. err = convertSidToStringSid(&sidBuffer[0], &strBuffer)
  58. if err != nil {
  59. return "", &AccountLookupError{name, err}
  60. }
  61. sid = syscall.UTF16ToString((*[0xffff]uint16)(unsafe.Pointer(strBuffer))[:])
  62. localFree(uintptr(unsafe.Pointer(strBuffer)))
  63. return sid, nil
  64. }
  65. func SddlToSecurityDescriptor(sddl string) ([]byte, error) {
  66. var sdBuffer uintptr
  67. err := convertStringSecurityDescriptorToSecurityDescriptor(sddl, 1, &sdBuffer, nil)
  68. if err != nil {
  69. return nil, &SddlConversionError{sddl, err}
  70. }
  71. defer localFree(sdBuffer)
  72. sd := make([]byte, getSecurityDescriptorLength(sdBuffer))
  73. copy(sd, (*[0xffff]byte)(unsafe.Pointer(sdBuffer))[:len(sd)])
  74. return sd, nil
  75. }
  76. func SecurityDescriptorToSddl(sd []byte) (string, error) {
  77. var sddl *uint16
  78. // The returned string length seems to including an aribtrary number of terminating NULs.
  79. // Don't use it.
  80. err := convertSecurityDescriptorToStringSecurityDescriptor(&sd[0], 1, 0xff, &sddl, nil)
  81. if err != nil {
  82. return "", err
  83. }
  84. defer localFree(uintptr(unsafe.Pointer(sddl)))
  85. return syscall.UTF16ToString((*[0xffff]uint16)(unsafe.Pointer(sddl))[:]), nil
  86. }