s4:drsuapi: make sure we never return the same highwatermark twice in a replication...
[Samba/wip.git] / source4 / rpc_server / drsuapi / getncchanges.c
blobe06aeed9c289fdc28c410b0acb6628c4d0f54375
1 /*
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/>.
23 #include "includes.h"
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 "libcli/security/security.h"
31 #include "libcli/security/session.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33 #include "rpc_server/dcerpc_server_proto.h"
34 #include "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
40 /* state of a partially completed getncchanges call */
41 struct drsuapi_getncchanges_state {
42 struct GUID *guids;
43 uint32_t num_records;
44 uint32_t num_processed;
45 struct ldb_dn *ncRoot_dn;
46 bool is_schema_nc;
47 uint64_t min_usn;
48 struct drsuapi_DsReplicaHighWaterMark last_hwm;
49 struct ldb_dn *last_dn;
50 struct drsuapi_DsReplicaLinkedAttribute *la_list;
51 uint32_t la_count;
52 bool la_sorted;
53 uint32_t la_idx;
56 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
57 const struct drsuapi_DsReplicaHighWaterMark *h2)
59 if (h1->highest_usn < h2->highest_usn) {
60 return -1;
61 } else if (h1->highest_usn > h2->highest_usn) {
62 return 1;
63 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
64 return -1;
65 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
66 return 1;
67 } else if (h1->reserved_usn < h2->reserved_usn) {
68 return -1;
69 } else if (h1->reserved_usn > h2->reserved_usn) {
70 return 1;
73 return 0;
77 build a DsReplicaObjectIdentifier from a ldb msg
79 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
80 struct ldb_message *msg)
82 struct drsuapi_DsReplicaObjectIdentifier *identifier;
83 struct dom_sid *sid;
85 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
86 if (identifier == NULL) {
87 return NULL;
90 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
91 identifier->guid = samdb_result_guid(msg, "objectGUID");
93 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
94 if (sid) {
95 identifier->sid = *sid;
96 } else {
97 ZERO_STRUCT(identifier->sid);
99 return identifier;
102 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
104 return GUID_compare(guid1, &guid2);
108 see if we can filter an attribute using the uptodateness_vector
110 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
111 const struct GUID *originating_invocation_id,
112 uint64_t originating_usn)
114 const struct drsuapi_DsReplicaCursor *c;
115 if (udv == NULL) return false;
116 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
117 originating_invocation_id, udv_compare, c);
118 if (c && originating_usn <= c->highest_usn) {
119 return true;
121 return false;
125 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
127 if (a1 == a2) return 0;
128 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
132 check if an attribute is in a partial_attribute_set
134 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
135 struct drsuapi_DsPartialAttributeSet *pas)
137 enum drsuapi_DsAttributeId *result;
138 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
139 attid_cmp, result);
140 return result != NULL;
145 drsuapi_DsGetNCChanges for one object
147 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
148 struct ldb_message *msg,
149 struct ldb_context *sam_ctx,
150 struct ldb_dn *ncRoot_dn,
151 bool is_schema_nc,
152 struct dsdb_schema *schema,
153 DATA_BLOB *session_key,
154 uint64_t highest_usn,
155 uint32_t replica_flags,
156 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
157 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
158 enum drsuapi_DsExtendedOperation extended_op,
159 bool force_object_return)
161 const struct ldb_val *md_value;
162 uint32_t i, n;
163 struct replPropertyMetaDataBlob md;
164 uint32_t rid = 0;
165 enum ndr_err_code ndr_err;
166 uint32_t *attids;
167 const char *rdn;
168 const struct dsdb_attribute *rdn_sa;
169 unsigned int instanceType;
170 struct dsdb_syntax_ctx syntax_ctx;
172 /* make dsdb sytanx context for conversions */
173 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
174 syntax_ctx.is_schema_nc = is_schema_nc;
176 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
177 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
178 obj->is_nc_prefix = true;
179 obj->parent_object_guid = NULL;
180 } else {
181 obj->is_nc_prefix = false;
182 obj->parent_object_guid = talloc(obj, struct GUID);
183 if (obj->parent_object_guid == NULL) {
184 return WERR_DS_DRA_INTERNAL_ERROR;
186 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
187 if (GUID_all_zero(obj->parent_object_guid)) {
188 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
189 ldb_dn_get_linearized(msg->dn)));
190 return WERR_DS_DRA_INTERNAL_ERROR;
193 obj->next_object = NULL;
195 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
196 if (!md_value) {
197 /* nothing to send */
198 return WERR_OK;
201 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
202 /* don't send uninstantiated objects */
203 return WERR_OK;
206 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
207 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
208 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
209 return WERR_DS_DRA_INTERNAL_ERROR;
212 if (md.version != 1) {
213 return WERR_DS_DRA_INTERNAL_ERROR;
216 rdn = ldb_dn_get_rdn_name(msg->dn);
217 if (rdn == NULL) {
218 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
219 return WERR_DS_DRA_INTERNAL_ERROR;
222 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
223 if (rdn_sa == NULL) {
224 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
225 rdn, ldb_dn_get_linearized(msg->dn)));
226 return WERR_DS_DRA_INTERNAL_ERROR;
229 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
230 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
232 obj->object.identifier = get_object_identifier(obj, msg);
233 if (obj->object.identifier == NULL) {
234 return WERR_NOMEM;
236 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
238 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
239 for (n=i=0; i<md.ctr.ctr1.count; i++) {
240 const struct dsdb_attribute *sa;
241 bool force_attribute = false;
243 /* if the attribute has not changed, and it is not the
244 instanceType then don't include it */
245 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
246 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
247 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
249 /* don't include the rDN */
250 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
252 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
253 if (!sa) {
254 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
255 (unsigned int)md.ctr.ctr1.array[i].attid,
256 ldb_dn_get_linearized(msg->dn)));
257 return WERR_DS_DRA_INTERNAL_ERROR;
260 if (sa->linkID) {
261 struct ldb_message_element *el;
262 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
263 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
264 /* don't send upgraded links inline */
265 continue;
269 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
270 !dsdb_attr_in_rodc_fas(sa)) {
271 force_attribute = true;
272 DEBUG(4,("Forcing attribute %s in %s\n",
273 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
276 /* filter by uptodateness_vector */
277 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
278 !force_attribute &&
279 udv_filter(uptodateness_vector,
280 &md.ctr.ctr1.array[i].originating_invocation_id,
281 md.ctr.ctr1.array[i].originating_usn)) {
282 continue;
285 /* filter by partial_attribute_set */
286 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
287 continue;
290 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
291 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
292 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
293 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
294 attids[n] = md.ctr.ctr1.array[i].attid;
295 n++;
298 /* ignore it if its an empty change. Note that renames always
299 * change the 'name' attribute, so they won't be ignored by
300 * this
302 * the force_object_return check is used to force an empty
303 * object return when we timeout in the getncchanges loop.
304 * This allows us to return an empty object, which keeps the
305 * client happy while preventing timeouts
307 if (n == 0 ||
308 (n == 1 &&
309 attids[0] == DRSUAPI_ATTID_instanceType &&
310 !force_object_return)) {
311 talloc_free(obj->meta_data_ctr);
312 obj->meta_data_ctr = NULL;
313 return WERR_OK;
316 obj->meta_data_ctr->count = n;
318 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
319 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
320 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
321 obj->object.attribute_ctr.num_attributes);
322 if (obj->object.attribute_ctr.attributes == NULL) {
323 return WERR_NOMEM;
327 * Note that the meta_data array and the attributes array must
328 * be the same size and in the same order
330 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
331 struct ldb_message_element *el;
332 WERROR werr;
333 const struct dsdb_attribute *sa;
335 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
336 if (!sa) {
337 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
338 return WERR_DS_DRA_INTERNAL_ERROR;
341 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
342 if (el == NULL) {
343 /* this happens for attributes that have been removed */
344 DEBUG(5,("No element '%s' for attributeID %u in message\n",
345 sa->lDAPDisplayName, attids[i]));
346 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
347 obj->object.attribute_ctr.attributes[i].attid =
348 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
349 } else {
350 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
351 &obj->object.attribute_ctr.attributes[i]);
352 if (!W_ERROR_IS_OK(werr)) {
353 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
354 sa->lDAPDisplayName, win_errstr(werr)));
355 return werr;
357 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
358 * check if attribute is secret and send a null value
360 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
361 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
362 &obj->meta_data_ctr->meta_data[i]);
364 /* some attributes needs to be encrypted
365 before being sent */
366 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
367 &obj->object.attribute_ctr.attributes[i]);
368 if (!W_ERROR_IS_OK(werr)) {
369 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
370 sa->lDAPDisplayName, win_errstr(werr)));
371 return werr;
376 return WERR_OK;
380 add one linked attribute from an object to the list of linked
381 attributes in a getncchanges request
383 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
384 struct ldb_context *sam_ctx,
385 const struct dsdb_schema *schema,
386 const struct dsdb_attribute *sa,
387 struct ldb_message *msg,
388 struct dsdb_dn *dsdb_dn,
389 struct drsuapi_DsReplicaLinkedAttribute **la_list,
390 uint32_t *la_count)
392 struct drsuapi_DsReplicaLinkedAttribute *la;
393 bool active;
394 NTSTATUS status;
395 WERROR werr;
397 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
398 W_ERROR_HAVE_NO_MEMORY(*la_list);
400 la = &(*la_list)[*la_count];
402 la->identifier = get_object_identifier(*la_list, msg);
403 W_ERROR_HAVE_NO_MEMORY(la->identifier);
405 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
407 if (!active) {
408 /* We have to check that the inactive link still point to an existing object */
409 struct GUID guid;
410 struct ldb_dn *tdn;
411 int ret;
412 const char *v;
414 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
415 if (strncmp(v, "TRUE", 4) == 0) {
417 * Note: we skip the transmition of the deleted link even if the other part used to
418 * know about it because when we transmit the deletion of the object, the link will
419 * be deleted too due to deletion of object where link points and Windows do so.
421 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
422 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
424 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
425 * if it join an existing domain with deleted objets, it firsts impose to have a
426 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
427 * either during initial replication or after the getNCChanges.
428 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
430 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
431 * If FL >=2K8R2 we are sure that this attribute will be here.
432 * For this kind of forest level we do not return the link if the object is recycled
433 * (isRecycled = true).
435 if (strncmp(v, "TRUE", 4) == 0) {
436 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
437 ldb_dn_get_linearized(msg->dn)));
438 return WERR_OK;
440 } else {
441 return WERR_OK;
444 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
445 if (!NT_STATUS_IS_OK(status)) {
446 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
447 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
448 return ntstatus_to_werror(status);
450 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
451 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
452 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
453 GUID_string(mem_ctx, &guid)));
454 return WERR_OK;
455 } else if (ret != LDB_SUCCESS) {
456 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
457 GUID_string(mem_ctx, &guid),
458 ret));
459 return WERR_OK;
462 la->attid = sa->attributeID_id;
463 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
465 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
466 if (!NT_STATUS_IS_OK(status)) {
467 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
468 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
469 return ntstatus_to_werror(status);
471 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
472 if (!NT_STATUS_IS_OK(status)) {
473 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
474 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
475 return ntstatus_to_werror(status);
477 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
478 if (!NT_STATUS_IS_OK(status)) {
479 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
480 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
481 return ntstatus_to_werror(status);
483 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
484 if (!NT_STATUS_IS_OK(status)) {
485 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
486 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
487 return ntstatus_to_werror(status);
490 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
491 if (!NT_STATUS_IS_OK(status)) {
492 /* this is possible for upgraded links */
493 la->originating_add_time = la->meta_data.originating_change_time;
496 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
497 W_ERROR_NOT_OK_RETURN(werr);
499 (*la_count)++;
500 return WERR_OK;
505 add linked attributes from an object to the list of linked
506 attributes in a getncchanges request
508 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
509 TALLOC_CTX *mem_ctx,
510 struct ldb_dn *ncRoot_dn,
511 struct dsdb_schema *schema,
512 uint64_t highest_usn,
513 uint32_t replica_flags,
514 struct ldb_message *msg,
515 struct drsuapi_DsReplicaLinkedAttribute **la_list,
516 uint32_t *la_count,
517 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
519 unsigned int i;
520 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
521 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
523 for (i=0; i<msg->num_elements; i++) {
524 struct ldb_message_element *el = &msg->elements[i];
525 const struct dsdb_attribute *sa;
526 unsigned int j;
528 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
530 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
531 /* we only want forward links */
532 continue;
535 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
536 /* its an old style link, it will have been
537 * sent in the main replication data */
538 continue;
541 for (j=0; j<el->num_values; j++) {
542 struct dsdb_dn *dsdb_dn;
543 uint64_t local_usn;
544 NTSTATUS status;
545 WERROR werr;
547 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
548 if (dsdb_dn == NULL) {
549 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
550 el->name, ldb_dn_get_linearized(msg->dn)));
551 talloc_free(tmp_ctx);
552 return WERR_DS_DRA_INTERNAL_ERROR;
555 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
556 if (!NT_STATUS_IS_OK(status)) {
557 /* this can happen for attributes
558 given to us with old style meta
559 data */
560 continue;
563 if (local_usn > uSNChanged) {
564 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
565 el->name, ldb_dn_get_linearized(msg->dn)));
566 talloc_free(tmp_ctx);
567 return WERR_DS_DRA_INTERNAL_ERROR;
570 if (local_usn < highest_usn) {
571 continue;
574 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
575 dsdb_dn, la_list, la_count);
576 if (!W_ERROR_IS_OK(werr)) {
577 talloc_free(tmp_ctx);
578 return werr;
583 talloc_free(tmp_ctx);
584 return WERR_OK;
588 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
590 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
591 struct ldb_dn *ncRoot_dn,
592 struct drsuapi_DsReplicaCursor2CtrEx *udv)
594 int ret;
596 udv->version = 2;
597 udv->reserved1 = 0;
598 udv->reserved2 = 0;
600 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
601 if (ret != LDB_SUCCESS) {
602 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
603 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
604 return WERR_DS_DRA_INTERNAL_ERROR;
607 return WERR_OK;
611 /* comparison function for linked attributes - see CompareLinks() in
612 * MS-DRSR section 4.1.10.5.17 */
613 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
614 const struct drsuapi_DsReplicaLinkedAttribute *la2,
615 struct ldb_context *sam_ctx)
617 int c;
618 WERROR werr;
619 TALLOC_CTX *tmp_ctx;
620 const struct dsdb_schema *schema;
621 const struct dsdb_attribute *schema_attrib;
622 struct dsdb_dn *dn1, *dn2;
623 struct GUID guid1, guid2;
624 NTSTATUS status;
626 c = GUID_compare(&la1->identifier->guid,
627 &la2->identifier->guid);
628 if (c != 0) return c;
630 if (la1->attid != la2->attid) {
631 return la1->attid < la2->attid? -1:1;
634 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
635 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
636 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
639 /* we need to get the target GUIDs to compare */
640 tmp_ctx = talloc_new(sam_ctx);
642 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
643 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
645 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
646 if (!W_ERROR_IS_OK(werr)) {
647 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
648 talloc_free(tmp_ctx);
649 return 0;
652 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
653 if (!W_ERROR_IS_OK(werr)) {
654 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
655 talloc_free(tmp_ctx);
656 return 0;
659 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
660 if (!NT_STATUS_IS_OK(status)) {
661 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
662 talloc_free(tmp_ctx);
663 return 0;
665 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
666 if (!NT_STATUS_IS_OK(status)) {
667 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
668 talloc_free(tmp_ctx);
669 return 0;
672 talloc_free(tmp_ctx);
674 return GUID_compare(&guid1, &guid2);
677 struct drsuapi_changed_objects {
678 struct ldb_dn *dn;
679 struct GUID guid;
680 uint64_t usn;
684 sort the objects we send by tree order
686 static int site_res_cmp_parent_order(struct drsuapi_changed_objects *m1,
687 struct drsuapi_changed_objects *m2)
689 return ldb_dn_compare(m2->dn, m1->dn);
693 sort the objects we send first by uSNChanged
695 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects *m1,
696 struct drsuapi_changed_objects *m2)
698 unsigned usnchanged1, usnchanged2;
699 unsigned cn1, cn2;
701 cn1 = ldb_dn_get_comp_num(m1->dn);
702 cn2 = ldb_dn_get_comp_num(m2->dn);
703 if (cn1 != cn2) {
704 return cn1 > cn2 ? 1 : -1;
706 usnchanged1 = m1->usn;
707 usnchanged2 = m2->usn;
708 if (usnchanged1 == usnchanged2) {
709 return 0;
711 return usnchanged1 > usnchanged2 ? 1 : -1;
716 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
718 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
719 TALLOC_CTX *mem_ctx,
720 struct drsuapi_DsGetNCChangesRequest10 *req10,
721 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
723 struct ldb_dn *rid_manager_dn, *req_dn;
724 int ret;
725 struct ldb_context *ldb = b_state->sam_ctx;
726 struct ldb_result *ext_res;
727 struct dsdb_fsmo_extended_op *exop;
728 bool is_us;
731 steps:
732 - verify that the DN being asked for is the RID Manager DN
733 - verify that we are the RID Manager
736 /* work out who is the RID Manager */
737 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
738 if (ret != LDB_SUCCESS) {
739 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
740 return WERR_DS_DRA_INTERNAL_ERROR;
743 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
744 if (!ldb_dn_validate(req_dn) ||
745 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
746 /* that isn't the RID Manager DN */
747 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
748 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
749 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
750 return WERR_OK;
753 /* find the DN of the RID Manager */
754 ret = samdb_reference_dn_is_our_ntdsa(ldb, rid_manager_dn, "fSMORoleOwner", &is_us);
755 if (ret != LDB_SUCCESS) {
756 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
757 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
758 return WERR_DS_DRA_INTERNAL_ERROR;
761 if (!is_us) {
762 /* we're not the RID Manager - go away */
763 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
764 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
765 return WERR_OK;
768 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
769 W_ERROR_HAVE_NO_MEMORY(exop);
771 exop->fsmo_info = req10->fsmo_info;
772 exop->destination_dsa_guid = req10->destination_dsa_guid;
774 ret = ldb_transaction_start(ldb);
775 if (ret != LDB_SUCCESS) {
776 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
777 ldb_errstring(ldb)));
778 return WERR_DS_DRA_INTERNAL_ERROR;
782 * FIXME (kim): this is a temp hack to return just few object,
783 * but not the whole domain NC.
784 * We should remove this hack and implement a 'scope'
785 * building function to return just the set of object
786 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
788 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
790 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
791 if (ret != LDB_SUCCESS) {
792 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
793 ldb_errstring(ldb)));
794 ldb_transaction_cancel(ldb);
795 return WERR_DS_DRA_INTERNAL_ERROR;
798 ret = ldb_transaction_commit(ldb);
799 if (ret != LDB_SUCCESS) {
800 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
801 ldb_errstring(ldb)));
802 return WERR_DS_DRA_INTERNAL_ERROR;
805 talloc_free(ext_res);
807 DEBUG(2,("Allocated RID pool for server %s\n",
808 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
810 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
812 return WERR_OK;
816 return an array of SIDs from a ldb_message given an attribute name
817 assumes the SIDs are in extended DN format
819 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
820 struct ldb_message *msg,
821 TALLOC_CTX *mem_ctx,
822 const char *attr,
823 const struct dom_sid ***sids)
825 struct ldb_message_element *el;
826 unsigned int i;
828 el = ldb_msg_find_element(msg, attr);
829 if (!el) {
830 *sids = NULL;
831 return WERR_OK;
834 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
835 W_ERROR_HAVE_NO_MEMORY(*sids);
837 for (i=0; i<el->num_values; i++) {
838 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
839 NTSTATUS status;
840 struct dom_sid *sid;
842 sid = talloc(*sids, struct dom_sid);
843 W_ERROR_HAVE_NO_MEMORY(sid);
844 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
845 if (!NT_STATUS_IS_OK(status)) {
846 return WERR_INTERNAL_DB_CORRUPTION;
848 (*sids)[i] = sid;
850 (*sids)[i] = NULL;
852 return WERR_OK;
857 return an array of SIDs from a ldb_message given an attribute name
858 assumes the SIDs are in NDR form
860 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
861 struct ldb_message *msg,
862 TALLOC_CTX *mem_ctx,
863 const char *attr,
864 const struct dom_sid ***sids)
866 struct ldb_message_element *el;
867 unsigned int i;
869 el = ldb_msg_find_element(msg, attr);
870 if (!el) {
871 *sids = NULL;
872 return WERR_OK;
875 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
876 W_ERROR_HAVE_NO_MEMORY(*sids);
878 for (i=0; i<el->num_values; i++) {
879 enum ndr_err_code ndr_err;
880 struct dom_sid *sid;
882 sid = talloc(*sids, struct dom_sid);
883 W_ERROR_HAVE_NO_MEMORY(sid);
885 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
886 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
887 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
888 return WERR_INTERNAL_DB_CORRUPTION;
890 (*sids)[i] = sid;
892 (*sids)[i] = NULL;
894 return WERR_OK;
898 see if any SIDs in list1 are in list2
900 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
902 unsigned int i, j;
903 /* do we ever have enough SIDs here to worry about O(n^2) ? */
904 for (i=0; list1[i]; i++) {
905 for (j=0; list2[j]; j++) {
906 if (dom_sid_equal(list1[i], list2[j])) {
907 return true;
911 return false;
915 handle a DRSUAPI_EXOP_REPL_SECRET call
917 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
918 TALLOC_CTX *mem_ctx,
919 struct drsuapi_DsGetNCChangesRequest10 *req10,
920 struct dom_sid *user_sid,
921 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
922 bool has_get_all_changes)
924 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
925 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
926 int ret;
927 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
928 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
929 struct ldb_result *rodc_res, *obj_res;
930 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
931 WERROR werr;
933 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
934 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
937 * we need to work out if we will allow this DC to
938 * replicate the secrets for this object
940 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
941 * of this function
944 if (b_state->sam_ctx_system == NULL) {
945 /* this operation needs system level access */
946 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
947 return WERR_DS_DRA_SOURCE_DISABLED;
951 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
953 * The pseudo code indicate
954 * revealsecrets = true
955 * if IsRevealSecretRequest(msgIn) then
956 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
957 * then
958 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
959 * <... check if this account is ok to be replicated on this DC ...>
960 * <... and if not reveal secrets = no ...>
961 * else
962 * reveal secrets = false
963 * endif
964 * endif
965 * endif
967 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
968 * then you can do EXOP_REPL_SECRETS
970 if (has_get_all_changes) {
971 goto allowed;
974 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
975 if (!ldb_dn_validate(obj_dn)) goto failed;
977 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
978 dom_sid_string(mem_ctx, user_sid));
979 if (!ldb_dn_validate(rodc_dn)) goto failed;
981 /* do the two searches we need */
982 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
983 DSDB_SEARCH_SHOW_EXTENDED_DN);
984 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
986 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
987 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
989 /* if the object SID is equal to the user_sid, allow */
990 if (dom_sid_equal(user_sid,
991 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
992 goto allowed;
995 /* an RODC is allowed to get its own krbtgt account secrets */
996 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
997 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
998 if (krbtgt_link_dn != NULL &&
999 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1000 goto allowed;
1003 /* but it isn't allowed to get anyone elses krbtgt secrets */
1004 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1005 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1006 goto denied;
1009 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1010 "userAccountControl", 0) &
1011 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1012 goto denied;
1015 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1016 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1017 if (!W_ERROR_IS_OK(werr)) {
1018 goto denied;
1021 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1022 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1023 if (!W_ERROR_IS_OK(werr)) {
1024 goto denied;
1027 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1028 mem_ctx, "tokenGroups", &token_sids);
1029 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1030 goto denied;
1033 if (never_reveal_sids &&
1034 sid_list_match(token_sids, never_reveal_sids)) {
1035 goto denied;
1038 if (reveal_sids &&
1039 sid_list_match(token_sids, reveal_sids)) {
1040 goto allowed;
1043 /* default deny */
1044 denied:
1045 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1046 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1047 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1048 return WERR_DS_DRA_ACCESS_DENIED;
1050 allowed:
1051 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1052 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1053 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1054 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1055 req10->highwatermark.highest_usn = 0;
1056 return WERR_OK;
1058 failed:
1059 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1060 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1061 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1062 return WERR_DS_DRA_BAD_DN;
1067 handle a DRSUAPI_EXOP_REPL_OBJ call
1069 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1070 TALLOC_CTX *mem_ctx,
1071 struct drsuapi_DsGetNCChangesRequest10 *req10,
1072 struct dom_sid *user_sid,
1073 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1075 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1077 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1078 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1080 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1081 return WERR_OK;
1086 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1087 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1088 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1090 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1091 TALLOC_CTX *mem_ctx,
1092 struct drsuapi_DsGetNCChangesRequest10 *req10,
1093 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1095 struct ldb_dn *req_dn, *ntds_dn;
1096 int ret;
1097 unsigned int i;
1098 struct ldb_context *ldb = b_state->sam_ctx;
1099 struct ldb_message *msg;
1100 bool is_us;
1103 steps:
1104 - verify that the client dn exists
1105 - verify that we are the current master
1108 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1109 if (!ldb_dn_validate(req_dn)) {
1110 /* that is not a valid dn */
1111 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1112 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1113 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1114 return WERR_OK;
1117 /* retrieve the current role owner */
1118 /* find the DN of the RID Manager */
1119 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1120 if (ret != LDB_SUCCESS) {
1121 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1122 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1123 return WERR_DS_DRA_INTERNAL_ERROR;
1126 if (!is_us) {
1127 /* we're not the RID Manager - go away */
1128 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1129 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1130 return WERR_OK;
1133 /* change the current master */
1134 msg = ldb_msg_new(ldb);
1135 W_ERROR_HAVE_NO_MEMORY(msg);
1136 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1137 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1139 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1140 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1141 if (ret != LDB_SUCCESS) {
1142 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1143 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1144 talloc_free(msg);
1145 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1146 return WERR_OK;
1149 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1150 if (ret != 0) {
1151 talloc_free(msg);
1152 return WERR_DS_DRA_INTERNAL_ERROR;
1155 for (i=0;i<msg->num_elements;i++) {
1156 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1159 ret = ldb_transaction_start(ldb);
1160 if (ret != LDB_SUCCESS) {
1161 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1162 ldb_errstring(ldb)));
1163 return WERR_DS_DRA_INTERNAL_ERROR;
1166 ret = ldb_modify(ldb, msg);
1167 if (ret != LDB_SUCCESS) {
1168 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1169 ldb_errstring(ldb)));
1170 ldb_transaction_cancel(ldb);
1171 return WERR_DS_DRA_INTERNAL_ERROR;
1174 ret = ldb_transaction_commit(ldb);
1175 if (ret != LDB_SUCCESS) {
1176 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1177 ldb_errstring(ldb)));
1178 return WERR_DS_DRA_INTERNAL_ERROR;
1181 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1183 return WERR_OK;
1187 see if this getncchanges request includes a request to reveal secret information
1189 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1190 struct drsuapi_DsGetNCChangesRequest10 *req10,
1191 bool *is_secret_request)
1193 enum drsuapi_DsExtendedOperation exop;
1194 uint32_t i;
1195 struct dsdb_schema *schema;
1197 *is_secret_request = true;
1199 exop = req10->extended_op;
1201 switch (exop) {
1202 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1203 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1204 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1205 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1206 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1207 /* FSMO exops can reveal secrets */
1208 *is_secret_request = true;
1209 return WERR_OK;
1210 case DRSUAPI_EXOP_REPL_SECRET:
1211 case DRSUAPI_EXOP_REPL_OBJ:
1212 case DRSUAPI_EXOP_NONE:
1213 break;
1216 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1217 *is_secret_request = false;
1218 return WERR_OK;
1221 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1222 req10->partial_attribute_set == NULL) {
1223 /* they want secrets */
1224 *is_secret_request = true;
1225 return WERR_OK;
1228 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1230 /* check the attributes they asked for */
1231 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1232 const struct dsdb_attribute *sa;
1233 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1234 if (sa == NULL) {
1235 return WERR_DS_DRA_SCHEMA_MISMATCH;
1237 if (!dsdb_attr_in_rodc_fas(sa)) {
1238 *is_secret_request = true;
1239 return WERR_OK;
1243 if (req10->partial_attribute_set_ex) {
1244 /* check the extended attributes they asked for */
1245 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1246 const struct dsdb_attribute *sa;
1247 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1248 if (sa == NULL) {
1249 return WERR_DS_DRA_SCHEMA_MISMATCH;
1251 if (!dsdb_attr_in_rodc_fas(sa)) {
1252 *is_secret_request = true;
1253 return WERR_OK;
1258 *is_secret_request = false;
1259 return WERR_OK;
1263 see if this getncchanges request is only for attributes in the GC
1264 partial attribute set
1266 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1267 struct drsuapi_DsGetNCChangesRequest10 *req10,
1268 bool *is_gc_pas_request)
1270 enum drsuapi_DsExtendedOperation exop;
1271 uint32_t i;
1272 struct dsdb_schema *schema;
1274 exop = req10->extended_op;
1276 switch (exop) {
1277 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1278 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1279 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1280 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1281 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1282 case DRSUAPI_EXOP_REPL_SECRET:
1283 *is_gc_pas_request = false;
1284 return WERR_OK;
1285 case DRSUAPI_EXOP_REPL_OBJ:
1286 case DRSUAPI_EXOP_NONE:
1287 break;
1290 if (req10->partial_attribute_set == NULL) {
1291 /* they want it all */
1292 *is_gc_pas_request = false;
1293 return WERR_OK;
1296 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1298 /* check the attributes they asked for */
1299 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1300 const struct dsdb_attribute *sa;
1301 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1302 if (sa == NULL) {
1303 return WERR_DS_DRA_SCHEMA_MISMATCH;
1305 if (!sa->isMemberOfPartialAttributeSet) {
1306 *is_gc_pas_request = false;
1307 return WERR_OK;
1311 if (req10->partial_attribute_set_ex) {
1312 /* check the extended attributes they asked for */
1313 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1314 const struct dsdb_attribute *sa;
1315 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1316 if (sa == NULL) {
1317 return WERR_DS_DRA_SCHEMA_MISMATCH;
1319 if (!sa->isMemberOfPartialAttributeSet) {
1320 *is_gc_pas_request = false;
1321 return WERR_OK;
1326 *is_gc_pas_request = true;
1327 return WERR_OK;
1332 map from req8 to req10
1334 static struct drsuapi_DsGetNCChangesRequest10 *
1335 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1336 struct drsuapi_DsGetNCChangesRequest8 *req8)
1338 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1339 struct drsuapi_DsGetNCChangesRequest10);
1340 if (req10 == NULL) {
1341 return NULL;
1344 req10->destination_dsa_guid = req8->destination_dsa_guid;
1345 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1346 req10->naming_context = req8->naming_context;
1347 req10->highwatermark = req8->highwatermark;
1348 req10->uptodateness_vector = req8->uptodateness_vector;
1349 req10->replica_flags = req8->replica_flags;
1350 req10->max_object_count = req8->max_object_count;
1351 req10->max_ndr_size = req8->max_ndr_size;
1352 req10->extended_op = req8->extended_op;
1353 req10->fsmo_info = req8->fsmo_info;
1354 req10->partial_attribute_set = req8->partial_attribute_set;
1355 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1356 req10->mapping_ctr = req8->mapping_ctr;
1358 return req10;
1363 * Collects object for normal replication cycle.
1365 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1366 TALLOC_CTX *mem_ctx,
1367 struct drsuapi_DsGetNCChangesRequest10 *req10,
1368 struct ldb_dn *search_dn,
1369 const char *extra_filter,
1370 struct ldb_result **search_res)
1372 int ret;
1373 char* search_filter;
1374 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1375 //const char *extra_filter;
1376 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1377 const char *attrs[] = { "uSNChanged",
1378 "objectGUID" ,
1379 NULL };
1381 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1382 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1383 scope = LDB_SCOPE_BASE;
1386 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1388 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1390 /* Construct response. */
1391 search_filter = talloc_asprintf(mem_ctx,
1392 "(uSNChanged>=%llu)",
1393 (unsigned long long)(getnc_state->min_usn+1));
1395 if (extra_filter) {
1396 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1399 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1400 search_filter = talloc_asprintf(mem_ctx,
1401 "(&%s(isCriticalSystemObject=TRUE))",
1402 search_filter);
1405 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1406 scope = LDB_SCOPE_BASE;
1409 if (!search_dn) {
1410 search_dn = getnc_state->ncRoot_dn;
1413 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1414 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1415 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1416 search_dn, scope, attrs,
1417 search_filter);
1418 if (ret != LDB_SUCCESS) {
1419 return WERR_DS_DRA_INTERNAL_ERROR;
1422 return WERR_OK;
1426 * Collects object for normal replication cycle.
1428 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1429 TALLOC_CTX *mem_ctx,
1430 struct drsuapi_DsGetNCChangesRequest10 *req10,
1431 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1432 struct ldb_dn *search_dn,
1433 const char *extra_filter,
1434 struct ldb_result **search_res)
1436 /* we have nothing to do in case of ex-op failure */
1437 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1438 return WERR_OK;
1441 /* TODO: implement extended op specific collection
1442 * of objects. Right now we just normal procedure
1443 * for collecting objects */
1444 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1448 drsuapi_DsGetNCChanges
1450 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1452 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1453 struct drsuapi_DsGetNCChanges *r)
1455 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1456 int ret;
1457 uint32_t i;
1458 struct dsdb_schema *schema;
1459 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1460 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1461 NTSTATUS status;
1462 DATA_BLOB session_key;
1463 WERROR werr;
1464 struct dcesrv_handle *h;
1465 struct drsuapi_bind_state *b_state;
1466 struct drsuapi_getncchanges_state *getnc_state;
1467 struct drsuapi_DsGetNCChangesRequest10 *req10;
1468 uint32_t options;
1469 uint32_t max_objects;
1470 uint32_t max_links;
1471 uint32_t link_count = 0;
1472 uint32_t link_total = 0;
1473 uint32_t link_given = 0;
1474 struct ldb_dn *search_dn = NULL;
1475 bool am_rodc, null_scope=false;
1476 enum security_user_level security_level;
1477 struct ldb_context *sam_ctx;
1478 struct dom_sid *user_sid;
1479 bool is_secret_request;
1480 bool is_gc_pas_request;
1481 struct drsuapi_changed_objects *changes;
1482 time_t max_wait;
1483 time_t start = time(NULL);
1484 bool max_wait_reached = false;
1485 bool has_get_all_changes = false;
1487 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1488 b_state = h->data;
1490 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1492 *r->out.level_out = 6;
1493 /* TODO: linked attributes*/
1494 r->out.ctr->ctr6.linked_attributes_count = 0;
1495 r->out.ctr->ctr6.linked_attributes = NULL;
1497 r->out.ctr->ctr6.object_count = 0;
1498 r->out.ctr->ctr6.nc_object_count = 0;
1499 r->out.ctr->ctr6.more_data = false;
1500 r->out.ctr->ctr6.uptodateness_vector = NULL;
1502 /* a RODC doesn't allow for any replication */
1503 ret = samdb_rodc(sam_ctx, &am_rodc);
1504 if (ret == LDB_SUCCESS && am_rodc) {
1505 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1506 return WERR_DS_DRA_SOURCE_DISABLED;
1509 /* Check request revision.
1511 switch (r->in.level) {
1512 case 8:
1513 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1514 if (req10 == NULL) {
1515 return WERR_NOMEM;
1517 break;
1518 case 10:
1519 req10 = &r->in.req->req10;
1520 break;
1521 default:
1522 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1523 r->in.level));
1524 return WERR_REVISION_MISMATCH;
1528 /* Perform access checks. */
1529 /* TODO: we need to support a sync on a specific non-root
1530 * DN. We'll need to find the real partition root here */
1531 ncRoot = req10->naming_context;
1532 if (ncRoot == NULL) {
1533 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1534 return WERR_DS_DRA_INVALID_PARAMETER;
1537 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1538 return WERR_DS_DRA_INTERNAL_ERROR;
1541 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1542 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1543 return WERR_DS_DRA_SOURCE_DISABLED;
1546 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1548 /* all clients must have GUID_DRS_GET_CHANGES */
1549 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1550 mem_ctx,
1551 dce_call->conn->auth_state.session_info->security_token,
1552 req10->naming_context,
1553 GUID_DRS_GET_CHANGES);
1554 if (!W_ERROR_IS_OK(werr)) {
1555 return werr;
1558 /* allowed if the GC PAS and client has
1559 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1560 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1561 if (!W_ERROR_IS_OK(werr)) {
1562 return werr;
1564 if (is_gc_pas_request) {
1565 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1566 mem_ctx,
1567 dce_call->conn->auth_state.session_info->security_token,
1568 req10->naming_context,
1569 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1570 if (W_ERROR_IS_OK(werr)) {
1571 goto allowed;
1575 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1576 if (!W_ERROR_IS_OK(werr)) {
1577 return werr;
1579 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1580 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1581 mem_ctx,
1582 dce_call->conn->auth_state.session_info->security_token,
1583 req10->naming_context,
1584 GUID_DRS_GET_ALL_CHANGES);
1585 if (!W_ERROR_IS_OK(werr)) {
1586 return werr;
1587 } else {
1588 has_get_all_changes = true;
1592 allowed:
1593 /* for non-administrator replications, check that they have
1594 given the correct source_dsa_invocation_id */
1595 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1596 samdb_domain_sid(sam_ctx));
1597 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1598 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1599 /* we rely on this flag being unset for RODC requests */
1600 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1604 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1605 /* Ignore the _in_ uptpdateness vector*/
1606 req10->uptodateness_vector = NULL;
1609 getnc_state = b_state->getncchanges_state;
1611 /* see if a previous replication has been abandoned */
1612 if (getnc_state) {
1613 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1614 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1615 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1616 ldb_dn_get_linearized(new_dn),
1617 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1618 ldb_dn_get_linearized(getnc_state->last_dn)));
1619 talloc_free(getnc_state);
1620 getnc_state = NULL;
1624 if (getnc_state) {
1625 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1626 &req10->highwatermark);
1627 if (ret != 0) {
1628 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1629 "on DN %s %s highwatermark (last_dn %s)\n",
1630 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1631 (ret > 0) ? "older" : "newer",
1632 ldb_dn_get_linearized(getnc_state->last_dn)));
1633 talloc_free(getnc_state);
1634 getnc_state = NULL;
1638 if (getnc_state == NULL) {
1639 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1640 if (getnc_state == NULL) {
1641 return WERR_NOMEM;
1643 b_state->getncchanges_state = getnc_state;
1644 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1646 /* find out if we are to replicate Schema NC */
1647 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1648 ldb_get_schema_basedn(b_state->sam_ctx));
1649 getnc_state->is_schema_nc = (0 == ret);
1651 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1652 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1656 * This is the first replication cycle and it is
1657 * a good place to handle extended operations
1659 * FIXME: we don't fully support extended operations yet
1661 switch (req10->extended_op) {
1662 case DRSUAPI_EXOP_NONE:
1663 break;
1664 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1665 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1666 W_ERROR_NOT_OK_RETURN(werr);
1667 search_dn = ldb_get_default_basedn(sam_ctx);
1668 break;
1669 case DRSUAPI_EXOP_REPL_SECRET:
1670 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1671 user_sid,
1672 &r->out.ctr->ctr6,
1673 has_get_all_changes);
1674 r->out.result = werr;
1675 W_ERROR_NOT_OK_RETURN(werr);
1676 break;
1677 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1678 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1679 W_ERROR_NOT_OK_RETURN(werr);
1680 break;
1681 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1682 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1683 W_ERROR_NOT_OK_RETURN(werr);
1684 break;
1685 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1686 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1687 W_ERROR_NOT_OK_RETURN(werr);
1688 break;
1689 case DRSUAPI_EXOP_REPL_OBJ:
1690 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1691 r->out.result = werr;
1692 W_ERROR_NOT_OK_RETURN(werr);
1693 break;
1695 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1697 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1698 (unsigned)req10->extended_op));
1699 return WERR_DS_DRA_NOT_SUPPORTED;
1703 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1704 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1705 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1706 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1707 return WERR_DS_DRA_INVALID_PARAMETER;
1710 /* we need the session key for encrypting password attributes */
1711 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1712 if (!NT_STATUS_IS_OK(status)) {
1713 DEBUG(0,(__location__ ": Failed to get session key\n"));
1714 return WERR_DS_DRA_INTERNAL_ERROR;
1718 TODO: MS-DRSR section 4.1.10.1.1
1719 Work out if this is the start of a new cycle */
1721 if (getnc_state->guids == NULL) {
1722 const char *extra_filter;
1723 struct ldb_result *search_res = NULL;
1725 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1727 getnc_state->min_usn = req10->highwatermark.highest_usn;
1729 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1730 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1731 search_dn, extra_filter,
1732 &search_res);
1733 } else {
1734 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1735 &r->out.ctr->ctr6,
1736 search_dn, extra_filter,
1737 &search_res);
1739 W_ERROR_NOT_OK_RETURN(werr);
1741 /* extract out the GUIDs list */
1742 getnc_state->num_records = search_res ? search_res->count : 0;
1743 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1744 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1746 changes = talloc_array(getnc_state,
1747 struct drsuapi_changed_objects,
1748 getnc_state->num_records);
1749 W_ERROR_HAVE_NO_MEMORY(changes);
1751 for (i=0; i<getnc_state->num_records; i++) {
1752 changes[i].dn = search_res->msgs[i]->dn;
1753 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1754 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1757 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1758 TYPESAFE_QSORT(changes,
1759 getnc_state->num_records,
1760 site_res_cmp_parent_order);
1761 } else {
1762 TYPESAFE_QSORT(changes,
1763 getnc_state->num_records,
1764 site_res_cmp_dn_usn_order);
1767 for (i=0; i < getnc_state->num_records; i++) {
1768 getnc_state->guids[i] = changes[i].guid;
1769 if (GUID_all_zero(&getnc_state->guids[i])) {
1770 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1771 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1772 return WERR_DS_DRA_INTERNAL_ERROR;
1776 talloc_free(search_res);
1777 talloc_free(changes);
1780 if (req10->uptodateness_vector) {
1781 /* make sure its sorted */
1782 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
1783 req10->uptodateness_vector->count,
1784 drsuapi_DsReplicaCursor_compare);
1787 /* Prefix mapping */
1788 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1789 if (!schema) {
1790 DEBUG(0,("No schema in sam_ctx\n"));
1791 return WERR_DS_DRA_INTERNAL_ERROR;
1794 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1795 *r->out.ctr->ctr6.naming_context = *ncRoot;
1797 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1798 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1799 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1800 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1801 return WERR_DS_DRA_INTERNAL_ERROR;
1804 /* find the SID if there is one */
1805 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1807 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1808 r->out.ctr->ctr6.mapping_ctr = *ctr;
1810 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1811 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1813 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1814 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1816 r->out.ctr->ctr6.first_object = NULL;
1817 currentObject = &r->out.ctr->ctr6.first_object;
1819 /* use this to force single objects at a time, which is useful
1820 * for working out what object is giving problems
1822 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1823 if (req10->max_object_count < max_objects) {
1824 max_objects = req10->max_object_count;
1827 * TODO: work out how the maximum should be calculated
1829 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1832 * Maximum time that we can spend in a getncchanges
1833 * in order to avoid timeout of the other part.
1834 * 10 seconds by default.
1836 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1837 for (i=getnc_state->num_processed;
1838 i<getnc_state->num_records &&
1839 !null_scope &&
1840 (r->out.ctr->ctr6.object_count < max_objects)
1841 && !max_wait_reached;
1842 i++) {
1843 int uSN;
1844 struct drsuapi_DsReplicaObjectListItemEx *obj;
1845 struct ldb_message *msg;
1846 static const char * const msg_attrs[] = {
1847 "*",
1848 "nTSecurityDescriptor",
1849 "parentGUID",
1850 "replPropertyMetaData",
1851 DSDB_SECRET_ATTRIBUTES,
1852 NULL };
1853 struct ldb_result *msg_res;
1854 struct ldb_dn *msg_dn;
1856 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1857 W_ERROR_HAVE_NO_MEMORY(obj);
1859 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1860 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1863 /* by re-searching here we avoid having a lot of full
1864 * records in memory between calls to getncchanges
1866 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1867 msg_dn,
1868 LDB_SCOPE_BASE, msg_attrs, NULL);
1869 if (ret != LDB_SUCCESS) {
1870 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1871 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1872 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1874 talloc_free(obj);
1875 continue;
1878 msg = msg_res->msgs[0];
1880 max_wait_reached = (time(NULL) - start > max_wait);
1882 werr = get_nc_changes_build_object(obj, msg,
1883 sam_ctx, getnc_state->ncRoot_dn,
1884 getnc_state->is_schema_nc,
1885 schema, &session_key, getnc_state->min_usn,
1886 req10->replica_flags,
1887 req10->partial_attribute_set,
1888 req10->uptodateness_vector,
1889 req10->extended_op,
1890 max_wait_reached);
1891 if (!W_ERROR_IS_OK(werr)) {
1892 return werr;
1895 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1896 getnc_state->ncRoot_dn,
1897 schema, getnc_state->min_usn,
1898 req10->replica_flags,
1899 msg,
1900 &getnc_state->la_list,
1901 &getnc_state->la_count,
1902 req10->uptodateness_vector);
1903 if (!W_ERROR_IS_OK(werr)) {
1904 return werr;
1907 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1908 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1909 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1910 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
1913 if (obj->meta_data_ctr == NULL) {
1914 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1915 ldb_dn_get_linearized(msg->dn)));
1916 /* no attributes to send */
1917 talloc_free(obj);
1918 continue;
1921 r->out.ctr->ctr6.object_count++;
1923 *currentObject = obj;
1924 currentObject = &obj->next_object;
1926 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1928 talloc_free(getnc_state->last_dn);
1929 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
1931 talloc_free(msg_res);
1932 talloc_free(msg_dn);
1935 getnc_state->num_processed = i;
1937 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1939 /* the client can us to call UpdateRefs on its behalf to
1940 re-establish monitoring of the NC */
1941 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1942 !GUID_all_zero(&req10->destination_dsa_guid)) {
1943 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1944 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1945 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1946 ureq.naming_context = ncRoot;
1947 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
1948 &req10->destination_dsa_guid);
1949 if (!ureq.dest_dsa_dns_name) {
1950 return WERR_NOMEM;
1952 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1953 ureq.options = DRSUAPI_DRS_ADD_REF |
1954 DRSUAPI_DRS_ASYNC_OP |
1955 DRSUAPI_DRS_GETCHG_CHECK;
1957 /* we also need to pass through the
1958 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1959 to send notifies using the GC SPN */
1960 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1962 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1963 if (!W_ERROR_IS_OK(werr)) {
1964 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1965 win_errstr(werr)));
1970 * TODO:
1971 * This is just a guess, how to calculate the
1972 * number of linked attributes to send, we need to
1973 * find out how to do this right.
1975 if (r->out.ctr->ctr6.object_count >= max_links) {
1976 max_links = 0;
1977 } else {
1978 max_links -= r->out.ctr->ctr6.object_count;
1981 link_total = getnc_state->la_count;
1983 if (i < getnc_state->num_records) {
1984 r->out.ctr->ctr6.more_data = true;
1985 } else {
1986 /* sort the whole array the first time */
1987 if (!getnc_state->la_sorted) {
1988 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1989 sam_ctx, linked_attribute_compare);
1990 getnc_state->la_sorted = true;
1993 link_count = getnc_state->la_count - getnc_state->la_idx;
1994 link_count = MIN(max_links, link_count);
1996 r->out.ctr->ctr6.linked_attributes_count = link_count;
1997 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1999 getnc_state->la_idx += link_count;
2000 link_given = getnc_state->la_idx;
2002 if (getnc_state->la_idx < getnc_state->la_count) {
2003 r->out.ctr->ctr6.more_data = true;
2007 if (!r->out.ctr->ctr6.more_data) {
2008 talloc_steal(mem_ctx, getnc_state->la_list);
2010 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
2011 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
2012 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
2014 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
2015 r->out.ctr->ctr6.uptodateness_vector);
2016 if (!W_ERROR_IS_OK(werr)) {
2017 return werr;
2020 talloc_free(getnc_state);
2021 b_state->getncchanges_state = NULL;
2022 } else {
2023 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2024 &r->out.ctr->ctr6.new_highwatermark);
2025 if (ret == 0) {
2027 * We need to make sure that we never return the
2028 * same highwatermark within the same replication
2029 * cycle more than once. Otherwise we cannot detect
2030 * when the client uses an unexptected highwatermark.
2032 * This is a HACK which is needed because our
2033 * object ordering is wrong and set tmp_highest_usn
2034 * to a value that is higher than what we already
2035 * sent to the client (destination dsa).
2037 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2040 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2043 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2044 r->out.ctr->ctr6.uptodateness_vector = NULL;
2045 r->out.ctr->ctr6.nc_object_count = 0;
2046 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2049 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2050 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2051 (unsigned long long)(req10->highwatermark.highest_usn+1),
2052 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2053 r->out.ctr->ctr6.object_count,
2054 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2055 r->out.ctr->ctr6.linked_attributes_count,
2056 link_given, link_total,
2057 dom_sid_string(mem_ctx, user_sid)));
2059 #if 0
2060 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2061 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2063 #endif
2065 return WERR_OK;