s4:drsuapi: always use the current uptodateness_vector
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / getncchanges.c
blobb3ceb50db71e45e0bb25f2a8b9fa11f658bd57ac
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 ldb_dn *last_dn;
49 struct drsuapi_DsReplicaLinkedAttribute *la_list;
50 uint32_t la_count;
51 bool la_sorted;
52 uint32_t la_idx;
56 build a DsReplicaObjectIdentifier from a ldb msg
58 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
59 struct ldb_message *msg)
61 struct drsuapi_DsReplicaObjectIdentifier *identifier;
62 struct dom_sid *sid;
64 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
65 if (identifier == NULL) {
66 return NULL;
69 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
70 identifier->guid = samdb_result_guid(msg, "objectGUID");
72 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
73 if (sid) {
74 identifier->sid = *sid;
75 } else {
76 ZERO_STRUCT(identifier->sid);
78 return identifier;
81 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
83 return GUID_compare(guid1, &guid2);
87 see if we can filter an attribute using the uptodateness_vector
89 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
90 const struct GUID *originating_invocation_id,
91 uint64_t originating_usn)
93 const struct drsuapi_DsReplicaCursor *c;
94 if (udv == NULL) return false;
95 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
96 originating_invocation_id, udv_compare, c);
97 if (c && originating_usn <= c->highest_usn) {
98 return true;
100 return false;
104 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
106 if (a1 == a2) return 0;
107 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
111 check if an attribute is in a partial_attribute_set
113 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
114 struct drsuapi_DsPartialAttributeSet *pas)
116 enum drsuapi_DsAttributeId *result;
117 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
118 attid_cmp, result);
119 return result != NULL;
124 drsuapi_DsGetNCChanges for one object
126 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
127 struct ldb_message *msg,
128 struct ldb_context *sam_ctx,
129 struct ldb_dn *ncRoot_dn,
130 bool is_schema_nc,
131 struct dsdb_schema *schema,
132 DATA_BLOB *session_key,
133 uint64_t highest_usn,
134 uint32_t replica_flags,
135 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
136 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
137 enum drsuapi_DsExtendedOperation extended_op,
138 bool force_object_return)
140 const struct ldb_val *md_value;
141 uint32_t i, n;
142 struct replPropertyMetaDataBlob md;
143 uint32_t rid = 0;
144 enum ndr_err_code ndr_err;
145 uint32_t *attids;
146 const char *rdn;
147 const struct dsdb_attribute *rdn_sa;
148 unsigned int instanceType;
149 struct dsdb_syntax_ctx syntax_ctx;
151 /* make dsdb sytanx context for conversions */
152 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
153 syntax_ctx.is_schema_nc = is_schema_nc;
155 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
156 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
157 obj->is_nc_prefix = true;
158 obj->parent_object_guid = NULL;
159 } else {
160 obj->is_nc_prefix = false;
161 obj->parent_object_guid = talloc(obj, struct GUID);
162 if (obj->parent_object_guid == NULL) {
163 return WERR_DS_DRA_INTERNAL_ERROR;
165 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
166 if (GUID_all_zero(obj->parent_object_guid)) {
167 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
168 ldb_dn_get_linearized(msg->dn)));
169 return WERR_DS_DRA_INTERNAL_ERROR;
172 obj->next_object = NULL;
174 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
175 if (!md_value) {
176 /* nothing to send */
177 return WERR_OK;
180 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
181 /* don't send uninstantiated objects */
182 return WERR_OK;
185 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
186 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
187 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
188 return WERR_DS_DRA_INTERNAL_ERROR;
191 if (md.version != 1) {
192 return WERR_DS_DRA_INTERNAL_ERROR;
195 rdn = ldb_dn_get_rdn_name(msg->dn);
196 if (rdn == NULL) {
197 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
198 return WERR_DS_DRA_INTERNAL_ERROR;
201 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
202 if (rdn_sa == NULL) {
203 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
204 rdn, ldb_dn_get_linearized(msg->dn)));
205 return WERR_DS_DRA_INTERNAL_ERROR;
208 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
209 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
211 obj->object.identifier = get_object_identifier(obj, msg);
212 if (obj->object.identifier == NULL) {
213 return WERR_NOMEM;
215 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
217 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
218 for (n=i=0; i<md.ctr.ctr1.count; i++) {
219 const struct dsdb_attribute *sa;
220 bool force_attribute = false;
222 /* if the attribute has not changed, and it is not the
223 instanceType then don't include it */
224 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
225 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
226 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
228 /* don't include the rDN */
229 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
231 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
232 if (!sa) {
233 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
234 (unsigned int)md.ctr.ctr1.array[i].attid,
235 ldb_dn_get_linearized(msg->dn)));
236 return WERR_DS_DRA_INTERNAL_ERROR;
239 if (sa->linkID) {
240 struct ldb_message_element *el;
241 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
242 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
243 /* don't send upgraded links inline */
244 continue;
248 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
249 !dsdb_attr_in_rodc_fas(sa)) {
250 force_attribute = true;
251 DEBUG(4,("Forcing attribute %s in %s\n",
252 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
255 /* filter by uptodateness_vector */
256 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
257 !force_attribute &&
258 udv_filter(uptodateness_vector,
259 &md.ctr.ctr1.array[i].originating_invocation_id,
260 md.ctr.ctr1.array[i].originating_usn)) {
261 continue;
264 /* filter by partial_attribute_set */
265 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
266 continue;
269 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
270 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
271 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
272 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
273 attids[n] = md.ctr.ctr1.array[i].attid;
274 n++;
277 /* ignore it if its an empty change. Note that renames always
278 * change the 'name' attribute, so they won't be ignored by
279 * this
281 * the force_object_return check is used to force an empty
282 * object return when we timeout in the getncchanges loop.
283 * This allows us to return an empty object, which keeps the
284 * client happy while preventing timeouts
286 if (n == 0 ||
287 (n == 1 &&
288 attids[0] == DRSUAPI_ATTID_instanceType &&
289 !force_object_return)) {
290 talloc_free(obj->meta_data_ctr);
291 obj->meta_data_ctr = NULL;
292 return WERR_OK;
295 obj->meta_data_ctr->count = n;
297 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
298 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
299 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
300 obj->object.attribute_ctr.num_attributes);
301 if (obj->object.attribute_ctr.attributes == NULL) {
302 return WERR_NOMEM;
306 * Note that the meta_data array and the attributes array must
307 * be the same size and in the same order
309 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
310 struct ldb_message_element *el;
311 WERROR werr;
312 const struct dsdb_attribute *sa;
314 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
315 if (!sa) {
316 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
317 return WERR_DS_DRA_INTERNAL_ERROR;
320 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
321 if (el == NULL) {
322 /* this happens for attributes that have been removed */
323 DEBUG(5,("No element '%s' for attributeID %u in message\n",
324 sa->lDAPDisplayName, attids[i]));
325 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
326 obj->object.attribute_ctr.attributes[i].attid =
327 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
328 } else {
329 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
330 &obj->object.attribute_ctr.attributes[i]);
331 if (!W_ERROR_IS_OK(werr)) {
332 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
333 sa->lDAPDisplayName, win_errstr(werr)));
334 return werr;
336 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
337 * check if attribute is secret and send a null value
339 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
340 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
341 &obj->meta_data_ctr->meta_data[i]);
343 /* some attributes needs to be encrypted
344 before being sent */
345 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
346 &obj->object.attribute_ctr.attributes[i]);
347 if (!W_ERROR_IS_OK(werr)) {
348 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
349 sa->lDAPDisplayName, win_errstr(werr)));
350 return werr;
355 return WERR_OK;
359 add one linked attribute from an object to the list of linked
360 attributes in a getncchanges request
362 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
363 struct ldb_context *sam_ctx,
364 const struct dsdb_schema *schema,
365 const struct dsdb_attribute *sa,
366 struct ldb_message *msg,
367 struct dsdb_dn *dsdb_dn,
368 struct drsuapi_DsReplicaLinkedAttribute **la_list,
369 uint32_t *la_count)
371 struct drsuapi_DsReplicaLinkedAttribute *la;
372 bool active;
373 NTSTATUS status;
374 WERROR werr;
376 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
377 W_ERROR_HAVE_NO_MEMORY(*la_list);
379 la = &(*la_list)[*la_count];
381 la->identifier = get_object_identifier(*la_list, msg);
382 W_ERROR_HAVE_NO_MEMORY(la->identifier);
384 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
386 if (!active) {
387 /* We have to check that the inactive link still point to an existing object */
388 struct GUID guid;
389 struct ldb_dn *tdn;
390 int ret;
391 const char *v;
393 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
394 if (strncmp(v, "TRUE", 4) == 0) {
396 * Note: we skip the transmition of the deleted link even if the other part used to
397 * know about it because when we transmit the deletion of the object, the link will
398 * be deleted too due to deletion of object where link points and Windows do so.
400 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
401 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
403 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
404 * if it join an existing domain with deleted objets, it firsts impose to have a
405 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
406 * either during initial replication or after the getNCChanges.
407 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
409 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
410 * If FL >=2K8R2 we are sure that this attribute will be here.
411 * For this kind of forest level we do not return the link if the object is recycled
412 * (isRecycled = true).
414 if (strncmp(v, "TRUE", 4) == 0) {
415 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
416 ldb_dn_get_linearized(msg->dn)));
417 return WERR_OK;
419 } else {
420 return WERR_OK;
423 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
424 if (!NT_STATUS_IS_OK(status)) {
425 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
426 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
427 return ntstatus_to_werror(status);
429 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
430 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
431 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
432 GUID_string(mem_ctx, &guid)));
433 return WERR_OK;
434 } else if (ret != LDB_SUCCESS) {
435 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
436 GUID_string(mem_ctx, &guid),
437 ret));
438 return WERR_OK;
441 la->attid = sa->attributeID_id;
442 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
444 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
445 if (!NT_STATUS_IS_OK(status)) {
446 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
447 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
448 return ntstatus_to_werror(status);
450 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
451 if (!NT_STATUS_IS_OK(status)) {
452 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
453 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
454 return ntstatus_to_werror(status);
456 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
457 if (!NT_STATUS_IS_OK(status)) {
458 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
459 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
460 return ntstatus_to_werror(status);
462 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
463 if (!NT_STATUS_IS_OK(status)) {
464 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
465 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
466 return ntstatus_to_werror(status);
469 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
470 if (!NT_STATUS_IS_OK(status)) {
471 /* this is possible for upgraded links */
472 la->originating_add_time = la->meta_data.originating_change_time;
475 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
476 W_ERROR_NOT_OK_RETURN(werr);
478 (*la_count)++;
479 return WERR_OK;
484 add linked attributes from an object to the list of linked
485 attributes in a getncchanges request
487 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
488 TALLOC_CTX *mem_ctx,
489 struct ldb_dn *ncRoot_dn,
490 struct dsdb_schema *schema,
491 uint64_t highest_usn,
492 uint32_t replica_flags,
493 struct ldb_message *msg,
494 struct drsuapi_DsReplicaLinkedAttribute **la_list,
495 uint32_t *la_count,
496 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
498 unsigned int i;
499 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
500 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
502 for (i=0; i<msg->num_elements; i++) {
503 struct ldb_message_element *el = &msg->elements[i];
504 const struct dsdb_attribute *sa;
505 unsigned int j;
507 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
509 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
510 /* we only want forward links */
511 continue;
514 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
515 /* its an old style link, it will have been
516 * sent in the main replication data */
517 continue;
520 for (j=0; j<el->num_values; j++) {
521 struct dsdb_dn *dsdb_dn;
522 uint64_t local_usn;
523 NTSTATUS status;
524 WERROR werr;
526 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
527 if (dsdb_dn == NULL) {
528 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
529 el->name, ldb_dn_get_linearized(msg->dn)));
530 talloc_free(tmp_ctx);
531 return WERR_DS_DRA_INTERNAL_ERROR;
534 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
535 if (!NT_STATUS_IS_OK(status)) {
536 /* this can happen for attributes
537 given to us with old style meta
538 data */
539 continue;
542 if (local_usn > uSNChanged) {
543 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
544 el->name, ldb_dn_get_linearized(msg->dn)));
545 talloc_free(tmp_ctx);
546 return WERR_DS_DRA_INTERNAL_ERROR;
549 if (local_usn < highest_usn) {
550 continue;
553 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
554 dsdb_dn, la_list, la_count);
555 if (!W_ERROR_IS_OK(werr)) {
556 talloc_free(tmp_ctx);
557 return werr;
562 talloc_free(tmp_ctx);
563 return WERR_OK;
567 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
569 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
570 struct ldb_dn *ncRoot_dn,
571 struct drsuapi_DsReplicaCursor2CtrEx *udv)
573 int ret;
575 udv->version = 2;
576 udv->reserved1 = 0;
577 udv->reserved2 = 0;
579 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
580 if (ret != LDB_SUCCESS) {
581 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
582 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
583 return WERR_DS_DRA_INTERNAL_ERROR;
586 return WERR_OK;
590 /* comparison function for linked attributes - see CompareLinks() in
591 * MS-DRSR section 4.1.10.5.17 */
592 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
593 const struct drsuapi_DsReplicaLinkedAttribute *la2,
594 struct ldb_context *sam_ctx)
596 int c;
597 WERROR werr;
598 TALLOC_CTX *tmp_ctx;
599 const struct dsdb_schema *schema;
600 const struct dsdb_attribute *schema_attrib;
601 struct dsdb_dn *dn1, *dn2;
602 struct GUID guid1, guid2;
603 NTSTATUS status;
605 c = GUID_compare(&la1->identifier->guid,
606 &la2->identifier->guid);
607 if (c != 0) return c;
609 if (la1->attid != la2->attid) {
610 return la1->attid < la2->attid? -1:1;
613 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
614 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
615 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
618 /* we need to get the target GUIDs to compare */
619 tmp_ctx = talloc_new(sam_ctx);
621 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
622 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
624 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
625 if (!W_ERROR_IS_OK(werr)) {
626 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
627 talloc_free(tmp_ctx);
628 return 0;
631 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
632 if (!W_ERROR_IS_OK(werr)) {
633 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
634 talloc_free(tmp_ctx);
635 return 0;
638 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
639 if (!NT_STATUS_IS_OK(status)) {
640 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
641 talloc_free(tmp_ctx);
642 return 0;
644 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
645 if (!NT_STATUS_IS_OK(status)) {
646 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
647 talloc_free(tmp_ctx);
648 return 0;
651 talloc_free(tmp_ctx);
653 return GUID_compare(&guid1, &guid2);
656 struct drsuapi_changed_objects {
657 struct ldb_dn *dn;
658 struct GUID guid;
659 uint64_t usn;
663 sort the objects we send by tree order
665 static int site_res_cmp_parent_order(struct drsuapi_changed_objects *m1,
666 struct drsuapi_changed_objects *m2)
668 return ldb_dn_compare(m2->dn, m1->dn);
672 sort the objects we send first by uSNChanged
674 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects *m1,
675 struct drsuapi_changed_objects *m2)
677 unsigned usnchanged1, usnchanged2;
678 unsigned cn1, cn2;
680 cn1 = ldb_dn_get_comp_num(m1->dn);
681 cn2 = ldb_dn_get_comp_num(m2->dn);
682 if (cn1 != cn2) {
683 return cn1 > cn2 ? 1 : -1;
685 usnchanged1 = m1->usn;
686 usnchanged2 = m2->usn;
687 if (usnchanged1 == usnchanged2) {
688 return 0;
690 return usnchanged1 > usnchanged2 ? 1 : -1;
695 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
697 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
698 TALLOC_CTX *mem_ctx,
699 struct drsuapi_DsGetNCChangesRequest10 *req10,
700 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
702 struct ldb_dn *rid_manager_dn, *req_dn;
703 int ret;
704 struct ldb_context *ldb = b_state->sam_ctx;
705 struct ldb_result *ext_res;
706 struct dsdb_fsmo_extended_op *exop;
707 bool is_us;
710 steps:
711 - verify that the DN being asked for is the RID Manager DN
712 - verify that we are the RID Manager
715 /* work out who is the RID Manager */
716 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
717 if (ret != LDB_SUCCESS) {
718 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
719 return WERR_DS_DRA_INTERNAL_ERROR;
722 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
723 if (!ldb_dn_validate(req_dn) ||
724 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
725 /* that isn't the RID Manager DN */
726 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
727 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
728 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
729 return WERR_OK;
732 /* find the DN of the RID Manager */
733 ret = samdb_reference_dn_is_our_ntdsa(ldb, rid_manager_dn, "fSMORoleOwner", &is_us);
734 if (ret != LDB_SUCCESS) {
735 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
736 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
737 return WERR_DS_DRA_INTERNAL_ERROR;
740 if (!is_us) {
741 /* we're not the RID Manager - go away */
742 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
743 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
744 return WERR_OK;
747 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
748 W_ERROR_HAVE_NO_MEMORY(exop);
750 exop->fsmo_info = req10->fsmo_info;
751 exop->destination_dsa_guid = req10->destination_dsa_guid;
753 ret = ldb_transaction_start(ldb);
754 if (ret != LDB_SUCCESS) {
755 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
756 ldb_errstring(ldb)));
757 return WERR_DS_DRA_INTERNAL_ERROR;
761 * FIXME (kim): this is a temp hack to return just few object,
762 * but not the whole domain NC.
763 * We should remove this hack and implement a 'scope'
764 * building function to return just the set of object
765 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
767 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
769 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
770 if (ret != LDB_SUCCESS) {
771 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
772 ldb_errstring(ldb)));
773 ldb_transaction_cancel(ldb);
774 return WERR_DS_DRA_INTERNAL_ERROR;
777 ret = ldb_transaction_commit(ldb);
778 if (ret != LDB_SUCCESS) {
779 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
780 ldb_errstring(ldb)));
781 return WERR_DS_DRA_INTERNAL_ERROR;
784 talloc_free(ext_res);
786 DEBUG(2,("Allocated RID pool for server %s\n",
787 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
789 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
791 return WERR_OK;
795 return an array of SIDs from a ldb_message given an attribute name
796 assumes the SIDs are in extended DN format
798 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
799 struct ldb_message *msg,
800 TALLOC_CTX *mem_ctx,
801 const char *attr,
802 const struct dom_sid ***sids)
804 struct ldb_message_element *el;
805 unsigned int i;
807 el = ldb_msg_find_element(msg, attr);
808 if (!el) {
809 *sids = NULL;
810 return WERR_OK;
813 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
814 W_ERROR_HAVE_NO_MEMORY(*sids);
816 for (i=0; i<el->num_values; i++) {
817 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
818 NTSTATUS status;
819 struct dom_sid *sid;
821 sid = talloc(*sids, struct dom_sid);
822 W_ERROR_HAVE_NO_MEMORY(sid);
823 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
824 if (!NT_STATUS_IS_OK(status)) {
825 return WERR_INTERNAL_DB_CORRUPTION;
827 (*sids)[i] = sid;
829 (*sids)[i] = NULL;
831 return WERR_OK;
836 return an array of SIDs from a ldb_message given an attribute name
837 assumes the SIDs are in NDR form
839 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
840 struct ldb_message *msg,
841 TALLOC_CTX *mem_ctx,
842 const char *attr,
843 const struct dom_sid ***sids)
845 struct ldb_message_element *el;
846 unsigned int i;
848 el = ldb_msg_find_element(msg, attr);
849 if (!el) {
850 *sids = NULL;
851 return WERR_OK;
854 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
855 W_ERROR_HAVE_NO_MEMORY(*sids);
857 for (i=0; i<el->num_values; i++) {
858 enum ndr_err_code ndr_err;
859 struct dom_sid *sid;
861 sid = talloc(*sids, struct dom_sid);
862 W_ERROR_HAVE_NO_MEMORY(sid);
864 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
865 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
866 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
867 return WERR_INTERNAL_DB_CORRUPTION;
869 (*sids)[i] = sid;
871 (*sids)[i] = NULL;
873 return WERR_OK;
877 see if any SIDs in list1 are in list2
879 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
881 unsigned int i, j;
882 /* do we ever have enough SIDs here to worry about O(n^2) ? */
883 for (i=0; list1[i]; i++) {
884 for (j=0; list2[j]; j++) {
885 if (dom_sid_equal(list1[i], list2[j])) {
886 return true;
890 return false;
894 handle a DRSUAPI_EXOP_REPL_SECRET call
896 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
897 TALLOC_CTX *mem_ctx,
898 struct drsuapi_DsGetNCChangesRequest10 *req10,
899 struct dom_sid *user_sid,
900 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
901 bool has_get_all_changes)
903 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
904 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
905 int ret;
906 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
907 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
908 struct ldb_result *rodc_res, *obj_res;
909 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
910 WERROR werr;
912 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
913 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
916 * we need to work out if we will allow this DC to
917 * replicate the secrets for this object
919 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
920 * of this function
923 if (b_state->sam_ctx_system == NULL) {
924 /* this operation needs system level access */
925 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
926 return WERR_DS_DRA_SOURCE_DISABLED;
930 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
932 * The pseudo code indicate
933 * revealsecrets = true
934 * if IsRevealSecretRequest(msgIn) then
935 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
936 * then
937 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
938 * <... check if this account is ok to be replicated on this DC ...>
939 * <... and if not reveal secrets = no ...>
940 * else
941 * reveal secrets = false
942 * endif
943 * endif
944 * endif
946 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
947 * then you can do EXOP_REPL_SECRETS
949 if (has_get_all_changes) {
950 goto allowed;
953 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
954 if (!ldb_dn_validate(obj_dn)) goto failed;
956 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
957 dom_sid_string(mem_ctx, user_sid));
958 if (!ldb_dn_validate(rodc_dn)) goto failed;
960 /* do the two searches we need */
961 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
962 DSDB_SEARCH_SHOW_EXTENDED_DN);
963 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
965 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
966 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
968 /* if the object SID is equal to the user_sid, allow */
969 if (dom_sid_equal(user_sid,
970 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
971 goto allowed;
974 /* an RODC is allowed to get its own krbtgt account secrets */
975 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
976 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
977 if (krbtgt_link_dn != NULL &&
978 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
979 goto allowed;
982 /* but it isn't allowed to get anyone elses krbtgt secrets */
983 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
984 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
985 goto denied;
988 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
989 "userAccountControl", 0) &
990 UF_INTERDOMAIN_TRUST_ACCOUNT) {
991 goto denied;
994 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
995 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
996 if (!W_ERROR_IS_OK(werr)) {
997 goto denied;
1000 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1001 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1002 if (!W_ERROR_IS_OK(werr)) {
1003 goto denied;
1006 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1007 mem_ctx, "tokenGroups", &token_sids);
1008 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1009 goto denied;
1012 if (never_reveal_sids &&
1013 sid_list_match(token_sids, never_reveal_sids)) {
1014 goto denied;
1017 if (reveal_sids &&
1018 sid_list_match(token_sids, reveal_sids)) {
1019 goto allowed;
1022 /* default deny */
1023 denied:
1024 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1025 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1026 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1027 return WERR_DS_DRA_ACCESS_DENIED;
1029 allowed:
1030 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1031 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1032 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1033 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1034 req10->highwatermark.highest_usn = 0;
1035 return WERR_OK;
1037 failed:
1038 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1039 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1040 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1041 return WERR_DS_DRA_BAD_DN;
1046 handle a DRSUAPI_EXOP_REPL_OBJ call
1048 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1049 TALLOC_CTX *mem_ctx,
1050 struct drsuapi_DsGetNCChangesRequest10 *req10,
1051 struct dom_sid *user_sid,
1052 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1054 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1056 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1057 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1059 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1060 return WERR_OK;
1065 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1066 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1067 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1069 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1070 TALLOC_CTX *mem_ctx,
1071 struct drsuapi_DsGetNCChangesRequest10 *req10,
1072 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1074 struct ldb_dn *req_dn, *ntds_dn;
1075 int ret;
1076 unsigned int i;
1077 struct ldb_context *ldb = b_state->sam_ctx;
1078 struct ldb_message *msg;
1079 bool is_us;
1082 steps:
1083 - verify that the client dn exists
1084 - verify that we are the current master
1087 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1088 if (!ldb_dn_validate(req_dn)) {
1089 /* that is not a valid dn */
1090 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1091 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1092 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1093 return WERR_OK;
1096 /* retrieve the current role owner */
1097 /* find the DN of the RID Manager */
1098 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1099 if (ret != LDB_SUCCESS) {
1100 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1101 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1102 return WERR_DS_DRA_INTERNAL_ERROR;
1105 if (!is_us) {
1106 /* we're not the RID Manager - go away */
1107 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1108 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1109 return WERR_OK;
1112 /* change the current master */
1113 msg = ldb_msg_new(ldb);
1114 W_ERROR_HAVE_NO_MEMORY(msg);
1115 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1116 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1118 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1119 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1120 if (ret != LDB_SUCCESS) {
1121 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1122 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1123 talloc_free(msg);
1124 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1125 return WERR_OK;
1128 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1129 if (ret != 0) {
1130 talloc_free(msg);
1131 return WERR_DS_DRA_INTERNAL_ERROR;
1134 for (i=0;i<msg->num_elements;i++) {
1135 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1138 ret = ldb_transaction_start(ldb);
1139 if (ret != LDB_SUCCESS) {
1140 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1141 ldb_errstring(ldb)));
1142 return WERR_DS_DRA_INTERNAL_ERROR;
1145 ret = ldb_modify(ldb, msg);
1146 if (ret != LDB_SUCCESS) {
1147 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1148 ldb_errstring(ldb)));
1149 ldb_transaction_cancel(ldb);
1150 return WERR_DS_DRA_INTERNAL_ERROR;
1153 ret = ldb_transaction_commit(ldb);
1154 if (ret != LDB_SUCCESS) {
1155 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1156 ldb_errstring(ldb)));
1157 return WERR_DS_DRA_INTERNAL_ERROR;
1160 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1162 return WERR_OK;
1166 see if this getncchanges request includes a request to reveal secret information
1168 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1169 struct drsuapi_DsGetNCChangesRequest10 *req10,
1170 bool *is_secret_request)
1172 enum drsuapi_DsExtendedOperation exop;
1173 uint32_t i;
1174 struct dsdb_schema *schema;
1176 *is_secret_request = true;
1178 exop = req10->extended_op;
1180 switch (exop) {
1181 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1182 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1183 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1184 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1185 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1186 /* FSMO exops can reveal secrets */
1187 *is_secret_request = true;
1188 return WERR_OK;
1189 case DRSUAPI_EXOP_REPL_SECRET:
1190 case DRSUAPI_EXOP_REPL_OBJ:
1191 case DRSUAPI_EXOP_NONE:
1192 break;
1195 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1196 *is_secret_request = false;
1197 return WERR_OK;
1200 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1201 req10->partial_attribute_set == NULL) {
1202 /* they want secrets */
1203 *is_secret_request = true;
1204 return WERR_OK;
1207 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1209 /* check the attributes they asked for */
1210 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1211 const struct dsdb_attribute *sa;
1212 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1213 if (sa == NULL) {
1214 return WERR_DS_DRA_SCHEMA_MISMATCH;
1216 if (!dsdb_attr_in_rodc_fas(sa)) {
1217 *is_secret_request = true;
1218 return WERR_OK;
1222 if (req10->partial_attribute_set_ex) {
1223 /* check the extended attributes they asked for */
1224 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1225 const struct dsdb_attribute *sa;
1226 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1227 if (sa == NULL) {
1228 return WERR_DS_DRA_SCHEMA_MISMATCH;
1230 if (!dsdb_attr_in_rodc_fas(sa)) {
1231 *is_secret_request = true;
1232 return WERR_OK;
1237 *is_secret_request = false;
1238 return WERR_OK;
1242 see if this getncchanges request is only for attributes in the GC
1243 partial attribute set
1245 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1246 struct drsuapi_DsGetNCChangesRequest10 *req10,
1247 bool *is_gc_pas_request)
1249 enum drsuapi_DsExtendedOperation exop;
1250 uint32_t i;
1251 struct dsdb_schema *schema;
1253 exop = req10->extended_op;
1255 switch (exop) {
1256 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1257 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1258 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1259 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1260 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1261 case DRSUAPI_EXOP_REPL_SECRET:
1262 *is_gc_pas_request = false;
1263 return WERR_OK;
1264 case DRSUAPI_EXOP_REPL_OBJ:
1265 case DRSUAPI_EXOP_NONE:
1266 break;
1269 if (req10->partial_attribute_set == NULL) {
1270 /* they want it all */
1271 *is_gc_pas_request = false;
1272 return WERR_OK;
1275 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1277 /* check the attributes they asked for */
1278 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1279 const struct dsdb_attribute *sa;
1280 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1281 if (sa == NULL) {
1282 return WERR_DS_DRA_SCHEMA_MISMATCH;
1284 if (!sa->isMemberOfPartialAttributeSet) {
1285 *is_gc_pas_request = false;
1286 return WERR_OK;
1290 if (req10->partial_attribute_set_ex) {
1291 /* check the extended attributes they asked for */
1292 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1293 const struct dsdb_attribute *sa;
1294 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1295 if (sa == NULL) {
1296 return WERR_DS_DRA_SCHEMA_MISMATCH;
1298 if (!sa->isMemberOfPartialAttributeSet) {
1299 *is_gc_pas_request = false;
1300 return WERR_OK;
1305 *is_gc_pas_request = true;
1306 return WERR_OK;
1311 map from req8 to req10
1313 static struct drsuapi_DsGetNCChangesRequest10 *
1314 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1315 struct drsuapi_DsGetNCChangesRequest8 *req8)
1317 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1318 struct drsuapi_DsGetNCChangesRequest10);
1319 if (req10 == NULL) {
1320 return NULL;
1323 req10->destination_dsa_guid = req8->destination_dsa_guid;
1324 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1325 req10->naming_context = req8->naming_context;
1326 req10->highwatermark = req8->highwatermark;
1327 req10->uptodateness_vector = req8->uptodateness_vector;
1328 req10->replica_flags = req8->replica_flags;
1329 req10->max_object_count = req8->max_object_count;
1330 req10->max_ndr_size = req8->max_ndr_size;
1331 req10->extended_op = req8->extended_op;
1332 req10->fsmo_info = req8->fsmo_info;
1333 req10->partial_attribute_set = req8->partial_attribute_set;
1334 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1335 req10->mapping_ctr = req8->mapping_ctr;
1337 return req10;
1342 * Collects object for normal replication cycle.
1344 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1345 TALLOC_CTX *mem_ctx,
1346 struct drsuapi_DsGetNCChangesRequest10 *req10,
1347 struct ldb_dn *search_dn,
1348 const char *extra_filter,
1349 struct ldb_result **search_res)
1351 int ret;
1352 char* search_filter;
1353 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1354 //const char *extra_filter;
1355 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1356 const char *attrs[] = { "uSNChanged",
1357 "objectGUID" ,
1358 NULL };
1360 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1361 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1362 scope = LDB_SCOPE_BASE;
1365 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1367 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1369 /* Construct response. */
1370 search_filter = talloc_asprintf(mem_ctx,
1371 "(uSNChanged>=%llu)",
1372 (unsigned long long)(getnc_state->min_usn+1));
1374 if (extra_filter) {
1375 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1378 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1379 search_filter = talloc_asprintf(mem_ctx,
1380 "(&%s(isCriticalSystemObject=TRUE))",
1381 search_filter);
1384 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1385 scope = LDB_SCOPE_BASE;
1388 if (!search_dn) {
1389 search_dn = getnc_state->ncRoot_dn;
1392 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1393 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1394 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1395 search_dn, scope, attrs,
1396 search_filter);
1397 if (ret != LDB_SUCCESS) {
1398 return WERR_DS_DRA_INTERNAL_ERROR;
1401 return WERR_OK;
1405 * Collects object for normal replication cycle.
1407 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1408 TALLOC_CTX *mem_ctx,
1409 struct drsuapi_DsGetNCChangesRequest10 *req10,
1410 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1411 struct ldb_dn *search_dn,
1412 const char *extra_filter,
1413 struct ldb_result **search_res)
1415 /* we have nothing to do in case of ex-op failure */
1416 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1417 return WERR_OK;
1420 /* TODO: implement extended op specific collection
1421 * of objects. Right now we just normal procedure
1422 * for collecting objects */
1423 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1427 drsuapi_DsGetNCChanges
1429 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1431 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1432 struct drsuapi_DsGetNCChanges *r)
1434 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1435 int ret;
1436 uint32_t i;
1437 struct dsdb_schema *schema;
1438 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1439 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1440 NTSTATUS status;
1441 DATA_BLOB session_key;
1442 WERROR werr;
1443 struct dcesrv_handle *h;
1444 struct drsuapi_bind_state *b_state;
1445 struct drsuapi_getncchanges_state *getnc_state;
1446 struct drsuapi_DsGetNCChangesRequest10 *req10;
1447 uint32_t options;
1448 uint32_t max_objects;
1449 uint32_t max_links;
1450 uint32_t link_count = 0;
1451 uint32_t link_total = 0;
1452 uint32_t link_given = 0;
1453 struct ldb_dn *search_dn = NULL;
1454 bool am_rodc, null_scope=false;
1455 enum security_user_level security_level;
1456 struct ldb_context *sam_ctx;
1457 struct dom_sid *user_sid;
1458 bool is_secret_request;
1459 bool is_gc_pas_request;
1460 struct drsuapi_changed_objects *changes;
1461 time_t max_wait;
1462 time_t start = time(NULL);
1463 bool max_wait_reached = false;
1464 bool has_get_all_changes = false;
1466 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1467 b_state = h->data;
1469 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1471 *r->out.level_out = 6;
1472 /* TODO: linked attributes*/
1473 r->out.ctr->ctr6.linked_attributes_count = 0;
1474 r->out.ctr->ctr6.linked_attributes = NULL;
1476 r->out.ctr->ctr6.object_count = 0;
1477 r->out.ctr->ctr6.nc_object_count = 0;
1478 r->out.ctr->ctr6.more_data = false;
1479 r->out.ctr->ctr6.uptodateness_vector = NULL;
1481 /* a RODC doesn't allow for any replication */
1482 ret = samdb_rodc(sam_ctx, &am_rodc);
1483 if (ret == LDB_SUCCESS && am_rodc) {
1484 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1485 return WERR_DS_DRA_SOURCE_DISABLED;
1488 /* Check request revision.
1490 switch (r->in.level) {
1491 case 8:
1492 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1493 if (req10 == NULL) {
1494 return WERR_NOMEM;
1496 break;
1497 case 10:
1498 req10 = &r->in.req->req10;
1499 break;
1500 default:
1501 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1502 r->in.level));
1503 return WERR_REVISION_MISMATCH;
1507 /* Perform access checks. */
1508 /* TODO: we need to support a sync on a specific non-root
1509 * DN. We'll need to find the real partition root here */
1510 ncRoot = req10->naming_context;
1511 if (ncRoot == NULL) {
1512 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1513 return WERR_DS_DRA_INVALID_PARAMETER;
1516 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1517 return WERR_DS_DRA_INTERNAL_ERROR;
1520 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1521 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1522 return WERR_DS_DRA_SOURCE_DISABLED;
1525 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1527 /* all clients must have GUID_DRS_GET_CHANGES */
1528 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1529 mem_ctx,
1530 dce_call->conn->auth_state.session_info->security_token,
1531 req10->naming_context,
1532 GUID_DRS_GET_CHANGES);
1533 if (!W_ERROR_IS_OK(werr)) {
1534 return werr;
1537 /* allowed if the GC PAS and client has
1538 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1539 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1540 if (!W_ERROR_IS_OK(werr)) {
1541 return werr;
1543 if (is_gc_pas_request) {
1544 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1545 mem_ctx,
1546 dce_call->conn->auth_state.session_info->security_token,
1547 req10->naming_context,
1548 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1549 if (W_ERROR_IS_OK(werr)) {
1550 goto allowed;
1554 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1555 if (!W_ERROR_IS_OK(werr)) {
1556 return werr;
1558 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1559 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1560 mem_ctx,
1561 dce_call->conn->auth_state.session_info->security_token,
1562 req10->naming_context,
1563 GUID_DRS_GET_ALL_CHANGES);
1564 if (!W_ERROR_IS_OK(werr)) {
1565 return werr;
1566 } else {
1567 has_get_all_changes = true;
1571 allowed:
1572 /* for non-administrator replications, check that they have
1573 given the correct source_dsa_invocation_id */
1574 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1575 samdb_domain_sid(sam_ctx));
1576 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1577 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1578 /* we rely on this flag being unset for RODC requests */
1579 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1583 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1584 /* Ignore the _in_ uptpdateness vector*/
1585 req10->uptodateness_vector = NULL;
1588 getnc_state = b_state->getncchanges_state;
1590 /* see if a previous replication has been abandoned */
1591 if (getnc_state) {
1592 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1593 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1594 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1595 ldb_dn_get_linearized(new_dn),
1596 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1597 ldb_dn_get_linearized(getnc_state->last_dn)));
1598 talloc_free(getnc_state);
1599 getnc_state = NULL;
1603 if (getnc_state == NULL) {
1604 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1605 if (getnc_state == NULL) {
1606 return WERR_NOMEM;
1608 b_state->getncchanges_state = getnc_state;
1609 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1611 /* find out if we are to replicate Schema NC */
1612 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1613 ldb_get_schema_basedn(b_state->sam_ctx));
1614 getnc_state->is_schema_nc = (0 == ret);
1616 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1617 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1621 * This is the first replication cycle and it is
1622 * a good place to handle extended operations
1624 * FIXME: we don't fully support extended operations yet
1626 switch (req10->extended_op) {
1627 case DRSUAPI_EXOP_NONE:
1628 break;
1629 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1630 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1631 W_ERROR_NOT_OK_RETURN(werr);
1632 search_dn = ldb_get_default_basedn(sam_ctx);
1633 break;
1634 case DRSUAPI_EXOP_REPL_SECRET:
1635 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1636 user_sid,
1637 &r->out.ctr->ctr6,
1638 has_get_all_changes);
1639 r->out.result = werr;
1640 W_ERROR_NOT_OK_RETURN(werr);
1641 break;
1642 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1643 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1644 W_ERROR_NOT_OK_RETURN(werr);
1645 break;
1646 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1647 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1648 W_ERROR_NOT_OK_RETURN(werr);
1649 break;
1650 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1651 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1652 W_ERROR_NOT_OK_RETURN(werr);
1653 break;
1654 case DRSUAPI_EXOP_REPL_OBJ:
1655 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1656 r->out.result = werr;
1657 W_ERROR_NOT_OK_RETURN(werr);
1658 break;
1660 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1662 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1663 (unsigned)req10->extended_op));
1664 return WERR_DS_DRA_NOT_SUPPORTED;
1668 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1669 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1670 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1671 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1672 return WERR_DS_DRA_INVALID_PARAMETER;
1675 /* we need the session key for encrypting password attributes */
1676 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1677 if (!NT_STATUS_IS_OK(status)) {
1678 DEBUG(0,(__location__ ": Failed to get session key\n"));
1679 return WERR_DS_DRA_INTERNAL_ERROR;
1683 TODO: MS-DRSR section 4.1.10.1.1
1684 Work out if this is the start of a new cycle */
1686 if (getnc_state->guids == NULL) {
1687 const char *extra_filter;
1688 struct ldb_result *search_res = NULL;
1690 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1692 getnc_state->min_usn = req10->highwatermark.highest_usn;
1694 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1695 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1696 search_dn, extra_filter,
1697 &search_res);
1698 } else {
1699 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1700 &r->out.ctr->ctr6,
1701 search_dn, extra_filter,
1702 &search_res);
1704 W_ERROR_NOT_OK_RETURN(werr);
1706 /* extract out the GUIDs list */
1707 getnc_state->num_records = search_res ? search_res->count : 0;
1708 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1709 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1711 changes = talloc_array(getnc_state,
1712 struct drsuapi_changed_objects,
1713 getnc_state->num_records);
1714 W_ERROR_HAVE_NO_MEMORY(changes);
1716 for (i=0; i<getnc_state->num_records; i++) {
1717 changes[i].dn = search_res->msgs[i]->dn;
1718 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1719 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1722 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1723 TYPESAFE_QSORT(changes,
1724 getnc_state->num_records,
1725 site_res_cmp_parent_order);
1726 } else {
1727 TYPESAFE_QSORT(changes,
1728 getnc_state->num_records,
1729 site_res_cmp_dn_usn_order);
1732 for (i=0; i < getnc_state->num_records; i++) {
1733 getnc_state->guids[i] = changes[i].guid;
1734 if (GUID_all_zero(&getnc_state->guids[i])) {
1735 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1736 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1737 return WERR_DS_DRA_INTERNAL_ERROR;
1741 talloc_free(search_res);
1742 talloc_free(changes);
1745 if (req10->uptodateness_vector) {
1746 /* make sure its sorted */
1747 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
1748 req10->uptodateness_vector->count,
1749 drsuapi_DsReplicaCursor_compare);
1752 /* Prefix mapping */
1753 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1754 if (!schema) {
1755 DEBUG(0,("No schema in sam_ctx\n"));
1756 return WERR_DS_DRA_INTERNAL_ERROR;
1759 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1760 *r->out.ctr->ctr6.naming_context = *ncRoot;
1762 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1763 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1764 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1765 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1766 return WERR_DS_DRA_INTERNAL_ERROR;
1769 /* find the SID if there is one */
1770 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1772 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1773 r->out.ctr->ctr6.mapping_ctr = *ctr;
1775 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1776 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1778 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1779 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1781 r->out.ctr->ctr6.first_object = NULL;
1782 currentObject = &r->out.ctr->ctr6.first_object;
1784 /* use this to force single objects at a time, which is useful
1785 * for working out what object is giving problems
1787 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1788 if (req10->max_object_count < max_objects) {
1789 max_objects = req10->max_object_count;
1792 * TODO: work out how the maximum should be calculated
1794 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1797 * Maximum time that we can spend in a getncchanges
1798 * in order to avoid timeout of the other part.
1799 * 10 seconds by default.
1801 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1802 for (i=getnc_state->num_processed;
1803 i<getnc_state->num_records &&
1804 !null_scope &&
1805 (r->out.ctr->ctr6.object_count < max_objects)
1806 && !max_wait_reached;
1807 i++) {
1808 int uSN;
1809 struct drsuapi_DsReplicaObjectListItemEx *obj;
1810 struct ldb_message *msg;
1811 static const char * const msg_attrs[] = {
1812 "*",
1813 "nTSecurityDescriptor",
1814 "parentGUID",
1815 "replPropertyMetaData",
1816 DSDB_SECRET_ATTRIBUTES,
1817 NULL };
1818 struct ldb_result *msg_res;
1819 struct ldb_dn *msg_dn;
1821 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1822 W_ERROR_HAVE_NO_MEMORY(obj);
1824 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1825 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1828 /* by re-searching here we avoid having a lot of full
1829 * records in memory between calls to getncchanges
1831 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1832 msg_dn,
1833 LDB_SCOPE_BASE, msg_attrs, NULL);
1834 if (ret != LDB_SUCCESS) {
1835 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1836 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1837 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1839 talloc_free(obj);
1840 continue;
1843 msg = msg_res->msgs[0];
1845 max_wait_reached = (time(NULL) - start > max_wait);
1847 werr = get_nc_changes_build_object(obj, msg,
1848 sam_ctx, getnc_state->ncRoot_dn,
1849 getnc_state->is_schema_nc,
1850 schema, &session_key, getnc_state->min_usn,
1851 req10->replica_flags,
1852 req10->partial_attribute_set,
1853 req10->uptodateness_vector,
1854 req10->extended_op,
1855 max_wait_reached);
1856 if (!W_ERROR_IS_OK(werr)) {
1857 return werr;
1860 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1861 getnc_state->ncRoot_dn,
1862 schema, getnc_state->min_usn,
1863 req10->replica_flags,
1864 msg,
1865 &getnc_state->la_list,
1866 &getnc_state->la_count,
1867 req10->uptodateness_vector);
1868 if (!W_ERROR_IS_OK(werr)) {
1869 return werr;
1872 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1873 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1874 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1877 if (obj->meta_data_ctr == NULL) {
1878 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1879 ldb_dn_get_linearized(msg->dn)));
1880 /* no attributes to send */
1881 talloc_free(obj);
1882 continue;
1885 r->out.ctr->ctr6.object_count++;
1887 *currentObject = obj;
1888 currentObject = &obj->next_object;
1890 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1892 talloc_free(getnc_state->last_dn);
1893 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
1895 talloc_free(msg_res);
1896 talloc_free(msg_dn);
1899 getnc_state->num_processed = i;
1901 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1903 /* the client can us to call UpdateRefs on its behalf to
1904 re-establish monitoring of the NC */
1905 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1906 !GUID_all_zero(&req10->destination_dsa_guid)) {
1907 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1908 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1909 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1910 ureq.naming_context = ncRoot;
1911 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
1912 &req10->destination_dsa_guid);
1913 if (!ureq.dest_dsa_dns_name) {
1914 return WERR_NOMEM;
1916 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1917 ureq.options = DRSUAPI_DRS_ADD_REF |
1918 DRSUAPI_DRS_ASYNC_OP |
1919 DRSUAPI_DRS_GETCHG_CHECK;
1921 /* we also need to pass through the
1922 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1923 to send notifies using the GC SPN */
1924 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1926 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1927 if (!W_ERROR_IS_OK(werr)) {
1928 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1929 win_errstr(werr)));
1934 * TODO:
1935 * This is just a guess, how to calculate the
1936 * number of linked attributes to send, we need to
1937 * find out how to do this right.
1939 if (r->out.ctr->ctr6.object_count >= max_links) {
1940 max_links = 0;
1941 } else {
1942 max_links -= r->out.ctr->ctr6.object_count;
1945 link_total = getnc_state->la_count;
1947 if (i < getnc_state->num_records) {
1948 r->out.ctr->ctr6.more_data = true;
1949 } else {
1950 /* sort the whole array the first time */
1951 if (!getnc_state->la_sorted) {
1952 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1953 sam_ctx, linked_attribute_compare);
1954 getnc_state->la_sorted = true;
1957 link_count = getnc_state->la_count - getnc_state->la_idx;
1958 link_count = MIN(max_links, link_count);
1960 r->out.ctr->ctr6.linked_attributes_count = link_count;
1961 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1963 getnc_state->la_idx += link_count;
1964 link_given = getnc_state->la_idx;
1966 if (getnc_state->la_idx < getnc_state->la_count) {
1967 r->out.ctr->ctr6.more_data = true;
1971 if (!r->out.ctr->ctr6.more_data) {
1972 talloc_steal(mem_ctx, getnc_state->la_list);
1974 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1975 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1977 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1978 r->out.ctr->ctr6.uptodateness_vector);
1979 if (!W_ERROR_IS_OK(werr)) {
1980 return werr;
1983 talloc_free(getnc_state);
1984 b_state->getncchanges_state = NULL;
1987 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1988 r->out.ctr->ctr6.uptodateness_vector = NULL;
1989 r->out.ctr->ctr6.nc_object_count = 0;
1990 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1993 DEBUG(r->out.ctr->ctr6.more_data?4:2,
1994 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1995 (unsigned long long)(req10->highwatermark.highest_usn+1),
1996 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
1997 r->out.ctr->ctr6.object_count,
1998 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
1999 r->out.ctr->ctr6.linked_attributes_count,
2000 link_given, link_total,
2001 dom_sid_string(mem_ctx, user_sid)));
2003 #if 0
2004 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2005 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2007 #endif
2009 return WERR_OK;