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 = NULL;
586 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
587 bool is_critical = ldb_msg_find_attr_as_bool(msg, "isCriticalSystemObject", false);
589 if (replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
595 tmp_ctx = talloc_new(mem_ctx);
596 if (tmp_ctx == NULL) {
597 return WERR_NOT_ENOUGH_MEMORY;
600 for (i=0; i<msg->num_elements; i++) {
601 struct ldb_message_element *el = &msg->elements[i];
602 const struct dsdb_attribute *sa;
605 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
607 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
608 /* we only want forward links */
612 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
613 /* its an old style link, it will have been
614 * sent in the main replication data */
618 for (j=0; j<el->num_values; j++) {
619 struct dsdb_dn *dsdb_dn;
621 uint64_t originating_usn;
622 NTSTATUS status, status2;
624 struct GUID originating_invocation_id;
626 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
627 if (dsdb_dn == NULL) {
628 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
629 el->name, ldb_dn_get_linearized(msg->dn)));
630 talloc_free(tmp_ctx);
631 return WERR_DS_DRA_INTERNAL_ERROR;
634 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
635 if (!NT_STATUS_IS_OK(status)) {
636 /* this can happen for attributes
637 given to us with old style meta
642 if (local_usn > uSNChanged) {
643 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
644 el->name, ldb_dn_get_linearized(msg->dn)));
645 talloc_free(tmp_ctx);
646 return WERR_DS_DRA_INTERNAL_ERROR;
649 if (local_usn < highest_usn) {
653 status = dsdb_get_extended_dn_guid(dsdb_dn->dn,
654 &originating_invocation_id,
656 status2 = dsdb_get_extended_dn_uint64(dsdb_dn->dn,
658 "RMD_ORIGINATING_USN");
660 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2)) {
661 if (udv_filter(uptodateness_vector,
662 &originating_invocation_id,
668 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema,
669 sa, msg, dsdb_dn, la_list,
670 la_count, is_schema_nc);
671 if (!W_ERROR_IS_OK(werr)) {
672 talloc_free(tmp_ctx);
678 talloc_free(tmp_ctx);
683 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
685 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
686 struct ldb_dn *ncRoot_dn,
687 struct drsuapi_DsReplicaCursor2CtrEx *udv)
695 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
696 if (ret != LDB_SUCCESS) {
697 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
698 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
699 return WERR_DS_DRA_INTERNAL_ERROR;
706 /* comparison function for linked attributes - see CompareLinks() in
707 * MS-DRSR section 4.1.10.5.17 */
708 static int linked_attribute_compare(const struct la_for_sorting *la1,
709 const struct la_for_sorting *la2,
713 c = memcmp(la1->source_guid,
714 la2->source_guid, sizeof(la2->source_guid));
719 if (la1->link->attid != la2->link->attid) {
720 return la1->link->attid < la2->link->attid? -1:1;
723 if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
724 (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
725 return (la1->link->flags &
726 DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
729 return memcmp(la1->target_guid,
730 la2->target_guid, sizeof(la2->target_guid));
733 struct drsuapi_changed_objects {
740 sort the objects we send by tree order
742 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
743 struct drsuapi_changed_objects *m2,
744 struct drsuapi_getncchanges_state *getnc_state)
746 return ldb_dn_compare(m2->dn, m1->dn);
750 sort the objects we send first by uSNChanged
752 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
753 struct drsuapi_changed_objects *m2,
754 struct drsuapi_getncchanges_state *getnc_state)
758 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
763 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
768 if (m1->usn == m2->usn) {
769 return ldb_dn_compare(m2->dn, m1->dn);
772 if (m1->usn < m2->usn) {
781 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
783 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
785 struct drsuapi_DsGetNCChangesRequest10 *req10,
786 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
787 struct ldb_dn **rid_manager_dn)
789 struct ldb_dn *req_dn, *ntds_dn = NULL;
791 struct ldb_context *ldb = b_state->sam_ctx;
792 struct ldb_result *ext_res;
793 struct dsdb_fsmo_extended_op *exop;
798 - verify that the DN being asked for is the RID Manager DN
799 - verify that we are the RID Manager
802 /* work out who is the RID Manager, also return to caller */
803 ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
804 if (ret != LDB_SUCCESS) {
805 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
806 return WERR_DS_DRA_INTERNAL_ERROR;
809 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
810 if (!ldb_dn_validate(req_dn) ||
811 ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
812 /* that isn't the RID Manager DN */
813 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
814 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
815 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
819 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
820 ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
821 if (ret != LDB_SUCCESS) {
822 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
823 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
824 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
828 /* find the DN of the RID Manager */
829 ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
830 if (ret != LDB_SUCCESS) {
831 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
832 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
833 return WERR_DS_DRA_INTERNAL_ERROR;
837 /* we're not the RID Manager - go away */
838 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
839 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
843 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
844 W_ERROR_HAVE_NO_MEMORY(exop);
846 exop->fsmo_info = req10->fsmo_info;
847 exop->destination_dsa_guid = req10->destination_dsa_guid;
849 ret = ldb_transaction_start(ldb);
850 if (ret != LDB_SUCCESS) {
851 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
852 ldb_errstring(ldb)));
853 return WERR_DS_DRA_INTERNAL_ERROR;
856 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
857 if (ret != LDB_SUCCESS) {
858 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
859 ldb_errstring(ldb)));
860 ldb_transaction_cancel(ldb);
861 return WERR_DS_DRA_INTERNAL_ERROR;
864 ret = ldb_transaction_commit(ldb);
865 if (ret != LDB_SUCCESS) {
866 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
867 ldb_errstring(ldb)));
868 return WERR_DS_DRA_INTERNAL_ERROR;
871 talloc_free(ext_res);
873 DEBUG(2,("Allocated RID pool for server %s\n",
874 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
876 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
882 return an array of SIDs from a ldb_message given an attribute name
883 assumes the SIDs are in extended DN format
885 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
886 struct ldb_message *msg,
889 const struct dom_sid ***sids)
891 struct ldb_message_element *el;
894 el = ldb_msg_find_element(msg, attr);
900 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
901 W_ERROR_HAVE_NO_MEMORY(*sids);
903 for (i=0; i<el->num_values; i++) {
904 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
908 sid = talloc(*sids, struct dom_sid);
909 W_ERROR_HAVE_NO_MEMORY(sid);
910 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
911 if (!NT_STATUS_IS_OK(status)) {
912 return WERR_INTERNAL_DB_CORRUPTION;
923 return an array of SIDs from a ldb_message given an attribute name
924 assumes the SIDs are in NDR form
926 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
927 struct ldb_message *msg,
930 const struct dom_sid ***sids)
932 struct ldb_message_element *el;
935 el = ldb_msg_find_element(msg, attr);
941 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
942 W_ERROR_HAVE_NO_MEMORY(*sids);
944 for (i=0; i<el->num_values; i++) {
945 enum ndr_err_code ndr_err;
948 sid = talloc(*sids, struct dom_sid);
949 W_ERROR_HAVE_NO_MEMORY(sid);
951 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
952 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
953 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
954 return WERR_INTERNAL_DB_CORRUPTION;
964 see if any SIDs in list1 are in list2
966 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
969 /* do we ever have enough SIDs here to worry about O(n^2) ? */
970 for (i=0; list1[i]; i++) {
971 for (j=0; list2[j]; j++) {
972 if (dom_sid_equal(list1[i], list2[j])) {
981 handle a DRSUAPI_EXOP_REPL_SECRET call
983 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
985 struct drsuapi_DsGetNCChangesRequest10 *req10,
986 struct dom_sid *user_sid,
987 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
988 bool has_get_all_changes)
990 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
991 struct ldb_dn *obj_dn = NULL;
992 struct ldb_dn *rodc_dn, *krbtgt_link_dn;
994 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
995 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
996 struct ldb_result *rodc_res, *obj_res;
997 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
1000 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
1001 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1004 * we need to work out if we will allow this DC to
1005 * replicate the secrets for this object
1007 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
1011 if (b_state->sam_ctx_system == NULL) {
1012 /* this operation needs system level access */
1013 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
1014 return WERR_DS_DRA_SOURCE_DISABLED;
1018 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
1020 * The pseudo code indicate
1021 * revealsecrets = true
1022 * if IsRevealSecretRequest(msgIn) then
1023 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
1025 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
1026 * <... check if this account is ok to be replicated on this DC ...>
1027 * <... and if not reveal secrets = no ...>
1029 * reveal secrets = false
1034 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
1035 * then you can do EXOP_REPL_SECRETS
1037 if (has_get_all_changes) {
1041 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
1042 if (!ldb_dn_validate(obj_dn)) goto failed;
1044 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1045 dom_sid_string(mem_ctx, user_sid));
1046 if (!ldb_dn_validate(rodc_dn)) goto failed;
1048 /* do the two searches we need */
1049 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1050 DSDB_SEARCH_SHOW_EXTENDED_DN);
1051 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1053 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1054 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1056 /* if the object SID is equal to the user_sid, allow */
1057 if (dom_sid_equal(user_sid,
1058 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1062 /* an RODC is allowed to get its own krbtgt account secrets */
1063 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1064 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1065 if (krbtgt_link_dn != NULL &&
1066 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1070 /* but it isn't allowed to get anyone elses krbtgt secrets */
1071 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1072 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1076 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1077 "userAccountControl", 0) &
1078 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1082 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1083 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1084 if (!W_ERROR_IS_OK(werr)) {
1088 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1089 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1090 if (!W_ERROR_IS_OK(werr)) {
1094 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1095 mem_ctx, "tokenGroups", &token_sids);
1096 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1100 if (never_reveal_sids &&
1101 sid_list_match(token_sids, never_reveal_sids)) {
1106 sid_list_match(token_sids, reveal_sids)) {
1112 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1113 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1114 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1115 return WERR_DS_DRA_ACCESS_DENIED;
1118 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1119 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1120 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1121 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1122 req10->highwatermark.highest_usn = 0;
1126 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1127 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1128 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1129 return WERR_DS_DRA_BAD_DN;
1134 handle a DRSUAPI_EXOP_REPL_OBJ call
1136 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1137 TALLOC_CTX *mem_ctx,
1138 struct drsuapi_DsGetNCChangesRequest10 *req10,
1139 struct dom_sid *user_sid,
1140 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1142 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1144 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1145 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1147 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1153 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1154 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1155 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1157 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1158 TALLOC_CTX *mem_ctx,
1159 struct drsuapi_DsGetNCChangesRequest10 *req10,
1160 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1162 struct ldb_dn *req_dn, *ntds_dn;
1165 struct ldb_context *ldb = b_state->sam_ctx;
1166 struct ldb_message *msg;
1171 - verify that the client dn exists
1172 - verify that we are the current master
1175 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1176 if (!ldb_dn_validate(req_dn)) {
1177 /* that is not a valid dn */
1178 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1179 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1180 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1184 /* find the DN of the current role owner */
1185 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1186 if (ret != LDB_SUCCESS) {
1187 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1188 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1189 return WERR_DS_DRA_INTERNAL_ERROR;
1193 /* we're not the RID Manager or role owner - go away */
1194 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1195 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1199 /* change the current master */
1200 msg = ldb_msg_new(ldb);
1201 W_ERROR_HAVE_NO_MEMORY(msg);
1202 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1203 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1205 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1206 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1207 if (ret != LDB_SUCCESS) {
1208 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1209 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1211 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1215 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1218 return WERR_DS_DRA_INTERNAL_ERROR;
1221 for (i=0;i<msg->num_elements;i++) {
1222 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1225 ret = ldb_transaction_start(ldb);
1226 if (ret != LDB_SUCCESS) {
1227 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1228 ldb_errstring(ldb)));
1229 return WERR_DS_DRA_INTERNAL_ERROR;
1232 ret = ldb_modify(ldb, msg);
1233 if (ret != LDB_SUCCESS) {
1234 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1235 ldb_errstring(ldb)));
1236 ldb_transaction_cancel(ldb);
1237 return WERR_DS_DRA_INTERNAL_ERROR;
1240 ret = ldb_transaction_commit(ldb);
1241 if (ret != LDB_SUCCESS) {
1242 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1243 ldb_errstring(ldb)));
1244 return WERR_DS_DRA_INTERNAL_ERROR;
1247 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1253 see if this getncchanges request includes a request to reveal secret information
1255 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1256 struct drsuapi_DsGetNCChangesRequest10 *req10,
1257 struct dsdb_schema_prefixmap *pfm_remote,
1258 bool *is_secret_request)
1260 enum drsuapi_DsExtendedOperation exop;
1262 struct dsdb_schema *schema;
1263 struct dsdb_syntax_ctx syntax_ctx;
1265 *is_secret_request = true;
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 /* FSMO exops can reveal secrets */
1276 *is_secret_request = true;
1278 case DRSUAPI_EXOP_REPL_SECRET:
1279 case DRSUAPI_EXOP_REPL_OBJ:
1280 case DRSUAPI_EXOP_NONE:
1284 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1285 *is_secret_request = false;
1289 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1290 req10->partial_attribute_set == NULL) {
1291 /* they want secrets */
1292 *is_secret_request = true;
1296 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1297 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1298 syntax_ctx.pfm_remote = pfm_remote;
1300 /* check the attributes they asked for */
1301 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1302 const struct dsdb_attribute *sa;
1303 WERROR werr = getncchanges_attid_remote_to_local(schema,
1305 req10->partial_attribute_set->attids[i],
1309 if (!W_ERROR_IS_OK(werr)) {
1310 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1311 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1315 if (!dsdb_attr_in_rodc_fas(sa)) {
1316 *is_secret_request = true;
1321 if (req10->partial_attribute_set_ex) {
1322 /* check the extended attributes they asked for */
1323 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1324 const struct dsdb_attribute *sa;
1325 WERROR werr = getncchanges_attid_remote_to_local(schema,
1327 req10->partial_attribute_set_ex->attids[i],
1331 if (!W_ERROR_IS_OK(werr)) {
1332 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1333 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1337 if (!dsdb_attr_in_rodc_fas(sa)) {
1338 *is_secret_request = true;
1344 *is_secret_request = false;
1349 see if this getncchanges request is only for attributes in the GC
1350 partial attribute set
1352 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1353 struct drsuapi_DsGetNCChangesRequest10 *req10,
1354 struct dsdb_schema_prefixmap *pfm_remote,
1355 bool *is_gc_pas_request)
1357 enum drsuapi_DsExtendedOperation exop;
1359 struct dsdb_schema *schema;
1360 struct dsdb_syntax_ctx syntax_ctx;
1362 exop = req10->extended_op;
1365 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1366 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1367 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1368 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1369 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1370 case DRSUAPI_EXOP_REPL_SECRET:
1371 *is_gc_pas_request = false;
1373 case DRSUAPI_EXOP_REPL_OBJ:
1374 case DRSUAPI_EXOP_NONE:
1378 if (req10->partial_attribute_set == NULL) {
1379 /* they want it all */
1380 *is_gc_pas_request = false;
1384 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1385 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1386 syntax_ctx.pfm_remote = pfm_remote;
1388 /* check the attributes they asked for */
1389 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1390 const struct dsdb_attribute *sa;
1391 WERROR werr = getncchanges_attid_remote_to_local(schema,
1393 req10->partial_attribute_set->attids[i],
1397 if (!W_ERROR_IS_OK(werr)) {
1398 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1399 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1403 if (!sa->isMemberOfPartialAttributeSet) {
1404 *is_gc_pas_request = false;
1409 if (req10->partial_attribute_set_ex) {
1410 /* check the extended attributes they asked for */
1411 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1412 const struct dsdb_attribute *sa;
1413 WERROR werr = getncchanges_attid_remote_to_local(schema,
1415 req10->partial_attribute_set_ex->attids[i],
1419 if (!W_ERROR_IS_OK(werr)) {
1420 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1421 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1425 if (!sa->isMemberOfPartialAttributeSet) {
1426 *is_gc_pas_request = false;
1432 *is_gc_pas_request = true;
1438 map from req8 to req10
1440 static struct drsuapi_DsGetNCChangesRequest10 *
1441 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1442 struct drsuapi_DsGetNCChangesRequest8 *req8)
1444 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1445 struct drsuapi_DsGetNCChangesRequest10);
1446 if (req10 == NULL) {
1450 req10->destination_dsa_guid = req8->destination_dsa_guid;
1451 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1452 req10->naming_context = req8->naming_context;
1453 req10->highwatermark = req8->highwatermark;
1454 req10->uptodateness_vector = req8->uptodateness_vector;
1455 req10->replica_flags = req8->replica_flags;
1456 req10->max_object_count = req8->max_object_count;
1457 req10->max_ndr_size = req8->max_ndr_size;
1458 req10->extended_op = req8->extended_op;
1459 req10->fsmo_info = req8->fsmo_info;
1460 req10->partial_attribute_set = req8->partial_attribute_set;
1461 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1462 req10->mapping_ctr = req8->mapping_ctr;
1467 static const char *collect_objects_attrs[] = { "uSNChanged",
1472 * Collects object for normal replication cycle.
1474 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1475 TALLOC_CTX *mem_ctx,
1476 struct drsuapi_DsGetNCChangesRequest10 *req10,
1477 struct ldb_dn *search_dn,
1478 const char *extra_filter,
1479 struct ldb_result **search_res)
1482 char* search_filter;
1483 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1484 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1485 bool critical_only = false;
1487 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1488 critical_only = true;
1491 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1492 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1493 scope = LDB_SCOPE_BASE;
1494 critical_only = false;
1497 /* Construct response. */
1498 search_filter = talloc_asprintf(mem_ctx,
1499 "(uSNChanged>=%llu)",
1500 (unsigned long long)(getnc_state->min_usn+1));
1503 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1506 if (critical_only) {
1507 search_filter = talloc_asprintf(mem_ctx,
1508 "(&%s(isCriticalSystemObject=TRUE))",
1512 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1513 scope = LDB_SCOPE_BASE;
1517 search_dn = getnc_state->ncRoot_dn;
1520 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1521 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1522 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1524 collect_objects_attrs,
1526 if (ret != LDB_SUCCESS) {
1527 return WERR_DS_DRA_INTERNAL_ERROR;
1534 * Collects object for normal replication cycle.
1536 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1537 TALLOC_CTX *mem_ctx,
1538 struct drsuapi_DsGetNCChangesRequest10 *req10,
1539 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1540 struct ldb_dn *search_dn,
1541 const char *extra_filter,
1542 struct ldb_result **search_res)
1544 /* we have nothing to do in case of ex-op failure */
1545 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1549 switch (req10->extended_op) {
1550 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1553 struct ldb_dn *ntds_dn = NULL;
1554 struct ldb_dn *server_dn = NULL;
1555 struct ldb_dn *machine_dn = NULL;
1556 struct ldb_dn *rid_set_dn = NULL;
1557 struct ldb_result *search_res2 = NULL;
1558 struct ldb_result *search_res3 = NULL;
1559 TALLOC_CTX *frame = talloc_stackframe();
1560 /* get RID manager, RID set and server DN (in that order) */
1562 /* This first search will get the RID Manager */
1563 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1565 search_dn, LDB_SCOPE_BASE,
1566 collect_objects_attrs,
1568 if (ret != LDB_SUCCESS) {
1569 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1570 ldb_dn_get_linearized(search_dn),
1571 ldb_errstring(b_state->sam_ctx)));
1573 return WERR_DS_DRA_INTERNAL_ERROR;
1576 if ((*search_res)->count != 1) {
1577 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1578 ldb_dn_get_linearized(search_dn),
1579 (*search_res)->count));
1581 return WERR_DS_DRA_INTERNAL_ERROR;
1584 /* Now extend it to the RID set */
1586 /* Find the computer account DN for the destination
1587 * dsa GUID specified */
1589 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1590 &req10->destination_dsa_guid, 0,
1592 if (ret != LDB_SUCCESS) {
1593 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1595 &req10->destination_dsa_guid),
1596 ldb_errstring(b_state->sam_ctx)));
1598 return WERR_DS_DRA_INTERNAL_ERROR;
1601 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1604 return WERR_DS_DRA_INTERNAL_ERROR;
1607 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1608 "serverReference", &machine_dn);
1609 if (ret != LDB_SUCCESS) {
1610 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1611 ldb_dn_get_linearized(server_dn),
1612 ldb_errstring(b_state->sam_ctx)));
1614 return WERR_DS_DRA_INTERNAL_ERROR;
1617 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1618 "rIDSetReferences", &rid_set_dn);
1619 if (ret != LDB_SUCCESS) {
1620 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1621 ldb_dn_get_linearized(server_dn),
1622 ldb_errstring(b_state->sam_ctx)));
1624 return WERR_DS_DRA_INTERNAL_ERROR;
1628 /* This first search will get the RID Manager, now get the RID set */
1629 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1631 rid_set_dn, LDB_SCOPE_BASE,
1632 collect_objects_attrs,
1634 if (ret != LDB_SUCCESS) {
1635 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1636 ldb_dn_get_linearized(rid_set_dn),
1637 ldb_errstring(b_state->sam_ctx)));
1639 return WERR_DS_DRA_INTERNAL_ERROR;
1642 if (search_res2->count != 1) {
1643 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1644 ldb_dn_get_linearized(rid_set_dn),
1645 search_res2->count));
1647 return WERR_DS_DRA_INTERNAL_ERROR;
1650 /* Finally get the server DN */
1651 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1653 machine_dn, LDB_SCOPE_BASE,
1654 collect_objects_attrs,
1656 if (ret != LDB_SUCCESS) {
1657 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1658 ldb_dn_get_linearized(server_dn),
1659 ldb_errstring(b_state->sam_ctx)));
1661 return WERR_DS_DRA_INTERNAL_ERROR;
1664 if (search_res3->count != 1) {
1665 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1666 ldb_dn_get_linearized(server_dn),
1667 search_res3->count));
1669 return WERR_DS_DRA_INTERNAL_ERROR;
1672 /* Now extend the original search_res with these answers */
1673 (*search_res)->count = 3;
1675 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1676 struct ldb_message *,
1677 (*search_res)->count);
1678 if ((*search_res)->msgs == NULL) {
1680 return WERR_NOT_ENOUGH_MEMORY;
1684 talloc_steal(mem_ctx, *search_res);
1685 (*search_res)->msgs[1] =
1686 talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1687 (*search_res)->msgs[2] =
1688 talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1694 /* TODO: implement extended op specific collection
1695 * of objects. Right now we just normal procedure
1696 * for collecting objects */
1697 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1702 drsuapi_DsGetNCChanges
1704 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1706 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1707 struct drsuapi_DsGetNCChanges *r)
1709 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1712 struct dsdb_schema *schema;
1713 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1714 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1716 DATA_BLOB session_key;
1718 struct dcesrv_handle *h;
1719 struct drsuapi_bind_state *b_state;
1720 struct drsuapi_getncchanges_state *getnc_state;
1721 struct drsuapi_DsGetNCChangesRequest10 *req10;
1723 uint32_t max_objects;
1725 uint32_t link_count = 0;
1726 uint32_t link_total = 0;
1727 uint32_t link_given = 0;
1728 struct ldb_dn *search_dn = NULL;
1729 bool am_rodc, null_scope=false;
1730 enum security_user_level security_level;
1731 struct ldb_context *sam_ctx;
1732 struct dom_sid *user_sid;
1733 bool is_secret_request;
1734 bool is_gc_pas_request;
1735 struct drsuapi_changed_objects *changes;
1737 time_t start = time(NULL);
1738 bool max_wait_reached = false;
1739 bool has_get_all_changes = false;
1740 struct GUID invocation_id;
1741 static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
1742 struct dsdb_schema_prefixmap *pfm_remote = NULL;
1744 uint32_t *local_pas = NULL;
1746 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1749 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1751 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1753 *r->out.level_out = 6;
1754 /* TODO: linked attributes*/
1755 r->out.ctr->ctr6.linked_attributes_count = 0;
1756 r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
1758 r->out.ctr->ctr6.object_count = 0;
1759 r->out.ctr->ctr6.nc_object_count = 0;
1760 r->out.ctr->ctr6.more_data = false;
1761 r->out.ctr->ctr6.uptodateness_vector = NULL;
1762 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1763 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1764 r->out.ctr->ctr6.first_object = NULL;
1766 /* a RODC doesn't allow for any replication */
1767 ret = samdb_rodc(sam_ctx, &am_rodc);
1768 if (ret == LDB_SUCCESS && am_rodc) {
1769 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1770 return WERR_DS_DRA_SOURCE_DISABLED;
1773 /* Check request revision.
1775 switch (r->in.level) {
1777 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1778 if (req10 == NULL) {
1779 return WERR_NOT_ENOUGH_MEMORY;
1783 req10 = &r->in.req->req10;
1786 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1788 return WERR_REVISION_MISMATCH;
1792 /* Perform access checks. */
1793 /* TODO: we need to support a sync on a specific non-root
1794 * DN. We'll need to find the real partition root here */
1795 ncRoot = req10->naming_context;
1796 if (ncRoot == NULL) {
1797 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1798 return WERR_DS_DRA_INVALID_PARAMETER;
1801 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1802 return WERR_DS_DRA_INTERNAL_ERROR;
1805 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1806 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1807 return WERR_DS_DRA_SOURCE_DISABLED;
1810 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1812 /* all clients must have GUID_DRS_GET_CHANGES */
1813 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1815 dce_call->conn->auth_state.session_info->security_token,
1816 req10->naming_context,
1817 GUID_DRS_GET_CHANGES);
1818 if (!W_ERROR_IS_OK(werr)) {
1822 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
1823 full = req10->partial_attribute_set == NULL &&
1824 req10->partial_attribute_set_ex == NULL;
1826 full = (options & DRSUAPI_DRS_WRIT_REP) != 0;
1829 werr = dsdb_schema_pfm_from_drsuapi_pfm(&req10->mapping_ctr, true,
1830 mem_ctx, &pfm_remote, NULL);
1832 /* We were supplied a partial attribute set, without the prefix map! */
1833 if (!full && !W_ERROR_IS_OK(werr)) {
1834 if (req10->mapping_ctr.num_mappings == 0) {
1836 * Despite the fact MS-DRSR specifies that this shouldn't
1837 * happen, Windows RODCs will in fact not provide a prefixMap.
1839 DEBUG(5,(__location__ ": Failed to provide a remote prefixMap,"
1840 " falling back to local prefixMap\n"));
1842 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
1848 /* allowed if the GC PAS and client has
1849 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1850 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, pfm_remote, &is_gc_pas_request);
1851 if (!W_ERROR_IS_OK(werr)) {
1854 if (is_gc_pas_request) {
1855 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1857 dce_call->conn->auth_state.session_info->security_token,
1858 req10->naming_context,
1859 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1860 if (W_ERROR_IS_OK(werr)) {
1865 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10,
1867 &is_secret_request);
1868 if (!W_ERROR_IS_OK(werr)) {
1871 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1872 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1874 dce_call->conn->auth_state.session_info->security_token,
1875 req10->naming_context,
1876 GUID_DRS_GET_ALL_CHANGES);
1877 if (!W_ERROR_IS_OK(werr)) {
1880 has_get_all_changes = true;
1885 /* for non-administrator replications, check that they have
1886 given the correct source_dsa_invocation_id */
1887 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1888 samdb_domain_sid(sam_ctx));
1889 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1890 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1891 /* we rely on this flag being unset for RODC requests */
1892 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1896 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1897 /* Ignore the _in_ uptpdateness vector*/
1898 req10->uptodateness_vector = NULL;
1901 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1902 req10->source_dsa_invocation_id = invocation_id;
1905 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1907 * The given highwatermark is only valid relative to the
1908 * specified source_dsa_invocation_id.
1910 ZERO_STRUCT(req10->highwatermark);
1913 getnc_state = b_state->getncchanges_state;
1915 /* see if a previous replication has been abandoned */
1917 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1918 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1919 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1920 ldb_dn_get_linearized(new_dn),
1921 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1922 ldb_dn_get_linearized(getnc_state->last_dn)));
1923 talloc_free(getnc_state);
1929 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1930 &req10->highwatermark);
1932 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1933 "on DN %s %s highwatermark (last_dn %s)\n",
1934 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1935 (ret > 0) ? "older" : "newer",
1936 ldb_dn_get_linearized(getnc_state->last_dn)));
1937 talloc_free(getnc_state);
1942 if (getnc_state == NULL) {
1943 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1944 if (getnc_state == NULL) {
1945 return WERR_NOT_ENOUGH_MEMORY;
1947 b_state->getncchanges_state = getnc_state;
1948 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1949 if (getnc_state->ncRoot_dn == NULL) {
1950 return WERR_NOT_ENOUGH_MEMORY;
1953 ret = dsdb_find_guid_by_dn(b_state->sam_ctx_system,
1954 getnc_state->ncRoot_dn,
1955 &getnc_state->ncRoot_guid);
1956 if (ret != LDB_SUCCESS) {
1957 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1958 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1959 return WERR_DS_DRA_INTERNAL_ERROR;
1961 ncRoot->guid = getnc_state->ncRoot_guid;
1963 /* find out if we are to replicate Schema NC */
1964 ret = ldb_dn_compare_base(ldb_get_schema_basedn(b_state->sam_ctx),
1965 getnc_state->ncRoot_dn);
1967 getnc_state->is_schema_nc = (0 == ret);
1969 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1970 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1974 * This is the first replication cycle and it is
1975 * a good place to handle extended operations
1977 * FIXME: we don't fully support extended operations yet
1979 switch (req10->extended_op) {
1980 case DRSUAPI_EXOP_NONE:
1982 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1983 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
1984 W_ERROR_NOT_OK_RETURN(werr);
1985 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1989 case DRSUAPI_EXOP_REPL_SECRET:
1990 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1993 has_get_all_changes);
1994 r->out.result = werr;
1995 W_ERROR_NOT_OK_RETURN(werr);
1997 case DRSUAPI_EXOP_FSMO_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_RID_REQ_ROLE:
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_FSMO_REQ_PDC:
2012 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2013 W_ERROR_NOT_OK_RETURN(werr);
2014 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2018 case DRSUAPI_EXOP_REPL_OBJ:
2019 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
2020 r->out.result = werr;
2021 W_ERROR_NOT_OK_RETURN(werr);
2024 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
2026 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
2027 (unsigned)req10->extended_op));
2028 return WERR_DS_DRA_NOT_SUPPORTED;
2032 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
2033 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
2034 DEBUG(0,(__location__ ": Bad DN '%s'\n",
2035 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
2036 return WERR_DS_DRA_INVALID_PARAMETER;
2039 ncRoot->guid = getnc_state->ncRoot_guid;
2041 /* we need the session key for encrypting password attributes */
2042 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
2043 if (!NT_STATUS_IS_OK(status)) {
2044 DEBUG(0,(__location__ ": Failed to get session key\n"));
2045 return WERR_DS_DRA_INTERNAL_ERROR;
2049 TODO: MS-DRSR section 4.1.10.1.1
2050 Work out if this is the start of a new cycle */
2052 if (getnc_state->guids == NULL) {
2053 const char *extra_filter;
2054 struct ldb_result *search_res = NULL;
2056 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
2058 getnc_state->min_usn = req10->highwatermark.highest_usn;
2059 getnc_state->max_usn = getnc_state->min_usn;
2061 getnc_state->final_udv = talloc_zero(getnc_state,
2062 struct drsuapi_DsReplicaCursor2CtrEx);
2063 if (getnc_state->final_udv == NULL) {
2064 return WERR_NOT_ENOUGH_MEMORY;
2066 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
2067 getnc_state->final_udv);
2068 if (!W_ERROR_IS_OK(werr)) {
2072 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2073 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
2074 search_dn, extra_filter,
2077 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
2079 search_dn, extra_filter,
2082 W_ERROR_NOT_OK_RETURN(werr);
2084 /* extract out the GUIDs list */
2085 getnc_state->num_records = search_res ? search_res->count : 0;
2086 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
2087 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
2089 changes = talloc_array(getnc_state,
2090 struct drsuapi_changed_objects,
2091 getnc_state->num_records);
2092 W_ERROR_HAVE_NO_MEMORY(changes);
2094 for (i=0; i<getnc_state->num_records; i++) {
2095 changes[i].dn = search_res->msgs[i]->dn;
2096 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
2097 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
2099 if (changes[i].usn > getnc_state->max_usn) {
2100 getnc_state->max_usn = changes[i].usn;
2104 /* RID_ALLOC returns 3 objects in a fixed order */
2105 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
2107 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
2108 LDB_TYPESAFE_QSORT(changes,
2109 getnc_state->num_records,
2111 site_res_cmp_anc_order);
2113 LDB_TYPESAFE_QSORT(changes,
2114 getnc_state->num_records,
2116 site_res_cmp_usn_order);
2119 for (i=0; i < getnc_state->num_records; i++) {
2120 getnc_state->guids[i] = changes[i].guid;
2121 if (GUID_all_zero(&getnc_state->guids[i])) {
2122 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
2123 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
2124 return WERR_DS_DRA_INTERNAL_ERROR;
2128 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
2129 getnc_state->final_hwm.reserved_usn = 0;
2130 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
2132 talloc_free(search_res);
2133 talloc_free(changes);
2136 if (req10->uptodateness_vector) {
2137 /* make sure its sorted */
2138 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
2139 req10->uptodateness_vector->count,
2140 drsuapi_DsReplicaCursor_compare);
2143 /* Prefix mapping */
2144 schema = dsdb_get_schema(sam_ctx, mem_ctx);
2146 DEBUG(0,("No schema in sam_ctx\n"));
2147 return WERR_DS_DRA_INTERNAL_ERROR;
2150 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
2151 if (r->out.ctr->ctr6.naming_context == NULL) {
2152 return WERR_NOT_ENOUGH_MEMORY;
2154 *r->out.ctr->ctr6.naming_context = *ncRoot;
2156 /* find the SID if there is one */
2157 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2159 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2160 r->out.ctr->ctr6.mapping_ctr = *ctr;
2162 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2163 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2165 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2166 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2168 currentObject = &r->out.ctr->ctr6.first_object;
2170 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2172 * The client control here only applies in normal replication, not extended
2173 * operations, which return a fixed set, even if the caller
2174 * sets max_object_count == 0
2176 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2177 /* use this to force single objects at a time, which is useful
2178 * for working out what object is giving problems
2180 if (req10->max_object_count < max_objects) {
2181 max_objects = req10->max_object_count;
2185 * TODO: work out how the maximum should be calculated
2187 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2190 * Maximum time that we can spend in a getncchanges
2191 * in order to avoid timeout of the other part.
2192 * 10 seconds by default.
2194 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2196 if (req10->partial_attribute_set != NULL) {
2197 struct dsdb_syntax_ctx syntax_ctx;
2200 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
2201 syntax_ctx.pfm_remote = pfm_remote;
2203 local_pas = talloc_array(b_state, uint32_t, req10->partial_attribute_set->num_attids);
2205 for (j = 0; j < req10->partial_attribute_set->num_attids; j++) {
2206 getncchanges_attid_remote_to_local(schema,
2208 req10->partial_attribute_set->attids[j],
2209 (enum drsuapi_DsAttributeId *)&local_pas[j],
2213 LDB_TYPESAFE_QSORT(local_pas,
2214 req10->partial_attribute_set->num_attids,
2219 for (i=getnc_state->num_processed;
2220 i<getnc_state->num_records &&
2222 (r->out.ctr->ctr6.object_count < max_objects)
2223 && !max_wait_reached;
2226 struct drsuapi_DsReplicaObjectListItemEx *obj;
2227 struct ldb_message *msg;
2228 static const char * const msg_attrs[] = {
2230 "nTSecurityDescriptor",
2232 "replPropertyMetaData",
2233 DSDB_SECRET_ATTRIBUTES,
2235 struct ldb_result *msg_res;
2236 struct ldb_dn *msg_dn;
2238 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2239 W_ERROR_HAVE_NO_MEMORY(obj);
2241 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2242 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2245 /* by re-searching here we avoid having a lot of full
2246 * records in memory between calls to getncchanges
2248 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2250 LDB_SCOPE_BASE, msg_attrs, NULL);
2251 if (ret != LDB_SUCCESS) {
2252 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2253 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2254 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2260 msg = msg_res->msgs[0];
2262 max_wait_reached = (time(NULL) - start > max_wait);
2264 werr = get_nc_changes_build_object(obj, msg,
2265 sam_ctx, getnc_state->ncRoot_dn,
2266 getnc_state->is_schema_nc,
2267 schema, &session_key, getnc_state->min_usn,
2268 req10->replica_flags,
2269 req10->partial_attribute_set,
2270 req10->uptodateness_vector,
2274 if (!W_ERROR_IS_OK(werr)) {
2278 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2279 getnc_state->ncRoot_dn,
2280 getnc_state->is_schema_nc,
2281 schema, getnc_state->min_usn,
2282 req10->replica_flags,
2284 &getnc_state->la_list,
2285 &getnc_state->la_count,
2286 req10->uptodateness_vector);
2287 if (!W_ERROR_IS_OK(werr)) {
2291 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
2292 if (uSN > getnc_state->max_usn) {
2294 * Only report the max_usn we had at the start
2295 * of the replication cycle.
2297 * If this object has changed lately we better
2298 * let the destination dsa refetch the change.
2299 * This is better than the risk of loosing some
2300 * objects or linked attributes.
2304 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
2305 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
2306 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
2309 if (obj->meta_data_ctr == NULL) {
2310 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2311 ldb_dn_get_linearized(msg->dn)));
2312 /* no attributes to send */
2317 r->out.ctr->ctr6.object_count++;
2319 *currentObject = obj;
2320 currentObject = &obj->next_object;
2322 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
2324 talloc_free(getnc_state->last_dn);
2325 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
2327 talloc_free(msg_res);
2328 talloc_free(msg_dn);
2331 getnc_state->num_processed = i;
2333 /* the client can us to call UpdateRefs on its behalf to
2334 re-establish monitoring of the NC */
2335 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2336 !GUID_all_zero(&req10->destination_dsa_guid)) {
2337 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2338 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2339 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2340 ureq.naming_context = ncRoot;
2341 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2342 &req10->destination_dsa_guid);
2343 if (!ureq.dest_dsa_dns_name) {
2344 return WERR_NOT_ENOUGH_MEMORY;
2346 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2347 ureq.options = DRSUAPI_DRS_ADD_REF |
2348 DRSUAPI_DRS_ASYNC_OP |
2349 DRSUAPI_DRS_GETCHG_CHECK;
2351 /* we also need to pass through the
2352 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2353 to send notifies using the GC SPN */
2354 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2356 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2357 if (!W_ERROR_IS_OK(werr)) {
2358 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2359 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2366 * This is just a guess, how to calculate the
2367 * number of linked attributes to send, we need to
2368 * find out how to do this right.
2370 if (r->out.ctr->ctr6.object_count >= max_links) {
2373 max_links -= r->out.ctr->ctr6.object_count;
2376 link_total = getnc_state->la_count;
2378 if (i < getnc_state->num_records) {
2379 r->out.ctr->ctr6.more_data = true;
2381 /* sort the whole array the first time */
2382 if (getnc_state->la_sorted == NULL) {
2384 struct la_for_sorting *guid_array = talloc_array(getnc_state, struct la_for_sorting, getnc_state->la_count);
2385 if (guid_array == NULL) {
2386 DEBUG(0, ("Out of memory allocating %u linked attributes for sorting", getnc_state->la_count));
2387 return WERR_NOT_ENOUGH_MEMORY;
2389 for (j = 0; j < getnc_state->la_count; j++) {
2390 /* we need to get the target GUIDs to compare */
2392 const struct drsuapi_DsReplicaLinkedAttribute *la = &getnc_state->la_list[j];
2393 const struct dsdb_attribute *schema_attrib;
2394 const struct ldb_val *target_guid;
2395 DATA_BLOB source_guid;
2396 TALLOC_CTX *frame = talloc_stackframe();
2398 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2400 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, frame, la->value.blob, &dn);
2401 if (!W_ERROR_IS_OK(werr)) {
2402 DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2407 /* Extract the target GUID in NDR form */
2408 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2409 if (target_guid == NULL
2410 || target_guid->length != sizeof(guid_array[0].target_guid)) {
2411 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2413 /* Repack the source GUID as NDR for sorting */
2414 status = GUID_to_ndr_blob(&la->identifier->guid,
2419 if (!NT_STATUS_IS_OK(status)
2420 || source_guid.length != sizeof(guid_array[0].source_guid)) {
2421 DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2423 return ntstatus_to_werror(status);
2426 guid_array[j].link = &getnc_state->la_list[j];
2427 memcpy(guid_array[j].target_guid, target_guid->data,
2428 sizeof(guid_array[j].target_guid));
2429 memcpy(guid_array[j].source_guid, source_guid.data,
2430 sizeof(guid_array[j].source_guid));
2434 LDB_TYPESAFE_QSORT(guid_array, getnc_state->la_count, NULL, linked_attribute_compare);
2435 getnc_state->la_sorted = guid_array;
2438 link_count = getnc_state->la_count - getnc_state->la_idx;
2439 link_count = MIN(max_links, link_count);
2441 r->out.ctr->ctr6.linked_attributes_count = link_count;
2442 r->out.ctr->ctr6.linked_attributes = talloc_array(r->out.ctr, struct drsuapi_DsReplicaLinkedAttribute, link_count);
2443 if (r->out.ctr->ctr6.linked_attributes == NULL) {
2444 DEBUG(0, ("Out of memory allocating %u linked attributes for output", link_count));
2445 return WERR_NOT_ENOUGH_MEMORY;
2448 for (k = 0; k < link_count; k++) {
2449 r->out.ctr->ctr6.linked_attributes[k]
2450 = *getnc_state->la_sorted[getnc_state->la_idx + k].link;
2453 getnc_state->la_idx += link_count;
2454 link_given = getnc_state->la_idx;
2456 if (getnc_state->la_idx < getnc_state->la_count) {
2457 r->out.ctr->ctr6.more_data = true;
2461 if (req10->replica_flags & DRSUAPI_DRS_GET_NC_SIZE) {
2463 * TODO: This implementation is wrong
2464 * we should find out the total number of
2465 * objects and links in the whole naming context
2466 * at the start of the cycle and return these
2467 * values in each message.
2469 * For now we keep our current strategy and return
2470 * the number of objects for this cycle and the number
2471 * of links we found so far during the cycle.
2473 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2474 r->out.ctr->ctr6.nc_linked_attributes_count = getnc_state->la_count;
2477 if (!r->out.ctr->ctr6.more_data) {
2478 talloc_steal(mem_ctx, getnc_state->la_list);
2480 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2481 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2482 &getnc_state->final_udv);
2484 talloc_free(getnc_state);
2485 b_state->getncchanges_state = NULL;
2487 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2488 &r->out.ctr->ctr6.new_highwatermark);
2491 * We need to make sure that we never return the
2492 * same highwatermark within the same replication
2493 * cycle more than once. Otherwise we cannot detect
2494 * when the client uses an unexptected highwatermark.
2496 * This is a HACK which is needed because our
2497 * object ordering is wrong and set tmp_highest_usn
2498 * to a value that is higher than what we already
2499 * sent to the client (destination dsa).
2501 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2504 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2507 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2508 r->out.ctr->ctr6.uptodateness_vector = NULL;
2509 r->out.ctr->ctr6.nc_object_count = 0;
2510 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2513 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2514 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2515 (unsigned long long)(req10->highwatermark.highest_usn+1),
2516 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2517 r->out.ctr->ctr6.object_count,
2518 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2519 r->out.ctr->ctr6.linked_attributes_count,
2520 link_given, link_total,
2521 dom_sid_string(mem_ctx, user_sid)));
2524 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2525 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);