Commit Graph

183 Commits

Author SHA1 Message Date
Zhang Boyang
7e40ca221e windows: implement GetsockoptInt
Change-Id: If0b17ccaba5c5d1e07499c222eb3f4c45a094636
GitHub-Last-Rev: a0fb2fc43d
GitHub-Pull-Request: golang/sys#70
Reviewed-on: https://go-review.googlesource.com/c/sys/+/233200
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-05-11 23:29:37 +00:00
ly303550688
6aff5f38e5 windows: add (*DLL).FindProcByOrdinal
Change-Id: I0b0ef2c62994fad4d89bf15c8c6c95fa660190c3
GitHub-Last-Rev: 379ab4a0f5
GitHub-Pull-Request: golang/sys#69
Reviewed-on: https://go-review.googlesource.com/c/sys/+/227439
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2020-05-09 04:47:56 +00:00
maltalex
b77594299b windows: add missing network-related constants
Change-Id: I75c432e527c42bdf3c91e07590a143c67d9a97a3
GitHub-Last-Rev: d738e281e2
GitHub-Pull-Request: golang/sys#53
Reviewed-on: https://go-review.googlesource.com/c/sys/+/214997
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-01-16 00:19:09 +00:00
maltalex
c709ea063b windows: added SO_RCVTIMEO and IPPROTO_ICMP constants
Added missing Windows constants SO_RCVTIMEO and IPPROTO_ICMP

Change-Id: I3a13fa0af2d085a7294b8d983a94d0d9c3c6ec31
GitHub-Last-Rev: cfafa03e60
GitHub-Pull-Request: golang/sys#50
Reviewed-on: https://go-review.googlesource.com/c/sys/+/211559
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-12-24 08:55:50 +00:00
maltalex
af0d71d358 windows: fix recvfrom and sendto data types
Fixes wrong data type used in Windows recvfrom and sendto calls

