2 # Unix SMB/CIFS implementation.
3 # backend code for provisioning a Samba4 server
5 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
6 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008-2009
7 # Copyright (C) Oliver Liebel <oliver@itc.li> 2008-2009
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 """Functions for setting up a Samba Schema."""
25 from base64 import b64encode
26 from samba import read_and_sub_file, substitute_var, check_all_substituted
27 from samba.dcerpc import security
28 from samba.ms_schema import read_ms_schema
29 from samba.ndr import ndr_pack
30 from samba.samdb import SamDB
31 from samba import dsdb
32 from ldb import SCOPE_SUBTREE, SCOPE_ONELEVEL
35 def get_schema_descriptor(domain_sid):
36 sddl = "O:SAG:SAD:AI(OA;;CR;e12b56b6-0a95-11d1-adbb-00c04fd8d5cd;;SA)" \
37 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
38 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
39 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
40 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
41 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
42 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
43 "(A;CI;RPLCLORC;;;AU)" \
44 "(A;CI;RPWPCRCCLCLORCWOWDSW;;;SA)" \
45 "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
46 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
47 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
48 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
49 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
50 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ER)" \
51 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ER)" \
52 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ER)" \
53 "S:(AU;SA;WPCCDCWOWDSDDTSW;;;WD)" \
57 "(OU;SA;CR;e12b56b6-0a95-11d1-adbb-00c04fd8d5cd;;WD)" \
58 "(OU;SA;CR;45ec5156-db7e-47bb-b53f-dbeb2d03c40f;;WD)"
59 sec = security.descriptor.from_sddl(sddl, domain_sid)
65 def __init__(self, setup_path, domain_sid, invocationid=None, schemadn=None,
66 files=None, override_prefixmap=None, additional_prefixmap=None):
67 """Load schema for the SamDB from the AD schema files and samba4_schema.ldif
69 :param samdb: Load a schema into a SamDB.
70 :param setup_path: Setup path function.
71 :param schemadn: DN of the schema
73 Returns the schema data loaded, to avoid double-parsing when then needing to add it to the db
76 self.schemadn = schemadn
77 # We need to have the am_rodc=False just to keep some warnings quiet - this isn't a real SAM, so it's meaningless.
78 self.ldb = SamDB(global_schema=False, am_rodc=False)
79 if invocationid is not None:
80 self.ldb.set_invocation_id(invocationid)
82 self.schema_data = read_ms_schema(
83 setup_path('ad-schema/MS-AD_Schema_2K8_R2_Attributes.txt'),
84 setup_path('ad-schema/MS-AD_Schema_2K8_R2_Classes.txt'))
88 self.schema_data += open(file, 'r').read()
90 self.schema_data = substitute_var(self.schema_data,
91 {"SCHEMADN": schemadn})
92 check_all_substituted(self.schema_data)
94 self.schema_dn_modify = read_and_sub_file(
95 setup_path("provision_schema_basedn_modify.ldif"),
96 {"SCHEMADN": schemadn})
98 descr = b64encode(get_schema_descriptor(domain_sid))
99 self.schema_dn_add = read_and_sub_file(
100 setup_path("provision_schema_basedn.ldif"),
101 {"SCHEMADN": schemadn, "DESCRIPTOR": descr})
103 if override_prefixmap is not None:
104 self.prefixmap_data = override_prefixmap
106 self.prefixmap_data = open(setup_path("prefixMap.txt"), 'r').read()
108 if additional_prefixmap is not None:
109 for map in additional_prefixmap:
110 self.prefixmap_data += "%s\n" % map
112 self.prefixmap_data = b64encode(self.prefixmap_data)
114 # We don't actually add this ldif, just parse it
115 prefixmap_ldif = "dn: cn=schema\nprefixMap:: %s\n\n" % self.prefixmap_data
116 self.set_from_ldif(prefixmap_ldif, self.schema_data)
118 def set_from_ldif(self, pf, df):
119 dsdb._dsdb_set_schema_from_ldif(self.ldb, pf, df)
121 def write_to_tmp_ldb(self, schemadb_path):
122 self.ldb.connect(url=schemadb_path)
123 self.ldb.transaction_start()
125 self.ldb.add_ldif("""dn: @ATTRIBUTES
130 @IDXATTR: attributeSyntax
132 # These bits of LDIF are supplied when the Schema object is created
133 self.ldb.add_ldif(self.schema_dn_add)
134 self.ldb.modify_ldif(self.schema_dn_modify)
135 self.ldb.add_ldif(self.schema_data)
137 self.ldb.transaction_cancel()
140 self.ldb.transaction_commit()
142 # Return a hash with the forward attribute as a key and the back as the value
143 def linked_attributes(self):
144 return get_linked_attributes(self.schemadn, self.ldb)
146 def dnsyntax_attributes(self):
147 return get_dnsyntax_attributes(self.schemadn, self.ldb)
149 def convert_to_openldap(self, target, mapping):
150 return dsdb._dsdb_convert_schema_to_openldap(self.ldb, target, mapping)
153 # Return a hash with the forward attribute as a key and the back as the value
154 def get_linked_attributes(schemadn,schemaldb):
155 attrs = ["linkID", "lDAPDisplayName"]
156 res = schemaldb.search(expression="(&(linkID=*)(!(linkID:1.2.840.113556.1.4.803:=1))(objectclass=attributeSchema)(attributeSyntax=2.5.5.1))", base=schemadn, scope=SCOPE_ONELEVEL, attrs=attrs)
158 for i in range (0, len(res)):
159 expression = "(&(objectclass=attributeSchema)(linkID=%d)(attributeSyntax=2.5.5.1))" % (int(res[i]["linkID"][0])+1)
160 target = schemaldb.searchone(basedn=schemadn,
161 expression=expression,
162 attribute="lDAPDisplayName",
164 if target is not None:
165 attributes[str(res[i]["lDAPDisplayName"])]=str(target)
170 def get_dnsyntax_attributes(schemadn,schemaldb):
171 res = schemaldb.search(
172 expression="(&(!(linkID=*))(objectclass=attributeSchema)(attributeSyntax=2.5.5.1))",
173 base=schemadn, scope=SCOPE_ONELEVEL,
174 attrs=["linkID", "lDAPDisplayName"])
176 for i in range (0, len(res)):
177 attributes.append(str(res[i]["lDAPDisplayName"]))
181 def ldb_with_schema(setup_dir=None,
182 schemadn="cn=schema,cn=configuration,dc=example,dc=com",
184 override_prefixmap=None):
185 """Load schema for the SamDB from the AD schema files and samba4_schema.ldif
187 :param setup_dir: Setup path
188 :param schemadn: DN of the schema
189 :param serverdn: DN of the server
191 Returns the schema data loaded as an object, with .ldb being a
192 new ldb with the schema loaded. This allows certain tests to
193 operate without a remote or local schema.
196 def setup_path(file):
197 return os.path.join(setup_dir, file)
199 if domainsid is None:
200 domainsid = security.random_sid()
202 domainsid = security.dom_sid(domainsid)
203 return Schema(setup_path, domainsid, schemadn=schemadn, override_prefixmap=override_prefixmap)