-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_ldif.py
80 lines (68 loc) · 2.49 KB
/
generate_ldif.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
#!/usr/bin/python
from genshi.template import NewTextTemplate
from genshi.template import TemplateLoader
import MySQLdb
import hashlib
import os
import sys
if len(sys.argv) == 4:
LDIF_TYPE = sys.argv[1]
ROOT_UID = sys.argv[2]
ROOT_PW = sys.argv[3]
def make_secret(password):
"""
Encodes the given password as a base64 SSHA hash+salt buffer
"""
salt = os.urandom(4)
# hash the password and append the salt
sha = hashlib.sha1(password)
sha.update(salt)
# create a base64 encoded string of the concatenated digest + salt
digest_salt_b64 = '{}{}'.format(sha.digest(), salt).encode('base64').strip()
# now tag the digest above with the {SSHA} tag
tagged_digest_salt = '{{SSHA}}{}'.format(digest_salt_b64)
return tagged_digest_salt
db = MySQLdb.connect(host='localhost',
user='root',
passwd=os.environ['MYSQL_ROOT_PASSWORD'],
db=os.environ['DB_NAME'])
cursor = db.cursor()
cursor.execute('SELECT * FROM bdr_registry_account')
loader = TemplateLoader([os.getcwd()])
tmpl = loader.load('ldap_template.txt', cls=NewTextTemplate)
with open('bdr.ldif', 'w') as f:
e_users = [{
"user": ROOT_UID,
"password": make_secret(ROOT_PW)
}]
b_users = []
if LDIF_TYPE in ["full", "eionet"]:
with open('eionet.users', 'r') as e_users_f:
for user in e_users_f:
user = user.rstrip('\n').split(':')
e_users.append({"user": user[0],
"password": make_secret(user[1])})
if LDIF_TYPE in ["full", "bdr"]:
for row in cursor.fetchall():
try:
cursor2 = db.cursor()
cursor2.execute(
'select c.name, country.name from bdr_registry_company'
' c inner join bdr_registry_account a on'
' (c.account_id=a.id) inner join bdr_registry_country '
'country on (c.country_id=country.id) where a.id = {0};'.format(
row[0]
)
)
company, country = cursor2.fetchone()
b_users.append({
'user': row[1],
'password': make_secret(row[2]),
'country': country,
'company': company
})
except:
continue
stream = tmpl.generate(eionetitems=e_users, bdritems=b_users)
f.write(stream.render())
db.close()