In Windows, `int` refers to a 32-bit signed integer
(https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#int).
However, the current implementation (https://golang.org/cl/208321)
uses the `int` type, which can have a different size. This is especially
important when recvfrom` returns a 32bit value of `-1`, indicating an error,
since it is interpreted as the 64bit value 4294967295.

Change-Id: Ib966ff317b0be7e29e48ee373a794cd0267fb007
GitHub-Last-Rev: 9aad4c377f
GitHub-Pull-Request: golang/sys#51
Reviewed-on: https://go-review.googlesource.com/c/sys/+/211998
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-12-19 23:57:34 +00:00
maltalex
6d18c012ae windows: add Recvfrom and Sendto implementations
Added the missing `Sendto` and `Recvfrom` syscalls for Windows.
Fixes golang/go#7170

Change-Id: I794b4f631fad424400fec85ccecc3a3b3abe5465
GitHub-Last-Rev: 39ebe929ca
GitHub-Pull-Request: golang/sys#46
Reviewed-on: https://go-review.googlesource.com/c/sys/+/208321
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-11-28 01:58:09 +00:00
Simon Rozman
6bfc516c86 windows: add Get*PreferredUILanguages
This commit adds the following MUI functions:
- GetUserPreferredUILanguages
- GetSystemPreferredUILanguages
- GetThreadPreferredUILanguages
- GetProcessPreferredUILanguages

Change-Id: I44f1c07245ab814935778c6b910b224d24cc753c
Reviewed-on: https://go-review.googlesource.com/c/sys/+/207860
Reviewed-by: Simon Rozman <simon@rozman.si>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-11-20 13:05:36 +00:00
Alex Brainman
b5d5184f72 windows: revert security_windows.go change of CL 202177
This change is golang.org/x/sys/windows copy of CL 204117.

Updates golang/go#34972

Change-Id: I338c91bfe3971a80cbf44c2a0f1807adaa00918d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/207199
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
2019-11-18 09:04:20 +00:00
Tobias Klauser
d32e6e3b99 windows: add empty.s to fix build with Go 1.11 and earlier
CL 199519 broke the build with Go 1.11. Even though this is not an
officially supported release anymore, people still seem to rely on it
and the fix is easy enough and won't break anyone using newer, supported
Go versions.

Change-Id: I632d6840bc1f49c01c372727c9a55d2daf98f4a8
Reviewed-on: https://go-review.googlesource.com/c/sys/+/206357
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-11-10 16:31:57 +00:00
Tobias Klauser
e8c54fb511 windows: add EnumProcesses function
See https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocesses

Change-Id: Ibccb0c3d8e4f32857547ee6d072d0b1cb2430366
Reviewed-on: https://go-review.googlesource.com/c/sys/+/205197
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-11-04 09:48:58 +00:00
Alex Brainman
f43be2a459 windows/registry: make '-gcflags=all=-d=checkptr' flag work
Mostly replaced [:x] slice operation with [:x:x]. 

This change is copy of CL 203442.

Updates golang/go#34972

Change-Id: I9b725a39aea469bd5669cc1deb3d02a88912843e
Reviewed-on: https://go-review.googlesource.com/c/sys/+/204017
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-10-29 15:55:21 +00:00
Jason A. Donenfeld
f8518d3b36 windows/registry: remove TestWalkFullRegistry due to false assumptions
This test's existence was predicated upon assumptions about the full
range of known data types and known data into those types. However,
we've learned from Microsoft that there are several undocumented secret
registry types that are in use by various parts of Windows, and we've
learned from inspection that many Microsoft uses of registry types don't
strictly adhere to the recommended value size. It's therefore foolhardy
to make any assumptions about what goes in and out of the registry, and
so this test is meaningless and error-prone.

Updates golang/go#35084

Change-Id: Ie545229afd8dc5bde90fffa0f735f7102cd4a6eb
Reviewed-on: https://go-review.googlesource.com/c/sys/+/203605
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-10-27 21:15:39 +00:00
Jason A. Donenfeld
53bf42e6b3 windows/registry: allow for non-null terminated strings
According to MSDN, "If the data has the REG_SZ, REG_MULTI_SZ or
REG_EXPAND_SZ type, this size includes any terminating null character or
characters unless the data was stored without them. [...] If the data
has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not
have been stored with the proper terminating null characters. Therefore,
even if the function returns ERROR_SUCCESS, the application should
ensure that the string is properly terminated before using it;
otherwise, it may overwrite a buffer."

It's therefore dangerous to pass it off unbounded as we do, and in fact
this led to crashes on real systems.

Change-Id: I2ab324e85f75dc3e4d6d62fec3b96937fec77510
Reviewed-on: https://go-review.googlesource.com/c/sys/+/202957
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-10-25 09:01:51 +00:00
Jason A. Donenfeld
742c48ecae windows: respect permission bits on file opening
Chmod toggles the FILE_ATTRIBUTES_READONLY flag depending on the
permission bits. That's a bit odd but I guess some compromises were made
at some point and this is what was chosen to map to a Unix concept that
Windows doesn't really have in the same way. That's fine. However, the
logic used in Chmod was forgotten from Open, which then manifested
itself in various places, most recently, go modules' read-only behavior.

This corresonds with the syscall CL 202439.

Updates golang/go#35033

Change-Id: Id8e74c5205057a74a35eda213516780b79a2aed2
Reviewed-on: https://go-review.googlesource.com/c/sys/+/202440
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-10-22 10:09:44 +00:00
Tobias Klauser
9984515f05 windows, unix: fix wrong unsafe.Pointer alignment in syscall
Same as CL 201877 did for package syscall.

Updates golang/go#34972

Change-Id: I3929841ab32378516edafb1f02a84b1bdcc77bbd
Reviewed-on: https://go-review.googlesource.com/c/sys/+/202177
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-10-20 15:20:52 +00:00
Takuto Ikuta
b09406accb windows: add GetDiskFreeSpaceEx function
ref:
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespaceexw

Change-Id: If57b0777106a2253e4287818d2c5aee2d6be13d3
Reviewed-on: https://go-review.googlesource.com/c/sys/+/200257
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-10-10 19:43:22 +00:00
Jason A. Donenfeld
0b153f535c windows: fix ShellExecute return condition
ShellExecute is an ancient API with an unusual return signature. It
pretends to return an HINSTANCE for backwards compatibility with Windows
3, but it's actualy a fake HINSTANCE. What's really happening here,
according to MSDN, is that it returns either an Windows error less
than or equal to 32, or it succeeds. So we adjust the return value
accordingly.

Prior to this commit, it was impossible to tell whether this command had
succeeded. For example, when using the "runas" verb, ShellExecute did
not correctly indicate whether or not permission was granted.

Change-Id: Ie60554d6465798bacb9a225c4ead7e8dd62bce14
Reviewed-on: https://go-review.googlesource.com/c/sys/+/199521
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Nenad Kozul <knenad@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-10-08 10:27:50 +00:00
Jason A. Donenfeld
ef33b2fb2c windows: use go:linkname instead of assembly
This is a bit cleaner and makes it more explicit what's happening, along
with a documenting comment.

Change-Id: I30c92f8576c72b05ebdb4634c68023237bde3cbf
Reviewed-on: https://go-review.googlesource.com/c/sys/+/199519
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-10-07 15:44:56 +00:00
Jason A. Donenfeld
5f54ce5427 windows: add module handle functions
These are in internal/syscall/windows, but not here, and they're quite
handy to have.

Change-Id: I79fe78d3c6bc2c001f994b03ce575287908d2c59
Reviewed-on: https://go-review.googlesource.com/c/sys/+/199518
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-10-07 09:26:33 +00:00
Jason A. Donenfeld
c178f38b41 windows/mkwinsyscall: import mksyscall_windows.go from go repo
This allows us to modify this file and fix it more fluidly. Users can
invoke it from go generate via:

   go run golang.org/x/sys/windows/mkwinsyscall

This was taken from Go repo commit 6b85fa80.

Updates golang/go#34388

Change-Id: I8dc39eed96b2499ccbde53554b3e16e6c1f6aa98
Reviewed-on: https://go-review.googlesource.com/c/sys/+/198637
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-10-03 21:23:58 +00:00
Jason A. Donenfeld
b397fe3ad8 windows/svc: use CloseServiceHandle for service handles
From MSDN on CreateServiceW:

  The returned handle is only valid for the process that called
  CreateService. It can be closed by calling the CloseServiceHandle
  function.

This isn't an actual kernel object to be closed with CloseHandle.

Change-Id: Iee225a666576d57a6c4864abef20206d54cbbce2
Reviewed-on: https://go-review.googlesource.com/c/sys/+/198298
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-10-02 09:15:54 +00:00
Jay Conrod
bb3f8db39f windows: add LockFileEx, UnlockFileEx system calls
Changes in syscall_windows.go were copied from
internal/syscall/syscall_windows.go.

zsyscall_windows.go was regenerated by running
'go generate -tags=generate golang.org/x/sys/windows'
using a toolchain built from tip (after CL 196122).

These functions are called by cmd/go/internal/lockedfile via
internal/syscall/windows. I'd like to copy lockedfile into another
project (CL 197299), but these functions need to be exposed here in
order to do so. We may need these in x/mod eventually, too.

Change-Id: I2033106d2ba65009e764591bfe5702f4d41dffdd
Reviewed-on: https://go-review.googlesource.com/c/sys/+/198060
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-10-01 15:17:50 +00:00
Brad Fitzpatrick
855e68c859 windows: revert breaking API changes from CL 196798, add new accessor funcs
CL 196798 changed API and broke a number of projects.

Revert the signature changes but add new convenience functions instead.

Change-Id: I49e389204f4756ec054ba8fd7555e235ef6370e8
Reviewed-on: https://go-review.googlesource.com/c/sys/+/197597
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2019-09-26 18:03:25 +00:00
Jason A. Donenfeld
98129a5cf4 windows: use pseudo handle constants to implement GetCurrentProcess
There's no point in adding a function call to retrieve a constant, or
worse, a syscall to retrieve a constant. These are fixed and baked so
deep into NT they'll never change. So let's benefit from the obvious
optimization and make these constants. Go easily inlines the function
calls as well. We also take the opportunity to sunset
OpenCurrentProcessToken and restore its original behavior, since users
should be invoking this deliberately with the correct access mask.

Change-Id: I92f7de56c0fcf5b69b59f5a79d2828c7ddf3c8f6
Reviewed-on: https://go-review.googlesource.com/c/sys/+/196800
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-09-24 09:22:10 +00:00
Jason A. Donenfeld
0a153f010e windows: make SID.String() conform to the String interface
This function shouldn't return an error. Like other String() functions
everywhere in Golang, this should instead return empty or a token value
during an error, so that it can be passed to %v and similar. Also, allow
for SID strings of maximum size.

Change-Id: Ib6d8407f8ad0bdabcb22c31b8f387594f2ea7672
Reviewed-on: https://go-review.googlesource.com/c/sys/+/196799
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-09-22 10:00:55 +00:00
Jason A. Donenfeld
2dccfee4fd windows: do not return invalid error for psuedo-handle functions
GetCurrentProcess and GetCurrentThread return -1 and -2 respectively. We
could arguably hard code those values, but MSDN cautions not to; I'm
sure this advice is old now, given that the psuedo handles for tokens
(not processes/threads) are now implemented with inline functions in the
headers for Windows 8, but anyway, we'll follow Microsoft's advice.
However, regardless of that, these functions never ever return an error.
MSDN doesn't indicate that they do, reverse engineering the functions
doesn't indicate that they do, and checking against 0 is just plain
wrong, considering 0!=INVALID_HANDLE_VALUE; however
INVALID_HANDLE_VALUE==-1, so that's not correct either. In fact,
checking any value and returning any error does not make sense.
Incidently having to check code for the pseudo handle is more verbose
too.

In order to make this function do the correct thing and meet the spec,
remove the error value from the return.

Change-Id: If03c9dab001be3bf5a04999aef20dbfcf8a4f405
Reviewed-on: https://go-review.googlesource.com/c/sys/+/196798
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-09-21 20:48:32 +00:00
Jason A. Donenfeld
5c00192e8c windows: use correct type for security attributes' descriptor member
The SECURITY_ATTRIBUTES struct always takes a SECURITY_DESCRIPTOR
pointer. Now that we've defined SECURITY_DESCRIPTOR, make
SECURITY_ATTRIBUTES properly specify the type. This eliminates the need
for terrible uintptr(unsafe.Pointer(...)) casts everywhere.

Change-Id: Ibbc85524cfe33589d43f963e10aa19d7f47686f2
Reviewed-on: https://go-review.googlesource.com/c/sys/+/196797
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-09-21 20:48:12 +00:00
Jason A. Donenfeld
14da1ac737 windows: support SECURITY_DESCRIPTOR and ACL for secured objects
This adds the basic foundation for dealing with security descriptors and
access control lists. The basic creators and getters are included in
this patch. These are some of the most fundamental security objects on NT,
and any work with the security API is fairly limited without it. These
are "core" NT structures.

Change-Id: I9a6399cb6ee41a825de30d5364ab69102d5f6d57
Reviewed-on: https://go-review.googlesource.com/c/sys/+/195498
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-09-21 19:09:40 +00:00
Jason A. Donenfeld
0c1ff786ef windows: do not corrupt stack with larger boolean return value
Windows type PBOOL is a pointer to a 4 byte value, where 0 means false
and not-0 means true. That means we should use uint32 here, not bool,
since Go bools can be 1 byte. This commit was re-generated using
mksyscall_windows.go from CL 196122.

Updates: golang/go#34364
Change-Id: I8e83b9a09c0b58d14ac9a7dee316553940ac6ee3
Reviewed-on: https://go-review.googlesource.com/c/sys/+/196123
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-09-19 04:47:23 +00:00
Jason A. Donenfeld
8a69140bde windows: add various functions for shutting down and logging out
There are a few functions to control the behavior of shutdown and
logout, both for what the current process does during shutdown, and also
whether or not the current process is running in an interactive session.
The below code is a port of the MSDN example code to Go using one of the
added new functions:

https://docs.microsoft.com/en-us/windows/win32/shutdown/how-to-shut-down-the-system

func shutdownLikeMSDNDoes() error {
  seShutdownName, err := windows.UTF16PtrFromString("SeShutdownPrivilege")
  if err != nil {
    return err
  }

  var shutdownPriv windows.Tokenprivileges
  err = windows.LookupPrivilegeValue(nil, seShutdownName, &shutdownPriv.Privileges[0].Luid)
  if err != nil {
    return err
  }
  shutdownPriv.Privileges[0].Attributes = windows.SE_PRIVILEGE_ENABLED
  shutdownPriv.PrivilegeCount = 1

  process, err := windows.GetCurrentProcess()
  if err != nil {
    return err
  }
  var token windows.Token
  err = windows.OpenProcessToken(process, windows.TOKEN_ADJUST_PRIVILEGES | windows.TOKEN_QUERY, &token)
  if err != nil {
    return err
  }
  defer token.Close()

  err = windows.AdjustTokenPrivileges(token, false, &shutdownPriv, 0, nil, nil)
  if err != nil {
    return err
  }

  err = windows.ExitWindowsEx(windows.EWX_SHUTDOWN | windows.EWX_FORCE,
    windows.SHTDN_REASON_MAJOR_OPERATINGSYSTEM | windows.SHTDN_REASON_MINOR_UPGRADE | windows.SHTDN_REASON_FLAG_PLANNED)
  if err != nil {
    return err
  }

  return nil
}

Note, though, that this function doesn't set the token privs back to how
they were before, which isn't good. A more robust method than the MSDN
one above would be to duplicate&impersonate.

Fixes: golang/go#34271
Change-Id: Ibe55ddd35b709d9ab793cb9af47c39901c5e5c69
Reviewed-on: https://go-review.googlesource.com/c/sys/+/195497
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bruce Downs <bruceadowns@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-09-16 16:59:10 +00:00
Jason A. Donenfeld
acfa387b8d windows: open process tokens with duplicate access
A usual thing to ask is, "Is my current token in group X?" The right way
of doing such a thing is:

	processToken, err := windows.OpenCurrentProcessToken()
	if err != nil {
		return false, err
	}
	defer processToken.Close()
	var checkableToken windows.Token
	err = windows.DuplicateTokenEx(token, windows.TOKEN_QUERY | windows.TOKEN_IMPERSONATE, nil, windows.SecurityIdentification, windows.TokenImpersonation, &checkableToken)
	if err != nil {
		return false, err
	}
	defer checkableToken.Close()
	isMember, err := checkableToken.IsMember(someSID)
	return isMember && err == nil, nil

This is the same flow that's used by, for example, shell32's internal
_LUAIsTokenAdmin function.

However, this all fails unless the original token is opened with
duplicate access. So this commit adjusts OpenCurrentProcessToken to do
the right thing.

Change-Id: I18efdfde43097ea9d10758018b0df132fba819f5
Reviewed-on: https://go-review.googlesource.com/c/sys/+/192337
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Simon Rozman <simon@rozman.si>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-08-30 14:18:01 +00:00
Jason A. Donenfeld
19e00faab6 windows: add mutex management functions
Indeed Go has mutexes of its own, but these are considerably
different from the native Windows ones, that can work across processes
and be put in various namespaces. They're an essential part of Windows
systems programming and important for interfacing with various external
interfaces.

Change-Id: I03987800ed1c134442321678c2c7d7aa359ecb36
Reviewed-on: https://go-review.googlesource.com/c/sys/+/192497
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-08-30 02:32:55 +00:00
Jason A. Donenfeld
c7b8b68b14 windows: add Windows directory accessors
These are useful for the same reason that the already existing
GetSystemDirectory is.

Change-Id: I3041ce6cbeb66a4f8a5960fbaf39381c8c9c80d6
Reviewed-on: https://go-review.googlesource.com/c/sys/+/191837
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-08-26 19:00:57 +00:00
Jason A. Donenfeld
acd9dae8e8 windows: allow re-enabling priority boost
The Go runtime disables priority boosting because it interferes with the
scheduler timer threads in some cases. But some apps might want to
re-enable it for specific reasons, so this commit adds the function to
do so.

Change-Id: Ida68b9f2b188560b46c322197461dd06453329e2
Reviewed-on: https://go-review.googlesource.com/c/sys/+/191839
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-08-26 16:37:24 +00:00
Jason A. Donenfeld
fb81701db8 windows: allow retrieving true version with RtlGetNtVersionNumbers
While RtlGetVersion was added so that users can get the Windows version
that isn't affected by manifesting, RtlGetVersion is still stubbed out
by the application compatibility layer (aclayers.dll and apphelp.dll)
for certain processes, such as msiexec.exe, rendering these functions
useless for actually determining the underlying operating system. This
matters in the case of msiexec.exe using a custom action DLL to install
a kernel driver, which of course is version specific. This is also
useful, it turns out, for the C runtime library, in which Microsoft uses
this function too. It's existed as a stable interface since Windows XP,
has Wine support, and is used in a decent amount of software.

Change-Id: If391e43bc6d798eff6803d5a7aa6a179f2b31d88
Reviewed-on: https://go-review.googlesource.com/c/sys/+/188119
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-08-25 16:06:03 +00:00
utkarsh-extc
51ab0e2dea windows: add support for automatic delayed start in windows service
Change-Id: Iad33ea0f6627ac98c89dbaab0b41b3dd724c3163
GitHub-Last-Rev: 8764fdbd32
GitHub-Pull-Request: golang/sys#36
Reviewed-on: https://go-review.googlesource.com/c/sys/+/187198
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-04 05:38:45 +00:00
Tianon Gravi
fc99dfbffb windows: add CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT
This is part of the changes necessary to allow simulated `SIGTERM` on Windows (these are the relevant events for `SetConsoleCtrlHandler` that would correspond to `SIGTERM` on Unix).

See https://docs.microsoft.com/en-us/windows/console/handlerroutine for a good documentation source upstream to confirm these values.

Updates golang/go#7479

(This exists under `src/cmd/vendor/golang.org/x/sys/windows/types_windows.go` in https://github.com/golang/go, so I figured I would start here and follow up with a https://github.com/golang/go PR/CL if reviewers here were amenable to this half.)

Change-Id: If3d0175bb889e4eddca838ef765faf5e379285ea
GitHub-Last-Rev: 7db84d6cd3
GitHub-Pull-Request: golang/sys#37
Reviewed-on: https://go-review.googlesource.com/c/sys/+/187578
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-07-26 09:17:11 +00:00
Jason A. Donenfeld
e40ef342dc windows: make CoTaskMemFree public
It's a useful function for people doing things with OLE32 from
elsewhere.

Change-Id: Id729d1c2b382ab4c097a06fb73448e671c0ad2f0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/182157
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-06-19 22:31:25 +00:00
Jason A. Donenfeld
516e3c2063 windows: allow determining real version number
Other functions, like GetVersion(), will lie about the OS version
depending on various win32 and manifest compatibility shims in place.
Calling RtlGetVersion is the proper way to retrieve the true OS version.

Change-Id: I2bd6d097dd763df51617cd825dc0ad300abf6212
Reviewed-on: https://go-review.googlesource.com/c/sys/+/182718
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-06-18 15:50:05 +00:00
Takuto Ikuta
d442b75600 windows: rename some arguments to follow MSDN
This is followup for CL 182097.

Change-Id: I55227381148dc5f83e185fafc294077b7a10df39
Reviewed-on: https://go-review.googlesource.com/c/sys/+/182218
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jason Donenfeld <Jason@zx2c4.com>
2019-06-14 08:40:37 +00:00
Jason A. Donenfeld
5ed2794edf windows: allow looking up well-known folder paths
This adds the recommended API for determining well-known folder paths,
such as where to place application configuration data. The MSDN
documentation mentions an optimization for the "current user" by passing
NULL as the token, so we provide both variants.

Updates golang/go#32248

Change-Id: I4a2d5d833543e6a0ba8f318944dd6493a0ec31d3
Reviewed-on: https://go-review.googlesource.com/c/sys/+/181637
Reviewed-by: Jason Donenfeld <Jason@zx2c4.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-06-13 12:46:09 +00:00
Takuto Ikuta
ab3f67ed27 windows: add OpenThread function
ref:
https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread

Change-Id: I4ba1d48603581e93c2f5cd9798f8ef067b714753
Reviewed-on: https://go-review.googlesource.com/c/sys/+/182097
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-06-13 10:11:56 +00:00
Takuto Ikuta
914ada52e9 windows: add GetProcessId function
ref:
https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-getprocessid

Document does not say anything about return value, but when I use 0 for HANDLE, GetProcessId
returns 0 with ERROR_INVALID_HANDLE from GetLastError().

Change-Id: Ib6782ccd5a4917e93ddbcf18c533921b12c67e3b
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180924
Reviewed-by: Marc-Antoine Ruel <maruel@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-06-13 09:49:25 +00:00
Takuto Ikuta
1e42afee0f windows: expose GetCurrentProcessId function
Change-Id: I537bce4415871f0d4669398bd72e4eb7c9c03481
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180921
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-06-10 08:10:24 +00:00
Jason A. Donenfeld
301114b31c windows: allow determining if manager is locked
The SCM can be locked by NT. While traditionally any process could lock
the SCM using "LockServiceDatabase", Microsoft removed this
functionality because it created so many bugs, and that function now
does nothing. However, the system itself, via the "NT Service Control
Manager", is still allowed to lock the SCM.

For example, at boot time on Windows 8.1, the SCM is locked after a
service is started until that service reports itself in a running state.
This poses a bit of a problem: it's useful to install device drivers
from inside services as part of their initialization, and mark the
service as having started only after the device has installed. But
device installation might potentially load new drivers, and drivers
themselves exist as a special type of service. This means that if a
driver is installed before marking the service as started, the entire
SCM will deadlock, and the OS will be partially unresponsive for a
minute or two.

Fortunately Microsoft supplies an API for exactly this purpose. The
solution is to mark the service as started before installing device
drivers, only under the circumstance that the SCM is locked. So, this
commit adds the proper API for determining this. It can be used like
this:

    if m, err := mgr.Connect(); err == nil {
        if lockStatus, err := m.LockStatus(); err == nil && lockStatus.IsLocked {
            log.Printf("SCM locked for %v by %s, marking service as started", lockStatus.Age, lockStatus.Owner)
            changes <- svc.Status{State: svc.Running}
        }
        m.Disconnect()
    }
    deviceDriver.Install()

This creates messages like the following, indicating that this API
works:

    SCM locked for 1s by .\NT Service Control Manager, marking service as started

Change-Id: Ic2f5b387e23efc3a287b2ab96ff84b357b712e36
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180977
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-06-09 08:25:36 +00:00
Jason A. Donenfeld
6df407bc07 windows: correct types and error values of internal GUID handling
This corrects the Windows int type to be the more correct int32 Go
analog, as well as not using GetLastError() for the error value of the
GUID string parsing function.

Change-Id: I9716f991ef649f7d299295e3f4e75d3986ec3a74
Reviewed-on: https://go-review.googlesource.com/c/sys/+/181397
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-06-09 07:43:00 +00:00
Takuto Ikuta
5b15430b70 windows: add Thread32{First,Next} funcions
ref:
https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/nf-tlhelp32-thread32first
https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/nf-tlhelp32-thread32next
https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/ns-tlhelp32-tagthreadentry32

Update #32404

Change-Id: I6c8150d1077cf1e8abd0b06403313fef01f4b6e4
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180918
Reviewed-by: Marc-Antoine Ruel <maruel@chromium.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-06-08 05:02:28 +00:00
Jason A. Donenfeld
83eebf7133 windows: properly plumb sidtype into service creation
Many service attributes are settable using the ordinary CreateService
function, but ones added later in Windows need to be set using the
ChangeServiceConfig2 function. One of these is the Description field,
which is nicely plumbed behind the scenes, so that users of the API can
set it, and the mgr package will just figure out what to do with it.
Another one that works exactly the same way is SidType. Support for its
constants was added in 30999d6 ("windows: add missing service
constants"), but it wasn't actually built into the mgr package's
configuration struct, creating inconstancies in interface.

This commit rectifies that by adding proper support to mgr's config
struct.

Change-Id: I4f148f2d2477a03a65e8a571a8401392f6fe9f28
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180978
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-06-08 04:32:40 +00:00
Jason A. Donenfeld
7fc4e5ec14 windows: add GUID handling functions
Virtually every project that works with x/sys/window's GUIDs winds up
rolling their own version of this in one way or another. So let's add
the correct win32 wrappers for it, so that these are always generated,
parsed, and converted in the uniform correct way.

Change-Id: I35f4b4ab5fc681d3e16fc5bbaf2cb20031eb3f12
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180938
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-06-06 20:33:20 +00:00
Jason A. Donenfeld
5da285871e windows: add accessor for duration since boot
In order to deal with boot time race conditions, sometimes it is
useful to determine the time since boot.

Change-Id: Ibc907b49a9b072b3ef3b6c94eec7e2e6428943ba
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180899
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
2019-06-06 16:51:38 +00:00