2 Unix SMB/CIFS mplementation.
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "dsdb/samdb/samdb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "../lib/util/dlinklist.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "param/param.h"
31 #include "lib/ldb/include/ldb_module.h"
33 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
35 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
40 schema->iconv_convenience = iconv_convenience;
45 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
49 schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
50 W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
52 for (i=0, j=0; i < ctr->num_mappings; i++) {
53 if (ctr->mappings[i].oid.oid == NULL) {
54 return WERR_INVALID_PARAM;
57 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
58 if (ctr->mappings[i].id_prefix != 0) {
59 return WERR_INVALID_PARAM;
62 /* the magic value should be in the last array member */
63 if (i != (ctr->num_mappings - 1)) {
64 return WERR_INVALID_PARAM;
67 if (ctr->mappings[i].oid.__ndr_size != 21) {
68 return WERR_INVALID_PARAM;
71 schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
72 W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
74 /* the last array member should contain the magic value not a oid */
75 if (i == (ctr->num_mappings - 1)) {
76 return WERR_INVALID_PARAM;
79 schema->prefixes[j].id = ctr->mappings[i].id_prefix<<16;
80 schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
81 ctr->mappings[i].oid.oid);
82 W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
83 schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
88 schema->num_prefixes = j;
92 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
93 const struct ldb_val *prefixMap,
94 const struct ldb_val *schemaInfo)
97 enum ndr_err_code ndr_err;
98 struct prefixMapBlob pfm;
101 TALLOC_CTX *mem_ctx = talloc_new(schema);
102 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
104 ndr_err = ndr_pull_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
105 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
107 talloc_free(mem_ctx);
108 return ntstatus_to_werror(nt_status);
111 if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
112 talloc_free(mem_ctx);
116 if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
117 talloc_free(mem_ctx);
121 /* append the schema info as last element */
122 pfm.ctr.dsdb.num_mappings++;
123 pfm.ctr.dsdb.mappings = talloc_realloc(mem_ctx, pfm.ctr.dsdb.mappings,
124 struct drsuapi_DsReplicaOIDMapping,
125 pfm.ctr.dsdb.num_mappings);
126 W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
128 schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
129 W_ERROR_HAVE_NO_MEMORY(schema_info);
131 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix = 0;
132 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size = schemaInfo->length;
133 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid = schema_info;
135 /* call the drsuapi version */
136 status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
137 talloc_free(mem_ctx);
139 W_ERROR_NOT_OK_RETURN(status);
144 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
145 bool include_schema_info,
147 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
149 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
152 ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
153 W_ERROR_HAVE_NO_MEMORY(ctr);
155 ctr->num_mappings = schema->num_prefixes;
156 if (include_schema_info) ctr->num_mappings++;
157 ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
158 W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
160 for (i=0; i < schema->num_prefixes; i++) {
161 ctr->mappings[i].id_prefix = schema->prefixes[i].id>>16;
162 ctr->mappings[i].oid.oid = talloc_strndup(ctr->mappings,
163 schema->prefixes[i].oid,
164 schema->prefixes[i].oid_len - 1);
165 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
168 if (include_schema_info) {
169 ctr->mappings[i].id_prefix = 0;
170 ctr->mappings[i].oid.oid = talloc_strdup(ctr->mappings,
171 schema->schema_info);
172 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
179 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
181 struct ldb_val *prefixMap,
182 struct ldb_val *schemaInfo)
185 enum ndr_err_code ndr_err;
186 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
187 struct prefixMapBlob pfm;
189 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
190 W_ERROR_NOT_OK_RETURN(status);
192 pfm.version = PREFIX_MAP_VERSION_DSDB;
196 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
198 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
199 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
200 return ntstatus_to_werror(nt_status);
203 *schemaInfo = strhex_to_data_blob(mem_ctx, schema->schema_info);
204 W_ERROR_HAVE_NO_MEMORY(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)
271 return dsdb_find_prefix_for_oid(schema->num_prefixes, schema->prefixes, in, out);
275 WERROR dsdb_map_int2oid(const struct dsdb_schema *schema, uint32_t in, TALLOC_CTX *mem_ctx, const char **out)
279 for (i=0; i < schema->num_prefixes; i++) {
281 if (schema->prefixes[i].id != (in & 0xFFFF0000)) {
285 val = talloc_asprintf(mem_ctx, "%s%u",
286 schema->prefixes[i].oid,
288 W_ERROR_HAVE_NO_MEMORY(val);
294 return WERR_DS_NO_MSDS_INTID;
298 * this function is called from within a ldb transaction from the schema_fsmo module
300 WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *schema, const char *full_oid)
303 uint32_t num_prefixes;
304 struct dsdb_schema_oid_prefix *prefixes;
308 mem_ctx = talloc_new(ldb);
309 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
311 /* Read prefixes from disk*/
312 status = dsdb_read_prefixes_from_ldb( mem_ctx, ldb, &num_prefixes, &prefixes );
313 if (!W_ERROR_IS_OK(status)) {
314 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb: %s\n",
315 win_errstr(status)));
316 talloc_free(mem_ctx);
320 /* Check if there is a prefix for the oid in the prefixes array*/
321 status = dsdb_find_prefix_for_oid( num_prefixes, prefixes, full_oid, &out );
322 if (W_ERROR_IS_OK(status)) {
324 talloc_free(mem_ctx);
326 } else if (!W_ERROR_EQUAL(WERR_DS_NO_MSDS_INTID, status)) {
328 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_find_prefix_for_oid: %s\n",
329 win_errstr(status)));
330 talloc_free(mem_ctx);
334 /* Create the new mapping for the prefix of full_oid */
335 status = dsdb_prefix_map_update(mem_ctx, &num_prefixes, &prefixes, full_oid);
336 if (!W_ERROR_IS_OK(status)) {
337 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_prefix_map_update: %s\n",
338 win_errstr(status)));
339 talloc_free(mem_ctx);
343 talloc_free(schema->prefixes);
344 schema->prefixes = talloc_steal(schema, prefixes);
345 schema->num_prefixes = num_prefixes;
347 /* Update prefixMap in ldb*/
348 status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
349 if (!W_ERROR_IS_OK(status)) {
350 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
351 win_errstr(status)));
352 talloc_free(mem_ctx);
356 DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
357 full_oid, num_prefixes));
359 talloc_free(mem_ctx);
363 WERROR dsdb_prefix_map_update(TALLOC_CTX *mem_ctx, uint32_t *num_prefixes, struct dsdb_schema_oid_prefix **prefixes, const char *oid)
365 uint32_t new_num_prefixes, index_new_prefix, new_entry_id;
366 const char* lastDotOffset;
369 new_num_prefixes = *num_prefixes + 1;
370 index_new_prefix = *num_prefixes;
373 * this is the algorithm we use to create new mappings for now
375 * TODO: find what algorithm windows use
377 new_entry_id = (*num_prefixes)<<16;
379 /* Extract the prefix from the oid*/
380 lastDotOffset = strrchr(oid, '.');
381 if (lastDotOffset == NULL) {
382 DEBUG(0,("dsdb_prefix_map_update: failed to find the last dot\n"));
383 return WERR_NOT_FOUND;
386 /* Calculate the size of the remainig string that should be the prefix of it */
387 size = strlen(oid) - strlen(lastDotOffset);
389 DEBUG(0,("dsdb_prefix_map_update: size of the remaining string invalid\n"));
392 /* Add one because we need to copy the dot */
395 /* Create a spot in the prefixMap for one more prefix*/
396 (*prefixes) = talloc_realloc(mem_ctx, *prefixes, struct dsdb_schema_oid_prefix, new_num_prefixes);
397 W_ERROR_HAVE_NO_MEMORY(*prefixes);
399 /* Add the new prefix entry*/
400 (*prefixes)[index_new_prefix].id = new_entry_id;
401 (*prefixes)[index_new_prefix].oid = talloc_strndup(mem_ctx, oid, size);
402 (*prefixes)[index_new_prefix].oid_len = strlen((*prefixes)[index_new_prefix].oid);
404 /* Increase num_prefixes because new prefix has been added */
410 WERROR dsdb_find_prefix_for_oid(uint32_t num_prefixes, const struct dsdb_schema_oid_prefix *prefixes, const char *in, uint32_t *out)
414 for (i=0; i < num_prefixes; i++) {
419 if (strncmp(prefixes[i].oid, in, prefixes[i].oid_len) != 0) {
423 val_str = in + prefixes[i].oid_len;
427 if (val_str[0] == '\0') {
428 return WERR_INVALID_PARAM;
431 /* two '.' chars are invalid */
432 if (val_str[0] == '.') {
433 return WERR_INVALID_PARAM;
436 val = strtoul(val_str, &end_str, 10);
437 if (end_str[0] == '.' && end_str[1] != '\0') {
439 * if it's a '.' and not the last char
440 * then maybe an other mapping apply
443 } else if (end_str[0] != '\0') {
444 return WERR_INVALID_PARAM;
445 } else if (val > 0xFFFF) {
446 return WERR_INVALID_PARAM;
449 *out = prefixes[i].id | val;
453 DEBUG(5,(__location__ " Failed to find oid %s - have %u prefixes\n", in, num_prefixes));
455 return WERR_DS_NO_MSDS_INTID;
458 WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
459 const struct dsdb_schema *schema)
461 struct ldb_message msg;
462 struct ldb_dn *schema_dn;
463 struct ldb_message_element el;
464 struct prefixMapBlob pm;
465 struct ldb_val ndr_blob;
466 enum ndr_err_code ndr_err;
470 schema_dn = samdb_schema_dn(ldb);
472 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));
476 pm.version = PREFIX_MAP_VERSION_DSDB;
477 pm.ctr.dsdb.num_mappings = schema->num_prefixes;
478 pm.ctr.dsdb.mappings = talloc_array(mem_ctx,
479 struct drsuapi_DsReplicaOIDMapping,
480 pm.ctr.dsdb.num_mappings);
481 if (!pm.ctr.dsdb.mappings) {
485 for (i=0; i < schema->num_prefixes; i++) {
486 pm.ctr.dsdb.mappings[i].id_prefix = schema->prefixes[i].id>>16;
487 pm.ctr.dsdb.mappings[i].oid.oid = talloc_strdup(pm.ctr.dsdb.mappings, schema->prefixes[i].oid);
490 ndr_err = ndr_push_struct_blob(&ndr_blob, ldb,
491 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
493 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
494 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
499 el.values = &ndr_blob;
500 el.flags = LDB_FLAG_MOD_REPLACE;
501 el.name = talloc_strdup(mem_ctx, "prefixMap");
503 msg.dn = ldb_dn_copy(mem_ctx, schema_dn);
504 msg.num_elements = 1;
507 ret = ldb_modify( ldb, &msg );
509 DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_modify failed\n"));
516 WERROR dsdb_read_prefixes_from_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, uint32_t* num_prefixes, struct dsdb_schema_oid_prefix **prefixes)
518 struct prefixMapBlob *blob;
519 enum ndr_err_code ndr_err;
521 const struct ldb_val *prefix_val;
522 struct ldb_dn *schema_dn;
523 struct ldb_result *schema_res;
525 static const char *schema_attrs[] = {
530 schema_dn = samdb_schema_dn(ldb);
532 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
536 ret = ldb_search(ldb, mem_ctx, &schema_res, schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
537 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
538 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
539 talloc_free(schema_res);
541 } else if (ret != LDB_SUCCESS) {
542 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
543 talloc_free(schema_res);
547 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
549 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
550 talloc_free(schema_res);
554 blob = talloc(mem_ctx, struct prefixMapBlob);
555 W_ERROR_HAVE_NO_MEMORY(blob);
557 ndr_err = ndr_pull_struct_blob(prefix_val, blob,
558 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
560 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
561 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
562 DEBUG(0,("dsdb_read_prefixes_from_ldb: ndr_pull_struct_blob failed\n"));
564 talloc_free(schema_res);
568 talloc_free(schema_res);
570 if (blob->version != PREFIX_MAP_VERSION_DSDB) {
571 DEBUG(0,("dsdb_read_prefixes_from_ldb: blob->version incorect\n"));
576 *num_prefixes = blob->ctr.dsdb.num_mappings;
577 *prefixes = talloc_array(mem_ctx, struct dsdb_schema_oid_prefix, *num_prefixes);
582 for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
584 (*prefixes)[i].id = blob->ctr.dsdb.mappings[i].id_prefix<<16;
585 oid = talloc_strdup(mem_ctx, blob->ctr.dsdb.mappings[i].oid.oid);
586 (*prefixes)[i].oid = talloc_asprintf_append(oid, ".");
587 (*prefixes)[i].oid_len = strlen((*prefixes)[i].oid);
595 this will be replaced with something that looks at the right part of
596 the schema once we know where unique indexing information is hidden
598 static bool dsdb_schema_unique_attribute(const char *attr)
600 const char *attrs[] = { "objectGUID", "objectSID" , NULL };
602 for (i=0;attrs[i];i++) {
603 if (strcasecmp(attr, attrs[i]) == 0) {
612 setup the ldb_schema_attribute field for a dsdb_attribute
614 static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
615 struct dsdb_attribute *attr)
617 const char *syntax = attr->syntax->ldb_syntax;
618 const struct ldb_schema_syntax *s;
619 struct ldb_schema_attribute *a;
622 syntax = attr->syntax->ldap_oid;
625 s = ldb_samba_syntax_by_lDAPDisplayName(ldb, attr->lDAPDisplayName);
627 s = ldb_samba_syntax_by_name(ldb, syntax);
630 s = ldb_standard_syntax_by_name(ldb, syntax);
634 return LDB_ERR_OPERATIONS_ERROR;
637 attr->ldb_schema_attribute = a = talloc(attr, struct ldb_schema_attribute);
638 if (attr->ldb_schema_attribute == NULL) {
640 return LDB_ERR_OPERATIONS_ERROR;
643 a->name = attr->lDAPDisplayName;
647 if (dsdb_schema_unique_attribute(a->name)) {
648 a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
655 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
656 const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
657 if (get_string_val == NULL) { \
659 d_printf("%s: %s == NULL\n", __location__, attr); \
660 return WERR_INVALID_PARAM; \
665 (p)->elem = talloc_strndup(mem_ctx, \
666 (const char *)get_string_val->data, \
667 get_string_val->length); \
669 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
675 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
676 int get_string_list_counter; \
677 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
678 if (get_string_list_el == NULL) { \
680 d_printf("%s: %s == NULL\n", __location__, attr); \
681 return WERR_INVALID_PARAM; \
687 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
688 for (get_string_list_counter=0; \
689 get_string_list_counter < get_string_list_el->num_values; \
690 get_string_list_counter++) { \
691 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
692 (const char *)get_string_list_el->values[get_string_list_counter].data, \
693 get_string_list_el->values[get_string_list_counter].length); \
694 if (!(p)->elem[get_string_list_counter]) { \
695 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
698 (p)->elem[get_string_list_counter+1] = NULL; \
700 talloc_steal(mem_ctx, (p)->elem); \
703 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
705 str = samdb_result_string(msg, attr, NULL);\
708 d_printf("%s: %s == NULL\n", __location__, attr); \
709 return WERR_INVALID_PARAM; \
713 } else if (strcasecmp("TRUE", str) == 0) { \
715 } else if (strcasecmp("FALSE", str) == 0) { \
718 d_printf("%s: %s == %s\n", __location__, attr, str); \
719 return WERR_INVALID_PARAM; \
723 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
724 (p)->elem = samdb_result_uint(msg, attr, 0);\
727 #define GET_UINT32_PTR_LDB(msg, attr, p, elem) do { \
728 uint64_t _v = samdb_result_uint64(msg, attr, UINT64_MAX);\
729 if (_v == UINT64_MAX) { \
731 } else if (_v > UINT32_MAX) { \
732 d_printf("%s: %s == 0x%llX\n", __location__, \
733 attr, (unsigned long long)_v); \
734 return WERR_INVALID_PARAM; \
736 (p)->elem = talloc(mem_ctx, uint32_t); \
738 d_printf("%s: talloc failed for %s\n", __location__, attr); \
741 *(p)->elem = (uint32_t)_v; \
745 #define GET_GUID_LDB(msg, attr, p, elem) do { \
746 (p)->elem = samdb_result_guid(msg, attr);\
749 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
750 const struct ldb_val *_val;\
751 _val = ldb_msg_find_ldb_val(msg, attr);\
754 talloc_steal(mem_ctx, (p)->elem.data);\
756 ZERO_STRUCT((p)->elem);\
760 WERROR dsdb_attribute_from_ldb(struct ldb_context *ldb,
761 const struct dsdb_schema *schema,
762 struct ldb_message *msg,
764 struct dsdb_attribute *attr)
768 GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
769 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
770 GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
771 if (schema->num_prefixes == 0) {
772 /* set an invalid value */
773 attr->attributeID_id = 0xFFFFFFFF;
775 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
776 if (!W_ERROR_IS_OK(status)) {
777 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
778 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
779 win_errstr(status)));
783 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
784 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
786 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
788 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
789 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
790 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
791 GET_UINT32_LDB(msg, "linkID", attr, linkID);
793 GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
794 if (schema->num_prefixes == 0) {
795 /* set an invalid value */
796 attr->attributeSyntax_id = 0xFFFFFFFF;
798 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
799 if (!W_ERROR_IS_OK(status)) {
800 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
801 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
802 win_errstr(status)));
806 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
807 GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
809 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
810 GET_UINT32_PTR_LDB(msg, "rangeLower", attr, rangeLower);
811 GET_UINT32_PTR_LDB(msg, "rangeUpper", attr, rangeUpper);
812 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
814 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
815 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
817 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
818 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
819 GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
820 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
821 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
822 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
823 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
825 attr->syntax = dsdb_syntax_for_attribute(attr);
827 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
830 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
831 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
837 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
838 struct ldb_message *msg,
840 struct dsdb_class *obj)
844 GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
845 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
846 GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
847 if (schema->num_prefixes == 0) {
848 /* set an invalid value */
849 obj->governsID_id = 0xFFFFFFFF;
851 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
852 if (!W_ERROR_IS_OK(status)) {
853 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
854 __location__, obj->lDAPDisplayName, obj->governsID_oid,
855 win_errstr(status)));
859 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
861 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
862 GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
863 GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
865 GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
867 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
868 GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
870 GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
871 GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
872 GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
873 GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
875 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
876 GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
878 GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
880 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
881 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
883 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
884 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
885 GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
886 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
887 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
888 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
889 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
894 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
897 Create a DSDB schema from the ldb results provided. This is called
898 directly when the schema is provisioned from an on-disk LDIF file, or
899 from dsdb_schema_from_schema_dn below
902 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
903 struct smb_iconv_convenience *iconv_convenience,
904 struct ldb_result *schema_res,
905 struct ldb_result *attrs_res, struct ldb_result *objectclass_res,
906 struct dsdb_schema **schema_out,
911 const struct ldb_val *prefix_val;
912 const struct ldb_val *info_val;
913 struct ldb_val info_val_default;
914 struct dsdb_schema *schema;
916 schema = dsdb_new_schema(mem_ctx, iconv_convenience);
918 dsdb_oom(error_string, mem_ctx);
919 return LDB_ERR_OPERATIONS_ERROR;
922 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
924 *error_string = talloc_asprintf(mem_ctx,
925 "schema_fsmo_init: no prefixMap attribute found");
926 return LDB_ERR_CONSTRAINT_VIOLATION;
928 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
930 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
931 if (!info_val_default.data) {
932 dsdb_oom(error_string, mem_ctx);
933 return LDB_ERR_OPERATIONS_ERROR;
935 info_val = &info_val_default;
938 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
939 if (!W_ERROR_IS_OK(status)) {
940 *error_string = talloc_asprintf(mem_ctx,
941 "schema_fsmo_init: failed to load oid mappings: %s",
943 return LDB_ERR_CONSTRAINT_VIOLATION;
946 for (i=0; i < attrs_res->count; i++) {
947 struct dsdb_attribute *sa;
949 sa = talloc_zero(schema, struct dsdb_attribute);
951 dsdb_oom(error_string, mem_ctx);
952 return LDB_ERR_OPERATIONS_ERROR;
955 status = dsdb_attribute_from_ldb(ldb, schema, attrs_res->msgs[i], sa, sa);
956 if (!W_ERROR_IS_OK(status)) {
957 *error_string = talloc_asprintf(mem_ctx,
958 "schema_fsmo_init: failed to load attribute definition: %s:%s",
959 ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
961 return LDB_ERR_CONSTRAINT_VIOLATION;
964 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
967 for (i=0; i < objectclass_res->count; i++) {
968 struct dsdb_class *sc;
970 sc = talloc_zero(schema, struct dsdb_class);
972 dsdb_oom(error_string, mem_ctx);
973 return LDB_ERR_OPERATIONS_ERROR;
976 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
977 if (!W_ERROR_IS_OK(status)) {
978 *error_string = talloc_asprintf(mem_ctx,
979 "schema_fsmo_init: failed to load class definition: %s:%s",
980 ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
982 return LDB_ERR_CONSTRAINT_VIOLATION;
985 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
988 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
989 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
990 schema->fsmo.we_are_master = true;
992 schema->fsmo.we_are_master = false;
995 DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
996 (schema->fsmo.we_are_master?"yes":"no")));
998 *schema_out = schema;
1003 Given an LDB, and the DN, return a populated schema
1006 int dsdb_schema_from_schema_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
1007 struct smb_iconv_convenience *iconv_convenience,
1008 struct ldb_dn *schema_dn,
1009 struct dsdb_schema **schema,
1010 char **error_string_out)
1012 TALLOC_CTX *tmp_ctx;
1016 struct ldb_result *schema_res;
1017 struct ldb_result *a_res;
1018 struct ldb_result *c_res;
1019 static const char *schema_attrs[] = {
1026 tmp_ctx = talloc_new(mem_ctx);
1028 dsdb_oom(error_string_out, mem_ctx);
1029 return LDB_ERR_OPERATIONS_ERROR;
1033 * setup the prefix mappings and schema info
1035 ret = ldb_search(ldb, tmp_ctx, &schema_res,
1036 schema_dn, LDB_SCOPE_BASE, schema_attrs, NULL);
1037 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1038 talloc_free(tmp_ctx);
1040 } else if (ret != LDB_SUCCESS) {
1041 *error_string_out = talloc_asprintf(mem_ctx,
1042 "dsdb_schema: failed to search the schema head: %s",
1043 ldb_errstring(ldb));
1044 talloc_free(tmp_ctx);
1047 if (schema_res->count != 1) {
1048 *error_string_out = talloc_asprintf(mem_ctx,
1049 "dsdb_schema: [%u] schema heads found on a base search",
1051 talloc_free(tmp_ctx);
1052 return LDB_ERR_CONSTRAINT_VIOLATION;
1056 * load the attribute definitions
1058 ret = ldb_search(ldb, tmp_ctx, &a_res,
1059 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
1060 "(objectClass=attributeSchema)");
1061 if (ret != LDB_SUCCESS) {
1062 *error_string_out = talloc_asprintf(mem_ctx,
1063 "dsdb_schema: failed to search attributeSchema objects: %s",
1064 ldb_errstring(ldb));
1065 talloc_free(tmp_ctx);
1070 * load the objectClass definitions
1072 ret = ldb_search(ldb, tmp_ctx, &c_res,
1073 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
1074 "(objectClass=classSchema)");
1075 if (ret != LDB_SUCCESS) {
1076 *error_string_out = talloc_asprintf(mem_ctx,
1077 "dsdb_schema: failed to search attributeSchema objects: %s",
1078 ldb_errstring(ldb));
1079 talloc_free(tmp_ctx);
1083 ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
1084 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1085 schema_res, a_res, c_res, schema, &error_string);
1086 if (ret != LDB_SUCCESS) {
1087 *error_string_out = talloc_asprintf(mem_ctx,
1088 "dsdb_schema load failed: %s",
1090 talloc_free(tmp_ctx);
1093 talloc_steal(mem_ctx, *schema);
1094 talloc_free(tmp_ctx);
1100 static const struct {
1103 } name_mappings[] = {
1104 { "cn", "2.5.4.3" },
1105 { "name", "1.2.840.113556.1.4.1" },
1106 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
1107 { "attributeID", "1.2.840.113556.1.2.30" },
1108 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
1109 { "mAPIID", "1.2.840.113556.1.2.49" },
1110 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
1111 { "searchFlags", "1.2.840.113556.1.2.334" },
1112 { "systemFlags", "1.2.840.113556.1.4.375" },
1113 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
1114 { "linkID", "1.2.840.113556.1.2.50" },
1115 { "attributeSyntax", "1.2.840.113556.1.2.32" },
1116 { "oMSyntax", "1.2.840.113556.1.2.231" },
1117 { "oMObjectClass", "1.2.840.113556.1.2.218" },
1118 { "isSingleValued", "1.2.840.113556.1.2.33" },
1119 { "rangeLower", "1.2.840.113556.1.2.34" },
1120 { "rangeUpper", "1.2.840.113556.1.2.35" },
1121 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
1122 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
1123 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
1124 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
1125 { "adminDisplayName", "1.2.840.113556.1.2.194" },
1126 { "adminDescription", "1.2.840.113556.1.2.226" },
1127 { "classDisplayName", "1.2.840.113556.1.4.610" },
1128 { "isEphemeral", "1.2.840.113556.1.4.1212" },
1129 { "isDefunct", "1.2.840.113556.1.4.661" },
1130 { "systemOnly", "1.2.840.113556.1.4.170" },
1131 { "governsID", "1.2.840.113556.1.2.22" },
1132 { "objectClassCategory", "1.2.840.113556.1.2.370" },
1133 { "rDNAttID", "1.2.840.113556.1.2.26" },
1134 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
1135 { "subClassOf", "1.2.840.113556.1.2.21" },
1136 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
1137 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
1138 { "systemMustContain", "1.2.840.113556.1.4.197" },
1139 { "systemMayContain", "1.2.840.113556.1.4.196" },
1140 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
1141 { "possSuperiors", "1.2.840.113556.1.2.8" },
1142 { "mustContain", "1.2.840.113556.1.2.24" },
1143 { "mayContain", "1.2.840.113556.1.2.25" },
1144 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
1145 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
1148 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
1149 struct drsuapi_DsReplicaObject *obj,
1155 const char *oid = NULL;
1157 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
1158 if (strcmp(name_mappings[i].name, name) != 0) continue;
1160 oid = name_mappings[i].oid;
1168 status = dsdb_map_oid2int(schema, oid, &id);
1169 if (!W_ERROR_IS_OK(status)) {
1173 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
1174 if (obj->attribute_ctr.attributes[i].attid != id) continue;
1177 return &obj->attribute_ctr.attributes[i];
1183 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1184 struct drsuapi_DsReplicaAttribute *_a; \
1185 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1186 if (strict && !_a) { \
1187 d_printf("%s: %s == NULL\n", __location__, attr); \
1188 return WERR_INVALID_PARAM; \
1190 if (strict && _a->value_ctr.num_values != 1) { \
1191 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1192 _a->value_ctr.num_values); \
1193 return WERR_INVALID_PARAM; \
1195 if (_a && _a->value_ctr.num_values >= 1) { \
1197 if (!convert_string_talloc_convenience(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
1198 _a->value_ctr.values[0].blob->data, \
1199 _a->value_ctr.values[0].blob->length, \
1200 (void **)discard_const(&(p)->elem), &_ret, false)) { \
1201 DEBUG(0,("%s: invalid data!\n", attr)); \
1203 _a->value_ctr.values[0].blob->data, \
1204 _a->value_ctr.values[0].blob->length); \
1205 return WERR_FOOBAR; \
1212 #define GET_UINT32_LIST_DS(s, r, attr, mem_ctx, p, elem) do { \
1214 struct drsuapi_DsReplicaAttribute *_a; \
1215 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1216 (p)->elem = _a ? talloc_array(mem_ctx, uint32_t, _a->value_ctr.num_values + 1) : NULL; \
1217 for (list_counter=0; \
1218 _a && list_counter < _a->value_ctr.num_values; \
1220 if (_a->value_ctr.values[list_counter].blob->length != 4) { \
1221 return WERR_INVALID_PARAM; \
1223 (p)->elem[list_counter] = IVAL(_a->value_ctr.values[list_counter].blob->data, 0); \
1225 if (_a) (p)->elem[list_counter] = 0; \
1228 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1229 struct drsuapi_DsReplicaAttribute *_a; \
1230 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1231 if (strict && !_a) { \
1232 d_printf("%s: %s == NULL\n", __location__, attr); \
1233 return WERR_INVALID_PARAM; \
1235 if (strict && _a->value_ctr.num_values != 1) { \
1236 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1237 _a->value_ctr.num_values); \
1238 return WERR_INVALID_PARAM; \
1240 if (strict && !_a->value_ctr.values[0].blob) { \
1241 d_printf("%s: %s data == NULL\n", __location__, attr); \
1242 return WERR_INVALID_PARAM; \
1244 if (_a && _a->value_ctr.num_values >= 1 \
1245 && _a->value_ctr.values[0].blob) { \
1246 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
1247 enum ndr_err_code _ndr_err; \
1248 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1249 mem_ctx, s->iconv_convenience, &_id3,\
1250 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
1251 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1252 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1253 return ntstatus_to_werror(_nt_status); \
1255 (p)->elem = _id3.dn; \
1261 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
1262 struct drsuapi_DsReplicaAttribute *_a; \
1263 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1264 if (strict && !_a) { \
1265 d_printf("%s: %s == NULL\n", __location__, attr); \
1266 return WERR_INVALID_PARAM; \
1268 if (strict && _a->value_ctr.num_values != 1) { \
1269 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1270 (unsigned int)_a->value_ctr.num_values); \
1271 return WERR_INVALID_PARAM; \
1273 if (strict && !_a->value_ctr.values[0].blob) { \
1274 d_printf("%s: %s data == NULL\n", __location__, attr); \
1275 return WERR_INVALID_PARAM; \
1277 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
1278 d_printf("%s: %s length == %u\n", __location__, attr, \
1279 (unsigned int)_a->value_ctr.values[0].blob->length); \
1280 return WERR_INVALID_PARAM; \
1282 if (_a && _a->value_ctr.num_values >= 1 \
1283 && _a->value_ctr.values[0].blob \
1284 && _a->value_ctr.values[0].blob->length == 4) { \
1285 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
1287 (p)->elem = false; \
1291 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
1292 struct drsuapi_DsReplicaAttribute *_a; \
1293 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1294 if (_a && _a->value_ctr.num_values >= 1 \
1295 && _a->value_ctr.values[0].blob \
1296 && _a->value_ctr.values[0].blob->length == 4) { \
1297 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1303 #define GET_UINT32_PTR_DS(s, r, attr, p, elem) do { \
1304 struct drsuapi_DsReplicaAttribute *_a; \
1305 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1306 if (_a && _a->value_ctr.num_values >= 1 \
1307 && _a->value_ctr.values[0].blob \
1308 && _a->value_ctr.values[0].blob->length == 4) { \
1309 (p)->elem = talloc(mem_ctx, uint32_t); \
1311 d_printf("%s: talloc failed for %s\n", __location__, attr); \
1312 return WERR_NOMEM; \
1314 *(p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1320 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1321 struct drsuapi_DsReplicaAttribute *_a; \
1322 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1323 if (_a && _a->value_ctr.num_values >= 1 \
1324 && _a->value_ctr.values[0].blob \
1325 && _a->value_ctr.values[0].blob->length == 16) { \
1326 enum ndr_err_code _ndr_err; \
1327 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1328 mem_ctx, s->iconv_convenience, &(p)->elem, \
1329 (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1330 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1331 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1332 return ntstatus_to_werror(_nt_status); \
1335 ZERO_STRUCT((p)->elem);\
1339 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1340 struct drsuapi_DsReplicaAttribute *_a; \
1341 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1342 if (_a && _a->value_ctr.num_values >= 1 \
1343 && _a->value_ctr.values[0].blob) { \
1344 (p)->elem = *_a->value_ctr.values[0].blob;\
1345 talloc_steal(mem_ctx, (p)->elem.data); \
1347 ZERO_STRUCT((p)->elem);\
1351 WERROR dsdb_attribute_from_drsuapi(struct ldb_context *ldb,
1352 struct dsdb_schema *schema,
1353 struct drsuapi_DsReplicaObject *r,
1354 TALLOC_CTX *mem_ctx,
1355 struct dsdb_attribute *attr)
1359 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1360 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1361 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1362 status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
1363 if (!W_ERROR_IS_OK(status)) {
1364 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1365 __location__, attr->lDAPDisplayName, attr->attributeID_id,
1366 win_errstr(status)));
1369 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1370 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1372 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1374 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1375 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1376 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1377 GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1379 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1380 status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
1381 if (!W_ERROR_IS_OK(status)) {
1382 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1383 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1384 win_errstr(status)));
1387 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1388 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1390 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1391 GET_UINT32_PTR_DS(schema, r, "rangeLower", attr, rangeLower);
1392 GET_UINT32_PTR_DS(schema, r, "rangeUpper", attr, rangeUpper);
1393 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1395 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1396 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1398 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1399 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1400 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1401 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1402 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1403 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1404 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1406 attr->syntax = dsdb_syntax_for_attribute(attr);
1407 if (!attr->syntax) {
1408 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1411 if (dsdb_schema_setup_ldb_schema_attribute(ldb, attr) != LDB_SUCCESS) {
1412 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1418 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1419 struct drsuapi_DsReplicaObject *r,
1420 TALLOC_CTX *mem_ctx,
1421 struct dsdb_class *obj)
1425 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1426 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1427 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1428 status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
1429 if (!W_ERROR_IS_OK(status)) {
1430 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1431 __location__, obj->lDAPDisplayName, obj->governsID_id,
1432 win_errstr(status)));
1435 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1437 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1438 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1439 GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1441 GET_UINT32_DS(schema, r, "subClassOf", obj, subClassOf_id);
1443 GET_UINT32_LIST_DS(schema, r, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass_ids);
1444 GET_UINT32_LIST_DS(schema, r, "auxiliaryClass", mem_ctx, obj, auxiliaryClass_ids);
1446 GET_UINT32_LIST_DS(schema, r, "systemMustContain", mem_ctx, obj, systemMustContain_ids);
1447 GET_UINT32_LIST_DS(schema, r, "systemMayContain", mem_ctx, obj, systemMayContain_ids);
1448 GET_UINT32_LIST_DS(schema, r, "mustContain", mem_ctx, obj, mustContain_ids);
1449 GET_UINT32_LIST_DS(schema, r, "mayContain", mem_ctx, obj, mayContain_ids);
1451 GET_UINT32_LIST_DS(schema, r, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors_ids);
1452 GET_UINT32_LIST_DS(schema, r, "possSuperiors", mem_ctx, obj, possSuperiors_ids);
1454 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1456 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1457 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1459 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1460 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1461 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1462 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1463 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1464 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1465 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);