2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009-2010
8 Copyright (C) Andrew Bartlett 2010-2016
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "rpc_server/dcerpc_server.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "param/param.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "libcli/security/security.h"
32 #include "libcli/security/session.h"
33 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
34 #include "rpc_server/dcerpc_server_proto.h"
35 #include "../libcli/drsuapi/drsuapi.h"
36 #include "lib/util/binsearch.h"
37 #include "lib/util/tsort.h"
38 #include "auth/session.h"
39 #include "dsdb/common/util.h"
41 /* state of a partially completed getncchanges call */
42 struct drsuapi_getncchanges_state {
45 uint32_t num_processed;
46 struct ldb_dn *ncRoot_dn;
50 struct drsuapi_DsReplicaHighWaterMark last_hwm;
51 struct ldb_dn *last_dn;
52 struct drsuapi_DsReplicaHighWaterMark final_hwm;
53 struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
54 struct drsuapi_DsReplicaLinkedAttribute *la_list;
60 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
61 const struct drsuapi_DsReplicaHighWaterMark *h2)
63 if (h1->highest_usn < h2->highest_usn) {
65 } else if (h1->highest_usn > h2->highest_usn) {
67 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
69 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
71 } else if (h1->reserved_usn < h2->reserved_usn) {
73 } else if (h1->reserved_usn > h2->reserved_usn) {
81 build a DsReplicaObjectIdentifier from a ldb msg
83 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
84 struct ldb_message *msg)
86 struct drsuapi_DsReplicaObjectIdentifier *identifier;
89 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
90 if (identifier == NULL) {
94 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
95 identifier->guid = samdb_result_guid(msg, "objectGUID");
97 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
99 identifier->sid = *sid;
101 ZERO_STRUCT(identifier->sid);
106 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
108 return GUID_compare(guid1, &guid2);
112 see if we can filter an attribute using the uptodateness_vector
114 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
115 const struct GUID *originating_invocation_id,
116 uint64_t originating_usn)
118 const struct drsuapi_DsReplicaCursor *c;
119 if (udv == NULL) return false;
120 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
121 originating_invocation_id, udv_compare, c);
122 if (c && originating_usn <= c->highest_usn) {
129 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
131 if (a1 == a2) return 0;
132 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
136 check if an attribute is in a partial_attribute_set
138 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
139 struct drsuapi_DsPartialAttributeSet *pas)
141 enum drsuapi_DsAttributeId *result;
142 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
144 return result != NULL;
149 drsuapi_DsGetNCChanges for one object
151 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
152 struct ldb_message *msg,
153 struct ldb_context *sam_ctx,
154 struct ldb_dn *ncRoot_dn,
156 struct dsdb_schema *schema,
157 DATA_BLOB *session_key,
158 uint64_t highest_usn,
159 uint32_t replica_flags,
160 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
161 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
162 enum drsuapi_DsExtendedOperation extended_op,
163 bool force_object_return)
165 const struct ldb_val *md_value;
167 struct replPropertyMetaDataBlob md;
169 enum ndr_err_code ndr_err;
172 const struct dsdb_attribute *rdn_sa;
173 unsigned int instanceType;
174 struct dsdb_syntax_ctx syntax_ctx;
176 /* make dsdb sytanx context for conversions */
177 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
178 syntax_ctx.is_schema_nc = is_schema_nc;
180 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
181 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
182 obj->is_nc_prefix = true;
183 obj->parent_object_guid = NULL;
185 obj->is_nc_prefix = false;
186 obj->parent_object_guid = talloc(obj, struct GUID);
187 if (obj->parent_object_guid == NULL) {
188 return WERR_DS_DRA_INTERNAL_ERROR;
190 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
191 if (GUID_all_zero(obj->parent_object_guid)) {
192 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
193 ldb_dn_get_linearized(msg->dn)));
194 return WERR_DS_DRA_INTERNAL_ERROR;
197 obj->next_object = NULL;
199 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
201 /* nothing to send */
205 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
206 /* don't send uninstantiated objects */
210 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
211 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
212 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
213 return WERR_DS_DRA_INTERNAL_ERROR;
216 if (md.version != 1) {
217 return WERR_DS_DRA_INTERNAL_ERROR;
220 rdn = ldb_dn_get_rdn_name(msg->dn);
222 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
223 return WERR_DS_DRA_INTERNAL_ERROR;
226 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
227 if (rdn_sa == NULL) {
228 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
229 rdn, ldb_dn_get_linearized(msg->dn)));
230 return WERR_DS_DRA_INTERNAL_ERROR;
233 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
234 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
236 obj->object.identifier = get_object_identifier(obj, msg);
237 if (obj->object.identifier == NULL) {
240 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
242 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
243 for (n=i=0; i<md.ctr.ctr1.count; i++) {
244 const struct dsdb_attribute *sa;
245 bool force_attribute = false;
247 /* if the attribute has not changed, and it is not the
248 instanceType then don't include it */
249 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
250 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
251 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
253 /* don't include the rDN */
254 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
256 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
258 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
259 (unsigned int)md.ctr.ctr1.array[i].attid,
260 ldb_dn_get_linearized(msg->dn)));
261 return WERR_DS_DRA_INTERNAL_ERROR;
265 struct ldb_message_element *el;
266 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
267 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
268 /* don't send upgraded links inline */
273 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
274 !dsdb_attr_in_rodc_fas(sa)) {
275 force_attribute = true;
276 DEBUG(4,("Forcing attribute %s in %s\n",
277 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
280 /* filter by uptodateness_vector */
281 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
283 udv_filter(uptodateness_vector,
284 &md.ctr.ctr1.array[i].originating_invocation_id,
285 md.ctr.ctr1.array[i].originating_usn)) {
289 /* filter by partial_attribute_set */
290 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
294 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
295 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
296 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
297 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
298 attids[n] = md.ctr.ctr1.array[i].attid;
302 /* ignore it if its an empty change. Note that renames always
303 * change the 'name' attribute, so they won't be ignored by
306 * the force_object_return check is used to force an empty
307 * object return when we timeout in the getncchanges loop.
308 * This allows us to return an empty object, which keeps the
309 * client happy while preventing timeouts
313 attids[0] == DRSUAPI_ATTID_instanceType &&
314 !force_object_return)) {
315 talloc_free(obj->meta_data_ctr);
316 obj->meta_data_ctr = NULL;
320 obj->meta_data_ctr->count = n;
322 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
323 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
324 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
325 obj->object.attribute_ctr.num_attributes);
326 if (obj->object.attribute_ctr.attributes == NULL) {
331 * Note that the meta_data array and the attributes array must
332 * be the same size and in the same order
334 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
335 struct ldb_message_element *el;
337 const struct dsdb_attribute *sa;
339 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
341 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
342 return WERR_DS_DRA_INTERNAL_ERROR;
345 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
347 /* this happens for attributes that have been removed */
348 DEBUG(5,("No element '%s' for attributeID %u in message\n",
349 sa->lDAPDisplayName, attids[i]));
350 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
351 obj->object.attribute_ctr.attributes[i].attid =
352 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
354 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
355 &obj->object.attribute_ctr.attributes[i]);
356 if (!W_ERROR_IS_OK(werr)) {
357 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
358 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
362 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
363 * check if attribute is secret and send a null value
365 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
366 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
367 &obj->meta_data_ctr->meta_data[i]);
369 /* some attributes needs to be encrypted
371 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
372 &obj->object.attribute_ctr.attributes[i]);
373 if (!W_ERROR_IS_OK(werr)) {
374 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
375 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
380 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
381 DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID: "
385 ldb_dn_get_linearized(msg->dn),
387 obj->object.attribute_ctr.attributes[i].attid));
388 return WERR_DS_DATABASE_ERROR;
396 add one linked attribute from an object to the list of linked
397 attributes in a getncchanges request
399 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
400 struct ldb_context *sam_ctx,
401 const struct dsdb_schema *schema,
402 const struct dsdb_attribute *sa,
403 struct ldb_message *msg,
404 struct dsdb_dn *dsdb_dn,
405 struct drsuapi_DsReplicaLinkedAttribute **la_list,
408 struct drsuapi_DsReplicaLinkedAttribute *la;
413 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
414 W_ERROR_HAVE_NO_MEMORY(*la_list);
416 la = &(*la_list)[*la_count];
418 la->identifier = get_object_identifier(*la_list, msg);
419 W_ERROR_HAVE_NO_MEMORY(la->identifier);
421 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
424 /* We have to check that the inactive link still point to an existing object */
430 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
431 if (strncmp(v, "TRUE", 4) == 0) {
433 * Note: we skip the transmition of the deleted link even if the other part used to
434 * know about it because when we transmit the deletion of the object, the link will
435 * be deleted too due to deletion of object where link points and Windows do so.
437 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
438 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
440 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
441 * if it join an existing domain with deleted objets, it firsts impose to have a
442 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
443 * either during initial replication or after the getNCChanges.
444 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
446 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
447 * If FL >=2K8R2 we are sure that this attribute will be here.
448 * For this kind of forest level we do not return the link if the object is recycled
449 * (isRecycled = true).
451 if (strncmp(v, "TRUE", 4) == 0) {
452 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
453 ldb_dn_get_linearized(msg->dn)));
460 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
461 if (!NT_STATUS_IS_OK(status)) {
462 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
463 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
464 return ntstatus_to_werror(status);
466 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
467 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
468 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
469 GUID_string(mem_ctx, &guid)));
471 } else if (ret != LDB_SUCCESS) {
472 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
473 GUID_string(mem_ctx, &guid),
478 la->attid = sa->attributeID_id;
479 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
481 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
482 if (!NT_STATUS_IS_OK(status)) {
483 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
484 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
485 return ntstatus_to_werror(status);
487 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
488 if (!NT_STATUS_IS_OK(status)) {
489 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
490 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
491 return ntstatus_to_werror(status);
493 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
494 if (!NT_STATUS_IS_OK(status)) {
495 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
496 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
497 return ntstatus_to_werror(status);
499 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
500 if (!NT_STATUS_IS_OK(status)) {
501 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
502 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
503 return ntstatus_to_werror(status);
506 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
507 if (!NT_STATUS_IS_OK(status)) {
508 /* this is possible for upgraded links */
509 la->originating_add_time = la->meta_data.originating_change_time;
512 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
513 W_ERROR_NOT_OK_RETURN(werr);
521 add linked attributes from an object to the list of linked
522 attributes in a getncchanges request
524 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
526 struct ldb_dn *ncRoot_dn,
527 struct dsdb_schema *schema,
528 uint64_t highest_usn,
529 uint32_t replica_flags,
530 struct ldb_message *msg,
531 struct drsuapi_DsReplicaLinkedAttribute **la_list,
533 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
536 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
537 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
539 for (i=0; i<msg->num_elements; i++) {
540 struct ldb_message_element *el = &msg->elements[i];
541 const struct dsdb_attribute *sa;
544 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
546 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
547 /* we only want forward links */
551 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
552 /* its an old style link, it will have been
553 * sent in the main replication data */
557 for (j=0; j<el->num_values; j++) {
558 struct dsdb_dn *dsdb_dn;
563 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
564 if (dsdb_dn == NULL) {
565 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
566 el->name, ldb_dn_get_linearized(msg->dn)));
567 talloc_free(tmp_ctx);
568 return WERR_DS_DRA_INTERNAL_ERROR;
571 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
572 if (!NT_STATUS_IS_OK(status)) {
573 /* this can happen for attributes
574 given to us with old style meta
579 if (local_usn > uSNChanged) {
580 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
581 el->name, ldb_dn_get_linearized(msg->dn)));
582 talloc_free(tmp_ctx);
583 return WERR_DS_DRA_INTERNAL_ERROR;
586 if (local_usn < highest_usn) {
590 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
591 dsdb_dn, la_list, la_count);
592 if (!W_ERROR_IS_OK(werr)) {
593 talloc_free(tmp_ctx);
599 talloc_free(tmp_ctx);
604 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
606 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
607 struct ldb_dn *ncRoot_dn,
608 struct drsuapi_DsReplicaCursor2CtrEx *udv)
616 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
617 if (ret != LDB_SUCCESS) {
618 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
619 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
620 return WERR_DS_DRA_INTERNAL_ERROR;
627 /* comparison function for linked attributes - see CompareLinks() in
628 * MS-DRSR section 4.1.10.5.17 */
629 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
630 const struct drsuapi_DsReplicaLinkedAttribute *la2,
631 struct ldb_context *sam_ctx)
636 const struct dsdb_schema *schema;
637 const struct dsdb_attribute *schema_attrib;
638 struct dsdb_dn *dn1, *dn2;
639 struct GUID guid1, guid2;
642 c = GUID_compare(&la1->identifier->guid,
643 &la2->identifier->guid);
644 if (c != 0) return c;
646 if (la1->attid != la2->attid) {
647 return la1->attid < la2->attid? -1:1;
650 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
651 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
652 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
655 /* we need to get the target GUIDs to compare */
656 tmp_ctx = talloc_new(sam_ctx);
658 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
659 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
661 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
662 if (!W_ERROR_IS_OK(werr)) {
663 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
664 talloc_free(tmp_ctx);
668 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
669 if (!W_ERROR_IS_OK(werr)) {
670 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
671 talloc_free(tmp_ctx);
675 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
676 if (!NT_STATUS_IS_OK(status)) {
677 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
678 talloc_free(tmp_ctx);
681 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
682 if (!NT_STATUS_IS_OK(status)) {
683 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
684 talloc_free(tmp_ctx);
688 talloc_free(tmp_ctx);
690 return GUID_compare(&guid1, &guid2);
693 struct drsuapi_changed_objects {
700 sort the objects we send by tree order
702 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
703 struct drsuapi_changed_objects *m2,
704 struct drsuapi_getncchanges_state *getnc_state)
706 return ldb_dn_compare(m2->dn, m1->dn);
710 sort the objects we send first by uSNChanged
712 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
713 struct drsuapi_changed_objects *m2,
714 struct drsuapi_getncchanges_state *getnc_state)
718 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
723 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
728 if (m1->usn == m2->usn) {
729 return ldb_dn_compare(m2->dn, m1->dn);
732 if (m1->usn < m2->usn) {
741 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
743 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
745 struct drsuapi_DsGetNCChangesRequest10 *req10,
746 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
747 struct ldb_dn **rid_manager_dn)
749 struct ldb_dn *req_dn, *ntds_dn = NULL;
751 struct ldb_context *ldb = b_state->sam_ctx;
752 struct ldb_result *ext_res;
753 struct dsdb_fsmo_extended_op *exop;
758 - verify that the DN being asked for is the RID Manager DN
759 - verify that we are the RID Manager
762 /* work out who is the RID Manager, also return to caller */
763 ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
764 if (ret != LDB_SUCCESS) {
765 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
766 return WERR_DS_DRA_INTERNAL_ERROR;
769 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
770 if (!ldb_dn_validate(req_dn) ||
771 ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
772 /* that isn't the RID Manager DN */
773 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
774 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
775 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
779 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
780 ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
781 if (ret != LDB_SUCCESS) {
782 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
783 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
784 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
788 /* find the DN of the RID Manager */
789 ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
790 if (ret != LDB_SUCCESS) {
791 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
792 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
793 return WERR_DS_DRA_INTERNAL_ERROR;
797 /* we're not the RID Manager - go away */
798 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
799 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
803 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
804 W_ERROR_HAVE_NO_MEMORY(exop);
806 exop->fsmo_info = req10->fsmo_info;
807 exop->destination_dsa_guid = req10->destination_dsa_guid;
809 ret = ldb_transaction_start(ldb);
810 if (ret != LDB_SUCCESS) {
811 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
812 ldb_errstring(ldb)));
813 return WERR_DS_DRA_INTERNAL_ERROR;
816 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
817 if (ret != LDB_SUCCESS) {
818 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
819 ldb_errstring(ldb)));
820 ldb_transaction_cancel(ldb);
821 return WERR_DS_DRA_INTERNAL_ERROR;
824 ret = ldb_transaction_commit(ldb);
825 if (ret != LDB_SUCCESS) {
826 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
827 ldb_errstring(ldb)));
828 return WERR_DS_DRA_INTERNAL_ERROR;
831 talloc_free(ext_res);
833 DEBUG(2,("Allocated RID pool for server %s\n",
834 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
836 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
842 return an array of SIDs from a ldb_message given an attribute name
843 assumes the SIDs are in extended DN format
845 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
846 struct ldb_message *msg,
849 const struct dom_sid ***sids)
851 struct ldb_message_element *el;
854 el = ldb_msg_find_element(msg, attr);
860 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
861 W_ERROR_HAVE_NO_MEMORY(*sids);
863 for (i=0; i<el->num_values; i++) {
864 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
868 sid = talloc(*sids, struct dom_sid);
869 W_ERROR_HAVE_NO_MEMORY(sid);
870 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
871 if (!NT_STATUS_IS_OK(status)) {
872 return WERR_INTERNAL_DB_CORRUPTION;
883 return an array of SIDs from a ldb_message given an attribute name
884 assumes the SIDs are in NDR form
886 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
887 struct ldb_message *msg,
890 const struct dom_sid ***sids)
892 struct ldb_message_element *el;
895 el = ldb_msg_find_element(msg, attr);
901 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
902 W_ERROR_HAVE_NO_MEMORY(*sids);
904 for (i=0; i<el->num_values; i++) {
905 enum ndr_err_code ndr_err;
908 sid = talloc(*sids, struct dom_sid);
909 W_ERROR_HAVE_NO_MEMORY(sid);
911 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
912 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
913 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
914 return WERR_INTERNAL_DB_CORRUPTION;
924 see if any SIDs in list1 are in list2
926 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
929 /* do we ever have enough SIDs here to worry about O(n^2) ? */
930 for (i=0; list1[i]; i++) {
931 for (j=0; list2[j]; j++) {
932 if (dom_sid_equal(list1[i], list2[j])) {
941 handle a DRSUAPI_EXOP_REPL_SECRET call
943 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
945 struct drsuapi_DsGetNCChangesRequest10 *req10,
946 struct dom_sid *user_sid,
947 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
948 bool has_get_all_changes)
950 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
951 struct ldb_dn *obj_dn = NULL;
952 struct ldb_dn *rodc_dn, *krbtgt_link_dn;
954 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
955 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
956 struct ldb_result *rodc_res, *obj_res;
957 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
960 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
961 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
964 * we need to work out if we will allow this DC to
965 * replicate the secrets for this object
967 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
971 if (b_state->sam_ctx_system == NULL) {
972 /* this operation needs system level access */
973 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
974 return WERR_DS_DRA_SOURCE_DISABLED;
978 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
980 * The pseudo code indicate
981 * revealsecrets = true
982 * if IsRevealSecretRequest(msgIn) then
983 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
985 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
986 * <... check if this account is ok to be replicated on this DC ...>
987 * <... and if not reveal secrets = no ...>
989 * reveal secrets = false
994 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
995 * then you can do EXOP_REPL_SECRETS
997 if (has_get_all_changes) {
1001 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
1002 if (!ldb_dn_validate(obj_dn)) goto failed;
1004 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1005 dom_sid_string(mem_ctx, user_sid));
1006 if (!ldb_dn_validate(rodc_dn)) goto failed;
1008 /* do the two searches we need */
1009 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1010 DSDB_SEARCH_SHOW_EXTENDED_DN);
1011 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1013 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1014 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1016 /* if the object SID is equal to the user_sid, allow */
1017 if (dom_sid_equal(user_sid,
1018 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1022 /* an RODC is allowed to get its own krbtgt account secrets */
1023 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1024 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1025 if (krbtgt_link_dn != NULL &&
1026 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1030 /* but it isn't allowed to get anyone elses krbtgt secrets */
1031 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1032 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1036 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1037 "userAccountControl", 0) &
1038 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1042 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1043 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1044 if (!W_ERROR_IS_OK(werr)) {
1048 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1049 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1050 if (!W_ERROR_IS_OK(werr)) {
1054 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1055 mem_ctx, "tokenGroups", &token_sids);
1056 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1060 if (never_reveal_sids &&
1061 sid_list_match(token_sids, never_reveal_sids)) {
1066 sid_list_match(token_sids, reveal_sids)) {
1072 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1073 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1074 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1075 return WERR_DS_DRA_ACCESS_DENIED;
1078 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1079 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1080 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1081 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1082 req10->highwatermark.highest_usn = 0;
1086 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1087 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1088 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1089 return WERR_DS_DRA_BAD_DN;
1094 handle a DRSUAPI_EXOP_REPL_OBJ call
1096 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1097 TALLOC_CTX *mem_ctx,
1098 struct drsuapi_DsGetNCChangesRequest10 *req10,
1099 struct dom_sid *user_sid,
1100 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1102 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1104 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1105 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1107 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1113 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1114 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1115 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1117 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1118 TALLOC_CTX *mem_ctx,
1119 struct drsuapi_DsGetNCChangesRequest10 *req10,
1120 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1122 struct ldb_dn *req_dn, *ntds_dn;
1125 struct ldb_context *ldb = b_state->sam_ctx;
1126 struct ldb_message *msg;
1131 - verify that the client dn exists
1132 - verify that we are the current master
1135 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1136 if (!ldb_dn_validate(req_dn)) {
1137 /* that is not a valid dn */
1138 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1139 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1140 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1144 /* find the DN of the current role owner */
1145 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1146 if (ret != LDB_SUCCESS) {
1147 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1148 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1149 return WERR_DS_DRA_INTERNAL_ERROR;
1153 /* we're not the RID Manager or role owner - go away */
1154 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1155 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1159 /* change the current master */
1160 msg = ldb_msg_new(ldb);
1161 W_ERROR_HAVE_NO_MEMORY(msg);
1162 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1163 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1165 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1166 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1167 if (ret != LDB_SUCCESS) {
1168 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1169 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1171 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1175 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1178 return WERR_DS_DRA_INTERNAL_ERROR;
1181 for (i=0;i<msg->num_elements;i++) {
1182 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1185 ret = ldb_transaction_start(ldb);
1186 if (ret != LDB_SUCCESS) {
1187 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1188 ldb_errstring(ldb)));
1189 return WERR_DS_DRA_INTERNAL_ERROR;
1192 ret = ldb_modify(ldb, msg);
1193 if (ret != LDB_SUCCESS) {
1194 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1195 ldb_errstring(ldb)));
1196 ldb_transaction_cancel(ldb);
1197 return WERR_DS_DRA_INTERNAL_ERROR;
1200 ret = ldb_transaction_commit(ldb);
1201 if (ret != LDB_SUCCESS) {
1202 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1203 ldb_errstring(ldb)));
1204 return WERR_DS_DRA_INTERNAL_ERROR;
1207 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1213 see if this getncchanges request includes a request to reveal secret information
1215 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1216 struct drsuapi_DsGetNCChangesRequest10 *req10,
1217 bool *is_secret_request)
1219 enum drsuapi_DsExtendedOperation exop;
1221 struct dsdb_schema *schema;
1223 *is_secret_request = true;
1225 exop = req10->extended_op;
1228 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1229 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1230 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1231 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1232 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1233 /* FSMO exops can reveal secrets */
1234 *is_secret_request = true;
1236 case DRSUAPI_EXOP_REPL_SECRET:
1237 case DRSUAPI_EXOP_REPL_OBJ:
1238 case DRSUAPI_EXOP_NONE:
1242 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1243 *is_secret_request = false;
1247 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1248 req10->partial_attribute_set == NULL) {
1249 /* they want secrets */
1250 *is_secret_request = true;
1254 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1256 /* check the attributes they asked for */
1257 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1258 const struct dsdb_attribute *sa;
1259 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1261 return WERR_DS_DRA_SCHEMA_MISMATCH;
1263 if (!dsdb_attr_in_rodc_fas(sa)) {
1264 *is_secret_request = true;
1269 if (req10->partial_attribute_set_ex) {
1270 /* check the extended attributes they asked for */
1271 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1272 const struct dsdb_attribute *sa;
1273 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1275 return WERR_DS_DRA_SCHEMA_MISMATCH;
1277 if (!dsdb_attr_in_rodc_fas(sa)) {
1278 *is_secret_request = true;
1284 *is_secret_request = false;
1289 see if this getncchanges request is only for attributes in the GC
1290 partial attribute set
1292 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1293 struct drsuapi_DsGetNCChangesRequest10 *req10,
1294 bool *is_gc_pas_request)
1296 enum drsuapi_DsExtendedOperation exop;
1298 struct dsdb_schema *schema;
1300 exop = req10->extended_op;
1303 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1304 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1305 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1306 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1307 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1308 case DRSUAPI_EXOP_REPL_SECRET:
1309 *is_gc_pas_request = false;
1311 case DRSUAPI_EXOP_REPL_OBJ:
1312 case DRSUAPI_EXOP_NONE:
1316 if (req10->partial_attribute_set == NULL) {
1317 /* they want it all */
1318 *is_gc_pas_request = false;
1322 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1324 /* check the attributes they asked for */
1325 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1326 const struct dsdb_attribute *sa;
1327 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1329 return WERR_DS_DRA_SCHEMA_MISMATCH;
1331 if (!sa->isMemberOfPartialAttributeSet) {
1332 *is_gc_pas_request = false;
1337 if (req10->partial_attribute_set_ex) {
1338 /* check the extended attributes they asked for */
1339 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1340 const struct dsdb_attribute *sa;
1341 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1343 return WERR_DS_DRA_SCHEMA_MISMATCH;
1345 if (!sa->isMemberOfPartialAttributeSet) {
1346 *is_gc_pas_request = false;
1352 *is_gc_pas_request = true;
1358 map from req8 to req10
1360 static struct drsuapi_DsGetNCChangesRequest10 *
1361 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1362 struct drsuapi_DsGetNCChangesRequest8 *req8)
1364 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1365 struct drsuapi_DsGetNCChangesRequest10);
1366 if (req10 == NULL) {
1370 req10->destination_dsa_guid = req8->destination_dsa_guid;
1371 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1372 req10->naming_context = req8->naming_context;
1373 req10->highwatermark = req8->highwatermark;
1374 req10->uptodateness_vector = req8->uptodateness_vector;
1375 req10->replica_flags = req8->replica_flags;
1376 req10->max_object_count = req8->max_object_count;
1377 req10->max_ndr_size = req8->max_ndr_size;
1378 req10->extended_op = req8->extended_op;
1379 req10->fsmo_info = req8->fsmo_info;
1380 req10->partial_attribute_set = req8->partial_attribute_set;
1381 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1382 req10->mapping_ctr = req8->mapping_ctr;
1387 static const char *collect_objects_attrs[] = { "uSNChanged",
1392 * Collects object for normal replication cycle.
1394 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1395 TALLOC_CTX *mem_ctx,
1396 struct drsuapi_DsGetNCChangesRequest10 *req10,
1397 struct ldb_dn *search_dn,
1398 const char *extra_filter,
1399 struct ldb_result **search_res)
1402 char* search_filter;
1403 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1404 //const char *extra_filter;
1405 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1407 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1408 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1409 scope = LDB_SCOPE_BASE;
1412 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1414 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1416 /* Construct response. */
1417 search_filter = talloc_asprintf(mem_ctx,
1418 "(uSNChanged>=%llu)",
1419 (unsigned long long)(getnc_state->min_usn+1));
1422 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1425 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1426 search_filter = talloc_asprintf(mem_ctx,
1427 "(&%s(isCriticalSystemObject=TRUE))",
1431 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1432 scope = LDB_SCOPE_BASE;
1436 search_dn = getnc_state->ncRoot_dn;
1439 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1440 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1441 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1443 collect_objects_attrs,
1445 if (ret != LDB_SUCCESS) {
1446 return WERR_DS_DRA_INTERNAL_ERROR;
1453 * Collects object for normal replication cycle.
1455 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1456 TALLOC_CTX *mem_ctx,
1457 struct drsuapi_DsGetNCChangesRequest10 *req10,
1458 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1459 struct ldb_dn *search_dn,
1460 const char *extra_filter,
1461 struct ldb_result **search_res)
1463 /* we have nothing to do in case of ex-op failure */
1464 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1468 switch (req10->extended_op) {
1469 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1472 struct ldb_dn *ntds_dn = NULL;
1473 struct ldb_dn *server_dn = NULL;
1474 struct ldb_dn *machine_dn = NULL;
1475 struct ldb_dn *rid_set_dn = NULL;
1476 struct ldb_result *search_res2 = NULL;
1477 struct ldb_result *search_res3 = NULL;
1478 TALLOC_CTX *frame = talloc_stackframe();
1479 /* get RID manager, RID set and server DN (in that order) */
1481 /* This first search will get the RID Manager */
1482 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1484 search_dn, LDB_SCOPE_BASE,
1485 collect_objects_attrs,
1487 if (ret != LDB_SUCCESS) {
1488 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1489 ldb_dn_get_linearized(search_dn),
1490 ldb_errstring(b_state->sam_ctx)));
1492 return WERR_DS_DRA_INTERNAL_ERROR;
1495 if ((*search_res)->count != 1) {
1496 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1497 ldb_dn_get_linearized(search_dn),
1498 (*search_res)->count));
1500 return WERR_DS_DRA_INTERNAL_ERROR;
1503 /* Now extend it to the RID set */
1505 /* Find the computer account DN for the destination
1506 * dsa GUID specified */
1508 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1509 &req10->destination_dsa_guid, 0,
1511 if (ret != LDB_SUCCESS) {
1512 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1514 &req10->destination_dsa_guid),
1515 ldb_errstring(b_state->sam_ctx)));
1517 return WERR_DS_DRA_INTERNAL_ERROR;
1520 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1523 return WERR_DS_DRA_INTERNAL_ERROR;
1526 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1527 "serverReference", &machine_dn);
1528 if (ret != LDB_SUCCESS) {
1529 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1530 ldb_dn_get_linearized(server_dn),
1531 ldb_errstring(b_state->sam_ctx)));
1533 return WERR_DS_DRA_INTERNAL_ERROR;
1536 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1537 "rIDSetReferences", &rid_set_dn);
1538 if (ret != LDB_SUCCESS) {
1539 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1540 ldb_dn_get_linearized(server_dn),
1541 ldb_errstring(b_state->sam_ctx)));
1543 return WERR_DS_DRA_INTERNAL_ERROR;
1547 /* This first search will get the RID Manager, now get the RID set */
1548 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1550 rid_set_dn, LDB_SCOPE_BASE,
1551 collect_objects_attrs,
1553 if (ret != LDB_SUCCESS) {
1554 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1555 ldb_dn_get_linearized(rid_set_dn),
1556 ldb_errstring(b_state->sam_ctx)));
1558 return WERR_DS_DRA_INTERNAL_ERROR;
1561 if (search_res2->count != 1) {
1562 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1563 ldb_dn_get_linearized(rid_set_dn),
1564 search_res2->count));
1566 return WERR_DS_DRA_INTERNAL_ERROR;
1569 /* Finally get the server DN */
1570 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1572 machine_dn, LDB_SCOPE_BASE,
1573 collect_objects_attrs,
1575 if (ret != LDB_SUCCESS) {
1576 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1577 ldb_dn_get_linearized(server_dn),
1578 ldb_errstring(b_state->sam_ctx)));
1580 return WERR_DS_DRA_INTERNAL_ERROR;
1583 if (search_res3->count != 1) {
1584 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1585 ldb_dn_get_linearized(server_dn),
1586 search_res3->count));
1588 return WERR_DS_DRA_INTERNAL_ERROR;
1591 /* Now extend the original search_res with these answers */
1592 (*search_res)->count = 3;
1594 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1595 struct ldb_message *,
1596 (*search_res)->count);
1597 if ((*search_res)->msgs == NULL) {
1603 talloc_steal(mem_ctx, *search_res);
1604 (*search_res)->msgs[1] =
1605 talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1606 (*search_res)->msgs[2] =
1607 talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1613 /* TODO: implement extended op specific collection
1614 * of objects. Right now we just normal procedure
1615 * for collecting objects */
1616 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1621 drsuapi_DsGetNCChanges
1623 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1625 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1626 struct drsuapi_DsGetNCChanges *r)
1628 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1631 struct dsdb_schema *schema;
1632 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1633 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1635 DATA_BLOB session_key;
1637 struct dcesrv_handle *h;
1638 struct drsuapi_bind_state *b_state;
1639 struct drsuapi_getncchanges_state *getnc_state;
1640 struct drsuapi_DsGetNCChangesRequest10 *req10;
1642 uint32_t max_objects;
1644 uint32_t link_count = 0;
1645 uint32_t link_total = 0;
1646 uint32_t link_given = 0;
1647 struct ldb_dn *search_dn = NULL;
1648 bool am_rodc, null_scope=false;
1649 enum security_user_level security_level;
1650 struct ldb_context *sam_ctx;
1651 struct dom_sid *user_sid;
1652 bool is_secret_request;
1653 bool is_gc_pas_request;
1654 struct drsuapi_changed_objects *changes;
1656 time_t start = time(NULL);
1657 bool max_wait_reached = false;
1658 bool has_get_all_changes = false;
1659 struct GUID invocation_id;
1660 static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
1662 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1665 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1667 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1669 *r->out.level_out = 6;
1670 /* TODO: linked attributes*/
1671 r->out.ctr->ctr6.linked_attributes_count = 0;
1672 r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
1674 r->out.ctr->ctr6.object_count = 0;
1675 r->out.ctr->ctr6.nc_object_count = 0;
1676 r->out.ctr->ctr6.more_data = false;
1677 r->out.ctr->ctr6.uptodateness_vector = NULL;
1678 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1679 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1680 r->out.ctr->ctr6.first_object = NULL;
1682 /* a RODC doesn't allow for any replication */
1683 ret = samdb_rodc(sam_ctx, &am_rodc);
1684 if (ret == LDB_SUCCESS && am_rodc) {
1685 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1686 return WERR_DS_DRA_SOURCE_DISABLED;
1689 /* Check request revision.
1691 switch (r->in.level) {
1693 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1694 if (req10 == NULL) {
1699 req10 = &r->in.req->req10;
1702 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1704 return WERR_REVISION_MISMATCH;
1708 /* Perform access checks. */
1709 /* TODO: we need to support a sync on a specific non-root
1710 * DN. We'll need to find the real partition root here */
1711 ncRoot = req10->naming_context;
1712 if (ncRoot == NULL) {
1713 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1714 return WERR_DS_DRA_INVALID_PARAMETER;
1717 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1718 return WERR_DS_DRA_INTERNAL_ERROR;
1721 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1722 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1723 return WERR_DS_DRA_SOURCE_DISABLED;
1726 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1728 /* all clients must have GUID_DRS_GET_CHANGES */
1729 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1731 dce_call->conn->auth_state.session_info->security_token,
1732 req10->naming_context,
1733 GUID_DRS_GET_CHANGES);
1734 if (!W_ERROR_IS_OK(werr)) {
1738 /* allowed if the GC PAS and client has
1739 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1740 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1741 if (!W_ERROR_IS_OK(werr)) {
1744 if (is_gc_pas_request) {
1745 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1747 dce_call->conn->auth_state.session_info->security_token,
1748 req10->naming_context,
1749 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1750 if (W_ERROR_IS_OK(werr)) {
1755 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1756 if (!W_ERROR_IS_OK(werr)) {
1759 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1760 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1762 dce_call->conn->auth_state.session_info->security_token,
1763 req10->naming_context,
1764 GUID_DRS_GET_ALL_CHANGES);
1765 if (!W_ERROR_IS_OK(werr)) {
1768 has_get_all_changes = true;
1773 /* for non-administrator replications, check that they have
1774 given the correct source_dsa_invocation_id */
1775 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1776 samdb_domain_sid(sam_ctx));
1777 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1778 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1779 /* we rely on this flag being unset for RODC requests */
1780 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1784 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1785 /* Ignore the _in_ uptpdateness vector*/
1786 req10->uptodateness_vector = NULL;
1789 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1790 req10->source_dsa_invocation_id = invocation_id;
1793 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1795 * The given highwatermark is only valid relative to the
1796 * specified source_dsa_invocation_id.
1798 ZERO_STRUCT(req10->highwatermark);
1801 getnc_state = b_state->getncchanges_state;
1803 /* see if a previous replication has been abandoned */
1805 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1806 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1807 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1808 ldb_dn_get_linearized(new_dn),
1809 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1810 ldb_dn_get_linearized(getnc_state->last_dn)));
1811 talloc_free(getnc_state);
1817 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1818 &req10->highwatermark);
1820 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1821 "on DN %s %s highwatermark (last_dn %s)\n",
1822 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1823 (ret > 0) ? "older" : "newer",
1824 ldb_dn_get_linearized(getnc_state->last_dn)));
1825 talloc_free(getnc_state);
1830 if (getnc_state == NULL) {
1831 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1832 if (getnc_state == NULL) {
1835 b_state->getncchanges_state = getnc_state;
1836 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1838 /* find out if we are to replicate Schema NC */
1839 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1840 ldb_get_schema_basedn(b_state->sam_ctx));
1841 getnc_state->is_schema_nc = (0 == ret);
1843 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1844 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1848 * This is the first replication cycle and it is
1849 * a good place to handle extended operations
1851 * FIXME: we don't fully support extended operations yet
1853 switch (req10->extended_op) {
1854 case DRSUAPI_EXOP_NONE:
1856 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1857 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
1858 W_ERROR_NOT_OK_RETURN(werr);
1859 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1863 case DRSUAPI_EXOP_REPL_SECRET:
1864 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1867 has_get_all_changes);
1868 r->out.result = werr;
1869 W_ERROR_NOT_OK_RETURN(werr);
1871 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1872 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1873 W_ERROR_NOT_OK_RETURN(werr);
1874 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1878 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1879 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1880 W_ERROR_NOT_OK_RETURN(werr);
1881 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1885 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1886 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1887 W_ERROR_NOT_OK_RETURN(werr);
1888 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1892 case DRSUAPI_EXOP_REPL_OBJ:
1893 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1894 r->out.result = werr;
1895 W_ERROR_NOT_OK_RETURN(werr);
1898 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1900 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1901 (unsigned)req10->extended_op));
1902 return WERR_DS_DRA_NOT_SUPPORTED;
1906 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1907 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1908 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1909 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1910 return WERR_DS_DRA_INVALID_PARAMETER;
1913 /* we need the session key for encrypting password attributes */
1914 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1915 if (!NT_STATUS_IS_OK(status)) {
1916 DEBUG(0,(__location__ ": Failed to get session key\n"));
1917 return WERR_DS_DRA_INTERNAL_ERROR;
1921 TODO: MS-DRSR section 4.1.10.1.1
1922 Work out if this is the start of a new cycle */
1924 if (getnc_state->guids == NULL) {
1925 const char *extra_filter;
1926 struct ldb_result *search_res = NULL;
1928 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1930 getnc_state->min_usn = req10->highwatermark.highest_usn;
1931 getnc_state->max_usn = getnc_state->min_usn;
1933 getnc_state->final_udv = talloc_zero(getnc_state,
1934 struct drsuapi_DsReplicaCursor2CtrEx);
1935 if (getnc_state->final_udv == NULL) {
1938 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1939 getnc_state->final_udv);
1940 if (!W_ERROR_IS_OK(werr)) {
1944 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1945 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1946 search_dn, extra_filter,
1949 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1951 search_dn, extra_filter,
1954 W_ERROR_NOT_OK_RETURN(werr);
1956 /* extract out the GUIDs list */
1957 getnc_state->num_records = search_res ? search_res->count : 0;
1958 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1959 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1961 changes = talloc_array(getnc_state,
1962 struct drsuapi_changed_objects,
1963 getnc_state->num_records);
1964 W_ERROR_HAVE_NO_MEMORY(changes);
1966 for (i=0; i<getnc_state->num_records; i++) {
1967 changes[i].dn = search_res->msgs[i]->dn;
1968 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1969 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1971 if (changes[i].usn > getnc_state->max_usn) {
1972 getnc_state->max_usn = changes[i].usn;
1976 /* RID_ALLOC returns 3 objects in a fixed order */
1977 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
1979 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1980 LDB_TYPESAFE_QSORT(changes,
1981 getnc_state->num_records,
1983 site_res_cmp_anc_order);
1985 LDB_TYPESAFE_QSORT(changes,
1986 getnc_state->num_records,
1988 site_res_cmp_usn_order);
1991 for (i=0; i < getnc_state->num_records; i++) {
1992 getnc_state->guids[i] = changes[i].guid;
1993 if (GUID_all_zero(&getnc_state->guids[i])) {
1994 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1995 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1996 return WERR_DS_DRA_INTERNAL_ERROR;
2000 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
2001 getnc_state->final_hwm.reserved_usn = 0;
2002 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
2004 talloc_free(search_res);
2005 talloc_free(changes);
2008 if (req10->uptodateness_vector) {
2009 /* make sure its sorted */
2010 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
2011 req10->uptodateness_vector->count,
2012 drsuapi_DsReplicaCursor_compare);
2015 /* Prefix mapping */
2016 schema = dsdb_get_schema(sam_ctx, mem_ctx);
2018 DEBUG(0,("No schema in sam_ctx\n"));
2019 return WERR_DS_DRA_INTERNAL_ERROR;
2022 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
2023 *r->out.ctr->ctr6.naming_context = *ncRoot;
2025 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
2026 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
2027 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
2028 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
2029 return WERR_DS_DRA_INTERNAL_ERROR;
2032 /* find the SID if there is one */
2033 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2035 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2036 r->out.ctr->ctr6.mapping_ctr = *ctr;
2038 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2039 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2041 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2042 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2044 currentObject = &r->out.ctr->ctr6.first_object;
2046 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2048 * The client control here only applies in normal replication, not extended
2049 * operations, which return a fixed set, even if the caller
2050 * sets max_object_count == 0
2052 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2053 /* use this to force single objects at a time, which is useful
2054 * for working out what object is giving problems
2056 if (req10->max_object_count < max_objects) {
2057 max_objects = req10->max_object_count;
2061 * TODO: work out how the maximum should be calculated
2063 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2066 * Maximum time that we can spend in a getncchanges
2067 * in order to avoid timeout of the other part.
2068 * 10 seconds by default.
2070 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2071 for (i=getnc_state->num_processed;
2072 i<getnc_state->num_records &&
2074 (r->out.ctr->ctr6.object_count < max_objects)
2075 && !max_wait_reached;
2078 struct drsuapi_DsReplicaObjectListItemEx *obj;
2079 struct ldb_message *msg;
2080 static const char * const msg_attrs[] = {
2082 "nTSecurityDescriptor",
2084 "replPropertyMetaData",
2085 DSDB_SECRET_ATTRIBUTES,
2087 struct ldb_result *msg_res;
2088 struct ldb_dn *msg_dn;
2090 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2091 W_ERROR_HAVE_NO_MEMORY(obj);
2093 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2094 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2097 /* by re-searching here we avoid having a lot of full
2098 * records in memory between calls to getncchanges
2100 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2102 LDB_SCOPE_BASE, msg_attrs, NULL);
2103 if (ret != LDB_SUCCESS) {
2104 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2105 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2106 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2112 msg = msg_res->msgs[0];
2114 max_wait_reached = (time(NULL) - start > max_wait);
2116 werr = get_nc_changes_build_object(obj, msg,
2117 sam_ctx, getnc_state->ncRoot_dn,
2118 getnc_state->is_schema_nc,
2119 schema, &session_key, getnc_state->min_usn,
2120 req10->replica_flags,
2121 req10->partial_attribute_set,
2122 req10->uptodateness_vector,
2125 if (!W_ERROR_IS_OK(werr)) {
2129 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2130 getnc_state->ncRoot_dn,
2131 schema, getnc_state->min_usn,
2132 req10->replica_flags,
2134 &getnc_state->la_list,
2135 &getnc_state->la_count,
2136 req10->uptodateness_vector);
2137 if (!W_ERROR_IS_OK(werr)) {
2141 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
2142 if (uSN > getnc_state->max_usn) {
2144 * Only report the max_usn we had at the start
2145 * of the replication cycle.
2147 * If this object has changed lately we better
2148 * let the destination dsa refetch the change.
2149 * This is better than the risk of loosing some
2150 * objects or linked attributes.
2154 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
2155 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
2156 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
2159 if (obj->meta_data_ctr == NULL) {
2160 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2161 ldb_dn_get_linearized(msg->dn)));
2162 /* no attributes to send */
2167 r->out.ctr->ctr6.object_count++;
2169 *currentObject = obj;
2170 currentObject = &obj->next_object;
2172 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
2174 talloc_free(getnc_state->last_dn);
2175 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
2177 talloc_free(msg_res);
2178 talloc_free(msg_dn);
2181 getnc_state->num_processed = i;
2183 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2185 /* the client can us to call UpdateRefs on its behalf to
2186 re-establish monitoring of the NC */
2187 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2188 !GUID_all_zero(&req10->destination_dsa_guid)) {
2189 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2190 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2191 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2192 ureq.naming_context = ncRoot;
2193 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2194 &req10->destination_dsa_guid);
2195 if (!ureq.dest_dsa_dns_name) {
2198 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2199 ureq.options = DRSUAPI_DRS_ADD_REF |
2200 DRSUAPI_DRS_ASYNC_OP |
2201 DRSUAPI_DRS_GETCHG_CHECK;
2203 /* we also need to pass through the
2204 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2205 to send notifies using the GC SPN */
2206 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2208 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2209 if (!W_ERROR_IS_OK(werr)) {
2210 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2211 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2218 * This is just a guess, how to calculate the
2219 * number of linked attributes to send, we need to
2220 * find out how to do this right.
2222 if (r->out.ctr->ctr6.object_count >= max_links) {
2225 max_links -= r->out.ctr->ctr6.object_count;
2228 link_total = getnc_state->la_count;
2230 if (i < getnc_state->num_records) {
2231 r->out.ctr->ctr6.more_data = true;
2233 /* sort the whole array the first time */
2234 if (!getnc_state->la_sorted) {
2235 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
2236 sam_ctx, linked_attribute_compare);
2237 getnc_state->la_sorted = true;
2240 link_count = getnc_state->la_count - getnc_state->la_idx;
2241 link_count = MIN(max_links, link_count);
2243 r->out.ctr->ctr6.linked_attributes_count = link_count;
2244 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
2246 getnc_state->la_idx += link_count;
2247 link_given = getnc_state->la_idx;
2249 if (getnc_state->la_idx < getnc_state->la_count) {
2250 r->out.ctr->ctr6.more_data = true;
2254 if (!r->out.ctr->ctr6.more_data) {
2255 talloc_steal(mem_ctx, getnc_state->la_list);
2257 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2258 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2259 &getnc_state->final_udv);
2261 talloc_free(getnc_state);
2262 b_state->getncchanges_state = NULL;
2264 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2265 &r->out.ctr->ctr6.new_highwatermark);
2268 * We need to make sure that we never return the
2269 * same highwatermark within the same replication
2270 * cycle more than once. Otherwise we cannot detect
2271 * when the client uses an unexptected highwatermark.
2273 * This is a HACK which is needed because our
2274 * object ordering is wrong and set tmp_highest_usn
2275 * to a value that is higher than what we already
2276 * sent to the client (destination dsa).
2278 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2281 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2284 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2285 r->out.ctr->ctr6.uptodateness_vector = NULL;
2286 r->out.ctr->ctr6.nc_object_count = 0;
2287 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2290 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2291 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2292 (unsigned long long)(req10->highwatermark.highest_usn+1),
2293 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2294 r->out.ctr->ctr6.object_count,
2295 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2296 r->out.ctr->ctr6.linked_attributes_count,
2297 link_given, link_total,
2298 dom_sid_string(mem_ctx, user_sid)));
2301 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2302 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);