2 Unix SMB/CIFS mplementation.
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
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"
32 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
34 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
39 schema->iconv_convenience = iconv_convenience;
44 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
48 schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
49 W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
51 for (i=0, j=0; i < ctr->num_mappings; i++) {
52 if (ctr->mappings[i].oid.oid == NULL) {
53 return WERR_INVALID_PARAM;
56 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
57 if (ctr->mappings[i].id_prefix != 0) {
58 return WERR_INVALID_PARAM;
61 /* the magic value should be in the last array member */
62 if (i != (ctr->num_mappings - 1)) {
63 return WERR_INVALID_PARAM;
66 if (ctr->mappings[i].oid.__ndr_size != 21) {
67 return WERR_INVALID_PARAM;
70 schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
71 W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
73 /* the last array member should contain the magic value not a oid */
74 if (i == (ctr->num_mappings - 1)) {
75 return WERR_INVALID_PARAM;
78 schema->prefixes[j].id = ctr->mappings[i].id_prefix<<16;
79 schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
80 ctr->mappings[i].oid.oid);
81 W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
82 schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
87 schema->num_prefixes = j;
91 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
92 const struct ldb_val *prefixMap,
93 const struct ldb_val *schemaInfo)
96 enum ndr_err_code ndr_err;
97 struct prefixMapBlob pfm;
100 TALLOC_CTX *mem_ctx = talloc_new(schema);
101 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
103 ndr_err = ndr_pull_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
104 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
105 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
106 talloc_free(mem_ctx);
107 return ntstatus_to_werror(nt_status);
110 if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
111 talloc_free(mem_ctx);
115 if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
116 talloc_free(mem_ctx);
120 /* append the schema info as last element */
121 pfm.ctr.dsdb.num_mappings++;
122 pfm.ctr.dsdb.mappings = talloc_realloc(mem_ctx, pfm.ctr.dsdb.mappings,
123 struct drsuapi_DsReplicaOIDMapping,
124 pfm.ctr.dsdb.num_mappings);
125 W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
127 schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
128 W_ERROR_HAVE_NO_MEMORY(schema_info);
130 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix = 0;
131 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size = schemaInfo->length;
132 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid = schema_info;
134 /* call the drsuapi version */
135 status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
136 talloc_free(mem_ctx);
138 W_ERROR_NOT_OK_RETURN(status);
143 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
144 bool include_schema_info,
146 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
148 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
151 ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
152 W_ERROR_HAVE_NO_MEMORY(ctr);
154 ctr->num_mappings = schema->num_prefixes;
155 if (include_schema_info) ctr->num_mappings++;
156 ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
157 W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
159 for (i=0; i < schema->num_prefixes; i++) {
160 ctr->mappings[i].id_prefix = schema->prefixes[i].id>>16;
161 ctr->mappings[i].oid.oid = talloc_strndup(ctr->mappings,
162 schema->prefixes[i].oid,
163 schema->prefixes[i].oid_len - 1);
164 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
167 if (include_schema_info) {
168 ctr->mappings[i].id_prefix = 0;
169 ctr->mappings[i].oid.oid = talloc_strdup(ctr->mappings,
170 schema->schema_info);
171 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
178 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
180 struct ldb_val *prefixMap,
181 struct ldb_val *schemaInfo)
184 enum ndr_err_code ndr_err;
185 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
186 struct prefixMapBlob pfm;
188 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
189 W_ERROR_NOT_OK_RETURN(status);
191 pfm.version = PREFIX_MAP_VERSION_DSDB;
195 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
197 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
198 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
199 return ntstatus_to_werror(nt_status);
202 *schemaInfo = strhex_to_data_blob(schema->schema_info);
203 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
204 talloc_steal(mem_ctx, schemaInfo->data);
209 WERROR dsdb_verify_oid_mappings_drsuapi(const struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
213 for (i=0; i < ctr->num_mappings; i++) {
214 if (ctr->mappings[i].oid.oid == NULL) {
215 return WERR_INVALID_PARAM;
218 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
219 if (ctr->mappings[i].id_prefix != 0) {
220 return WERR_INVALID_PARAM;
223 /* the magic value should be in the last array member */
224 if (i != (ctr->num_mappings - 1)) {
225 return WERR_INVALID_PARAM;
228 if (ctr->mappings[i].oid.__ndr_size != 21) {
229 return WERR_INVALID_PARAM;
232 if (strcasecmp(schema->schema_info, ctr->mappings[i].oid.oid) != 0) {
233 return WERR_DS_DRA_SCHEMA_MISMATCH;
236 /* the last array member should contain the magic value not a oid */
237 if (i == (ctr->num_mappings - 1)) {
238 return WERR_INVALID_PARAM;
241 for (j=0; j < schema->num_prefixes; j++) {
243 if (schema->prefixes[j].id != (ctr->mappings[i].id_prefix<<16)) {
247 oid_len = strlen(ctr->mappings[i].oid.oid);
249 if (oid_len != (schema->prefixes[j].oid_len - 1)) {
250 return WERR_DS_DRA_SCHEMA_MISMATCH;
253 if (strncmp(ctr->mappings[i].oid.oid, schema->prefixes[j].oid, oid_len) != 0) {
254 return WERR_DS_DRA_SCHEMA_MISMATCH;
260 if (j == schema->num_prefixes) {
261 return WERR_DS_DRA_SCHEMA_MISMATCH;
269 WERROR dsdb_map_oid2int(const struct dsdb_schema *schema, const char *in, uint32_t *out)
273 for (i=0; i < schema->num_prefixes; i++) {
278 if (strncmp(schema->prefixes[i].oid, in, schema->prefixes[i].oid_len) != 0) {
282 val_str = in + schema->prefixes[i].oid_len;
286 if (val_str[0] == '\0') {
287 return WERR_INVALID_PARAM;
290 /* two '.' chars are invalid */
291 if (val_str[0] == '.') {
292 return WERR_INVALID_PARAM;
295 val = strtoul(val_str, &end_str, 10);
296 if (end_str[0] == '.' && end_str[1] != '\0') {
298 * if it's a '.' and not the last char
299 * then maybe an other mapping apply
302 } else if (end_str[0] != '\0') {
303 return WERR_INVALID_PARAM;
304 } else if (val > 0xFFFF) {
305 return WERR_INVALID_PARAM;
308 *out = schema->prefixes[i].id | val;
312 return WERR_DS_NO_MSDS_INTID;
315 WERROR dsdb_map_int2oid(const struct dsdb_schema *schema, uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
319 for (i=0; i < schema->num_prefixes; i++) {
321 if (schema->prefixes[i].id != (in & 0xFFFF0000)) {
325 val = talloc_asprintf(mem_ctx, "%s%u",
326 schema->prefixes[i].oid,
328 W_ERROR_HAVE_NO_MEMORY(val);
334 return WERR_DS_NO_MSDS_INTID;
338 * this function is called from within a ldb transaction from the schema_fsmo module
340 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
344 * - (maybe) read the old prefixMap attribute and parse it
346 * - recheck the prefix doesn't exist (because the ldb
347 * has maybe a more uptodate value than schem->prefixes
349 * - calculate a new mapping for the oid prefix of full_oid
350 * - store the new prefixMap attribute
352 * - (maybe) update schema->prefixes
354 * - better find a way to indicate a schema reload,
355 * so that other processes also notice the schema change
357 return WERR_NOT_SUPPORTED;
360 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
361 (p)->elem = samdb_result_string(msg, attr, NULL);\
362 if (strict && (p)->elem == NULL) { \
363 d_printf("%s: %s == NULL\n", __location__, attr); \
364 return WERR_INVALID_PARAM; \
366 talloc_steal(mem_ctx, (p)->elem); \
369 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
370 int get_string_list_counter; \
371 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
372 if (get_string_list_el == NULL) { \
374 d_printf("%s: %s == NULL\n", __location__, attr); \
375 return WERR_INVALID_PARAM; \
381 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
382 for (get_string_list_counter=0; \
383 get_string_list_counter < get_string_list_el->num_values; \
384 get_string_list_counter++) { \
385 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
386 (const char *)get_string_list_el->values[get_string_list_counter].data, \
387 get_string_list_el->values[get_string_list_counter].length); \
388 if (!(p)->elem[get_string_list_counter]) { \
389 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
392 (p)->elem[get_string_list_counter+1] = NULL; \
394 talloc_steal(mem_ctx, (p)->elem); \
397 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
399 str = samdb_result_string(msg, attr, NULL);\
402 d_printf("%s: %s == NULL\n", __location__, attr); \
403 return WERR_INVALID_PARAM; \
407 } else if (strcasecmp("TRUE", str) == 0) { \
409 } else if (strcasecmp("FALSE", str) == 0) { \
412 d_printf("%s: %s == %s\n", __location__, attr, str); \
413 return WERR_INVALID_PARAM; \
417 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
418 (p)->elem = samdb_result_uint(msg, attr, 0);\
421 #define GET_GUID_LDB(msg, attr, p, elem) do { \
422 (p)->elem = samdb_result_guid(msg, attr);\
425 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
426 const struct ldb_val *_val;\
427 _val = ldb_msg_find_ldb_val(msg, attr);\
430 talloc_steal(mem_ctx, (p)->elem.data);\
432 ZERO_STRUCT((p)->elem);\
436 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
437 struct ldb_message *msg,
439 struct dsdb_attribute *attr)
443 GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
444 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
445 GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
446 if (schema->num_prefixes == 0) {
447 /* set an invalid value */
448 attr->attributeID_id = 0xFFFFFFFF;
450 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
451 if (!W_ERROR_IS_OK(status)) {
452 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
453 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
454 win_errstr(status)));
458 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
459 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
461 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
463 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
464 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
465 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
466 GET_UINT32_LDB(msg, "linkID", attr, linkID);
468 GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
469 if (schema->num_prefixes == 0) {
470 /* set an invalid value */
471 attr->attributeSyntax_id = 0xFFFFFFFF;
473 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
474 if (!W_ERROR_IS_OK(status)) {
475 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
476 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
477 win_errstr(status)));
481 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
482 GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
484 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
485 GET_UINT32_LDB(msg, "rangeLower", attr, rangeLower);
486 GET_UINT32_LDB(msg, "rangeUpper", attr, rangeUpper);
487 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
489 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
490 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
492 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
493 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
494 GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
495 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
496 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
497 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
498 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
500 attr->syntax = dsdb_syntax_for_attribute(attr);
502 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
508 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
509 struct ldb_message *msg,
511 struct dsdb_class *obj)
515 GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
516 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
517 GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
518 if (schema->num_prefixes == 0) {
519 /* set an invalid value */
520 obj->governsID_id = 0xFFFFFFFF;
522 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
523 if (!W_ERROR_IS_OK(status)) {
524 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
525 __location__, obj->lDAPDisplayName, obj->governsID_oid,
526 win_errstr(status)));
530 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
532 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
533 GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
534 GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
536 GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
538 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
539 GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
541 GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
542 GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
543 GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
544 GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
546 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
547 GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
548 GET_STRING_LIST_LDB(msg, "possibleInferiors", mem_ctx, obj, possibleInferiors, false);
550 GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
552 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
553 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
555 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
556 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
557 GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
558 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
559 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
560 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
561 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
566 static const struct {
569 } name_mappings[] = {
571 { "name", "1.2.840.113556.1.4.1" },
572 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
573 { "attributeID", "1.2.840.113556.1.2.30" },
574 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
575 { "mAPIID", "1.2.840.113556.1.2.49" },
576 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
577 { "searchFlags", "1.2.840.113556.1.2.334" },
578 { "systemFlags", "1.2.840.113556.1.4.375" },
579 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
580 { "linkID", "1.2.840.113556.1.2.50" },
581 { "attributeSyntax", "1.2.840.113556.1.2.32" },
582 { "oMSyntax", "1.2.840.113556.1.2.231" },
583 { "oMObjectClass", "1.2.840.113556.1.2.218" },
584 { "isSingleValued", "1.2.840.113556.1.2.33" },
585 { "rangeLower", "1.2.840.113556.1.2.34" },
586 { "rangeUpper", "1.2.840.113556.1.2.35" },
587 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
588 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
589 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
590 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
591 { "adminDisplayName", "1.2.840.113556.1.2.194" },
592 { "adminDescription", "1.2.840.113556.1.2.226" },
593 { "classDisplayName", "1.2.840.113556.1.4.610" },
594 { "isEphemeral", "1.2.840.113556.1.4.1212" },
595 { "isDefunct", "1.2.840.113556.1.4.661" },
596 { "systemOnly", "1.2.840.113556.1.4.170" },
597 { "governsID", "1.2.840.113556.1.2.22" },
598 { "objectClassCategory", "1.2.840.113556.1.2.370" },
599 { "rDNAttID", "1.2.840.113556.1.2.26" },
600 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
601 { "subClassOf", "1.2.840.113556.1.2.21" },
602 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
603 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
604 { "systemMustContain", "1.2.840.113556.1.4.197" },
605 { "systemMayContain", "1.2.840.113556.1.4.196" },
606 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
607 { "possSuperiors", "1.2.840.113556.1.2.8" },
608 { "mustContain", "1.2.840.113556.1.2.24" },
609 { "mayContain", "1.2.840.113556.1.2.25" },
610 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
611 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
614 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
615 struct drsuapi_DsReplicaObject *obj,
621 const char *oid = NULL;
623 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
624 if (strcmp(name_mappings[i].name, name) != 0) continue;
626 oid = name_mappings[i].oid;
634 status = dsdb_map_oid2int(schema, oid, &id);
635 if (!W_ERROR_IS_OK(status)) {
639 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
640 if (obj->attribute_ctr.attributes[i].attid != id) continue;
643 return &obj->attribute_ctr.attributes[i];
649 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
650 struct drsuapi_DsReplicaAttribute *_a; \
651 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
652 if (strict && !_a) { \
653 d_printf("%s: %s == NULL\n", __location__, attr); \
654 return WERR_INVALID_PARAM; \
656 if (strict && _a->value_ctr.num_values != 1) { \
657 d_printf("%s: %s num_values == %u\n", __location__, attr, \
658 _a->value_ctr.num_values); \
659 return WERR_INVALID_PARAM; \
661 if (_a && _a->value_ctr.num_values >= 1) { \
663 _ret = convert_string_talloc(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
664 _a->value_ctr.values[0].blob->data, \
665 _a->value_ctr.values[0].blob->length, \
666 (void **)discard_const(&(p)->elem)); \
668 DEBUG(0,("%s: invalid data!\n", attr)); \
670 _a->value_ctr.values[0].blob->data, \
671 _a->value_ctr.values[0].blob->length); \
672 return WERR_FOOBAR; \
679 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
680 struct drsuapi_DsReplicaAttribute *_a; \
681 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
682 if (strict && !_a) { \
683 d_printf("%s: %s == NULL\n", __location__, attr); \
684 return WERR_INVALID_PARAM; \
686 if (strict && _a->value_ctr.num_values != 1) { \
687 d_printf("%s: %s num_values == %u\n", __location__, attr, \
688 _a->value_ctr.num_values); \
689 return WERR_INVALID_PARAM; \
691 if (strict && !_a->value_ctr.values[0].blob) { \
692 d_printf("%s: %s data == NULL\n", __location__, attr); \
693 return WERR_INVALID_PARAM; \
695 if (_a && _a->value_ctr.num_values >= 1 \
696 && _a->value_ctr.values[0].blob) { \
697 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
698 enum ndr_err_code _ndr_err; \
699 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
700 mem_ctx, s->iconv_convenience, &_id3,\
701 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
702 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
703 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
704 return ntstatus_to_werror(_nt_status); \
706 (p)->elem = _id3.dn; \
712 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
713 struct drsuapi_DsReplicaAttribute *_a; \
714 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
715 if (strict && !_a) { \
716 d_printf("%s: %s == NULL\n", __location__, attr); \
717 return WERR_INVALID_PARAM; \
719 if (strict && _a->value_ctr.num_values != 1) { \
720 d_printf("%s: %s num_values == %u\n", __location__, attr, \
721 (unsigned int)_a->value_ctr.num_values); \
722 return WERR_INVALID_PARAM; \
724 if (strict && !_a->value_ctr.values[0].blob) { \
725 d_printf("%s: %s data == NULL\n", __location__, attr); \
726 return WERR_INVALID_PARAM; \
728 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
729 d_printf("%s: %s length == %u\n", __location__, attr, \
730 (unsigned int)_a->value_ctr.values[0].blob->length); \
731 return WERR_INVALID_PARAM; \
733 if (_a && _a->value_ctr.num_values >= 1 \
734 && _a->value_ctr.values[0].blob \
735 && _a->value_ctr.values[0].blob->length == 4) { \
736 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
742 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
743 struct drsuapi_DsReplicaAttribute *_a; \
744 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
745 if (_a && _a->value_ctr.num_values >= 1 \
746 && _a->value_ctr.values[0].blob \
747 && _a->value_ctr.values[0].blob->length == 4) { \
748 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
754 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
755 struct drsuapi_DsReplicaAttribute *_a; \
756 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
757 if (_a && _a->value_ctr.num_values >= 1 \
758 && _a->value_ctr.values[0].blob \
759 && _a->value_ctr.values[0].blob->length == 16) { \
760 enum ndr_err_code _ndr_err; \
761 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
762 mem_ctx, s->iconv_convenience, &(p)->elem, \
763 (ndr_pull_flags_fn_t)ndr_pull_GUID); \
764 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
765 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
766 return ntstatus_to_werror(_nt_status); \
769 ZERO_STRUCT((p)->elem);\
773 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
774 struct drsuapi_DsReplicaAttribute *_a; \
775 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
776 if (_a && _a->value_ctr.num_values >= 1 \
777 && _a->value_ctr.values[0].blob) { \
778 (p)->elem = *_a->value_ctr.values[0].blob;\
779 talloc_steal(mem_ctx, (p)->elem.data); \
781 ZERO_STRUCT((p)->elem);\
785 WERROR dsdb_attribute_from_drsuapi(struct dsdb_schema *schema,
786 struct drsuapi_DsReplicaObject *r,
788 struct dsdb_attribute *attr)
792 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
793 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
794 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
795 status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
796 if (!W_ERROR_IS_OK(status)) {
797 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
798 __location__, attr->lDAPDisplayName, attr->attributeID_id,
799 win_errstr(status)));
802 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
803 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
805 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
807 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
808 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
809 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
810 GET_UINT32_DS(schema, r, "linkID", attr, linkID);
812 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
813 status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
814 if (!W_ERROR_IS_OK(status)) {
815 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
816 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
817 win_errstr(status)));
820 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
821 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
823 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
824 GET_UINT32_DS(schema, r, "rangeLower", attr, rangeLower);
825 GET_UINT32_DS(schema, r, "rangeUpper", attr, rangeUpper);
826 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
828 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
829 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
831 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
832 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
833 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
834 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
835 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
836 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
837 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
839 attr->syntax = dsdb_syntax_for_attribute(attr);
841 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
847 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
848 struct drsuapi_DsReplicaObject *r,
850 struct dsdb_class *obj)
854 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
855 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
856 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
857 status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
858 if (!W_ERROR_IS_OK(status)) {
859 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
860 __location__, obj->lDAPDisplayName, obj->governsID_id,
861 win_errstr(status)));
864 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
866 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
867 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
868 GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
870 GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, true);
872 obj->systemAuxiliaryClass = NULL;
873 obj->systemPossSuperiors = NULL;
874 obj->systemMustContain = NULL;
875 obj->systemMayContain = NULL;
877 obj->auxiliaryClass = NULL;
878 obj->possSuperiors = NULL;
879 obj->mustContain = NULL;
880 obj->mayContain = NULL;
882 obj->possibleInferiors = NULL;
884 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
886 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
887 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
889 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
890 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
891 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
892 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
893 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
894 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
895 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
900 const struct dsdb_attribute *dsdb_attribute_by_attributeID_id(const struct dsdb_schema *schema,
903 struct dsdb_attribute *cur;
906 * 0xFFFFFFFF is used as value when no mapping table is available,
907 * so don't try to match with it
909 if (id == 0xFFFFFFFF) return NULL;
911 /* TODO: add binary search */
912 for (cur = schema->attributes; cur; cur = cur->next) {
913 if (cur->attributeID_id != id) continue;
921 const struct dsdb_attribute *dsdb_attribute_by_attributeID_oid(const struct dsdb_schema *schema,
924 struct dsdb_attribute *cur;
926 if (!oid) return NULL;
928 /* TODO: add binary search */
929 for (cur = schema->attributes; cur; cur = cur->next) {
930 if (strcmp(cur->attributeID_oid, oid) != 0) continue;
938 const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName(const struct dsdb_schema *schema,
941 struct dsdb_attribute *cur;
943 if (!name) return NULL;
945 /* TODO: add binary search */
946 for (cur = schema->attributes; cur; cur = cur->next) {
947 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
955 const struct dsdb_attribute *dsdb_attribute_by_linkID(const struct dsdb_schema *schema,
958 struct dsdb_attribute *cur;
960 /* TODO: add binary search */
961 for (cur = schema->attributes; cur; cur = cur->next) {
962 if (cur->linkID != linkID) continue;
970 const struct dsdb_class *dsdb_class_by_governsID_id(const struct dsdb_schema *schema,
973 struct dsdb_class *cur;
976 * 0xFFFFFFFF is used as value when no mapping table is available,
977 * so don't try to match with it
979 if (id == 0xFFFFFFFF) return NULL;
981 /* TODO: add binary search */
982 for (cur = schema->classes; cur; cur = cur->next) {
983 if (cur->governsID_id != id) continue;
991 const struct dsdb_class *dsdb_class_by_governsID_oid(const struct dsdb_schema *schema,
994 struct dsdb_class *cur;
996 if (!oid) return NULL;
998 /* TODO: add binary search */
999 for (cur = schema->classes; cur; cur = cur->next) {
1000 if (strcmp(cur->governsID_oid, oid) != 0) continue;
1008 const struct dsdb_class *dsdb_class_by_lDAPDisplayName(const struct dsdb_schema *schema,
1011 struct dsdb_class *cur;
1013 if (!name) return NULL;
1015 /* TODO: add binary search */
1016 for (cur = schema->classes; cur; cur = cur->next) {
1017 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
1025 const struct dsdb_class *dsdb_class_by_cn(const struct dsdb_schema *schema,
1028 struct dsdb_class *cur;
1030 if (!cn) return NULL;
1032 /* TODO: add binary search */
1033 for (cur = schema->classes; cur; cur = cur->next) {
1034 if (strcasecmp(cur->cn, cn) != 0) continue;
1042 const char *dsdb_lDAPDisplayName_by_id(const struct dsdb_schema *schema,
1045 const struct dsdb_attribute *a;
1046 const struct dsdb_class *c;
1048 /* TODO: add binary search */
1049 a = dsdb_attribute_by_attributeID_id(schema, id);
1051 return a->lDAPDisplayName;
1054 c = dsdb_class_by_governsID_id(schema, id);
1056 return c->lDAPDisplayName;
1062 WERROR dsdb_linked_attribute_lDAPDisplayName_list(const struct dsdb_schema *schema, TALLOC_CTX *mem_ctx, const char ***attr_list_ret)
1064 const char **attr_list = NULL;
1065 struct dsdb_attribute *cur;
1067 for (cur = schema->attributes; cur; cur = cur->next) {
1068 if (cur->linkID == 0) continue;
1070 attr_list = talloc_realloc(mem_ctx, attr_list, const char *, i+2);
1074 attr_list[i] = cur->lDAPDisplayName;
1077 attr_list[i] = NULL;
1078 *attr_list_ret = attr_list;
1083 * Attach the schema to an opaque pointer on the ldb, so ldb modules
1087 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
1091 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
1092 if (ret != LDB_SUCCESS) {
1096 talloc_steal(ldb, schema);
1102 * Global variable to hold one copy of the schema, used to avoid memory bloat
1104 static struct dsdb_schema *global_schema;
1107 * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
1109 int dsdb_set_global_schema(struct ldb_context *ldb)
1112 if (!global_schema) {
1115 ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
1116 if (ret != LDB_SUCCESS) {
1124 * Find the schema object for this ldb
1127 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
1130 struct dsdb_schema *schema;
1132 /* see if we have a cached copy */
1133 p = ldb_get_opaque(ldb, "dsdb_schema");
1138 schema = talloc_get_type(p, struct dsdb_schema);
1147 * Make the schema found on this ldb the 'global' schema
1150 void dsdb_make_schema_global(struct ldb_context *ldb)
1152 struct dsdb_schema *schema = dsdb_get_schema(ldb);
1157 talloc_steal(talloc_autofree_context(), schema);
1158 global_schema = schema;
1160 dsdb_set_global_schema(ldb);
1165 * Rather than read a schema from the LDB itself, read it from an ldif
1166 * file. This allows schema to be loaded and used while adding the
1167 * schema itself to the directory.
1170 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
1172 struct ldb_ldif *ldif;
1173 struct ldb_message *msg;
1174 TALLOC_CTX *mem_ctx;
1177 struct dsdb_schema *schema;
1178 const struct ldb_val *prefix_val;
1179 const struct ldb_val *info_val;
1180 struct ldb_val info_val_default;
1182 mem_ctx = talloc_new(ldb);
1187 schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
1189 schema->fsmo.we_are_master = true;
1190 schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
1191 if (!schema->fsmo.master_dn) {
1196 * load the prefixMap attribute from pf
1198 ldif = ldb_ldif_read_string(ldb, &pf);
1200 status = WERR_INVALID_PARAM;
1203 talloc_steal(mem_ctx, ldif);
1205 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1209 talloc_steal(mem_ctx, msg);
1212 prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
1214 status = WERR_INVALID_PARAM;
1218 info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
1220 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
1221 if (!info_val_default.data) {
1224 talloc_steal(mem_ctx, info_val_default.data);
1225 info_val = &info_val_default;
1228 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1229 if (!W_ERROR_IS_OK(status)) {
1234 * load the attribute and class definitions outof df
1236 while ((ldif = ldb_ldif_read_string(ldb, &df))) {
1240 talloc_steal(mem_ctx, ldif);
1242 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1247 talloc_steal(mem_ctx, msg);
1250 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
1251 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
1254 struct dsdb_attribute *sa;
1256 sa = talloc_zero(schema, struct dsdb_attribute);
1261 status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
1262 if (!W_ERROR_IS_OK(status)) {
1266 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
1268 struct dsdb_class *sc;
1270 sc = talloc_zero(schema, struct dsdb_class);
1275 status = dsdb_class_from_ldb(schema, msg, sc, sc);
1276 if (!W_ERROR_IS_OK(status)) {
1280 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
1284 ret = dsdb_set_schema(ldb, schema);
1285 if (ret != LDB_SUCCESS) {
1286 status = WERR_FOOBAR;
1293 status = WERR_NOMEM;
1296 talloc_free(mem_ctx);