2 Unix SMB/CIFS implementation.
4 implement the DRSUpdateRefs call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
31 #include "rpc_server/dcerpc_server_proto.h"
32 #include "../libcli/drsuapi/drsuapi.h"
33 #include "libcli/security/security.h"
34 #include "lib/util/binsearch.h"
35 #include "lib/util/tsort.h"
36 #include "auth/session.h"
37 #include "dsdb/common/util.h"
40 build a DsReplicaObjectIdentifier from a ldb msg
42 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
43 struct ldb_message *msg)
45 struct drsuapi_DsReplicaObjectIdentifier *identifier;
48 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
49 if (identifier == NULL) {
53 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
54 identifier->guid = samdb_result_guid(msg, "objectGUID");
56 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
58 identifier->sid = *sid;
60 ZERO_STRUCT(identifier->sid);
65 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
67 return GUID_compare(guid1, &guid2);
71 see if we can filter an attribute using the uptodateness_vector
73 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
74 const struct GUID *originating_invocation_id,
75 uint64_t originating_usn)
77 const struct drsuapi_DsReplicaCursor *c;
78 if (udv == NULL) return false;
79 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
80 originating_invocation_id, udv_compare, c);
81 if (c && originating_usn <= c->highest_usn) {
89 drsuapi_DsGetNCChanges for one object
91 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
92 struct ldb_message *msg,
93 struct ldb_context *sam_ctx,
94 struct ldb_dn *ncRoot_dn,
96 struct dsdb_schema *schema,
97 DATA_BLOB *session_key,
99 uint32_t replica_flags,
100 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
101 enum drsuapi_DsExtendedOperation extended_op)
103 const struct ldb_val *md_value;
105 struct replPropertyMetaDataBlob md;
107 enum ndr_err_code ndr_err;
110 const struct dsdb_attribute *rdn_sa;
111 unsigned int instanceType;
112 struct dsdb_syntax_ctx syntax_ctx;
114 /* make dsdb sytanx context for conversions */
115 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
116 syntax_ctx.is_schema_nc = is_schema_nc;
118 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
119 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
120 obj->is_nc_prefix = true;
121 obj->parent_object_guid = NULL;
123 obj->is_nc_prefix = false;
124 obj->parent_object_guid = talloc(obj, struct GUID);
125 if (obj->parent_object_guid == NULL) {
126 return WERR_DS_DRA_INTERNAL_ERROR;
128 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
129 if (GUID_all_zero(obj->parent_object_guid)) {
130 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
131 ldb_dn_get_linearized(msg->dn)));
132 return WERR_DS_DRA_INTERNAL_ERROR;
135 obj->next_object = NULL;
137 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
139 /* nothing to send */
143 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
144 /* don't send uninstantiated objects */
148 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
149 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
150 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
151 return WERR_DS_DRA_INTERNAL_ERROR;
154 if (md.version != 1) {
155 return WERR_DS_DRA_INTERNAL_ERROR;
158 rdn = ldb_dn_get_rdn_name(msg->dn);
160 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
161 return WERR_DS_DRA_INTERNAL_ERROR;
164 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
165 if (rdn_sa == NULL) {
166 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
167 rdn, ldb_dn_get_linearized(msg->dn)));
168 return WERR_DS_DRA_INTERNAL_ERROR;
171 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
172 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
174 obj->object.identifier = get_object_identifier(obj, msg);
175 if (obj->object.identifier == NULL) {
178 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
180 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
181 for (n=i=0; i<md.ctr.ctr1.count; i++) {
182 const struct dsdb_attribute *sa;
183 bool force_attribute = false;
185 /* if the attribute has not changed, and it is not the
186 instanceType then don't include it */
187 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
188 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
189 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType) continue;
191 /* don't include the rDN */
192 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
194 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
196 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
197 (unsigned int)md.ctr.ctr1.array[i].attid,
198 ldb_dn_get_linearized(msg->dn)));
199 return WERR_DS_DRA_INTERNAL_ERROR;
203 struct ldb_message_element *el;
204 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
205 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
206 /* don't send upgraded links inline */
211 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
212 !dsdb_attr_in_rodc_fas(sa)) {
213 force_attribute = true;
214 DEBUG(4,("Forcing attribute %s in %s\n",
215 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
218 /* filter by uptodateness_vector */
219 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType &&
221 udv_filter(uptodateness_vector,
222 &md.ctr.ctr1.array[i].originating_invocation_id,
223 md.ctr.ctr1.array[i].originating_usn)) {
228 * If the recipient is a RODC, then we should only give
229 * attributes from the RODC filtered attribute set
231 * TODO: This is not strictly correct, as it doesn't allow for administrators
232 * to setup some users to transfer passwords to specific RODCs. To support that
233 * we would instead remove this check and rely on extended ACL checking in the dsdb
236 if (!(replica_flags & DRSUAPI_DRS_WRIT_REP) &&
238 !dsdb_attr_in_rodc_fas(sa)) {
239 DEBUG(4,("Skipping non-FAS attr %s in %s\n",
241 ldb_dn_get_linearized(msg->dn)));
245 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
246 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
247 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
248 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
249 attids[n] = md.ctr.ctr1.array[i].attid;
253 /* ignore it if its an empty change. Note that renames always
254 * change the 'name' attribute, so they won't be ignored by
257 (n == 1 && attids[0] == DRSUAPI_ATTRIBUTE_instanceType)) {
258 talloc_free(obj->meta_data_ctr);
259 obj->meta_data_ctr = NULL;
263 obj->meta_data_ctr->count = n;
265 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
266 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
267 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
268 obj->object.attribute_ctr.num_attributes);
271 * Note that the meta_data array and the attributes array must
272 * be the same size and in the same order
274 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
275 struct ldb_message_element *el;
277 const struct dsdb_attribute *sa;
279 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
281 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
282 return WERR_DS_DRA_INTERNAL_ERROR;
285 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
287 /* this happens for attributes that have been removed */
288 DEBUG(5,("No element '%s' for attributeID %u in message\n",
289 sa->lDAPDisplayName, attids[i]));
290 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
291 obj->object.attribute_ctr.attributes[i].attid =
292 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
294 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
295 &obj->object.attribute_ctr.attributes[i]);
296 if (!W_ERROR_IS_OK(werr)) {
297 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
298 sa->lDAPDisplayName, win_errstr(werr)));
301 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
302 * check if attribute is secret and send a null value
304 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
305 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
306 &obj->meta_data_ctr->meta_data[i]);
308 /* some attributes needs to be encrypted
310 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
311 &obj->object.attribute_ctr.attributes[i]);
312 if (!W_ERROR_IS_OK(werr)) {
313 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
314 sa->lDAPDisplayName, win_errstr(werr)));
325 add one linked attribute from an object to the list of linked
326 attributes in a getncchanges request
328 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
329 struct ldb_context *sam_ctx,
330 const struct dsdb_schema *schema,
331 const struct dsdb_attribute *sa,
332 struct ldb_message *msg,
333 struct dsdb_dn *dsdb_dn,
334 struct drsuapi_DsReplicaLinkedAttribute **la_list,
337 struct drsuapi_DsReplicaLinkedAttribute *la;
342 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
343 W_ERROR_HAVE_NO_MEMORY(*la_list);
345 la = &(*la_list)[*la_count];
347 la->identifier = get_object_identifier(*la_list, msg);
348 W_ERROR_HAVE_NO_MEMORY(la->identifier);
350 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
352 la->attid = sa->attributeID_id;
353 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
355 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
356 if (!NT_STATUS_IS_OK(status)) {
357 return ntstatus_to_werror(status);
359 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
360 if (!NT_STATUS_IS_OK(status)) {
361 return ntstatus_to_werror(status);
363 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
364 if (!NT_STATUS_IS_OK(status)) {
365 return ntstatus_to_werror(status);
367 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
368 if (!NT_STATUS_IS_OK(status)) {
369 return ntstatus_to_werror(status);
371 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
372 if (!NT_STATUS_IS_OK(status)) {
373 return ntstatus_to_werror(status);
376 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
377 W_ERROR_NOT_OK_RETURN(werr);
385 add linked attributes from an object to the list of linked
386 attributes in a getncchanges request
388 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
390 struct ldb_dn *ncRoot_dn,
391 struct dsdb_schema *schema,
392 uint64_t highest_usn,
393 uint32_t replica_flags,
394 struct ldb_message *msg,
395 struct drsuapi_DsReplicaLinkedAttribute **la_list,
397 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
400 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
401 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
403 for (i=0; i<msg->num_elements; i++) {
404 struct ldb_message_element *el = &msg->elements[i];
405 const struct dsdb_attribute *sa;
408 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
410 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
411 /* we only want forward links */
415 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
416 /* its an old style link, it will have been
417 * sent in the main replication data */
421 for (j=0; j<el->num_values; j++) {
422 struct dsdb_dn *dsdb_dn;
427 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
428 if (dsdb_dn == NULL) {
429 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
430 el->name, ldb_dn_get_linearized(msg->dn)));
431 talloc_free(tmp_ctx);
432 return WERR_DS_DRA_INTERNAL_ERROR;
435 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
436 if (!NT_STATUS_IS_OK(status)) {
437 /* this can happen for attributes
438 given to us with old style meta
443 if (local_usn > uSNChanged) {
444 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
445 el->name, ldb_dn_get_linearized(msg->dn)));
446 talloc_free(tmp_ctx);
447 return WERR_DS_DRA_INTERNAL_ERROR;
450 if (local_usn < highest_usn) {
454 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
455 dsdb_dn, la_list, la_count);
456 if (!W_ERROR_IS_OK(werr)) {
457 talloc_free(tmp_ctx);
463 talloc_free(tmp_ctx);
468 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
470 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
471 struct ldb_dn *ncRoot_dn,
472 struct drsuapi_DsReplicaCursor2CtrEx *udv)
480 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
481 if (ret != LDB_SUCCESS) {
482 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
483 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
484 return WERR_DS_DRA_INTERNAL_ERROR;
491 /* comparison function for linked attributes - see CompareLinks() in
492 * MS-DRSR section 4.1.10.5.17 */
493 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
494 const struct drsuapi_DsReplicaLinkedAttribute *la2,
495 struct ldb_context *sam_ctx)
500 const struct dsdb_schema *schema;
501 const struct dsdb_attribute *schema_attrib;
502 struct dsdb_dn *dn1, *dn2;
503 struct GUID guid1, guid2;
506 c = GUID_compare(&la1->identifier->guid,
507 &la2->identifier->guid);
508 if (c != 0) return c;
510 if (la1->attid != la2->attid) {
511 return la1->attid < la2->attid? -1:1;
514 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
515 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
516 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
519 /* we need to get the target GUIDs to compare */
520 tmp_ctx = talloc_new(sam_ctx);
522 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
523 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
525 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
526 if (!W_ERROR_IS_OK(werr)) {
527 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
528 talloc_free(tmp_ctx);
532 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
533 if (!W_ERROR_IS_OK(werr)) {
534 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
535 talloc_free(tmp_ctx);
539 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
540 if (!NT_STATUS_IS_OK(status)) {
541 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
542 talloc_free(tmp_ctx);
545 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
546 if (!NT_STATUS_IS_OK(status)) {
547 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
548 talloc_free(tmp_ctx);
552 talloc_free(tmp_ctx);
554 return GUID_compare(&guid1, &guid2);
559 sort the objects we send by tree order
561 static int site_res_cmp_parent_order(struct ldb_message **m1, struct ldb_message **m2)
563 return ldb_dn_compare((*m2)->dn, (*m1)->dn);
567 sort the objects we send first by uSNChanged
569 static int site_res_cmp_usn_order(struct ldb_message **m1, struct ldb_message **m2)
571 unsigned usnchanged1, usnchanged2;
573 cn1 = ldb_dn_get_comp_num((*m1)->dn);
574 cn2 = ldb_dn_get_comp_num((*m2)->dn);
576 return cn1 > cn2 ? 1 : -1;
578 usnchanged1 = ldb_msg_find_attr_as_uint(*m1, "uSNChanged", 0);
579 usnchanged2 = ldb_msg_find_attr_as_uint(*m2, "uSNChanged", 0);
580 if (usnchanged1 == usnchanged2) {
583 return usnchanged1 > usnchanged2 ? 1 : -1;
588 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
590 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
592 struct drsuapi_DsGetNCChangesRequest8 *req8,
593 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
595 struct ldb_dn *rid_manager_dn, *fsmo_role_dn, *req_dn;
597 struct ldb_context *ldb = b_state->sam_ctx;
598 struct ldb_result *ext_res;
599 struct ldb_dn *base_dn;
600 struct dsdb_fsmo_extended_op *exop;
604 - verify that the DN being asked for is the RID Manager DN
605 - verify that we are the RID Manager
608 /* work out who is the RID Manager */
609 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
610 if (ret != LDB_SUCCESS) {
611 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
612 return WERR_DS_DRA_INTERNAL_ERROR;
615 req_dn = ldb_dn_new(mem_ctx, ldb, req8->naming_context->dn);
617 !ldb_dn_validate(req_dn) ||
618 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
619 /* that isn't the RID Manager DN */
620 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
621 req8->naming_context->dn));
622 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
626 /* find the DN of the RID Manager */
627 ret = samdb_reference_dn(ldb, mem_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
628 if (ret != LDB_SUCCESS) {
629 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s\n",
630 ldb_errstring(ldb)));
631 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
632 return WERR_DS_DRA_INTERNAL_ERROR;
635 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
636 /* we're not the RID Manager - go away */
637 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
638 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
642 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
643 W_ERROR_HAVE_NO_MEMORY(exop);
645 exop->fsmo_info = req8->fsmo_info;
646 exop->destination_dsa_guid = req8->destination_dsa_guid;
648 ret = ldb_transaction_start(ldb);
649 if (ret != LDB_SUCCESS) {
650 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
651 ldb_errstring(ldb)));
652 return WERR_DS_DRA_INTERNAL_ERROR;
655 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
656 if (ret != LDB_SUCCESS) {
657 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
658 ldb_errstring(ldb)));
659 ldb_transaction_cancel(ldb);
660 return WERR_DS_DRA_INTERNAL_ERROR;
663 ret = ldb_transaction_commit(ldb);
664 if (ret != LDB_SUCCESS) {
665 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
666 ldb_errstring(ldb)));
667 return WERR_DS_DRA_INTERNAL_ERROR;
670 talloc_free(ext_res);
672 base_dn = ldb_get_default_basedn(ldb);
674 DEBUG(2,("Allocated RID pool for server %s\n",
675 GUID_string(mem_ctx, &req8->destination_dsa_guid)));
677 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
683 return an array of SIDs from a ldb_message given an attribute name
684 assumes the SIDs are in extended DN format
686 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
687 struct ldb_message *msg,
690 const struct dom_sid ***sids)
692 struct ldb_message_element *el;
695 el = ldb_msg_find_element(msg, attr);
701 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
702 W_ERROR_HAVE_NO_MEMORY(*sids);
704 for (i=0; i<el->num_values; i++) {
705 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
709 sid = talloc(*sids, struct dom_sid);
710 W_ERROR_HAVE_NO_MEMORY(sid);
711 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
712 if (!NT_STATUS_IS_OK(status)) {
713 return WERR_INTERNAL_DB_CORRUPTION;
724 return an array of SIDs from a ldb_message given an attribute name
725 assumes the SIDs are in NDR form
727 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
728 struct ldb_message *msg,
731 const struct dom_sid ***sids)
733 struct ldb_message_element *el;
736 el = ldb_msg_find_element(msg, attr);
742 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
743 W_ERROR_HAVE_NO_MEMORY(*sids);
745 for (i=0; i<el->num_values; i++) {
746 enum ndr_err_code ndr_err;
749 sid = talloc(*sids, struct dom_sid);
750 W_ERROR_HAVE_NO_MEMORY(sid);
752 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
753 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
754 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
755 return WERR_INTERNAL_DB_CORRUPTION;
765 see if any SIDs in list1 are in list2
767 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
770 /* do we ever have enough SIDs here to worry about O(n^2) ? */
771 for (i=0; list1[i]; i++) {
772 for (j=0; list2[j]; j++) {
773 if (dom_sid_equal(list1[i], list2[j])) {
782 handle a DRSUAPI_EXOP_REPL_SECRET call
784 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
786 struct drsuapi_DsGetNCChangesRequest8 *req8,
787 struct dom_sid *user_sid,
788 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
790 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req8->naming_context;
791 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
793 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
794 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
795 struct ldb_result *rodc_res, *obj_res;
796 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
799 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n", ncRoot->dn));
802 * we need to work out if we will allow this RODC to
803 * replicate the secrets for this object
805 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
809 if (b_state->sam_ctx_system == NULL) {
810 /* this operation needs system level access */
811 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
812 return WERR_DS_DRA_SOURCE_DISABLED;
815 obj_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx_system, ncRoot->dn);
816 if (!ldb_dn_validate(obj_dn)) goto failed;
818 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
819 dom_sid_string(mem_ctx, user_sid));
820 if (!ldb_dn_validate(rodc_dn)) goto failed;
822 /* do the two searches we need */
823 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
824 DSDB_SEARCH_SHOW_EXTENDED_DN);
825 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
827 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
828 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
830 /* if the object SID is equal to the user_sid, allow */
831 if (dom_sid_equal(user_sid,
832 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
836 /* an RODC is allowed to get its own krbtgt account secrets */
837 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
838 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
839 if (krbtgt_link_dn != NULL &&
840 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
844 /* but it isn't allowed to get anyone elses krbtgt secrets */
845 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
846 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
850 if (samdb_result_uint(obj_res->msgs[0], "UserAccountControl", 0) &
851 UF_INTERDOMAIN_TRUST_ACCOUNT) {
855 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
856 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
857 if (!W_ERROR_IS_OK(werr)) {
861 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
862 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
863 if (!W_ERROR_IS_OK(werr)) {
867 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
868 mem_ctx, "tokenGroups", &token_sids);
869 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
873 if (never_reveal_sids &&
874 sid_list_match(token_sids, never_reveal_sids)) {
879 sid_list_match(token_sids, reveal_sids)) {
885 DEBUG(2,(__location__ ": Denied RODC secret replication for %s by RODC %s\n",
886 ncRoot->dn, ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
887 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
888 return WERR_DS_DRA_ACCESS_DENIED;
891 DEBUG(2,(__location__ ": Allowed RODC secret replication for %s by RODC %s\n",
892 ncRoot->dn, ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
893 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
894 req8->highwatermark.highest_usn = 0;
898 DEBUG(2,(__location__ ": Failed RODC secret replication for %s by RODC %s\n",
899 ncRoot->dn, dom_sid_string(mem_ctx, user_sid)));
900 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
901 return WERR_DS_DRA_BAD_DN;
906 /* state of a partially completed getncchanges call */
907 struct drsuapi_getncchanges_state {
908 struct ldb_result *site_res;
910 struct ldb_dn *ncRoot_dn;
913 uint64_t highest_usn;
914 struct ldb_dn *last_dn;
915 struct drsuapi_DsReplicaLinkedAttribute *la_list;
919 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
923 drsuapi_DsGetNCChanges
925 see MS-DRSR 4.1.10.5.2 for basic logic of this function
927 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
928 struct drsuapi_DsGetNCChanges *r)
930 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
933 struct dsdb_schema *schema;
934 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
935 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
937 DATA_BLOB session_key;
938 const char *attrs[] = { "*", "distinguishedName",
939 "nTSecurityDescriptor",
941 "replPropertyMetaData",
946 "supplementalCredentials",
949 struct dcesrv_handle *h;
950 struct drsuapi_bind_state *b_state;
951 struct drsuapi_getncchanges_state *getnc_state;
952 struct drsuapi_DsGetNCChangesRequest8 *req8;
954 uint32_t max_objects;
956 uint32_t link_count = 0;
957 uint32_t link_total = 0;
958 uint32_t link_given = 0;
959 struct ldb_dn *search_dn = NULL;
960 bool am_rodc, null_scope=false;
961 enum security_user_level security_level;
962 struct ldb_context *sam_ctx;
963 struct dom_sid *user_sid;
965 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
968 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
970 *r->out.level_out = 6;
971 /* TODO: linked attributes*/
972 r->out.ctr->ctr6.linked_attributes_count = 0;
973 r->out.ctr->ctr6.linked_attributes = NULL;
975 r->out.ctr->ctr6.object_count = 0;
976 r->out.ctr->ctr6.nc_object_count = 0;
977 r->out.ctr->ctr6.more_data = false;
978 r->out.ctr->ctr6.uptodateness_vector = NULL;
980 /* a RODC doesn't allow for any replication */
981 ret = samdb_rodc(sam_ctx, &am_rodc);
982 if (ret == LDB_SUCCESS && am_rodc) {
983 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
984 return WERR_DS_DRA_SOURCE_DISABLED;
987 /* Check request revision.
988 TODO: Adding mappings to req8 from the other levels
990 if (r->in.level != 8) {
991 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
993 return WERR_REVISION_MISMATCH;
996 req8 = &r->in.req->req8;
998 /* Perform access checks. */
999 /* TODO: we need to support a sync on a specific non-root
1000 * DN. We'll need to find the real partition root here */
1001 ncRoot = req8->naming_context;
1002 if (ncRoot == NULL) {
1003 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1004 return WERR_DS_DRA_INVALID_PARAMETER;
1007 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1008 return WERR_DS_DRA_INTERNAL_ERROR;
1011 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1012 !(req8->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1013 return WERR_DS_DRA_SOURCE_DISABLED;
1016 werr = drs_security_level_check(dce_call, "DsGetNCChanges", SECURITY_RO_DOMAIN_CONTROLLER,
1017 samdb_domain_sid(sam_ctx));
1018 if (!W_ERROR_IS_OK(werr)) {
1022 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1025 /* for non-administrator replications, check that they have
1026 given the correct source_dsa_invocation_id */
1027 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1028 samdb_domain_sid(sam_ctx));
1029 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER &&
1030 req8->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1031 /* we rely on this flag being unset for RODC requests */
1032 req8->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1036 if (req8->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1037 /* Ignore the _in_ uptpdateness vector*/
1038 req8->uptodateness_vector = NULL;
1041 /* we don't yet support extended operations */
1042 switch (req8->extended_op) {
1043 case DRSUAPI_EXOP_NONE:
1046 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1047 werr = getncchanges_rid_alloc(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1048 W_ERROR_NOT_OK_RETURN(werr);
1049 search_dn = ldb_get_default_basedn(sam_ctx);
1052 case DRSUAPI_EXOP_REPL_SECRET:
1053 werr = getncchanges_repl_secret(b_state, mem_ctx, req8, user_sid, &r->out.ctr->ctr6);
1054 r->out.result = werr;
1055 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
1056 W_ERROR_NOT_OK_RETURN(werr);
1059 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1060 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1061 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1062 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1063 case DRSUAPI_EXOP_REPL_OBJ:
1064 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1065 (unsigned)req8->extended_op));
1066 return WERR_DS_DRA_NOT_SUPPORTED;
1069 getnc_state = b_state->getncchanges_state;
1071 /* see if a previous replication has been abandoned */
1073 struct ldb_dn *new_dn = ldb_dn_new(getnc_state, sam_ctx, ncRoot->dn);
1074 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1075 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1076 ldb_dn_get_linearized(new_dn),
1077 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1078 ldb_dn_get_linearized(getnc_state->last_dn)));
1079 talloc_free(getnc_state);
1084 if (getnc_state == NULL) {
1085 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1086 if (getnc_state == NULL) {
1089 b_state->getncchanges_state = getnc_state;
1090 getnc_state->ncRoot_dn = ldb_dn_new(getnc_state, sam_ctx, ncRoot->dn);
1092 /* find out if we are to replicate Schema NC */
1093 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1094 ldb_get_schema_basedn(b_state->sam_ctx));
1095 getnc_state->is_schema_nc = (0 == ret);
1098 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1099 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1100 DEBUG(0,(__location__ ": Bad DN '%s'\n", ncRoot->dn));
1101 return WERR_DS_DRA_INVALID_PARAMETER;
1104 /* we need the session key for encrypting password attributes */
1105 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1106 if (!NT_STATUS_IS_OK(status)) {
1107 DEBUG(0,(__location__ ": Failed to get session key\n"));
1108 return WERR_DS_DRA_INTERNAL_ERROR;
1112 TODO: MS-DRSR section 4.1.10.1.1
1113 Work out if this is the start of a new cycle */
1115 if (getnc_state->site_res == NULL) {
1116 char* search_filter;
1117 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1118 const char *extra_filter;
1120 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1122 getnc_state->min_usn = req8->highwatermark.highest_usn;
1124 /* Construct response. */
1125 search_filter = talloc_asprintf(mem_ctx,
1126 "(uSNChanged>=%llu)",
1127 (unsigned long long)(getnc_state->min_usn+1));
1130 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1133 if (req8->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1134 search_filter = talloc_asprintf(mem_ctx,
1135 "(&%s(isCriticalSystemObject=TRUE))",
1139 if (req8->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1140 scope = LDB_SCOPE_BASE;
1144 search_dn = getnc_state->ncRoot_dn;
1147 DEBUG(1,(__location__ ": getncchanges on %s using filter %s\n",
1148 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1149 ret = drsuapi_search_with_extended_dn(sam_ctx, getnc_state, &getnc_state->site_res,
1150 search_dn, scope, attrs,
1152 if (ret != LDB_SUCCESS) {
1153 return WERR_DS_DRA_INTERNAL_ERROR;
1156 if (req8->replica_flags & DRSUAPI_DRS_GET_ANC) {
1157 TYPESAFE_QSORT(getnc_state->site_res->msgs,
1158 getnc_state->site_res->count,
1159 site_res_cmp_parent_order);
1161 TYPESAFE_QSORT(getnc_state->site_res->msgs,
1162 getnc_state->site_res->count,
1163 site_res_cmp_usn_order);
1166 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req8->uptodateness_vector);
1167 if (getnc_state->uptodateness_vector) {
1168 /* make sure its sorted */
1169 TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1170 getnc_state->uptodateness_vector->count,
1171 drsuapi_DsReplicaCursor_compare);
1175 /* Prefix mapping */
1176 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1178 DEBUG(0,("No schema in sam_ctx\n"));
1179 return WERR_DS_DRA_INTERNAL_ERROR;
1182 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1183 *r->out.ctr->ctr6.naming_context = *ncRoot;
1185 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1186 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1187 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1188 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1189 return WERR_DS_DRA_INTERNAL_ERROR;
1192 /* find the SID if there is one */
1193 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1195 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1196 r->out.ctr->ctr6.mapping_ctr = *ctr;
1198 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1199 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1201 r->out.ctr->ctr6.old_highwatermark = req8->highwatermark;
1202 r->out.ctr->ctr6.new_highwatermark = req8->highwatermark;
1204 r->out.ctr->ctr6.first_object = NULL;
1205 currentObject = &r->out.ctr->ctr6.first_object;
1207 /* use this to force single objects at a time, which is useful
1208 * for working out what object is giving problems
1210 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1211 if (req8->max_object_count < max_objects) {
1212 max_objects = req8->max_object_count;
1215 * TODO: work out how the maximum should be calculated
1217 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1219 for (i=getnc_state->num_sent;
1220 i<getnc_state->site_res->count &&
1222 (r->out.ctr->ctr6.object_count < max_objects);
1225 struct drsuapi_DsReplicaObjectListItemEx *obj;
1226 struct ldb_message *msg = getnc_state->site_res->msgs[i];
1228 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1230 werr = get_nc_changes_build_object(obj, msg,
1231 sam_ctx, getnc_state->ncRoot_dn,
1232 getnc_state->is_schema_nc,
1233 schema, &session_key, getnc_state->min_usn,
1234 req8->replica_flags, getnc_state->uptodateness_vector,
1236 if (!W_ERROR_IS_OK(werr)) {
1240 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1241 getnc_state->ncRoot_dn,
1242 schema, getnc_state->min_usn,
1243 req8->replica_flags,
1245 &getnc_state->la_list,
1246 &getnc_state->la_count,
1247 getnc_state->uptodateness_vector);
1248 if (!W_ERROR_IS_OK(werr)) {
1252 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1253 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1254 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1256 if (uSN > getnc_state->highest_usn) {
1257 getnc_state->highest_usn = uSN;
1260 if (obj->meta_data_ctr == NULL) {
1261 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1262 ldb_dn_get_linearized(msg->dn)));
1263 /* no attributes to send */
1268 r->out.ctr->ctr6.object_count++;
1270 *currentObject = obj;
1271 currentObject = &obj->next_object;
1273 talloc_free(getnc_state->last_dn);
1274 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1276 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1279 getnc_state->num_sent += r->out.ctr->ctr6.object_count;
1281 r->out.ctr->ctr6.nc_object_count = getnc_state->site_res->count;
1283 /* the client can us to call UpdateRefs on its behalf to
1284 re-establish monitoring of the NC */
1285 if ((req8->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1286 !GUID_all_zero(&req8->destination_dsa_guid)) {
1287 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1288 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1289 GUID_string(mem_ctx, &req8->destination_dsa_guid)));
1290 ureq.naming_context = ncRoot;
1291 ureq.dest_dsa_dns_name = talloc_asprintf(mem_ctx, "%s._msdcs.%s",
1292 GUID_string(mem_ctx, &req8->destination_dsa_guid),
1293 lpcfg_realm(dce_call->conn->dce_ctx->lp_ctx));
1294 if (!ureq.dest_dsa_dns_name) {
1297 ureq.dest_dsa_guid = req8->destination_dsa_guid;
1298 ureq.options = DRSUAPI_DRS_ADD_REF |
1299 DRSUAPI_DRS_ASYNC_OP |
1300 DRSUAPI_DRS_GETCHG_CHECK;
1301 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1302 if (!W_ERROR_IS_OK(werr)) {
1303 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1310 * This is just a guess, how to calculate the
1311 * number of linked attributes to send, we need to
1312 * find out how to do this right.
1314 if (r->out.ctr->ctr6.object_count >= max_links) {
1317 max_links -= r->out.ctr->ctr6.object_count;
1320 link_total = getnc_state->la_count;
1322 if (i < getnc_state->site_res->count) {
1323 r->out.ctr->ctr6.more_data = true;
1325 /* sort the whole array the first time */
1326 if (!getnc_state->la_sorted) {
1327 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1328 sam_ctx, linked_attribute_compare);
1329 getnc_state->la_sorted = true;
1332 link_count = getnc_state->la_count - getnc_state->la_idx;
1333 link_count = MIN(max_links, link_count);
1335 r->out.ctr->ctr6.linked_attributes_count = link_count;
1336 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1338 getnc_state->la_idx += link_count;
1339 link_given = getnc_state->la_idx;
1341 if (getnc_state->la_idx < getnc_state->la_count) {
1342 r->out.ctr->ctr6.more_data = true;
1346 if (!r->out.ctr->ctr6.more_data) {
1347 talloc_steal(mem_ctx, getnc_state->la_list);
1349 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1350 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1352 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1353 r->out.ctr->ctr6.uptodateness_vector);
1354 if (!W_ERROR_IS_OK(werr)) {
1358 talloc_free(getnc_state);
1359 b_state->getncchanges_state = NULL;
1362 if (req8->extended_op != DRSUAPI_EXOP_NONE) {
1363 r->out.ctr->ctr6.uptodateness_vector = NULL;
1364 r->out.ctr->ctr6.nc_object_count = 0;
1365 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1368 DEBUG(r->out.ctr->ctr6.more_data?2:1,
1369 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u)\n",
1370 (unsigned long long)(req8->highwatermark.highest_usn+1),
1371 req8->replica_flags, ncRoot->dn,
1372 r->out.ctr->ctr6.object_count,
1373 i, r->out.ctr->ctr6.more_data?getnc_state->site_res->count:i,
1374 r->out.ctr->ctr6.linked_attributes_count,
1375 link_given, link_total));
1378 if (!r->out.ctr->ctr6.more_data && req8->extended_op != DRSUAPI_EXOP_NONE) {
1379 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);