2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009-2010
8 Copyright (C) Andrew Bartlett 2010-2016
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "rpc_server/dcerpc_server.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "param/param.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "libcli/security/security.h"
32 #include "libcli/security/session.h"
33 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
34 #include "rpc_server/dcerpc_server_proto.h"
35 #include "rpc_server/common/sid_helper.h"
36 #include "../libcli/drsuapi/drsuapi.h"
37 #include "lib/util/binsearch.h"
38 #include "lib/util/tsort.h"
39 #include "auth/session.h"
40 #include "dsdb/common/util.h"
41 #include "lib/dbwrap/dbwrap.h"
42 #include "lib/dbwrap/dbwrap_rbt.h"
43 #include "librpc/gen_ndr/ndr_misc.h"
46 #define DBGC_CLASS DBGC_DRS_REPL
48 #define DRS_GUID_SIZE 16
51 * state of a partially-completed replication cycle. This state persists
52 * over multiple calls to dcesrv_drsuapi_DsGetNCChanges()
54 struct drsuapi_getncchanges_state {
55 struct db_context *obj_cache;
58 uint32_t num_processed;
59 struct ldb_dn *ncRoot_dn;
60 struct GUID ncRoot_guid;
65 struct drsuapi_DsReplicaHighWaterMark last_hwm;
66 struct ldb_dn *last_dn;
67 struct drsuapi_DsReplicaHighWaterMark final_hwm;
68 struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
69 struct drsuapi_DsReplicaLinkedAttribute *la_list;
71 struct la_for_sorting *la_sorted;
75 /* We must keep the GUIDs in NDR form for sorting */
76 struct la_for_sorting {
77 const struct drsuapi_DsReplicaLinkedAttribute *link;
78 uint8_t target_guid[DRS_GUID_SIZE];
79 uint8_t source_guid[DRS_GUID_SIZE];
83 * stores the state for a chunk of replication data. This state information
84 * only exists for a single call to dcesrv_drsuapi_DsGetNCChanges()
86 struct getncchanges_repl_chunk {
87 struct drsuapi_DsGetNCChangesCtr6 *ctr6;
89 /* the last object written to the response */
90 struct drsuapi_DsReplicaObjectListItemEx *last_object;
93 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
94 const struct drsuapi_DsReplicaHighWaterMark *h2)
96 if (h1->highest_usn < h2->highest_usn) {
98 } else if (h1->highest_usn > h2->highest_usn) {
100 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
102 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
104 } else if (h1->reserved_usn < h2->reserved_usn) {
106 } else if (h1->reserved_usn > h2->reserved_usn) {
114 build a DsReplicaObjectIdentifier from a ldb msg
116 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
117 const struct ldb_message *msg)
119 struct drsuapi_DsReplicaObjectIdentifier *identifier;
122 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
123 if (identifier == NULL) {
127 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
128 identifier->guid = samdb_result_guid(msg, "objectGUID");
130 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
132 identifier->sid = *sid;
134 ZERO_STRUCT(identifier->sid);
139 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
141 return GUID_compare(guid1, &guid2);
145 see if we can filter an attribute using the uptodateness_vector
147 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
148 const struct GUID *originating_invocation_id,
149 uint64_t originating_usn)
151 const struct drsuapi_DsReplicaCursor *c;
152 if (udv == NULL) return false;
153 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
154 originating_invocation_id, udv_compare, c);
155 if (c && originating_usn <= c->highest_usn) {
161 static int uint32_t_cmp(uint32_t a1, uint32_t a2)
163 if (a1 == a2) return 0;
164 return a1 > a2 ? 1 : -1;
167 static int uint32_t_ptr_cmp(uint32_t *a1, uint32_t *a2, void *unused)
169 if (*a1 == *a2) return 0;
170 return *a1 > *a2 ? 1 : -1;
173 static WERROR getncchanges_attid_remote_to_local(const struct dsdb_schema *schema,
174 const struct dsdb_syntax_ctx *ctx,
175 enum drsuapi_DsAttributeId remote_attid_as_enum,
176 enum drsuapi_DsAttributeId *local_attid_as_enum,
177 const struct dsdb_attribute **_sa)
180 const struct dsdb_attribute *sa = NULL;
182 if (ctx->pfm_remote == NULL) {
183 DEBUG(7, ("No prefixMap supplied, falling back to local prefixMap.\n"));
187 werr = dsdb_attribute_drsuapi_remote_to_local(ctx,
188 remote_attid_as_enum,
191 if (!W_ERROR_IS_OK(werr)) {
192 DEBUG(3, ("WARNING: Unable to resolve remote attid, falling back to local prefixMap.\n"));
199 sa = dsdb_attribute_by_attributeID_id(schema, remote_attid_as_enum);
201 return WERR_DS_DRA_SCHEMA_MISMATCH;
203 if (local_attid_as_enum != NULL) {
204 *local_attid_as_enum = sa->attributeID_id;
214 * Similar to function in repl_meta_data without the extra
217 static WERROR get_parsed_dns_trusted(TALLOC_CTX *mem_ctx, struct ldb_message_element *el,
218 struct parsed_dn **pdn)
220 /* Here we get a list of 'struct parsed_dns' without the parsing */
222 *pdn = talloc_zero_array(mem_ctx, struct parsed_dn,
225 return WERR_DS_DRA_INTERNAL_ERROR;
228 for (i = 0; i < el->num_values; i++) {
229 (*pdn)[i].v = &el->values[i];
235 static WERROR getncchanges_update_revealed_list(struct ldb_context *sam_ctx,
237 struct ldb_message **msg,
238 struct ldb_dn *object_dn,
239 const struct GUID *object_guid,
240 const struct dsdb_attribute *sa,
241 struct replPropertyMetaData1 *meta_data,
242 struct ldb_message *revealed_users)
244 enum ndr_err_code ndr_err;
246 char *attr_str = NULL;
247 char *attr_hex = NULL;
249 struct ldb_message_element *existing = NULL, *el_add = NULL, *el_del = NULL;
250 const char * const * secret_attributes = ldb_get_opaque(sam_ctx, "LDB_SECRET_ATTRIBUTE_LIST");
252 if (!ldb_attr_in_list(secret_attributes,
253 sa->lDAPDisplayName)) {
258 ndr_err = ndr_push_struct_blob(&attr_blob, mem_ctx, meta_data, (ndr_push_flags_fn_t)ndr_push_replPropertyMetaData1);
259 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
260 return WERR_DS_DRA_INTERNAL_ERROR;
263 attr_hex = hex_encode_talloc(mem_ctx, attr_blob.data, attr_blob.length);
264 if (attr_hex == NULL) {
265 return WERR_NOT_ENOUGH_MEMORY;
268 attr_str = talloc_asprintf(mem_ctx, "B:%zd:%s:%s", attr_blob.length*2, attr_hex, ldb_dn_get_linearized(object_dn));
269 if (attr_str == NULL) {
270 return WERR_NOT_ENOUGH_MEMORY;
273 existing = ldb_msg_find_element(revealed_users, "msDS-RevealedUsers");
274 if (existing != NULL) {
275 /* Replace the old value (if one exists) with the current one */
276 struct parsed_dn *link_dns;
277 struct parsed_dn *exact = NULL, *unused = NULL;
280 DATA_BLOB partial_meta;
282 werr = get_parsed_dns_trusted(mem_ctx, existing, &link_dns);
283 if (!W_ERROR_IS_OK(werr)) {
287 /* Construct a partial metadata blob to match on in the DB */
288 SIVAL(attid, 0, sa->attributeID_id);
289 partial_meta.length = 4;
290 partial_meta.data = attid;
292 /* Binary search using GUID and attribute id for uniqueness */
293 ldb_err = parsed_dn_find(sam_ctx, link_dns, existing->num_values,
294 object_guid, object_dn,
297 DSDB_SYNTAX_BINARY_DN, true);
299 if (ldb_err != LDB_SUCCESS) {
300 DEBUG(0,(__location__ ": Failed parsed DN find - %s\n",
301 ldb_errstring(sam_ctx)));
302 return WERR_DS_DRA_INTERNAL_ERROR;
306 /* Perform some verification of the blob */
307 struct replPropertyMetaData1 existing_meta_data;
308 ndr_err = ndr_pull_struct_blob_all_noalloc(&exact->dsdb_dn->extra_part,
310 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaData1);
311 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
312 return WERR_DS_DRA_INTERNAL_ERROR;
315 if (existing_meta_data.attid == sa->attributeID_id) {
316 ldb_err = ldb_msg_add_empty(*msg, "msDS-RevealedUsers", LDB_FLAG_MOD_DELETE, &el_del);
317 if (ldb_err != LDB_SUCCESS) {
318 return WERR_DS_DRA_INTERNAL_ERROR;
321 el_del->values = talloc_array((*msg)->elements, struct ldb_val, 1);
322 if (el_del->values == NULL) {
323 return WERR_NOT_ENOUGH_MEMORY;
325 el_del->values[0] = *exact->v;
326 el_del->num_values = 1;
328 return WERR_DS_DRA_INTERNAL_ERROR;
333 ldb_err = ldb_msg_add_empty(*msg, "msDS-RevealedUsers", LDB_FLAG_MOD_ADD, &el_add);
334 if (ldb_err != LDB_SUCCESS) {
335 return WERR_DS_DRA_INTERNAL_ERROR;
338 el_add->values = talloc_array((*msg)->elements, struct ldb_val, 1);
339 if (el_add->values == NULL) {
340 return WERR_NOT_ENOUGH_MEMORY;
344 el_add->values[0] = data_blob_string_const(attr_str);
345 el_add->num_values = 1;
351 * This function filter attributes for build_object based on the
352 * uptodatenessvector and partial attribute set.
354 * Any secret attributes are forced here for REPL_SECRET, and audited at this
355 * point with msDS-RevealedUsers.
357 static WERROR get_nc_changes_filter_attrs(struct drsuapi_DsReplicaObjectListItemEx *obj,
358 struct replPropertyMetaDataBlob md,
359 struct ldb_context *sam_ctx,
360 const struct ldb_message *msg,
361 const struct GUID *guid,
363 uint64_t highest_usn,
364 const struct dsdb_attribute *rdn_sa,
365 struct dsdb_schema *schema,
366 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
367 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
371 struct ldb_message **revealed_list_msg,
372 struct ldb_message *existing_revealed_list_msg)
376 for (n=i=0; i<md.ctr.ctr1.count; i++) {
377 const struct dsdb_attribute *sa;
378 bool force_attribute = false;
380 /* if the attribute has not changed, and it is not the
381 instanceType then don't include it */
382 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
384 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
386 /* don't include the rDN */
387 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
389 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
391 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
392 (unsigned int)md.ctr.ctr1.array[i].attid,
393 ldb_dn_get_linearized(msg->dn)));
394 return WERR_DS_DRA_INTERNAL_ERROR;
398 struct ldb_message_element *el;
399 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
400 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
401 /* don't send upgraded links inline */
407 !dsdb_attr_in_rodc_fas(sa)) {
408 force_attribute = true;
409 DEBUG(4,("Forcing attribute %s in %s\n",
410 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
411 werr = getncchanges_update_revealed_list(sam_ctx, obj,
414 &md.ctr.ctr1.array[i],
415 existing_revealed_list_msg);
416 if (!W_ERROR_IS_OK(werr)) {
421 /* filter by uptodateness_vector */
422 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
424 udv_filter(uptodateness_vector,
425 &md.ctr.ctr1.array[i].originating_invocation_id,
426 md.ctr.ctr1.array[i].originating_usn)) {
430 /* filter by partial_attribute_set */
431 if (partial_attribute_set && !force_attribute) {
432 uint32_t *result = NULL;
433 BINARY_ARRAY_SEARCH_V(local_pas, partial_attribute_set->num_attids, sa->attributeID_id,
434 uint32_t_cmp, result);
435 if (result == NULL) {
440 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
441 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
442 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
443 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
444 attids[n] = md.ctr.ctr1.array[i].attid;
455 drsuapi_DsGetNCChanges for one object
457 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
458 const struct ldb_message *msg,
459 struct ldb_context *sam_ctx,
460 struct ldb_dn *ncRoot_dn,
462 struct dsdb_schema *schema,
463 DATA_BLOB *session_key,
464 uint64_t highest_usn,
465 uint32_t replica_flags,
466 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
467 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
468 enum drsuapi_DsExtendedOperation extended_op,
469 bool force_object_return,
471 struct ldb_dn *machine_dn,
472 const struct GUID *guid)
474 const struct ldb_val *md_value;
476 struct replPropertyMetaDataBlob md;
479 enum ndr_err_code ndr_err;
482 const struct dsdb_attribute *rdn_sa;
484 unsigned int instanceType;
485 struct dsdb_syntax_ctx syntax_ctx;
486 struct ldb_result *res = NULL;
490 /* make dsdb sytanx context for conversions */
491 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
492 syntax_ctx.is_schema_nc = is_schema_nc;
494 uSNChanged = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
495 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
496 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
497 obj->is_nc_prefix = true;
498 obj->parent_object_guid = NULL;
500 obj->is_nc_prefix = false;
501 obj->parent_object_guid = talloc(obj, struct GUID);
502 if (obj->parent_object_guid == NULL) {
503 return WERR_DS_DRA_INTERNAL_ERROR;
505 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
506 if (GUID_all_zero(obj->parent_object_guid)) {
507 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
508 ldb_dn_get_linearized(msg->dn)));
509 return WERR_DS_DRA_INTERNAL_ERROR;
512 obj->next_object = NULL;
514 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
516 /* nothing to send */
520 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
521 /* don't send uninstantiated objects */
525 if (uSNChanged <= highest_usn) {
526 /* nothing to send */
530 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
531 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
532 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
533 return WERR_DS_DRA_INTERNAL_ERROR;
536 if (md.version != 1) {
537 return WERR_DS_DRA_INTERNAL_ERROR;
540 rdn = ldb_dn_get_rdn_name(msg->dn);
542 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
543 return WERR_DS_DRA_INTERNAL_ERROR;
546 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
547 if (rdn_sa == NULL) {
548 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
549 rdn, ldb_dn_get_linearized(msg->dn)));
550 return WERR_DS_DRA_INTERNAL_ERROR;
553 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
554 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
556 obj->object.identifier = get_object_identifier(obj, msg);
557 if (obj->object.identifier == NULL) {
558 return WERR_NOT_ENOUGH_MEMORY;
560 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
562 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
564 if (extended_op == DRSUAPI_EXOP_REPL_SECRET) {
565 /* Get the existing revealed users for the destination */
566 struct ldb_message *revealed_list_msg = NULL;
567 struct ldb_message *existing_revealed_list_msg = NULL;
568 const char *machine_attrs[] = {
569 "msDS-RevealedUsers",
573 revealed_list_msg = ldb_msg_new(sam_ctx);
574 if (revealed_list_msg == NULL) {
575 return WERR_NOT_ENOUGH_MEMORY;
577 revealed_list_msg->dn = machine_dn;
579 ret = ldb_transaction_start(sam_ctx);
580 if (ret != LDB_SUCCESS) {
581 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
582 ldb_errstring(sam_ctx)));
583 return WERR_DS_DRA_INTERNAL_ERROR;
586 ldb_err = dsdb_search_dn(sam_ctx, obj, &res, machine_dn, machine_attrs, DSDB_SEARCH_SHOW_EXTENDED_DN);
587 if (ldb_err != LDB_SUCCESS || res->count != 1) {
588 ldb_transaction_cancel(sam_ctx);
589 return WERR_DS_DRA_INTERNAL_ERROR;
592 existing_revealed_list_msg = res->msgs[0];
594 werr = get_nc_changes_filter_attrs(obj, md, sam_ctx, msg,
595 guid, &n, highest_usn,
598 partial_attribute_set, local_pas,
602 existing_revealed_list_msg);
603 if (!W_ERROR_IS_OK(werr)) {
604 ldb_transaction_cancel(sam_ctx);
608 if (revealed_list_msg != NULL) {
609 ret = ldb_modify(sam_ctx, revealed_list_msg);
610 if (ret != LDB_SUCCESS) {
611 DEBUG(0,(__location__ ": Failed to alter revealed links - %s\n",
612 ldb_errstring(sam_ctx)));
613 ldb_transaction_cancel(sam_ctx);
614 return WERR_DS_DRA_INTERNAL_ERROR;
618 ret = ldb_transaction_commit(sam_ctx);
619 if (ret != LDB_SUCCESS) {
620 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
621 ldb_errstring(sam_ctx)));
622 return WERR_DS_DRA_INTERNAL_ERROR;
625 werr = get_nc_changes_filter_attrs(obj, md, sam_ctx, msg, guid,
626 &n, highest_usn, rdn_sa,
627 schema, uptodateness_vector,
628 partial_attribute_set, local_pas,
633 if (!W_ERROR_IS_OK(werr)) {
638 /* ignore it if its an empty change. Note that renames always
639 * change the 'name' attribute, so they won't be ignored by
642 * the force_object_return check is used to force an empty
643 * object return when we timeout in the getncchanges loop.
644 * This allows us to return an empty object, which keeps the
645 * client happy while preventing timeouts
649 attids[0] == DRSUAPI_ATTID_instanceType &&
650 !force_object_return)) {
651 talloc_free(obj->meta_data_ctr);
652 obj->meta_data_ctr = NULL;
656 obj->meta_data_ctr->count = n;
658 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
659 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
660 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
661 obj->object.attribute_ctr.num_attributes);
662 if (obj->object.attribute_ctr.attributes == NULL) {
663 return WERR_NOT_ENOUGH_MEMORY;
667 * Note that the meta_data array and the attributes array must
668 * be the same size and in the same order
670 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
671 struct ldb_message_element *el;
672 const struct dsdb_attribute *sa;
674 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
676 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
677 return WERR_DS_DRA_INTERNAL_ERROR;
680 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
682 /* this happens for attributes that have been removed */
683 DEBUG(5,("No element '%s' for attributeID %u in message\n",
684 sa->lDAPDisplayName, attids[i]));
685 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
686 obj->object.attribute_ctr.attributes[i].attid =
687 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
689 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
690 &obj->object.attribute_ctr.attributes[i]);
691 if (!W_ERROR_IS_OK(werr)) {
692 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
693 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
697 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
698 * check if attribute is secret and send a null value
700 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
701 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
702 &obj->meta_data_ctr->meta_data[i]);
704 /* some attributes needs to be encrypted
706 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
707 &obj->object.attribute_ctr.attributes[i]);
708 if (!W_ERROR_IS_OK(werr)) {
709 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
710 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
715 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
716 DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID: "
720 ldb_dn_get_linearized(msg->dn),
722 obj->object.attribute_ctr.attributes[i].attid));
723 return WERR_DS_DATABASE_ERROR;
731 add one linked attribute from an object to the list of linked
732 attributes in a getncchanges request
734 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
735 struct ldb_context *sam_ctx,
736 const struct dsdb_schema *schema,
737 const struct dsdb_attribute *sa,
738 const struct ldb_message *msg,
739 struct dsdb_dn *dsdb_dn,
740 struct drsuapi_DsReplicaLinkedAttribute **la_list,
744 struct drsuapi_DsReplicaLinkedAttribute *la;
749 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
750 W_ERROR_HAVE_NO_MEMORY(*la_list);
752 la = &(*la_list)[*la_count];
754 la->identifier = get_object_identifier(*la_list, msg);
755 W_ERROR_HAVE_NO_MEMORY(la->identifier);
757 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
760 /* We have to check that the inactive link still point to an existing object */
766 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
767 if (strncmp(v, "TRUE", 4) == 0) {
769 * Note: we skip the transmition of the deleted link even if the other part used to
770 * know about it because when we transmit the deletion of the object, the link will
771 * be deleted too due to deletion of object where link points and Windows do so.
773 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
774 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
776 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
777 * if it join an existing domain with deleted objets, it firsts impose to have a
778 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
779 * either during initial replication or after the getNCChanges.
780 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
782 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
783 * If FL >=2K8R2 we are sure that this attribute will be here.
784 * For this kind of forest level we do not return the link if the object is recycled
785 * (isRecycled = true).
787 if (strncmp(v, "TRUE", 4) == 0) {
788 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
789 ldb_dn_get_linearized(msg->dn)));
796 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
797 if (!NT_STATUS_IS_OK(status)) {
798 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
799 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
800 return ntstatus_to_werror(status);
802 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
803 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
804 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
805 GUID_string(mem_ctx, &guid)));
807 } else if (ret != LDB_SUCCESS) {
808 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
809 GUID_string(mem_ctx, &guid),
814 la->attid = dsdb_attribute_get_attid(sa, is_schema_nc);
815 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
817 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
818 if (!NT_STATUS_IS_OK(status)) {
819 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
820 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
821 return ntstatus_to_werror(status);
823 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
824 if (!NT_STATUS_IS_OK(status)) {
825 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
826 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
827 return ntstatus_to_werror(status);
829 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
830 if (!NT_STATUS_IS_OK(status)) {
831 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
832 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
833 return ntstatus_to_werror(status);
835 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
836 if (!NT_STATUS_IS_OK(status)) {
837 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
838 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
839 return ntstatus_to_werror(status);
842 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
843 if (!NT_STATUS_IS_OK(status)) {
844 /* this is possible for upgraded links */
845 la->originating_add_time = la->meta_data.originating_change_time;
848 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
849 W_ERROR_NOT_OK_RETURN(werr);
857 add linked attributes from an object to the list of linked
858 attributes in a getncchanges request
860 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
862 struct ldb_dn *ncRoot_dn,
864 struct dsdb_schema *schema,
865 uint64_t highest_usn,
866 uint32_t replica_flags,
867 const struct ldb_message *msg,
868 struct drsuapi_DsReplicaLinkedAttribute **la_list,
870 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
873 TALLOC_CTX *tmp_ctx = NULL;
874 uint64_t uSNChanged = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
875 bool is_critical = ldb_msg_find_attr_as_bool(msg, "isCriticalSystemObject", false);
877 if (replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
883 if (uSNChanged <= highest_usn) {
887 tmp_ctx = talloc_new(mem_ctx);
888 if (tmp_ctx == NULL) {
889 return WERR_NOT_ENOUGH_MEMORY;
892 for (i=0; i<msg->num_elements; i++) {
893 struct ldb_message_element *el = &msg->elements[i];
894 const struct dsdb_attribute *sa;
897 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
899 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
900 /* we only want forward links */
904 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
905 /* its an old style link, it will have been
906 * sent in the main replication data */
910 for (j=0; j<el->num_values; j++) {
911 struct dsdb_dn *dsdb_dn;
913 uint64_t originating_usn;
914 NTSTATUS status, status2;
916 struct GUID originating_invocation_id;
918 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
919 if (dsdb_dn == NULL) {
920 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
921 el->name, ldb_dn_get_linearized(msg->dn)));
922 talloc_free(tmp_ctx);
923 return WERR_DS_DRA_INTERNAL_ERROR;
926 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
927 if (!NT_STATUS_IS_OK(status)) {
928 /* this can happen for attributes
929 given to us with old style meta
934 if (local_usn > uSNChanged) {
935 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
936 el->name, ldb_dn_get_linearized(msg->dn)));
937 talloc_free(tmp_ctx);
938 return WERR_DS_DRA_INTERNAL_ERROR;
941 if (local_usn <= highest_usn) {
945 status = dsdb_get_extended_dn_guid(dsdb_dn->dn,
946 &originating_invocation_id,
948 status2 = dsdb_get_extended_dn_uint64(dsdb_dn->dn,
950 "RMD_ORIGINATING_USN");
952 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2)) {
953 if (udv_filter(uptodateness_vector,
954 &originating_invocation_id,
960 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema,
961 sa, msg, dsdb_dn, la_list,
962 la_count, is_schema_nc);
963 if (!W_ERROR_IS_OK(werr)) {
964 talloc_free(tmp_ctx);
970 talloc_free(tmp_ctx);
975 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
977 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
978 struct ldb_dn *ncRoot_dn,
979 struct drsuapi_DsReplicaCursor2CtrEx *udv)
987 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
988 if (ret != LDB_SUCCESS) {
989 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
990 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
991 return WERR_DS_DRA_INTERNAL_ERROR;
998 /* comparison function for linked attributes - see CompareLinks() in
999 * MS-DRSR section 4.1.10.5.17 */
1000 static int linked_attribute_compare(const struct la_for_sorting *la1,
1001 const struct la_for_sorting *la2,
1005 c = memcmp(la1->source_guid,
1006 la2->source_guid, sizeof(la2->source_guid));
1011 if (la1->link->attid != la2->link->attid) {
1012 return la1->link->attid < la2->link->attid? -1:1;
1015 if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
1016 (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
1017 return (la1->link->flags &
1018 DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
1021 return memcmp(la1->target_guid,
1022 la2->target_guid, sizeof(la2->target_guid));
1025 struct drsuapi_changed_objects {
1032 sort the objects we send first by uSNChanged
1034 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
1035 struct drsuapi_changed_objects *m2,
1036 struct drsuapi_getncchanges_state *getnc_state)
1040 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
1045 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
1050 if (m1->usn == m2->usn) {
1051 return ldb_dn_compare(m2->dn, m1->dn);
1054 if (m1->usn < m2->usn) {
1063 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
1065 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
1066 TALLOC_CTX *mem_ctx,
1067 struct drsuapi_DsGetNCChangesRequest10 *req10,
1068 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1069 struct ldb_dn **rid_manager_dn)
1071 struct ldb_dn *req_dn, *ntds_dn = NULL;
1073 struct ldb_context *ldb = b_state->sam_ctx;
1074 struct ldb_result *ext_res;
1075 struct dsdb_fsmo_extended_op *exop;
1080 - verify that the DN being asked for is the RID Manager DN
1081 - verify that we are the RID Manager
1084 /* work out who is the RID Manager, also return to caller */
1085 ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
1086 if (ret != LDB_SUCCESS) {
1087 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
1088 return WERR_DS_DRA_INTERNAL_ERROR;
1091 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1092 if (!ldb_dn_validate(req_dn) ||
1093 ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
1094 /* that isn't the RID Manager DN */
1095 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
1096 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1097 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1101 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1102 ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
1103 if (ret != LDB_SUCCESS) {
1104 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1105 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1106 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1110 /* find the DN of the RID Manager */
1111 ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
1112 if (ret != LDB_SUCCESS) {
1113 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1114 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1115 return WERR_DS_DRA_INTERNAL_ERROR;
1119 /* we're not the RID Manager - go away */
1120 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1121 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1125 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
1126 W_ERROR_HAVE_NO_MEMORY(exop);
1128 exop->fsmo_info = req10->fsmo_info;
1129 exop->destination_dsa_guid = req10->destination_dsa_guid;
1131 ret = ldb_transaction_start(ldb);
1132 if (ret != LDB_SUCCESS) {
1133 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1134 ldb_errstring(ldb)));
1135 return WERR_DS_DRA_INTERNAL_ERROR;
1138 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
1139 if (ret != LDB_SUCCESS) {
1140 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
1141 ldb_errstring(ldb)));
1142 ldb_transaction_cancel(ldb);
1143 return WERR_DS_DRA_INTERNAL_ERROR;
1146 ret = ldb_transaction_commit(ldb);
1147 if (ret != LDB_SUCCESS) {
1148 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1149 ldb_errstring(ldb)));
1150 return WERR_DS_DRA_INTERNAL_ERROR;
1153 talloc_free(ext_res);
1155 DEBUG(2,("Allocated RID pool for server %s\n",
1156 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1158 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1164 handle a DRSUAPI_EXOP_REPL_SECRET call
1166 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
1167 TALLOC_CTX *mem_ctx,
1168 struct drsuapi_DsGetNCChangesRequest10 *req10,
1169 struct dom_sid *user_sid,
1170 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1171 bool has_get_all_changes,
1172 struct ldb_dn **machine_dn)
1174 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1175 struct ldb_dn *obj_dn = NULL;
1176 struct ldb_dn *ntds_dn = NULL, *server_dn = NULL;
1177 struct ldb_dn *rodc_dn, *krbtgt_link_dn;
1179 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", "objectGUID", NULL };
1180 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
1181 struct ldb_result *rodc_res = NULL, *obj_res = NULL;
1182 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
1183 const struct dom_sid *object_sid = NULL;
1185 const struct dom_sid *additional_sids[] = { NULL, NULL };
1187 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
1188 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1191 * we need to work out if we will allow this DC to
1192 * replicate the secrets for this object
1194 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
1198 if (b_state->sam_ctx_system == NULL) {
1199 /* this operation needs system level access */
1200 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
1201 return WERR_DS_DRA_ACCESS_DENIED;
1205 * Before we accept or deny, fetch the machine DN for the destination
1208 * If we are the RODC, we will check that this matches the SID.
1210 ret = dsdb_find_dn_by_guid(b_state->sam_ctx_system, mem_ctx,
1211 &req10->destination_dsa_guid, 0,
1213 if (ret != LDB_SUCCESS) {
1217 server_dn = ldb_dn_get_parent(mem_ctx, ntds_dn);
1218 if (server_dn == NULL) {
1222 ret = samdb_reference_dn(b_state->sam_ctx_system, mem_ctx, server_dn,
1223 "serverReference", machine_dn);
1225 if (ret != LDB_SUCCESS) {
1230 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
1232 * The pseudo code indicate
1233 * revealsecrets = true
1234 * if IsRevealSecretRequest(msgIn) then
1235 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
1237 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
1238 * <... check if this account is ok to be replicated on this DC ...>
1239 * <... and if not reveal secrets = no ...>
1241 * reveal secrets = false
1246 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
1247 * then you can do EXOP_REPL_SECRETS
1249 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
1250 if (!ldb_dn_validate(obj_dn)) goto failed;
1252 if (has_get_all_changes) {
1256 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1257 dom_sid_string(mem_ctx, user_sid));
1258 if (!ldb_dn_validate(rodc_dn)) goto failed;
1260 /* do the two searches we need */
1261 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1262 DSDB_SEARCH_SHOW_EXTENDED_DN);
1263 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1265 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1266 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1268 /* if the object SID is equal to the user_sid, allow */
1269 object_sid = samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid");
1270 if (dom_sid_equal(user_sid, object_sid)) {
1274 additional_sids[0] = object_sid;
1277 * Must be an RODC account at this point, verify machine DN matches the
1280 if (ldb_dn_compare(rodc_res->msgs[0]->dn, *machine_dn) != 0) {
1284 /* an RODC is allowed to get its own krbtgt account secrets */
1285 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1286 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1287 if (krbtgt_link_dn != NULL &&
1288 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1292 /* but it isn't allowed to get anyone elses krbtgt secrets */
1293 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1294 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1298 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1299 "userAccountControl", 0) &
1300 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1304 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1305 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1306 if (!W_ERROR_IS_OK(werr)) {
1310 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1311 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1312 if (!W_ERROR_IS_OK(werr)) {
1317 * The SID list needs to include itself as well as the tokenGroups.
1319 * TODO determine if sIDHistory is required for this check
1321 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1322 mem_ctx, "tokenGroups", &token_sids,
1323 additional_sids, 1);
1324 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1328 if (never_reveal_sids &&
1329 sid_list_match(token_sids, never_reveal_sids)) {
1334 sid_list_match(token_sids, reveal_sids)) {
1340 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1341 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1342 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1343 return WERR_DS_DRA_SECRETS_DENIED;
1346 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1347 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1348 ldb_dn_get_linearized(*machine_dn)));
1349 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1350 req10->highwatermark.highest_usn = 0;
1354 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1355 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1356 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1357 return WERR_DS_DRA_BAD_DN;
1361 handle a DRSUAPI_EXOP_REPL_OBJ call
1363 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1364 TALLOC_CTX *mem_ctx,
1365 struct drsuapi_DsGetNCChangesRequest10 *req10,
1366 struct dom_sid *user_sid,
1367 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1369 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1371 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1372 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1374 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1380 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1381 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1382 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1384 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1385 TALLOC_CTX *mem_ctx,
1386 struct drsuapi_DsGetNCChangesRequest10 *req10,
1387 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1389 struct ldb_dn *req_dn, *ntds_dn;
1392 struct ldb_context *ldb = b_state->sam_ctx;
1393 struct ldb_message *msg;
1398 - verify that the client dn exists
1399 - verify that we are the current master
1402 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1403 if (!ldb_dn_validate(req_dn)) {
1404 /* that is not a valid dn */
1405 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1406 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1407 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1411 /* find the DN of the current role owner */
1412 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1413 if (ret != LDB_SUCCESS) {
1414 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1415 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1416 return WERR_DS_DRA_INTERNAL_ERROR;
1420 /* we're not the RID Manager or role owner - go away */
1421 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1422 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1426 /* change the current master */
1427 msg = ldb_msg_new(ldb);
1428 W_ERROR_HAVE_NO_MEMORY(msg);
1429 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1430 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1432 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1433 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1434 if (ret != LDB_SUCCESS) {
1435 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1436 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1438 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1442 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1445 return WERR_DS_DRA_INTERNAL_ERROR;
1448 for (i=0;i<msg->num_elements;i++) {
1449 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1452 ret = ldb_transaction_start(ldb);
1453 if (ret != LDB_SUCCESS) {
1454 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1455 ldb_errstring(ldb)));
1456 return WERR_DS_DRA_INTERNAL_ERROR;
1459 ret = ldb_modify(ldb, msg);
1460 if (ret != LDB_SUCCESS) {
1461 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1462 ldb_errstring(ldb)));
1463 ldb_transaction_cancel(ldb);
1464 return WERR_DS_DRA_INTERNAL_ERROR;
1467 ret = ldb_transaction_commit(ldb);
1468 if (ret != LDB_SUCCESS) {
1469 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1470 ldb_errstring(ldb)));
1471 return WERR_DS_DRA_INTERNAL_ERROR;
1474 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1480 see if this getncchanges request includes a request to reveal secret information
1482 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1483 struct drsuapi_DsGetNCChangesRequest10 *req10,
1484 struct dsdb_schema_prefixmap *pfm_remote,
1485 bool *is_secret_request)
1487 enum drsuapi_DsExtendedOperation exop;
1489 struct dsdb_schema *schema;
1490 struct dsdb_syntax_ctx syntax_ctx;
1492 *is_secret_request = true;
1494 exop = req10->extended_op;
1497 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1498 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1499 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1500 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1501 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1502 /* FSMO exops can reveal secrets */
1503 *is_secret_request = true;
1505 case DRSUAPI_EXOP_REPL_SECRET:
1506 case DRSUAPI_EXOP_REPL_OBJ:
1507 case DRSUAPI_EXOP_NONE:
1511 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1512 *is_secret_request = false;
1516 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1517 req10->partial_attribute_set == NULL) {
1518 /* they want secrets */
1519 *is_secret_request = true;
1523 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1524 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1525 syntax_ctx.pfm_remote = pfm_remote;
1527 /* check the attributes they asked for */
1528 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1529 const struct dsdb_attribute *sa;
1530 WERROR werr = getncchanges_attid_remote_to_local(schema,
1532 req10->partial_attribute_set->attids[i],
1536 if (!W_ERROR_IS_OK(werr)) {
1537 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1538 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1542 if (!dsdb_attr_in_rodc_fas(sa)) {
1543 *is_secret_request = true;
1548 if (req10->partial_attribute_set_ex) {
1549 /* check the extended attributes they asked for */
1550 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1551 const struct dsdb_attribute *sa;
1552 WERROR werr = getncchanges_attid_remote_to_local(schema,
1554 req10->partial_attribute_set_ex->attids[i],
1558 if (!W_ERROR_IS_OK(werr)) {
1559 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1560 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1564 if (!dsdb_attr_in_rodc_fas(sa)) {
1565 *is_secret_request = true;
1571 *is_secret_request = false;
1576 see if this getncchanges request is only for attributes in the GC
1577 partial attribute set
1579 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1580 struct drsuapi_DsGetNCChangesRequest10 *req10,
1581 struct dsdb_schema_prefixmap *pfm_remote,
1582 bool *is_gc_pas_request)
1584 enum drsuapi_DsExtendedOperation exop;
1586 struct dsdb_schema *schema;
1587 struct dsdb_syntax_ctx syntax_ctx;
1589 exop = req10->extended_op;
1592 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1593 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1594 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1595 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1596 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1597 case DRSUAPI_EXOP_REPL_SECRET:
1598 *is_gc_pas_request = false;
1600 case DRSUAPI_EXOP_REPL_OBJ:
1601 case DRSUAPI_EXOP_NONE:
1605 if (req10->partial_attribute_set == NULL) {
1606 /* they want it all */
1607 *is_gc_pas_request = false;
1611 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1612 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1613 syntax_ctx.pfm_remote = pfm_remote;
1615 /* check the attributes they asked for */
1616 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1617 const struct dsdb_attribute *sa;
1618 WERROR werr = getncchanges_attid_remote_to_local(schema,
1620 req10->partial_attribute_set->attids[i],
1624 if (!W_ERROR_IS_OK(werr)) {
1625 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1626 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1630 if (!sa->isMemberOfPartialAttributeSet) {
1631 *is_gc_pas_request = false;
1636 if (req10->partial_attribute_set_ex) {
1637 /* check the extended attributes they asked for */
1638 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1639 const struct dsdb_attribute *sa;
1640 WERROR werr = getncchanges_attid_remote_to_local(schema,
1642 req10->partial_attribute_set_ex->attids[i],
1646 if (!W_ERROR_IS_OK(werr)) {
1647 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1648 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1652 if (!sa->isMemberOfPartialAttributeSet) {
1653 *is_gc_pas_request = false;
1659 *is_gc_pas_request = true;
1665 map from req8 to req10
1667 static struct drsuapi_DsGetNCChangesRequest10 *
1668 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1669 struct drsuapi_DsGetNCChangesRequest8 *req8)
1671 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1672 struct drsuapi_DsGetNCChangesRequest10);
1673 if (req10 == NULL) {
1677 req10->destination_dsa_guid = req8->destination_dsa_guid;
1678 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1679 req10->naming_context = req8->naming_context;
1680 req10->highwatermark = req8->highwatermark;
1681 req10->uptodateness_vector = req8->uptodateness_vector;
1682 req10->replica_flags = req8->replica_flags;
1683 req10->max_object_count = req8->max_object_count;
1684 req10->max_ndr_size = req8->max_ndr_size;
1685 req10->extended_op = req8->extended_op;
1686 req10->fsmo_info = req8->fsmo_info;
1687 req10->partial_attribute_set = req8->partial_attribute_set;
1688 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1689 req10->mapping_ctr = req8->mapping_ctr;
1694 static const char *collect_objects_attrs[] = { "uSNChanged",
1699 * Collects object for normal replication cycle.
1701 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1702 TALLOC_CTX *mem_ctx,
1703 struct drsuapi_DsGetNCChangesRequest10 *req10,
1704 struct ldb_dn *search_dn,
1705 const char *extra_filter,
1706 struct ldb_result **search_res)
1709 char* search_filter;
1710 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1711 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1712 bool critical_only = false;
1714 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1715 critical_only = true;
1718 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1719 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1720 scope = LDB_SCOPE_BASE;
1721 critical_only = false;
1724 /* Construct response. */
1725 search_filter = talloc_asprintf(mem_ctx,
1726 "(uSNChanged>=%llu)",
1727 (unsigned long long)(getnc_state->min_usn+1));
1730 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1733 if (critical_only) {
1734 search_filter = talloc_asprintf(mem_ctx,
1735 "(&%s(isCriticalSystemObject=TRUE))",
1739 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1740 scope = LDB_SCOPE_BASE;
1744 search_dn = getnc_state->ncRoot_dn;
1747 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1748 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1749 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1751 collect_objects_attrs,
1753 if (ret != LDB_SUCCESS) {
1754 return WERR_DS_DRA_INTERNAL_ERROR;
1761 * Collects object for normal replication cycle.
1763 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1764 TALLOC_CTX *mem_ctx,
1765 struct drsuapi_DsGetNCChangesRequest10 *req10,
1766 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1767 struct ldb_dn *search_dn,
1768 const char *extra_filter,
1769 struct ldb_result **search_res)
1771 /* we have nothing to do in case of ex-op failure */
1772 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1776 switch (req10->extended_op) {
1777 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1780 struct ldb_dn *ntds_dn = NULL;
1781 struct ldb_dn *server_dn = NULL;
1782 struct ldb_dn *machine_dn = NULL;
1783 struct ldb_dn *rid_set_dn = NULL;
1784 struct ldb_result *search_res2 = NULL;
1785 struct ldb_result *search_res3 = NULL;
1786 TALLOC_CTX *frame = talloc_stackframe();
1787 /* get RID manager, RID set and server DN (in that order) */
1789 /* This first search will get the RID Manager */
1790 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1792 search_dn, LDB_SCOPE_BASE,
1793 collect_objects_attrs,
1795 if (ret != LDB_SUCCESS) {
1796 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1797 ldb_dn_get_linearized(search_dn),
1798 ldb_errstring(b_state->sam_ctx)));
1800 return WERR_DS_DRA_INTERNAL_ERROR;
1803 if ((*search_res)->count != 1) {
1804 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1805 ldb_dn_get_linearized(search_dn),
1806 (*search_res)->count));
1808 return WERR_DS_DRA_INTERNAL_ERROR;
1811 /* Now extend it to the RID set */
1813 /* Find the computer account DN for the destination
1814 * dsa GUID specified */
1816 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1817 &req10->destination_dsa_guid, 0,
1819 if (ret != LDB_SUCCESS) {
1820 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1822 &req10->destination_dsa_guid),
1823 ldb_errstring(b_state->sam_ctx)));
1825 return WERR_DS_DRA_INTERNAL_ERROR;
1828 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1831 return WERR_DS_DRA_INTERNAL_ERROR;
1834 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1835 "serverReference", &machine_dn);
1836 if (ret != LDB_SUCCESS) {
1837 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1838 ldb_dn_get_linearized(server_dn),
1839 ldb_errstring(b_state->sam_ctx)));
1841 return WERR_DS_DRA_INTERNAL_ERROR;
1844 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1845 "rIDSetReferences", &rid_set_dn);
1846 if (ret != LDB_SUCCESS) {
1847 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1848 ldb_dn_get_linearized(server_dn),
1849 ldb_errstring(b_state->sam_ctx)));
1851 return WERR_DS_DRA_INTERNAL_ERROR;
1855 /* This first search will get the RID Manager, now get the RID set */
1856 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1858 rid_set_dn, LDB_SCOPE_BASE,
1859 collect_objects_attrs,
1861 if (ret != LDB_SUCCESS) {
1862 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1863 ldb_dn_get_linearized(rid_set_dn),
1864 ldb_errstring(b_state->sam_ctx)));
1866 return WERR_DS_DRA_INTERNAL_ERROR;
1869 if (search_res2->count != 1) {
1870 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1871 ldb_dn_get_linearized(rid_set_dn),
1872 search_res2->count));
1874 return WERR_DS_DRA_INTERNAL_ERROR;
1877 /* Finally get the server DN */
1878 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1880 machine_dn, LDB_SCOPE_BASE,
1881 collect_objects_attrs,
1883 if (ret != LDB_SUCCESS) {
1884 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1885 ldb_dn_get_linearized(server_dn),
1886 ldb_errstring(b_state->sam_ctx)));
1888 return WERR_DS_DRA_INTERNAL_ERROR;
1891 if (search_res3->count != 1) {
1892 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1893 ldb_dn_get_linearized(server_dn),
1894 search_res3->count));
1896 return WERR_DS_DRA_INTERNAL_ERROR;
1899 /* Now extend the original search_res with these answers */
1900 (*search_res)->count = 3;
1902 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1903 struct ldb_message *,
1904 (*search_res)->count);
1905 if ((*search_res)->msgs == NULL) {
1907 return WERR_NOT_ENOUGH_MEMORY;
1911 talloc_steal(mem_ctx, *search_res);
1912 (*search_res)->msgs[1] =
1913 talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1914 (*search_res)->msgs[2] =
1915 talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1921 /* TODO: implement extended op specific collection
1922 * of objects. Right now we just normal procedure
1923 * for collecting objects */
1924 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1928 static void dcesrv_drsuapi_update_highwatermark(const struct ldb_message *msg,
1930 struct drsuapi_DsReplicaHighWaterMark *hwm)
1932 uint64_t uSN = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
1934 if (uSN > max_usn) {
1936 * Only report the max_usn we had at the start
1937 * of the replication cycle.
1939 * If this object has changed lately we better
1940 * let the destination dsa refetch the change.
1941 * This is better than the risk of loosing some
1942 * objects or linked attributes.
1947 if (uSN <= hwm->tmp_highest_usn) {
1951 hwm->tmp_highest_usn = uSN;
1952 hwm->reserved_usn = 0;
1956 * Adds an object's GUID to the cache of objects already sent.
1957 * This avoids us sending the same object multiple times when
1958 * the GetNCChanges request uses a flag like GET_ANC.
1960 static WERROR dcesrv_drsuapi_obj_cache_add(struct db_context *obj_cache,
1961 const struct GUID *guid)
1963 enum ndr_err_code ndr_err;
1964 uint8_t guid_buf[DRS_GUID_SIZE] = { 0, };
1967 .length = sizeof(guid_buf),
1979 ndr_err = ndr_push_struct_into_fixed_blob(&b, guid,
1980 (ndr_push_flags_fn_t)ndr_push_GUID);
1981 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1982 return WERR_DS_DRA_INTERNAL_ERROR;
1985 status = dbwrap_store(obj_cache, key, val, TDB_REPLACE);
1986 if (!NT_STATUS_IS_OK(status)) {
1987 return WERR_DS_DRA_INTERNAL_ERROR;
1994 * Checks if the object with the GUID specified already exists in the
1995 * object cache, i.e. it's already been sent in a GetNCChanges response.
1997 static WERROR dcesrv_drsuapi_obj_cache_exists(struct db_context *obj_cache,
1998 const struct GUID *guid)
2000 enum ndr_err_code ndr_err;
2001 uint8_t guid_buf[DRS_GUID_SIZE] = { 0, };
2004 .length = sizeof(guid_buf),
2012 ndr_err = ndr_push_struct_into_fixed_blob(&b, guid,
2013 (ndr_push_flags_fn_t)ndr_push_GUID);
2014 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2015 return WERR_DS_DRA_INTERNAL_ERROR;
2018 exists = dbwrap_exists(obj_cache, key);
2020 return WERR_OBJECT_NOT_FOUND;
2023 return WERR_OBJECT_NAME_EXISTS;
2027 * Copies the la_list specified into a sorted array, ready to be sent in a
2028 * GetNCChanges response.
2030 static WERROR getncchanges_get_sorted_array(const struct drsuapi_DsReplicaLinkedAttribute *la_list,
2031 const uint32_t link_count,
2032 struct ldb_context *sam_ctx,
2033 TALLOC_CTX *mem_ctx,
2034 const struct dsdb_schema *schema,
2035 struct la_for_sorting **ret_array)
2038 struct la_for_sorting *guid_array;
2039 WERROR werr = WERR_OK;
2042 guid_array = talloc_array(mem_ctx, struct la_for_sorting, link_count);
2043 if (guid_array == NULL) {
2044 DEBUG(0, ("Out of memory allocating %u linked attributes for sorting", link_count));
2045 return WERR_NOT_ENOUGH_MEMORY;
2048 for (j = 0; j < link_count; j++) {
2050 /* we need to get the target GUIDs to compare */
2052 const struct drsuapi_DsReplicaLinkedAttribute *la = &la_list[j];
2053 const struct dsdb_attribute *schema_attrib;
2054 const struct ldb_val *target_guid;
2055 DATA_BLOB source_guid;
2056 TALLOC_CTX *frame = talloc_stackframe();
2059 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2061 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, frame, la->value.blob, &dn);
2062 if (!W_ERROR_IS_OK(werr)) {
2063 DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2068 /* Extract the target GUID in NDR form */
2069 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2070 if (target_guid == NULL
2071 || target_guid->length != sizeof(guid_array[0].target_guid)) {
2072 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2074 /* Repack the source GUID as NDR for sorting */
2075 status = GUID_to_ndr_blob(&la->identifier->guid,
2080 if (!NT_STATUS_IS_OK(status)
2081 || source_guid.length != sizeof(guid_array[0].source_guid)) {
2082 DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2084 return ntstatus_to_werror(status);
2087 guid_array[j].link = &la_list[j];
2088 memcpy(guid_array[j].target_guid, target_guid->data,
2089 sizeof(guid_array[j].target_guid));
2090 memcpy(guid_array[j].source_guid, source_guid.data,
2091 sizeof(guid_array[j].source_guid));
2095 LDB_TYPESAFE_QSORT(guid_array, link_count, NULL, linked_attribute_compare);
2097 *ret_array = guid_array;
2104 * Adds any ancestor/parent objects of the child_obj specified.
2105 * This is needed when the GET_ANC flag is specified in the request.
2106 * @param new_objs if parents are added, this gets updated to point to a chain
2107 * of parent objects (with the parents first and the child last)
2109 static WERROR getncchanges_add_ancestors(struct drsuapi_DsReplicaObjectListItemEx *child_obj,
2110 struct ldb_dn *child_dn,
2111 TALLOC_CTX *mem_ctx,
2112 struct ldb_context *sam_ctx,
2113 struct drsuapi_getncchanges_state *getnc_state,
2114 struct dsdb_schema *schema,
2115 DATA_BLOB *session_key,
2116 struct drsuapi_DsGetNCChangesRequest10 *req10,
2117 uint32_t *local_pas,
2118 struct ldb_dn *machine_dn,
2119 struct drsuapi_DsReplicaObjectListItemEx **new_objs)
2122 const struct GUID *next_anc_guid = NULL;
2123 WERROR werr = WERR_OK;
2124 static const char * const msg_attrs[] = {
2126 "nTSecurityDescriptor",
2128 "replPropertyMetaData",
2129 DSDB_SECRET_ATTRIBUTES,
2132 next_anc_guid = child_obj->parent_object_guid;
2134 while (next_anc_guid != NULL) {
2135 struct drsuapi_DsReplicaObjectListItemEx *anc_obj = NULL;
2136 struct ldb_message *anc_msg = NULL;
2137 struct ldb_result *anc_res = NULL;
2138 struct ldb_dn *anc_dn = NULL;
2141 * Don't send an object twice. (If we've sent the object, then
2142 * we've also sent all its parents as well)
2144 werr = dcesrv_drsuapi_obj_cache_exists(getnc_state->obj_cache,
2146 if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
2149 if (W_ERROR_IS_OK(werr)) {
2150 return WERR_INTERNAL_ERROR;
2152 if (!W_ERROR_EQUAL(werr, WERR_OBJECT_NOT_FOUND)) {
2156 anc_obj = talloc_zero(mem_ctx,
2157 struct drsuapi_DsReplicaObjectListItemEx);
2158 if (anc_obj == NULL) {
2159 return WERR_NOT_ENOUGH_MEMORY;
2162 anc_dn = ldb_dn_new_fmt(anc_obj, sam_ctx, "<GUID=%s>",
2163 GUID_string(anc_obj, next_anc_guid));
2164 if (anc_dn == NULL) {
2165 return WERR_NOT_ENOUGH_MEMORY;
2168 ret = drsuapi_search_with_extended_dn(sam_ctx, anc_obj,
2172 if (ret != LDB_SUCCESS) {
2173 const char *anc_str = NULL;
2174 const char *obj_str = NULL;
2176 anc_str = ldb_dn_get_extended_linearized(anc_obj,
2179 obj_str = ldb_dn_get_extended_linearized(anc_obj,
2183 DBG_ERR("getncchanges: failed to fetch ANC "
2184 "DN %s for DN %s - %s\n",
2185 anc_str, obj_str, ldb_errstring(sam_ctx));
2186 return WERR_DS_DRA_INCONSISTENT_DIT;
2189 anc_msg = anc_res->msgs[0];
2191 werr = get_nc_changes_build_object(anc_obj, anc_msg,
2193 getnc_state->ncRoot_dn,
2194 getnc_state->is_schema_nc,
2195 schema, session_key,
2196 getnc_state->min_usn,
2197 req10->replica_flags,
2198 req10->partial_attribute_set,
2199 req10->uptodateness_vector,
2201 false, /* force_object_return */
2205 if (!W_ERROR_IS_OK(werr)) {
2210 * Regardless of whether we actually use it or not,
2211 * we add it to the cache so we don't look at it again
2213 werr = dcesrv_drsuapi_obj_cache_add(getnc_state->obj_cache,
2215 if (!W_ERROR_IS_OK(werr)) {
2220 * Any ancestors which are below the highwatermark
2221 * or uptodateness_vector shouldn't be added,
2222 * but we still look further up the
2223 * tree for ones which have been changed recently.
2225 if (anc_obj->meta_data_ctr != NULL) {
2228 * prepend the parent to the list so that the client-side
2229 * adds the parent object before it adds the children
2231 anc_obj->next_object = *new_objs;
2232 *new_objs = anc_obj;
2236 TALLOC_FREE(anc_res);
2237 TALLOC_FREE(anc_dn);
2240 * We may need to resolve more parents...
2242 next_anc_guid = anc_obj->parent_object_guid;
2248 * Adds a list of new objects into the getNCChanges response message
2250 static void getncchanges_add_objs_to_resp(struct getncchanges_repl_chunk *repl_chunk,
2251 struct drsuapi_DsReplicaObjectListItemEx *obj_list)
2253 struct drsuapi_DsReplicaObjectListItemEx *obj;
2256 * We track the last object added to the response message, so just add
2257 * the new object-list onto the end
2259 if (repl_chunk->last_object == NULL) {
2260 repl_chunk->ctr6->first_object = obj_list;
2262 repl_chunk->last_object->next_object = obj_list;
2265 for (obj = obj_list; obj != NULL; obj = obj->next_object) {
2266 repl_chunk->ctr6->object_count += 1;
2269 * Remember the last object in the response - we'll use this to
2270 * link the next object(s) processed onto the existing list
2272 if (obj->next_object == NULL) {
2273 repl_chunk->last_object = obj;
2279 drsuapi_DsGetNCChanges
2281 see MS-DRSR 4.1.10.5.2 for basic logic of this function
2283 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2284 struct drsuapi_DsGetNCChanges *r)
2286 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
2289 struct dsdb_schema *schema;
2290 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
2291 struct getncchanges_repl_chunk repl_chunk = { 0 };
2293 DATA_BLOB session_key;
2295 struct dcesrv_handle *h;
2296 struct drsuapi_bind_state *b_state;
2297 struct drsuapi_getncchanges_state *getnc_state;
2298 struct drsuapi_DsGetNCChangesRequest10 *req10;
2300 uint32_t max_objects;
2302 uint32_t link_count = 0;
2303 uint32_t link_total = 0;
2304 uint32_t link_given = 0;
2305 struct ldb_dn *search_dn = NULL;
2307 enum security_user_level security_level;
2308 struct ldb_context *sam_ctx;
2309 struct dom_sid *user_sid;
2310 bool is_secret_request;
2311 bool is_gc_pas_request;
2312 struct drsuapi_changed_objects *changes;
2314 time_t start = time(NULL);
2315 bool max_wait_reached = false;
2316 bool has_get_all_changes = false;
2317 struct GUID invocation_id;
2318 static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
2319 struct dsdb_schema_prefixmap *pfm_remote = NULL;
2321 uint32_t *local_pas = NULL;
2322 struct ldb_dn *machine_dn = NULL; /* Only used for REPL SECRET EXOP */
2324 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
2327 /* sam_ctx_system is not present for non-administrator users */
2328 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
2330 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2332 *r->out.level_out = 6;
2333 /* TODO: linked attributes*/
2334 r->out.ctr->ctr6.linked_attributes_count = 0;
2335 r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
2337 r->out.ctr->ctr6.object_count = 0;
2338 r->out.ctr->ctr6.nc_object_count = 0;
2339 r->out.ctr->ctr6.more_data = false;
2340 r->out.ctr->ctr6.uptodateness_vector = NULL;
2341 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2342 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2343 r->out.ctr->ctr6.first_object = NULL;
2345 /* a RODC doesn't allow for any replication */
2346 ret = samdb_rodc(sam_ctx, &am_rodc);
2347 if (ret == LDB_SUCCESS && am_rodc) {
2348 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
2349 return WERR_DS_DRA_SOURCE_DISABLED;
2352 /* Check request revision.
2354 switch (r->in.level) {
2356 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
2357 if (req10 == NULL) {
2358 return WERR_NOT_ENOUGH_MEMORY;
2362 req10 = &r->in.req->req10;
2365 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
2367 return WERR_REVISION_MISMATCH;
2371 /* Perform access checks. */
2372 /* TODO: we need to support a sync on a specific non-root
2373 * DN. We'll need to find the real partition root here */
2374 ncRoot = req10->naming_context;
2375 if (ncRoot == NULL) {
2376 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
2377 return WERR_DS_DRA_INVALID_PARAMETER;
2380 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
2381 return WERR_DS_DRA_INTERNAL_ERROR;
2384 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
2385 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
2386 return WERR_DS_DRA_SOURCE_DISABLED;
2389 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
2391 /* all clients must have GUID_DRS_GET_CHANGES */
2392 werr = drs_security_access_check_nc_root(sam_ctx,
2394 dce_call->conn->auth_state.session_info->security_token,
2395 req10->naming_context,
2396 GUID_DRS_GET_CHANGES);
2397 if (!W_ERROR_IS_OK(werr)) {
2401 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
2402 full = req10->partial_attribute_set == NULL &&
2403 req10->partial_attribute_set_ex == NULL;
2405 full = (options & DRSUAPI_DRS_WRIT_REP) != 0;
2408 werr = dsdb_schema_pfm_from_drsuapi_pfm(&req10->mapping_ctr, true,
2409 mem_ctx, &pfm_remote, NULL);
2411 /* We were supplied a partial attribute set, without the prefix map! */
2412 if (!full && !W_ERROR_IS_OK(werr)) {
2413 if (req10->mapping_ctr.num_mappings == 0) {
2415 * Despite the fact MS-DRSR specifies that this shouldn't
2416 * happen, Windows RODCs will in fact not provide a prefixMap.
2418 DEBUG(5,(__location__ ": Failed to provide a remote prefixMap,"
2419 " falling back to local prefixMap\n"));
2421 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
2427 /* allowed if the GC PAS and client has
2428 GUID_DRS_GET_FILTERED_ATTRIBUTES */
2429 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, pfm_remote, &is_gc_pas_request);
2430 if (!W_ERROR_IS_OK(werr)) {
2433 if (is_gc_pas_request) {
2434 werr = drs_security_access_check_nc_root(sam_ctx,
2436 dce_call->conn->auth_state.session_info->security_token,
2437 req10->naming_context,
2438 GUID_DRS_GET_FILTERED_ATTRIBUTES);
2439 if (W_ERROR_IS_OK(werr)) {
2444 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10,
2446 &is_secret_request);
2447 if (!W_ERROR_IS_OK(werr)) {
2450 if (is_secret_request) {
2451 werr = drs_security_access_check_nc_root(sam_ctx,
2453 dce_call->conn->auth_state.session_info->security_token,
2454 req10->naming_context,
2455 GUID_DRS_GET_ALL_CHANGES);
2456 if (!W_ERROR_IS_OK(werr)) {
2457 /* Only bail if this is not a EXOP_REPL_SECRET */
2458 if (req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
2462 has_get_all_changes = true;
2467 /* for non-administrator replications, check that they have
2468 given the correct source_dsa_invocation_id */
2469 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
2470 samdb_domain_sid(sam_ctx));
2471 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
2472 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
2473 /* we rely on this flag being unset for RODC requests */
2474 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2478 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
2479 /* Ignore the _in_ uptpdateness vector*/
2480 req10->uptodateness_vector = NULL;
2483 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
2484 req10->source_dsa_invocation_id = invocation_id;
2487 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
2489 * The given highwatermark is only valid relative to the
2490 * specified source_dsa_invocation_id.
2492 ZERO_STRUCT(req10->highwatermark);
2495 getnc_state = b_state->getncchanges_state;
2497 /* see if a previous replication has been abandoned */
2499 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
2500 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
2501 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
2502 ldb_dn_get_linearized(new_dn),
2503 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
2504 ldb_dn_get_linearized(getnc_state->last_dn)));
2505 TALLOC_FREE(getnc_state);
2506 b_state->getncchanges_state = NULL;
2511 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
2512 &req10->highwatermark);
2514 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
2515 "on DN %s %s highwatermark (last_dn %s)\n",
2516 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
2517 (ret > 0) ? "older" : "newer",
2518 ldb_dn_get_linearized(getnc_state->last_dn)));
2519 TALLOC_FREE(getnc_state);
2520 b_state->getncchanges_state = NULL;
2524 if (getnc_state == NULL) {
2525 struct ldb_result *res = NULL;
2526 const char *attrs[] = {
2531 uint32_t nc_instanceType;
2532 struct ldb_dn *ncRoot_dn;
2534 ncRoot_dn = drs_ObjectIdentifier_to_dn(mem_ctx, sam_ctx, ncRoot);
2535 if (ncRoot_dn == NULL) {
2536 return WERR_NOT_ENOUGH_MEMORY;
2539 ret = dsdb_search_dn(sam_ctx, mem_ctx, &res,
2541 DSDB_SEARCH_SHOW_DELETED |
2542 DSDB_SEARCH_SHOW_RECYCLED);
2543 if (ret != LDB_SUCCESS) {
2544 DBG_WARNING("Failed to find ncRoot_dn %s\n",
2545 ldb_dn_get_linearized(ncRoot_dn));
2546 return WERR_DS_DRA_BAD_DN;
2548 nc_instanceType = ldb_msg_find_attr_as_int(res->msgs[0],
2552 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2553 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
2557 * This is the first replication cycle and it is
2558 * a good place to handle extended operations
2560 * FIXME: we don't fully support extended operations yet
2562 switch (req10->extended_op) {
2563 case DRSUAPI_EXOP_NONE:
2564 if ((nc_instanceType & INSTANCE_TYPE_IS_NC_HEAD) == 0) {
2566 = ldb_dn_get_linearized(ncRoot_dn);
2568 DBG_NOTICE("Rejecting full replication on "
2569 "not NC %s", dn_str);
2571 return WERR_DS_CANT_FIND_EXPECTED_NC;
2575 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
2576 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
2577 W_ERROR_NOT_OK_RETURN(werr);
2578 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2582 case DRSUAPI_EXOP_REPL_SECRET:
2583 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
2586 has_get_all_changes,
2588 r->out.result = werr;
2589 W_ERROR_NOT_OK_RETURN(werr);
2591 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
2592 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2593 W_ERROR_NOT_OK_RETURN(werr);
2594 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2598 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
2599 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2600 W_ERROR_NOT_OK_RETURN(werr);
2601 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2605 case DRSUAPI_EXOP_FSMO_REQ_PDC:
2606 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2607 W_ERROR_NOT_OK_RETURN(werr);
2608 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2612 case DRSUAPI_EXOP_REPL_OBJ:
2613 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
2614 r->out.result = werr;
2615 W_ERROR_NOT_OK_RETURN(werr);
2618 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
2620 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
2621 (unsigned)req10->extended_op));
2622 return WERR_DS_DRA_NOT_SUPPORTED;
2625 /* Initialize the state we'll store over the replication cycle */
2626 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
2627 if (getnc_state == NULL) {
2628 return WERR_NOT_ENOUGH_MEMORY;
2630 b_state->getncchanges_state = getnc_state;
2632 getnc_state->ncRoot_dn = ncRoot_dn;
2633 talloc_steal(getnc_state, ncRoot_dn);
2635 getnc_state->ncRoot_guid = samdb_result_guid(res->msgs[0],
2637 ncRoot->guid = getnc_state->ncRoot_guid;
2639 /* find out if we are to replicate Schema NC */
2640 ret = ldb_dn_compare_base(ldb_get_schema_basedn(sam_ctx),
2642 getnc_state->is_schema_nc = (0 == ret);
2647 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
2648 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
2649 DEBUG(0,(__location__ ": Bad DN '%s'\n",
2650 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
2651 return WERR_DS_DRA_INVALID_PARAMETER;
2654 ncRoot->guid = getnc_state->ncRoot_guid;
2656 /* we need the session key for encrypting password attributes */
2657 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
2658 if (!NT_STATUS_IS_OK(status)) {
2659 DEBUG(0,(__location__ ": Failed to get session key\n"));
2660 return WERR_DS_DRA_INTERNAL_ERROR;
2664 TODO: MS-DRSR section 4.1.10.1.1
2665 Work out if this is the start of a new cycle */
2667 if (getnc_state->guids == NULL) {
2668 const char *extra_filter;
2669 struct ldb_result *search_res = NULL;
2670 static const struct drsuapi_DsReplicaCursorCtrEx empty_udv;
2671 const struct drsuapi_DsReplicaCursorCtrEx *udv = NULL;
2673 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
2675 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2676 if (req10->uptodateness_vector != NULL) {
2677 udv = req10->uptodateness_vector;
2682 getnc_state->min_usn = req10->highwatermark.highest_usn;
2683 for (i = 0; i < udv->count; i++) {
2685 const struct drsuapi_DsReplicaCursor *cur =
2688 match = GUID_equal(&invocation_id,
2689 &cur->source_dsa_invocation_id);
2693 if (cur->highest_usn > getnc_state->min_usn) {
2694 getnc_state->min_usn = cur->highest_usn;
2699 /* We do not want REPL_SECRETS or REPL_SINGLE to return empty-handed */
2701 getnc_state->min_usn = 0;
2704 getnc_state->max_usn = getnc_state->min_usn;
2706 getnc_state->final_udv = talloc_zero(getnc_state,
2707 struct drsuapi_DsReplicaCursor2CtrEx);
2708 if (getnc_state->final_udv == NULL) {
2709 return WERR_NOT_ENOUGH_MEMORY;
2711 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
2712 getnc_state->final_udv);
2713 if (!W_ERROR_IS_OK(werr)) {
2717 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2718 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
2719 search_dn, extra_filter,
2722 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
2724 search_dn, extra_filter,
2727 W_ERROR_NOT_OK_RETURN(werr);
2729 /* extract out the GUIDs list */
2730 getnc_state->num_records = search_res ? search_res->count : 0;
2731 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
2732 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
2734 changes = talloc_array(getnc_state,
2735 struct drsuapi_changed_objects,
2736 getnc_state->num_records);
2737 W_ERROR_HAVE_NO_MEMORY(changes);
2739 for (i=0; i<getnc_state->num_records; i++) {
2740 changes[i].dn = search_res->msgs[i]->dn;
2741 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
2742 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
2744 if (changes[i].usn > getnc_state->max_usn) {
2745 getnc_state->max_usn = changes[i].usn;
2749 /* RID_ALLOC returns 3 objects in a fixed order */
2750 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
2753 LDB_TYPESAFE_QSORT(changes,
2754 getnc_state->num_records,
2756 site_res_cmp_usn_order);
2759 for (i=0; i < getnc_state->num_records; i++) {
2760 getnc_state->guids[i] = changes[i].guid;
2761 if (GUID_all_zero(&getnc_state->guids[i])) {
2762 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
2763 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
2764 return WERR_DS_DRA_INTERNAL_ERROR;
2768 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
2769 getnc_state->final_hwm.reserved_usn = 0;
2770 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
2772 talloc_free(search_res);
2773 talloc_free(changes);
2775 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2777 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
2778 getnc_state->obj_cache = db_open_rbt(getnc_state);
2779 if (getnc_state->obj_cache == NULL) {
2780 return WERR_NOT_ENOUGH_MEMORY;
2782 getnc_state->is_get_anc = true;
2786 if (req10->uptodateness_vector) {
2787 /* make sure its sorted */
2788 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
2789 req10->uptodateness_vector->count,
2790 drsuapi_DsReplicaCursor_compare);
2793 /* Prefix mapping */
2794 schema = dsdb_get_schema(sam_ctx, mem_ctx);
2796 DEBUG(0,("No schema in sam_ctx\n"));
2797 return WERR_DS_DRA_INTERNAL_ERROR;
2800 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
2801 if (r->out.ctr->ctr6.naming_context == NULL) {
2802 return WERR_NOT_ENOUGH_MEMORY;
2804 *r->out.ctr->ctr6.naming_context = *ncRoot;
2806 /* find the SID if there is one */
2807 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2809 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2810 r->out.ctr->ctr6.mapping_ctr = *ctr;
2812 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2813 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2815 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2816 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2818 repl_chunk.ctr6 = &r->out.ctr->ctr6;
2819 repl_chunk.last_object = NULL;
2821 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2823 * The client control here only applies in normal replication, not extended
2824 * operations, which return a fixed set, even if the caller
2825 * sets max_object_count == 0
2827 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2828 /* use this to force single objects at a time, which is useful
2829 * for working out what object is giving problems
2831 if (req10->max_object_count < max_objects) {
2832 max_objects = req10->max_object_count;
2836 * TODO: work out how the maximum should be calculated
2838 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2841 * Maximum time that we can spend in a getncchanges
2842 * in order to avoid timeout of the other part.
2843 * 10 seconds by default.
2845 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2847 if (req10->partial_attribute_set != NULL) {
2848 struct dsdb_syntax_ctx syntax_ctx;
2851 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
2852 syntax_ctx.pfm_remote = pfm_remote;
2854 local_pas = talloc_array(b_state, uint32_t, req10->partial_attribute_set->num_attids);
2856 for (j = 0; j < req10->partial_attribute_set->num_attids; j++) {
2857 getncchanges_attid_remote_to_local(schema,
2859 req10->partial_attribute_set->attids[j],
2860 (enum drsuapi_DsAttributeId *)&local_pas[j],
2864 LDB_TYPESAFE_QSORT(local_pas,
2865 req10->partial_attribute_set->num_attids,
2870 for (i=getnc_state->num_processed;
2871 i<getnc_state->num_records &&
2872 (r->out.ctr->ctr6.object_count < max_objects)
2873 && !max_wait_reached;
2875 struct drsuapi_DsReplicaObjectListItemEx *new_objs = NULL;
2876 struct drsuapi_DsReplicaObjectListItemEx *obj;
2877 struct ldb_message *msg;
2878 static const char * const msg_attrs[] = {
2880 "nTSecurityDescriptor",
2882 "replPropertyMetaData",
2883 DSDB_SECRET_ATTRIBUTES,
2885 struct ldb_result *msg_res;
2886 struct ldb_dn *msg_dn;
2887 bool obj_already_sent = false;
2889 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2890 W_ERROR_HAVE_NO_MEMORY(obj);
2892 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2893 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2897 * by re-searching here we avoid having a lot of full
2898 * records in memory between calls to getncchanges.
2900 * We expect that we may get some objects that vanish
2901 * (tombstone expunge) between the first and second
2904 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2906 LDB_SCOPE_BASE, msg_attrs, NULL);
2907 if (ret != LDB_SUCCESS) {
2908 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2909 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2910 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2916 if (msg_res->count == 0) {
2917 DEBUG(1,("getncchanges: got LDB_SUCCESS but failed"
2918 "to get any results in fetch of DN "
2919 "%s (race with tombstone expunge?)\n",
2920 ldb_dn_get_extended_linearized(obj,
2926 msg = msg_res->msgs[0];
2929 * Check if we've already sent the object as an ancestor of
2930 * another object. If so, we don't need to send it again
2932 if (getnc_state->obj_cache != NULL) {
2933 werr = dcesrv_drsuapi_obj_cache_exists(getnc_state->obj_cache,
2934 &getnc_state->guids[i]);
2935 if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
2936 obj_already_sent = true;
2940 if (!obj_already_sent) {
2941 max_wait_reached = (time(NULL) - start > max_wait);
2943 werr = get_nc_changes_build_object(obj, msg,
2944 sam_ctx, getnc_state->ncRoot_dn,
2945 getnc_state->is_schema_nc,
2946 schema, &session_key, getnc_state->min_usn,
2947 req10->replica_flags,
2948 req10->partial_attribute_set,
2949 req10->uptodateness_vector,
2952 local_pas, machine_dn,
2953 &getnc_state->guids[i]);
2954 if (!W_ERROR_IS_OK(werr)) {
2960 * We've reached the USN where this object naturally occurs.
2961 * Regardless of whether we've already sent the object (as an
2962 * ancestor), we add its links and update the HWM at this point
2964 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2965 getnc_state->ncRoot_dn,
2966 getnc_state->is_schema_nc,
2967 schema, getnc_state->min_usn,
2968 req10->replica_flags,
2970 &getnc_state->la_list,
2971 &getnc_state->la_count,
2972 req10->uptodateness_vector);
2973 if (!W_ERROR_IS_OK(werr)) {
2977 dcesrv_drsuapi_update_highwatermark(msg,
2978 getnc_state->max_usn,
2979 &r->out.ctr->ctr6.new_highwatermark);
2981 if (obj_already_sent || obj->meta_data_ctr == NULL) {
2982 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2983 ldb_dn_get_linearized(msg->dn)));
2984 /* nothing to send */
2991 if (getnc_state->obj_cache != NULL) {
2992 werr = dcesrv_drsuapi_obj_cache_add(getnc_state->obj_cache,
2993 &getnc_state->guids[i]);
2994 if (!W_ERROR_IS_OK(werr)) {
3000 * For GET_ANC, prepend any parents that the client needs
3001 * to know about before it can add this object
3003 if (getnc_state->is_get_anc) {
3004 werr = getncchanges_add_ancestors(obj, msg->dn, mem_ctx,
3005 sam_ctx, getnc_state,
3006 schema, &session_key,
3010 if (!W_ERROR_IS_OK(werr)) {
3016 * Add the object (and any parents it might have) into the
3019 getncchanges_add_objs_to_resp(&repl_chunk, new_objs);
3021 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
3023 talloc_free(getnc_state->last_dn);
3024 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
3026 talloc_free(msg_res);
3027 talloc_free(msg_dn);
3030 getnc_state->num_processed = i;
3032 /* the client can us to call UpdateRefs on its behalf to
3033 re-establish monitoring of the NC */
3034 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
3035 !GUID_all_zero(&req10->destination_dsa_guid)) {
3036 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
3037 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
3038 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
3039 ureq.naming_context = ncRoot;
3040 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(sam_ctx, mem_ctx,
3041 &req10->destination_dsa_guid);
3042 if (!ureq.dest_dsa_dns_name) {
3043 return WERR_NOT_ENOUGH_MEMORY;
3045 ureq.dest_dsa_guid = req10->destination_dsa_guid;
3046 ureq.options = DRSUAPI_DRS_ADD_REF |
3047 DRSUAPI_DRS_ASYNC_OP |
3048 DRSUAPI_DRS_GETCHG_CHECK;
3050 /* we also need to pass through the
3051 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
3052 to send notifies using the GC SPN */
3053 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
3055 werr = drsuapi_UpdateRefs(dce_call->msg_ctx,
3056 dce_call->event_ctx, b_state,
3058 if (!W_ERROR_IS_OK(werr)) {
3059 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
3060 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
3067 * This is just a guess, how to calculate the
3068 * number of linked attributes to send, we need to
3069 * find out how to do this right.
3071 if (r->out.ctr->ctr6.object_count >= max_links) {
3074 max_links -= r->out.ctr->ctr6.object_count;
3077 link_total = getnc_state->la_count;
3079 if (i < getnc_state->num_records) {
3080 r->out.ctr->ctr6.more_data = true;
3082 /* sort the whole array the first time */
3083 if (getnc_state->la_sorted == NULL) {
3084 werr = getncchanges_get_sorted_array(getnc_state->la_list,
3085 getnc_state->la_count,
3086 sam_ctx, getnc_state,
3088 &getnc_state->la_sorted);
3089 if (!W_ERROR_IS_OK(werr)) {
3094 link_count = getnc_state->la_count - getnc_state->la_idx;
3095 link_count = MIN(max_links, link_count);
3097 r->out.ctr->ctr6.linked_attributes_count = link_count;
3098 r->out.ctr->ctr6.linked_attributes = talloc_array(r->out.ctr, struct drsuapi_DsReplicaLinkedAttribute, link_count);
3099 if (r->out.ctr->ctr6.linked_attributes == NULL) {
3100 DEBUG(0, ("Out of memory allocating %u linked attributes for output", link_count));
3101 return WERR_NOT_ENOUGH_MEMORY;
3104 for (k = 0; k < link_count; k++) {
3105 r->out.ctr->ctr6.linked_attributes[k]
3106 = *getnc_state->la_sorted[getnc_state->la_idx + k].link;
3109 getnc_state->la_idx += link_count;
3110 link_given = getnc_state->la_idx;
3112 if (getnc_state->la_idx < getnc_state->la_count) {
3113 r->out.ctr->ctr6.more_data = true;
3117 if (req10->replica_flags & DRSUAPI_DRS_GET_NC_SIZE) {
3119 * TODO: This implementation is wrong
3120 * we should find out the total number of
3121 * objects and links in the whole naming context
3122 * at the start of the cycle and return these
3123 * values in each message.
3125 * For now we keep our current strategy and return
3126 * the number of objects for this cycle and the number
3127 * of links we found so far during the cycle.
3129 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
3130 r->out.ctr->ctr6.nc_linked_attributes_count = getnc_state->la_count;
3133 if (!r->out.ctr->ctr6.more_data) {
3134 talloc_steal(mem_ctx, getnc_state->la_list);
3136 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
3137 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
3138 &getnc_state->final_udv);
3140 talloc_free(getnc_state);
3141 b_state->getncchanges_state = NULL;
3143 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
3144 &r->out.ctr->ctr6.new_highwatermark);
3147 * We need to make sure that we never return the
3148 * same highwatermark within the same replication
3149 * cycle more than once. Otherwise we cannot detect
3150 * when the client uses an unexptected highwatermark.
3152 * This is a HACK which is needed because our
3153 * object ordering is wrong and set tmp_highest_usn
3154 * to a value that is higher than what we already
3155 * sent to the client (destination dsa).
3157 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
3160 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
3163 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
3164 r->out.ctr->ctr6.uptodateness_vector = NULL;
3165 r->out.ctr->ctr6.nc_object_count = 0;
3166 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
3169 DEBUG(r->out.ctr->ctr6.more_data?4:2,
3170 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
3171 (unsigned long long)(req10->highwatermark.highest_usn+1),
3172 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
3173 r->out.ctr->ctr6.object_count,
3174 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
3175 r->out.ctr->ctr6.linked_attributes_count,
3176 link_given, link_total,
3177 dom_sid_string(mem_ctx, user_sid)));
3180 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
3181 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);