summaryrefslogtreecommitdiffstats
path: root/generate-ldap-users.py
blob: 0d46f3857f7259b6dabd67a8962ad291e0eab420 (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
67
68
69
70
71
72
73
74
75
76
#! /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)

users = []

for entry in ldap_conn.search_s("ou=ouGED,ou=ouPCF,ou=ouUsers,dc=win,dc=info,dc=pcf",
        ldap.SCOPE_SUBTREE, "objectclass=user"):
    if not entry[0]:
        continue

    try:
        username = entry[1]['sAMAccountName'][0]
        mail = entry[1]['mail'][0]
    except KeyError:
        continue
    users.append({'username': username, 'mail': mail})

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

for user in users:
    vars = {'username': user.get('username'),
            'username_lower': user.get('username').lower(),
            'mail': user.get('mail'),
            }
    username = user.get('username')
    vars.update(dict(cfg.items('variables')))
    print >> fd, '''#[ged-gen-%(mail)s]
#default_type = dmsdocument
#default_directory = Members/%(username)s
#user = %(username)s

[ged-%(mail)s]
store_path = /srv/ocr/%(username)s

[/srv/ocr2/%(username)s]
store_path = /srv/ocr/%(username)s

#[ged-test-gen-%(mail)s]
#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)

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


fd.close()