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 "rpc_server/drsuapi/dcesrv_drsuapi.h"
30 #include "rpc_server/dcerpc_server_proto.h"
31 #include "../libcli/drsuapi/drsuapi.h"
32 #include "libcli/security/security.h"
35 build a DsReplicaObjectIdentifier from a ldb msg
37 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
38 struct ldb_message *msg)
40 struct drsuapi_DsReplicaObjectIdentifier *identifier;
43 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
44 if (identifier == NULL) {
48 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
49 identifier->guid = samdb_result_guid(msg, "objectGUID");
51 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
53 identifier->sid = *sid;
55 ZERO_STRUCT(identifier->sid);
61 drsuapi_DsGetNCChanges for one object
63 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
64 struct ldb_message *msg,
65 struct ldb_context *sam_ctx,
66 struct ldb_dn *ncRoot_dn,
67 struct dsdb_schema *schema,
68 DATA_BLOB *session_key,
70 uint32_t replica_flags)
72 const struct ldb_val *md_value;
74 struct replPropertyMetaDataBlob md;
76 enum ndr_err_code ndr_err;
79 const struct dsdb_attribute *rdn_sa;
81 if (ldb_dn_compare(ncRoot_dn, msg->dn) == 0) {
82 obj->is_nc_prefix = true;
83 obj->parent_object_guid = NULL;
85 obj->is_nc_prefix = false;
86 obj->parent_object_guid = talloc(obj, struct GUID);
87 if (obj->parent_object_guid == NULL) {
88 return WERR_DS_DRA_INTERNAL_ERROR;
90 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
91 if (GUID_all_zero(obj->parent_object_guid)) {
92 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
93 ldb_dn_get_linearized(msg->dn)));
94 return WERR_DS_DRA_INTERNAL_ERROR;
97 obj->next_object = NULL;
99 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
101 /* nothing to send */
105 ndr_err = ndr_pull_struct_blob(md_value, obj,
106 lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")), &md,
107 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
108 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
109 return WERR_DS_DRA_INTERNAL_ERROR;
112 if (md.version != 1) {
113 return WERR_DS_DRA_INTERNAL_ERROR;
116 rdn = ldb_dn_get_rdn_name(msg->dn);
118 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
119 return WERR_DS_DRA_INTERNAL_ERROR;
122 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
123 if (rdn_sa == NULL) {
124 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
125 rdn, ldb_dn_get_linearized(msg->dn)));
126 return WERR_DS_DRA_INTERNAL_ERROR;
129 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
130 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
132 obj->object.identifier = get_object_identifier(obj, msg);
133 if (obj->object.identifier == NULL) {
136 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
138 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
139 for (n=i=0; i<md.ctr.ctr1.count; i++) {
140 const struct dsdb_attribute *sa;
141 /* if the attribute has not changed, and it is not the
142 instanceType then don't include it */
143 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
144 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType) continue;
146 /* don't include the rDN */
147 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
149 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
151 struct ldb_message_element *el;
152 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
153 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
154 /* don't send upgraded links inline */
159 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
160 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
161 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
162 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
163 attids[n] = md.ctr.ctr1.array[i].attid;
168 note that if n==0 we still need to send the change, as it
169 could be a rename, which changes the uSNChanged, but not any
170 of the replicated attributes
173 obj->meta_data_ctr->count = n;
175 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
176 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
177 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
178 obj->object.attribute_ctr.num_attributes);
181 * Note that the meta_data array and the attributes array must
182 * be the same size and in the same order
184 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
185 struct ldb_message_element *el;
187 const struct dsdb_attribute *sa;
189 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
191 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
192 return WERR_DS_DRA_INTERNAL_ERROR;
195 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
197 /* this happens for attributes that have been removed */
198 DEBUG(5,("No element '%s' for attributeID %u in message\n",
199 sa->lDAPDisplayName, attids[i]));
200 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
201 obj->object.attribute_ctr.attributes[i].attid = attids[i];
203 werr = dsdb_attribute_ldb_to_drsuapi(sam_ctx, schema, el, obj,
204 &obj->object.attribute_ctr.attributes[i]);
205 if (!W_ERROR_IS_OK(werr)) {
206 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
207 sa->lDAPDisplayName, win_errstr(werr)));
210 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
211 * check if attribute is secret and send a null value
213 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
214 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
215 &obj->meta_data_ctr->meta_data[i]);
217 /* some attributes needs to be encrypted
219 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
220 &obj->object.attribute_ctr.attributes[i]);
221 if (!W_ERROR_IS_OK(werr)) {
222 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
223 sa->lDAPDisplayName, win_errstr(werr)));
234 add one linked attribute from an object to the list of linked
235 attributes in a getncchanges request
237 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
238 struct ldb_context *sam_ctx,
239 const struct dsdb_schema *schema,
240 const struct dsdb_attribute *sa,
241 struct ldb_message *msg,
242 struct dsdb_dn *dsdb_dn,
243 struct drsuapi_DsReplicaLinkedAttribute **la_list,
246 struct drsuapi_DsReplicaLinkedAttribute *la;
251 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
252 W_ERROR_HAVE_NO_MEMORY(*la_list);
254 la = &(*la_list)[*la_count];
256 la->identifier = get_object_identifier(*la_list, msg);
257 W_ERROR_HAVE_NO_MEMORY(la->identifier);
259 active = ldb_dn_get_extended_component(dsdb_dn->dn, "DELETED")?false:true;
261 la->attid = sa->attributeID_id;
262 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
264 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
265 if (!NT_STATUS_IS_OK(status)) {
266 return ntstatus_to_werror(status);
268 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
269 if (!NT_STATUS_IS_OK(status)) {
270 return ntstatus_to_werror(status);
272 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
273 if (!NT_STATUS_IS_OK(status)) {
274 return ntstatus_to_werror(status);
276 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
277 if (!NT_STATUS_IS_OK(status)) {
278 return ntstatus_to_werror(status);
280 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
281 if (!NT_STATUS_IS_OK(status)) {
282 return ntstatus_to_werror(status);
285 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
286 W_ERROR_NOT_OK_RETURN(werr);
294 add linked attributes from an object to the list of linked
295 attributes in a getncchanges request
297 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
299 struct ldb_dn *ncRoot_dn,
300 struct dsdb_schema *schema,
301 uint64_t highest_usn,
302 uint32_t replica_flags,
303 struct ldb_message *msg,
304 struct drsuapi_DsReplicaLinkedAttribute **la_list,
308 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
309 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
311 for (i=0; i<msg->num_elements; i++) {
312 struct ldb_message_element *el = &msg->elements[i];
313 const struct dsdb_attribute *sa;
316 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
318 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
319 /* we only want forward links */
323 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
324 /* its an old style link, it will have been
325 * sent in the main replication data */
329 for (j=0; j<el->num_values; j++) {
330 struct dsdb_dn *dsdb_dn;
335 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
336 if (dsdb_dn == NULL) {
337 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
338 el->name, ldb_dn_get_linearized(msg->dn)));
339 talloc_free(tmp_ctx);
340 return WERR_DS_DRA_INTERNAL_ERROR;
343 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
344 if (!NT_STATUS_IS_OK(status)) {
345 /* this can happen for attributes
346 given to us with old style meta
351 if (local_usn > uSNChanged) {
352 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
353 el->name, ldb_dn_get_linearized(msg->dn)));
354 talloc_free(tmp_ctx);
355 return WERR_DS_DRA_INTERNAL_ERROR;
358 if (local_usn < highest_usn) {
362 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
363 dsdb_dn, la_list, la_count);
364 if (!W_ERROR_IS_OK(werr)) {
365 talloc_free(tmp_ctx);
371 talloc_free(tmp_ctx);
376 load replUpToDateVector from a DN
378 static WERROR load_udv(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
379 struct ldb_dn *dn, struct replUpToDateVectorBlob *ouv)
381 const char *attrs[] = { "replUpToDateVector", NULL };
382 struct ldb_result *res = NULL;
383 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
384 struct ldb_message_element *el;
385 enum ndr_err_code ndr_err;
389 if (ldb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, NULL) != LDB_SUCCESS ||
391 DEBUG(0,("load_udv: failed to read partition object\n"));
392 talloc_free(tmp_ctx);
393 return WERR_DS_DRA_INTERNAL_ERROR;
396 el = ldb_msg_find_element(res->msgs[0], "replUpToDateVector");
397 if (el == NULL || el->num_values < 1) {
398 talloc_free(tmp_ctx);
403 ndr_err = ndr_pull_struct_blob(&el->values[0],
404 mem_ctx, lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")),
406 (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
407 talloc_free(tmp_ctx);
408 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
409 DEBUG(0,(__location__ ": Failed to parse replUpToDateVector for %s\n",
410 ldb_dn_get_linearized(dn)));
411 return WERR_DS_DRA_INTERNAL_ERROR;
419 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
421 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
422 struct ldb_dn *ncRoot_dn,
423 struct drsuapi_DsReplicaCursor2CtrEx *udv,
427 struct drsuapi_DsReplicaCursor2 *tmp_cursor;
429 time_t t = time(NULL);
430 struct replUpToDateVectorBlob ouv;
433 werr = load_udv(sam_ctx, udv, ncRoot_dn, &ouv);
434 if (!W_ERROR_IS_OK(werr)) {
438 tmp_cursor = talloc(udv, struct drsuapi_DsReplicaCursor2);
439 tmp_cursor->source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
440 tmp_cursor->highest_usn = highestUSN;
441 unix_to_nt_time(&now, t);
442 tmp_cursor->last_sync_success = now;
444 udv->count = ouv.ctr.ctr2.count;
445 udv->cursors = talloc_steal(udv, ouv.ctr.ctr2.cursors);
447 for (i=0; i<udv->count; i++) {
448 if (GUID_equal(&tmp_cursor->source_dsa_invocation_id,
449 &udv->cursors[i].source_dsa_invocation_id)) {
450 udv->cursors[i] = *tmp_cursor;
454 if (i == udv->count) {
455 udv->cursors = talloc_realloc(udv, udv->cursors, struct drsuapi_DsReplicaCursor2, udv->count+1);
457 return WERR_DS_DRA_INTERNAL_ERROR;
459 udv->cursors[udv->count] = *tmp_cursor;
463 qsort(udv->cursors, udv->count,
464 sizeof(struct drsuapi_DsReplicaCursor2),
465 (comparison_fn_t)drsuapi_DsReplicaCursor2_compare);
471 sort the objects we send by tree order
473 static int site_res_cmp_parent_order(const struct ldb_message **m1, const struct ldb_message **m2)
475 return ldb_dn_compare((*m2)->dn, (*m1)->dn);
479 sort the objects we send first by uSNChanged
481 static int site_res_cmp_usn_order(const struct ldb_message **m1, const struct ldb_message **m2)
483 unsigned usnchanged1, usnchanged2;
485 cn1 = ldb_dn_get_comp_num((*m1)->dn);
486 cn2 = ldb_dn_get_comp_num((*m2)->dn);
488 return cn1 > cn2 ? 1 : -1;
490 usnchanged1 = ldb_msg_find_attr_as_uint(*m1, "uSNChanged", 0);
491 usnchanged2 = ldb_msg_find_attr_as_uint(*m2, "uSNChanged", 0);
492 if (usnchanged1 == usnchanged2) {
495 return usnchanged1 > usnchanged2 ? 1 : -1;
499 /* state of a partially completed getncchanges call */
500 struct drsuapi_getncchanges_state {
501 struct ldb_result *site_res;
503 struct ldb_dn *ncRoot_dn;
505 uint64_t highest_usn;
506 struct ldb_dn *last_dn;
507 struct drsuapi_DsReplicaLinkedAttribute *la_list;
512 drsuapi_DsGetNCChanges
514 see MS-DRSR 4.1.10.5.2 for basic logic of this function
516 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
517 struct drsuapi_DsGetNCChanges *r)
519 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
522 struct dsdb_schema *schema;
523 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
524 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
526 DATA_BLOB session_key;
527 const char *attrs[] = { "*", "distinguishedName",
528 "nTSecurityDescriptor",
530 "replPropertyMetaData",
535 "supplementalCredentials",
538 struct dcesrv_handle *h;
539 struct drsuapi_bind_state *b_state;
540 struct drsuapi_getncchanges_state *getnc_state;
541 struct drsuapi_DsGetNCChangesRequest8 *req8;
543 uint32_t max_objects;
545 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
548 *r->out.level_out = 6;
549 /* TODO: linked attributes*/
550 r->out.ctr->ctr6.linked_attributes_count = 0;
551 r->out.ctr->ctr6.linked_attributes = NULL;
553 r->out.ctr->ctr6.object_count = 0;
554 r->out.ctr->ctr6.nc_object_count = 0;
555 r->out.ctr->ctr6.more_data = false;
556 r->out.ctr->ctr6.uptodateness_vector = NULL;
558 /* a RODC doesn't allow for any replication */
559 if (samdb_rodc(ldb_get_opaque(b_state->sam_ctx, "loadparm"))) {
560 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
561 return WERR_DS_DRA_SOURCE_DISABLED;
564 /* Check request revision.
565 TODO: Adding mappings to req8 from the other levels
567 if (r->in.level != 8) {
568 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
570 return WERR_REVISION_MISMATCH;
573 req8 = &r->in.req->req8;
575 /* Perform access checks. */
576 /* TODO: we need to support a sync on a specific non-root
577 * DN. We'll need to find the real partition root here */
578 ncRoot = req8->naming_context;
579 if (ncRoot == NULL) {
580 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
581 return WERR_DS_DRA_INVALID_PARAMETER;
584 if (samdb_ntds_options(b_state->sam_ctx, &options) != LDB_SUCCESS) {
585 return WERR_DS_DRA_INTERNAL_ERROR;
588 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
589 !(req8->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
590 return WERR_DS_DRA_SOURCE_DISABLED;
594 if (req8->replica_flags & DRSUAPI_DS_REPLICA_NEIGHBOUR_FULL_SYNC_PACKET) {
595 /* Ignore the _in_ uptpdateness vector*/
596 req8->uptodateness_vector = NULL;
599 werr = drs_security_level_check(dce_call, "DsGetNCChanges");
600 if (!W_ERROR_IS_OK(werr)) {
604 getnc_state = b_state->getncchanges_state;
606 /* see if a previous replication has been abandoned */
608 struct ldb_dn *new_dn = ldb_dn_new(getnc_state, b_state->sam_ctx, ncRoot->dn);
609 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
610 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
611 ldb_dn_get_linearized(new_dn),
612 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
613 ldb_dn_get_linearized(getnc_state->last_dn)));
614 talloc_free(getnc_state);
619 if (getnc_state == NULL) {
620 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
621 if (getnc_state == NULL) {
624 b_state->getncchanges_state = getnc_state;
625 getnc_state->ncRoot_dn = ldb_dn_new(getnc_state, b_state->sam_ctx, ncRoot->dn);
628 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
629 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
630 DEBUG(0,(__location__ ": Bad DN '%s'\n", ncRoot->dn));
631 return WERR_DS_DRA_INVALID_PARAMETER;
634 /* we need the session key for encrypting password attributes */
635 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
636 if (!NT_STATUS_IS_OK(status)) {
637 DEBUG(0,(__location__ ": Failed to get session key\n"));
638 return WERR_DS_DRA_INTERNAL_ERROR;
641 /* we don't yet support extended operations */
642 if (req8->extended_op != DRSUAPI_EXOP_NONE) {
643 DEBUG(0,(__location__ ": Request for DsGetNCChanges extended op 0x%x\n",
644 (unsigned)req8->extended_op));
645 return WERR_DS_DRA_NOT_SUPPORTED;
649 TODO: MS-DRSR section 4.1.10.1.1
650 Work out if this is the start of a new cycle */
652 if (getnc_state->site_res == NULL) {
654 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
655 const char *extra_filter;
657 extra_filter = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
659 getnc_state->min_usn = req8->highwatermark.highest_usn;
661 /* Construct response. */
662 search_filter = talloc_asprintf(mem_ctx,
663 "(uSNChanged>=%llu)",
664 (unsigned long long)(getnc_state->min_usn+1));
667 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
670 if (req8->replica_flags & DRSUAPI_DS_REPLICA_NEIGHBOUR_CRITICAL_ONLY) {
671 search_filter = talloc_asprintf(mem_ctx,
672 "(&%s(isCriticalSystemObject=TRUE))",
676 if (req8->replica_flags & DRSUAPI_DS_REPLICA_NEIGHBOUR_ASYNC_REP) {
677 scope = LDB_SCOPE_BASE;
680 DEBUG(1,(__location__ ": getncchanges on %s using filter %s\n",
681 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
682 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, &getnc_state->site_res,
683 getnc_state->ncRoot_dn, scope, attrs,
685 if (ret != LDB_SUCCESS) {
686 return WERR_DS_DRA_INTERNAL_ERROR;
689 if (req8->replica_flags & DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS) {
690 qsort(getnc_state->site_res->msgs,
691 getnc_state->site_res->count,
692 sizeof(getnc_state->site_res->msgs[0]),
693 (comparison_fn_t)site_res_cmp_parent_order);
695 qsort(getnc_state->site_res->msgs,
696 getnc_state->site_res->count,
697 sizeof(getnc_state->site_res->msgs[0]),
698 (comparison_fn_t)site_res_cmp_usn_order);
704 schema = dsdb_get_schema(b_state->sam_ctx);
706 DEBUG(0,("No schema in sam_ctx\n"));
707 return WERR_DS_DRA_INTERNAL_ERROR;
710 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
711 *r->out.ctr->ctr6.naming_context = *ncRoot;
713 if (dsdb_find_guid_by_dn(b_state->sam_ctx, getnc_state->ncRoot_dn,
714 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
715 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
716 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
717 return WERR_DS_DRA_INTERNAL_ERROR;
720 /* find the SID if there is one */
721 dsdb_find_sid_by_dn(b_state->sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
723 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
724 r->out.ctr->ctr6.mapping_ctr = *ctr;
726 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(b_state->sam_ctx));
727 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(b_state->sam_ctx));
729 r->out.ctr->ctr6.old_highwatermark = req8->highwatermark;
730 r->out.ctr->ctr6.new_highwatermark = req8->highwatermark;
732 r->out.ctr->ctr6.first_object = NULL;
733 currentObject = &r->out.ctr->ctr6.first_object;
735 /* use this to force single objects at a time, which is useful
736 * for working out what object is giving problems
738 max_objects = lp_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
739 if (req8->max_object_count < max_objects) {
740 max_objects = req8->max_object_count;
743 for(i=getnc_state->num_sent;
744 i<getnc_state->site_res->count &&
745 (r->out.ctr->ctr6.object_count < max_objects);
748 struct drsuapi_DsReplicaObjectListItemEx *obj;
749 struct ldb_message *msg = getnc_state->site_res->msgs[i];
751 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
753 werr = get_nc_changes_build_object(obj, msg,
754 b_state->sam_ctx, getnc_state->ncRoot_dn,
755 schema, &session_key, getnc_state->min_usn,
756 req8->replica_flags);
757 if (!W_ERROR_IS_OK(werr)) {
761 werr = get_nc_changes_add_links(b_state->sam_ctx, getnc_state,
762 getnc_state->ncRoot_dn,
763 schema, getnc_state->min_usn,
766 &getnc_state->la_list,
767 &getnc_state->la_count);
768 if (!W_ERROR_IS_OK(werr)) {
772 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
773 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
774 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
776 if (uSN > getnc_state->highest_usn) {
777 getnc_state->highest_usn = uSN;
780 if (obj->meta_data_ctr == NULL) {
781 DEBUG(0,(__location__ ": getncchanges skipping send of object %s\n",
782 ldb_dn_get_linearized(msg->dn)));
783 /* no attributes to send */
788 r->out.ctr->ctr6.object_count++;
790 *currentObject = obj;
791 currentObject = &obj->next_object;
793 talloc_free(getnc_state->last_dn);
794 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
796 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
799 getnc_state->num_sent += r->out.ctr->ctr6.object_count;
801 r->out.ctr->ctr6.nc_object_count = getnc_state->site_res->count;
803 /* the client can us to call UpdateRefs on its behalf to
804 re-establish monitoring of the NC */
805 if ((req8->replica_flags & DRSUAPI_DRS_ADD_REF) &&
806 !GUID_all_zero(&req8->destination_dsa_guid)) {
807 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
808 ureq.naming_context = ncRoot;
809 ureq.dest_dsa_dns_name = talloc_asprintf(mem_ctx, "%s._msdcs.%s",
810 GUID_string(mem_ctx, &req8->destination_dsa_guid),
811 lp_realm(dce_call->conn->dce_ctx->lp_ctx));
812 if (!ureq.dest_dsa_dns_name) {
815 ureq.dest_dsa_guid = req8->destination_dsa_guid;
816 ureq.options = DRSUAPI_DS_REPLICA_UPDATE_ADD_REFERENCE |
817 DRSUAPI_DS_REPLICA_UPDATE_ASYNCHRONOUS_OPERATION |
818 DRSUAPI_DS_REPLICA_UPDATE_GETCHG_CHECK;
819 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
820 if (!W_ERROR_IS_OK(werr)) {
821 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
826 if (i < getnc_state->site_res->count) {
827 r->out.ctr->ctr6.more_data = true;
829 r->out.ctr->ctr6.linked_attributes_count = getnc_state->la_count;
830 r->out.ctr->ctr6.linked_attributes = talloc_steal(mem_ctx, getnc_state->la_list);
831 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
832 r->out.ctr->ctr6.uptodateness_vector->version = 2;
833 r->out.ctr->ctr6.uptodateness_vector->reserved1 = 0;
834 r->out.ctr->ctr6.uptodateness_vector->reserved2 = 0;
836 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
838 werr = get_nc_changes_udv(b_state->sam_ctx, getnc_state->ncRoot_dn,
839 r->out.ctr->ctr6.uptodateness_vector,
840 getnc_state->highest_usn);
841 if (!W_ERROR_IS_OK(werr)) {
845 talloc_free(getnc_state);
846 b_state->getncchanges_state = NULL;
849 DEBUG(r->out.ctr->ctr6.more_data?2:1,
850 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %d/%d la=%d)\n",
851 (unsigned long long)(req8->highwatermark.highest_usn+1),
853 ncRoot->dn, r->out.ctr->ctr6.object_count,
854 i, r->out.ctr->ctr6.more_data?getnc_state->site_res->count:i,
855 r->out.ctr->ctr6.linked_attributes_count));
858 if (!r->out.ctr->ctr6.more_data) {
859 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);