Commit Graph

152 Commits

Author SHA1 Message Date
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
Takuto Ikuta
79a91cf218 windows: add constants for access rights
This CL adds access rights constants for process and thread.
https://docs.microsoft.com/en-us/windows/desktop/procthread/process-security-and-access-rights
https://docs.microsoft.com/en-us/windows/desktop/procthread/thread-security-and-access-rights

Change-Id: I478216cf60c2e082d55a63479781d8f4640c73da
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180697
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-06 12:20:18 +00:00
Jason A. Donenfeld
4c4f7f33c9 windows: make it easier to iterate over groups and privileges
Rather than having to write grotesque things like:

    groups, _ := processToken.GetTokenGroups()
    for _, g := range (*[(1 << 28) - 1]windows.SIDAndAttributes)(unsafe.Pointer(&groups.Groups[0]))[:groups.GroupCount] {
        // ...
    }

Users can now write clean things like this:

    groups, _ := processToken.GetTokenGroups()
    for _, g := range groups.AllGroups() {
        // ...
    }

Change-Id: Ief06de6899c497175628ff51b9d6ae55a90d14f1
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178857
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-06-02 01:53:25 +00:00
Alex Brainman
afe098805b windows: delete mkerrors.go
mkerrors.go is called by 'go generate' command to generate
zerrors_windows.go

mkerrors.go executes mkerrors.bash. But mkerrors.bash requires
winerror.h to be present on the system to succeed. And winerror.h is
never present on the system. So executing mkerrors.go always fails.

zerrors_windows.go is never changes. It was changed recently once.
The change before that was in 2014.

This change removes mkerrors.go, so other commands that are part of
'go generate' execution can proceed. If someone needs to update
zerrors_windows.go, they can call mkerrors.bash directly.

Fixes golang/go#32349

Change-Id: I9b3e581e183377201e8f74c2d991d97564f36778
Reviewed-on: https://go-review.googlesource.com/c/sys/+/179582
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Jason Donenfeld <Jason@zx2c4.com>
2019-06-02 01:07:38 +00:00
Alex Brainman
46560c3f3c Revert "windows: make zsyscall_windows.go generatable on non-WSL env"
This reverts commit ad28b68e88.

Reason for revert: It completely breaks 'go generate' on windows

Update golang/go#32349

Change-Id: I4bedc6cf5130eb48c18c3208c8029433407430e4
Reviewed-on: https://go-review.googlesource.com/c/sys/+/179580
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
2019-05-31 07:31:56 +00:00
Takuto Ikuta
ad28b68e88 windows: make zsyscall_windows.go generatable on non-WSL env
mkerrors.bash requires a Windows host, as it hard-codes paths like
/mnt/c/Program Files (x86)/Windows Kits.
But this does not allow to generate in non-WSL env.

Currently mkerrors.bash runs earlier than mksyscall_windows.go
when we run `go generate` and it fails on non-WSL env.
So zsyscall_windows.go is not generated in that case.

To fix that, I reoreder of code generation in this CL so that we can
generate zsyscall_windows.go in non-WSL env.

Also update error message in mkerrors.bash.

Change-Id: I04eccdbfe1e1ff56f8c405250f0724000d66d579
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178781
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-30 18:20:44 +00:00
Takuto Ikuta
95d888ea44 windows: add GenerateConsoleCtrlEvent function
ref:
https://docs.microsoft.com/en-us/windows/console/generateconsolectrlevent

Change-Id: Id5db1d4c7d73a142405061353f578b1be93dbbb9
Reviewed-on: https://go-review.googlesource.com/c/sys/+/179538
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-30 18:20:36 +00:00
Takuto Ikuta
cc920278c2 windows: add SetInformationJobObject functions
This CL also adds some struct for the function.
https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_jobobject_extended_limit_information
https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_jobobject_basic_limit_information
https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_jobobject_basic_ui_restrictions
https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-io_counters

Change-Id: Ie3b7570fc344a25ad2a39129b74434b8baa1eed9
Reviewed-on: https://go-review.googlesource.com/c/sys/+/179039
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-29 11:55:39 +00:00
Takuto Ikuta
854af27f14 windows: add functions for priority class
took const value from
https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-getpriorityclass

Change-Id: I376bb8e1f5de8968177512857d60169cb7b7c776
Reviewed-on: https://go-review.googlesource.com/c/sys/+/179038
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-29 08:50:34 +00:00
Takuto Ikuta
9cd6430ef9 windows: add ResumeThread function
ref:
https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread

