2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
7 Copyright (C) Matthieu Patou <mat@matws.net> 2011
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/>.
25 #include "lib/util/dlinklist.h"
26 #include "dsdb/samdb/samdb.h"
27 #include <ldb_module.h>
28 #include "param/param.h"
29 #include "librpc/ndr/libndr.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "lib/util/tsort.h"
33 /* change this when we change something in our schema code that
34 * requires a re-index of the database
36 #define SAMDB_INDEXING_VERSION "2"
39 override the name to attribute handler function
41 const struct ldb_schema_attribute *dsdb_attribute_handler_override(struct ldb_context *ldb,
45 struct dsdb_schema *schema = talloc_get_type_abort(private_data, struct dsdb_schema);
46 const struct dsdb_attribute *a = dsdb_attribute_by_lDAPDisplayName(schema, name);
48 /* this will fall back to ldb internal handling */
51 return a->ldb_schema_attribute;
54 * Set the attribute handlers onto the LDB, and potentially write the
55 * @INDEXLIST, @IDXONE and @ATTRIBUTES records. The @ATTRIBUTES records
56 * are required so we can operate on a schema-less database (say the
57 * backend during emergency fixes) and during the schema load.
59 int dsdb_schema_set_indices_and_attributes(struct ldb_context *ldb,
60 struct dsdb_schema *schema,
61 enum schema_set_enum mode)
63 int ret = LDB_SUCCESS;
64 struct ldb_result *res;
65 struct ldb_result *res_idx;
66 struct dsdb_attribute *attr;
67 struct ldb_message *mod_msg;
69 struct ldb_message *msg;
70 struct ldb_message *msg_idx;
72 struct loadparm_context *lp_ctx =
73 talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
74 struct loadparm_context);
75 /* setup our own attribute name to schema handler */
76 ldb_schema_attribute_set_override_handler(ldb, dsdb_attribute_handler_override, schema);
77 ldb_schema_set_override_indexlist(ldb, true);
79 lpcfg_parm_bool(lp_ctx, NULL, "dsdb", "guid index", true)) {
80 ldb_schema_set_override_GUID_index(ldb, "objectGUID", "GUID");
83 if (mode == SCHEMA_MEMORY_ONLY) {
87 mem_ctx = talloc_new(ldb);
92 msg = ldb_msg_new(mem_ctx);
97 msg_idx = ldb_msg_new(mem_ctx);
102 msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
107 msg_idx->dn = ldb_dn_new(msg_idx, ldb, "@INDEXLIST");
113 ret = ldb_msg_add_string(msg_idx, "@IDXONE", "1");
114 if (ret != LDB_SUCCESS) {
118 if (lp_ctx == NULL ||
119 lpcfg_parm_bool(lp_ctx, NULL, "dsdb", "guid index", true)) {
120 ret = ldb_msg_add_string(msg_idx, "@IDXGUID", "objectGUID");
121 if (ret != LDB_SUCCESS) {
125 ret = ldb_msg_add_string(msg_idx, "@IDX_DN_GUID", "GUID");
126 if (ret != LDB_SUCCESS) {
131 ret = ldb_msg_add_string(msg_idx, "@SAMDB_INDEXING_VERSION", SAMDB_INDEXING_VERSION);
132 if (ret != LDB_SUCCESS) {
136 ret = ldb_msg_add_string(msg_idx, SAMBA_FEATURES_SUPPORTED_FLAG, "1");
137 if (ret != LDB_SUCCESS) {
141 for (attr = schema->attributes; attr; attr = attr->next) {
142 const char *syntax = attr->syntax->ldb_syntax;
145 syntax = attr->syntax->ldap_oid;
149 * Write out a rough approximation of the schema
150 * as an @ATTRIBUTES value, for bootstrapping
152 if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
153 ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
154 } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
155 ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "CASE_INSENSITIVE");
157 if (ret != LDB_SUCCESS) {
161 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX) {
162 ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
163 if (ret != LDB_SUCCESS) {
169 if (ret != LDB_SUCCESS) {
170 talloc_free(mem_ctx);
175 * Try to avoid churning the attributes too much,
176 * we only want to do this if they have changed
178 ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL,
180 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
181 if (mode == SCHEMA_COMPARE) {
182 /* We are probably not in a transaction */
185 ret = ldb_add(ldb, msg);
186 } else if (ret != LDB_SUCCESS) {
187 } else if (res->count != 1) {
188 if (mode == SCHEMA_COMPARE) {
189 /* We are probably not in a transaction */
192 ret = ldb_add(ldb, msg);
195 /* Annoyingly added to our search results */
196 ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
198 ret = ldb_msg_difference(ldb, mem_ctx,
199 res->msgs[0], msg, &mod_msg);
200 if (ret != LDB_SUCCESS) {
203 if (mod_msg->num_elements > 0) {
205 * Do the replace with the difference, as we
206 * are under the read lock and we wish to do a
207 * delete of any removed/renamed attributes
209 if (mode == SCHEMA_COMPARE) {
210 /* We are probably not in a transaction */
213 ret = dsdb_modify(ldb, mod_msg, 0);
215 talloc_free(mod_msg);
218 if (ret == LDB_ERR_OPERATIONS_ERROR || ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS || ret == LDB_ERR_INVALID_DN_SYNTAX) {
219 /* We might be on a read-only DB or LDAP */
222 if (ret != LDB_SUCCESS) {
223 DBG_ERR("Failed to set schema into @ATTRIBUTES: %s\n",
225 talloc_free(mem_ctx);
229 /* Now write out the indexes, as found in the schema (if they have changed) */
231 ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE,
233 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
234 if (mode == SCHEMA_COMPARE) {
235 /* We are probably not in a transaction */
238 ret = ldb_add(ldb, msg_idx);
239 } else if (ret != LDB_SUCCESS) {
240 } else if (res_idx->count != 1) {
241 if (mode == SCHEMA_COMPARE) {
242 /* We are probably not in a transaction */
245 ret = ldb_add(ldb, msg_idx);
248 /* Annoyingly added to our search results */
249 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
251 ret = ldb_msg_difference(ldb, mem_ctx,
252 res_idx->msgs[0], msg_idx, &mod_msg);
253 if (ret != LDB_SUCCESS) {
258 * We don't want to re-index just because we didn't
261 * DO NOT backport this logic earlier than 4.7, it
262 * isn't needed and would be dangerous before 4.6,
263 * where we add logic to samba_dsdb to manage
264 * @SAMBA_FEATURES_SUPPORTED and need to know if the
265 * DB has been re-opened by an earlier version.
269 if (mod_msg->num_elements == 1
270 && ldb_attr_cmp(mod_msg->elements[0].name,
271 SAMBA_FEATURES_SUPPORTED_FLAG) == 0) {
274 * @SAMBA_FEATURES_SUPPORTED
276 } else if (mod_msg->num_elements > 0) {
279 * Do the replace with the difference, as we
280 * are under the read lock and we wish to do a
281 * delete of any removed/renamed attributes
283 if (mode == SCHEMA_COMPARE) {
284 /* We are probably not in a transaction */
287 ret = dsdb_modify(ldb, mod_msg, 0);
289 talloc_free(mod_msg);
291 if (ret == LDB_ERR_OPERATIONS_ERROR || ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS || ret == LDB_ERR_INVALID_DN_SYNTAX) {
292 /* We might be on a read-only DB */
296 if (ret != LDB_SUCCESS) {
297 DBG_ERR("Failed to set schema into @INDEXLIST: %s\n",
301 talloc_free(mem_ctx);
305 talloc_free(mem_ctx);
306 return ldb_operr(ldb);
309 talloc_free(mem_ctx);
315 create extra attribute shortcuts
317 static void dsdb_setup_attribute_shortcuts(struct ldb_context *ldb, struct dsdb_schema *schema)
319 struct dsdb_attribute *attribute;
321 /* setup fast access to one_way_link and DN format */
322 for (attribute=schema->attributes; attribute; attribute=attribute->next) {
323 attribute->dn_format = dsdb_dn_oid_to_format(attribute->syntax->ldap_oid);
325 if (attribute->dn_format == DSDB_INVALID_DN) {
326 attribute->one_way_link = false;
330 /* these are not considered to be one way links for
331 the purpose of DN link fixups */
332 if (ldb_attr_cmp("distinguishedName", attribute->lDAPDisplayName) == 0 ||
333 ldb_attr_cmp("objectCategory", attribute->lDAPDisplayName) == 0) {
334 attribute->one_way_link = false;
338 if (attribute->linkID == 0) {
339 attribute->one_way_link = true;
342 /* handle attributes with a linkID but no backlink */
343 if ((attribute->linkID & 1) == 0 &&
344 dsdb_attribute_by_linkID(schema, attribute->linkID + 1) == NULL) {
345 attribute->one_way_link = true;
348 attribute->one_way_link = false;
352 static int uint32_cmp(uint32_t c1, uint32_t c2)
354 if (c1 == c2) return 0;
355 return c1 > c2 ? 1 : -1;
358 static int dsdb_compare_class_by_lDAPDisplayName(struct dsdb_class **c1, struct dsdb_class **c2)
360 return strcasecmp((*c1)->lDAPDisplayName, (*c2)->lDAPDisplayName);
362 static int dsdb_compare_class_by_governsID_id(struct dsdb_class **c1, struct dsdb_class **c2)
364 return uint32_cmp((*c1)->governsID_id, (*c2)->governsID_id);
366 static int dsdb_compare_class_by_governsID_oid(struct dsdb_class **c1, struct dsdb_class **c2)
368 return strcasecmp((*c1)->governsID_oid, (*c2)->governsID_oid);
370 static int dsdb_compare_class_by_cn(struct dsdb_class **c1, struct dsdb_class **c2)
372 return strcasecmp((*c1)->cn, (*c2)->cn);
375 static int dsdb_compare_attribute_by_lDAPDisplayName(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
377 return strcasecmp((*a1)->lDAPDisplayName, (*a2)->lDAPDisplayName);
379 static int dsdb_compare_attribute_by_attributeID_id(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
381 return uint32_cmp((*a1)->attributeID_id, (*a2)->attributeID_id);
383 static int dsdb_compare_attribute_by_msDS_IntId(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
385 return uint32_cmp((*a1)->msDS_IntId, (*a2)->msDS_IntId);
387 static int dsdb_compare_attribute_by_attributeID_oid(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
389 return strcasecmp((*a1)->attributeID_oid, (*a2)->attributeID_oid);
391 static int dsdb_compare_attribute_by_linkID(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
393 return uint32_cmp((*a1)->linkID, (*a2)->linkID);
397 * Clean up Classes and Attributes accessor arrays
399 static void dsdb_sorted_accessors_free(struct dsdb_schema *schema)
401 /* free classes accessors */
402 TALLOC_FREE(schema->classes_by_lDAPDisplayName);
403 TALLOC_FREE(schema->classes_by_governsID_id);
404 TALLOC_FREE(schema->classes_by_governsID_oid);
405 TALLOC_FREE(schema->classes_by_cn);
406 /* free attribute accessors */
407 TALLOC_FREE(schema->attributes_by_lDAPDisplayName);
408 TALLOC_FREE(schema->attributes_by_attributeID_id);
409 TALLOC_FREE(schema->attributes_by_msDS_IntId);
410 TALLOC_FREE(schema->attributes_by_attributeID_oid);
411 TALLOC_FREE(schema->attributes_by_linkID);
415 create the sorted accessor arrays for the schema
417 int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
418 struct dsdb_schema *schema)
420 struct dsdb_class *cur;
421 struct dsdb_attribute *a;
423 unsigned int num_int_id;
426 for (i=0; i < schema->classes_to_remove_size; i++) {
427 DLIST_REMOVE(schema->classes, schema->classes_to_remove[i]);
428 TALLOC_FREE(schema->classes_to_remove[i]);
430 for (i=0; i < schema->attributes_to_remove_size; i++) {
431 DLIST_REMOVE(schema->attributes, schema->attributes_to_remove[i]);
432 TALLOC_FREE(schema->attributes_to_remove[i]);
435 TALLOC_FREE(schema->classes_to_remove);
436 schema->classes_to_remove_size = 0;
437 TALLOC_FREE(schema->attributes_to_remove);
438 schema->attributes_to_remove_size = 0;
440 /* free all caches */
441 dsdb_sorted_accessors_free(schema);
443 /* count the classes */
444 for (i=0, cur=schema->classes; cur; i++, cur=cur->next) /* noop */ ;
445 schema->num_classes = i;
447 /* setup classes_by_* */
448 schema->classes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_class *, i);
449 schema->classes_by_governsID_id = talloc_array(schema, struct dsdb_class *, i);
450 schema->classes_by_governsID_oid = talloc_array(schema, struct dsdb_class *, i);
451 schema->classes_by_cn = talloc_array(schema, struct dsdb_class *, i);
452 if (schema->classes_by_lDAPDisplayName == NULL ||
453 schema->classes_by_governsID_id == NULL ||
454 schema->classes_by_governsID_oid == NULL ||
455 schema->classes_by_cn == NULL) {
459 for (i=0, cur=schema->classes; cur; i++, cur=cur->next) {
460 schema->classes_by_lDAPDisplayName[i] = cur;
461 schema->classes_by_governsID_id[i] = cur;
462 schema->classes_by_governsID_oid[i] = cur;
463 schema->classes_by_cn[i] = cur;
466 /* sort the arrays */
467 TYPESAFE_QSORT(schema->classes_by_lDAPDisplayName, schema->num_classes, dsdb_compare_class_by_lDAPDisplayName);
468 TYPESAFE_QSORT(schema->classes_by_governsID_id, schema->num_classes, dsdb_compare_class_by_governsID_id);
469 TYPESAFE_QSORT(schema->classes_by_governsID_oid, schema->num_classes, dsdb_compare_class_by_governsID_oid);
470 TYPESAFE_QSORT(schema->classes_by_cn, schema->num_classes, dsdb_compare_class_by_cn);
472 /* now build the attribute accessor arrays */
474 /* count the attributes
475 * and attributes with msDS-IntId set */
477 for (i=0, a=schema->attributes; a; i++, a=a->next) {
478 if (a->msDS_IntId != 0) {
482 schema->num_attributes = i;
483 schema->num_int_id_attr = num_int_id;
485 /* setup attributes_by_* */
486 schema->attributes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_attribute *, i);
487 schema->attributes_by_attributeID_id = talloc_array(schema, struct dsdb_attribute *, i);
488 schema->attributes_by_msDS_IntId = talloc_array(schema,
489 struct dsdb_attribute *, num_int_id);
490 schema->attributes_by_attributeID_oid = talloc_array(schema, struct dsdb_attribute *, i);
491 schema->attributes_by_linkID = talloc_array(schema, struct dsdb_attribute *, i);
492 if (schema->attributes_by_lDAPDisplayName == NULL ||
493 schema->attributes_by_attributeID_id == NULL ||
494 schema->attributes_by_msDS_IntId == NULL ||
495 schema->attributes_by_attributeID_oid == NULL ||
496 schema->attributes_by_linkID == NULL) {
501 for (i=0, a=schema->attributes; a; i++, a=a->next) {
502 schema->attributes_by_lDAPDisplayName[i] = a;
503 schema->attributes_by_attributeID_id[i] = a;
504 schema->attributes_by_attributeID_oid[i] = a;
505 schema->attributes_by_linkID[i] = a;
506 /* append attr-by-msDS-IntId values */
507 if (a->msDS_IntId != 0) {
508 schema->attributes_by_msDS_IntId[num_int_id] = a;
512 SMB_ASSERT(num_int_id == schema->num_int_id_attr);
514 /* sort the arrays */
515 TYPESAFE_QSORT(schema->attributes_by_lDAPDisplayName, schema->num_attributes, dsdb_compare_attribute_by_lDAPDisplayName);
516 TYPESAFE_QSORT(schema->attributes_by_attributeID_id, schema->num_attributes, dsdb_compare_attribute_by_attributeID_id);
517 TYPESAFE_QSORT(schema->attributes_by_msDS_IntId, schema->num_int_id_attr, dsdb_compare_attribute_by_msDS_IntId);
518 TYPESAFE_QSORT(schema->attributes_by_attributeID_oid, schema->num_attributes, dsdb_compare_attribute_by_attributeID_oid);
519 TYPESAFE_QSORT(schema->attributes_by_linkID, schema->num_attributes, dsdb_compare_attribute_by_linkID);
521 dsdb_setup_attribute_shortcuts(ldb, schema);
523 ret = schema_fill_constructed(schema);
524 if (ret != LDB_SUCCESS) {
525 dsdb_sorted_accessors_free(schema);
532 dsdb_sorted_accessors_free(schema);
537 * Attach the schema to an opaque pointer on the ldb,
538 * so ldb modules can find it
540 int dsdb_set_schema_refresh_function(struct ldb_context *ldb,
541 dsdb_schema_refresh_fn refresh_fn,
542 struct ldb_module *module)
544 int ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", refresh_fn);
545 if (ret != LDB_SUCCESS) {
549 ret = ldb_set_opaque(ldb, "dsdb_schema_refresh_fn_private_data", module);
550 if (ret != LDB_SUCCESS) {
557 * Attach the schema to an opaque pointer on the ldb,
558 * so ldb modules can find it
560 int dsdb_set_schema(struct ldb_context *ldb,
561 struct dsdb_schema *schema,
562 enum schema_set_enum write_indices_and_attributes)
564 struct dsdb_schema *old_schema;
567 ret = dsdb_setup_sorted_accessors(ldb, schema);
568 if (ret != LDB_SUCCESS) {
572 old_schema = ldb_get_opaque(ldb, "dsdb_schema");
574 ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
575 if (ret != LDB_SUCCESS) {
579 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
580 if (ret != LDB_SUCCESS) {
584 talloc_steal(ldb, schema);
586 /* Set the new attributes based on the new schema */
587 ret = dsdb_schema_set_indices_and_attributes(ldb, schema,
588 write_indices_and_attributes);
589 if (ret != LDB_SUCCESS) {
594 * Remove the reference to the schema we just overwrote - if there was
595 * none, NULL is harmless here.
597 if (old_schema != schema) {
598 talloc_unlink(ldb, old_schema);
605 * Global variable to hold one copy of the schema, used to avoid memory bloat
607 static struct dsdb_schema *global_schema;
610 * Make this ldb use a specified schema, already fully calculated and belonging to another ldb
612 * The write_indices_and_attributes controls writing of the @ records
613 * because we cannot write to a database that does not yet exist on
616 int dsdb_reference_schema(struct ldb_context *ldb, struct dsdb_schema *schema,
617 enum schema_set_enum write_indices_and_attributes)
620 struct dsdb_schema *old_schema;
621 old_schema = ldb_get_opaque(ldb, "dsdb_schema");
622 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
623 if (ret != LDB_SUCCESS) {
627 /* Remove the reference to the schema we just overwrote - if there was
628 * none, NULL is harmless here */
629 talloc_unlink(ldb, old_schema);
631 if (talloc_reference(ldb, schema) == NULL) {
635 /* Make this ldb use local schema preferably */
636 ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
637 if (ret != LDB_SUCCESS) {
641 ret = ldb_set_opaque(ldb, "dsdb_refresh_fn", NULL);
642 if (ret != LDB_SUCCESS) {
646 ret = ldb_set_opaque(ldb, "dsdb_refresh_fn_private_data", NULL);
647 if (ret != LDB_SUCCESS) {
651 ret = dsdb_schema_set_indices_and_attributes(ldb, schema, write_indices_and_attributes);
652 if (ret != LDB_SUCCESS) {
660 * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
662 int dsdb_set_global_schema(struct ldb_context *ldb)
665 void *use_global_schema = (void *)1;
666 struct dsdb_schema *old_schema = ldb_get_opaque(ldb, "dsdb_schema");
668 ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", use_global_schema);
669 if (ret != LDB_SUCCESS) {
673 if (global_schema == NULL) {
677 /* Remove any pointer to a previous schema */
678 ret = ldb_set_opaque(ldb, "dsdb_schema", NULL);
679 if (ret != LDB_SUCCESS) {
683 /* Remove the reference to the schema we just overwrote - if there was
684 * none, NULL is harmless here */
685 talloc_unlink(ldb, old_schema);
687 /* Set the new attributes based on the new schema */
688 /* Don't write indices and attributes, it's expensive */
689 ret = dsdb_schema_set_indices_and_attributes(ldb, global_schema, SCHEMA_MEMORY_ONLY);
690 if (ret == LDB_SUCCESS) {
691 /* Keep a reference to this schema, just in case the original copy is replaced */
692 if (talloc_reference(ldb, global_schema) == NULL) {
700 bool dsdb_uses_global_schema(struct ldb_context *ldb)
702 return (ldb_get_opaque(ldb, "dsdb_use_global_schema") != NULL);
706 * Find the schema object for this ldb
708 * If reference_ctx is not NULL, then talloc_reference onto that context
711 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb, TALLOC_CTX *reference_ctx)
714 struct dsdb_schema *schema_out = NULL;
715 struct dsdb_schema *schema_in = NULL;
716 dsdb_schema_refresh_fn refresh_fn;
717 struct ldb_module *loaded_from_module;
718 bool use_global_schema;
719 TALLOC_CTX *tmp_ctx = talloc_new(reference_ctx);
720 if (tmp_ctx == NULL) {
724 /* see if we have a cached copy */
725 use_global_schema = dsdb_uses_global_schema(ldb);
726 if (use_global_schema) {
727 schema_in = global_schema;
729 p = ldb_get_opaque(ldb, "dsdb_schema");
731 schema_in = talloc_get_type_abort(p, struct dsdb_schema);
735 refresh_fn = ldb_get_opaque(ldb, "dsdb_schema_refresh_fn");
737 loaded_from_module = ldb_get_opaque(ldb, "dsdb_schema_refresh_fn_private_data");
739 SMB_ASSERT(loaded_from_module && (ldb_module_get_ctx(loaded_from_module) == ldb));
743 /* We need to guard against recurisve calls here */
744 if (ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", NULL) != LDB_SUCCESS) {
745 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
746 "dsdb_get_schema: clearing dsdb_schema_refresh_fn failed");
748 schema_out = refresh_fn(loaded_from_module,
749 ldb_get_event_context(ldb),
753 if (ldb_set_opaque(ldb, "dsdb_schema_refresh_fn", refresh_fn) != LDB_SUCCESS) {
754 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
755 "dsdb_get_schema: re-setting dsdb_schema_refresh_fn failed");
758 schema_out = schema_in;
759 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
760 "dsdb_get_schema: refresh_fn() failed");
763 schema_out = schema_in;
766 /* This removes the extra reference above */
767 talloc_free(tmp_ctx);
768 if (!reference_ctx) {
771 return talloc_reference(reference_ctx, schema_out);
776 * Make the schema found on this ldb the 'global' schema
779 void dsdb_make_schema_global(struct ldb_context *ldb, struct dsdb_schema *schema)
786 talloc_unlink(NULL, global_schema);
789 /* we want the schema to be around permanently */
790 talloc_reparent(ldb, NULL, schema);
791 global_schema = schema;
793 /* This calls the talloc_reference() of the global schema back onto the ldb */
794 dsdb_set_global_schema(ldb);
798 * When loading the schema from LDIF files, we don't get the extended DNs.
800 * We need to set these up, so that from the moment we start the provision,
801 * the defaultObjectCategory links are set up correctly.
803 int dsdb_schema_fill_extended_dn(struct ldb_context *ldb, struct dsdb_schema *schema)
805 struct dsdb_class *cur;
806 const struct dsdb_class *target_class;
807 for (cur = schema->classes; cur; cur = cur->next) {
808 const struct ldb_val *rdn;
811 struct ldb_dn *dn = ldb_dn_new(NULL, ldb, cur->defaultObjectCategory);
814 return LDB_ERR_INVALID_DN_SYNTAX;
816 rdn = ldb_dn_get_component_val(dn, 0);
819 return LDB_ERR_INVALID_DN_SYNTAX;
821 target_class = dsdb_class_by_cn_ldb_val(schema, rdn);
824 return LDB_ERR_CONSTRAINT_VIOLATION;
827 status = GUID_to_ndr_blob(&target_class->objectGUID, dn, &guid);
828 if (!NT_STATUS_IS_OK(status)) {
830 return ldb_operr(ldb);
832 ldb_dn_set_extended_component(dn, "GUID", &guid);
834 cur->defaultObjectCategory = ldb_dn_get_extended_linearized(cur, dn, 1);
841 * @brief Add a new element to the schema and checks if it's a duplicate
843 * This function will add a new element to the schema and checks for existing
846 * @param[in] ldb A pointer to an LDB context
848 * @param[in] schema A pointer to the dsdb_schema where the element
851 * @param[in] msg The ldb_message object representing the element
854 * @param[in] checkdups A boolean to indicate if checks for duplicates
857 * @return A WERROR code
859 WERROR dsdb_schema_set_el_from_ldb_msg_dups(struct ldb_context *ldb, struct dsdb_schema *schema,
860 struct ldb_message *msg, bool checkdups)
864 tstring = ldb_msg_find_attr_as_string(msg, "whenChanged", NULL);
865 /* keep a trace of the ts of the most recently changed object */
867 ts = ldb_string_to_time(tstring);
868 if (ts > schema->ts_last_change) {
869 schema->ts_last_change = ts;
872 if (samdb_find_attribute(ldb, msg,
873 "objectclass", "attributeSchema") != NULL) {
875 return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, checkdups);
876 } else if (samdb_find_attribute(ldb, msg,
877 "objectclass", "classSchema") != NULL) {
878 return dsdb_set_class_from_ldb_dups(schema, msg, checkdups);
880 /* Don't fail on things not classes or attributes */
884 WERROR dsdb_schema_set_el_from_ldb_msg(struct ldb_context *ldb,
885 struct dsdb_schema *schema,
886 struct ldb_message *msg)
888 return dsdb_schema_set_el_from_ldb_msg_dups(ldb, schema, msg, false);
892 * Rather than read a schema from the LDB itself, read it from an ldif
893 * file. This allows schema to be loaded and used while adding the
894 * schema itself to the directory.
896 * Should be called with a transaction (or failing that, have no concurrent
897 * access while called).
900 WERROR dsdb_set_schema_from_ldif(struct ldb_context *ldb,
901 const char *pf, const char *df,
904 struct ldb_ldif *ldif;
905 struct ldb_message *msg;
909 struct dsdb_schema *schema;
910 const struct ldb_val *prefix_val;
911 const struct ldb_val *info_val;
912 struct ldb_val info_val_default;
915 mem_ctx = talloc_new(ldb);
920 schema = dsdb_new_schema(mem_ctx);
924 schema->fsmo.we_are_master = true;
925 schema->fsmo.update_allowed = true;
926 schema->fsmo.master_dn = ldb_dn_new(schema, ldb, "@PROVISION_SCHEMA_MASTER");
927 if (!schema->fsmo.master_dn) {
932 * load the prefixMap attribute from pf
934 ldif = ldb_ldif_read_string(ldb, &pf);
936 status = WERR_INVALID_PARAMETER;
939 talloc_steal(mem_ctx, ldif);
941 ret = ldb_msg_normalize(ldb, mem_ctx, ldif->msg, &msg);
942 if (ret != LDB_SUCCESS) {
947 prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
949 status = WERR_INVALID_PARAMETER;
953 info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
955 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
956 W_ERROR_NOT_OK_GOTO(status, failed);
957 info_val = &info_val_default;
960 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
961 if (!W_ERROR_IS_OK(status)) {
962 DEBUG(0,("ERROR: dsdb_load_oid_mappings_ldb() failed with %s\n", win_errstr(status)));
966 schema->ts_last_change = 0;
967 /* load the attribute and class definitions out of df */
968 while ((ldif = ldb_ldif_read_string(ldb, &df))) {
969 talloc_steal(mem_ctx, ldif);
971 ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &msg);
972 if (ret != LDB_SUCCESS) {
976 status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, msg);
978 if (!W_ERROR_IS_OK(status)) {
984 * TODO We may need a transaction here, otherwise this causes races.
986 * To do so may require an ldb_in_transaction function. In the
987 * meantime, assume that this is always called with a transaction or in
990 ret = dsdb_set_schema(ldb, schema, SCHEMA_WRITE);
991 if (ret != LDB_SUCCESS) {
992 status = WERR_FOOBAR;
993 DEBUG(0,("ERROR: dsdb_set_schema() failed with %s / %s\n",
994 ldb_strerror(ret), ldb_errstring(ldb)));
998 ret = dsdb_schema_fill_extended_dn(ldb, schema);
999 if (ret != LDB_SUCCESS) {
1000 status = WERR_FOOBAR;
1007 status = WERR_NOT_ENOUGH_MEMORY;
1010 talloc_free(mem_ctx);