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 /* We must keep the GUIDs in NDR form for sorting */
61 struct la_for_sorting {
62 struct drsuapi_DsReplicaLinkedAttribute *link;
63 DATA_BLOB target_guid;
64 DATA_BLOB source_guid;
67 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
68 const struct drsuapi_DsReplicaHighWaterMark *h2)
70 if (h1->highest_usn < h2->highest_usn) {
72 } else if (h1->highest_usn > h2->highest_usn) {
74 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
76 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
78 } else if (h1->reserved_usn < h2->reserved_usn) {
80 } else if (h1->reserved_usn > h2->reserved_usn) {
88 build a DsReplicaObjectIdentifier from a ldb msg
90 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
91 struct ldb_message *msg)
93 struct drsuapi_DsReplicaObjectIdentifier *identifier;
96 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
97 if (identifier == NULL) {
101 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
102 identifier->guid = samdb_result_guid(msg, "objectGUID");
104 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
106 identifier->sid = *sid;
108 ZERO_STRUCT(identifier->sid);
113 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
115 return GUID_compare(guid1, &guid2);
119 see if we can filter an attribute using the uptodateness_vector
121 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
122 const struct GUID *originating_invocation_id,
123 uint64_t originating_usn)
125 const struct drsuapi_DsReplicaCursor *c;
126 if (udv == NULL) return false;
127 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
128 originating_invocation_id, udv_compare, c);
129 if (c && originating_usn <= c->highest_usn) {
136 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
138 if (a1 == a2) return 0;
139 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
143 check if an attribute is in a partial_attribute_set
145 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
146 struct drsuapi_DsPartialAttributeSet *pas)
148 enum drsuapi_DsAttributeId *result;
149 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
151 return result != NULL;
156 drsuapi_DsGetNCChanges for one object
158 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
159 struct ldb_message *msg,
160 struct ldb_context *sam_ctx,
161 struct ldb_dn *ncRoot_dn,
163 struct dsdb_schema *schema,
164 DATA_BLOB *session_key,
165 uint64_t highest_usn,
166 uint32_t replica_flags,
167 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
168 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
169 enum drsuapi_DsExtendedOperation extended_op,
170 bool force_object_return)
172 const struct ldb_val *md_value;
174 struct replPropertyMetaDataBlob md;
176 enum ndr_err_code ndr_err;
179 const struct dsdb_attribute *rdn_sa;
180 unsigned int instanceType;
181 struct dsdb_syntax_ctx syntax_ctx;
183 /* make dsdb sytanx context for conversions */
184 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
185 syntax_ctx.is_schema_nc = is_schema_nc;
187 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
188 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
189 obj->is_nc_prefix = true;
190 obj->parent_object_guid = NULL;
192 obj->is_nc_prefix = false;
193 obj->parent_object_guid = talloc(obj, struct GUID);
194 if (obj->parent_object_guid == NULL) {
195 return WERR_DS_DRA_INTERNAL_ERROR;
197 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
198 if (GUID_all_zero(obj->parent_object_guid)) {
199 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
200 ldb_dn_get_linearized(msg->dn)));
201 return WERR_DS_DRA_INTERNAL_ERROR;
204 obj->next_object = NULL;
206 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
208 /* nothing to send */
212 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
213 /* don't send uninstantiated objects */
217 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
218 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
219 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
220 return WERR_DS_DRA_INTERNAL_ERROR;
223 if (md.version != 1) {
224 return WERR_DS_DRA_INTERNAL_ERROR;
227 rdn = ldb_dn_get_rdn_name(msg->dn);
229 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
230 return WERR_DS_DRA_INTERNAL_ERROR;
233 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
234 if (rdn_sa == NULL) {
235 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
236 rdn, ldb_dn_get_linearized(msg->dn)));
237 return WERR_DS_DRA_INTERNAL_ERROR;
240 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
241 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
243 obj->object.identifier = get_object_identifier(obj, msg);
244 if (obj->object.identifier == NULL) {
247 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
249 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
250 for (n=i=0; i<md.ctr.ctr1.count; i++) {
251 const struct dsdb_attribute *sa;
252 bool force_attribute = false;
254 /* if the attribute has not changed, and it is not the
255 instanceType then don't include it */
256 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
257 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
258 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
260 /* don't include the rDN */
261 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
263 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
265 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
266 (unsigned int)md.ctr.ctr1.array[i].attid,
267 ldb_dn_get_linearized(msg->dn)));
268 return WERR_DS_DRA_INTERNAL_ERROR;
272 struct ldb_message_element *el;
273 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
274 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
275 /* don't send upgraded links inline */
280 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
281 !dsdb_attr_in_rodc_fas(sa)) {
282 force_attribute = true;
283 DEBUG(4,("Forcing attribute %s in %s\n",
284 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
287 /* filter by uptodateness_vector */
288 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
290 udv_filter(uptodateness_vector,
291 &md.ctr.ctr1.array[i].originating_invocation_id,
292 md.ctr.ctr1.array[i].originating_usn)) {
296 /* filter by partial_attribute_set */
297 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
301 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
302 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
303 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
304 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
305 attids[n] = md.ctr.ctr1.array[i].attid;
309 /* ignore it if its an empty change. Note that renames always
310 * change the 'name' attribute, so they won't be ignored by
313 * the force_object_return check is used to force an empty
314 * object return when we timeout in the getncchanges loop.
315 * This allows us to return an empty object, which keeps the
316 * client happy while preventing timeouts
320 attids[0] == DRSUAPI_ATTID_instanceType &&
321 !force_object_return)) {
322 talloc_free(obj->meta_data_ctr);
323 obj->meta_data_ctr = NULL;
327 obj->meta_data_ctr->count = n;
329 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
330 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
331 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
332 obj->object.attribute_ctr.num_attributes);
333 if (obj->object.attribute_ctr.attributes == NULL) {
338 * Note that the meta_data array and the attributes array must
339 * be the same size and in the same order
341 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
342 struct ldb_message_element *el;
344 const struct dsdb_attribute *sa;
346 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
348 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
349 return WERR_DS_DRA_INTERNAL_ERROR;
352 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
354 /* this happens for attributes that have been removed */
355 DEBUG(5,("No element '%s' for attributeID %u in message\n",
356 sa->lDAPDisplayName, attids[i]));
357 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
358 obj->object.attribute_ctr.attributes[i].attid =
359 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
361 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
362 &obj->object.attribute_ctr.attributes[i]);
363 if (!W_ERROR_IS_OK(werr)) {
364 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
365 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
369 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
370 * check if attribute is secret and send a null value
372 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
373 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
374 &obj->meta_data_ctr->meta_data[i]);
376 /* some attributes needs to be encrypted
378 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
379 &obj->object.attribute_ctr.attributes[i]);
380 if (!W_ERROR_IS_OK(werr)) {
381 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
382 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
387 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
388 DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID: "
392 ldb_dn_get_linearized(msg->dn),
394 obj->object.attribute_ctr.attributes[i].attid));
395 return WERR_DS_DATABASE_ERROR;
403 add one linked attribute from an object to the list of linked
404 attributes in a getncchanges request
406 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
407 struct ldb_context *sam_ctx,
408 const struct dsdb_schema *schema,
409 const struct dsdb_attribute *sa,
410 struct ldb_message *msg,
411 struct dsdb_dn *dsdb_dn,
412 struct drsuapi_DsReplicaLinkedAttribute **la_list,
415 struct drsuapi_DsReplicaLinkedAttribute *la;
420 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
421 W_ERROR_HAVE_NO_MEMORY(*la_list);
423 la = &(*la_list)[*la_count];
425 la->identifier = get_object_identifier(*la_list, msg);
426 W_ERROR_HAVE_NO_MEMORY(la->identifier);
428 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
431 /* We have to check that the inactive link still point to an existing object */
437 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
438 if (strncmp(v, "TRUE", 4) == 0) {
440 * Note: we skip the transmition of the deleted link even if the other part used to
441 * know about it because when we transmit the deletion of the object, the link will
442 * be deleted too due to deletion of object where link points and Windows do so.
444 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
445 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
447 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
448 * if it join an existing domain with deleted objets, it firsts impose to have a
449 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
450 * either during initial replication or after the getNCChanges.
451 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
453 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
454 * If FL >=2K8R2 we are sure that this attribute will be here.
455 * For this kind of forest level we do not return the link if the object is recycled
456 * (isRecycled = true).
458 if (strncmp(v, "TRUE", 4) == 0) {
459 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
460 ldb_dn_get_linearized(msg->dn)));
467 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
468 if (!NT_STATUS_IS_OK(status)) {
469 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
470 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
471 return ntstatus_to_werror(status);
473 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
474 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
475 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
476 GUID_string(mem_ctx, &guid)));
478 } else if (ret != LDB_SUCCESS) {
479 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
480 GUID_string(mem_ctx, &guid),
485 la->attid = sa->attributeID_id;
486 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
488 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
489 if (!NT_STATUS_IS_OK(status)) {
490 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
491 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
492 return ntstatus_to_werror(status);
494 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
495 if (!NT_STATUS_IS_OK(status)) {
496 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
497 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
498 return ntstatus_to_werror(status);
500 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
501 if (!NT_STATUS_IS_OK(status)) {
502 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
503 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
504 return ntstatus_to_werror(status);
506 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
507 if (!NT_STATUS_IS_OK(status)) {
508 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
509 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
510 return ntstatus_to_werror(status);
513 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
514 if (!NT_STATUS_IS_OK(status)) {
515 /* this is possible for upgraded links */
516 la->originating_add_time = la->meta_data.originating_change_time;
519 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
520 W_ERROR_NOT_OK_RETURN(werr);
528 add linked attributes from an object to the list of linked
529 attributes in a getncchanges request
531 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
533 struct ldb_dn *ncRoot_dn,
534 struct dsdb_schema *schema,
535 uint64_t highest_usn,
536 uint32_t replica_flags,
537 struct ldb_message *msg,
538 struct drsuapi_DsReplicaLinkedAttribute **la_list,
540 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
543 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
544 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
546 for (i=0; i<msg->num_elements; i++) {
547 struct ldb_message_element *el = &msg->elements[i];
548 const struct dsdb_attribute *sa;
551 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
553 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
554 /* we only want forward links */
558 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
559 /* its an old style link, it will have been
560 * sent in the main replication data */
564 for (j=0; j<el->num_values; j++) {
565 struct dsdb_dn *dsdb_dn;
570 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
571 if (dsdb_dn == NULL) {
572 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
573 el->name, ldb_dn_get_linearized(msg->dn)));
574 talloc_free(tmp_ctx);
575 return WERR_DS_DRA_INTERNAL_ERROR;
578 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
579 if (!NT_STATUS_IS_OK(status)) {
580 /* this can happen for attributes
581 given to us with old style meta
586 if (local_usn > uSNChanged) {
587 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
588 el->name, ldb_dn_get_linearized(msg->dn)));
589 talloc_free(tmp_ctx);
590 return WERR_DS_DRA_INTERNAL_ERROR;
593 if (local_usn < highest_usn) {
597 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
598 dsdb_dn, la_list, la_count);
599 if (!W_ERROR_IS_OK(werr)) {
600 talloc_free(tmp_ctx);
606 talloc_free(tmp_ctx);
611 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
613 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
614 struct ldb_dn *ncRoot_dn,
615 struct drsuapi_DsReplicaCursor2CtrEx *udv)
623 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
624 if (ret != LDB_SUCCESS) {
625 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
626 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
627 return WERR_DS_DRA_INTERNAL_ERROR;
634 /* comparison function for linked attributes - see CompareLinks() in
635 * MS-DRSR section 4.1.10.5.17 */
636 static int linked_attribute_compare(const struct la_for_sorting *la1,
637 const struct la_for_sorting *la2,
641 c = data_blob_cmp(&la1->source_guid,
647 if (la1->link->attid != la2->link->attid) {
648 return la1->link->attid < la2->link->attid? -1:1;
651 if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
652 (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
653 return (la1->link->flags &
654 DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
657 return data_blob_cmp(&la1->target_guid, &la2->target_guid);
660 struct drsuapi_changed_objects {
667 sort the objects we send by tree order
669 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
670 struct drsuapi_changed_objects *m2,
671 struct drsuapi_getncchanges_state *getnc_state)
673 return ldb_dn_compare(m2->dn, m1->dn);
677 sort the objects we send first by uSNChanged
679 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
680 struct drsuapi_changed_objects *m2,
681 struct drsuapi_getncchanges_state *getnc_state)
685 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
690 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
695 if (m1->usn == m2->usn) {
696 return ldb_dn_compare(m2->dn, m1->dn);
699 if (m1->usn < m2->usn) {
708 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
710 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
712 struct drsuapi_DsGetNCChangesRequest10 *req10,
713 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
714 struct ldb_dn **rid_manager_dn)
716 struct ldb_dn *req_dn, *ntds_dn = NULL;
718 struct ldb_context *ldb = b_state->sam_ctx;
719 struct ldb_result *ext_res;
720 struct dsdb_fsmo_extended_op *exop;
725 - verify that the DN being asked for is the RID Manager DN
726 - verify that we are the RID Manager
729 /* work out who is the RID Manager, also return to caller */
730 ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
731 if (ret != LDB_SUCCESS) {
732 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
733 return WERR_DS_DRA_INTERNAL_ERROR;
736 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
737 if (!ldb_dn_validate(req_dn) ||
738 ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
739 /* that isn't the RID Manager DN */
740 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
741 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
742 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
746 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
747 ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
748 if (ret != LDB_SUCCESS) {
749 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
750 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
751 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
755 /* find the DN of the RID Manager */
756 ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
757 if (ret != LDB_SUCCESS) {
758 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
759 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
760 return WERR_DS_DRA_INTERNAL_ERROR;
764 /* we're not the RID Manager - go away */
765 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
766 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
770 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
771 W_ERROR_HAVE_NO_MEMORY(exop);
773 exop->fsmo_info = req10->fsmo_info;
774 exop->destination_dsa_guid = req10->destination_dsa_guid;
776 ret = ldb_transaction_start(ldb);
777 if (ret != LDB_SUCCESS) {
778 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
779 ldb_errstring(ldb)));
780 return WERR_DS_DRA_INTERNAL_ERROR;
783 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
784 if (ret != LDB_SUCCESS) {
785 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
786 ldb_errstring(ldb)));
787 ldb_transaction_cancel(ldb);
788 return WERR_DS_DRA_INTERNAL_ERROR;
791 ret = ldb_transaction_commit(ldb);
792 if (ret != LDB_SUCCESS) {
793 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
794 ldb_errstring(ldb)));
795 return WERR_DS_DRA_INTERNAL_ERROR;
798 talloc_free(ext_res);
800 DEBUG(2,("Allocated RID pool for server %s\n",
801 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
803 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
809 return an array of SIDs from a ldb_message given an attribute name
810 assumes the SIDs are in extended DN format
812 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
813 struct ldb_message *msg,
816 const struct dom_sid ***sids)
818 struct ldb_message_element *el;
821 el = ldb_msg_find_element(msg, attr);
827 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
828 W_ERROR_HAVE_NO_MEMORY(*sids);
830 for (i=0; i<el->num_values; i++) {
831 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
835 sid = talloc(*sids, struct dom_sid);
836 W_ERROR_HAVE_NO_MEMORY(sid);
837 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
838 if (!NT_STATUS_IS_OK(status)) {
839 return WERR_INTERNAL_DB_CORRUPTION;
850 return an array of SIDs from a ldb_message given an attribute name
851 assumes the SIDs are in NDR form
853 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
854 struct ldb_message *msg,
857 const struct dom_sid ***sids)
859 struct ldb_message_element *el;
862 el = ldb_msg_find_element(msg, attr);
868 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
869 W_ERROR_HAVE_NO_MEMORY(*sids);
871 for (i=0; i<el->num_values; i++) {
872 enum ndr_err_code ndr_err;
875 sid = talloc(*sids, struct dom_sid);
876 W_ERROR_HAVE_NO_MEMORY(sid);
878 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
879 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
880 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
881 return WERR_INTERNAL_DB_CORRUPTION;
891 see if any SIDs in list1 are in list2
893 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
896 /* do we ever have enough SIDs here to worry about O(n^2) ? */
897 for (i=0; list1[i]; i++) {
898 for (j=0; list2[j]; j++) {
899 if (dom_sid_equal(list1[i], list2[j])) {
908 handle a DRSUAPI_EXOP_REPL_SECRET call
910 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
912 struct drsuapi_DsGetNCChangesRequest10 *req10,
913 struct dom_sid *user_sid,
914 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
915 bool has_get_all_changes)
917 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
918 struct ldb_dn *obj_dn = NULL;
919 struct ldb_dn *rodc_dn, *krbtgt_link_dn;
921 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
922 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
923 struct ldb_result *rodc_res, *obj_res;
924 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
927 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
928 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
931 * we need to work out if we will allow this DC to
932 * replicate the secrets for this object
934 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
938 if (b_state->sam_ctx_system == NULL) {
939 /* this operation needs system level access */
940 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
941 return WERR_DS_DRA_SOURCE_DISABLED;
945 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
947 * The pseudo code indicate
948 * revealsecrets = true
949 * if IsRevealSecretRequest(msgIn) then
950 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
952 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
953 * <... check if this account is ok to be replicated on this DC ...>
954 * <... and if not reveal secrets = no ...>
956 * reveal secrets = false
961 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
962 * then you can do EXOP_REPL_SECRETS
964 if (has_get_all_changes) {
968 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
969 if (!ldb_dn_validate(obj_dn)) goto failed;
971 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
972 dom_sid_string(mem_ctx, user_sid));
973 if (!ldb_dn_validate(rodc_dn)) goto failed;
975 /* do the two searches we need */
976 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
977 DSDB_SEARCH_SHOW_EXTENDED_DN);
978 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
980 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
981 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
983 /* if the object SID is equal to the user_sid, allow */
984 if (dom_sid_equal(user_sid,
985 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
989 /* an RODC is allowed to get its own krbtgt account secrets */
990 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
991 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
992 if (krbtgt_link_dn != NULL &&
993 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
997 /* but it isn't allowed to get anyone elses krbtgt secrets */
998 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
999 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1003 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1004 "userAccountControl", 0) &
1005 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1009 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1010 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1011 if (!W_ERROR_IS_OK(werr)) {
1015 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1016 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1017 if (!W_ERROR_IS_OK(werr)) {
1021 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1022 mem_ctx, "tokenGroups", &token_sids);
1023 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1027 if (never_reveal_sids &&
1028 sid_list_match(token_sids, never_reveal_sids)) {
1033 sid_list_match(token_sids, reveal_sids)) {
1039 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1040 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1041 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1042 return WERR_DS_DRA_ACCESS_DENIED;
1045 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1046 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1047 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1048 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1049 req10->highwatermark.highest_usn = 0;
1053 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1054 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1055 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1056 return WERR_DS_DRA_BAD_DN;
1061 handle a DRSUAPI_EXOP_REPL_OBJ call
1063 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1064 TALLOC_CTX *mem_ctx,
1065 struct drsuapi_DsGetNCChangesRequest10 *req10,
1066 struct dom_sid *user_sid,
1067 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1069 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1071 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1072 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1074 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1080 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1081 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1082 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1084 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1085 TALLOC_CTX *mem_ctx,
1086 struct drsuapi_DsGetNCChangesRequest10 *req10,
1087 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1089 struct ldb_dn *req_dn, *ntds_dn;
1092 struct ldb_context *ldb = b_state->sam_ctx;
1093 struct ldb_message *msg;
1098 - verify that the client dn exists
1099 - verify that we are the current master
1102 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1103 if (!ldb_dn_validate(req_dn)) {
1104 /* that is not a valid dn */
1105 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1106 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1107 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1111 /* find the DN of the current role owner */
1112 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1113 if (ret != LDB_SUCCESS) {
1114 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1115 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1116 return WERR_DS_DRA_INTERNAL_ERROR;
1120 /* we're not the RID Manager or role owner - go away */
1121 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1122 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1126 /* change the current master */
1127 msg = ldb_msg_new(ldb);
1128 W_ERROR_HAVE_NO_MEMORY(msg);
1129 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1130 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1132 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1133 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1134 if (ret != LDB_SUCCESS) {
1135 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1136 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1138 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1142 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1145 return WERR_DS_DRA_INTERNAL_ERROR;
1148 for (i=0;i<msg->num_elements;i++) {
1149 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1152 ret = ldb_transaction_start(ldb);
1153 if (ret != LDB_SUCCESS) {
1154 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1155 ldb_errstring(ldb)));
1156 return WERR_DS_DRA_INTERNAL_ERROR;
1159 ret = ldb_modify(ldb, msg);
1160 if (ret != LDB_SUCCESS) {
1161 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1162 ldb_errstring(ldb)));
1163 ldb_transaction_cancel(ldb);
1164 return WERR_DS_DRA_INTERNAL_ERROR;
1167 ret = ldb_transaction_commit(ldb);
1168 if (ret != LDB_SUCCESS) {
1169 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1170 ldb_errstring(ldb)));
1171 return WERR_DS_DRA_INTERNAL_ERROR;
1174 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1180 see if this getncchanges request includes a request to reveal secret information
1182 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1183 struct drsuapi_DsGetNCChangesRequest10 *req10,
1184 bool *is_secret_request)
1186 enum drsuapi_DsExtendedOperation exop;
1188 struct dsdb_schema *schema;
1190 *is_secret_request = true;
1192 exop = req10->extended_op;
1195 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1196 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1197 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1198 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1199 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1200 /* FSMO exops can reveal secrets */
1201 *is_secret_request = true;
1203 case DRSUAPI_EXOP_REPL_SECRET:
1204 case DRSUAPI_EXOP_REPL_OBJ:
1205 case DRSUAPI_EXOP_NONE:
1209 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1210 *is_secret_request = false;
1214 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1215 req10->partial_attribute_set == NULL) {
1216 /* they want secrets */
1217 *is_secret_request = true;
1221 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1223 /* check the attributes they asked for */
1224 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1225 const struct dsdb_attribute *sa;
1226 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1228 return WERR_DS_DRA_SCHEMA_MISMATCH;
1230 if (!dsdb_attr_in_rodc_fas(sa)) {
1231 *is_secret_request = true;
1236 if (req10->partial_attribute_set_ex) {
1237 /* check the extended attributes they asked for */
1238 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1239 const struct dsdb_attribute *sa;
1240 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1242 return WERR_DS_DRA_SCHEMA_MISMATCH;
1244 if (!dsdb_attr_in_rodc_fas(sa)) {
1245 *is_secret_request = true;
1251 *is_secret_request = false;
1256 see if this getncchanges request is only for attributes in the GC
1257 partial attribute set
1259 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1260 struct drsuapi_DsGetNCChangesRequest10 *req10,
1261 bool *is_gc_pas_request)
1263 enum drsuapi_DsExtendedOperation exop;
1265 struct dsdb_schema *schema;
1267 exop = req10->extended_op;
1270 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1271 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1272 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1273 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1274 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1275 case DRSUAPI_EXOP_REPL_SECRET:
1276 *is_gc_pas_request = false;
1278 case DRSUAPI_EXOP_REPL_OBJ:
1279 case DRSUAPI_EXOP_NONE:
1283 if (req10->partial_attribute_set == NULL) {
1284 /* they want it all */
1285 *is_gc_pas_request = false;
1289 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1291 /* check the attributes they asked for */
1292 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1293 const struct dsdb_attribute *sa;
1294 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1296 return WERR_DS_DRA_SCHEMA_MISMATCH;
1298 if (!sa->isMemberOfPartialAttributeSet) {
1299 *is_gc_pas_request = false;
1304 if (req10->partial_attribute_set_ex) {
1305 /* check the extended attributes they asked for */
1306 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1307 const struct dsdb_attribute *sa;
1308 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1310 return WERR_DS_DRA_SCHEMA_MISMATCH;
1312 if (!sa->isMemberOfPartialAttributeSet) {
1313 *is_gc_pas_request = false;
1319 *is_gc_pas_request = true;
1325 map from req8 to req10
1327 static struct drsuapi_DsGetNCChangesRequest10 *
1328 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1329 struct drsuapi_DsGetNCChangesRequest8 *req8)
1331 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1332 struct drsuapi_DsGetNCChangesRequest10);
1333 if (req10 == NULL) {
1337 req10->destination_dsa_guid = req8->destination_dsa_guid;
1338 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1339 req10->naming_context = req8->naming_context;
1340 req10->highwatermark = req8->highwatermark;
1341 req10->uptodateness_vector = req8->uptodateness_vector;
1342 req10->replica_flags = req8->replica_flags;
1343 req10->max_object_count = req8->max_object_count;
1344 req10->max_ndr_size = req8->max_ndr_size;
1345 req10->extended_op = req8->extended_op;
1346 req10->fsmo_info = req8->fsmo_info;
1347 req10->partial_attribute_set = req8->partial_attribute_set;
1348 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1349 req10->mapping_ctr = req8->mapping_ctr;
1354 static const char *collect_objects_attrs[] = { "uSNChanged",
1359 * Collects object for normal replication cycle.
1361 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1362 TALLOC_CTX *mem_ctx,
1363 struct drsuapi_DsGetNCChangesRequest10 *req10,
1364 struct ldb_dn *search_dn,
1365 const char *extra_filter,
1366 struct ldb_result **search_res)
1369 char* search_filter;
1370 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1371 //const char *extra_filter;
1372 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1374 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1375 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1376 scope = LDB_SCOPE_BASE;
1379 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1381 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1383 /* Construct response. */
1384 search_filter = talloc_asprintf(mem_ctx,
1385 "(uSNChanged>=%llu)",
1386 (unsigned long long)(getnc_state->min_usn+1));
1389 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1392 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1393 search_filter = talloc_asprintf(mem_ctx,
1394 "(&%s(isCriticalSystemObject=TRUE))",
1398 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1399 scope = LDB_SCOPE_BASE;
1403 search_dn = getnc_state->ncRoot_dn;
1406 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1407 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1408 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1410 collect_objects_attrs,
1412 if (ret != LDB_SUCCESS) {
1413 return WERR_DS_DRA_INTERNAL_ERROR;
1420 * Collects object for normal replication cycle.
1422 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1423 TALLOC_CTX *mem_ctx,
1424 struct drsuapi_DsGetNCChangesRequest10 *req10,
1425 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1426 struct ldb_dn *search_dn,
1427 const char *extra_filter,
1428 struct ldb_result **search_res)
1430 /* we have nothing to do in case of ex-op failure */
1431 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1435 switch (req10->extended_op) {
1436 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1439 struct ldb_dn *ntds_dn = NULL;
1440 struct ldb_dn *server_dn = NULL;
1441 struct ldb_dn *machine_dn = NULL;
1442 struct ldb_dn *rid_set_dn = NULL;
1443 struct ldb_result *search_res2 = NULL;
1444 struct ldb_result *search_res3 = NULL;
1445 TALLOC_CTX *frame = talloc_stackframe();
1446 /* get RID manager, RID set and server DN (in that order) */
1448 /* This first search will get the RID Manager */
1449 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1451 search_dn, LDB_SCOPE_BASE,
1452 collect_objects_attrs,
1454 if (ret != LDB_SUCCESS) {
1455 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1456 ldb_dn_get_linearized(search_dn),
1457 ldb_errstring(b_state->sam_ctx)));
1459 return WERR_DS_DRA_INTERNAL_ERROR;
1462 if ((*search_res)->count != 1) {
1463 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1464 ldb_dn_get_linearized(search_dn),
1465 (*search_res)->count));
1467 return WERR_DS_DRA_INTERNAL_ERROR;
1470 /* Now extend it to the RID set */
1472 /* Find the computer account DN for the destination
1473 * dsa GUID specified */
1475 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1476 &req10->destination_dsa_guid, 0,
1478 if (ret != LDB_SUCCESS) {
1479 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1481 &req10->destination_dsa_guid),
1482 ldb_errstring(b_state->sam_ctx)));
1484 return WERR_DS_DRA_INTERNAL_ERROR;
1487 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1490 return WERR_DS_DRA_INTERNAL_ERROR;
1493 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1494 "serverReference", &machine_dn);
1495 if (ret != LDB_SUCCESS) {
1496 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1497 ldb_dn_get_linearized(server_dn),
1498 ldb_errstring(b_state->sam_ctx)));
1500 return WERR_DS_DRA_INTERNAL_ERROR;
1503 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1504 "rIDSetReferences", &rid_set_dn);
1505 if (ret != LDB_SUCCESS) {
1506 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1507 ldb_dn_get_linearized(server_dn),
1508 ldb_errstring(b_state->sam_ctx)));
1510 return WERR_DS_DRA_INTERNAL_ERROR;
1514 /* This first search will get the RID Manager, now get the RID set */
1515 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1517 rid_set_dn, LDB_SCOPE_BASE,
1518 collect_objects_attrs,
1520 if (ret != LDB_SUCCESS) {
1521 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1522 ldb_dn_get_linearized(rid_set_dn),
1523 ldb_errstring(b_state->sam_ctx)));
1525 return WERR_DS_DRA_INTERNAL_ERROR;
1528 if (search_res2->count != 1) {
1529 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1530 ldb_dn_get_linearized(rid_set_dn),
1531 search_res2->count));
1533 return WERR_DS_DRA_INTERNAL_ERROR;
1536 /* Finally get the server DN */
1537 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1539 machine_dn, LDB_SCOPE_BASE,
1540 collect_objects_attrs,
1542 if (ret != LDB_SUCCESS) {
1543 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1544 ldb_dn_get_linearized(server_dn),
1545 ldb_errstring(b_state->sam_ctx)));
1547 return WERR_DS_DRA_INTERNAL_ERROR;
1550 if (search_res3->count != 1) {
1551 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1552 ldb_dn_get_linearized(server_dn),
1553 search_res3->count));
1555 return WERR_DS_DRA_INTERNAL_ERROR;
1558 /* Now extend the original search_res with these answers */
1559 (*search_res)->count = 3;
1561 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1562 struct ldb_message *,
1563 (*search_res)->count);
1564 if ((*search_res)->msgs == NULL) {
1570 talloc_steal(mem_ctx, *search_res);
1571 (*search_res)->msgs[1] =
1572 talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1573 (*search_res)->msgs[2] =
1574 talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1580 /* TODO: implement extended op specific collection
1581 * of objects. Right now we just normal procedure
1582 * for collecting objects */
1583 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1588 drsuapi_DsGetNCChanges
1590 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1592 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1593 struct drsuapi_DsGetNCChanges *r)
1595 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1598 struct dsdb_schema *schema;
1599 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1600 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1602 DATA_BLOB session_key;
1604 struct dcesrv_handle *h;
1605 struct drsuapi_bind_state *b_state;
1606 struct drsuapi_getncchanges_state *getnc_state;
1607 struct drsuapi_DsGetNCChangesRequest10 *req10;
1609 uint32_t max_objects;
1611 uint32_t link_count = 0;
1612 uint32_t link_total = 0;
1613 uint32_t link_given = 0;
1614 struct ldb_dn *search_dn = NULL;
1615 bool am_rodc, null_scope=false;
1616 enum security_user_level security_level;
1617 struct ldb_context *sam_ctx;
1618 struct dom_sid *user_sid;
1619 bool is_secret_request;
1620 bool is_gc_pas_request;
1621 struct drsuapi_changed_objects *changes;
1623 time_t start = time(NULL);
1624 bool max_wait_reached = false;
1625 bool has_get_all_changes = false;
1626 struct GUID invocation_id;
1627 static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
1629 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1632 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1634 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1636 *r->out.level_out = 6;
1637 /* TODO: linked attributes*/
1638 r->out.ctr->ctr6.linked_attributes_count = 0;
1639 r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
1641 r->out.ctr->ctr6.object_count = 0;
1642 r->out.ctr->ctr6.nc_object_count = 0;
1643 r->out.ctr->ctr6.more_data = false;
1644 r->out.ctr->ctr6.uptodateness_vector = NULL;
1645 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1646 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1647 r->out.ctr->ctr6.first_object = NULL;
1649 /* a RODC doesn't allow for any replication */
1650 ret = samdb_rodc(sam_ctx, &am_rodc);
1651 if (ret == LDB_SUCCESS && am_rodc) {
1652 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1653 return WERR_DS_DRA_SOURCE_DISABLED;
1656 /* Check request revision.
1658 switch (r->in.level) {
1660 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1661 if (req10 == NULL) {
1666 req10 = &r->in.req->req10;
1669 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1671 return WERR_REVISION_MISMATCH;
1675 /* Perform access checks. */
1676 /* TODO: we need to support a sync on a specific non-root
1677 * DN. We'll need to find the real partition root here */
1678 ncRoot = req10->naming_context;
1679 if (ncRoot == NULL) {
1680 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1681 return WERR_DS_DRA_INVALID_PARAMETER;
1684 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1685 return WERR_DS_DRA_INTERNAL_ERROR;
1688 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1689 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1690 return WERR_DS_DRA_SOURCE_DISABLED;
1693 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1695 /* all clients must have GUID_DRS_GET_CHANGES */
1696 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1698 dce_call->conn->auth_state.session_info->security_token,
1699 req10->naming_context,
1700 GUID_DRS_GET_CHANGES);
1701 if (!W_ERROR_IS_OK(werr)) {
1705 /* allowed if the GC PAS and client has
1706 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1707 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1708 if (!W_ERROR_IS_OK(werr)) {
1711 if (is_gc_pas_request) {
1712 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1714 dce_call->conn->auth_state.session_info->security_token,
1715 req10->naming_context,
1716 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1717 if (W_ERROR_IS_OK(werr)) {
1722 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1723 if (!W_ERROR_IS_OK(werr)) {
1726 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1727 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1729 dce_call->conn->auth_state.session_info->security_token,
1730 req10->naming_context,
1731 GUID_DRS_GET_ALL_CHANGES);
1732 if (!W_ERROR_IS_OK(werr)) {
1735 has_get_all_changes = true;
1740 /* for non-administrator replications, check that they have
1741 given the correct source_dsa_invocation_id */
1742 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1743 samdb_domain_sid(sam_ctx));
1744 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1745 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1746 /* we rely on this flag being unset for RODC requests */
1747 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1751 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1752 /* Ignore the _in_ uptpdateness vector*/
1753 req10->uptodateness_vector = NULL;
1756 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1757 req10->source_dsa_invocation_id = invocation_id;
1760 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1762 * The given highwatermark is only valid relative to the
1763 * specified source_dsa_invocation_id.
1765 ZERO_STRUCT(req10->highwatermark);
1768 getnc_state = b_state->getncchanges_state;
1770 /* see if a previous replication has been abandoned */
1772 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1773 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1774 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1775 ldb_dn_get_linearized(new_dn),
1776 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1777 ldb_dn_get_linearized(getnc_state->last_dn)));
1778 talloc_free(getnc_state);
1784 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1785 &req10->highwatermark);
1787 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1788 "on DN %s %s highwatermark (last_dn %s)\n",
1789 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1790 (ret > 0) ? "older" : "newer",
1791 ldb_dn_get_linearized(getnc_state->last_dn)));
1792 talloc_free(getnc_state);
1797 if (getnc_state == NULL) {
1798 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1799 if (getnc_state == NULL) {
1802 b_state->getncchanges_state = getnc_state;
1803 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1805 /* find out if we are to replicate Schema NC */
1806 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1807 ldb_get_schema_basedn(b_state->sam_ctx));
1808 getnc_state->is_schema_nc = (0 == ret);
1810 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1811 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1815 * This is the first replication cycle and it is
1816 * a good place to handle extended operations
1818 * FIXME: we don't fully support extended operations yet
1820 switch (req10->extended_op) {
1821 case DRSUAPI_EXOP_NONE:
1823 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1824 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
1825 W_ERROR_NOT_OK_RETURN(werr);
1826 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1830 case DRSUAPI_EXOP_REPL_SECRET:
1831 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1834 has_get_all_changes);
1835 r->out.result = werr;
1836 W_ERROR_NOT_OK_RETURN(werr);
1838 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1839 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1840 W_ERROR_NOT_OK_RETURN(werr);
1841 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1845 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1846 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1847 W_ERROR_NOT_OK_RETURN(werr);
1848 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1852 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1853 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1854 W_ERROR_NOT_OK_RETURN(werr);
1855 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1859 case DRSUAPI_EXOP_REPL_OBJ:
1860 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1861 r->out.result = werr;
1862 W_ERROR_NOT_OK_RETURN(werr);
1865 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1867 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1868 (unsigned)req10->extended_op));
1869 return WERR_DS_DRA_NOT_SUPPORTED;
1873 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1874 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1875 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1876 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1877 return WERR_DS_DRA_INVALID_PARAMETER;
1880 /* we need the session key for encrypting password attributes */
1881 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1882 if (!NT_STATUS_IS_OK(status)) {
1883 DEBUG(0,(__location__ ": Failed to get session key\n"));
1884 return WERR_DS_DRA_INTERNAL_ERROR;
1888 TODO: MS-DRSR section 4.1.10.1.1
1889 Work out if this is the start of a new cycle */
1891 if (getnc_state->guids == NULL) {
1892 const char *extra_filter;
1893 struct ldb_result *search_res = NULL;
1895 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1897 getnc_state->min_usn = req10->highwatermark.highest_usn;
1898 getnc_state->max_usn = getnc_state->min_usn;
1900 getnc_state->final_udv = talloc_zero(getnc_state,
1901 struct drsuapi_DsReplicaCursor2CtrEx);
1902 if (getnc_state->final_udv == NULL) {
1905 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1906 getnc_state->final_udv);
1907 if (!W_ERROR_IS_OK(werr)) {
1911 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1912 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1913 search_dn, extra_filter,
1916 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1918 search_dn, extra_filter,
1921 W_ERROR_NOT_OK_RETURN(werr);
1923 /* extract out the GUIDs list */
1924 getnc_state->num_records = search_res ? search_res->count : 0;
1925 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1926 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1928 changes = talloc_array(getnc_state,
1929 struct drsuapi_changed_objects,
1930 getnc_state->num_records);
1931 W_ERROR_HAVE_NO_MEMORY(changes);
1933 for (i=0; i<getnc_state->num_records; i++) {
1934 changes[i].dn = search_res->msgs[i]->dn;
1935 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1936 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1938 if (changes[i].usn > getnc_state->max_usn) {
1939 getnc_state->max_usn = changes[i].usn;
1943 /* RID_ALLOC returns 3 objects in a fixed order */
1944 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
1946 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1947 LDB_TYPESAFE_QSORT(changes,
1948 getnc_state->num_records,
1950 site_res_cmp_anc_order);
1952 LDB_TYPESAFE_QSORT(changes,
1953 getnc_state->num_records,
1955 site_res_cmp_usn_order);
1958 for (i=0; i < getnc_state->num_records; i++) {
1959 getnc_state->guids[i] = changes[i].guid;
1960 if (GUID_all_zero(&getnc_state->guids[i])) {
1961 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1962 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1963 return WERR_DS_DRA_INTERNAL_ERROR;
1967 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
1968 getnc_state->final_hwm.reserved_usn = 0;
1969 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
1971 talloc_free(search_res);
1972 talloc_free(changes);
1975 if (req10->uptodateness_vector) {
1976 /* make sure its sorted */
1977 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
1978 req10->uptodateness_vector->count,
1979 drsuapi_DsReplicaCursor_compare);
1982 /* Prefix mapping */
1983 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1985 DEBUG(0,("No schema in sam_ctx\n"));
1986 return WERR_DS_DRA_INTERNAL_ERROR;
1989 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1990 *r->out.ctr->ctr6.naming_context = *ncRoot;
1992 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1993 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1994 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1995 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1996 return WERR_DS_DRA_INTERNAL_ERROR;
1999 /* find the SID if there is one */
2000 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2002 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2003 r->out.ctr->ctr6.mapping_ctr = *ctr;
2005 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2006 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2008 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2009 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2011 currentObject = &r->out.ctr->ctr6.first_object;
2013 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2015 * The client control here only applies in normal replication, not extended
2016 * operations, which return a fixed set, even if the caller
2017 * sets max_object_count == 0
2019 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2020 /* use this to force single objects at a time, which is useful
2021 * for working out what object is giving problems
2023 if (req10->max_object_count < max_objects) {
2024 max_objects = req10->max_object_count;
2028 * TODO: work out how the maximum should be calculated
2030 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2033 * Maximum time that we can spend in a getncchanges
2034 * in order to avoid timeout of the other part.
2035 * 10 seconds by default.
2037 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2038 for (i=getnc_state->num_processed;
2039 i<getnc_state->num_records &&
2041 (r->out.ctr->ctr6.object_count < max_objects)
2042 && !max_wait_reached;
2045 struct drsuapi_DsReplicaObjectListItemEx *obj;
2046 struct ldb_message *msg;
2047 static const char * const msg_attrs[] = {
2049 "nTSecurityDescriptor",
2051 "replPropertyMetaData",
2052 DSDB_SECRET_ATTRIBUTES,
2054 struct ldb_result *msg_res;
2055 struct ldb_dn *msg_dn;
2057 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2058 W_ERROR_HAVE_NO_MEMORY(obj);
2060 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2061 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2064 /* by re-searching here we avoid having a lot of full
2065 * records in memory between calls to getncchanges
2067 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2069 LDB_SCOPE_BASE, msg_attrs, NULL);
2070 if (ret != LDB_SUCCESS) {
2071 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2072 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2073 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2079 msg = msg_res->msgs[0];
2081 max_wait_reached = (time(NULL) - start > max_wait);
2083 werr = get_nc_changes_build_object(obj, msg,
2084 sam_ctx, getnc_state->ncRoot_dn,
2085 getnc_state->is_schema_nc,
2086 schema, &session_key, getnc_state->min_usn,
2087 req10->replica_flags,
2088 req10->partial_attribute_set,
2089 req10->uptodateness_vector,
2092 if (!W_ERROR_IS_OK(werr)) {
2096 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2097 getnc_state->ncRoot_dn,
2098 schema, getnc_state->min_usn,
2099 req10->replica_flags,
2101 &getnc_state->la_list,
2102 &getnc_state->la_count,
2103 req10->uptodateness_vector);
2104 if (!W_ERROR_IS_OK(werr)) {
2108 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
2109 if (uSN > getnc_state->max_usn) {
2111 * Only report the max_usn we had at the start
2112 * of the replication cycle.
2114 * If this object has changed lately we better
2115 * let the destination dsa refetch the change.
2116 * This is better than the risk of loosing some
2117 * objects or linked attributes.
2121 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
2122 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
2123 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
2126 if (obj->meta_data_ctr == NULL) {
2127 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2128 ldb_dn_get_linearized(msg->dn)));
2129 /* no attributes to send */
2134 r->out.ctr->ctr6.object_count++;
2136 *currentObject = obj;
2137 currentObject = &obj->next_object;
2139 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
2141 talloc_free(getnc_state->last_dn);
2142 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
2144 talloc_free(msg_res);
2145 talloc_free(msg_dn);
2148 getnc_state->num_processed = i;
2150 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2152 /* the client can us to call UpdateRefs on its behalf to
2153 re-establish monitoring of the NC */
2154 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2155 !GUID_all_zero(&req10->destination_dsa_guid)) {
2156 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2157 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2158 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2159 ureq.naming_context = ncRoot;
2160 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2161 &req10->destination_dsa_guid);
2162 if (!ureq.dest_dsa_dns_name) {
2165 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2166 ureq.options = DRSUAPI_DRS_ADD_REF |
2167 DRSUAPI_DRS_ASYNC_OP |
2168 DRSUAPI_DRS_GETCHG_CHECK;
2170 /* we also need to pass through the
2171 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2172 to send notifies using the GC SPN */
2173 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2175 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2176 if (!W_ERROR_IS_OK(werr)) {
2177 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2178 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2185 * This is just a guess, how to calculate the
2186 * number of linked attributes to send, we need to
2187 * find out how to do this right.
2189 if (r->out.ctr->ctr6.object_count >= max_links) {
2192 max_links -= r->out.ctr->ctr6.object_count;
2195 link_total = getnc_state->la_count;
2197 if (i < getnc_state->num_records) {
2198 r->out.ctr->ctr6.more_data = true;
2200 /* sort the whole array the first time */
2201 if (!getnc_state->la_sorted) {
2203 struct la_for_sorting guid_array[getnc_state->la_count];
2204 struct drsuapi_DsReplicaLinkedAttribute tmp_array[getnc_state->la_count];
2205 TALLOC_CTX *tmp_ctx = talloc_new(sam_ctx);
2206 for (j = 0; j < getnc_state->la_count; j++) {
2207 /* we need to get the target GUIDs to compare */
2209 const struct drsuapi_DsReplicaLinkedAttribute *la = &getnc_state->la_list[j];
2210 const struct dsdb_attribute *schema_attrib;
2211 const struct ldb_val *target_guid;
2212 DATA_BLOB source_guid;
2214 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2216 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la->value.blob, &dn);
2217 if (!W_ERROR_IS_OK(werr)) {
2218 DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2219 talloc_free(tmp_ctx);
2223 /* Extract the target GUID in NDR form */
2224 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2225 if (target_guid == NULL) {
2226 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2228 /* Repack the source GUID as NDR for sorting */
2229 status = GUID_to_ndr_blob(&la->identifier->guid,
2234 if (!NT_STATUS_IS_OK(status)) {
2235 DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2236 talloc_free(tmp_ctx);
2237 return ntstatus_to_werror(status);
2240 guid_array[j] = (struct la_for_sorting)
2242 .source_guid = source_guid,
2243 .target_guid = *target_guid,
2244 .link = &getnc_state->la_list[j]
2248 LDB_TYPESAFE_QSORT(guid_array, getnc_state->la_count, NULL, linked_attribute_compare);
2250 /* apply the sort to the original list */
2251 for (j = 0; j < getnc_state->la_count; j++) {
2252 tmp_array[j] = *guid_array[j].link;
2254 memcpy(getnc_state->la_list, tmp_array,
2255 getnc_state->la_count * sizeof(struct drsuapi_DsReplicaLinkedAttribute));
2257 getnc_state->la_sorted = true;
2258 TALLOC_FREE(tmp_ctx);
2261 link_count = getnc_state->la_count - getnc_state->la_idx;
2262 link_count = MIN(max_links, link_count);
2264 r->out.ctr->ctr6.linked_attributes_count = link_count;
2265 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
2267 getnc_state->la_idx += link_count;
2268 link_given = getnc_state->la_idx;
2270 if (getnc_state->la_idx < getnc_state->la_count) {
2271 r->out.ctr->ctr6.more_data = true;
2275 if (!r->out.ctr->ctr6.more_data) {
2276 talloc_steal(mem_ctx, getnc_state->la_list);
2278 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2279 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2280 &getnc_state->final_udv);
2282 talloc_free(getnc_state);
2283 b_state->getncchanges_state = NULL;
2285 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2286 &r->out.ctr->ctr6.new_highwatermark);
2289 * We need to make sure that we never return the
2290 * same highwatermark within the same replication
2291 * cycle more than once. Otherwise we cannot detect
2292 * when the client uses an unexptected highwatermark.
2294 * This is a HACK which is needed because our
2295 * object ordering is wrong and set tmp_highest_usn
2296 * to a value that is higher than what we already
2297 * sent to the client (destination dsa).
2299 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2302 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2305 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2306 r->out.ctr->ctr6.uptodateness_vector = NULL;
2307 r->out.ctr->ctr6.nc_object_count = 0;
2308 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2311 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2312 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2313 (unsigned long long)(req10->highwatermark.highest_usn+1),
2314 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2315 r->out.ctr->ctr6.object_count,
2316 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2317 r->out.ctr->ctr6.linked_attributes_count,
2318 link_given, link_total,
2319 dom_sid_string(mem_ctx, user_sid)));
2322 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2323 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);