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 <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"
36 struct dsdb_schema
*dsdb_new_schema(TALLOC_CTX
*mem_ctx
)
38 struct dsdb_schema
*schema
= talloc_zero(mem_ctx
, struct dsdb_schema
);
42 schema
->refresh_interval
= 120;
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
);
62 schema_copy
->base_dn
= ldb_dn_copy(schema_copy
, schema
->base_dn
);
63 if (!schema_copy
->base_dn
) {
67 /* copy prexiMap & schemaInfo */
68 schema_copy
->prefixmap
= dsdb_schema_pfm_copy_shallow(schema_copy
,
70 if (!schema_copy
->prefixmap
) {
74 schema_copy
->schema_info
= talloc_strdup(schema_copy
, schema
->schema_info
);
76 /* copy classes and attributes*/
77 for (cls
= schema
->classes
; cls
; cls
= cls
->next
) {
78 struct dsdb_class
*class_copy
= talloc_memdup(schema_copy
,
83 DLIST_ADD(schema_copy
->classes
, class_copy
);
85 schema_copy
->num_classes
= schema
->num_classes
;
87 for (attr
= schema
->attributes
; attr
; attr
= attr
->next
) {
88 struct dsdb_attribute
*a_copy
= talloc_memdup(schema_copy
,
93 DLIST_ADD(schema_copy
->attributes
, a_copy
);
95 schema_copy
->num_attributes
= schema
->num_attributes
;
97 schema_copy
->refresh_interval
= schema
->refresh_interval
;
100 ret
= dsdb_setup_sorted_accessors(ldb
, schema_copy
);
101 if (ret
!= LDB_SUCCESS
) {
105 /* leave reload_seq_number = 0 so it will be refresh ASAP */
106 schema_copy
->refresh_fn
= schema
->refresh_fn
;
107 schema_copy
->loaded_from_module
= schema
->loaded_from_module
;
112 talloc_free(schema_copy
);
117 WERROR
dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema
*schema
,
118 const struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
)
121 const char *schema_info
;
122 struct dsdb_schema_prefixmap
*pfm
;
124 werr
= dsdb_schema_pfm_from_drsuapi_pfm(ctr
, true, schema
, &pfm
, &schema_info
);
125 W_ERROR_NOT_OK_RETURN(werr
);
127 /* set loaded prefixMap */
128 talloc_free(schema
->prefixmap
);
129 schema
->prefixmap
= pfm
;
131 talloc_free(discard_const(schema
->schema_info
));
132 schema
->schema_info
= schema_info
;
137 static WERROR
_dsdb_prefixmap_from_ldb_val(const struct ldb_val
*pfm_ldb_val
,
139 struct dsdb_schema_prefixmap
**_pfm
)
142 enum ndr_err_code ndr_err
;
143 struct prefixMapBlob pfm_blob
;
145 TALLOC_CTX
*temp_ctx
= talloc_new(mem_ctx
);
146 W_ERROR_HAVE_NO_MEMORY(temp_ctx
);
148 ndr_err
= ndr_pull_struct_blob(pfm_ldb_val
, temp_ctx
,
150 (ndr_pull_flags_fn_t
)ndr_pull_prefixMapBlob
);
151 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
152 NTSTATUS nt_status
= ndr_map_error2ntstatus(ndr_err
);
153 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
154 (unsigned int)pfm_ldb_val
->length
, ndr_map_error2string(ndr_err
)));
155 talloc_free(temp_ctx
);
156 return ntstatus_to_werror(nt_status
);
159 if (pfm_blob
.version
!= PREFIX_MAP_VERSION_DSDB
) {
160 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob
.version
));
161 talloc_free(temp_ctx
);
162 return WERR_VERSION_PARSE_ERROR
;
165 /* call the drsuapi version */
166 werr
= dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob
.ctr
.dsdb
, false, mem_ctx
, _pfm
, NULL
);
167 if (!W_ERROR_IS_OK(werr
)) {
168 DEBUG(0, (__location__
" dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr
)));
169 talloc_free(temp_ctx
);
173 talloc_free(temp_ctx
);
178 WERROR
dsdb_load_oid_mappings_ldb(struct dsdb_schema
*schema
,
179 const struct ldb_val
*prefixMap
,
180 const struct ldb_val
*schemaInfo
)
183 const char *schema_info
;
184 struct dsdb_schema_prefixmap
*pfm
;
187 /* verify schemaInfo blob is valid one */
188 if (!dsdb_schema_info_blob_is_valid(schemaInfo
)) {
189 DEBUG(0,(__location__
": dsdb_schema_info_blob_is_valid() failed.\n"));
190 return WERR_INVALID_PARAMETER
;
193 mem_ctx
= talloc_new(schema
);
194 W_ERROR_HAVE_NO_MEMORY(mem_ctx
);
196 /* fetch prefixMap */
197 werr
= _dsdb_prefixmap_from_ldb_val(prefixMap
,
199 if (!W_ERROR_IS_OK(werr
)) {
200 DEBUG(0, (__location__
" _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr
)));
201 talloc_free(mem_ctx
);
205 /* decode schema_info */
206 schema_info
= hex_encode_talloc(mem_ctx
,
210 talloc_free(mem_ctx
);
214 /* store prefixMap and schema_info into cached Schema */
215 talloc_free(schema
->prefixmap
);
216 schema
->prefixmap
= talloc_steal(schema
, pfm
);
218 talloc_free(discard_const(schema
->schema_info
));
219 schema
->schema_info
= talloc_steal(schema
, schema_info
);
221 /* clean up locally allocated mem */
222 talloc_free(mem_ctx
);
227 WERROR
dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema
*schema
,
228 bool include_schema_info
,
230 struct drsuapi_DsReplicaOIDMapping_Ctr
**_ctr
)
232 return dsdb_drsuapi_pfm_from_schema_pfm(schema
->prefixmap
,
233 include_schema_info
? schema
->schema_info
: NULL
,
237 WERROR
dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
,
239 struct ldb_val
*prefixMap
)
241 struct prefixMapBlob pfm
;
242 enum ndr_err_code ndr_err
;
243 pfm
.version
= PREFIX_MAP_VERSION_DSDB
;
247 ndr_err
= ndr_push_struct_blob(prefixMap
, mem_ctx
, &pfm
,
248 (ndr_push_flags_fn_t
)ndr_push_prefixMapBlob
);
249 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
250 NTSTATUS nt_status
= ndr_map_error2ntstatus(ndr_err
);
251 return ntstatus_to_werror(nt_status
);
256 WERROR
dsdb_get_oid_mappings_ldb(const struct dsdb_schema
*schema
,
258 struct ldb_val
*prefixMap
,
259 struct ldb_val
*schemaInfo
)
262 struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
;
264 status
= dsdb_get_oid_mappings_drsuapi(schema
, false, mem_ctx
, &ctr
);
265 W_ERROR_NOT_OK_RETURN(status
);
267 status
= dsdb_get_drsuapi_prefixmap_as_blob(ctr
, mem_ctx
, prefixMap
);
269 W_ERROR_NOT_OK_RETURN(status
);
271 *schemaInfo
= strhex_to_data_blob(mem_ctx
, schema
->schema_info
);
272 W_ERROR_HAVE_NO_MEMORY(schemaInfo
->data
);
279 * this function is called from within a ldb transaction from the schema_fsmo module
281 WERROR
dsdb_create_prefix_mapping(struct ldb_context
*ldb
, struct dsdb_schema
*schema
, const char *full_oid
)
286 struct dsdb_schema_prefixmap
*pfm
;
288 mem_ctx
= talloc_new(ldb
);
289 W_ERROR_HAVE_NO_MEMORY(mem_ctx
);
291 /* Read prefixes from disk*/
292 status
= dsdb_read_prefixes_from_ldb(ldb
, mem_ctx
, &pfm
);
293 if (!W_ERROR_IS_OK(status
)) {
294 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
295 win_errstr(status
)));
296 talloc_free(mem_ctx
);
300 /* Check if there is a prefix for the oid in the prefixes array*/
301 status
= dsdb_schema_pfm_find_oid(pfm
, full_oid
, NULL
);
302 if (W_ERROR_IS_OK(status
)) {
304 talloc_free(mem_ctx
);
306 } else if (!W_ERROR_EQUAL(status
, WERR_NOT_FOUND
)) {
308 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
309 win_errstr(status
)));
310 talloc_free(mem_ctx
);
314 /* Create the new mapping for the prefix of full_oid */
315 status
= dsdb_schema_pfm_make_attid(pfm
, full_oid
, &attid
);
316 if (!W_ERROR_IS_OK(status
)) {
317 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
318 win_errstr(status
)));
319 talloc_free(mem_ctx
);
323 talloc_unlink(schema
, schema
->prefixmap
);
324 schema
->prefixmap
= talloc_steal(schema
, pfm
);
326 /* Update prefixMap in ldb*/
327 status
= dsdb_write_prefixes_from_schema_to_ldb(mem_ctx
, ldb
, schema
);
328 if (!W_ERROR_IS_OK(status
)) {
329 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
330 win_errstr(status
)));
331 talloc_free(mem_ctx
);
335 DEBUG(2,(__location__
" Added prefixMap %s - now have %u prefixes\n",
336 full_oid
, schema
->prefixmap
->length
));
338 talloc_free(mem_ctx
);
343 WERROR
dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX
*mem_ctx
, struct ldb_context
*ldb
,
344 const struct dsdb_schema
*schema
)
348 struct ldb_message
*msg
;
349 struct ldb_dn
*schema_dn
;
350 struct prefixMapBlob pfm_blob
;
351 struct ldb_val ndr_blob
;
352 enum ndr_err_code ndr_err
;
353 TALLOC_CTX
*temp_ctx
;
354 struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
;
356 schema_dn
= ldb_get_schema_basedn(ldb
);
358 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
362 temp_ctx
= talloc_new(mem_ctx
);
363 W_ERROR_HAVE_NO_MEMORY(temp_ctx
);
365 /* convert schema_prefixMap to prefixMap blob */
366 status
= dsdb_get_oid_mappings_drsuapi(schema
, false, temp_ctx
, &ctr
);
367 if (!W_ERROR_IS_OK(status
)) {
368 talloc_free(temp_ctx
);
372 pfm_blob
.version
= PREFIX_MAP_VERSION_DSDB
;
373 pfm_blob
.ctr
.dsdb
= *ctr
;
375 ndr_err
= ndr_push_struct_blob(&ndr_blob
, temp_ctx
,
377 (ndr_push_flags_fn_t
)ndr_push_prefixMapBlob
);
378 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
379 talloc_free(temp_ctx
);
383 /* write serialized prefixMap into LDB */
384 msg
= ldb_msg_new(temp_ctx
);
386 talloc_free(temp_ctx
);
391 ldb_ret
= ldb_msg_add_value(msg
, "prefixMap", &ndr_blob
, NULL
);
393 talloc_free(temp_ctx
);
394 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
398 ldb_ret
= dsdb_replace(ldb
, msg
, DSDB_FLAG_AS_SYSTEM
);
400 talloc_free(temp_ctx
);
403 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
410 WERROR
dsdb_read_prefixes_from_ldb(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct dsdb_schema_prefixmap
**_pfm
)
414 const struct ldb_val
*prefix_val
;
415 struct ldb_dn
*schema_dn
;
416 struct ldb_result
*schema_res
= NULL
;
417 static const char *schema_attrs
[] = {
422 schema_dn
= ldb_get_schema_basedn(ldb
);
424 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
428 ldb_ret
= ldb_search(ldb
, mem_ctx
, &schema_res
, schema_dn
, LDB_SCOPE_BASE
, schema_attrs
, NULL
);
429 if (ldb_ret
== LDB_ERR_NO_SUCH_OBJECT
) {
430 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
431 talloc_free(schema_res
);
433 } else if (ldb_ret
!= LDB_SUCCESS
) {
434 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
435 talloc_free(schema_res
);
439 prefix_val
= ldb_msg_find_ldb_val(schema_res
->msgs
[0], "prefixMap");
441 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
442 talloc_free(schema_res
);
446 werr
= _dsdb_prefixmap_from_ldb_val(prefix_val
,
449 talloc_free(schema_res
);
450 W_ERROR_NOT_OK_RETURN(werr
);
456 this will be replaced with something that looks at the right part of
457 the schema once we know where unique indexing information is hidden
459 static bool dsdb_schema_unique_attribute(const char *attr
)
461 const char *attrs
[] = { "objectGUID", "objectSid" , NULL
};
463 for (i
=0;attrs
[i
];i
++) {
464 if (ldb_attr_cmp(attr
, attrs
[i
]) == 0) {
473 setup the ldb_schema_attribute field for a dsdb_attribute
475 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context
*ldb
,
476 struct dsdb_attribute
*attr
)
478 const char *syntax
= attr
->syntax
->ldb_syntax
;
479 const struct ldb_schema_syntax
*s
;
480 struct ldb_schema_attribute
*a
;
483 syntax
= attr
->syntax
->ldap_oid
;
486 s
= ldb_samba_syntax_by_lDAPDisplayName(ldb
, attr
->lDAPDisplayName
);
488 s
= ldb_samba_syntax_by_name(ldb
, syntax
);
491 s
= ldb_standard_syntax_by_name(ldb
, syntax
);
495 return ldb_operr(ldb
);
498 attr
->ldb_schema_attribute
= a
= talloc(attr
, struct ldb_schema_attribute
);
499 if (attr
->ldb_schema_attribute
== NULL
) {
503 a
->name
= attr
->lDAPDisplayName
;
507 if (dsdb_schema_unique_attribute(a
->name
)) {
508 a
->flags
|= LDB_ATTR_FLAG_UNIQUE_INDEX
;
510 if (attr
->isSingleValued
) {
511 a
->flags
|= LDB_ATTR_FLAG_SINGLE_VALUE
;
519 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
520 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
521 if (get_string_val == NULL) { \
523 d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
524 return WERR_INVALID_PARAM; \
529 (p)->elem = talloc_strndup(mem_ctx, \
530 (const char *)get_string_val->data, \
531 get_string_val->length); \
533 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
539 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do { \
540 int get_string_list_counter; \
541 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
542 /* We may get empty attributes over the replication channel */ \
543 if (get_string_list_el == NULL || get_string_list_el->num_values == 0) { \
547 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
548 for (get_string_list_counter=0; \
549 get_string_list_counter < get_string_list_el->num_values; \
550 get_string_list_counter++) { \
551 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
552 (const char *)get_string_list_el->values[get_string_list_counter].data, \
553 get_string_list_el->values[get_string_list_counter].length); \
554 if (!(p)->elem[get_string_list_counter]) { \
555 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
558 (p)->elem[get_string_list_counter+1] = NULL; \
560 talloc_steal(mem_ctx, (p)->elem); \
563 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
565 str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
568 d_printf("%s: %s == NULL\n", __location__, attr); \
569 return WERR_INVALID_PARAM; \
573 } else if (strcasecmp("TRUE", str) == 0) { \
575 } else if (strcasecmp("FALSE", str) == 0) { \
578 d_printf("%s: %s == %s\n", __location__, attr, str); \
579 return WERR_INVALID_PARAM; \
583 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
584 (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
587 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
588 uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
589 if (_v == UINT64_MAX) { \
591 } else if (_v > UINT32_MAX) { \
592 d_printf("%s: %s == 0x%llX\n", __location__, \
593 attr, (unsigned long long)_v); \
594 return WERR_INVALID_PARAM; \
596 (p)->elem = talloc(mem_ctx, uint32_t); \
598 d_printf("%s: talloc failed for %s\n", __location__, attr); \
601 *(p)->elem = (uint32_t)_v; \
605 #define GET_GUID_LDB(msg, attr, p, elem) do { \
606 (p)->elem = samdb_result_guid(msg, attr);\
609 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
610 const struct ldb_val *_val;\
611 _val = ldb_msg_find_ldb_val(msg, attr);\
614 talloc_steal(mem_ctx, (p)->elem.data);\
616 ZERO_STRUCT((p)->elem);\
620 /** Create an dsdb_attribute out of ldb message, attr must be already talloced
623 WERROR
dsdb_attribute_from_ldb(const struct dsdb_schema
*schema
,
624 struct ldb_message
*msg
,
625 struct dsdb_attribute
*attr
)
629 DEBUG(0, ("%s: attr is null, it's expected not to be so\n", __location__
));
630 return WERR_INVALID_PARAM
;
633 GET_STRING_LDB(msg
, "cn", attr
, attr
, cn
, false);
634 GET_STRING_LDB(msg
, "lDAPDisplayName", attr
, attr
, lDAPDisplayName
, true);
635 GET_STRING_LDB(msg
, "attributeID", attr
, attr
, attributeID_oid
, true);
636 if (!schema
->prefixmap
|| schema
->prefixmap
->length
== 0) {
637 /* set an invalid value */
638 attr
->attributeID_id
= DRSUAPI_ATTID_INVALID
;
640 status
= dsdb_schema_pfm_make_attid(schema
->prefixmap
,
641 attr
->attributeID_oid
,
642 &attr
->attributeID_id
);
643 if (!W_ERROR_IS_OK(status
)) {
644 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
645 __location__
, attr
->lDAPDisplayName
, attr
->attributeID_oid
,
646 win_errstr(status
)));
650 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
651 GET_UINT32_LDB(msg
, "msDS-IntId", attr
, msDS_IntId
);
653 GET_GUID_LDB(msg
, "schemaIDGUID", attr
, schemaIDGUID
);
654 GET_UINT32_LDB(msg
, "mAPIID", attr
, mAPIID
);
656 GET_GUID_LDB(msg
, "attributeSecurityGUID", attr
, attributeSecurityGUID
);
658 GET_GUID_LDB(msg
, "objectGUID", attr
, objectGUID
);
660 GET_UINT32_LDB(msg
, "searchFlags", attr
, searchFlags
);
661 GET_UINT32_LDB(msg
, "systemFlags", attr
, systemFlags
);
662 GET_BOOL_LDB(msg
, "isMemberOfPartialAttributeSet", attr
, isMemberOfPartialAttributeSet
, false);
663 GET_UINT32_LDB(msg
, "linkID", attr
, linkID
);
665 GET_STRING_LDB(msg
, "attributeSyntax", attr
, attr
, attributeSyntax_oid
, true);
666 if (!schema
->prefixmap
|| schema
->prefixmap
->length
== 0) {
667 /* set an invalid value */
668 attr
->attributeSyntax_id
= DRSUAPI_ATTID_INVALID
;
670 status
= dsdb_schema_pfm_attid_from_oid(schema
->prefixmap
,
671 attr
->attributeSyntax_oid
,
672 &attr
->attributeSyntax_id
);
673 if (!W_ERROR_IS_OK(status
)) {
674 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
675 __location__
, attr
->lDAPDisplayName
, attr
->attributeSyntax_oid
,
676 win_errstr(status
)));
680 GET_UINT32_LDB(msg
, "oMSyntax", attr
, oMSyntax
);
681 GET_BLOB_LDB(msg
, "oMObjectClass", attr
, attr
, oMObjectClass
);
683 GET_BOOL_LDB(msg
, "isSingleValued", attr
, isSingleValued
, true);
684 GET_UINT32_PTR_LDB(msg
, "rangeLower", attr
, attr
, rangeLower
);
685 GET_UINT32_PTR_LDB(msg
, "rangeUpper", attr
, attr
, rangeUpper
);
686 GET_BOOL_LDB(msg
, "extendedCharsAllowed", attr
, extendedCharsAllowed
, false);
688 GET_UINT32_LDB(msg
, "schemaFlagsEx", attr
, schemaFlagsEx
);
689 GET_BLOB_LDB(msg
, "msDs-Schema-Extensions", attr
, attr
, msDs_Schema_Extensions
);
691 GET_BOOL_LDB(msg
, "showInAdvancedViewOnly", attr
, showInAdvancedViewOnly
, false);
692 GET_STRING_LDB(msg
, "adminDisplayName", attr
, attr
, adminDisplayName
, false);
693 GET_STRING_LDB(msg
, "adminDescription", attr
, attr
, adminDescription
, false);
694 GET_STRING_LDB(msg
, "classDisplayName", attr
, attr
, classDisplayName
, false);
695 GET_BOOL_LDB(msg
, "isEphemeral", attr
, isEphemeral
, false);
696 GET_BOOL_LDB(msg
, "isDefunct", attr
, isDefunct
, false);
697 GET_BOOL_LDB(msg
, "systemOnly", attr
, systemOnly
, false);
702 WERROR
dsdb_set_attribute_from_ldb(struct ldb_context
*ldb
,
703 struct dsdb_schema
*schema
,
704 struct ldb_message
*msg
)
707 struct dsdb_attribute
*attr
= talloc_zero(schema
, struct dsdb_attribute
);
712 status
= dsdb_attribute_from_ldb(schema
, msg
, attr
);
713 if (!W_ERROR_IS_OK(status
)) {
717 attr
->syntax
= dsdb_syntax_for_attribute(attr
);
719 DEBUG(0,(__location__
": Unknown schema syntax for %s\n",
720 attr
->lDAPDisplayName
));
721 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX
;
724 if (dsdb_schema_setup_ldb_schema_attribute(ldb
, attr
) != LDB_SUCCESS
) {
725 DEBUG(0,(__location__
": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
726 attr
->lDAPDisplayName
,
727 attr
->syntax
->ldb_syntax
,
728 attr
->syntax
->ldap_oid
));
729 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX
;
732 DLIST_ADD(schema
->attributes
, attr
);
736 WERROR
dsdb_set_class_from_ldb(struct dsdb_schema
*schema
,
737 struct ldb_message
*msg
)
740 struct dsdb_class
*obj
= talloc_zero(schema
, struct dsdb_class
);
744 GET_STRING_LDB(msg
, "cn", obj
, obj
, cn
, false);
745 GET_STRING_LDB(msg
, "lDAPDisplayName", obj
, obj
, lDAPDisplayName
, true);
746 GET_STRING_LDB(msg
, "governsID", obj
, obj
, governsID_oid
, true);
747 if (!schema
->prefixmap
|| schema
->prefixmap
->length
== 0) {
748 /* set an invalid value */
749 obj
->governsID_id
= DRSUAPI_ATTID_INVALID
;
751 status
= dsdb_schema_pfm_make_attid(schema
->prefixmap
,
754 if (!W_ERROR_IS_OK(status
)) {
755 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
756 __location__
, obj
->lDAPDisplayName
, obj
->governsID_oid
,
757 win_errstr(status
)));
761 GET_GUID_LDB(msg
, "schemaIDGUID", obj
, schemaIDGUID
);
762 GET_GUID_LDB(msg
, "objectGUID", obj
, objectGUID
);
764 GET_UINT32_LDB(msg
, "objectClassCategory", obj
, objectClassCategory
);
765 GET_STRING_LDB(msg
, "rDNAttID", obj
, obj
, rDNAttID
, false);
766 GET_STRING_LDB(msg
, "defaultObjectCategory", obj
, obj
, defaultObjectCategory
, true);
768 GET_STRING_LDB(msg
, "subClassOf", obj
, obj
, subClassOf
, true);
770 GET_STRING_LIST_LDB(msg
, "systemAuxiliaryClass", obj
, obj
, systemAuxiliaryClass
);
771 GET_STRING_LIST_LDB(msg
, "auxiliaryClass", obj
, obj
, auxiliaryClass
);
773 GET_STRING_LIST_LDB(msg
, "systemMustContain", obj
, obj
, systemMustContain
);
774 GET_STRING_LIST_LDB(msg
, "systemMayContain", obj
, obj
, systemMayContain
);
775 GET_STRING_LIST_LDB(msg
, "mustContain", obj
, obj
, mustContain
);
776 GET_STRING_LIST_LDB(msg
, "mayContain", obj
, obj
, mayContain
);
778 GET_STRING_LIST_LDB(msg
, "systemPossSuperiors", obj
, obj
, systemPossSuperiors
);
779 GET_STRING_LIST_LDB(msg
, "possSuperiors", obj
, obj
, possSuperiors
);
781 GET_STRING_LDB(msg
, "defaultSecurityDescriptor", obj
, obj
, defaultSecurityDescriptor
, false);
783 GET_UINT32_LDB(msg
, "schemaFlagsEx", obj
, schemaFlagsEx
);
784 GET_UINT32_LDB(msg
, "systemFlags", obj
, systemFlags
);
785 GET_BLOB_LDB(msg
, "msDs-Schema-Extensions", obj
, obj
, msDs_Schema_Extensions
);
787 GET_BOOL_LDB(msg
, "showInAdvancedViewOnly", obj
, showInAdvancedViewOnly
, false);
788 GET_STRING_LDB(msg
, "adminDisplayName", obj
, obj
, adminDisplayName
, false);
789 GET_STRING_LDB(msg
, "adminDescription", obj
, obj
, adminDescription
, false);
790 GET_STRING_LDB(msg
, "classDisplayName", obj
, obj
, classDisplayName
, false);
791 GET_BOOL_LDB(msg
, "defaultHidingValue", obj
, defaultHidingValue
, false);
792 GET_BOOL_LDB(msg
, "isDefunct", obj
, isDefunct
, false);
793 GET_BOOL_LDB(msg
, "systemOnly", obj
, systemOnly
, false);
795 DLIST_ADD(schema
->classes
, obj
);
799 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
802 Fill a DSDB schema from the ldb results provided. This is called
803 directly when a schema must be created with a pre-initialised prefixMap
806 int dsdb_load_ldb_results_into_schema(TALLOC_CTX
*mem_ctx
, struct ldb_context
*ldb
,
807 struct dsdb_schema
*schema
,
808 struct ldb_result
*attrs_class_res
,
813 schema
->ts_last_change
= 0;
814 for (i
=0; i
< attrs_class_res
->count
; i
++) {
815 WERROR status
= dsdb_schema_set_el_from_ldb_msg(ldb
, schema
, attrs_class_res
->msgs
[i
]);
816 if (!W_ERROR_IS_OK(status
)) {
817 *error_string
= talloc_asprintf(mem_ctx
,
818 "dsdb_load_ldb_results_into_schema: failed to load attribute or class definition: %s:%s",
819 ldb_dn_get_linearized(attrs_class_res
->msgs
[i
]->dn
),
821 DEBUG(0,(__location__
": %s\n", *error_string
));
822 return LDB_ERR_CONSTRAINT_VIOLATION
;
830 Create a DSDB schema from the ldb results provided. This is called
831 directly when the schema is provisioned from an on-disk LDIF file, or
832 from dsdb_schema_from_schema_dn in schema_fsmo
835 int dsdb_schema_from_ldb_results(TALLOC_CTX
*mem_ctx
, struct ldb_context
*ldb
,
836 struct ldb_result
*schema_res
,
837 struct ldb_result
*attrs_class_res
,
838 struct dsdb_schema
**schema_out
,
842 const struct ldb_val
*prefix_val
;
843 const struct ldb_val
*info_val
;
844 struct ldb_val info_val_default
;
845 struct dsdb_schema
*schema
;
846 void *lp_opaque
= ldb_get_opaque(ldb
, "loadparm");
849 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
851 dsdb_oom(error_string
, mem_ctx
);
852 return ldb_operr(ldb
);
855 schema
= dsdb_new_schema(tmp_ctx
);
857 dsdb_oom(error_string
, mem_ctx
);
858 talloc_free(tmp_ctx
);
859 return ldb_operr(ldb
);
863 struct loadparm_context
*lp_ctx
= talloc_get_type_abort(lp_opaque
, struct loadparm_context
);
864 schema
->refresh_interval
= lpcfg_parm_int(lp_ctx
, NULL
, "dsdb", "schema_reload_interval", schema
->refresh_interval
);
865 lp_ctx
= talloc_get_type(ldb_get_opaque(ldb
, "loadparm"),
866 struct loadparm_context
);
867 schema
->fsmo
.update_allowed
= lpcfg_parm_bool(lp_ctx
, NULL
,
868 "dsdb", "schema update allowed",
872 schema
->base_dn
= talloc_steal(schema
, schema_res
->msgs
[0]->dn
);
874 prefix_val
= ldb_msg_find_ldb_val(schema_res
->msgs
[0], "prefixMap");
876 *error_string
= talloc_asprintf(mem_ctx
,
877 "schema_fsmo_init: no prefixMap attribute found");
878 DEBUG(0,(__location__
": %s\n", *error_string
));
879 talloc_free(tmp_ctx
);
880 return LDB_ERR_CONSTRAINT_VIOLATION
;
882 info_val
= ldb_msg_find_ldb_val(schema_res
->msgs
[0], "schemaInfo");
884 status
= dsdb_schema_info_blob_new(mem_ctx
, &info_val_default
);
885 if (!W_ERROR_IS_OK(status
)) {
886 *error_string
= talloc_asprintf(mem_ctx
,
887 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
889 DEBUG(0,(__location__
": %s\n", *error_string
));
890 talloc_free(tmp_ctx
);
891 return ldb_operr(ldb
);
893 info_val
= &info_val_default
;
896 status
= dsdb_load_oid_mappings_ldb(schema
, prefix_val
, info_val
);
897 if (!W_ERROR_IS_OK(status
)) {
898 *error_string
= talloc_asprintf(mem_ctx
,
899 "schema_fsmo_init: failed to load oid mappings: %s",
901 DEBUG(0,(__location__
": %s\n", *error_string
));
902 talloc_free(tmp_ctx
);
903 return LDB_ERR_CONSTRAINT_VIOLATION
;
906 ret
= dsdb_load_ldb_results_into_schema(mem_ctx
, ldb
, schema
, attrs_class_res
, error_string
);
907 if (ret
!= LDB_SUCCESS
) {
908 talloc_free(tmp_ctx
);
912 schema
->fsmo
.master_dn
= ldb_msg_find_attr_as_dn(ldb
, schema
, schema_res
->msgs
[0], "fSMORoleOwner");
913 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb
, tmp_ctx
), schema
->fsmo
.master_dn
) == 0) {
914 schema
->fsmo
.we_are_master
= true;
916 schema
->fsmo
.we_are_master
= false;
919 DEBUG(5, ("schema_fsmo_init: we are master[%s] updates allowed[%s]\n",
920 (schema
->fsmo
.we_are_master
?"yes":"no"),
921 (schema
->fsmo
.update_allowed
?"yes":"no")));
923 *schema_out
= talloc_steal(mem_ctx
, schema
);
924 talloc_free(tmp_ctx
);