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
.common
import get_string
32 from samba
import dsdb
33 from ldb
import SCOPE_SUBTREE
, SCOPE_ONELEVEL
36 def get_schema_descriptor(domain_sid
, name_map
=None):
40 sddl
= "O:SAG:SAD:AI(OA;;CR;e12b56b6-0a95-11d1-adbb-00c04fd8d5cd;;SA)" \
41 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
42 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
43 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
44 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
45 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
46 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
47 "(A;CI;RPLCLORC;;;AU)" \
48 "(A;CI;RPWPCRCCLCLORCWOWDSW;;;SA)" \
49 "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
50 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
51 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
52 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
53 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
54 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
55 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
56 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;RO)" \
57 "S:(AU;SA;WPCCDCWOWDSDDTSW;;;WD)" \
61 "(OU;SA;CR;e12b56b6-0a95-11d1-adbb-00c04fd8d5cd;;WD)" \
62 "(OU;SA;CR;45ec5156-db7e-47bb-b53f-dbeb2d03c40f;;WD)"
63 sec
= security
.descriptor
.from_sddl(sddl
, domain_sid
)
69 # the schema files (and corresponding object version) that we know about
71 "2008_R2_old": ("MS-AD_Schema_2K8_R2_Attributes.txt",
72 "MS-AD_Schema_2K8_R2_Classes.txt",
74 "2008_R2": ("Attributes_for_AD_DS__Windows_Server_2008_R2.ldf",
75 "Classes_for_AD_DS__Windows_Server_2008_R2.ldf",
77 "2012": ("Attributes_for_AD_DS__Windows_Server_2012.ldf",
78 "Classes_for_AD_DS__Windows_Server_2012.ldf",
80 "2012_R2": ("AD_DS_Attributes__Windows_Server_2012_R2.ldf",
81 "AD_DS_Classes__Windows_Server_2012_R2.ldf",
83 "2016": ("AD_DS_Attributes__Windows_Server_v1803.ldf",
84 "AD_DS_Classes__Windows_Server_v1803.ldf",
86 "2019": ("AD_DS_Attributes_Windows_Server_v1903.ldf",
87 "AD_DS_Classes_Windows_Server_v1903.ldf",
91 def __init__(self
, domain_sid
, invocationid
=None, schemadn
=None,
92 files
=None, override_prefixmap
=None, additional_prefixmap
=None,
94 from samba
.provision
import setup_path
96 """Load schema for the SamDB from the AD schema files and
99 :param samdb: Load a schema into a SamDB.
100 :param schemadn: DN of the schema
102 Returns the schema data loaded, to avoid double-parsing when then
103 needing to add it to the db
106 if base_schema
is None:
107 base_schema
= Schema
.default_base_schema()
109 self
.base_schema
= base_schema
111 self
.schemadn
= schemadn
112 # We need to have the am_rodc=False just to keep some warnings quiet -
113 # this isn't a real SAM, so it's meaningless.
114 self
.ldb
= SamDB(global_schema
=False, am_rodc
=False)
115 if invocationid
is not None:
116 self
.ldb
.set_invocation_id(invocationid
)
118 self
.schema_data
= read_ms_schema(
119 setup_path('ad-schema/%s' % Schema
.base_schemas
[base_schema
][0]),
120 setup_path('ad-schema/%s' % Schema
.base_schemas
[base_schema
][1]))
123 with
open(file, 'rb') as data_file
:
124 return data_file
.read()
126 if files
is not None:
127 self
.schema_data
= "".join(get_string(read_file(file))
130 self
.schema_data
= substitute_var(self
.schema_data
,
131 {"SCHEMADN": schemadn
})
132 check_all_substituted(self
.schema_data
)
134 schema_version
= str(Schema
.get_version(base_schema
))
135 self
.schema_dn_modify
= read_and_sub_file(
136 setup_path("provision_schema_basedn_modify.ldif"),
137 {"SCHEMADN": schemadn
, "OBJVERSION": schema_version
})
139 descr
= b64encode(get_schema_descriptor(domain_sid
)).decode('utf8')
140 self
.schema_dn_add
= read_and_sub_file(
141 setup_path("provision_schema_basedn.ldif"),
142 {"SCHEMADN": schemadn
, "DESCRIPTOR": descr
})
144 if override_prefixmap
is not None:
145 self
.prefixmap_data
= override_prefixmap
147 self
.prefixmap_data
= read_file(setup_path("prefixMap.txt"))
149 if additional_prefixmap
is not None:
150 self
.prefixmap_data
+= "".join("%s\n" % map for map in additional_prefixmap
)
152 self
.prefixmap_data
= b64encode(self
.prefixmap_data
).decode('utf8')
154 # We don't actually add this ldif, just parse it
155 prefixmap_ldif
= "dn: %s\nprefixMap:: %s\n\n" % (self
.schemadn
, self
.prefixmap_data
)
156 self
.set_from_ldif(prefixmap_ldif
, self
.schema_data
, self
.schemadn
)
159 def default_base_schema():
160 """Returns the default base schema to use"""
164 def get_version(base_schema
):
165 """Returns the base schema's object version, e.g. 47 for 2008_R2"""
166 return Schema
.base_schemas
[base_schema
][2]
168 def set_from_ldif(self
, pf
, df
, dn
):
169 dsdb
._dsdb
_set
_schema
_from
_ldif
(self
.ldb
, pf
, df
, dn
)
171 def write_to_tmp_ldb(self
, schemadb_path
):
172 self
.ldb
.connect(url
=schemadb_path
)
173 self
.ldb
.transaction_start()
175 # These are actually ignored, as the schema has been forced
176 # when the ldb object was created, and that overrides this
177 self
.ldb
.add_ldif("""dn: @ATTRIBUTES
182 @IDXATTR: attributeSyntax
186 schema_dn_add
= self
.schema_dn_add \
187 + "objectGUID: 24e2ca70-b093-4ae8-84c0-2d7ac652a1b8\n"
189 # These bits of LDIF are supplied when the Schema object is created
190 self
.ldb
.add_ldif(schema_dn_add
)
191 self
.ldb
.modify_ldif(self
.schema_dn_modify
)
192 self
.ldb
.add_ldif(self
.schema_data
)
194 self
.ldb
.transaction_cancel()
197 self
.ldb
.transaction_commit()
199 # Return a hash with the forward attribute as a key and the back as the
201 def linked_attributes(self
):
202 return get_linked_attributes(self
.schemadn
, self
.ldb
)
204 def dnsyntax_attributes(self
):
205 return get_dnsyntax_attributes(self
.schemadn
, self
.ldb
)
207 def convert_to_openldap(self
, target
, mapping
):
208 return dsdb
._dsdb
_convert
_schema
_to
_openldap
(self
.ldb
, target
, mapping
)
211 # Return a hash with the forward attribute as a key and the back as the value
212 def get_linked_attributes(schemadn
, schemaldb
):
213 attrs
= ["linkID", "lDAPDisplayName"]
214 res
= schemaldb
.search(
215 expression
="(&(linkID=*)"
216 "(!(linkID:1.2.840.113556.1.4.803:=1))"
217 "(objectclass=attributeSchema)"
218 "(attributeSyntax=2.5.5.1))",
219 base
=schemadn
, scope
=SCOPE_ONELEVEL
, attrs
=attrs
)
221 for i
in range(0, len(res
)):
222 expression
= ("(&(objectclass=attributeSchema)(linkID=%d)"
223 "(attributeSyntax=2.5.5.1))" %
224 (int(res
[i
]["linkID"][0]) + 1))
225 target
= schemaldb
.searchone(basedn
=schemadn
,
226 expression
=expression
,
227 attribute
="lDAPDisplayName",
229 if target
is not None:
230 attributes
[str(res
[i
]["lDAPDisplayName"])] = target
.decode('utf-8')
235 def get_dnsyntax_attributes(schemadn
, schemaldb
):
236 res
= schemaldb
.search(
237 expression
="(&(!(linkID=*))(objectclass=attributeSchema)(attributeSyntax=2.5.5.1))",
238 base
=schemadn
, scope
=SCOPE_ONELEVEL
,
239 attrs
=["linkID", "lDAPDisplayName"])
241 for i
in range(0, len(res
)):
242 attributes
.append(str(res
[i
]["lDAPDisplayName"]))
246 def ldb_with_schema(schemadn
="cn=schema,cn=configuration,dc=example,dc=com",
248 override_prefixmap
=None):
249 """Load schema for the SamDB from the AD schema files and samba4_schema.ldif
251 :param schemadn: DN of the schema
252 :param serverdn: DN of the server
254 Returns the schema data loaded as an object, with .ldb being a
255 new ldb with the schema loaded. This allows certain tests to
256 operate without a remote or local schema.
259 if domainsid
is None:
260 domainsid
= security
.random_sid()
262 domainsid
= security
.dom_sid(domainsid
)
263 return Schema(domainsid
, schemadn
=schemadn
,
264 override_prefixmap
=override_prefixmap
)