1
2
3
4
5 package winio
6
7 import (
8 "syscall"
9 "unsafe"
10
11 "golang.org/x/sys/windows"
12 )
13
14 var _ unsafe.Pointer
15
16
17
18 const (
19 errnoERROR_IO_PENDING = 997
20 )
21
22 var (
23 errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING)
24 errERROR_EINVAL error = syscall.EINVAL
25 )
26
27
28
29 func errnoErr(e syscall.Errno) error {
30 switch e {
31 case 0:
32 return errERROR_EINVAL
33 case errnoERROR_IO_PENDING:
34 return errERROR_IO_PENDING
35 }
36
37
38
39 return e
40 }
41
42 var (
43 modadvapi32 = windows.NewLazySystemDLL("advapi32.dll")
44 modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
45 modntdll = windows.NewLazySystemDLL("ntdll.dll")
46 modws2_32 = windows.NewLazySystemDLL("ws2_32.dll")
47
48 procAdjustTokenPrivileges = modadvapi32.NewProc("AdjustTokenPrivileges")
49 procConvertSecurityDescriptorToStringSecurityDescriptorW = modadvapi32.NewProc("ConvertSecurityDescriptorToStringSecurityDescriptorW")
50 procConvertSidToStringSidW = modadvapi32.NewProc("ConvertSidToStringSidW")
51 procConvertStringSecurityDescriptorToSecurityDescriptorW = modadvapi32.NewProc("ConvertStringSecurityDescriptorToSecurityDescriptorW")
52 procConvertStringSidToSidW = modadvapi32.NewProc("ConvertStringSidToSidW")
53 procGetSecurityDescriptorLength = modadvapi32.NewProc("GetSecurityDescriptorLength")
54 procImpersonateSelf = modadvapi32.NewProc("ImpersonateSelf")
55 procLookupAccountNameW = modadvapi32.NewProc("LookupAccountNameW")
56 procLookupAccountSidW = modadvapi32.NewProc("LookupAccountSidW")
57 procLookupPrivilegeDisplayNameW = modadvapi32.NewProc("LookupPrivilegeDisplayNameW")
58 procLookupPrivilegeNameW = modadvapi32.NewProc("LookupPrivilegeNameW")
59 procLookupPrivilegeValueW = modadvapi32.NewProc("LookupPrivilegeValueW")
60 procOpenThreadToken = modadvapi32.NewProc("OpenThreadToken")
61 procRevertToSelf = modadvapi32.NewProc("RevertToSelf")
62 procBackupRead = modkernel32.NewProc("BackupRead")
63 procBackupWrite = modkernel32.NewProc("BackupWrite")
64 procCancelIoEx = modkernel32.NewProc("CancelIoEx")
65 procConnectNamedPipe = modkernel32.NewProc("ConnectNamedPipe")
66 procCreateIoCompletionPort = modkernel32.NewProc("CreateIoCompletionPort")
67 procCreateNamedPipeW = modkernel32.NewProc("CreateNamedPipeW")
68 procGetCurrentThread = modkernel32.NewProc("GetCurrentThread")
69 procGetNamedPipeHandleStateW = modkernel32.NewProc("GetNamedPipeHandleStateW")
70 procGetNamedPipeInfo = modkernel32.NewProc("GetNamedPipeInfo")
71 procGetQueuedCompletionStatus = modkernel32.NewProc("GetQueuedCompletionStatus")
72 procLocalAlloc = modkernel32.NewProc("LocalAlloc")
73 procLocalFree = modkernel32.NewProc("LocalFree")
74 procSetFileCompletionNotificationModes = modkernel32.NewProc("SetFileCompletionNotificationModes")
75 procNtCreateNamedPipeFile = modntdll.NewProc("NtCreateNamedPipeFile")
76 procRtlDefaultNpAcl = modntdll.NewProc("RtlDefaultNpAcl")
77 procRtlDosPathNameToNtPathName_U = modntdll.NewProc("RtlDosPathNameToNtPathName_U")
78 procRtlNtStatusToDosErrorNoTeb = modntdll.NewProc("RtlNtStatusToDosErrorNoTeb")
79 procWSAGetOverlappedResult = modws2_32.NewProc("WSAGetOverlappedResult")
80 )
81
82 func adjustTokenPrivileges(token windows.Token, releaseAll bool, input *byte, outputSize uint32, output *byte, requiredSize *uint32) (success bool, err error) {
83 var _p0 uint32
84 if releaseAll {
85 _p0 = 1
86 }
87 r0, _, e1 := syscall.Syscall6(procAdjustTokenPrivileges.Addr(), 6, uintptr(token), uintptr(_p0), uintptr(unsafe.Pointer(input)), uintptr(outputSize), uintptr(unsafe.Pointer(output)), uintptr(unsafe.Pointer(requiredSize)))
88 success = r0 != 0
89 if true {
90 err = errnoErr(e1)
91 }
92 return
93 }
94
95 func convertSecurityDescriptorToStringSecurityDescriptor(sd *byte, revision uint32, secInfo uint32, sddl **uint16, sddlSize *uint32) (err error) {
96 r1, _, e1 := syscall.Syscall6(procConvertSecurityDescriptorToStringSecurityDescriptorW.Addr(), 5, uintptr(unsafe.Pointer(sd)), uintptr(revision), uintptr(secInfo), uintptr(unsafe.Pointer(sddl)), uintptr(unsafe.Pointer(sddlSize)), 0)
97 if r1 == 0 {
98 err = errnoErr(e1)
99 }
100 return
101 }
102
103 func convertSidToStringSid(sid *byte, str **uint16) (err error) {
104 r1, _, e1 := syscall.Syscall(procConvertSidToStringSidW.Addr(), 2, uintptr(unsafe.Pointer(sid)), uintptr(unsafe.Pointer(str)), 0)
105 if r1 == 0 {
106 err = errnoErr(e1)
107 }
108 return
109 }
110
111 func convertStringSecurityDescriptorToSecurityDescriptor(str string, revision uint32, sd *uintptr, size *uint32) (err error) {
112 var _p0 *uint16
113 _p0, err = syscall.UTF16PtrFromString(str)
114 if err != nil {
115 return
116 }
117 return _convertStringSecurityDescriptorToSecurityDescriptor(_p0, revision, sd, size)
118 }
119
120 func _convertStringSecurityDescriptorToSecurityDescriptor(str *uint16, revision uint32, sd *uintptr, size *uint32) (err error) {
121 r1, _, e1 := syscall.Syscall6(procConvertStringSecurityDescriptorToSecurityDescriptorW.Addr(), 4, uintptr(unsafe.Pointer(str)), uintptr(revision), uintptr(unsafe.Pointer(sd)), uintptr(unsafe.Pointer(size)), 0, 0)
122 if r1 == 0 {
123 err = errnoErr(e1)
124 }
125 return
126 }
127
128 func convertStringSidToSid(str *uint16, sid **byte) (err error) {
129 r1, _, e1 := syscall.Syscall(procConvertStringSidToSidW.Addr(), 2, uintptr(unsafe.Pointer(str)), uintptr(unsafe.Pointer(sid)), 0)
130 if r1 == 0 {
131 err = errnoErr(e1)
132 }
133 return
134 }
135
136 func getSecurityDescriptorLength(sd uintptr) (len uint32) {
137 r0, _, _ := syscall.Syscall(procGetSecurityDescriptorLength.Addr(), 1, uintptr(sd), 0, 0)
138 len = uint32(r0)
139 return
140 }
141
142 func impersonateSelf(level uint32) (err error) {
143 r1, _, e1 := syscall.Syscall(procImpersonateSelf.Addr(), 1, uintptr(level), 0, 0)
144 if r1 == 0 {
145 err = errnoErr(e1)
146 }
147 return
148 }
149
150 func lookupAccountName(systemName *uint16, accountName string, sid *byte, sidSize *uint32, refDomain *uint16, refDomainSize *uint32, sidNameUse *uint32) (err error) {
151 var _p0 *uint16
152 _p0, err = syscall.UTF16PtrFromString(accountName)
153 if err != nil {
154 return
155 }
156 return _lookupAccountName(systemName, _p0, sid, sidSize, refDomain, refDomainSize, sidNameUse)
157 }
158
159 func _lookupAccountName(systemName *uint16, accountName *uint16, sid *byte, sidSize *uint32, refDomain *uint16, refDomainSize *uint32, sidNameUse *uint32) (err error) {
160 r1, _, e1 := syscall.Syscall9(procLookupAccountNameW.Addr(), 7, uintptr(unsafe.Pointer(systemName)), uintptr(unsafe.Pointer(accountName)), uintptr(unsafe.Pointer(sid)), uintptr(unsafe.Pointer(sidSize)), uintptr(unsafe.Pointer(refDomain)), uintptr(unsafe.Pointer(refDomainSize)), uintptr(unsafe.Pointer(sidNameUse)), 0, 0)
161 if r1 == 0 {
162 err = errnoErr(e1)
163 }
164 return
165 }
166
167 func lookupAccountSid(systemName *uint16, sid *byte, name *uint16, nameSize *uint32, refDomain *uint16, refDomainSize *uint32, sidNameUse *uint32) (err error) {
168 r1, _, e1 := syscall.Syscall9(procLookupAccountSidW.Addr(), 7, uintptr(unsafe.Pointer(systemName)), uintptr(unsafe.Pointer(sid)), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(nameSize)), uintptr(unsafe.Pointer(refDomain)), uintptr(unsafe.Pointer(refDomainSize)), uintptr(unsafe.Pointer(sidNameUse)), 0, 0)
169 if r1 == 0 {
170 err = errnoErr(e1)
171 }
172 return
173 }
174
175 func lookupPrivilegeDisplayName(systemName string, name *uint16, buffer *uint16, size *uint32, languageId *uint32) (err error) {
176 var _p0 *uint16
177 _p0, err = syscall.UTF16PtrFromString(systemName)
178 if err != nil {
179 return
180 }
181 return _lookupPrivilegeDisplayName(_p0, name, buffer, size, languageId)
182 }
183
184 func _lookupPrivilegeDisplayName(systemName *uint16, name *uint16, buffer *uint16, size *uint32, languageId *uint32) (err error) {
185 r1, _, e1 := syscall.Syscall6(procLookupPrivilegeDisplayNameW.Addr(), 5, uintptr(unsafe.Pointer(systemName)), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(buffer)), uintptr(unsafe.Pointer(size)), uintptr(unsafe.Pointer(languageId)), 0)
186 if r1 == 0 {
187 err = errnoErr(e1)
188 }
189 return
190 }
191
192 func lookupPrivilegeName(systemName string, luid *uint64, buffer *uint16, size *uint32) (err error) {
193 var _p0 *uint16
194 _p0, err = syscall.UTF16PtrFromString(systemName)
195 if err != nil {
196 return
197 }
198 return _lookupPrivilegeName(_p0, luid, buffer, size)
199 }
200
201 func _lookupPrivilegeName(systemName *uint16, luid *uint64, buffer *uint16, size *uint32) (err error) {
202 r1, _, e1 := syscall.Syscall6(procLookupPrivilegeNameW.Addr(), 4, uintptr(unsafe.Pointer(systemName)), uintptr(unsafe.Pointer(luid)), uintptr(unsafe.Pointer(buffer)), uintptr(unsafe.Pointer(size)), 0, 0)
203 if r1 == 0 {
204 err = errnoErr(e1)
205 }
206 return
207 }
208
209 func lookupPrivilegeValue(systemName string, name string, luid *uint64) (err error) {
210 var _p0 *uint16
211 _p0, err = syscall.UTF16PtrFromString(systemName)
212 if err != nil {
213 return
214 }
215 var _p1 *uint16
216 _p1, err = syscall.UTF16PtrFromString(name)
217 if err != nil {
218 return
219 }
220 return _lookupPrivilegeValue(_p0, _p1, luid)
221 }
222
223 func _lookupPrivilegeValue(systemName *uint16, name *uint16, luid *uint64) (err error) {
224 r1, _, e1 := syscall.Syscall(procLookupPrivilegeValueW.Addr(), 3, uintptr(unsafe.Pointer(systemName)), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(luid)))
225 if r1 == 0 {
226 err = errnoErr(e1)
227 }
228 return
229 }
230
231 func openThreadToken(thread syscall.Handle, accessMask uint32, openAsSelf bool, token *windows.Token) (err error) {
232 var _p0 uint32
233 if openAsSelf {
234 _p0 = 1
235 }
236 r1, _, e1 := syscall.Syscall6(procOpenThreadToken.Addr(), 4, uintptr(thread), uintptr(accessMask), uintptr(_p0), uintptr(unsafe.Pointer(token)), 0, 0)
237 if r1 == 0 {
238 err = errnoErr(e1)
239 }
240 return
241 }
242
243 func revertToSelf() (err error) {
244 r1, _, e1 := syscall.Syscall(procRevertToSelf.Addr(), 0, 0, 0, 0)
245 if r1 == 0 {
246 err = errnoErr(e1)
247 }
248 return
249 }
250
251 func backupRead(h syscall.Handle, b []byte, bytesRead *uint32, abort bool, processSecurity bool, context *uintptr) (err error) {
252 var _p0 *byte
253 if len(b) > 0 {
254 _p0 = &b[0]
255 }
256 var _p1 uint32
257 if abort {
258 _p1 = 1
259 }
260 var _p2 uint32
261 if processSecurity {
262 _p2 = 1
263 }
264 r1, _, e1 := syscall.Syscall9(procBackupRead.Addr(), 7, uintptr(h), uintptr(unsafe.Pointer(_p0)), uintptr(len(b)), uintptr(unsafe.Pointer(bytesRead)), uintptr(_p1), uintptr(_p2), uintptr(unsafe.Pointer(context)), 0, 0)
265 if r1 == 0 {
266 err = errnoErr(e1)
267 }
268 return
269 }
270
271 func backupWrite(h syscall.Handle, b []byte, bytesWritten *uint32, abort bool, processSecurity bool, context *uintptr) (err error) {
272 var _p0 *byte
273 if len(b) > 0 {
274 _p0 = &b[0]
275 }
276 var _p1 uint32
277 if abort {
278 _p1 = 1
279 }
280 var _p2 uint32
281 if processSecurity {
282 _p2 = 1
283 }
284 r1, _, e1 := syscall.Syscall9(procBackupWrite.Addr(), 7, uintptr(h), uintptr(unsafe.Pointer(_p0)), uintptr(len(b)), uintptr(unsafe.Pointer(bytesWritten)), uintptr(_p1), uintptr(_p2), uintptr(unsafe.Pointer(context)), 0, 0)
285 if r1 == 0 {
286 err = errnoErr(e1)
287 }
288 return
289 }
290
291 func cancelIoEx(file syscall.Handle, o *syscall.Overlapped) (err error) {
292 r1, _, e1 := syscall.Syscall(procCancelIoEx.Addr(), 2, uintptr(file), uintptr(unsafe.Pointer(o)), 0)
293 if r1 == 0 {
294 err = errnoErr(e1)
295 }
296 return
297 }
298
299 func connectNamedPipe(pipe syscall.Handle, o *syscall.Overlapped) (err error) {
300 r1, _, e1 := syscall.Syscall(procConnectNamedPipe.Addr(), 2, uintptr(pipe), uintptr(unsafe.Pointer(o)), 0)
301 if r1 == 0 {
302 err = errnoErr(e1)
303 }
304 return
305 }
306
307 func createIoCompletionPort(file syscall.Handle, port syscall.Handle, key uintptr, threadCount uint32) (newport syscall.Handle, err error) {
308 r0, _, e1 := syscall.Syscall6(procCreateIoCompletionPort.Addr(), 4, uintptr(file), uintptr(port), uintptr(key), uintptr(threadCount), 0, 0)
309 newport = syscall.Handle(r0)
310 if newport == 0 {
311 err = errnoErr(e1)
312 }
313 return
314 }
315
316 func createNamedPipe(name string, flags uint32, pipeMode uint32, maxInstances uint32, outSize uint32, inSize uint32, defaultTimeout uint32, sa *syscall.SecurityAttributes) (handle syscall.Handle, err error) {
317 var _p0 *uint16
318 _p0, err = syscall.UTF16PtrFromString(name)
319 if err != nil {
320 return
321 }
322 return _createNamedPipe(_p0, flags, pipeMode, maxInstances, outSize, inSize, defaultTimeout, sa)
323 }
324
325 func _createNamedPipe(name *uint16, flags uint32, pipeMode uint32, maxInstances uint32, outSize uint32, inSize uint32, defaultTimeout uint32, sa *syscall.SecurityAttributes) (handle syscall.Handle, err error) {
326 r0, _, e1 := syscall.Syscall9(procCreateNamedPipeW.Addr(), 8, uintptr(unsafe.Pointer(name)), uintptr(flags), uintptr(pipeMode), uintptr(maxInstances), uintptr(outSize), uintptr(inSize), uintptr(defaultTimeout), uintptr(unsafe.Pointer(sa)), 0)
327 handle = syscall.Handle(r0)
328 if handle == syscall.InvalidHandle {
329 err = errnoErr(e1)
330 }
331 return
332 }
333
334 func getCurrentThread() (h syscall.Handle) {
335 r0, _, _ := syscall.Syscall(procGetCurrentThread.Addr(), 0, 0, 0, 0)
336 h = syscall.Handle(r0)
337 return
338 }
339
340 func getNamedPipeHandleState(pipe syscall.Handle, state *uint32, curInstances *uint32, maxCollectionCount *uint32, collectDataTimeout *uint32, userName *uint16, maxUserNameSize uint32) (err error) {
341 r1, _, e1 := syscall.Syscall9(procGetNamedPipeHandleStateW.Addr(), 7, uintptr(pipe), uintptr(unsafe.Pointer(state)), uintptr(unsafe.Pointer(curInstances)), uintptr(unsafe.Pointer(maxCollectionCount)), uintptr(unsafe.Pointer(collectDataTimeout)), uintptr(unsafe.Pointer(userName)), uintptr(maxUserNameSize), 0, 0)
342 if r1 == 0 {
343 err = errnoErr(e1)
344 }
345 return
346 }
347
348 func getNamedPipeInfo(pipe syscall.Handle, flags *uint32, outSize *uint32, inSize *uint32, maxInstances *uint32) (err error) {
349 r1, _, e1 := syscall.Syscall6(procGetNamedPipeInfo.Addr(), 5, uintptr(pipe), uintptr(unsafe.Pointer(flags)), uintptr(unsafe.Pointer(outSize)), uintptr(unsafe.Pointer(inSize)), uintptr(unsafe.Pointer(maxInstances)), 0)
350 if r1 == 0 {
351 err = errnoErr(e1)
352 }
353 return
354 }
355
356 func getQueuedCompletionStatus(port syscall.Handle, bytes *uint32, key *uintptr, o **ioOperation, timeout uint32) (err error) {
357 r1, _, e1 := syscall.Syscall6(procGetQueuedCompletionStatus.Addr(), 5, uintptr(port), uintptr(unsafe.Pointer(bytes)), uintptr(unsafe.Pointer(key)), uintptr(unsafe.Pointer(o)), uintptr(timeout), 0)
358 if r1 == 0 {
359 err = errnoErr(e1)
360 }
361 return
362 }
363
364 func localAlloc(uFlags uint32, length uint32) (ptr uintptr) {
365 r0, _, _ := syscall.Syscall(procLocalAlloc.Addr(), 2, uintptr(uFlags), uintptr(length), 0)
366 ptr = uintptr(r0)
367 return
368 }
369
370 func localFree(mem uintptr) {
371 syscall.Syscall(procLocalFree.Addr(), 1, uintptr(mem), 0, 0)
372 return
373 }
374
375 func setFileCompletionNotificationModes(h syscall.Handle, flags uint8) (err error) {
376 r1, _, e1 := syscall.Syscall(procSetFileCompletionNotificationModes.Addr(), 2, uintptr(h), uintptr(flags), 0)
377 if r1 == 0 {
378 err = errnoErr(e1)
379 }
380 return
381 }
382
383 func ntCreateNamedPipeFile(pipe *syscall.Handle, access uint32, oa *objectAttributes, iosb *ioStatusBlock, share uint32, disposition uint32, options uint32, typ uint32, readMode uint32, completionMode uint32, maxInstances uint32, inboundQuota uint32, outputQuota uint32, timeout *int64) (status ntStatus) {
384 r0, _, _ := syscall.Syscall15(procNtCreateNamedPipeFile.Addr(), 14, uintptr(unsafe.Pointer(pipe)), uintptr(access), uintptr(unsafe.Pointer(oa)), uintptr(unsafe.Pointer(iosb)), uintptr(share), uintptr(disposition), uintptr(options), uintptr(typ), uintptr(readMode), uintptr(completionMode), uintptr(maxInstances), uintptr(inboundQuota), uintptr(outputQuota), uintptr(unsafe.Pointer(timeout)), 0)
385 status = ntStatus(r0)
386 return
387 }
388
389 func rtlDefaultNpAcl(dacl *uintptr) (status ntStatus) {
390 r0, _, _ := syscall.Syscall(procRtlDefaultNpAcl.Addr(), 1, uintptr(unsafe.Pointer(dacl)), 0, 0)
391 status = ntStatus(r0)
392 return
393 }
394
395 func rtlDosPathNameToNtPathName(name *uint16, ntName *unicodeString, filePart uintptr, reserved uintptr) (status ntStatus) {
396 r0, _, _ := syscall.Syscall6(procRtlDosPathNameToNtPathName_U.Addr(), 4, uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(ntName)), uintptr(filePart), uintptr(reserved), 0, 0)
397 status = ntStatus(r0)
398 return
399 }
400
401 func rtlNtStatusToDosError(status ntStatus) (winerr error) {
402 r0, _, _ := syscall.Syscall(procRtlNtStatusToDosErrorNoTeb.Addr(), 1, uintptr(status), 0, 0)
403 if r0 != 0 {
404 winerr = syscall.Errno(r0)
405 }
406 return
407 }
408
409 func wsaGetOverlappedResult(h syscall.Handle, o *syscall.Overlapped, bytes *uint32, wait bool, flags *uint32) (err error) {
410 var _p0 uint32
411 if wait {
412 _p0 = 1
413 }
414 r1, _, e1 := syscall.Syscall6(procWSAGetOverlappedResult.Addr(), 5, uintptr(h), uintptr(unsafe.Pointer(o)), uintptr(unsafe.Pointer(bytes)), uintptr(_p0), uintptr(unsafe.Pointer(flags)), 0)
415 if r1 == 0 {
416 err = errnoErr(e1)
417 }
418 return
419 }
420
View as plain text