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 "lib/ldb/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 "lib/ldb/include/ldb_module.h"
33 #include "../lib/util/asn1.h"
36 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
38 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
43 schema->iconv_convenience = iconv_convenience;
48 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
49 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
52 const char *schema_info;
53 struct dsdb_schema_prefixmap *pfm;
55 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
56 W_ERROR_NOT_OK_RETURN(werr);
58 /* set loaded prefixMap */
59 talloc_free(schema->prefixmap);
60 schema->prefixmap = pfm;
62 talloc_free(discard_const(schema->schema_info));
63 schema->schema_info = schema_info;
68 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
69 struct smb_iconv_convenience *iconv_convenience,
71 struct dsdb_schema_prefixmap **_pfm)
74 enum ndr_err_code ndr_err;
75 struct prefixMapBlob pfm_blob;
77 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
78 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
80 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
81 iconv_convenience, &pfm_blob,
82 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
83 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
84 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
85 talloc_free(temp_ctx);
86 return ntstatus_to_werror(nt_status);
89 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
90 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version incorrect\n"));
91 talloc_free(temp_ctx);
92 return WERR_VERSION_PARSE_ERROR;
95 /* call the drsuapi version */
96 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
98 talloc_free(temp_ctx);
103 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
104 const struct ldb_val *prefixMap,
105 const struct ldb_val *schemaInfo)
108 const char *schema_info;
109 struct dsdb_schema_prefixmap *pfm;
112 /* verify input params */
113 if (schemaInfo->length != 21) {
114 return WERR_INVALID_PARAMETER;
116 if (schemaInfo->data[0] != 0xFF) {
117 return WERR_INVALID_PARAMETER;
120 mem_ctx = talloc_new(schema);
121 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
123 /* fetch prefixMap */
124 status = _dsdb_prefixmap_from_ldb_val(prefixMap,
125 schema->iconv_convenience,
127 W_ERROR_NOT_OK_RETURN(status);
129 /* decode schema_info */
130 schema_info = hex_encode_talloc(mem_ctx,
134 talloc_free(mem_ctx);
138 /* store prefixMap and schema_info into cached Schema */
139 talloc_free(schema->prefixmap);
140 schema->prefixmap = talloc_steal(schema, pfm);
142 talloc_free(discard_const(schema->schema_info));
143 schema->schema_info = talloc_steal(schema, schema_info);
145 /* clean up locally allocated mem */
146 talloc_free(mem_ctx);
151 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
152 bool include_schema_info,
154 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
156 return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
157 include_schema_info ? schema->schema_info : NULL,
161 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
163 struct ldb_val *prefixMap,
164 struct ldb_val *schemaInfo)
167 enum ndr_err_code ndr_err;
168 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
169 struct prefixMapBlob pfm;
171 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
172 W_ERROR_NOT_OK_RETURN(status);
174 pfm.version = PREFIX_MAP_VERSION_DSDB;
178 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm,
179 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
181 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
182 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
183 return ntstatus_to_werror(nt_status);
186 *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
187 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
194 * this function is called from within a ldb transaction from the schema_fsmo module
196 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
201 struct dsdb_schema_prefixmap *pfm;
203 mem_ctx = talloc_new(ldb);
204 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
206 /* Read prefixes from disk*/
207 status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
208 if (!W_ERROR_IS_OK(status)) {
209 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
210 win_errstr(status)));
211 talloc_free(mem_ctx);
215 /* Check if there is a prefix for the oid in the prefixes array*/
216 status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
217 if (W_ERROR_IS_OK(status)) {
219 talloc_free(mem_ctx);
221 } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
223 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
224 win_errstr(status)));
225 talloc_free(mem_ctx);
229 /* Create the new mapping for the prefix of full_oid */
230 status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
231 if (!W_ERROR_IS_OK(status)) {
232 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
233 win_errstr(status)));
234 talloc_free(mem_ctx);
238 talloc_unlink(schema, schema->prefixmap);
239 schema->prefixmap = talloc_steal(schema, pfm);
241 /* Update prefixMap in ldb*/
242 status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
243 if (!W_ERROR_IS_OK(status)) {
244 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
245 win_errstr(status)));
246 talloc_free(mem_ctx);
250 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
251 full_oid, schema->prefixmap->length));
253 talloc_free(mem_ctx);
258 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
259 const struct dsdb_schema *schema)
263 struct ldb_message *msg;
264 struct ldb_dn *schema_dn;
265 struct prefixMapBlob pfm_blob;
266 struct ldb_val ndr_blob;
267 enum ndr_err_code ndr_err;
268 TALLOC_CTX *temp_ctx;
269 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
271 schema_dn = samdb_schema_dn(ldb);
273 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
277 temp_ctx = talloc_new(mem_ctx);
278 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
280 /* convert schema_prefixMap to prefixMap blob */
281 status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
282 if (!W_ERROR_IS_OK(status)) {
283 talloc_free(temp_ctx);
287 pfm_blob.version = PREFIX_MAP_VERSION_DSDB;
288 pfm_blob.ctr.dsdb = *ctr;
290 ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
291 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
293 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
294 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
295 talloc_free(temp_ctx);
299 /* write serialized prefixMap into LDB */
300 msg = ldb_msg_new(temp_ctx);
302 talloc_free(temp_ctx);
307 ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
309 talloc_free(temp_ctx);
310 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
314 ldb_ret = dsdb_replace(ldb, msg, DSDB_FLAG_AS_SYSTEM);
316 talloc_free(temp_ctx);
319 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: dsdb_replace failed\n"));
326 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
330 const struct ldb_val *prefix_val;
331 struct smb_iconv_convenience *iconv_convenience;
332 struct ldb_dn *schema_dn;
333 struct ldb_result *schema_res = NULL;
334 static const char *schema_attrs[] = {
339 schema_dn = samdb_schema_dn(ldb);
341 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
345 ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
346 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
347 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
348 talloc_free(schema_res);
350 } else if (ldb_ret != LDB_SUCCESS) {
351 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
352 talloc_free(schema_res);
356 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
358 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
359 talloc_free(schema_res);
363 iconv_convenience = lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm"));
365 werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
369 talloc_free(schema_res);
370 W_ERROR_NOT_OK_RETURN(werr);
376 this will be replaced with something that looks at the right part of
377 the schema once we know where unique indexing information is hidden
379 static bool dsdb_schema_unique_attribute(const char *attr)
381 const char *attrs[] = { "objectGUID", "objectSID" , NULL };
383 for (i=0;attrs[i];i++) {
384 if (strcasecmp(attr, attrs[i]) == 0) {
393 setup the ldb_schema_attribute field for a dsdb_attribute
395 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
396 struct dsdb_attribute *attr)
398 const char *syntax = attr->syntax->ldb_syntax;
399 const struct ldb_schema_syntax *s;
400 struct ldb_schema_attribute *a;
403 syntax = attr->syntax->ldap_oid;
406 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
408 s = ldb_samba_syntax_by_name(ldb, syntax);
411 s = ldb_standard_syntax_by_name(ldb, syntax);
415 return LDB_ERR_OPERATIONS_ERROR;
418 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
419 if (attr->ldb_schema_attribute == NULL) {
421 return LDB_ERR_OPERATIONS_ERROR;
424 a->name = attr->lDAPDisplayName;
428 if (dsdb_schema_unique_attribute(a->name)) {
429 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
431 if (attr->isSingleValued) {
432 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
440 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
441 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
442 if (get_string_val == NULL) { \
444 d_printf("%s: %s == NULL\n", __location__, attr); \
445 return WERR_INVALID_PARAM; \
450 (p)->elem = talloc_strndup(mem_ctx, \
451 (const char *)get_string_val->data, \
452 get_string_val->length); \
454 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
460 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
461 int get_string_list_counter; \
462 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
463 if (get_string_list_el == NULL) { \
465 d_printf("%s: %s == NULL\n", __location__, attr); \
466 return WERR_INVALID_PARAM; \
472 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
473 for (get_string_list_counter=0; \
474 get_string_list_counter < get_string_list_el->num_values; \
475 get_string_list_counter++) { \
476 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
477 (const char *)get_string_list_el->values[get_string_list_counter].data, \
478 get_string_list_el->values[get_string_list_counter].length); \
479 if (!(p)->elem[get_string_list_counter]) { \
480 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
483 (p)->elem[get_string_list_counter+1] = NULL; \
485 talloc_steal(mem_ctx, (p)->elem); \
488 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
490 str = samdb_result_string(msg, attr, NULL);\
493 d_printf("%s: %s == NULL\n", __location__, attr); \
494 return WERR_INVALID_PARAM; \
498 } else if (strcasecmp("TRUE", str) == 0) { \
500 } else if (strcasecmp("FALSE", str) == 0) { \
503 d_printf("%s: %s == %s\n", __location__, attr, str); \
504 return WERR_INVALID_PARAM; \
508 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
509 (p)->elem = samdb_result_uint(msg, attr, 0);\
512 #define GET_UINT32_PTR_LDB(msg, attr, mem_ctx, p, elem) do { \
513 uint64_t _v = samdb_result_uint64(msg, attr, UINT64_MAX);\
514 if (_v == UINT64_MAX) { \
516 } else if (_v > UINT32_MAX) { \
517 d_printf("%s: %s == 0x%llX\n", __location__, \
518 attr, (unsigned long long)_v); \
519 return WERR_INVALID_PARAM; \
521 (p)->elem = talloc(mem_ctx, uint32_t); \
523 d_printf("%s: talloc failed for %s\n", __location__, attr); \
526 *(p)->elem = (uint32_t)_v; \
530 #define GET_GUID_LDB(msg, attr, p, elem) do { \
531 (p)->elem = samdb_result_guid(msg, attr);\
534 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
535 const struct ldb_val *_val;\
536 _val = ldb_msg_find_ldb_val(msg, attr);\
539 talloc_steal(mem_ctx, (p)->elem.data);\
541 ZERO_STRUCT((p)->elem);\
545 WERROR dsdb_attribute_from_ldb(struct ldb_context *ldb,
546 struct dsdb_schema *schema,
547 struct ldb_message *msg)
550 struct dsdb_attribute *attr = talloc_zero(schema, struct dsdb_attribute);
555 GET_STRING_LDB(msg, "cn", attr, attr, cn, false);
556 GET_STRING_LDB(msg, "lDAPDisplayName", attr, attr, lDAPDisplayName, true);
557 GET_STRING_LDB(msg, "attributeID", attr, attr, attributeID_oid, true);
558 if (!schema->prefixmap || schema->prefixmap->length == 0) {
559 /* set an invalid value */
560 attr->attributeID_id = 0xFFFFFFFF;
562 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
563 attr->attributeID_oid,
564 &attr->attributeID_id);
565 if (!W_ERROR_IS_OK(status)) {
566 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
567 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
568 win_errstr(status)));
572 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
573 GET_UINT32_LDB(msg, "msDS-IntId", attr, msDS_IntId);
575 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
576 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
578 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
580 GET_GUID_LDB(msg, "objectGUID", attr, objectGUID);
582 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
583 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
584 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
585 GET_UINT32_LDB(msg, "linkID", attr, linkID);
587 GET_STRING_LDB(msg, "attributeSyntax", attr, attr, attributeSyntax_oid, true);
588 if (!schema->prefixmap || schema->prefixmap->length == 0) {
589 /* set an invalid value */
590 attr->attributeSyntax_id = 0xFFFFFFFF;
592 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
593 attr->attributeSyntax_oid,
594 &attr->attributeSyntax_id);
595 if (!W_ERROR_IS_OK(status)) {
596 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
597 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
598 win_errstr(status)));
602 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
603 GET_BLOB_LDB(msg, "oMObjectClass", attr, attr, oMObjectClass);
605 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
606 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, attr, rangeLower);
607 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, attr, rangeUpper);
608 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
610 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
611 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", attr, attr, msDs_Schema_Extensions);
613 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
614 GET_STRING_LDB(msg, "adminDisplayName", attr, attr, adminDisplayName, false);
615 GET_STRING_LDB(msg, "adminDescription", attr, attr, adminDescription, false);
616 GET_STRING_LDB(msg, "classDisplayName", attr, attr, classDisplayName, false);
617 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
618 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
619 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
621 attr->syntax = dsdb_syntax_for_attribute(attr);
623 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
624 attr->lDAPDisplayName));
625 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
628 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
629 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
630 attr->lDAPDisplayName));
631 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
634 DLIST_ADD(schema->attributes, attr);
638 WERROR dsdb_class_from_ldb(struct dsdb_schema *schema,
639 struct ldb_message *msg)
642 struct dsdb_class *obj = talloc_zero(schema, struct dsdb_class);
646 GET_STRING_LDB(msg, "cn", obj, obj, cn, false);
647 GET_STRING_LDB(msg, "lDAPDisplayName", obj, obj, lDAPDisplayName, true);
648 GET_STRING_LDB(msg, "governsID", obj, obj, governsID_oid, true);
649 if (!schema->prefixmap || schema->prefixmap->length == 0) {
650 /* set an invalid value */
651 obj->governsID_id = 0xFFFFFFFF;
653 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
656 if (!W_ERROR_IS_OK(status)) {
657 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
658 __location__, obj->lDAPDisplayName, obj->governsID_oid,
659 win_errstr(status)));
663 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
664 GET_GUID_LDB(msg, "objectGUID", obj, objectGUID);
666 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
667 GET_STRING_LDB(msg, "rDNAttID", obj, obj, rDNAttID, false);
668 GET_STRING_LDB(msg, "defaultObjectCategory", obj, obj, defaultObjectCategory, true);
670 GET_STRING_LDB(msg, "subClassOf", obj, obj, subClassOf, true);
672 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", obj, obj, systemAuxiliaryClass, false);
673 GET_STRING_LIST_LDB(msg, "auxiliaryClass", obj, obj, auxiliaryClass, false);
675 GET_STRING_LIST_LDB(msg, "systemMustContain", obj, obj, systemMustContain, false);
676 GET_STRING_LIST_LDB(msg, "systemMayContain", obj, obj, systemMayContain, false);
677 GET_STRING_LIST_LDB(msg, "mustContain", obj, obj, mustContain, false);
678 GET_STRING_LIST_LDB(msg, "mayContain", obj, obj, mayContain, false);
680 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", obj, obj, systemPossSuperiors, false);
681 GET_STRING_LIST_LDB(msg, "possSuperiors", obj, obj, possSuperiors, false);
683 GET_STRING_LDB(msg, "defaultSecurityDescriptor", obj, obj, defaultSecurityDescriptor, false);
685 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
686 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", obj, obj, msDs_Schema_Extensions);
688 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
689 GET_STRING_LDB(msg, "adminDisplayName", obj, obj, adminDisplayName, false);
690 GET_STRING_LDB(msg, "adminDescription", obj, obj, adminDescription, false);
691 GET_STRING_LDB(msg, "classDisplayName", obj, obj, classDisplayName, false);
692 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
693 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
694 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
696 DLIST_ADD(schema->classes, obj);
700 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
703 Create a DSDB schema from the ldb results provided. This is called
704 directly when the schema is provisioned from an on-disk LDIF file, or
705 from dsdb_schema_from_schema_dn in schema_fsmo
708 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
709 struct smb_iconv_convenience *iconv_convenience,
710 struct ldb_result *schema_res,
711 struct ldb_result *attrs_res, struct ldb_result *objectclass_res,
712 struct dsdb_schema **schema_out,
717 const struct ldb_val *prefix_val;
718 const struct ldb_val *info_val;
719 struct ldb_val info_val_default;
720 struct dsdb_schema *schema;
722 schema = dsdb_new_schema(mem_ctx, iconv_convenience);
724 dsdb_oom(error_string, mem_ctx);
725 return LDB_ERR_OPERATIONS_ERROR;
728 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
730 *error_string = talloc_asprintf(mem_ctx,
731 "schema_fsmo_init: no prefixMap attribute found");
732 DEBUG(0,(__location__ ": %s\n", *error_string));
733 return LDB_ERR_CONSTRAINT_VIOLATION;
735 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
737 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
738 if (!info_val_default.data) {
739 dsdb_oom(error_string, mem_ctx);
740 return LDB_ERR_OPERATIONS_ERROR;
742 info_val = &info_val_default;
745 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
746 if (!W_ERROR_IS_OK(status)) {
747 *error_string = talloc_asprintf(mem_ctx,
748 "schema_fsmo_init: failed to load oid mappings: %s",
750 DEBUG(0,(__location__ ": %s\n", *error_string));
751 return LDB_ERR_CONSTRAINT_VIOLATION;
754 for (i=0; i < attrs_res->count; i++) {
755 status = dsdb_attribute_from_ldb(ldb, schema, attrs_res->msgs[i]);
756 if (!W_ERROR_IS_OK(status)) {
757 *error_string = talloc_asprintf(mem_ctx,
758 "schema_fsmo_init: failed to load attribute definition: %s:%s",
759 ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
761 DEBUG(0,(__location__ ": %s\n", *error_string));
762 return LDB_ERR_CONSTRAINT_VIOLATION;
766 for (i=0; i < objectclass_res->count; i++) {
767 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i]);
768 if (!W_ERROR_IS_OK(status)) {
769 *error_string = talloc_asprintf(mem_ctx,
770 "schema_fsmo_init: failed to load class definition: %s:%s",
771 ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
773 DEBUG(0,(__location__ ": %s\n", *error_string));
774 return LDB_ERR_CONSTRAINT_VIOLATION;
778 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
779 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
780 schema->fsmo.we_are_master = true;
782 schema->fsmo.we_are_master = false;
785 DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
786 (schema->fsmo.we_are_master?"yes":"no")));
788 *schema_out = schema;
793 static const struct {
796 } name_mappings[] = {
798 { "name", "1.2.840.113556.1.4.1" },
799 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
800 { "attributeID", "1.2.840.113556.1.2.30" },
801 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
802 { "mAPIID", "1.2.840.113556.1.2.49" },
803 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
804 { "searchFlags", "1.2.840.113556.1.2.334" },
805 { "systemFlags", "1.2.840.113556.1.4.375" },
806 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
807 { "linkID", "1.2.840.113556.1.2.50" },
808 { "attributeSyntax", "1.2.840.113556.1.2.32" },
809 { "oMSyntax", "1.2.840.113556.1.2.231" },
810 { "oMObjectClass", "1.2.840.113556.1.2.218" },
811 { "isSingleValued", "1.2.840.113556.1.2.33" },
812 { "rangeLower", "1.2.840.113556.1.2.34" },
813 { "rangeUpper", "1.2.840.113556.1.2.35" },
814 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
815 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
816 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
817 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
818 { "adminDisplayName", "1.2.840.113556.1.2.194" },
819 { "adminDescription", "1.2.840.113556.1.2.226" },
820 { "classDisplayName", "1.2.840.113556.1.4.610" },
821 { "isEphemeral", "1.2.840.113556.1.4.1212" },
822 { "isDefunct", "1.2.840.113556.1.4.661" },
823 { "systemOnly", "1.2.840.113556.1.4.170" },
824 { "governsID", "1.2.840.113556.1.2.22" },
825 { "objectClassCategory", "1.2.840.113556.1.2.370" },
826 { "rDNAttID", "1.2.840.113556.1.2.26" },
827 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
828 { "subClassOf", "1.2.840.113556.1.2.21" },
829 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
830 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
831 { "systemMustContain", "1.2.840.113556.1.4.197" },
832 { "systemMayContain", "1.2.840.113556.1.4.196" },
833 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
834 { "possSuperiors", "1.2.840.113556.1.2.8" },
835 { "mustContain", "1.2.840.113556.1.2.24" },
836 { "mayContain", "1.2.840.113556.1.2.25" },
837 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
838 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
839 { "msDS-IntId", "1.2.840.113556.1.4.1716" },
842 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
843 struct drsuapi_DsReplicaObject *obj,
849 const char *oid = NULL;
851 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
852 if (strcmp(name_mappings[i].name, name) != 0) continue;
854 oid = name_mappings[i].oid;
862 status = dsdb_schema_pfm_make_attid(schema->prefixmap, oid, &attid);
863 if (!W_ERROR_IS_OK(status)) {
867 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
868 if (obj->attribute_ctr.attributes[i].attid != attid) continue;
871 return &obj->attribute_ctr.attributes[i];
877 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
878 struct drsuapi_DsReplicaAttribute *_a; \
879 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
880 if (strict && !_a) { \
881 d_printf("%s: %s == NULL\n", __location__, attr); \
882 return WERR_INVALID_PARAM; \
884 if (strict && _a->value_ctr.num_values != 1) { \
885 d_printf("%s: %s num_values == %u\n", __location__, attr, \
886 _a->value_ctr.num_values); \
887 return WERR_INVALID_PARAM; \
889 if (_a && _a->value_ctr.num_values >= 1) { \
891 if (!convert_string_talloc_convenience(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
892 _a->value_ctr.values[0].blob->data, \
893 _a->value_ctr.values[0].blob->length, \
894 (void **)discard_const(&(p)->elem), &_ret, false)) { \
895 DEBUG(0,("%s: invalid data!\n", attr)); \
897 _a->value_ctr.values[0].blob->data, \
898 _a->value_ctr.values[0].blob->length); \
899 return WERR_FOOBAR; \
906 #define GET_UINT32_LIST_DS(s, r, attr, mem_ctx, p, elem) do { \
908 struct drsuapi_DsReplicaAttribute *_a; \
909 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
910 (p)->elem = _a ? talloc_array(mem_ctx, uint32_t, _a->value_ctr.num_values + 1) : NULL; \
911 for (list_counter=0; \
912 _a && list_counter < _a->value_ctr.num_values; \
914 if (_a->value_ctr.values[list_counter].blob->length != 4) { \
915 return WERR_INVALID_PARAM; \
917 (p)->elem[list_counter] = IVAL(_a->value_ctr.values[list_counter].blob->data, 0); \
919 if (_a) (p)->elem[list_counter] = 0; \
922 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
923 struct drsuapi_DsReplicaAttribute *_a; \
924 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
925 if (strict && !_a) { \
926 d_printf("%s: %s == NULL\n", __location__, attr); \
927 return WERR_INVALID_PARAM; \
929 if (strict && _a->value_ctr.num_values != 1) { \
930 d_printf("%s: %s num_values == %u\n", __location__, attr, \
931 (unsigned int)_a->value_ctr.num_values); \
932 return WERR_INVALID_PARAM; \
934 if (strict && !_a->value_ctr.values[0].blob) { \
935 d_printf("%s: %s data == NULL\n", __location__, attr); \
936 return WERR_INVALID_PARAM; \
938 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
939 d_printf("%s: %s length == %u\n", __location__, attr, \
940 (unsigned int)_a->value_ctr.values[0].blob->length); \
941 return WERR_INVALID_PARAM; \
943 if (_a && _a->value_ctr.num_values >= 1 \
944 && _a->value_ctr.values[0].blob \
945 && _a->value_ctr.values[0].blob->length == 4) { \
946 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
952 #define GET_UINT32_DS(s, r, attr, p, elem, def_val) do { \
953 struct drsuapi_DsReplicaAttribute *_a; \
954 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
955 if (_a && _a->value_ctr.num_values >= 1 \
956 && _a->value_ctr.values[0].blob \
957 && _a->value_ctr.values[0].blob->length == 4) { \
958 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
960 (p)->elem = def_val; \
964 #define GET_UINT32_PTR_DS(s, r, attr, p, elem) do { \
965 struct drsuapi_DsReplicaAttribute *_a; \
966 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
967 if (_a && _a->value_ctr.num_values >= 1 \
968 && _a->value_ctr.values[0].blob \
969 && _a->value_ctr.values[0].blob->length == 4) { \
970 (p)->elem = talloc(mem_ctx, uint32_t); \
972 d_printf("%s: talloc failed for %s\n", __location__, attr); \
975 *(p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
981 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
982 struct drsuapi_DsReplicaAttribute *_a; \
983 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
984 if (_a && _a->value_ctr.num_values >= 1 \
985 && _a->value_ctr.values[0].blob \
986 && _a->value_ctr.values[0].blob->length == 16) { \
987 NTSTATUS _nt_status = GUID_from_ndr_blob(_a->value_ctr.values[0].blob, &(p)->elem); \
988 if (!NT_STATUS_IS_OK(_nt_status)) { \
989 return ntstatus_to_werror(_nt_status); \
992 ZERO_STRUCT((p)->elem);\
996 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
997 struct drsuapi_DsReplicaAttribute *_a; \
998 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
999 if (_a && _a->value_ctr.num_values >= 1 \
1000 && _a->value_ctr.values[0].blob) { \
1001 (p)->elem = *_a->value_ctr.values[0].blob;\
1002 talloc_steal(mem_ctx, (p)->elem.data); \
1004 ZERO_STRUCT((p)->elem);\
1008 WERROR dsdb_attribute_from_drsuapi(struct ldb_context *ldb,
1009 struct dsdb_schema *schema,
1010 struct drsuapi_DsReplicaObject *r,
1011 TALLOC_CTX *mem_ctx,
1012 struct dsdb_attribute *attr)
1016 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1017 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1018 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id, 0xFFFFFFFF);
1019 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attr->attributeID_id,
1020 mem_ctx, &attr->attributeID_oid);
1021 if (!W_ERROR_IS_OK(status)) {
1022 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1023 __location__, attr->lDAPDisplayName, attr->attributeID_id,
1024 win_errstr(status)));
1027 /* fetch msDS-IntId to be used in resolving ATTRTYP values */
1028 GET_UINT32_DS(schema, r, "msDS-IntId", attr, msDS_IntId, 0);
1030 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1031 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID, 0);
1033 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1035 attr->objectGUID = r->identifier->guid;
1037 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags, 0);
1038 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags, 0);
1039 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1040 GET_UINT32_DS(schema, r, "linkID", attr, linkID, 0);
1042 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id, 0xFFFFFFFF);
1043 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attr->attributeSyntax_id,
1044 mem_ctx, &attr->attributeSyntax_oid);
1045 if (!W_ERROR_IS_OK(status)) {
1046 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1047 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1048 win_errstr(status)));
1051 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax, 0);
1052 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1054 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1055 GET_UINT32_PTR_DS(schema, r, "rangeLower", attr, rangeLower);
1056 GET_UINT32_PTR_DS(schema, r, "rangeUpper", attr, rangeUpper);
1057 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1059 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx, 0);
1060 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1062 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1063 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1064 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1065 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1066 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1067 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1068 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1070 attr->syntax = dsdb_syntax_for_attribute(attr);
1071 if (!attr->syntax) {
1072 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
1073 attr->lDAPDisplayName));
1074 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1077 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
1078 DEBUG(0,(__location__ ": Unknown schema syntax for %s\n",
1079 attr->lDAPDisplayName));
1080 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1086 WERROR dsdb_class_from_drsuapi(struct ldb_context *ldb,
1087 struct dsdb_schema *schema,
1088 struct drsuapi_DsReplicaObject *r,
1089 TALLOC_CTX *mem_ctx,
1090 struct dsdb_class *obj)
1093 struct drsuapi_DsReplicaAttribute *attr;
1096 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1097 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1098 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id, 0xFFFFFFFF);
1099 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, obj->governsID_id,
1100 mem_ctx, &obj->governsID_oid);
1101 if (!W_ERROR_IS_OK(status)) {
1102 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1103 __location__, obj->lDAPDisplayName, obj->governsID_id,
1104 win_errstr(status)));
1107 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1109 obj->objectGUID = r->identifier->guid;
1111 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory, 0);
1112 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1114 attr = dsdb_find_object_attr_name(schema, r, "defaultObjectCategory", NULL);
1116 if (!attr || attr->value_ctr.num_values != 1 || !attr->value_ctr.values[0].blob) {
1117 d_printf("%s: no defaultObjectCategory supplied\n", __location__);
1118 return WERR_INVALID_PARAM;
1121 status = dsdb_syntax_one_DN_drsuapi_to_ldb(mem_ctx, ldb, find_syntax_map_by_standard_oid(LDB_SYNTAX_DN),
1122 schema->iconv_convenience, attr->value_ctr.values[0].blob, &blob);
1123 if (!W_ERROR_IS_OK(status)) {
1126 obj->defaultObjectCategory = (char *)blob.data;
1128 GET_UINT32_DS(schema, r, "subClassOf", obj, subClassOf_id, 0);
1130 GET_UINT32_LIST_DS(schema, r, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass_ids);
1131 GET_UINT32_LIST_DS(schema, r, "auxiliaryClass", mem_ctx, obj, auxiliaryClass_ids);
1133 GET_UINT32_LIST_DS(schema, r, "systemMustContain", mem_ctx, obj, systemMustContain_ids);
1134 GET_UINT32_LIST_DS(schema, r, "systemMayContain", mem_ctx, obj, systemMayContain_ids);
1135 GET_UINT32_LIST_DS(schema, r, "mustContain", mem_ctx, obj, mustContain_ids);
1136 GET_UINT32_LIST_DS(schema, r, "mayContain", mem_ctx, obj, mayContain_ids);
1138 GET_UINT32_LIST_DS(schema, r, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors_ids);
1139 GET_UINT32_LIST_DS(schema, r, "possSuperiors", mem_ctx, obj, possSuperiors_ids);
1141 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1143 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx, 0);
1144 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1146 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1147 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1148 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1149 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1150 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1151 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1152 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);