-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelevate_windows.go
More file actions
70 lines (63 loc) · 1.42 KB
/
Copy pathelevate_windows.go
File metadata and controls
70 lines (63 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//go:build windows
package main
import (
"os"
"strings"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
// ensureAdmin exits the current process after launching an elevated copy when needed.
// Production builds also request admin via the exe manifest (UAC).
func ensureAdmin() {
if isAdmin() {
return
}
exe, err := os.Executable()
if err != nil {
os.Exit(1)
}
// Re-launch elevated
verb, _ := syscall.UTF16PtrFromString("runas")
file, _ := syscall.UTF16PtrFromString(exe)
// preserve args (skip argv0)
args := strings.Join(os.Args[1:], " ")
param, _ := syscall.UTF16PtrFromString(args)
dir, _ := syscall.UTF16PtrFromString("")
var show int32 = 1 // SW_SHOWNORMAL
ret, _, _ := windows.NewLazySystemDLL("shell32.dll").NewProc("ShellExecuteW").Call(
0,
uintptr(unsafe.Pointer(verb)),
uintptr(unsafe.Pointer(file)),
uintptr(unsafe.Pointer(param)),
uintptr(unsafe.Pointer(dir)),
uintptr(show),
)
// ShellExecute returns >32 on success
if ret <= 32 {
os.Exit(1)
}
os.Exit(0)
}
func isAdmin() bool {
var sid *windows.SID
// BUILTIN\Administrators
err := windows.AllocateAndInitializeSid(
&windows.SECURITY_NT_AUTHORITY,
2,
windows.SECURITY_BUILTIN_DOMAIN_RID,
windows.DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&sid,
)
if err != nil {
return false
}
defer windows.FreeSid(sid)
token := windows.Token(0)
member, err := token.IsMember(sid)
if err != nil {
return false
}
return member
}