|
|
|
1 |
#!/usr/bin/python2.6 |
2 |
# -*- coding: utf-8 -*- |
3 |
# |
4 |
# Univention Directory Listener |
5 |
"""Read ldap from the DC master and create ldif file (and update local schema)""" |
6 |
# |
7 |
# Copyright 2004-2014 Univention GmbH |
8 |
# |
9 |
# http://www.univention.de/ |
10 |
# |
11 |
# All rights reserved. |
12 |
# |
13 |
# The source code of this program is made available |
14 |
# under the terms of the GNU Affero General Public License version 3 |
15 |
# (GNU AGPL V3) as published by the Free Software Foundation. |
16 |
# |
17 |
# Binary versions of this program provided by Univention to you as |
18 |
# well as other copyrighted, protected or trademarked materials like |
19 |
# Logos, graphics, fonts, specific documentations and configurations, |
20 |
# cryptographic keys etc. are subject to a license agreement between |
21 |
# you and Univention and not subject to the GNU AGPL V3. |
22 |
# |
23 |
# In the case you use this program under the terms of the GNU AGPL V3, |
24 |
# the program is provided in the hope that it will be useful, |
25 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
27 |
# GNU Affero General Public License for more details. |
28 |
# |
29 |
# You should have received a copy of the GNU Affero General Public |
30 |
# License with the Debian GNU/Linux or Univention distribution in file |
31 |
# /usr/share/common-licenses/AGPL-3; if not, see |
32 |
# <http://www.gnu.org/licenses/>. |
33 |
|
34 |
import univention.uldap as uldap |
35 |
import univention.config_registry |
36 |
|
37 |
import ldap |
38 |
import ldif |
39 |
import sys |
40 |
import os |
41 |
import optparse |
42 |
|
43 |
from ldap.controls import LDAPControl |
44 |
from ldap.controls import SimplePagedResultsControl |
45 |
|
46 |
sys.path.append("/usr/lib/univention-directory-listener/system/") |
47 |
import replication |
48 |
|
49 |
# from replication.py |
50 |
def _update_schema(attr): |
51 |
fp = open('/var/lib/univention-ldap/schema.conf.new', 'w') |
52 |
print >>fp, '# This schema was automatically replicated from the master server' |
53 |
print >>fp, '# Please do not edit this file\n' |
54 |
subschema = ldap.schema.SubSchema(attr) |
55 |
for oid in replication.subschema_sort(subschema, ldap.schema.AttributeType): |
56 |
if oid in replication.BUILTIN_OIDS: |
57 |
continue |
58 |
obj = subschema.get_obj(ldap.schema.AttributeType, oid) |
59 |
print >>fp, 'attributetype', str(obj) |
60 |
for oid in replication.subschema_sort(subschema, ldap.schema.ObjectClass): |
61 |
if oid in replication.BUILTIN_OIDS: |
62 |
continue |
63 |
obj = subschema.get_obj(ldap.schema.ObjectClass, oid) |
64 |
print >>fp, 'objectclass', str(obj) |
65 |
fp.close() |
66 |
os.rename('/var/lib/univention-ldap/schema.conf.new', '/var/lib/univention-ldap/schema.conf') |
67 |
|
68 |
# update the ldap schema file |
69 |
def update_schema(lo): |
70 |
res = lo.search(base="cn=Subschema", scope=ldap.SCOPE_BASE, filter='(objectclass=*)', attr=['+', '*']) |
71 |
for r in res: |
72 |
dn, data = r |
73 |
_update_schema(data) |
74 |
|
75 |
# create ldif file from everything from lo |
76 |
def create_ldif_from_master(lo, ldif_file, base, page_size): |
77 |
|
78 |
if os.path.isfile(ldif_file): |
79 |
os.unlink(ldif_file) |
80 |
|
81 |
lc = SimplePagedResultsControl( |
82 |
ldap.LDAP_CONTROL_PAGE_OID, True, (page_size,'') |
83 |
) |
84 |
|
85 |
msgid = lo.lo.search_ext(base, ldap.SCOPE_SUBTREE, '(objectclass=*)', ['+', '*'], serverctrls=[lc]) |
86 |
fh = open(ldif_file, "w") |
87 |
while True: |
88 |
rtype, rdata, rmsgid, serverctrls = lo.lo.result3(msgid) |
89 |
for i in rdata: |
90 |
dn, data = i |
91 |
fh.write(ldif.CreateLDIF(dn, data)) |
92 |
fh.close() |
93 |
pctrls = [ |
94 |
c |
95 |
for c in serverctrls |
96 |
if c.controlType == ldap.LDAP_CONTROL_PAGE_OID |
97 |
] |
98 |
if pctrls: |
99 |
est, cookie = pctrls[0].controlValue |
100 |
if cookie: |
101 |
lc.controlValue = (page_size, cookie) |
102 |
msgid = lo.lo.search_ext(base, ldap.SCOPE_SUBTREE, filter, attrs, serverctrls=[lc]) |
103 |
else: |
104 |
break |
105 |
else: |
106 |
print "Warning: Server ignores RFC 2696 control." |
107 |
break |
108 |
fh.close() |
109 |
|
110 |
if __name__ == "__main__": |
111 |
|
112 |
usage = "usage: %prog [options]\n" |
113 |
parser = optparse.OptionParser(usage=usage) |
114 |
parser.add_option("-l", "--ldif", dest="ldif", action="store_true", default=False, help="create ldif file") |
115 |
parser.add_option("-s", "--schema", dest="schema", action="store_true", default=False, help="update ldap schema") |
116 |
parser.add_option("-o", "--outfile", dest="outfile", default="/var/lib/univention-directory-listener/master.ldif", help="file to store ldif data") |
117 |
parser.add_option("-p", "--pagesize", dest="pagesize", type="int", default=10000, help="page size to use for ldap paged search") |
118 |
opts, args = parser.parse_args() |
119 |
|
120 |
ucr = univention.config_registry.ConfigRegistry() |
121 |
ucr.load() |
122 |
base = ucr.get("ldap/base") |
123 |
lo = uldap.getMachineConnection(ldap_master = True) |
124 |
|
125 |
if opts.schema: |
126 |
sys.stdout.write("updating schema ...") |
127 |
sys.stdout.flush() |
128 |
update_schema(lo) |
129 |
sys.stdout.write(" OK\n") |
130 |
|
131 |
if opts.ldif: |
132 |
sys.stdout.write("creating ldif file %s ..." % opts.outfile) |
133 |
sys.stdout.flush() |
134 |
create_ldif_from_master(lo, opts.outfile, base, opts.pagesize) |
135 |
sys.stdout.write(" OK\n") |
136 |
|
137 |
sys.exit(0) |
0 |
+ * |
138 |
+ * |