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"
32 struct dsdb_schema *dsdb_new_schema(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
34 struct dsdb_schema *schema = talloc_zero(mem_ctx, struct dsdb_schema);
39 schema->iconv_convenience = iconv_convenience;
44 WERROR dsdb_load_oid_mappings_drsuapi(struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
48 schema->prefixes = talloc_array(schema, struct dsdb_schema_oid_prefix, ctr->num_mappings);
49 W_ERROR_HAVE_NO_MEMORY(schema->prefixes);
51 for (i=0, j=0; i < ctr->num_mappings; i++) {
52 if (ctr->mappings[i].oid.oid == NULL) {
53 return WERR_INVALID_PARAM;
56 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
57 if (ctr->mappings[i].id_prefix != 0) {
58 return WERR_INVALID_PARAM;
61 /* the magic value should be in the last array member */
62 if (i != (ctr->num_mappings - 1)) {
63 return WERR_INVALID_PARAM;
66 if (ctr->mappings[i].oid.__ndr_size != 21) {
67 return WERR_INVALID_PARAM;
70 schema->schema_info = talloc_strdup(schema, ctr->mappings[i].oid.oid);
71 W_ERROR_HAVE_NO_MEMORY(schema->schema_info);
73 /* the last array member should contain the magic value not a oid */
74 if (i == (ctr->num_mappings - 1)) {
75 return WERR_INVALID_PARAM;
78 schema->prefixes[j].id = ctr->mappings[i].id_prefix<<16;
79 schema->prefixes[j].oid = talloc_asprintf(schema->prefixes, "%s.",
80 ctr->mappings[i].oid.oid);
81 W_ERROR_HAVE_NO_MEMORY(schema->prefixes[j].oid);
82 schema->prefixes[j].oid_len = strlen(schema->prefixes[j].oid);
87 schema->num_prefixes = j;
91 WERROR dsdb_load_oid_mappings_ldb(struct dsdb_schema *schema,
92 const struct ldb_val *prefixMap,
93 const struct ldb_val *schemaInfo)
96 enum ndr_err_code ndr_err;
97 struct prefixMapBlob pfm;
100 TALLOC_CTX *mem_ctx = talloc_new(schema);
101 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
103 ndr_err = ndr_pull_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
104 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
105 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
106 talloc_free(mem_ctx);
107 return ntstatus_to_werror(nt_status);
110 if (pfm.version != PREFIX_MAP_VERSION_DSDB) {
111 talloc_free(mem_ctx);
115 if (schemaInfo->length != 21 && schemaInfo->data[0] == 0xFF) {
116 talloc_free(mem_ctx);
120 /* append the schema info as last element */
121 pfm.ctr.dsdb.num_mappings++;
122 pfm.ctr.dsdb.mappings = talloc_realloc(mem_ctx, pfm.ctr.dsdb.mappings,
123 struct drsuapi_DsReplicaOIDMapping,
124 pfm.ctr.dsdb.num_mappings);
125 W_ERROR_HAVE_NO_MEMORY(pfm.ctr.dsdb.mappings);
127 schema_info = data_blob_hex_string(pfm.ctr.dsdb.mappings, schemaInfo);
128 W_ERROR_HAVE_NO_MEMORY(schema_info);
130 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].id_prefix = 0;
131 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.__ndr_size = schemaInfo->length;
132 pfm.ctr.dsdb.mappings[pfm.ctr.dsdb.num_mappings - 1].oid.oid = schema_info;
134 /* call the drsuapi version */
135 status = dsdb_load_oid_mappings_drsuapi(schema, &pfm.ctr.dsdb);
136 talloc_free(mem_ctx);
138 W_ERROR_NOT_OK_RETURN(status);
143 WERROR dsdb_get_oid_mappings_drsuapi(const struct dsdb_schema *schema,
144 bool include_schema_info,
146 struct drsuapi_DsReplicaOIDMapping_Ctr **_ctr)
148 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
151 ctr = talloc(mem_ctx, struct drsuapi_DsReplicaOIDMapping_Ctr);
152 W_ERROR_HAVE_NO_MEMORY(ctr);
154 ctr->num_mappings = schema->num_prefixes;
155 if (include_schema_info) ctr->num_mappings++;
156 ctr->mappings = talloc_array(schema, struct drsuapi_DsReplicaOIDMapping, ctr->num_mappings);
157 W_ERROR_HAVE_NO_MEMORY(ctr->mappings);
159 for (i=0; i < schema->num_prefixes; i++) {
160 ctr->mappings[i].id_prefix = schema->prefixes[i].id>>16;
161 ctr->mappings[i].oid.oid = talloc_strndup(ctr->mappings,
162 schema->prefixes[i].oid,
163 schema->prefixes[i].oid_len - 1);
164 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
167 if (include_schema_info) {
168 ctr->mappings[i].id_prefix = 0;
169 ctr->mappings[i].oid.oid = talloc_strdup(ctr->mappings,
170 schema->schema_info);
171 W_ERROR_HAVE_NO_MEMORY(ctr->mappings[i].oid.oid);
178 WERROR dsdb_get_oid_mappings_ldb(const struct dsdb_schema *schema,
180 struct ldb_val *prefixMap,
181 struct ldb_val *schemaInfo)
184 enum ndr_err_code ndr_err;
185 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
186 struct prefixMapBlob pfm;
188 status = dsdb_get_oid_mappings_drsuapi(schema, false, mem_ctx, &ctr);
189 W_ERROR_NOT_OK_RETURN(status);
191 pfm.version = PREFIX_MAP_VERSION_DSDB;
195 ndr_err = ndr_push_struct_blob(prefixMap, mem_ctx, schema->iconv_convenience, &pfm, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
197 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
198 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
199 return ntstatus_to_werror(nt_status);
202 *schemaInfo = strhex_to_data_blob(schema->schema_info);
203 W_ERROR_HAVE_NO_MEMORY(schemaInfo->data);
204 talloc_steal(mem_ctx, schemaInfo->data);
209 WERROR dsdb_verify_oid_mappings_drsuapi(const struct dsdb_schema *schema, const struct drsuapi_DsReplicaOIDMapping_Ctr *ctr)
213 for (i=0; i < ctr->num_mappings; i++) {
214 if (ctr->mappings[i].oid.oid == NULL) {
215 return WERR_INVALID_PARAM;
218 if (strncasecmp(ctr->mappings[i].oid.oid, "ff", 2) == 0) {
219 if (ctr->mappings[i].id_prefix != 0) {
220 return WERR_INVALID_PARAM;
223 /* the magic value should be in the last array member */
224 if (i != (ctr->num_mappings - 1)) {
225 return WERR_INVALID_PARAM;
228 if (ctr->mappings[i].oid.__ndr_size != 21) {
229 return WERR_INVALID_PARAM;
232 if (strcasecmp(schema->schema_info, ctr->mappings[i].oid.oid) != 0) {
233 return WERR_DS_DRA_SCHEMA_MISMATCH;
236 /* the last array member should contain the magic value not a oid */
237 if (i == (ctr->num_mappings - 1)) {
238 return WERR_INVALID_PARAM;
241 for (j=0; j < schema->num_prefixes; j++) {
243 if (schema->prefixes[j].id != (ctr->mappings[i].id_prefix<<16)) {
247 oid_len = strlen(ctr->mappings[i].oid.oid);
249 if (oid_len != (schema->prefixes[j].oid_len - 1)) {
250 return WERR_DS_DRA_SCHEMA_MISMATCH;
253 if (strncmp(ctr->mappings[i].oid.oid, schema->prefixes[j].oid, oid_len) != 0) {
254 return WERR_DS_DRA_SCHEMA_MISMATCH;
260 if (j == schema->num_prefixes) {
261 return WERR_DS_DRA_SCHEMA_MISMATCH;
269 WERROR dsdb_map_oid2int(const struct dsdb_schema *schema, const char *in, uint32_t *out)
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;
305 struct ldb_val ndr_blob;
309 mem_ctx = talloc_new(ldb);
310 W_ERROR_HAVE_NO_MEMORY(mem_ctx);
312 /* Read prefixes from disk*/
313 status = dsdb_read_prefixes_from_ldb( mem_ctx, ldb, &num_prefixes, &prefixes );
314 if (!W_ERROR_IS_OK(status)) {
315 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_read_prefixes_from_ldb failed\n"));
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);
327 /* Update prefix map in ldb*/
328 /* Update the prefixes */
329 status = dsdb_prefix_map_update(mem_ctx, &num_prefixes, &prefixes, full_oid);
330 if (!W_ERROR_IS_OK(status)) {
331 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_prefix_map_update failed\n"));
332 talloc_free(mem_ctx);
335 /* Convert prefixes in ndr blob*/
336 status = dsdb_write_prefixes_to_ndr( mem_ctx, ldb, num_prefixes, prefixes, &ndr_blob );
337 if (!W_ERROR_IS_OK(status)) {
338 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ndr failed\n"));
339 talloc_free(mem_ctx);
343 /* Update prefixMap in ldb*/
344 status = dsdb_write_prefixes_to_ldb( mem_ctx, ldb, &ndr_blob );
345 if (!W_ERROR_IS_OK(status)) {
346 DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb failed\n"));
347 talloc_free(mem_ctx);
351 talloc_free(mem_ctx);
355 WERROR dsdb_prefix_map_update(TALLOC_CTX *mem_ctx, uint32_t *num_prefixes, struct dsdb_schema_oid_prefix **prefixes, const char *oid)
357 uint32_t new_num_prefixes, index_new_prefix, new_entry_id;
358 const char* lastDotOffset;
361 new_num_prefixes = *num_prefixes + 1;
362 index_new_prefix = *num_prefixes;
363 new_entry_id = (*num_prefixes)<<16;
365 /* Extract the prefix from the oid*/
366 lastDotOffset = strrchr(oid, '.');
367 if (lastDotOffset == NULL) {
368 DEBUG(0,("dsdb_prefix_map_update: failed to find the last dot\n"));
369 return WERR_NOT_FOUND;
372 /* Calculate the size of the remainig string that should be the prefix of it */
373 size = strlen(oid) - strlen(lastDotOffset);
375 DEBUG(0,("dsdb_prefix_map_update: size of the remaining string invalid\n"));
378 /* Add one because we need to copy the dot */
381 /* Create a spot in the prefixMap for one more prefix*/
382 (*prefixes) = talloc_realloc(mem_ctx, *prefixes, struct dsdb_schema_oid_prefix, new_num_prefixes);
383 W_ERROR_HAVE_NO_MEMORY(*prefixes);
385 /* Add the new prefix entry*/
386 (*prefixes)[index_new_prefix].id = new_entry_id;
387 (*prefixes)[index_new_prefix].oid = talloc_strndup(mem_ctx, oid, size);
388 (*prefixes)[index_new_prefix].oid_len = strlen((*prefixes)[index_new_prefix].oid);
390 /* Increase num_prefixes because new prefix has been added */
396 WERROR dsdb_find_prefix_for_oid(uint32_t num_prefixes, const struct dsdb_schema_oid_prefix *prefixes, const char *in, uint32_t *out)
400 for (i=0; i < num_prefixes; i++) {
405 if (strncmp(prefixes[i].oid, in, prefixes[i].oid_len) != 0) {
409 val_str = in + prefixes[i].oid_len;
413 if (val_str[0] == '\0') {
414 return WERR_INVALID_PARAM;
417 /* two '.' chars are invalid */
418 if (val_str[0] == '.') {
419 return WERR_INVALID_PARAM;
422 val = strtoul(val_str, &end_str, 10);
423 if (end_str[0] == '.' && end_str[1] != '\0') {
425 * if it's a '.' and not the last char
426 * then maybe an other mapping apply
429 } else if (end_str[0] != '\0') {
430 return WERR_INVALID_PARAM;
431 } else if (val > 0xFFFF) {
432 return WERR_INVALID_PARAM;
435 *out = prefixes[i].id | val;
439 return WERR_DS_NO_MSDS_INTID;
442 WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldb_val *ndr_blob)
444 struct ldb_message msg;
445 struct ldb_dn *schema_dn;
446 struct ldb_message_element el;
449 schema_dn = samdb_schema_dn(ldb);
451 DEBUG(0,("dsdb_write_prefixes_to_ldb: no schema dn present\n"));
456 el.values = ndr_blob;
457 el.flags = LDB_FLAG_MOD_REPLACE;
458 el.name = talloc_strdup(mem_ctx, "prefixMap");
460 msg.dn = ldb_dn_copy(mem_ctx, schema_dn);
461 msg.num_elements = 1;
464 ret = ldb_modify( ldb, &msg );
466 DEBUG(0,("dsdb_write_prefixes_to_ldb: ldb_modify failed\n"));
473 WERROR dsdb_read_prefixes_from_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, uint32_t* num_prefixes, struct dsdb_schema_oid_prefix **prefixes)
475 struct prefixMapBlob *blob;
476 enum ndr_err_code ndr_err;
478 const struct ldb_val *prefix_val;
479 struct ldb_dn *schema_dn;
480 struct ldb_result *schema_res;
482 static const char *schema_attrs[] = {
487 schema_dn = samdb_schema_dn(ldb);
489 DEBUG(0,("dsdb_read_prefixes_from_ldb: no schema dn present\n"));
493 ret = ldb_search(ldb, schema_dn, LDB_SCOPE_BASE,NULL, schema_attrs,&schema_res);
494 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
495 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefix map present\n"));
497 } else if (ret != LDB_SUCCESS) {
498 DEBUG(0,("dsdb_read_prefixes_from_ldb: failed to search the schema head\n"));
502 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
504 DEBUG(0,("dsdb_read_prefixes_from_ldb: no prefixMap attribute found\n"));
508 blob = talloc(mem_ctx, struct prefixMapBlob);
509 W_ERROR_HAVE_NO_MEMORY(blob);
511 ndr_err = ndr_pull_struct_blob(prefix_val, blob,
512 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
514 (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
515 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
516 DEBUG(0,("dsdb_read_prefixes_from_ldb: ndr_pull_struct_blob failed\n"));
521 if (blob->version != PREFIX_MAP_VERSION_DSDB) {
522 DEBUG(0,("dsdb_read_prefixes_from_ldb: blob->version incorect\n"));
527 *num_prefixes = blob->ctr.dsdb.num_mappings;
528 *prefixes = talloc_array(mem_ctx, struct dsdb_schema_oid_prefix, *num_prefixes);
533 for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
534 (*prefixes)[i].id = blob->ctr.dsdb.mappings[i].id_prefix<<16;
535 (*prefixes)[i].oid = talloc_strdup(mem_ctx, blob->ctr.dsdb.mappings[i].oid.oid);
536 (*prefixes)[i].oid = talloc_asprintf_append((*prefixes)[i].oid, ".");
537 (*prefixes)[i].oid_len = strlen(blob->ctr.dsdb.mappings[i].oid.oid);
545 WERROR dsdb_write_prefixes_to_ndr(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, uint32_t num_prefixes, const struct dsdb_schema_oid_prefix *prefixes, struct ldb_val *out)
547 struct prefixMapBlob *blob;
548 enum ndr_err_code ndr_err;
551 blob = talloc_zero(mem_ctx, struct prefixMapBlob);
552 W_ERROR_HAVE_NO_MEMORY(blob);
554 blob->version = PREFIX_MAP_VERSION_DSDB;
555 blob->ctr.dsdb.num_mappings = num_prefixes;
556 blob->ctr.dsdb.mappings = talloc_realloc(blob,
557 blob->ctr.dsdb.mappings,
558 struct drsuapi_DsReplicaOIDMapping,
559 blob->ctr.dsdb.num_mappings);
560 if (!blob->ctr.dsdb.mappings) {
564 for (i=0; i < num_prefixes; i++) {
565 blob->ctr.dsdb.mappings[i].id_prefix = prefixes[i].id>>16;
566 blob->ctr.dsdb.mappings[i].oid.oid = talloc_strdup(blob->ctr.dsdb.mappings, prefixes[i].oid);
569 ndr_err = ndr_push_struct_blob(out, ldb,
570 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
572 (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
573 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
581 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
582 (p)->elem = samdb_result_string(msg, attr, NULL);\
583 if (strict && (p)->elem == NULL) { \
584 d_printf("%s: %s == NULL\n", __location__, attr); \
585 return WERR_INVALID_PARAM; \
587 talloc_steal(mem_ctx, (p)->elem); \
590 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
591 int get_string_list_counter; \
592 struct ldb_message_element *get_string_list_el = ldb_msg_find_element(msg, attr); \
593 if (get_string_list_el == NULL) { \
595 d_printf("%s: %s == NULL\n", __location__, attr); \
596 return WERR_INVALID_PARAM; \
602 (p)->elem = talloc_array(mem_ctx, const char *, get_string_list_el->num_values + 1); \
603 for (get_string_list_counter=0; \
604 get_string_list_counter < get_string_list_el->num_values; \
605 get_string_list_counter++) { \
606 (p)->elem[get_string_list_counter] = talloc_strndup((p)->elem, \
607 (const char *)get_string_list_el->values[get_string_list_counter].data, \
608 get_string_list_el->values[get_string_list_counter].length); \
609 if (!(p)->elem[get_string_list_counter]) { \
610 d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
613 (p)->elem[get_string_list_counter+1] = NULL; \
615 talloc_steal(mem_ctx, (p)->elem); \
618 #define GET_BOOL_LDB(msg, attr, p, elem, strict) do { \
620 str = samdb_result_string(msg, attr, NULL);\
623 d_printf("%s: %s == NULL\n", __location__, attr); \
624 return WERR_INVALID_PARAM; \
628 } else if (strcasecmp("TRUE", str) == 0) { \
630 } else if (strcasecmp("FALSE", str) == 0) { \
633 d_printf("%s: %s == %s\n", __location__, attr, str); \
634 return WERR_INVALID_PARAM; \
638 #define GET_UINT32_LDB(msg, attr, p, elem) do { \
639 (p)->elem = samdb_result_uint(msg, attr, 0);\
642 #define GET_GUID_LDB(msg, attr, p, elem) do { \
643 (p)->elem = samdb_result_guid(msg, attr);\
646 #define GET_BLOB_LDB(msg, attr, mem_ctx, p, elem) do { \
647 const struct ldb_val *_val;\
648 _val = ldb_msg_find_ldb_val(msg, attr);\
651 talloc_steal(mem_ctx, (p)->elem.data);\
653 ZERO_STRUCT((p)->elem);\
657 WERROR dsdb_attribute_from_ldb(const struct dsdb_schema *schema,
658 struct ldb_message *msg,
660 struct dsdb_attribute *attr)
664 GET_STRING_LDB(msg, "cn", mem_ctx, attr, cn, false);
665 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
666 GET_STRING_LDB(msg, "attributeID", mem_ctx, attr, attributeID_oid, true);
667 if (schema->num_prefixes == 0) {
668 /* set an invalid value */
669 attr->attributeID_id = 0xFFFFFFFF;
671 status = dsdb_map_oid2int(schema, attr->attributeID_oid, &attr->attributeID_id);
672 if (!W_ERROR_IS_OK(status)) {
673 DEBUG(0,("%s: '%s': unable to map attributeID %s: %s\n",
674 __location__, attr->lDAPDisplayName, attr->attributeID_oid,
675 win_errstr(status)));
679 GET_GUID_LDB(msg, "schemaIDGUID", attr, schemaIDGUID);
680 GET_UINT32_LDB(msg, "mAPIID", attr, mAPIID);
682 GET_GUID_LDB(msg, "attributeSecurityGUID", attr, attributeSecurityGUID);
684 GET_UINT32_LDB(msg, "searchFlags", attr, searchFlags);
685 GET_UINT32_LDB(msg, "systemFlags", attr, systemFlags);
686 GET_BOOL_LDB(msg, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
687 GET_UINT32_LDB(msg, "linkID", attr, linkID);
689 GET_STRING_LDB(msg, "attributeSyntax", mem_ctx, attr, attributeSyntax_oid, true);
690 if (schema->num_prefixes == 0) {
691 /* set an invalid value */
692 attr->attributeSyntax_id = 0xFFFFFFFF;
694 status = dsdb_map_oid2int(schema, attr->attributeSyntax_oid, &attr->attributeSyntax_id);
695 if (!W_ERROR_IS_OK(status)) {
696 DEBUG(0,("%s: '%s': unable to map attributeSyntax_ %s: %s\n",
697 __location__, attr->lDAPDisplayName, attr->attributeSyntax_oid,
698 win_errstr(status)));
702 GET_UINT32_LDB(msg, "oMSyntax", attr, oMSyntax);
703 GET_BLOB_LDB(msg, "oMObjectClass", mem_ctx, attr, oMObjectClass);
705 GET_BOOL_LDB(msg, "isSingleValued", attr, isSingleValued, true);
706 GET_UINT32_LDB(msg, "rangeLower", attr, rangeLower);
707 GET_UINT32_LDB(msg, "rangeUpper", attr, rangeUpper);
708 GET_BOOL_LDB(msg, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
710 GET_UINT32_LDB(msg, "schemaFlagsEx", attr, schemaFlagsEx);
711 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
713 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
714 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
715 GET_STRING_LDB(msg, "adminDescription", mem_ctx, attr, adminDescription, false);
716 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, attr, classDisplayName, false);
717 GET_BOOL_LDB(msg, "isEphemeral", attr, isEphemeral, false);
718 GET_BOOL_LDB(msg, "isDefunct", attr, isDefunct, false);
719 GET_BOOL_LDB(msg, "systemOnly", attr, systemOnly, false);
721 attr->syntax = dsdb_syntax_for_attribute(attr);
723 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
729 WERROR dsdb_class_from_ldb(const struct dsdb_schema *schema,
730 struct ldb_message *msg,
732 struct dsdb_class *obj)
736 GET_STRING_LDB(msg, "cn", mem_ctx, obj, cn, false);
737 GET_STRING_LDB(msg, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
738 GET_STRING_LDB(msg, "governsID", mem_ctx, obj, governsID_oid, true);
739 if (schema->num_prefixes == 0) {
740 /* set an invalid value */
741 obj->governsID_id = 0xFFFFFFFF;
743 status = dsdb_map_oid2int(schema, obj->governsID_oid, &obj->governsID_id);
744 if (!W_ERROR_IS_OK(status)) {
745 DEBUG(0,("%s: '%s': unable to map governsID %s: %s\n",
746 __location__, obj->lDAPDisplayName, obj->governsID_oid,
747 win_errstr(status)));
751 GET_GUID_LDB(msg, "schemaIDGUID", obj, schemaIDGUID);
753 GET_UINT32_LDB(msg, "objectClassCategory", obj, objectClassCategory);
754 GET_STRING_LDB(msg, "rDNAttID", mem_ctx, obj, rDNAttID, false);
755 GET_STRING_LDB(msg, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
757 GET_STRING_LDB(msg, "subClassOf", mem_ctx, obj, subClassOf, true);
759 GET_STRING_LIST_LDB(msg, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
760 GET_STRING_LIST_LDB(msg, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
762 GET_STRING_LIST_LDB(msg, "systemMustContain", mem_ctx, obj, systemMustContain, false);
763 GET_STRING_LIST_LDB(msg, "systemMayContain", mem_ctx, obj, systemMayContain, false);
764 GET_STRING_LIST_LDB(msg, "mustContain", mem_ctx, obj, mustContain, false);
765 GET_STRING_LIST_LDB(msg, "mayContain", mem_ctx, obj, mayContain, false);
767 GET_STRING_LIST_LDB(msg, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
768 GET_STRING_LIST_LDB(msg, "possSuperiors", mem_ctx, obj, possSuperiors, false);
769 GET_STRING_LIST_LDB(msg, "possibleInferiors", mem_ctx, obj, possibleInferiors, false);
771 GET_STRING_LDB(msg, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
773 GET_UINT32_LDB(msg, "schemaFlagsEx", obj, schemaFlagsEx);
774 GET_BLOB_LDB(msg, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
776 GET_BOOL_LDB(msg, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
777 GET_STRING_LDB(msg, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
778 GET_STRING_LDB(msg, "adminDescription", mem_ctx, obj, adminDescription, false);
779 GET_STRING_LDB(msg, "classDisplayName", mem_ctx, obj, classDisplayName, false);
780 GET_BOOL_LDB(msg, "defaultHidingValue", obj, defaultHidingValue, false);
781 GET_BOOL_LDB(msg, "isDefunct", obj, isDefunct, false);
782 GET_BOOL_LDB(msg, "systemOnly", obj, systemOnly, false);
787 #define dsdb_oom(error_string, mem_ctx) *error_string = talloc_asprintf(mem_ctx, "dsdb out of memory at %s:%d\n", __FILE__, __LINE__)
789 int dsdb_schema_from_ldb_results(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
790 struct smb_iconv_convenience *iconv_convenience,
791 struct ldb_result *schema_res,
792 struct ldb_result *attrs_res, struct ldb_result *objectclass_res,
793 struct dsdb_schema **schema_out,
798 const struct ldb_val *prefix_val;
799 const struct ldb_val *info_val;
800 struct ldb_val info_val_default;
801 struct dsdb_schema *schema;
803 schema = dsdb_new_schema(mem_ctx, iconv_convenience);
805 dsdb_oom(error_string, mem_ctx);
806 return LDB_ERR_OPERATIONS_ERROR;
809 prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
811 *error_string = talloc_asprintf(mem_ctx,
812 "schema_fsmo_init: no prefixMap attribute found");
813 talloc_free(mem_ctx);
814 return LDB_ERR_CONSTRAINT_VIOLATION;
816 info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
818 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
819 if (!info_val_default.data) {
820 dsdb_oom(error_string, mem_ctx);
821 return LDB_ERR_OPERATIONS_ERROR;
823 talloc_steal(mem_ctx, info_val_default.data);
824 info_val = &info_val_default;
827 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
828 if (!W_ERROR_IS_OK(status)) {
829 *error_string = talloc_asprintf(mem_ctx,
830 "schema_fsmo_init: failed to load oid mappings: %s",
832 talloc_free(mem_ctx);
833 return LDB_ERR_CONSTRAINT_VIOLATION;
836 for (i=0; i < attrs_res->count; i++) {
837 struct dsdb_attribute *sa;
839 sa = talloc_zero(schema, struct dsdb_attribute);
841 dsdb_oom(error_string, mem_ctx);
842 return LDB_ERR_OPERATIONS_ERROR;
845 status = dsdb_attribute_from_ldb(schema, attrs_res->msgs[i], sa, sa);
846 if (!W_ERROR_IS_OK(status)) {
847 *error_string = talloc_asprintf(mem_ctx,
848 "schema_fsmo_init: failed to load attribute definition: %s:%s",
849 ldb_dn_get_linearized(attrs_res->msgs[i]->dn),
851 talloc_free(mem_ctx);
852 return LDB_ERR_CONSTRAINT_VIOLATION;
855 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
858 for (i=0; i < objectclass_res->count; i++) {
859 struct dsdb_class *sc;
861 sc = talloc_zero(schema, struct dsdb_class);
863 dsdb_oom(error_string, mem_ctx);
864 return LDB_ERR_OPERATIONS_ERROR;
867 status = dsdb_class_from_ldb(schema, objectclass_res->msgs[i], sc, sc);
868 if (!W_ERROR_IS_OK(status)) {
869 *error_string = talloc_asprintf(mem_ctx,
870 "schema_fsmo_init: failed to load class definition: %s:%s",
871 ldb_dn_get_linearized(objectclass_res->msgs[i]->dn),
873 talloc_free(mem_ctx);
874 return LDB_ERR_CONSTRAINT_VIOLATION;
877 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
880 schema->fsmo.master_dn = ldb_msg_find_attr_as_dn(ldb, schema, schema_res->msgs[0], "fSMORoleOwner");
881 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), schema->fsmo.master_dn) == 0) {
882 schema->fsmo.we_are_master = true;
884 schema->fsmo.we_are_master = false;
887 DEBUG(5, ("schema_fsmo_init: we are master: %s\n",
888 (schema->fsmo.we_are_master?"yes":"no")));
890 *schema_out = schema;
894 /* This recursive load of the objectClasses presumes that they
895 * everything is in a strict subClassOf hirarchy.
897 * We load this in order so we produce certain outputs (such as the
898 * exported schema for openldap, and sorted objectClass attribute) 'in
901 static int fetch_oc_recursive(struct ldb_context *ldb, struct ldb_dn *schemadn,
903 struct ldb_result *search_from,
904 struct ldb_result *res_list)
908 for (i=0; i < search_from->count; i++) {
909 struct ldb_result *res;
910 const char *name = ldb_msg_find_attr_as_string(search_from->msgs[i],
911 "lDAPDisplayname", NULL);
913 ret = ldb_search_exp_fmt(ldb, mem_ctx, &res,
914 schemadn, LDB_SCOPE_SUBTREE, NULL,
915 "(&(&(objectClass=classSchema)(subClassOf=%s))(!(lDAPDisplayName=%s)))",
917 if (ret != LDB_SUCCESS) {
918 printf("Search failed: %s\n", ldb_errstring(ldb));
922 res_list->msgs = talloc_realloc(res_list, res_list->msgs,
923 struct ldb_message *, res_list->count + 2);
924 if (!res_list->msgs) {
925 return LDB_ERR_OPERATIONS_ERROR;
927 res_list->msgs[res_list->count] = talloc_move(res_list,
928 &search_from->msgs[i]);
930 res_list->msgs[res_list->count] = NULL;
932 if (res->count > 0) {
933 ret = fetch_oc_recursive(ldb, schemadn, mem_ctx, res, res_list);
935 if (ret != LDB_SUCCESS) {
942 static int fetch_objectclass_schema(struct ldb_context *ldb, struct ldb_dn *schemadn,
944 struct ldb_result **objectclasses_res)
946 TALLOC_CTX *local_ctx = talloc_new(mem_ctx);
947 struct ldb_result *top_res, *ret_res;
950 return LDB_ERR_OPERATIONS_ERROR;
954 ret = ldb_search(ldb, schemadn, LDB_SCOPE_SUBTREE,
955 "(&(objectClass=classSchema)(lDAPDisplayName=top))",
957 if (ret != LDB_SUCCESS) {
958 printf("Search failed: %s\n", ldb_errstring(ldb));
959 return LDB_ERR_OPERATIONS_ERROR;
962 talloc_steal(local_ctx, top_res);
964 if (top_res->count != 1) {
965 return LDB_ERR_OPERATIONS_ERROR;
968 ret_res = talloc_zero(local_ctx, struct ldb_result);
970 return LDB_ERR_OPERATIONS_ERROR;
973 ret = fetch_oc_recursive(ldb, schemadn, local_ctx, top_res, ret_res);
975 if (ret != LDB_SUCCESS) {
976 printf("Search failed: %s\n", ldb_errstring(ldb));
977 return LDB_ERR_OPERATIONS_ERROR;
980 *objectclasses_res = talloc_move(mem_ctx, &ret_res);
984 int dsdb_schema_from_schema_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
985 struct smb_iconv_convenience *iconv_convenience,
986 struct ldb_dn *schema_dn,
987 struct dsdb_schema **schema,
988 char **error_string_out)
994 struct ldb_result *schema_res;
995 struct ldb_result *a_res;
996 struct ldb_result *c_res;
997 static const char *schema_attrs[] = {
1004 tmp_ctx = talloc_new(mem_ctx);
1006 dsdb_oom(error_string_out, mem_ctx);
1007 return LDB_ERR_OPERATIONS_ERROR;
1011 * setup the prefix mappings and schema info
1013 ret = ldb_search(ldb, schema_dn,
1017 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1018 talloc_free(tmp_ctx);
1020 } else if (ret != LDB_SUCCESS) {
1021 *error_string_out = talloc_asprintf(mem_ctx,
1022 "dsdb_schema: failed to search the schema head: %s",
1023 ldb_errstring(ldb));
1024 talloc_free(tmp_ctx);
1027 talloc_steal(tmp_ctx, schema_res);
1028 if (schema_res->count != 1) {
1029 *error_string_out = talloc_asprintf(mem_ctx,
1030 "dsdb_schema: [%u] schema heads found on a base search",
1032 talloc_free(tmp_ctx);
1033 return LDB_ERR_CONSTRAINT_VIOLATION;
1037 * load the attribute definitions
1039 ret = ldb_search(ldb, schema_dn,
1041 "(objectClass=attributeSchema)", NULL,
1043 if (ret != LDB_SUCCESS) {
1044 *error_string_out = talloc_asprintf(mem_ctx,
1045 "dsdb_schema: failed to search attributeSchema objects: %s",
1046 ldb_errstring(ldb));
1047 talloc_free(tmp_ctx);
1050 talloc_steal(tmp_ctx, a_res);
1053 * load the objectClass definitions
1055 ret = fetch_objectclass_schema(ldb, schema_dn, tmp_ctx, &c_res);
1056 if (ret != LDB_SUCCESS) {
1057 *error_string_out = talloc_asprintf(mem_ctx,
1058 "Failed to fetch objectClass schema elements: %s\n", ldb_errstring(ldb));
1059 talloc_free(tmp_ctx);
1063 ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
1064 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1065 schema_res, a_res, c_res, schema, &error_string);
1066 if (ret != LDB_SUCCESS) {
1067 *error_string_out = talloc_asprintf(mem_ctx,
1068 "dsdb_schema load failed: %s",
1070 talloc_free(tmp_ctx);
1073 talloc_steal(mem_ctx, *schema);
1074 talloc_free(tmp_ctx);
1080 static const struct {
1083 } name_mappings[] = {
1084 { "cn", "2.5.4.3" },
1085 { "name", "1.2.840.113556.1.4.1" },
1086 { "lDAPDisplayName", "1.2.840.113556.1.2.460" },
1087 { "attributeID", "1.2.840.113556.1.2.30" },
1088 { "schemaIDGUID", "1.2.840.113556.1.4.148" },
1089 { "mAPIID", "1.2.840.113556.1.2.49" },
1090 { "attributeSecurityGUID", "1.2.840.113556.1.4.149" },
1091 { "searchFlags", "1.2.840.113556.1.2.334" },
1092 { "systemFlags", "1.2.840.113556.1.4.375" },
1093 { "isMemberOfPartialAttributeSet", "1.2.840.113556.1.4.639" },
1094 { "linkID", "1.2.840.113556.1.2.50" },
1095 { "attributeSyntax", "1.2.840.113556.1.2.32" },
1096 { "oMSyntax", "1.2.840.113556.1.2.231" },
1097 { "oMObjectClass", "1.2.840.113556.1.2.218" },
1098 { "isSingleValued", "1.2.840.113556.1.2.33" },
1099 { "rangeLower", "1.2.840.113556.1.2.34" },
1100 { "rangeUpper", "1.2.840.113556.1.2.35" },
1101 { "extendedCharsAllowed", "1.2.840.113556.1.2.380" },
1102 { "schemaFlagsEx", "1.2.840.113556.1.4.120" },
1103 { "msDs-Schema-Extensions", "1.2.840.113556.1.4.1440" },
1104 { "showInAdvancedViewOnly", "1.2.840.113556.1.2.169" },
1105 { "adminDisplayName", "1.2.840.113556.1.2.194" },
1106 { "adminDescription", "1.2.840.113556.1.2.226" },
1107 { "classDisplayName", "1.2.840.113556.1.4.610" },
1108 { "isEphemeral", "1.2.840.113556.1.4.1212" },
1109 { "isDefunct", "1.2.840.113556.1.4.661" },
1110 { "systemOnly", "1.2.840.113556.1.4.170" },
1111 { "governsID", "1.2.840.113556.1.2.22" },
1112 { "objectClassCategory", "1.2.840.113556.1.2.370" },
1113 { "rDNAttID", "1.2.840.113556.1.2.26" },
1114 { "defaultObjectCategory", "1.2.840.113556.1.4.783" },
1115 { "subClassOf", "1.2.840.113556.1.2.21" },
1116 { "systemAuxiliaryClass", "1.2.840.113556.1.4.198" },
1117 { "systemPossSuperiors", "1.2.840.113556.1.4.195" },
1118 { "systemMustContain", "1.2.840.113556.1.4.197" },
1119 { "systemMayContain", "1.2.840.113556.1.4.196" },
1120 { "auxiliaryClass", "1.2.840.113556.1.2.351" },
1121 { "possSuperiors", "1.2.840.113556.1.2.8" },
1122 { "mustContain", "1.2.840.113556.1.2.24" },
1123 { "mayContain", "1.2.840.113556.1.2.25" },
1124 { "defaultSecurityDescriptor", "1.2.840.113556.1.4.224" },
1125 { "defaultHidingValue", "1.2.840.113556.1.4.518" },
1128 static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb_schema *schema,
1129 struct drsuapi_DsReplicaObject *obj,
1135 const char *oid = NULL;
1137 for(i=0; i < ARRAY_SIZE(name_mappings); i++) {
1138 if (strcmp(name_mappings[i].name, name) != 0) continue;
1140 oid = name_mappings[i].oid;
1148 status = dsdb_map_oid2int(schema, oid, &id);
1149 if (!W_ERROR_IS_OK(status)) {
1153 for (i=0; i < obj->attribute_ctr.num_attributes; i++) {
1154 if (obj->attribute_ctr.attributes[i].attid != id) continue;
1157 return &obj->attribute_ctr.attributes[i];
1163 #define GET_STRING_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1164 struct drsuapi_DsReplicaAttribute *_a; \
1165 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1166 if (strict && !_a) { \
1167 d_printf("%s: %s == NULL\n", __location__, attr); \
1168 return WERR_INVALID_PARAM; \
1170 if (strict && _a->value_ctr.num_values != 1) { \
1171 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1172 _a->value_ctr.num_values); \
1173 return WERR_INVALID_PARAM; \
1175 if (_a && _a->value_ctr.num_values >= 1) { \
1177 _ret = convert_string_talloc(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
1178 _a->value_ctr.values[0].blob->data, \
1179 _a->value_ctr.values[0].blob->length, \
1180 (void **)discard_const(&(p)->elem)); \
1182 DEBUG(0,("%s: invalid data!\n", attr)); \
1184 _a->value_ctr.values[0].blob->data, \
1185 _a->value_ctr.values[0].blob->length); \
1186 return WERR_FOOBAR; \
1193 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
1194 struct drsuapi_DsReplicaAttribute *_a; \
1195 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1196 if (strict && !_a) { \
1197 d_printf("%s: %s == NULL\n", __location__, attr); \
1198 return WERR_INVALID_PARAM; \
1200 if (strict && _a->value_ctr.num_values != 1) { \
1201 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1202 _a->value_ctr.num_values); \
1203 return WERR_INVALID_PARAM; \
1205 if (strict && !_a->value_ctr.values[0].blob) { \
1206 d_printf("%s: %s data == NULL\n", __location__, attr); \
1207 return WERR_INVALID_PARAM; \
1209 if (_a && _a->value_ctr.num_values >= 1 \
1210 && _a->value_ctr.values[0].blob) { \
1211 struct drsuapi_DsReplicaObjectIdentifier3 _id3; \
1212 enum ndr_err_code _ndr_err; \
1213 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1214 mem_ctx, s->iconv_convenience, &_id3,\
1215 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);\
1216 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1217 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1218 return ntstatus_to_werror(_nt_status); \
1220 (p)->elem = _id3.dn; \
1226 #define GET_BOOL_DS(s, r, attr, p, elem, strict) do { \
1227 struct drsuapi_DsReplicaAttribute *_a; \
1228 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1229 if (strict && !_a) { \
1230 d_printf("%s: %s == NULL\n", __location__, attr); \
1231 return WERR_INVALID_PARAM; \
1233 if (strict && _a->value_ctr.num_values != 1) { \
1234 d_printf("%s: %s num_values == %u\n", __location__, attr, \
1235 (unsigned int)_a->value_ctr.num_values); \
1236 return WERR_INVALID_PARAM; \
1238 if (strict && !_a->value_ctr.values[0].blob) { \
1239 d_printf("%s: %s data == NULL\n", __location__, attr); \
1240 return WERR_INVALID_PARAM; \
1242 if (strict && _a->value_ctr.values[0].blob->length != 4) { \
1243 d_printf("%s: %s length == %u\n", __location__, attr, \
1244 (unsigned int)_a->value_ctr.values[0].blob->length); \
1245 return WERR_INVALID_PARAM; \
1247 if (_a && _a->value_ctr.num_values >= 1 \
1248 && _a->value_ctr.values[0].blob \
1249 && _a->value_ctr.values[0].blob->length == 4) { \
1250 (p)->elem = (IVAL(_a->value_ctr.values[0].blob->data,0)?true:false);\
1252 (p)->elem = false; \
1256 #define GET_UINT32_DS(s, r, attr, p, elem) do { \
1257 struct drsuapi_DsReplicaAttribute *_a; \
1258 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1259 if (_a && _a->value_ctr.num_values >= 1 \
1260 && _a->value_ctr.values[0].blob \
1261 && _a->value_ctr.values[0].blob->length == 4) { \
1262 (p)->elem = IVAL(_a->value_ctr.values[0].blob->data,0);\
1268 #define GET_GUID_DS(s, r, attr, mem_ctx, p, elem) do { \
1269 struct drsuapi_DsReplicaAttribute *_a; \
1270 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1271 if (_a && _a->value_ctr.num_values >= 1 \
1272 && _a->value_ctr.values[0].blob \
1273 && _a->value_ctr.values[0].blob->length == 16) { \
1274 enum ndr_err_code _ndr_err; \
1275 _ndr_err = ndr_pull_struct_blob_all(_a->value_ctr.values[0].blob, \
1276 mem_ctx, s->iconv_convenience, &(p)->elem, \
1277 (ndr_pull_flags_fn_t)ndr_pull_GUID); \
1278 if (!NDR_ERR_CODE_IS_SUCCESS(_ndr_err)) { \
1279 NTSTATUS _nt_status = ndr_map_error2ntstatus(_ndr_err); \
1280 return ntstatus_to_werror(_nt_status); \
1283 ZERO_STRUCT((p)->elem);\
1287 #define GET_BLOB_DS(s, r, attr, mem_ctx, p, elem) do { \
1288 struct drsuapi_DsReplicaAttribute *_a; \
1289 _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
1290 if (_a && _a->value_ctr.num_values >= 1 \
1291 && _a->value_ctr.values[0].blob) { \
1292 (p)->elem = *_a->value_ctr.values[0].blob;\
1293 talloc_steal(mem_ctx, (p)->elem.data); \
1295 ZERO_STRUCT((p)->elem);\
1299 WERROR dsdb_attribute_from_drsuapi(struct dsdb_schema *schema,
1300 struct drsuapi_DsReplicaObject *r,
1301 TALLOC_CTX *mem_ctx,
1302 struct dsdb_attribute *attr)
1306 GET_STRING_DS(schema, r, "name", mem_ctx, attr, cn, true);
1307 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, attr, lDAPDisplayName, true);
1308 GET_UINT32_DS(schema, r, "attributeID", attr, attributeID_id);
1309 status = dsdb_map_int2oid(schema, attr->attributeID_id, mem_ctx, &attr->attributeID_oid);
1310 if (!W_ERROR_IS_OK(status)) {
1311 DEBUG(0,("%s: '%s': unable to map attributeID 0x%08X: %s\n",
1312 __location__, attr->lDAPDisplayName, attr->attributeID_id,
1313 win_errstr(status)));
1316 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, attr, schemaIDGUID);
1317 GET_UINT32_DS(schema, r, "mAPIID", attr, mAPIID);
1319 GET_GUID_DS(schema, r, "attributeSecurityGUID", mem_ctx, attr, attributeSecurityGUID);
1321 GET_UINT32_DS(schema, r, "searchFlags", attr, searchFlags);
1322 GET_UINT32_DS(schema, r, "systemFlags", attr, systemFlags);
1323 GET_BOOL_DS(schema, r, "isMemberOfPartialAttributeSet", attr, isMemberOfPartialAttributeSet, false);
1324 GET_UINT32_DS(schema, r, "linkID", attr, linkID);
1326 GET_UINT32_DS(schema, r, "attributeSyntax", attr, attributeSyntax_id);
1327 status = dsdb_map_int2oid(schema, attr->attributeSyntax_id, mem_ctx, &attr->attributeSyntax_oid);
1328 if (!W_ERROR_IS_OK(status)) {
1329 DEBUG(0,("%s: '%s': unable to map attributeSyntax 0x%08X: %s\n",
1330 __location__, attr->lDAPDisplayName, attr->attributeSyntax_id,
1331 win_errstr(status)));
1334 GET_UINT32_DS(schema, r, "oMSyntax", attr, oMSyntax);
1335 GET_BLOB_DS(schema, r, "oMObjectClass", mem_ctx, attr, oMObjectClass);
1337 GET_BOOL_DS(schema, r, "isSingleValued", attr, isSingleValued, true);
1338 GET_UINT32_DS(schema, r, "rangeLower", attr, rangeLower);
1339 GET_UINT32_DS(schema, r, "rangeUpper", attr, rangeUpper);
1340 GET_BOOL_DS(schema, r, "extendedCharsAllowed", attr, extendedCharsAllowed, false);
1342 GET_UINT32_DS(schema, r, "schemaFlagsEx", attr, schemaFlagsEx);
1343 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, attr, msDs_Schema_Extensions);
1345 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", attr, showInAdvancedViewOnly, false);
1346 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, attr, adminDisplayName, false);
1347 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, attr, adminDescription, false);
1348 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, attr, classDisplayName, false);
1349 GET_BOOL_DS(schema, r, "isEphemeral", attr, isEphemeral, false);
1350 GET_BOOL_DS(schema, r, "isDefunct", attr, isDefunct, false);
1351 GET_BOOL_DS(schema, r, "systemOnly", attr, systemOnly, false);
1353 attr->syntax = dsdb_syntax_for_attribute(attr);
1354 if (!attr->syntax) {
1355 return WERR_DS_ATT_SCHEMA_REQ_SYNTAX;
1361 WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
1362 struct drsuapi_DsReplicaObject *r,
1363 TALLOC_CTX *mem_ctx,
1364 struct dsdb_class *obj)
1368 GET_STRING_DS(schema, r, "name", mem_ctx, obj, cn, true);
1369 GET_STRING_DS(schema, r, "lDAPDisplayName", mem_ctx, obj, lDAPDisplayName, true);
1370 GET_UINT32_DS(schema, r, "governsID", obj, governsID_id);
1371 status = dsdb_map_int2oid(schema, obj->governsID_id, mem_ctx, &obj->governsID_oid);
1372 if (!W_ERROR_IS_OK(status)) {
1373 DEBUG(0,("%s: '%s': unable to map governsID 0x%08X: %s\n",
1374 __location__, obj->lDAPDisplayName, obj->governsID_id,
1375 win_errstr(status)));
1378 GET_GUID_DS(schema, r, "schemaIDGUID", mem_ctx, obj, schemaIDGUID);
1380 GET_UINT32_DS(schema, r, "objectClassCategory", obj, objectClassCategory);
1381 GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
1382 GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
1384 GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, true);
1386 obj->systemAuxiliaryClass = NULL;
1387 obj->systemPossSuperiors = NULL;
1388 obj->systemMustContain = NULL;
1389 obj->systemMayContain = NULL;
1391 obj->auxiliaryClass = NULL;
1392 obj->possSuperiors = NULL;
1393 obj->mustContain = NULL;
1394 obj->mayContain = NULL;
1396 obj->possibleInferiors = NULL;
1398 GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);
1400 GET_UINT32_DS(schema, r, "schemaFlagsEx", obj, schemaFlagsEx);
1401 GET_BLOB_DS(schema, r, "msDs-Schema-Extensions", mem_ctx, obj, msDs_Schema_Extensions);
1403 GET_BOOL_DS(schema, r, "showInAdvancedViewOnly", obj, showInAdvancedViewOnly, false);
1404 GET_STRING_DS(schema, r, "adminDisplayName", mem_ctx, obj, adminDisplayName, false);
1405 GET_STRING_DS(schema, r, "adminDescription", mem_ctx, obj, adminDescription, false);
1406 GET_STRING_DS(schema, r, "classDisplayName", mem_ctx, obj, classDisplayName, false);
1407 GET_BOOL_DS(schema, r, "defaultHidingValue", obj, defaultHidingValue, false);
1408 GET_BOOL_DS(schema, r, "isDefunct", obj, isDefunct, false);
1409 GET_BOOL_DS(schema, r, "systemOnly", obj, systemOnly, false);
1414 const struct dsdb_attribute *dsdb_attribute_by_attributeID_id(const struct dsdb_schema *schema,
1417 struct dsdb_attribute *cur;
1420 * 0xFFFFFFFF is used as value when no mapping table is available,
1421 * so don't try to match with it
1423 if (id == 0xFFFFFFFF) return NULL;
1425 /* TODO: add binary search */
1426 for (cur = schema->attributes; cur; cur = cur->next) {
1427 if (cur->attributeID_id != id) continue;
1435 const struct dsdb_attribute *dsdb_attribute_by_attributeID_oid(const struct dsdb_schema *schema,
1438 struct dsdb_attribute *cur;
1440 if (!oid) return NULL;
1442 /* TODO: add binary search */
1443 for (cur = schema->attributes; cur; cur = cur->next) {
1444 if (strcmp(cur->attributeID_oid, oid) != 0) continue;
1452 const struct dsdb_attribute *dsdb_attribute_by_lDAPDisplayName(const struct dsdb_schema *schema,
1455 struct dsdb_attribute *cur;
1457 if (!name) return NULL;
1459 /* TODO: add binary search */
1460 for (cur = schema->attributes; cur; cur = cur->next) {
1461 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
1469 const struct dsdb_attribute *dsdb_attribute_by_linkID(const struct dsdb_schema *schema,
1472 struct dsdb_attribute *cur;
1474 /* TODO: add binary search */
1475 for (cur = schema->attributes; cur; cur = cur->next) {
1476 if (cur->linkID != linkID) continue;
1484 const struct dsdb_class *dsdb_class_by_governsID_id(const struct dsdb_schema *schema,
1487 struct dsdb_class *cur;
1490 * 0xFFFFFFFF is used as value when no mapping table is available,
1491 * so don't try to match with it
1493 if (id == 0xFFFFFFFF) return NULL;
1495 /* TODO: add binary search */
1496 for (cur = schema->classes; cur; cur = cur->next) {
1497 if (cur->governsID_id != id) continue;
1505 const struct dsdb_class *dsdb_class_by_governsID_oid(const struct dsdb_schema *schema,
1508 struct dsdb_class *cur;
1510 if (!oid) return NULL;
1512 /* TODO: add binary search */
1513 for (cur = schema->classes; cur; cur = cur->next) {
1514 if (strcmp(cur->governsID_oid, oid) != 0) continue;
1522 const struct dsdb_class *dsdb_class_by_lDAPDisplayName(const struct dsdb_schema *schema,
1525 struct dsdb_class *cur;
1527 if (!name) return NULL;
1529 /* TODO: add binary search */
1530 for (cur = schema->classes; cur; cur = cur->next) {
1531 if (strcasecmp(cur->lDAPDisplayName, name) != 0) continue;
1539 const struct dsdb_class *dsdb_class_by_cn(const struct dsdb_schema *schema,
1542 struct dsdb_class *cur;
1544 if (!cn) return NULL;
1546 /* TODO: add binary search */
1547 for (cur = schema->classes; cur; cur = cur->next) {
1548 if (strcasecmp(cur->cn, cn) != 0) continue;
1556 const char *dsdb_lDAPDisplayName_by_id(const struct dsdb_schema *schema,
1559 const struct dsdb_attribute *a;
1560 const struct dsdb_class *c;
1562 /* TODO: add binary search */
1563 a = dsdb_attribute_by_attributeID_id(schema, id);
1565 return a->lDAPDisplayName;
1568 c = dsdb_class_by_governsID_id(schema, id);
1570 return c->lDAPDisplayName;
1577 Return a list of linked attributes, in lDAPDisplayName format.
1579 This may be used to determine if a modification would require
1580 backlinks to be updated, for example
1583 WERROR dsdb_linked_attribute_lDAPDisplayName_list(const struct dsdb_schema *schema, TALLOC_CTX *mem_ctx, const char ***attr_list_ret)
1585 const char **attr_list = NULL;
1586 struct dsdb_attribute *cur;
1588 for (cur = schema->attributes; cur; cur = cur->next) {
1589 if (cur->linkID == 0) continue;
1591 attr_list = talloc_realloc(mem_ctx, attr_list, const char *, i+2);
1595 attr_list[i] = cur->lDAPDisplayName;
1598 attr_list[i] = NULL;
1599 *attr_list_ret = attr_list;
1603 static char **merge_attr_list(TALLOC_CTX *mem_ctx,
1604 char **attrs, const char **new_attrs)
1608 size_t new_len, orig_len = str_list_length((const char **)attrs);
1613 ret_attrs = talloc_realloc(mem_ctx,
1614 attrs, char *, orig_len + str_list_length(new_attrs) + 1);
1616 for (i=0; i < str_list_length(new_attrs); i++) {
1617 ret_attrs[orig_len + i] = new_attrs[i];
1619 new_len = orig_len + str_list_length(new_attrs);
1621 ret_attrs[new_len] = NULL;
1628 char **dsdb_full_attribute_list_internal(TALLOC_CTX *mem_ctx,
1629 const struct dsdb_schema *schema,
1630 const char **class_list,
1631 enum dsdb_attr_list_query query)
1634 const struct dsdb_class *class;
1636 char **attr_list = NULL;
1637 char **recursive_list;
1639 for (i=0; class_list && class_list[i]; i++) {
1640 class = dsdb_class_by_lDAPDisplayName(schema, class_list[i]);
1643 case DSDB_SCHEMA_ALL_MAY:
1644 attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1645 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1648 case DSDB_SCHEMA_ALL_MUST:
1649 attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1650 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1653 case DSDB_SCHEMA_SYS_MAY:
1654 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1657 case DSDB_SCHEMA_SYS_MUST:
1658 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1661 case DSDB_SCHEMA_MAY:
1662 attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1665 case DSDB_SCHEMA_MUST:
1666 attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1669 case DSDB_SCHEMA_ALL:
1670 attr_list = merge_attr_list(mem_ctx, attr_list, class->mayContain);
1671 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMayContain);
1672 attr_list = merge_attr_list(mem_ctx, attr_list, class->mustContain);
1673 attr_list = merge_attr_list(mem_ctx, attr_list, class->systemMustContain);
1677 recursive_list = dsdb_full_attribute_list_internal(mem_ctx, schema,
1678 class->systemAuxiliaryClass,
1681 attr_list = merge_attr_list(mem_ctx, attr_list, (const char **)recursive_list);
1683 recursive_list = dsdb_full_attribute_list_internal(mem_ctx, schema,
1684 class->auxiliaryClass,
1687 attr_list = merge_attr_list(mem_ctx, attr_list, (const char **)recursive_list);
1693 char **dsdb_full_attribute_list(TALLOC_CTX *mem_ctx,
1694 const struct dsdb_schema *schema,
1695 const char **class_list,
1696 enum dsdb_attr_list_query query)
1698 char **attr_list = dsdb_full_attribute_list_internal(mem_ctx, schema, class_list, query);
1699 size_t new_len = str_list_length((const char **)attr_list);
1701 /* Remove duplicates */
1704 qsort(attr_list, new_len,
1706 (comparison_fn_t)strcasecmp);
1708 for (i=1 ; i < new_len; i++) {
1709 char **val1 = &attr_list[i-1];
1710 char **val2 = &attr_list[i];
1711 if (ldb_attr_cmp(*val1, *val2) == 0) {
1712 memmove(val1, val2, (new_len - i) * sizeof( *attr_list));
1721 * Attach the schema to an opaque pointer on the ldb, so ldb modules
1725 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
1729 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
1730 if (ret != LDB_SUCCESS) {
1734 talloc_steal(ldb, schema);
1740 * Global variable to hold one copy of the schema, used to avoid memory bloat
1742 static struct dsdb_schema *global_schema;
1745 * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
1747 int dsdb_set_global_schema(struct ldb_context *ldb)
1750 if (!global_schema) {
1753 ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
1754 if (ret != LDB_SUCCESS) {
1762 * Find the schema object for this ldb
1765 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
1768 struct dsdb_schema *schema;
1770 /* see if we have a cached copy */
1771 p = ldb_get_opaque(ldb, "dsdb_schema");
1776 schema = talloc_get_type(p, struct dsdb_schema);
1785 * Make the schema found on this ldb the 'global' schema
1788 void dsdb_make_schema_global(struct ldb_context *ldb)
1790 struct dsdb_schema *schema = dsdb_get_schema(ldb);
1795 talloc_steal(talloc_autofree_context(), schema);
1796 global_schema = schema;
1798 dsdb_set_global_schema(ldb);
1803 * Rather than read a schema from the LDB itself, read it from an ldif
1804 * file. This allows schema to be loaded and used while adding the
1805 * schema itself to the directory.
1808 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
1810 struct ldb_ldif *ldif;
1811 struct ldb_message *msg;
1812 TALLOC_CTX *mem_ctx;
1815 struct dsdb_schema *schema;
1816 const struct ldb_val *prefix_val;
1817 const struct ldb_val *info_val;
1818 struct ldb_val info_val_default;
1820 mem_ctx = talloc_new(ldb);
1825 schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
1827 schema->fsmo.we_are_master = true;
1828 schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
1829 if (!schema->fsmo.master_dn) {
1834 * load the prefixMap attribute from pf
1836 ldif = ldb_ldif_read_string(ldb, &pf);
1838 status = WERR_INVALID_PARAM;
1841 talloc_steal(mem_ctx, ldif);
1843 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1847 talloc_steal(mem_ctx, msg);
1850 prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
1852 status = WERR_INVALID_PARAM;
1856 info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
1858 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
1859 if (!info_val_default.data) {
1862 talloc_steal(mem_ctx, info_val_default.data);
1863 info_val = &info_val_default;
1866 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
1867 if (!W_ERROR_IS_OK(status)) {
1872 * load the attribute and class definitions outof df
1874 while ((ldif = ldb_ldif_read_string(ldb, &df))) {
1878 talloc_steal(mem_ctx, ldif);
1880 msg = ldb_msg_canonicalize(ldb, ldif->msg);
1885 talloc_steal(mem_ctx, msg);
1888 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
1889 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
1892 struct dsdb_attribute *sa;
1894 sa = talloc_zero(schema, struct dsdb_attribute);
1899 status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
1900 if (!W_ERROR_IS_OK(status)) {
1904 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
1906 struct dsdb_class *sc;
1908 sc = talloc_zero(schema, struct dsdb_class);
1913 status = dsdb_class_from_ldb(schema, msg, sc, sc);
1914 if (!W_ERROR_IS_OK(status)) {
1918 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
1922 ret = dsdb_set_schema(ldb, schema);
1923 if (ret != LDB_SUCCESS) {
1924 status = WERR_FOOBAR;
1931 status = WERR_NOMEM;
1934 talloc_free(mem_ctx);