forked from kardianos/service
/
zwinsessions.go
138 lines (122 loc) · 4.25 KB
/
zwinsessions.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Code generated by 'go generate'; DO NOT EDIT.
package service
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)
)
// errnoErr returns common boxed Errno values, to prevent
// allocations at runtime.
func errnoErr(e syscall.Errno) error {
switch e {
case 0:
return nil
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 (
modsecur32 = windows.NewLazySystemDLL("secur32.dll")
modwtsapi32 = windows.NewLazySystemDLL("wtsapi32.dll")
modadvapi32 = windows.NewLazySystemDLL("advapi32.dll")
modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
moduserenv = windows.NewLazySystemDLL("userenv.dll")
procGetUserNameExW = modsecur32.NewProc("GetUserNameExW")
procTranslateNameW = modsecur32.NewProc("TranslateNameW")
procWTSQueryUserToken = modwtsapi32.NewProc("WTSQueryUserToken")
procGetTokenInformation = modadvapi32.NewProc("GetTokenInformation")
procWTSGetActiveConsoleSessionId = modkernel32.NewProc("WTSGetActiveConsoleSessionId")
procGetUserProfileDirectoryW = moduserenv.NewProc("GetUserProfileDirectoryW")
procWTSQuerySessionInformationW = modwtsapi32.NewProc("WTSQuerySessionInformationW")
)
func GetUserNameEx(nameFormat uint32, nameBuffre *uint16, nSize *uint32) (err error) {
r1, _, e1 := syscall.Syscall(procGetUserNameExW.Addr(), 3, uintptr(nameFormat), uintptr(unsafe.Pointer(nameBuffre)), uintptr(unsafe.Pointer(nSize)))
if r1&0xff == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func TranslateName(accName *uint16, accNameFormat uint32, desiredNameFormat uint32, translatedName *uint16, nSize *uint32) (err error) {
r1, _, e1 := syscall.Syscall6(procTranslateNameW.Addr(), 5, uintptr(unsafe.Pointer(accName)), uintptr(accNameFormat), uintptr(desiredNameFormat), uintptr(unsafe.Pointer(translatedName)), uintptr(unsafe.Pointer(nSize)), 0)
if r1&0xff == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func WTSQueryUserToken(sessionId uint32, phToken *windows.Handle) (err error) {
r1, _, e1 := syscall.Syscall(procWTSQueryUserToken.Addr(), 2, uintptr(sessionId), uintptr(unsafe.Pointer(phToken)), 0)
if r1&0xff == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func GetTokenInformation(tokenHandle windows.Handle, tokenInformationClass TOKEN_INFORMATION_CLASS, tokenInformation *byte, tokenInformationLength uint32, returnLength *uint32) (err error) {
r1, _, e1 := syscall.Syscall6(procGetTokenInformation.Addr(), 5, uintptr(tokenHandle), uintptr(tokenInformationClass), uintptr(unsafe.Pointer(tokenInformation)), uintptr(tokenInformationLength), uintptr(unsafe.Pointer(returnLength)), 0)
if r1&0xff == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func WTSGetActiveConsoleSessionId() (sessionId uint32, err error) {
r0, _, e1 := syscall.Syscall(procWTSGetActiveConsoleSessionId.Addr(), 0, 0, 0, 0)
sessionId = uint32(r0)
if sessionId == 0xFFFFFFFF {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func GetUserProfileDirectory(hToken windows.Handle, lpProfileDir *uint16, lpcchSize *uint32) (err error) {
r1, _, e1 := syscall.Syscall(procGetUserProfileDirectoryW.Addr(), 3, uintptr(hToken), uintptr(unsafe.Pointer(lpProfileDir)), uintptr(unsafe.Pointer(lpcchSize)))
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func WTSQuerySessionInformation(hServer windows.Handle, sessionId uint32, WTSInfoClass WTS_INFO_CLASS, buffer *uintptr, bytesReturned *uint32) (err error) {
r1, _, e1 := syscall.Syscall6(procWTSQuerySessionInformationW.Addr(), 5, uintptr(hServer), uintptr(sessionId), uintptr(WTSInfoClass), uintptr(unsafe.Pointer(buffer)), uintptr(unsafe.Pointer(bytesReturned)), 0)
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}