Browse Source

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>
Jason A. Donenfeld 6 years ago
parent
commit
4c4f7f33c9
1 changed files with 12 additions and 2 deletions
  1. 12 2
      windows/security_windows.go

+ 12 - 2
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 {