-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmake_ldap_json.py
More file actions
62 lines (52 loc) · 1.41 KB
/
make_ldap_json.py
File metadata and controls
62 lines (52 loc) · 1.41 KB
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
from ldap3 import Server, Connection, ALL_ATTRIBUTES, ALL
import os
import subprocess
import time
basedir = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.join(basedir, "OpenLDAP", "data")
config_dir = os.path.join(basedir, "OpenLDAP")
prefix = "/usr"
slapd = os.path.join(prefix, "libexec", "slapd")
# Ensure the OpenLDAP data dir exists
if not os.path.exists(data_dir):
os.makedirs(data_dir)
# Slapadd the data
with open("data.ldif", "rb") as f:
ldif = f.read()
slapadd = subprocess.run(
["slapadd", "-f", os.path.join(config_dir, "slapd.conf")],
input=ldif,
cwd=config_dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Start a slapd
slapd = subprocess.Popen(
[
slapd,
"-f",
os.path.join(config_dir, "slapd.conf"),
"-d1",
"-h",
"ldap://localhost:8369/",
],
stdout=subprocess.PIPE,
# stderr=subprocess.PIPE,
cwd=config_dir,
)
# Allow the slapd to start
time.sleep(30)
print(slapd)
# Get the info we need
server = Server("localhost", port=8369, get_info=ALL)
connection = Connection(
server, "uid=root,dc=my-domain,dc=com", "secret", auto_bind=True
)
if connection.search(
"dc=my-domain,dc=com", "(objectclass=*)", attributes=ALL_ATTRIBUTES
):
connection.response_to_file("entries.json", raw=True)
server.schema.to_file("schema.json")
server.info.to_file("info.json")
# kill the slapd again
slapd.kill()