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
);
46 struct dsdb_schema
*dsdb_schema_copy_shallow(TALLOC_CTX
*mem_ctx
,
47 struct ldb_context
*ldb
,
48 const struct dsdb_schema
*schema
)
51 struct dsdb_class
*cls
;
52 struct dsdb_attribute
*attr
;
53 struct dsdb_schema
*schema_copy
;
55 schema_copy
= dsdb_new_schema(mem_ctx
);
61 schema_copy
->base_dn
= ldb_dn_copy(schema_copy
, schema
->base_dn
);
62 if (!schema_copy
->base_dn
) {
66 /* copy prexiMap & schemaInfo */
67 schema_copy
->prefixmap
= dsdb_schema_pfm_copy_shallow(schema_copy
,
69 if (!schema_copy
->prefixmap
) {
73 schema_copy
->schema_info
= talloc_strdup(schema_copy
, schema
->schema_info
);
75 /* copy classes and attributes*/
76 for (cls
= schema
->classes
; cls
; cls
= cls
->next
) {
77 struct dsdb_class
*class_copy
= talloc_memdup(schema_copy
,
82 DLIST_ADD(schema_copy
->classes
, class_copy
);
84 schema_copy
->num_classes
= schema
->num_classes
;
86 for (attr
= schema
->attributes
; attr
; attr
= attr
->next
) {
87 struct dsdb_attribute
*a_copy
= talloc_memdup(schema_copy
,
92 DLIST_ADD(schema_copy
->attributes
, a_copy
);
94 schema_copy
->num_attributes
= schema
->num_attributes
;
97 ret
= dsdb_setup_sorted_accessors(ldb
, schema_copy
);
98 if (ret
!= LDB_SUCCESS
) {
102 /* leave reload_seq_number = 0 so it will be refresh ASAP */
103 schema_copy
->refresh_fn
= schema
->refresh_fn
;
104 schema_copy
->loaded_from_module
= schema
->loaded_from_module
;
109 talloc_free(schema_copy
);
114 WERROR
dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema
*schema
,
115 const struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
)
118 const char *schema_info
;
119 struct dsdb_schema_prefixmap
*pfm
;
121 werr
= dsdb_schema_pfm_from_drsuapi_pfm(ctr
, true, schema
, &pfm
, &schema_info
);
122 W_ERROR_NOT_OK_RETURN(werr
);
124 /* set loaded prefixMap */
125 talloc_free(schema
->prefixmap
);
126 schema
->prefixmap
= pfm
;
128 talloc_free(discard_const(schema
->schema_info
));
129 schema
->schema_info
= schema_info
;
134 static WERROR
_dsdb_prefixmap_from_ldb_val(const struct ldb_val
*pfm_ldb_val
,
136 struct dsdb_schema_prefixmap
**_pfm
)
139 enum ndr_err_code ndr_err
;
140 struct prefixMapBlob pfm_blob
;
142 TALLOC_CTX
*temp_ctx
= talloc_new(mem_ctx
);
143 W_ERROR_HAVE_NO_MEMORY(temp_ctx
);
145 ndr_err
= ndr_pull_struct_blob(pfm_ldb_val
, temp_ctx
,
147 (ndr_pull_flags_fn_t
)ndr_pull_prefixMapBlob
);
148 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
149 NTSTATUS nt_status
= ndr_map_error2ntstatus(ndr_err
);
150 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: Failed to parse prefixmap of length %u: %s\n",
151 (unsigned int)pfm_ldb_val
->length
, ndr_map_error2string(ndr_err
)));
152 talloc_free(temp_ctx
);
153 return ntstatus_to_werror(nt_status
);
156 if (pfm_blob
.version
!= PREFIX_MAP_VERSION_DSDB
) {
157 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version %u incorrect\n", (unsigned int)pfm_blob
.version
));
158 talloc_free(temp_ctx
);
159 return WERR_VERSION_PARSE_ERROR
;
162 /* call the drsuapi version */
163 werr
= dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob
.ctr
.dsdb
, false, mem_ctx
, _pfm
, NULL
);
164 if (!W_ERROR_IS_OK(werr
)) {
165 DEBUG(0, (__location__
" dsdb_schema_pfm_from_drsuapi_pfm failed: %s\n", win_errstr(werr
)));
166 talloc_free(temp_ctx
);
170 talloc_free(temp_ctx
);
175 WERROR
dsdb_load_oid_mappings_ldb(struct dsdb_schema
*schema
,
176 const struct ldb_val
*prefixMap
,
177 const struct ldb_val
*schemaInfo
)
180 const char *schema_info
;
181 struct dsdb_schema_prefixmap
*pfm
;
184 /* verify schemaInfo blob is valid one */
185 if (!dsdb_schema_info_blob_is_valid(schemaInfo
)) {
186 DEBUG(0,(__location__
": dsdb_schema_info_blob_is_valid() failed.\n"));
187 return WERR_INVALID_PARAMETER
;
190 mem_ctx
= talloc_new(schema
);
191 W_ERROR_HAVE_NO_MEMORY(mem_ctx
);
193 /* fetch prefixMap */
194 werr
= _dsdb_prefixmap_from_ldb_val(prefixMap
,
196 if (!W_ERROR_IS_OK(werr
)) {
197 DEBUG(0, (__location__
" _dsdb_prefixmap_from_ldb_val failed: %s\n", win_errstr(werr
)));
198 talloc_free(mem_ctx
);
202 /* decode schema_info */
203 schema_info
= hex_encode_talloc(mem_ctx
,
207 talloc_free(mem_ctx
);
211 /* store prefixMap and schema_info into cached Schema */
212 talloc_free(schema
->prefixmap
);
213 schema
->prefixmap
= talloc_steal(schema
, pfm
);
215 talloc_free(discard_const(schema
->schema_info
));
216 schema
->schema_info
= talloc_steal(schema
, schema_info
);
218 /* clean up locally allocated mem */
219 talloc_free(mem_ctx
);
224 WERROR
dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema
*schema
,
225 bool include_schema_info
,
227 struct drsuapi_DsReplicaOIDMapping_Ctr
**_ctr
)
229 return dsdb_drsuapi_pfm_from_schema_pfm(schema
->prefixmap
,
230 include_schema_info
? schema
->schema_info
: NULL
,
234 WERROR
dsdb_get_drsuapi_prefixmap_as_blob(const struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
,
236 struct ldb_val
*prefixMap
)
238 struct prefixMapBlob pfm
;
239 enum ndr_err_code ndr_err
;
240 pfm
.version
= PREFIX_MAP_VERSION_DSDB
;
244 ndr_err
= ndr_push_struct_blob(prefixMap
, mem_ctx
, &pfm
,
245 (ndr_push_flags_fn_t
)ndr_push_prefixMapBlob
);
246 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
247 NTSTATUS nt_status
= ndr_map_error2ntstatus(ndr_err
);
248 return ntstatus_to_werror(nt_status
);
253 WERROR
dsdb_get_oid_mappings_ldb(const struct dsdb_schema
*schema
,
255 struct ldb_val
*prefixMap
,
256 struct ldb_val
*schemaInfo
)
259 struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
;
261 status
= dsdb_get_oid_mappings_drsuapi(schema
, false, mem_ctx
, &ctr
);
262 W_ERROR_NOT_OK_RETURN(status
);
264 status
= dsdb_get_drsuapi_prefixmap_as_blob(ctr
, mem_ctx
, prefixMap
);
266 W_ERROR_NOT_OK_RETURN(status
);
268 *schemaInfo
= strhex_to_data_blob(mem_ctx
, schema
->schema_info
);
269 W_ERROR_HAVE_NO_MEMORY(schemaInfo
->data
);
276 * this function is called from within a ldb transaction from the schema_fsmo module
278 WERROR
dsdb_create_prefix_mapping(struct ldb_context
*ldb
, struct dsdb_schema
*schema
, const char *full_oid
)
283 struct dsdb_schema_prefixmap
*pfm
;
285 mem_ctx
= talloc_new(ldb
);
286 W_ERROR_HAVE_NO_MEMORY(mem_ctx
);
288 /* Read prefixes from disk*/
289 status
= dsdb_read_prefixes_from_ldb(ldb
, mem_ctx
, &pfm
);
290 if (!W_ERROR_IS_OK(status
)) {
291 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
292 win_errstr(status
)));
293 talloc_free(mem_ctx
);
297 /* Check if there is a prefix for the oid in the prefixes array*/
298 status
= dsdb_schema_pfm_find_oid(pfm
, full_oid
, NULL
);
299 if (W_ERROR_IS_OK(status
)) {
301 talloc_free(mem_ctx
);
303 } else if (!W_ERROR_EQUAL(status
, WERR_NOT_FOUND
)) {
305 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
306 win_errstr(status
)));
307 talloc_free(mem_ctx
);
311 /* Create the new mapping for the prefix of full_oid */
312 status
= dsdb_schema_pfm_make_attid(pfm
, full_oid
, &attid
);
313 if (!W_ERROR_IS_OK(status
)) {
314 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
315 win_errstr(status
)));
316 talloc_free(mem_ctx
);
320 talloc_unlink(schema
, schema
->prefixmap
);
321 schema
->prefixmap
= talloc_steal(schema
, 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
));
335 talloc_free(mem_ctx
);
340 WERROR
dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX
*mem_ctx
, struct ldb_context
*ldb
,
341 const struct dsdb_schema
*schema
)
345 struct ldb_message
*msg
;
346 struct ldb_dn
*schema_dn
;
347 struct prefixMapBlob pfm_blob
;
348 struct ldb_val ndr_blob
;
349 enum ndr_err_code ndr_err
;
350 TALLOC_CTX
*temp_ctx
;
351 struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
;
353 schema_dn
= ldb_get_schema_basedn(ldb
);
355 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
359 temp_ctx
= talloc_new(mem_ctx
);
360 W_ERROR_HAVE_NO_MEMORY(temp_ctx
);
362 /* convert schema_prefixMap to prefixMap blob */
363 status
= dsdb_get_oid_mappings_drsuapi(schema
, false, temp_ctx
, &ctr
);
364 if (!W_ERROR_IS_OK(status
)) {
365 talloc_free(temp_ctx
);
369 pfm_blob
.version
= PREFIX_MAP_VERSION_DSDB
;
370 pfm_blob
.ctr
.dsdb
= *ctr
;
372 ndr_err
= ndr_push_struct_blob(&ndr_blob
, temp_ctx
,
374 (ndr_push_flags_fn_t
)ndr_push_prefixMapBlob
);
375 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
376 talloc_free(temp_ctx
);
380 /* write serialized prefixMap into LDB */
381 msg
= ldb_msg_new(temp_ctx
);
383 talloc_free(temp_ctx
);
388 ldb_ret
= ldb_msg_add_value(msg
, "prefixMap", &ndr_blob
, NULL
);
390 talloc_free(temp_ctx
);
391 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
395 ldb_ret
= dsdb_replace(ldb
, msg
, DSDB_FLAG_AS_SYSTEM
);
397 talloc_free(temp_ctx
);
400 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
407 WERROR
dsdb_read_prefixes_from_ldb(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, struct dsdb_schema_prefixmap
**_pfm
)
411 const struct ldb_val
*prefix_val
;
412 struct ldb_dn
*schema_dn
;
413 struct ldb_result
*schema_res
= NULL
;
414 static const char *schema_attrs
[] = {
419 schema_dn
= ldb_get_schema_basedn(ldb
);
421 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
425 ldb_ret
= ldb_search(ldb
, mem_ctx
, &schema_res
, schema_dn
, LDB_SCOPE_BASE
, schema_attrs
, NULL
);
426 if (ldb_ret
== LDB_ERR_NO_SUCH_OBJECT
) {
427 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
428 talloc_free(schema_res
);
430 } else if (ldb_ret
!= LDB_SUCCESS
) {
431 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
432 talloc_free(schema_res
);
436 prefix_val
= ldb_msg_find_ldb_val(schema_res
->msgs
[0], "prefixMap");
438 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
439 talloc_free(schema_res
);
443 werr
= _dsdb_prefixmap_from_ldb_val(prefix_val
,
446 talloc_free(schema_res
);
447 W_ERROR_NOT_OK_RETURN(werr
);
453 this will be replaced with something that looks at the right part of
454 the schema once we know where unique indexing information is hidden
456 static bool dsdb_schema_unique_attribute(const char *attr
)
458 const char *attrs
[] = { "objectGUID", "objectSid" , NULL
};
460 for (i
=0;attrs
[i
];i
++) {
461 if (strcasecmp(attr
, attrs
[i
]) == 0) {
470 setup the ldb_schema_attribute field for a dsdb_attribute
472 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context
*ldb
,
473 struct dsdb_attribute
*attr
)
475 const char *syntax
= attr
->syntax
->ldb_syntax
;
476 const struct ldb_schema_syntax
*s
;
477 struct ldb_schema_attribute
*a
;
480 syntax
= attr
->syntax
->ldap_oid
;
483 s
= ldb_samba_syntax_by_lDAPDisplayName(ldb
, attr
->lDAPDisplayName
);
485 s
= ldb_samba_syntax_by_name(ldb
, syntax
);
488 s
= ldb_standard_syntax_by_name(ldb
, syntax
);
492 return ldb_operr(ldb
);
495 attr
->ldb_schema_attribute
= a
= talloc(attr
, struct ldb_schema_attribute
);
496 if (attr
->ldb_schema_attribute
== NULL
) {
500 a
->name
= attr
->lDAPDisplayName
;
504 if (dsdb_schema_unique_attribute(a
->name
)) {
505 a
->flags
|= LDB_ATTR_FLAG_UNIQUE_INDEX
;
507 if (attr
->isSingleValued
) {
508 a
->flags
|= LDB_ATTR_FLAG_SINGLE_VALUE
;
516 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
517 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
518 if (get_string_val == NULL) { \
520 d_printf("%s: %s == NULL in %s\n", __location__, attr, ldb_dn_get_linearized(msg->dn)); \
521 return WERR_INVALID_PARAM; \
526 (p)->elem = talloc_strndup(mem_ctx, \
527 (const char *)get_string_val->data, \
528 get_string_val->length); \
530 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
536 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem) do { \
537 int get_string_list_counter; \
538 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
539 /* We may get empty attributes over the replication channel */ \
540 if (get_string_list_el == NULL || get_string_list_el->num_values == 0) { \
544 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
545 for (get_string_list_counter=0; \
546 get_string_list_counter < get_string_list_el->num_values; \
547 get_string_list_counter++) { \
548 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
549 (const char *)get_string_list_el->values[get_string_list_counter].data, \
550 get_string_list_el->values[get_string_list_counter].length); \
551 if (!(p)->elem[get_string_list_counter]) { \
552 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
555 (p)->elem[get_string_list_counter+1] = NULL; \
557 talloc_steal(mem_ctx, (p)->elem); \
560 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
562 str = ldb_msg_find_attr_as_string(msg, attr, NULL);\
565 d_printf("%s: %s == NULL\n", __location__, attr); \
566 return WERR_INVALID_PARAM; \
570 } else if (strcasecmp("TRUE", str) == 0) { \
572 } else if (strcasecmp("FALSE", str) == 0) { \
575 d_printf("%s: %s == %s\n", __location__, attr, str); \
576 return WERR_INVALID_PARAM; \
580 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
581 (p)->elem = ldb_msg_find_attr_as_uint(msg, attr, 0);\
584 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
585 uint64_t _v = ldb_msg_find_attr_as_uint64(msg, attr, UINT64_MAX);\
586 if (_v == UINT64_MAX) { \
588 } else if (_v > UINT32_MAX) { \
589 d_printf("%s: %s == 0x%llX\n", __location__, \
590 attr, (unsigned long long)_v); \
591 return WERR_INVALID_PARAM; \
593 (p)->elem = talloc(mem_ctx, uint32_t); \
595 d_printf("%s: talloc failed for %s\n", __location__, attr); \
598 *(p)->elem = (uint32_t)_v; \
602 #define GET_GUID_LDB(msg, attr, p, elem) do { \
603 (p)->elem = samdb_result_guid(msg, attr);\
606 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
607 const struct ldb_val *_val;\
608 _val = ldb_msg_find_ldb_val(msg, attr);\
611 talloc_steal(mem_ctx, (p)->elem.data);\
613 ZERO_STRUCT((p)->elem);\
617 WERROR
dsdb_attribute_from_ldb(struct ldb_context
*ldb
,
618 struct dsdb_schema
*schema
,
619 struct ldb_message
*msg
)
622 struct dsdb_attribute
*attr
= talloc_zero(schema
, struct dsdb_attribute
);
627 GET_STRING_LDB(msg
, "cn", attr
, attr
, cn
, false);
628 GET_STRING_LDB(msg
, "lDAPDisplayName", attr
, attr
, lDAPDisplayName
, true);
629 GET_STRING_LDB(msg
, "attributeID", attr
, attr
, attributeID_oid
, true);
630 if (!schema
->prefixmap
|| schema
->prefixmap
->length
== 0) {
631 /* set an invalid value */
632 attr
->attributeID_id
= DRSUAPI_ATTID_INVALID
;
634 status
= dsdb_schema_pfm_make_attid(schema
->prefixmap
,
635 attr
->attributeID_oid
,
636 &attr
->attributeID_id
);
637 if (!W_ERROR_IS_OK(status
)) {
638 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
639 __location__
, attr
->lDAPDisplayName
, attr
->attributeID_oid
,
640 win_errstr(status
)));
644 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
645 GET_UINT32_LDB(msg
, "msDS-IntId", attr
, msDS_IntId
);
647 GET_GUID_LDB(msg
, "schemaIDGUID", attr
, schemaIDGUID
);
648 GET_UINT32_LDB(msg
, "mAPIID", attr
, mAPIID
);
650 GET_GUID_LDB(msg
, "attributeSecurityGUID", attr
, attributeSecurityGUID
);
652 GET_GUID_LDB(msg
, "objectGUID", attr
, objectGUID
);
654 GET_UINT32_LDB(msg
, "searchFlags", attr
, searchFlags
);
655 GET_UINT32_LDB(msg
, "systemFlags", attr
, systemFlags
);
656 GET_BOOL_LDB(msg
, "isMemberOfPartialAttributeSet", attr
, isMemberOfPartialAttributeSet
, false);
657 GET_UINT32_LDB(msg
, "linkID", attr
, linkID
);
659 GET_STRING_LDB(msg
, "attributeSyntax", attr
, attr
, attributeSyntax_oid
, true);
660 if (!schema
->prefixmap
|| schema
->prefixmap
->length
== 0) {
661 /* set an invalid value */
662 attr
->attributeSyntax_id
= DRSUAPI_ATTID_INVALID
;
664 status
= dsdb_schema_pfm_attid_from_oid(schema
->prefixmap
,
665 attr
->attributeSyntax_oid
,
666 &attr
->attributeSyntax_id
);
667 if (!W_ERROR_IS_OK(status
)) {
668 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
669 __location__
, attr
->lDAPDisplayName
, attr
->attributeSyntax_oid
,
670 win_errstr(status
)));
674 GET_UINT32_LDB(msg
, "oMSyntax", attr
, oMSyntax
);
675 GET_BLOB_LDB(msg
, "oMObjectClass", attr
, attr
, oMObjectClass
);
677 GET_BOOL_LDB(msg
, "isSingleValued", attr
, isSingleValued
, true);
678 GET_UINT32_PTR_LDB(msg
, "rangeLower", attr
, attr
, rangeLower
);
679 GET_UINT32_PTR_LDB(msg
, "rangeUpper", attr
, attr
, rangeUpper
);
680 GET_BOOL_LDB(msg
, "extendedCharsAllowed", attr
, extendedCharsAllowed
, false);
682 GET_UINT32_LDB(msg
, "schemaFlagsEx", attr
, schemaFlagsEx
);
683 GET_BLOB_LDB(msg
, "msDs-Schema-Extensions", attr
, attr
, msDs_Schema_Extensions
);
685 GET_BOOL_LDB(msg
, "showInAdvancedViewOnly", attr
, showInAdvancedViewOnly
, false);
686 GET_STRING_LDB(msg
, "adminDisplayName", attr
, attr
, adminDisplayName
, false);
687 GET_STRING_LDB(msg
, "adminDescription", attr
, attr
, adminDescription
, false);
688 GET_STRING_LDB(msg
, "classDisplayName", attr
, attr
, classDisplayName
, false);
689 GET_BOOL_LDB(msg
, "isEphemeral", attr
, isEphemeral
, false);
690 GET_BOOL_LDB(msg
, "isDefunct", attr
, isDefunct
, false);
691 GET_BOOL_LDB(msg
, "systemOnly", attr
, systemOnly
, false);
693 attr
->syntax
= dsdb_syntax_for_attribute(attr
);
695 DEBUG(0,(__location__
": Unknown schema syntax for %s\n",
696 attr
->lDAPDisplayName
));
697 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX
;
700 if (dsdb_schema_setup_ldb_schema_attribute(ldb
, attr
) != LDB_SUCCESS
) {
701 DEBUG(0,(__location__
": Unknown schema syntax for %s - ldb_syntax: %s, ldap_oid: %s\n",
702 attr
->lDAPDisplayName
,
703 attr
->syntax
->ldb_syntax
,
704 attr
->syntax
->ldap_oid
));
705 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX
;
708 DLIST_ADD(schema
->attributes
, attr
);
712 WERROR
dsdb_class_from_ldb(struct dsdb_schema
*schema
,
713 struct ldb_message
*msg
)
716 struct dsdb_class
*obj
= talloc_zero(schema
, struct dsdb_class
);
720 GET_STRING_LDB(msg
, "cn", obj
, obj
, cn
, false);
721 GET_STRING_LDB(msg
, "lDAPDisplayName", obj
, obj
, lDAPDisplayName
, true);
722 GET_STRING_LDB(msg
, "governsID", obj
, obj
, governsID_oid
, true);
723 if (!schema
->prefixmap
|| schema
->prefixmap
->length
== 0) {
724 /* set an invalid value */
725 obj
->governsID_id
= DRSUAPI_ATTID_INVALID
;
727 status
= dsdb_schema_pfm_make_attid(schema
->prefixmap
,
730 if (!W_ERROR_IS_OK(status
)) {
731 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
732 __location__
, obj
->lDAPDisplayName
, obj
->governsID_oid
,
733 win_errstr(status
)));
737 GET_GUID_LDB(msg
, "schemaIDGUID", obj
, schemaIDGUID
);
738 GET_GUID_LDB(msg
, "objectGUID", obj
, objectGUID
);
740 GET_UINT32_LDB(msg
, "objectClassCategory", obj
, objectClassCategory
);
741 GET_STRING_LDB(msg
, "rDNAttID", obj
, obj
, rDNAttID
, false);
742 GET_STRING_LDB(msg
, "defaultObjectCategory", obj
, obj
, defaultObjectCategory
, true);
744 GET_STRING_LDB(msg
, "subClassOf", obj
, obj
, subClassOf
, true);
746 GET_STRING_LIST_LDB(msg
, "systemAuxiliaryClass", obj
, obj
, systemAuxiliaryClass
);
747 GET_STRING_LIST_LDB(msg
, "auxiliaryClass", obj
, obj
, auxiliaryClass
);
749 GET_STRING_LIST_LDB(msg
, "systemMustContain", obj
, obj
, systemMustContain
);
750 GET_STRING_LIST_LDB(msg
, "systemMayContain", obj
, obj
, systemMayContain
);
751 GET_STRING_LIST_LDB(msg
, "mustContain", obj
, obj
, mustContain
);
752 GET_STRING_LIST_LDB(msg
, "mayContain", obj
, obj
, mayContain
);
754 GET_STRING_LIST_LDB(msg
, "systemPossSuperiors", obj
, obj
, systemPossSuperiors
);
755 GET_STRING_LIST_LDB(msg
, "possSuperiors", obj
, obj
, possSuperiors
);
757 GET_STRING_LDB(msg
, "defaultSecurityDescriptor", obj
, obj
, defaultSecurityDescriptor
, false);
759 GET_UINT32_LDB(msg
, "schemaFlagsEx", obj
, schemaFlagsEx
);
760 GET_BLOB_LDB(msg
, "msDs-Schema-Extensions", obj
, obj
, msDs_Schema_Extensions
);
762 GET_BOOL_LDB(msg
, "showInAdvancedViewOnly", obj
, showInAdvancedViewOnly
, false);
763 GET_STRING_LDB(msg
, "adminDisplayName", obj
, obj
, adminDisplayName
, false);
764 GET_STRING_LDB(msg
, "adminDescription", obj
, obj
, adminDescription
, false);
765 GET_STRING_LDB(msg
, "classDisplayName", obj
, obj
, classDisplayName
, false);
766 GET_BOOL_LDB(msg
, "defaultHidingValue", obj
, defaultHidingValue
, false);
767 GET_BOOL_LDB(msg
, "isDefunct", obj
, isDefunct
, false);
768 GET_BOOL_LDB(msg
, "systemOnly", obj
, systemOnly
, false);
770 DLIST_ADD(schema
->classes
, obj
);
774 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
777 Create a DSDB schema from the ldb results provided. This is called
778 directly when the schema is provisioned from an on-disk LDIF file, or
779 from dsdb_schema_from_schema_dn in schema_fsmo
782 int dsdb_schema_from_ldb_results(TALLOC_CTX
*mem_ctx
, struct ldb_context
*ldb
,
783 struct ldb_result
*schema_res
,
784 struct ldb_result
*attrs_res
, struct ldb_result
*objectclass_res
,
785 struct dsdb_schema
**schema_out
,
790 const struct ldb_val
*prefix_val
;
791 const struct ldb_val
*info_val
;
792 struct ldb_val info_val_default
;
793 struct dsdb_schema
*schema
;
795 schema
= dsdb_new_schema(mem_ctx
);
797 dsdb_oom(error_string
, mem_ctx
);
798 return ldb_operr(ldb
);
801 schema
->base_dn
= talloc_steal(schema
, schema_res
->msgs
[0]->dn
);
803 prefix_val
= ldb_msg_find_ldb_val(schema_res
->msgs
[0], "prefixMap");
805 *error_string
= talloc_asprintf(mem_ctx
,
806 "schema_fsmo_init: no prefixMap attribute found");
807 DEBUG(0,(__location__
": %s\n", *error_string
));
808 return LDB_ERR_CONSTRAINT_VIOLATION
;
810 info_val
= ldb_msg_find_ldb_val(schema_res
->msgs
[0], "schemaInfo");
812 status
= dsdb_schema_info_blob_new(mem_ctx
, &info_val_default
);
813 if (!W_ERROR_IS_OK(status
)) {
814 *error_string
= talloc_asprintf(mem_ctx
,
815 "schema_fsmo_init: dsdb_schema_info_blob_new() failed - %s",
817 DEBUG(0,(__location__
": %s\n", *error_string
));
818 return ldb_operr(ldb
);
820 info_val
= &info_val_default
;
823 status
= dsdb_load_oid_mappings_ldb(schema
, prefix_val
, info_val
);
824 if (!W_ERROR_IS_OK(status
)) {
825 *error_string
= talloc_asprintf(mem_ctx
,
826 "schema_fsmo_init: failed to load oid mappings: %s",
828 DEBUG(0,(__location__
": %s\n", *error_string
));
829 return LDB_ERR_CONSTRAINT_VIOLATION
;
832 for (i
=0; i
< attrs_res
->count
; i
++) {
833 status
= dsdb_attribute_from_ldb(ldb
, schema
, attrs_res
->msgs
[i
]);
834 if (!W_ERROR_IS_OK(status
)) {
835 *error_string
= talloc_asprintf(mem_ctx
,
836 "schema_fsmo_init: failed to load attribute definition: %s:%s",
837 ldb_dn_get_linearized(attrs_res
->msgs
[i
]->dn
),
839 DEBUG(0,(__location__
": %s\n", *error_string
));
840 return LDB_ERR_CONSTRAINT_VIOLATION
;
844 for (i
=0; i
< objectclass_res
->count
; i
++) {
845 status
= dsdb_class_from_ldb(schema
, objectclass_res
->msgs
[i
]);
846 if (!W_ERROR_IS_OK(status
)) {
847 *error_string
= talloc_asprintf(mem_ctx
,
848 "schema_fsmo_init: failed to load class definition: %s:%s",
849 ldb_dn_get_linearized(objectclass_res
->msgs
[i
]->dn
),
851 DEBUG(0,(__location__
": %s\n", *error_string
));
852 return LDB_ERR_CONSTRAINT_VIOLATION
;
856 schema
->fsmo
.master_dn
= ldb_msg_find_attr_as_dn(ldb
, schema
, schema_res
->msgs
[0], "fSMORoleOwner");
857 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb
), schema
->fsmo
.master_dn
) == 0) {
858 schema
->fsmo
.we_are_master
= true;
860 schema
->fsmo
.we_are_master
= false;
863 DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
864 (schema
->fsmo
.we_are_master
?"yes":"no")));
866 *schema_out
= schema
;