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;
47 struct GUID ncRoot_guid;
51 struct drsuapi_DsReplicaHighWaterMark last_hwm;
52 struct ldb_dn *last_dn;
53 struct drsuapi_DsReplicaHighWaterMark final_hwm;
54 struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
55 struct drsuapi_DsReplicaLinkedAttribute *la_list;
57 struct la_for_sorting *la_sorted;
61 /* We must keep the GUIDs in NDR form for sorting */
62 struct la_for_sorting {
63 struct drsuapi_DsReplicaLinkedAttribute *link;
64 uint8_t target_guid[16];
65 uint8_t source_guid[16];
68 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
69 const struct drsuapi_DsReplicaHighWaterMark *h2)
71 if (h1->highest_usn < h2->highest_usn) {
73 } else if (h1->highest_usn > h2->highest_usn) {
75 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
77 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
79 } else if (h1->reserved_usn < h2->reserved_usn) {
81 } else if (h1->reserved_usn > h2->reserved_usn) {
89 build a DsReplicaObjectIdentifier from a ldb msg
91 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
92 const struct ldb_message *msg)
94 struct drsuapi_DsReplicaObjectIdentifier *identifier;
97 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
98 if (identifier == NULL) {
102 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
103 identifier->guid = samdb_result_guid(msg, "objectGUID");
105 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
107 identifier->sid = *sid;
109 ZERO_STRUCT(identifier->sid);
114 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
116 return GUID_compare(guid1, &guid2);
120 see if we can filter an attribute using the uptodateness_vector
122 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
123 const struct GUID *originating_invocation_id,
124 uint64_t originating_usn)
126 const struct drsuapi_DsReplicaCursor *c;
127 if (udv == NULL) return false;
128 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
129 originating_invocation_id, udv_compare, c);
130 if (c && originating_usn <= c->highest_usn) {
137 static int uint32_t_cmp(uint32_t a1, uint32_t a2)
139 if (a1 == a2) return 0;
140 return a1 > a2 ? 1 : -1;
143 static int uint32_t_ptr_cmp(uint32_t *a1, uint32_t *a2, void *unused)
145 if (*a1 == *a2) return 0;
146 return *a1 > *a2 ? 1 : -1;
149 static WERROR getncchanges_attid_remote_to_local(const struct dsdb_schema *schema,
150 const struct dsdb_syntax_ctx *ctx,
151 enum drsuapi_DsAttributeId remote_attid_as_enum,
152 enum drsuapi_DsAttributeId *local_attid_as_enum,
153 const struct dsdb_attribute **_sa)
156 const struct dsdb_attribute *sa = NULL;
158 if (ctx->pfm_remote == NULL) {
159 DEBUG(7, ("No prefixMap supplied, falling back to local prefixMap.\n"));
163 werr = dsdb_attribute_drsuapi_remote_to_local(ctx,
164 remote_attid_as_enum,
167 if (!W_ERROR_IS_OK(werr)) {
168 DEBUG(3, ("WARNING: Unable to resolve remote attid, falling back to local prefixMap.\n"));
175 sa = dsdb_attribute_by_attributeID_id(schema, remote_attid_as_enum);
177 return WERR_DS_DRA_SCHEMA_MISMATCH;
179 if (local_attid_as_enum != NULL) {
180 *local_attid_as_enum = sa->attributeID_id;
190 drsuapi_DsGetNCChanges for one object
192 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
193 const struct ldb_message *msg,
194 struct ldb_context *sam_ctx,
195 struct ldb_dn *ncRoot_dn,
197 struct dsdb_schema *schema,
198 DATA_BLOB *session_key,
199 uint64_t highest_usn,
200 uint32_t replica_flags,
201 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
202 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
203 enum drsuapi_DsExtendedOperation extended_op,
204 bool force_object_return,
207 const struct ldb_val *md_value;
209 struct replPropertyMetaDataBlob md;
211 enum ndr_err_code ndr_err;
214 const struct dsdb_attribute *rdn_sa;
215 unsigned int instanceType;
216 struct dsdb_syntax_ctx syntax_ctx;
218 /* make dsdb sytanx context for conversions */
219 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
220 syntax_ctx.is_schema_nc = is_schema_nc;
222 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
223 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
224 obj->is_nc_prefix = true;
225 obj->parent_object_guid = NULL;
227 obj->is_nc_prefix = false;
228 obj->parent_object_guid = talloc(obj, struct GUID);
229 if (obj->parent_object_guid == NULL) {
230 return WERR_DS_DRA_INTERNAL_ERROR;
232 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
233 if (GUID_all_zero(obj->parent_object_guid)) {
234 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
235 ldb_dn_get_linearized(msg->dn)));
236 return WERR_DS_DRA_INTERNAL_ERROR;
239 obj->next_object = NULL;
241 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
243 /* nothing to send */
247 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
248 /* don't send uninstantiated objects */
252 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
253 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
254 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
255 return WERR_DS_DRA_INTERNAL_ERROR;
258 if (md.version != 1) {
259 return WERR_DS_DRA_INTERNAL_ERROR;
262 rdn = ldb_dn_get_rdn_name(msg->dn);
264 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
265 return WERR_DS_DRA_INTERNAL_ERROR;
268 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
269 if (rdn_sa == NULL) {
270 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
271 rdn, ldb_dn_get_linearized(msg->dn)));
272 return WERR_DS_DRA_INTERNAL_ERROR;
275 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
276 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
278 obj->object.identifier = get_object_identifier(obj, msg);
279 if (obj->object.identifier == NULL) {
280 return WERR_NOT_ENOUGH_MEMORY;
282 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
284 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
285 for (n=i=0; i<md.ctr.ctr1.count; i++) {
286 const struct dsdb_attribute *sa;
287 bool force_attribute = false;
289 /* if the attribute has not changed, and it is not the
290 instanceType then don't include it */
291 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
292 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
293 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
295 /* don't include the rDN */
296 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
298 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
300 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
301 (unsigned int)md.ctr.ctr1.array[i].attid,
302 ldb_dn_get_linearized(msg->dn)));
303 return WERR_DS_DRA_INTERNAL_ERROR;
307 struct ldb_message_element *el;
308 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
309 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
310 /* don't send upgraded links inline */
315 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
316 !dsdb_attr_in_rodc_fas(sa)) {
317 force_attribute = true;
318 DEBUG(4,("Forcing attribute %s in %s\n",
319 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
322 /* filter by uptodateness_vector */
323 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
325 udv_filter(uptodateness_vector,
326 &md.ctr.ctr1.array[i].originating_invocation_id,
327 md.ctr.ctr1.array[i].originating_usn)) {
331 /* filter by partial_attribute_set */
332 if (partial_attribute_set) {
333 uint32_t *result = NULL;
334 BINARY_ARRAY_SEARCH_V(local_pas, partial_attribute_set->num_attids, sa->attributeID_id,
335 uint32_t_cmp, result);
336 if (result == NULL) {
341 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
342 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
343 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
344 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
345 attids[n] = md.ctr.ctr1.array[i].attid;
349 /* ignore it if its an empty change. Note that renames always
350 * change the 'name' attribute, so they won't be ignored by
353 * the force_object_return check is used to force an empty
354 * object return when we timeout in the getncchanges loop.
355 * This allows us to return an empty object, which keeps the
356 * client happy while preventing timeouts
360 attids[0] == DRSUAPI_ATTID_instanceType &&
361 !force_object_return)) {
362 talloc_free(obj->meta_data_ctr);
363 obj->meta_data_ctr = NULL;
367 obj->meta_data_ctr->count = n;
369 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
370 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
371 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
372 obj->object.attribute_ctr.num_attributes);
373 if (obj->object.attribute_ctr.attributes == NULL) {
374 return WERR_NOT_ENOUGH_MEMORY;
378 * Note that the meta_data array and the attributes array must
379 * be the same size and in the same order
381 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
382 struct ldb_message_element *el;
384 const struct dsdb_attribute *sa;
386 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
388 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
389 return WERR_DS_DRA_INTERNAL_ERROR;
392 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
394 /* this happens for attributes that have been removed */
395 DEBUG(5,("No element '%s' for attributeID %u in message\n",
396 sa->lDAPDisplayName, attids[i]));
397 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
398 obj->object.attribute_ctr.attributes[i].attid =
399 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
401 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
402 &obj->object.attribute_ctr.attributes[i]);
403 if (!W_ERROR_IS_OK(werr)) {
404 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
405 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
409 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
410 * check if attribute is secret and send a null value
412 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
413 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
414 &obj->meta_data_ctr->meta_data[i]);
416 /* some attributes needs to be encrypted
418 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
419 &obj->object.attribute_ctr.attributes[i]);
420 if (!W_ERROR_IS_OK(werr)) {
421 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
422 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
427 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
428 DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID: "
432 ldb_dn_get_linearized(msg->dn),
434 obj->object.attribute_ctr.attributes[i].attid));
435 return WERR_DS_DATABASE_ERROR;
443 add one linked attribute from an object to the list of linked
444 attributes in a getncchanges request
446 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
447 struct ldb_context *sam_ctx,
448 const struct dsdb_schema *schema,
449 const struct dsdb_attribute *sa,
450 const struct ldb_message *msg,
451 struct dsdb_dn *dsdb_dn,
452 struct drsuapi_DsReplicaLinkedAttribute **la_list,
456 struct drsuapi_DsReplicaLinkedAttribute *la;
461 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
462 W_ERROR_HAVE_NO_MEMORY(*la_list);
464 la = &(*la_list)[*la_count];
466 la->identifier = get_object_identifier(*la_list, msg);
467 W_ERROR_HAVE_NO_MEMORY(la->identifier);
469 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
472 /* We have to check that the inactive link still point to an existing object */
478 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
479 if (strncmp(v, "TRUE", 4) == 0) {
481 * Note: we skip the transmition of the deleted link even if the other part used to
482 * know about it because when we transmit the deletion of the object, the link will
483 * be deleted too due to deletion of object where link points and Windows do so.
485 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
486 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
488 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
489 * if it join an existing domain with deleted objets, it firsts impose to have a
490 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
491 * either during initial replication or after the getNCChanges.
492 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
494 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
495 * If FL >=2K8R2 we are sure that this attribute will be here.
496 * For this kind of forest level we do not return the link if the object is recycled
497 * (isRecycled = true).
499 if (strncmp(v, "TRUE", 4) == 0) {
500 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
501 ldb_dn_get_linearized(msg->dn)));
508 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
509 if (!NT_STATUS_IS_OK(status)) {
510 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
511 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
512 return ntstatus_to_werror(status);
514 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
515 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
516 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
517 GUID_string(mem_ctx, &guid)));
519 } else if (ret != LDB_SUCCESS) {
520 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
521 GUID_string(mem_ctx, &guid),
526 la->attid = dsdb_attribute_get_attid(sa, is_schema_nc);
527 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
529 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
530 if (!NT_STATUS_IS_OK(status)) {
531 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
532 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
533 return ntstatus_to_werror(status);
535 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
536 if (!NT_STATUS_IS_OK(status)) {
537 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
538 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
539 return ntstatus_to_werror(status);
541 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
542 if (!NT_STATUS_IS_OK(status)) {
543 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
544 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
545 return ntstatus_to_werror(status);
547 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
548 if (!NT_STATUS_IS_OK(status)) {
549 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
550 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
551 return ntstatus_to_werror(status);
554 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
555 if (!NT_STATUS_IS_OK(status)) {
556 /* this is possible for upgraded links */
557 la->originating_add_time = la->meta_data.originating_change_time;
560 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
561 W_ERROR_NOT_OK_RETURN(werr);
569 add linked attributes from an object to the list of linked
570 attributes in a getncchanges request
572 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
574 struct ldb_dn *ncRoot_dn,
576 struct dsdb_schema *schema,
577 uint64_t highest_usn,
578 uint32_t replica_flags,
579 const struct ldb_message *msg,
580 struct drsuapi_DsReplicaLinkedAttribute **la_list,
582 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
585 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
586 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
588 for (i=0; i<msg->num_elements; i++) {
589 struct ldb_message_element *el = &msg->elements[i];
590 const struct dsdb_attribute *sa;
593 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
595 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
596 /* we only want forward links */
600 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
601 /* its an old style link, it will have been
602 * sent in the main replication data */
606 for (j=0; j<el->num_values; j++) {
607 struct dsdb_dn *dsdb_dn;
609 uint64_t originating_usn;
610 NTSTATUS status, status2;
612 struct GUID originating_invocation_id;
614 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
615 if (dsdb_dn == NULL) {
616 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
617 el->name, ldb_dn_get_linearized(msg->dn)));
618 talloc_free(tmp_ctx);
619 return WERR_DS_DRA_INTERNAL_ERROR;
622 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
623 if (!NT_STATUS_IS_OK(status)) {
624 /* this can happen for attributes
625 given to us with old style meta
630 if (local_usn > uSNChanged) {
631 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
632 el->name, ldb_dn_get_linearized(msg->dn)));
633 talloc_free(tmp_ctx);
634 return WERR_DS_DRA_INTERNAL_ERROR;
637 if (local_usn < highest_usn) {
641 status = dsdb_get_extended_dn_guid(dsdb_dn->dn,
642 &originating_invocation_id,
644 status2 = dsdb_get_extended_dn_uint64(dsdb_dn->dn,
646 "RMD_ORIGINATING_USN");
648 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2)) {
649 if (udv_filter(uptodateness_vector,
650 &originating_invocation_id,
656 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema,
657 sa, msg, dsdb_dn, la_list,
658 la_count, is_schema_nc);
659 if (!W_ERROR_IS_OK(werr)) {
660 talloc_free(tmp_ctx);
666 talloc_free(tmp_ctx);
671 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
673 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
674 struct ldb_dn *ncRoot_dn,
675 struct drsuapi_DsReplicaCursor2CtrEx *udv)
683 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
684 if (ret != LDB_SUCCESS) {
685 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
686 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
687 return WERR_DS_DRA_INTERNAL_ERROR;
694 /* comparison function for linked attributes - see CompareLinks() in
695 * MS-DRSR section 4.1.10.5.17 */
696 static int linked_attribute_compare(const struct la_for_sorting *la1,
697 const struct la_for_sorting *la2,
701 c = memcmp(la1->source_guid,
702 la2->source_guid, sizeof(la2->source_guid));
707 if (la1->link->attid != la2->link->attid) {
708 return la1->link->attid < la2->link->attid? -1:1;
711 if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
712 (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
713 return (la1->link->flags &
714 DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
717 return memcmp(la1->target_guid,
718 la2->target_guid, sizeof(la2->target_guid));
721 struct drsuapi_changed_objects {
728 sort the objects we send by tree order
730 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
731 struct drsuapi_changed_objects *m2,
732 struct drsuapi_getncchanges_state *getnc_state)
734 return ldb_dn_compare(m2->dn, m1->dn);
738 sort the objects we send first by uSNChanged
740 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
741 struct drsuapi_changed_objects *m2,
742 struct drsuapi_getncchanges_state *getnc_state)
746 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
751 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
756 if (m1->usn == m2->usn) {
757 return ldb_dn_compare(m2->dn, m1->dn);
760 if (m1->usn < m2->usn) {
769 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
771 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
773 struct drsuapi_DsGetNCChangesRequest10 *req10,
774 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
775 struct ldb_dn **rid_manager_dn)
777 struct ldb_dn *req_dn, *ntds_dn = NULL;
779 struct ldb_context *ldb = b_state->sam_ctx;
780 struct ldb_result *ext_res;
781 struct dsdb_fsmo_extended_op *exop;
786 - verify that the DN being asked for is the RID Manager DN
787 - verify that we are the RID Manager
790 /* work out who is the RID Manager, also return to caller */
791 ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
792 if (ret != LDB_SUCCESS) {
793 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
794 return WERR_DS_DRA_INTERNAL_ERROR;
797 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
798 if (!ldb_dn_validate(req_dn) ||
799 ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
800 /* that isn't the RID Manager DN */
801 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
802 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
803 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
807 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
808 ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
809 if (ret != LDB_SUCCESS) {
810 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
811 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
812 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
816 /* find the DN of the RID Manager */
817 ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
818 if (ret != LDB_SUCCESS) {
819 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
820 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
821 return WERR_DS_DRA_INTERNAL_ERROR;
825 /* we're not the RID Manager - go away */
826 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
827 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
831 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
832 W_ERROR_HAVE_NO_MEMORY(exop);
834 exop->fsmo_info = req10->fsmo_info;
835 exop->destination_dsa_guid = req10->destination_dsa_guid;
837 ret = ldb_transaction_start(ldb);
838 if (ret != LDB_SUCCESS) {
839 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
840 ldb_errstring(ldb)));
841 return WERR_DS_DRA_INTERNAL_ERROR;
844 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
845 if (ret != LDB_SUCCESS) {
846 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
847 ldb_errstring(ldb)));
848 ldb_transaction_cancel(ldb);
849 return WERR_DS_DRA_INTERNAL_ERROR;
852 ret = ldb_transaction_commit(ldb);
853 if (ret != LDB_SUCCESS) {
854 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
855 ldb_errstring(ldb)));
856 return WERR_DS_DRA_INTERNAL_ERROR;
859 talloc_free(ext_res);
861 DEBUG(2,("Allocated RID pool for server %s\n",
862 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
864 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
870 return an array of SIDs from a ldb_message given an attribute name
871 assumes the SIDs are in extended DN format
873 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
874 struct ldb_message *msg,
877 const struct dom_sid ***sids)
879 struct ldb_message_element *el;
882 el = ldb_msg_find_element(msg, attr);
888 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
889 W_ERROR_HAVE_NO_MEMORY(*sids);
891 for (i=0; i<el->num_values; i++) {
892 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
896 sid = talloc(*sids, struct dom_sid);
897 W_ERROR_HAVE_NO_MEMORY(sid);
898 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
899 if (!NT_STATUS_IS_OK(status)) {
900 return WERR_INTERNAL_DB_CORRUPTION;
911 return an array of SIDs from a ldb_message given an attribute name
912 assumes the SIDs are in NDR form
914 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
915 struct ldb_message *msg,
918 const struct dom_sid ***sids)
920 struct ldb_message_element *el;
923 el = ldb_msg_find_element(msg, attr);
929 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
930 W_ERROR_HAVE_NO_MEMORY(*sids);
932 for (i=0; i<el->num_values; i++) {
933 enum ndr_err_code ndr_err;
936 sid = talloc(*sids, struct dom_sid);
937 W_ERROR_HAVE_NO_MEMORY(sid);
939 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
940 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
941 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
942 return WERR_INTERNAL_DB_CORRUPTION;
952 see if any SIDs in list1 are in list2
954 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
957 /* do we ever have enough SIDs here to worry about O(n^2) ? */
958 for (i=0; list1[i]; i++) {
959 for (j=0; list2[j]; j++) {
960 if (dom_sid_equal(list1[i], list2[j])) {
969 handle a DRSUAPI_EXOP_REPL_SECRET call
971 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
973 struct drsuapi_DsGetNCChangesRequest10 *req10,
974 struct dom_sid *user_sid,
975 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
976 bool has_get_all_changes)
978 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
979 struct ldb_dn *obj_dn = NULL;
980 struct ldb_dn *rodc_dn, *krbtgt_link_dn;
982 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
983 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
984 struct ldb_result *rodc_res, *obj_res;
985 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
988 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
989 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
992 * we need to work out if we will allow this DC to
993 * replicate the secrets for this object
995 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
999 if (b_state->sam_ctx_system == NULL) {
1000 /* this operation needs system level access */
1001 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
1002 return WERR_DS_DRA_SOURCE_DISABLED;
1006 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
1008 * The pseudo code indicate
1009 * revealsecrets = true
1010 * if IsRevealSecretRequest(msgIn) then
1011 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
1013 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
1014 * <... check if this account is ok to be replicated on this DC ...>
1015 * <... and if not reveal secrets = no ...>
1017 * reveal secrets = false
1022 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
1023 * then you can do EXOP_REPL_SECRETS
1025 if (has_get_all_changes) {
1029 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
1030 if (!ldb_dn_validate(obj_dn)) goto failed;
1032 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1033 dom_sid_string(mem_ctx, user_sid));
1034 if (!ldb_dn_validate(rodc_dn)) goto failed;
1036 /* do the two searches we need */
1037 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1038 DSDB_SEARCH_SHOW_EXTENDED_DN);
1039 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1041 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1042 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1044 /* if the object SID is equal to the user_sid, allow */
1045 if (dom_sid_equal(user_sid,
1046 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1050 /* an RODC is allowed to get its own krbtgt account secrets */
1051 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1052 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1053 if (krbtgt_link_dn != NULL &&
1054 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1058 /* but it isn't allowed to get anyone elses krbtgt secrets */
1059 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1060 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1064 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1065 "userAccountControl", 0) &
1066 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1070 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1071 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1072 if (!W_ERROR_IS_OK(werr)) {
1076 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1077 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1078 if (!W_ERROR_IS_OK(werr)) {
1082 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1083 mem_ctx, "tokenGroups", &token_sids);
1084 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1088 if (never_reveal_sids &&
1089 sid_list_match(token_sids, never_reveal_sids)) {
1094 sid_list_match(token_sids, reveal_sids)) {
1100 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1101 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1102 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1103 return WERR_DS_DRA_ACCESS_DENIED;
1106 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1107 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1108 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1109 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1110 req10->highwatermark.highest_usn = 0;
1114 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1115 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1116 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1117 return WERR_DS_DRA_BAD_DN;
1122 handle a DRSUAPI_EXOP_REPL_OBJ call
1124 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1125 TALLOC_CTX *mem_ctx,
1126 struct drsuapi_DsGetNCChangesRequest10 *req10,
1127 struct dom_sid *user_sid,
1128 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1130 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1132 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1133 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1135 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1141 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1142 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1143 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1145 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1146 TALLOC_CTX *mem_ctx,
1147 struct drsuapi_DsGetNCChangesRequest10 *req10,
1148 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1150 struct ldb_dn *req_dn, *ntds_dn;
1153 struct ldb_context *ldb = b_state->sam_ctx;
1154 struct ldb_message *msg;
1159 - verify that the client dn exists
1160 - verify that we are the current master
1163 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1164 if (!ldb_dn_validate(req_dn)) {
1165 /* that is not a valid dn */
1166 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1167 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1168 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1172 /* find the DN of the current role owner */
1173 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1174 if (ret != LDB_SUCCESS) {
1175 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1176 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1177 return WERR_DS_DRA_INTERNAL_ERROR;
1181 /* we're not the RID Manager or role owner - go away */
1182 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1183 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1187 /* change the current master */
1188 msg = ldb_msg_new(ldb);
1189 W_ERROR_HAVE_NO_MEMORY(msg);
1190 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1191 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1193 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1194 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1195 if (ret != LDB_SUCCESS) {
1196 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1197 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1199 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1203 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1206 return WERR_DS_DRA_INTERNAL_ERROR;
1209 for (i=0;i<msg->num_elements;i++) {
1210 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1213 ret = ldb_transaction_start(ldb);
1214 if (ret != LDB_SUCCESS) {
1215 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1216 ldb_errstring(ldb)));
1217 return WERR_DS_DRA_INTERNAL_ERROR;
1220 ret = ldb_modify(ldb, msg);
1221 if (ret != LDB_SUCCESS) {
1222 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1223 ldb_errstring(ldb)));
1224 ldb_transaction_cancel(ldb);
1225 return WERR_DS_DRA_INTERNAL_ERROR;
1228 ret = ldb_transaction_commit(ldb);
1229 if (ret != LDB_SUCCESS) {
1230 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1231 ldb_errstring(ldb)));
1232 return WERR_DS_DRA_INTERNAL_ERROR;
1235 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1241 see if this getncchanges request includes a request to reveal secret information
1243 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1244 struct drsuapi_DsGetNCChangesRequest10 *req10,
1245 struct dsdb_schema_prefixmap *pfm_remote,
1246 bool *is_secret_request)
1248 enum drsuapi_DsExtendedOperation exop;
1250 struct dsdb_schema *schema;
1251 struct dsdb_syntax_ctx syntax_ctx;
1253 *is_secret_request = true;
1255 exop = req10->extended_op;
1258 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1259 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1260 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1261 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1262 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1263 /* FSMO exops can reveal secrets */
1264 *is_secret_request = true;
1266 case DRSUAPI_EXOP_REPL_SECRET:
1267 case DRSUAPI_EXOP_REPL_OBJ:
1268 case DRSUAPI_EXOP_NONE:
1272 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1273 *is_secret_request = false;
1277 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1278 req10->partial_attribute_set == NULL) {
1279 /* they want secrets */
1280 *is_secret_request = true;
1284 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1285 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1286 syntax_ctx.pfm_remote = pfm_remote;
1288 /* check the attributes they asked for */
1289 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1290 const struct dsdb_attribute *sa;
1291 WERROR werr = getncchanges_attid_remote_to_local(schema,
1293 req10->partial_attribute_set->attids[i],
1297 if (!W_ERROR_IS_OK(werr)) {
1298 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1299 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1303 if (!dsdb_attr_in_rodc_fas(sa)) {
1304 *is_secret_request = true;
1309 if (req10->partial_attribute_set_ex) {
1310 /* check the extended attributes they asked for */
1311 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1312 const struct dsdb_attribute *sa;
1313 WERROR werr = getncchanges_attid_remote_to_local(schema,
1315 req10->partial_attribute_set_ex->attids[i],
1319 if (!W_ERROR_IS_OK(werr)) {
1320 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1321 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1325 if (!dsdb_attr_in_rodc_fas(sa)) {
1326 *is_secret_request = true;
1332 *is_secret_request = false;
1337 see if this getncchanges request is only for attributes in the GC
1338 partial attribute set
1340 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1341 struct drsuapi_DsGetNCChangesRequest10 *req10,
1342 struct dsdb_schema_prefixmap *pfm_remote,
1343 bool *is_gc_pas_request)
1345 enum drsuapi_DsExtendedOperation exop;
1347 struct dsdb_schema *schema;
1348 struct dsdb_syntax_ctx syntax_ctx;
1350 exop = req10->extended_op;
1353 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1354 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1355 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1356 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1357 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1358 case DRSUAPI_EXOP_REPL_SECRET:
1359 *is_gc_pas_request = false;
1361 case DRSUAPI_EXOP_REPL_OBJ:
1362 case DRSUAPI_EXOP_NONE:
1366 if (req10->partial_attribute_set == NULL) {
1367 /* they want it all */
1368 *is_gc_pas_request = false;
1372 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1373 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1374 syntax_ctx.pfm_remote = pfm_remote;
1376 /* check the attributes they asked for */
1377 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1378 const struct dsdb_attribute *sa;
1379 WERROR werr = getncchanges_attid_remote_to_local(schema,
1381 req10->partial_attribute_set->attids[i],
1385 if (!W_ERROR_IS_OK(werr)) {
1386 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1387 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1391 if (!sa->isMemberOfPartialAttributeSet) {
1392 *is_gc_pas_request = false;
1397 if (req10->partial_attribute_set_ex) {
1398 /* check the extended attributes they asked for */
1399 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1400 const struct dsdb_attribute *sa;
1401 WERROR werr = getncchanges_attid_remote_to_local(schema,
1403 req10->partial_attribute_set_ex->attids[i],
1407 if (!W_ERROR_IS_OK(werr)) {
1408 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1409 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1413 if (!sa->isMemberOfPartialAttributeSet) {
1414 *is_gc_pas_request = false;
1420 *is_gc_pas_request = true;
1426 map from req8 to req10
1428 static struct drsuapi_DsGetNCChangesRequest10 *
1429 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1430 struct drsuapi_DsGetNCChangesRequest8 *req8)
1432 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1433 struct drsuapi_DsGetNCChangesRequest10);
1434 if (req10 == NULL) {
1438 req10->destination_dsa_guid = req8->destination_dsa_guid;
1439 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1440 req10->naming_context = req8->naming_context;
1441 req10->highwatermark = req8->highwatermark;
1442 req10->uptodateness_vector = req8->uptodateness_vector;
1443 req10->replica_flags = req8->replica_flags;
1444 req10->max_object_count = req8->max_object_count;
1445 req10->max_ndr_size = req8->max_ndr_size;
1446 req10->extended_op = req8->extended_op;
1447 req10->fsmo_info = req8->fsmo_info;
1448 req10->partial_attribute_set = req8->partial_attribute_set;
1449 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1450 req10->mapping_ctr = req8->mapping_ctr;
1455 static const char *collect_objects_attrs[] = { "uSNChanged",
1460 * Collects object for normal replication cycle.
1462 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1463 TALLOC_CTX *mem_ctx,
1464 struct drsuapi_DsGetNCChangesRequest10 *req10,
1465 struct ldb_dn *search_dn,
1466 const char *extra_filter,
1467 struct ldb_result **search_res)
1470 char* search_filter;
1471 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1472 //const char *extra_filter;
1473 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1474 bool critical_only = false;
1476 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1477 critical_only = true;
1480 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1481 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1482 scope = LDB_SCOPE_BASE;
1483 critical_only = false;
1486 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1488 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1490 /* Construct response. */
1491 search_filter = talloc_asprintf(mem_ctx,
1492 "(uSNChanged>=%llu)",
1493 (unsigned long long)(getnc_state->min_usn+1));
1496 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1499 if (critical_only) {
1500 search_filter = talloc_asprintf(mem_ctx,
1501 "(&%s(isCriticalSystemObject=TRUE))",
1505 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1506 scope = LDB_SCOPE_BASE;
1510 search_dn = getnc_state->ncRoot_dn;
1513 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1514 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1515 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1517 collect_objects_attrs,
1519 if (ret != LDB_SUCCESS) {
1520 return WERR_DS_DRA_INTERNAL_ERROR;
1527 * Collects object for normal replication cycle.
1529 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1530 TALLOC_CTX *mem_ctx,
1531 struct drsuapi_DsGetNCChangesRequest10 *req10,
1532 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1533 struct ldb_dn *search_dn,
1534 const char *extra_filter,
1535 struct ldb_result **search_res)
1537 /* we have nothing to do in case of ex-op failure */
1538 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1542 switch (req10->extended_op) {
1543 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1546 struct ldb_dn *ntds_dn = NULL;
1547 struct ldb_dn *server_dn = NULL;
1548 struct ldb_dn *machine_dn = NULL;
1549 struct ldb_dn *rid_set_dn = NULL;
1550 struct ldb_result *search_res2 = NULL;
1551 struct ldb_result *search_res3 = NULL;
1552 TALLOC_CTX *frame = talloc_stackframe();
1553 /* get RID manager, RID set and server DN (in that order) */
1555 /* This first search will get the RID Manager */
1556 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1558 search_dn, LDB_SCOPE_BASE,
1559 collect_objects_attrs,
1561 if (ret != LDB_SUCCESS) {
1562 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1563 ldb_dn_get_linearized(search_dn),
1564 ldb_errstring(b_state->sam_ctx)));
1566 return WERR_DS_DRA_INTERNAL_ERROR;
1569 if ((*search_res)->count != 1) {
1570 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1571 ldb_dn_get_linearized(search_dn),
1572 (*search_res)->count));
1574 return WERR_DS_DRA_INTERNAL_ERROR;
1577 /* Now extend it to the RID set */
1579 /* Find the computer account DN for the destination
1580 * dsa GUID specified */
1582 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1583 &req10->destination_dsa_guid, 0,
1585 if (ret != LDB_SUCCESS) {
1586 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1588 &req10->destination_dsa_guid),
1589 ldb_errstring(b_state->sam_ctx)));
1591 return WERR_DS_DRA_INTERNAL_ERROR;
1594 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1597 return WERR_DS_DRA_INTERNAL_ERROR;
1600 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1601 "serverReference", &machine_dn);
1602 if (ret != LDB_SUCCESS) {
1603 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1604 ldb_dn_get_linearized(server_dn),
1605 ldb_errstring(b_state->sam_ctx)));
1607 return WERR_DS_DRA_INTERNAL_ERROR;
1610 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1611 "rIDSetReferences", &rid_set_dn);
1612 if (ret != LDB_SUCCESS) {
1613 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1614 ldb_dn_get_linearized(server_dn),
1615 ldb_errstring(b_state->sam_ctx)));
1617 return WERR_DS_DRA_INTERNAL_ERROR;
1621 /* This first search will get the RID Manager, now get the RID set */
1622 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1624 rid_set_dn, LDB_SCOPE_BASE,
1625 collect_objects_attrs,
1627 if (ret != LDB_SUCCESS) {
1628 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1629 ldb_dn_get_linearized(rid_set_dn),
1630 ldb_errstring(b_state->sam_ctx)));
1632 return WERR_DS_DRA_INTERNAL_ERROR;
1635 if (search_res2->count != 1) {
1636 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1637 ldb_dn_get_linearized(rid_set_dn),
1638 search_res2->count));
1640 return WERR_DS_DRA_INTERNAL_ERROR;
1643 /* Finally get the server DN */
1644 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1646 machine_dn, LDB_SCOPE_BASE,
1647 collect_objects_attrs,
1649 if (ret != LDB_SUCCESS) {
1650 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1651 ldb_dn_get_linearized(server_dn),
1652 ldb_errstring(b_state->sam_ctx)));
1654 return WERR_DS_DRA_INTERNAL_ERROR;
1657 if (search_res3->count != 1) {
1658 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1659 ldb_dn_get_linearized(server_dn),
1660 search_res3->count));
1662 return WERR_DS_DRA_INTERNAL_ERROR;
1665 /* Now extend the original search_res with these answers */
1666 (*search_res)->count = 3;
1668 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1669 struct ldb_message *,
1670 (*search_res)->count);
1671 if ((*search_res)->msgs == NULL) {
1673 return WERR_NOT_ENOUGH_MEMORY;
1677 talloc_steal(mem_ctx, *search_res);
1678 (*search_res)->msgs[1] =
1679 talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1680 (*search_res)->msgs[2] =
1681 talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1687 /* TODO: implement extended op specific collection
1688 * of objects. Right now we just normal procedure
1689 * for collecting objects */
1690 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1695 drsuapi_DsGetNCChanges
1697 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1699 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1700 struct drsuapi_DsGetNCChanges *r)
1702 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1705 struct dsdb_schema *schema;
1706 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1707 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1709 DATA_BLOB session_key;
1711 struct dcesrv_handle *h;
1712 struct drsuapi_bind_state *b_state;
1713 struct drsuapi_getncchanges_state *getnc_state;
1714 struct drsuapi_DsGetNCChangesRequest10 *req10;
1716 uint32_t max_objects;
1718 uint32_t link_count = 0;
1719 uint32_t link_total = 0;
1720 uint32_t link_given = 0;
1721 struct ldb_dn *search_dn = NULL;
1722 bool am_rodc, null_scope=false;
1723 enum security_user_level security_level;
1724 struct ldb_context *sam_ctx;
1725 struct dom_sid *user_sid;
1726 bool is_secret_request;
1727 bool is_gc_pas_request;
1728 struct drsuapi_changed_objects *changes;
1730 time_t start = time(NULL);
1731 bool max_wait_reached = false;
1732 bool has_get_all_changes = false;
1733 struct GUID invocation_id;
1734 static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
1735 struct dsdb_schema_prefixmap *pfm_remote = NULL;
1737 uint32_t *local_pas = NULL;
1739 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1742 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1744 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1746 *r->out.level_out = 6;
1747 /* TODO: linked attributes*/
1748 r->out.ctr->ctr6.linked_attributes_count = 0;
1749 r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
1751 r->out.ctr->ctr6.object_count = 0;
1752 r->out.ctr->ctr6.nc_object_count = 0;
1753 r->out.ctr->ctr6.more_data = false;
1754 r->out.ctr->ctr6.uptodateness_vector = NULL;
1755 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1756 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1757 r->out.ctr->ctr6.first_object = NULL;
1759 /* a RODC doesn't allow for any replication */
1760 ret = samdb_rodc(sam_ctx, &am_rodc);
1761 if (ret == LDB_SUCCESS && am_rodc) {
1762 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1763 return WERR_DS_DRA_SOURCE_DISABLED;
1766 /* Check request revision.
1768 switch (r->in.level) {
1770 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1771 if (req10 == NULL) {
1772 return WERR_NOT_ENOUGH_MEMORY;
1776 req10 = &r->in.req->req10;
1779 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1781 return WERR_REVISION_MISMATCH;
1785 /* Perform access checks. */
1786 /* TODO: we need to support a sync on a specific non-root
1787 * DN. We'll need to find the real partition root here */
1788 ncRoot = req10->naming_context;
1789 if (ncRoot == NULL) {
1790 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1791 return WERR_DS_DRA_INVALID_PARAMETER;
1794 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1795 return WERR_DS_DRA_INTERNAL_ERROR;
1798 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1799 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1800 return WERR_DS_DRA_SOURCE_DISABLED;
1803 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1805 /* all clients must have GUID_DRS_GET_CHANGES */
1806 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1808 dce_call->conn->auth_state.session_info->security_token,
1809 req10->naming_context,
1810 GUID_DRS_GET_CHANGES);
1811 if (!W_ERROR_IS_OK(werr)) {
1815 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
1816 full = req10->partial_attribute_set == NULL &&
1817 req10->partial_attribute_set_ex == NULL;
1819 full = (options & DRSUAPI_DRS_WRIT_REP) != 0;
1822 werr = dsdb_schema_pfm_from_drsuapi_pfm(&req10->mapping_ctr, true,
1823 mem_ctx, &pfm_remote, NULL);
1825 /* We were supplied a partial attribute set, without the prefix map! */
1826 if (!full && !W_ERROR_IS_OK(werr)) {
1827 if (req10->mapping_ctr.num_mappings == 0) {
1829 * Despite the fact MS-DRSR specifies that this shouldn't
1830 * happen, Windows RODCs will in fact not provide a prefixMap.
1832 DEBUG(5,(__location__ ": Failed to provide a remote prefixMap,"
1833 " falling back to local prefixMap\n"));
1835 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
1841 /* allowed if the GC PAS and client has
1842 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1843 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, pfm_remote, &is_gc_pas_request);
1844 if (!W_ERROR_IS_OK(werr)) {
1847 if (is_gc_pas_request) {
1848 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1850 dce_call->conn->auth_state.session_info->security_token,
1851 req10->naming_context,
1852 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1853 if (W_ERROR_IS_OK(werr)) {
1858 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10,
1860 &is_secret_request);
1861 if (!W_ERROR_IS_OK(werr)) {
1864 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1865 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1867 dce_call->conn->auth_state.session_info->security_token,
1868 req10->naming_context,
1869 GUID_DRS_GET_ALL_CHANGES);
1870 if (!W_ERROR_IS_OK(werr)) {
1873 has_get_all_changes = true;
1878 /* for non-administrator replications, check that they have
1879 given the correct source_dsa_invocation_id */
1880 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1881 samdb_domain_sid(sam_ctx));
1882 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1883 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1884 /* we rely on this flag being unset for RODC requests */
1885 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1889 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1890 /* Ignore the _in_ uptpdateness vector*/
1891 req10->uptodateness_vector = NULL;
1894 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1895 req10->source_dsa_invocation_id = invocation_id;
1898 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1900 * The given highwatermark is only valid relative to the
1901 * specified source_dsa_invocation_id.
1903 ZERO_STRUCT(req10->highwatermark);
1906 getnc_state = b_state->getncchanges_state;
1908 /* see if a previous replication has been abandoned */
1910 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1911 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1912 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1913 ldb_dn_get_linearized(new_dn),
1914 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1915 ldb_dn_get_linearized(getnc_state->last_dn)));
1916 talloc_free(getnc_state);
1922 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1923 &req10->highwatermark);
1925 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1926 "on DN %s %s highwatermark (last_dn %s)\n",
1927 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1928 (ret > 0) ? "older" : "newer",
1929 ldb_dn_get_linearized(getnc_state->last_dn)));
1930 talloc_free(getnc_state);
1935 if (getnc_state == NULL) {
1936 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1937 if (getnc_state == NULL) {
1938 return WERR_NOT_ENOUGH_MEMORY;
1940 b_state->getncchanges_state = getnc_state;
1941 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1942 if (getnc_state->ncRoot_dn == NULL) {
1943 return WERR_NOT_ENOUGH_MEMORY;
1946 ret = dsdb_find_guid_by_dn(b_state->sam_ctx_system,
1947 getnc_state->ncRoot_dn,
1948 &getnc_state->ncRoot_guid);
1949 if (ret != LDB_SUCCESS) {
1950 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1951 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1952 return WERR_DS_DRA_INTERNAL_ERROR;
1954 ncRoot->guid = getnc_state->ncRoot_guid;
1956 /* find out if we are to replicate Schema NC */
1957 ret = ldb_dn_compare_base(ldb_get_schema_basedn(b_state->sam_ctx),
1958 getnc_state->ncRoot_dn);
1960 getnc_state->is_schema_nc = (0 == ret);
1962 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1963 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1967 * This is the first replication cycle and it is
1968 * a good place to handle extended operations
1970 * FIXME: we don't fully support extended operations yet
1972 switch (req10->extended_op) {
1973 case DRSUAPI_EXOP_NONE:
1975 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1976 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
1977 W_ERROR_NOT_OK_RETURN(werr);
1978 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1982 case DRSUAPI_EXOP_REPL_SECRET:
1983 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1986 has_get_all_changes);
1987 r->out.result = werr;
1988 W_ERROR_NOT_OK_RETURN(werr);
1990 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1991 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1992 W_ERROR_NOT_OK_RETURN(werr);
1993 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1997 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1998 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1999 W_ERROR_NOT_OK_RETURN(werr);
2000 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2004 case DRSUAPI_EXOP_FSMO_REQ_PDC:
2005 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2006 W_ERROR_NOT_OK_RETURN(werr);
2007 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2011 case DRSUAPI_EXOP_REPL_OBJ:
2012 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
2013 r->out.result = werr;
2014 W_ERROR_NOT_OK_RETURN(werr);
2017 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
2019 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
2020 (unsigned)req10->extended_op));
2021 return WERR_DS_DRA_NOT_SUPPORTED;
2025 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
2026 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
2027 DEBUG(0,(__location__ ": Bad DN '%s'\n",
2028 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
2029 return WERR_DS_DRA_INVALID_PARAMETER;
2032 ncRoot->guid = getnc_state->ncRoot_guid;
2034 /* we need the session key for encrypting password attributes */
2035 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
2036 if (!NT_STATUS_IS_OK(status)) {
2037 DEBUG(0,(__location__ ": Failed to get session key\n"));
2038 return WERR_DS_DRA_INTERNAL_ERROR;
2042 TODO: MS-DRSR section 4.1.10.1.1
2043 Work out if this is the start of a new cycle */
2045 if (getnc_state->guids == NULL) {
2046 const char *extra_filter;
2047 struct ldb_result *search_res = NULL;
2049 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
2051 getnc_state->min_usn = req10->highwatermark.highest_usn;
2052 getnc_state->max_usn = getnc_state->min_usn;
2054 getnc_state->final_udv = talloc_zero(getnc_state,
2055 struct drsuapi_DsReplicaCursor2CtrEx);
2056 if (getnc_state->final_udv == NULL) {
2057 return WERR_NOT_ENOUGH_MEMORY;
2059 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
2060 getnc_state->final_udv);
2061 if (!W_ERROR_IS_OK(werr)) {
2065 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2066 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
2067 search_dn, extra_filter,
2070 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
2072 search_dn, extra_filter,
2075 W_ERROR_NOT_OK_RETURN(werr);
2077 /* extract out the GUIDs list */
2078 getnc_state->num_records = search_res ? search_res->count : 0;
2079 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
2080 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
2082 changes = talloc_array(getnc_state,
2083 struct drsuapi_changed_objects,
2084 getnc_state->num_records);
2085 W_ERROR_HAVE_NO_MEMORY(changes);
2087 for (i=0; i<getnc_state->num_records; i++) {
2088 changes[i].dn = search_res->msgs[i]->dn;
2089 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
2090 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
2092 if (changes[i].usn > getnc_state->max_usn) {
2093 getnc_state->max_usn = changes[i].usn;
2097 /* RID_ALLOC returns 3 objects in a fixed order */
2098 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
2100 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
2101 LDB_TYPESAFE_QSORT(changes,
2102 getnc_state->num_records,
2104 site_res_cmp_anc_order);
2106 LDB_TYPESAFE_QSORT(changes,
2107 getnc_state->num_records,
2109 site_res_cmp_usn_order);
2112 for (i=0; i < getnc_state->num_records; i++) {
2113 getnc_state->guids[i] = changes[i].guid;
2114 if (GUID_all_zero(&getnc_state->guids[i])) {
2115 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
2116 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
2117 return WERR_DS_DRA_INTERNAL_ERROR;
2121 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
2122 getnc_state->final_hwm.reserved_usn = 0;
2123 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
2125 talloc_free(search_res);
2126 talloc_free(changes);
2129 if (req10->uptodateness_vector) {
2130 /* make sure its sorted */
2131 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
2132 req10->uptodateness_vector->count,
2133 drsuapi_DsReplicaCursor_compare);
2136 /* Prefix mapping */
2137 schema = dsdb_get_schema(sam_ctx, mem_ctx);
2139 DEBUG(0,("No schema in sam_ctx\n"));
2140 return WERR_DS_DRA_INTERNAL_ERROR;
2143 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
2144 if (r->out.ctr->ctr6.naming_context == NULL) {
2145 return WERR_NOT_ENOUGH_MEMORY;
2147 *r->out.ctr->ctr6.naming_context = *ncRoot;
2149 /* find the SID if there is one */
2150 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2152 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2153 r->out.ctr->ctr6.mapping_ctr = *ctr;
2155 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2156 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2158 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2159 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2161 currentObject = &r->out.ctr->ctr6.first_object;
2163 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2165 * The client control here only applies in normal replication, not extended
2166 * operations, which return a fixed set, even if the caller
2167 * sets max_object_count == 0
2169 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2170 /* use this to force single objects at a time, which is useful
2171 * for working out what object is giving problems
2173 if (req10->max_object_count < max_objects) {
2174 max_objects = req10->max_object_count;
2178 * TODO: work out how the maximum should be calculated
2180 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2183 * Maximum time that we can spend in a getncchanges
2184 * in order to avoid timeout of the other part.
2185 * 10 seconds by default.
2187 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2189 if (req10->partial_attribute_set != NULL) {
2190 struct dsdb_syntax_ctx syntax_ctx;
2193 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
2194 syntax_ctx.pfm_remote = pfm_remote;
2196 local_pas = talloc_array(b_state, uint32_t, req10->partial_attribute_set->num_attids);
2198 for (j = 0; j < req10->partial_attribute_set->num_attids; j++) {
2199 getncchanges_attid_remote_to_local(schema,
2201 req10->partial_attribute_set->attids[j],
2202 (enum drsuapi_DsAttributeId *)&local_pas[j],
2206 LDB_TYPESAFE_QSORT(local_pas,
2207 req10->partial_attribute_set->num_attids,
2212 for (i=getnc_state->num_processed;
2213 i<getnc_state->num_records &&
2215 (r->out.ctr->ctr6.object_count < max_objects)
2216 && !max_wait_reached;
2219 struct drsuapi_DsReplicaObjectListItemEx *obj;
2220 struct ldb_message *msg;
2221 static const char * const msg_attrs[] = {
2223 "nTSecurityDescriptor",
2225 "replPropertyMetaData",
2226 DSDB_SECRET_ATTRIBUTES,
2228 struct ldb_result *msg_res;
2229 struct ldb_dn *msg_dn;
2231 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2232 W_ERROR_HAVE_NO_MEMORY(obj);
2234 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2235 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2238 /* by re-searching here we avoid having a lot of full
2239 * records in memory between calls to getncchanges
2241 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2243 LDB_SCOPE_BASE, msg_attrs, NULL);
2244 if (ret != LDB_SUCCESS) {
2245 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2246 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2247 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2253 msg = msg_res->msgs[0];
2255 max_wait_reached = (time(NULL) - start > max_wait);
2257 werr = get_nc_changes_build_object(obj, msg,
2258 sam_ctx, getnc_state->ncRoot_dn,
2259 getnc_state->is_schema_nc,
2260 schema, &session_key, getnc_state->min_usn,
2261 req10->replica_flags,
2262 req10->partial_attribute_set,
2263 req10->uptodateness_vector,
2267 if (!W_ERROR_IS_OK(werr)) {
2271 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2272 getnc_state->ncRoot_dn,
2273 getnc_state->is_schema_nc,
2274 schema, getnc_state->min_usn,
2275 req10->replica_flags,
2277 &getnc_state->la_list,
2278 &getnc_state->la_count,
2279 req10->uptodateness_vector);
2280 if (!W_ERROR_IS_OK(werr)) {
2284 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
2285 if (uSN > getnc_state->max_usn) {
2287 * Only report the max_usn we had at the start
2288 * of the replication cycle.
2290 * If this object has changed lately we better
2291 * let the destination dsa refetch the change.
2292 * This is better than the risk of loosing some
2293 * objects or linked attributes.
2297 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
2298 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
2299 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
2302 if (obj->meta_data_ctr == NULL) {
2303 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2304 ldb_dn_get_linearized(msg->dn)));
2305 /* no attributes to send */
2310 r->out.ctr->ctr6.object_count++;
2312 *currentObject = obj;
2313 currentObject = &obj->next_object;
2315 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
2317 talloc_free(getnc_state->last_dn);
2318 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
2320 talloc_free(msg_res);
2321 talloc_free(msg_dn);
2324 getnc_state->num_processed = i;
2326 /* the client can us to call UpdateRefs on its behalf to
2327 re-establish monitoring of the NC */
2328 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2329 !GUID_all_zero(&req10->destination_dsa_guid)) {
2330 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2331 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2332 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2333 ureq.naming_context = ncRoot;
2334 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2335 &req10->destination_dsa_guid);
2336 if (!ureq.dest_dsa_dns_name) {
2337 return WERR_NOT_ENOUGH_MEMORY;
2339 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2340 ureq.options = DRSUAPI_DRS_ADD_REF |
2341 DRSUAPI_DRS_ASYNC_OP |
2342 DRSUAPI_DRS_GETCHG_CHECK;
2344 /* we also need to pass through the
2345 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2346 to send notifies using the GC SPN */
2347 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2349 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2350 if (!W_ERROR_IS_OK(werr)) {
2351 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2352 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2359 * This is just a guess, how to calculate the
2360 * number of linked attributes to send, we need to
2361 * find out how to do this right.
2363 if (r->out.ctr->ctr6.object_count >= max_links) {
2366 max_links -= r->out.ctr->ctr6.object_count;
2369 link_total = getnc_state->la_count;
2371 if (i < getnc_state->num_records) {
2372 r->out.ctr->ctr6.more_data = true;
2374 /* sort the whole array the first time */
2375 if (getnc_state->la_sorted == NULL) {
2377 struct la_for_sorting *guid_array = talloc_array(getnc_state, struct la_for_sorting, getnc_state->la_count);
2378 if (guid_array == NULL) {
2379 DEBUG(0, ("Out of memory allocating %u linked attributes for sorting", getnc_state->la_count));
2380 return WERR_NOT_ENOUGH_MEMORY;
2382 for (j = 0; j < getnc_state->la_count; j++) {
2383 /* we need to get the target GUIDs to compare */
2385 const struct drsuapi_DsReplicaLinkedAttribute *la = &getnc_state->la_list[j];
2386 const struct dsdb_attribute *schema_attrib;
2387 const struct ldb_val *target_guid;
2388 DATA_BLOB source_guid;
2389 TALLOC_CTX *frame = talloc_stackframe();
2391 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2393 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, frame, la->value.blob, &dn);
2394 if (!W_ERROR_IS_OK(werr)) {
2395 DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2400 /* Extract the target GUID in NDR form */
2401 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2402 if (target_guid == NULL
2403 || target_guid->length != sizeof(guid_array[0].target_guid)) {
2404 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2406 /* Repack the source GUID as NDR for sorting */
2407 status = GUID_to_ndr_blob(&la->identifier->guid,
2412 if (!NT_STATUS_IS_OK(status)
2413 || source_guid.length != sizeof(guid_array[0].source_guid)) {
2414 DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2416 return ntstatus_to_werror(status);
2419 guid_array[j].link = &getnc_state->la_list[j];
2420 memcpy(guid_array[j].target_guid, target_guid->data,
2421 sizeof(guid_array[j].target_guid));
2422 memcpy(guid_array[j].source_guid, source_guid.data,
2423 sizeof(guid_array[j].source_guid));
2427 LDB_TYPESAFE_QSORT(guid_array, getnc_state->la_count, NULL, linked_attribute_compare);
2428 getnc_state->la_sorted = guid_array;
2431 link_count = getnc_state->la_count - getnc_state->la_idx;
2432 link_count = MIN(max_links, link_count);
2434 r->out.ctr->ctr6.linked_attributes_count = link_count;
2435 r->out.ctr->ctr6.linked_attributes = talloc_array(r->out.ctr, struct drsuapi_DsReplicaLinkedAttribute, link_count);
2436 if (r->out.ctr->ctr6.linked_attributes == NULL) {
2437 DEBUG(0, ("Out of memory allocating %u linked attributes for output", link_count));
2438 return WERR_NOT_ENOUGH_MEMORY;
2441 for (k = 0; k < link_count; k++) {
2442 r->out.ctr->ctr6.linked_attributes[k]
2443 = *getnc_state->la_sorted[getnc_state->la_idx + k].link;
2446 getnc_state->la_idx += link_count;
2447 link_given = getnc_state->la_idx;
2449 if (getnc_state->la_idx < getnc_state->la_count) {
2450 r->out.ctr->ctr6.more_data = true;
2454 if (req10->replica_flags & DRSUAPI_DRS_GET_NC_SIZE) {
2456 * TODO: This implementation is wrong
2457 * we should find out the total number of
2458 * objects and links in the whole naming context
2459 * at the start of the cycle and return these
2460 * values in each message.
2462 * For now we keep our current strategy and return
2463 * the number of objects for this cycle and the number
2464 * of links we found so far during the cycle.
2466 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2467 r->out.ctr->ctr6.nc_linked_attributes_count = getnc_state->la_count;
2470 if (!r->out.ctr->ctr6.more_data) {
2471 talloc_steal(mem_ctx, getnc_state->la_list);
2473 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2474 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2475 &getnc_state->final_udv);
2477 talloc_free(getnc_state);
2478 b_state->getncchanges_state = NULL;
2480 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2481 &r->out.ctr->ctr6.new_highwatermark);
2484 * We need to make sure that we never return the
2485 * same highwatermark within the same replication
2486 * cycle more than once. Otherwise we cannot detect
2487 * when the client uses an unexptected highwatermark.
2489 * This is a HACK which is needed because our
2490 * object ordering is wrong and set tmp_highest_usn
2491 * to a value that is higher than what we already
2492 * sent to the client (destination dsa).
2494 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2497 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2500 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2501 r->out.ctr->ctr6.uptodateness_vector = NULL;
2502 r->out.ctr->ctr6.nc_object_count = 0;
2503 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2506 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2507 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2508 (unsigned long long)(req10->highwatermark.highest_usn+1),
2509 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2510 r->out.ctr->ctr6.object_count,
2511 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2512 r->out.ctr->ctr6.linked_attributes_count,
2513 link_given, link_total,
2514 dom_sid_string(mem_ctx, user_sid)));
2517 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2518 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);