sd.go 3.2 KB

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