summaryrefslogtreecommitdiffstats
path: root/generate-ldap-users.py
blob: 208d124cb0d0196bc9c95c1357ed6835219a3429 (plain)
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
#! /usr/bin/env python

import ConfigParser
import optparse
import os
import ldap
import sys


parser = optparse.OptionParser()
parser.add_option('--config', dest='config', default='ldap.ini')
parser.add_option('--output', dest='output_filename',
        default='ocrloader-complete.ini')
parser.add_option('--bindpw', dest='bindpw')
(options, args) = parser.parse_args()
if not options.bindpw:
    parser.error('Missing LDAP bind password')

cfg = ConfigParser.ConfigParser()
cfg.read(options.config)

ldap_conn = ldap.initialize(cfg.get('general', 'ldap_uri'))
ldap_conn.simple_bind_s(cfg.get('general', 'bind_dn'), options.bindpw)

usernames = []

for entry in ldap_conn.search_s("dc=win,dc=info,dc=pcf", ldap.SCOPE_SUBTREE, "objectclass=user"):
    if not entry[0]:
        continue
    if not entry[1].get('profilePath'):
        continue

    try:
        username = entry[1]['sAMAccountName'][0]
    except KeyError:
        continue
    usernames.append(username)

fd = file(options.output_filename, 'w')
print >> fd, file(cfg.get('general', 'template')).read()

for username in usernames:
    vars = {'username': username, 'username_lower': username.lower()}
    vars.update(dict(cfg.items('variables')))
    print username
    print >> fd, '''[ged-%(username_lower)s@pfwb.be]
default_type = dmsdocument
default_directory = Members/%(username)s
user = %(username)s

[ged-file-%(username)s@pfwb.be]
store_path = /srv/ocr/%(username)s

[ged-test-%(username_lower)s@pfwb.be]
ged_base_url = %(test_server_url)s
default_type = dmsdocument
default_directory = Members/%(username)s
user = %(username)s

''' % vars

    if not os.path.exists('/srv/ocr/%s' % username):
        os.mkdir('/srv/ocr/%s' % username)

fd.close()