ctdb-tests: add a comment to the generated public_addresses file used by eventscript...
[Samba.git] / python / samba / schema.py
bloba3adc162fa3817b5e6c2d750112db09895fba4d7
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={}):
37 sddl = "O:SAG:SAD:AI(OA;;CR;e12b56b6-0a95-11d1-adbb-00c04fd8d5cd;;SA)" \
38 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
39 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
40 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
41 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
42 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
43 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
44 "(A;CI;RPLCLORC;;;AU)" \
45 "(A;CI;RPWPCRCCLCLORCWOWDSW;;;SA)" \
46 "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
47 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
48 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
49 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
50 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
51 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ER)" \
52 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ER)" \
53 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ER)" \
54 "S:(AU;SA;WPCCDCWOWDSDDTSW;;;WD)" \
55 "(AU;CISA;WP;;;WD)" \
56 "(AU;SA;CR;;;BA)" \
57 "(AU;SA;CR;;;DU)" \
58 "(OU;SA;CR;e12b56b6-0a95-11d1-adbb-00c04fd8d5cd;;WD)" \
59 "(OU;SA;CR;45ec5156-db7e-47bb-b53f-dbeb2d03c40f;;WD)"
60 sec = security.descriptor.from_sddl(sddl, domain_sid)
61 return ndr_pack(sec)
64 class Schema(object):
66 # the schema files (and corresponding object version) that we know about
67 base_schemas = {
68 "2008_R2_old": ("MS-AD_Schema_2K8_R2_Attributes.txt",
69 "MS-AD_Schema_2K8_R2_Classes.txt",
70 47),
71 "2008_R2": ("Attributes_for_AD_DS__Windows_Server_2008_R2.ldf",
72 "Classes_for_AD_DS__Windows_Server_2008_R2.ldf",
73 47),
74 "2012": ("AD_DS_Attributes__Windows_Server_2012.ldf",
75 "AD_DS_Classes__Windows_Server_2012.ldf",
76 56),
77 "2012_R2": ("AD_DS_Attributes__Windows_Server_2012_R2.ldf",
78 "AD_DS_Classes__Windows_Server_2012_R2.ldf",
79 69),
82 def __init__(self, domain_sid, invocationid=None, schemadn=None,
83 files=None, override_prefixmap=None, additional_prefixmap=None,
84 base_schema=None):
85 from samba.provision import setup_path
87 """Load schema for the SamDB from the AD schema files and
88 samba4_schema.ldif
90 :param samdb: Load a schema into a SamDB.
91 :param schemadn: DN of the schema
93 Returns the schema data loaded, to avoid double-parsing when then
94 needing to add it to the db
95 """
97 if base_schema is None:
98 base_schema = Schema.default_base_schema()
100 self.base_schema = base_schema
102 self.schemadn = schemadn
103 # We need to have the am_rodc=False just to keep some warnings quiet -
104 # this isn't a real SAM, so it's meaningless.
105 self.ldb = SamDB(global_schema=False, am_rodc=False)
106 if invocationid is not None:
107 self.ldb.set_invocation_id(invocationid)
109 self.schema_data = read_ms_schema(
110 setup_path('ad-schema/%s' % Schema.base_schemas[base_schema][0]),
111 setup_path('ad-schema/%s' % Schema.base_schemas[base_schema][1]))
113 def read_file(file):
114 with open(file, 'rb') as data_file:
115 return data_file.read()
117 if files is not None:
118 self.schema_data = "".join(get_string(read_file(file))
119 for file in files)
121 self.schema_data = substitute_var(self.schema_data,
122 {"SCHEMADN": schemadn})
123 check_all_substituted(self.schema_data)
125 schema_version = str(Schema.get_version(base_schema))
126 self.schema_dn_modify = read_and_sub_file(
127 setup_path("provision_schema_basedn_modify.ldif"),
128 {"SCHEMADN": schemadn, "OBJVERSION": schema_version})
130 descr = b64encode(get_schema_descriptor(domain_sid)).decode('utf8')
131 self.schema_dn_add = read_and_sub_file(
132 setup_path("provision_schema_basedn.ldif"),
133 {"SCHEMADN": schemadn, "DESCRIPTOR": descr})
135 if override_prefixmap is not None:
136 self.prefixmap_data = override_prefixmap
137 else:
138 self.prefixmap_data = read_file(setup_path("prefixMap.txt"))
140 if additional_prefixmap is not None:
141 self.prefixmap_data += "".join("%s\n" % map for map in additional_prefixmap)
143 self.prefixmap_data = b64encode(self.prefixmap_data).decode('utf8')
145 # We don't actually add this ldif, just parse it
146 prefixmap_ldif = "dn: %s\nprefixMap:: %s\n\n" % (self.schemadn, self.prefixmap_data)
147 self.set_from_ldif(prefixmap_ldif, self.schema_data, self.schemadn)
149 @staticmethod
150 def default_base_schema():
151 """Returns the default base schema to use"""
152 return "2012_R2"
154 @staticmethod
155 def get_version(base_schema):
156 """Returns the base schema's object version, e.g. 47 for 2008_R2"""
157 return Schema.base_schemas[base_schema][2]
159 def set_from_ldif(self, pf, df, dn):
160 dsdb._dsdb_set_schema_from_ldif(self.ldb, pf, df, dn)
162 def write_to_tmp_ldb(self, schemadb_path):
163 self.ldb.connect(url=schemadb_path)
164 self.ldb.transaction_start()
165 try:
166 # These are actually ignored, as the schema has been forced
167 # when the ldb object was created, and that overrides this
168 self.ldb.add_ldif("""dn: @ATTRIBUTES
169 linkID: INTEGER
171 dn: @INDEXLIST
172 @IDXATTR: linkID
173 @IDXATTR: attributeSyntax
174 @IDXGUID: objectGUID
175 """)
177 schema_dn_add = self.schema_dn_add \
178 + "objectGUID: 24e2ca70-b093-4ae8-84c0-2d7ac652a1b8\n"
180 # These bits of LDIF are supplied when the Schema object is created
181 self.ldb.add_ldif(schema_dn_add)
182 self.ldb.modify_ldif(self.schema_dn_modify)
183 self.ldb.add_ldif(self.schema_data)
184 except:
185 self.ldb.transaction_cancel()
186 raise
187 else:
188 self.ldb.transaction_commit()
190 # Return a hash with the forward attribute as a key and the back as the
191 # value
192 def linked_attributes(self):
193 return get_linked_attributes(self.schemadn, self.ldb)
195 def dnsyntax_attributes(self):
196 return get_dnsyntax_attributes(self.schemadn, self.ldb)
198 def convert_to_openldap(self, target, mapping):
199 return dsdb._dsdb_convert_schema_to_openldap(self.ldb, target, mapping)
202 # Return a hash with the forward attribute as a key and the back as the value
203 def get_linked_attributes(schemadn, schemaldb):
204 attrs = ["linkID", "lDAPDisplayName"]
205 res = schemaldb.search(
206 expression="(&(linkID=*)"
207 "(!(linkID:1.2.840.113556.1.4.803:=1))"
208 "(objectclass=attributeSchema)"
209 "(attributeSyntax=2.5.5.1))",
210 base=schemadn, scope=SCOPE_ONELEVEL, attrs=attrs)
211 attributes = {}
212 for i in range(0, len(res)):
213 expression = ("(&(objectclass=attributeSchema)(linkID=%d)"
214 "(attributeSyntax=2.5.5.1))" %
215 (int(res[i]["linkID"][0]) + 1))
216 target = schemaldb.searchone(basedn=schemadn,
217 expression=expression,
218 attribute="lDAPDisplayName",
219 scope=SCOPE_SUBTREE)
220 if target is not None:
221 attributes[str(res[i]["lDAPDisplayName"])] = str(target)
223 return attributes
226 def get_dnsyntax_attributes(schemadn, schemaldb):
227 res = schemaldb.search(
228 expression="(&(!(linkID=*))(objectclass=attributeSchema)(attributeSyntax=2.5.5.1))",
229 base=schemadn, scope=SCOPE_ONELEVEL,
230 attrs=["linkID", "lDAPDisplayName"])
231 attributes = []
232 for i in range(0, len(res)):
233 attributes.append(str(res[i]["lDAPDisplayName"]))
234 return attributes
237 def ldb_with_schema(schemadn="cn=schema,cn=configuration,dc=example,dc=com",
238 domainsid=None,
239 override_prefixmap=None):
240 """Load schema for the SamDB from the AD schema files and samba4_schema.ldif
242 :param schemadn: DN of the schema
243 :param serverdn: DN of the server
245 Returns the schema data loaded as an object, with .ldb being a
246 new ldb with the schema loaded. This allows certain tests to
247 operate without a remote or local schema.
250 if domainsid is None:
251 domainsid = security.random_sid()
252 else:
253 domainsid = security.dom_sid(domainsid)
254 return Schema(domainsid, schemadn=schemadn,
255 override_prefixmap=override_prefixmap)