2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009-2010
8 Copyright (C) Andrew Bartlett 2010-2016
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "rpc_server/dcerpc_server.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "param/param.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "libcli/security/security.h"
32 #include "libcli/security/session.h"
33 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
34 #include "rpc_server/dcerpc_server_proto.h"
35 #include "../libcli/drsuapi/drsuapi.h"
36 #include "lib/util/binsearch.h"
37 #include "lib/util/tsort.h"
38 #include "auth/session.h"
39 #include "dsdb/common/util.h"
40 #include "lib/dbwrap/dbwrap.h"
41 #include "lib/dbwrap/dbwrap_rbt.h"
42 #include "librpc/gen_ndr/ndr_misc.h"
44 /* state of a partially completed getncchanges call */
45 struct drsuapi_getncchanges_state {
46 struct db_context *anc_cache;
49 uint32_t num_processed;
50 struct ldb_dn *ncRoot_dn;
51 struct GUID ncRoot_guid;
55 struct drsuapi_DsReplicaHighWaterMark last_hwm;
56 struct ldb_dn *last_dn;
57 struct drsuapi_DsReplicaHighWaterMark final_hwm;
58 struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
59 struct drsuapi_DsReplicaLinkedAttribute *la_list;
61 struct la_for_sorting *la_sorted;
65 /* We must keep the GUIDs in NDR form for sorting */
66 struct la_for_sorting {
67 struct drsuapi_DsReplicaLinkedAttribute *link;
68 uint8_t target_guid[16];
69 uint8_t source_guid[16];
72 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
73 const struct drsuapi_DsReplicaHighWaterMark *h2)
75 if (h1->highest_usn < h2->highest_usn) {
77 } else if (h1->highest_usn > h2->highest_usn) {
79 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
81 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
83 } else if (h1->reserved_usn < h2->reserved_usn) {
85 } else if (h1->reserved_usn > h2->reserved_usn) {
93 build a DsReplicaObjectIdentifier from a ldb msg
95 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
96 const struct ldb_message *msg)
98 struct drsuapi_DsReplicaObjectIdentifier *identifier;
101 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
102 if (identifier == NULL) {
106 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
107 identifier->guid = samdb_result_guid(msg, "objectGUID");
109 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
111 identifier->sid = *sid;
113 ZERO_STRUCT(identifier->sid);
118 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
120 return GUID_compare(guid1, &guid2);
124 see if we can filter an attribute using the uptodateness_vector
126 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
127 const struct GUID *originating_invocation_id,
128 uint64_t originating_usn)
130 const struct drsuapi_DsReplicaCursor *c;
131 if (udv == NULL) return false;
132 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
133 originating_invocation_id, udv_compare, c);
134 if (c && originating_usn <= c->highest_usn) {
141 static int uint32_t_cmp(uint32_t a1, uint32_t a2)
143 if (a1 == a2) return 0;
144 return a1 > a2 ? 1 : -1;
147 static int uint32_t_ptr_cmp(uint32_t *a1, uint32_t *a2, void *unused)
149 if (*a1 == *a2) return 0;
150 return *a1 > *a2 ? 1 : -1;
153 static WERROR getncchanges_attid_remote_to_local(const struct dsdb_schema *schema,
154 const struct dsdb_syntax_ctx *ctx,
155 enum drsuapi_DsAttributeId remote_attid_as_enum,
156 enum drsuapi_DsAttributeId *local_attid_as_enum,
157 const struct dsdb_attribute **_sa)
160 const struct dsdb_attribute *sa = NULL;
162 if (ctx->pfm_remote == NULL) {
163 DEBUG(7, ("No prefixMap supplied, falling back to local prefixMap.\n"));
167 werr = dsdb_attribute_drsuapi_remote_to_local(ctx,
168 remote_attid_as_enum,
171 if (!W_ERROR_IS_OK(werr)) {
172 DEBUG(3, ("WARNING: Unable to resolve remote attid, falling back to local prefixMap.\n"));
179 sa = dsdb_attribute_by_attributeID_id(schema, remote_attid_as_enum);
181 return WERR_DS_DRA_SCHEMA_MISMATCH;
183 if (local_attid_as_enum != NULL) {
184 *local_attid_as_enum = sa->attributeID_id;
194 drsuapi_DsGetNCChanges for one object
196 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
197 const struct ldb_message *msg,
198 struct ldb_context *sam_ctx,
199 struct ldb_dn *ncRoot_dn,
201 struct dsdb_schema *schema,
202 DATA_BLOB *session_key,
203 uint64_t highest_usn,
204 uint32_t replica_flags,
205 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
206 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
207 enum drsuapi_DsExtendedOperation extended_op,
208 bool force_object_return,
211 const struct ldb_val *md_value;
213 struct replPropertyMetaDataBlob md;
215 enum ndr_err_code ndr_err;
218 const struct dsdb_attribute *rdn_sa;
220 unsigned int instanceType;
221 struct dsdb_syntax_ctx syntax_ctx;
223 /* make dsdb sytanx context for conversions */
224 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
225 syntax_ctx.is_schema_nc = is_schema_nc;
227 uSNChanged = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
228 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
229 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
230 obj->is_nc_prefix = true;
231 obj->parent_object_guid = NULL;
233 obj->is_nc_prefix = false;
234 obj->parent_object_guid = talloc(obj, struct GUID);
235 if (obj->parent_object_guid == NULL) {
236 return WERR_DS_DRA_INTERNAL_ERROR;
238 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
239 if (GUID_all_zero(obj->parent_object_guid)) {
240 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
241 ldb_dn_get_linearized(msg->dn)));
242 return WERR_DS_DRA_INTERNAL_ERROR;
245 obj->next_object = NULL;
247 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
249 /* nothing to send */
253 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
254 /* don't send uninstantiated objects */
258 if (uSNChanged <= highest_usn) {
259 /* nothing to send */
263 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
264 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
265 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
266 return WERR_DS_DRA_INTERNAL_ERROR;
269 if (md.version != 1) {
270 return WERR_DS_DRA_INTERNAL_ERROR;
273 rdn = ldb_dn_get_rdn_name(msg->dn);
275 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
276 return WERR_DS_DRA_INTERNAL_ERROR;
279 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
280 if (rdn_sa == NULL) {
281 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
282 rdn, ldb_dn_get_linearized(msg->dn)));
283 return WERR_DS_DRA_INTERNAL_ERROR;
286 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
287 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
289 obj->object.identifier = get_object_identifier(obj, msg);
290 if (obj->object.identifier == NULL) {
291 return WERR_NOT_ENOUGH_MEMORY;
293 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
295 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
296 for (n=i=0; i<md.ctr.ctr1.count; i++) {
297 const struct dsdb_attribute *sa;
298 bool force_attribute = false;
300 /* if the attribute has not changed, and it is not the
301 instanceType then don't include it */
302 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
303 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
304 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
306 /* don't include the rDN */
307 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
309 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
311 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
312 (unsigned int)md.ctr.ctr1.array[i].attid,
313 ldb_dn_get_linearized(msg->dn)));
314 return WERR_DS_DRA_INTERNAL_ERROR;
318 struct ldb_message_element *el;
319 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
320 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
321 /* don't send upgraded links inline */
326 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
327 !dsdb_attr_in_rodc_fas(sa)) {
328 force_attribute = true;
329 DEBUG(4,("Forcing attribute %s in %s\n",
330 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
333 /* filter by uptodateness_vector */
334 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
336 udv_filter(uptodateness_vector,
337 &md.ctr.ctr1.array[i].originating_invocation_id,
338 md.ctr.ctr1.array[i].originating_usn)) {
342 /* filter by partial_attribute_set */
343 if (partial_attribute_set) {
344 uint32_t *result = NULL;
345 BINARY_ARRAY_SEARCH_V(local_pas, partial_attribute_set->num_attids, sa->attributeID_id,
346 uint32_t_cmp, result);
347 if (result == NULL) {
352 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
353 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
354 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
355 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
356 attids[n] = md.ctr.ctr1.array[i].attid;
360 /* ignore it if its an empty change. Note that renames always
361 * change the 'name' attribute, so they won't be ignored by
364 * the force_object_return check is used to force an empty
365 * object return when we timeout in the getncchanges loop.
366 * This allows us to return an empty object, which keeps the
367 * client happy while preventing timeouts
371 attids[0] == DRSUAPI_ATTID_instanceType &&
372 !force_object_return)) {
373 talloc_free(obj->meta_data_ctr);
374 obj->meta_data_ctr = NULL;
378 obj->meta_data_ctr->count = n;
380 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
381 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
382 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
383 obj->object.attribute_ctr.num_attributes);
384 if (obj->object.attribute_ctr.attributes == NULL) {
385 return WERR_NOT_ENOUGH_MEMORY;
389 * Note that the meta_data array and the attributes array must
390 * be the same size and in the same order
392 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
393 struct ldb_message_element *el;
395 const struct dsdb_attribute *sa;
397 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
399 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
400 return WERR_DS_DRA_INTERNAL_ERROR;
403 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
405 /* this happens for attributes that have been removed */
406 DEBUG(5,("No element '%s' for attributeID %u in message\n",
407 sa->lDAPDisplayName, attids[i]));
408 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
409 obj->object.attribute_ctr.attributes[i].attid =
410 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
412 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
413 &obj->object.attribute_ctr.attributes[i]);
414 if (!W_ERROR_IS_OK(werr)) {
415 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
416 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
420 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
421 * check if attribute is secret and send a null value
423 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
424 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
425 &obj->meta_data_ctr->meta_data[i]);
427 /* some attributes needs to be encrypted
429 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
430 &obj->object.attribute_ctr.attributes[i]);
431 if (!W_ERROR_IS_OK(werr)) {
432 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
433 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
438 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
439 DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID: "
443 ldb_dn_get_linearized(msg->dn),
445 obj->object.attribute_ctr.attributes[i].attid));
446 return WERR_DS_DATABASE_ERROR;
454 add one linked attribute from an object to the list of linked
455 attributes in a getncchanges request
457 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
458 struct ldb_context *sam_ctx,
459 const struct dsdb_schema *schema,
460 const struct dsdb_attribute *sa,
461 const struct ldb_message *msg,
462 struct dsdb_dn *dsdb_dn,
463 struct drsuapi_DsReplicaLinkedAttribute **la_list,
467 struct drsuapi_DsReplicaLinkedAttribute *la;
472 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
473 W_ERROR_HAVE_NO_MEMORY(*la_list);
475 la = &(*la_list)[*la_count];
477 la->identifier = get_object_identifier(*la_list, msg);
478 W_ERROR_HAVE_NO_MEMORY(la->identifier);
480 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
483 /* We have to check that the inactive link still point to an existing object */
489 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
490 if (strncmp(v, "TRUE", 4) == 0) {
492 * Note: we skip the transmition of the deleted link even if the other part used to
493 * know about it because when we transmit the deletion of the object, the link will
494 * be deleted too due to deletion of object where link points and Windows do so.
496 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
497 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
499 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
500 * if it join an existing domain with deleted objets, it firsts impose to have a
501 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
502 * either during initial replication or after the getNCChanges.
503 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
505 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
506 * If FL >=2K8R2 we are sure that this attribute will be here.
507 * For this kind of forest level we do not return the link if the object is recycled
508 * (isRecycled = true).
510 if (strncmp(v, "TRUE", 4) == 0) {
511 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
512 ldb_dn_get_linearized(msg->dn)));
519 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
520 if (!NT_STATUS_IS_OK(status)) {
521 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
522 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
523 return ntstatus_to_werror(status);
525 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
526 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
527 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
528 GUID_string(mem_ctx, &guid)));
530 } else if (ret != LDB_SUCCESS) {
531 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
532 GUID_string(mem_ctx, &guid),
537 la->attid = dsdb_attribute_get_attid(sa, is_schema_nc);
538 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
540 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
541 if (!NT_STATUS_IS_OK(status)) {
542 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
543 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
544 return ntstatus_to_werror(status);
546 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
547 if (!NT_STATUS_IS_OK(status)) {
548 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
549 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
550 return ntstatus_to_werror(status);
552 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
553 if (!NT_STATUS_IS_OK(status)) {
554 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
555 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
556 return ntstatus_to_werror(status);
558 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
559 if (!NT_STATUS_IS_OK(status)) {
560 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
561 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
562 return ntstatus_to_werror(status);
565 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
566 if (!NT_STATUS_IS_OK(status)) {
567 /* this is possible for upgraded links */
568 la->originating_add_time = la->meta_data.originating_change_time;
571 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
572 W_ERROR_NOT_OK_RETURN(werr);
580 add linked attributes from an object to the list of linked
581 attributes in a getncchanges request
583 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
585 struct ldb_dn *ncRoot_dn,
587 struct dsdb_schema *schema,
588 uint64_t highest_usn,
589 uint32_t replica_flags,
590 const struct ldb_message *msg,
591 struct drsuapi_DsReplicaLinkedAttribute **la_list,
593 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
596 TALLOC_CTX *tmp_ctx = NULL;
597 uint64_t uSNChanged = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
598 bool is_critical = ldb_msg_find_attr_as_bool(msg, "isCriticalSystemObject", false);
600 if (replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
606 if (uSNChanged <= highest_usn) {
610 tmp_ctx = talloc_new(mem_ctx);
611 if (tmp_ctx == NULL) {
612 return WERR_NOT_ENOUGH_MEMORY;
615 for (i=0; i<msg->num_elements; i++) {
616 struct ldb_message_element *el = &msg->elements[i];
617 const struct dsdb_attribute *sa;
620 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
622 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
623 /* we only want forward links */
627 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
628 /* its an old style link, it will have been
629 * sent in the main replication data */
633 for (j=0; j<el->num_values; j++) {
634 struct dsdb_dn *dsdb_dn;
636 uint64_t originating_usn;
637 NTSTATUS status, status2;
639 struct GUID originating_invocation_id;
641 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
642 if (dsdb_dn == NULL) {
643 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
644 el->name, ldb_dn_get_linearized(msg->dn)));
645 talloc_free(tmp_ctx);
646 return WERR_DS_DRA_INTERNAL_ERROR;
649 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
650 if (!NT_STATUS_IS_OK(status)) {
651 /* this can happen for attributes
652 given to us with old style meta
657 if (local_usn > uSNChanged) {
658 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
659 el->name, ldb_dn_get_linearized(msg->dn)));
660 talloc_free(tmp_ctx);
661 return WERR_DS_DRA_INTERNAL_ERROR;
664 if (local_usn <= highest_usn) {
668 status = dsdb_get_extended_dn_guid(dsdb_dn->dn,
669 &originating_invocation_id,
671 status2 = dsdb_get_extended_dn_uint64(dsdb_dn->dn,
673 "RMD_ORIGINATING_USN");
675 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2)) {
676 if (udv_filter(uptodateness_vector,
677 &originating_invocation_id,
683 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema,
684 sa, msg, dsdb_dn, la_list,
685 la_count, is_schema_nc);
686 if (!W_ERROR_IS_OK(werr)) {
687 talloc_free(tmp_ctx);
693 talloc_free(tmp_ctx);
698 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
700 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
701 struct ldb_dn *ncRoot_dn,
702 struct drsuapi_DsReplicaCursor2CtrEx *udv)
710 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
711 if (ret != LDB_SUCCESS) {
712 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
713 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
714 return WERR_DS_DRA_INTERNAL_ERROR;
721 /* comparison function for linked attributes - see CompareLinks() in
722 * MS-DRSR section 4.1.10.5.17 */
723 static int linked_attribute_compare(const struct la_for_sorting *la1,
724 const struct la_for_sorting *la2,
728 c = memcmp(la1->source_guid,
729 la2->source_guid, sizeof(la2->source_guid));
734 if (la1->link->attid != la2->link->attid) {
735 return la1->link->attid < la2->link->attid? -1:1;
738 if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
739 (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
740 return (la1->link->flags &
741 DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
744 return memcmp(la1->target_guid,
745 la2->target_guid, sizeof(la2->target_guid));
748 struct drsuapi_changed_objects {
755 sort the objects we send first by uSNChanged
757 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
758 struct drsuapi_changed_objects *m2,
759 struct drsuapi_getncchanges_state *getnc_state)
763 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
768 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
773 if (m1->usn == m2->usn) {
774 return ldb_dn_compare(m2->dn, m1->dn);
777 if (m1->usn < m2->usn) {
786 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
788 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
790 struct drsuapi_DsGetNCChangesRequest10 *req10,
791 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
792 struct ldb_dn **rid_manager_dn)
794 struct ldb_dn *req_dn, *ntds_dn = NULL;
796 struct ldb_context *ldb = b_state->sam_ctx;
797 struct ldb_result *ext_res;
798 struct dsdb_fsmo_extended_op *exop;
803 - verify that the DN being asked for is the RID Manager DN
804 - verify that we are the RID Manager
807 /* work out who is the RID Manager, also return to caller */
808 ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
809 if (ret != LDB_SUCCESS) {
810 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
811 return WERR_DS_DRA_INTERNAL_ERROR;
814 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
815 if (!ldb_dn_validate(req_dn) ||
816 ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
817 /* that isn't the RID Manager DN */
818 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
819 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
820 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
824 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
825 ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
826 if (ret != LDB_SUCCESS) {
827 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
828 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
829 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
833 /* find the DN of the RID Manager */
834 ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
835 if (ret != LDB_SUCCESS) {
836 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
837 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
838 return WERR_DS_DRA_INTERNAL_ERROR;
842 /* we're not the RID Manager - go away */
843 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
844 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
848 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
849 W_ERROR_HAVE_NO_MEMORY(exop);
851 exop->fsmo_info = req10->fsmo_info;
852 exop->destination_dsa_guid = req10->destination_dsa_guid;
854 ret = ldb_transaction_start(ldb);
855 if (ret != LDB_SUCCESS) {
856 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
857 ldb_errstring(ldb)));
858 return WERR_DS_DRA_INTERNAL_ERROR;
861 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
862 if (ret != LDB_SUCCESS) {
863 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
864 ldb_errstring(ldb)));
865 ldb_transaction_cancel(ldb);
866 return WERR_DS_DRA_INTERNAL_ERROR;
869 ret = ldb_transaction_commit(ldb);
870 if (ret != LDB_SUCCESS) {
871 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
872 ldb_errstring(ldb)));
873 return WERR_DS_DRA_INTERNAL_ERROR;
876 talloc_free(ext_res);
878 DEBUG(2,("Allocated RID pool for server %s\n",
879 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
881 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
887 return an array of SIDs from a ldb_message given an attribute name
888 assumes the SIDs are in extended DN format
890 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
891 struct ldb_message *msg,
894 const struct dom_sid ***sids)
896 struct ldb_message_element *el;
899 el = ldb_msg_find_element(msg, attr);
905 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
906 W_ERROR_HAVE_NO_MEMORY(*sids);
908 for (i=0; i<el->num_values; i++) {
909 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
913 sid = talloc(*sids, struct dom_sid);
914 W_ERROR_HAVE_NO_MEMORY(sid);
915 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
916 if (!NT_STATUS_IS_OK(status)) {
917 return WERR_INTERNAL_DB_CORRUPTION;
928 return an array of SIDs from a ldb_message given an attribute name
929 assumes the SIDs are in NDR form
931 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
932 struct ldb_message *msg,
935 const struct dom_sid ***sids)
937 struct ldb_message_element *el;
940 el = ldb_msg_find_element(msg, attr);
946 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
947 W_ERROR_HAVE_NO_MEMORY(*sids);
949 for (i=0; i<el->num_values; i++) {
950 enum ndr_err_code ndr_err;
953 sid = talloc(*sids, struct dom_sid);
954 W_ERROR_HAVE_NO_MEMORY(sid);
956 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
957 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
958 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
959 return WERR_INTERNAL_DB_CORRUPTION;
969 see if any SIDs in list1 are in list2
971 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
974 /* do we ever have enough SIDs here to worry about O(n^2) ? */
975 for (i=0; list1[i]; i++) {
976 for (j=0; list2[j]; j++) {
977 if (dom_sid_equal(list1[i], list2[j])) {
986 handle a DRSUAPI_EXOP_REPL_SECRET call
988 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
990 struct drsuapi_DsGetNCChangesRequest10 *req10,
991 struct dom_sid *user_sid,
992 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
993 bool has_get_all_changes)
995 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
996 struct ldb_dn *obj_dn = NULL;
997 struct ldb_dn *rodc_dn, *krbtgt_link_dn;
999 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
1000 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
1001 struct ldb_result *rodc_res, *obj_res;
1002 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
1005 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
1006 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1009 * we need to work out if we will allow this DC to
1010 * replicate the secrets for this object
1012 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
1016 if (b_state->sam_ctx_system == NULL) {
1017 /* this operation needs system level access */
1018 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
1019 return WERR_DS_DRA_SOURCE_DISABLED;
1023 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
1025 * The pseudo code indicate
1026 * revealsecrets = true
1027 * if IsRevealSecretRequest(msgIn) then
1028 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
1030 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
1031 * <... check if this account is ok to be replicated on this DC ...>
1032 * <... and if not reveal secrets = no ...>
1034 * reveal secrets = false
1039 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
1040 * then you can do EXOP_REPL_SECRETS
1042 if (has_get_all_changes) {
1046 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
1047 if (!ldb_dn_validate(obj_dn)) goto failed;
1049 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1050 dom_sid_string(mem_ctx, user_sid));
1051 if (!ldb_dn_validate(rodc_dn)) goto failed;
1053 /* do the two searches we need */
1054 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1055 DSDB_SEARCH_SHOW_EXTENDED_DN);
1056 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1058 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1059 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1061 /* if the object SID is equal to the user_sid, allow */
1062 if (dom_sid_equal(user_sid,
1063 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1067 /* an RODC is allowed to get its own krbtgt account secrets */
1068 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1069 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1070 if (krbtgt_link_dn != NULL &&
1071 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1075 /* but it isn't allowed to get anyone elses krbtgt secrets */
1076 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1077 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1081 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1082 "userAccountControl", 0) &
1083 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1087 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1088 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1089 if (!W_ERROR_IS_OK(werr)) {
1093 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1094 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1095 if (!W_ERROR_IS_OK(werr)) {
1099 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1100 mem_ctx, "tokenGroups", &token_sids);
1101 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1105 if (never_reveal_sids &&
1106 sid_list_match(token_sids, never_reveal_sids)) {
1111 sid_list_match(token_sids, reveal_sids)) {
1117 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1118 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1119 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1120 return WERR_DS_DRA_SECRETS_DENIED;
1123 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1124 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1125 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1126 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1127 req10->highwatermark.highest_usn = 0;
1131 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1132 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1133 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1134 return WERR_DS_DRA_BAD_DN;
1139 handle a DRSUAPI_EXOP_REPL_OBJ call
1141 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1142 TALLOC_CTX *mem_ctx,
1143 struct drsuapi_DsGetNCChangesRequest10 *req10,
1144 struct dom_sid *user_sid,
1145 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1147 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1149 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1150 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1152 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1158 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1159 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1160 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1162 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1163 TALLOC_CTX *mem_ctx,
1164 struct drsuapi_DsGetNCChangesRequest10 *req10,
1165 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1167 struct ldb_dn *req_dn, *ntds_dn;
1170 struct ldb_context *ldb = b_state->sam_ctx;
1171 struct ldb_message *msg;
1176 - verify that the client dn exists
1177 - verify that we are the current master
1180 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1181 if (!ldb_dn_validate(req_dn)) {
1182 /* that is not a valid dn */
1183 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1184 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1185 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1189 /* find the DN of the current role owner */
1190 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1191 if (ret != LDB_SUCCESS) {
1192 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1193 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1194 return WERR_DS_DRA_INTERNAL_ERROR;
1198 /* we're not the RID Manager or role owner - go away */
1199 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1200 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1204 /* change the current master */
1205 msg = ldb_msg_new(ldb);
1206 W_ERROR_HAVE_NO_MEMORY(msg);
1207 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1208 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1210 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1211 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1212 if (ret != LDB_SUCCESS) {
1213 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1214 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1216 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1220 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1223 return WERR_DS_DRA_INTERNAL_ERROR;
1226 for (i=0;i<msg->num_elements;i++) {
1227 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1230 ret = ldb_transaction_start(ldb);
1231 if (ret != LDB_SUCCESS) {
1232 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1233 ldb_errstring(ldb)));
1234 return WERR_DS_DRA_INTERNAL_ERROR;
1237 ret = ldb_modify(ldb, msg);
1238 if (ret != LDB_SUCCESS) {
1239 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1240 ldb_errstring(ldb)));
1241 ldb_transaction_cancel(ldb);
1242 return WERR_DS_DRA_INTERNAL_ERROR;
1245 ret = ldb_transaction_commit(ldb);
1246 if (ret != LDB_SUCCESS) {
1247 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1248 ldb_errstring(ldb)));
1249 return WERR_DS_DRA_INTERNAL_ERROR;
1252 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1258 see if this getncchanges request includes a request to reveal secret information
1260 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1261 struct drsuapi_DsGetNCChangesRequest10 *req10,
1262 struct dsdb_schema_prefixmap *pfm_remote,
1263 bool *is_secret_request)
1265 enum drsuapi_DsExtendedOperation exop;
1267 struct dsdb_schema *schema;
1268 struct dsdb_syntax_ctx syntax_ctx;
1270 *is_secret_request = true;
1272 exop = req10->extended_op;
1275 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1276 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1277 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1278 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1279 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1280 /* FSMO exops can reveal secrets */
1281 *is_secret_request = true;
1283 case DRSUAPI_EXOP_REPL_SECRET:
1284 case DRSUAPI_EXOP_REPL_OBJ:
1285 case DRSUAPI_EXOP_NONE:
1289 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1290 *is_secret_request = false;
1294 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1295 req10->partial_attribute_set == NULL) {
1296 /* they want secrets */
1297 *is_secret_request = true;
1301 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1302 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1303 syntax_ctx.pfm_remote = pfm_remote;
1305 /* check the attributes they asked for */
1306 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1307 const struct dsdb_attribute *sa;
1308 WERROR werr = getncchanges_attid_remote_to_local(schema,
1310 req10->partial_attribute_set->attids[i],
1314 if (!W_ERROR_IS_OK(werr)) {
1315 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1316 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1320 if (!dsdb_attr_in_rodc_fas(sa)) {
1321 *is_secret_request = true;
1326 if (req10->partial_attribute_set_ex) {
1327 /* check the extended attributes they asked for */
1328 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1329 const struct dsdb_attribute *sa;
1330 WERROR werr = getncchanges_attid_remote_to_local(schema,
1332 req10->partial_attribute_set_ex->attids[i],
1336 if (!W_ERROR_IS_OK(werr)) {
1337 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1338 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1342 if (!dsdb_attr_in_rodc_fas(sa)) {
1343 *is_secret_request = true;
1349 *is_secret_request = false;
1354 see if this getncchanges request is only for attributes in the GC
1355 partial attribute set
1357 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1358 struct drsuapi_DsGetNCChangesRequest10 *req10,
1359 struct dsdb_schema_prefixmap *pfm_remote,
1360 bool *is_gc_pas_request)
1362 enum drsuapi_DsExtendedOperation exop;
1364 struct dsdb_schema *schema;
1365 struct dsdb_syntax_ctx syntax_ctx;
1367 exop = req10->extended_op;
1370 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1371 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1372 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1373 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1374 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1375 case DRSUAPI_EXOP_REPL_SECRET:
1376 *is_gc_pas_request = false;
1378 case DRSUAPI_EXOP_REPL_OBJ:
1379 case DRSUAPI_EXOP_NONE:
1383 if (req10->partial_attribute_set == NULL) {
1384 /* they want it all */
1385 *is_gc_pas_request = false;
1389 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1390 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1391 syntax_ctx.pfm_remote = pfm_remote;
1393 /* check the attributes they asked for */
1394 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1395 const struct dsdb_attribute *sa;
1396 WERROR werr = getncchanges_attid_remote_to_local(schema,
1398 req10->partial_attribute_set->attids[i],
1402 if (!W_ERROR_IS_OK(werr)) {
1403 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1404 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1408 if (!sa->isMemberOfPartialAttributeSet) {
1409 *is_gc_pas_request = false;
1414 if (req10->partial_attribute_set_ex) {
1415 /* check the extended attributes they asked for */
1416 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1417 const struct dsdb_attribute *sa;
1418 WERROR werr = getncchanges_attid_remote_to_local(schema,
1420 req10->partial_attribute_set_ex->attids[i],
1424 if (!W_ERROR_IS_OK(werr)) {
1425 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1426 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1430 if (!sa->isMemberOfPartialAttributeSet) {
1431 *is_gc_pas_request = false;
1437 *is_gc_pas_request = true;
1443 map from req8 to req10
1445 static struct drsuapi_DsGetNCChangesRequest10 *
1446 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1447 struct drsuapi_DsGetNCChangesRequest8 *req8)
1449 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1450 struct drsuapi_DsGetNCChangesRequest10);
1451 if (req10 == NULL) {
1455 req10->destination_dsa_guid = req8->destination_dsa_guid;
1456 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1457 req10->naming_context = req8->naming_context;
1458 req10->highwatermark = req8->highwatermark;
1459 req10->uptodateness_vector = req8->uptodateness_vector;
1460 req10->replica_flags = req8->replica_flags;
1461 req10->max_object_count = req8->max_object_count;
1462 req10->max_ndr_size = req8->max_ndr_size;
1463 req10->extended_op = req8->extended_op;
1464 req10->fsmo_info = req8->fsmo_info;
1465 req10->partial_attribute_set = req8->partial_attribute_set;
1466 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1467 req10->mapping_ctr = req8->mapping_ctr;
1472 static const char *collect_objects_attrs[] = { "uSNChanged",
1477 * Collects object for normal replication cycle.
1479 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1480 TALLOC_CTX *mem_ctx,
1481 struct drsuapi_DsGetNCChangesRequest10 *req10,
1482 struct ldb_dn *search_dn,
1483 const char *extra_filter,
1484 struct ldb_result **search_res)
1487 char* search_filter;
1488 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1489 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1490 bool critical_only = false;
1492 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1493 critical_only = true;
1496 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1497 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1498 scope = LDB_SCOPE_BASE;
1499 critical_only = false;
1502 /* Construct response. */
1503 search_filter = talloc_asprintf(mem_ctx,
1504 "(uSNChanged>=%llu)",
1505 (unsigned long long)(getnc_state->min_usn+1));
1508 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1511 if (critical_only) {
1512 search_filter = talloc_asprintf(mem_ctx,
1513 "(&%s(isCriticalSystemObject=TRUE))",
1517 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1518 scope = LDB_SCOPE_BASE;
1522 search_dn = getnc_state->ncRoot_dn;
1525 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1526 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1527 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1529 collect_objects_attrs,
1531 if (ret != LDB_SUCCESS) {
1532 return WERR_DS_DRA_INTERNAL_ERROR;
1539 * Collects object for normal replication cycle.
1541 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1542 TALLOC_CTX *mem_ctx,
1543 struct drsuapi_DsGetNCChangesRequest10 *req10,
1544 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1545 struct ldb_dn *search_dn,
1546 const char *extra_filter,
1547 struct ldb_result **search_res)
1549 /* we have nothing to do in case of ex-op failure */
1550 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1554 switch (req10->extended_op) {
1555 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1558 struct ldb_dn *ntds_dn = NULL;
1559 struct ldb_dn *server_dn = NULL;
1560 struct ldb_dn *machine_dn = NULL;
1561 struct ldb_dn *rid_set_dn = NULL;
1562 struct ldb_result *search_res2 = NULL;
1563 struct ldb_result *search_res3 = NULL;
1564 TALLOC_CTX *frame = talloc_stackframe();
1565 /* get RID manager, RID set and server DN (in that order) */
1567 /* This first search will get the RID Manager */
1568 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1570 search_dn, LDB_SCOPE_BASE,
1571 collect_objects_attrs,
1573 if (ret != LDB_SUCCESS) {
1574 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1575 ldb_dn_get_linearized(search_dn),
1576 ldb_errstring(b_state->sam_ctx)));
1578 return WERR_DS_DRA_INTERNAL_ERROR;
1581 if ((*search_res)->count != 1) {
1582 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1583 ldb_dn_get_linearized(search_dn),
1584 (*search_res)->count));
1586 return WERR_DS_DRA_INTERNAL_ERROR;
1589 /* Now extend it to the RID set */
1591 /* Find the computer account DN for the destination
1592 * dsa GUID specified */
1594 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1595 &req10->destination_dsa_guid, 0,
1597 if (ret != LDB_SUCCESS) {
1598 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1600 &req10->destination_dsa_guid),
1601 ldb_errstring(b_state->sam_ctx)));
1603 return WERR_DS_DRA_INTERNAL_ERROR;
1606 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1609 return WERR_DS_DRA_INTERNAL_ERROR;
1612 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1613 "serverReference", &machine_dn);
1614 if (ret != LDB_SUCCESS) {
1615 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1616 ldb_dn_get_linearized(server_dn),
1617 ldb_errstring(b_state->sam_ctx)));
1619 return WERR_DS_DRA_INTERNAL_ERROR;
1622 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1623 "rIDSetReferences", &rid_set_dn);
1624 if (ret != LDB_SUCCESS) {
1625 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1626 ldb_dn_get_linearized(server_dn),
1627 ldb_errstring(b_state->sam_ctx)));
1629 return WERR_DS_DRA_INTERNAL_ERROR;
1633 /* This first search will get the RID Manager, now get the RID set */
1634 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1636 rid_set_dn, LDB_SCOPE_BASE,
1637 collect_objects_attrs,
1639 if (ret != LDB_SUCCESS) {
1640 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1641 ldb_dn_get_linearized(rid_set_dn),
1642 ldb_errstring(b_state->sam_ctx)));
1644 return WERR_DS_DRA_INTERNAL_ERROR;
1647 if (search_res2->count != 1) {
1648 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1649 ldb_dn_get_linearized(rid_set_dn),
1650 search_res2->count));
1652 return WERR_DS_DRA_INTERNAL_ERROR;
1655 /* Finally get the server DN */
1656 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1658 machine_dn, LDB_SCOPE_BASE,
1659 collect_objects_attrs,
1661 if (ret != LDB_SUCCESS) {
1662 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1663 ldb_dn_get_linearized(server_dn),
1664 ldb_errstring(b_state->sam_ctx)));
1666 return WERR_DS_DRA_INTERNAL_ERROR;
1669 if (search_res3->count != 1) {
1670 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1671 ldb_dn_get_linearized(server_dn),
1672 search_res3->count));
1674 return WERR_DS_DRA_INTERNAL_ERROR;
1677 /* Now extend the original search_res with these answers */
1678 (*search_res)->count = 3;
1680 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1681 struct ldb_message *,
1682 (*search_res)->count);
1683 if ((*search_res)->msgs == NULL) {
1685 return WERR_NOT_ENOUGH_MEMORY;
1689 talloc_steal(mem_ctx, *search_res);
1690 (*search_res)->msgs[1] =
1691 talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1692 (*search_res)->msgs[2] =
1693 talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1699 /* TODO: implement extended op specific collection
1700 * of objects. Right now we just normal procedure
1701 * for collecting objects */
1702 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1706 static void dcesrv_drsuapi_update_highwatermark(const struct ldb_message *msg,
1708 struct drsuapi_DsReplicaHighWaterMark *hwm)
1710 uint64_t uSN = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
1712 if (uSN > max_usn) {
1714 * Only report the max_usn we had at the start
1715 * of the replication cycle.
1717 * If this object has changed lately we better
1718 * let the destination dsa refetch the change.
1719 * This is better than the risk of loosing some
1720 * objects or linked attributes.
1725 if (uSN <= hwm->tmp_highest_usn) {
1729 hwm->tmp_highest_usn = uSN;
1730 hwm->reserved_usn = 0;
1733 static WERROR dcesrv_drsuapi_anc_cache_add(struct db_context *anc_cache,
1734 const struct GUID *guid)
1736 enum ndr_err_code ndr_err;
1737 uint8_t guid_buf[16] = { 0, };
1740 .length = sizeof(guid_buf),
1752 ndr_err = ndr_push_struct_into_fixed_blob(&b, guid,
1753 (ndr_push_flags_fn_t)ndr_push_GUID);
1754 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1755 return WERR_DS_DRA_INTERNAL_ERROR;
1758 status = dbwrap_store(anc_cache, key, val, TDB_REPLACE);
1759 if (!NT_STATUS_IS_OK(status)) {
1760 return WERR_DS_DRA_INTERNAL_ERROR;
1766 static WERROR dcesrv_drsuapi_anc_cache_exists(struct db_context *anc_cache,
1767 const struct GUID *guid)
1769 enum ndr_err_code ndr_err;
1770 uint8_t guid_buf[16] = { 0, };
1773 .length = sizeof(guid_buf),
1781 ndr_err = ndr_push_struct_into_fixed_blob(&b, guid,
1782 (ndr_push_flags_fn_t)ndr_push_GUID);
1783 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1784 return WERR_DS_DRA_INTERNAL_ERROR;
1787 exists = dbwrap_exists(anc_cache, key);
1789 return WERR_OBJECT_NOT_FOUND;
1792 return WERR_OBJECT_NAME_EXISTS;
1796 drsuapi_DsGetNCChanges
1798 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1800 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1801 struct drsuapi_DsGetNCChanges *r)
1803 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1806 struct dsdb_schema *schema;
1807 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1808 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1810 DATA_BLOB session_key;
1812 struct dcesrv_handle *h;
1813 struct drsuapi_bind_state *b_state;
1814 struct drsuapi_getncchanges_state *getnc_state;
1815 struct drsuapi_DsGetNCChangesRequest10 *req10;
1817 uint32_t max_objects;
1819 uint32_t link_count = 0;
1820 uint32_t link_total = 0;
1821 uint32_t link_given = 0;
1822 struct ldb_dn *search_dn = NULL;
1823 bool am_rodc, null_scope=false;
1824 enum security_user_level security_level;
1825 struct ldb_context *sam_ctx;
1826 struct dom_sid *user_sid;
1827 bool is_secret_request;
1828 bool is_gc_pas_request;
1829 struct drsuapi_changed_objects *changes;
1831 time_t start = time(NULL);
1832 bool max_wait_reached = false;
1833 bool has_get_all_changes = false;
1834 struct GUID invocation_id;
1835 static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
1836 struct dsdb_schema_prefixmap *pfm_remote = NULL;
1838 uint32_t *local_pas = NULL;
1840 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1843 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1845 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1847 *r->out.level_out = 6;
1848 /* TODO: linked attributes*/
1849 r->out.ctr->ctr6.linked_attributes_count = 0;
1850 r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
1852 r->out.ctr->ctr6.object_count = 0;
1853 r->out.ctr->ctr6.nc_object_count = 0;
1854 r->out.ctr->ctr6.more_data = false;
1855 r->out.ctr->ctr6.uptodateness_vector = NULL;
1856 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1857 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1858 r->out.ctr->ctr6.first_object = NULL;
1860 /* a RODC doesn't allow for any replication */
1861 ret = samdb_rodc(sam_ctx, &am_rodc);
1862 if (ret == LDB_SUCCESS && am_rodc) {
1863 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1864 return WERR_DS_DRA_SOURCE_DISABLED;
1867 /* Check request revision.
1869 switch (r->in.level) {
1871 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1872 if (req10 == NULL) {
1873 return WERR_NOT_ENOUGH_MEMORY;
1877 req10 = &r->in.req->req10;
1880 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1882 return WERR_REVISION_MISMATCH;
1886 /* Perform access checks. */
1887 /* TODO: we need to support a sync on a specific non-root
1888 * DN. We'll need to find the real partition root here */
1889 ncRoot = req10->naming_context;
1890 if (ncRoot == NULL) {
1891 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1892 return WERR_DS_DRA_INVALID_PARAMETER;
1895 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1896 return WERR_DS_DRA_INTERNAL_ERROR;
1899 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1900 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1901 return WERR_DS_DRA_SOURCE_DISABLED;
1904 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1906 /* all clients must have GUID_DRS_GET_CHANGES */
1907 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1909 dce_call->conn->auth_state.session_info->security_token,
1910 req10->naming_context,
1911 GUID_DRS_GET_CHANGES);
1912 if (!W_ERROR_IS_OK(werr)) {
1916 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
1917 full = req10->partial_attribute_set == NULL &&
1918 req10->partial_attribute_set_ex == NULL;
1920 full = (options & DRSUAPI_DRS_WRIT_REP) != 0;
1923 werr = dsdb_schema_pfm_from_drsuapi_pfm(&req10->mapping_ctr, true,
1924 mem_ctx, &pfm_remote, NULL);
1926 /* We were supplied a partial attribute set, without the prefix map! */
1927 if (!full && !W_ERROR_IS_OK(werr)) {
1928 if (req10->mapping_ctr.num_mappings == 0) {
1930 * Despite the fact MS-DRSR specifies that this shouldn't
1931 * happen, Windows RODCs will in fact not provide a prefixMap.
1933 DEBUG(5,(__location__ ": Failed to provide a remote prefixMap,"
1934 " falling back to local prefixMap\n"));
1936 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
1942 /* allowed if the GC PAS and client has
1943 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1944 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, pfm_remote, &is_gc_pas_request);
1945 if (!W_ERROR_IS_OK(werr)) {
1948 if (is_gc_pas_request) {
1949 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1951 dce_call->conn->auth_state.session_info->security_token,
1952 req10->naming_context,
1953 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1954 if (W_ERROR_IS_OK(werr)) {
1959 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10,
1961 &is_secret_request);
1962 if (!W_ERROR_IS_OK(werr)) {
1965 if (is_secret_request) {
1966 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1968 dce_call->conn->auth_state.session_info->security_token,
1969 req10->naming_context,
1970 GUID_DRS_GET_ALL_CHANGES);
1971 if (!W_ERROR_IS_OK(werr)) {
1972 /* Only bail if this is not a EXOP_REPL_SECRET */
1973 if (req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1977 has_get_all_changes = true;
1982 /* for non-administrator replications, check that they have
1983 given the correct source_dsa_invocation_id */
1984 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1985 samdb_domain_sid(sam_ctx));
1986 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1987 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1988 /* we rely on this flag being unset for RODC requests */
1989 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1993 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1994 /* Ignore the _in_ uptpdateness vector*/
1995 req10->uptodateness_vector = NULL;
1998 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1999 req10->source_dsa_invocation_id = invocation_id;
2002 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
2004 * The given highwatermark is only valid relative to the
2005 * specified source_dsa_invocation_id.
2007 ZERO_STRUCT(req10->highwatermark);
2010 getnc_state = b_state->getncchanges_state;
2012 /* see if a previous replication has been abandoned */
2014 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
2015 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
2016 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
2017 ldb_dn_get_linearized(new_dn),
2018 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
2019 ldb_dn_get_linearized(getnc_state->last_dn)));
2020 talloc_free(getnc_state);
2026 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
2027 &req10->highwatermark);
2029 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
2030 "on DN %s %s highwatermark (last_dn %s)\n",
2031 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
2032 (ret > 0) ? "older" : "newer",
2033 ldb_dn_get_linearized(getnc_state->last_dn)));
2034 talloc_free(getnc_state);
2039 if (getnc_state == NULL) {
2040 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
2041 if (getnc_state == NULL) {
2042 return WERR_NOT_ENOUGH_MEMORY;
2044 b_state->getncchanges_state = getnc_state;
2045 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
2046 if (getnc_state->ncRoot_dn == NULL) {
2047 return WERR_NOT_ENOUGH_MEMORY;
2050 ret = dsdb_find_guid_by_dn(b_state->sam_ctx_system,
2051 getnc_state->ncRoot_dn,
2052 &getnc_state->ncRoot_guid);
2053 if (ret != LDB_SUCCESS) {
2054 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
2055 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
2056 return WERR_DS_DRA_INTERNAL_ERROR;
2058 ncRoot->guid = getnc_state->ncRoot_guid;
2060 /* find out if we are to replicate Schema NC */
2061 ret = ldb_dn_compare_base(ldb_get_schema_basedn(b_state->sam_ctx),
2062 getnc_state->ncRoot_dn);
2064 getnc_state->is_schema_nc = (0 == ret);
2066 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2067 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
2071 * This is the first replication cycle and it is
2072 * a good place to handle extended operations
2074 * FIXME: we don't fully support extended operations yet
2076 switch (req10->extended_op) {
2077 case DRSUAPI_EXOP_NONE:
2079 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
2080 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
2081 W_ERROR_NOT_OK_RETURN(werr);
2082 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2086 case DRSUAPI_EXOP_REPL_SECRET:
2087 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
2090 has_get_all_changes);
2091 r->out.result = werr;
2092 W_ERROR_NOT_OK_RETURN(werr);
2094 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
2095 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2096 W_ERROR_NOT_OK_RETURN(werr);
2097 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2101 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
2102 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2103 W_ERROR_NOT_OK_RETURN(werr);
2104 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2108 case DRSUAPI_EXOP_FSMO_REQ_PDC:
2109 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2110 W_ERROR_NOT_OK_RETURN(werr);
2111 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2115 case DRSUAPI_EXOP_REPL_OBJ:
2116 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
2117 r->out.result = werr;
2118 W_ERROR_NOT_OK_RETURN(werr);
2121 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
2123 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
2124 (unsigned)req10->extended_op));
2125 return WERR_DS_DRA_NOT_SUPPORTED;
2129 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
2130 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
2131 DEBUG(0,(__location__ ": Bad DN '%s'\n",
2132 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
2133 return WERR_DS_DRA_INVALID_PARAMETER;
2136 ncRoot->guid = getnc_state->ncRoot_guid;
2138 /* we need the session key for encrypting password attributes */
2139 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
2140 if (!NT_STATUS_IS_OK(status)) {
2141 DEBUG(0,(__location__ ": Failed to get session key\n"));
2142 return WERR_DS_DRA_INTERNAL_ERROR;
2146 TODO: MS-DRSR section 4.1.10.1.1
2147 Work out if this is the start of a new cycle */
2149 if (getnc_state->guids == NULL) {
2150 const char *extra_filter;
2151 struct ldb_result *search_res = NULL;
2152 static const struct drsuapi_DsReplicaCursorCtrEx empty_udv;
2153 const struct drsuapi_DsReplicaCursorCtrEx *udv = NULL;
2155 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
2157 if (req10->uptodateness_vector != NULL) {
2158 udv = req10->uptodateness_vector;
2163 getnc_state->min_usn = req10->highwatermark.highest_usn;
2164 for (i = 0; i < udv->count; i++) {
2166 const struct drsuapi_DsReplicaCursor *cur =
2169 match = GUID_equal(&invocation_id,
2170 &cur->source_dsa_invocation_id);
2174 if (cur->highest_usn > getnc_state->min_usn) {
2175 getnc_state->min_usn = cur->highest_usn;
2179 getnc_state->max_usn = getnc_state->min_usn;
2181 getnc_state->final_udv = talloc_zero(getnc_state,
2182 struct drsuapi_DsReplicaCursor2CtrEx);
2183 if (getnc_state->final_udv == NULL) {
2184 return WERR_NOT_ENOUGH_MEMORY;
2186 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
2187 getnc_state->final_udv);
2188 if (!W_ERROR_IS_OK(werr)) {
2192 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2193 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
2194 search_dn, extra_filter,
2197 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
2199 search_dn, extra_filter,
2202 W_ERROR_NOT_OK_RETURN(werr);
2204 /* extract out the GUIDs list */
2205 getnc_state->num_records = search_res ? search_res->count : 0;
2206 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
2207 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
2209 changes = talloc_array(getnc_state,
2210 struct drsuapi_changed_objects,
2211 getnc_state->num_records);
2212 W_ERROR_HAVE_NO_MEMORY(changes);
2214 for (i=0; i<getnc_state->num_records; i++) {
2215 changes[i].dn = search_res->msgs[i]->dn;
2216 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
2217 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
2219 if (changes[i].usn > getnc_state->max_usn) {
2220 getnc_state->max_usn = changes[i].usn;
2224 /* RID_ALLOC returns 3 objects in a fixed order */
2225 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
2228 LDB_TYPESAFE_QSORT(changes,
2229 getnc_state->num_records,
2231 site_res_cmp_usn_order);
2234 for (i=0; i < getnc_state->num_records; i++) {
2235 getnc_state->guids[i] = changes[i].guid;
2236 if (GUID_all_zero(&getnc_state->guids[i])) {
2237 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
2238 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
2239 return WERR_DS_DRA_INTERNAL_ERROR;
2243 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
2244 getnc_state->final_hwm.reserved_usn = 0;
2245 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
2247 talloc_free(search_res);
2248 talloc_free(changes);
2250 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2252 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
2253 getnc_state->anc_cache = db_open_rbt(getnc_state);
2254 if (getnc_state->anc_cache == NULL) {
2255 return WERR_NOT_ENOUGH_MEMORY;
2260 if (req10->uptodateness_vector) {
2261 /* make sure its sorted */
2262 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
2263 req10->uptodateness_vector->count,
2264 drsuapi_DsReplicaCursor_compare);
2267 /* Prefix mapping */
2268 schema = dsdb_get_schema(sam_ctx, mem_ctx);
2270 DEBUG(0,("No schema in sam_ctx\n"));
2271 return WERR_DS_DRA_INTERNAL_ERROR;
2274 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
2275 if (r->out.ctr->ctr6.naming_context == NULL) {
2276 return WERR_NOT_ENOUGH_MEMORY;
2278 *r->out.ctr->ctr6.naming_context = *ncRoot;
2280 /* find the SID if there is one */
2281 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2283 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2284 r->out.ctr->ctr6.mapping_ctr = *ctr;
2286 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2287 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2289 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2290 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2292 currentObject = &r->out.ctr->ctr6.first_object;
2294 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2296 * The client control here only applies in normal replication, not extended
2297 * operations, which return a fixed set, even if the caller
2298 * sets max_object_count == 0
2300 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2301 /* use this to force single objects at a time, which is useful
2302 * for working out what object is giving problems
2304 if (req10->max_object_count < max_objects) {
2305 max_objects = req10->max_object_count;
2309 * TODO: work out how the maximum should be calculated
2311 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2314 * Maximum time that we can spend in a getncchanges
2315 * in order to avoid timeout of the other part.
2316 * 10 seconds by default.
2318 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2320 if (req10->partial_attribute_set != NULL) {
2321 struct dsdb_syntax_ctx syntax_ctx;
2324 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
2325 syntax_ctx.pfm_remote = pfm_remote;
2327 local_pas = talloc_array(b_state, uint32_t, req10->partial_attribute_set->num_attids);
2329 for (j = 0; j < req10->partial_attribute_set->num_attids; j++) {
2330 getncchanges_attid_remote_to_local(schema,
2332 req10->partial_attribute_set->attids[j],
2333 (enum drsuapi_DsAttributeId *)&local_pas[j],
2337 LDB_TYPESAFE_QSORT(local_pas,
2338 req10->partial_attribute_set->num_attids,
2343 for (i=getnc_state->num_processed;
2344 i<getnc_state->num_records &&
2346 (r->out.ctr->ctr6.object_count < max_objects)
2347 && !max_wait_reached;
2349 struct drsuapi_DsReplicaObjectListItemEx *new_objs = NULL;
2350 struct drsuapi_DsReplicaObjectListItemEx *obj;
2351 struct ldb_message *msg;
2352 static const char * const msg_attrs[] = {
2354 "nTSecurityDescriptor",
2356 "replPropertyMetaData",
2357 DSDB_SECRET_ATTRIBUTES,
2359 struct ldb_result *msg_res;
2360 struct ldb_dn *msg_dn;
2361 const struct GUID *next_anc_guid = NULL;
2363 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2364 W_ERROR_HAVE_NO_MEMORY(obj);
2366 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2367 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2370 /* by re-searching here we avoid having a lot of full
2371 * records in memory between calls to getncchanges
2373 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2375 LDB_SCOPE_BASE, msg_attrs, NULL);
2376 if (ret != LDB_SUCCESS) {
2377 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2378 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2379 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2385 msg = msg_res->msgs[0];
2388 * If it has already been added as an ancestor of
2389 * an object, we don't need to do anything more,
2390 * as we've already added the links.
2392 if (getnc_state->anc_cache != NULL) {
2393 werr = dcesrv_drsuapi_anc_cache_exists(getnc_state->anc_cache,
2394 &getnc_state->guids[i]);
2395 if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
2396 dcesrv_drsuapi_update_highwatermark(msg,
2397 getnc_state->max_usn,
2398 &r->out.ctr->ctr6.new_highwatermark);
2399 /* no attributes to send */
2405 max_wait_reached = (time(NULL) - start > max_wait);
2407 werr = get_nc_changes_build_object(obj, msg,
2408 sam_ctx, getnc_state->ncRoot_dn,
2409 getnc_state->is_schema_nc,
2410 schema, &session_key, getnc_state->min_usn,
2411 req10->replica_flags,
2412 req10->partial_attribute_set,
2413 req10->uptodateness_vector,
2417 if (!W_ERROR_IS_OK(werr)) {
2421 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2422 getnc_state->ncRoot_dn,
2423 getnc_state->is_schema_nc,
2424 schema, getnc_state->min_usn,
2425 req10->replica_flags,
2427 &getnc_state->la_list,
2428 &getnc_state->la_count,
2429 req10->uptodateness_vector);
2430 if (!W_ERROR_IS_OK(werr)) {
2434 dcesrv_drsuapi_update_highwatermark(msg,
2435 getnc_state->max_usn,
2436 &r->out.ctr->ctr6.new_highwatermark);
2438 if (obj->meta_data_ctr == NULL) {
2439 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2440 ldb_dn_get_linearized(msg->dn)));
2441 /* no attributes to send */
2448 if (getnc_state->anc_cache != NULL) {
2449 werr = dcesrv_drsuapi_anc_cache_add(getnc_state->anc_cache,
2450 &getnc_state->guids[i]);
2451 if (!W_ERROR_IS_OK(werr)) {
2455 next_anc_guid = obj->parent_object_guid;
2458 while (next_anc_guid != NULL) {
2459 struct drsuapi_DsReplicaObjectListItemEx *anc_obj = NULL;
2460 struct ldb_message *anc_msg = NULL;
2461 struct ldb_result *anc_res = NULL;
2462 struct ldb_dn *anc_dn = NULL;
2464 werr = dcesrv_drsuapi_anc_cache_exists(getnc_state->anc_cache,
2466 if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
2468 * We don't need to send it twice.
2472 if (W_ERROR_IS_OK(werr)) {
2473 return WERR_INTERNAL_ERROR;
2475 if (!W_ERROR_EQUAL(werr, WERR_OBJECT_NOT_FOUND)) {
2480 anc_obj = talloc_zero(mem_ctx,
2481 struct drsuapi_DsReplicaObjectListItemEx);
2482 if (anc_obj == NULL) {
2483 return WERR_NOT_ENOUGH_MEMORY;
2486 anc_dn = ldb_dn_new_fmt(anc_obj, sam_ctx, "<GUID=%s>",
2487 GUID_string(anc_obj, next_anc_guid));
2488 if (anc_dn == NULL) {
2489 return WERR_NOT_ENOUGH_MEMORY;
2492 ret = drsuapi_search_with_extended_dn(sam_ctx, anc_obj,
2496 if (ret != LDB_SUCCESS) {
2497 const char *anc_str = NULL;
2498 const char *obj_str = NULL;
2500 anc_str = ldb_dn_get_extended_linearized(anc_obj,
2503 obj_str = ldb_dn_get_extended_linearized(anc_obj,
2507 DBG_ERR("getncchanges: failed to fetch ANC "
2508 "DN %s for DN %s - %s\n",
2510 ldb_errstring(sam_ctx));
2511 return WERR_DS_DRA_INCONSISTENT_DIT;
2514 anc_msg = anc_res->msgs[0];
2516 werr = get_nc_changes_build_object(anc_obj, anc_msg,
2518 getnc_state->ncRoot_dn,
2519 getnc_state->is_schema_nc,
2520 schema, &session_key,
2521 getnc_state->min_usn,
2522 req10->replica_flags,
2523 req10->partial_attribute_set,
2524 req10->uptodateness_vector,
2526 false, /* force_object_return */
2528 if (!W_ERROR_IS_OK(werr)) {
2532 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2533 getnc_state->ncRoot_dn,
2534 getnc_state->is_schema_nc,
2535 schema, getnc_state->min_usn,
2536 req10->replica_flags,
2538 &getnc_state->la_list,
2539 &getnc_state->la_count,
2540 req10->uptodateness_vector);
2541 if (!W_ERROR_IS_OK(werr)) {
2546 * Regardless of if we actually use it or not,
2547 * we add it to the cache so we don't look at it again
2549 werr = dcesrv_drsuapi_anc_cache_add(getnc_state->anc_cache,
2551 if (!W_ERROR_IS_OK(werr)) {
2556 * Any ancestors which are below the highwatermark
2557 * or uptodateness_vector shouldn't be added,
2558 * but we still look further up the
2559 * tree for ones which have been changed recently.
2561 if (anc_obj->meta_data_ctr != NULL) {
2563 * prepend it to the list
2565 anc_obj->next_object = new_objs;
2570 TALLOC_FREE(anc_res);
2571 TALLOC_FREE(anc_dn);
2574 * We may need to resolve more...
2576 next_anc_guid = anc_obj->parent_object_guid;
2579 *currentObject = new_objs;
2580 while (new_objs != NULL) {
2581 r->out.ctr->ctr6.object_count += 1;
2582 if (new_objs->next_object == NULL) {
2583 currentObject = &new_objs->next_object;
2585 new_objs = new_objs->next_object;
2588 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
2590 talloc_free(getnc_state->last_dn);
2591 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
2593 talloc_free(msg_res);
2594 talloc_free(msg_dn);
2597 getnc_state->num_processed = i;
2599 /* the client can us to call UpdateRefs on its behalf to
2600 re-establish monitoring of the NC */
2601 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2602 !GUID_all_zero(&req10->destination_dsa_guid)) {
2603 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2604 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2605 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2606 ureq.naming_context = ncRoot;
2607 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2608 &req10->destination_dsa_guid);
2609 if (!ureq.dest_dsa_dns_name) {
2610 return WERR_NOT_ENOUGH_MEMORY;
2612 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2613 ureq.options = DRSUAPI_DRS_ADD_REF |
2614 DRSUAPI_DRS_ASYNC_OP |
2615 DRSUAPI_DRS_GETCHG_CHECK;
2617 /* we also need to pass through the
2618 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2619 to send notifies using the GC SPN */
2620 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2622 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2623 if (!W_ERROR_IS_OK(werr)) {
2624 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2625 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2632 * This is just a guess, how to calculate the
2633 * number of linked attributes to send, we need to
2634 * find out how to do this right.
2636 if (r->out.ctr->ctr6.object_count >= max_links) {
2639 max_links -= r->out.ctr->ctr6.object_count;
2642 link_total = getnc_state->la_count;
2644 if (i < getnc_state->num_records) {
2645 r->out.ctr->ctr6.more_data = true;
2647 /* sort the whole array the first time */
2648 if (getnc_state->la_sorted == NULL) {
2650 struct la_for_sorting *guid_array = talloc_array(getnc_state, struct la_for_sorting, getnc_state->la_count);
2651 if (guid_array == NULL) {
2652 DEBUG(0, ("Out of memory allocating %u linked attributes for sorting", getnc_state->la_count));
2653 return WERR_NOT_ENOUGH_MEMORY;
2655 for (j = 0; j < getnc_state->la_count; j++) {
2656 /* we need to get the target GUIDs to compare */
2658 const struct drsuapi_DsReplicaLinkedAttribute *la = &getnc_state->la_list[j];
2659 const struct dsdb_attribute *schema_attrib;
2660 const struct ldb_val *target_guid;
2661 DATA_BLOB source_guid;
2662 TALLOC_CTX *frame = talloc_stackframe();
2664 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2666 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, frame, la->value.blob, &dn);
2667 if (!W_ERROR_IS_OK(werr)) {
2668 DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2673 /* Extract the target GUID in NDR form */
2674 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2675 if (target_guid == NULL
2676 || target_guid->length != sizeof(guid_array[0].target_guid)) {
2677 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2679 /* Repack the source GUID as NDR for sorting */
2680 status = GUID_to_ndr_blob(&la->identifier->guid,
2685 if (!NT_STATUS_IS_OK(status)
2686 || source_guid.length != sizeof(guid_array[0].source_guid)) {
2687 DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2689 return ntstatus_to_werror(status);
2692 guid_array[j].link = &getnc_state->la_list[j];
2693 memcpy(guid_array[j].target_guid, target_guid->data,
2694 sizeof(guid_array[j].target_guid));
2695 memcpy(guid_array[j].source_guid, source_guid.data,
2696 sizeof(guid_array[j].source_guid));
2700 LDB_TYPESAFE_QSORT(guid_array, getnc_state->la_count, NULL, linked_attribute_compare);
2701 getnc_state->la_sorted = guid_array;
2704 link_count = getnc_state->la_count - getnc_state->la_idx;
2705 link_count = MIN(max_links, link_count);
2707 r->out.ctr->ctr6.linked_attributes_count = link_count;
2708 r->out.ctr->ctr6.linked_attributes = talloc_array(r->out.ctr, struct drsuapi_DsReplicaLinkedAttribute, link_count);
2709 if (r->out.ctr->ctr6.linked_attributes == NULL) {
2710 DEBUG(0, ("Out of memory allocating %u linked attributes for output", link_count));
2711 return WERR_NOT_ENOUGH_MEMORY;
2714 for (k = 0; k < link_count; k++) {
2715 r->out.ctr->ctr6.linked_attributes[k]
2716 = *getnc_state->la_sorted[getnc_state->la_idx + k].link;
2719 getnc_state->la_idx += link_count;
2720 link_given = getnc_state->la_idx;
2722 if (getnc_state->la_idx < getnc_state->la_count) {
2723 r->out.ctr->ctr6.more_data = true;
2727 if (req10->replica_flags & DRSUAPI_DRS_GET_NC_SIZE) {
2729 * TODO: This implementation is wrong
2730 * we should find out the total number of
2731 * objects and links in the whole naming context
2732 * at the start of the cycle and return these
2733 * values in each message.
2735 * For now we keep our current strategy and return
2736 * the number of objects for this cycle and the number
2737 * of links we found so far during the cycle.
2739 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2740 r->out.ctr->ctr6.nc_linked_attributes_count = getnc_state->la_count;
2743 if (!r->out.ctr->ctr6.more_data) {
2744 talloc_steal(mem_ctx, getnc_state->la_list);
2746 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2747 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2748 &getnc_state->final_udv);
2750 talloc_free(getnc_state);
2751 b_state->getncchanges_state = NULL;
2753 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2754 &r->out.ctr->ctr6.new_highwatermark);
2757 * We need to make sure that we never return the
2758 * same highwatermark within the same replication
2759 * cycle more than once. Otherwise we cannot detect
2760 * when the client uses an unexptected highwatermark.
2762 * This is a HACK which is needed because our
2763 * object ordering is wrong and set tmp_highest_usn
2764 * to a value that is higher than what we already
2765 * sent to the client (destination dsa).
2767 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2770 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2773 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2774 r->out.ctr->ctr6.uptodateness_vector = NULL;
2775 r->out.ctr->ctr6.nc_object_count = 0;
2776 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2779 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2780 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2781 (unsigned long long)(req10->highwatermark.highest_usn+1),
2782 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2783 r->out.ctr->ctr6.object_count,
2784 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2785 r->out.ctr->ctr6.linked_attributes_count,
2786 link_given, link_total,
2787 dom_sid_string(mem_ctx, user_sid)));
2790 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2791 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);