2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009-2010
8 Copyright (C) Andrew Bartlett 2010-2016
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "rpc_server/dcerpc_server.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "param/param.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "libcli/security/security.h"
32 #include "libcli/security/session.h"
33 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
34 #include "rpc_server/dcerpc_server_proto.h"
35 #include "../libcli/drsuapi/drsuapi.h"
36 #include "lib/util/binsearch.h"
37 #include "lib/util/tsort.h"
38 #include "auth/session.h"
39 #include "dsdb/common/util.h"
41 /* state of a partially completed getncchanges call */
42 struct drsuapi_getncchanges_state {
45 uint32_t num_processed;
46 struct ldb_dn *ncRoot_dn;
50 struct drsuapi_DsReplicaHighWaterMark last_hwm;
51 struct ldb_dn *last_dn;
52 struct drsuapi_DsReplicaHighWaterMark final_hwm;
53 struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
54 struct drsuapi_DsReplicaLinkedAttribute *la_list;
56 struct la_for_sorting *la_sorted;
60 /* We must keep the GUIDs in NDR form for sorting */
61 struct la_for_sorting {
62 struct drsuapi_DsReplicaLinkedAttribute *link;
63 uint8_t target_guid[16];
64 uint8_t source_guid[16];
67 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
68 const struct drsuapi_DsReplicaHighWaterMark *h2)
70 if (h1->highest_usn < h2->highest_usn) {
72 } else if (h1->highest_usn > h2->highest_usn) {
74 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
76 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
78 } else if (h1->reserved_usn < h2->reserved_usn) {
80 } else if (h1->reserved_usn > h2->reserved_usn) {
88 build a DsReplicaObjectIdentifier from a ldb msg
90 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
91 const struct ldb_message *msg)
93 struct drsuapi_DsReplicaObjectIdentifier *identifier;
96 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
97 if (identifier == NULL) {
101 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
102 identifier->guid = samdb_result_guid(msg, "objectGUID");
104 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
106 identifier->sid = *sid;
108 ZERO_STRUCT(identifier->sid);
113 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
115 return GUID_compare(guid1, &guid2);
119 see if we can filter an attribute using the uptodateness_vector
121 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
122 const struct GUID *originating_invocation_id,
123 uint64_t originating_usn)
125 const struct drsuapi_DsReplicaCursor *c;
126 if (udv == NULL) return false;
127 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
128 originating_invocation_id, udv_compare, c);
129 if (c && originating_usn <= c->highest_usn) {
136 static int uint32_t_cmp(uint32_t a1, uint32_t a2)
138 if (a1 == a2) return 0;
139 return a1 > a2 ? 1 : -1;
142 static int uint32_t_ptr_cmp(uint32_t *a1, uint32_t *a2, void *unused)
144 if (*a1 == *a2) return 0;
145 return *a1 > *a2 ? 1 : -1;
148 static WERROR getncchanges_attid_remote_to_local(const struct dsdb_schema *schema,
149 const struct dsdb_syntax_ctx *ctx,
150 enum drsuapi_DsAttributeId remote_attid_as_enum,
151 enum drsuapi_DsAttributeId *local_attid_as_enum,
152 const struct dsdb_attribute **_sa)
155 const struct dsdb_attribute *sa = NULL;
157 if (ctx->pfm_remote == NULL) {
158 DEBUG(7, ("No prefixMap supplied, falling back to local prefixMap.\n"));
162 werr = dsdb_attribute_drsuapi_remote_to_local(ctx,
163 remote_attid_as_enum,
166 if (!W_ERROR_IS_OK(werr)) {
167 DEBUG(3, ("WARNING: Unable to resolve remote attid, falling back to local prefixMap.\n"));
174 sa = dsdb_attribute_by_attributeID_id(schema, remote_attid_as_enum);
176 return WERR_DS_DRA_SCHEMA_MISMATCH;
178 if (local_attid_as_enum != NULL) {
179 *local_attid_as_enum = sa->attributeID_id;
189 drsuapi_DsGetNCChanges for one object
191 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
192 const struct ldb_message *msg,
193 struct ldb_context *sam_ctx,
194 struct ldb_dn *ncRoot_dn,
196 struct dsdb_schema *schema,
197 DATA_BLOB *session_key,
198 uint64_t highest_usn,
199 uint32_t replica_flags,
200 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
201 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
202 enum drsuapi_DsExtendedOperation extended_op,
203 bool force_object_return,
206 const struct ldb_val *md_value;
208 struct replPropertyMetaDataBlob md;
210 enum ndr_err_code ndr_err;
213 const struct dsdb_attribute *rdn_sa;
214 unsigned int instanceType;
215 struct dsdb_syntax_ctx syntax_ctx;
217 /* make dsdb sytanx context for conversions */
218 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
219 syntax_ctx.is_schema_nc = is_schema_nc;
221 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
222 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
223 obj->is_nc_prefix = true;
224 obj->parent_object_guid = NULL;
226 obj->is_nc_prefix = false;
227 obj->parent_object_guid = talloc(obj, struct GUID);
228 if (obj->parent_object_guid == NULL) {
229 return WERR_DS_DRA_INTERNAL_ERROR;
231 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
232 if (GUID_all_zero(obj->parent_object_guid)) {
233 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
234 ldb_dn_get_linearized(msg->dn)));
235 return WERR_DS_DRA_INTERNAL_ERROR;
238 obj->next_object = NULL;
240 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
242 /* nothing to send */
246 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
247 /* don't send uninstantiated objects */
251 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
252 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
253 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
254 return WERR_DS_DRA_INTERNAL_ERROR;
257 if (md.version != 1) {
258 return WERR_DS_DRA_INTERNAL_ERROR;
261 rdn = ldb_dn_get_rdn_name(msg->dn);
263 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
264 return WERR_DS_DRA_INTERNAL_ERROR;
267 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
268 if (rdn_sa == NULL) {
269 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
270 rdn, ldb_dn_get_linearized(msg->dn)));
271 return WERR_DS_DRA_INTERNAL_ERROR;
274 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
275 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
277 obj->object.identifier = get_object_identifier(obj, msg);
278 if (obj->object.identifier == NULL) {
279 return WERR_NOT_ENOUGH_MEMORY;
281 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
283 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
284 for (n=i=0; i<md.ctr.ctr1.count; i++) {
285 const struct dsdb_attribute *sa;
286 bool force_attribute = false;
288 /* if the attribute has not changed, and it is not the
289 instanceType then don't include it */
290 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
291 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
292 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
294 /* don't include the rDN */
295 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
297 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
299 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
300 (unsigned int)md.ctr.ctr1.array[i].attid,
301 ldb_dn_get_linearized(msg->dn)));
302 return WERR_DS_DRA_INTERNAL_ERROR;
306 struct ldb_message_element *el;
307 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
308 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
309 /* don't send upgraded links inline */
314 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
315 !dsdb_attr_in_rodc_fas(sa)) {
316 force_attribute = true;
317 DEBUG(4,("Forcing attribute %s in %s\n",
318 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
321 /* filter by uptodateness_vector */
322 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
324 udv_filter(uptodateness_vector,
325 &md.ctr.ctr1.array[i].originating_invocation_id,
326 md.ctr.ctr1.array[i].originating_usn)) {
330 /* filter by partial_attribute_set */
331 if (partial_attribute_set) {
332 uint32_t *result = NULL;
333 BINARY_ARRAY_SEARCH_V(local_pas, partial_attribute_set->num_attids, sa->attributeID_id,
334 uint32_t_cmp, result);
335 if (result == NULL) {
340 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
341 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
342 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
343 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
344 attids[n] = md.ctr.ctr1.array[i].attid;
348 /* ignore it if its an empty change. Note that renames always
349 * change the 'name' attribute, so they won't be ignored by
352 * the force_object_return check is used to force an empty
353 * object return when we timeout in the getncchanges loop.
354 * This allows us to return an empty object, which keeps the
355 * client happy while preventing timeouts
359 attids[0] == DRSUAPI_ATTID_instanceType &&
360 !force_object_return)) {
361 talloc_free(obj->meta_data_ctr);
362 obj->meta_data_ctr = NULL;
366 obj->meta_data_ctr->count = n;
368 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
369 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
370 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
371 obj->object.attribute_ctr.num_attributes);
372 if (obj->object.attribute_ctr.attributes == NULL) {
373 return WERR_NOT_ENOUGH_MEMORY;
377 * Note that the meta_data array and the attributes array must
378 * be the same size and in the same order
380 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
381 struct ldb_message_element *el;
383 const struct dsdb_attribute *sa;
385 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
387 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
388 return WERR_DS_DRA_INTERNAL_ERROR;
391 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
393 /* this happens for attributes that have been removed */
394 DEBUG(5,("No element '%s' for attributeID %u in message\n",
395 sa->lDAPDisplayName, attids[i]));
396 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
397 obj->object.attribute_ctr.attributes[i].attid =
398 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
400 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
401 &obj->object.attribute_ctr.attributes[i]);
402 if (!W_ERROR_IS_OK(werr)) {
403 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
404 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
408 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
409 * check if attribute is secret and send a null value
411 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
412 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
413 &obj->meta_data_ctr->meta_data[i]);
415 /* some attributes needs to be encrypted
417 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
418 &obj->object.attribute_ctr.attributes[i]);
419 if (!W_ERROR_IS_OK(werr)) {
420 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
421 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
426 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
427 DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID: "
431 ldb_dn_get_linearized(msg->dn),
433 obj->object.attribute_ctr.attributes[i].attid));
434 return WERR_DS_DATABASE_ERROR;
442 add one linked attribute from an object to the list of linked
443 attributes in a getncchanges request
445 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
446 struct ldb_context *sam_ctx,
447 const struct dsdb_schema *schema,
448 const struct dsdb_attribute *sa,
449 const struct ldb_message *msg,
450 struct dsdb_dn *dsdb_dn,
451 struct drsuapi_DsReplicaLinkedAttribute **la_list,
455 struct drsuapi_DsReplicaLinkedAttribute *la;
460 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
461 W_ERROR_HAVE_NO_MEMORY(*la_list);
463 la = &(*la_list)[*la_count];
465 la->identifier = get_object_identifier(*la_list, msg);
466 W_ERROR_HAVE_NO_MEMORY(la->identifier);
468 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
471 /* We have to check that the inactive link still point to an existing object */
477 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
478 if (strncmp(v, "TRUE", 4) == 0) {
480 * Note: we skip the transmition of the deleted link even if the other part used to
481 * know about it because when we transmit the deletion of the object, the link will
482 * be deleted too due to deletion of object where link points and Windows do so.
484 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
485 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
487 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
488 * if it join an existing domain with deleted objets, it firsts impose to have a
489 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
490 * either during initial replication or after the getNCChanges.
491 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
493 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
494 * If FL >=2K8R2 we are sure that this attribute will be here.
495 * For this kind of forest level we do not return the link if the object is recycled
496 * (isRecycled = true).
498 if (strncmp(v, "TRUE", 4) == 0) {
499 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
500 ldb_dn_get_linearized(msg->dn)));
507 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
508 if (!NT_STATUS_IS_OK(status)) {
509 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
510 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
511 return ntstatus_to_werror(status);
513 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
514 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
515 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
516 GUID_string(mem_ctx, &guid)));
518 } else if (ret != LDB_SUCCESS) {
519 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
520 GUID_string(mem_ctx, &guid),
525 la->attid = dsdb_attribute_get_attid(sa, is_schema_nc);
526 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
528 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
529 if (!NT_STATUS_IS_OK(status)) {
530 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
531 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
532 return ntstatus_to_werror(status);
534 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
535 if (!NT_STATUS_IS_OK(status)) {
536 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
537 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
538 return ntstatus_to_werror(status);
540 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
541 if (!NT_STATUS_IS_OK(status)) {
542 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
543 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
544 return ntstatus_to_werror(status);
546 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
547 if (!NT_STATUS_IS_OK(status)) {
548 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
549 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
550 return ntstatus_to_werror(status);
553 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
554 if (!NT_STATUS_IS_OK(status)) {
555 /* this is possible for upgraded links */
556 la->originating_add_time = la->meta_data.originating_change_time;
559 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
560 W_ERROR_NOT_OK_RETURN(werr);
568 add linked attributes from an object to the list of linked
569 attributes in a getncchanges request
571 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
573 struct ldb_dn *ncRoot_dn,
575 struct dsdb_schema *schema,
576 uint64_t highest_usn,
577 uint32_t replica_flags,
578 const struct ldb_message *msg,
579 struct drsuapi_DsReplicaLinkedAttribute **la_list,
581 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
584 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
585 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
587 for (i=0; i<msg->num_elements; i++) {
588 struct ldb_message_element *el = &msg->elements[i];
589 const struct dsdb_attribute *sa;
592 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
594 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
595 /* we only want forward links */
599 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
600 /* its an old style link, it will have been
601 * sent in the main replication data */
605 for (j=0; j<el->num_values; j++) {
606 struct dsdb_dn *dsdb_dn;
608 uint64_t originating_usn;
609 NTSTATUS status, status2;
611 struct GUID originating_invocation_id;
613 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
614 if (dsdb_dn == NULL) {
615 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
616 el->name, ldb_dn_get_linearized(msg->dn)));
617 talloc_free(tmp_ctx);
618 return WERR_DS_DRA_INTERNAL_ERROR;
621 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
622 if (!NT_STATUS_IS_OK(status)) {
623 /* this can happen for attributes
624 given to us with old style meta
629 if (local_usn > uSNChanged) {
630 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
631 el->name, ldb_dn_get_linearized(msg->dn)));
632 talloc_free(tmp_ctx);
633 return WERR_DS_DRA_INTERNAL_ERROR;
636 if (local_usn < highest_usn) {
640 status = dsdb_get_extended_dn_guid(dsdb_dn->dn,
641 &originating_invocation_id,
643 status2 = dsdb_get_extended_dn_uint64(dsdb_dn->dn,
645 "RMD_ORIGINATING_USN");
647 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2)) {
648 if (udv_filter(uptodateness_vector,
649 &originating_invocation_id,
655 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema,
656 sa, msg, dsdb_dn, la_list,
657 la_count, is_schema_nc);
658 if (!W_ERROR_IS_OK(werr)) {
659 talloc_free(tmp_ctx);
665 talloc_free(tmp_ctx);
670 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
672 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
673 struct ldb_dn *ncRoot_dn,
674 struct drsuapi_DsReplicaCursor2CtrEx *udv)
682 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
683 if (ret != LDB_SUCCESS) {
684 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
685 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
686 return WERR_DS_DRA_INTERNAL_ERROR;
693 /* comparison function for linked attributes - see CompareLinks() in
694 * MS-DRSR section 4.1.10.5.17 */
695 static int linked_attribute_compare(const struct la_for_sorting *la1,
696 const struct la_for_sorting *la2,
700 c = memcmp(la1->source_guid,
701 la2->source_guid, sizeof(la2->source_guid));
706 if (la1->link->attid != la2->link->attid) {
707 return la1->link->attid < la2->link->attid? -1:1;
710 if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
711 (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
712 return (la1->link->flags &
713 DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
716 return memcmp(la1->target_guid,
717 la2->target_guid, sizeof(la2->target_guid));
720 struct drsuapi_changed_objects {
727 sort the objects we send by tree order
729 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
730 struct drsuapi_changed_objects *m2,
731 struct drsuapi_getncchanges_state *getnc_state)
733 return ldb_dn_compare(m2->dn, m1->dn);
737 sort the objects we send first by uSNChanged
739 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
740 struct drsuapi_changed_objects *m2,
741 struct drsuapi_getncchanges_state *getnc_state)
745 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
750 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
755 if (m1->usn == m2->usn) {
756 return ldb_dn_compare(m2->dn, m1->dn);
759 if (m1->usn < m2->usn) {
768 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
770 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
772 struct drsuapi_DsGetNCChangesRequest10 *req10,
773 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
774 struct ldb_dn **rid_manager_dn)
776 struct ldb_dn *req_dn, *ntds_dn = NULL;
778 struct ldb_context *ldb = b_state->sam_ctx;
779 struct ldb_result *ext_res;
780 struct dsdb_fsmo_extended_op *exop;
785 - verify that the DN being asked for is the RID Manager DN
786 - verify that we are the RID Manager
789 /* work out who is the RID Manager, also return to caller */
790 ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
791 if (ret != LDB_SUCCESS) {
792 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
793 return WERR_DS_DRA_INTERNAL_ERROR;
796 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
797 if (!ldb_dn_validate(req_dn) ||
798 ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
799 /* that isn't the RID Manager DN */
800 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
801 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
802 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
806 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
807 ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
808 if (ret != LDB_SUCCESS) {
809 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
810 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
811 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
815 /* find the DN of the RID Manager */
816 ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
817 if (ret != LDB_SUCCESS) {
818 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
819 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
820 return WERR_DS_DRA_INTERNAL_ERROR;
824 /* we're not the RID Manager - go away */
825 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
826 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
830 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
831 W_ERROR_HAVE_NO_MEMORY(exop);
833 exop->fsmo_info = req10->fsmo_info;
834 exop->destination_dsa_guid = req10->destination_dsa_guid;
836 ret = ldb_transaction_start(ldb);
837 if (ret != LDB_SUCCESS) {
838 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
839 ldb_errstring(ldb)));
840 return WERR_DS_DRA_INTERNAL_ERROR;
843 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
844 if (ret != LDB_SUCCESS) {
845 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
846 ldb_errstring(ldb)));
847 ldb_transaction_cancel(ldb);
848 return WERR_DS_DRA_INTERNAL_ERROR;
851 ret = ldb_transaction_commit(ldb);
852 if (ret != LDB_SUCCESS) {
853 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
854 ldb_errstring(ldb)));
855 return WERR_DS_DRA_INTERNAL_ERROR;
858 talloc_free(ext_res);
860 DEBUG(2,("Allocated RID pool for server %s\n",
861 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
863 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
869 return an array of SIDs from a ldb_message given an attribute name
870 assumes the SIDs are in extended DN format
872 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
873 struct ldb_message *msg,
876 const struct dom_sid ***sids)
878 struct ldb_message_element *el;
881 el = ldb_msg_find_element(msg, attr);
887 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
888 W_ERROR_HAVE_NO_MEMORY(*sids);
890 for (i=0; i<el->num_values; i++) {
891 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
895 sid = talloc(*sids, struct dom_sid);
896 W_ERROR_HAVE_NO_MEMORY(sid);
897 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
898 if (!NT_STATUS_IS_OK(status)) {
899 return WERR_INTERNAL_DB_CORRUPTION;
910 return an array of SIDs from a ldb_message given an attribute name
911 assumes the SIDs are in NDR form
913 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
914 struct ldb_message *msg,
917 const struct dom_sid ***sids)
919 struct ldb_message_element *el;
922 el = ldb_msg_find_element(msg, attr);
928 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
929 W_ERROR_HAVE_NO_MEMORY(*sids);
931 for (i=0; i<el->num_values; i++) {
932 enum ndr_err_code ndr_err;
935 sid = talloc(*sids, struct dom_sid);
936 W_ERROR_HAVE_NO_MEMORY(sid);
938 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
939 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
940 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
941 return WERR_INTERNAL_DB_CORRUPTION;
951 see if any SIDs in list1 are in list2
953 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
956 /* do we ever have enough SIDs here to worry about O(n^2) ? */
957 for (i=0; list1[i]; i++) {
958 for (j=0; list2[j]; j++) {
959 if (dom_sid_equal(list1[i], list2[j])) {
968 handle a DRSUAPI_EXOP_REPL_SECRET call
970 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
972 struct drsuapi_DsGetNCChangesRequest10 *req10,
973 struct dom_sid *user_sid,
974 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
975 bool has_get_all_changes)
977 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
978 struct ldb_dn *obj_dn = NULL;
979 struct ldb_dn *rodc_dn, *krbtgt_link_dn;
981 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
982 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
983 struct ldb_result *rodc_res, *obj_res;
984 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
987 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
988 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
991 * we need to work out if we will allow this DC to
992 * replicate the secrets for this object
994 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
998 if (b_state->sam_ctx_system == NULL) {
999 /* this operation needs system level access */
1000 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
1001 return WERR_DS_DRA_SOURCE_DISABLED;
1005 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
1007 * The pseudo code indicate
1008 * revealsecrets = true
1009 * if IsRevealSecretRequest(msgIn) then
1010 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
1012 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
1013 * <... check if this account is ok to be replicated on this DC ...>
1014 * <... and if not reveal secrets = no ...>
1016 * reveal secrets = false
1021 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
1022 * then you can do EXOP_REPL_SECRETS
1024 if (has_get_all_changes) {
1028 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
1029 if (!ldb_dn_validate(obj_dn)) goto failed;
1031 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1032 dom_sid_string(mem_ctx, user_sid));
1033 if (!ldb_dn_validate(rodc_dn)) goto failed;
1035 /* do the two searches we need */
1036 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1037 DSDB_SEARCH_SHOW_EXTENDED_DN);
1038 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1040 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1041 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1043 /* if the object SID is equal to the user_sid, allow */
1044 if (dom_sid_equal(user_sid,
1045 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1049 /* an RODC is allowed to get its own krbtgt account secrets */
1050 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1051 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1052 if (krbtgt_link_dn != NULL &&
1053 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1057 /* but it isn't allowed to get anyone elses krbtgt secrets */
1058 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1059 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1063 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1064 "userAccountControl", 0) &
1065 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1069 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1070 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1071 if (!W_ERROR_IS_OK(werr)) {
1075 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1076 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1077 if (!W_ERROR_IS_OK(werr)) {
1081 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1082 mem_ctx, "tokenGroups", &token_sids);
1083 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1087 if (never_reveal_sids &&
1088 sid_list_match(token_sids, never_reveal_sids)) {
1093 sid_list_match(token_sids, reveal_sids)) {
1099 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1100 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1101 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1102 return WERR_DS_DRA_ACCESS_DENIED;
1105 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1106 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1107 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1108 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1109 req10->highwatermark.highest_usn = 0;
1113 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1114 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1115 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1116 return WERR_DS_DRA_BAD_DN;
1121 handle a DRSUAPI_EXOP_REPL_OBJ call
1123 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1124 TALLOC_CTX *mem_ctx,
1125 struct drsuapi_DsGetNCChangesRequest10 *req10,
1126 struct dom_sid *user_sid,
1127 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1129 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1131 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1132 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1134 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1140 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1141 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1142 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1144 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1145 TALLOC_CTX *mem_ctx,
1146 struct drsuapi_DsGetNCChangesRequest10 *req10,
1147 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1149 struct ldb_dn *req_dn, *ntds_dn;
1152 struct ldb_context *ldb = b_state->sam_ctx;
1153 struct ldb_message *msg;
1158 - verify that the client dn exists
1159 - verify that we are the current master
1162 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1163 if (!ldb_dn_validate(req_dn)) {
1164 /* that is not a valid dn */
1165 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1166 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1167 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1171 /* find the DN of the current role owner */
1172 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1173 if (ret != LDB_SUCCESS) {
1174 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1175 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1176 return WERR_DS_DRA_INTERNAL_ERROR;
1180 /* we're not the RID Manager or role owner - go away */
1181 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1182 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1186 /* change the current master */
1187 msg = ldb_msg_new(ldb);
1188 W_ERROR_HAVE_NO_MEMORY(msg);
1189 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1190 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1192 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1193 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1194 if (ret != LDB_SUCCESS) {
1195 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1196 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1198 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1202 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1205 return WERR_DS_DRA_INTERNAL_ERROR;
1208 for (i=0;i<msg->num_elements;i++) {
1209 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1212 ret = ldb_transaction_start(ldb);
1213 if (ret != LDB_SUCCESS) {
1214 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1215 ldb_errstring(ldb)));
1216 return WERR_DS_DRA_INTERNAL_ERROR;
1219 ret = ldb_modify(ldb, msg);
1220 if (ret != LDB_SUCCESS) {
1221 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1222 ldb_errstring(ldb)));
1223 ldb_transaction_cancel(ldb);
1224 return WERR_DS_DRA_INTERNAL_ERROR;
1227 ret = ldb_transaction_commit(ldb);
1228 if (ret != LDB_SUCCESS) {
1229 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1230 ldb_errstring(ldb)));
1231 return WERR_DS_DRA_INTERNAL_ERROR;
1234 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1240 see if this getncchanges request includes a request to reveal secret information
1242 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1243 struct drsuapi_DsGetNCChangesRequest10 *req10,
1244 struct dsdb_schema_prefixmap *pfm_remote,
1245 bool *is_secret_request)
1247 enum drsuapi_DsExtendedOperation exop;
1249 struct dsdb_schema *schema;
1250 struct dsdb_syntax_ctx syntax_ctx;
1252 *is_secret_request = true;
1254 exop = req10->extended_op;
1257 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1258 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1259 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1260 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1261 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1262 /* FSMO exops can reveal secrets */
1263 *is_secret_request = true;
1265 case DRSUAPI_EXOP_REPL_SECRET:
1266 case DRSUAPI_EXOP_REPL_OBJ:
1267 case DRSUAPI_EXOP_NONE:
1271 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1272 *is_secret_request = false;
1276 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1277 req10->partial_attribute_set == NULL) {
1278 /* they want secrets */
1279 *is_secret_request = true;
1283 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1284 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1285 syntax_ctx.pfm_remote = pfm_remote;
1287 /* check the attributes they asked for */
1288 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1289 const struct dsdb_attribute *sa;
1290 WERROR werr = getncchanges_attid_remote_to_local(schema,
1292 req10->partial_attribute_set->attids[i],
1296 if (!W_ERROR_IS_OK(werr)) {
1297 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1298 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1302 if (!dsdb_attr_in_rodc_fas(sa)) {
1303 *is_secret_request = true;
1308 if (req10->partial_attribute_set_ex) {
1309 /* check the extended attributes they asked for */
1310 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1311 const struct dsdb_attribute *sa;
1312 WERROR werr = getncchanges_attid_remote_to_local(schema,
1314 req10->partial_attribute_set_ex->attids[i],
1318 if (!W_ERROR_IS_OK(werr)) {
1319 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1320 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1324 if (!dsdb_attr_in_rodc_fas(sa)) {
1325 *is_secret_request = true;
1331 *is_secret_request = false;
1336 see if this getncchanges request is only for attributes in the GC
1337 partial attribute set
1339 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1340 struct drsuapi_DsGetNCChangesRequest10 *req10,
1341 struct dsdb_schema_prefixmap *pfm_remote,
1342 bool *is_gc_pas_request)
1344 enum drsuapi_DsExtendedOperation exop;
1346 struct dsdb_schema *schema;
1347 struct dsdb_syntax_ctx syntax_ctx;
1349 exop = req10->extended_op;
1352 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1353 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1354 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1355 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1356 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1357 case DRSUAPI_EXOP_REPL_SECRET:
1358 *is_gc_pas_request = false;
1360 case DRSUAPI_EXOP_REPL_OBJ:
1361 case DRSUAPI_EXOP_NONE:
1365 if (req10->partial_attribute_set == NULL) {
1366 /* they want it all */
1367 *is_gc_pas_request = false;
1371 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1372 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1373 syntax_ctx.pfm_remote = pfm_remote;
1375 /* check the attributes they asked for */
1376 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1377 const struct dsdb_attribute *sa;
1378 WERROR werr = getncchanges_attid_remote_to_local(schema,
1380 req10->partial_attribute_set->attids[i],
1384 if (!W_ERROR_IS_OK(werr)) {
1385 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1386 req10->partial_attribute_set->attids[i], win_errstr(werr)));
1390 if (!sa->isMemberOfPartialAttributeSet) {
1391 *is_gc_pas_request = false;
1396 if (req10->partial_attribute_set_ex) {
1397 /* check the extended attributes they asked for */
1398 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1399 const struct dsdb_attribute *sa;
1400 WERROR werr = getncchanges_attid_remote_to_local(schema,
1402 req10->partial_attribute_set_ex->attids[i],
1406 if (!W_ERROR_IS_OK(werr)) {
1407 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1408 req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1412 if (!sa->isMemberOfPartialAttributeSet) {
1413 *is_gc_pas_request = false;
1419 *is_gc_pas_request = true;
1425 map from req8 to req10
1427 static struct drsuapi_DsGetNCChangesRequest10 *
1428 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1429 struct drsuapi_DsGetNCChangesRequest8 *req8)
1431 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1432 struct drsuapi_DsGetNCChangesRequest10);
1433 if (req10 == NULL) {
1437 req10->destination_dsa_guid = req8->destination_dsa_guid;
1438 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1439 req10->naming_context = req8->naming_context;
1440 req10->highwatermark = req8->highwatermark;
1441 req10->uptodateness_vector = req8->uptodateness_vector;
1442 req10->replica_flags = req8->replica_flags;
1443 req10->max_object_count = req8->max_object_count;
1444 req10->max_ndr_size = req8->max_ndr_size;
1445 req10->extended_op = req8->extended_op;
1446 req10->fsmo_info = req8->fsmo_info;
1447 req10->partial_attribute_set = req8->partial_attribute_set;
1448 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1449 req10->mapping_ctr = req8->mapping_ctr;
1454 static const char *collect_objects_attrs[] = { "uSNChanged",
1459 * Collects object for normal replication cycle.
1461 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1462 TALLOC_CTX *mem_ctx,
1463 struct drsuapi_DsGetNCChangesRequest10 *req10,
1464 struct ldb_dn *search_dn,
1465 const char *extra_filter,
1466 struct ldb_result **search_res)
1469 char* search_filter;
1470 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1471 //const char *extra_filter;
1472 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1474 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1475 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1476 scope = LDB_SCOPE_BASE;
1479 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1481 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1483 /* Construct response. */
1484 search_filter = talloc_asprintf(mem_ctx,
1485 "(uSNChanged>=%llu)",
1486 (unsigned long long)(getnc_state->min_usn+1));
1489 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1492 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1493 search_filter = talloc_asprintf(mem_ctx,
1494 "(&%s(isCriticalSystemObject=TRUE))",
1498 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1499 scope = LDB_SCOPE_BASE;
1503 search_dn = getnc_state->ncRoot_dn;
1506 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1507 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1508 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1510 collect_objects_attrs,
1512 if (ret != LDB_SUCCESS) {
1513 return WERR_DS_DRA_INTERNAL_ERROR;
1520 * Collects object for normal replication cycle.
1522 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1523 TALLOC_CTX *mem_ctx,
1524 struct drsuapi_DsGetNCChangesRequest10 *req10,
1525 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1526 struct ldb_dn *search_dn,
1527 const char *extra_filter,
1528 struct ldb_result **search_res)
1530 /* we have nothing to do in case of ex-op failure */
1531 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1535 switch (req10->extended_op) {
1536 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1539 struct ldb_dn *ntds_dn = NULL;
1540 struct ldb_dn *server_dn = NULL;
1541 struct ldb_dn *machine_dn = NULL;
1542 struct ldb_dn *rid_set_dn = NULL;
1543 struct ldb_result *search_res2 = NULL;
1544 struct ldb_result *search_res3 = NULL;
1545 TALLOC_CTX *frame = talloc_stackframe();
1546 /* get RID manager, RID set and server DN (in that order) */
1548 /* This first search will get the RID Manager */
1549 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1551 search_dn, LDB_SCOPE_BASE,
1552 collect_objects_attrs,
1554 if (ret != LDB_SUCCESS) {
1555 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1556 ldb_dn_get_linearized(search_dn),
1557 ldb_errstring(b_state->sam_ctx)));
1559 return WERR_DS_DRA_INTERNAL_ERROR;
1562 if ((*search_res)->count != 1) {
1563 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1564 ldb_dn_get_linearized(search_dn),
1565 (*search_res)->count));
1567 return WERR_DS_DRA_INTERNAL_ERROR;
1570 /* Now extend it to the RID set */
1572 /* Find the computer account DN for the destination
1573 * dsa GUID specified */
1575 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1576 &req10->destination_dsa_guid, 0,
1578 if (ret != LDB_SUCCESS) {
1579 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1581 &req10->destination_dsa_guid),
1582 ldb_errstring(b_state->sam_ctx)));
1584 return WERR_DS_DRA_INTERNAL_ERROR;
1587 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1590 return WERR_DS_DRA_INTERNAL_ERROR;
1593 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1594 "serverReference", &machine_dn);
1595 if (ret != LDB_SUCCESS) {
1596 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1597 ldb_dn_get_linearized(server_dn),
1598 ldb_errstring(b_state->sam_ctx)));
1600 return WERR_DS_DRA_INTERNAL_ERROR;
1603 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1604 "rIDSetReferences", &rid_set_dn);
1605 if (ret != LDB_SUCCESS) {
1606 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1607 ldb_dn_get_linearized(server_dn),
1608 ldb_errstring(b_state->sam_ctx)));
1610 return WERR_DS_DRA_INTERNAL_ERROR;
1614 /* This first search will get the RID Manager, now get the RID set */
1615 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1617 rid_set_dn, LDB_SCOPE_BASE,
1618 collect_objects_attrs,
1620 if (ret != LDB_SUCCESS) {
1621 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1622 ldb_dn_get_linearized(rid_set_dn),
1623 ldb_errstring(b_state->sam_ctx)));
1625 return WERR_DS_DRA_INTERNAL_ERROR;
1628 if (search_res2->count != 1) {
1629 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1630 ldb_dn_get_linearized(rid_set_dn),
1631 search_res2->count));
1633 return WERR_DS_DRA_INTERNAL_ERROR;
1636 /* Finally get the server DN */
1637 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1639 machine_dn, LDB_SCOPE_BASE,
1640 collect_objects_attrs,
1642 if (ret != LDB_SUCCESS) {
1643 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1644 ldb_dn_get_linearized(server_dn),
1645 ldb_errstring(b_state->sam_ctx)));
1647 return WERR_DS_DRA_INTERNAL_ERROR;
1650 if (search_res3->count != 1) {
1651 DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1652 ldb_dn_get_linearized(server_dn),
1653 search_res3->count));
1655 return WERR_DS_DRA_INTERNAL_ERROR;
1658 /* Now extend the original search_res with these answers */
1659 (*search_res)->count = 3;
1661 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1662 struct ldb_message *,
1663 (*search_res)->count);
1664 if ((*search_res)->msgs == NULL) {
1666 return WERR_NOT_ENOUGH_MEMORY;
1670 talloc_steal(mem_ctx, *search_res);
1671 (*search_res)->msgs[1] =
1672 talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1673 (*search_res)->msgs[2] =
1674 talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1680 /* TODO: implement extended op specific collection
1681 * of objects. Right now we just normal procedure
1682 * for collecting objects */
1683 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1688 drsuapi_DsGetNCChanges
1690 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1692 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1693 struct drsuapi_DsGetNCChanges *r)
1695 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1698 struct dsdb_schema *schema;
1699 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1700 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1702 DATA_BLOB session_key;
1704 struct dcesrv_handle *h;
1705 struct drsuapi_bind_state *b_state;
1706 struct drsuapi_getncchanges_state *getnc_state;
1707 struct drsuapi_DsGetNCChangesRequest10 *req10;
1709 uint32_t max_objects;
1711 uint32_t link_count = 0;
1712 uint32_t link_total = 0;
1713 uint32_t link_given = 0;
1714 struct ldb_dn *search_dn = NULL;
1715 bool am_rodc, null_scope=false;
1716 enum security_user_level security_level;
1717 struct ldb_context *sam_ctx;
1718 struct dom_sid *user_sid;
1719 bool is_secret_request;
1720 bool is_gc_pas_request;
1721 struct drsuapi_changed_objects *changes;
1723 time_t start = time(NULL);
1724 bool max_wait_reached = false;
1725 bool has_get_all_changes = false;
1726 struct GUID invocation_id;
1727 static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
1728 struct dsdb_schema_prefixmap *pfm_remote = NULL;
1730 uint32_t *local_pas = NULL;
1732 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1735 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1737 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1739 *r->out.level_out = 6;
1740 /* TODO: linked attributes*/
1741 r->out.ctr->ctr6.linked_attributes_count = 0;
1742 r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
1744 r->out.ctr->ctr6.object_count = 0;
1745 r->out.ctr->ctr6.nc_object_count = 0;
1746 r->out.ctr->ctr6.more_data = false;
1747 r->out.ctr->ctr6.uptodateness_vector = NULL;
1748 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1749 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1750 r->out.ctr->ctr6.first_object = NULL;
1752 /* a RODC doesn't allow for any replication */
1753 ret = samdb_rodc(sam_ctx, &am_rodc);
1754 if (ret == LDB_SUCCESS && am_rodc) {
1755 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1756 return WERR_DS_DRA_SOURCE_DISABLED;
1759 /* Check request revision.
1761 switch (r->in.level) {
1763 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1764 if (req10 == NULL) {
1765 return WERR_NOT_ENOUGH_MEMORY;
1769 req10 = &r->in.req->req10;
1772 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1774 return WERR_REVISION_MISMATCH;
1778 /* Perform access checks. */
1779 /* TODO: we need to support a sync on a specific non-root
1780 * DN. We'll need to find the real partition root here */
1781 ncRoot = req10->naming_context;
1782 if (ncRoot == NULL) {
1783 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1784 return WERR_DS_DRA_INVALID_PARAMETER;
1787 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1788 return WERR_DS_DRA_INTERNAL_ERROR;
1791 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1792 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1793 return WERR_DS_DRA_SOURCE_DISABLED;
1796 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1798 /* all clients must have GUID_DRS_GET_CHANGES */
1799 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1801 dce_call->conn->auth_state.session_info->security_token,
1802 req10->naming_context,
1803 GUID_DRS_GET_CHANGES);
1804 if (!W_ERROR_IS_OK(werr)) {
1808 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
1809 full = req10->partial_attribute_set == NULL &&
1810 req10->partial_attribute_set_ex == NULL;
1812 full = (options & DRSUAPI_DRS_WRIT_REP) != 0;
1815 werr = dsdb_schema_pfm_from_drsuapi_pfm(&req10->mapping_ctr, true,
1816 mem_ctx, &pfm_remote, NULL);
1818 /* We were supplied a partial attribute set, without the prefix map! */
1819 if (!full && !W_ERROR_IS_OK(werr)) {
1820 if (req10->mapping_ctr.num_mappings == 0) {
1822 * Despite the fact MS-DRSR specifies that this shouldn't
1823 * happen, Windows RODCs will in fact not provide a prefixMap.
1825 DEBUG(5,(__location__ ": Failed to provide a remote prefixMap,"
1826 " falling back to local prefixMap\n"));
1828 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
1834 /* allowed if the GC PAS and client has
1835 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1836 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, pfm_remote, &is_gc_pas_request);
1837 if (!W_ERROR_IS_OK(werr)) {
1840 if (is_gc_pas_request) {
1841 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1843 dce_call->conn->auth_state.session_info->security_token,
1844 req10->naming_context,
1845 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1846 if (W_ERROR_IS_OK(werr)) {
1851 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10,
1853 &is_secret_request);
1854 if (!W_ERROR_IS_OK(werr)) {
1857 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1858 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1860 dce_call->conn->auth_state.session_info->security_token,
1861 req10->naming_context,
1862 GUID_DRS_GET_ALL_CHANGES);
1863 if (!W_ERROR_IS_OK(werr)) {
1866 has_get_all_changes = true;
1871 /* for non-administrator replications, check that they have
1872 given the correct source_dsa_invocation_id */
1873 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1874 samdb_domain_sid(sam_ctx));
1875 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1876 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1877 /* we rely on this flag being unset for RODC requests */
1878 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1882 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1883 /* Ignore the _in_ uptpdateness vector*/
1884 req10->uptodateness_vector = NULL;
1887 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1888 req10->source_dsa_invocation_id = invocation_id;
1891 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1893 * The given highwatermark is only valid relative to the
1894 * specified source_dsa_invocation_id.
1896 ZERO_STRUCT(req10->highwatermark);
1899 getnc_state = b_state->getncchanges_state;
1901 /* see if a previous replication has been abandoned */
1903 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1904 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1905 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1906 ldb_dn_get_linearized(new_dn),
1907 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1908 ldb_dn_get_linearized(getnc_state->last_dn)));
1909 talloc_free(getnc_state);
1915 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1916 &req10->highwatermark);
1918 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1919 "on DN %s %s highwatermark (last_dn %s)\n",
1920 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1921 (ret > 0) ? "older" : "newer",
1922 ldb_dn_get_linearized(getnc_state->last_dn)));
1923 talloc_free(getnc_state);
1928 if (getnc_state == NULL) {
1929 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1930 if (getnc_state == NULL) {
1931 return WERR_NOT_ENOUGH_MEMORY;
1933 b_state->getncchanges_state = getnc_state;
1934 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1936 /* find out if we are to replicate Schema NC */
1937 ret = ldb_dn_compare_base(ldb_get_schema_basedn(b_state->sam_ctx),
1938 getnc_state->ncRoot_dn);
1940 getnc_state->is_schema_nc = (0 == ret);
1942 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1943 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1947 * This is the first replication cycle and it is
1948 * a good place to handle extended operations
1950 * FIXME: we don't fully support extended operations yet
1952 switch (req10->extended_op) {
1953 case DRSUAPI_EXOP_NONE:
1955 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1956 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
1957 W_ERROR_NOT_OK_RETURN(werr);
1958 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1962 case DRSUAPI_EXOP_REPL_SECRET:
1963 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1966 has_get_all_changes);
1967 r->out.result = werr;
1968 W_ERROR_NOT_OK_RETURN(werr);
1970 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1971 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1972 W_ERROR_NOT_OK_RETURN(werr);
1973 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1977 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1978 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1979 W_ERROR_NOT_OK_RETURN(werr);
1980 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1984 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1985 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1986 W_ERROR_NOT_OK_RETURN(werr);
1987 if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1991 case DRSUAPI_EXOP_REPL_OBJ:
1992 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1993 r->out.result = werr;
1994 W_ERROR_NOT_OK_RETURN(werr);
1997 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1999 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
2000 (unsigned)req10->extended_op));
2001 return WERR_DS_DRA_NOT_SUPPORTED;
2005 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
2006 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
2007 DEBUG(0,(__location__ ": Bad DN '%s'\n",
2008 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
2009 return WERR_DS_DRA_INVALID_PARAMETER;
2012 /* we need the session key for encrypting password attributes */
2013 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
2014 if (!NT_STATUS_IS_OK(status)) {
2015 DEBUG(0,(__location__ ": Failed to get session key\n"));
2016 return WERR_DS_DRA_INTERNAL_ERROR;
2020 TODO: MS-DRSR section 4.1.10.1.1
2021 Work out if this is the start of a new cycle */
2023 if (getnc_state->guids == NULL) {
2024 const char *extra_filter;
2025 struct ldb_result *search_res = NULL;
2027 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
2029 getnc_state->min_usn = req10->highwatermark.highest_usn;
2030 getnc_state->max_usn = getnc_state->min_usn;
2032 getnc_state->final_udv = talloc_zero(getnc_state,
2033 struct drsuapi_DsReplicaCursor2CtrEx);
2034 if (getnc_state->final_udv == NULL) {
2035 return WERR_NOT_ENOUGH_MEMORY;
2037 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
2038 getnc_state->final_udv);
2039 if (!W_ERROR_IS_OK(werr)) {
2043 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2044 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
2045 search_dn, extra_filter,
2048 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
2050 search_dn, extra_filter,
2053 W_ERROR_NOT_OK_RETURN(werr);
2055 /* extract out the GUIDs list */
2056 getnc_state->num_records = search_res ? search_res->count : 0;
2057 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
2058 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
2060 changes = talloc_array(getnc_state,
2061 struct drsuapi_changed_objects,
2062 getnc_state->num_records);
2063 W_ERROR_HAVE_NO_MEMORY(changes);
2065 for (i=0; i<getnc_state->num_records; i++) {
2066 changes[i].dn = search_res->msgs[i]->dn;
2067 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
2068 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
2070 if (changes[i].usn > getnc_state->max_usn) {
2071 getnc_state->max_usn = changes[i].usn;
2075 /* RID_ALLOC returns 3 objects in a fixed order */
2076 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
2078 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
2079 LDB_TYPESAFE_QSORT(changes,
2080 getnc_state->num_records,
2082 site_res_cmp_anc_order);
2084 LDB_TYPESAFE_QSORT(changes,
2085 getnc_state->num_records,
2087 site_res_cmp_usn_order);
2090 for (i=0; i < getnc_state->num_records; i++) {
2091 getnc_state->guids[i] = changes[i].guid;
2092 if (GUID_all_zero(&getnc_state->guids[i])) {
2093 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
2094 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
2095 return WERR_DS_DRA_INTERNAL_ERROR;
2099 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
2100 getnc_state->final_hwm.reserved_usn = 0;
2101 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
2103 talloc_free(search_res);
2104 talloc_free(changes);
2107 if (req10->uptodateness_vector) {
2108 /* make sure its sorted */
2109 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
2110 req10->uptodateness_vector->count,
2111 drsuapi_DsReplicaCursor_compare);
2114 /* Prefix mapping */
2115 schema = dsdb_get_schema(sam_ctx, mem_ctx);
2117 DEBUG(0,("No schema in sam_ctx\n"));
2118 return WERR_DS_DRA_INTERNAL_ERROR;
2121 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
2122 *r->out.ctr->ctr6.naming_context = *ncRoot;
2124 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
2125 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
2126 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
2127 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
2128 return WERR_DS_DRA_INTERNAL_ERROR;
2131 /* find the SID if there is one */
2132 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2134 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2135 r->out.ctr->ctr6.mapping_ctr = *ctr;
2137 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2138 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2140 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2141 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2143 currentObject = &r->out.ctr->ctr6.first_object;
2145 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2147 * The client control here only applies in normal replication, not extended
2148 * operations, which return a fixed set, even if the caller
2149 * sets max_object_count == 0
2151 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2152 /* use this to force single objects at a time, which is useful
2153 * for working out what object is giving problems
2155 if (req10->max_object_count < max_objects) {
2156 max_objects = req10->max_object_count;
2160 * TODO: work out how the maximum should be calculated
2162 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2165 * Maximum time that we can spend in a getncchanges
2166 * in order to avoid timeout of the other part.
2167 * 10 seconds by default.
2169 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2171 if (req10->partial_attribute_set != NULL) {
2172 struct dsdb_syntax_ctx syntax_ctx;
2175 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
2176 syntax_ctx.pfm_remote = pfm_remote;
2178 local_pas = talloc_array(b_state, uint32_t, req10->partial_attribute_set->num_attids);
2180 for (j = 0; j < req10->partial_attribute_set->num_attids; j++) {
2181 getncchanges_attid_remote_to_local(schema,
2183 req10->partial_attribute_set->attids[j],
2184 (enum drsuapi_DsAttributeId *)&local_pas[j],
2188 LDB_TYPESAFE_QSORT(local_pas,
2189 req10->partial_attribute_set->num_attids,
2194 for (i=getnc_state->num_processed;
2195 i<getnc_state->num_records &&
2197 (r->out.ctr->ctr6.object_count < max_objects)
2198 && !max_wait_reached;
2201 struct drsuapi_DsReplicaObjectListItemEx *obj;
2202 struct ldb_message *msg;
2203 static const char * const msg_attrs[] = {
2205 "nTSecurityDescriptor",
2207 "replPropertyMetaData",
2208 DSDB_SECRET_ATTRIBUTES,
2210 struct ldb_result *msg_res;
2211 struct ldb_dn *msg_dn;
2213 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2214 W_ERROR_HAVE_NO_MEMORY(obj);
2216 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2217 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2220 /* by re-searching here we avoid having a lot of full
2221 * records in memory between calls to getncchanges
2223 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2225 LDB_SCOPE_BASE, msg_attrs, NULL);
2226 if (ret != LDB_SUCCESS) {
2227 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2228 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2229 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2235 msg = msg_res->msgs[0];
2237 max_wait_reached = (time(NULL) - start > max_wait);
2239 werr = get_nc_changes_build_object(obj, msg,
2240 sam_ctx, getnc_state->ncRoot_dn,
2241 getnc_state->is_schema_nc,
2242 schema, &session_key, getnc_state->min_usn,
2243 req10->replica_flags,
2244 req10->partial_attribute_set,
2245 req10->uptodateness_vector,
2249 if (!W_ERROR_IS_OK(werr)) {
2253 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2254 getnc_state->ncRoot_dn,
2255 getnc_state->is_schema_nc,
2256 schema, getnc_state->min_usn,
2257 req10->replica_flags,
2259 &getnc_state->la_list,
2260 &getnc_state->la_count,
2261 req10->uptodateness_vector);
2262 if (!W_ERROR_IS_OK(werr)) {
2266 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
2267 if (uSN > getnc_state->max_usn) {
2269 * Only report the max_usn we had at the start
2270 * of the replication cycle.
2272 * If this object has changed lately we better
2273 * let the destination dsa refetch the change.
2274 * This is better than the risk of loosing some
2275 * objects or linked attributes.
2279 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
2280 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
2281 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
2284 if (obj->meta_data_ctr == NULL) {
2285 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2286 ldb_dn_get_linearized(msg->dn)));
2287 /* no attributes to send */
2292 r->out.ctr->ctr6.object_count++;
2294 *currentObject = obj;
2295 currentObject = &obj->next_object;
2297 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
2299 talloc_free(getnc_state->last_dn);
2300 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
2302 talloc_free(msg_res);
2303 talloc_free(msg_dn);
2306 getnc_state->num_processed = i;
2308 /* the client can us to call UpdateRefs on its behalf to
2309 re-establish monitoring of the NC */
2310 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2311 !GUID_all_zero(&req10->destination_dsa_guid)) {
2312 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2313 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2314 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2315 ureq.naming_context = ncRoot;
2316 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2317 &req10->destination_dsa_guid);
2318 if (!ureq.dest_dsa_dns_name) {
2319 return WERR_NOT_ENOUGH_MEMORY;
2321 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2322 ureq.options = DRSUAPI_DRS_ADD_REF |
2323 DRSUAPI_DRS_ASYNC_OP |
2324 DRSUAPI_DRS_GETCHG_CHECK;
2326 /* we also need to pass through the
2327 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2328 to send notifies using the GC SPN */
2329 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2331 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2332 if (!W_ERROR_IS_OK(werr)) {
2333 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2334 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2341 * This is just a guess, how to calculate the
2342 * number of linked attributes to send, we need to
2343 * find out how to do this right.
2345 if (r->out.ctr->ctr6.object_count >= max_links) {
2348 max_links -= r->out.ctr->ctr6.object_count;
2351 link_total = getnc_state->la_count;
2353 if (i < getnc_state->num_records) {
2354 r->out.ctr->ctr6.more_data = true;
2356 /* sort the whole array the first time */
2357 if (getnc_state->la_sorted == NULL) {
2359 struct la_for_sorting *guid_array = talloc_array(getnc_state, struct la_for_sorting, getnc_state->la_count);
2360 if (guid_array == NULL) {
2361 DEBUG(0, ("Out of memory allocating %u linked attributes for sorting", getnc_state->la_count));
2362 return WERR_NOT_ENOUGH_MEMORY;
2364 for (j = 0; j < getnc_state->la_count; j++) {
2365 /* we need to get the target GUIDs to compare */
2367 const struct drsuapi_DsReplicaLinkedAttribute *la = &getnc_state->la_list[j];
2368 const struct dsdb_attribute *schema_attrib;
2369 const struct ldb_val *target_guid;
2370 DATA_BLOB source_guid;
2371 TALLOC_CTX *frame = talloc_stackframe();
2373 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2375 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, frame, la->value.blob, &dn);
2376 if (!W_ERROR_IS_OK(werr)) {
2377 DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2382 /* Extract the target GUID in NDR form */
2383 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2384 if (target_guid == NULL
2385 || target_guid->length != sizeof(guid_array[0].target_guid)) {
2386 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2388 /* Repack the source GUID as NDR for sorting */
2389 status = GUID_to_ndr_blob(&la->identifier->guid,
2394 if (!NT_STATUS_IS_OK(status)
2395 || source_guid.length != sizeof(guid_array[0].source_guid)) {
2396 DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2398 return ntstatus_to_werror(status);
2401 guid_array[j].link = &getnc_state->la_list[j];
2402 memcpy(guid_array[j].target_guid, target_guid->data,
2403 sizeof(guid_array[j].target_guid));
2404 memcpy(guid_array[j].source_guid, source_guid.data,
2405 sizeof(guid_array[j].source_guid));
2409 LDB_TYPESAFE_QSORT(guid_array, getnc_state->la_count, NULL, linked_attribute_compare);
2410 getnc_state->la_sorted = guid_array;
2413 link_count = getnc_state->la_count - getnc_state->la_idx;
2414 link_count = MIN(max_links, link_count);
2416 r->out.ctr->ctr6.linked_attributes_count = link_count;
2417 r->out.ctr->ctr6.linked_attributes = talloc_array(r->out.ctr, struct drsuapi_DsReplicaLinkedAttribute, link_count);
2418 if (r->out.ctr->ctr6.linked_attributes == NULL) {
2419 DEBUG(0, ("Out of memory allocating %u linked attributes for output", link_count));
2420 return WERR_NOT_ENOUGH_MEMORY;
2423 for (k = 0; k < link_count; k++) {
2424 r->out.ctr->ctr6.linked_attributes[k]
2425 = *getnc_state->la_sorted[getnc_state->la_idx + k].link;
2428 getnc_state->la_idx += link_count;
2429 link_given = getnc_state->la_idx;
2431 if (getnc_state->la_idx < getnc_state->la_count) {
2432 r->out.ctr->ctr6.more_data = true;
2436 if (req10->replica_flags & DRSUAPI_DRS_GET_NC_SIZE) {
2438 * TODO: This implementation is wrong
2439 * we should find out the total number of
2440 * objects and links in the whole naming context
2441 * at the start of the cycle and return these
2442 * values in each message.
2444 * For now we keep our current strategy and return
2445 * the number of objects for this cycle and the number
2446 * of links we found so far during the cycle.
2448 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2449 r->out.ctr->ctr6.nc_linked_attributes_count = getnc_state->la_count;
2452 if (!r->out.ctr->ctr6.more_data) {
2453 talloc_steal(mem_ctx, getnc_state->la_list);
2455 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2456 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2457 &getnc_state->final_udv);
2459 talloc_free(getnc_state);
2460 b_state->getncchanges_state = NULL;
2462 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2463 &r->out.ctr->ctr6.new_highwatermark);
2466 * We need to make sure that we never return the
2467 * same highwatermark within the same replication
2468 * cycle more than once. Otherwise we cannot detect
2469 * when the client uses an unexptected highwatermark.
2471 * This is a HACK which is needed because our
2472 * object ordering is wrong and set tmp_highest_usn
2473 * to a value that is higher than what we already
2474 * sent to the client (destination dsa).
2476 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2479 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2482 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2483 r->out.ctr->ctr6.uptodateness_vector = NULL;
2484 r->out.ctr->ctr6.nc_object_count = 0;
2485 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2488 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2489 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2490 (unsigned long long)(req10->highwatermark.highest_usn+1),
2491 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2492 r->out.ctr->ctr6.object_count,
2493 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2494 r->out.ctr->ctr6.linked_attributes_count,
2495 link_given, link_total,
2496 dom_sid_string(mem_ctx, user_sid)));
2499 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2500 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);