From 4c4f7f33c9ed00de01c4c741d2177abfcfe19307 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 25 May 2019 12:09:16 +0200 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Alex Brainman --- windows/security_windows.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/windows/security_windows.go b/windows/security_windows.go index b84840fe..61b49647 100644 --- a/windows/security_windows.go +++ b/windows/security_windows.go @@ -603,12 +603,22 @@ type Tokenprimarygroup struct { type Tokengroups struct { GroupCount uint32 - Groups [1]SIDAndAttributes + Groups [1]SIDAndAttributes // Use AllGroups() for iterating. +} + +// AllGroups returns a slice that can be used to iterate over the groups in g. +func (g *Tokengroups) AllGroups() []SIDAndAttributes { + return (*[(1 << 28) - 1]SIDAndAttributes)(unsafe.Pointer(&g.Groups[0]))[:g.GroupCount:g.GroupCount] } type Tokenprivileges struct { PrivilegeCount uint32 - Privileges [1]LUIDAndAttributes + Privileges [1]LUIDAndAttributes // Use AllPrivileges() for iterating. +} + +// AllPrivileges returns a slice that can be used to iterate over the privileges in p. +func (p *Tokenprivileges) AllPrivileges() []LUIDAndAttributes { + return (*[(1 << 27) - 1]LUIDAndAttributes)(unsafe.Pointer(&p.Privileges[0]))[:p.PrivilegeCount:p.PrivilegeCount] } type Tokenmandatorylabel struct {