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 "lib/ldb/include/ldb_errors.h"
26 #include "../lib/util/dlinklist.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "param/param.h"
31 #include "lib/ldb/include/ldb_module.h"
32 #include "../lib/util/asn1.h"
35 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
37 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
42 schema->iconv_convenience = iconv_convenience;
47 WERROR dsdb_load_prefixmap_from_drsuapi(struct dsdb_schema *schema,
48 const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
51 const char *schema_info;
52 struct dsdb_schema_prefixmap *pfm;
54 werr = dsdb_schema_pfm_from_drsuapi_pfm(ctr, true, schema, &pfm, &schema_info);
55 W_ERROR_NOT_OK_RETURN(werr);
57 /* set loaded prefixMap */
58 talloc_free(schema->prefixmap);
59 schema->prefixmap = pfm;
61 talloc_free(discard_const(schema->schema_info));
62 schema->schema_info = schema_info;
67 static WERROR _dsdb_prefixmap_from_ldb_val(const struct ldb_val *pfm_ldb_val,
68 struct smb_iconv_convenience *iconv_convenience,
70 struct dsdb_schema_prefixmap **_pfm)
73 enum ndr_err_code ndr_err;
74 struct prefixMapBlob pfm_blob;
76 TALLOC_CTX *temp_ctx = talloc_new(mem_ctx);
77 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
79 ndr_err = ndr_pull_struct_blob(pfm_ldb_val, temp_ctx,
80 iconv_convenience, &pfm_blob,
81 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
82 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
83 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
84 talloc_free(temp_ctx);
85 return ntstatus_to_werror(nt_status);
88 if (pfm_blob.version != PREFIX_MAP_VERSION_DSDB) {
89 DEBUG(0,("_dsdb_prefixmap_from_ldb_val: pfm_blob->version incorrect\n"));
90 talloc_free(temp_ctx);
91 return WERR_VERSION_PARSE_ERROR;
94 /* call the drsuapi version */
95 werr = dsdb_schema_pfm_from_drsuapi_pfm(&pfm_blob.ctr.dsdb, false, mem_ctx, _pfm, NULL);
97 talloc_free(temp_ctx);
102 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
103 const struct ldb_val *prefixMap,
104 const struct ldb_val *schemaInfo)
107 const char *schema_info;
108 struct dsdb_schema_prefixmap *pfm;
111 /* verify input params */
112 if (schemaInfo->length != 21) {
113 return WERR_INVALID_PARAMETER;
115 if (schemaInfo->data[0] != 0xFF) {
116 return WERR_INVALID_PARAMETER;
119 mem_ctx = talloc_new(schema);
120 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
122 /* fetch prefixMap */
123 status = _dsdb_prefixmap_from_ldb_val(prefixMap,
124 schema->iconv_convenience,
126 W_ERROR_NOT_OK_RETURN(status);
128 /* decode schema_info */
129 schema_info = hex_encode_talloc(mem_ctx,
133 talloc_free(mem_ctx);
137 /* store prefixMap and schema_info into cached Schema */
138 talloc_free(schema->prefixmap);
139 schema->prefixmap = talloc_steal(schema, pfm);
141 talloc_free(discard_const(schema->schema_info));
142 schema->schema_info = talloc_steal(schema, schema_info);
144 /* clean up locally allocated mem */
145 talloc_free(mem_ctx);
150 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
151 bool include_schema_info,
153 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
155 return dsdb_drsuapi_pfm_from_schema_pfm(schema->prefixmap,
156 include_schema_info ? schema->schema_info : NULL,
160 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
162 struct ldb_val *prefixMap,
163 struct ldb_val *schemaInfo)
166 enum ndr_err_code ndr_err;
167 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
168 struct prefixMapBlob pfm;
170 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
171 W_ERROR_NOT_OK_RETURN(status);
173 pfm.version = PREFIX_MAP_VERSION_DSDB;
177 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
179 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
180 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
181 return ntstatus_to_werror(nt_status);
184 *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
185 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
192 * this function is called from within a ldb transaction from the schema_fsmo module
194 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
199 struct dsdb_schema_prefixmap *pfm;
201 mem_ctx = talloc_new(ldb);
202 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
204 /* Read prefixes from disk*/
205 status = dsdb_read_prefixes_from_ldb(ldb, mem_ctx, &pfm);
206 if (!W_ERROR_IS_OK(status)) {
207 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
208 win_errstr(status)));
209 talloc_free(mem_ctx);
213 /* Check if there is a prefix for the oid in the prefixes array*/
214 status = dsdb_schema_pfm_find_oid(pfm, full_oid, NULL);
215 if (W_ERROR_IS_OK(status)) {
217 talloc_free(mem_ctx);
219 } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
221 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
222 win_errstr(status)));
223 talloc_free(mem_ctx);
227 /* Create the new mapping for the prefix of full_oid */
228 status = dsdb_schema_pfm_make_attid(pfm, full_oid, &attid);
229 if (!W_ERROR_IS_OK(status)) {
230 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_schema_pfm_make_attid: %s\n",
231 win_errstr(status)));
232 talloc_free(mem_ctx);
236 talloc_unlink(schema, schema->prefixmap);
237 schema->prefixmap = talloc_steal(schema, pfm);
239 /* Update prefixMap in ldb*/
240 status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
241 if (!W_ERROR_IS_OK(status)) {
242 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
243 win_errstr(status)));
244 talloc_free(mem_ctx);
248 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
249 full_oid, schema->prefixmap->length));
251 talloc_free(mem_ctx);
256 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
257 const struct dsdb_schema *schema)
261 struct ldb_message *msg;
262 struct ldb_dn *schema_dn;
263 struct prefixMapBlob pfm_blob;
264 struct ldb_val ndr_blob;
265 enum ndr_err_code ndr_err;
266 TALLOC_CTX *temp_ctx;
267 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
269 schema_dn = samdb_schema_dn(ldb);
271 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
275 temp_ctx = talloc_new(mem_ctx);
276 W_ERROR_HAVE_NO_MEMORY(temp_ctx);
278 /* convert schema_prefixMap to prefixMap blob */
279 status = dsdb_get_oid_mappings_drsuapi(schema, false, temp_ctx, &ctr);
280 if (!W_ERROR_IS_OK(status)) {
281 talloc_free(temp_ctx);
285 pfm_blob.version = PREFIX_MAP_VERSION_DSDB;
286 pfm_blob.ctr.dsdb = *ctr;
288 ndr_err = ndr_push_struct_blob(&ndr_blob, temp_ctx,
289 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
291 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
292 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
293 talloc_free(temp_ctx);
297 /* write serialized prefixMap into LDB */
298 msg = ldb_msg_new(temp_ctx);
300 talloc_free(temp_ctx);
305 ldb_ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
307 talloc_free(temp_ctx);
308 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));
312 ldb_ret = samdb_replace( ldb, msg, msg );
314 talloc_free(temp_ctx);
317 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: samdb_replace failed\n"));
324 WERROR dsdb_read_prefixes_from_ldb(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct dsdb_schema_prefixmap **_pfm)
328 const struct ldb_val *prefix_val;
329 struct smb_iconv_convenience *iconv_convenience;
330 struct ldb_dn *schema_dn;
331 struct ldb_result *schema_res = NULL;
332 static const char *schema_attrs[] = {
337 schema_dn = samdb_schema_dn(ldb);
339 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
343 ldb_ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
344 if (ldb_ret == LDB_ERR_NO_SUCH_OBJECT) {
345 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
346 talloc_free(schema_res);
348 } else if (ldb_ret != LDB_SUCCESS) {
349 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
350 talloc_free(schema_res);
354 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
356 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
357 talloc_free(schema_res);
361 iconv_convenience = lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm"));
363 werr = _dsdb_prefixmap_from_ldb_val(prefix_val,
367 talloc_free(schema_res);
368 W_ERROR_NOT_OK_RETURN(werr);
374 this will be replaced with something that looks at the right part of
375 the schema once we know where unique indexing information is hidden
377 static bool dsdb_schema_unique_attribute(const char *attr)
379 const char *attrs[] = { "objectGUID", "objectSID" , NULL };
381 for (i=0;attrs[i];i++) {
382 if (strcasecmp(attr, attrs[i]) == 0) {
391 setup the ldb_schema_attribute field for a dsdb_attribute
393 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
394 struct dsdb_attribute *attr)
396 const char *syntax = attr->syntax->ldb_syntax;
397 const struct ldb_schema_syntax *s;
398 struct ldb_schema_attribute *a;
401 syntax = attr->syntax->ldap_oid;
404 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
406 s = ldb_samba_syntax_by_name(ldb, syntax);
409 s = ldb_standard_syntax_by_name(ldb, syntax);
413 return LDB_ERR_OPERATIONS_ERROR;
416 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
417 if (attr->ldb_schema_attribute == NULL) {
419 return LDB_ERR_OPERATIONS_ERROR;
422 a->name = attr->lDAPDisplayName;
426 if (dsdb_schema_unique_attribute(a->name)) {
427 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
429 if (attr->isSingleValued) {
430 a->flags |= LDB_ATTR_FLAG_SINGLE_VALUE;
438 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
439 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
440 if (get_string_val == NULL) { \
442 d_printf("%s: %s == NULL\n", __location__, attr); \
443 return WERR_INVALID_PARAM; \
448 (p)->elem = talloc_strndup(mem_ctx, \
449 (const char *)get_string_val->data, \
450 get_string_val->length); \
452 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
458 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
459 int get_string_list_counter; \
460 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
461 if (get_string_list_el == NULL) { \
463 d_printf("%s: %s == NULL\n", __location__, attr); \
464 return WERR_INVALID_PARAM; \
470 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
471 for (get_string_list_counter=0; \
472 get_string_list_counter < get_string_list_el->num_values; \
473 get_string_list_counter++) { \
474 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
475 (const char *)get_string_list_el->values[get_string_list_counter].data, \
476 get_string_list_el->values[get_string_list_counter].length); \
477 if (!(p)->elem[get_string_list_counter]) { \
478 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
481 (p)->elem[get_string_list_counter+1] = NULL; \
483 talloc_steal(mem_ctx, (p)->elem); \
486 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
488 str = samdb_result_string(msg, attr, NULL);\
491 d_printf("%s: %s == NULL\n", __location__, attr); \
492 return WERR_INVALID_PARAM; \
496 } else if (strcasecmp("TRUE", str) == 0) { \
498 } else if (strcasecmp("FALSE", str) == 0) { \
501 d_printf("%s: %s == %s\n", __location__, attr, str); \
502 return WERR_INVALID_PARAM; \
506 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
507 (p)->elem = samdb_result_uint(msg, attr, 0);\
510 #define GET_UINT32_PTR_LDB(msg, attr, p, elem) do { \
511 uint64_t _v = samdb_result_uint64(msg, attr, UINT64_MAX);\
512 if (_v == UINT64_MAX) { \
514 } else if (_v > UINT32_MAX) { \
515 d_printf("%s: %s == 0x%llX\n", __location__, \
516 attr, (unsigned long long)_v); \
517 return WERR_INVALID_PARAM; \
519 (p)->elem = talloc(mem_ctx, uint32_t); \
521 d_printf("%s: talloc failed for %s\n", __location__, attr); \
524 *(p)->elem = (uint32_t)_v; \
528 #define GET_GUID_LDB(msg, attr, p, elem) do { \
529 (p)->elem = samdb_result_guid(msg, attr);\
532 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
533 const struct ldb_val *_val;\
534 _val = ldb_msg_find_ldb_val(msg, attr);\
537 talloc_steal(mem_ctx, (p)->elem.data);\
539 ZERO_STRUCT((p)->elem);\
543 WERROR dsdb_attribute_from_ldb(struct ldb_context *ldb,
544 const struct dsdb_schema *schema,
545 struct ldb_message *msg,
547 struct dsdb_attribute *attr)
551 GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
552 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
553 GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
554 if (!schema->prefixmap || schema->prefixmap->length == 0) {
555 /* set an invalid value */
556 attr->attributeID_id = 0xFFFFFFFF;
558 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
559 attr->attributeID_oid,
560 &attr->attributeID_id);
561 if (!W_ERROR_IS_OK(status)) {
562 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
563 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
564 win_errstr(status)));
568 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
569 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
571 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
573 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
574 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
575 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
576 GET_UINT32_LDB(msg, "linkID", attr, linkID);
578 GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
579 if (!schema->prefixmap || schema->prefixmap->length == 0) {
580 /* set an invalid value */
581 attr->attributeSyntax_id = 0xFFFFFFFF;
583 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
584 attr->attributeSyntax_oid,
585 &attr->attributeSyntax_id);
586 if (!W_ERROR_IS_OK(status)) {
587 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
588 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
589 win_errstr(status)));
593 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
594 GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
596 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
597 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, rangeLower);
598 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, rangeUpper);
599 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
601 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
602 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
604 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
605 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
606 GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
607 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
608 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
609 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
610 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
612 attr->syntax = dsdb_syntax_for_attribute(attr);
614 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
617 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
618 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
624 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
625 struct ldb_message *msg,
627 struct dsdb_class *obj)
631 GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
632 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
633 GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
634 if (!schema->prefixmap || schema->prefixmap->length == 0) {
635 /* set an invalid value */
636 obj->governsID_id = 0xFFFFFFFF;
638 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
641 if (!W_ERROR_IS_OK(status)) {
642 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
643 __location__, obj->lDAPDisplayName, obj->governsID_oid,
644 win_errstr(status)));
648 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
650 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
651 GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
652 GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
654 GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
656 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
657 GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
659 GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
660 GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
661 GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
662 GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
664 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
665 GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
667 GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
669 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
670 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
672 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
673 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
674 GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
675 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
676 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
677 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
678 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
683 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
686 Create a DSDB schema from the ldb results provided. This is called
687 directly when the schema is provisioned from an on-disk LDIF file, or
688 from dsdb_schema_from_schema_dn in schema_fsmo
691 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
692 struct smb_iconv_convenience *iconv_convenience,
693 struct ldb_result *schema_res,
694 struct ldb_result *attrs_res, struct ldb_result *objectclass_res,
695 struct dsdb_schema **schema_out,
700 const struct ldb_val *prefix_val;
701 const struct ldb_val *info_val;
702 struct ldb_val info_val_default;
703 struct dsdb_schema *schema;
705 schema = dsdb_new_schema(mem_ctx, iconv_convenience);
707 dsdb_oom(error_string, mem_ctx);
708 return LDB_ERR_OPERATIONS_ERROR;
711 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
713 *error_string = talloc_asprintf(mem_ctx,
714 "schema_fsmo_init: no prefixMap attribute found");
715 DEBUG(0,(__location__ ": %s\n", *error_string));
716 return LDB_ERR_CONSTRAINT_VIOLATION;
718 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
720 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
721 if (!info_val_default.data) {
722 dsdb_oom(error_string, mem_ctx);
723 return LDB_ERR_OPERATIONS_ERROR;
725 info_val = &info_val_default;
728 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
729 if (!W_ERROR_IS_OK(status)) {
730 *error_string = talloc_asprintf(mem_ctx,
731 "schema_fsmo_init: failed to load oid mappings: %s",
733 DEBUG(0,(__location__ ": %s\n", *error_string));
734 return LDB_ERR_CONSTRAINT_VIOLATION;
737 for (i=0; i < attrs_res->count; i++) {
738 struct dsdb_attribute *sa;
740 sa = talloc_zero(schema, struct dsdb_attribute);
742 dsdb_oom(error_string, mem_ctx);
743 return LDB_ERR_OPERATIONS_ERROR;
746 status = dsdb_attribute_from_ldb(ldb, schema, attrs_res->msgs[i], sa, sa);
747 if (!W_ERROR_IS_OK(status)) {
748 *error_string = talloc_asprintf(mem_ctx,
749 "schema_fsmo_init: failed to load attribute definition: %s:%s",
750 ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
752 DEBUG(0,(__location__ ": %s\n", *error_string));
753 return LDB_ERR_CONSTRAINT_VIOLATION;
756 DLIST_ADD(schema->attributes, sa);
759 for (i=0; i < objectclass_res->count; i++) {
760 struct dsdb_class *sc;
762 sc = talloc_zero(schema, struct dsdb_class);
764 dsdb_oom(error_string, mem_ctx);
765 return LDB_ERR_OPERATIONS_ERROR;
768 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
769 if (!W_ERROR_IS_OK(status)) {
770 *error_string = talloc_asprintf(mem_ctx,
771 "schema_fsmo_init: failed to load class definition: %s:%s",
772 ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
774 DEBUG(0,(__location__ ": %s\n", *error_string));
775 return LDB_ERR_CONSTRAINT_VIOLATION;
778 DLIST_ADD(schema->classes, sc);
781 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
782 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
783 schema->fsmo.we_are_master = true;
785 schema->fsmo.we_are_master = false;
788 DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
789 (schema->fsmo.we_are_master?"yes":"no")));
791 *schema_out = schema;
796 static const struct {
799 } name_mappings[] = {
801 { "name", "1.2.840.113556.1.4.1" },
802 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
803 { "attributeID", "1.2.840.113556.1.2.30" },
804 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
805 { "mAPIID", "1.2.840.113556.1.2.49" },
806 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
807 { "searchFlags", "1.2.840.113556.1.2.334" },
808 { "systemFlags", "1.2.840.113556.1.4.375" },
809 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
810 { "linkID", "1.2.840.113556.1.2.50" },
811 { "attributeSyntax", "1.2.840.113556.1.2.32" },
812 { "oMSyntax", "1.2.840.113556.1.2.231" },
813 { "oMObjectClass", "1.2.840.113556.1.2.218" },
814 { "isSingleValued", "1.2.840.113556.1.2.33" },
815 { "rangeLower", "1.2.840.113556.1.2.34" },
816 { "rangeUpper", "1.2.840.113556.1.2.35" },
817 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
818 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
819 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
820 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
821 { "adminDisplayName", "1.2.840.113556.1.2.194" },
822 { "adminDescription", "1.2.840.113556.1.2.226" },
823 { "classDisplayName", "1.2.840.113556.1.4.610" },
824 { "isEphemeral", "1.2.840.113556.1.4.1212" },
825 { "isDefunct", "1.2.840.113556.1.4.661" },
826 { "systemOnly", "1.2.840.113556.1.4.170" },
827 { "governsID", "1.2.840.113556.1.2.22" },
828 { "objectClassCategory", "1.2.840.113556.1.2.370" },
829 { "rDNAttID", "1.2.840.113556.1.2.26" },
830 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
831 { "subClassOf", "1.2.840.113556.1.2.21" },
832 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
833 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
834 { "systemMustContain", "1.2.840.113556.1.4.197" },
835 { "systemMayContain", "1.2.840.113556.1.4.196" },
836 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
837 { "possSuperiors", "1.2.840.113556.1.2.8" },
838 { "mustContain", "1.2.840.113556.1.2.24" },
839 { "mayContain", "1.2.840.113556.1.2.25" },
840 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
841 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
844 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
845 struct drsuapi_DsReplicaObject *obj,
851 const char *oid = NULL;
853 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
854 if (strcmp(name_mappings[i].name, name) != 0) continue;
856 oid = name_mappings[i].oid;
864 status = dsdb_schema_pfm_make_attid(schema->prefixmap, oid, &attid);
865 if (!W_ERROR_IS_OK(status)) {
869 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
870 if (obj->attribute_ctr.attributes[i].attid != attid) continue;
873 return &obj->attribute_ctr.attributes[i];
879 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
880 struct drsuapi_DsReplicaAttribute *_a; \
881 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
882 if (strict && !_a) { \
883 d_printf("%s: %s == NULL\n", __location__, attr); \
884 return WERR_INVALID_PARAM; \
886 if (strict && _a->value_ctr.num_values != 1) { \
887 d_printf("%s: %s num_values == %u\n", __location__, attr, \
888 _a->value_ctr.num_values); \
889 return WERR_INVALID_PARAM; \
891 if (_a && _a->value_ctr.num_values >= 1) { \
893 if (!convert_string_talloc_convenience(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
894 _a->value_ctr.values[0].blob->data, \
895 _a->value_ctr.values[0].blob->length, \
896 (void **)discard_const(&(p)->elem), &_ret, false)) { \
897 DEBUG(0,("%s: invalid data!\n", attr)); \
899 _a->value_ctr.values[0].blob->data, \
900 _a->value_ctr.values[0].blob->length); \
901 return WERR_FOOBAR; \
908 #define GET_UINT32_LIST_DS(s, r, attr, mem_ctx, p, elem) do { \
910 struct drsuapi_DsReplicaAttribute *_a; \
911 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
912 (p)->elem = _a ? talloc_array(mem_ctx, uint32_t, _a->value_ctr.num_values + 1) : NULL; \
913 for (list_counter=0; \
914 _a && list_counter < _a->value_ctr.num_values; \
916 if (_a->value_ctr.values[list_counter].blob->length != 4) { \
917 return WERR_INVALID_PARAM; \
919 (p)->elem[list_counter] = IVAL(_a->value_ctr.values[list_counter].blob->data, 0); \
921 if (_a) (p)->elem[list_counter] = 0; \
924 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
925 struct drsuapi_DsReplicaAttribute *_a; \
926 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
927 if (strict && !_a) { \
928 d_printf("%s: %s == NULL\n", __location__, attr); \
929 return WERR_INVALID_PARAM; \
931 if (strict && _a->value_ctr.num_values != 1) { \
932 d_printf("%s: %s num_values == %u\n", __location__, attr, \
933 _a->value_ctr.num_values); \
934 return WERR_INVALID_PARAM; \
936 if (strict && !_a->value_ctr.values[0].blob) { \
937 d_printf("%s: %s data == NULL\n", __location__, attr); \
938 return WERR_INVALID_PARAM; \
940 if (_a && _a->value_ctr.num_values >= 1 \
941 && _a->value_ctr.values[0].blob) { \
942 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
943 enum ndr_err_code _ndr_err; \
944 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
945 mem_ctx, s->iconv_convenience, &_id3,\
946 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
947 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
948 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
949 return ntstatus_to_werror(_nt_status); \
951 (p)->elem = _id3.dn; \
957 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
958 struct drsuapi_DsReplicaAttribute *_a; \
959 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
960 if (strict && !_a) { \
961 d_printf("%s: %s == NULL\n", __location__, attr); \
962 return WERR_INVALID_PARAM; \
964 if (strict && _a->value_ctr.num_values != 1) { \
965 d_printf("%s: %s num_values == %u\n", __location__, attr, \
966 (unsigned int)_a->value_ctr.num_values); \
967 return WERR_INVALID_PARAM; \
969 if (strict && !_a->value_ctr.values[0].blob) { \
970 d_printf("%s: %s data == NULL\n", __location__, attr); \
971 return WERR_INVALID_PARAM; \
973 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
974 d_printf("%s: %s length == %u\n", __location__, attr, \
975 (unsigned int)_a->value_ctr.values[0].blob->length); \
976 return WERR_INVALID_PARAM; \
978 if (_a && _a->value_ctr.num_values >= 1 \
979 && _a->value_ctr.values[0].blob \
980 && _a->value_ctr.values[0].blob->length == 4) { \
981 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
987 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
988 struct drsuapi_DsReplicaAttribute *_a; \
989 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
990 if (_a && _a->value_ctr.num_values >= 1 \
991 && _a->value_ctr.values[0].blob \
992 && _a->value_ctr.values[0].blob->length == 4) { \
993 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
999 #define GET_UINT32_PTR_DS(s, r, attr, p, elem) do { \
1000 struct drsuapi_DsReplicaAttribute *_a; \
1001 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1002 if (_a && _a->value_ctr.num_values >= 1 \
1003 && _a->value_ctr.values[0].blob \
1004 && _a->value_ctr.values[0].blob->length == 4) { \
1005 (p)->elem = talloc(mem_ctx, uint32_t); \
1007 d_printf("%s: talloc failed for %s\n", __location__, attr); \
1008 return WERR_NOMEM; \
1010 *(p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1016 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1017 struct drsuapi_DsReplicaAttribute *_a; \
1018 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1019 if (_a && _a->value_ctr.num_values >= 1 \
1020 && _a->value_ctr.values[0].blob \
1021 && _a->value_ctr.values[0].blob->length == 16) { \
1022 enum ndr_err_code _ndr_err; \
1023 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1024 mem_ctx, s->iconv_convenience, &(p)->elem, \
1025 (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1026 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1027 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1028 return ntstatus_to_werror(_nt_status); \
1031 ZERO_STRUCT((p)->elem);\
1035 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1036 struct drsuapi_DsReplicaAttribute *_a; \
1037 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1038 if (_a && _a->value_ctr.num_values >= 1 \
1039 && _a->value_ctr.values[0].blob) { \
1040 (p)->elem = *_a->value_ctr.values[0].blob;\
1041 talloc_steal(mem_ctx, (p)->elem.data); \
1043 ZERO_STRUCT((p)->elem);\
1047 WERROR dsdb_attribute_from_drsuapi(struct ldb_context *ldb,
1048 struct dsdb_schema *schema,
1049 struct drsuapi_DsReplicaObject *r,
1050 TALLOC_CTX *mem_ctx,
1051 struct dsdb_attribute *attr)
1055 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1056 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1057 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1058 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attr->attributeID_id,
1059 mem_ctx, &attr->attributeID_oid);
1060 if (!W_ERROR_IS_OK(status)) {
1061 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1062 __location__, attr->lDAPDisplayName, attr->attributeID_id,
1063 win_errstr(status)));
1066 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1067 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1069 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1071 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1072 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1073 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1074 GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1076 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1077 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attr->attributeSyntax_id,
1078 mem_ctx, &attr->attributeSyntax_oid);
1079 if (!W_ERROR_IS_OK(status)) {
1080 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1081 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1082 win_errstr(status)));
1085 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1086 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1088 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1089 GET_UINT32_PTR_DS(schema, r, "rangeLower", attr, rangeLower);
1090 GET_UINT32_PTR_DS(schema, r, "rangeUpper", attr, rangeUpper);
1091 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1093 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1094 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1096 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1097 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1098 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1099 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1100 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1101 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1102 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1104 attr->syntax = dsdb_syntax_for_attribute(attr);
1105 if (!attr->syntax) {
1106 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1109 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
1110 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1116 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1117 struct drsuapi_DsReplicaObject *r,
1118 TALLOC_CTX *mem_ctx,
1119 struct dsdb_class *obj)
1123 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1124 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1125 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1126 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, obj->governsID_id,
1127 mem_ctx, &obj->governsID_oid);
1128 if (!W_ERROR_IS_OK(status)) {
1129 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1130 __location__, obj->lDAPDisplayName, obj->governsID_id,
1131 win_errstr(status)));
1134 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1136 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1137 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1138 GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1140 GET_UINT32_DS(schema, r, "subClassOf", obj, subClassOf_id);
1142 GET_UINT32_LIST_DS(schema, r, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass_ids);
1143 GET_UINT32_LIST_DS(schema, r, "auxiliaryClass", mem_ctx, obj, auxiliaryClass_ids);
1145 GET_UINT32_LIST_DS(schema, r, "systemMustContain", mem_ctx, obj, systemMustContain_ids);
1146 GET_UINT32_LIST_DS(schema, r, "systemMayContain", mem_ctx, obj, systemMayContain_ids);
1147 GET_UINT32_LIST_DS(schema, r, "mustContain", mem_ctx, obj, mustContain_ids);
1148 GET_UINT32_LIST_DS(schema, r, "mayContain", mem_ctx, obj, mayContain_ids);
1150 GET_UINT32_LIST_DS(schema, r, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors_ids);
1151 GET_UINT32_LIST_DS(schema, r, "possSuperiors", mem_ctx, obj, possSuperiors_ids);
1153 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1155 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1156 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1158 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1159 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1160 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1161 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1162 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1163 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1164 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);