2 Unix SMB/CIFS mplementation.
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "dsdb/samdb/samdb.h"
25 #include "dsdb/common/util.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "../lib/util/dlinklist.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include "lib/ldb/include/ldb_module.h"
33 #include "../lib/util/asn1.h"
36 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx)
38 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
46 struct dsdb_schema *dsdb_schema_copy_shallow(TALLOC_CTX *mem_ctx,
47 struct ldb_context *ldb,
48 const struct dsdb_schema *schema)
51 struct dsdb_class *cls;
52 struct dsdb_attribute *attr;
53 struct dsdb_schema *schema_copy;
55 schema_copy = dsdb_new_schema(mem_ctx);
60 /* copy prexiMap & schemaInfo */
61 schema_copy->prefixmap = dsdb_schema_pfm_copy_shallow(schema_copy,
63 if (!schema_copy->prefixmap) {
67 schema_copy->schema_info = talloc_strdup(schema_copy, schema->schema_info);
69 /* copy classes and attributes*/
70 for (cls = schema->classes; cls; cls = cls->next) {
71 struct dsdb_class *class_copy = talloc_memdup(schema_copy,
76 DLIST_ADD(schema_copy->classes, class_copy);
78 schema_copy->num_classes = schema->num_classes;
80 for (attr = schema->attributes; attr; attr = attr->next) {
81 struct dsdb_attribute *a_copy = talloc_memdup(schema_copy,
86 DLIST_ADD(schema_copy->attributes, a_copy);
88 schema_copy->num_attributes = schema->num_attributes;
91 ret = dsdb_setup_sorted_accessors(ldb, schema_copy);
92 if (ret != LDB_SUCCESS) {
99 talloc_free(schema_copy);
104 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
105 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
108 const char *schema_info;
109 struct dsdb_schema_prefixmap *pfm;
111 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
112 W_ERROR_NOT_OK_RETURN(werr);
114 /* set loaded prefixMap */
115 talloc_free(schema->prefixmap);
116 schema->prefixmap = pfm;
118 talloc_free(discard_const(schema->schema_info));
119 schema->schema_info = schema_info;
124 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
126 struct dsdb_schema_prefixmap **_pfm)
129 enum ndr_err_code ndr_err;
130 struct prefixMapBlob pfm_blob;
132 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
133 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
135 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
137 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
138 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
139 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
140 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
141 (unsigned int)pfm_ldb_val->length, ndr_map_error2string(ndr_err)));
142 talloc_free(temp_ctx);
143 return ntstatus_to_werror(nt_status);
146 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
147 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob.version));
148 talloc_free(temp_ctx);
149 return WERR_VERSION_PARSE_ERROR;
152 /* call the drsuapi version */
153 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
154 if (!W_ERROR_IS_OK(werr)) {
155 DEBUG(0, (__location__ " dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr)));
156 talloc_free(temp_ctx);
160 talloc_free(temp_ctx);
165 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
166 const struct ldb_val *prefixMap,
167 const struct ldb_val *schemaInfo)
170 const char *schema_info;
171 struct dsdb_schema_prefixmap *pfm;
174 /* verify schemaInfo blob is valid one */
175 if (!dsdb_schema_info_blob_is_valid(schemaInfo)) {
176 DEBUG(0,(__location__": dsdb_schema_info_blob_is_valid() failed.\n"));
177 return WERR_INVALID_PARAMETER;
180 mem_ctx = talloc_new(schema);
181 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
183 /* fetch prefixMap */
184 werr = _dsdb_prefixmap_from_ldb_val(prefixMap,
186 if (!W_ERROR_IS_OK(werr)) {
187 DEBUG(0, (__location__ " _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr)));
188 talloc_free(mem_ctx);
192 /* decode schema_info */
193 schema_info = hex_encode_talloc(mem_ctx,
197 talloc_free(mem_ctx);
201 /* store prefixMap and schema_info into cached Schema */
202 talloc_free(schema->prefixmap);
203 schema->prefixmap = talloc_steal(schema, pfm);
205 talloc_free(discard_const(schema->schema_info));
206 schema->schema_info = talloc_steal(schema, schema_info);
208 /* clean up locally allocated mem */
209 talloc_free(mem_ctx);
214 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
215 bool include_schema_info,
217 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
219 return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
220 include_schema_info ? schema->schema_info : NULL,
224 WERROR dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr,
226 struct ldb_val *prefixMap)
228 struct prefixMapBlob pfm;
229 enum ndr_err_code ndr_err;
230 pfm.version = PREFIX_MAP_VERSION_DSDB;
234 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, &pfm,
235 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
236 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
237 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
238 return ntstatus_to_werror(nt_status);
243 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
245 struct ldb_val *prefixMap,
246 struct ldb_val *schemaInfo)
249 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
251 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
252 W_ERROR_NOT_OK_RETURN(status);
254 status = dsdb_get_drsuapi_prefixmap_as_blob(ctr, mem_ctx, prefixMap);
256 W_ERROR_NOT_OK_RETURN(status);
258 *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
259 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
266 * this function is called from within a ldb transaction from the schema_fsmo module
268 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
273 struct dsdb_schema_prefixmap *pfm;
275 mem_ctx = talloc_new(ldb);
276 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
278 /* Read prefixes from disk*/
279 status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
280 if (!W_ERROR_IS_OK(status)) {
281 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
282 win_errstr(status)));
283 talloc_free(mem_ctx);
287 /* Check if there is a prefix for the oid in the prefixes array*/
288 status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
289 if (W_ERROR_IS_OK(status)) {
291 talloc_free(mem_ctx);
293 } else if (!W_ERROR_EQUAL(status, WERR_NOT_FOUND)) {
295 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
296 win_errstr(status)));
297 talloc_free(mem_ctx);
301 /* Create the new mapping for the prefix of full_oid */
302 status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
303 if (!W_ERROR_IS_OK(status)) {
304 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
305 win_errstr(status)));
306 talloc_free(mem_ctx);
310 talloc_unlink(schema, schema->prefixmap);
311 schema->prefixmap = talloc_steal(schema, pfm);
313 /* Update prefixMap in ldb*/
314 status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
315 if (!W_ERROR_IS_OK(status)) {
316 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
317 win_errstr(status)));
318 talloc_free(mem_ctx);
322 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
323 full_oid, schema->prefixmap->length));
325 talloc_free(mem_ctx);
330 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
331 const struct dsdb_schema *schema)
335 struct ldb_message *msg;
336 struct ldb_dn *schema_dn;
337 struct prefixMapBlob pfm_blob;
338 struct ldb_val ndr_blob;
339 enum ndr_err_code ndr_err;
340 TALLOC_CTX *temp_ctx;
341 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
343 schema_dn = ldb_get_schema_basedn(ldb);
345 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
349 temp_ctx = talloc_new(mem_ctx);
350 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
352 /* convert schema_prefixMap to prefixMap blob */
353 status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
354 if (!W_ERROR_IS_OK(status)) {
355 talloc_free(temp_ctx);
359 pfm_blob.version = PREFIX_MAP_VERSION_DSDB;
360 pfm_blob.ctr.dsdb = *ctr;
362 ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
364 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
365 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
366 talloc_free(temp_ctx);
370 /* write serialized prefixMap into LDB */
371 msg = ldb_msg_new(temp_ctx);
373 talloc_free(temp_ctx);
378 ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
380 talloc_free(temp_ctx);
381 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
385 ldb_ret = dsdb_replace(ldb, msg, DSDB_FLAG_AS_SYSTEM);
387 talloc_free(temp_ctx);
390 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
397 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
401 const struct ldb_val *prefix_val;
402 struct ldb_dn *schema_dn;
403 struct ldb_result *schema_res = NULL;
404 static const char *schema_attrs[] = {
409 schema_dn = ldb_get_schema_basedn(ldb);
411 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
415 ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
416 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
417 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
418 talloc_free(schema_res);
420 } else if (ldb_ret != LDB_SUCCESS) {
421 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
422 talloc_free(schema_res);
426 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
428 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
429 talloc_free(schema_res);
433 werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
436 talloc_free(schema_res);
437 W_ERROR_NOT_OK_RETURN(werr);
443 this will be replaced with something that looks at the right part of
444 the schema once we know where unique indexing information is hidden
446 static bool dsdb_schema_unique_attribute(const char *attr)
448 const char *attrs[] = { "objectGUID", "objectSid" , NULL };
450 for (i=0;attrs[i];i++) {
451 if (strcasecmp(attr, attrs[i]) == 0) {
460 setup the ldb_schema_attribute field for a dsdb_attribute
462 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
463 struct dsdb_attribute *attr)
465 const char *syntax = attr->syntax->ldb_syntax;
466 const struct ldb_schema_syntax *s;
467 struct ldb_schema_attribute *a;
470 syntax = attr->syntax->ldap_oid;
473 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
475 s = ldb_samba_syntax_by_name(ldb, syntax);
478 s = ldb_standard_syntax_by_name(ldb, syntax);
482 return ldb_operr(ldb);
485 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
486 if (attr->ldb_schema_attribute == NULL) {
490 a->name = attr->lDAPDisplayName;
494 if (dsdb_schema_unique_attribute(a->name)) {
495 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
497 if (attr->isSingleValued) {
498 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
506 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
507 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
508 if (get_string_val == NULL) { \
510 d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
511 return WERR_INVALID_PARAM; \
516 (p)->elem = talloc_strndup(mem_ctx, \
517 (const char *)get_string_val->data, \
518 get_string_val->length); \
520 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
526 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do { \
527 int get_string_list_counter; \
528 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
529 /* We may get empty attributes over the replication channel */ \
530 if (get_string_list_el == NULL || get_string_list_el->num_values == 0) { \
534 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
535 for (get_string_list_counter=0; \
536 get_string_list_counter < get_string_list_el->num_values; \
537 get_string_list_counter++) { \
538 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
539 (const char *)get_string_list_el->values[get_string_list_counter].data, \
540 get_string_list_el->values[get_string_list_counter].length); \
541 if (!(p)->elem[get_string_list_counter]) { \
542 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
545 (p)->elem[get_string_list_counter+1] = NULL; \
547 talloc_steal(mem_ctx, (p)->elem); \
550 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
552 str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
555 d_printf("%s: %s == NULL\n", __location__, attr); \
556 return WERR_INVALID_PARAM; \
560 } else if (strcasecmp("TRUE", str) == 0) { \
562 } else if (strcasecmp("FALSE", str) == 0) { \
565 d_printf("%s: %s == %s\n", __location__, attr, str); \
566 return WERR_INVALID_PARAM; \
570 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
571 (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
574 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
575 uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
576 if (_v == UINT64_MAX) { \
578 } else if (_v > UINT32_MAX) { \
579 d_printf("%s: %s == 0x%llX\n", __location__, \
580 attr, (unsigned long long)_v); \
581 return WERR_INVALID_PARAM; \
583 (p)->elem = talloc(mem_ctx, uint32_t); \
585 d_printf("%s: talloc failed for %s\n", __location__, attr); \
588 *(p)->elem = (uint32_t)_v; \
592 #define GET_GUID_LDB(msg, attr, p, elem) do { \
593 (p)->elem = samdb_result_guid(msg, attr);\
596 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
597 const struct ldb_val *_val;\
598 _val = ldb_msg_find_ldb_val(msg, attr);\
601 talloc_steal(mem_ctx, (p)->elem.data);\
603 ZERO_STRUCT((p)->elem);\
607 WERROR dsdb_attribute_from_ldb(struct ldb_context *ldb,
608 struct dsdb_schema *schema,
609 struct ldb_message *msg)
612 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
617 GET_STRING_LDB(msg, "cn", attr, attr, cn, false);
618 GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
619 GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
620 if (!schema->prefixmap || schema->prefixmap->length == 0) {
621 /* set an invalid value */
622 attr->attributeID_id = DRSUAPI_ATTID_INVALID;
624 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
625 attr->attributeID_oid,
626 &attr->attributeID_id);
627 if (!W_ERROR_IS_OK(status)) {
628 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
629 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
630 win_errstr(status)));
634 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
635 GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
637 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
638 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
640 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
642 GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
644 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
645 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
646 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
647 GET_UINT32_LDB(msg, "linkID", attr, linkID);
649 GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
650 if (!schema->prefixmap || schema->prefixmap->length == 0) {
651 /* set an invalid value */
652 attr->attributeSyntax_id = DRSUAPI_ATTID_INVALID;
654 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
655 attr->attributeSyntax_oid,
656 &attr->attributeSyntax_id);
657 if (!W_ERROR_IS_OK(status)) {
658 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
659 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
660 win_errstr(status)));
664 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
665 GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
667 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
668 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
669 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
670 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
672 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
673 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
675 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
676 GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
677 GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
678 GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
679 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
680 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
681 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
683 attr->syntax = dsdb_syntax_for_attribute(attr);
685 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
686 attr->lDAPDisplayName));
687 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
690 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
691 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
692 attr->lDAPDisplayName,
693 attr->syntax->ldb_syntax,
694 attr->syntax->ldap_oid));
695 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
698 DLIST_ADD(schema->attributes, attr);
702 WERROR dsdb_class_from_ldb(struct dsdb_schema *schema,
703 struct ldb_message *msg)
706 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
710 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
711 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
712 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
713 if (!schema->prefixmap || schema->prefixmap->length == 0) {
714 /* set an invalid value */
715 obj->governsID_id = DRSUAPI_ATTID_INVALID;
717 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
720 if (!W_ERROR_IS_OK(status)) {
721 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
722 __location__, obj->lDAPDisplayName, obj->governsID_oid,
723 win_errstr(status)));
727 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
728 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
730 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
731 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
732 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
734 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
736 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
737 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
739 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
740 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
741 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
742 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
744 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
745 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
747 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
749 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
750 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
752 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
753 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
754 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
755 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
756 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
757 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
758 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
760 DLIST_ADD(schema->classes, obj);
764 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
767 Create a DSDB schema from the ldb results provided. This is called
768 directly when the schema is provisioned from an on-disk LDIF file, or
769 from dsdb_schema_from_schema_dn in schema_fsmo
772 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
773 struct ldb_result *schema_res,
774 struct ldb_result *attrs_res, struct ldb_result *objectclass_res,
775 struct dsdb_schema **schema_out,
780 const struct ldb_val *prefix_val;
781 const struct ldb_val *info_val;
782 struct ldb_val info_val_default;
783 struct dsdb_schema *schema;
785 schema = dsdb_new_schema(mem_ctx);
787 dsdb_oom(error_string, mem_ctx);
788 return ldb_operr(ldb);
791 schema->base_dn = talloc_steal(schema, schema_res->msgs[0]->dn);
793 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
795 *error_string = talloc_asprintf(mem_ctx,
796 "schema_fsmo_init: no prefixMap attribute found");
797 DEBUG(0,(__location__ ": %s\n", *error_string));
798 return LDB_ERR_CONSTRAINT_VIOLATION;
800 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
802 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
803 if (!W_ERROR_IS_OK(status)) {
804 *error_string = talloc_asprintf(mem_ctx,
805 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
807 DEBUG(0,(__location__ ": %s\n", *error_string));
808 return ldb_operr(ldb);
810 info_val = &info_val_default;
813 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
814 if (!W_ERROR_IS_OK(status)) {
815 *error_string = talloc_asprintf(mem_ctx,
816 "schema_fsmo_init: failed to load oid mappings: %s",
818 DEBUG(0,(__location__ ": %s\n", *error_string));
819 return LDB_ERR_CONSTRAINT_VIOLATION;
822 for (i=0; i < attrs_res->count; i++) {
823 status = dsdb_attribute_from_ldb(ldb, schema, attrs_res->msgs[i]);
824 if (!W_ERROR_IS_OK(status)) {
825 *error_string = talloc_asprintf(mem_ctx,
826 "schema_fsmo_init: failed to load attribute definition: %s:%s",
827 ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
829 DEBUG(0,(__location__ ": %s\n", *error_string));
830 return LDB_ERR_CONSTRAINT_VIOLATION;
834 for (i=0; i < objectclass_res->count; i++) {
835 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i]);
836 if (!W_ERROR_IS_OK(status)) {
837 *error_string = talloc_asprintf(mem_ctx,
838 "schema_fsmo_init: failed to load class definition: %s:%s",
839 ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
841 DEBUG(0,(__location__ ": %s\n", *error_string));
842 return LDB_ERR_CONSTRAINT_VIOLATION;
846 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
847 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
848 schema->fsmo.we_are_master = true;
850 schema->fsmo.we_are_master = false;
853 DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
854 (schema->fsmo.we_are_master?"yes":"no")));
856 *schema_out = schema;