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 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
36 schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
37 W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
39 for (i=0, j=0; i < ctr->num_mappings; i++) {
40 if (ctr->mappings[i].oid.oid == NULL) {
41 return WERR_INVALID_PARAM;
44 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
45 if (ctr->mappings[i].id_prefix != 0) {
46 return WERR_INVALID_PARAM;
49 /* the magic value should be in the last array member */
50 if (i != (ctr->num_mappings - 1)) {
51 return WERR_INVALID_PARAM;
54 if (ctr->mappings[i].oid.__ndr_size != 21) {
55 return WERR_INVALID_PARAM;
58 schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
59 W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
61 /* the last array member should contain the magic value not a oid */
62 if (i == (ctr->num_mappings - 1)) {
63 return WERR_INVALID_PARAM;
66 schema->prefixes[j].id = ctr->mappings[i].id_prefix<<16;
67 schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
68 ctr->mappings[i].oid.oid);
69 W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
70 schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
75 schema->num_prefixes = j;
79 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
80 const struct ldb_val *prefixMap,
81 const struct ldb_val *schemaInfo)
84 enum ndr_err_code ndr_err;
85 struct prefixMapBlob pfm;
88 ndr_err = ndr_pull_struct_blob(prefixMap, schema, &pfm,
89 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
90 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
91 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
92 return ntstatus_to_werror(nt_status);
95 if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
99 if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
103 /* append the schema info as last element */
104 pfm.ctr.dsdb.num_mappings++;
105 pfm.ctr.dsdb.mappings = talloc_realloc(schema, pfm.ctr.dsdb.mappings,
106 struct drsuapi_DsReplicaOIDMapping,
107 pfm.ctr.dsdb.num_mappings);
108 W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
110 schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
111 W_ERROR_HAVE_NO_MEMORY(schema_info);
113 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix = 0;
114 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size = schemaInfo->length;
115 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid = schema_info;
117 /* call the drsuapi version */
118 status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
119 talloc_free(pfm.ctr.dsdb.mappings);
120 W_ERROR_NOT_OK_RETURN(status);
125 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
126 bool include_schema_info,
128 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
130 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
133 ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
134 W_ERROR_HAVE_NO_MEMORY(ctr);
136 ctr->num_mappings = schema->num_prefixes;
137 if (include_schema_info) ctr->num_mappings++;
138 ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
139 W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
141 for (i=0; i < schema->num_prefixes; i++) {
142 ctr->mappings[i].id_prefix = schema->prefixes[i].id>>16;
143 ctr->mappings[i].oid.oid = talloc_strndup(ctr->mappings,
144 schema->prefixes[i].oid,
145 schema->prefixes[i].oid_len - 1);
146 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
149 if (include_schema_info) {
150 ctr->mappings[i].id_prefix = 0;
151 ctr->mappings[i].oid.oid = talloc_strdup(ctr->mappings,
152 schema->schema_info);
153 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
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, lp_iconv_convenience(global_loadparm), &pfm,
178 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
180 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
181 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
182 return ntstatus_to_werror(nt_status);
185 *schemaInfo = strhex_to_data_blob(schema->schema_info);
186 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
187 talloc_steal(mem_ctx, schemaInfo->data);
192 WERROR dsdb_verify_oid_mappings_drsuapi(const struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
196 for (i=0; i < ctr->num_mappings; i++) {
197 if (ctr->mappings[i].oid.oid == NULL) {
198 return WERR_INVALID_PARAM;
201 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
202 if (ctr->mappings[i].id_prefix != 0) {
203 return WERR_INVALID_PARAM;
206 /* the magic value should be in the last array member */
207 if (i != (ctr->num_mappings - 1)) {
208 return WERR_INVALID_PARAM;
211 if (ctr->mappings[i].oid.__ndr_size != 21) {
212 return WERR_INVALID_PARAM;
215 if (strcasecmp(schema->schema_info, ctr->mappings[i].oid.oid) != 0) {
216 return WERR_DS_DRA_SCHEMA_MISMATCH;
219 /* the last array member should contain the magic value not a oid */
220 if (i == (ctr->num_mappings - 1)) {
221 return WERR_INVALID_PARAM;
224 for (j=0; j < schema->num_prefixes; j++) {
226 if (schema->prefixes[j].id != (ctr->mappings[i].id_prefix<<16)) {
230 oid_len = strlen(ctr->mappings[i].oid.oid);
232 if (oid_len != (schema->prefixes[j].oid_len - 1)) {
233 return WERR_DS_DRA_SCHEMA_MISMATCH;
236 if (strncmp(ctr->mappings[i].oid.oid, schema->prefixes[j].oid, oid_len) != 0) {
237 return WERR_DS_DRA_SCHEMA_MISMATCH;
243 if (j == schema->num_prefixes) {
244 return WERR_DS_DRA_SCHEMA_MISMATCH;
252 WERROR dsdb_map_oid2int(const struct dsdb_schema *schema, const char *in, uint32_t *out)
256 for (i=0; i < schema->num_prefixes; i++) {
261 if (strncmp(schema->prefixes[i].oid, in, schema->prefixes[i].oid_len) != 0) {
265 val_str = in + schema->prefixes[i].oid_len;
269 if (val_str[0] == '\0') {
270 return WERR_INVALID_PARAM;
273 /* two '.' chars are invalid */
274 if (val_str[0] == '.') {
275 return WERR_INVALID_PARAM;
278 val = strtoul(val_str, &end_str, 10);
279 if (end_str[0] == '.' && end_str[1] != '\0') {
281 * if it's a '.' and not the last char
282 * then maybe an other mapping apply
285 } else if (end_str[0] != '\0') {
286 return WERR_INVALID_PARAM;
287 } else if (val > 0xFFFF) {
288 return WERR_INVALID_PARAM;
291 *out = schema->prefixes[i].id | val;
295 return WERR_DS_NO_MSDS_INTID;
298 WERROR dsdb_map_int2oid(const struct dsdb_schema *schema, uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
302 for (i=0; i < schema->num_prefixes; i++) {
304 if (schema->prefixes[i].id != (in & 0xFFFF0000)) {
308 val = talloc_asprintf(mem_ctx, "%s%u",
309 schema->prefixes[i].oid,
311 W_ERROR_HAVE_NO_MEMORY(val);
317 return WERR_DS_NO_MSDS_INTID;
320 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
321 (p)->elem = samdb_result_string(msg, attr, NULL);\
322 if (strict && (p)->elem == NULL) { \
323 d_printf("%s: %s == NULL\n", __location__, attr); \
324 return WERR_INVALID_PARAM; \
326 talloc_steal(mem_ctx, (p)->elem); \
329 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
330 int get_string_list_counter; \
331 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
332 if (get_string_list_el == NULL) { \
334 d_printf("%s: %s == NULL\n", __location__, attr); \
335 return WERR_INVALID_PARAM; \
341 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
342 for (get_string_list_counter=0; \
343 get_string_list_counter < get_string_list_el->num_values; \
344 get_string_list_counter++) { \
345 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
346 (const char *)get_string_list_el->values[get_string_list_counter].data, \
347 get_string_list_el->values[get_string_list_counter].length); \
348 if (!(p)->elem[get_string_list_counter]) { \
349 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
352 (p)->elem[get_string_list_counter+1] = NULL; \
354 talloc_steal(mem_ctx, (p)->elem); \
357 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
359 str = samdb_result_string(msg, attr, NULL);\
362 d_printf("%s: %s == NULL\n", __location__, attr); \
363 return WERR_INVALID_PARAM; \
367 } else if (strcasecmp("TRUE", str) == 0) { \
369 } else if (strcasecmp("FALSE", str) == 0) { \
372 d_printf("%s: %s == %s\n", __location__, attr, str); \
373 return WERR_INVALID_PARAM; \
377 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
378 (p)->elem = samdb_result_uint(msg, attr, 0);\
381 #define GET_GUID_LDB(msg, attr, p, elem) do { \
382 (p)->elem = samdb_result_guid(msg, attr);\
385 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
386 const struct ldb_val *_val;\
387 _val = ldb_msg_find_ldb_val(msg, attr);\
390 talloc_steal(mem_ctx, (p)->elem.data);\
392 ZERO_STRUCT((p)->elem);\
396 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
397 struct ldb_message *msg,
399 struct dsdb_attribute *attr)
403 GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
404 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
405 GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
406 if (schema->num_prefixes == 0) {
407 /* set an invalid value */
408 attr->attributeID_id = 0xFFFFFFFF;
410 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
411 if (!W_ERROR_IS_OK(status)) {
412 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
413 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
414 win_errstr(status)));
418 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
419 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
421 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
423 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
424 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
425 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
426 GET_UINT32_LDB(msg, "linkID", attr, linkID);
428 GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
429 if (schema->num_prefixes == 0) {
430 /* set an invalid value */
431 attr->attributeSyntax_id = 0xFFFFFFFF;
433 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
434 if (!W_ERROR_IS_OK(status)) {
435 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
436 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
437 win_errstr(status)));
441 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
442 GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
444 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
445 GET_UINT32_LDB(msg, "rangeLower", attr, rangeLower);
446 GET_UINT32_LDB(msg, "rangeUpper", attr, rangeUpper);
447 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
449 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
450 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
452 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
453 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
454 GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
455 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
456 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
457 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
458 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
460 attr->syntax = dsdb_syntax_for_attribute(attr);
462 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
468 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
469 struct ldb_message *msg,
471 struct dsdb_class *obj)
475 GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
476 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
477 GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
478 if (schema->num_prefixes == 0) {
479 /* set an invalid value */
480 obj->governsID_id = 0xFFFFFFFF;
482 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
483 if (!W_ERROR_IS_OK(status)) {
484 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
485 __location__, obj->lDAPDisplayName, obj->governsID_oid,
486 win_errstr(status)));
490 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
492 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
493 GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
494 GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
496 GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
498 obj->systemAuxiliaryClass = NULL;
500 obj->auxiliaryClass = NULL;
502 GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
503 GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
504 GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
505 GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
507 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
508 GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
509 GET_STRING_LIST_LDB(msg, "possibleInferiors", mem_ctx, obj, possibleInferiors, false);
511 GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
513 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
514 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
516 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
517 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
518 GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
519 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
520 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
521 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
522 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
527 static const struct {
530 } name_mappings[] = {
532 { "name", "1.2.840.113556.1.4.1" },
533 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
534 { "attributeID", "1.2.840.113556.1.2.30" },
535 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
536 { "mAPIID", "1.2.840.113556.1.2.49" },
537 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
538 { "searchFlags", "1.2.840.113556.1.2.334" },
539 { "systemFlags", "1.2.840.113556.1.4.375" },
540 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
541 { "linkID", "1.2.840.113556.1.2.50" },
542 { "attributeSyntax", "1.2.840.113556.1.2.32" },
543 { "oMSyntax", "1.2.840.113556.1.2.231" },
544 { "oMObjectClass", "1.2.840.113556.1.2.218" },
545 { "isSingleValued", "1.2.840.113556.1.2.33" },
546 { "rangeLower", "1.2.840.113556.1.2.34" },
547 { "rangeUpper", "1.2.840.113556.1.2.35" },
548 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
549 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
550 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
551 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
552 { "adminDisplayName", "1.2.840.113556.1.2.194" },
553 { "adminDescription", "1.2.840.113556.1.2.226" },
554 { "classDisplayName", "1.2.840.113556.1.4.610" },
555 { "isEphemeral", "1.2.840.113556.1.4.1212" },
556 { "isDefunct", "1.2.840.113556.1.4.661" },
557 { "systemOnly", "1.2.840.113556.1.4.170" },
558 { "governsID", "1.2.840.113556.1.2.22" },
559 { "objectClassCategory", "1.2.840.113556.1.2.370" },
560 { "rDNAttID", "1.2.840.113556.1.2.26" },
561 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
562 { "subClassOf", "1.2.840.113556.1.2.21" },
563 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
564 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
565 { "systemMustContain", "1.2.840.113556.1.4.197" },
566 { "systemMayContain", "1.2.840.113556.1.4.196" },
567 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
568 { "possSuperiors", "1.2.840.113556.1.2.8" },
569 { "mustContain", "1.2.840.113556.1.2.24" },
570 { "mayContain", "1.2.840.113556.1.2.25" },
571 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
572 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
575 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
576 struct drsuapi_DsReplicaObject *obj,
582 const char *oid = NULL;
584 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
585 if (strcmp(name_mappings[i].name, name) != 0) continue;
587 oid = name_mappings[i].oid;
595 status = dsdb_map_oid2int(schema, oid, &id);
596 if (!W_ERROR_IS_OK(status)) {
600 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
601 if (obj->attribute_ctr.attributes[i].attid != id) continue;
604 return &obj->attribute_ctr.attributes[i];
610 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
611 struct drsuapi_DsReplicaAttribute *_a; \
612 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
613 if (strict && !_a) { \
614 d_printf("%s: %s == NULL\n", __location__, attr); \
615 return WERR_INVALID_PARAM; \
617 if (strict && _a->value_ctr.num_values != 1) { \
618 d_printf("%s: %s num_values == %u\n", __location__, attr, \
619 _a->value_ctr.num_values); \
620 return WERR_INVALID_PARAM; \
622 if (_a && _a->value_ctr.num_values >= 1) { \
624 _ret = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX, \
625 _a->value_ctr.values[0].blob->data, \
626 _a->value_ctr.values[0].blob->length, \
627 (void **)discard_const(&(p)->elem)); \
629 DEBUG(0,("%s: invalid data!\n", attr)); \
631 _a->value_ctr.values[0].blob->data, \
632 _a->value_ctr.values[0].blob->length); \
633 return WERR_FOOBAR; \
640 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
641 struct drsuapi_DsReplicaAttribute *_a; \
642 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
643 if (strict && !_a) { \
644 d_printf("%s: %s == NULL\n", __location__, attr); \
645 return WERR_INVALID_PARAM; \
647 if (strict && _a->value_ctr.num_values != 1) { \
648 d_printf("%s: %s num_values == %u\n", __location__, attr, \
649 _a->value_ctr.num_values); \
650 return WERR_INVALID_PARAM; \
652 if (strict && !_a->value_ctr.values[0].blob) { \
653 d_printf("%s: %s data == NULL\n", __location__, attr); \
654 return WERR_INVALID_PARAM; \
656 if (_a && _a->value_ctr.num_values >= 1 \
657 && _a->value_ctr.values[0].blob) { \
658 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
659 enum ndr_err_code _ndr_err; \
660 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
662 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
663 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
664 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
665 return ntstatus_to_werror(_nt_status); \
667 (p)->elem = _id3.dn; \
673 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
674 struct drsuapi_DsReplicaAttribute *_a; \
675 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
676 if (strict && !_a) { \
677 d_printf("%s: %s == NULL\n", __location__, attr); \
678 return WERR_INVALID_PARAM; \
680 if (strict && _a->value_ctr.num_values != 1) { \
681 d_printf("%s: %s num_values == %u\n", __location__, attr, \
682 (unsigned int)_a->value_ctr.num_values); \
683 return WERR_INVALID_PARAM; \
685 if (strict && !_a->value_ctr.values[0].blob) { \
686 d_printf("%s: %s data == NULL\n", __location__, attr); \
687 return WERR_INVALID_PARAM; \
689 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
690 d_printf("%s: %s length == %u\n", __location__, attr, \
691 (unsigned int)_a->value_ctr.values[0].blob->length); \
692 return WERR_INVALID_PARAM; \
694 if (_a && _a->value_ctr.num_values >= 1 \
695 && _a->value_ctr.values[0].blob \
696 && _a->value_ctr.values[0].blob->length == 4) { \
697 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
703 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
704 struct drsuapi_DsReplicaAttribute *_a; \
705 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
706 if (_a && _a->value_ctr.num_values >= 1 \
707 && _a->value_ctr.values[0].blob \
708 && _a->value_ctr.values[0].blob->length == 4) { \
709 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
715 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
716 struct drsuapi_DsReplicaAttribute *_a; \
717 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
718 if (_a && _a->value_ctr.num_values >= 1 \
719 && _a->value_ctr.values[0].blob \
720 && _a->value_ctr.values[0].blob->length == 16) { \
721 enum ndr_err_code _ndr_err; \
722 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
723 mem_ctx, &(p)->elem, \
724 (ndr_pull_flags_fn_t)ndr_pull_GUID); \
725 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
726 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
727 return ntstatus_to_werror(_nt_status); \
730 ZERO_STRUCT((p)->elem);\
734 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
735 struct drsuapi_DsReplicaAttribute *_a; \
736 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
737 if (_a && _a->value_ctr.num_values >= 1 \
738 && _a->value_ctr.values[0].blob) { \
739 (p)->elem = *_a->value_ctr.values[0].blob;\
740 talloc_steal(mem_ctx, (p)->elem.data); \
742 ZERO_STRUCT((p)->elem);\
746 WERROR dsdb_attribute_from_drsuapi(struct dsdb_schema *schema,
747 struct drsuapi_DsReplicaObject *r,
749 struct dsdb_attribute *attr)
753 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
754 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
755 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
756 status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
757 if (!W_ERROR_IS_OK(status)) {
758 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
759 __location__, attr->lDAPDisplayName, attr->attributeID_id,
760 win_errstr(status)));
763 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
764 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
766 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
768 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
769 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
770 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
771 GET_UINT32_DS(schema, r, "linkID", attr, linkID);
773 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
774 status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
775 if (!W_ERROR_IS_OK(status)) {
776 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
777 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
778 win_errstr(status)));
781 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
782 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
784 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
785 GET_UINT32_DS(schema, r, "rangeLower", attr, rangeLower);
786 GET_UINT32_DS(schema, r, "rangeUpper", attr, rangeUpper);
787 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
789 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
790 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
792 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
793 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
794 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
795 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
796 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
797 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
798 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
800 attr->syntax = dsdb_syntax_for_attribute(attr);
802 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
808 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
809 struct drsuapi_DsReplicaObject *r,
811 struct dsdb_class *obj)
815 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
816 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
817 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
818 status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
819 if (!W_ERROR_IS_OK(status)) {
820 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
821 __location__, obj->lDAPDisplayName, obj->governsID_id,
822 win_errstr(status)));
825 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
827 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
828 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
829 GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
831 GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, true);
833 obj->systemAuxiliaryClass = NULL;
834 obj->systemPossSuperiors = NULL;
835 obj->systemMustContain = NULL;
836 obj->systemMayContain = NULL;
838 obj->auxiliaryClass = NULL;
839 obj->possSuperiors = NULL;
840 obj->mustContain = NULL;
841 obj->mayContain = NULL;
843 obj->possibleInferiors = NULL;
845 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
847 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
848 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
850 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
851 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
852 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
853 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
854 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
855 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
856 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
861 const struct dsdb_attribute *dsdb_attribute_by_attributeID_id(const struct dsdb_schema *schema,
864 struct dsdb_attribute *cur;
867 * 0xFFFFFFFF is used as value when no mapping table is available,
868 * so don't try to match with it
870 if (id == 0xFFFFFFFF) return NULL;
872 /* TODO: add binary search */
873 for (cur = schema->attributes; cur; cur = cur->next) {
874 if (cur->attributeID_id != id) continue;
882 const struct dsdb_attribute *dsdb_attribute_by_attributeID_oid(const struct dsdb_schema *schema,
885 struct dsdb_attribute *cur;
887 if (!oid) return NULL;
889 /* TODO: add binary search */
890 for (cur = schema->attributes; cur; cur = cur->next) {
891 if (strcmp(cur->attributeID_oid, oid) != 0) continue;
899 const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName(const struct dsdb_schema *schema,
902 struct dsdb_attribute *cur;
904 if (!name) return NULL;
906 /* TODO: add binary search */
907 for (cur = schema->attributes; cur; cur = cur->next) {
908 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
916 const struct dsdb_attribute *dsdb_attribute_by_linkID(const struct dsdb_schema *schema,
919 struct dsdb_attribute *cur;
921 /* TODO: add binary search */
922 for (cur = schema->attributes; cur; cur = cur->next) {
923 if (cur->linkID != linkID) continue;
931 const struct dsdb_class *dsdb_class_by_governsID_id(const struct dsdb_schema *schema,
934 struct dsdb_class *cur;
937 * 0xFFFFFFFF is used as value when no mapping table is available,
938 * so don't try to match with it
940 if (id == 0xFFFFFFFF) return NULL;
942 /* TODO: add binary search */
943 for (cur = schema->classes; cur; cur = cur->next) {
944 if (cur->governsID_id != id) continue;
952 const struct dsdb_class *dsdb_class_by_governsID_oid(const struct dsdb_schema *schema,
955 struct dsdb_class *cur;
957 if (!oid) return NULL;
959 /* TODO: add binary search */
960 for (cur = schema->classes; cur; cur = cur->next) {
961 if (strcmp(cur->governsID_oid, oid) != 0) continue;
969 const struct dsdb_class *dsdb_class_by_lDAPDisplayName(const struct dsdb_schema *schema,
972 struct dsdb_class *cur;
974 if (!name) return NULL;
976 /* TODO: add binary search */
977 for (cur = schema->classes; cur; cur = cur->next) {
978 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
986 const struct dsdb_class *dsdb_class_by_cn(const struct dsdb_schema *schema,
989 struct dsdb_class *cur;
991 if (!cn) return NULL;
993 /* TODO: add binary search */
994 for (cur = schema->classes; cur; cur = cur->next) {
995 if (strcasecmp(cur->cn, cn) != 0) continue;
1003 const char *dsdb_lDAPDisplayName_by_id(const struct dsdb_schema *schema,
1006 const struct dsdb_attribute *a;
1007 const struct dsdb_class *c;
1009 /* TODO: add binary search */
1010 a = dsdb_attribute_by_attributeID_id(schema, id);
1012 return a->lDAPDisplayName;
1015 c = dsdb_class_by_governsID_id(schema, id);
1017 return c->lDAPDisplayName;
1023 WERROR dsdb_linked_attribute_lDAPDisplayName_list(const struct dsdb_schema *schema, TALLOC_CTX *mem_ctx, const char ***attr_list_ret)
1025 const char **attr_list = NULL;
1026 struct dsdb_attribute *cur;
1028 for (cur = schema->attributes; cur; cur = cur->next) {
1029 if (cur->linkID == 0) continue;
1031 attr_list = talloc_realloc(mem_ctx, attr_list, const char *, i+2);
1035 attr_list[i] = cur->lDAPDisplayName;
1038 attr_list[i] = NULL;
1039 *attr_list_ret = attr_list;
1044 * Attach the schema to an opaque pointer on the ldb, so ldb modules
1048 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
1052 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
1053 if (ret != LDB_SUCCESS) {
1057 talloc_steal(ldb, schema);
1063 * Global variable to hold one copy of the schema, used to avoid memory bloat
1065 static struct dsdb_schema *global_schema;
1068 * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
1070 int dsdb_set_global_schema(struct ldb_context *ldb)
1073 if (!global_schema) {
1076 ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
1077 if (ret != LDB_SUCCESS) {
1085 * Find the schema object for this ldb
1088 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
1091 struct dsdb_schema *schema;
1093 /* see if we have a cached copy */
1094 p = ldb_get_opaque(ldb, "dsdb_schema");
1099 schema = talloc_get_type(p, struct dsdb_schema);
1108 * Make the schema found on this ldb the 'global' schema
1111 void dsdb_make_schema_global(struct ldb_context *ldb)
1113 struct dsdb_schema *schema = dsdb_get_schema(ldb);
1118 talloc_steal(talloc_autofree_context(), schema);
1119 global_schema = schema;
1121 dsdb_set_global_schema(ldb);
1126 * Rather than read a schema from the LDB itself, read it from an ldif
1127 * file. This allows schema to be loaded and used while adding the
1128 * schema itself to the directory.
1131 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
1133 struct ldb_ldif *ldif;
1134 struct ldb_message *msg;
1135 TALLOC_CTX *mem_ctx;
1138 struct dsdb_schema *schema;
1139 const struct ldb_val *prefix_val;
1140 const struct ldb_val *info_val;
1141 struct ldb_val info_val_default;
1143 mem_ctx = talloc_new(ldb);
1148 schema = talloc_zero(mem_ctx, struct dsdb_schema);
1154 * load the prefixMap attribute from pf
1156 ldif = ldb_ldif_read_string(ldb, &pf);
1158 status = WERR_INVALID_PARAM;
1161 talloc_steal(mem_ctx, ldif);
1163 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1168 prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
1170 status = WERR_INVALID_PARAM;
1174 info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
1176 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
1177 if (!info_val_default.data) {
1180 talloc_steal(mem_ctx, info_val_default.data);
1181 info_val = &info_val_default;
1184 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1185 if (!W_ERROR_IS_OK(status)) {
1190 * load the attribute and class definitions outof df
1192 while ((ldif = ldb_ldif_read_string(ldb, &df))) {
1196 talloc_steal(mem_ctx, ldif);
1198 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1203 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
1204 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
1207 struct dsdb_attribute *sa;
1209 sa = talloc_zero(schema, struct dsdb_attribute);
1214 status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
1215 if (!W_ERROR_IS_OK(status)) {
1219 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
1221 struct dsdb_class *sc;
1223 sc = talloc_zero(schema, struct dsdb_class);
1228 status = dsdb_class_from_ldb(schema, msg, sc, sc);
1229 if (!W_ERROR_IS_OK(status)) {
1233 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
1237 ret = dsdb_set_schema(ldb, schema);
1238 if (ret != LDB_SUCCESS) {
1239 status = WERR_FOOBAR;
1246 status = WERR_NOMEM;
1249 talloc_free(mem_ctx);