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 "dsdb/samdb/samdb.h"
25 #include "dsdb/common/util.h"
26 #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 <ldb_module.h>
33 #include "../lib/util/asn1.h"
37 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx)
39 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
47 struct dsdb_schema *dsdb_schema_copy_shallow(TALLOC_CTX *mem_ctx,
48 struct ldb_context *ldb,
49 const struct dsdb_schema *schema)
52 struct dsdb_class *cls;
53 struct dsdb_attribute *attr;
54 struct dsdb_schema *schema_copy;
56 schema_copy = dsdb_new_schema(mem_ctx);
61 /* copy prexiMap & schemaInfo */
62 schema_copy->prefixmap = dsdb_schema_pfm_copy_shallow(schema_copy,
64 if (!schema_copy->prefixmap) {
68 schema_copy->schema_info = talloc(schema_copy, struct dsdb_schema_info);
69 if (!schema_copy->schema_info) {
72 *schema_copy->schema_info = *schema->schema_info;
74 /* copy classes and attributes*/
75 for (cls = schema->classes; cls; cls = cls->next) {
76 struct dsdb_class *class_copy = talloc_memdup(schema_copy,
81 DLIST_ADD(schema_copy->classes, class_copy);
83 schema_copy->num_classes = schema->num_classes;
85 for (attr = schema->attributes; attr; attr = attr->next) {
86 struct dsdb_attribute *a_copy = talloc_memdup(schema_copy,
91 DLIST_ADD(schema_copy->attributes, a_copy);
93 schema_copy->num_attributes = schema->num_attributes;
96 ret = dsdb_setup_sorted_accessors(ldb, schema_copy);
97 if (ret != LDB_SUCCESS) {
101 /* leave reload_seq_number = 0 so it will be refresh ASAP */
106 talloc_free(schema_copy);
111 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
112 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
115 struct dsdb_schema_info *schema_info = NULL;
116 struct dsdb_schema_prefixmap *pfm = NULL;
118 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
119 W_ERROR_NOT_OK_RETURN(werr);
121 /* set loaded prefixMap */
122 talloc_free(schema->prefixmap);
123 schema->prefixmap = pfm;
125 talloc_free(schema->schema_info);
126 schema->schema_info = schema_info;
131 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
133 struct dsdb_schema_prefixmap **_pfm)
136 enum ndr_err_code ndr_err;
137 struct prefixMapBlob pfm_blob;
139 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
140 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
142 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
144 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
145 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
146 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
147 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
148 (unsigned int)pfm_ldb_val->length, ndr_map_error2string(ndr_err)));
149 talloc_free(temp_ctx);
150 return ntstatus_to_werror(nt_status);
153 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
154 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob.version));
155 talloc_free(temp_ctx);
156 return WERR_VERSION_PARSE_ERROR;
159 /* call the drsuapi version */
160 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
161 if (!W_ERROR_IS_OK(werr)) {
162 DEBUG(0, (__location__ " dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr)));
163 talloc_free(temp_ctx);
167 talloc_free(temp_ctx);
172 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
173 const struct ldb_val *prefixMap,
174 const struct ldb_val *schemaInfo)
177 struct dsdb_schema_info *schema_info = NULL;
178 struct dsdb_schema_prefixmap *pfm = NULL;
181 /* verify schemaInfo blob is valid one */
182 if (!dsdb_schema_info_blob_is_valid(schemaInfo)) {
183 DEBUG(0,(__location__": dsdb_schema_info_blob_is_valid() failed.\n"));
184 return WERR_INVALID_PARAMETER;
187 mem_ctx = talloc_new(schema);
188 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
190 /* fetch prefixMap */
191 werr = _dsdb_prefixmap_from_ldb_val(prefixMap,
193 if (!W_ERROR_IS_OK(werr)) {
194 DEBUG(0, (__location__ " _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr)));
195 talloc_free(mem_ctx);
199 /* decode schema_info */
200 werr = dsdb_schema_info_from_blob(schemaInfo, mem_ctx, &schema_info);
201 if (!W_ERROR_IS_OK(werr)) {
202 DEBUG(0, (__location__ " dsdb_schema_info_from_blob failed: %s\n", win_errstr(werr)));
203 talloc_free(mem_ctx);
207 /* store prefixMap and schema_info into cached Schema */
208 talloc_free(schema->prefixmap);
209 schema->prefixmap = talloc_steal(schema, pfm);
211 talloc_free(schema->schema_info);
212 schema->schema_info = talloc_steal(schema, schema_info);
214 /* clean up locally allocated mem */
215 talloc_free(mem_ctx);
220 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
221 bool include_schema_info,
223 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
225 return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
226 include_schema_info ? schema->schema_info : NULL,
230 WERROR dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr,
232 struct ldb_val *prefixMap)
234 struct prefixMapBlob pfm;
235 enum ndr_err_code ndr_err;
236 pfm.version = PREFIX_MAP_VERSION_DSDB;
240 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, &pfm,
241 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
242 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
243 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
244 return ntstatus_to_werror(nt_status);
249 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
251 struct ldb_val *prefixMap,
252 struct ldb_val *schemaInfo)
255 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
257 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
258 W_ERROR_NOT_OK_RETURN(status);
260 status = dsdb_get_drsuapi_prefixmap_as_blob(ctr, mem_ctx, prefixMap);
262 W_ERROR_NOT_OK_RETURN(status);
264 status = dsdb_blob_from_schema_info(schema->schema_info, mem_ctx, schemaInfo);
265 W_ERROR_NOT_OK_RETURN(status);
272 * this function is called from within a ldb transaction from the schema_fsmo module
274 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
279 struct dsdb_schema_prefixmap *pfm;
280 struct dsdb_schema_prefixmap *orig_pfm = NULL;
282 mem_ctx = talloc_new(ldb);
283 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
285 /* Read prefixes from disk*/
286 status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
287 if (!W_ERROR_IS_OK(status)) {
288 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
289 win_errstr(status)));
290 talloc_free(mem_ctx);
294 /* Check if there is a prefix for the oid in the prefixes array*/
295 status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
296 if (W_ERROR_IS_OK(status)) {
298 talloc_free(mem_ctx);
300 } else if (!W_ERROR_EQUAL(status, WERR_NOT_FOUND)) {
302 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
303 win_errstr(status)));
304 talloc_free(mem_ctx);
308 /* Create the new mapping for the prefix of full_oid */
309 status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
310 if (!W_ERROR_IS_OK(status)) {
311 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
312 win_errstr(status)));
313 talloc_free(mem_ctx);
318 * We temporary replcate schema->prefixmap.
320 orig_pfm = schema->prefixmap;
321 schema->prefixmap = pfm;
323 /* Update prefixMap in ldb*/
324 status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
325 if (!W_ERROR_IS_OK(status)) {
326 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
327 win_errstr(status)));
328 talloc_free(mem_ctx);
332 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
333 full_oid, schema->prefixmap->length));
336 * We restore the original prefix map.
338 * The next schema reload should get an updated prefix map!
340 schema->prefixmap = orig_pfm;
342 talloc_free(mem_ctx);
347 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
348 const struct dsdb_schema *schema)
352 struct ldb_message *msg;
353 struct ldb_dn *schema_dn;
354 struct prefixMapBlob pfm_blob;
355 struct ldb_val ndr_blob;
356 enum ndr_err_code ndr_err;
357 TALLOC_CTX *temp_ctx;
358 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
360 schema_dn = ldb_get_schema_basedn(ldb);
362 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
366 temp_ctx = talloc_new(mem_ctx);
367 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
369 /* convert schema_prefixMap to prefixMap blob */
370 status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
371 if (!W_ERROR_IS_OK(status)) {
372 talloc_free(temp_ctx);
376 pfm_blob.version = PREFIX_MAP_VERSION_DSDB;
377 pfm_blob.ctr.dsdb = *ctr;
379 ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
381 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
382 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
383 talloc_free(temp_ctx);
387 /* write serialized prefixMap into LDB */
388 msg = ldb_msg_new(temp_ctx);
390 talloc_free(temp_ctx);
391 return WERR_NOT_ENOUGH_MEMORY;
395 ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
397 talloc_free(temp_ctx);
398 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
399 return WERR_NOT_ENOUGH_MEMORY;
402 ldb_ret = dsdb_replace(ldb, msg, DSDB_FLAG_AS_SYSTEM);
404 talloc_free(temp_ctx);
407 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
414 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
418 const struct ldb_val *prefix_val;
419 struct ldb_dn *schema_dn;
420 struct ldb_result *schema_res = NULL;
421 static const char *schema_attrs[] = {
426 schema_dn = ldb_get_schema_basedn(ldb);
428 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
432 ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
433 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
434 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
435 talloc_free(schema_res);
437 } else if (ldb_ret != LDB_SUCCESS) {
438 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
439 talloc_free(schema_res);
443 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
445 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
446 talloc_free(schema_res);
450 werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
453 talloc_free(schema_res);
454 W_ERROR_NOT_OK_RETURN(werr);
460 this will be replaced with something that looks at the right part of
461 the schema once we know where unique indexing information is hidden
463 static bool dsdb_schema_unique_attribute(const char *attr)
465 const char *attrs[] = { "objectGUID", NULL };
467 for (i=0;attrs[i];i++) {
468 if (ldb_attr_cmp(attr, attrs[i]) == 0) {
477 setup the ldb_schema_attribute field for a dsdb_attribute
479 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
480 struct dsdb_attribute *attr)
482 const char *syntax = attr->syntax->ldb_syntax;
483 const struct ldb_schema_syntax *s;
484 struct ldb_schema_attribute *a;
487 syntax = attr->syntax->ldap_oid;
490 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
492 s = ldb_samba_syntax_by_name(ldb, syntax);
495 s = ldb_standard_syntax_by_name(ldb, syntax);
499 return ldb_operr(ldb);
502 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
503 if (attr->ldb_schema_attribute == NULL) {
507 a->name = attr->lDAPDisplayName;
511 if (dsdb_schema_unique_attribute(a->name)) {
512 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
514 if (attr->isSingleValued) {
515 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
518 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX) {
519 a->flags |= LDB_ATTR_FLAG_INDEXED;
527 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
528 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
529 if (get_string_val == NULL) { \
531 d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
532 return WERR_INVALID_PARAMETER; \
537 (p)->elem = talloc_strndup(mem_ctx, \
538 (const char *)get_string_val->data, \
539 get_string_val->length); \
541 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
542 return WERR_NOT_ENOUGH_MEMORY; \
547 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do { \
548 int get_string_list_counter; \
549 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
550 /* We may get empty attributes over the replication channel */ \
551 if (get_string_list_el == NULL || get_string_list_el->num_values == 0) { \
555 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
556 for (get_string_list_counter=0; \
557 get_string_list_counter < get_string_list_el->num_values; \
558 get_string_list_counter++) { \
559 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
560 (const char *)get_string_list_el->values[get_string_list_counter].data, \
561 get_string_list_el->values[get_string_list_counter].length); \
562 if (!(p)->elem[get_string_list_counter]) { \
563 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
564 return WERR_NOT_ENOUGH_MEMORY; \
566 (p)->elem[get_string_list_counter+1] = NULL; \
568 talloc_steal(mem_ctx, (p)->elem); \
571 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
573 str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
576 d_printf("%s: %s == NULL\n", __location__, attr); \
577 return WERR_INVALID_PARAMETER; \
581 } else if (strcasecmp("TRUE", str) == 0) { \
583 } else if (strcasecmp("FALSE", str) == 0) { \
586 d_printf("%s: %s == %s\n", __location__, attr, str); \
587 return WERR_INVALID_PARAMETER; \
591 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
592 (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
595 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
596 uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
597 if (_v == UINT64_MAX) { \
599 } else if (_v > UINT32_MAX) { \
600 d_printf("%s: %s == 0x%llX\n", __location__, \
601 attr, (unsigned long long)_v); \
602 return WERR_INVALID_PARAMETER; \
604 (p)->elem = talloc(mem_ctx, uint32_t); \
606 d_printf("%s: talloc failed for %s\n", __location__, attr); \
607 return WERR_NOT_ENOUGH_MEMORY; \
609 *(p)->elem = (uint32_t)_v; \
613 #define GET_GUID_LDB(msg, attr, p, elem) do { \
614 (p)->elem = samdb_result_guid(msg, attr);\
617 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
618 const struct ldb_val *_val;\
619 _val = ldb_msg_find_ldb_val(msg, attr);\
622 talloc_steal(mem_ctx, (p)->elem.data);\
624 ZERO_STRUCT((p)->elem);\
628 /** Create an dsdb_attribute out of ldb message, attr must be already talloced
630 * If supplied the attribute will be checked against the prefixmap to
631 * ensure it can be mapped. However we can't have this attribute
632 * const as dsdb_schema_pfm_attid_from_oid calls
633 * dsdb_schema_pfm_make_attid_impl() which may modify prefixmap in
637 WERROR dsdb_attribute_from_ldb(struct dsdb_schema_prefixmap *prefixmap,
638 struct ldb_message *msg,
639 struct dsdb_attribute *attr)
643 DEBUG(0, ("%s: attr is null, it's expected not to be so\n", __location__));
644 return WERR_INVALID_PARAMETER;
647 GET_STRING_LDB(msg, "cn", attr, attr, cn, false);
650 * This allows for the fact that the CN attribute is not
651 * replicated over DRS, it is only replicated under the alias
654 if (attr->cn == NULL) {
655 GET_STRING_LDB(msg, "name", attr, attr, cn, true);
658 GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
659 GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
660 if (!prefixmap || prefixmap->length == 0) {
661 /* set an invalid value */
662 attr->attributeID_id = DRSUAPI_ATTID_INVALID;
664 status = dsdb_schema_pfm_attid_from_oid(prefixmap,
665 attr->attributeID_oid,
666 &attr->attributeID_id);
667 if (!W_ERROR_IS_OK(status)) {
668 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
669 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
670 win_errstr(status)));
674 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
675 GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
677 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
678 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
680 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
682 GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
684 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
685 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
686 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
687 GET_UINT32_LDB(msg, "linkID", attr, linkID);
689 GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
690 if (!prefixmap || prefixmap->length == 0) {
691 /* set an invalid value */
692 attr->attributeSyntax_id = DRSUAPI_ATTID_INVALID;
694 status = dsdb_schema_pfm_attid_from_oid(prefixmap,
695 attr->attributeSyntax_oid,
696 &attr->attributeSyntax_id);
697 if (!W_ERROR_IS_OK(status)) {
698 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
699 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
700 win_errstr(status)));
704 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
705 GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
707 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
708 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
709 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
710 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
712 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
713 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
715 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
716 GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
717 GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
718 GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
719 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
720 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
721 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
726 WERROR dsdb_set_attribute_from_ldb_dups(struct ldb_context *ldb,
727 struct dsdb_schema *schema,
728 struct ldb_message *msg,
732 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
734 return WERR_NOT_ENOUGH_MEMORY;
737 status = dsdb_attribute_from_ldb(schema->prefixmap, msg, attr);
738 if (!W_ERROR_IS_OK(status)) {
742 attr->syntax = dsdb_syntax_for_attribute(attr);
744 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
745 attr->lDAPDisplayName));
746 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
749 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
750 DEBUG(0,(__location__ ": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
751 attr->lDAPDisplayName,
752 attr->syntax->ldb_syntax,
753 attr->syntax->ldap_oid));
754 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
758 const struct dsdb_attribute *a2;
759 struct dsdb_attribute **a;
762 a2 = dsdb_attribute_by_attributeID_id(schema,
763 attr->attributeID_id);
768 i = schema->attributes_to_remove_size;
769 a = talloc_realloc(schema, schema->attributes_to_remove,
770 struct dsdb_attribute *, i + 1);
772 return WERR_NOT_ENOUGH_MEMORY;
774 /* Mark the old attribute as to be removed */
775 a[i] = discard_const_p(struct dsdb_attribute, a2);
776 schema->attributes_to_remove = a;
777 schema->attributes_to_remove_size++;
781 DLIST_ADD(schema->attributes, attr);
785 WERROR dsdb_set_attribute_from_ldb(struct ldb_context *ldb,
786 struct dsdb_schema *schema,
787 struct ldb_message *msg)
789 return dsdb_set_attribute_from_ldb_dups(ldb, schema, msg, false);
792 WERROR dsdb_set_class_from_ldb_dups(struct dsdb_schema *schema,
793 struct ldb_message *msg,
797 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
799 return WERR_NOT_ENOUGH_MEMORY;
801 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
804 * This allows for the fact that the CN attribute is not
805 * replicated over DRS, it is only replicated under the alias
808 if (obj->cn == NULL) {
809 GET_STRING_LDB(msg, "name", obj, obj, cn, true);
812 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
813 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
814 if (!schema->prefixmap || schema->prefixmap->length == 0) {
815 /* set an invalid value */
816 obj->governsID_id = DRSUAPI_ATTID_INVALID;
818 status = dsdb_schema_pfm_attid_from_oid(schema->prefixmap,
821 if (!W_ERROR_IS_OK(status)) {
822 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
823 __location__, obj->lDAPDisplayName, obj->governsID_oid,
824 win_errstr(status)));
828 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
829 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
831 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
832 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
833 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
835 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
837 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass);
838 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass);
840 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain);
841 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain);
842 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain);
843 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain);
845 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors);
846 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors);
848 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
850 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
851 GET_UINT32_LDB(msg, "systemFlags", obj, systemFlags);
852 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
854 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
855 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
856 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
857 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
858 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
859 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
860 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
863 const struct dsdb_class *c2;
864 struct dsdb_class **c;
867 c2 = dsdb_class_by_governsID_id(schema, obj->governsID_id);
872 i = schema->classes_to_remove_size;
873 c = talloc_realloc(schema, schema->classes_to_remove,
874 struct dsdb_class *, i + 1);
876 return WERR_NOT_ENOUGH_MEMORY;
878 /* Mark the old class to be removed */
879 c[i] = discard_const_p(struct dsdb_class, c2);
880 schema->classes_to_remove = c;
881 schema->classes_to_remove_size++;
885 DLIST_ADD(schema->classes, obj);
889 WERROR dsdb_set_class_from_ldb(struct dsdb_schema *schema,
890 struct ldb_message *msg)
892 return dsdb_set_class_from_ldb_dups(schema, msg, false);
895 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
898 Fill a DSDB schema from the ldb results provided. This is called
899 directly when a schema must be created with a pre-initialised prefixMap
902 int dsdb_load_ldb_results_into_schema(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
903 struct dsdb_schema *schema,
904 struct ldb_result *attrs_class_res,
910 schema->ts_last_change = 0;
911 for (i=0; i < attrs_class_res->count; i++) {
912 const char *prefixMap = NULL;
914 * attrs_class_res also includes the schema object;
915 * we only want to process classes & attributes
917 prefixMap = ldb_msg_find_attr_as_string(
918 attrs_class_res->msgs[i],
920 if (prefixMap != NULL) {
924 status = dsdb_schema_set_el_from_ldb_msg(ldb, schema,
925 attrs_class_res->msgs[i]);
926 if (!W_ERROR_IS_OK(status)) {
927 *error_string = talloc_asprintf(mem_ctx,
928 "dsdb_load_ldb_results_into_schema: failed to load attribute or class definition: %s:%s",
929 ldb_dn_get_linearized(attrs_class_res->msgs[i]->dn),
931 DEBUG(0,(__location__ ": %s\n", *error_string));
932 return LDB_ERR_CONSTRAINT_VIOLATION;
940 Create a DSDB schema from the ldb results provided. This is called
941 directly when the schema is provisioned from an on-disk LDIF file, or
942 from dsdb_schema_from_schema_dn in schema_fsmo
945 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
946 struct ldb_message *schema_msg,
947 struct ldb_result *attrs_class_res,
948 struct dsdb_schema **schema_out,
952 const struct ldb_val *prefix_val;
953 const struct ldb_val *info_val;
954 struct ldb_val info_val_default;
955 struct dsdb_schema *schema;
956 void *lp_opaque = ldb_get_opaque(ldb, "loadparm");
959 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
961 dsdb_oom(error_string, mem_ctx);
962 return ldb_operr(ldb);
965 schema = dsdb_new_schema(tmp_ctx);
967 dsdb_oom(error_string, mem_ctx);
968 talloc_free(tmp_ctx);
969 return ldb_operr(ldb);
973 struct loadparm_context *lp_ctx = talloc_get_type_abort(lp_opaque, struct loadparm_context);
974 schema->fsmo.update_allowed = lpcfg_parm_bool(lp_ctx, NULL,
975 "dsdb", "schema update allowed",
979 prefix_val = ldb_msg_find_ldb_val(schema_msg, "prefixMap");
981 *error_string = talloc_asprintf(mem_ctx,
982 "schema_fsmo_init: no prefixMap attribute found");
983 DEBUG(0,(__location__ ": %s\n", *error_string));
984 talloc_free(tmp_ctx);
985 return LDB_ERR_CONSTRAINT_VIOLATION;
987 info_val = ldb_msg_find_ldb_val(schema_msg, "schemaInfo");
989 status = dsdb_schema_info_blob_new(mem_ctx, &info_val_default);
990 if (!W_ERROR_IS_OK(status)) {
991 *error_string = talloc_asprintf(mem_ctx,
992 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
994 DEBUG(0,(__location__ ": %s\n", *error_string));
995 talloc_free(tmp_ctx);
996 return ldb_operr(ldb);
998 info_val = &info_val_default;
1001 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1002 if (!W_ERROR_IS_OK(status)) {
1003 *error_string = talloc_asprintf(mem_ctx,
1004 "schema_fsmo_init: failed to load oid mappings: %s",
1005 win_errstr(status));
1006 DEBUG(0,(__location__ ": %s\n", *error_string));
1007 talloc_free(tmp_ctx);
1008 return LDB_ERR_CONSTRAINT_VIOLATION;
1011 ret = dsdb_load_ldb_results_into_schema(mem_ctx, ldb, schema, attrs_class_res, error_string);
1012 if (ret != LDB_SUCCESS) {
1013 talloc_free(tmp_ctx);
1017 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_msg, "fSMORoleOwner");
1018 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb, tmp_ctx), schema->fsmo.master_dn) == 0) {
1019 schema->fsmo.we_are_master = true;
1021 schema->fsmo.we_are_master = false;
1024 DEBUG(5, ("schema_fsmo_init: we are master[%s] updates allowed[%s]\n",
1025 (schema->fsmo.we_are_master?"yes":"no"),
1026 (schema->fsmo.update_allowed?"yes":"no")));
1028 *schema_out = talloc_steal(mem_ctx, schema);
1029 talloc_free(tmp_ctx);