s4:torture/smb2: add 'smb2.bench.session-setup'
[Samba.git] / source4 / rpc_server / drsuapi / getncchanges.c
blob0ae6aa510cff05292b03551b3930126300873666
1 /*
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/>.
24 #include "includes.h"
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 "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
39 #include "lib/dbwrap/dbwrap.h"
40 #include "lib/dbwrap/dbwrap_rbt.h"
41 #include "librpc/gen_ndr/ndr_misc.h"
43 #undef DBGC_CLASS
44 #define DBGC_CLASS DBGC_DRS_REPL
46 #define DRS_GUID_SIZE 16
47 #define DEFAULT_MAX_OBJECTS 1000
48 #define DEFAULT_MAX_LINKS 1500
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;
56 struct GUID *guids;
57 uint32_t num_records;
58 uint32_t num_processed;
59 struct ldb_dn *ncRoot_dn;
60 struct GUID ncRoot_guid;
61 bool is_schema_nc;
62 bool is_get_anc;
63 bool broken_samba_4_5_get_anc_emulation;
64 bool is_get_tgt;
65 bool send_nc_root_first;
66 uint64_t min_usn;
67 uint64_t max_usn;
68 struct drsuapi_DsReplicaHighWaterMark last_hwm;
69 struct ldb_dn *last_dn;
70 struct drsuapi_DsReplicaHighWaterMark final_hwm;
71 struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
72 struct drsuapi_DsReplicaLinkedAttribute *la_list;
73 uint32_t la_count;
74 uint32_t la_idx;
76 /* these are just used for debugging the replication's progress */
77 uint32_t links_given;
78 uint32_t total_links;
81 /* We must keep the GUIDs in NDR form for sorting */
82 struct la_for_sorting {
83 const struct drsuapi_DsReplicaLinkedAttribute *link;
84 uint8_t target_guid[DRS_GUID_SIZE];
85 uint8_t source_guid[DRS_GUID_SIZE];
89 * stores the state for a chunk of replication data. This state information
90 * only exists for a single call to dcesrv_drsuapi_DsGetNCChanges()
92 struct getncchanges_repl_chunk {
93 uint32_t max_objects;
94 uint32_t max_links;
95 uint32_t tgt_la_count;
96 bool immediate_link_sync;
97 time_t max_wait;
98 time_t start;
100 /* stores the objects to be sent in this chunk */
101 uint32_t object_count;
102 struct drsuapi_DsReplicaObjectListItemEx *object_list;
104 /* the last object added to this replication chunk */
105 struct drsuapi_DsReplicaObjectListItemEx *last_object;
108 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
109 const struct drsuapi_DsReplicaHighWaterMark *h2)
111 if (h1->highest_usn < h2->highest_usn) {
112 return -1;
113 } else if (h1->highest_usn > h2->highest_usn) {
114 return 1;
115 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
116 return -1;
117 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
118 return 1;
119 } else if (h1->reserved_usn < h2->reserved_usn) {
120 return -1;
121 } else if (h1->reserved_usn > h2->reserved_usn) {
122 return 1;
125 return 0;
129 build a DsReplicaObjectIdentifier from a ldb msg
131 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
132 const struct ldb_message *msg)
134 struct drsuapi_DsReplicaObjectIdentifier *identifier;
135 struct dom_sid *sid;
137 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
138 if (identifier == NULL) {
139 return NULL;
142 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
143 identifier->guid = samdb_result_guid(msg, "objectGUID");
145 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
146 if (sid) {
147 identifier->sid = *sid;
148 } else {
149 ZERO_STRUCT(identifier->sid);
151 return identifier;
154 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
156 return GUID_compare(guid1, &guid2);
160 see if we can filter an attribute using the uptodateness_vector
162 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
163 const struct GUID *originating_invocation_id,
164 uint64_t originating_usn)
166 const struct drsuapi_DsReplicaCursor *c;
167 if (udv == NULL) return false;
168 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
169 originating_invocation_id, udv_compare, c);
170 if (c && originating_usn <= c->highest_usn) {
171 return true;
173 return false;
176 static int uint32_t_cmp(uint32_t a1, uint32_t a2)
178 if (a1 == a2) return 0;
179 return a1 > a2 ? 1 : -1;
182 static int uint32_t_ptr_cmp(uint32_t *a1, uint32_t *a2)
184 if (*a1 == *a2) return 0;
185 return *a1 > *a2 ? 1 : -1;
188 static WERROR getncchanges_attid_remote_to_local(const struct dsdb_schema *schema,
189 const struct dsdb_syntax_ctx *ctx,
190 enum drsuapi_DsAttributeId remote_attid_as_enum,
191 enum drsuapi_DsAttributeId *local_attid_as_enum,
192 const struct dsdb_attribute **_sa)
194 WERROR werr;
195 const struct dsdb_attribute *sa = NULL;
197 if (ctx->pfm_remote == NULL) {
198 DEBUG(7, ("No prefixMap supplied, falling back to local prefixMap.\n"));
199 goto fail;
202 werr = dsdb_attribute_drsuapi_remote_to_local(ctx,
203 remote_attid_as_enum,
204 local_attid_as_enum,
205 _sa);
206 if (!W_ERROR_IS_OK(werr)) {
207 DEBUG(3, ("WARNING: Unable to resolve remote attid, falling back to local prefixMap.\n"));
208 goto fail;
211 return werr;
212 fail:
214 sa = dsdb_attribute_by_attributeID_id(schema, remote_attid_as_enum);
215 if (sa == NULL) {
216 return WERR_DS_DRA_SCHEMA_MISMATCH;
217 } else {
218 if (local_attid_as_enum != NULL) {
219 *local_attid_as_enum = sa->attributeID_id;
221 if (_sa != NULL) {
222 *_sa = sa;
224 return WERR_OK;
228 static WERROR getncchanges_update_revealed_list(struct ldb_context *sam_ctx,
229 TALLOC_CTX *mem_ctx,
230 struct ldb_message **msg,
231 struct ldb_dn *object_dn,
232 const struct GUID *object_guid,
233 const struct dsdb_attribute *sa,
234 struct replPropertyMetaData1 *meta_data,
235 struct ldb_message *revealed_users)
237 enum ndr_err_code ndr_err;
238 int ldb_err;
239 char *attr_str = NULL;
240 char *attr_hex = NULL;
241 DATA_BLOB attr_blob;
242 struct ldb_message_element *existing = NULL, *el_add = NULL, *el_del = NULL;
243 const char * const * secret_attributes = ldb_get_opaque(sam_ctx, "LDB_SECRET_ATTRIBUTE_LIST");
245 if (!ldb_attr_in_list(secret_attributes,
246 sa->lDAPDisplayName)) {
247 return WERR_OK;
251 ndr_err = ndr_push_struct_blob(&attr_blob, mem_ctx, meta_data, (ndr_push_flags_fn_t)ndr_push_replPropertyMetaData1);
252 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
253 return WERR_DS_DRA_INTERNAL_ERROR;
256 attr_hex = hex_encode_talloc(mem_ctx, attr_blob.data, attr_blob.length);
257 if (attr_hex == NULL) {
258 return WERR_NOT_ENOUGH_MEMORY;
261 attr_str = talloc_asprintf(mem_ctx, "B:%zd:%s:%s", attr_blob.length*2, attr_hex, ldb_dn_get_linearized(object_dn));
262 if (attr_str == NULL) {
263 return WERR_NOT_ENOUGH_MEMORY;
266 existing = ldb_msg_find_element(revealed_users, "msDS-RevealedUsers");
267 if (existing != NULL) {
268 /* Replace the old value (if one exists) with the current one */
269 struct parsed_dn *link_dns;
270 struct parsed_dn *exact = NULL, *unused = NULL;
271 uint8_t attid[4];
272 DATA_BLOB partial_meta;
274 ldb_err = get_parsed_dns_trusted(mem_ctx, existing, &link_dns);
275 if (ldb_err != LDB_SUCCESS) {
276 return WERR_DS_DRA_INTERNAL_ERROR;
279 /* Construct a partial metadata blob to match on in the DB */
280 SIVAL(attid, 0, sa->attributeID_id);
281 partial_meta.length = 4;
282 partial_meta.data = attid;
284 /* Binary search using GUID and attribute id for uniqueness */
285 ldb_err = parsed_dn_find(sam_ctx, link_dns, existing->num_values,
286 object_guid, object_dn,
287 partial_meta, 4,
288 &exact, &unused,
289 DSDB_SYNTAX_BINARY_DN, true);
291 if (ldb_err != LDB_SUCCESS) {
292 DEBUG(0,(__location__ ": Failed parsed DN find - %s\n",
293 ldb_errstring(sam_ctx)));
294 return WERR_DS_DRA_INTERNAL_ERROR;
297 if (exact != NULL) {
298 /* Perform some verification of the blob */
299 struct replPropertyMetaData1 existing_meta_data;
300 ndr_err = ndr_pull_struct_blob_all_noalloc(&exact->dsdb_dn->extra_part,
301 &existing_meta_data,
302 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaData1);
303 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
304 return WERR_DS_DRA_INTERNAL_ERROR;
307 if (existing_meta_data.attid == sa->attributeID_id) {
308 ldb_err = ldb_msg_add_empty(*msg, "msDS-RevealedUsers", LDB_FLAG_MOD_DELETE, &el_del);
309 if (ldb_err != LDB_SUCCESS) {
310 return WERR_DS_DRA_INTERNAL_ERROR;
313 el_del->values = talloc_array((*msg)->elements, struct ldb_val, 1);
314 if (el_del->values == NULL) {
315 return WERR_NOT_ENOUGH_MEMORY;
317 el_del->values[0] = *exact->v;
318 el_del->num_values = 1;
319 } else {
320 return WERR_DS_DRA_INTERNAL_ERROR;
325 ldb_err = ldb_msg_add_empty(*msg, "msDS-RevealedUsers", LDB_FLAG_MOD_ADD, &el_add);
326 if (ldb_err != LDB_SUCCESS) {
327 return WERR_DS_DRA_INTERNAL_ERROR;
330 el_add->values = talloc_array((*msg)->elements, struct ldb_val, 1);
331 if (el_add->values == NULL) {
332 return WERR_NOT_ENOUGH_MEMORY;
336 el_add->values[0] = data_blob_string_const(attr_str);
337 el_add->num_values = 1;
339 return WERR_OK;
343 * This function filter attributes for build_object based on the
344 * uptodatenessvector and partial attribute set.
346 * Any secret attributes are forced here for REPL_SECRET, and audited at this
347 * point with msDS-RevealedUsers.
349 static WERROR get_nc_changes_filter_attrs(struct drsuapi_DsReplicaObjectListItemEx *obj,
350 struct replPropertyMetaDataBlob md,
351 struct ldb_context *sam_ctx,
352 const struct ldb_message *msg,
353 const struct GUID *guid,
354 uint32_t *count,
355 uint64_t highest_usn,
356 const struct dsdb_attribute *rdn_sa,
357 struct dsdb_schema *schema,
358 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
359 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
360 uint32_t *local_pas,
361 uint32_t *attids,
362 bool exop_secret,
363 struct ldb_message **revealed_list_msg,
364 struct ldb_message *existing_revealed_list_msg)
366 uint32_t i, n;
367 WERROR werr;
368 for (n=i=0; i<md.ctr.ctr1.count; i++) {
369 const struct dsdb_attribute *sa;
370 bool force_attribute = false;
372 /* if the attribute has not changed, and it is not the
373 instanceType then don't include it */
374 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
375 !exop_secret &&
376 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
378 /* don't include the rDN */
379 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
381 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
382 if (!sa) {
383 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
384 (unsigned int)md.ctr.ctr1.array[i].attid,
385 ldb_dn_get_linearized(msg->dn)));
386 return WERR_DS_DRA_INTERNAL_ERROR;
389 if (sa->linkID) {
390 struct ldb_message_element *el;
391 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
392 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
393 /* don't send upgraded links inline */
394 continue;
398 if (exop_secret &&
399 !dsdb_attr_in_rodc_fas(sa)) {
400 force_attribute = true;
401 DEBUG(4,("Forcing attribute %s in %s\n",
402 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
403 werr = getncchanges_update_revealed_list(sam_ctx, obj,
404 revealed_list_msg,
405 msg->dn, guid, sa,
406 &md.ctr.ctr1.array[i],
407 existing_revealed_list_msg);
408 if (!W_ERROR_IS_OK(werr)) {
409 return werr;
413 /* filter by uptodateness_vector */
414 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
415 !force_attribute &&
416 udv_filter(uptodateness_vector,
417 &md.ctr.ctr1.array[i].originating_invocation_id,
418 md.ctr.ctr1.array[i].originating_usn)) {
419 continue;
422 /* filter by partial_attribute_set */
423 if (partial_attribute_set && !force_attribute) {
424 uint32_t *result = NULL;
425 BINARY_ARRAY_SEARCH_V(local_pas, partial_attribute_set->num_attids, sa->attributeID_id,
426 uint32_t_cmp, result);
427 if (result == NULL) {
428 continue;
432 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
433 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
434 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
435 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
436 attids[n] = md.ctr.ctr1.array[i].attid;
438 n++;
441 *count = n;
443 return WERR_OK;
447 drsuapi_DsGetNCChanges for one object
449 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
450 const struct ldb_message *msg,
451 struct ldb_context *sam_ctx,
452 struct drsuapi_getncchanges_state *getnc_state,
453 struct dsdb_schema *schema,
454 DATA_BLOB *session_key,
455 struct drsuapi_DsGetNCChangesRequest10 *req10,
456 bool force_object_return,
457 uint32_t *local_pas,
458 struct ldb_dn *machine_dn,
459 const struct GUID *guid)
461 const struct ldb_val *md_value;
462 uint32_t i, n;
463 struct replPropertyMetaDataBlob md;
464 uint32_t rid = 0;
465 int ldb_err;
466 enum ndr_err_code ndr_err;
467 uint32_t *attids;
468 const char *rdn;
469 const struct dsdb_attribute *rdn_sa;
470 uint64_t uSNChanged;
471 unsigned int instanceType;
472 struct dsdb_syntax_ctx syntax_ctx;
473 struct ldb_result *res = NULL;
474 WERROR werr;
475 int ret;
476 uint32_t replica_flags = req10->replica_flags;
477 struct drsuapi_DsPartialAttributeSet *partial_attribute_set =
478 req10->partial_attribute_set;
479 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector =
480 req10->uptodateness_vector;
481 enum drsuapi_DsExtendedOperation extended_op = req10->extended_op;
482 bool is_schema_nc = getnc_state->is_schema_nc;
483 uint64_t highest_usn = getnc_state->min_usn;
485 /* make dsdb syntax context for conversions */
486 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
487 syntax_ctx.is_schema_nc = is_schema_nc;
489 uSNChanged = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
490 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
491 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
492 obj->is_nc_prefix = true;
493 obj->parent_object_guid = NULL;
494 } else {
495 obj->is_nc_prefix = false;
496 obj->parent_object_guid = talloc(obj, struct GUID);
497 if (obj->parent_object_guid == NULL) {
498 return WERR_DS_DRA_INTERNAL_ERROR;
500 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
501 if (GUID_all_zero(obj->parent_object_guid)) {
502 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
503 ldb_dn_get_linearized(msg->dn)));
504 return WERR_DS_DRA_INTERNAL_ERROR;
507 obj->next_object = NULL;
509 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
510 if (!md_value) {
511 /* nothing to send */
512 return WERR_OK;
515 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
516 /* don't send uninstantiated objects */
517 return WERR_OK;
520 if (uSNChanged <= highest_usn) {
521 /* nothing to send */
522 return WERR_OK;
525 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
526 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
527 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
528 return WERR_DS_DRA_INTERNAL_ERROR;
531 if (md.version != 1) {
532 return WERR_DS_DRA_INTERNAL_ERROR;
535 rdn = ldb_dn_get_rdn_name(msg->dn);
536 if (rdn == NULL) {
537 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
538 return WERR_DS_DRA_INTERNAL_ERROR;
541 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
542 if (rdn_sa == NULL) {
543 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
544 rdn, ldb_dn_get_linearized(msg->dn)));
545 return WERR_DS_DRA_INTERNAL_ERROR;
548 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
549 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
551 obj->object.identifier = get_object_identifier(obj, msg);
552 if (obj->object.identifier == NULL) {
553 return WERR_NOT_ENOUGH_MEMORY;
555 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
557 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
559 if (extended_op == DRSUAPI_EXOP_REPL_SECRET) {
560 /* Get the existing revealed users for the destination */
561 struct ldb_message *revealed_list_msg = NULL;
562 struct ldb_message *existing_revealed_list_msg = NULL;
563 const char *machine_attrs[] = {
564 "msDS-RevealedUsers",
565 NULL
568 revealed_list_msg = ldb_msg_new(sam_ctx);
569 if (revealed_list_msg == NULL) {
570 return WERR_NOT_ENOUGH_MEMORY;
572 revealed_list_msg->dn = machine_dn;
574 ret = ldb_transaction_start(sam_ctx);
575 if (ret != LDB_SUCCESS) {
576 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
577 ldb_errstring(sam_ctx)));
578 return WERR_DS_DRA_INTERNAL_ERROR;
581 ldb_err = dsdb_search_dn(sam_ctx, obj, &res, machine_dn, machine_attrs, DSDB_SEARCH_SHOW_EXTENDED_DN);
582 if (ldb_err != LDB_SUCCESS || res->count != 1) {
583 ldb_transaction_cancel(sam_ctx);
584 return WERR_DS_DRA_INTERNAL_ERROR;
587 existing_revealed_list_msg = res->msgs[0];
589 werr = get_nc_changes_filter_attrs(obj, md, sam_ctx, msg,
590 guid, &n, highest_usn,
591 rdn_sa, schema,
592 uptodateness_vector,
593 partial_attribute_set, local_pas,
594 attids,
595 true,
596 &revealed_list_msg,
597 existing_revealed_list_msg);
598 if (!W_ERROR_IS_OK(werr)) {
599 ldb_transaction_cancel(sam_ctx);
600 return werr;
603 if (revealed_list_msg != NULL) {
604 ret = ldb_modify(sam_ctx, revealed_list_msg);
605 if (ret != LDB_SUCCESS) {
606 DEBUG(0,(__location__ ": Failed to alter revealed links - %s\n",
607 ldb_errstring(sam_ctx)));
608 ldb_transaction_cancel(sam_ctx);
609 return WERR_DS_DRA_INTERNAL_ERROR;
613 ret = ldb_transaction_commit(sam_ctx);
614 if (ret != LDB_SUCCESS) {
615 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
616 ldb_errstring(sam_ctx)));
617 return WERR_DS_DRA_INTERNAL_ERROR;
619 } else {
620 werr = get_nc_changes_filter_attrs(obj, md, sam_ctx, msg, guid,
621 &n, highest_usn, rdn_sa,
622 schema, uptodateness_vector,
623 partial_attribute_set, local_pas,
624 attids,
625 false,
626 NULL,
627 NULL);
628 if (!W_ERROR_IS_OK(werr)) {
629 return werr;
633 /* ignore it if its an empty change. Note that renames always
634 * change the 'name' attribute, so they won't be ignored by
635 * this
637 * the force_object_return check is used to force an empty
638 * object return when we timeout in the getncchanges loop.
639 * This allows us to return an empty object, which keeps the
640 * client happy while preventing timeouts
642 if (n == 0 ||
643 (n == 1 &&
644 attids[0] == DRSUAPI_ATTID_instanceType &&
645 !force_object_return)) {
646 talloc_free(obj->meta_data_ctr);
647 obj->meta_data_ctr = NULL;
648 return WERR_OK;
651 obj->meta_data_ctr->count = n;
653 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
654 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
655 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
656 obj->object.attribute_ctr.num_attributes);
657 if (obj->object.attribute_ctr.attributes == NULL) {
658 return WERR_NOT_ENOUGH_MEMORY;
662 * Note that the meta_data array and the attributes array must
663 * be the same size and in the same order
665 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
666 struct ldb_message_element *el;
667 const struct dsdb_attribute *sa;
669 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
670 if (!sa) {
671 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
672 return WERR_DS_DRA_INTERNAL_ERROR;
675 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
676 if (el == NULL) {
677 /* this happens for attributes that have been removed */
678 DEBUG(5,("No element '%s' for attributeID %u in message\n",
679 sa->lDAPDisplayName, attids[i]));
680 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
681 obj->object.attribute_ctr.attributes[i].attid =
682 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
683 } else {
684 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
685 &obj->object.attribute_ctr.attributes[i]);
686 if (!W_ERROR_IS_OK(werr)) {
687 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
688 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
689 win_errstr(werr)));
690 return werr;
692 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
693 * check if attribute is secret and send a null value
695 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
696 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
697 &obj->meta_data_ctr->meta_data[i]);
699 /* some attributes needs to be encrypted
700 before being sent */
701 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
702 &obj->object.attribute_ctr.attributes[i]);
703 if (!W_ERROR_IS_OK(werr)) {
704 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
705 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
706 win_errstr(werr)));
707 return werr;
710 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
711 DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID: "
712 "0x%08x vs 0x%08x "
713 "Run dbcheck!\n",
714 sa->lDAPDisplayName,
715 ldb_dn_get_linearized(msg->dn),
716 attids[i],
717 obj->object.attribute_ctr.attributes[i].attid));
718 return WERR_DS_DATABASE_ERROR;
722 return WERR_OK;
726 add one linked attribute from an object to the list of linked
727 attributes in a getncchanges request
729 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
730 struct ldb_context *sam_ctx,
731 const struct dsdb_schema *schema,
732 const struct dsdb_attribute *sa,
733 const struct ldb_message *msg,
734 struct dsdb_dn *dsdb_dn,
735 struct drsuapi_DsReplicaLinkedAttribute **la_list,
736 uint32_t *la_count,
737 bool is_schema_nc)
739 struct drsuapi_DsReplicaLinkedAttribute *la;
740 bool active;
741 NTSTATUS status;
742 WERROR werr;
744 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
745 W_ERROR_HAVE_NO_MEMORY(*la_list);
747 la = &(*la_list)[*la_count];
749 la->identifier = get_object_identifier(*la_list, msg);
750 W_ERROR_HAVE_NO_MEMORY(la->identifier);
752 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
754 if (!active) {
755 /* We have to check that the inactive link still point to an existing object */
756 struct GUID guid;
757 struct ldb_dn *tdn;
758 int ret;
759 const char *v;
761 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
762 if (strncmp(v, "TRUE", 4) == 0) {
764 * Note: we skip the transmission of the deleted link even if the other part used to
765 * know about it because when we transmit the deletion of the object, the link will
766 * be deleted too due to deletion of object where link points and Windows do so.
768 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
769 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
771 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
772 * if it join an existing domain with deleted objects, it firsts impose to have a
773 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
774 * either during initial replication or after the getNCChanges.
775 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
777 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
778 * If FL >=2K8R2 we are sure that this attribute will be here.
779 * For this kind of forest level we do not return the link if the object is recycled
780 * (isRecycled = true).
782 if (strncmp(v, "TRUE", 4) == 0) {
783 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
784 ldb_dn_get_linearized(msg->dn)));
785 return WERR_OK;
787 } else {
788 return WERR_OK;
791 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
792 if (!NT_STATUS_IS_OK(status)) {
793 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
794 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
795 return ntstatus_to_werror(status);
797 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
798 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
799 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
800 GUID_string(mem_ctx, &guid)));
801 return WERR_OK;
802 } else if (ret != LDB_SUCCESS) {
803 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
804 GUID_string(mem_ctx, &guid),
805 ret));
806 return WERR_OK;
809 la->attid = dsdb_attribute_get_attid(sa, is_schema_nc);
810 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
812 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
813 if (!NT_STATUS_IS_OK(status)) {
814 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
815 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
816 return ntstatus_to_werror(status);
818 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
819 if (!NT_STATUS_IS_OK(status)) {
820 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
821 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
822 return ntstatus_to_werror(status);
824 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
825 if (!NT_STATUS_IS_OK(status)) {
826 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
827 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
828 return ntstatus_to_werror(status);
830 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
831 if (!NT_STATUS_IS_OK(status)) {
832 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
833 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
834 return ntstatus_to_werror(status);
837 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
838 if (!NT_STATUS_IS_OK(status)) {
839 /* this is possible for upgraded links */
840 la->originating_add_time = la->meta_data.originating_change_time;
843 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
844 W_ERROR_NOT_OK_RETURN(werr);
846 (*la_count)++;
847 return WERR_OK;
852 add linked attributes from an object to the list of linked
853 attributes in a getncchanges request
855 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
856 TALLOC_CTX *mem_ctx,
857 bool is_schema_nc,
858 struct dsdb_schema *schema,
859 uint64_t highest_usn,
860 uint32_t replica_flags,
861 const struct ldb_message *msg,
862 struct drsuapi_DsReplicaLinkedAttribute **la_list,
863 uint32_t *la_count,
864 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
866 unsigned int i;
867 TALLOC_CTX *tmp_ctx = NULL;
868 uint64_t uSNChanged = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
869 bool is_critical = ldb_msg_find_attr_as_bool(msg, "isCriticalSystemObject", false);
871 if (replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
872 if (!is_critical) {
873 return WERR_OK;
877 if (uSNChanged <= highest_usn) {
878 return WERR_OK;
881 tmp_ctx = talloc_new(mem_ctx);
882 if (tmp_ctx == NULL) {
883 return WERR_NOT_ENOUGH_MEMORY;
886 for (i=0; i<msg->num_elements; i++) {
887 struct ldb_message_element *el = &msg->elements[i];
888 const struct dsdb_attribute *sa;
889 unsigned int j;
891 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
893 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
894 /* we only want forward links */
895 continue;
898 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
899 /* its an old style link, it will have been
900 * sent in the main replication data */
901 continue;
904 for (j=0; j<el->num_values; j++) {
905 struct dsdb_dn *dsdb_dn;
906 uint64_t local_usn;
907 uint64_t originating_usn;
908 NTSTATUS status, status2;
909 WERROR werr;
910 struct GUID originating_invocation_id;
912 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
913 if (dsdb_dn == NULL) {
914 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
915 el->name, ldb_dn_get_linearized(msg->dn)));
916 talloc_free(tmp_ctx);
917 return WERR_DS_DRA_INTERNAL_ERROR;
920 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
921 if (!NT_STATUS_IS_OK(status)) {
922 /* this can happen for attributes
923 given to us with old style meta
924 data */
925 continue;
928 if (local_usn > uSNChanged) {
929 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
930 el->name, ldb_dn_get_linearized(msg->dn)));
931 talloc_free(tmp_ctx);
932 return WERR_DS_DRA_INTERNAL_ERROR;
935 if (local_usn <= highest_usn) {
936 continue;
939 status = dsdb_get_extended_dn_guid(dsdb_dn->dn,
940 &originating_invocation_id,
941 "RMD_INVOCID");
942 status2 = dsdb_get_extended_dn_uint64(dsdb_dn->dn,
943 &originating_usn,
944 "RMD_ORIGINATING_USN");
946 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2)) {
947 if (udv_filter(uptodateness_vector,
948 &originating_invocation_id,
949 originating_usn)) {
950 continue;
954 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema,
955 sa, msg, dsdb_dn, la_list,
956 la_count, is_schema_nc);
957 if (!W_ERROR_IS_OK(werr)) {
958 talloc_free(tmp_ctx);
959 return werr;
964 talloc_free(tmp_ctx);
965 return WERR_OK;
969 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
971 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
972 struct ldb_dn *ncRoot_dn,
973 struct drsuapi_DsReplicaCursor2CtrEx *udv)
975 int ret;
977 udv->version = 2;
978 udv->reserved1 = 0;
979 udv->reserved2 = 0;
981 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
982 if (ret != LDB_SUCCESS) {
983 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
984 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
985 return WERR_DS_DRA_INTERNAL_ERROR;
988 return WERR_OK;
992 /* comparison function for linked attributes - see CompareLinks() in
993 * MS-DRSR section 4.1.10.5.17 */
994 static int linked_attribute_compare(const struct la_for_sorting *la1,
995 const struct la_for_sorting *la2)
997 int c;
998 c = memcmp(la1->source_guid,
999 la2->source_guid, sizeof(la2->source_guid));
1000 if (c != 0) {
1001 return c;
1004 if (la1->link->attid != la2->link->attid) {
1005 return la1->link->attid < la2->link->attid? -1:1;
1008 if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
1009 (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
1010 return (la1->link->flags &
1011 DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
1014 return memcmp(la1->target_guid,
1015 la2->target_guid, sizeof(la2->target_guid));
1018 struct drsuapi_changed_objects {
1019 struct ldb_dn *dn;
1020 struct GUID guid;
1021 uint64_t usn;
1026 sort the objects we send by tree order (Samba 4.5 emulation)
1028 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
1029 struct drsuapi_changed_objects *m2)
1031 return ldb_dn_compare(m2->dn, m1->dn);
1035 sort the objects we send first by uSNChanged
1037 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
1038 struct drsuapi_changed_objects *m2)
1040 if (m1->usn == m2->usn) {
1041 return ldb_dn_compare(m2->dn, m1->dn);
1044 if (m1->usn < m2->usn) {
1045 return -1;
1048 return 1;
1053 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
1055 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
1056 TALLOC_CTX *mem_ctx,
1057 struct drsuapi_DsGetNCChangesRequest10 *req10,
1058 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1059 struct ldb_dn **rid_manager_dn)
1061 struct ldb_dn *req_dn, *ntds_dn = NULL;
1062 int ret;
1063 struct ldb_context *ldb = b_state->sam_ctx;
1064 struct ldb_result *ext_res;
1065 struct dsdb_fsmo_extended_op *exop;
1066 bool is_us;
1069 steps:
1070 - verify that the DN being asked for is the RID Manager DN
1071 - verify that we are the RID Manager
1074 /* work out who is the RID Manager, also return to caller */
1075 ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
1076 if (ret != LDB_SUCCESS) {
1077 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
1078 return WERR_DS_DRA_INTERNAL_ERROR;
1081 ret = drs_ObjectIdentifier_to_dn_and_nc_root(mem_ctx,
1082 ldb,
1083 req10->naming_context,
1084 &req_dn,
1085 NULL);
1086 if (ret != LDB_SUCCESS) {
1087 DBG_ERR("RID Alloc request for invalid DN %s: %s\n",
1088 drs_ObjectIdentifier_to_debug_string(mem_ctx, req10->naming_context),
1089 ldb_strerror(ret));
1090 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1091 return WERR_OK;
1094 if (ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
1095 /* that isn't the RID Manager DN */
1096 DBG_ERR("RID Alloc request for wrong DN %s\n",
1097 drs_ObjectIdentifier_to_debug_string(mem_ctx,
1098 req10->naming_context));
1099 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1100 return WERR_OK;
1103 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1104 ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
1105 if (ret != LDB_SUCCESS) {
1106 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1107 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1108 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1109 return WERR_OK;
1112 /* find the DN of the RID Manager */
1113 ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
1114 if (ret != LDB_SUCCESS) {
1115 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1116 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1117 return WERR_DS_DRA_INTERNAL_ERROR;
1120 if (!is_us) {
1121 /* we're not the RID Manager - go away */
1122 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1123 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1124 return WERR_OK;
1127 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
1128 W_ERROR_HAVE_NO_MEMORY(exop);
1130 exop->fsmo_info = req10->fsmo_info;
1131 exop->destination_dsa_guid = req10->destination_dsa_guid;
1133 ret = ldb_transaction_start(ldb);
1134 if (ret != LDB_SUCCESS) {
1135 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1136 ldb_errstring(ldb)));
1137 return WERR_DS_DRA_INTERNAL_ERROR;
1140 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
1141 if (ret != LDB_SUCCESS) {
1142 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
1143 ldb_errstring(ldb)));
1144 ldb_transaction_cancel(ldb);
1145 return WERR_DS_DRA_INTERNAL_ERROR;
1148 ret = ldb_transaction_commit(ldb);
1149 if (ret != LDB_SUCCESS) {
1150 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1151 ldb_errstring(ldb)));
1152 return WERR_DS_DRA_INTERNAL_ERROR;
1155 talloc_free(ext_res);
1157 DEBUG(2,("Allocated RID pool for server %s\n",
1158 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1160 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1162 return WERR_OK;
1166 handle a DRSUAPI_EXOP_REPL_SECRET call
1168 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
1169 TALLOC_CTX *mem_ctx,
1170 struct drsuapi_DsGetNCChangesRequest10 *req10,
1171 struct dom_sid *user_sid,
1172 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1173 bool has_get_all_changes,
1174 struct ldb_dn **machine_dn)
1176 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1177 struct ldb_dn *obj_dn = NULL;
1178 struct ldb_message *ntds_msg = NULL;
1179 struct ldb_dn *ntds_dn = NULL, *server_dn = NULL;
1180 struct ldb_dn *rodc_dn, *krbtgt_link_dn;
1181 int ret;
1182 const char *ntds_attrs[] = { NULL };
1183 const char *rodc_attrs[] = { "msDS-KrbTgtLink",
1184 "msDS-NeverRevealGroup",
1185 "msDS-RevealOnDemandGroup",
1186 "userAccountControl",
1187 NULL };
1188 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
1189 struct ldb_result *rodc_res = NULL, *obj_res = NULL;
1190 WERROR werr;
1191 struct GUID_txt_buf guid_buf;
1193 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
1194 drs_ObjectIdentifier_to_debug_string(mem_ctx, ncRoot)));
1197 * we need to work out if we will allow this DC to
1198 * replicate the secrets for this object
1200 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
1201 * of this function
1204 if (b_state->sam_ctx_system == NULL) {
1205 /* this operation needs system level access */
1206 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
1207 return WERR_DS_DRA_ACCESS_DENIED;
1211 * Before we accept or deny, fetch the machine DN for the destination
1212 * DSA GUID.
1214 * If we are the RODC, we will check that this matches the SID.
1216 ret = samdb_get_ntds_obj_by_guid(mem_ctx,
1217 b_state->sam_ctx_system,
1218 &req10->destination_dsa_guid,
1219 ntds_attrs,
1220 &ntds_msg);
1221 if (ret != LDB_SUCCESS) {
1222 goto dest_dsa_error;
1225 ntds_dn = ntds_msg->dn;
1227 server_dn = ldb_dn_get_parent(mem_ctx, ntds_dn);
1228 if (server_dn == NULL) {
1229 goto failed;
1232 ret = samdb_reference_dn(b_state->sam_ctx_system, mem_ctx, server_dn,
1233 "serverReference", machine_dn);
1235 if (ret != LDB_SUCCESS) {
1236 goto dest_dsa_error;
1240 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
1242 * The pseudo code indicate
1243 * revealsecrets = true
1244 * if IsRevealSecretRequest(msgIn) then
1245 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
1246 * then
1247 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
1248 * <... check if this account is ok to be replicated on this DC ...>
1249 * <... and if not reveal secrets = no ...>
1250 * else
1251 * reveal secrets = false
1252 * endif
1253 * endif
1254 * endif
1256 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
1257 * then you can do EXOP_REPL_SECRETS
1259 ret = drs_ObjectIdentifier_to_dn_and_nc_root(mem_ctx,
1260 b_state->sam_ctx_system,
1261 ncRoot,
1262 &obj_dn,
1263 NULL);
1264 if (ret != LDB_SUCCESS) {
1265 DBG_ERR("RevealSecretRequest for invalid DN %s\n",
1266 drs_ObjectIdentifier_to_debug_string(mem_ctx, ncRoot));
1267 goto failed;
1270 if (!ldb_dn_validate(obj_dn)) goto failed;
1272 if (has_get_all_changes) {
1273 goto allowed;
1276 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1277 dom_sid_string(mem_ctx, user_sid));
1278 if (!ldb_dn_validate(rodc_dn)) goto failed;
1281 * do the two searches we need
1282 * We need DSDB_SEARCH_SHOW_EXTENDED_DN as we get a SID lists
1283 * out of the extended DNs
1285 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1286 DSDB_SEARCH_SHOW_EXTENDED_DN);
1287 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1289 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1290 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1293 * Must be an RODC account at this point, verify machine DN matches the
1294 * SID account
1296 if (ldb_dn_compare(rodc_res->msgs[0]->dn, *machine_dn) != 0) {
1297 goto denied;
1300 /* an RODC is allowed to get its own krbtgt account secrets */
1301 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1302 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1303 if (krbtgt_link_dn != NULL &&
1304 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1305 goto allowed;
1308 werr = samdb_confirm_rodc_allowed_to_repl_to(b_state->sam_ctx_system,
1309 user_sid,
1310 rodc_res->msgs[0],
1311 obj_res->msgs[0]);
1313 if (W_ERROR_IS_OK(werr)) {
1314 goto allowed;
1317 /* default deny */
1318 denied:
1319 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1320 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1321 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1322 return WERR_DS_DRA_SECRETS_DENIED;
1324 allowed:
1325 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1326 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1327 ldb_dn_get_linearized(*machine_dn)));
1328 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1329 req10->highwatermark.highest_usn = 0;
1330 return WERR_OK;
1332 failed:
1333 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1334 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1335 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1336 return WERR_DS_DRA_BAD_DN;
1338 dest_dsa_error:
1339 DBG_WARNING("Failed secret replication for %s by RODC %s as dest_dsa_guid %s is invalid\n",
1340 ldb_dn_get_linearized(obj_dn),
1341 dom_sid_string(mem_ctx, user_sid),
1342 GUID_buf_string(&req10->destination_dsa_guid,
1343 &guid_buf));
1344 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1345 return WERR_DS_DRA_DB_ERROR;
1349 handle a DRSUAPI_EXOP_REPL_OBJ call
1351 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1352 TALLOC_CTX *mem_ctx,
1353 struct drsuapi_DsGetNCChangesRequest10 *req10,
1354 struct dom_sid *user_sid,
1355 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1357 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1359 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1360 drs_ObjectIdentifier_to_debug_string(mem_ctx, ncRoot)));
1362 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1363 return WERR_OK;
1368 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1369 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1370 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1372 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1373 TALLOC_CTX *mem_ctx,
1374 struct drsuapi_DsGetNCChangesRequest10 *req10,
1375 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1377 struct ldb_dn *req_dn, *ntds_dn;
1378 int ret;
1379 unsigned int i;
1380 struct ldb_context *ldb = b_state->sam_ctx;
1381 struct ldb_message *msg;
1382 bool is_us;
1385 steps:
1386 - verify that the client dn exists
1387 - verify that we are the current master
1390 ret = drs_ObjectIdentifier_to_dn_and_nc_root(mem_ctx, ldb, req10->naming_context,
1391 &req_dn, NULL);
1392 if (ret != LDB_SUCCESS) {
1393 /* that is not a valid dn */
1394 DBG_ERR("FSMO role transfer request for invalid DN %s: %s\n",
1395 drs_ObjectIdentifier_to_debug_string(mem_ctx, req10->naming_context),
1396 ldb_strerror(ret));
1397 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1398 return WERR_OK;
1401 /* find the DN of the current role owner */
1402 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1403 if (ret != LDB_SUCCESS) {
1404 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1405 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1406 return WERR_DS_DRA_INTERNAL_ERROR;
1409 if (!is_us) {
1410 /* we're not the RID Manager or role owner - go away */
1411 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1412 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1413 return WERR_OK;
1416 /* change the current master */
1417 msg = ldb_msg_new(ldb);
1418 W_ERROR_HAVE_NO_MEMORY(msg);
1419 ret = drs_ObjectIdentifier_to_dn_and_nc_root(msg, ldb, req10->naming_context,
1420 &msg->dn, NULL);
1421 if (ret != LDB_SUCCESS) {
1422 /* that is not a valid dn */
1423 DBG_ERR("FSMO role transfer request for invalid DN %s: %s\n",
1424 drs_ObjectIdentifier_to_debug_string(mem_ctx, req10->naming_context),
1425 ldb_strerror(ret));
1426 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1427 return WERR_OK;
1430 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1431 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1432 if (ret != LDB_SUCCESS) {
1433 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1434 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1435 talloc_free(msg);
1436 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1437 return WERR_OK;
1440 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1441 if (ret != 0) {
1442 talloc_free(msg);
1443 return WERR_DS_DRA_INTERNAL_ERROR;
1446 for (i=0;i<msg->num_elements;i++) {
1447 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1450 ret = ldb_transaction_start(ldb);
1451 if (ret != LDB_SUCCESS) {
1452 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1453 ldb_errstring(ldb)));
1454 return WERR_DS_DRA_INTERNAL_ERROR;
1457 ret = ldb_modify(ldb, msg);
1458 if (ret != LDB_SUCCESS) {
1459 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1460 ldb_errstring(ldb)));
1461 ldb_transaction_cancel(ldb);
1462 return WERR_DS_DRA_INTERNAL_ERROR;
1465 ret = ldb_transaction_commit(ldb);
1466 if (ret != LDB_SUCCESS) {
1467 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1468 ldb_errstring(ldb)));
1469 return WERR_DS_DRA_INTERNAL_ERROR;
1472 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1474 return WERR_OK;
1478 see if this getncchanges request includes a request to reveal secret information
1480 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1481 struct drsuapi_DsGetNCChangesRequest10 *req10,
1482 struct dsdb_schema_prefixmap *pfm_remote,
1483 bool *is_secret_request)
1485 enum drsuapi_DsExtendedOperation exop;
1486 uint32_t i;
1487 struct dsdb_schema *schema;
1488 struct dsdb_syntax_ctx syntax_ctx;
1490 *is_secret_request = true;
1492 exop = req10->extended_op;
1494 switch (exop) {
1495 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1496 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1497 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1498 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1499 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1500 /* FSMO exops can reveal secrets */
1501 *is_secret_request = true;
1502 return WERR_OK;
1503 case DRSUAPI_EXOP_REPL_SECRET:
1504 case DRSUAPI_EXOP_REPL_OBJ:
1505 case DRSUAPI_EXOP_NONE:
1506 break;
1509 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1510 *is_secret_request = false;
1511 return WERR_OK;
1514 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1515 req10->partial_attribute_set == NULL) {
1516 /* they want secrets */
1517 *is_secret_request = true;
1518 return WERR_OK;
1521 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1522 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1523 syntax_ctx.pfm_remote = pfm_remote;
1525 /* check the attributes they asked for */
1526 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1527 const struct dsdb_attribute *sa;
1528 WERROR werr = getncchanges_attid_remote_to_local(schema,
1529 &syntax_ctx,
1530 req10->partial_attribute_set->attids[i],
1531 NULL,
1532 &sa);
1534 if (!W_ERROR_IS_OK(werr)) {
1535 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1536 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1537 return werr;
1540 if (!dsdb_attr_in_rodc_fas(sa)) {
1541 *is_secret_request = true;
1542 return WERR_OK;
1546 if (req10->partial_attribute_set_ex) {
1547 /* check the extended attributes they asked for */
1548 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1549 const struct dsdb_attribute *sa;
1550 WERROR werr = getncchanges_attid_remote_to_local(schema,
1551 &syntax_ctx,
1552 req10->partial_attribute_set_ex->attids[i],
1553 NULL,
1554 &sa);
1556 if (!W_ERROR_IS_OK(werr)) {
1557 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1558 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1559 return werr;
1562 if (!dsdb_attr_in_rodc_fas(sa)) {
1563 *is_secret_request = true;
1564 return WERR_OK;
1569 *is_secret_request = false;
1570 return WERR_OK;
1574 see if this getncchanges request is only for attributes in the GC
1575 partial attribute set
1577 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1578 struct drsuapi_DsGetNCChangesRequest10 *req10,
1579 struct dsdb_schema_prefixmap *pfm_remote,
1580 bool *is_gc_pas_request)
1582 enum drsuapi_DsExtendedOperation exop;
1583 uint32_t i;
1584 struct dsdb_schema *schema;
1585 struct dsdb_syntax_ctx syntax_ctx;
1587 exop = req10->extended_op;
1589 switch (exop) {
1590 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1591 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1592 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1593 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1594 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1595 case DRSUAPI_EXOP_REPL_SECRET:
1596 *is_gc_pas_request = false;
1597 return WERR_OK;
1598 case DRSUAPI_EXOP_REPL_OBJ:
1599 case DRSUAPI_EXOP_NONE:
1600 break;
1603 if (req10->partial_attribute_set == NULL) {
1604 /* they want it all */
1605 *is_gc_pas_request = false;
1606 return WERR_OK;
1609 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1610 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1611 syntax_ctx.pfm_remote = pfm_remote;
1613 /* check the attributes they asked for */
1614 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1615 const struct dsdb_attribute *sa;
1616 WERROR werr = getncchanges_attid_remote_to_local(schema,
1617 &syntax_ctx,
1618 req10->partial_attribute_set->attids[i],
1619 NULL,
1620 &sa);
1622 if (!W_ERROR_IS_OK(werr)) {
1623 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1624 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1625 return werr;
1628 if (!sa->isMemberOfPartialAttributeSet) {
1629 *is_gc_pas_request = false;
1630 return WERR_OK;
1634 if (req10->partial_attribute_set_ex) {
1635 /* check the extended attributes they asked for */
1636 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1637 const struct dsdb_attribute *sa;
1638 WERROR werr = getncchanges_attid_remote_to_local(schema,
1639 &syntax_ctx,
1640 req10->partial_attribute_set_ex->attids[i],
1641 NULL,
1642 &sa);
1644 if (!W_ERROR_IS_OK(werr)) {
1645 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1646 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1647 return werr;
1650 if (!sa->isMemberOfPartialAttributeSet) {
1651 *is_gc_pas_request = false;
1652 return WERR_OK;
1657 *is_gc_pas_request = true;
1658 return WERR_OK;
1663 map from req8 to req10
1665 static struct drsuapi_DsGetNCChangesRequest10 *
1666 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1667 struct drsuapi_DsGetNCChangesRequest8 *req8)
1669 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1670 struct drsuapi_DsGetNCChangesRequest10);
1671 if (req10 == NULL) {
1672 return NULL;
1675 req10->destination_dsa_guid = req8->destination_dsa_guid;
1676 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1677 req10->naming_context = req8->naming_context;
1678 req10->highwatermark = req8->highwatermark;
1679 req10->uptodateness_vector = req8->uptodateness_vector;
1680 req10->replica_flags = req8->replica_flags;
1681 req10->max_object_count = req8->max_object_count;
1682 req10->max_ndr_size = req8->max_ndr_size;
1683 req10->extended_op = req8->extended_op;
1684 req10->fsmo_info = req8->fsmo_info;
1685 req10->partial_attribute_set = req8->partial_attribute_set;
1686 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1687 req10->mapping_ctr = req8->mapping_ctr;
1689 return req10;
1692 static const char *collect_objects_attrs[] = { "uSNChanged",
1693 "objectGUID" ,
1694 NULL };
1697 * Collects object for normal replication cycle.
1699 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1700 TALLOC_CTX *mem_ctx,
1701 struct drsuapi_getncchanges_state *getnc_state,
1702 struct drsuapi_DsGetNCChangesRequest10 *req10,
1703 struct ldb_dn *search_dn,
1704 const char *extra_filter,
1705 struct ldb_result **search_res)
1707 int ret;
1708 char* search_filter;
1709 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1710 bool critical_only = false;
1712 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1713 critical_only = true;
1716 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1717 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1718 scope = LDB_SCOPE_BASE;
1719 critical_only = false;
1722 /* Construct response. */
1723 search_filter = talloc_asprintf(mem_ctx,
1724 "(uSNChanged>=%llu)",
1725 (unsigned long long)(getnc_state->min_usn+1));
1727 if (extra_filter) {
1728 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1731 if (critical_only) {
1732 search_filter = talloc_asprintf(mem_ctx,
1733 "(&%s(isCriticalSystemObject=TRUE))",
1734 search_filter);
1737 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1738 scope = LDB_SCOPE_BASE;
1741 if (!search_dn) {
1742 search_dn = getnc_state->ncRoot_dn;
1745 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1746 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1747 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1748 search_dn, scope,
1749 collect_objects_attrs,
1750 search_filter);
1751 if (ret != LDB_SUCCESS) {
1752 return WERR_DS_DRA_INTERNAL_ERROR;
1755 return WERR_OK;
1759 * Collects object for normal replication cycle.
1761 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1762 TALLOC_CTX *mem_ctx,
1763 struct drsuapi_getncchanges_state *getnc_state,
1764 struct drsuapi_DsGetNCChangesRequest10 *req10,
1765 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1766 struct ldb_dn *search_dn,
1767 const char *extra_filter,
1768 struct ldb_result **search_res)
1770 /* we have nothing to do in case of ex-op failure */
1771 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1772 return WERR_OK;
1775 switch (req10->extended_op) {
1776 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1778 int ret;
1779 struct ldb_dn *ntds_dn = NULL;
1780 struct ldb_dn *server_dn = NULL;
1781 struct ldb_dn *machine_dn = NULL;
1782 struct ldb_dn *rid_set_dn = NULL;
1783 struct ldb_result *search_res2 = NULL;
1784 struct ldb_result *search_res3 = NULL;
1785 TALLOC_CTX *frame = talloc_stackframe();
1786 /* get RID manager, RID set and server DN (in that order) */
1788 /* This first search will get the RID Manager */
1789 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1790 search_res,
1791 search_dn, LDB_SCOPE_BASE,
1792 collect_objects_attrs,
1793 NULL);
1794 if (ret != LDB_SUCCESS) {
1795 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s\n",
1796 ldb_dn_get_linearized(search_dn),
1797 ldb_errstring(b_state->sam_ctx)));
1798 TALLOC_FREE(frame);
1799 return WERR_DS_DRA_INTERNAL_ERROR;
1802 if ((*search_res)->count != 1) {
1803 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned\n",
1804 ldb_dn_get_linearized(search_dn),
1805 (*search_res)->count));
1806 TALLOC_FREE(frame);
1807 return WERR_DS_DRA_INTERNAL_ERROR;
1810 /* Now extend it to the RID set */
1812 /* Find the computer account DN for the destination
1813 * dsa GUID specified */
1815 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1816 &req10->destination_dsa_guid, 0,
1817 &ntds_dn);
1818 if (ret != LDB_SUCCESS) {
1819 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1820 GUID_string(frame,
1821 &req10->destination_dsa_guid),
1822 ldb_errstring(b_state->sam_ctx)));
1823 TALLOC_FREE(frame);
1824 return WERR_DS_DRA_INTERNAL_ERROR;
1827 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1828 if (!server_dn) {
1829 TALLOC_FREE(frame);
1830 return WERR_DS_DRA_INTERNAL_ERROR;
1833 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1834 "serverReference", &machine_dn);
1835 if (ret != LDB_SUCCESS) {
1836 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s\n",
1837 ldb_dn_get_linearized(server_dn),
1838 ldb_errstring(b_state->sam_ctx)));
1839 TALLOC_FREE(frame);
1840 return WERR_DS_DRA_INTERNAL_ERROR;
1843 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1844 "rIDSetReferences", &rid_set_dn);
1845 if (ret != LDB_SUCCESS) {
1846 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s\n",
1847 ldb_dn_get_linearized(server_dn),
1848 ldb_errstring(b_state->sam_ctx)));
1849 TALLOC_FREE(frame);
1850 return WERR_DS_DRA_INTERNAL_ERROR;
1854 /* This first search will get the RID Manager, now get the RID set */
1855 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1856 &search_res2,
1857 rid_set_dn, LDB_SCOPE_BASE,
1858 collect_objects_attrs,
1859 NULL);
1860 if (ret != LDB_SUCCESS) {
1861 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s\n",
1862 ldb_dn_get_linearized(rid_set_dn),
1863 ldb_errstring(b_state->sam_ctx)));
1864 TALLOC_FREE(frame);
1865 return WERR_DS_DRA_INTERNAL_ERROR;
1868 if (search_res2->count != 1) {
1869 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned\n",
1870 ldb_dn_get_linearized(rid_set_dn),
1871 search_res2->count));
1872 TALLOC_FREE(frame);
1873 return WERR_DS_DRA_INTERNAL_ERROR;
1876 /* Finally get the server DN */
1877 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1878 &search_res3,
1879 machine_dn, LDB_SCOPE_BASE,
1880 collect_objects_attrs,
1881 NULL);
1882 if (ret != LDB_SUCCESS) {
1883 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s\n",
1884 ldb_dn_get_linearized(server_dn),
1885 ldb_errstring(b_state->sam_ctx)));
1886 TALLOC_FREE(frame);
1887 return WERR_DS_DRA_INTERNAL_ERROR;
1890 if (search_res3->count != 1) {
1891 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned\n",
1892 ldb_dn_get_linearized(server_dn),
1893 search_res3->count));
1894 TALLOC_FREE(frame);
1895 return WERR_DS_DRA_INTERNAL_ERROR;
1898 /* Now extend the original search_res with these answers */
1899 (*search_res)->count = 3;
1901 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1902 struct ldb_message *,
1903 (*search_res)->count);
1904 if ((*search_res)->msgs == NULL) {
1905 TALLOC_FREE(frame);
1906 return WERR_NOT_ENOUGH_MEMORY;
1910 talloc_steal(mem_ctx, *search_res);
1911 (*search_res)->msgs[1] =
1912 talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1913 (*search_res)->msgs[2] =
1914 talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1916 TALLOC_FREE(frame);
1917 return WERR_OK;
1919 default:
1920 /* TODO: implement extended op specific collection
1921 * of objects. Right now we just normal procedure
1922 * for collecting objects */
1923 return getncchanges_collect_objects(b_state,
1924 mem_ctx,
1925 getnc_state,
1926 req10,
1927 search_dn,
1928 extra_filter,
1929 search_res);
1933 static void dcesrv_drsuapi_update_highwatermark(const struct ldb_message *msg,
1934 uint64_t max_usn,
1935 struct drsuapi_DsReplicaHighWaterMark *hwm)
1937 uint64_t uSN = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
1939 if (uSN > max_usn) {
1941 * Only report the max_usn we had at the start
1942 * of the replication cycle.
1944 * If this object has changed lately we better
1945 * let the destination dsa refetch the change.
1946 * This is better than the risk of losing some
1947 * objects or linked attributes.
1949 return;
1952 if (uSN <= hwm->tmp_highest_usn) {
1953 return;
1956 hwm->tmp_highest_usn = uSN;
1957 hwm->reserved_usn = 0;
1961 * Adds an object's GUID to the cache of objects already sent.
1962 * This avoids us sending the same object multiple times when
1963 * the GetNCChanges request uses a flag like GET_ANC.
1965 static WERROR dcesrv_drsuapi_obj_cache_add(struct db_context *obj_cache,
1966 const struct GUID *guid)
1968 enum ndr_err_code ndr_err;
1969 uint8_t guid_buf[DRS_GUID_SIZE] = { 0, };
1970 DATA_BLOB b = {
1971 .data = guid_buf,
1972 .length = sizeof(guid_buf),
1974 TDB_DATA key = {
1975 .dptr = b.data,
1976 .dsize = b.length,
1978 TDB_DATA val = {
1979 .dptr = NULL,
1980 .dsize = 0,
1982 NTSTATUS status;
1984 ndr_err = ndr_push_struct_into_fixed_blob(&b, guid,
1985 (ndr_push_flags_fn_t)ndr_push_GUID);
1986 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1987 return WERR_DS_DRA_INTERNAL_ERROR;
1990 status = dbwrap_store(obj_cache, key, val, TDB_REPLACE);
1991 if (!NT_STATUS_IS_OK(status)) {
1992 return WERR_DS_DRA_INTERNAL_ERROR;
1995 return WERR_OK;
1999 * Checks if the object with the GUID specified already exists in the
2000 * object cache, i.e. it's already been sent in a GetNCChanges response.
2002 static WERROR dcesrv_drsuapi_obj_cache_exists(struct db_context *obj_cache,
2003 const struct GUID *guid)
2005 enum ndr_err_code ndr_err;
2006 uint8_t guid_buf[DRS_GUID_SIZE] = { 0, };
2007 DATA_BLOB b = {
2008 .data = guid_buf,
2009 .length = sizeof(guid_buf),
2011 TDB_DATA key = {
2012 .dptr = b.data,
2013 .dsize = b.length,
2015 bool exists;
2017 ndr_err = ndr_push_struct_into_fixed_blob(&b, guid,
2018 (ndr_push_flags_fn_t)ndr_push_GUID);
2019 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2020 return WERR_DS_DRA_INTERNAL_ERROR;
2023 exists = dbwrap_exists(obj_cache, key);
2024 if (!exists) {
2025 return WERR_OBJECT_NOT_FOUND;
2028 return WERR_OBJECT_NAME_EXISTS;
2032 * Copies the la_list specified into a sorted array, ready to be sent in a
2033 * GetNCChanges response.
2035 static WERROR getncchanges_get_sorted_array(const struct drsuapi_DsReplicaLinkedAttribute *la_list,
2036 const uint32_t link_count,
2037 struct ldb_context *sam_ctx,
2038 TALLOC_CTX *mem_ctx,
2039 const struct dsdb_schema *schema,
2040 struct la_for_sorting **ret_array)
2042 int j;
2043 struct la_for_sorting *guid_array;
2044 WERROR werr = WERR_OK;
2046 *ret_array = NULL;
2047 guid_array = talloc_array(mem_ctx, struct la_for_sorting, link_count);
2048 if (guid_array == NULL) {
2049 DEBUG(0, ("Out of memory allocating %u linked attributes for sorting\n", link_count));
2050 return WERR_NOT_ENOUGH_MEMORY;
2053 for (j = 0; j < link_count; j++) {
2055 /* we need to get the target GUIDs to compare */
2056 struct dsdb_dn *dn;
2057 const struct drsuapi_DsReplicaLinkedAttribute *la = &la_list[j];
2058 const struct dsdb_attribute *schema_attrib;
2059 const struct ldb_val *target_guid;
2060 DATA_BLOB source_guid;
2061 TALLOC_CTX *frame = talloc_stackframe();
2062 NTSTATUS status;
2064 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2066 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, frame, la->value.blob, &dn);
2067 if (!W_ERROR_IS_OK(werr)) {
2068 DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2069 TALLOC_FREE(frame);
2070 return werr;
2073 /* Extract the target GUID in NDR form */
2074 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2075 if (target_guid == NULL
2076 || target_guid->length != sizeof(guid_array[0].target_guid)) {
2077 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2078 } else {
2079 /* Repack the source GUID as NDR for sorting */
2080 status = GUID_to_ndr_blob(&la->identifier->guid,
2081 frame,
2082 &source_guid);
2085 if (!NT_STATUS_IS_OK(status)
2086 || source_guid.length != sizeof(guid_array[0].source_guid)) {
2087 DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2088 TALLOC_FREE(frame);
2089 return ntstatus_to_werror(status);
2092 guid_array[j].link = &la_list[j];
2093 memcpy(guid_array[j].target_guid, target_guid->data,
2094 sizeof(guid_array[j].target_guid));
2095 memcpy(guid_array[j].source_guid, source_guid.data,
2096 sizeof(guid_array[j].source_guid));
2097 TALLOC_FREE(frame);
2100 TYPESAFE_QSORT(guid_array, link_count, linked_attribute_compare);
2102 *ret_array = guid_array;
2104 return werr;
2109 * Adds any ancestor/parent objects of the child_obj specified.
2110 * This is needed when the GET_ANC flag is specified in the request.
2111 * @param new_objs if parents are added, this gets updated to point to a chain
2112 * of parent objects (with the parents first and the child last)
2114 static WERROR getncchanges_add_ancestors(const struct GUID *parent_object_guid,
2115 struct ldb_dn *child_dn,
2116 TALLOC_CTX *mem_ctx,
2117 struct ldb_context *sam_ctx,
2118 struct drsuapi_getncchanges_state *getnc_state,
2119 struct dsdb_schema *schema,
2120 DATA_BLOB *session_key,
2121 struct drsuapi_DsGetNCChangesRequest10 *req10,
2122 uint32_t *local_pas,
2123 struct ldb_dn *machine_dn,
2124 struct drsuapi_DsReplicaObjectListItemEx **new_objs)
2126 int ret;
2127 const struct GUID *next_anc_guid = NULL;
2128 WERROR werr = WERR_OK;
2129 static const char * const msg_attrs[] = {
2130 "*",
2131 "nTSecurityDescriptor",
2132 "parentGUID",
2133 "replPropertyMetaData",
2134 DSDB_SECRET_ATTRIBUTES,
2135 NULL };
2137 next_anc_guid = parent_object_guid;
2139 while (next_anc_guid != NULL) {
2140 struct drsuapi_DsReplicaObjectListItemEx *anc_obj = NULL;
2141 struct ldb_message *anc_msg = NULL;
2142 struct ldb_result *anc_res = NULL;
2143 struct ldb_dn *anc_dn = NULL;
2146 * For the GET_ANC case (but not the 'send NC root
2147 * first' case), don't send an object twice.
2149 * (If we've sent the object, then we've also sent all
2150 * its parents as well)
2152 if (getnc_state->obj_cache) {
2153 werr = dcesrv_drsuapi_obj_cache_exists(getnc_state->obj_cache,
2154 next_anc_guid);
2155 if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
2156 return WERR_OK;
2158 if (W_ERROR_IS_OK(werr)) {
2159 return WERR_INTERNAL_ERROR;
2161 if (!W_ERROR_EQUAL(werr, WERR_OBJECT_NOT_FOUND)) {
2162 return werr;
2166 anc_obj = talloc_zero(mem_ctx,
2167 struct drsuapi_DsReplicaObjectListItemEx);
2168 if (anc_obj == NULL) {
2169 return WERR_NOT_ENOUGH_MEMORY;
2172 anc_dn = ldb_dn_new_fmt(anc_obj, sam_ctx, "<GUID=%s>",
2173 GUID_string(anc_obj, next_anc_guid));
2174 if (anc_dn == NULL) {
2175 return WERR_NOT_ENOUGH_MEMORY;
2178 ret = drsuapi_search_with_extended_dn(sam_ctx, anc_obj,
2179 &anc_res, anc_dn,
2180 LDB_SCOPE_BASE,
2181 msg_attrs, NULL);
2182 if (ret != LDB_SUCCESS) {
2183 const char *anc_str = NULL;
2184 const char *obj_str = NULL;
2186 anc_str = ldb_dn_get_extended_linearized(anc_obj,
2187 anc_dn,
2189 obj_str = ldb_dn_get_extended_linearized(anc_obj,
2190 child_dn,
2193 DBG_ERR("getncchanges: failed to fetch ANC "
2194 "DN %s for DN %s - %s\n",
2195 anc_str, obj_str, ldb_errstring(sam_ctx));
2196 return WERR_DS_DRA_INCONSISTENT_DIT;
2199 anc_msg = anc_res->msgs[0];
2201 werr = get_nc_changes_build_object(anc_obj, anc_msg,
2202 sam_ctx,
2203 getnc_state,
2204 schema, session_key,
2205 req10,
2206 false, /* force_object_return */
2207 local_pas,
2208 machine_dn,
2209 next_anc_guid);
2210 if (!W_ERROR_IS_OK(werr)) {
2211 return werr;
2215 * Regardless of whether we actually use it or not,
2216 * we add it to the cache so we don't look at it again
2218 * The only time we are here without
2219 * getnc_state->obj_cache is for the forced addition
2220 * of the NC root to the start of the reply, this we
2221 * want to add each and every call..
2223 if (getnc_state->obj_cache) {
2224 werr = dcesrv_drsuapi_obj_cache_add(getnc_state->obj_cache,
2225 next_anc_guid);
2226 if (!W_ERROR_IS_OK(werr)) {
2227 return werr;
2232 * Any ancestors which are below the highwatermark
2233 * or uptodateness_vector shouldn't be added,
2234 * but we still look further up the
2235 * tree for ones which have been changed recently.
2237 if (anc_obj->meta_data_ctr != NULL) {
2240 * prepend the parent to the list so that the client-side
2241 * adds the parent object before it adds the children
2243 anc_obj->next_object = *new_objs;
2244 *new_objs = anc_obj;
2247 anc_msg = NULL;
2248 TALLOC_FREE(anc_res);
2249 TALLOC_FREE(anc_dn);
2252 * We may need to resolve more parents...
2254 next_anc_guid = anc_obj->parent_object_guid;
2256 return werr;
2260 * Adds a list of new objects into the current chunk of replication data to send
2262 static void getncchanges_chunk_add_objects(struct getncchanges_repl_chunk *repl_chunk,
2263 struct drsuapi_DsReplicaObjectListItemEx *obj_list)
2265 struct drsuapi_DsReplicaObjectListItemEx *obj;
2268 * We track the last object added to the replication chunk, so just add
2269 * the new object-list onto the end
2271 if (repl_chunk->object_list == NULL) {
2272 repl_chunk->object_list = obj_list;
2273 } else {
2274 repl_chunk->last_object->next_object = obj_list;
2277 for (obj = obj_list; obj != NULL; obj = obj->next_object) {
2278 repl_chunk->object_count += 1;
2281 * Remember the last object in the response - we'll use this to
2282 * link the next object(s) processed onto the existing list
2284 if (obj->next_object == NULL) {
2285 repl_chunk->last_object = obj;
2291 * Gets the object to send, packed into an RPC struct ready to send. This also
2292 * adds the object to the object cache, and adds any ancestors (if needed).
2293 * @param msg - DB search result for the object to add
2294 * @param guid - GUID of the object to add
2295 * @param ret_obj_list - returns the object ready to be sent (in a list, along
2296 * with any ancestors that might be needed). NULL if nothing to send.
2298 static WERROR getncchanges_get_obj_to_send(const struct ldb_message *msg,
2299 TALLOC_CTX *mem_ctx,
2300 struct ldb_context *sam_ctx,
2301 struct drsuapi_getncchanges_state *getnc_state,
2302 struct dsdb_schema *schema,
2303 DATA_BLOB *session_key,
2304 struct drsuapi_DsGetNCChangesRequest10 *req10,
2305 bool force_object_return,
2306 uint32_t *local_pas,
2307 struct ldb_dn *machine_dn,
2308 const struct GUID *guid,
2309 struct drsuapi_DsReplicaObjectListItemEx **ret_obj_list)
2311 struct drsuapi_DsReplicaObjectListItemEx *obj;
2312 WERROR werr;
2314 *ret_obj_list = NULL;
2316 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2317 W_ERROR_HAVE_NO_MEMORY(obj);
2319 werr = get_nc_changes_build_object(obj, msg, sam_ctx, getnc_state,
2320 schema, session_key, req10,
2321 force_object_return,
2322 local_pas, machine_dn, guid);
2323 if (!W_ERROR_IS_OK(werr)) {
2324 return werr;
2328 * The object may get filtered out by the UTDV's USN and not actually
2329 * sent, in which case there's nothing more to do here
2331 if (obj->meta_data_ctr == NULL) {
2332 TALLOC_FREE(obj);
2333 return WERR_OK;
2336 if (getnc_state->obj_cache != NULL) {
2337 werr = dcesrv_drsuapi_obj_cache_add(getnc_state->obj_cache,
2338 guid);
2339 if (!W_ERROR_IS_OK(werr)) {
2340 return werr;
2344 *ret_obj_list = obj;
2347 * If required, also add any ancestors that the client may need to know
2348 * about before it can resolve this object. These get prepended to the
2349 * ret_obj_list so the client adds them first.
2351 * We allow this to be disabled to permit testing of a
2352 * client-side fallback for the broken behaviour in Samba 4.5
2353 * and earlier.
2355 if (getnc_state->is_get_anc
2356 && !getnc_state->broken_samba_4_5_get_anc_emulation) {
2357 werr = getncchanges_add_ancestors(obj->parent_object_guid,
2358 msg->dn, mem_ctx,
2359 sam_ctx, getnc_state,
2360 schema, session_key,
2361 req10, local_pas,
2362 machine_dn, ret_obj_list);
2365 return werr;
2369 * Returns the number of links that are waiting to be sent
2371 static uint32_t getncchanges_chunk_links_pending(struct getncchanges_repl_chunk *repl_chunk,
2372 struct drsuapi_getncchanges_state *getnc_state)
2374 uint32_t links_to_send = 0;
2376 if (getnc_state->is_get_tgt) {
2379 * when the GET_TGT flag is set, only include the linked
2380 * attributes whose target object has already been checked
2381 * (i.e. they're ready to send).
2383 if (repl_chunk->tgt_la_count > getnc_state->la_idx) {
2384 links_to_send = (repl_chunk->tgt_la_count -
2385 getnc_state->la_idx);
2387 } else {
2388 links_to_send = getnc_state->la_count - getnc_state->la_idx;
2391 return links_to_send;
2395 * Returns the max number of links that will fit in the current replication chunk
2397 static uint32_t getncchanges_chunk_max_links(struct getncchanges_repl_chunk *repl_chunk)
2399 uint32_t max_links = 0;
2401 if (repl_chunk->max_links != DEFAULT_MAX_LINKS ||
2402 repl_chunk->max_objects != DEFAULT_MAX_OBJECTS) {
2405 * We're using non-default settings, so don't try to adjust
2406 * them, just trust the user has configured decent values
2408 max_links = repl_chunk->max_links;
2410 } else if (repl_chunk->max_links > repl_chunk->object_count) {
2413 * This is just an approximate guess to avoid overfilling the
2414 * replication chunk. It's the logic we've used historically.
2415 * E.g. if we've already sent 1000 objects, then send 1000 fewer
2416 * links. For comparison, the max that Windows seems to send is
2417 * ~2700 links and ~250 objects (although this may vary based
2418 * on timeouts)
2420 max_links = repl_chunk->max_links - repl_chunk->object_count;
2423 return max_links;
2427 * Returns true if the current GetNCChanges() call has taken longer than its
2428 * allotted time. This prevents the client from timing out.
2430 static bool getncchanges_chunk_timed_out(struct getncchanges_repl_chunk *repl_chunk)
2432 return (time(NULL) - repl_chunk->start > repl_chunk->max_wait);
2436 * Returns true if the current chunk of replication data has reached the
2437 * max_objects and/or max_links thresholds.
2439 static bool getncchanges_chunk_is_full(struct getncchanges_repl_chunk *repl_chunk,
2440 struct drsuapi_getncchanges_state *getnc_state)
2442 bool chunk_full = false;
2443 uint32_t links_to_send;
2444 uint32_t chunk_limit;
2446 /* check if the current chunk is already full with objects */
2447 if (repl_chunk->object_count >= repl_chunk->max_objects) {
2448 chunk_full = true;
2450 } else if (repl_chunk->object_count > 0 &&
2451 getncchanges_chunk_timed_out(repl_chunk)) {
2454 * We've exceeded our allotted time building this chunk,
2455 * and we have at least one object to send back to the client
2457 chunk_full = true;
2459 } else if (repl_chunk->immediate_link_sync) {
2461 /* check if the chunk is already full with links */
2462 links_to_send = getncchanges_chunk_links_pending(repl_chunk,
2463 getnc_state);
2465 chunk_limit = getncchanges_chunk_max_links(repl_chunk);
2468 * The chunk is full if we've got more links to send than will
2469 * fit in one chunk
2471 if (links_to_send > 0 && chunk_limit <= links_to_send) {
2472 chunk_full = true;
2476 return chunk_full;
2480 * Goes through any new linked attributes and checks that the target object
2481 * will be known to the client, i.e. we've already sent it in an replication
2482 * chunk. If not, then it adds the target object to the current replication
2483 * chunk. This is only done when the client specifies DRS_GET_TGT.
2485 static WERROR getncchanges_chunk_add_la_targets(struct getncchanges_repl_chunk *repl_chunk,
2486 struct drsuapi_getncchanges_state *getnc_state,
2487 uint32_t start_la_index,
2488 TALLOC_CTX *mem_ctx,
2489 struct ldb_context *sam_ctx,
2490 struct dsdb_schema *schema,
2491 DATA_BLOB *session_key,
2492 struct drsuapi_DsGetNCChangesRequest10 *req10,
2493 uint32_t *local_pas,
2494 struct ldb_dn *machine_dn)
2496 int ret;
2497 uint32_t i;
2498 uint32_t max_la_index;
2499 uint32_t max_links;
2500 uint32_t target_count = 0;
2501 WERROR werr = WERR_OK;
2502 static const char * const msg_attrs[] = {
2503 "*",
2504 "nTSecurityDescriptor",
2505 "parentGUID",
2506 "replPropertyMetaData",
2507 DSDB_SECRET_ATTRIBUTES,
2508 NULL };
2511 * A object can potentially link to thousands of targets. Only bother
2512 * checking as many targets as will fit into the current response
2514 max_links = getncchanges_chunk_max_links(repl_chunk);
2515 max_la_index = MIN(getnc_state->la_count,
2516 start_la_index + max_links);
2518 /* loop through any linked attributes to check */
2519 for (i = start_la_index;
2520 (i < max_la_index &&
2521 !getncchanges_chunk_is_full(repl_chunk, getnc_state));
2522 i++) {
2524 struct GUID target_guid;
2525 struct drsuapi_DsReplicaObjectListItemEx *new_objs = NULL;
2526 const struct drsuapi_DsReplicaLinkedAttribute *la;
2527 struct ldb_result *msg_res;
2528 struct ldb_dn *search_dn;
2529 TALLOC_CTX *tmp_ctx;
2530 struct dsdb_dn *dn;
2531 const struct dsdb_attribute *schema_attrib;
2532 NTSTATUS status;
2533 bool same_nc;
2535 la = &getnc_state->la_list[i];
2536 tmp_ctx = talloc_new(mem_ctx);
2539 * Track what linked attribute targets we've checked. We might
2540 * not have time to check them all, so we should only send back
2541 * the ones we've actually checked.
2543 repl_chunk->tgt_la_count = i + 1;
2545 /* get the GUID of the linked attribute's target object */
2546 schema_attrib = dsdb_attribute_by_attributeID_id(schema,
2547 la->attid);
2549 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema,
2550 tmp_ctx, la->value.blob, &dn);
2552 if (!W_ERROR_IS_OK(werr)) {
2553 DEBUG(0,(__location__ ": Bad la blob\n"));
2554 return werr;
2557 status = dsdb_get_extended_dn_guid(dn->dn, &target_guid, "GUID");
2559 if (!NT_STATUS_IS_OK(status)) {
2560 return ntstatus_to_werror(status);
2564 * if the target isn't in the cache, then the client
2565 * might not know about it, so send the target now
2567 werr = dcesrv_drsuapi_obj_cache_exists(getnc_state->obj_cache,
2568 &target_guid);
2570 if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
2572 /* target already sent, nothing to do */
2573 TALLOC_FREE(tmp_ctx);
2574 continue;
2577 same_nc = dsdb_objects_have_same_nc(sam_ctx, tmp_ctx, dn->dn,
2578 getnc_state->ncRoot_dn);
2580 /* don't try to fetch target objects from another partition */
2581 if (!same_nc) {
2582 TALLOC_FREE(tmp_ctx);
2583 continue;
2586 search_dn = ldb_dn_new_fmt(tmp_ctx, sam_ctx, "<GUID=%s>",
2587 GUID_string(tmp_ctx, &target_guid));
2588 W_ERROR_HAVE_NO_MEMORY(search_dn);
2590 ret = drsuapi_search_with_extended_dn(sam_ctx, tmp_ctx,
2591 &msg_res, search_dn,
2592 LDB_SCOPE_BASE,
2593 msg_attrs, NULL);
2596 * Don't fail the replication if we can't find the target.
2597 * This could happen for a one-way linked attribute, if the
2598 * target is deleted and then later expunged (thus, the source
2599 * object can be left with a hanging link). Continue to send
2600 * the the link (the client-side has already tried once with
2601 * GET_TGT, so it should just end up ignoring it).
2603 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2604 DBG_WARNING("Encountered unknown link target DN %s\n",
2605 ldb_dn_get_extended_linearized(tmp_ctx, dn->dn, 1));
2606 TALLOC_FREE(tmp_ctx);
2607 continue;
2609 } else if (ret != LDB_SUCCESS) {
2610 DBG_ERR("Failed to fetch link target DN %s - %s\n",
2611 ldb_dn_get_extended_linearized(tmp_ctx, dn->dn, 1),
2612 ldb_errstring(sam_ctx));
2613 return WERR_DS_DRA_INCONSISTENT_DIT;
2617 * Construct an object, ready to send (this will include
2618 * the object's ancestors as well, if GET_ANC is set)
2620 werr = getncchanges_get_obj_to_send(msg_res->msgs[0], mem_ctx,
2621 sam_ctx, getnc_state,
2622 schema, session_key, req10,
2623 false, local_pas,
2624 machine_dn, &target_guid,
2625 &new_objs);
2626 if (!W_ERROR_IS_OK(werr)) {
2627 return werr;
2630 if (new_objs != NULL) {
2631 target_count++;
2632 getncchanges_chunk_add_objects(repl_chunk, new_objs);
2634 TALLOC_FREE(tmp_ctx);
2637 if (target_count > 0) {
2638 DEBUG(3, ("GET_TGT: checked %u link-attrs, added %u target objs\n",
2639 i - start_la_index, target_count));
2642 return WERR_OK;
2646 * Creates a helper struct used for building a chunk of replication data,
2647 * i.e. used over a single call to dcesrv_drsuapi_DsGetNCChanges().
2649 static struct getncchanges_repl_chunk * getncchanges_chunk_new(TALLOC_CTX *mem_ctx,
2650 struct dcesrv_call_state *dce_call,
2651 struct drsuapi_DsGetNCChangesRequest10 *req10)
2653 struct getncchanges_repl_chunk *repl_chunk;
2655 repl_chunk = talloc_zero(mem_ctx, struct getncchanges_repl_chunk);
2657 repl_chunk->start = time(NULL);
2659 repl_chunk->max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL,
2660 "drs", "max object sync",
2661 DEFAULT_MAX_OBJECTS);
2664 * The client control here only applies in normal replication, not extended
2665 * operations, which return a fixed set, even if the caller
2666 * sets max_object_count == 0
2668 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2671 * use this to force single objects at a time, which is useful
2672 * for working out what object is giving problems
2674 if (req10->max_object_count < repl_chunk->max_objects) {
2675 repl_chunk->max_objects = req10->max_object_count;
2679 repl_chunk->max_links =
2680 lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL,
2681 "drs", "max link sync",
2682 DEFAULT_MAX_LINKS);
2684 repl_chunk->immediate_link_sync =
2685 lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL,
2686 "drs", "immediate link sync", false);
2689 * Maximum time that we can spend in a getncchanges
2690 * in order to avoid timeout of the other part.
2691 * 10 seconds by default.
2693 repl_chunk->max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx,
2694 NULL, "drs", "max work time", 10);
2696 return repl_chunk;
2700 drsuapi_DsGetNCChanges
2702 see MS-DRSR 4.1.10.5.2 for basic logic of this function
2704 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
2705 struct drsuapi_DsGetNCChanges *r)
2707 struct auth_session_info *session_info =
2708 dcesrv_call_session_info(dce_call);
2709 struct imessaging_context *imsg_ctx =
2710 dcesrv_imessaging_context(dce_call->conn);
2711 struct drsuapi_DsReplicaObjectIdentifier *untrusted_ncRoot;
2712 int ret;
2713 uint32_t i, k;
2714 struct dsdb_schema *schema;
2715 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
2716 struct getncchanges_repl_chunk *repl_chunk;
2717 NTSTATUS status;
2718 DATA_BLOB session_key;
2719 WERROR werr;
2720 struct dcesrv_handle *h;
2721 struct drsuapi_bind_state *b_state;
2722 struct drsuapi_getncchanges_state *getnc_state = NULL;
2723 struct drsuapi_DsGetNCChangesRequest10 *req10;
2724 uint32_t options;
2725 uint32_t link_count = 0;
2726 struct ldb_dn *search_dn = NULL;
2727 bool am_rodc;
2728 enum security_user_level security_level;
2729 struct ldb_context *sam_ctx;
2730 struct dom_sid *user_sid;
2731 bool is_secret_request;
2732 bool is_gc_pas_request;
2733 struct drsuapi_changed_objects *changes;
2734 bool has_get_all_changes = false;
2735 struct GUID invocation_id;
2736 static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
2737 struct dsdb_schema_prefixmap *pfm_remote = NULL;
2738 bool full = true;
2739 uint32_t *local_pas = NULL;
2740 struct ldb_dn *machine_dn = NULL; /* Only used for REPL SECRET EXOP */
2742 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
2743 b_state = h->data;
2745 /* sam_ctx_system is not present for non-administrator users */
2746 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
2748 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2750 *r->out.level_out = 6;
2752 r->out.ctr->ctr6.linked_attributes_count = 0;
2753 r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
2755 r->out.ctr->ctr6.object_count = 0;
2756 r->out.ctr->ctr6.nc_object_count = 0;
2757 r->out.ctr->ctr6.more_data = false;
2758 r->out.ctr->ctr6.uptodateness_vector = NULL;
2759 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2760 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2761 r->out.ctr->ctr6.first_object = NULL;
2763 /* Check request revision.
2765 switch (r->in.level) {
2766 case 8:
2767 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
2768 if (req10 == NULL) {
2769 return WERR_NOT_ENOUGH_MEMORY;
2771 break;
2772 case 10:
2773 req10 = &r->in.req->req10;
2774 break;
2775 default:
2776 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
2777 r->in.level));
2778 return WERR_REVISION_MISMATCH;
2781 repl_chunk = getncchanges_chunk_new(mem_ctx, dce_call, req10);
2783 if (repl_chunk == NULL) {
2784 return WERR_NOT_ENOUGH_MEMORY;
2787 /* a RODC doesn't allow for any replication */
2788 ret = samdb_rodc(sam_ctx, &am_rodc);
2789 if (ret == LDB_SUCCESS && am_rodc) {
2790 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
2791 return WERR_DS_DRA_SOURCE_DISABLED;
2795 * Help our tests pass by pre-checking the
2796 * destination_dsa_guid before the NC permissions. Info on
2797 * valid DSA GUIDs is not sensitive so this isn't a leak
2799 switch (req10->extended_op) {
2800 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
2801 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
2802 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
2803 case DRSUAPI_EXOP_FSMO_REQ_PDC:
2804 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
2806 const char *attrs[] = { NULL };
2808 ret = samdb_get_ntds_obj_by_guid(mem_ctx,
2809 sam_ctx,
2810 &req10->destination_dsa_guid,
2811 attrs,
2812 NULL);
2813 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2815 * Error out with an EXOP error but success at
2816 * the top level return value
2818 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
2819 return WERR_OK;
2820 } else if (ret != LDB_SUCCESS) {
2821 return WERR_DS_DRA_INTERNAL_ERROR;
2824 break;
2826 case DRSUAPI_EXOP_REPL_SECRET:
2827 case DRSUAPI_EXOP_REPL_OBJ:
2828 case DRSUAPI_EXOP_NONE:
2829 break;
2832 /* Perform access checks. */
2833 untrusted_ncRoot = req10->naming_context;
2834 if (untrusted_ncRoot == NULL) {
2835 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
2836 return WERR_DS_DRA_INVALID_PARAMETER;
2839 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
2840 return WERR_DS_DRA_INTERNAL_ERROR;
2843 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
2844 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
2845 return WERR_DS_DRA_SOURCE_DISABLED;
2848 user_sid = &session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
2851 * all clients must have GUID_DRS_GET_CHANGES. This finds the
2852 * actual NC root of the given value and checks that, allowing
2853 * REPL_OBJ to work safely
2855 werr = drs_security_access_check_nc_root(sam_ctx,
2856 mem_ctx,
2857 session_info->security_token,
2858 req10->naming_context,
2859 GUID_DRS_GET_CHANGES);
2861 if (W_ERROR_EQUAL(werr, WERR_DS_DRA_BAD_NC)) {
2863 * These extended operations need a different error if
2864 * the supplied DN can't be found
2866 switch (req10->extended_op) {
2867 case DRSUAPI_EXOP_REPL_OBJ:
2868 case DRSUAPI_EXOP_REPL_SECRET:
2869 return WERR_DS_DRA_BAD_DN;
2870 default:
2871 return werr;
2874 if (!W_ERROR_IS_OK(werr)) {
2875 return werr;
2878 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
2879 full = req10->partial_attribute_set == NULL &&
2880 req10->partial_attribute_set_ex == NULL;
2881 } else {
2882 full = (options & DRSUAPI_DRS_WRIT_REP) != 0;
2885 werr = dsdb_schema_pfm_from_drsuapi_pfm(&req10->mapping_ctr, true,
2886 mem_ctx, &pfm_remote, NULL);
2888 /* We were supplied a partial attribute set, without the prefix map! */
2889 if (!full && !W_ERROR_IS_OK(werr)) {
2890 if (req10->mapping_ctr.num_mappings == 0) {
2892 * Despite the fact MS-DRSR specifies that this shouldn't
2893 * happen, Windows RODCs will in fact not provide a prefixMap.
2895 DEBUG(5,(__location__ ": Failed to provide a remote prefixMap,"
2896 " falling back to local prefixMap\n"));
2897 } else {
2898 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
2899 win_errstr(werr)));
2900 return werr;
2904 /* allowed if the GC PAS and client has
2905 GUID_DRS_GET_FILTERED_ATTRIBUTES */
2906 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, pfm_remote, &is_gc_pas_request);
2907 if (!W_ERROR_IS_OK(werr)) {
2908 return werr;
2910 if (is_gc_pas_request) {
2911 werr = drs_security_access_check_nc_root(sam_ctx,
2912 mem_ctx,
2913 session_info->security_token,
2914 req10->naming_context,
2915 GUID_DRS_GET_FILTERED_ATTRIBUTES);
2916 if (W_ERROR_IS_OK(werr)) {
2917 goto allowed;
2921 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10,
2922 pfm_remote,
2923 &is_secret_request);
2924 if (!W_ERROR_IS_OK(werr)) {
2925 return werr;
2927 if (is_secret_request) {
2928 werr = drs_security_access_check_nc_root(sam_ctx,
2929 mem_ctx,
2930 session_info->security_token,
2931 req10->naming_context,
2932 GUID_DRS_GET_ALL_CHANGES);
2933 if (!W_ERROR_IS_OK(werr)) {
2934 /* Only bail if this is not a EXOP_REPL_SECRET */
2935 if (req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
2936 return werr;
2938 } else {
2939 has_get_all_changes = true;
2943 allowed:
2944 /* for non-administrator replications, check that they have
2945 given the correct source_dsa_invocation_id */
2946 security_level = security_session_user_level(session_info,
2947 samdb_domain_sid(sam_ctx));
2948 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
2949 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
2950 /* we rely on this flag being unset for RODC requests */
2951 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
2955 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
2956 /* Ignore the _in_ uptodateness vector*/
2957 req10->uptodateness_vector = NULL;
2960 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
2961 req10->source_dsa_invocation_id = invocation_id;
2964 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
2966 * The given highwatermark is only valid relative to the
2967 * specified source_dsa_invocation_id.
2969 ZERO_STRUCT(req10->highwatermark);
2973 * An extended operation is "special single-response cycle"
2974 * per MS-DRSR 4.1.10.1.1 "Start and Finish" so we don't need
2975 * to guess if this is a continuation of any long-term
2976 * state.
2978 * Otherwise, maintain (including marking as stale, which is
2979 * what the below is for) the replication state.
2981 * Note that point 5 "The server implementation MAY declare
2982 * the supplied values ... as too stale to use." would allow
2983 * resetting the state at almost any point, Microsoft Azure AD
2984 * Connect will switch back and forth between a REPL_OBJ and a
2985 * full replication, so we must not reset the statue during
2986 * extended operations.
2988 if (req10->extended_op == DRSUAPI_EXOP_NONE &&
2989 b_state->getncchanges_full_repl_state != NULL) {
2991 * Knowing that this is not an extended operation, we
2992 * can access (and validate) the full replication
2993 * state
2995 getnc_state = b_state->getncchanges_full_repl_state;
2998 /* see if a previous replication has been abandoned */
2999 if (getnc_state != NULL) {
3000 struct ldb_dn *new_dn;
3001 ret = drs_ObjectIdentifier_to_dn_and_nc_root(getnc_state,
3002 sam_ctx,
3003 untrusted_ncRoot,
3004 &new_dn,
3005 NULL);
3006 if (ret != LDB_SUCCESS) {
3008 * This can't fail as we have done this above
3009 * implicitly but not got the DN out, but
3010 * print a good error message regardless just
3011 * in case.
3013 DBG_ERR("Bad DN '%s' as Naming Context for GetNCChanges: %s\n",
3014 drs_ObjectIdentifier_to_debug_string(mem_ctx, untrusted_ncRoot),
3015 ldb_strerror(ret));
3016 return WERR_DS_DRA_INVALID_PARAMETER;
3018 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
3019 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
3020 ldb_dn_get_linearized(new_dn),
3021 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
3022 ldb_dn_get_linearized(getnc_state->last_dn)));
3023 TALLOC_FREE(getnc_state);
3024 b_state->getncchanges_full_repl_state = NULL;
3028 if (getnc_state != NULL) {
3029 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
3030 &req10->highwatermark);
3031 if (ret != 0) {
3032 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
3033 "on DN %s %s highwatermark (last_dn %s)\n",
3034 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
3035 (ret > 0) ? "older" : "newer",
3036 ldb_dn_get_linearized(getnc_state->last_dn)));
3037 TALLOC_FREE(getnc_state);
3038 b_state->getncchanges_full_repl_state = NULL;
3043 * This is either a new replication cycle, or an extended
3044 * operation. A new cycle is triggered above by the
3045 * TALLOC_FREE() which sets getnc_state to NULL.
3047 if (getnc_state == NULL) {
3048 struct ldb_result *res = NULL;
3049 const char *attrs[] = {
3050 "instanceType",
3051 "objectGuID",
3052 NULL
3054 uint32_t nc_instanceType;
3055 struct ldb_dn *ncRoot_dn;
3057 ret = drs_ObjectIdentifier_to_dn_and_nc_root(mem_ctx,
3058 sam_ctx,
3059 untrusted_ncRoot,
3060 &ncRoot_dn,
3061 NULL);
3062 if (ret != LDB_SUCCESS) {
3063 DBG_ERR("Bad DN '%s' as Naming Context or EXOP DN for GetNCChanges: %s\n",
3064 drs_ObjectIdentifier_to_debug_string(mem_ctx, untrusted_ncRoot),
3065 ldb_strerror(ret));
3066 return WERR_DS_DRA_BAD_DN;
3069 ret = dsdb_search_dn(sam_ctx, mem_ctx, &res,
3070 ncRoot_dn, attrs,
3071 DSDB_SEARCH_SHOW_DELETED |
3072 DSDB_SEARCH_SHOW_RECYCLED);
3073 if (ret != LDB_SUCCESS) {
3074 DBG_WARNING("Failed to find ncRoot_dn %s\n",
3075 ldb_dn_get_linearized(ncRoot_dn));
3076 return WERR_DS_DRA_BAD_DN;
3078 nc_instanceType = ldb_msg_find_attr_as_int(res->msgs[0],
3079 "instanceType",
3082 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
3083 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
3087 * This is the first replication cycle and it is
3088 * a good place to handle extended operations
3090 * FIXME: we don't fully support extended operations yet
3092 switch (req10->extended_op) {
3093 case DRSUAPI_EXOP_NONE:
3094 if ((nc_instanceType & INSTANCE_TYPE_IS_NC_HEAD) == 0) {
3095 const char *dn_str
3096 = ldb_dn_get_linearized(ncRoot_dn);
3098 DBG_NOTICE("Rejecting full replication on "
3099 "not NC %s\n", dn_str);
3101 return WERR_DS_CANT_FIND_EXPECTED_NC;
3104 break;
3105 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
3106 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
3107 W_ERROR_NOT_OK_RETURN(werr);
3108 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
3109 return WERR_OK;
3111 break;
3112 case DRSUAPI_EXOP_REPL_SECRET:
3113 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
3114 user_sid,
3115 &r->out.ctr->ctr6,
3116 has_get_all_changes,
3117 &machine_dn);
3118 r->out.result = werr;
3119 W_ERROR_NOT_OK_RETURN(werr);
3120 break;
3121 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
3122 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
3123 W_ERROR_NOT_OK_RETURN(werr);
3124 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
3125 return WERR_OK;
3127 break;
3128 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
3129 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
3130 W_ERROR_NOT_OK_RETURN(werr);
3131 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
3132 return WERR_OK;
3134 break;
3135 case DRSUAPI_EXOP_FSMO_REQ_PDC:
3136 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
3137 W_ERROR_NOT_OK_RETURN(werr);
3138 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
3139 return WERR_OK;
3141 break;
3142 case DRSUAPI_EXOP_REPL_OBJ:
3143 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
3144 r->out.result = werr;
3145 W_ERROR_NOT_OK_RETURN(werr);
3146 break;
3148 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
3150 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
3151 (unsigned)req10->extended_op));
3152 return WERR_DS_DRA_NOT_SUPPORTED;
3156 * Initialize the state, initially for the remainder
3157 * of this call (EXOPs)
3159 * An extended operation is a "special single-response
3160 * cycle" per MS-DRSR 4.1.10.1.1 "Start and Finish"
3163 getnc_state = talloc_zero(mem_ctx, struct drsuapi_getncchanges_state);
3164 if (getnc_state == NULL) {
3165 return WERR_NOT_ENOUGH_MEMORY;
3168 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
3170 * Promote the memory to being a store of
3171 * long-term state that we will use over the
3172 * replication cycle for full replication
3173 * requests
3175 * Store the state in a clearly named location
3176 * for pulling back only during full
3177 * replications
3179 b_state->getncchanges_full_repl_state
3180 = talloc_steal(b_state, getnc_state);
3183 getnc_state->ncRoot_dn = ncRoot_dn;
3184 talloc_steal(getnc_state, ncRoot_dn);
3186 getnc_state->ncRoot_guid = samdb_result_guid(res->msgs[0],
3187 "objectGUID");
3189 /* find out if we are to replicate Schema NC */
3190 ret = ldb_dn_compare_base(ldb_get_schema_basedn(sam_ctx),
3191 ncRoot_dn);
3192 getnc_state->is_schema_nc = (0 == ret);
3194 TALLOC_FREE(res);
3197 /* we need the session key for encrypting password attributes */
3198 status = dcesrv_auth_session_key(dce_call, &session_key);
3199 if (!NT_STATUS_IS_OK(status)) {
3200 DEBUG(0,(__location__ ": Failed to get session key\n"));
3201 return WERR_DS_DRA_INTERNAL_ERROR;
3205 TODO: MS-DRSR section 4.1.10.1.1
3206 Work out if this is the start of a new cycle */
3208 if (getnc_state->guids == NULL) {
3209 const char *extra_filter;
3210 struct ldb_result *search_res = NULL;
3211 static const struct drsuapi_DsReplicaCursorCtrEx empty_udv;
3212 const struct drsuapi_DsReplicaCursorCtrEx *udv = NULL;
3214 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
3216 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
3217 if (req10->uptodateness_vector != NULL) {
3218 udv = req10->uptodateness_vector;
3219 } else {
3220 udv = &empty_udv;
3223 getnc_state->min_usn = req10->highwatermark.highest_usn;
3224 for (i = 0; i < udv->count; i++) {
3225 bool match;
3226 const struct drsuapi_DsReplicaCursor *cur =
3227 &udv->cursors[i];
3229 match = GUID_equal(&invocation_id,
3230 &cur->source_dsa_invocation_id);
3231 if (!match) {
3232 continue;
3234 if (cur->highest_usn > getnc_state->min_usn) {
3235 getnc_state->min_usn = cur->highest_usn;
3237 break;
3239 } else {
3240 /* We do not want REPL_SECRETS or REPL_SINGLE to return empty-handed */
3241 udv = &empty_udv;
3242 getnc_state->min_usn = 0;
3245 getnc_state->max_usn = getnc_state->min_usn;
3247 getnc_state->final_udv = talloc_zero(getnc_state,
3248 struct drsuapi_DsReplicaCursor2CtrEx);
3249 if (getnc_state->final_udv == NULL) {
3250 return WERR_NOT_ENOUGH_MEMORY;
3252 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
3253 getnc_state->final_udv);
3254 if (!W_ERROR_IS_OK(werr)) {
3255 return werr;
3258 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
3259 werr = getncchanges_collect_objects(b_state, mem_ctx,
3260 getnc_state, req10,
3261 search_dn, extra_filter,
3262 &search_res);
3263 } else {
3264 werr = getncchanges_collect_objects_exop(b_state, mem_ctx,
3265 getnc_state, req10,
3266 &r->out.ctr->ctr6,
3267 search_dn, extra_filter,
3268 &search_res);
3270 W_ERROR_NOT_OK_RETURN(werr);
3272 /* extract out the GUIDs list */
3273 getnc_state->num_records = search_res ? search_res->count : 0;
3274 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
3275 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
3277 changes = talloc_array(getnc_state,
3278 struct drsuapi_changed_objects,
3279 getnc_state->num_records);
3280 W_ERROR_HAVE_NO_MEMORY(changes);
3282 for (i=0; i<getnc_state->num_records; i++) {
3283 changes[i].dn = search_res->msgs[i]->dn;
3284 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
3285 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
3287 if (changes[i].usn > getnc_state->max_usn) {
3288 getnc_state->max_usn = changes[i].usn;
3291 if (req10->extended_op == DRSUAPI_EXOP_NONE &&
3292 GUID_equal(&changes[i].guid, &getnc_state->ncRoot_guid))
3294 getnc_state->send_nc_root_first = true;
3298 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
3299 getnc_state->is_get_anc =
3300 ((req10->replica_flags & DRSUAPI_DRS_GET_ANC) != 0);
3301 if (getnc_state->is_get_anc
3302 && lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
3303 NULL,
3304 "drs",
3305 "broken_samba_4.5_get_anc_emulation",
3306 false)) {
3307 getnc_state->broken_samba_4_5_get_anc_emulation = true;
3309 if (lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
3310 NULL,
3311 "drs",
3312 "get_tgt_support",
3313 true)) {
3314 getnc_state->is_get_tgt =
3315 ((req10->more_flags & DRSUAPI_DRS_GET_TGT) != 0);
3319 /* RID_ALLOC returns 3 objects in a fixed order */
3320 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
3321 /* Do nothing */
3322 } else if (getnc_state->broken_samba_4_5_get_anc_emulation) {
3323 TYPESAFE_QSORT(changes,
3324 getnc_state->num_records,
3325 site_res_cmp_anc_order);
3326 } else {
3327 TYPESAFE_QSORT(changes,
3328 getnc_state->num_records,
3329 site_res_cmp_usn_order);
3332 for (i=0; i < getnc_state->num_records; i++) {
3333 getnc_state->guids[i] = changes[i].guid;
3334 if (GUID_all_zero(&getnc_state->guids[i])) {
3335 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
3336 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
3337 return WERR_DS_DRA_INTERNAL_ERROR;
3341 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
3342 getnc_state->final_hwm.reserved_usn = 0;
3343 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
3345 talloc_free(search_res);
3346 talloc_free(changes);
3349 * when using GET_ANC or GET_TGT, cache the objects that have
3350 * been already sent, to avoid sending them multiple times
3352 if (getnc_state->is_get_anc || getnc_state->is_get_tgt) {
3353 DEBUG(3,("Using object cache, GET_ANC %u, GET_TGT %u\n",
3354 getnc_state->is_get_anc,
3355 getnc_state->is_get_tgt));
3357 getnc_state->obj_cache = db_open_rbt(getnc_state);
3358 if (getnc_state->obj_cache == NULL) {
3359 return WERR_NOT_ENOUGH_MEMORY;
3364 if (req10->uptodateness_vector) {
3365 /* make sure its sorted */
3366 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
3367 req10->uptodateness_vector->count,
3368 drsuapi_DsReplicaCursor_compare);
3371 /* Prefix mapping */
3372 schema = dsdb_get_schema(sam_ctx, mem_ctx);
3373 if (!schema) {
3374 DEBUG(0,("No schema in sam_ctx\n"));
3375 return WERR_DS_DRA_INTERNAL_ERROR;
3378 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
3379 if (r->out.ctr->ctr6.naming_context == NULL) {
3380 return WERR_NOT_ENOUGH_MEMORY;
3384 * Match Windows and echo back the original values from the request, even if
3385 * they say DummyDN for the string NC
3387 *r->out.ctr->ctr6.naming_context = *untrusted_ncRoot;
3389 /* find the SID if there is one */
3390 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
3392 /* Set GUID */
3393 r->out.ctr->ctr6.naming_context->guid = getnc_state->ncRoot_guid;
3395 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
3396 r->out.ctr->ctr6.mapping_ctr = *ctr;
3398 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
3399 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
3401 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
3402 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
3405 * If the client has already set GET_TGT then we know they can handle
3406 * receiving the linked attributes interleaved with the source objects
3408 if (getnc_state->is_get_tgt) {
3409 repl_chunk->immediate_link_sync = true;
3412 if (req10->partial_attribute_set != NULL) {
3413 struct dsdb_syntax_ctx syntax_ctx;
3414 uint32_t j = 0;
3416 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
3417 syntax_ctx.pfm_remote = pfm_remote;
3419 local_pas = talloc_array(b_state, uint32_t, req10->partial_attribute_set->num_attids);
3421 for (j = 0; j < req10->partial_attribute_set->num_attids; j++) {
3422 getncchanges_attid_remote_to_local(schema,
3423 &syntax_ctx,
3424 req10->partial_attribute_set->attids[j],
3425 (enum drsuapi_DsAttributeId *)&local_pas[j],
3426 NULL);
3429 TYPESAFE_QSORT(local_pas,
3430 req10->partial_attribute_set->num_attids,
3431 uint32_t_ptr_cmp);
3435 * If we have the NC root in this replication, send it
3436 * first regardless. However, don't bump the USN now,
3437 * treat it as if it was sent early due to GET_ANC
3439 * This is triggered for each call, so every page of responses
3440 * gets the NC root as the first object, up to the point where
3441 * it naturally occurs in the replication.
3444 if (getnc_state->send_nc_root_first) {
3445 struct drsuapi_DsReplicaObjectListItemEx *new_objs = NULL;
3447 werr = getncchanges_add_ancestors(&getnc_state->ncRoot_guid,
3448 NULL, mem_ctx,
3449 sam_ctx, getnc_state,
3450 schema, &session_key,
3451 req10, local_pas,
3452 machine_dn, &new_objs);
3454 if (!W_ERROR_IS_OK(werr)) {
3455 return werr;
3458 getncchanges_chunk_add_objects(repl_chunk, new_objs);
3460 DEBUG(8,(__location__ ": replicating NC root %s\n",
3461 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
3465 * Check in case we're still processing the links from an object in the
3466 * previous chunk. We want to send the links (and any targets needed)
3467 * before moving on to the next object.
3469 if (getnc_state->is_get_tgt) {
3470 werr = getncchanges_chunk_add_la_targets(repl_chunk,
3471 getnc_state,
3472 getnc_state->la_idx,
3473 mem_ctx, sam_ctx,
3474 schema, &session_key,
3475 req10, local_pas,
3476 machine_dn);
3478 if (!W_ERROR_IS_OK(werr)) {
3479 return werr;
3483 for (i=getnc_state->num_processed;
3484 i<getnc_state->num_records &&
3485 !getncchanges_chunk_is_full(repl_chunk, getnc_state);
3486 i++) {
3487 struct drsuapi_DsReplicaObjectListItemEx *new_objs = NULL;
3488 struct ldb_message *msg;
3489 static const char * const msg_attrs[] = {
3490 "*",
3491 "nTSecurityDescriptor",
3492 "parentGUID",
3493 "replPropertyMetaData",
3494 DSDB_SECRET_ATTRIBUTES,
3495 NULL };
3496 struct ldb_result *msg_res;
3497 struct ldb_dn *msg_dn;
3498 bool obj_already_sent = false;
3499 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3500 uint32_t old_la_index;
3503 * Once we get to the 'natural' place to send the NC
3504 * root, stop sending it at the front of each reply
3505 * and make sure to suppress sending it now
3507 * We don't just 'continue' here as we must send links
3508 * and unlike Windows we want to update the
3509 * tmp_highest_usn
3512 if (getnc_state->send_nc_root_first &&
3513 GUID_equal(&getnc_state->guids[i], &getnc_state->ncRoot_guid))
3515 getnc_state->send_nc_root_first = false;
3516 obj_already_sent = true;
3519 msg_dn = ldb_dn_new_fmt(tmp_ctx, sam_ctx, "<GUID=%s>",
3520 GUID_string(tmp_ctx, &getnc_state->guids[i]));
3521 W_ERROR_HAVE_NO_MEMORY(msg_dn);
3524 * by re-searching here we avoid having a lot of full
3525 * records in memory between calls to getncchanges.
3527 * We expect that we may get some objects that vanish
3528 * (tombstone expunge) between the first and second
3529 * check.
3531 ret = drsuapi_search_with_extended_dn(sam_ctx, tmp_ctx, &msg_res,
3532 msg_dn,
3533 LDB_SCOPE_BASE, msg_attrs, NULL);
3534 if (ret != LDB_SUCCESS) {
3535 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
3536 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
3537 ldb_dn_get_extended_linearized(tmp_ctx, msg_dn, 1),
3538 ldb_errstring(sam_ctx)));
3540 TALLOC_FREE(tmp_ctx);
3541 continue;
3544 if (msg_res->count == 0) {
3545 DEBUG(1,("getncchanges: got LDB_SUCCESS but failed"
3546 "to get any results in fetch of DN "
3547 "%s (race with tombstone expunge?)\n",
3548 ldb_dn_get_extended_linearized(tmp_ctx,
3549 msg_dn, 1)));
3550 TALLOC_FREE(tmp_ctx);
3551 continue;
3554 msg = msg_res->msgs[0];
3557 * Check if we've already sent the object as an ancestor of
3558 * another object. If so, we don't need to send it again
3560 if (getnc_state->obj_cache != NULL) {
3561 werr = dcesrv_drsuapi_obj_cache_exists(getnc_state->obj_cache,
3562 &getnc_state->guids[i]);
3563 if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
3564 obj_already_sent = true;
3568 if (!obj_already_sent) {
3569 bool max_wait_reached;
3571 max_wait_reached = getncchanges_chunk_timed_out(repl_chunk);
3574 * Construct an object, ready to send (this will include
3575 * the object's ancestors as well, if needed)
3577 werr = getncchanges_get_obj_to_send(msg, mem_ctx, sam_ctx,
3578 getnc_state, schema,
3579 &session_key, req10,
3580 max_wait_reached,
3581 local_pas, machine_dn,
3582 &getnc_state->guids[i],
3583 &new_objs);
3584 if (!W_ERROR_IS_OK(werr)) {
3585 return werr;
3589 old_la_index = getnc_state->la_count;
3592 * We've reached the USN where this object naturally occurs.
3593 * Regardless of whether we've already sent the object (as an
3594 * ancestor), we add its links and update the HWM at this point
3596 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
3597 getnc_state->is_schema_nc,
3598 schema, getnc_state->min_usn,
3599 req10->replica_flags,
3600 msg,
3601 &getnc_state->la_list,
3602 &getnc_state->la_count,
3603 req10->uptodateness_vector);
3604 if (!W_ERROR_IS_OK(werr)) {
3605 return werr;
3608 dcesrv_drsuapi_update_highwatermark(msg,
3609 getnc_state->max_usn,
3610 &r->out.ctr->ctr6.new_highwatermark);
3612 if (new_objs != NULL) {
3615 * Add the object (and, if GET_ANC, any parents it may
3616 * have) into the current chunk of replication data
3618 getncchanges_chunk_add_objects(repl_chunk, new_objs);
3620 talloc_free(getnc_state->last_dn);
3622 * talloc_steal() as we still need msg->dn to
3623 * be a valid pointer for the log on the next
3624 * line.
3626 * msg only remains in scope for the next 25
3627 * lines or so anyway.
3629 getnc_state->last_dn = talloc_steal(getnc_state, msg->dn);
3632 DEBUG(8,(__location__ ": %s object %s new tmp_highest_usn=%" PRIu64 "\n",
3633 new_objs ? "replicating" : "skipping send of",
3634 ldb_dn_get_linearized(msg->dn),
3635 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn));
3637 getnc_state->total_links += (getnc_state->la_count - old_la_index);
3640 * If the GET_TGT flag was set, check any new links added to
3641 * make sure the client knows about the link target object
3643 if (getnc_state->is_get_tgt) {
3644 werr = getncchanges_chunk_add_la_targets(repl_chunk,
3645 getnc_state,
3646 old_la_index,
3647 mem_ctx, sam_ctx,
3648 schema, &session_key,
3649 req10, local_pas,
3650 machine_dn);
3652 if (!W_ERROR_IS_OK(werr)) {
3653 return werr;
3657 TALLOC_FREE(tmp_ctx);
3660 /* copy the constructed object list into the response message */
3661 r->out.ctr->ctr6.object_count = repl_chunk->object_count;
3662 r->out.ctr->ctr6.first_object = repl_chunk->object_list;
3664 getnc_state->num_processed = i;
3666 if (i < getnc_state->num_records) {
3667 r->out.ctr->ctr6.more_data = true;
3670 /* the client can us to call UpdateRefs on its behalf to
3671 re-establish monitoring of the NC */
3672 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
3673 !GUID_all_zero(&req10->destination_dsa_guid)) {
3674 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
3675 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
3676 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
3679 * We pass the pre-validation NC root here as
3680 * drsuapi_UpdateRefs() has to check its own input
3681 * values due to being called from
3682 * dcesrv_drsuapi_DsReplicaUpdateRefs()
3685 ureq.naming_context = untrusted_ncRoot;
3686 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(sam_ctx, mem_ctx,
3687 &req10->destination_dsa_guid);
3688 if (!ureq.dest_dsa_dns_name) {
3689 return WERR_NOT_ENOUGH_MEMORY;
3691 ureq.dest_dsa_guid = req10->destination_dsa_guid;
3692 ureq.options = DRSUAPI_DRS_ADD_REF |
3693 DRSUAPI_DRS_ASYNC_OP |
3694 DRSUAPI_DRS_GETCHG_CHECK;
3696 /* we also need to pass through the
3697 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
3698 to send notifies using the GC SPN */
3699 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
3701 werr = drsuapi_UpdateRefs(imsg_ctx,
3702 dce_call->event_ctx,
3703 b_state,
3704 mem_ctx,
3705 &ureq);
3706 if (!W_ERROR_IS_OK(werr)) {
3707 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
3708 drs_ObjectIdentifier_to_debug_string(mem_ctx, untrusted_ncRoot),
3709 ureq.dest_dsa_dns_name,
3710 win_errstr(werr)));
3715 * Work out how many links we can send in this chunk. The default is to
3716 * send all the links last, but there is a config option to send them
3717 * immediately, in the same chunk as their source object
3719 if (!r->out.ctr->ctr6.more_data || repl_chunk->immediate_link_sync) {
3720 link_count = getncchanges_chunk_links_pending(repl_chunk,
3721 getnc_state);
3722 link_count = MIN(link_count,
3723 getncchanges_chunk_max_links(repl_chunk));
3726 /* If we've got linked attributes to send, add them now */
3727 if (link_count > 0) {
3728 struct la_for_sorting *la_sorted;
3731 * Grab a chunk of linked attributes off the list and put them
3732 * in sorted array, ready to send
3734 werr = getncchanges_get_sorted_array(&getnc_state->la_list[getnc_state->la_idx],
3735 link_count,
3736 sam_ctx, getnc_state,
3737 schema,
3738 &la_sorted);
3739 if (!W_ERROR_IS_OK(werr)) {
3740 return werr;
3743 r->out.ctr->ctr6.linked_attributes_count = link_count;
3744 r->out.ctr->ctr6.linked_attributes = talloc_array(r->out.ctr, struct drsuapi_DsReplicaLinkedAttribute, link_count);
3745 if (r->out.ctr->ctr6.linked_attributes == NULL) {
3746 DEBUG(0, ("Out of memory allocating %u linked attributes for output\n", link_count));
3747 return WERR_NOT_ENOUGH_MEMORY;
3750 for (k = 0; k < link_count; k++) {
3751 r->out.ctr->ctr6.linked_attributes[k] = *la_sorted[k].link;
3754 getnc_state->la_idx += link_count;
3755 getnc_state->links_given += link_count;
3757 if (getnc_state->la_idx < getnc_state->la_count) {
3758 r->out.ctr->ctr6.more_data = true;
3759 } else {
3762 * We've now sent all the links seen so far, so we can
3763 * reset la_list back to an empty list again. Note that
3764 * the steal means the linked attribute memory gets
3765 * freed after this RPC message is sent on the wire.
3767 talloc_steal(mem_ctx, getnc_state->la_list);
3768 getnc_state->la_list = NULL;
3769 getnc_state->la_idx = 0;
3770 getnc_state->la_count = 0;
3773 TALLOC_FREE(la_sorted);
3776 if (req10->replica_flags & DRSUAPI_DRS_GET_NC_SIZE) {
3778 * TODO: This implementation is wrong
3779 * we should find out the total number of
3780 * objects and links in the whole naming context
3781 * at the start of the cycle and return these
3782 * values in each message.
3784 * For now we keep our current strategy and return
3785 * the number of objects for this cycle and the number
3786 * of links we found so far during the cycle.
3788 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
3789 r->out.ctr->ctr6.nc_linked_attributes_count = getnc_state->total_links;
3792 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
3793 r->out.ctr->ctr6.uptodateness_vector = NULL;
3794 r->out.ctr->ctr6.nc_object_count = 0;
3795 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
3796 } else if (!r->out.ctr->ctr6.more_data) {
3798 /* this is the last response in the replication cycle */
3799 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
3800 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
3801 &getnc_state->final_udv);
3804 * Free the state info stored for the replication cycle. Note
3805 * that the RPC message we're sending contains links stored in
3806 * getnc_state. mem_ctx is local to this RPC call, so the memory
3807 * will get freed after the RPC message is sent on the wire.
3809 * We must not do this for an EXOP, as that should not
3810 * end the replication state, which is why that is
3811 * checked first above.
3813 talloc_steal(mem_ctx, getnc_state);
3814 b_state->getncchanges_full_repl_state = NULL;
3815 } else {
3816 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
3817 &r->out.ctr->ctr6.new_highwatermark);
3818 if (ret == 0) {
3820 * We need to make sure that we never return the
3821 * same highwatermark within the same replication
3822 * cycle more than once. Otherwise we cannot detect
3823 * when the client uses an unexpected highwatermark.
3825 * This is a HACK which is needed because our
3826 * object ordering is wrong and set tmp_highest_usn
3827 * to a value that is higher than what we already
3828 * sent to the client (destination dsa).
3830 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
3833 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
3836 TALLOC_FREE(repl_chunk);
3838 DEBUG(r->out.ctr->ctr6.more_data?4:2,
3839 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
3840 (unsigned long long)(req10->highwatermark.highest_usn+1),
3841 req10->replica_flags,
3842 drs_ObjectIdentifier_to_debug_string(mem_ctx, untrusted_ncRoot),
3843 r->out.ctr->ctr6.object_count,
3844 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
3845 r->out.ctr->ctr6.linked_attributes_count,
3846 getnc_state->links_given, getnc_state->total_links,
3847 dom_sid_string(mem_ctx, user_sid)));
3849 #if 0
3850 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
3851 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
3853 #endif
3855 return WERR_OK;