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
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 "lib/util/dlinklist.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "lib/ldb/include/ldb_module.h"
27 #include "param/param.h"
28 #include "librpc/ndr/libndr.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "lib/util/tsort.h"
33 override the name to attribute handler function
35 const struct ldb_schema_attribute *dsdb_attribute_handler_override(struct ldb_context *ldb,
39 struct dsdb_schema *schema = talloc_get_type_abort(private_data, struct dsdb_schema);
40 const struct dsdb_attribute *a = dsdb_attribute_by_lDAPDisplayName(schema, name);
42 /* this will fall back to ldb internal handling */
45 return a->ldb_schema_attribute;
48 static int dsdb_schema_set_attributes(struct ldb_context *ldb, struct dsdb_schema *schema, bool write_attributes)
50 int ret = LDB_SUCCESS;
51 struct ldb_result *res;
52 struct ldb_result *res_idx;
53 struct dsdb_attribute *attr;
54 struct ldb_message *mod_msg;
56 struct ldb_message *msg;
57 struct ldb_message *msg_idx;
59 /* setup our own attribute name to schema handler */
60 ldb_schema_attribute_set_override_handler(ldb, dsdb_attribute_handler_override, schema);
62 if (!write_attributes) {
66 mem_ctx = talloc_new(ldb);
71 msg = ldb_msg_new(mem_ctx);
76 msg_idx = ldb_msg_new(mem_ctx);
81 msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
86 msg_idx->dn = ldb_dn_new(msg_idx, ldb, "@INDEXLIST");
92 ret = ldb_msg_add_string(msg_idx, "@IDXONE", "1");
93 if (ret != LDB_SUCCESS) {
97 for (attr = schema->attributes; attr; attr = attr->next) {
98 const char *syntax = attr->syntax->ldb_syntax;
101 syntax = attr->syntax->ldap_oid;
105 * Write out a rough approximation of the schema
106 * as an @ATTRIBUTES value, for bootstrapping
108 if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
109 ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
110 } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
111 ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "CASE_INSENSITIVE");
113 if (ret != LDB_SUCCESS) {
117 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX) {
118 ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
119 if (ret != LDB_SUCCESS) {
125 if (ret != LDB_SUCCESS) {
126 talloc_free(mem_ctx);
131 * Try to avoid churning the attributes too much,
132 * we only want to do this if they have changed
134 ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL,
136 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
137 ret = ldb_add(ldb, msg);
138 } else if (ret != LDB_SUCCESS) {
139 } else if (res->count != 1) {
140 ret = ldb_add(ldb, msg);
143 /* Annoyingly added to our search results */
144 ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
146 ret = ldb_msg_difference(ldb, mem_ctx,
147 res->msgs[0], msg, &mod_msg);
148 if (ret != LDB_SUCCESS) {
151 if (mod_msg->num_elements > 0) {
152 ret = dsdb_replace(ldb, mod_msg, 0);
154 talloc_free(mod_msg);
157 if (ret == LDB_ERR_OPERATIONS_ERROR || ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS || ret == LDB_ERR_INVALID_DN_SYNTAX) {
158 /* We might be on a read-only DB or LDAP */
161 if (ret != LDB_SUCCESS) {
162 talloc_free(mem_ctx);
166 /* Now write out the indexes, as found in the schema (if they have changed) */
168 ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE,
170 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
171 ret = ldb_add(ldb, msg_idx);
172 } else if (ret != LDB_SUCCESS) {
173 } else if (res_idx->count != 1) {
174 ret = ldb_add(ldb, msg_idx);
177 /* Annoyingly added to our search results */
178 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
180 ret = ldb_msg_difference(ldb, mem_ctx,
181 res_idx->msgs[0], msg_idx, &mod_msg);
182 if (ret != LDB_SUCCESS) {
185 if (mod_msg->num_elements > 0) {
186 ret = dsdb_replace(ldb, mod_msg, 0);
188 talloc_free(mod_msg);
190 if (ret == LDB_ERR_OPERATIONS_ERROR || ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS || ret == LDB_ERR_INVALID_DN_SYNTAX) {
191 /* We might be on a read-only DB */
194 talloc_free(mem_ctx);
198 talloc_free(mem_ctx);
199 return ldb_operr(ldb);
202 static int uint32_cmp(uint32_t c1, uint32_t c2)
204 if (c1 == c2) return 0;
205 return c1 > c2 ? 1 : -1;
208 static int dsdb_compare_class_by_lDAPDisplayName(struct dsdb_class **c1, struct dsdb_class **c2)
210 return strcasecmp((*c1)->lDAPDisplayName, (*c2)->lDAPDisplayName);
212 static int dsdb_compare_class_by_governsID_id(struct dsdb_class **c1, struct dsdb_class **c2)
214 return uint32_cmp((*c1)->governsID_id, (*c2)->governsID_id);
216 static int dsdb_compare_class_by_governsID_oid(struct dsdb_class **c1, struct dsdb_class **c2)
218 return strcasecmp((*c1)->governsID_oid, (*c2)->governsID_oid);
220 static int dsdb_compare_class_by_cn(struct dsdb_class **c1, struct dsdb_class **c2)
222 return strcasecmp((*c1)->cn, (*c2)->cn);
225 static int dsdb_compare_attribute_by_lDAPDisplayName(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
227 return strcasecmp((*a1)->lDAPDisplayName, (*a2)->lDAPDisplayName);
229 static int dsdb_compare_attribute_by_attributeID_id(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
231 return uint32_cmp((*a1)->attributeID_id, (*a2)->attributeID_id);
233 static int dsdb_compare_attribute_by_msDS_IntId(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
235 return uint32_cmp((*a1)->msDS_IntId, (*a2)->msDS_IntId);
237 static int dsdb_compare_attribute_by_attributeID_oid(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
239 return strcasecmp((*a1)->attributeID_oid, (*a2)->attributeID_oid);
241 static int dsdb_compare_attribute_by_linkID(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
243 return uint32_cmp((*a1)->linkID, (*a2)->linkID);
247 * Clean up Classes and Attributes accessor arrays
249 static void dsdb_sorted_accessors_free(struct dsdb_schema *schema)
251 /* free classes accessors */
252 TALLOC_FREE(schema->classes_by_lDAPDisplayName);
253 TALLOC_FREE(schema->classes_by_governsID_id);
254 TALLOC_FREE(schema->classes_by_governsID_oid);
255 TALLOC_FREE(schema->classes_by_cn);
256 /* free attribute accessors */
257 TALLOC_FREE(schema->attributes_by_lDAPDisplayName);
258 TALLOC_FREE(schema->attributes_by_attributeID_id);
259 TALLOC_FREE(schema->attributes_by_msDS_IntId);
260 TALLOC_FREE(schema->attributes_by_attributeID_oid);
261 TALLOC_FREE(schema->attributes_by_linkID);
265 create the sorted accessor arrays for the schema
267 int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
268 struct dsdb_schema *schema)
270 struct dsdb_class *cur;
271 struct dsdb_attribute *a;
273 unsigned int num_int_id;
275 /* free all caches */
276 dsdb_sorted_accessors_free(schema);
278 /* count the classes */
279 for (i=0, cur=schema->classes; cur; i++, cur=cur->next) /* noop */ ;
280 schema->num_classes = i;
282 /* setup classes_by_* */
283 schema->classes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_class *, i);
284 schema->classes_by_governsID_id = talloc_array(schema, struct dsdb_class *, i);
285 schema->classes_by_governsID_oid = talloc_array(schema, struct dsdb_class *, i);
286 schema->classes_by_cn = talloc_array(schema, struct dsdb_class *, i);
287 if (schema->classes_by_lDAPDisplayName == NULL ||
288 schema->classes_by_governsID_id == NULL ||
289 schema->classes_by_governsID_oid == NULL ||
290 schema->classes_by_cn == NULL) {
294 for (i=0, cur=schema->classes; cur; i++, cur=cur->next) {
295 schema->classes_by_lDAPDisplayName[i] = cur;
296 schema->classes_by_governsID_id[i] = cur;
297 schema->classes_by_governsID_oid[i] = cur;
298 schema->classes_by_cn[i] = cur;
301 /* sort the arrays */
302 TYPESAFE_QSORT(schema->classes_by_lDAPDisplayName, schema->num_classes, dsdb_compare_class_by_lDAPDisplayName);
303 TYPESAFE_QSORT(schema->classes_by_governsID_id, schema->num_classes, dsdb_compare_class_by_governsID_id);
304 TYPESAFE_QSORT(schema->classes_by_governsID_oid, schema->num_classes, dsdb_compare_class_by_governsID_oid);
305 TYPESAFE_QSORT(schema->classes_by_cn, schema->num_classes, dsdb_compare_class_by_cn);
307 /* now build the attribute accessor arrays */
309 /* count the attributes
310 * and attributes with msDS-IntId set */
312 for (i=0, a=schema->attributes; a; i++, a=a->next) {
313 if (a->msDS_IntId != 0) {
317 schema->num_attributes = i;
318 schema->num_int_id_attr = num_int_id;
320 /* setup attributes_by_* */
321 schema->attributes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_attribute *, i);
322 schema->attributes_by_attributeID_id = talloc_array(schema, struct dsdb_attribute *, i);
323 schema->attributes_by_msDS_IntId = talloc_array(schema,
324 struct dsdb_attribute *, num_int_id);
325 schema->attributes_by_attributeID_oid = talloc_array(schema, struct dsdb_attribute *, i);
326 schema->attributes_by_linkID = talloc_array(schema, struct dsdb_attribute *, i);
327 if (schema->attributes_by_lDAPDisplayName == NULL ||
328 schema->attributes_by_attributeID_id == NULL ||
329 schema->attributes_by_msDS_IntId == NULL ||
330 schema->attributes_by_attributeID_oid == NULL ||
331 schema->attributes_by_linkID == NULL) {
336 for (i=0, a=schema->attributes; a; i++, a=a->next) {
337 schema->attributes_by_lDAPDisplayName[i] = a;
338 schema->attributes_by_attributeID_id[i] = a;
339 schema->attributes_by_attributeID_oid[i] = a;
340 schema->attributes_by_linkID[i] = a;
341 /* append attr-by-msDS-IntId values */
342 if (a->msDS_IntId != 0) {
343 schema->attributes_by_msDS_IntId[num_int_id] = a;
347 SMB_ASSERT(num_int_id == schema->num_int_id_attr);
349 /* sort the arrays */
350 TYPESAFE_QSORT(schema->attributes_by_lDAPDisplayName, schema->num_attributes, dsdb_compare_attribute_by_lDAPDisplayName);
351 TYPESAFE_QSORT(schema->attributes_by_attributeID_id, schema->num_attributes, dsdb_compare_attribute_by_attributeID_id);
352 TYPESAFE_QSORT(schema->attributes_by_msDS_IntId, schema->num_int_id_attr, dsdb_compare_attribute_by_msDS_IntId);
353 TYPESAFE_QSORT(schema->attributes_by_attributeID_oid, schema->num_attributes, dsdb_compare_attribute_by_attributeID_oid);
354 TYPESAFE_QSORT(schema->attributes_by_linkID, schema->num_attributes, dsdb_compare_attribute_by_linkID);
359 dsdb_sorted_accessors_free(schema);
363 int dsdb_setup_schema_inversion(struct ldb_context *ldb, struct dsdb_schema *schema)
365 /* Walk the list of schema classes */
367 /* For each subClassOf, add us to subclasses of the parent */
369 /* collect these subclasses into a recursive list of total subclasses, preserving order */
371 /* For each subclass under 'top', write the index from it's
372 * order as an integer in the dsdb_class (for sorting
373 * objectClass lists efficiently) */
375 /* Walk the list of schema classes */
377 /* Create a 'total possible superiors' on each class */
382 * Attach the schema to an opaque pointer on the ldb,
383 * so ldb modules can find it
385 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
387 struct dsdb_schema *old_schema;
390 ret = dsdb_setup_sorted_accessors(ldb, schema);
391 if (ret != LDB_SUCCESS) {
395 ret = schema_fill_constructed(schema);
396 if (ret != LDB_SUCCESS) {
400 old_schema = ldb_get_opaque(ldb, "dsdb_schema");
402 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
403 if (ret != LDB_SUCCESS) {
407 /* Remove the reference to the schema we just overwrote - if there was
408 * none, NULL is harmless here */
409 if (old_schema != schema) {
410 talloc_unlink(ldb, old_schema);
411 talloc_steal(ldb, schema);
414 ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
415 if (ret != LDB_SUCCESS) {
419 /* Set the new attributes based on the new schema */
420 ret = dsdb_schema_set_attributes(ldb, schema, true);
421 if (ret != LDB_SUCCESS) {
429 * Global variable to hold one copy of the schema, used to avoid memory bloat
431 static struct dsdb_schema *global_schema;
434 * Make this ldb use a specified schema, already fully calculated and belonging to another ldb
436 int dsdb_reference_schema(struct ldb_context *ldb, struct dsdb_schema *schema,
437 bool write_attributes)
440 struct dsdb_schema *old_schema;
441 old_schema = ldb_get_opaque(ldb, "dsdb_schema");
442 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
443 if (ret != LDB_SUCCESS) {
447 /* Remove the reference to the schema we just overwrote - if there was
448 * none, NULL is harmless here */
449 talloc_unlink(ldb, old_schema);
451 if (talloc_reference(ldb, schema) == NULL) {
455 /* Make this ldb use local schema preferably */
456 ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
457 if (ret != LDB_SUCCESS) {
461 ret = dsdb_schema_set_attributes(ldb, schema, write_attributes);
462 if (ret != LDB_SUCCESS) {
470 * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
472 int dsdb_set_global_schema(struct ldb_context *ldb)
475 void *use_global_schema = (void *)1;
476 if (!global_schema) {
479 ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", use_global_schema);
480 if (ret != LDB_SUCCESS) {
484 /* Set the new attributes based on the new schema */
485 ret = dsdb_schema_set_attributes(ldb, global_schema, false /* Don't write attributes, it's expensive */);
486 if (ret == LDB_SUCCESS) {
487 /* Keep a reference to this schema, just in case the original copy is replaced */
488 if (talloc_reference(ldb, global_schema) == NULL) {
496 bool dsdb_uses_global_schema(struct ldb_context *ldb)
498 return (ldb_get_opaque(ldb, "dsdb_use_global_schema") != NULL);
502 * Find the schema object for this ldb
504 * If reference_ctx is not NULL, then talloc_reference onto that context
507 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb, TALLOC_CTX *reference_ctx)
510 struct dsdb_schema *schema_out;
511 struct dsdb_schema *schema_in;
512 bool use_global_schema;
513 TALLOC_CTX *tmp_ctx = talloc_new(reference_ctx);
518 /* see if we have a cached copy */
519 use_global_schema = dsdb_uses_global_schema(ldb);
520 if (use_global_schema) {
521 schema_in = global_schema;
523 p = ldb_get_opaque(ldb, "dsdb_schema");
525 schema_in = talloc_get_type(p, struct dsdb_schema);
527 talloc_free(tmp_ctx);
532 if (schema_in->refresh_fn && !schema_in->refresh_in_progress) {
533 if (!talloc_reference(tmp_ctx, schema_in)) {
535 * ensure that the schema_in->refresh_in_progress
536 * remains valid for the right amount of time
538 talloc_free(tmp_ctx);
541 schema_in->refresh_in_progress = true;
542 /* This may change schema, if it needs to reload it from disk */
543 schema_out = schema_in->refresh_fn(schema_in->loaded_from_module,
546 schema_in->refresh_in_progress = false;
548 schema_out = schema_in;
551 /* This removes the extra reference above */
552 talloc_free(tmp_ctx);
553 if (!reference_ctx) {
556 return talloc_reference(reference_ctx, schema_out);
561 * Make the schema found on this ldb the 'global' schema
564 void dsdb_make_schema_global(struct ldb_context *ldb, struct dsdb_schema *schema)
571 talloc_unlink(talloc_autofree_context(), global_schema);
574 /* we want the schema to be around permanently */
575 talloc_reparent(ldb, talloc_autofree_context(), schema);
576 global_schema = schema;
578 /* This calls the talloc_reference() of the global schema back onto the ldb */
579 dsdb_set_global_schema(ldb);
583 * When loading the schema from LDIF files, we don't get the extended DNs.
585 * We need to set these up, so that from the moment we start the provision,
586 * the defaultObjectCategory links are set up correctly.
588 int dsdb_schema_fill_extended_dn(struct ldb_context *ldb, struct dsdb_schema *schema)
590 struct dsdb_class *cur;
591 const struct dsdb_class *target_class;
592 for (cur = schema->classes; cur; cur = cur->next) {
593 const struct ldb_val *rdn;
596 struct ldb_dn *dn = ldb_dn_new(NULL, ldb, cur->defaultObjectCategory);
599 return LDB_ERR_INVALID_DN_SYNTAX;
601 rdn = ldb_dn_get_component_val(dn, 0);
604 return LDB_ERR_INVALID_DN_SYNTAX;
606 target_class = dsdb_class_by_cn_ldb_val(schema, rdn);
609 return LDB_ERR_CONSTRAINT_VIOLATION;
612 status = GUID_to_ndr_blob(&target_class->objectGUID, dn, &guid);
613 if (!NT_STATUS_IS_OK(status)) {
615 return ldb_operr(ldb);
617 ldb_dn_set_extended_component(dn, "GUID", &guid);
619 cur->defaultObjectCategory = ldb_dn_get_extended_linearized(cur, dn, 1);
626 * Add an element to the schema (attribute or class) from an LDB message
628 WERROR dsdb_schema_set_el_from_ldb_msg(struct ldb_context *ldb, struct dsdb_schema *schema,
629 struct ldb_message *msg)
631 if (samdb_find_attribute(ldb, msg,
632 "objectclass", "attributeSchema") != NULL) {
633 return dsdb_attribute_from_ldb(ldb, schema, msg);
634 } else if (samdb_find_attribute(ldb, msg,
635 "objectclass", "classSchema") != NULL) {
636 return dsdb_class_from_ldb(schema, msg);
639 /* Don't fail on things not classes or attributes */
644 * Rather than read a schema from the LDB itself, read it from an ldif
645 * file. This allows schema to be loaded and used while adding the
646 * schema itself to the directory.
649 WERROR dsdb_set_schema_from_ldif(struct ldb_context *ldb, const char *pf, const char *df)
651 struct ldb_ldif *ldif;
652 struct ldb_message *msg;
656 struct dsdb_schema *schema;
657 const struct ldb_val *prefix_val;
658 const struct ldb_val *info_val;
659 struct ldb_val info_val_default;
662 mem_ctx = talloc_new(ldb);
667 schema = dsdb_new_schema(mem_ctx);
669 schema->fsmo.we_are_master = true;
670 schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
671 if (!schema->fsmo.master_dn) {
676 * load the prefixMap attribute from pf
678 ldif = ldb_ldif_read_string(ldb, &pf);
680 status = WERR_INVALID_PARAM;
683 talloc_steal(mem_ctx, ldif);
685 ret = ldb_msg_normalize(ldb, mem_ctx, ldif->msg, &msg);
686 if (ret != LDB_SUCCESS) {
691 prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
693 status = WERR_INVALID_PARAM;
697 info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
699 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
700 W_ERROR_NOT_OK_GOTO(status, failed);
701 info_val = &info_val_default;
704 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
705 if (!W_ERROR_IS_OK(status)) {
706 DEBUG(0,("ERROR: dsdb_load_oid_mappings_ldb() failed with %s\n", win_errstr(status)));
710 /* load the attribute and class definitions out of df */
711 while ((ldif = ldb_ldif_read_string(ldb, &df))) {
712 talloc_steal(mem_ctx, ldif);
714 ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &msg);
715 if (ret != LDB_SUCCESS) {
719 status = dsdb_schema_set_el_from_ldb_msg(ldb, schema, msg);
721 if (!W_ERROR_IS_OK(status)) {
726 ret = dsdb_set_schema(ldb, schema);
727 if (ret != LDB_SUCCESS) {
728 status = WERR_FOOBAR;
732 ret = dsdb_schema_fill_extended_dn(ldb, schema);
733 if (ret != LDB_SUCCESS) {
734 status = WERR_FOOBAR;
744 talloc_free(mem_ctx);