2 Unix SMB/CIFS implementation.
4 implement the DRSUpdateRefs call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
31 #include "rpc_server/dcerpc_server_proto.h"
32 #include "../libcli/drsuapi/drsuapi.h"
33 #include "libcli/security/security.h"
34 #include "lib/util/binsearch.h"
35 #include "lib/util/tsort.h"
36 #include "auth/session.h"
37 #include "dsdb/common/util.h"
40 build a DsReplicaObjectIdentifier from a ldb msg
42 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
43 struct ldb_message *msg)
45 struct drsuapi_DsReplicaObjectIdentifier *identifier;
48 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
49 if (identifier == NULL) {
53 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
54 identifier->guid = samdb_result_guid(msg, "objectGUID");
56 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
58 identifier->sid = *sid;
60 ZERO_STRUCT(identifier->sid);
65 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
67 return GUID_compare(guid1, &guid2);
71 see if we can filter an attribute using the uptodateness_vector
73 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
74 const struct GUID *originating_invocation_id,
75 uint64_t originating_usn)
77 const struct drsuapi_DsReplicaCursor *c;
78 if (udv == NULL) return false;
79 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
80 originating_invocation_id, udv_compare, c);
81 if (c && originating_usn <= c->highest_usn) {
89 drsuapi_DsGetNCChanges for one object
91 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
92 struct ldb_message *msg,
93 struct ldb_context *sam_ctx,
94 struct ldb_dn *ncRoot_dn,
96 struct dsdb_schema *schema,
97 DATA_BLOB *session_key,
99 uint32_t replica_flags,
100 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
101 enum drsuapi_DsExtendedOperation extended_op)
103 const struct ldb_val *md_value;
105 struct replPropertyMetaDataBlob md;
107 enum ndr_err_code ndr_err;
110 const struct dsdb_attribute *rdn_sa;
111 unsigned int instanceType;
112 struct dsdb_syntax_ctx syntax_ctx;
114 /* make dsdb sytanx context for conversions */
115 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
116 syntax_ctx.is_schema_nc = is_schema_nc;
118 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
119 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
120 obj->is_nc_prefix = true;
121 obj->parent_object_guid = NULL;
123 obj->is_nc_prefix = false;
124 obj->parent_object_guid = talloc(obj, struct GUID);
125 if (obj->parent_object_guid == NULL) {
126 return WERR_DS_DRA_INTERNAL_ERROR;
128 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
129 if (GUID_all_zero(obj->parent_object_guid)) {
130 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
131 ldb_dn_get_linearized(msg->dn)));
132 return WERR_DS_DRA_INTERNAL_ERROR;
135 obj->next_object = NULL;
137 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
139 /* nothing to send */
143 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
144 /* don't send uninstantiated objects */
148 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
149 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
150 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
151 return WERR_DS_DRA_INTERNAL_ERROR;
154 if (md.version != 1) {
155 return WERR_DS_DRA_INTERNAL_ERROR;
158 rdn = ldb_dn_get_rdn_name(msg->dn);
160 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
161 return WERR_DS_DRA_INTERNAL_ERROR;
164 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
165 if (rdn_sa == NULL) {
166 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
167 rdn, ldb_dn_get_linearized(msg->dn)));
168 return WERR_DS_DRA_INTERNAL_ERROR;
171 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
172 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
174 obj->object.identifier = get_object_identifier(obj, msg);
175 if (obj->object.identifier == NULL) {
178 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
180 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
181 for (n=i=0; i<md.ctr.ctr1.count; i++) {
182 const struct dsdb_attribute *sa;
183 bool force_attribute = false;
185 /* if the attribute has not changed, and it is not the
186 instanceType then don't include it */
187 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
188 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
189 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType) continue;
191 /* don't include the rDN */
192 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
194 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
196 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
197 (unsigned int)md.ctr.ctr1.array[i].attid,
198 ldb_dn_get_linearized(msg->dn)));
199 return WERR_DS_DRA_INTERNAL_ERROR;
203 struct ldb_message_element *el;
204 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
205 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
206 /* don't send upgraded links inline */
211 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
212 !dsdb_attr_in_rodc_fas(sa)) {
213 force_attribute = true;
214 DEBUG(4,("Forcing attribute %s in %s\n",
215 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
218 /* filter by uptodateness_vector */
219 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType &&
221 udv_filter(uptodateness_vector,
222 &md.ctr.ctr1.array[i].originating_invocation_id,
223 md.ctr.ctr1.array[i].originating_usn)) {
228 * If the recipient is a RODC, then we should only give
229 * attributes from the RODC filtered attribute set
231 * TODO: This is not strictly correct, as it doesn't allow for administrators
232 * to setup some users to transfer passwords to specific RODCs. To support that
233 * we would instead remove this check and rely on extended ACL checking in the dsdb
236 if (!(replica_flags & DRSUAPI_DRS_WRIT_REP) &&
238 !dsdb_attr_in_rodc_fas(sa)) {
239 DEBUG(4,("Skipping non-FAS attr %s in %s\n",
241 ldb_dn_get_linearized(msg->dn)));
245 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
246 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
247 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
248 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
249 attids[n] = md.ctr.ctr1.array[i].attid;
253 /* ignore it if its an empty change. Note that renames always
254 * change the 'name' attribute, so they won't be ignored by
257 (n == 1 && attids[0] == DRSUAPI_ATTRIBUTE_instanceType)) {
258 talloc_free(obj->meta_data_ctr);
259 obj->meta_data_ctr = NULL;
263 obj->meta_data_ctr->count = n;
265 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
266 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
267 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
268 obj->object.attribute_ctr.num_attributes);
271 * Note that the meta_data array and the attributes array must
272 * be the same size and in the same order
274 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
275 struct ldb_message_element *el;
277 const struct dsdb_attribute *sa;
279 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
281 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
282 return WERR_DS_DRA_INTERNAL_ERROR;
285 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
287 /* this happens for attributes that have been removed */
288 DEBUG(5,("No element '%s' for attributeID %u in message\n",
289 sa->lDAPDisplayName, attids[i]));
290 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
291 obj->object.attribute_ctr.attributes[i].attid =
292 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
294 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
295 &obj->object.attribute_ctr.attributes[i]);
296 if (!W_ERROR_IS_OK(werr)) {
297 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
298 sa->lDAPDisplayName, win_errstr(werr)));
301 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
302 * check if attribute is secret and send a null value
304 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
305 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
306 &obj->meta_data_ctr->meta_data[i]);
308 /* some attributes needs to be encrypted
310 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
311 &obj->object.attribute_ctr.attributes[i]);
312 if (!W_ERROR_IS_OK(werr)) {
313 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
314 sa->lDAPDisplayName, win_errstr(werr)));
325 add one linked attribute from an object to the list of linked
326 attributes in a getncchanges request
328 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
329 struct ldb_context *sam_ctx,
330 const struct dsdb_schema *schema,
331 const struct dsdb_attribute *sa,
332 struct ldb_message *msg,
333 struct dsdb_dn *dsdb_dn,
334 struct drsuapi_DsReplicaLinkedAttribute **la_list,
337 struct drsuapi_DsReplicaLinkedAttribute *la;
342 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
343 W_ERROR_HAVE_NO_MEMORY(*la_list);
345 la = &(*la_list)[*la_count];
347 la->identifier = get_object_identifier(*la_list, msg);
348 W_ERROR_HAVE_NO_MEMORY(la->identifier);
350 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
352 la->attid = sa->attributeID_id;
353 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
355 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
356 if (!NT_STATUS_IS_OK(status)) {
357 return ntstatus_to_werror(status);
359 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
360 if (!NT_STATUS_IS_OK(status)) {
361 return ntstatus_to_werror(status);
363 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
364 if (!NT_STATUS_IS_OK(status)) {
365 return ntstatus_to_werror(status);
367 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
368 if (!NT_STATUS_IS_OK(status)) {
369 return ntstatus_to_werror(status);
371 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
372 if (!NT_STATUS_IS_OK(status)) {
373 return ntstatus_to_werror(status);
376 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
377 W_ERROR_NOT_OK_RETURN(werr);
385 add linked attributes from an object to the list of linked
386 attributes in a getncchanges request
388 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
390 struct ldb_dn *ncRoot_dn,
391 struct dsdb_schema *schema,
392 uint64_t highest_usn,
393 uint32_t replica_flags,
394 struct ldb_message *msg,
395 struct drsuapi_DsReplicaLinkedAttribute **la_list,
397 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
400 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
401 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
403 for (i=0; i<msg->num_elements; i++) {
404 struct ldb_message_element *el = &msg->elements[i];
405 const struct dsdb_attribute *sa;
408 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
410 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
411 /* we only want forward links */
415 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
416 /* its an old style link, it will have been
417 * sent in the main replication data */
421 for (j=0; j<el->num_values; j++) {
422 struct dsdb_dn *dsdb_dn;
427 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
428 if (dsdb_dn == NULL) {
429 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
430 el->name, ldb_dn_get_linearized(msg->dn)));
431 talloc_free(tmp_ctx);
432 return WERR_DS_DRA_INTERNAL_ERROR;
435 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
436 if (!NT_STATUS_IS_OK(status)) {
437 /* this can happen for attributes
438 given to us with old style meta
443 if (local_usn > uSNChanged) {
444 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
445 el->name, ldb_dn_get_linearized(msg->dn)));
446 talloc_free(tmp_ctx);
447 return WERR_DS_DRA_INTERNAL_ERROR;
450 if (local_usn < highest_usn) {
454 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
455 dsdb_dn, la_list, la_count);
456 if (!W_ERROR_IS_OK(werr)) {
457 talloc_free(tmp_ctx);
463 talloc_free(tmp_ctx);
468 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
470 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
471 struct ldb_dn *ncRoot_dn,
472 struct drsuapi_DsReplicaCursor2CtrEx *udv)
480 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
481 if (ret != LDB_SUCCESS) {
482 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
483 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
484 return WERR_DS_DRA_INTERNAL_ERROR;
491 /* comparison function for linked attributes - see CompareLinks() in
492 * MS-DRSR section 4.1.10.5.17 */
493 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
494 const struct drsuapi_DsReplicaLinkedAttribute *la2,
495 struct ldb_context *sam_ctx)
500 const struct dsdb_schema *schema;
501 const struct dsdb_attribute *schema_attrib;
502 struct dsdb_dn *dn1, *dn2;
503 struct GUID guid1, guid2;
506 c = GUID_compare(&la1->identifier->guid,
507 &la2->identifier->guid);
508 if (c != 0) return c;
510 if (la1->attid != la2->attid) {
511 return la1->attid < la2->attid? -1:1;
514 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
515 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
516 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
519 /* we need to get the target GUIDs to compare */
520 tmp_ctx = talloc_new(sam_ctx);
522 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
523 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
525 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
526 if (!W_ERROR_IS_OK(werr)) {
527 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
528 talloc_free(tmp_ctx);
532 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
533 if (!W_ERROR_IS_OK(werr)) {
534 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
535 talloc_free(tmp_ctx);
539 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
540 if (!NT_STATUS_IS_OK(status)) {
541 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
542 talloc_free(tmp_ctx);
545 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
546 if (!NT_STATUS_IS_OK(status)) {
547 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
548 talloc_free(tmp_ctx);
552 talloc_free(tmp_ctx);
554 return GUID_compare(&guid1, &guid2);
559 sort the objects we send by tree order
561 static int site_res_cmp_parent_order(struct ldb_message **m1, struct ldb_message **m2)
563 return ldb_dn_compare((*m2)->dn, (*m1)->dn);
567 sort the objects we send first by uSNChanged
569 static int site_res_cmp_usn_order(struct ldb_message **m1, struct ldb_message **m2)
571 unsigned usnchanged1, usnchanged2;
573 cn1 = ldb_dn_get_comp_num((*m1)->dn);
574 cn2 = ldb_dn_get_comp_num((*m2)->dn);
576 return cn1 > cn2 ? 1 : -1;
578 usnchanged1 = ldb_msg_find_attr_as_uint(*m1, "uSNChanged", 0);
579 usnchanged2 = ldb_msg_find_attr_as_uint(*m2, "uSNChanged", 0);
580 if (usnchanged1 == usnchanged2) {
583 return usnchanged1 > usnchanged2 ? 1 : -1;
588 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
590 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
592 struct drsuapi_DsGetNCChangesRequest8 *req8,
593 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
595 struct ldb_dn *rid_manager_dn, *fsmo_role_dn, *req_dn;
597 struct ldb_context *ldb = b_state->sam_ctx;
598 struct ldb_result *ext_res;
599 struct ldb_dn *base_dn;
600 struct dsdb_fsmo_extended_op *exop;
604 - verify that the DN being asked for is the RID Manager DN
605 - verify that we are the RID Manager
608 /* work out who is the RID Manager */
609 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
610 if (ret != LDB_SUCCESS) {
611 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
612 return WERR_DS_DRA_INTERNAL_ERROR;
615 req_dn = ldb_dn_new(mem_ctx, ldb, req8->naming_context->dn);
617 !ldb_dn_validate(req_dn) ||
618 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
619 /* that isn't the RID Manager DN */
620 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
621 req8->naming_context->dn));
622 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
626 /* find the DN of the RID Manager */
627 ret = samdb_reference_dn(ldb, mem_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
628 if (ret != LDB_SUCCESS) {
629 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s\n",
630 ldb_errstring(ldb)));
631 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
632 return WERR_DS_DRA_INTERNAL_ERROR;
635 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
636 /* we're not the RID Manager - go away */
637 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
638 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
642 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
643 W_ERROR_HAVE_NO_MEMORY(exop);
645 exop->fsmo_info = req8->fsmo_info;
646 exop->destination_dsa_guid = req8->destination_dsa_guid;
648 ret = ldb_transaction_start(ldb);
649 if (ret != LDB_SUCCESS) {
650 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
651 ldb_errstring(ldb)));
652 return WERR_DS_DRA_INTERNAL_ERROR;
656 * FIXME (kim): this is a temp hack to return just few object,
657 * but not the whole domain NC.
658 * We should remove this hack and implement a 'scope'
659 * building function to return just the set of object
660 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
662 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req8->highwatermark.highest_usn);
664 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
665 if (ret != LDB_SUCCESS) {
666 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
667 ldb_errstring(ldb)));
668 ldb_transaction_cancel(ldb);
669 return WERR_DS_DRA_INTERNAL_ERROR;
672 ret = ldb_transaction_commit(ldb);
673 if (ret != LDB_SUCCESS) {
674 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
675 ldb_errstring(ldb)));
676 return WERR_DS_DRA_INTERNAL_ERROR;
679 talloc_free(ext_res);
681 base_dn = ldb_get_default_basedn(ldb);
683 DEBUG(2,("Allocated RID pool for server %s\n",
684 GUID_string(mem_ctx, &req8->destination_dsa_guid)));
686 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
692 return an array of SIDs from a ldb_message given an attribute name
693 assumes the SIDs are in extended DN format
695 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
696 struct ldb_message *msg,
699 const struct dom_sid ***sids)
701 struct ldb_message_element *el;
704 el = ldb_msg_find_element(msg, attr);
710 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
711 W_ERROR_HAVE_NO_MEMORY(*sids);
713 for (i=0; i<el->num_values; i++) {
714 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
718 sid = talloc(*sids, struct dom_sid);
719 W_ERROR_HAVE_NO_MEMORY(sid);
720 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
721 if (!NT_STATUS_IS_OK(status)) {
722 return WERR_INTERNAL_DB_CORRUPTION;
733 return an array of SIDs from a ldb_message given an attribute name
734 assumes the SIDs are in NDR form
736 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
737 struct ldb_message *msg,
740 const struct dom_sid ***sids)
742 struct ldb_message_element *el;
745 el = ldb_msg_find_element(msg, attr);
751 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
752 W_ERROR_HAVE_NO_MEMORY(*sids);
754 for (i=0; i<el->num_values; i++) {
755 enum ndr_err_code ndr_err;
758 sid = talloc(*sids, struct dom_sid);
759 W_ERROR_HAVE_NO_MEMORY(sid);
761 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
762 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
763 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
764 return WERR_INTERNAL_DB_CORRUPTION;
774 see if any SIDs in list1 are in list2
776 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
779 /* do we ever have enough SIDs here to worry about O(n^2) ? */
780 for (i=0; list1[i]; i++) {
781 for (j=0; list2[j]; j++) {
782 if (dom_sid_equal(list1[i], list2[j])) {
791 handle a DRSUAPI_EXOP_REPL_SECRET call
793 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
795 struct drsuapi_DsGetNCChangesRequest8 *req8,
796 struct dom_sid *user_sid,
797 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
799 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req8->naming_context;
800 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
802 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
803 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
804 struct ldb_result *rodc_res, *obj_res;
805 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
808 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n", ncRoot->dn));
811 * we need to work out if we will allow this RODC to
812 * replicate the secrets for this object
814 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
818 if (b_state->sam_ctx_system == NULL) {
819 /* this operation needs system level access */
820 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
821 return WERR_DS_DRA_SOURCE_DISABLED;
824 obj_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx_system, ncRoot->dn);
825 if (!ldb_dn_validate(obj_dn)) goto failed;
827 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
828 dom_sid_string(mem_ctx, user_sid));
829 if (!ldb_dn_validate(rodc_dn)) goto failed;
831 /* do the two searches we need */
832 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
833 DSDB_SEARCH_SHOW_EXTENDED_DN);
834 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
836 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
837 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
839 /* if the object SID is equal to the user_sid, allow */
840 if (dom_sid_equal(user_sid,
841 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
845 /* an RODC is allowed to get its own krbtgt account secrets */
846 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
847 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
848 if (krbtgt_link_dn != NULL &&
849 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
853 /* but it isn't allowed to get anyone elses krbtgt secrets */
854 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
855 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
859 if (samdb_result_uint(obj_res->msgs[0], "UserAccountControl", 0) &
860 UF_INTERDOMAIN_TRUST_ACCOUNT) {
864 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
865 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
866 if (!W_ERROR_IS_OK(werr)) {
870 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
871 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
872 if (!W_ERROR_IS_OK(werr)) {
876 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
877 mem_ctx, "tokenGroups", &token_sids);
878 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
882 if (never_reveal_sids &&
883 sid_list_match(token_sids, never_reveal_sids)) {
888 sid_list_match(token_sids, reveal_sids)) {
894 DEBUG(2,(__location__ ": Denied RODC secret replication for %s by RODC %s\n",
895 ncRoot->dn, ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
896 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
897 return WERR_DS_DRA_ACCESS_DENIED;
900 DEBUG(2,(__location__ ": Allowed RODC secret replication for %s by RODC %s\n",
901 ncRoot->dn, ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
902 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
903 req8->highwatermark.highest_usn = 0;
907 DEBUG(2,(__location__ ": Failed RODC secret replication for %s by RODC %s\n",
908 ncRoot->dn, dom_sid_string(mem_ctx, user_sid)));
909 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
910 return WERR_DS_DRA_BAD_DN;
914 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
915 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
916 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
918 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
920 struct drsuapi_DsGetNCChangesRequest8 *req8,
921 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
923 struct ldb_dn *fsmo_role_dn, *req_dn, *ntds_dn;
926 struct ldb_context *ldb = b_state->sam_ctx;
927 struct ldb_message *msg;
931 - verify that the client dn exists
932 - verify that we are the current master
935 req_dn = ldb_dn_new(mem_ctx, ldb, req8->naming_context->dn);
937 !ldb_dn_validate(req_dn)) {
938 /* that is not a valid dn */
939 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
940 req8->naming_context->dn));
941 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
945 /* retrieve the current role owner */
946 ret = samdb_reference_dn(ldb, mem_ctx, req_dn, "fSMORoleOwner", &fsmo_role_dn);
947 if (ret != LDB_SUCCESS) {
948 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in context - %s\n",
949 ldb_errstring(ldb)));
950 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
951 return WERR_DS_DRA_INTERNAL_ERROR;
954 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
955 /* we're not the current owner - go away */
956 DEBUG(0,(__location__ ": FSMO transfer request when not owner\n"));
957 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
961 /* change the current master */
962 msg = ldb_msg_new(ldb);
963 W_ERROR_HAVE_NO_MEMORY(msg);
964 msg->dn = ldb_dn_new(msg, ldb, req8->naming_context->dn);
965 W_ERROR_HAVE_NO_MEMORY(msg->dn);
967 ret = dsdb_find_dn_by_guid(ldb, msg, &req8->destination_dsa_guid, &ntds_dn);
968 if (ret != LDB_SUCCESS) {
969 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
970 GUID_string(mem_ctx, &req8->destination_dsa_guid), ldb_errstring(ldb)));
972 return WERR_DS_DRA_INTERNAL_ERROR;
975 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
978 return WERR_DS_DRA_INTERNAL_ERROR;
981 for (i=0;i<msg->num_elements;i++) {
982 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
985 ret = ldb_transaction_start(ldb);
986 if (ret != LDB_SUCCESS) {
987 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
988 ldb_errstring(ldb)));
989 return WERR_DS_DRA_INTERNAL_ERROR;
992 ret = ldb_modify(ldb, msg);
993 if (ret != LDB_SUCCESS) {
994 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
995 ldb_errstring(ldb)));
996 ldb_transaction_cancel(ldb);
997 return WERR_DS_DRA_INTERNAL_ERROR;
1000 ret = ldb_transaction_commit(ldb);
1001 if (ret != LDB_SUCCESS) {
1002 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1003 ldb_errstring(ldb)));
1004 return WERR_DS_DRA_INTERNAL_ERROR;
1007 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1012 /* state of a partially completed getncchanges call */
1013 struct drsuapi_getncchanges_state {
1014 struct ldb_result *site_res;
1016 struct ldb_dn *ncRoot_dn;
1019 uint64_t highest_usn;
1020 struct ldb_dn *last_dn;
1021 struct drsuapi_DsReplicaLinkedAttribute *la_list;
1025 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
1029 drsuapi_DsGetNCChanges
1031 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1033 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1034 struct drsuapi_DsGetNCChanges *r)
1036 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1039 struct dsdb_schema *schema;
1040 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1041 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1043 DATA_BLOB session_key;
1044 const char *attrs[] = { "*", "distinguishedName",
1045 "nTSecurityDescriptor",
1047 "replPropertyMetaData",
1052 "supplementalCredentials",
1055 struct dcesrv_handle *h;
1056 struct drsuapi_bind_state *b_state;
1057 struct drsuapi_getncchanges_state *getnc_state;
1058 struct drsuapi_DsGetNCChangesRequest8 *req8;
1060 uint32_t max_objects;
1062 uint32_t link_count = 0;
1063 uint32_t link_total = 0;
1064 uint32_t link_given = 0;
1065 struct ldb_dn *search_dn = NULL;
1066 bool am_rodc, null_scope=false;
1067 enum security_user_level security_level;
1068 struct ldb_context *sam_ctx;
1069 struct dom_sid *user_sid;
1071 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1074 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1076 *r->out.level_out = 6;
1077 /* TODO: linked attributes*/
1078 r->out.ctr->ctr6.linked_attributes_count = 0;
1079 r->out.ctr->ctr6.linked_attributes = NULL;
1081 r->out.ctr->ctr6.object_count = 0;
1082 r->out.ctr->ctr6.nc_object_count = 0;
1083 r->out.ctr->ctr6.more_data = false;
1084 r->out.ctr->ctr6.uptodateness_vector = NULL;
1086 /* a RODC doesn't allow for any replication */
1087 ret = samdb_rodc(sam_ctx, &am_rodc);
1088 if (ret == LDB_SUCCESS && am_rodc) {
1089 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1090 return WERR_DS_DRA_SOURCE_DISABLED;
1093 /* Check request revision.
1094 TODO: Adding mappings to req8 from the other levels
1096 if (r->in.level != 8) {
1097 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1099 return WERR_REVISION_MISMATCH;
1102 req8 = &r->in.req->req8;
1104 /* Perform access checks. */
1105 /* TODO: we need to support a sync on a specific non-root
1106 * DN. We'll need to find the real partition root here */
1107 ncRoot = req8->naming_context;
1108 if (ncRoot == NULL) {
1109 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1110 return WERR_DS_DRA_INVALID_PARAMETER;
1113 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1114 return WERR_DS_DRA_INTERNAL_ERROR;
1117 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1118 !(req8->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1119 return WERR_DS_DRA_SOURCE_DISABLED;
1122 werr = drs_security_level_check(dce_call, "DsGetNCChanges", SECURITY_RO_DOMAIN_CONTROLLER,
1123 samdb_domain_sid(sam_ctx));
1124 if (!W_ERROR_IS_OK(werr)) {
1128 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1131 /* for non-administrator replications, check that they have
1132 given the correct source_dsa_invocation_id */
1133 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1134 samdb_domain_sid(sam_ctx));
1135 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER &&
1136 req8->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1137 /* we rely on this flag being unset for RODC requests */
1138 req8->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1142 if (req8->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1143 /* Ignore the _in_ uptpdateness vector*/
1144 req8->uptodateness_vector = NULL;
1147 getnc_state = b_state->getncchanges_state;
1149 /* see if a previous replication has been abandoned */
1151 struct ldb_dn *new_dn = ldb_dn_new(getnc_state, sam_ctx, ncRoot->dn);
1152 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1153 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1154 ldb_dn_get_linearized(new_dn),
1155 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1156 ldb_dn_get_linearized(getnc_state->last_dn)));
1157 talloc_free(getnc_state);
1162 if (getnc_state == NULL) {
1163 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1164 if (getnc_state == NULL) {
1167 b_state->getncchanges_state = getnc_state;
1168 getnc_state->ncRoot_dn = ldb_dn_new(getnc_state, sam_ctx, ncRoot->dn);
1170 /* find out if we are to replicate Schema NC */
1171 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1172 ldb_get_schema_basedn(b_state->sam_ctx));
1173 getnc_state->is_schema_nc = (0 == ret);
1176 * This is the first replication cycle and it is
1177 * a good place to handle extended operations
1179 * FIXME: we don't fully support extended operations yet
1181 switch (req8->extended_op) {
1182 case DRSUAPI_EXOP_NONE:
1184 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1185 werr = getncchanges_rid_alloc(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1186 W_ERROR_NOT_OK_RETURN(werr);
1187 search_dn = ldb_get_default_basedn(sam_ctx);
1189 case DRSUAPI_EXOP_REPL_SECRET:
1190 werr = getncchanges_repl_secret(b_state, mem_ctx, req8, user_sid, &r->out.ctr->ctr6);
1191 r->out.result = werr;
1192 W_ERROR_NOT_OK_RETURN(werr);
1194 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1195 werr = getncchanges_change_master(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1196 W_ERROR_NOT_OK_RETURN(werr);
1198 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1199 werr = getncchanges_change_master(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1200 W_ERROR_NOT_OK_RETURN(werr);
1202 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1203 werr = getncchanges_change_master(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1204 W_ERROR_NOT_OK_RETURN(werr);
1206 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1207 case DRSUAPI_EXOP_REPL_OBJ:
1208 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1209 (unsigned)req8->extended_op));
1210 return WERR_DS_DRA_NOT_SUPPORTED;
1214 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1215 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1216 DEBUG(0,(__location__ ": Bad DN '%s'\n", ncRoot->dn));
1217 return WERR_DS_DRA_INVALID_PARAMETER;
1220 /* we need the session key for encrypting password attributes */
1221 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 DEBUG(0,(__location__ ": Failed to get session key\n"));
1224 return WERR_DS_DRA_INTERNAL_ERROR;
1228 TODO: MS-DRSR section 4.1.10.1.1
1229 Work out if this is the start of a new cycle */
1231 if (getnc_state->site_res == NULL) {
1232 char* search_filter;
1233 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1234 const char *extra_filter;
1236 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1238 getnc_state->min_usn = req8->highwatermark.highest_usn;
1240 /* Construct response. */
1241 search_filter = talloc_asprintf(mem_ctx,
1242 "(uSNChanged>=%llu)",
1243 (unsigned long long)(getnc_state->min_usn+1));
1246 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1249 if (req8->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1250 search_filter = talloc_asprintf(mem_ctx,
1251 "(&%s(isCriticalSystemObject=TRUE))",
1255 if (req8->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1256 scope = LDB_SCOPE_BASE;
1260 search_dn = getnc_state->ncRoot_dn;
1263 DEBUG(1,(__location__ ": getncchanges on %s using filter %s\n",
1264 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1265 ret = drsuapi_search_with_extended_dn(sam_ctx, getnc_state, &getnc_state->site_res,
1266 search_dn, scope, attrs,
1268 if (ret != LDB_SUCCESS) {
1269 return WERR_DS_DRA_INTERNAL_ERROR;
1272 if (req8->replica_flags & DRSUAPI_DRS_GET_ANC) {
1273 TYPESAFE_QSORT(getnc_state->site_res->msgs,
1274 getnc_state->site_res->count,
1275 site_res_cmp_parent_order);
1277 TYPESAFE_QSORT(getnc_state->site_res->msgs,
1278 getnc_state->site_res->count,
1279 site_res_cmp_usn_order);
1282 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req8->uptodateness_vector);
1283 if (getnc_state->uptodateness_vector) {
1284 /* make sure its sorted */
1285 TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1286 getnc_state->uptodateness_vector->count,
1287 drsuapi_DsReplicaCursor_compare);
1291 /* Prefix mapping */
1292 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1294 DEBUG(0,("No schema in sam_ctx\n"));
1295 return WERR_DS_DRA_INTERNAL_ERROR;
1298 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1299 *r->out.ctr->ctr6.naming_context = *ncRoot;
1301 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1302 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1303 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1304 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1305 return WERR_DS_DRA_INTERNAL_ERROR;
1308 /* find the SID if there is one */
1309 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1311 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1312 r->out.ctr->ctr6.mapping_ctr = *ctr;
1314 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1315 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1317 r->out.ctr->ctr6.old_highwatermark = req8->highwatermark;
1318 r->out.ctr->ctr6.new_highwatermark = req8->highwatermark;
1320 r->out.ctr->ctr6.first_object = NULL;
1321 currentObject = &r->out.ctr->ctr6.first_object;
1323 /* use this to force single objects at a time, which is useful
1324 * for working out what object is giving problems
1326 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1327 if (req8->max_object_count < max_objects) {
1328 max_objects = req8->max_object_count;
1331 * TODO: work out how the maximum should be calculated
1333 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1335 for (i=getnc_state->num_sent;
1336 i<getnc_state->site_res->count &&
1338 (r->out.ctr->ctr6.object_count < max_objects);
1341 struct drsuapi_DsReplicaObjectListItemEx *obj;
1342 struct ldb_message *msg = getnc_state->site_res->msgs[i];
1344 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1346 werr = get_nc_changes_build_object(obj, msg,
1347 sam_ctx, getnc_state->ncRoot_dn,
1348 getnc_state->is_schema_nc,
1349 schema, &session_key, getnc_state->min_usn,
1350 req8->replica_flags, getnc_state->uptodateness_vector,
1352 if (!W_ERROR_IS_OK(werr)) {
1356 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1357 getnc_state->ncRoot_dn,
1358 schema, getnc_state->min_usn,
1359 req8->replica_flags,
1361 &getnc_state->la_list,
1362 &getnc_state->la_count,
1363 getnc_state->uptodateness_vector);
1364 if (!W_ERROR_IS_OK(werr)) {
1368 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1369 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1370 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1372 if (uSN > getnc_state->highest_usn) {
1373 getnc_state->highest_usn = uSN;
1376 if (obj->meta_data_ctr == NULL) {
1377 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1378 ldb_dn_get_linearized(msg->dn)));
1379 /* no attributes to send */
1384 r->out.ctr->ctr6.object_count++;
1386 *currentObject = obj;
1387 currentObject = &obj->next_object;
1389 talloc_free(getnc_state->last_dn);
1390 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1392 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1395 getnc_state->num_sent += r->out.ctr->ctr6.object_count;
1397 r->out.ctr->ctr6.nc_object_count = getnc_state->site_res->count;
1399 /* the client can us to call UpdateRefs on its behalf to
1400 re-establish monitoring of the NC */
1401 if ((req8->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1402 !GUID_all_zero(&req8->destination_dsa_guid)) {
1403 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1404 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1405 GUID_string(mem_ctx, &req8->destination_dsa_guid)));
1406 ureq.naming_context = ncRoot;
1407 ureq.dest_dsa_dns_name = talloc_asprintf(mem_ctx, "%s._msdcs.%s",
1408 GUID_string(mem_ctx, &req8->destination_dsa_guid),
1409 lpcfg_dnsdomain(dce_call->conn->dce_ctx->lp_ctx));
1410 if (!ureq.dest_dsa_dns_name) {
1413 ureq.dest_dsa_guid = req8->destination_dsa_guid;
1414 ureq.options = DRSUAPI_DRS_ADD_REF |
1415 DRSUAPI_DRS_ASYNC_OP |
1416 DRSUAPI_DRS_GETCHG_CHECK;
1417 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1418 if (!W_ERROR_IS_OK(werr)) {
1419 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1426 * This is just a guess, how to calculate the
1427 * number of linked attributes to send, we need to
1428 * find out how to do this right.
1430 if (r->out.ctr->ctr6.object_count >= max_links) {
1433 max_links -= r->out.ctr->ctr6.object_count;
1436 link_total = getnc_state->la_count;
1438 if (i < getnc_state->site_res->count) {
1439 r->out.ctr->ctr6.more_data = true;
1441 /* sort the whole array the first time */
1442 if (!getnc_state->la_sorted) {
1443 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1444 sam_ctx, linked_attribute_compare);
1445 getnc_state->la_sorted = true;
1448 link_count = getnc_state->la_count - getnc_state->la_idx;
1449 link_count = MIN(max_links, link_count);
1451 r->out.ctr->ctr6.linked_attributes_count = link_count;
1452 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1454 getnc_state->la_idx += link_count;
1455 link_given = getnc_state->la_idx;
1457 if (getnc_state->la_idx < getnc_state->la_count) {
1458 r->out.ctr->ctr6.more_data = true;
1462 if (!r->out.ctr->ctr6.more_data) {
1463 talloc_steal(mem_ctx, getnc_state->la_list);
1465 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1466 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1468 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1469 r->out.ctr->ctr6.uptodateness_vector);
1470 if (!W_ERROR_IS_OK(werr)) {
1474 talloc_free(getnc_state);
1475 b_state->getncchanges_state = NULL;
1478 if (req8->extended_op != DRSUAPI_EXOP_NONE) {
1479 r->out.ctr->ctr6.uptodateness_vector = NULL;
1480 r->out.ctr->ctr6.nc_object_count = 0;
1481 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1482 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1485 DEBUG(r->out.ctr->ctr6.more_data?2:1,
1486 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1487 (unsigned long long)(req8->highwatermark.highest_usn+1),
1488 req8->replica_flags, ncRoot->dn,
1489 r->out.ctr->ctr6.object_count,
1490 i, r->out.ctr->ctr6.more_data?getnc_state->site_res->count:i,
1491 r->out.ctr->ctr6.linked_attributes_count,
1492 link_given, link_total,
1493 dom_sid_string(mem_ctx, user_sid)));
1496 if (!r->out.ctr->ctr6.more_data && req8->extended_op != DRSUAPI_EXOP_NONE) {
1497 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);