dsdb: Allow dsdb_find_dn_by_guid to show deleted DNs
[Samba/id10ts.git] / source4 / rpc_server / drsuapi / getncchanges.c
blob5ee87cbfbfaba5845b8ed01ee46c2ebfd9a8cf99
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 uint64_t max_usn;
49 struct drsuapi_DsReplicaHighWaterMark last_hwm;
50 struct ldb_dn *last_dn;
51 struct drsuapi_DsReplicaHighWaterMark final_hwm;
52 struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
53 struct drsuapi_DsReplicaLinkedAttribute *la_list;
54 uint32_t la_count;
55 bool la_sorted;
56 uint32_t la_idx;
59 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
60 const struct drsuapi_DsReplicaHighWaterMark *h2)
62 if (h1->highest_usn < h2->highest_usn) {
63 return -1;
64 } else if (h1->highest_usn > h2->highest_usn) {
65 return 1;
66 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
67 return -1;
68 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
69 return 1;
70 } else if (h1->reserved_usn < h2->reserved_usn) {
71 return -1;
72 } else if (h1->reserved_usn > h2->reserved_usn) {
73 return 1;
76 return 0;
80 build a DsReplicaObjectIdentifier from a ldb msg
82 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
83 struct ldb_message *msg)
85 struct drsuapi_DsReplicaObjectIdentifier *identifier;
86 struct dom_sid *sid;
88 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
89 if (identifier == NULL) {
90 return NULL;
93 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
94 identifier->guid = samdb_result_guid(msg, "objectGUID");
96 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
97 if (sid) {
98 identifier->sid = *sid;
99 } else {
100 ZERO_STRUCT(identifier->sid);
102 return identifier;
105 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
107 return GUID_compare(guid1, &guid2);
111 see if we can filter an attribute using the uptodateness_vector
113 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
114 const struct GUID *originating_invocation_id,
115 uint64_t originating_usn)
117 const struct drsuapi_DsReplicaCursor *c;
118 if (udv == NULL) return false;
119 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
120 originating_invocation_id, udv_compare, c);
121 if (c && originating_usn <= c->highest_usn) {
122 return true;
124 return false;
128 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
130 if (a1 == a2) return 0;
131 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
135 check if an attribute is in a partial_attribute_set
137 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
138 struct drsuapi_DsPartialAttributeSet *pas)
140 enum drsuapi_DsAttributeId *result;
141 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
142 attid_cmp, result);
143 return result != NULL;
148 drsuapi_DsGetNCChanges for one object
150 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
151 struct ldb_message *msg,
152 struct ldb_context *sam_ctx,
153 struct ldb_dn *ncRoot_dn,
154 bool is_schema_nc,
155 struct dsdb_schema *schema,
156 DATA_BLOB *session_key,
157 uint64_t highest_usn,
158 uint32_t replica_flags,
159 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
160 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
161 enum drsuapi_DsExtendedOperation extended_op,
162 bool force_object_return)
164 const struct ldb_val *md_value;
165 uint32_t i, n;
166 struct replPropertyMetaDataBlob md;
167 uint32_t rid = 0;
168 enum ndr_err_code ndr_err;
169 uint32_t *attids;
170 const char *rdn;
171 const struct dsdb_attribute *rdn_sa;
172 unsigned int instanceType;
173 struct dsdb_syntax_ctx syntax_ctx;
175 /* make dsdb sytanx context for conversions */
176 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
177 syntax_ctx.is_schema_nc = is_schema_nc;
179 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
180 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
181 obj->is_nc_prefix = true;
182 obj->parent_object_guid = NULL;
183 } else {
184 obj->is_nc_prefix = false;
185 obj->parent_object_guid = talloc(obj, struct GUID);
186 if (obj->parent_object_guid == NULL) {
187 return WERR_DS_DRA_INTERNAL_ERROR;
189 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
190 if (GUID_all_zero(obj->parent_object_guid)) {
191 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
192 ldb_dn_get_linearized(msg->dn)));
193 return WERR_DS_DRA_INTERNAL_ERROR;
196 obj->next_object = NULL;
198 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
199 if (!md_value) {
200 /* nothing to send */
201 return WERR_OK;
204 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
205 /* don't send uninstantiated objects */
206 return WERR_OK;
209 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
210 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
211 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
212 return WERR_DS_DRA_INTERNAL_ERROR;
215 if (md.version != 1) {
216 return WERR_DS_DRA_INTERNAL_ERROR;
219 rdn = ldb_dn_get_rdn_name(msg->dn);
220 if (rdn == NULL) {
221 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
222 return WERR_DS_DRA_INTERNAL_ERROR;
225 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
226 if (rdn_sa == NULL) {
227 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
228 rdn, ldb_dn_get_linearized(msg->dn)));
229 return WERR_DS_DRA_INTERNAL_ERROR;
232 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
233 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
235 obj->object.identifier = get_object_identifier(obj, msg);
236 if (obj->object.identifier == NULL) {
237 return WERR_NOMEM;
239 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
241 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
242 for (n=i=0; i<md.ctr.ctr1.count; i++) {
243 const struct dsdb_attribute *sa;
244 bool force_attribute = false;
246 /* if the attribute has not changed, and it is not the
247 instanceType then don't include it */
248 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
249 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
250 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
252 /* don't include the rDN */
253 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
255 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
256 if (!sa) {
257 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
258 (unsigned int)md.ctr.ctr1.array[i].attid,
259 ldb_dn_get_linearized(msg->dn)));
260 return WERR_DS_DRA_INTERNAL_ERROR;
263 if (sa->linkID) {
264 struct ldb_message_element *el;
265 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
266 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
267 /* don't send upgraded links inline */
268 continue;
272 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
273 !dsdb_attr_in_rodc_fas(sa)) {
274 force_attribute = true;
275 DEBUG(4,("Forcing attribute %s in %s\n",
276 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
279 /* filter by uptodateness_vector */
280 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
281 !force_attribute &&
282 udv_filter(uptodateness_vector,
283 &md.ctr.ctr1.array[i].originating_invocation_id,
284 md.ctr.ctr1.array[i].originating_usn)) {
285 continue;
288 /* filter by partial_attribute_set */
289 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
290 continue;
293 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
294 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
295 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
296 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
297 attids[n] = md.ctr.ctr1.array[i].attid;
298 n++;
301 /* ignore it if its an empty change. Note that renames always
302 * change the 'name' attribute, so they won't be ignored by
303 * this
305 * the force_object_return check is used to force an empty
306 * object return when we timeout in the getncchanges loop.
307 * This allows us to return an empty object, which keeps the
308 * client happy while preventing timeouts
310 if (n == 0 ||
311 (n == 1 &&
312 attids[0] == DRSUAPI_ATTID_instanceType &&
313 !force_object_return)) {
314 talloc_free(obj->meta_data_ctr);
315 obj->meta_data_ctr = NULL;
316 return WERR_OK;
319 obj->meta_data_ctr->count = n;
321 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
322 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
323 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
324 obj->object.attribute_ctr.num_attributes);
325 if (obj->object.attribute_ctr.attributes == NULL) {
326 return WERR_NOMEM;
330 * Note that the meta_data array and the attributes array must
331 * be the same size and in the same order
333 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
334 struct ldb_message_element *el;
335 WERROR werr;
336 const struct dsdb_attribute *sa;
338 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
339 if (!sa) {
340 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
341 return WERR_DS_DRA_INTERNAL_ERROR;
344 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
345 if (el == NULL) {
346 /* this happens for attributes that have been removed */
347 DEBUG(5,("No element '%s' for attributeID %u in message\n",
348 sa->lDAPDisplayName, attids[i]));
349 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
350 obj->object.attribute_ctr.attributes[i].attid =
351 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
352 } else {
353 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
354 &obj->object.attribute_ctr.attributes[i]);
355 if (!W_ERROR_IS_OK(werr)) {
356 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
357 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
358 win_errstr(werr)));
359 return werr;
361 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
362 * check if attribute is secret and send a null value
364 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
365 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
366 &obj->meta_data_ctr->meta_data[i]);
368 /* some attributes needs to be encrypted
369 before being sent */
370 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
371 &obj->object.attribute_ctr.attributes[i]);
372 if (!W_ERROR_IS_OK(werr)) {
373 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
374 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
375 win_errstr(werr)));
376 return werr;
381 return WERR_OK;
385 add one linked attribute from an object to the list of linked
386 attributes in a getncchanges request
388 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
389 struct ldb_context *sam_ctx,
390 const struct dsdb_schema *schema,
391 const struct dsdb_attribute *sa,
392 struct ldb_message *msg,
393 struct dsdb_dn *dsdb_dn,
394 struct drsuapi_DsReplicaLinkedAttribute **la_list,
395 uint32_t *la_count)
397 struct drsuapi_DsReplicaLinkedAttribute *la;
398 bool active;
399 NTSTATUS status;
400 WERROR werr;
402 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
403 W_ERROR_HAVE_NO_MEMORY(*la_list);
405 la = &(*la_list)[*la_count];
407 la->identifier = get_object_identifier(*la_list, msg);
408 W_ERROR_HAVE_NO_MEMORY(la->identifier);
410 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
412 if (!active) {
413 /* We have to check that the inactive link still point to an existing object */
414 struct GUID guid;
415 struct ldb_dn *tdn;
416 int ret;
417 const char *v;
419 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
420 if (strncmp(v, "TRUE", 4) == 0) {
422 * Note: we skip the transmition of the deleted link even if the other part used to
423 * know about it because when we transmit the deletion of the object, the link will
424 * be deleted too due to deletion of object where link points and Windows do so.
426 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
427 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
429 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
430 * if it join an existing domain with deleted objets, it firsts impose to have a
431 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
432 * either during initial replication or after the getNCChanges.
433 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
435 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
436 * If FL >=2K8R2 we are sure that this attribute will be here.
437 * For this kind of forest level we do not return the link if the object is recycled
438 * (isRecycled = true).
440 if (strncmp(v, "TRUE", 4) == 0) {
441 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
442 ldb_dn_get_linearized(msg->dn)));
443 return WERR_OK;
445 } else {
446 return WERR_OK;
449 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
450 if (!NT_STATUS_IS_OK(status)) {
451 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
452 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
453 return ntstatus_to_werror(status);
455 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
456 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
457 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
458 GUID_string(mem_ctx, &guid)));
459 return WERR_OK;
460 } else if (ret != LDB_SUCCESS) {
461 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
462 GUID_string(mem_ctx, &guid),
463 ret));
464 return WERR_OK;
467 la->attid = sa->attributeID_id;
468 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
470 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
471 if (!NT_STATUS_IS_OK(status)) {
472 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
473 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
474 return ntstatus_to_werror(status);
476 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
477 if (!NT_STATUS_IS_OK(status)) {
478 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
479 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
480 return ntstatus_to_werror(status);
482 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
483 if (!NT_STATUS_IS_OK(status)) {
484 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
485 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
486 return ntstatus_to_werror(status);
488 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
489 if (!NT_STATUS_IS_OK(status)) {
490 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
491 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
492 return ntstatus_to_werror(status);
495 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
496 if (!NT_STATUS_IS_OK(status)) {
497 /* this is possible for upgraded links */
498 la->originating_add_time = la->meta_data.originating_change_time;
501 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
502 W_ERROR_NOT_OK_RETURN(werr);
504 (*la_count)++;
505 return WERR_OK;
510 add linked attributes from an object to the list of linked
511 attributes in a getncchanges request
513 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
514 TALLOC_CTX *mem_ctx,
515 struct ldb_dn *ncRoot_dn,
516 struct dsdb_schema *schema,
517 uint64_t highest_usn,
518 uint32_t replica_flags,
519 struct ldb_message *msg,
520 struct drsuapi_DsReplicaLinkedAttribute **la_list,
521 uint32_t *la_count,
522 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
524 unsigned int i;
525 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
526 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
528 for (i=0; i<msg->num_elements; i++) {
529 struct ldb_message_element *el = &msg->elements[i];
530 const struct dsdb_attribute *sa;
531 unsigned int j;
533 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
535 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
536 /* we only want forward links */
537 continue;
540 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
541 /* its an old style link, it will have been
542 * sent in the main replication data */
543 continue;
546 for (j=0; j<el->num_values; j++) {
547 struct dsdb_dn *dsdb_dn;
548 uint64_t local_usn;
549 NTSTATUS status;
550 WERROR werr;
552 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
553 if (dsdb_dn == NULL) {
554 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
555 el->name, ldb_dn_get_linearized(msg->dn)));
556 talloc_free(tmp_ctx);
557 return WERR_DS_DRA_INTERNAL_ERROR;
560 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
561 if (!NT_STATUS_IS_OK(status)) {
562 /* this can happen for attributes
563 given to us with old style meta
564 data */
565 continue;
568 if (local_usn > uSNChanged) {
569 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
570 el->name, ldb_dn_get_linearized(msg->dn)));
571 talloc_free(tmp_ctx);
572 return WERR_DS_DRA_INTERNAL_ERROR;
575 if (local_usn < highest_usn) {
576 continue;
579 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
580 dsdb_dn, la_list, la_count);
581 if (!W_ERROR_IS_OK(werr)) {
582 talloc_free(tmp_ctx);
583 return werr;
588 talloc_free(tmp_ctx);
589 return WERR_OK;
593 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
595 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
596 struct ldb_dn *ncRoot_dn,
597 struct drsuapi_DsReplicaCursor2CtrEx *udv)
599 int ret;
601 udv->version = 2;
602 udv->reserved1 = 0;
603 udv->reserved2 = 0;
605 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
606 if (ret != LDB_SUCCESS) {
607 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
608 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
609 return WERR_DS_DRA_INTERNAL_ERROR;
612 return WERR_OK;
616 /* comparison function for linked attributes - see CompareLinks() in
617 * MS-DRSR section 4.1.10.5.17 */
618 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
619 const struct drsuapi_DsReplicaLinkedAttribute *la2,
620 struct ldb_context *sam_ctx)
622 int c;
623 WERROR werr;
624 TALLOC_CTX *tmp_ctx;
625 const struct dsdb_schema *schema;
626 const struct dsdb_attribute *schema_attrib;
627 struct dsdb_dn *dn1, *dn2;
628 struct GUID guid1, guid2;
629 NTSTATUS status;
631 c = GUID_compare(&la1->identifier->guid,
632 &la2->identifier->guid);
633 if (c != 0) return c;
635 if (la1->attid != la2->attid) {
636 return la1->attid < la2->attid? -1:1;
639 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
640 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
641 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
644 /* we need to get the target GUIDs to compare */
645 tmp_ctx = talloc_new(sam_ctx);
647 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
648 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
650 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
651 if (!W_ERROR_IS_OK(werr)) {
652 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
653 talloc_free(tmp_ctx);
654 return 0;
657 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
658 if (!W_ERROR_IS_OK(werr)) {
659 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
660 talloc_free(tmp_ctx);
661 return 0;
664 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
665 if (!NT_STATUS_IS_OK(status)) {
666 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
667 talloc_free(tmp_ctx);
668 return 0;
670 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
671 if (!NT_STATUS_IS_OK(status)) {
672 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
673 talloc_free(tmp_ctx);
674 return 0;
677 talloc_free(tmp_ctx);
679 return GUID_compare(&guid1, &guid2);
682 struct drsuapi_changed_objects {
683 struct ldb_dn *dn;
684 struct GUID guid;
685 uint64_t usn;
689 sort the objects we send by tree order
691 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
692 struct drsuapi_changed_objects *m2,
693 struct drsuapi_getncchanges_state *getnc_state)
695 return ldb_dn_compare(m2->dn, m1->dn);
699 sort the objects we send first by uSNChanged
701 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
702 struct drsuapi_changed_objects *m2,
703 struct drsuapi_getncchanges_state *getnc_state)
705 int ret;
707 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
708 if (ret == 0) {
709 return -1;
712 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
713 if (ret == 0) {
714 return 1;
717 if (m1->usn == m2->usn) {
718 return ldb_dn_compare(m2->dn, m1->dn);
721 if (m1->usn < m2->usn) {
722 return -1;
725 return 1;
730 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
732 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
733 TALLOC_CTX *mem_ctx,
734 struct drsuapi_DsGetNCChangesRequest10 *req10,
735 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
737 struct ldb_dn *rid_manager_dn, *req_dn;
738 int ret;
739 struct ldb_context *ldb = b_state->sam_ctx;
740 struct ldb_result *ext_res;
741 struct dsdb_fsmo_extended_op *exop;
742 bool is_us;
745 steps:
746 - verify that the DN being asked for is the RID Manager DN
747 - verify that we are the RID Manager
750 /* work out who is the RID Manager */
751 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
752 if (ret != LDB_SUCCESS) {
753 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
754 return WERR_DS_DRA_INTERNAL_ERROR;
757 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
758 if (!ldb_dn_validate(req_dn) ||
759 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
760 /* that isn't the RID Manager DN */
761 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
762 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
763 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
764 return WERR_OK;
767 /* find the DN of the RID Manager */
768 ret = samdb_reference_dn_is_our_ntdsa(ldb, rid_manager_dn, "fSMORoleOwner", &is_us);
769 if (ret != LDB_SUCCESS) {
770 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
771 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
772 return WERR_DS_DRA_INTERNAL_ERROR;
775 if (!is_us) {
776 /* we're not the RID Manager - go away */
777 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
778 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
779 return WERR_OK;
782 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
783 W_ERROR_HAVE_NO_MEMORY(exop);
785 exop->fsmo_info = req10->fsmo_info;
786 exop->destination_dsa_guid = req10->destination_dsa_guid;
788 ret = ldb_transaction_start(ldb);
789 if (ret != LDB_SUCCESS) {
790 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
791 ldb_errstring(ldb)));
792 return WERR_DS_DRA_INTERNAL_ERROR;
796 * FIXME (kim): this is a temp hack to return just few object,
797 * but not the whole domain NC.
798 * We should remove this hack and implement a 'scope'
799 * building function to return just the set of object
800 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
802 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
804 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
805 if (ret != LDB_SUCCESS) {
806 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
807 ldb_errstring(ldb)));
808 ldb_transaction_cancel(ldb);
809 return WERR_DS_DRA_INTERNAL_ERROR;
812 ret = ldb_transaction_commit(ldb);
813 if (ret != LDB_SUCCESS) {
814 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
815 ldb_errstring(ldb)));
816 return WERR_DS_DRA_INTERNAL_ERROR;
819 talloc_free(ext_res);
821 DEBUG(2,("Allocated RID pool for server %s\n",
822 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
824 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
826 return WERR_OK;
830 return an array of SIDs from a ldb_message given an attribute name
831 assumes the SIDs are in extended DN format
833 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
834 struct ldb_message *msg,
835 TALLOC_CTX *mem_ctx,
836 const char *attr,
837 const struct dom_sid ***sids)
839 struct ldb_message_element *el;
840 unsigned int i;
842 el = ldb_msg_find_element(msg, attr);
843 if (!el) {
844 *sids = NULL;
845 return WERR_OK;
848 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
849 W_ERROR_HAVE_NO_MEMORY(*sids);
851 for (i=0; i<el->num_values; i++) {
852 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
853 NTSTATUS status;
854 struct dom_sid *sid;
856 sid = talloc(*sids, struct dom_sid);
857 W_ERROR_HAVE_NO_MEMORY(sid);
858 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
859 if (!NT_STATUS_IS_OK(status)) {
860 return WERR_INTERNAL_DB_CORRUPTION;
862 (*sids)[i] = sid;
864 (*sids)[i] = NULL;
866 return WERR_OK;
871 return an array of SIDs from a ldb_message given an attribute name
872 assumes the SIDs are in NDR form
874 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
875 struct ldb_message *msg,
876 TALLOC_CTX *mem_ctx,
877 const char *attr,
878 const struct dom_sid ***sids)
880 struct ldb_message_element *el;
881 unsigned int i;
883 el = ldb_msg_find_element(msg, attr);
884 if (!el) {
885 *sids = NULL;
886 return WERR_OK;
889 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
890 W_ERROR_HAVE_NO_MEMORY(*sids);
892 for (i=0; i<el->num_values; i++) {
893 enum ndr_err_code ndr_err;
894 struct dom_sid *sid;
896 sid = talloc(*sids, struct dom_sid);
897 W_ERROR_HAVE_NO_MEMORY(sid);
899 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
900 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
901 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
902 return WERR_INTERNAL_DB_CORRUPTION;
904 (*sids)[i] = sid;
906 (*sids)[i] = NULL;
908 return WERR_OK;
912 see if any SIDs in list1 are in list2
914 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
916 unsigned int i, j;
917 /* do we ever have enough SIDs here to worry about O(n^2) ? */
918 for (i=0; list1[i]; i++) {
919 for (j=0; list2[j]; j++) {
920 if (dom_sid_equal(list1[i], list2[j])) {
921 return true;
925 return false;
929 handle a DRSUAPI_EXOP_REPL_SECRET call
931 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
932 TALLOC_CTX *mem_ctx,
933 struct drsuapi_DsGetNCChangesRequest10 *req10,
934 struct dom_sid *user_sid,
935 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
936 bool has_get_all_changes)
938 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
939 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
940 int ret;
941 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
942 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
943 struct ldb_result *rodc_res, *obj_res;
944 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
945 WERROR werr;
947 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
948 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
951 * we need to work out if we will allow this DC to
952 * replicate the secrets for this object
954 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
955 * of this function
958 if (b_state->sam_ctx_system == NULL) {
959 /* this operation needs system level access */
960 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
961 return WERR_DS_DRA_SOURCE_DISABLED;
965 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
967 * The pseudo code indicate
968 * revealsecrets = true
969 * if IsRevealSecretRequest(msgIn) then
970 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
971 * then
972 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
973 * <... check if this account is ok to be replicated on this DC ...>
974 * <... and if not reveal secrets = no ...>
975 * else
976 * reveal secrets = false
977 * endif
978 * endif
979 * endif
981 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
982 * then you can do EXOP_REPL_SECRETS
984 if (has_get_all_changes) {
985 goto allowed;
988 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
989 if (!ldb_dn_validate(obj_dn)) goto failed;
991 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
992 dom_sid_string(mem_ctx, user_sid));
993 if (!ldb_dn_validate(rodc_dn)) goto failed;
995 /* do the two searches we need */
996 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
997 DSDB_SEARCH_SHOW_EXTENDED_DN);
998 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1000 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1001 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1003 /* if the object SID is equal to the user_sid, allow */
1004 if (dom_sid_equal(user_sid,
1005 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1006 goto allowed;
1009 /* an RODC is allowed to get its own krbtgt account secrets */
1010 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1011 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1012 if (krbtgt_link_dn != NULL &&
1013 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1014 goto allowed;
1017 /* but it isn't allowed to get anyone elses krbtgt secrets */
1018 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1019 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1020 goto denied;
1023 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1024 "userAccountControl", 0) &
1025 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1026 goto denied;
1029 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1030 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1031 if (!W_ERROR_IS_OK(werr)) {
1032 goto denied;
1035 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1036 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1037 if (!W_ERROR_IS_OK(werr)) {
1038 goto denied;
1041 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1042 mem_ctx, "tokenGroups", &token_sids);
1043 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1044 goto denied;
1047 if (never_reveal_sids &&
1048 sid_list_match(token_sids, never_reveal_sids)) {
1049 goto denied;
1052 if (reveal_sids &&
1053 sid_list_match(token_sids, reveal_sids)) {
1054 goto allowed;
1057 /* default deny */
1058 denied:
1059 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1060 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1061 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1062 return WERR_DS_DRA_ACCESS_DENIED;
1064 allowed:
1065 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1066 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1067 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1068 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1069 req10->highwatermark.highest_usn = 0;
1070 return WERR_OK;
1072 failed:
1073 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1074 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1075 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1076 return WERR_DS_DRA_BAD_DN;
1081 handle a DRSUAPI_EXOP_REPL_OBJ call
1083 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1084 TALLOC_CTX *mem_ctx,
1085 struct drsuapi_DsGetNCChangesRequest10 *req10,
1086 struct dom_sid *user_sid,
1087 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1089 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1091 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1092 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1094 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1095 return WERR_OK;
1100 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1101 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1102 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1104 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1105 TALLOC_CTX *mem_ctx,
1106 struct drsuapi_DsGetNCChangesRequest10 *req10,
1107 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1109 struct ldb_dn *req_dn, *ntds_dn;
1110 int ret;
1111 unsigned int i;
1112 struct ldb_context *ldb = b_state->sam_ctx;
1113 struct ldb_message *msg;
1114 bool is_us;
1117 steps:
1118 - verify that the client dn exists
1119 - verify that we are the current master
1122 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1123 if (!ldb_dn_validate(req_dn)) {
1124 /* that is not a valid dn */
1125 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1126 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1127 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1128 return WERR_OK;
1131 /* retrieve the current role owner */
1132 /* find the DN of the RID Manager */
1133 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1134 if (ret != LDB_SUCCESS) {
1135 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1136 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1137 return WERR_DS_DRA_INTERNAL_ERROR;
1140 if (!is_us) {
1141 /* we're not the RID Manager - go away */
1142 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1143 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1144 return WERR_OK;
1147 /* change the current master */
1148 msg = ldb_msg_new(ldb);
1149 W_ERROR_HAVE_NO_MEMORY(msg);
1150 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1151 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1153 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1154 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1155 if (ret != LDB_SUCCESS) {
1156 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1157 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1158 talloc_free(msg);
1159 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1160 return WERR_OK;
1163 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1164 if (ret != 0) {
1165 talloc_free(msg);
1166 return WERR_DS_DRA_INTERNAL_ERROR;
1169 for (i=0;i<msg->num_elements;i++) {
1170 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1173 ret = ldb_transaction_start(ldb);
1174 if (ret != LDB_SUCCESS) {
1175 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1176 ldb_errstring(ldb)));
1177 return WERR_DS_DRA_INTERNAL_ERROR;
1180 ret = ldb_modify(ldb, msg);
1181 if (ret != LDB_SUCCESS) {
1182 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1183 ldb_errstring(ldb)));
1184 ldb_transaction_cancel(ldb);
1185 return WERR_DS_DRA_INTERNAL_ERROR;
1188 ret = ldb_transaction_commit(ldb);
1189 if (ret != LDB_SUCCESS) {
1190 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1191 ldb_errstring(ldb)));
1192 return WERR_DS_DRA_INTERNAL_ERROR;
1195 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1197 return WERR_OK;
1201 see if this getncchanges request includes a request to reveal secret information
1203 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1204 struct drsuapi_DsGetNCChangesRequest10 *req10,
1205 bool *is_secret_request)
1207 enum drsuapi_DsExtendedOperation exop;
1208 uint32_t i;
1209 struct dsdb_schema *schema;
1211 *is_secret_request = true;
1213 exop = req10->extended_op;
1215 switch (exop) {
1216 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1217 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1218 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1219 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1220 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1221 /* FSMO exops can reveal secrets */
1222 *is_secret_request = true;
1223 return WERR_OK;
1224 case DRSUAPI_EXOP_REPL_SECRET:
1225 case DRSUAPI_EXOP_REPL_OBJ:
1226 case DRSUAPI_EXOP_NONE:
1227 break;
1230 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1231 *is_secret_request = false;
1232 return WERR_OK;
1235 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1236 req10->partial_attribute_set == NULL) {
1237 /* they want secrets */
1238 *is_secret_request = true;
1239 return WERR_OK;
1242 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1244 /* check the attributes they asked for */
1245 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1246 const struct dsdb_attribute *sa;
1247 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->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;
1257 if (req10->partial_attribute_set_ex) {
1258 /* check the extended attributes they asked for */
1259 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1260 const struct dsdb_attribute *sa;
1261 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1262 if (sa == NULL) {
1263 return WERR_DS_DRA_SCHEMA_MISMATCH;
1265 if (!dsdb_attr_in_rodc_fas(sa)) {
1266 *is_secret_request = true;
1267 return WERR_OK;
1272 *is_secret_request = false;
1273 return WERR_OK;
1277 see if this getncchanges request is only for attributes in the GC
1278 partial attribute set
1280 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1281 struct drsuapi_DsGetNCChangesRequest10 *req10,
1282 bool *is_gc_pas_request)
1284 enum drsuapi_DsExtendedOperation exop;
1285 uint32_t i;
1286 struct dsdb_schema *schema;
1288 exop = req10->extended_op;
1290 switch (exop) {
1291 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1292 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1293 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1294 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1295 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1296 case DRSUAPI_EXOP_REPL_SECRET:
1297 *is_gc_pas_request = false;
1298 return WERR_OK;
1299 case DRSUAPI_EXOP_REPL_OBJ:
1300 case DRSUAPI_EXOP_NONE:
1301 break;
1304 if (req10->partial_attribute_set == NULL) {
1305 /* they want it all */
1306 *is_gc_pas_request = false;
1307 return WERR_OK;
1310 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1312 /* check the attributes they asked for */
1313 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1314 const struct dsdb_attribute *sa;
1315 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->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;
1325 if (req10->partial_attribute_set_ex) {
1326 /* check the extended attributes they asked for */
1327 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1328 const struct dsdb_attribute *sa;
1329 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1330 if (sa == NULL) {
1331 return WERR_DS_DRA_SCHEMA_MISMATCH;
1333 if (!sa->isMemberOfPartialAttributeSet) {
1334 *is_gc_pas_request = false;
1335 return WERR_OK;
1340 *is_gc_pas_request = true;
1341 return WERR_OK;
1346 map from req8 to req10
1348 static struct drsuapi_DsGetNCChangesRequest10 *
1349 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1350 struct drsuapi_DsGetNCChangesRequest8 *req8)
1352 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1353 struct drsuapi_DsGetNCChangesRequest10);
1354 if (req10 == NULL) {
1355 return NULL;
1358 req10->destination_dsa_guid = req8->destination_dsa_guid;
1359 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1360 req10->naming_context = req8->naming_context;
1361 req10->highwatermark = req8->highwatermark;
1362 req10->uptodateness_vector = req8->uptodateness_vector;
1363 req10->replica_flags = req8->replica_flags;
1364 req10->max_object_count = req8->max_object_count;
1365 req10->max_ndr_size = req8->max_ndr_size;
1366 req10->extended_op = req8->extended_op;
1367 req10->fsmo_info = req8->fsmo_info;
1368 req10->partial_attribute_set = req8->partial_attribute_set;
1369 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1370 req10->mapping_ctr = req8->mapping_ctr;
1372 return req10;
1377 * Collects object for normal replication cycle.
1379 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1380 TALLOC_CTX *mem_ctx,
1381 struct drsuapi_DsGetNCChangesRequest10 *req10,
1382 struct ldb_dn *search_dn,
1383 const char *extra_filter,
1384 struct ldb_result **search_res)
1386 int ret;
1387 char* search_filter;
1388 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1389 //const char *extra_filter;
1390 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1391 const char *attrs[] = { "uSNChanged",
1392 "objectGUID" ,
1393 NULL };
1395 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1396 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1397 scope = LDB_SCOPE_BASE;
1400 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1402 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1404 /* Construct response. */
1405 search_filter = talloc_asprintf(mem_ctx,
1406 "(uSNChanged>=%llu)",
1407 (unsigned long long)(getnc_state->min_usn+1));
1409 if (extra_filter) {
1410 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1413 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1414 search_filter = talloc_asprintf(mem_ctx,
1415 "(&%s(isCriticalSystemObject=TRUE))",
1416 search_filter);
1419 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1420 scope = LDB_SCOPE_BASE;
1423 if (!search_dn) {
1424 search_dn = getnc_state->ncRoot_dn;
1427 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1428 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1429 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1430 search_dn, scope, attrs,
1431 search_filter);
1432 if (ret != LDB_SUCCESS) {
1433 return WERR_DS_DRA_INTERNAL_ERROR;
1436 return WERR_OK;
1440 * Collects object for normal replication cycle.
1442 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1443 TALLOC_CTX *mem_ctx,
1444 struct drsuapi_DsGetNCChangesRequest10 *req10,
1445 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1446 struct ldb_dn *search_dn,
1447 const char *extra_filter,
1448 struct ldb_result **search_res)
1450 /* we have nothing to do in case of ex-op failure */
1451 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1452 return WERR_OK;
1455 /* TODO: implement extended op specific collection
1456 * of objects. Right now we just normal procedure
1457 * for collecting objects */
1458 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1462 drsuapi_DsGetNCChanges
1464 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1466 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1467 struct drsuapi_DsGetNCChanges *r)
1469 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1470 int ret;
1471 uint32_t i;
1472 struct dsdb_schema *schema;
1473 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1474 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1475 NTSTATUS status;
1476 DATA_BLOB session_key;
1477 WERROR werr;
1478 struct dcesrv_handle *h;
1479 struct drsuapi_bind_state *b_state;
1480 struct drsuapi_getncchanges_state *getnc_state;
1481 struct drsuapi_DsGetNCChangesRequest10 *req10;
1482 uint32_t options;
1483 uint32_t max_objects;
1484 uint32_t max_links;
1485 uint32_t link_count = 0;
1486 uint32_t link_total = 0;
1487 uint32_t link_given = 0;
1488 struct ldb_dn *search_dn = NULL;
1489 bool am_rodc, null_scope=false;
1490 enum security_user_level security_level;
1491 struct ldb_context *sam_ctx;
1492 struct dom_sid *user_sid;
1493 bool is_secret_request;
1494 bool is_gc_pas_request;
1495 struct drsuapi_changed_objects *changes;
1496 time_t max_wait;
1497 time_t start = time(NULL);
1498 bool max_wait_reached = false;
1499 bool has_get_all_changes = false;
1500 struct GUID invocation_id;
1502 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1503 b_state = h->data;
1505 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1507 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1509 *r->out.level_out = 6;
1510 /* TODO: linked attributes*/
1511 r->out.ctr->ctr6.linked_attributes_count = 0;
1512 r->out.ctr->ctr6.linked_attributes = NULL;
1514 r->out.ctr->ctr6.object_count = 0;
1515 r->out.ctr->ctr6.nc_object_count = 0;
1516 r->out.ctr->ctr6.more_data = false;
1517 r->out.ctr->ctr6.uptodateness_vector = NULL;
1519 /* a RODC doesn't allow for any replication */
1520 ret = samdb_rodc(sam_ctx, &am_rodc);
1521 if (ret == LDB_SUCCESS && am_rodc) {
1522 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1523 return WERR_DS_DRA_SOURCE_DISABLED;
1526 /* Check request revision.
1528 switch (r->in.level) {
1529 case 8:
1530 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1531 if (req10 == NULL) {
1532 return WERR_NOMEM;
1534 break;
1535 case 10:
1536 req10 = &r->in.req->req10;
1537 break;
1538 default:
1539 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1540 r->in.level));
1541 return WERR_REVISION_MISMATCH;
1545 /* Perform access checks. */
1546 /* TODO: we need to support a sync on a specific non-root
1547 * DN. We'll need to find the real partition root here */
1548 ncRoot = req10->naming_context;
1549 if (ncRoot == NULL) {
1550 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1551 return WERR_DS_DRA_INVALID_PARAMETER;
1554 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1555 return WERR_DS_DRA_INTERNAL_ERROR;
1558 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1559 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1560 return WERR_DS_DRA_SOURCE_DISABLED;
1563 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1565 /* all clients must have GUID_DRS_GET_CHANGES */
1566 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1567 mem_ctx,
1568 dce_call->conn->auth_state.session_info->security_token,
1569 req10->naming_context,
1570 GUID_DRS_GET_CHANGES);
1571 if (!W_ERROR_IS_OK(werr)) {
1572 return werr;
1575 /* allowed if the GC PAS and client has
1576 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1577 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1578 if (!W_ERROR_IS_OK(werr)) {
1579 return werr;
1581 if (is_gc_pas_request) {
1582 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1583 mem_ctx,
1584 dce_call->conn->auth_state.session_info->security_token,
1585 req10->naming_context,
1586 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1587 if (W_ERROR_IS_OK(werr)) {
1588 goto allowed;
1592 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1593 if (!W_ERROR_IS_OK(werr)) {
1594 return werr;
1596 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1597 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1598 mem_ctx,
1599 dce_call->conn->auth_state.session_info->security_token,
1600 req10->naming_context,
1601 GUID_DRS_GET_ALL_CHANGES);
1602 if (!W_ERROR_IS_OK(werr)) {
1603 return werr;
1604 } else {
1605 has_get_all_changes = true;
1609 allowed:
1610 /* for non-administrator replications, check that they have
1611 given the correct source_dsa_invocation_id */
1612 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1613 samdb_domain_sid(sam_ctx));
1614 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1615 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1616 /* we rely on this flag being unset for RODC requests */
1617 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1621 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1622 /* Ignore the _in_ uptpdateness vector*/
1623 req10->uptodateness_vector = NULL;
1626 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1627 req10->source_dsa_invocation_id = invocation_id;
1630 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1632 * The given highwatermark is only valid relative to the
1633 * specified source_dsa_invocation_id.
1635 ZERO_STRUCT(req10->highwatermark);
1638 getnc_state = b_state->getncchanges_state;
1640 /* see if a previous replication has been abandoned */
1641 if (getnc_state) {
1642 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1643 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1644 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1645 ldb_dn_get_linearized(new_dn),
1646 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1647 ldb_dn_get_linearized(getnc_state->last_dn)));
1648 talloc_free(getnc_state);
1649 getnc_state = NULL;
1653 if (getnc_state) {
1654 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1655 &req10->highwatermark);
1656 if (ret != 0) {
1657 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1658 "on DN %s %s highwatermark (last_dn %s)\n",
1659 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1660 (ret > 0) ? "older" : "newer",
1661 ldb_dn_get_linearized(getnc_state->last_dn)));
1662 talloc_free(getnc_state);
1663 getnc_state = NULL;
1667 if (getnc_state == NULL) {
1668 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1669 if (getnc_state == NULL) {
1670 return WERR_NOMEM;
1672 b_state->getncchanges_state = getnc_state;
1673 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1675 /* find out if we are to replicate Schema NC */
1676 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1677 ldb_get_schema_basedn(b_state->sam_ctx));
1678 getnc_state->is_schema_nc = (0 == ret);
1680 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1681 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1685 * This is the first replication cycle and it is
1686 * a good place to handle extended operations
1688 * FIXME: we don't fully support extended operations yet
1690 switch (req10->extended_op) {
1691 case DRSUAPI_EXOP_NONE:
1692 break;
1693 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1694 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1695 W_ERROR_NOT_OK_RETURN(werr);
1696 search_dn = ldb_get_default_basedn(sam_ctx);
1697 break;
1698 case DRSUAPI_EXOP_REPL_SECRET:
1699 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1700 user_sid,
1701 &r->out.ctr->ctr6,
1702 has_get_all_changes);
1703 r->out.result = werr;
1704 W_ERROR_NOT_OK_RETURN(werr);
1705 break;
1706 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1707 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1708 W_ERROR_NOT_OK_RETURN(werr);
1709 break;
1710 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1711 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1712 W_ERROR_NOT_OK_RETURN(werr);
1713 break;
1714 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1715 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1716 W_ERROR_NOT_OK_RETURN(werr);
1717 break;
1718 case DRSUAPI_EXOP_REPL_OBJ:
1719 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1720 r->out.result = werr;
1721 W_ERROR_NOT_OK_RETURN(werr);
1722 break;
1724 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1726 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1727 (unsigned)req10->extended_op));
1728 return WERR_DS_DRA_NOT_SUPPORTED;
1732 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1733 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1734 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1735 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1736 return WERR_DS_DRA_INVALID_PARAMETER;
1739 /* we need the session key for encrypting password attributes */
1740 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1741 if (!NT_STATUS_IS_OK(status)) {
1742 DEBUG(0,(__location__ ": Failed to get session key\n"));
1743 return WERR_DS_DRA_INTERNAL_ERROR;
1747 TODO: MS-DRSR section 4.1.10.1.1
1748 Work out if this is the start of a new cycle */
1750 if (getnc_state->guids == NULL) {
1751 const char *extra_filter;
1752 struct ldb_result *search_res = NULL;
1754 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1756 getnc_state->min_usn = req10->highwatermark.highest_usn;
1757 getnc_state->max_usn = getnc_state->min_usn;
1759 getnc_state->final_udv = talloc_zero(getnc_state,
1760 struct drsuapi_DsReplicaCursor2CtrEx);
1761 if (getnc_state->final_udv == NULL) {
1762 return WERR_NOMEM;
1764 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1765 getnc_state->final_udv);
1766 if (!W_ERROR_IS_OK(werr)) {
1767 return werr;
1770 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1771 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1772 search_dn, extra_filter,
1773 &search_res);
1774 } else {
1775 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1776 &r->out.ctr->ctr6,
1777 search_dn, extra_filter,
1778 &search_res);
1780 W_ERROR_NOT_OK_RETURN(werr);
1782 /* extract out the GUIDs list */
1783 getnc_state->num_records = search_res ? search_res->count : 0;
1784 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1785 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1787 changes = talloc_array(getnc_state,
1788 struct drsuapi_changed_objects,
1789 getnc_state->num_records);
1790 W_ERROR_HAVE_NO_MEMORY(changes);
1792 for (i=0; i<getnc_state->num_records; i++) {
1793 changes[i].dn = search_res->msgs[i]->dn;
1794 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1795 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1797 if (changes[i].usn > getnc_state->max_usn) {
1798 getnc_state->max_usn = changes[i].usn;
1802 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1803 LDB_TYPESAFE_QSORT(changes,
1804 getnc_state->num_records,
1805 getnc_state,
1806 site_res_cmp_anc_order);
1807 } else {
1808 LDB_TYPESAFE_QSORT(changes,
1809 getnc_state->num_records,
1810 getnc_state,
1811 site_res_cmp_usn_order);
1814 for (i=0; i < getnc_state->num_records; i++) {
1815 getnc_state->guids[i] = changes[i].guid;
1816 if (GUID_all_zero(&getnc_state->guids[i])) {
1817 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1818 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1819 return WERR_DS_DRA_INTERNAL_ERROR;
1823 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
1824 getnc_state->final_hwm.reserved_usn = 0;
1825 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
1827 talloc_free(search_res);
1828 talloc_free(changes);
1831 if (req10->uptodateness_vector) {
1832 /* make sure its sorted */
1833 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
1834 req10->uptodateness_vector->count,
1835 drsuapi_DsReplicaCursor_compare);
1838 /* Prefix mapping */
1839 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1840 if (!schema) {
1841 DEBUG(0,("No schema in sam_ctx\n"));
1842 return WERR_DS_DRA_INTERNAL_ERROR;
1845 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1846 *r->out.ctr->ctr6.naming_context = *ncRoot;
1848 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1849 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1850 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1851 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1852 return WERR_DS_DRA_INTERNAL_ERROR;
1855 /* find the SID if there is one */
1856 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1858 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1859 r->out.ctr->ctr6.mapping_ctr = *ctr;
1861 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1862 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1864 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1865 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1867 r->out.ctr->ctr6.first_object = NULL;
1868 currentObject = &r->out.ctr->ctr6.first_object;
1870 /* use this to force single objects at a time, which is useful
1871 * for working out what object is giving problems
1873 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1874 if (req10->max_object_count < max_objects) {
1875 max_objects = req10->max_object_count;
1878 * TODO: work out how the maximum should be calculated
1880 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1883 * Maximum time that we can spend in a getncchanges
1884 * in order to avoid timeout of the other part.
1885 * 10 seconds by default.
1887 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1888 for (i=getnc_state->num_processed;
1889 i<getnc_state->num_records &&
1890 !null_scope &&
1891 (r->out.ctr->ctr6.object_count < max_objects)
1892 && !max_wait_reached;
1893 i++) {
1894 int uSN;
1895 struct drsuapi_DsReplicaObjectListItemEx *obj;
1896 struct ldb_message *msg;
1897 static const char * const msg_attrs[] = {
1898 "*",
1899 "nTSecurityDescriptor",
1900 "parentGUID",
1901 "replPropertyMetaData",
1902 DSDB_SECRET_ATTRIBUTES,
1903 NULL };
1904 struct ldb_result *msg_res;
1905 struct ldb_dn *msg_dn;
1907 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1908 W_ERROR_HAVE_NO_MEMORY(obj);
1910 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1911 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1914 /* by re-searching here we avoid having a lot of full
1915 * records in memory between calls to getncchanges
1917 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1918 msg_dn,
1919 LDB_SCOPE_BASE, msg_attrs, NULL);
1920 if (ret != LDB_SUCCESS) {
1921 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1922 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1923 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1925 talloc_free(obj);
1926 continue;
1929 msg = msg_res->msgs[0];
1931 max_wait_reached = (time(NULL) - start > max_wait);
1933 werr = get_nc_changes_build_object(obj, msg,
1934 sam_ctx, getnc_state->ncRoot_dn,
1935 getnc_state->is_schema_nc,
1936 schema, &session_key, getnc_state->min_usn,
1937 req10->replica_flags,
1938 req10->partial_attribute_set,
1939 req10->uptodateness_vector,
1940 req10->extended_op,
1941 max_wait_reached);
1942 if (!W_ERROR_IS_OK(werr)) {
1943 return werr;
1946 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1947 getnc_state->ncRoot_dn,
1948 schema, getnc_state->min_usn,
1949 req10->replica_flags,
1950 msg,
1951 &getnc_state->la_list,
1952 &getnc_state->la_count,
1953 req10->uptodateness_vector);
1954 if (!W_ERROR_IS_OK(werr)) {
1955 return werr;
1958 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1959 if (uSN > getnc_state->max_usn) {
1961 * Only report the max_usn we had at the start
1962 * of the replication cycle.
1964 * If this object has changed lately we better
1965 * let the destination dsa refetch the change.
1966 * This is better than the risk of loosing some
1967 * objects or linked attributes.
1969 uSN = 0;
1971 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1972 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1973 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
1976 if (obj->meta_data_ctr == NULL) {
1977 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1978 ldb_dn_get_linearized(msg->dn)));
1979 /* no attributes to send */
1980 talloc_free(obj);
1981 continue;
1984 r->out.ctr->ctr6.object_count++;
1986 *currentObject = obj;
1987 currentObject = &obj->next_object;
1989 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1991 talloc_free(getnc_state->last_dn);
1992 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
1994 talloc_free(msg_res);
1995 talloc_free(msg_dn);
1998 getnc_state->num_processed = i;
2000 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2002 /* the client can us to call UpdateRefs on its behalf to
2003 re-establish monitoring of the NC */
2004 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2005 !GUID_all_zero(&req10->destination_dsa_guid)) {
2006 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2007 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2008 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2009 ureq.naming_context = ncRoot;
2010 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2011 &req10->destination_dsa_guid);
2012 if (!ureq.dest_dsa_dns_name) {
2013 return WERR_NOMEM;
2015 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2016 ureq.options = DRSUAPI_DRS_ADD_REF |
2017 DRSUAPI_DRS_ASYNC_OP |
2018 DRSUAPI_DRS_GETCHG_CHECK;
2020 /* we also need to pass through the
2021 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2022 to send notifies using the GC SPN */
2023 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2025 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2026 if (!W_ERROR_IS_OK(werr)) {
2027 DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2028 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2029 win_errstr(werr)));
2034 * TODO:
2035 * This is just a guess, how to calculate the
2036 * number of linked attributes to send, we need to
2037 * find out how to do this right.
2039 if (r->out.ctr->ctr6.object_count >= max_links) {
2040 max_links = 0;
2041 } else {
2042 max_links -= r->out.ctr->ctr6.object_count;
2045 link_total = getnc_state->la_count;
2047 if (i < getnc_state->num_records) {
2048 r->out.ctr->ctr6.more_data = true;
2049 } else {
2050 /* sort the whole array the first time */
2051 if (!getnc_state->la_sorted) {
2052 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
2053 sam_ctx, linked_attribute_compare);
2054 getnc_state->la_sorted = true;
2057 link_count = getnc_state->la_count - getnc_state->la_idx;
2058 link_count = MIN(max_links, link_count);
2060 r->out.ctr->ctr6.linked_attributes_count = link_count;
2061 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
2063 getnc_state->la_idx += link_count;
2064 link_given = getnc_state->la_idx;
2066 if (getnc_state->la_idx < getnc_state->la_count) {
2067 r->out.ctr->ctr6.more_data = true;
2071 if (!r->out.ctr->ctr6.more_data) {
2072 talloc_steal(mem_ctx, getnc_state->la_list);
2074 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2075 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2076 &getnc_state->final_udv);
2078 talloc_free(getnc_state);
2079 b_state->getncchanges_state = NULL;
2080 } else {
2081 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2082 &r->out.ctr->ctr6.new_highwatermark);
2083 if (ret == 0) {
2085 * We need to make sure that we never return the
2086 * same highwatermark within the same replication
2087 * cycle more than once. Otherwise we cannot detect
2088 * when the client uses an unexptected highwatermark.
2090 * This is a HACK which is needed because our
2091 * object ordering is wrong and set tmp_highest_usn
2092 * to a value that is higher than what we already
2093 * sent to the client (destination dsa).
2095 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2098 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2101 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2102 r->out.ctr->ctr6.uptodateness_vector = NULL;
2103 r->out.ctr->ctr6.nc_object_count = 0;
2104 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2107 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2108 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2109 (unsigned long long)(req10->highwatermark.highest_usn+1),
2110 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2111 r->out.ctr->ctr6.object_count,
2112 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2113 r->out.ctr->ctr6.linked_attributes_count,
2114 link_given, link_total,
2115 dom_sid_string(mem_ctx, user_sid)));
2117 #if 0
2118 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2119 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2121 #endif
2123 return WERR_OK;