-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathadclient.py
337 lines (264 loc) · 11.7 KB
/
adclient.py
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
"""
Active Directory manipulation class wrapper for low level c++ class.
"""
import sys
import _adclient
from _adclient import *
class ADConnParams(object):
def __init__(self):
self.domain = ""
self.site = ""
self.binddn = ""
self.bindpw = ""
self.search_base = ""
self.secured = True
self.use_gssapi = False
self.use_tls = False
self.use_ldaps = False
self.nettimeout = -1
self.timelimit = -1
self.uries = []
class ADClient(object):
"""
ADClient.login can throw ADBindError on errors.
all search functions can throw ADSearchError on errors.
all modify functions can throw both ADSearchError and ADOperationalError on errors.
text description will be in exception object
numeric code can be obtained from 'get_error_num' function
"""
def __init__(self):
self.obj = _adclient.new_adclient()
def login(self, params, binddn="", bindpw="", search_base="", secured=True):
if not isinstance(params, ADConnParams):
uries = params
params = ADConnParams()
if sys.version_info[0] == 3:
string_type = str
else:
string_type = basestring
if isinstance(uries, string_type):
params.domain = uries
elif isinstance(uries, list):
params.uries = uries
params.binddn = binddn
params.bindpw = bindpw
params.search_base = search_base
params.secured = secured
_adclient.login_adclient(self.obj, params.__dict__)
def binded_uri(self):
return _adclient.binded_uri_adclient(self.obj)
def search_base(self):
return _adclient.search_base_adclient(self.obj)
def login_method(self):
return _adclient.login_method_adclient(self.obj)
def bind_method(self):
return _adclient.bind_method_adclient(self.obj)
def searchDN(self, search_base, filter, scope):
""" It returns list with DNs found with 'filter'
"""
return _adclient.searchDN_adclient(self.obj, search_base, filter, scope)
def search(self, ou, scope, filter, attributes):
""" General search function.
It returns dict with users found with 'filter' with specified 'attributes'.
"""
return _adclient.search_adclient(self.obj, ou, scope, filter, attributes)
def getUserGroups(self, user, nested=False):
""" It returns list with "user" groups if operation was successfull.
"""
return _adclient.getUserGroups_adclient(self.obj, user, nested)
def getUsersInGroup(self, group, nested=False):
""" It returns list with members of Active Directory "group" if operation was successfull.
"""
return _adclient.getUsersInGroup_adclient(self.obj, group, nested)
def getUserControls(self, user):
""" It returns map with "user" controls:
('disabled', 'locked', 'dontExpirePassword', 'mustChangePassword', 'expired').
"""
return _adclient.getUserControls_adclient(self.obj, user)
def groupAddUser(self, group, user):
""" It adds "user" to Active Directory "group".
It returns nothing if operation was successfull.
"""
_adclient.groupAddUser_adclient(self.obj, group, user)
def groupRemoveUser(self, group, user):
""" It removes "user" from Active Directory "group".
It returns nothing if operation was successfull.
"""
_adclient.groupRemoveUser_adclient(self.obj, group, user)
def ifDialinUser(self, user):
""" It returns True if msNPAllowDialin user attribute set to TRUE, False - otherwise.
"""
if _adclient.ifDialinUser_adclient(self.obj, user) == 1:
return True
else:
return False
def getDialinUsers(self):
""" It returns list of all users with msNPAllowDialin = TRUE.
"""
return _adclient.getDialinUsers_adclient(self.obj)
def getDisabledUsers(self):
""" It returns list of all users with ADS_UF_ACCOUNTDISABLE in userAccountControl.
"""
return _adclient.getDisabledUsers_adclient(self.obj)
def getObjectDN(self, user):
""" It returns user DN by short name.
"""
return _adclient.getObjectDN_adclient(self.obj, user)
def ifUserDisabled(self, user):
""" It returns True if UserAccountControl flag contain ACCOUNTDISABLE property,
False - otherwise.
"""
return _adclient.ifUserDisabled_adclient(self.obj, user)
def ifDNExists(self, dn, objectclass='*'):
""" It returns True of False depends on object DN existence.
dn objectclass can be limited with corresponding argument.
"""
return _adclient.ifDNExists_adclient(self.obj, dn, objectclass)
def getOUs(self):
""" It returns list of all organizationalUnits in Active Directory.
"""
return _adclient.getOUs_adclient(self.obj)
def getUsersInOU(self, OU, scope):
""" It returns list of all users in OU.
"""
return _adclient.getUsersInOU_adclient(self.obj, OU, scope)
def getComputersInOU(self, OU, scope):
""" It returns list of all users in OU.
"""
return _adclient.getComputersInOU_adclient(self.obj, OU, scope)
def getGroupsInOU(self, OU, scope):
""" It returns list of all users in OU.
"""
return _adclient.getGroupsInOU_adclient(self.obj, OU, scope)
def getGroups(self):
""" It returns list of all groups in Active Directory.
"""
return _adclient.getGroups_adclient(self.obj)
def getUsers(self):
""" It returns list of all users in Active Directory.
"""
return _adclient.getUsers_adclient(self.obj)
def getOUsInOU(self, OU, scope):
""" It returns list of all OUs in OU.
"""
return _adclient.getOUsInOU_adclient(self.obj, OU, scope)
def getUserDisplayName(self, user):
""" It returns string with user DisplayName property.
"""
return _adclient.getUserDisplayName_adclient(self.obj, user)
def getUserIpAddress(self, user):
""" It returns string with user msRADIUSFramedIPAddress property.
"""
return _adclient.getUserIpAddress_adclient(self.obj, user)
def getObjectAttribute(self, object, attribute):
""" It returns list with values of object attribute.
"""
return _adclient.getObjectAttribute_adclient(self.obj, object, attribute)
def getObjectAttributes(self, object):
""" It returns map of all object attributes.
"""
return _adclient.getObjectAttributes_adclient(self.obj, object)
def CreateComputer(self, name, container):
""" It creates computer with given name in given container.
"""
_adclient.CreateComputer_adclient(self.obj, name, container)
def CreateUser(self, cn, container, short_name):
""" It creates user with given common name and short name in given container.
"""
_adclient.CreateUser_adclient(self.obj, cn, container, short_name)
def CreateGroup(self, cn, container, short_name):
""" It creates user with given common name and short name in given container.
"""
_adclient.CreateGroup_adclient(self.obj, cn, container, short_name)
def DeleteDN(self, dn):
""" It deletes given DN.
"""
_adclient.DeleteDN_adclient(self.obj, dn)
def CreateOU(self, ou):
""" It creates given OU (with subOUs if needed).
"""
_adclient.CreateOU_adclient(self.obj, ou)
def EnableUser(self, short_name):
""" It enables given user.
"""
_adclient.EnableUser_adclient(self.obj, short_name)
def DisableUser(self, short_name):
""" It disables given user.
"""
_adclient.DisableUser_adclient(self.obj, short_name)
def setUserDescription(self, dn, descr):
_adclient.setUserDescription_adclient(self.obj, dn, descr)
def changeUserPassword(self, dn, old_password, new_password):
_adclient.changeUserPassword_adclient(self.obj, dn, old_password, new_password)
def setUserPassword(self, dn, password):
_adclient.setUserPassword_adclient(self.obj, dn, password)
def checkUserPassword(self, dn, password):
""" It returns True of False depends on user credentials correctness. """
return _adclient.checkUserPassword_adclient(self.obj, dn, password)
def setUserDialinAllowed(self, user):
_adclient.setUserDialinAllowed_adclient(self.obj, user)
def setUserDialinDisabled(self, user):
_adclient.setUserDialinDisabled_adclient(self.obj, user)
def setUserSN_adclient(self, user, sn):
_adclient.setUserSN_adclient(self.obj, user, sn)
def setUserInitials(self, user, initials):
_adclient.setUserInitials_adclient(self.obj, user, initials)
def setUserGivenName(self, user, givenName):
_adclient.setUserGivenName_adclient(self.obj, user, givenName)
def setUserDisplayName(self, user, displayName):
_adclient.setUserDisplayName_adclient(self.obj, user, displayName)
def setUserRoomNumber(self, user, roomNum):
_adclient.setUserRoomNumber_adclient(self.obj, user, roomNum)
def setUserAddress(self, user, streetAddress):
_adclient.setUserAddress_adclient(self.obj, user, streetAddress)
def setUserInfo(self, user, info):
_adclient.setUserInfo_adclient(self.obj, user, info)
def setUserTitle(self, user, title):
_adclient.setUserTitle_adclient(self.obj, user, title)
def setUserDepartment(self, user, department):
_adclient.setUserDepartment_adclient(self.obj, user, department)
def setUserCompany(self, user, company):
_adclient.setUserCompany_adclient(self.obj, user, company)
def setUserPhone(self, user, phone):
_adclient.setUserPhone_adclient(self.obj, user, phone)
def setUserSN(self, user, phone):
_adclient.setUserSN_adclient(self.obj, user, phone)
def setUserIpAddress(self, user, ip):
_adclient.setUserIpAddress_adclient(self.obj, user, ip)
def clearObjectAttribute(self, obj, attr):
_adclient.clearObjectAttribute_adclient(self.obj, obj, attr)
def setObjectAttribute(self, obj, attr, values):
if not isinstance(values, list):
values = [values]
_adclient.setObjectAttribute_adclient(self.obj, obj, attr, values)
def UnLockUser(self, user):
""" It unlocks given user.
"""
_adclient.UnLockUser_adclient(self.obj, user)
def MoveUser(self, user, new_container):
""" It moves given user to new container.
"""
_adclient.MoveUser_adclient(self.obj, user, new_container)
def MoveObject(self, obj, new_container):
""" It moves given user to new container.
"""
_adclient.MoveObject_adclient(self.obj, obj, new_container)
def RenameUser(self, user, shortname, cn=""):
""" It renames sAMAccountName, UPN and CN for given user
CN is taken from shortname when empty
"""
_adclient.RenameUser_adclient(self.obj, user, shortname, cn)
def get_error_num(self):
""" It returns int of last error occured
"""
return _adclient.get_error_num()
def int2ip(self, ipstr):
return _adclient.int2ip(ipstr)
def decodeSID(self, sid):
return _adclient.decodeSID(sid)
def FileTimeToPOSIX(self, filetime):
return _adclient.FileTimeToPOSIX(filetime)
def domain2dn(self, domain):
return _adclient.domain2dn(domain)
def get_ldap_servers(self, domain, site=""):
return _adclient.get_ldap_servers(domain, site)