-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldapLoaderUsersGroups.py
executable file
·234 lines (190 loc) · 4.83 KB
/
ldapLoaderUsersGroups.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
#! /usr/bin/env python3
import sys
import ldap
import environ
import json
"""
dump all users: objectclass user and not computer
dump all groups [todo]
create links between users and groups [todo]
write json to stdout
"""
def prepLdapConnect(base: str, verbose: bool = False):
ldap.set_option(ldap.OPT_REFERRALS, 0)
ldap.protocol_version = 3
conn = ldap.initialize(env.str("LDAP_URL"))
conn.set_option(ldap.OPT_REFERRALS, 0) # to search the object and all its descendants
try:
login = env.str("LDAP_LOGIN")
conn.simple_bind_s(
login,
env.str("LDAP_PASSWORD"),
)
r = conn.search_st( # s t means synchronous with timeout
base,
ldap.SCOPE_SUBTREE,
f"(&(objectClass=person)(userPrincipalName={login}))",
["cn"],
0,
30,
)
if verbose:
print(r, file=sys.stderr)
except ldap.INVALID_CREDENTIALS as e:
print(f"wrong password provided: {e}", file=sys.stderr)
exit(1)
return conn
def prepPageControl(size: int = 1000):
if size > 1000:
size = 1000
if size < 250:
size = 250
page_control = ldap.controls.libldap.SimplePagedResultsControl(
True,
size=size,
cookie="",
)
return page_control
def ldapSearchWithPages(
conn,
base: str,
page_control,
searchStr: str,
resultSet: list,
):
response = conn.search_ext(
base,
ldap.SCOPE_SUBTREE,
searchStr,
resultSet,
serverctrls=[page_control],
)
result = []
pages = 0
while True:
pages += 1
rtype, rdata, rmsgid, serverctrls = conn.result3(response)
result.extend(rdata)
controls = [control for control in serverctrls if control.controlType == ldap.controls.libldap.SimplePagedResultsControl.controlType]
if not controls:
print("The server ignores RFC 2696 control")
break
if not controls[0].cookie:
break
page_control.cookie = controls[0].cookie
response = conn.search_ext(
base,
ldap.SCOPE_SUBTREE,
searchStr,
resultSet,
serverctrls=[page_control],
)
return result
def doOneUserItem(item):
data = {
"cn": None,
"nType": "ldapPerson",
}
cn = item[0]
if cn is None or cn == "None":
return None
data["cn"] = cn
for k, v in item[1].items():
if len(v) == 1:
j = v[0]
j = j.decode("unicode-escape").encode("latin1").decode("utf-8")
data[k] = j
else:
data[k] = []
for j in v:
j = j.decode("unicode-escape").encode("latin1").decode("utf-8")
data[k].append(j)
return data
def doOneGroupItem(item):
data = {
"cn": None,
"nType": "ldapGroup",
}
cn = item[0]
if cn is None or cn == "None":
return None
data["cn"] = cn
for k, v in item[1].items():
if len(v) == 1:
j = v[0]
j = j.decode("unicode-escape").encode("latin1").decode("utf-8")
data[k] = j
else:
data[k] = []
for j in v:
j = j.decode("unicode-escape").encode("latin1").decode("utf-8")
data[k].append(j)
return data
def searchUsersNotComputers(
conn,
base: str,
page_control,
):
searchStr = "(&(objectclass=user)(!(objectclass=computer)))"
resultSet = [
"ufn",
"samaccountname",
"mail",
"sn",
"givenname",
"displayName",
"memberof",
"description",
"name",
"userPrincipalName",
]
return ldapSearchWithPages(
conn,
base,
page_control,
searchStr,
resultSet,
)
def searchGroups(
conn,
base: str,
page_control,
):
searchStr = "(objectclass=group)"
resultSet = [
"name",
"description",
"distinguishedName",
"ufn",
"member",
"samaccountname",
]
return ldapSearchWithPages(
conn,
base,
page_control,
searchStr,
resultSet,
)
def xMain():
base = env.str("LDAP_BASE")
conn = prepLdapConnect(base)
page_control = prepPageControl(1000)
users = searchUsersNotComputers(conn, base, page_control)
groups = searchGroups(conn, base, page_control)
dd = {
"nodes": [],
"edges": [],
}
for item in users:
data = doOneUserItem(item)
if data:
dd["nodes"].append(data)
for item in groups:
data = doOneGroupItem(item)
if data:
dd["nodes"].append(data)
print(json.dumps(dd, indent=4))
env = environ.Env()
environ.Env.read_env()
xMain()