forked from paleg/libadclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadclient.go
541 lines (448 loc) · 11.4 KB
/
adclient.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package adclient
// #cgo CPPFLAGS: -DOPENLDAP -DKRB5
// #cgo LDFLAGS: -lstdc++ -lldap -lsasl2 -lstdc++ -llber -lresolv -lkrb5
import "C"
import "fmt"
import "strings"
import "strconv"
type ADError struct {
msg string
code int
}
type ADConnParams struct {
Domain string
Site string
Uries []string
Binddn string
Bindpw string
Search_base string
Secured bool
UseGSSAPI bool
Nettimeout int
Timelimit int
Onssl bool
}
func DefaultADConnParams() (params ADConnParams) {
params.Nettimeout = -1
params.Timelimit = -1
params.Secured = true
params.UseGSSAPI = false
return
}
func LdapPrefix() string {
return GetAdclientLdap_prefix()
}
func (err ADError) Error() string {
return fmt.Sprintf("%v: %v", err.code, err.msg)
}
func catch(err *error) {
if r := recover(); r != nil {
err_splitted := strings.SplitN(r.(string), ":", 2)
if len(err_splitted) != 2 {
*err = ADError{
r.(string),
-1,
}
} else {
code, code_err := strconv.Atoi(err_splitted[0])
if code_err != nil {
code = -1
}
*err = ADError{
err_splitted[1],
code,
}
}
}
}
func vector2slice(vector StringVector) []string {
result := make([]string, vector.Size())
for i := 0; i < int(vector.Size()); i++ {
result[i] = vector.Get(i)
}
return result
}
func commonStringToSlice(f func(string) StringVector, thing string) (result []string, err error) {
defer catch(&err)
vector := f(thing)
defer DeleteStringVector(vector)
result = vector2slice(vector)
return
}
func common2StringsToSlice(f func(string, string) StringVector, thing1 string, thing2 string) (result []string, err error) {
defer catch(&err)
vector := f(thing1, thing2)
defer DeleteStringVector(vector)
result = vector2slice(vector)
return
}
func commonStringsIntToSlice(f func(string, int) StringVector, thing1 string, thing2 int) (result []string, err error) {
defer catch(&err)
vector := f(thing1, thing2)
defer DeleteStringVector(vector)
result = vector2slice(vector)
return
}
func commonEmptyToSlice(f func() StringVector) (result []string, err error) {
defer catch(&err)
vector := f()
defer DeleteStringVector(vector)
result = vector2slice(vector)
return
}
var ad Adclient
func New() {
ad = NewAdclient()
}
func Delete() {
DeleteAdclient(ad)
}
func Login(_params ADConnParams) (err error) {
defer catch(&err)
params := NewAdConnParams()
defer DeleteAdConnParams(params)
params.SetDomain(_params.Domain)
params.SetSite(_params.Site)
params.SetBinddn(_params.Binddn)
params.SetBindpw(_params.Bindpw)
params.SetSearch_base(_params.Search_base)
params.SetSecured(_params.Secured)
params.SetUse_gssapi(_params.UseGSSAPI)
params.SetNettimeout(_params.Nettimeout)
params.SetTimelimit(_params.Timelimit)
params.SetOnssl(_params.Onssl)
uries := NewStringVector()
defer DeleteStringVector(uries)
for _, uri := range _params.Uries {
uries.Add(uri)
}
params.SetUries(uries)
ad.Login(params)
return
}
func LoginOld(uri interface{}, user string, passwd string, sb string, secured bool) (err error) {
defer catch(&err)
args := DefaultADConnParams()
args.Binddn = user
args.Bindpw = passwd
args.Search_base = sb
args.UseGSSAPI = secured
switch uri.(type) {
case string:
args.Domain = uri.(string)
case []string:
args.Uries = uri.([]string)
default:
err = ADError{
fmt.Sprintf("unknown uri type - %#v", uri),
-1,
}
}
Login(args)
return
}
func BindedUri() (result string) {
return ad.Binded_uri()
}
func SearchBase() (result string) {
return ad.Search_base()
}
func LoginMethod() (result string) {
return ad.Login_method()
}
func GroupAddUser(group string, user string) (err error) {
defer catch(&err)
ad.GroupAddUser(group, user)
return
}
func GroupRemoveUser(group string, user string) (err error) {
defer catch(&err)
ad.GroupRemoveUser(group, user)
return
}
func CreateComputer(name string, container string) (err error) {
defer catch(&err)
ad.CreateComputer(name, container)
return
}
func CreateUser(cn string, container string, user_short string) (err error) {
defer catch(&err)
ad.CreateUser(cn, container, user_short)
return
}
func CreateGroup(cn string, container string, group_short string) (err error) {
defer catch(&err)
ad.CreateGroup(cn, container, group_short)
return
}
func DeleteDN(dn string) (err error) {
defer catch(&err)
ad.DeleteDN(dn)
return
}
func RenameDN(dn string, new_rdn string) (err error) {
defer catch(&err)
ad.RenameDN(dn, new_rdn)
return
}
func RenameGroup(dn string, new_rdn string) (err error) {
defer catch(&err)
ad.RenameGroup(dn, new_rdn)
return
}
func RenameUser(old_sam string, new_sam string, new_rdn string) (err error) {
defer catch(&err)
ad.RenameUser(old_sam, new_sam, new_rdn)
return
}
func CreateOU(ou string) (err error) {
defer catch(&err)
ad.CreateOU(ou)
return
}
func EnableUser(user string) (err error) {
defer catch(&err)
ad.EnableUser(user)
return
}
func DisableUser(user string) (err error) {
defer catch(&err)
ad.DisableUser(user)
return
}
func UnLockUser(user string) (err error) {
defer catch(&err)
ad.UnLockUser(user)
return
}
func SetUserPassword(user string, password string) (err error) {
defer catch(&err)
ad.SetUserPassword(user, password)
return
}
func SetUserDialinAllowed(user string) (err error) {
defer catch(&err)
ad.SetUserDialinAllowed(user)
return
}
func SetUserDialinDisabled(user string) (err error) {
defer catch(&err)
ad.SetUserDialinDisabled(user)
return
}
func CheckUserPassword(user string, password string) (result bool, err error) {
defer catch(&err)
result = ad.CheckUserPassword(user, password)
return
}
func SetUserSN(user string, sn string) (err error) {
defer catch(&err)
ad.SetUserSN(user, sn)
return
}
func SetUserInitials(user string, initials string) (err error) {
defer catch(&err)
ad.SetUserInitials(user, initials)
return
}
func SetUserGivenName(user string, givenName string) (err error) {
defer catch(&err)
ad.SetUserGivenName(user, givenName)
return
}
func SetUserDisplayName(user string, displayName string) (err error) {
defer catch(&err)
ad.SetUserDisplayName(user, displayName)
return
}
func SetUserRoomNumber(user string, roomNum string) (err error) {
defer catch(&err)
ad.SetUserRoomNumber(user, roomNum)
return
}
func SetUserAddress(user string, streetAddress string) (err error) {
defer catch(&err)
ad.SetUserAddress(user, streetAddress)
return
}
func SetUserInfo(user string, info string) (err error) {
defer catch(&err)
ad.SetUserInfo(user, info)
return
}
func SetUserTitle(user string, title string) (err error) {
defer catch(&err)
ad.SetUserTitle(user, title)
return
}
func SetUserDepartment(user string, department string) (err error) {
defer catch(&err)
ad.SetUserDepartment(user, department)
return
}
func SetUserCompany(user string, company string) (err error) {
defer catch(&err)
ad.SetUserCompany(user, company)
return
}
func SetUserPhone(user string, phone string) (err error) {
defer catch(&err)
ad.SetUserPhone(user, phone)
return
}
func SetUserDescription(user string, descr string) (err error) {
defer catch(&err)
ad.SetUserDescription(user, descr)
return
}
func GetUserControls(user string) (result map[string]bool, err error) {
result = make(map[string]bool)
defer catch(&err)
cmap := ad.GetUserControls(user)
defer DeleteStringBoolMap(cmap)
keys := cmap.Keys()
for i := 0; i < int(keys.Size()); i++ {
key := keys.Get(i)
result[key] = cmap.Get(key)
}
return
}
func GetUserControl(user string, control string) (result bool, err error) {
defer catch(&err)
result = ad.GetUserControl(user, control)
return
}
func IfUserExpired(user string) (result bool, err error) {
defer catch(&err)
result = ad.IfUserExpired(user)
return
}
func IfUserLocked(user string) (result bool, err error) {
defer catch(&err)
result = ad.IfUserLocked(user)
return
}
func IfUserDisabled(user string) (result bool, err error) {
defer catch(&err)
result = ad.IfUserDisabled(user)
return
}
func IfUserMustChangePassword(user string) (result bool, err error) {
defer catch(&err)
result = ad.IfUserMustChangePassword(user)
return
}
func IfUserDontExpirePassword(user string) (result bool, err error) {
defer catch(&err)
result = ad.IfUserDontExpirePassword(user)
return
}
func GetObjectDN(object string) (result string, err error) {
defer catch(&err)
result = ad.GetObjectDN(object)
return
}
func GetUserDisplayName(user string) (result string, err error) {
defer catch(&err)
result = ad.GetUserDisplayName(user)
return
}
func IfDialinUser(user string) (result bool, err error) {
defer catch(&err)
result = ad.IfDialinUser(user)
return
}
func IfDNExists(args ...string) (result bool, err error) {
defer catch(&err)
switch len(args) {
case 1:
result = ad.IfDNExists(args[0])
case 2:
result = ad.IfDNExists(args[0], args[1])
default:
panic("wrong number of args for IfDNExists")
}
return
}
func GetGroups() ([]string, error) {
return commonEmptyToSlice(ad.GetGroups)
}
func GetUsers() ([]string, error) {
return commonEmptyToSlice(ad.GetUsers)
}
func GetOUs() ([]string, error) {
return commonEmptyToSlice(ad.GetOUs)
}
func GetDialinUsers() ([]string, error) {
return commonEmptyToSlice(ad.GetDialinUsers)
}
func GetDisabledUsers() ([]string, error) {
return commonEmptyToSlice(ad.GetDisabledUsers)
}
func GetUserGroups(user string, nested bool) (result []string, err error) {
defer catch(&err)
vector := ad.GetUserGroups(user, nested)
defer DeleteStringVector(vector)
result = vector2slice(vector)
return
}
func GetUsersInGroup(group string, nested bool) (result []string, err error) {
defer catch(&err)
vector := ad.GetUsersInGroup(group, nested)
defer DeleteStringVector(vector)
result = vector2slice(vector)
return
}
func GetGroupsInOU(OU string, scope int) (result []string, err error) {
return commonStringsIntToSlice(ad.GetGroupsInOU, OU, scope)
}
func GetComputersInOU(OU string, scope int) (result []string, err error) {
return commonStringsIntToSlice(ad.GetComputersInOU, OU, scope)
}
func GetOUsInOU(OU string, scope int) (result []string, err error) {
return commonStringsIntToSlice(ad.GetOUsInOU, OU, scope)
}
func GetUsersInOU(OU string, scope int) (result []string, err error) {
return commonStringsIntToSlice(ad.GetUsersInOU, OU, scope)
}
/*
struct berval getBinaryObjectAttribute(string object, string attribute);
*/
func GetObjectAttribute(object string, attribute string) (result []string, err error) {
return common2StringsToSlice(ad.GetObjectAttribute, object, attribute)
}
func SearchDN(search_base string, filter string, scope int) (result []string, err error) {
defer catch(&err)
vector := ad.SearchDN(search_base, filter, scope)
defer DeleteStringVector(vector)
result = vector2slice(vector)
return
}
/*
map < string, map < string, std::vector<string> > > search(string OU, int scope, string filter, const std::vector <string> &attributes);
*/
func GetObjectAttributes(object string, attrs ...string) (result map[string][]string, err error) {
cattrs := NewStringVector()
defer DeleteStringVector(cattrs)
if len(attrs) == 0 {
cattrs.Add("*")
} else {
for _, attr := range attrs {
cattrs.Add(attr)
}
}
result = make(map[string][]string)
defer catch(&err)
cmap := ad.GetObjectAttributes(object, cattrs)
defer DeleteString_VectorString_Map(cmap)
keys := cmap.Keys()
for i := 0; i < int(keys.Size()); i++ {
key := keys.Get(i)
value := cmap.Get(key)
result[key] = vector2slice(value)
}
return
}
/*
map <string, std::vector <string> > GetObjectAttributes(string object, const std::vector<string> &attributes);
*/