2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges 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) {
88 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
90 if (a1 == a2) return 0;
91 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
95 check if an attribute is in a partial_attribute_set
97 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
98 struct drsuapi_DsPartialAttributeSet *pas)
100 enum drsuapi_DsAttributeId *result;
101 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
103 return result != NULL;
108 drsuapi_DsGetNCChanges for one object
110 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
111 struct ldb_message *msg,
112 struct ldb_context *sam_ctx,
113 struct ldb_dn *ncRoot_dn,
115 struct dsdb_schema *schema,
116 DATA_BLOB *session_key,
117 uint64_t highest_usn,
118 uint32_t replica_flags,
119 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
120 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
121 enum drsuapi_DsExtendedOperation extended_op)
123 const struct ldb_val *md_value;
125 struct replPropertyMetaDataBlob md;
127 enum ndr_err_code ndr_err;
130 const struct dsdb_attribute *rdn_sa;
131 unsigned int instanceType;
132 struct dsdb_syntax_ctx syntax_ctx;
134 /* make dsdb sytanx context for conversions */
135 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
136 syntax_ctx.is_schema_nc = is_schema_nc;
138 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
139 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
140 obj->is_nc_prefix = true;
141 obj->parent_object_guid = NULL;
143 obj->is_nc_prefix = false;
144 obj->parent_object_guid = talloc(obj, struct GUID);
145 if (obj->parent_object_guid == NULL) {
146 return WERR_DS_DRA_INTERNAL_ERROR;
148 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
149 if (GUID_all_zero(obj->parent_object_guid)) {
150 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
151 ldb_dn_get_linearized(msg->dn)));
152 return WERR_DS_DRA_INTERNAL_ERROR;
155 obj->next_object = NULL;
157 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
159 /* nothing to send */
163 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
164 /* don't send uninstantiated objects */
168 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
169 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
170 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
171 return WERR_DS_DRA_INTERNAL_ERROR;
174 if (md.version != 1) {
175 return WERR_DS_DRA_INTERNAL_ERROR;
178 rdn = ldb_dn_get_rdn_name(msg->dn);
180 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
181 return WERR_DS_DRA_INTERNAL_ERROR;
184 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
185 if (rdn_sa == NULL) {
186 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
187 rdn, ldb_dn_get_linearized(msg->dn)));
188 return WERR_DS_DRA_INTERNAL_ERROR;
191 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
192 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
194 obj->object.identifier = get_object_identifier(obj, msg);
195 if (obj->object.identifier == NULL) {
198 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
200 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
201 for (n=i=0; i<md.ctr.ctr1.count; i++) {
202 const struct dsdb_attribute *sa;
203 bool force_attribute = false;
205 /* if the attribute has not changed, and it is not the
206 instanceType then don't include it */
207 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
208 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
209 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType) continue;
211 /* don't include the rDN */
212 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
214 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
216 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
217 (unsigned int)md.ctr.ctr1.array[i].attid,
218 ldb_dn_get_linearized(msg->dn)));
219 return WERR_DS_DRA_INTERNAL_ERROR;
223 struct ldb_message_element *el;
224 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
225 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
226 /* don't send upgraded links inline */
231 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
232 !dsdb_attr_in_rodc_fas(sa)) {
233 force_attribute = true;
234 DEBUG(4,("Forcing attribute %s in %s\n",
235 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
238 /* filter by uptodateness_vector */
239 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType &&
241 udv_filter(uptodateness_vector,
242 &md.ctr.ctr1.array[i].originating_invocation_id,
243 md.ctr.ctr1.array[i].originating_usn)) {
247 /* filter by partial_attribute_set */
248 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
252 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
253 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
254 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
255 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
256 attids[n] = md.ctr.ctr1.array[i].attid;
260 /* ignore it if its an empty change. Note that renames always
261 * change the 'name' attribute, so they won't be ignored by
264 (n == 1 && attids[0] == DRSUAPI_ATTRIBUTE_instanceType)) {
265 talloc_free(obj->meta_data_ctr);
266 obj->meta_data_ctr = NULL;
270 obj->meta_data_ctr->count = n;
272 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
273 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
274 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
275 obj->object.attribute_ctr.num_attributes);
278 * Note that the meta_data array and the attributes array must
279 * be the same size and in the same order
281 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
282 struct ldb_message_element *el;
284 const struct dsdb_attribute *sa;
286 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
288 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
289 return WERR_DS_DRA_INTERNAL_ERROR;
292 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
294 /* this happens for attributes that have been removed */
295 DEBUG(5,("No element '%s' for attributeID %u in message\n",
296 sa->lDAPDisplayName, attids[i]));
297 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
298 obj->object.attribute_ctr.attributes[i].attid =
299 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
301 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
302 &obj->object.attribute_ctr.attributes[i]);
303 if (!W_ERROR_IS_OK(werr)) {
304 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
305 sa->lDAPDisplayName, win_errstr(werr)));
308 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
309 * check if attribute is secret and send a null value
311 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
312 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
313 &obj->meta_data_ctr->meta_data[i]);
315 /* some attributes needs to be encrypted
317 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
318 &obj->object.attribute_ctr.attributes[i]);
319 if (!W_ERROR_IS_OK(werr)) {
320 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
321 sa->lDAPDisplayName, win_errstr(werr)));
332 add one linked attribute from an object to the list of linked
333 attributes in a getncchanges request
335 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
336 struct ldb_context *sam_ctx,
337 const struct dsdb_schema *schema,
338 const struct dsdb_attribute *sa,
339 struct ldb_message *msg,
340 struct dsdb_dn *dsdb_dn,
341 struct drsuapi_DsReplicaLinkedAttribute **la_list,
344 struct drsuapi_DsReplicaLinkedAttribute *la;
349 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
350 W_ERROR_HAVE_NO_MEMORY(*la_list);
352 la = &(*la_list)[*la_count];
354 la->identifier = get_object_identifier(*la_list, msg);
355 W_ERROR_HAVE_NO_MEMORY(la->identifier);
357 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
359 la->attid = sa->attributeID_id;
360 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
362 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
363 if (!NT_STATUS_IS_OK(status)) {
364 return ntstatus_to_werror(status);
366 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
367 if (!NT_STATUS_IS_OK(status)) {
368 return ntstatus_to_werror(status);
370 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
371 if (!NT_STATUS_IS_OK(status)) {
372 return ntstatus_to_werror(status);
374 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
375 if (!NT_STATUS_IS_OK(status)) {
376 return ntstatus_to_werror(status);
378 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
379 if (!NT_STATUS_IS_OK(status)) {
380 return ntstatus_to_werror(status);
383 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
384 W_ERROR_NOT_OK_RETURN(werr);
392 add linked attributes from an object to the list of linked
393 attributes in a getncchanges request
395 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
397 struct ldb_dn *ncRoot_dn,
398 struct dsdb_schema *schema,
399 uint64_t highest_usn,
400 uint32_t replica_flags,
401 struct ldb_message *msg,
402 struct drsuapi_DsReplicaLinkedAttribute **la_list,
404 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
407 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
408 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
410 for (i=0; i<msg->num_elements; i++) {
411 struct ldb_message_element *el = &msg->elements[i];
412 const struct dsdb_attribute *sa;
415 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
417 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
418 /* we only want forward links */
422 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
423 /* its an old style link, it will have been
424 * sent in the main replication data */
428 for (j=0; j<el->num_values; j++) {
429 struct dsdb_dn *dsdb_dn;
434 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
435 if (dsdb_dn == NULL) {
436 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
437 el->name, ldb_dn_get_linearized(msg->dn)));
438 talloc_free(tmp_ctx);
439 return WERR_DS_DRA_INTERNAL_ERROR;
442 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
443 if (!NT_STATUS_IS_OK(status)) {
444 /* this can happen for attributes
445 given to us with old style meta
450 if (local_usn > uSNChanged) {
451 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
452 el->name, ldb_dn_get_linearized(msg->dn)));
453 talloc_free(tmp_ctx);
454 return WERR_DS_DRA_INTERNAL_ERROR;
457 if (local_usn < highest_usn) {
461 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
462 dsdb_dn, la_list, la_count);
463 if (!W_ERROR_IS_OK(werr)) {
464 talloc_free(tmp_ctx);
470 talloc_free(tmp_ctx);
475 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
477 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
478 struct ldb_dn *ncRoot_dn,
479 struct drsuapi_DsReplicaCursor2CtrEx *udv)
487 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
488 if (ret != LDB_SUCCESS) {
489 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
490 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
491 return WERR_DS_DRA_INTERNAL_ERROR;
498 /* comparison function for linked attributes - see CompareLinks() in
499 * MS-DRSR section 4.1.10.5.17 */
500 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
501 const struct drsuapi_DsReplicaLinkedAttribute *la2,
502 struct ldb_context *sam_ctx)
507 const struct dsdb_schema *schema;
508 const struct dsdb_attribute *schema_attrib;
509 struct dsdb_dn *dn1, *dn2;
510 struct GUID guid1, guid2;
513 c = GUID_compare(&la1->identifier->guid,
514 &la2->identifier->guid);
515 if (c != 0) return c;
517 if (la1->attid != la2->attid) {
518 return la1->attid < la2->attid? -1:1;
521 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
522 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
523 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
526 /* we need to get the target GUIDs to compare */
527 tmp_ctx = talloc_new(sam_ctx);
529 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
530 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
532 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
533 if (!W_ERROR_IS_OK(werr)) {
534 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
535 talloc_free(tmp_ctx);
539 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
540 if (!W_ERROR_IS_OK(werr)) {
541 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
542 talloc_free(tmp_ctx);
546 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
547 if (!NT_STATUS_IS_OK(status)) {
548 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
549 talloc_free(tmp_ctx);
552 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
553 if (!NT_STATUS_IS_OK(status)) {
554 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
555 talloc_free(tmp_ctx);
559 talloc_free(tmp_ctx);
561 return GUID_compare(&guid1, &guid2);
566 sort the objects we send by tree order
568 static int site_res_cmp_parent_order(struct ldb_message **m1, struct ldb_message **m2)
570 return ldb_dn_compare((*m2)->dn, (*m1)->dn);
574 sort the objects we send first by uSNChanged
576 static int site_res_cmp_usn_order(struct ldb_message **m1, struct ldb_message **m2)
578 unsigned usnchanged1, usnchanged2;
580 cn1 = ldb_dn_get_comp_num((*m1)->dn);
581 cn2 = ldb_dn_get_comp_num((*m2)->dn);
583 return cn1 > cn2 ? 1 : -1;
585 usnchanged1 = ldb_msg_find_attr_as_uint(*m1, "uSNChanged", 0);
586 usnchanged2 = ldb_msg_find_attr_as_uint(*m2, "uSNChanged", 0);
587 if (usnchanged1 == usnchanged2) {
590 return usnchanged1 > usnchanged2 ? 1 : -1;
595 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
597 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
599 struct drsuapi_DsGetNCChangesRequest10 *req10,
600 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
602 struct ldb_dn *rid_manager_dn, *fsmo_role_dn, *req_dn;
604 struct ldb_context *ldb = b_state->sam_ctx;
605 struct ldb_result *ext_res;
606 struct ldb_dn *base_dn;
607 struct dsdb_fsmo_extended_op *exop;
611 - verify that the DN being asked for is the RID Manager DN
612 - verify that we are the RID Manager
615 /* work out who is the RID Manager */
616 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
617 if (ret != LDB_SUCCESS) {
618 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
619 return WERR_DS_DRA_INTERNAL_ERROR;
622 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
624 !ldb_dn_validate(req_dn) ||
625 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
626 /* that isn't the RID Manager DN */
627 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
628 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
629 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
633 /* find the DN of the RID Manager */
634 ret = samdb_reference_dn(ldb, mem_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
635 if (ret != LDB_SUCCESS) {
636 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s\n",
637 ldb_errstring(ldb)));
638 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
639 return WERR_DS_DRA_INTERNAL_ERROR;
642 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
643 /* we're not the RID Manager - go away */
644 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
645 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
649 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
650 W_ERROR_HAVE_NO_MEMORY(exop);
652 exop->fsmo_info = req10->fsmo_info;
653 exop->destination_dsa_guid = req10->destination_dsa_guid;
655 ret = ldb_transaction_start(ldb);
656 if (ret != LDB_SUCCESS) {
657 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
658 ldb_errstring(ldb)));
659 return WERR_DS_DRA_INTERNAL_ERROR;
663 * FIXME (kim): this is a temp hack to return just few object,
664 * but not the whole domain NC.
665 * We should remove this hack and implement a 'scope'
666 * building function to return just the set of object
667 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
669 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
671 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
672 if (ret != LDB_SUCCESS) {
673 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
674 ldb_errstring(ldb)));
675 ldb_transaction_cancel(ldb);
676 return WERR_DS_DRA_INTERNAL_ERROR;
679 ret = ldb_transaction_commit(ldb);
680 if (ret != LDB_SUCCESS) {
681 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
682 ldb_errstring(ldb)));
683 return WERR_DS_DRA_INTERNAL_ERROR;
686 talloc_free(ext_res);
688 base_dn = ldb_get_default_basedn(ldb);
690 DEBUG(2,("Allocated RID pool for server %s\n",
691 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
693 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
699 return an array of SIDs from a ldb_message given an attribute name
700 assumes the SIDs are in extended DN format
702 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
703 struct ldb_message *msg,
706 const struct dom_sid ***sids)
708 struct ldb_message_element *el;
711 el = ldb_msg_find_element(msg, attr);
717 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
718 W_ERROR_HAVE_NO_MEMORY(*sids);
720 for (i=0; i<el->num_values; i++) {
721 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
725 sid = talloc(*sids, struct dom_sid);
726 W_ERROR_HAVE_NO_MEMORY(sid);
727 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
728 if (!NT_STATUS_IS_OK(status)) {
729 return WERR_INTERNAL_DB_CORRUPTION;
740 return an array of SIDs from a ldb_message given an attribute name
741 assumes the SIDs are in NDR form
743 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
744 struct ldb_message *msg,
747 const struct dom_sid ***sids)
749 struct ldb_message_element *el;
752 el = ldb_msg_find_element(msg, attr);
758 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
759 W_ERROR_HAVE_NO_MEMORY(*sids);
761 for (i=0; i<el->num_values; i++) {
762 enum ndr_err_code ndr_err;
765 sid = talloc(*sids, struct dom_sid);
766 W_ERROR_HAVE_NO_MEMORY(sid);
768 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
769 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
770 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
771 return WERR_INTERNAL_DB_CORRUPTION;
781 see if any SIDs in list1 are in list2
783 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
786 /* do we ever have enough SIDs here to worry about O(n^2) ? */
787 for (i=0; list1[i]; i++) {
788 for (j=0; list2[j]; j++) {
789 if (dom_sid_equal(list1[i], list2[j])) {
798 handle a DRSUAPI_EXOP_REPL_SECRET call
800 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
802 struct drsuapi_DsGetNCChangesRequest10 *req10,
803 struct dom_sid *user_sid,
804 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
806 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
807 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
809 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
810 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
811 struct ldb_result *rodc_res, *obj_res;
812 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
815 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
816 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
819 * we need to work out if we will allow this RODC to
820 * replicate the secrets for this object
822 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
826 if (b_state->sam_ctx_system == NULL) {
827 /* this operation needs system level access */
828 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
829 return WERR_DS_DRA_SOURCE_DISABLED;
832 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
833 if (!ldb_dn_validate(obj_dn)) goto failed;
835 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
836 dom_sid_string(mem_ctx, user_sid));
837 if (!ldb_dn_validate(rodc_dn)) goto failed;
839 /* do the two searches we need */
840 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
841 DSDB_SEARCH_SHOW_EXTENDED_DN);
842 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
844 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
845 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
847 /* if the object SID is equal to the user_sid, allow */
848 if (dom_sid_equal(user_sid,
849 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
853 /* an RODC is allowed to get its own krbtgt account secrets */
854 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
855 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
856 if (krbtgt_link_dn != NULL &&
857 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
861 /* but it isn't allowed to get anyone elses krbtgt secrets */
862 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
863 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
867 if (samdb_result_uint(obj_res->msgs[0], "UserAccountControl", 0) &
868 UF_INTERDOMAIN_TRUST_ACCOUNT) {
872 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
873 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
874 if (!W_ERROR_IS_OK(werr)) {
878 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
879 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
880 if (!W_ERROR_IS_OK(werr)) {
884 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
885 mem_ctx, "tokenGroups", &token_sids);
886 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
890 if (never_reveal_sids &&
891 sid_list_match(token_sids, never_reveal_sids)) {
896 sid_list_match(token_sids, reveal_sids)) {
902 DEBUG(2,(__location__ ": Denied RODC secret replication for %s by RODC %s\n",
903 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
904 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
905 return WERR_DS_DRA_ACCESS_DENIED;
908 DEBUG(2,(__location__ ": Allowed RODC secret replication for %s by RODC %s\n",
909 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
910 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
911 req10->highwatermark.highest_usn = 0;
915 DEBUG(2,(__location__ ": Failed RODC secret replication for %s by RODC %s\n",
916 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
917 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
918 return WERR_DS_DRA_BAD_DN;
923 handle a DRSUAPI_EXOP_REPL_OBJ call
925 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
927 struct drsuapi_DsGetNCChangesRequest10 *req10,
928 struct dom_sid *user_sid,
929 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
931 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
933 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
934 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
936 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
937 req10->highwatermark.highest_usn = 0;
943 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
944 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
945 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
947 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
949 struct drsuapi_DsGetNCChangesRequest10 *req10,
950 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
952 struct ldb_dn *fsmo_role_dn, *req_dn, *ntds_dn;
955 struct ldb_context *ldb = b_state->sam_ctx;
956 struct ldb_message *msg;
960 - verify that the client dn exists
961 - verify that we are the current master
964 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
966 !ldb_dn_validate(req_dn)) {
967 /* that is not a valid dn */
968 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
969 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
970 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
974 /* retrieve the current role owner */
975 ret = samdb_reference_dn(ldb, mem_ctx, req_dn, "fSMORoleOwner", &fsmo_role_dn);
976 if (ret != LDB_SUCCESS) {
977 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in context - %s\n",
978 ldb_errstring(ldb)));
979 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
980 return WERR_DS_DRA_INTERNAL_ERROR;
983 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
984 /* we're not the current owner - go away */
985 DEBUG(0,(__location__ ": FSMO transfer request when not owner\n"));
986 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
990 /* change the current master */
991 msg = ldb_msg_new(ldb);
992 W_ERROR_HAVE_NO_MEMORY(msg);
993 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
994 W_ERROR_HAVE_NO_MEMORY(msg->dn);
996 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
997 if (ret != LDB_SUCCESS) {
998 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
999 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1001 return WERR_DS_DRA_INTERNAL_ERROR;
1004 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1007 return WERR_DS_DRA_INTERNAL_ERROR;
1010 for (i=0;i<msg->num_elements;i++) {
1011 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1014 ret = ldb_transaction_start(ldb);
1015 if (ret != LDB_SUCCESS) {
1016 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1017 ldb_errstring(ldb)));
1018 return WERR_DS_DRA_INTERNAL_ERROR;
1021 ret = ldb_modify(ldb, msg);
1022 if (ret != LDB_SUCCESS) {
1023 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1024 ldb_errstring(ldb)));
1025 ldb_transaction_cancel(ldb);
1026 return WERR_DS_DRA_INTERNAL_ERROR;
1029 ret = ldb_transaction_commit(ldb);
1030 if (ret != LDB_SUCCESS) {
1031 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1032 ldb_errstring(ldb)));
1033 return WERR_DS_DRA_INTERNAL_ERROR;
1036 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1041 /* state of a partially completed getncchanges call */
1042 struct drsuapi_getncchanges_state {
1043 struct ldb_result *site_res;
1045 struct ldb_dn *ncRoot_dn;
1048 uint64_t highest_usn;
1049 struct ldb_dn *last_dn;
1050 struct drsuapi_DsReplicaLinkedAttribute *la_list;
1054 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
1058 see if this getncchanges request includes a request to reveal secret information
1060 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1061 struct drsuapi_DsGetNCChangesRequest10 *req10,
1062 bool *is_secret_request)
1064 enum drsuapi_DsExtendedOperation exop;
1066 struct dsdb_schema *schema;
1068 *is_secret_request = true;
1070 exop = req10->extended_op;
1073 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1074 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1075 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1076 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1077 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1078 /* FSMO exops can reveal secrets */
1079 *is_secret_request = true;
1081 case DRSUAPI_EXOP_REPL_SECRET:
1082 case DRSUAPI_EXOP_REPL_OBJ:
1083 case DRSUAPI_EXOP_NONE:
1087 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1088 *is_secret_request = false;
1092 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1093 req10->partial_attribute_set == NULL) {
1094 /* they want secrets */
1095 *is_secret_request = true;
1099 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1101 /* check the attributes they asked for */
1102 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1103 const struct dsdb_attribute *sa;
1104 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1106 return WERR_DS_DRA_SCHEMA_MISMATCH;
1108 if (!dsdb_attr_in_rodc_fas(sa)) {
1109 *is_secret_request = true;
1114 /* check the attributes they asked for */
1115 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1116 const struct dsdb_attribute *sa;
1117 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1119 return WERR_DS_DRA_SCHEMA_MISMATCH;
1121 if (!dsdb_attr_in_rodc_fas(sa)) {
1122 *is_secret_request = true;
1127 *is_secret_request = false;
1133 map from req8 to req10
1135 static struct drsuapi_DsGetNCChangesRequest10 *
1136 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1137 struct drsuapi_DsGetNCChangesRequest8 *req8)
1139 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1140 struct drsuapi_DsGetNCChangesRequest10);
1141 if (req10 == NULL) {
1145 req10->destination_dsa_guid = req8->destination_dsa_guid;
1146 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1147 req10->naming_context = req8->naming_context;
1148 req10->highwatermark = req8->highwatermark;
1149 req10->uptodateness_vector = req8->uptodateness_vector;
1150 req10->replica_flags = req8->replica_flags;
1151 req10->max_object_count = req8->max_object_count;
1152 req10->max_ndr_size = req8->max_ndr_size;
1153 req10->extended_op = req8->extended_op;
1154 req10->fsmo_info = req8->fsmo_info;
1155 req10->partial_attribute_set = req8->partial_attribute_set;
1156 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1157 req10->mapping_ctr = req8->mapping_ctr;
1164 drsuapi_DsGetNCChanges
1166 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1168 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1169 struct drsuapi_DsGetNCChanges *r)
1171 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1174 struct dsdb_schema *schema;
1175 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1176 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1178 DATA_BLOB session_key;
1179 const char *attrs[] = { "*", "distinguishedName",
1180 "nTSecurityDescriptor",
1182 "replPropertyMetaData",
1187 "supplementalCredentials",
1190 struct dcesrv_handle *h;
1191 struct drsuapi_bind_state *b_state;
1192 struct drsuapi_getncchanges_state *getnc_state;
1193 struct drsuapi_DsGetNCChangesRequest10 *req10;
1195 uint32_t max_objects;
1197 uint32_t link_count = 0;
1198 uint32_t link_total = 0;
1199 uint32_t link_given = 0;
1200 struct ldb_dn *search_dn = NULL;
1201 bool am_rodc, null_scope=false;
1202 enum security_user_level security_level;
1203 struct ldb_context *sam_ctx;
1204 struct dom_sid *user_sid;
1205 bool is_secret_request;
1207 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1210 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1212 *r->out.level_out = 6;
1213 /* TODO: linked attributes*/
1214 r->out.ctr->ctr6.linked_attributes_count = 0;
1215 r->out.ctr->ctr6.linked_attributes = NULL;
1217 r->out.ctr->ctr6.object_count = 0;
1218 r->out.ctr->ctr6.nc_object_count = 0;
1219 r->out.ctr->ctr6.more_data = false;
1220 r->out.ctr->ctr6.uptodateness_vector = NULL;
1222 /* a RODC doesn't allow for any replication */
1223 ret = samdb_rodc(sam_ctx, &am_rodc);
1224 if (ret == LDB_SUCCESS && am_rodc) {
1225 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1226 return WERR_DS_DRA_SOURCE_DISABLED;
1229 /* Check request revision.
1231 switch (r->in.level) {
1233 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1234 if (req10 == NULL) {
1239 req10 = &r->in.req->req10;
1242 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1244 return WERR_REVISION_MISMATCH;
1248 /* Perform access checks. */
1249 /* TODO: we need to support a sync on a specific non-root
1250 * DN. We'll need to find the real partition root here */
1251 ncRoot = req10->naming_context;
1252 if (ncRoot == NULL) {
1253 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1254 return WERR_DS_DRA_INVALID_PARAMETER;
1257 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1258 return WERR_DS_DRA_INTERNAL_ERROR;
1261 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1262 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1263 return WERR_DS_DRA_SOURCE_DISABLED;
1266 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1268 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1270 dce_call->conn->auth_state.session_info->security_token,
1271 req10->naming_context,
1272 GUID_DRS_GET_CHANGES);
1273 if (!W_ERROR_IS_OK(werr)) {
1277 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1278 if (!W_ERROR_IS_OK(werr)) {
1281 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1282 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1284 dce_call->conn->auth_state.session_info->security_token,
1285 req10->naming_context,
1286 GUID_DRS_GET_ALL_CHANGES);
1287 if (!W_ERROR_IS_OK(werr)) {
1292 /* for non-administrator replications, check that they have
1293 given the correct source_dsa_invocation_id */
1294 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1295 samdb_domain_sid(sam_ctx));
1296 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1297 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1298 /* we rely on this flag being unset for RODC requests */
1299 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1303 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1304 /* Ignore the _in_ uptpdateness vector*/
1305 req10->uptodateness_vector = NULL;
1308 getnc_state = b_state->getncchanges_state;
1310 /* see if a previous replication has been abandoned */
1312 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1313 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1314 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1315 ldb_dn_get_linearized(new_dn),
1316 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1317 ldb_dn_get_linearized(getnc_state->last_dn)));
1318 talloc_free(getnc_state);
1323 if (getnc_state == NULL) {
1324 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1325 if (getnc_state == NULL) {
1328 b_state->getncchanges_state = getnc_state;
1329 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1331 /* find out if we are to replicate Schema NC */
1332 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1333 ldb_get_schema_basedn(b_state->sam_ctx));
1334 getnc_state->is_schema_nc = (0 == ret);
1337 * This is the first replication cycle and it is
1338 * a good place to handle extended operations
1340 * FIXME: we don't fully support extended operations yet
1342 switch (req10->extended_op) {
1343 case DRSUAPI_EXOP_NONE:
1345 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1346 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1347 W_ERROR_NOT_OK_RETURN(werr);
1348 search_dn = ldb_get_default_basedn(sam_ctx);
1350 case DRSUAPI_EXOP_REPL_SECRET:
1351 werr = getncchanges_repl_secret(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1352 r->out.result = werr;
1353 W_ERROR_NOT_OK_RETURN(werr);
1355 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1356 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1357 W_ERROR_NOT_OK_RETURN(werr);
1359 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1360 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1361 W_ERROR_NOT_OK_RETURN(werr);
1363 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1364 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1365 W_ERROR_NOT_OK_RETURN(werr);
1367 case DRSUAPI_EXOP_REPL_OBJ:
1368 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1369 r->out.result = werr;
1370 W_ERROR_NOT_OK_RETURN(werr);
1373 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1375 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1376 (unsigned)req10->extended_op));
1377 return WERR_DS_DRA_NOT_SUPPORTED;
1381 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1382 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1383 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1384 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1385 return WERR_DS_DRA_INVALID_PARAMETER;
1388 /* we need the session key for encrypting password attributes */
1389 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1390 if (!NT_STATUS_IS_OK(status)) {
1391 DEBUG(0,(__location__ ": Failed to get session key\n"));
1392 return WERR_DS_DRA_INTERNAL_ERROR;
1396 TODO: MS-DRSR section 4.1.10.1.1
1397 Work out if this is the start of a new cycle */
1399 if (getnc_state->site_res == NULL) {
1400 char* search_filter;
1401 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1402 const char *extra_filter;
1404 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1405 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1406 scope = LDB_SCOPE_BASE;
1409 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1411 getnc_state->min_usn = req10->highwatermark.highest_usn;
1413 /* Construct response. */
1414 search_filter = talloc_asprintf(mem_ctx,
1415 "(uSNChanged>=%llu)",
1416 (unsigned long long)(getnc_state->min_usn+1));
1419 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1422 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1423 search_filter = talloc_asprintf(mem_ctx,
1424 "(&%s(isCriticalSystemObject=TRUE))",
1428 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1429 scope = LDB_SCOPE_BASE;
1433 search_dn = getnc_state->ncRoot_dn;
1436 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1437 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1438 ret = drsuapi_search_with_extended_dn(sam_ctx, getnc_state, &getnc_state->site_res,
1439 search_dn, scope, attrs,
1441 if (ret != LDB_SUCCESS) {
1442 return WERR_DS_DRA_INTERNAL_ERROR;
1445 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1446 TYPESAFE_QSORT(getnc_state->site_res->msgs,
1447 getnc_state->site_res->count,
1448 site_res_cmp_parent_order);
1450 TYPESAFE_QSORT(getnc_state->site_res->msgs,
1451 getnc_state->site_res->count,
1452 site_res_cmp_usn_order);
1455 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req10->uptodateness_vector);
1456 if (getnc_state->uptodateness_vector) {
1457 /* make sure its sorted */
1458 TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1459 getnc_state->uptodateness_vector->count,
1460 drsuapi_DsReplicaCursor_compare);
1464 /* Prefix mapping */
1465 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1467 DEBUG(0,("No schema in sam_ctx\n"));
1468 return WERR_DS_DRA_INTERNAL_ERROR;
1471 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1472 *r->out.ctr->ctr6.naming_context = *ncRoot;
1474 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1475 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1476 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1477 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1478 return WERR_DS_DRA_INTERNAL_ERROR;
1481 /* find the SID if there is one */
1482 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1484 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1485 r->out.ctr->ctr6.mapping_ctr = *ctr;
1487 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1488 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1490 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1491 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1493 r->out.ctr->ctr6.first_object = NULL;
1494 currentObject = &r->out.ctr->ctr6.first_object;
1496 /* use this to force single objects at a time, which is useful
1497 * for working out what object is giving problems
1499 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1500 if (req10->max_object_count < max_objects) {
1501 max_objects = req10->max_object_count;
1504 * TODO: work out how the maximum should be calculated
1506 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1508 for (i=getnc_state->num_sent;
1509 i<getnc_state->site_res->count &&
1511 (r->out.ctr->ctr6.object_count < max_objects);
1514 struct drsuapi_DsReplicaObjectListItemEx *obj;
1515 struct ldb_message *msg = getnc_state->site_res->msgs[i];
1517 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1519 werr = get_nc_changes_build_object(obj, msg,
1520 sam_ctx, getnc_state->ncRoot_dn,
1521 getnc_state->is_schema_nc,
1522 schema, &session_key, getnc_state->min_usn,
1523 req10->replica_flags,
1524 req10->partial_attribute_set,
1525 getnc_state->uptodateness_vector,
1526 req10->extended_op);
1527 if (!W_ERROR_IS_OK(werr)) {
1531 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1532 getnc_state->ncRoot_dn,
1533 schema, getnc_state->min_usn,
1534 req10->replica_flags,
1536 &getnc_state->la_list,
1537 &getnc_state->la_count,
1538 getnc_state->uptodateness_vector);
1539 if (!W_ERROR_IS_OK(werr)) {
1543 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1544 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1545 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1547 if (uSN > getnc_state->highest_usn) {
1548 getnc_state->highest_usn = uSN;
1551 if (obj->meta_data_ctr == NULL) {
1552 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1553 ldb_dn_get_linearized(msg->dn)));
1554 /* no attributes to send */
1559 r->out.ctr->ctr6.object_count++;
1561 *currentObject = obj;
1562 currentObject = &obj->next_object;
1564 talloc_free(getnc_state->last_dn);
1565 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1567 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1570 getnc_state->num_sent += r->out.ctr->ctr6.object_count;
1572 r->out.ctr->ctr6.nc_object_count = getnc_state->site_res->count;
1574 /* the client can us to call UpdateRefs on its behalf to
1575 re-establish monitoring of the NC */
1576 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1577 !GUID_all_zero(&req10->destination_dsa_guid)) {
1578 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1579 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1580 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1581 ureq.naming_context = ncRoot;
1582 ureq.dest_dsa_dns_name = talloc_asprintf(mem_ctx, "%s._msdcs.%s",
1583 GUID_string(mem_ctx, &req10->destination_dsa_guid),
1584 lpcfg_dnsdomain(dce_call->conn->dce_ctx->lp_ctx));
1585 if (!ureq.dest_dsa_dns_name) {
1588 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1589 ureq.options = DRSUAPI_DRS_ADD_REF |
1590 DRSUAPI_DRS_ASYNC_OP |
1591 DRSUAPI_DRS_GETCHG_CHECK;
1593 /* we also need to pass through the
1594 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1595 to send notifies using the GC SPN */
1596 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1598 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1599 if (!W_ERROR_IS_OK(werr)) {
1600 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1607 * This is just a guess, how to calculate the
1608 * number of linked attributes to send, we need to
1609 * find out how to do this right.
1611 if (r->out.ctr->ctr6.object_count >= max_links) {
1614 max_links -= r->out.ctr->ctr6.object_count;
1617 link_total = getnc_state->la_count;
1619 if (i < getnc_state->site_res->count) {
1620 r->out.ctr->ctr6.more_data = true;
1622 /* sort the whole array the first time */
1623 if (!getnc_state->la_sorted) {
1624 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1625 sam_ctx, linked_attribute_compare);
1626 getnc_state->la_sorted = true;
1629 link_count = getnc_state->la_count - getnc_state->la_idx;
1630 link_count = MIN(max_links, link_count);
1632 r->out.ctr->ctr6.linked_attributes_count = link_count;
1633 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1635 getnc_state->la_idx += link_count;
1636 link_given = getnc_state->la_idx;
1638 if (getnc_state->la_idx < getnc_state->la_count) {
1639 r->out.ctr->ctr6.more_data = true;
1643 if (!r->out.ctr->ctr6.more_data) {
1644 talloc_steal(mem_ctx, getnc_state->la_list);
1646 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1647 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1649 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1650 r->out.ctr->ctr6.uptodateness_vector);
1651 if (!W_ERROR_IS_OK(werr)) {
1655 talloc_free(getnc_state);
1656 b_state->getncchanges_state = NULL;
1659 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1660 r->out.ctr->ctr6.uptodateness_vector = NULL;
1661 r->out.ctr->ctr6.nc_object_count = 0;
1662 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1663 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1666 DEBUG(r->out.ctr->ctr6.more_data?4:2,
1667 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1668 (unsigned long long)(req10->highwatermark.highest_usn+1),
1669 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
1670 r->out.ctr->ctr6.object_count,
1671 i, r->out.ctr->ctr6.more_data?getnc_state->site_res->count:i,
1672 r->out.ctr->ctr6.linked_attributes_count,
1673 link_given, link_total,
1674 dom_sid_string(mem_ctx, user_sid)));
1677 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
1678 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);