From f05b1b5c25d985c59369c0a908eb5e1090c297a6 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 3 Mar 2021 23:36:27 +0100 Subject: elevate: use ole32 functions from x/sys Signed-off-by: Jason A. Donenfeld --- elevate/mksyscall.go | 8 ------ elevate/shellexecute.go | 14 +++++----- elevate/syscall_windows.go | 27 ------------------ elevate/zsyscall_windows.go | 67 --------------------------------------------- 4 files changed, 7 insertions(+), 109 deletions(-) delete mode 100644 elevate/mksyscall.go delete mode 100644 elevate/syscall_windows.go delete mode 100644 elevate/zsyscall_windows.go diff --git a/elevate/mksyscall.go b/elevate/mksyscall.go deleted file mode 100644 index fd3fab8f..00000000 --- a/elevate/mksyscall.go +++ /dev/null @@ -1,8 +0,0 @@ -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved. - */ - -package elevate - -//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zsyscall_windows.go syscall_windows.go diff --git a/elevate/shellexecute.go b/elevate/shellexecute.go index b681a8a7..2ac5581c 100644 --- a/elevate/shellexecute.go +++ b/elevate/shellexecute.go @@ -113,19 +113,19 @@ func ShellExecute(program string, arguments string, directory string, show int32 runtime.KeepAlive(explorerPath) }() - if err = coInitializeEx(0, cCOINIT_APARTMENTTHREADED); err == nil { - defer coUninitialize() + if err = windows.CoInitializeEx(0, windows.COINIT_APARTMENTTHREADED); err == nil { + defer windows.CoUninitialize() } var interfacePointer **[0xffff]uintptr - if err = coGetObject( + if err = windows.CoGetObject( windows.StringToUTF16Ptr("Elevation:Administrator!new:{3E5FC7F9-9A51-4367-9063-A120244FBEC7}"), - &cBIND_OPTS3{ - cbStruct: uint32(unsafe.Sizeof(cBIND_OPTS3{})), - dwClassContext: cCLSCTX_LOCAL_SERVER, + &windows.BIND_OPTS3{ + CbStruct: uint32(unsafe.Sizeof(windows.BIND_OPTS3{})), + ClassContext: windows.CLSCTX_LOCAL_SERVER, }, &windows.GUID{0x6EDD6D74, 0xC007, 0x4E75, [8]byte{0xB7, 0x6A, 0xE5, 0x74, 0x09, 0x95, 0xE2, 0x4C}}, - &interfacePointer, + (**uintptr)(unsafe.Pointer(&interfacePointer)), ); err != nil { return } diff --git a/elevate/syscall_windows.go b/elevate/syscall_windows.go deleted file mode 100644 index 2c820083..00000000 --- a/elevate/syscall_windows.go +++ /dev/null @@ -1,27 +0,0 @@ -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved. - */ - -package elevate - -type cBIND_OPTS3 struct { - cbStruct uint32 - grfFlags uint32 - grfMode uint32 - dwTickCountDeadline uint32 - dwTrackFlags uint32 - dwClassContext uint32 - locale uint32 - pServerInfo *uintptr - hwnd *uintptr -} - -const ( - cCLSCTX_LOCAL_SERVER = 4 - cCOINIT_APARTMENTTHREADED = 2 -) - -//sys coInitializeEx(reserved uintptr, coInit uint32) (ret error) = ole32.CoInitializeEx -//sys coUninitialize() = ole32.CoUninitialize -//sys coGetObject(name *uint16, bindOpts *cBIND_OPTS3, guid *windows.GUID, functionTable ***[0xffff]uintptr) (ret error) = ole32.CoGetObject diff --git a/elevate/zsyscall_windows.go b/elevate/zsyscall_windows.go deleted file mode 100644 index 452c8cfc..00000000 --- a/elevate/zsyscall_windows.go +++ /dev/null @@ -1,67 +0,0 @@ -// Code generated by 'go generate'; DO NOT EDIT. - -package elevate - -import ( - "syscall" - "unsafe" - - "golang.org/x/sys/windows" -) - -var _ unsafe.Pointer - -// Do the interface allocations only once for common -// Errno values. -const ( - errnoERROR_IO_PENDING = 997 -) - -var ( - errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING) - errERROR_EINVAL error = syscall.EINVAL -) - -// errnoErr returns common boxed Errno values, to prevent -// allocations at runtime. -func errnoErr(e syscall.Errno) error { - switch e { - case 0: - return errERROR_EINVAL - case errnoERROR_IO_PENDING: - return errERROR_IO_PENDING - } - // TODO: add more here, after collecting data on the common - // error values see on Windows. (perhaps when running - // all.bat?) - return e -} - -var ( - modole32 = windows.NewLazySystemDLL("ole32.dll") - - procCoGetObject = modole32.NewProc("CoGetObject") - procCoInitializeEx = modole32.NewProc("CoInitializeEx") - procCoUninitialize = modole32.NewProc("CoUninitialize") -) - -func coGetObject(name *uint16, bindOpts *cBIND_OPTS3, guid *windows.GUID, functionTable ***[0xffff]uintptr) (ret error) { - r0, _, _ := syscall.Syscall6(procCoGetObject.Addr(), 4, uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(bindOpts)), uintptr(unsafe.Pointer(guid)), uintptr(unsafe.Pointer(functionTable)), 0, 0) - if r0 != 0 { - ret = syscall.Errno(r0) - } - return -} - -func coInitializeEx(reserved uintptr, coInit uint32) (ret error) { - r0, _, _ := syscall.Syscall(procCoInitializeEx.Addr(), 2, uintptr(reserved), uintptr(coInit), 0) - if r0 != 0 { - ret = syscall.Errno(r0) - } - return -} - -func coUninitialize() { - syscall.Syscall(procCoUninitialize.Addr(), 0, 0, 0, 0) - return -} -- cgit v1.2.3-59-g8ed1b