2 Unix SMB/CIFS mplementation.
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "dsdb/samdb/samdb.h"
24 #include "lib/ldb/include/ldb_errors.h"
25 #include "lib/util/dlinklist.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
27 #include "librpc/gen_ndr/ndr_drsuapi.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
34 schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
35 W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
37 for (i=0, j=0; i < ctr->num_mappings; i++) {
38 if (ctr->mappings[i].oid.oid == NULL) {
39 return WERR_INVALID_PARAM;
42 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
43 if (ctr->mappings[i].id_prefix != 0) {
44 return WERR_INVALID_PARAM;
47 /* the magic value should be in the last array member */
48 if (i != (ctr->num_mappings - 1)) {
49 return WERR_INVALID_PARAM;
52 if (ctr->mappings[i].oid.__ndr_size != 21) {
53 return WERR_INVALID_PARAM;
56 schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
57 W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
59 /* the last array member should contain the magic value not a oid */
60 if (i == (ctr->num_mappings - 1)) {
61 return WERR_INVALID_PARAM;
64 schema->prefixes[j].id = ctr->mappings[i].id_prefix<<16;
65 schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
66 ctr->mappings[i].oid.oid);
67 W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
68 schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
73 schema->num_prefixes = j;
77 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
78 const struct ldb_val *prefixMap,
79 const struct ldb_val *schemaInfo)
83 struct prefixMapBlob pfm;
86 nt_status = ndr_pull_struct_blob(prefixMap, schema, &pfm,
87 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
88 if (!NT_STATUS_IS_OK(nt_status)) {
89 return ntstatus_to_werror(nt_status);
92 if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
96 if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
100 /* append the schema info as last element */
101 pfm.ctr.dsdb.num_mappings++;
102 pfm.ctr.dsdb.mappings = talloc_realloc(schema, pfm.ctr.dsdb.mappings,
103 struct drsuapi_DsReplicaOIDMapping,
104 pfm.ctr.dsdb.num_mappings);
105 W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
107 schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
108 W_ERROR_HAVE_NO_MEMORY(schema_info);
110 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix = 0;
111 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size = schemaInfo->length;
112 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid = schema_info;
114 /* call the drsuapi version */
115 status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
116 talloc_free(pfm.ctr.dsdb.mappings);
117 W_ERROR_NOT_OK_RETURN(status);
122 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
123 bool include_schema_info,
125 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
127 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
130 ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
131 W_ERROR_HAVE_NO_MEMORY(ctr);
133 ctr->num_mappings = schema->num_prefixes;
134 if (include_schema_info) ctr->num_mappings++;
135 ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
136 W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
138 for (i=0; i < schema->num_prefixes; i++) {
139 ctr->mappings[i].id_prefix = schema->prefixes[i].id>>16;
140 ctr->mappings[i].oid.oid = talloc_strndup(ctr->mappings,
141 schema->prefixes[i].oid,
142 schema->prefixes[i].oid_len - 1);
143 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
146 if (include_schema_info) {
147 ctr->mappings[i].id_prefix = 0;
148 ctr->mappings[i].oid.oid = talloc_strdup(ctr->mappings,
149 schema->schema_info);
150 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
157 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
159 struct ldb_val *prefixMap,
160 struct ldb_val *schemaInfo)
164 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
165 struct prefixMapBlob pfm;
167 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
168 W_ERROR_NOT_OK_RETURN(status);
170 pfm.version = PREFIX_MAP_VERSION_DSDB;
174 nt_status = ndr_push_struct_blob(prefixMap, mem_ctx, &pfm,
175 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
177 if (!NT_STATUS_IS_OK(nt_status)) {
178 return ntstatus_to_werror(nt_status);
181 *schemaInfo = strhex_to_data_blob(schema->schema_info);
182 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
183 talloc_steal(mem_ctx, schemaInfo->data);
188 WERROR dsdb_verify_oid_mappings_drsuapi(const struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
192 for (i=0; i < ctr->num_mappings; i++) {
193 if (ctr->mappings[i].oid.oid == NULL) {
194 return WERR_INVALID_PARAM;
197 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
198 if (ctr->mappings[i].id_prefix != 0) {
199 return WERR_INVALID_PARAM;
202 /* the magic value should be in the last array member */
203 if (i != (ctr->num_mappings - 1)) {
204 return WERR_INVALID_PARAM;
207 if (ctr->mappings[i].oid.__ndr_size != 21) {
208 return WERR_INVALID_PARAM;
211 if (strcasecmp(schema->schema_info, ctr->mappings[i].oid.oid) != 0) {
212 return WERR_DS_DRA_SCHEMA_MISMATCH;
215 /* the last array member should contain the magic value not a oid */
216 if (i == (ctr->num_mappings - 1)) {
217 return WERR_INVALID_PARAM;
220 for (j=0; j < schema->num_prefixes; j++) {
222 if (schema->prefixes[j].id != (ctr->mappings[i].id_prefix<<16)) {
226 oid_len = strlen(ctr->mappings[i].oid.oid);
228 if (oid_len != (schema->prefixes[j].oid_len - 1)) {
229 return WERR_DS_DRA_SCHEMA_MISMATCH;
232 if (strncmp(ctr->mappings[i].oid.oid, schema->prefixes[j].oid, oid_len) != 0) {
233 return WERR_DS_DRA_SCHEMA_MISMATCH;
239 if (j == schema->num_prefixes) {
240 return WERR_DS_DRA_SCHEMA_MISMATCH;
248 WERROR dsdb_map_oid2int(const struct dsdb_schema *schema, const char *in, uint32_t *out)
252 for (i=0; i < schema->num_prefixes; i++) {
257 if (strncmp(schema->prefixes[i].oid, in, schema->prefixes[i].oid_len) != 0) {
261 val_str = in + schema->prefixes[i].oid_len;
265 if (val_str[0] == '\0') {
266 return WERR_INVALID_PARAM;
269 /* two '.' chars are invalid */
270 if (val_str[0] == '.') {
271 return WERR_INVALID_PARAM;
274 val = strtoul(val_str, &end_str, 10);
275 if (end_str[0] == '.' && end_str[1] != '\0') {
277 * if it's a '.' and not the last char
278 * then maybe an other mapping apply
281 } else if (end_str[0] != '\0') {
282 return WERR_INVALID_PARAM;
283 } else if (val > 0xFFFF) {
284 return WERR_INVALID_PARAM;
287 *out = schema->prefixes[i].id | val;
291 return WERR_DS_NO_MSDS_INTID;
294 WERROR dsdb_map_int2oid(const struct dsdb_schema *schema, uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
298 for (i=0; i < schema->num_prefixes; i++) {
300 if (schema->prefixes[i].id != (in & 0xFFFF0000)) {
304 val = talloc_asprintf(mem_ctx, "%s%u",
305 schema->prefixes[i].oid,
307 W_ERROR_HAVE_NO_MEMORY(val);
313 return WERR_DS_NO_MSDS_INTID;
316 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
317 (p)->elem = samdb_result_string(msg, attr, NULL);\
318 if (strict && (p)->elem == NULL) { \
319 d_printf("%s: %s == NULL\n", __location__, attr); \
320 return WERR_INVALID_PARAM; \
322 talloc_steal(mem_ctx, (p)->elem); \
325 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
326 int get_string_list_counter; \
327 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
328 if (get_string_list_el == NULL) { \
330 d_printf("%s: %s == NULL\n", __location__, attr); \
331 return WERR_INVALID_PARAM; \
337 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
338 for (get_string_list_counter=0; \
339 get_string_list_counter < get_string_list_el->num_values; \
340 get_string_list_counter++) { \
341 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
342 (const char *)get_string_list_el->values[get_string_list_counter].data, \
343 get_string_list_el->values[get_string_list_counter].length); \
344 if (!(p)->elem[get_string_list_counter]) { \
345 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
348 (p)->elem[get_string_list_counter+1] = NULL; \
350 talloc_steal(mem_ctx, (p)->elem); \
353 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
355 str = samdb_result_string(msg, attr, NULL);\
358 d_printf("%s: %s == NULL\n", __location__, attr); \
359 return WERR_INVALID_PARAM; \
363 } else if (strcasecmp("TRUE", str) == 0) { \
365 } else if (strcasecmp("FALSE", str) == 0) { \
368 d_printf("%s: %s == %s\n", __location__, attr, str); \
369 return WERR_INVALID_PARAM; \
373 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
374 (p)->elem = samdb_result_uint(msg, attr, 0);\
377 #define GET_GUID_LDB(msg, attr, p, elem) do { \
378 (p)->elem = samdb_result_guid(msg, attr);\
381 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
382 const struct ldb_val *_val;\
383 _val = ldb_msg_find_ldb_val(msg, attr);\
386 talloc_steal(mem_ctx, (p)->elem.data);\
388 ZERO_STRUCT((p)->elem);\
392 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
393 struct ldb_message *msg,
395 struct dsdb_attribute *attr)
399 GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, False);
400 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, True);
401 GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, True);
402 if (schema->num_prefixes == 0) {
403 /* set an invalid value */
404 attr->attributeID_id = 0xFFFFFFFF;
406 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
407 if (!W_ERROR_IS_OK(status)) {
408 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
409 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
410 win_errstr(status)));
414 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
415 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
417 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
419 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
420 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
421 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, False);
422 GET_UINT32_LDB(msg, "linkID", attr, linkID);
424 GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, True);
425 if (schema->num_prefixes == 0) {
426 /* set an invalid value */
427 attr->attributeSyntax_id = 0xFFFFFFFF;
429 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
430 if (!W_ERROR_IS_OK(status)) {
431 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
432 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
433 win_errstr(status)));
437 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
438 GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
440 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, True);
441 GET_UINT32_LDB(msg, "rangeLower", attr, rangeLower);
442 GET_UINT32_LDB(msg, "rangeUpper", attr, rangeUpper);
443 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, False);
445 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
446 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
448 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, False);
449 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, False);
450 GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, False);
451 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, False);
452 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, False);
453 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, False);
454 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, False);
456 attr->syntax = dsdb_syntax_for_attribute(attr);
458 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
464 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
465 struct ldb_message *msg,
467 struct dsdb_class *obj)
471 GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, False);
472 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, True);
473 GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, True);
474 if (schema->num_prefixes == 0) {
475 /* set an invalid value */
476 obj->governsID_id = 0xFFFFFFFF;
478 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
479 if (!W_ERROR_IS_OK(status)) {
480 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
481 __location__, obj->lDAPDisplayName, obj->governsID_oid,
482 win_errstr(status)));
486 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
488 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
489 GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, False);
490 GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, True);
492 GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, True);
494 obj->systemAuxiliaryClass = NULL;
496 obj->auxiliaryClass = NULL;
498 GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, False);
499 GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, False);
500 GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, False);
501 GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, False);
503 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, False);
504 GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, False);
505 GET_STRING_LIST_LDB(msg, "possibleInferiors", mem_ctx, obj, possibleInferiors, False);
507 GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, False);
509 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
510 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
512 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, False);
513 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, False);
514 GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, False);
515 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, False);
516 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, False);
517 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, False);
518 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, False);
523 static const struct {
526 } name_mappings[] = {
528 { "name", "1.2.840.113556.1.4.1" },
529 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
530 { "attributeID", "1.2.840.113556.1.2.30" },
531 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
532 { "mAPIID", "1.2.840.113556.1.2.49" },
533 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
534 { "searchFlags", "1.2.840.113556.1.2.334" },
535 { "systemFlags", "1.2.840.113556.1.4.375" },
536 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
537 { "linkID", "1.2.840.113556.1.2.50" },
538 { "attributeSyntax", "1.2.840.113556.1.2.32" },
539 { "oMSyntax", "1.2.840.113556.1.2.231" },
540 { "oMObjectClass", "1.2.840.113556.1.2.218" },
541 { "isSingleValued", "1.2.840.113556.1.2.33" },
542 { "rangeLower", "1.2.840.113556.1.2.34" },
543 { "rangeUpper", "1.2.840.113556.1.2.35" },
544 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
545 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
546 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
547 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
548 { "adminDisplayName", "1.2.840.113556.1.2.194" },
549 { "adminDescription", "1.2.840.113556.1.2.226" },
550 { "classDisplayName", "1.2.840.113556.1.4.610" },
551 { "isEphemeral", "1.2.840.113556.1.4.1212" },
552 { "isDefunct", "1.2.840.113556.1.4.661" },
553 { "systemOnly", "1.2.840.113556.1.4.170" },
554 { "governsID", "1.2.840.113556.1.2.22" },
555 { "objectClassCategory", "1.2.840.113556.1.2.370" },
556 { "rDNAttID", "1.2.840.113556.1.2.26" },
557 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
558 { "subClassOf", "1.2.840.113556.1.2.21" },
559 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
560 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
561 { "systemMustContain", "1.2.840.113556.1.4.197" },
562 { "systemMayContain", "1.2.840.113556.1.4.196" },
563 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
564 { "possSuperiors", "1.2.840.113556.1.2.8" },
565 { "mustContain", "1.2.840.113556.1.2.24" },
566 { "mayContain", "1.2.840.113556.1.2.25" },
567 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
568 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
571 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
572 struct drsuapi_DsReplicaObject *obj,
578 const char *oid = NULL;
580 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
581 if (strcmp(name_mappings[i].name, name) != 0) continue;
583 oid = name_mappings[i].oid;
591 status = dsdb_map_oid2int(schema, oid, &id);
592 if (!W_ERROR_IS_OK(status)) {
596 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
597 if (obj->attribute_ctr.attributes[i].attid != id) continue;
600 return &obj->attribute_ctr.attributes[i];
606 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
607 struct drsuapi_DsReplicaAttribute *_a; \
608 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
609 if (strict && !_a) { \
610 d_printf("%s: %s == NULL\n", __location__, attr); \
611 return WERR_INVALID_PARAM; \
613 if (strict && _a->value_ctr.num_values != 1) { \
614 d_printf("%s: %s num_values == %u\n", __location__, attr, \
615 _a->value_ctr.num_values); \
616 return WERR_INVALID_PARAM; \
618 if (_a && _a->value_ctr.num_values >= 1) { \
620 _ret = convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX, \
621 _a->value_ctr.values[0].blob->data, \
622 _a->value_ctr.values[0].blob->length, \
623 (void **)discard_const(&(p)->elem)); \
625 DEBUG(0,("%s: invalid data!\n", attr)); \
627 _a->value_ctr.values[0].blob->data, \
628 _a->value_ctr.values[0].blob->length); \
629 return WERR_FOOBAR; \
636 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
637 struct drsuapi_DsReplicaAttribute *_a; \
638 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
639 if (strict && !_a) { \
640 d_printf("%s: %s == NULL\n", __location__, attr); \
641 return WERR_INVALID_PARAM; \
643 if (strict && _a->value_ctr.num_values != 1) { \
644 d_printf("%s: %s num_values == %u\n", __location__, attr, \
645 _a->value_ctr.num_values); \
646 return WERR_INVALID_PARAM; \
648 if (strict && !_a->value_ctr.values[0].blob) { \
649 d_printf("%s: %s data == NULL\n", __location__, attr); \
650 return WERR_INVALID_PARAM; \
652 if (_a && _a->value_ctr.num_values >= 1 \
653 && _a->value_ctr.values[0].blob) { \
654 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
655 NTSTATUS _nt_status; \
656 _nt_status = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
658 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
659 if (!NT_STATUS_IS_OK(_nt_status)) { \
660 return ntstatus_to_werror(_nt_status); \
662 (p)->elem = _id3.dn; \
668 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
669 struct drsuapi_DsReplicaAttribute *_a; \
670 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
671 if (strict && !_a) { \
672 d_printf("%s: %s == NULL\n", __location__, attr); \
673 return WERR_INVALID_PARAM; \
675 if (strict && _a->value_ctr.num_values != 1) { \
676 d_printf("%s: %s num_values == %u\n", __location__, attr, \
677 (unsigned int)_a->value_ctr.num_values); \
678 return WERR_INVALID_PARAM; \
680 if (strict && !_a->value_ctr.values[0].blob) { \
681 d_printf("%s: %s data == NULL\n", __location__, attr); \
682 return WERR_INVALID_PARAM; \
684 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
685 d_printf("%s: %s length == %u\n", __location__, attr, \
686 (unsigned int)_a->value_ctr.values[0].blob->length); \
687 return WERR_INVALID_PARAM; \
689 if (_a && _a->value_ctr.num_values >= 1 \
690 && _a->value_ctr.values[0].blob \
691 && _a->value_ctr.values[0].blob->length == 4) { \
692 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?True:False);\
698 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
699 struct drsuapi_DsReplicaAttribute *_a; \
700 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
701 if (_a && _a->value_ctr.num_values >= 1 \
702 && _a->value_ctr.values[0].blob \
703 && _a->value_ctr.values[0].blob->length == 4) { \
704 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
710 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
711 struct drsuapi_DsReplicaAttribute *_a; \
712 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
713 if (_a && _a->value_ctr.num_values >= 1 \
714 && _a->value_ctr.values[0].blob \
715 && _a->value_ctr.values[0].blob->length == 16) { \
716 NTSTATUS _nt_status; \
717 _nt_status = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
718 mem_ctx, &(p)->elem, \
719 (ndr_pull_flags_fn_t)ndr_pull_GUID); \
720 if (!NT_STATUS_IS_OK(_nt_status)) { \
721 return ntstatus_to_werror(_nt_status); \
724 ZERO_STRUCT((p)->elem);\
728 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
729 struct drsuapi_DsReplicaAttribute *_a; \
730 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
731 if (_a && _a->value_ctr.num_values >= 1 \
732 && _a->value_ctr.values[0].blob) { \
733 (p)->elem = *_a->value_ctr.values[0].blob;\
734 talloc_steal(mem_ctx, (p)->elem.data); \
736 ZERO_STRUCT((p)->elem);\
740 WERROR dsdb_attribute_from_drsuapi(struct dsdb_schema *schema,
741 struct drsuapi_DsReplicaObject *r,
743 struct dsdb_attribute *attr)
747 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, True);
748 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, True);
749 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
750 status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
751 if (!W_ERROR_IS_OK(status)) {
752 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
753 __location__, attr->lDAPDisplayName, attr->attributeID_id,
754 win_errstr(status)));
757 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
758 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
760 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
762 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
763 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
764 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, False);
765 GET_UINT32_DS(schema, r, "linkID", attr, linkID);
767 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
768 status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
769 if (!W_ERROR_IS_OK(status)) {
770 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
771 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
772 win_errstr(status)));
775 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
776 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
778 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, True);
779 GET_UINT32_DS(schema, r, "rangeLower", attr, rangeLower);
780 GET_UINT32_DS(schema, r, "rangeUpper", attr, rangeUpper);
781 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, False);
783 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
784 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
786 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, False);
787 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, False);
788 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, False);
789 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, False);
790 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, False);
791 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, False);
792 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, False);
794 attr->syntax = dsdb_syntax_for_attribute(attr);
796 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
802 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
803 struct drsuapi_DsReplicaObject *r,
805 struct dsdb_class *obj)
809 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, True);
810 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, True);
811 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
812 status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
813 if (!W_ERROR_IS_OK(status)) {
814 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
815 __location__, obj->lDAPDisplayName, obj->governsID_id,
816 win_errstr(status)));
819 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
821 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
822 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, False);
823 GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, True);
825 GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, True);
827 obj->systemAuxiliaryClass = NULL;
828 obj->systemPossSuperiors = NULL;
829 obj->systemMustContain = NULL;
830 obj->systemMayContain = NULL;
832 obj->auxiliaryClass = NULL;
833 obj->possSuperiors = NULL;
834 obj->mustContain = NULL;
835 obj->mayContain = NULL;
837 obj->possibleInferiors = NULL;
839 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, False);
841 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
842 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
844 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, False);
845 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, False);
846 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, False);
847 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, False);
848 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, False);
849 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, False);
850 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, False);
855 const struct dsdb_attribute *dsdb_attribute_by_attributeID_id(const struct dsdb_schema *schema,
858 struct dsdb_attribute *cur;
861 * 0xFFFFFFFF is used as value when no mapping table is available,
862 * so don't try to match with it
864 if (id == 0xFFFFFFFF) return NULL;
866 /* TODO: add binary search */
867 for (cur = schema->attributes; cur; cur = cur->next) {
868 if (cur->attributeID_id != id) continue;
876 const struct dsdb_attribute *dsdb_attribute_by_attributeID_oid(const struct dsdb_schema *schema,
879 struct dsdb_attribute *cur;
881 if (!oid) return NULL;
883 /* TODO: add binary search */
884 for (cur = schema->attributes; cur; cur = cur->next) {
885 if (strcmp(cur->attributeID_oid, oid) != 0) continue;
893 const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName(const struct dsdb_schema *schema,
896 struct dsdb_attribute *cur;
898 if (!name) return NULL;
900 /* TODO: add binary search */
901 for (cur = schema->attributes; cur; cur = cur->next) {
902 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
910 const struct dsdb_class *dsdb_class_by_governsID_id(const struct dsdb_schema *schema,
913 struct dsdb_class *cur;
916 * 0xFFFFFFFF is used as value when no mapping table is available,
917 * so don't try to match with it
919 if (id == 0xFFFFFFFF) return NULL;
921 /* TODO: add binary search */
922 for (cur = schema->classes; cur; cur = cur->next) {
923 if (cur->governsID_id != id) continue;
931 const struct dsdb_class *dsdb_class_by_governsID_oid(const struct dsdb_schema *schema,
934 struct dsdb_class *cur;
936 if (!oid) return NULL;
938 /* TODO: add binary search */
939 for (cur = schema->classes; cur; cur = cur->next) {
940 if (strcmp(cur->governsID_oid, oid) != 0) continue;
948 const struct dsdb_class *dsdb_class_by_lDAPDisplayName(const struct dsdb_schema *schema,
951 struct dsdb_class *cur;
953 if (!name) return NULL;
955 /* TODO: add binary search */
956 for (cur = schema->classes; cur; cur = cur->next) {
957 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
965 const struct dsdb_class *dsdb_class_by_cn(const struct dsdb_schema *schema,
968 struct dsdb_class *cur;
970 if (!cn) return NULL;
972 /* TODO: add binary search */
973 for (cur = schema->classes; cur; cur = cur->next) {
974 if (strcasecmp(cur->cn, cn) != 0) continue;
982 const char *dsdb_lDAPDisplayName_by_id(const struct dsdb_schema *schema,
985 const struct dsdb_attribute *a;
986 const struct dsdb_class *c;
988 /* TODO: add binary search */
989 a = dsdb_attribute_by_attributeID_id(schema, id);
991 return a->lDAPDisplayName;
994 c = dsdb_class_by_governsID_id(schema, id);
996 return c->lDAPDisplayName;
1002 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
1006 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
1007 if (ret != LDB_SUCCESS) {
1011 talloc_steal(ldb, schema);
1016 static struct dsdb_schema *global_schema;
1018 int dsdb_set_global_schema(struct ldb_context *ldb)
1021 if (!global_schema) {
1024 ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
1025 if (ret != LDB_SUCCESS) {
1032 const struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
1035 const struct dsdb_schema *schema;
1037 /* see if we have a cached copy */
1038 p = ldb_get_opaque(ldb, "dsdb_schema");
1043 schema = talloc_get_type(p, struct dsdb_schema);
1051 void dsdb_make_schema_global(struct ldb_context *ldb)
1054 const struct dsdb_schema *schema;
1056 /* see if we have a cached copy */
1057 p = ldb_get_opaque(ldb, "dsdb_schema");
1062 schema = talloc_get_type(p, struct dsdb_schema);
1067 talloc_steal(talloc_autofree_context(), schema);
1068 global_schema = schema;
1070 dsdb_set_global_schema(ldb);
1073 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
1075 struct ldb_ldif *ldif;
1076 struct ldb_message *msg;
1077 TALLOC_CTX *mem_ctx;
1080 struct dsdb_schema *schema;
1081 const struct ldb_val *prefix_val;
1082 const struct ldb_val *info_val;
1083 struct ldb_val info_val_default;
1085 mem_ctx = talloc_new(ldb);
1090 schema = talloc_zero(mem_ctx, struct dsdb_schema);
1096 * load the prefixMap attribute from pf
1098 ldif = ldb_ldif_read_string(ldb, &pf);
1100 status = WERR_INVALID_PARAM;
1103 talloc_steal(mem_ctx, ldif);
1105 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1110 prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
1112 status = WERR_INVALID_PARAM;
1116 info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
1118 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
1119 if (!info_val_default.data) {
1122 talloc_steal(mem_ctx, info_val_default.data);
1123 info_val = &info_val_default;
1126 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1127 if (!W_ERROR_IS_OK(status)) {
1132 * load the attribute and class definitions outof df
1134 while ((ldif = ldb_ldif_read_string(ldb, &df))) {
1138 talloc_steal(mem_ctx, ldif);
1140 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1145 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
1146 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
1149 struct dsdb_attribute *sa;
1151 sa = talloc_zero(schema, struct dsdb_attribute);
1156 status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
1157 if (!W_ERROR_IS_OK(status)) {
1161 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
1163 struct dsdb_class *sc;
1165 sc = talloc_zero(schema, struct dsdb_class);
1170 status = dsdb_class_from_ldb(schema, msg, sc, sc);
1171 if (!W_ERROR_IS_OK(status)) {
1175 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
1179 ret = dsdb_set_schema(ldb, schema);
1180 if (ret != LDB_SUCCESS) {
1181 status = WERR_FOOBAR;
1188 status = WERR_NOMEM;
1191 talloc_free(mem_ctx);