Change-Id: I114846c85bc5b8eb8dc742fdc3bdacb7b28cddaa
Reviewed-on: https://go-review.googlesource.com/c/sys/+/179037
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-27 10:42:16 +00:00
Jason A. Donenfeld
ea4c425e90 windows: allow Windows-style printf debugging via MessageBox
I realize that at this time, x/sys/windows is most certainly not aiming
to be some sort of UI library or anything remotely close to that.
However, MessageBox is sort of the one universal Windows API that's used
even for console programs. It's *the* classic debugging helper. It's
even pretty customary for console programs to display their usage
message in a message box rather than stdout. I realize this might be
somewhat disturbing to consider if you're in the blissful Unix utopia,
but on Windows, this is just sort of how things roll. Easy access to
MessageBox() is important.

Change-Id: I16183b69e8a27a5ddaf73d1d4e106bb7b201a6f0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178899
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-27 09:26:32 +00:00
Jason A. Donenfeld
56c252d053 windows: add ShellExecute
This is the way to do things like execute a process elevated with UAC
and interact with that whole system. It turns out to be quite important
for writing Windows software.

Change-Id: I5e05dc9b89ea308d42ac86ba563fd01922fc940c
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178898
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-05-27 09:25:20 +00:00
Jason A. Donenfeld
791d8a0f4d windows: allow determining if running 32-on-64bit
This is useful for determining whether or not it's going to be possible
to install device drivers, for example.

Change-Id: I628c6f3279b16832bcd6b4ca66dfa3e7334b88ff
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178897
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-05-26 05:23:59 +00:00
Jason A. Donenfeld
2219a0101f windows: add SID getter functions for the various components
These allow actual inspection of SIDs. For example, it might be
desirable to iterate through the group SIDs in order to find one set by
SERVICE_CONFIG_SERVICE_SID_INFO:

    for _, g := range groups {
        if g.Attributes != windows.SE_GROUP_ENABLED|windows.SE_GROUP_ENABLED_BY_DEFAULT|windows.SE_GROUP_OWNER {
            continue
        }
        if !g.Sid.IsValid() {
            continue
        }
        if g.Sid.IdentifierAuthority() != windows.SECURITY_NT_AUTHORITY {
            continue
        }
        if g.Sid.SubAuthorityCount() < 6 || g.Sid.SubAuthority(0) != 80 {
            continue
        }
        sid = g.Sid
        break
    }

Another usage of the APIs added would be to find if a user is in the
administrator group with either an elevated or unelevated token:

    isAdmin := false
    for _, g := range groups {
        if g.Attributes&(windows.SE_GROUP_ENABLED|windows.SE_GROUP_USE_FOR_DENY_ONLY) == 0 {
            continue
	}
        if !g.Sid.IsWellKnown(windows.WinBuiltinAdministratorsSid) {
            continue
	}
        isAdmin = true
        break
    }

Change-Id: I8f8dc8d37b71ec58fd51e21ea1f1b3aada6d66b0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/177841
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-05-26 03:56:09 +00:00
Takuto Ikuta
e44a3b55db windows: add SetErrorMode function
Change-Id: I79cd7d7e5b49b55281e63131388bc4693b7eed1e
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178779
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-26 03:10:47 +00:00
Jason A. Donenfeld
dbbf3f1254 windows: add IP() accessor to SocketAddress type
This is what everybody winds up doing with this object, so we make it
somewhat nicer than copying and pasting this everywhere or using type
aliases.

Change-Id: I3e12395cadfe212a7d01ce86478de9486383729a
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178577
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-24 15:25:21 +00:00
Takuto Ikuta
c3d486d151 windows: add JobObject functions
Change-Id: Ib8c3fe735ed72e3b211328c89170eb03fc64b102
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178777
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-24 15:12:52 +00:00
Jason A. Donenfeld
8097e1b27f windows: do not query library for inline functions
The GetCurrent*Token variety of functions are actually implemented as
inline functions in the header files of the SDK. Attempting to call out
to these as library functions is an error. This commit also adds a test
to ensure that these work as expected.

Change-Id: I105f1ca1a8936114fe61bc22188200c31f240a23
Reviewed-on: https://go-review.googlesource.com/c/sys/+/177840
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-22 04:47:17 +00:00
Jason A. Donenfeld
30999d67c8 windows: add missing service constants
This adds a number of useful comments for ChangeServiceConfig2. It looks
like the MingW headers have these in two different places, awkwardly,
and whoever imported these constants missed the second half of them,
whereas the real Microsoft header files have them all together in one
place.

Change-Id: I723c4bcd86e5a1b905cb738c5c2b49805a354af4
Reviewed-on: https://go-review.googlesource.com/c/sys/+/177842
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-22 04:46:51 +00:00
Jason A. Donenfeld
c46e0d965b windows/registry: do not generate unaligned loads
Byte slices aren't necessarily aligned, which means casting them to
integer types and dereferencing may result in an unaligned load. This
is mostly fine on Intel but isn't necessarily fine on other platforms.
Any good compiler will generate optimal code for the platform using the
pattern of this commit.

