github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/security_windows.go (about)

     1  // based on github.com/hectane/go-acl
     2  
     3  /*
     4  The MIT License (MIT)
     5  
     6  Copyright (c) 2015 Nathan Osman
     7  
     8  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
     9  
    10  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    11  
    12  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    13  */
    14  
    15  package libkb
    16  
    17  import (
    18  	"golang.org/x/sys/windows"
    19  
    20  	"syscall"
    21  	"unsafe"
    22  )
    23  
    24  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa379593.aspx
    25  const (
    26  	SE_UNKNOWN_OBJECT_TYPE = iota
    27  	SE_FILE_OBJECT
    28  	SE_SERVICE
    29  	SE_PRINTER
    30  	SE_REGISTRY_KEY
    31  	SE_LMSHARE
    32  	SE_KERNEL_OBJECT
    33  	SE_WINDOW_OBJECT
    34  	SE_DS_OBJECT
    35  	SE_DS_OBJECT_ALL
    36  	SE_PROVIDER_DEFINED_OBJECT
    37  	SE_WMIGUID_OBJECT
    38  	SE_REGISTRY_WOW64_32KEY
    39  )
    40  
    41  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa379573.aspx
    42  const (
    43  	OWNER_SECURITY_INFORMATION               = 0x00001
    44  	GROUP_SECURITY_INFORMATION               = 0x00002
    45  	DACL_SECURITY_INFORMATION                = 0x00004
    46  	SACL_SECURITY_INFORMATION                = 0x00008
    47  	LABEL_SECURITY_INFORMATION               = 0x00010
    48  	ATTRIBUTE_SECURITY_INFORMATION           = 0x00020
    49  	SCOPE_SECURITY_INFORMATION               = 0x00040
    50  	PROCESS_TRUST_LABEL_SECURITY_INFORMATION = 0x00080
    51  	BACKUP_SECURITY_INFORMATION              = 0x10000
    52  
    53  	PROTECTED_DACL_SECURITY_INFORMATION   = 0x80000000
    54  	PROTECTED_SACL_SECURITY_INFORMATION   = 0x40000000
    55  	UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000
    56  	UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000
    57  )
    58  
    59  var (
    60  	advapi32                  = windows.NewLazySystemDLL("advapi32.dll")
    61  	procGetNamedSecurityInfoW = advapi32.NewProc("GetNamedSecurityInfoW")
    62  )
    63  
    64  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa446645.aspx
    65  func GetNamedSecurityInfo(objectName string, objectType int32, secInfo uint32, owner, group **windows.SID, dacl, sacl, secDesc *windows.Handle) error {
    66  	ret, _, _ := procGetNamedSecurityInfoW.Call(
    67  		uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(objectName))),
    68  		uintptr(objectType),
    69  		uintptr(secInfo),
    70  		uintptr(unsafe.Pointer(owner)),
    71  		uintptr(unsafe.Pointer(group)),
    72  		uintptr(unsafe.Pointer(dacl)),
    73  		uintptr(unsafe.Pointer(sacl)),
    74  		uintptr(unsafe.Pointer(secDesc)),
    75  	)
    76  	if ret != 0 {
    77  		if ret == ERROR_PIPE_BUSY {
    78  			return PipeBusyError
    79  		}
    80  		return syscall.Errno(ret)
    81  	}
    82  
    83  	return nil
    84  }