2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges 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 "libcli/security/security.h"
31 #include "libcli/security/session.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33 #include "rpc_server/dcerpc_server_proto.h"
34 #include "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
40 /* state of a partially completed getncchanges call */
41 struct drsuapi_getncchanges_state {
44 uint32_t num_processed;
45 struct ldb_dn *ncRoot_dn;
48 struct ldb_dn *last_dn;
49 struct drsuapi_DsReplicaLinkedAttribute *la_list;
55 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
56 const struct drsuapi_DsReplicaHighWaterMark *h2)
58 if (h1->highest_usn < h2->highest_usn) {
60 } else if (h1->highest_usn > h2->highest_usn) {
62 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
64 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
66 } else if (h1->reserved_usn < h2->reserved_usn) {
68 } else if (h1->reserved_usn > h2->reserved_usn) {
76 build a DsReplicaObjectIdentifier from a ldb msg
78 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
79 struct ldb_message *msg)
81 struct drsuapi_DsReplicaObjectIdentifier *identifier;
84 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
85 if (identifier == NULL) {
89 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
90 identifier->guid = samdb_result_guid(msg, "objectGUID");
92 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
94 identifier->sid = *sid;
96 ZERO_STRUCT(identifier->sid);
101 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
103 return GUID_compare(guid1, &guid2);
107 see if we can filter an attribute using the uptodateness_vector
109 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
110 const struct GUID *originating_invocation_id,
111 uint64_t originating_usn)
113 const struct drsuapi_DsReplicaCursor *c;
114 if (udv == NULL) return false;
115 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
116 originating_invocation_id, udv_compare, c);
117 if (c && originating_usn <= c->highest_usn) {
124 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
126 if (a1 == a2) return 0;
127 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
131 check if an attribute is in a partial_attribute_set
133 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
134 struct drsuapi_DsPartialAttributeSet *pas)
136 enum drsuapi_DsAttributeId *result;
137 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
139 return result != NULL;
144 drsuapi_DsGetNCChanges for one object
146 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
147 struct ldb_message *msg,
148 struct ldb_context *sam_ctx,
149 struct ldb_dn *ncRoot_dn,
151 struct dsdb_schema *schema,
152 DATA_BLOB *session_key,
153 uint64_t highest_usn,
154 uint32_t replica_flags,
155 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
156 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
157 enum drsuapi_DsExtendedOperation extended_op,
158 bool force_object_return)
160 const struct ldb_val *md_value;
162 struct replPropertyMetaDataBlob md;
164 enum ndr_err_code ndr_err;
167 const struct dsdb_attribute *rdn_sa;
168 unsigned int instanceType;
169 struct dsdb_syntax_ctx syntax_ctx;
171 /* make dsdb sytanx context for conversions */
172 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
173 syntax_ctx.is_schema_nc = is_schema_nc;
175 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
176 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
177 obj->is_nc_prefix = true;
178 obj->parent_object_guid = NULL;
180 obj->is_nc_prefix = false;
181 obj->parent_object_guid = talloc(obj, struct GUID);
182 if (obj->parent_object_guid == NULL) {
183 return WERR_DS_DRA_INTERNAL_ERROR;
185 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
186 if (GUID_all_zero(obj->parent_object_guid)) {
187 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
188 ldb_dn_get_linearized(msg->dn)));
189 return WERR_DS_DRA_INTERNAL_ERROR;
192 obj->next_object = NULL;
194 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
196 /* nothing to send */
200 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
201 /* don't send uninstantiated objects */
205 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
206 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
207 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
208 return WERR_DS_DRA_INTERNAL_ERROR;
211 if (md.version != 1) {
212 return WERR_DS_DRA_INTERNAL_ERROR;
215 rdn = ldb_dn_get_rdn_name(msg->dn);
217 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
218 return WERR_DS_DRA_INTERNAL_ERROR;
221 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
222 if (rdn_sa == NULL) {
223 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
224 rdn, ldb_dn_get_linearized(msg->dn)));
225 return WERR_DS_DRA_INTERNAL_ERROR;
228 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
229 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
231 obj->object.identifier = get_object_identifier(obj, msg);
232 if (obj->object.identifier == NULL) {
235 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
237 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
238 for (n=i=0; i<md.ctr.ctr1.count; i++) {
239 const struct dsdb_attribute *sa;
240 bool force_attribute = false;
242 /* if the attribute has not changed, and it is not the
243 instanceType then don't include it */
244 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
245 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
246 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
248 /* don't include the rDN */
249 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
251 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
253 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
254 (unsigned int)md.ctr.ctr1.array[i].attid,
255 ldb_dn_get_linearized(msg->dn)));
256 return WERR_DS_DRA_INTERNAL_ERROR;
260 struct ldb_message_element *el;
261 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
262 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
263 /* don't send upgraded links inline */
268 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
269 !dsdb_attr_in_rodc_fas(sa)) {
270 force_attribute = true;
271 DEBUG(4,("Forcing attribute %s in %s\n",
272 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
275 /* filter by uptodateness_vector */
276 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
278 udv_filter(uptodateness_vector,
279 &md.ctr.ctr1.array[i].originating_invocation_id,
280 md.ctr.ctr1.array[i].originating_usn)) {
284 /* filter by partial_attribute_set */
285 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
289 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
290 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
291 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
292 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
293 attids[n] = md.ctr.ctr1.array[i].attid;
297 /* ignore it if its an empty change. Note that renames always
298 * change the 'name' attribute, so they won't be ignored by
301 * the force_object_return check is used to force an empty
302 * object return when we timeout in the getncchanges loop.
303 * This allows us to return an empty object, which keeps the
304 * client happy while preventing timeouts
308 attids[0] == DRSUAPI_ATTID_instanceType &&
309 !force_object_return)) {
310 talloc_free(obj->meta_data_ctr);
311 obj->meta_data_ctr = NULL;
315 obj->meta_data_ctr->count = n;
317 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
318 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
319 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
320 obj->object.attribute_ctr.num_attributes);
321 if (obj->object.attribute_ctr.attributes == NULL) {
326 * Note that the meta_data array and the attributes array must
327 * be the same size and in the same order
329 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
330 struct ldb_message_element *el;
332 const struct dsdb_attribute *sa;
334 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
336 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
337 return WERR_DS_DRA_INTERNAL_ERROR;
340 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
342 /* this happens for attributes that have been removed */
343 DEBUG(5,("No element '%s' for attributeID %u in message\n",
344 sa->lDAPDisplayName, attids[i]));
345 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
346 obj->object.attribute_ctr.attributes[i].attid =
347 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
349 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
350 &obj->object.attribute_ctr.attributes[i]);
351 if (!W_ERROR_IS_OK(werr)) {
352 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
353 sa->lDAPDisplayName, win_errstr(werr)));
356 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
357 * check if attribute is secret and send a null value
359 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
360 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
361 &obj->meta_data_ctr->meta_data[i]);
363 /* some attributes needs to be encrypted
365 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
366 &obj->object.attribute_ctr.attributes[i]);
367 if (!W_ERROR_IS_OK(werr)) {
368 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
369 sa->lDAPDisplayName, win_errstr(werr)));
379 add one linked attribute from an object to the list of linked
380 attributes in a getncchanges request
382 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
383 struct ldb_context *sam_ctx,
384 const struct dsdb_schema *schema,
385 const struct dsdb_attribute *sa,
386 struct ldb_message *msg,
387 struct dsdb_dn *dsdb_dn,
388 struct drsuapi_DsReplicaLinkedAttribute **la_list,
391 struct drsuapi_DsReplicaLinkedAttribute *la;
396 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
397 W_ERROR_HAVE_NO_MEMORY(*la_list);
399 la = &(*la_list)[*la_count];
401 la->identifier = get_object_identifier(*la_list, msg);
402 W_ERROR_HAVE_NO_MEMORY(la->identifier);
404 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
407 /* We have to check that the inactive link still point to an existing object */
413 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
414 if (strncmp(v, "TRUE", 4) == 0) {
416 * Note: we skip the transmition of the deleted link even if the other part used to
417 * know about it because when we transmit the deletion of the object, the link will
418 * be deleted too due to deletion of object where link points and Windows do so.
420 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
421 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
423 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
424 * if it join an existing domain with deleted objets, it firsts impose to have a
425 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
426 * either during initial replication or after the getNCChanges.
427 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
429 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
430 * If FL >=2K8R2 we are sure that this attribute will be here.
431 * For this kind of forest level we do not return the link if the object is recycled
432 * (isRecycled = true).
434 if (strncmp(v, "TRUE", 4) == 0) {
435 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
436 ldb_dn_get_linearized(msg->dn)));
443 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
444 if (!NT_STATUS_IS_OK(status)) {
445 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
446 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
447 return ntstatus_to_werror(status);
449 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
450 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
451 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
452 GUID_string(mem_ctx, &guid)));
454 } else if (ret != LDB_SUCCESS) {
455 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
456 GUID_string(mem_ctx, &guid),
461 la->attid = sa->attributeID_id;
462 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
464 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
465 if (!NT_STATUS_IS_OK(status)) {
466 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
467 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
468 return ntstatus_to_werror(status);
470 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
471 if (!NT_STATUS_IS_OK(status)) {
472 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
473 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
474 return ntstatus_to_werror(status);
476 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
477 if (!NT_STATUS_IS_OK(status)) {
478 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
479 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
480 return ntstatus_to_werror(status);
482 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
483 if (!NT_STATUS_IS_OK(status)) {
484 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
485 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
486 return ntstatus_to_werror(status);
489 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
490 if (!NT_STATUS_IS_OK(status)) {
491 /* this is possible for upgraded links */
492 la->originating_add_time = la->meta_data.originating_change_time;
495 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
496 W_ERROR_NOT_OK_RETURN(werr);
504 add linked attributes from an object to the list of linked
505 attributes in a getncchanges request
507 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
509 struct ldb_dn *ncRoot_dn,
510 struct dsdb_schema *schema,
511 uint64_t highest_usn,
512 uint32_t replica_flags,
513 struct ldb_message *msg,
514 struct drsuapi_DsReplicaLinkedAttribute **la_list,
516 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
519 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
520 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
522 for (i=0; i<msg->num_elements; i++) {
523 struct ldb_message_element *el = &msg->elements[i];
524 const struct dsdb_attribute *sa;
527 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
529 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
530 /* we only want forward links */
534 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
535 /* its an old style link, it will have been
536 * sent in the main replication data */
540 for (j=0; j<el->num_values; j++) {
541 struct dsdb_dn *dsdb_dn;
546 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
547 if (dsdb_dn == NULL) {
548 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
549 el->name, ldb_dn_get_linearized(msg->dn)));
550 talloc_free(tmp_ctx);
551 return WERR_DS_DRA_INTERNAL_ERROR;
554 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
555 if (!NT_STATUS_IS_OK(status)) {
556 /* this can happen for attributes
557 given to us with old style meta
562 if (local_usn > uSNChanged) {
563 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
564 el->name, ldb_dn_get_linearized(msg->dn)));
565 talloc_free(tmp_ctx);
566 return WERR_DS_DRA_INTERNAL_ERROR;
569 if (local_usn < highest_usn) {
573 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
574 dsdb_dn, la_list, la_count);
575 if (!W_ERROR_IS_OK(werr)) {
576 talloc_free(tmp_ctx);
582 talloc_free(tmp_ctx);
587 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
589 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
590 struct ldb_dn *ncRoot_dn,
591 struct drsuapi_DsReplicaCursor2CtrEx *udv)
599 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
600 if (ret != LDB_SUCCESS) {
601 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
602 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
603 return WERR_DS_DRA_INTERNAL_ERROR;
610 /* comparison function for linked attributes - see CompareLinks() in
611 * MS-DRSR section 4.1.10.5.17 */
612 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
613 const struct drsuapi_DsReplicaLinkedAttribute *la2,
614 struct ldb_context *sam_ctx)
619 const struct dsdb_schema *schema;
620 const struct dsdb_attribute *schema_attrib;
621 struct dsdb_dn *dn1, *dn2;
622 struct GUID guid1, guid2;
625 c = GUID_compare(&la1->identifier->guid,
626 &la2->identifier->guid);
627 if (c != 0) return c;
629 if (la1->attid != la2->attid) {
630 return la1->attid < la2->attid? -1:1;
633 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
634 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
635 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
638 /* we need to get the target GUIDs to compare */
639 tmp_ctx = talloc_new(sam_ctx);
641 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
642 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
644 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
645 if (!W_ERROR_IS_OK(werr)) {
646 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
647 talloc_free(tmp_ctx);
651 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
652 if (!W_ERROR_IS_OK(werr)) {
653 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
654 talloc_free(tmp_ctx);
658 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
659 if (!NT_STATUS_IS_OK(status)) {
660 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
661 talloc_free(tmp_ctx);
664 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
665 if (!NT_STATUS_IS_OK(status)) {
666 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
667 talloc_free(tmp_ctx);
671 talloc_free(tmp_ctx);
673 return GUID_compare(&guid1, &guid2);
676 struct drsuapi_changed_objects {
683 sort the objects we send by tree order
685 static int site_res_cmp_parent_order(struct drsuapi_changed_objects *m1,
686 struct drsuapi_changed_objects *m2)
688 return ldb_dn_compare(m2->dn, m1->dn);
692 sort the objects we send first by uSNChanged
694 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects *m1,
695 struct drsuapi_changed_objects *m2)
697 unsigned usnchanged1, usnchanged2;
700 cn1 = ldb_dn_get_comp_num(m1->dn);
701 cn2 = ldb_dn_get_comp_num(m2->dn);
703 return cn1 > cn2 ? 1 : -1;
705 usnchanged1 = m1->usn;
706 usnchanged2 = m2->usn;
707 if (usnchanged1 == usnchanged2) {
710 return usnchanged1 > usnchanged2 ? 1 : -1;
715 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
717 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
719 struct drsuapi_DsGetNCChangesRequest10 *req10,
720 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
722 struct ldb_dn *rid_manager_dn, *req_dn;
724 struct ldb_context *ldb = b_state->sam_ctx;
725 struct ldb_result *ext_res;
726 struct dsdb_fsmo_extended_op *exop;
731 - verify that the DN being asked for is the RID Manager DN
732 - verify that we are the RID Manager
735 /* work out who is the RID Manager */
736 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
737 if (ret != LDB_SUCCESS) {
738 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
739 return WERR_DS_DRA_INTERNAL_ERROR;
742 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
743 if (!ldb_dn_validate(req_dn) ||
744 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
745 /* that isn't the RID Manager DN */
746 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
747 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
748 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
752 /* find the DN of the RID Manager */
753 ret = samdb_reference_dn_is_our_ntdsa(ldb, rid_manager_dn, "fSMORoleOwner", &is_us);
754 if (ret != LDB_SUCCESS) {
755 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
756 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
757 return WERR_DS_DRA_INTERNAL_ERROR;
761 /* we're not the RID Manager - go away */
762 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
763 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
767 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
768 W_ERROR_HAVE_NO_MEMORY(exop);
770 exop->fsmo_info = req10->fsmo_info;
771 exop->destination_dsa_guid = req10->destination_dsa_guid;
773 ret = ldb_transaction_start(ldb);
774 if (ret != LDB_SUCCESS) {
775 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
776 ldb_errstring(ldb)));
777 return WERR_DS_DRA_INTERNAL_ERROR;
781 * FIXME (kim): this is a temp hack to return just few object,
782 * but not the whole domain NC.
783 * We should remove this hack and implement a 'scope'
784 * building function to return just the set of object
785 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
787 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
789 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
790 if (ret != LDB_SUCCESS) {
791 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
792 ldb_errstring(ldb)));
793 ldb_transaction_cancel(ldb);
794 return WERR_DS_DRA_INTERNAL_ERROR;
797 ret = ldb_transaction_commit(ldb);
798 if (ret != LDB_SUCCESS) {
799 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
800 ldb_errstring(ldb)));
801 return WERR_DS_DRA_INTERNAL_ERROR;
804 talloc_free(ext_res);
806 DEBUG(2,("Allocated RID pool for server %s\n",
807 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
809 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
815 return an array of SIDs from a ldb_message given an attribute name
816 assumes the SIDs are in extended DN format
818 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
819 struct ldb_message *msg,
822 const struct dom_sid ***sids)
824 struct ldb_message_element *el;
827 el = ldb_msg_find_element(msg, attr);
833 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
834 W_ERROR_HAVE_NO_MEMORY(*sids);
836 for (i=0; i<el->num_values; i++) {
837 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
841 sid = talloc(*sids, struct dom_sid);
842 W_ERROR_HAVE_NO_MEMORY(sid);
843 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
844 if (!NT_STATUS_IS_OK(status)) {
845 return WERR_INTERNAL_DB_CORRUPTION;
856 return an array of SIDs from a ldb_message given an attribute name
857 assumes the SIDs are in NDR form
859 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
860 struct ldb_message *msg,
863 const struct dom_sid ***sids)
865 struct ldb_message_element *el;
868 el = ldb_msg_find_element(msg, attr);
874 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
875 W_ERROR_HAVE_NO_MEMORY(*sids);
877 for (i=0; i<el->num_values; i++) {
878 enum ndr_err_code ndr_err;
881 sid = talloc(*sids, struct dom_sid);
882 W_ERROR_HAVE_NO_MEMORY(sid);
884 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
885 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
886 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
887 return WERR_INTERNAL_DB_CORRUPTION;
897 see if any SIDs in list1 are in list2
899 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
902 /* do we ever have enough SIDs here to worry about O(n^2) ? */
903 for (i=0; list1[i]; i++) {
904 for (j=0; list2[j]; j++) {
905 if (dom_sid_equal(list1[i], list2[j])) {
914 handle a DRSUAPI_EXOP_REPL_SECRET call
916 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
918 struct drsuapi_DsGetNCChangesRequest10 *req10,
919 struct dom_sid *user_sid,
920 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
921 bool has_get_all_changes)
923 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
924 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
926 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
927 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
928 struct ldb_result *rodc_res, *obj_res;
929 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
932 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
933 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
936 * we need to work out if we will allow this DC to
937 * replicate the secrets for this object
939 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
943 if (b_state->sam_ctx_system == NULL) {
944 /* this operation needs system level access */
945 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
946 return WERR_DS_DRA_SOURCE_DISABLED;
950 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
952 * The pseudo code indicate
953 * revealsecrets = true
954 * if IsRevealSecretRequest(msgIn) then
955 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
957 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
958 * <... check if this account is ok to be replicated on this DC ...>
959 * <... and if not reveal secrets = no ...>
961 * reveal secrets = false
966 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
967 * then you can do EXOP_REPL_SECRETS
969 if (has_get_all_changes) {
973 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
974 if (!ldb_dn_validate(obj_dn)) goto failed;
976 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
977 dom_sid_string(mem_ctx, user_sid));
978 if (!ldb_dn_validate(rodc_dn)) goto failed;
980 /* do the two searches we need */
981 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
982 DSDB_SEARCH_SHOW_EXTENDED_DN);
983 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
985 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
986 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
988 /* if the object SID is equal to the user_sid, allow */
989 if (dom_sid_equal(user_sid,
990 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
994 /* an RODC is allowed to get its own krbtgt account secrets */
995 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
996 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
997 if (krbtgt_link_dn != NULL &&
998 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1002 /* but it isn't allowed to get anyone elses krbtgt secrets */
1003 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1004 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1008 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1009 "userAccountControl", 0) &
1010 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1014 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1015 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1016 if (!W_ERROR_IS_OK(werr)) {
1020 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1021 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1022 if (!W_ERROR_IS_OK(werr)) {
1026 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1027 mem_ctx, "tokenGroups", &token_sids);
1028 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1032 if (never_reveal_sids &&
1033 sid_list_match(token_sids, never_reveal_sids)) {
1038 sid_list_match(token_sids, reveal_sids)) {
1044 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1045 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1046 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1047 return WERR_DS_DRA_ACCESS_DENIED;
1050 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1051 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1052 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1053 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1054 req10->highwatermark.highest_usn = 0;
1058 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1059 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1060 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1061 return WERR_DS_DRA_BAD_DN;
1066 handle a DRSUAPI_EXOP_REPL_OBJ call
1068 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1069 TALLOC_CTX *mem_ctx,
1070 struct drsuapi_DsGetNCChangesRequest10 *req10,
1071 struct dom_sid *user_sid,
1072 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1074 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1076 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1077 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1079 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1085 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1086 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1087 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1089 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1090 TALLOC_CTX *mem_ctx,
1091 struct drsuapi_DsGetNCChangesRequest10 *req10,
1092 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1094 struct ldb_dn *req_dn, *ntds_dn;
1097 struct ldb_context *ldb = b_state->sam_ctx;
1098 struct ldb_message *msg;
1103 - verify that the client dn exists
1104 - verify that we are the current master
1107 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1108 if (!ldb_dn_validate(req_dn)) {
1109 /* that is not a valid dn */
1110 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1111 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1112 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1116 /* retrieve the current role owner */
1117 /* find the DN of the RID Manager */
1118 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1119 if (ret != LDB_SUCCESS) {
1120 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1121 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1122 return WERR_DS_DRA_INTERNAL_ERROR;
1126 /* we're not the RID Manager - go away */
1127 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1128 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1132 /* change the current master */
1133 msg = ldb_msg_new(ldb);
1134 W_ERROR_HAVE_NO_MEMORY(msg);
1135 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1136 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1138 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1139 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1140 if (ret != LDB_SUCCESS) {
1141 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1142 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1144 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1148 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1151 return WERR_DS_DRA_INTERNAL_ERROR;
1154 for (i=0;i<msg->num_elements;i++) {
1155 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1158 ret = ldb_transaction_start(ldb);
1159 if (ret != LDB_SUCCESS) {
1160 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1161 ldb_errstring(ldb)));
1162 return WERR_DS_DRA_INTERNAL_ERROR;
1165 ret = ldb_modify(ldb, msg);
1166 if (ret != LDB_SUCCESS) {
1167 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1168 ldb_errstring(ldb)));
1169 ldb_transaction_cancel(ldb);
1170 return WERR_DS_DRA_INTERNAL_ERROR;
1173 ret = ldb_transaction_commit(ldb);
1174 if (ret != LDB_SUCCESS) {
1175 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1176 ldb_errstring(ldb)));
1177 return WERR_DS_DRA_INTERNAL_ERROR;
1180 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1186 see if this getncchanges request includes a request to reveal secret information
1188 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1189 struct drsuapi_DsGetNCChangesRequest10 *req10,
1190 bool *is_secret_request)
1192 enum drsuapi_DsExtendedOperation exop;
1194 struct dsdb_schema *schema;
1196 *is_secret_request = true;
1198 exop = req10->extended_op;
1201 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1202 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1203 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1204 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1205 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1206 /* FSMO exops can reveal secrets */
1207 *is_secret_request = true;
1209 case DRSUAPI_EXOP_REPL_SECRET:
1210 case DRSUAPI_EXOP_REPL_OBJ:
1211 case DRSUAPI_EXOP_NONE:
1215 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1216 *is_secret_request = false;
1220 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1221 req10->partial_attribute_set == NULL) {
1222 /* they want secrets */
1223 *is_secret_request = true;
1227 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1229 /* check the attributes they asked for */
1230 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1231 const struct dsdb_attribute *sa;
1232 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1234 return WERR_DS_DRA_SCHEMA_MISMATCH;
1236 if (!dsdb_attr_in_rodc_fas(sa)) {
1237 *is_secret_request = true;
1242 if (req10->partial_attribute_set_ex) {
1243 /* check the extended attributes they asked for */
1244 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1245 const struct dsdb_attribute *sa;
1246 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1248 return WERR_DS_DRA_SCHEMA_MISMATCH;
1250 if (!dsdb_attr_in_rodc_fas(sa)) {
1251 *is_secret_request = true;
1257 *is_secret_request = false;
1262 see if this getncchanges request is only for attributes in the GC
1263 partial attribute set
1265 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1266 struct drsuapi_DsGetNCChangesRequest10 *req10,
1267 bool *is_gc_pas_request)
1269 enum drsuapi_DsExtendedOperation exop;
1271 struct dsdb_schema *schema;
1273 exop = req10->extended_op;
1276 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1277 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1278 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1279 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1280 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1281 case DRSUAPI_EXOP_REPL_SECRET:
1282 *is_gc_pas_request = false;
1284 case DRSUAPI_EXOP_REPL_OBJ:
1285 case DRSUAPI_EXOP_NONE:
1289 if (req10->partial_attribute_set == NULL) {
1290 /* they want it all */
1291 *is_gc_pas_request = false;
1295 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1297 /* check the attributes they asked for */
1298 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1299 const struct dsdb_attribute *sa;
1300 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1302 return WERR_DS_DRA_SCHEMA_MISMATCH;
1304 if (!sa->isMemberOfPartialAttributeSet) {
1305 *is_gc_pas_request = false;
1310 if (req10->partial_attribute_set_ex) {
1311 /* check the extended attributes they asked for */
1312 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1313 const struct dsdb_attribute *sa;
1314 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1316 return WERR_DS_DRA_SCHEMA_MISMATCH;
1318 if (!sa->isMemberOfPartialAttributeSet) {
1319 *is_gc_pas_request = false;
1325 *is_gc_pas_request = true;
1331 map from req8 to req10
1333 static struct drsuapi_DsGetNCChangesRequest10 *
1334 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1335 struct drsuapi_DsGetNCChangesRequest8 *req8)
1337 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1338 struct drsuapi_DsGetNCChangesRequest10);
1339 if (req10 == NULL) {
1343 req10->destination_dsa_guid = req8->destination_dsa_guid;
1344 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1345 req10->naming_context = req8->naming_context;
1346 req10->highwatermark = req8->highwatermark;
1347 req10->uptodateness_vector = req8->uptodateness_vector;
1348 req10->replica_flags = req8->replica_flags;
1349 req10->max_object_count = req8->max_object_count;
1350 req10->max_ndr_size = req8->max_ndr_size;
1351 req10->extended_op = req8->extended_op;
1352 req10->fsmo_info = req8->fsmo_info;
1353 req10->partial_attribute_set = req8->partial_attribute_set;
1354 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1355 req10->mapping_ctr = req8->mapping_ctr;
1362 * Collects object for normal replication cycle.
1364 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1365 TALLOC_CTX *mem_ctx,
1366 struct drsuapi_DsGetNCChangesRequest10 *req10,
1367 struct ldb_dn *search_dn,
1368 const char *extra_filter,
1369 struct ldb_result **search_res)
1372 char* search_filter;
1373 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1374 //const char *extra_filter;
1375 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1376 const char *attrs[] = { "uSNChanged",
1380 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1381 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1382 scope = LDB_SCOPE_BASE;
1385 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1387 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1389 /* Construct response. */
1390 search_filter = talloc_asprintf(mem_ctx,
1391 "(uSNChanged>=%llu)",
1392 (unsigned long long)(getnc_state->min_usn+1));
1395 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1398 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1399 search_filter = talloc_asprintf(mem_ctx,
1400 "(&%s(isCriticalSystemObject=TRUE))",
1404 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1405 scope = LDB_SCOPE_BASE;
1409 search_dn = getnc_state->ncRoot_dn;
1412 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1413 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1414 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1415 search_dn, scope, attrs,
1417 if (ret != LDB_SUCCESS) {
1418 return WERR_DS_DRA_INTERNAL_ERROR;
1425 * Collects object for normal replication cycle.
1427 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1428 TALLOC_CTX *mem_ctx,
1429 struct drsuapi_DsGetNCChangesRequest10 *req10,
1430 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1431 struct ldb_dn *search_dn,
1432 const char *extra_filter,
1433 struct ldb_result **search_res)
1435 /* we have nothing to do in case of ex-op failure */
1436 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1440 /* TODO: implement extended op specific collection
1441 * of objects. Right now we just normal procedure
1442 * for collecting objects */
1443 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1447 drsuapi_DsGetNCChanges
1449 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1451 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1452 struct drsuapi_DsGetNCChanges *r)
1454 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1457 struct dsdb_schema *schema;
1458 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1459 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1461 DATA_BLOB session_key;
1463 struct dcesrv_handle *h;
1464 struct drsuapi_bind_state *b_state;
1465 struct drsuapi_getncchanges_state *getnc_state;
1466 struct drsuapi_DsGetNCChangesRequest10 *req10;
1468 uint32_t max_objects;
1470 uint32_t link_count = 0;
1471 uint32_t link_total = 0;
1472 uint32_t link_given = 0;
1473 struct ldb_dn *search_dn = NULL;
1474 bool am_rodc, null_scope=false;
1475 enum security_user_level security_level;
1476 struct ldb_context *sam_ctx;
1477 struct dom_sid *user_sid;
1478 bool is_secret_request;
1479 bool is_gc_pas_request;
1480 struct drsuapi_changed_objects *changes;
1482 time_t start = time(NULL);
1483 bool max_wait_reached = false;
1484 bool has_get_all_changes = false;
1486 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1489 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1491 *r->out.level_out = 6;
1492 /* TODO: linked attributes*/
1493 r->out.ctr->ctr6.linked_attributes_count = 0;
1494 r->out.ctr->ctr6.linked_attributes = NULL;
1496 r->out.ctr->ctr6.object_count = 0;
1497 r->out.ctr->ctr6.nc_object_count = 0;
1498 r->out.ctr->ctr6.more_data = false;
1499 r->out.ctr->ctr6.uptodateness_vector = NULL;
1501 /* a RODC doesn't allow for any replication */
1502 ret = samdb_rodc(sam_ctx, &am_rodc);
1503 if (ret == LDB_SUCCESS && am_rodc) {
1504 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1505 return WERR_DS_DRA_SOURCE_DISABLED;
1508 /* Check request revision.
1510 switch (r->in.level) {
1512 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1513 if (req10 == NULL) {
1518 req10 = &r->in.req->req10;
1521 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1523 return WERR_REVISION_MISMATCH;
1527 /* Perform access checks. */
1528 /* TODO: we need to support a sync on a specific non-root
1529 * DN. We'll need to find the real partition root here */
1530 ncRoot = req10->naming_context;
1531 if (ncRoot == NULL) {
1532 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1533 return WERR_DS_DRA_INVALID_PARAMETER;
1536 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1537 return WERR_DS_DRA_INTERNAL_ERROR;
1540 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1541 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1542 return WERR_DS_DRA_SOURCE_DISABLED;
1545 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1547 /* all clients must have GUID_DRS_GET_CHANGES */
1548 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1550 dce_call->conn->auth_state.session_info->security_token,
1551 req10->naming_context,
1552 GUID_DRS_GET_CHANGES);
1553 if (!W_ERROR_IS_OK(werr)) {
1557 /* allowed if the GC PAS and client has
1558 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1559 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1560 if (!W_ERROR_IS_OK(werr)) {
1563 if (is_gc_pas_request) {
1564 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1566 dce_call->conn->auth_state.session_info->security_token,
1567 req10->naming_context,
1568 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1569 if (W_ERROR_IS_OK(werr)) {
1574 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1575 if (!W_ERROR_IS_OK(werr)) {
1578 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1579 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1581 dce_call->conn->auth_state.session_info->security_token,
1582 req10->naming_context,
1583 GUID_DRS_GET_ALL_CHANGES);
1584 if (!W_ERROR_IS_OK(werr)) {
1587 has_get_all_changes = true;
1592 /* for non-administrator replications, check that they have
1593 given the correct source_dsa_invocation_id */
1594 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1595 samdb_domain_sid(sam_ctx));
1596 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1597 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1598 /* we rely on this flag being unset for RODC requests */
1599 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1603 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1604 /* Ignore the _in_ uptpdateness vector*/
1605 req10->uptodateness_vector = NULL;
1608 getnc_state = b_state->getncchanges_state;
1610 /* see if a previous replication has been abandoned */
1612 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1613 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1614 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1615 ldb_dn_get_linearized(new_dn),
1616 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1617 ldb_dn_get_linearized(getnc_state->last_dn)));
1618 talloc_free(getnc_state);
1623 if (getnc_state == NULL) {
1624 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1625 if (getnc_state == NULL) {
1628 b_state->getncchanges_state = getnc_state;
1629 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1631 /* find out if we are to replicate Schema NC */
1632 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1633 ldb_get_schema_basedn(b_state->sam_ctx));
1634 getnc_state->is_schema_nc = (0 == ret);
1636 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1637 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1641 * This is the first replication cycle and it is
1642 * a good place to handle extended operations
1644 * FIXME: we don't fully support extended operations yet
1646 switch (req10->extended_op) {
1647 case DRSUAPI_EXOP_NONE:
1649 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1650 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1651 W_ERROR_NOT_OK_RETURN(werr);
1652 search_dn = ldb_get_default_basedn(sam_ctx);
1654 case DRSUAPI_EXOP_REPL_SECRET:
1655 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1658 has_get_all_changes);
1659 r->out.result = werr;
1660 W_ERROR_NOT_OK_RETURN(werr);
1662 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1663 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1664 W_ERROR_NOT_OK_RETURN(werr);
1666 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1667 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1668 W_ERROR_NOT_OK_RETURN(werr);
1670 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1671 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1672 W_ERROR_NOT_OK_RETURN(werr);
1674 case DRSUAPI_EXOP_REPL_OBJ:
1675 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1676 r->out.result = werr;
1677 W_ERROR_NOT_OK_RETURN(werr);
1680 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1682 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1683 (unsigned)req10->extended_op));
1684 return WERR_DS_DRA_NOT_SUPPORTED;
1688 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1689 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1690 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1691 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1692 return WERR_DS_DRA_INVALID_PARAMETER;
1695 /* we need the session key for encrypting password attributes */
1696 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1697 if (!NT_STATUS_IS_OK(status)) {
1698 DEBUG(0,(__location__ ": Failed to get session key\n"));
1699 return WERR_DS_DRA_INTERNAL_ERROR;
1703 TODO: MS-DRSR section 4.1.10.1.1
1704 Work out if this is the start of a new cycle */
1706 if (getnc_state->guids == NULL) {
1707 const char *extra_filter;
1708 struct ldb_result *search_res = NULL;
1710 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1712 getnc_state->min_usn = req10->highwatermark.highest_usn;
1714 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1715 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1716 search_dn, extra_filter,
1719 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1721 search_dn, extra_filter,
1724 W_ERROR_NOT_OK_RETURN(werr);
1726 /* extract out the GUIDs list */
1727 getnc_state->num_records = search_res ? search_res->count : 0;
1728 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1729 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1731 changes = talloc_array(getnc_state,
1732 struct drsuapi_changed_objects,
1733 getnc_state->num_records);
1734 W_ERROR_HAVE_NO_MEMORY(changes);
1736 for (i=0; i<getnc_state->num_records; i++) {
1737 changes[i].dn = search_res->msgs[i]->dn;
1738 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1739 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1742 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1743 TYPESAFE_QSORT(changes,
1744 getnc_state->num_records,
1745 site_res_cmp_parent_order);
1747 TYPESAFE_QSORT(changes,
1748 getnc_state->num_records,
1749 site_res_cmp_dn_usn_order);
1752 for (i=0; i < getnc_state->num_records; i++) {
1753 getnc_state->guids[i] = changes[i].guid;
1754 if (GUID_all_zero(&getnc_state->guids[i])) {
1755 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1756 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1757 return WERR_DS_DRA_INTERNAL_ERROR;
1761 talloc_free(search_res);
1762 talloc_free(changes);
1765 if (req10->uptodateness_vector) {
1766 /* make sure its sorted */
1767 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
1768 req10->uptodateness_vector->count,
1769 drsuapi_DsReplicaCursor_compare);
1772 /* Prefix mapping */
1773 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1775 DEBUG(0,("No schema in sam_ctx\n"));
1776 return WERR_DS_DRA_INTERNAL_ERROR;
1779 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1780 *r->out.ctr->ctr6.naming_context = *ncRoot;
1782 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1783 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1784 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1785 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1786 return WERR_DS_DRA_INTERNAL_ERROR;
1789 /* find the SID if there is one */
1790 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1792 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1793 r->out.ctr->ctr6.mapping_ctr = *ctr;
1795 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1796 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1798 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1799 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1801 r->out.ctr->ctr6.first_object = NULL;
1802 currentObject = &r->out.ctr->ctr6.first_object;
1804 /* use this to force single objects at a time, which is useful
1805 * for working out what object is giving problems
1807 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1808 if (req10->max_object_count < max_objects) {
1809 max_objects = req10->max_object_count;
1812 * TODO: work out how the maximum should be calculated
1814 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1817 * Maximum time that we can spend in a getncchanges
1818 * in order to avoid timeout of the other part.
1819 * 10 seconds by default.
1821 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1822 for (i=getnc_state->num_processed;
1823 i<getnc_state->num_records &&
1825 (r->out.ctr->ctr6.object_count < max_objects)
1826 && !max_wait_reached;
1829 struct drsuapi_DsReplicaObjectListItemEx *obj;
1830 struct ldb_message *msg;
1831 static const char * const msg_attrs[] = {
1833 "nTSecurityDescriptor",
1835 "replPropertyMetaData",
1836 DSDB_SECRET_ATTRIBUTES,
1838 struct ldb_result *msg_res;
1839 struct ldb_dn *msg_dn;
1841 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1842 W_ERROR_HAVE_NO_MEMORY(obj);
1844 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1845 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1848 /* by re-searching here we avoid having a lot of full
1849 * records in memory between calls to getncchanges
1851 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1853 LDB_SCOPE_BASE, msg_attrs, NULL);
1854 if (ret != LDB_SUCCESS) {
1855 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1856 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1857 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1863 msg = msg_res->msgs[0];
1865 max_wait_reached = (time(NULL) - start > max_wait);
1867 werr = get_nc_changes_build_object(obj, msg,
1868 sam_ctx, getnc_state->ncRoot_dn,
1869 getnc_state->is_schema_nc,
1870 schema, &session_key, getnc_state->min_usn,
1871 req10->replica_flags,
1872 req10->partial_attribute_set,
1873 req10->uptodateness_vector,
1876 if (!W_ERROR_IS_OK(werr)) {
1880 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1881 getnc_state->ncRoot_dn,
1882 schema, getnc_state->min_usn,
1883 req10->replica_flags,
1885 &getnc_state->la_list,
1886 &getnc_state->la_count,
1887 req10->uptodateness_vector);
1888 if (!W_ERROR_IS_OK(werr)) {
1892 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1893 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1894 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1897 if (obj->meta_data_ctr == NULL) {
1898 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1899 ldb_dn_get_linearized(msg->dn)));
1900 /* no attributes to send */
1905 r->out.ctr->ctr6.object_count++;
1907 *currentObject = obj;
1908 currentObject = &obj->next_object;
1910 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1912 talloc_free(getnc_state->last_dn);
1913 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
1915 talloc_free(msg_res);
1916 talloc_free(msg_dn);
1919 getnc_state->num_processed = i;
1921 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1923 /* the client can us to call UpdateRefs on its behalf to
1924 re-establish monitoring of the NC */
1925 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1926 !GUID_all_zero(&req10->destination_dsa_guid)) {
1927 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1928 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1929 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1930 ureq.naming_context = ncRoot;
1931 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
1932 &req10->destination_dsa_guid);
1933 if (!ureq.dest_dsa_dns_name) {
1936 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1937 ureq.options = DRSUAPI_DRS_ADD_REF |
1938 DRSUAPI_DRS_ASYNC_OP |
1939 DRSUAPI_DRS_GETCHG_CHECK;
1941 /* we also need to pass through the
1942 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1943 to send notifies using the GC SPN */
1944 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1946 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1947 if (!W_ERROR_IS_OK(werr)) {
1948 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1955 * This is just a guess, how to calculate the
1956 * number of linked attributes to send, we need to
1957 * find out how to do this right.
1959 if (r->out.ctr->ctr6.object_count >= max_links) {
1962 max_links -= r->out.ctr->ctr6.object_count;
1965 link_total = getnc_state->la_count;
1967 if (i < getnc_state->num_records) {
1968 r->out.ctr->ctr6.more_data = true;
1970 /* sort the whole array the first time */
1971 if (!getnc_state->la_sorted) {
1972 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1973 sam_ctx, linked_attribute_compare);
1974 getnc_state->la_sorted = true;
1977 link_count = getnc_state->la_count - getnc_state->la_idx;
1978 link_count = MIN(max_links, link_count);
1980 r->out.ctr->ctr6.linked_attributes_count = link_count;
1981 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1983 getnc_state->la_idx += link_count;
1984 link_given = getnc_state->la_idx;
1986 if (getnc_state->la_idx < getnc_state->la_count) {
1987 r->out.ctr->ctr6.more_data = true;
1991 if (!r->out.ctr->ctr6.more_data) {
1992 talloc_steal(mem_ctx, getnc_state->la_list);
1994 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1995 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1997 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1998 r->out.ctr->ctr6.uptodateness_vector);
1999 if (!W_ERROR_IS_OK(werr)) {
2003 talloc_free(getnc_state);
2004 b_state->getncchanges_state = NULL;
2007 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2008 r->out.ctr->ctr6.uptodateness_vector = NULL;
2009 r->out.ctr->ctr6.nc_object_count = 0;
2010 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2013 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2014 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2015 (unsigned long long)(req10->highwatermark.highest_usn+1),
2016 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2017 r->out.ctr->ctr6.object_count,
2018 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2019 r->out.ctr->ctr6.linked_attributes_count,
2020 link_given, link_total,
2021 dom_sid_string(mem_ctx, user_sid)));
2024 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2025 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);