-
Notifications
You must be signed in to change notification settings - Fork 3
/
add_gluu_admin_user.py
80 lines (54 loc) · 1.95 KB
/
add_gluu_admin_user.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
import sys
import uuid
import ldap
import ldap.modlist as modlist
import os
import hashlib
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
admin_user = raw_input('New admin username:')
admin_pw = raw_input('Admin password:')
for l in open('/etc/gluu/conf/gluu-ldap.properties'):
ls = l.strip()
if ls.startswith('bindPassword:'):
n = ls.find(':')
pwe = ls[n+1:]
bind_pw = os.popen('/opt/gluu/bin/encode.py -D ' + pwe).read().strip()
bind_dn = 'cn=directory manager'
ldap_host = 'localhost'
conn = ldap.initialize('ldaps://{0}:1636'.format(ldap_host))
conn.simple_bind_s(bind_dn, bind_pw)
result = conn.search_s('ou=groups,o=gluu',ldap.SCOPE_SUBTREE, ('gluuGroupType=gluuManagerGroup'))
admin_dn = result[0][0]
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
admin_pw_e = make_secret(admin_pw)
inum = str(uuid.uuid4())
entry = {
'objectClass': ['top', 'gluuPerson'],
'givenname': admin_user,
"cn": admin_user,
'sn': admin_user,
'uid': admin_user,
'inum': str(uuid.uuid4()),
'gluustatus': 'active',
'userpassword': admin_pw_e,
'mail': admin_user+'@foo.org',
'displayname': admin_user,
'givenname': admin_user,
'memberOf': admin_dn,
}
dn = 'inum={},ou=people,o=gluu'.format(inum)
ldif = modlist.addModlist(entry)
conn.add_s(dn, ldif)
conn.modify_s(admin_dn, [( ldap.MOD_ADD, 'member', dn)])