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 "dlinklist.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "lib/ldb/include/ldb_module.h"
27 #include "param/param.h"
30 override the name to attribute handler function
32 const struct ldb_schema_attribute *dsdb_attribute_handler_override(struct ldb_context *ldb,
36 struct dsdb_schema *schema = talloc_get_type_abort(private_data, struct dsdb_schema);
37 const struct dsdb_attribute *a = dsdb_attribute_by_lDAPDisplayName(schema, name);
39 /* this will fall back to ldb internal handling */
42 return a->ldb_schema_attribute;
45 static int dsdb_schema_set_attributes(struct ldb_context *ldb, struct dsdb_schema *schema, bool write_attributes)
47 int ret = LDB_SUCCESS;
48 struct ldb_result *res;
49 struct ldb_result *res_idx;
50 struct dsdb_attribute *attr;
51 struct ldb_message *mod_msg;
53 struct ldb_message *msg;
54 struct ldb_message *msg_idx;
56 /* setup our own attribute name to schema handler */
57 ldb_schema_attribute_set_override_handler(ldb, dsdb_attribute_handler_override, schema);
59 if (!write_attributes) {
63 mem_ctx = talloc_new(ldb);
65 return LDB_ERR_OPERATIONS_ERROR;
68 msg = ldb_msg_new(mem_ctx);
73 msg_idx = ldb_msg_new(mem_ctx);
78 msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
83 msg_idx->dn = ldb_dn_new(msg, ldb, "@INDEXLIST");
89 for (attr = schema->attributes; attr; attr = attr->next) {
90 const char *syntax = attr->syntax->ldb_syntax;
93 syntax = attr->syntax->ldap_oid;
96 /* Write out a rough approximation of the schema as an @ATTRIBUTES value, for bootstrapping */
97 if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
98 ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
99 } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
100 ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "CASE_INSENSITIVE");
102 if (ret != LDB_SUCCESS) {
106 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX) {
107 ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
108 if (ret != LDB_SUCCESS) {
114 if (ret != LDB_SUCCESS) {
115 talloc_free(mem_ctx);
119 /* Try to avoid churning the attributes too much - we only want to do this if they have changed */
120 ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL, "dn=%s", ldb_dn_get_linearized(msg->dn));
121 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
122 ret = ldb_add(ldb, msg);
123 } else if (ret != LDB_SUCCESS) {
124 } else if (res->count != 1) {
125 ret = ldb_add(ldb, msg);
128 /* Annoyingly added to our search results */
129 ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
131 mod_msg = ldb_msg_diff(ldb, res->msgs[0], msg);
132 if (mod_msg->num_elements > 0) {
133 ret = ldb_modify(ldb, mod_msg);
137 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
138 /* We might be on a read-only DB */
141 if (ret != LDB_SUCCESS) {
142 talloc_free(mem_ctx);
146 /* Now write out the indexs, as found in the schema (if they have changed) */
148 ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE, NULL, "dn=%s", ldb_dn_get_linearized(msg_idx->dn));
149 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
150 ret = ldb_add(ldb, msg_idx);
151 } else if (ret != LDB_SUCCESS) {
152 } else if (res->count != 1) {
153 ret = ldb_add(ldb, msg_idx);
156 /* Annoyingly added to our search results */
157 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
159 mod_msg = ldb_msg_diff(ldb, res_idx->msgs[0], msg_idx);
160 if (mod_msg->num_elements > 0) {
161 ret = ldb_modify(ldb, mod_msg);
164 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
165 /* We might be on a read-only DB */
168 talloc_free(mem_ctx);
172 talloc_free(mem_ctx);
173 return LDB_ERR_OPERATIONS_ERROR;
176 static int dsdb_compare_class_by_lDAPDisplayName(struct dsdb_class **c1, struct dsdb_class **c2)
178 return strcasecmp((*c1)->lDAPDisplayName, (*c2)->lDAPDisplayName);
180 static int dsdb_compare_class_by_governsID_id(struct dsdb_class **c1, struct dsdb_class **c2)
182 return (*c1)->governsID_id - (*c2)->governsID_id;
184 static int dsdb_compare_class_by_governsID_oid(struct dsdb_class **c1, struct dsdb_class **c2)
186 return strcasecmp((*c1)->governsID_oid, (*c2)->governsID_oid);
188 static int dsdb_compare_class_by_cn(struct dsdb_class **c1, struct dsdb_class **c2)
190 return strcasecmp((*c1)->cn, (*c2)->cn);
193 static int dsdb_compare_attribute_by_lDAPDisplayName(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
195 return strcasecmp((*a1)->lDAPDisplayName, (*a2)->lDAPDisplayName);
197 static int dsdb_compare_attribute_by_attributeID_id(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
199 return (*a1)->attributeID_id - (*a2)->attributeID_id;
201 static int dsdb_compare_attribute_by_attributeID_oid(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
203 return strcasecmp((*a1)->attributeID_oid, (*a2)->attributeID_oid);
205 static int dsdb_compare_attribute_by_linkID(struct dsdb_attribute **a1, struct dsdb_attribute **a2)
207 return (*a1)->linkID - (*a2)->linkID;
211 create the sorted accessor arrays for the schema
213 static int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
214 struct dsdb_schema *schema)
216 struct dsdb_class *cur;
217 struct dsdb_attribute *a;
220 talloc_free(schema->classes_by_lDAPDisplayName);
221 talloc_free(schema->classes_by_governsID_id);
222 talloc_free(schema->classes_by_governsID_oid);
223 talloc_free(schema->classes_by_cn);
225 /* count the classes */
226 for (i=0, cur=schema->classes; cur; i++, cur=cur->next) /* noop */ ;
227 schema->num_classes = i;
229 /* setup classes_by_* */
230 schema->classes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_class *, i);
231 schema->classes_by_governsID_id = talloc_array(schema, struct dsdb_class *, i);
232 schema->classes_by_governsID_oid = talloc_array(schema, struct dsdb_class *, i);
233 schema->classes_by_cn = talloc_array(schema, struct dsdb_class *, i);
234 if (schema->classes_by_lDAPDisplayName == NULL ||
235 schema->classes_by_governsID_id == NULL ||
236 schema->classes_by_governsID_oid == NULL ||
237 schema->classes_by_cn == NULL) {
241 for (i=0, cur=schema->classes; cur; i++, cur=cur->next) {
242 schema->classes_by_lDAPDisplayName[i] = cur;
243 schema->classes_by_governsID_id[i] = cur;
244 schema->classes_by_governsID_oid[i] = cur;
245 schema->classes_by_cn[i] = cur;
248 /* sort the arrays */
249 qsort(schema->classes_by_lDAPDisplayName, schema->num_classes,
250 sizeof(struct dsdb_class *), QSORT_CAST dsdb_compare_class_by_lDAPDisplayName);
251 qsort(schema->classes_by_governsID_id, schema->num_classes,
252 sizeof(struct dsdb_class *), QSORT_CAST dsdb_compare_class_by_governsID_id);
253 qsort(schema->classes_by_governsID_oid, schema->num_classes,
254 sizeof(struct dsdb_class *), QSORT_CAST dsdb_compare_class_by_governsID_oid);
255 qsort(schema->classes_by_cn, schema->num_classes,
256 sizeof(struct dsdb_class *), QSORT_CAST dsdb_compare_class_by_cn);
258 /* now build the attribute accessor arrays */
259 talloc_free(schema->attributes_by_lDAPDisplayName);
260 talloc_free(schema->attributes_by_attributeID_id);
261 talloc_free(schema->attributes_by_attributeID_oid);
262 talloc_free(schema->attributes_by_linkID);
264 /* count the attributes */
265 for (i=0, a=schema->attributes; a; i++, a=a->next) /* noop */ ;
266 schema->num_attributes = i;
268 /* setup attributes_by_* */
269 schema->attributes_by_lDAPDisplayName = talloc_array(schema, struct dsdb_attribute *, i);
270 schema->attributes_by_attributeID_id = talloc_array(schema, struct dsdb_attribute *, i);
271 schema->attributes_by_attributeID_oid = talloc_array(schema, struct dsdb_attribute *, i);
272 schema->attributes_by_linkID = talloc_array(schema, struct dsdb_attribute *, i);
273 if (schema->attributes_by_lDAPDisplayName == NULL ||
274 schema->attributes_by_attributeID_id == NULL ||
275 schema->attributes_by_attributeID_oid == NULL ||
276 schema->attributes_by_linkID == NULL) {
280 for (i=0, a=schema->attributes; a; i++, a=a->next) {
281 schema->attributes_by_lDAPDisplayName[i] = a;
282 schema->attributes_by_attributeID_id[i] = a;
283 schema->attributes_by_attributeID_oid[i] = a;
284 schema->attributes_by_linkID[i] = a;
287 /* sort the arrays */
288 qsort(schema->attributes_by_lDAPDisplayName, schema->num_attributes,
289 sizeof(struct dsdb_attribute *), QSORT_CAST dsdb_compare_attribute_by_lDAPDisplayName);
290 qsort(schema->attributes_by_attributeID_id, schema->num_attributes,
291 sizeof(struct dsdb_attribute *), QSORT_CAST dsdb_compare_attribute_by_attributeID_id);
292 qsort(schema->attributes_by_attributeID_oid, schema->num_attributes,
293 sizeof(struct dsdb_attribute *), QSORT_CAST dsdb_compare_attribute_by_attributeID_oid);
294 qsort(schema->attributes_by_linkID, schema->num_attributes,
295 sizeof(struct dsdb_attribute *), QSORT_CAST dsdb_compare_attribute_by_linkID);
300 schema->classes_by_lDAPDisplayName = NULL;
301 schema->classes_by_governsID_id = NULL;
302 schema->classes_by_governsID_oid = NULL;
303 schema->classes_by_cn = NULL;
304 schema->attributes_by_lDAPDisplayName = NULL;
305 schema->attributes_by_attributeID_id = NULL;
306 schema->attributes_by_attributeID_oid = NULL;
307 schema->attributes_by_linkID = NULL;
309 return LDB_ERR_OPERATIONS_ERROR;
312 int dsdb_setup_schema_inversion(struct ldb_context *ldb, struct dsdb_schema *schema)
314 /* Walk the list of schema classes */
316 /* For each subClassOf, add us to subclasses of the parent */
318 /* collect these subclasses into a recursive list of total subclasses, preserving order */
320 /* For each subclass under 'top', write the index from it's
321 * order as an integer in the dsdb_class (for sorting
322 * objectClass lists efficiently) */
324 /* Walk the list of scheam classes */
326 /* Create a 'total possible superiors' on each class */
331 * Attach the schema to an opaque pointer on the ldb, so ldb modules
335 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
339 ret = dsdb_setup_sorted_accessors(ldb, schema);
340 if (ret != LDB_SUCCESS) {
344 schema_fill_constructed(schema);
346 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
347 if (ret != LDB_SUCCESS) {
351 /* Set the new attributes based on the new schema */
352 ret = dsdb_schema_set_attributes(ldb, schema, true);
353 if (ret != LDB_SUCCESS) {
357 talloc_steal(ldb, schema);
363 * Global variable to hold one copy of the schema, used to avoid memory bloat
365 static struct dsdb_schema *global_schema;
368 * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
370 int dsdb_set_global_schema(struct ldb_context *ldb)
373 if (!global_schema) {
377 ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
378 if (ret != LDB_SUCCESS) {
382 /* Set the new attributes based on the new schema */
383 ret = dsdb_schema_set_attributes(ldb, global_schema, false);
384 if (ret != LDB_SUCCESS) {
388 /* Keep a reference to this schema, just incase the global copy is replaced */
389 if (talloc_reference(ldb, global_schema) == NULL) {
390 return LDB_ERR_OPERATIONS_ERROR;
397 * Find the schema object for this ldb
400 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
403 struct dsdb_schema *schema;
405 /* see if we have a cached copy */
406 p = ldb_get_opaque(ldb, "dsdb_schema");
411 schema = talloc_get_type(p, struct dsdb_schema);
420 * Make the schema found on this ldb the 'global' schema
423 void dsdb_make_schema_global(struct ldb_context *ldb)
425 struct dsdb_schema *schema = dsdb_get_schema(ldb);
431 talloc_unlink(talloc_autofree_context(), schema);
434 talloc_steal(talloc_autofree_context(), schema);
435 global_schema = schema;
437 dsdb_set_global_schema(ldb);
442 * Rather than read a schema from the LDB itself, read it from an ldif
443 * file. This allows schema to be loaded and used while adding the
444 * schema itself to the directory.
447 WERROR dsdb_attach_schema_from_ldif(struct ldb_context *ldb, const char *pf, const char *df)
449 struct ldb_ldif *ldif;
450 struct ldb_message *msg;
454 struct dsdb_schema *schema;
455 const struct ldb_val *prefix_val;
456 const struct ldb_val *info_val;
457 struct ldb_val info_val_default;
459 mem_ctx = talloc_new(ldb);
464 schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
466 schema->fsmo.we_are_master = true;
467 schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
468 if (!schema->fsmo.master_dn) {
473 * load the prefixMap attribute from pf
475 ldif = ldb_ldif_read_string(ldb, &pf);
477 status = WERR_INVALID_PARAM;
480 talloc_steal(mem_ctx, ldif);
482 msg = ldb_msg_canonicalize(ldb, ldif->msg);
486 talloc_steal(mem_ctx, msg);
489 prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
491 status = WERR_INVALID_PARAM;
495 info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
497 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
498 if (!info_val_default.data) {
501 info_val = &info_val_default;
504 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
505 if (!W_ERROR_IS_OK(status)) {
510 * load the attribute and class definitions outof df
512 while ((ldif = ldb_ldif_read_string(ldb, &df))) {
516 talloc_steal(mem_ctx, ldif);
518 msg = ldb_msg_canonicalize(ldb, ldif->msg);
523 talloc_steal(mem_ctx, msg);
526 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
527 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
530 struct dsdb_attribute *sa;
532 sa = talloc_zero(schema, struct dsdb_attribute);
537 status = dsdb_attribute_from_ldb(ldb, schema, msg, sa, sa);
538 if (!W_ERROR_IS_OK(status)) {
542 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
544 struct dsdb_class *sc;
546 sc = talloc_zero(schema, struct dsdb_class);
551 status = dsdb_class_from_ldb(schema, msg, sc, sc);
552 if (!W_ERROR_IS_OK(status)) {
556 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
560 ret = dsdb_set_schema(ldb, schema);
561 if (ret != LDB_SUCCESS) {
562 status = WERR_FOOBAR;
572 talloc_free(mem_ctx);