-
Notifications
You must be signed in to change notification settings - Fork 100
/
groups.go
executable file
·283 lines (248 loc) · 9.99 KB
/
groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//go:build windows && amd64
// +build windows,amd64
package winapi
import (
"fmt"
"os"
"strings"
"syscall"
"unsafe"
so "github.com/iamacarpet/go-win64api/shared"
)
var (
usrNetLocalGroupAdd = modNetapi32.NewProc("NetLocalGroupAdd")
usrNetLocalGroupEnum = modNetapi32.NewProc("NetLocalGroupEnum")
usrNetLocalGroupDel = modNetapi32.NewProc("NetLocalGroupDel")
usrNetLocalGroupSetMembers = modNetapi32.NewProc("NetLocalGroupSetMembers")
usrNetLocalGroupGetMembers = modNetapi32.NewProc("NetLocalGroupGetMembers")
)
// Possible errors returned by local group management functions
// Error code enumerations taken from MS-ERREF documentation:
// https://msdn.microsoft.com/en-us/library/cc231196.aspx
const (
NERR_GroupNotFound syscall.Errno = 2220 // 0x000008AC
ERROR_ACCESS_DENIED syscall.Errno = 5 // 0x00000005
ERROR_MEMBER_NOT_IN_ALIAS syscall.Errno = 1377 // 0x00000561
ERROR_MEMBER_IN_ALIAS syscall.Errno = 1378 // 0x00000562
ERROR_NO_SUCH_MEMBER syscall.Errno = 1387 // 0x0000056B
ERROR_INVALID_MEMBER syscall.Errno = 1388 // 0x0000056C
)
// LOCALGROUP_INFO_0 represents level 0 information about local Windows groups.
// This struct matches the struct definition in the Windows headers (lmaccess.h).
type LOCALGROUP_INFO_0 struct {
Lgrpi0_name *uint16 // UTF-16 group name
}
// LOCALGROUP_INFO_1 represents level 1 information about local Windows groups.
// This struct matches the struct definition in the Windows headers (lmaccess.h).
type LOCALGROUP_INFO_1 struct {
Lgrpi1_name *uint16 // UTF-16 group name
Lgrpi1_comment *uint16 // UTF-16 group comment
}
// LocalGroupAdd adds a new local group with the specified name and comment.
func LocalGroupAdd(name, comment string) (bool, error) {
var parmErr uint32
var err error
var gInfo LOCALGROUP_INFO_1
gInfo.Lgrpi1_name, err = syscall.UTF16PtrFromString(name)
if err != nil {
return false, fmt.Errorf("Unable to encode group name to UTF16: %s", err)
}
gInfo.Lgrpi1_comment, err = syscall.UTF16PtrFromString(comment)
if err != nil {
return false, fmt.Errorf("Unable to encode comment to UTF16: %s", err)
}
ret, _, _ := usrNetLocalGroupAdd.Call(
uintptr(0), // server name
uintptr(uint32(1)), // information level
uintptr(unsafe.Pointer(&gInfo)), // group information
uintptr(unsafe.Pointer(&parmErr)), // error code out param
)
if ret != NET_API_STATUS_NERR_Success {
return false, syscall.Errno(ret)
}
return true, nil
}
// ListLocalGroups enumerates the local groups defined on the system.
//
// If an error occurs in the call to the underlying NetLocalGroupEnum function, the
// returned error will be a syscall.Errno containing the error code.
// See: https://docs.microsoft.com/en-us/windows/desktop/api/lmaccess/nf-lmaccess-netlocalgroupenum
func ListLocalGroups() ([]so.LocalGroup, error) {
var (
dataPointer uintptr
resumeHandle uintptr
entriesRead uint32
entriesTotal uint32
sizeTest LOCALGROUP_INFO_1
retVal = make([]so.LocalGroup, 0)
)
ret, _, _ := usrNetLocalGroupEnum.Call(
uintptr(0), // servername
uintptr(uint32(1)), // level, LOCALGROUP_INFO_1
uintptr(unsafe.Pointer(&dataPointer)), // struct buffer for output data.
uintptr(uint32(USER_MAX_PREFERRED_LENGTH)), // allow as much memory as required.
uintptr(unsafe.Pointer(&entriesRead)),
uintptr(unsafe.Pointer(&entriesTotal)),
uintptr(unsafe.Pointer(&resumeHandle)),
)
if ret != NET_API_STATUS_NERR_Success {
return nil, syscall.Errno(ret)
} else if dataPointer == uintptr(0) {
return nil, fmt.Errorf("null pointer while fetching entry")
}
defer usrNetApiBufferFree.Call(dataPointer)
var iter = dataPointer
for i := uint32(0); i < entriesRead; i++ {
var data = (*LOCALGROUP_INFO_1)(unsafe.Pointer(iter))
gd := so.LocalGroup{
Name: UTF16toString(data.Lgrpi1_name),
Comment: UTF16toString(data.Lgrpi1_comment),
}
retVal = append(retVal, gd)
iter = uintptr(unsafe.Pointer(iter + unsafe.Sizeof(sizeTest)))
}
return retVal, nil
}
// LocalGroupDel deletes the specified local group.
//
// If an error occurs in the call to the underlying NetLocalGroupDel function, the
// returned error will be a syscall.Errno containing the error code.
// See: https://docs.microsoft.com/en-us/windows/desktop/api/lmaccess/nf-lmaccess-netlocalgroupdel
func LocalGroupDel(name string) (bool, error) {
namePtr, err := syscall.UTF16PtrFromString(name)
if err != nil {
return false, fmt.Errorf("Unable to encode group name to UTF16: %s", err)
}
ret, _, _ := usrNetLocalGroupDel.Call(
uintptr(0), // servername
uintptr(unsafe.Pointer(namePtr)),
)
if ret != NET_API_STATUS_NERR_Success {
return false, syscall.Errno(ret)
}
return true, nil
}
func localGroupModMembers(proc *syscall.LazyProc, groupname string, usernames []string) (bool, error) {
memberInfos := make([]LOCALGROUP_MEMBERS_INFO_3, 0, len(usernames))
hostname, err := os.Hostname()
if err != nil {
return false, fmt.Errorf("Unable to determine hostname: %s", err)
}
groupnamePtr, err := syscall.UTF16PtrFromString(groupname)
if err != nil {
return false, fmt.Errorf("Unable to encode group name to UTF16: %s", err)
}
for _, username := range usernames {
domainAndUsername := username
if !strings.ContainsRune(username, '\\') {
domainAndUsername = fmt.Sprintf(`%s\%s`, hostname, username)
}
namePtr, err := syscall.UTF16PtrFromString(domainAndUsername)
if err != nil {
return false, fmt.Errorf("Unable to encode username to UTF16: %s", err)
}
memberInfos = append(memberInfos, LOCALGROUP_MEMBERS_INFO_3{
Lgrmi3_domainandname: namePtr,
})
}
if len(memberInfos) == 0 {
// Add a fake entry just so that the slice isn't empty, so we can take
// the address of the first entry
memberInfos = append(memberInfos, LOCALGROUP_MEMBERS_INFO_3{})
}
ret, _, _ := proc.Call(
uintptr(0), // servername
uintptr(unsafe.Pointer(groupnamePtr)), // group name
uintptr(3), // level, LOCALGROUP_MEMBERS_INFO_3
uintptr(unsafe.Pointer(&memberInfos[0])), // buf
uintptr(len(usernames)), // totalEntries
)
if ret != NET_API_STATUS_NERR_Success {
return false, syscall.Errno(ret)
}
return true, nil
}
// LocalGroupSetMembers sets the membership of the group to contain exactly the
// set of users specified in usernames.
//
// If an error occurs in the call to the underlying NetLocalGroupSetMembers function, the
// returned error will be a syscall.Errno containing the error code.
// See: https://docs.microsoft.com/en-us/windows/desktop/api/lmaccess/nf-lmaccess-netlocalgroupsetmembers
func LocalGroupSetMembers(groupname string, usernames []string) (bool, error) {
return localGroupModMembers(usrNetLocalGroupSetMembers, groupname, usernames)
}
// LocalGroupAddMembers adds the specified members to the group, if they are not
// already members.
//
// If an error occurs in the call to the underlying NetLocalGroupAddMembers function, the
// returned error will be a syscall.Errno containing the error code.
// See: https://docs.microsoft.com/en-us/windows/desktop/api/lmaccess/nf-lmaccess-netlocalgroupaddmembers
func LocalGroupAddMembers(groupname string, usernames []string) (bool, error) {
return localGroupModMembers(usrNetLocalGroupAddMembers, groupname, usernames)
}
// LocalGroupDelMembers removes the specified members from the local group.
//
// If an error occurs in the call to the underlying NetLocalGroupDelMembers function, the
// returned error will be a syscall.Errno containing the error code.
// See: https://docs.microsoft.com/en-us/windows/desktop/api/lmaccess/nf-lmaccess-netlocalgroupdelmembers
func LocalGroupDelMembers(groupname string, usernames []string) (bool, error) {
return localGroupModMembers(usrNetLocalGroupDelMembers, groupname, usernames)
}
// LocalGroupGetMembers returns information about the members of the specified
// local group.
//
// If an error occurs in the call to the underlying NetLocalGroupGetMembers function, the
// returned error will be a syscall.Errno containing the error code.
// See: https://docs.microsoft.com/en-us/windows/desktop/api/lmaccess/nf-lmaccess-netlocalgroupgetmembers
func LocalGroupGetMembers(groupname string) ([]so.LocalGroupMember, error) {
var (
dataPointer uintptr
resumeHandle uintptr
entriesRead uint32
entriesTotal uint32
sizeTest LOCALGROUP_MEMBERS_INFO_3
retVal []so.LocalGroupMember = make([]so.LocalGroupMember, 0)
)
groupnamePtr, err := syscall.UTF16PtrFromString(groupname)
if err != nil {
return nil, fmt.Errorf("Unable to encode group name to UTF16: %s", err)
}
ret, _, _ := usrNetLocalGroupGetMembers.Call(
uintptr(0), // servername
uintptr(unsafe.Pointer(groupnamePtr)), // group name
uintptr(3), // level, LOCALGROUP_MEMBERS_INFO_3
uintptr(unsafe.Pointer(&dataPointer)), // bufptr
uintptr(uint32(USER_MAX_PREFERRED_LENGTH)), // prefmaxlen
uintptr(unsafe.Pointer(&entriesRead)), // entriesread
uintptr(unsafe.Pointer(&entriesTotal)), // totalentries
uintptr(unsafe.Pointer(&resumeHandle)), // resumehandle
)
if ret != NET_API_STATUS_NERR_Success {
return nil, syscall.Errno(ret)
} else if dataPointer == uintptr(0) {
return nil, fmt.Errorf("null pointer while fetching entry")
}
defer usrNetApiBufferFree.Call(dataPointer)
var iter = dataPointer
for i := uint32(0); i < entriesRead; i++ {
var data = (*LOCALGROUP_MEMBERS_INFO_3)(unsafe.Pointer(iter))
domainAndUsername := UTF16toString(data.Lgrmi3_domainandname)
split := strings.SplitN(domainAndUsername, "\\", 1)
var domain, name string
if len(split) > 1 {
domain = split[0]
name = split[1]
} else {
// This really shouldn't happen, but just in case...
name = split[0]
}
gd := so.LocalGroupMember{
Domain: domain,
Name: name,
DomainAndName: domainAndUsername,
}
retVal = append(retVal, gd)
iter = uintptr(unsafe.Pointer(iter + unsafe.Sizeof(sizeTest)))
}
return retVal, nil
}