fix(windows): launch commands on the interactive desktop
This commit is contained in:
1 parent
30e7bb8545
commit
8d28925e44
20 files changed
+1036
-28
No files matched your search
@@ -0,0 +1,98 @@
|
||||
//go:build windows
|
||||
|
||||
package windows
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
winapi "golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
var (
|
||||
desktopUser32 = winapi.NewLazySystemDLL("user32.dll")
|
||||
procProcessWindowStation = desktopUser32.NewProc("GetProcessWindowStation")
|
||||
procThreadDesktop = desktopUser32.NewProc("GetThreadDesktop")
|
||||
procUserObjectInformation = desktopUser32.NewProc("GetUserObjectInformationW")
|
||||
procOpenWindowStation = desktopUser32.NewProc("OpenWindowStationW")
|
||||
procCloseWindowStation = desktopUser32.NewProc("CloseWindowStation")
|
||||
procOpenDesktop = desktopUser32.NewProc("OpenDesktopW")
|
||||
procCloseDesktop = desktopUser32.NewProc("CloseDesktop")
|
||||
)
|
||||
|
||||
func desktopPointer(desktop string) *uint16 {
|
||||
if desktop == "" {
|
||||
return nil
|
||||
}
|
||||
value, _ := winapi.UTF16PtrFromString(desktop)
|
||||
return value
|
||||
}
|
||||
|
||||
func userObjectName(handle uintptr) (string, error) {
|
||||
var name [256]uint16
|
||||
var needed uint32
|
||||
result, _, err := procUserObjectInformation.Call(handle, 2 /* UOI_NAME */, uintptr(unsafe.Pointer(&name[0])), unsafe.Sizeof(name), uintptr(unsafe.Pointer(&needed)))
|
||||
if result == 0 {
|
||||
return "", fmt.Errorf("read window station/desktop name: %w", err)
|
||||
}
|
||||
if needed < 2 || needed > uint32(unsafe.Sizeof(name)) {
|
||||
return "", fmt.Errorf("invalid window station/desktop name size %d", needed)
|
||||
}
|
||||
return winapi.UTF16ToString(name[:]), nil
|
||||
}
|
||||
|
||||
// Check access in the selected-session helper, not Session 0's service. Never
|
||||
// broaden desktop ACLs or switch to Winlogon/the secure input desktop. A locked
|
||||
// session can open Default now and display its UI when the user unlocks.
|
||||
func verifyCurrentInteractiveDesktop(sessionID uint32) (string, error) {
|
||||
if sessionID == 0 {
|
||||
return "", nil
|
||||
}
|
||||
station, _, err := procProcessWindowStation.Call()
|
||||
if station == 0 {
|
||||
return "", fmt.Errorf("get process window station: %w", err)
|
||||
}
|
||||
desktop, _, err := procThreadDesktop.Call(uintptr(winapi.GetCurrentThreadId()))
|
||||
if desktop == 0 {
|
||||
return "", fmt.Errorf("get thread desktop: %w", err)
|
||||
}
|
||||
stationName, err := userObjectName(station)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
desktopName, err := userObjectName(desktop)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
path := stationName + `\` + desktopName
|
||||
if err := validateInteractiveDesktop(path); err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Open non-inheritable handles to check existing logon-token access. The
|
||||
// handles returned by GetProcessWindowStation/GetThreadDesktop are borrowed.
|
||||
stationHandle, _, err := procOpenWindowStation.Call(uintptr(unsafe.Pointer(desktopPointer("winsta0"))), 0,
|
||||
0x0002|0x0020 /* READATTRIBUTES | ACCESSGLOBALATOMS */)
|
||||
if stationHandle == 0 {
|
||||
return "", fmt.Errorf("interactive window station access: %w", err)
|
||||
}
|
||||
defer procCloseWindowStation.Call(stationHandle)
|
||||
desktopHandle, _, err := procOpenDesktop.Call(uintptr(unsafe.Pointer(desktopPointer("default"))), 0, 0,
|
||||
0x0001|0x0002|0x0004|0x0080 /* READOBJECTS | CREATEWINDOW | CREATEMENU | WRITEOBJECTS */)
|
||||
if desktopHandle == 0 {
|
||||
return "", fmt.Errorf("interactive desktop access: %w", err)
|
||||
}
|
||||
defer procCloseDesktop.Call(desktopHandle)
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func commandStartup(stdin, stdout, stderr winapi.Handle, desktop string) (winapi.StartupInfoEx, uint32) {
|
||||
startup := winapi.StartupInfoEx{StartupInfo: winapi.StartupInfo{
|
||||
Cb: uint32(unsafe.Sizeof(winapi.StartupInfoEx{})), Desktop: desktopPointer(desktop),
|
||||
Flags: winapi.STARTF_USESTDHANDLES | winapi.STARTF_USESHOWWINDOW, ShowWindow: winapi.SW_SHOWNORMAL,
|
||||
StdInput: stdin, StdOutput: stdout, StdErr: stderr,
|
||||
}}
|
||||
// A private headless console does not ask GUI descendants to hide. Keep
|
||||
// this distinct from DETACHED_PROCESS so the TERM helper can still attach.
|
||||
flags := uint32(winapi.CREATE_NO_WINDOW | winapi.CREATE_SUSPENDED | winapi.CREATE_UNICODE_ENVIRONMENT | winapi.EXTENDED_STARTUPINFO_PRESENT)
|
||||
return startup, flags
|
||||
}
|
||||
Reference in new issue
Block a user