Change-Id: I6dd8debad1cb850b8562ee96ae0f366d1f822a6f
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176857
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-05-22 03:42:48 +00:00
Jason A. Donenfeld
61b9204099 windows: add functions for dealing with elevated tokens
These are required when dealing with UAC or launching processes as
elevated administrators on behalf of other users.

Change-Id: If256c838b1f0202a8703d91496ffdbe16be3a700
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176858
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-05-16 11:00:30 +00:00
Jason A. Donenfeld
cedb8e16d1 windows: add token group adjustment function
We have the useful AdjustTokenPrivileges, but we don't have the equally
as useful AdjustTokenGroups function. So this CL adds it.

Change-Id: Ic09b4688ee5ec7f1a626a21216a85b075961aad5
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176859
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-05-16 10:27:23 +00:00
Jason A. Donenfeld
06a5c49444 windows: add token environment functions
This brings the x/sys package into parity with the capabilities provided
indirectly in CL 176619, and adds a helper to make it useful.

Change-Id: I81f2d205bbb0c2b2c530b2bd991c1e6ff30cc94e
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176620
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-05-15 12:05:40 +00:00
Jason A. Donenfeld
f91f9b37d0 windows: add basic WTS functions for windows/svc usage
The svc package exposes svc.SessionChange, but it's impossible to do
anything with them without these structures, and without being able to
enumerate them prior to events, the events themselves aren't useful, so
we add the enumeration functions as well.

Change-Id: I14c932dfe97c6712fd4868c1b3a0e3a61a6a562c
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176623
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-05-15 10:53:50 +00:00
Jason A. Donenfeld
24a5b2278f windows: add service notification support
This lays the groundwork for service notification and tracking by adding
the required API functions. Users can make notifiers directly using it,
or later if we're feeling ambitious, we can see if we can come up with a
generalized solution in x/windows/svc.

Change-Id: I80503cc27970fbb23bf17cd8bc50eaa7787aa6bd
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176624
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-05-15 10:31:49 +00:00
Jason A. Donenfeld
3a4b5fb9f7 windows: add token manipulation functions and constants
These are extremely useful functions and core to the Windows security
API. They are so useful, in fact, that most of these were taken right
out of the Go repo's internal/syscall/windows package.

Change-Id: I13e34b830dd60f59fcae8085ae2be189d9cc9282
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176625
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-14 13:59:07 +00:00
Tobias Klauser
a5b02f93d8 windows: add "generate" build tag
cmd/go supports the "generate" build tag as of CL 175983. Add it to the
files which are just used for generating errors and syscall wrappers.

Also see golang/go#31920

Change-Id: Ib26c90af0ac1fb7bae81366a46dedf028b787566
Reviewed-on: https://go-review.googlesource.com/c/sys/+/175899
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-09 14:14:14 +00:00
Jason A. Donenfeld
ca7f33d411 windows: CreateFile's templatefile parameter has always been a HANDLE
This fixes a bug in the declaration. The documentation involving this
parameter is at:
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilew

Change-Id: I650c54b3110d29921627b08d47be4fa41d60bbf4
Reviewed-on: https://go-review.googlesource.com/c/sys/+/175159
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
2019-05-06 11:50:46 +00:00
Brad Fitzpatrick
d89cdac9e8 windows: don't return EINVAL on zero Chmod mode
To match CL 174320

Updates golang/go#20858

Change-Id: Ic4e332c8a84b7d427fc7057aa5b0c2877cc9f7ea
Reviewed-on: https://go-review.googlesource.com/c/sys/+/174321
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-04-29 19:08:28 +00:00
Jason A. Donenfeld
2cc0cad0ac windows: document new ProcessId field
Change-Id: I761f962f30eb48471a0f033cf30ea6c428d3ce49
Reviewed-on: https://go-review.googlesource.com/c/sys/+/174237
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2019-04-29 09:44:11 +00:00
Jason A. Donenfeld
1607263960 windows: allow querying service ProcessId
The two functions have the same levels of compatibility, but this latter
one gives us access to the process ID, which is important for things
like WFP whitelisting. The change required is fairly trivial too.

Change-Id: Ifb6b3ee3e897202b9cffa1388c53c25cbcfede61
Reviewed-on: https://go-review.googlesource.com/c/sys/+/173666
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-04-25 14:56:19 +00:00
James Nugent
16da32be82 windows: add GetFileInformationByHandleEx function
Change-Id: I950762ef59c665d027641b4410ac30697edad22d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/171939
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-04-15 08:10:28 +00:00