s4:drsuapi: make use of LDB_TYPESAFE_QSORT() and pass getnc_state
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / getncchanges.c
blob8ea17e5bfd39d4c35ef2e941face78c29555e6fb
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 to DRS object - %s\n",
357 sa->lDAPDisplayName, win_errstr(werr)));
358 return werr;
360 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
361 * check if attribute is secret and send a null value
363 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
364 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
365 &obj->meta_data_ctr->meta_data[i]);
367 /* some attributes needs to be encrypted
368 before being sent */
369 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
370 &obj->object.attribute_ctr.attributes[i]);
371 if (!W_ERROR_IS_OK(werr)) {
372 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
373 sa->lDAPDisplayName, win_errstr(werr)));
374 return werr;
379 return WERR_OK;
383 add one linked attribute from an object to the list of linked
384 attributes in a getncchanges request
386 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
387 struct ldb_context *sam_ctx,
388 const struct dsdb_schema *schema,
389 const struct dsdb_attribute *sa,
390 struct ldb_message *msg,
391 struct dsdb_dn *dsdb_dn,
392 struct drsuapi_DsReplicaLinkedAttribute **la_list,
393 uint32_t *la_count)
395 struct drsuapi_DsReplicaLinkedAttribute *la;
396 bool active;
397 NTSTATUS status;
398 WERROR werr;
400 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
401 W_ERROR_HAVE_NO_MEMORY(*la_list);
403 la = &(*la_list)[*la_count];
405 la->identifier = get_object_identifier(*la_list, msg);
406 W_ERROR_HAVE_NO_MEMORY(la->identifier);
408 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
410 if (!active) {
411 /* We have to check that the inactive link still point to an existing object */
412 struct GUID guid;
413 struct ldb_dn *tdn;
414 int ret;
415 const char *v;
417 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
418 if (strncmp(v, "TRUE", 4) == 0) {
420 * Note: we skip the transmition of the deleted link even if the other part used to
421 * know about it because when we transmit the deletion of the object, the link will
422 * be deleted too due to deletion of object where link points and Windows do so.
424 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
425 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
427 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
428 * if it join an existing domain with deleted objets, it firsts impose to have a
429 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
430 * either during initial replication or after the getNCChanges.
431 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
433 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
434 * If FL >=2K8R2 we are sure that this attribute will be here.
435 * For this kind of forest level we do not return the link if the object is recycled
436 * (isRecycled = true).
438 if (strncmp(v, "TRUE", 4) == 0) {
439 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
440 ldb_dn_get_linearized(msg->dn)));
441 return WERR_OK;
443 } else {
444 return WERR_OK;
447 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
448 if (!NT_STATUS_IS_OK(status)) {
449 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
450 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
451 return ntstatus_to_werror(status);
453 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
454 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
455 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
456 GUID_string(mem_ctx, &guid)));
457 return WERR_OK;
458 } else if (ret != LDB_SUCCESS) {
459 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
460 GUID_string(mem_ctx, &guid),
461 ret));
462 return WERR_OK;
465 la->attid = sa->attributeID_id;
466 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
468 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
469 if (!NT_STATUS_IS_OK(status)) {
470 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
471 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
472 return ntstatus_to_werror(status);
474 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
475 if (!NT_STATUS_IS_OK(status)) {
476 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
477 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
478 return ntstatus_to_werror(status);
480 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
481 if (!NT_STATUS_IS_OK(status)) {
482 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
483 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
484 return ntstatus_to_werror(status);
486 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
487 if (!NT_STATUS_IS_OK(status)) {
488 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
489 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
490 return ntstatus_to_werror(status);
493 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
494 if (!NT_STATUS_IS_OK(status)) {
495 /* this is possible for upgraded links */
496 la->originating_add_time = la->meta_data.originating_change_time;
499 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
500 W_ERROR_NOT_OK_RETURN(werr);
502 (*la_count)++;
503 return WERR_OK;
508 add linked attributes from an object to the list of linked
509 attributes in a getncchanges request
511 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
512 TALLOC_CTX *mem_ctx,
513 struct ldb_dn *ncRoot_dn,
514 struct dsdb_schema *schema,
515 uint64_t highest_usn,
516 uint32_t replica_flags,
517 struct ldb_message *msg,
518 struct drsuapi_DsReplicaLinkedAttribute **la_list,
519 uint32_t *la_count,
520 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
522 unsigned int i;
523 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
524 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
526 for (i=0; i<msg->num_elements; i++) {
527 struct ldb_message_element *el = &msg->elements[i];
528 const struct dsdb_attribute *sa;
529 unsigned int j;
531 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
533 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
534 /* we only want forward links */
535 continue;
538 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
539 /* its an old style link, it will have been
540 * sent in the main replication data */
541 continue;
544 for (j=0; j<el->num_values; j++) {
545 struct dsdb_dn *dsdb_dn;
546 uint64_t local_usn;
547 NTSTATUS status;
548 WERROR werr;
550 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
551 if (dsdb_dn == NULL) {
552 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
553 el->name, ldb_dn_get_linearized(msg->dn)));
554 talloc_free(tmp_ctx);
555 return WERR_DS_DRA_INTERNAL_ERROR;
558 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
559 if (!NT_STATUS_IS_OK(status)) {
560 /* this can happen for attributes
561 given to us with old style meta
562 data */
563 continue;
566 if (local_usn > uSNChanged) {
567 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
568 el->name, ldb_dn_get_linearized(msg->dn)));
569 talloc_free(tmp_ctx);
570 return WERR_DS_DRA_INTERNAL_ERROR;
573 if (local_usn < highest_usn) {
574 continue;
577 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
578 dsdb_dn, la_list, la_count);
579 if (!W_ERROR_IS_OK(werr)) {
580 talloc_free(tmp_ctx);
581 return werr;
586 talloc_free(tmp_ctx);
587 return WERR_OK;
591 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
593 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
594 struct ldb_dn *ncRoot_dn,
595 struct drsuapi_DsReplicaCursor2CtrEx *udv)
597 int ret;
599 udv->version = 2;
600 udv->reserved1 = 0;
601 udv->reserved2 = 0;
603 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
604 if (ret != LDB_SUCCESS) {
605 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
606 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
607 return WERR_DS_DRA_INTERNAL_ERROR;
610 return WERR_OK;
614 /* comparison function for linked attributes - see CompareLinks() in
615 * MS-DRSR section 4.1.10.5.17 */
616 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
617 const struct drsuapi_DsReplicaLinkedAttribute *la2,
618 struct ldb_context *sam_ctx)
620 int c;
621 WERROR werr;
622 TALLOC_CTX *tmp_ctx;
623 const struct dsdb_schema *schema;
624 const struct dsdb_attribute *schema_attrib;
625 struct dsdb_dn *dn1, *dn2;
626 struct GUID guid1, guid2;
627 NTSTATUS status;
629 c = GUID_compare(&la1->identifier->guid,
630 &la2->identifier->guid);
631 if (c != 0) return c;
633 if (la1->attid != la2->attid) {
634 return la1->attid < la2->attid? -1:1;
637 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
638 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
639 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
642 /* we need to get the target GUIDs to compare */
643 tmp_ctx = talloc_new(sam_ctx);
645 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
646 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
648 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
649 if (!W_ERROR_IS_OK(werr)) {
650 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
651 talloc_free(tmp_ctx);
652 return 0;
655 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
656 if (!W_ERROR_IS_OK(werr)) {
657 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
658 talloc_free(tmp_ctx);
659 return 0;
662 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
663 if (!NT_STATUS_IS_OK(status)) {
664 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
665 talloc_free(tmp_ctx);
666 return 0;
668 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
669 if (!NT_STATUS_IS_OK(status)) {
670 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
671 talloc_free(tmp_ctx);
672 return 0;
675 talloc_free(tmp_ctx);
677 return GUID_compare(&guid1, &guid2);
680 struct drsuapi_changed_objects {
681 struct ldb_dn *dn;
682 struct GUID guid;
683 uint64_t usn;
687 sort the objects we send by tree order
689 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
690 struct drsuapi_changed_objects *m2,
691 struct drsuapi_getncchanges_state *getnc_state)
693 return ldb_dn_compare(m2->dn, m1->dn);
697 sort the objects we send first by uSNChanged
699 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
700 struct drsuapi_changed_objects *m2,
701 struct drsuapi_getncchanges_state *getnc_state)
703 unsigned usnchanged1, usnchanged2;
704 unsigned cn1, cn2;
706 cn1 = ldb_dn_get_comp_num(m1->dn);
707 cn2 = ldb_dn_get_comp_num(m2->dn);
708 if (cn1 != cn2) {
709 return cn1 > cn2 ? 1 : -1;
711 usnchanged1 = m1->usn;
712 usnchanged2 = m2->usn;
713 if (usnchanged1 == usnchanged2) {
714 return 0;
716 return usnchanged1 > usnchanged2 ? 1 : -1;
721 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
723 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
724 TALLOC_CTX *mem_ctx,
725 struct drsuapi_DsGetNCChangesRequest10 *req10,
726 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
728 struct ldb_dn *rid_manager_dn, *req_dn;
729 int ret;
730 struct ldb_context *ldb = b_state->sam_ctx;
731 struct ldb_result *ext_res;
732 struct dsdb_fsmo_extended_op *exop;
733 bool is_us;
736 steps:
737 - verify that the DN being asked for is the RID Manager DN
738 - verify that we are the RID Manager
741 /* work out who is the RID Manager */
742 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
743 if (ret != LDB_SUCCESS) {
744 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
745 return WERR_DS_DRA_INTERNAL_ERROR;
748 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
749 if (!ldb_dn_validate(req_dn) ||
750 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
751 /* that isn't the RID Manager DN */
752 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
753 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
754 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
755 return WERR_OK;
758 /* find the DN of the RID Manager */
759 ret = samdb_reference_dn_is_our_ntdsa(ldb, rid_manager_dn, "fSMORoleOwner", &is_us);
760 if (ret != LDB_SUCCESS) {
761 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
762 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
763 return WERR_DS_DRA_INTERNAL_ERROR;
766 if (!is_us) {
767 /* we're not the RID Manager - go away */
768 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
769 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
770 return WERR_OK;
773 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
774 W_ERROR_HAVE_NO_MEMORY(exop);
776 exop->fsmo_info = req10->fsmo_info;
777 exop->destination_dsa_guid = req10->destination_dsa_guid;
779 ret = ldb_transaction_start(ldb);
780 if (ret != LDB_SUCCESS) {
781 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
782 ldb_errstring(ldb)));
783 return WERR_DS_DRA_INTERNAL_ERROR;
787 * FIXME (kim): this is a temp hack to return just few object,
788 * but not the whole domain NC.
789 * We should remove this hack and implement a 'scope'
790 * building function to return just the set of object
791 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
793 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
795 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
796 if (ret != LDB_SUCCESS) {
797 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
798 ldb_errstring(ldb)));
799 ldb_transaction_cancel(ldb);
800 return WERR_DS_DRA_INTERNAL_ERROR;
803 ret = ldb_transaction_commit(ldb);
804 if (ret != LDB_SUCCESS) {
805 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
806 ldb_errstring(ldb)));
807 return WERR_DS_DRA_INTERNAL_ERROR;
810 talloc_free(ext_res);
812 DEBUG(2,("Allocated RID pool for server %s\n",
813 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
815 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
817 return WERR_OK;
821 return an array of SIDs from a ldb_message given an attribute name
822 assumes the SIDs are in extended DN format
824 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
825 struct ldb_message *msg,
826 TALLOC_CTX *mem_ctx,
827 const char *attr,
828 const struct dom_sid ***sids)
830 struct ldb_message_element *el;
831 unsigned int i;
833 el = ldb_msg_find_element(msg, attr);
834 if (!el) {
835 *sids = NULL;
836 return WERR_OK;
839 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
840 W_ERROR_HAVE_NO_MEMORY(*sids);
842 for (i=0; i<el->num_values; i++) {
843 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
844 NTSTATUS status;
845 struct dom_sid *sid;
847 sid = talloc(*sids, struct dom_sid);
848 W_ERROR_HAVE_NO_MEMORY(sid);
849 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
850 if (!NT_STATUS_IS_OK(status)) {
851 return WERR_INTERNAL_DB_CORRUPTION;
853 (*sids)[i] = sid;
855 (*sids)[i] = NULL;
857 return WERR_OK;
862 return an array of SIDs from a ldb_message given an attribute name
863 assumes the SIDs are in NDR form
865 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
866 struct ldb_message *msg,
867 TALLOC_CTX *mem_ctx,
868 const char *attr,
869 const struct dom_sid ***sids)
871 struct ldb_message_element *el;
872 unsigned int i;
874 el = ldb_msg_find_element(msg, attr);
875 if (!el) {
876 *sids = NULL;
877 return WERR_OK;
880 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
881 W_ERROR_HAVE_NO_MEMORY(*sids);
883 for (i=0; i<el->num_values; i++) {
884 enum ndr_err_code ndr_err;
885 struct dom_sid *sid;
887 sid = talloc(*sids, struct dom_sid);
888 W_ERROR_HAVE_NO_MEMORY(sid);
890 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
891 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
892 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
893 return WERR_INTERNAL_DB_CORRUPTION;
895 (*sids)[i] = sid;
897 (*sids)[i] = NULL;
899 return WERR_OK;
903 see if any SIDs in list1 are in list2
905 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
907 unsigned int i, j;
908 /* do we ever have enough SIDs here to worry about O(n^2) ? */
909 for (i=0; list1[i]; i++) {
910 for (j=0; list2[j]; j++) {
911 if (dom_sid_equal(list1[i], list2[j])) {
912 return true;
916 return false;
920 handle a DRSUAPI_EXOP_REPL_SECRET call
922 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
923 TALLOC_CTX *mem_ctx,
924 struct drsuapi_DsGetNCChangesRequest10 *req10,
925 struct dom_sid *user_sid,
926 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
927 bool has_get_all_changes)
929 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
930 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
931 int ret;
932 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
933 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
934 struct ldb_result *rodc_res, *obj_res;
935 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
936 WERROR werr;
938 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
939 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
942 * we need to work out if we will allow this DC to
943 * replicate the secrets for this object
945 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
946 * of this function
949 if (b_state->sam_ctx_system == NULL) {
950 /* this operation needs system level access */
951 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
952 return WERR_DS_DRA_SOURCE_DISABLED;
956 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
958 * The pseudo code indicate
959 * revealsecrets = true
960 * if IsRevealSecretRequest(msgIn) then
961 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
962 * then
963 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
964 * <... check if this account is ok to be replicated on this DC ...>
965 * <... and if not reveal secrets = no ...>
966 * else
967 * reveal secrets = false
968 * endif
969 * endif
970 * endif
972 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
973 * then you can do EXOP_REPL_SECRETS
975 if (has_get_all_changes) {
976 goto allowed;
979 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
980 if (!ldb_dn_validate(obj_dn)) goto failed;
982 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
983 dom_sid_string(mem_ctx, user_sid));
984 if (!ldb_dn_validate(rodc_dn)) goto failed;
986 /* do the two searches we need */
987 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
988 DSDB_SEARCH_SHOW_EXTENDED_DN);
989 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
991 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
992 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
994 /* if the object SID is equal to the user_sid, allow */
995 if (dom_sid_equal(user_sid,
996 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
997 goto allowed;
1000 /* an RODC is allowed to get its own krbtgt account secrets */
1001 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1002 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1003 if (krbtgt_link_dn != NULL &&
1004 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1005 goto allowed;
1008 /* but it isn't allowed to get anyone elses krbtgt secrets */
1009 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1010 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1011 goto denied;
1014 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1015 "userAccountControl", 0) &
1016 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1017 goto denied;
1020 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1021 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1022 if (!W_ERROR_IS_OK(werr)) {
1023 goto denied;
1026 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1027 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1028 if (!W_ERROR_IS_OK(werr)) {
1029 goto denied;
1032 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1033 mem_ctx, "tokenGroups", &token_sids);
1034 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1035 goto denied;
1038 if (never_reveal_sids &&
1039 sid_list_match(token_sids, never_reveal_sids)) {
1040 goto denied;
1043 if (reveal_sids &&
1044 sid_list_match(token_sids, reveal_sids)) {
1045 goto allowed;
1048 /* default deny */
1049 denied:
1050 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1051 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1052 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1053 return WERR_DS_DRA_ACCESS_DENIED;
1055 allowed:
1056 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1057 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1058 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1059 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1060 req10->highwatermark.highest_usn = 0;
1061 return WERR_OK;
1063 failed:
1064 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1065 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1066 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1067 return WERR_DS_DRA_BAD_DN;
1072 handle a DRSUAPI_EXOP_REPL_OBJ call
1074 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1075 TALLOC_CTX *mem_ctx,
1076 struct drsuapi_DsGetNCChangesRequest10 *req10,
1077 struct dom_sid *user_sid,
1078 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1080 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1082 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1083 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1085 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1086 return WERR_OK;
1091 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1092 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1093 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1095 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1096 TALLOC_CTX *mem_ctx,
1097 struct drsuapi_DsGetNCChangesRequest10 *req10,
1098 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1100 struct ldb_dn *req_dn, *ntds_dn;
1101 int ret;
1102 unsigned int i;
1103 struct ldb_context *ldb = b_state->sam_ctx;
1104 struct ldb_message *msg;
1105 bool is_us;
1108 steps:
1109 - verify that the client dn exists
1110 - verify that we are the current master
1113 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1114 if (!ldb_dn_validate(req_dn)) {
1115 /* that is not a valid dn */
1116 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1117 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1118 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1119 return WERR_OK;
1122 /* retrieve the current role owner */
1123 /* find the DN of the RID Manager */
1124 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1125 if (ret != LDB_SUCCESS) {
1126 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1127 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1128 return WERR_DS_DRA_INTERNAL_ERROR;
1131 if (!is_us) {
1132 /* we're not the RID Manager - go away */
1133 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1134 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1135 return WERR_OK;
1138 /* change the current master */
1139 msg = ldb_msg_new(ldb);
1140 W_ERROR_HAVE_NO_MEMORY(msg);
1141 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1142 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1144 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1145 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1146 if (ret != LDB_SUCCESS) {
1147 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1148 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1149 talloc_free(msg);
1150 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1151 return WERR_OK;
1154 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1155 if (ret != 0) {
1156 talloc_free(msg);
1157 return WERR_DS_DRA_INTERNAL_ERROR;
1160 for (i=0;i<msg->num_elements;i++) {
1161 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1164 ret = ldb_transaction_start(ldb);
1165 if (ret != LDB_SUCCESS) {
1166 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1167 ldb_errstring(ldb)));
1168 return WERR_DS_DRA_INTERNAL_ERROR;
1171 ret = ldb_modify(ldb, msg);
1172 if (ret != LDB_SUCCESS) {
1173 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1174 ldb_errstring(ldb)));
1175 ldb_transaction_cancel(ldb);
1176 return WERR_DS_DRA_INTERNAL_ERROR;
1179 ret = ldb_transaction_commit(ldb);
1180 if (ret != LDB_SUCCESS) {
1181 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1182 ldb_errstring(ldb)));
1183 return WERR_DS_DRA_INTERNAL_ERROR;
1186 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1188 return WERR_OK;
1192 see if this getncchanges request includes a request to reveal secret information
1194 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1195 struct drsuapi_DsGetNCChangesRequest10 *req10,
1196 bool *is_secret_request)
1198 enum drsuapi_DsExtendedOperation exop;
1199 uint32_t i;
1200 struct dsdb_schema *schema;
1202 *is_secret_request = true;
1204 exop = req10->extended_op;
1206 switch (exop) {
1207 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1208 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1209 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1210 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1211 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1212 /* FSMO exops can reveal secrets */
1213 *is_secret_request = true;
1214 return WERR_OK;
1215 case DRSUAPI_EXOP_REPL_SECRET:
1216 case DRSUAPI_EXOP_REPL_OBJ:
1217 case DRSUAPI_EXOP_NONE:
1218 break;
1221 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1222 *is_secret_request = false;
1223 return WERR_OK;
1226 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1227 req10->partial_attribute_set == NULL) {
1228 /* they want secrets */
1229 *is_secret_request = true;
1230 return WERR_OK;
1233 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1235 /* check the attributes they asked for */
1236 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1237 const struct dsdb_attribute *sa;
1238 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1239 if (sa == NULL) {
1240 return WERR_DS_DRA_SCHEMA_MISMATCH;
1242 if (!dsdb_attr_in_rodc_fas(sa)) {
1243 *is_secret_request = true;
1244 return WERR_OK;
1248 if (req10->partial_attribute_set_ex) {
1249 /* check the extended attributes they asked for */
1250 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1251 const struct dsdb_attribute *sa;
1252 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1253 if (sa == NULL) {
1254 return WERR_DS_DRA_SCHEMA_MISMATCH;
1256 if (!dsdb_attr_in_rodc_fas(sa)) {
1257 *is_secret_request = true;
1258 return WERR_OK;
1263 *is_secret_request = false;
1264 return WERR_OK;
1268 see if this getncchanges request is only for attributes in the GC
1269 partial attribute set
1271 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1272 struct drsuapi_DsGetNCChangesRequest10 *req10,
1273 bool *is_gc_pas_request)
1275 enum drsuapi_DsExtendedOperation exop;
1276 uint32_t i;
1277 struct dsdb_schema *schema;
1279 exop = req10->extended_op;
1281 switch (exop) {
1282 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1283 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1284 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1285 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1286 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1287 case DRSUAPI_EXOP_REPL_SECRET:
1288 *is_gc_pas_request = false;
1289 return WERR_OK;
1290 case DRSUAPI_EXOP_REPL_OBJ:
1291 case DRSUAPI_EXOP_NONE:
1292 break;
1295 if (req10->partial_attribute_set == NULL) {
1296 /* they want it all */
1297 *is_gc_pas_request = false;
1298 return WERR_OK;
1301 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1303 /* check the attributes they asked for */
1304 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1305 const struct dsdb_attribute *sa;
1306 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1307 if (sa == NULL) {
1308 return WERR_DS_DRA_SCHEMA_MISMATCH;
1310 if (!sa->isMemberOfPartialAttributeSet) {
1311 *is_gc_pas_request = false;
1312 return WERR_OK;
1316 if (req10->partial_attribute_set_ex) {
1317 /* check the extended attributes they asked for */
1318 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1319 const struct dsdb_attribute *sa;
1320 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1321 if (sa == NULL) {
1322 return WERR_DS_DRA_SCHEMA_MISMATCH;
1324 if (!sa->isMemberOfPartialAttributeSet) {
1325 *is_gc_pas_request = false;
1326 return WERR_OK;
1331 *is_gc_pas_request = true;
1332 return WERR_OK;
1337 map from req8 to req10
1339 static struct drsuapi_DsGetNCChangesRequest10 *
1340 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1341 struct drsuapi_DsGetNCChangesRequest8 *req8)
1343 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1344 struct drsuapi_DsGetNCChangesRequest10);
1345 if (req10 == NULL) {
1346 return NULL;
1349 req10->destination_dsa_guid = req8->destination_dsa_guid;
1350 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1351 req10->naming_context = req8->naming_context;
1352 req10->highwatermark = req8->highwatermark;
1353 req10->uptodateness_vector = req8->uptodateness_vector;
1354 req10->replica_flags = req8->replica_flags;
1355 req10->max_object_count = req8->max_object_count;
1356 req10->max_ndr_size = req8->max_ndr_size;
1357 req10->extended_op = req8->extended_op;
1358 req10->fsmo_info = req8->fsmo_info;
1359 req10->partial_attribute_set = req8->partial_attribute_set;
1360 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1361 req10->mapping_ctr = req8->mapping_ctr;
1363 return req10;
1368 * Collects object for normal replication cycle.
1370 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1371 TALLOC_CTX *mem_ctx,
1372 struct drsuapi_DsGetNCChangesRequest10 *req10,
1373 struct ldb_dn *search_dn,
1374 const char *extra_filter,
1375 struct ldb_result **search_res)
1377 int ret;
1378 char* search_filter;
1379 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1380 //const char *extra_filter;
1381 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1382 const char *attrs[] = { "uSNChanged",
1383 "objectGUID" ,
1384 NULL };
1386 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1387 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1388 scope = LDB_SCOPE_BASE;
1391 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1393 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1395 /* Construct response. */
1396 search_filter = talloc_asprintf(mem_ctx,
1397 "(uSNChanged>=%llu)",
1398 (unsigned long long)(getnc_state->min_usn+1));
1400 if (extra_filter) {
1401 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1404 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1405 search_filter = talloc_asprintf(mem_ctx,
1406 "(&%s(isCriticalSystemObject=TRUE))",
1407 search_filter);
1410 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1411 scope = LDB_SCOPE_BASE;
1414 if (!search_dn) {
1415 search_dn = getnc_state->ncRoot_dn;
1418 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1419 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1420 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1421 search_dn, scope, attrs,
1422 search_filter);
1423 if (ret != LDB_SUCCESS) {
1424 return WERR_DS_DRA_INTERNAL_ERROR;
1427 return WERR_OK;
1431 * Collects object for normal replication cycle.
1433 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1434 TALLOC_CTX *mem_ctx,
1435 struct drsuapi_DsGetNCChangesRequest10 *req10,
1436 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1437 struct ldb_dn *search_dn,
1438 const char *extra_filter,
1439 struct ldb_result **search_res)
1441 /* we have nothing to do in case of ex-op failure */
1442 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1443 return WERR_OK;
1446 /* TODO: implement extended op specific collection
1447 * of objects. Right now we just normal procedure
1448 * for collecting objects */
1449 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1453 drsuapi_DsGetNCChanges
1455 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1457 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1458 struct drsuapi_DsGetNCChanges *r)
1460 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1461 int ret;
1462 uint32_t i;
1463 struct dsdb_schema *schema;
1464 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1465 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1466 NTSTATUS status;
1467 DATA_BLOB session_key;
1468 WERROR werr;
1469 struct dcesrv_handle *h;
1470 struct drsuapi_bind_state *b_state;
1471 struct drsuapi_getncchanges_state *getnc_state;
1472 struct drsuapi_DsGetNCChangesRequest10 *req10;
1473 uint32_t options;
1474 uint32_t max_objects;
1475 uint32_t max_links;
1476 uint32_t link_count = 0;
1477 uint32_t link_total = 0;
1478 uint32_t link_given = 0;
1479 struct ldb_dn *search_dn = NULL;
1480 bool am_rodc, null_scope=false;
1481 enum security_user_level security_level;
1482 struct ldb_context *sam_ctx;
1483 struct dom_sid *user_sid;
1484 bool is_secret_request;
1485 bool is_gc_pas_request;
1486 struct drsuapi_changed_objects *changes;
1487 time_t max_wait;
1488 time_t start = time(NULL);
1489 bool max_wait_reached = false;
1490 bool has_get_all_changes = false;
1491 struct GUID invocation_id;
1493 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1494 b_state = h->data;
1496 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1498 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1500 *r->out.level_out = 6;
1501 /* TODO: linked attributes*/
1502 r->out.ctr->ctr6.linked_attributes_count = 0;
1503 r->out.ctr->ctr6.linked_attributes = NULL;
1505 r->out.ctr->ctr6.object_count = 0;
1506 r->out.ctr->ctr6.nc_object_count = 0;
1507 r->out.ctr->ctr6.more_data = false;
1508 r->out.ctr->ctr6.uptodateness_vector = NULL;
1510 /* a RODC doesn't allow for any replication */
1511 ret = samdb_rodc(sam_ctx, &am_rodc);
1512 if (ret == LDB_SUCCESS && am_rodc) {
1513 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1514 return WERR_DS_DRA_SOURCE_DISABLED;
1517 /* Check request revision.
1519 switch (r->in.level) {
1520 case 8:
1521 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1522 if (req10 == NULL) {
1523 return WERR_NOMEM;
1525 break;
1526 case 10:
1527 req10 = &r->in.req->req10;
1528 break;
1529 default:
1530 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1531 r->in.level));
1532 return WERR_REVISION_MISMATCH;
1536 /* Perform access checks. */
1537 /* TODO: we need to support a sync on a specific non-root
1538 * DN. We'll need to find the real partition root here */
1539 ncRoot = req10->naming_context;
1540 if (ncRoot == NULL) {
1541 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1542 return WERR_DS_DRA_INVALID_PARAMETER;
1545 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1546 return WERR_DS_DRA_INTERNAL_ERROR;
1549 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1550 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1551 return WERR_DS_DRA_SOURCE_DISABLED;
1554 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1556 /* all clients must have GUID_DRS_GET_CHANGES */
1557 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1558 mem_ctx,
1559 dce_call->conn->auth_state.session_info->security_token,
1560 req10->naming_context,
1561 GUID_DRS_GET_CHANGES);
1562 if (!W_ERROR_IS_OK(werr)) {
1563 return werr;
1566 /* allowed if the GC PAS and client has
1567 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1568 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1569 if (!W_ERROR_IS_OK(werr)) {
1570 return werr;
1572 if (is_gc_pas_request) {
1573 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1574 mem_ctx,
1575 dce_call->conn->auth_state.session_info->security_token,
1576 req10->naming_context,
1577 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1578 if (W_ERROR_IS_OK(werr)) {
1579 goto allowed;
1583 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1584 if (!W_ERROR_IS_OK(werr)) {
1585 return werr;
1587 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1588 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1589 mem_ctx,
1590 dce_call->conn->auth_state.session_info->security_token,
1591 req10->naming_context,
1592 GUID_DRS_GET_ALL_CHANGES);
1593 if (!W_ERROR_IS_OK(werr)) {
1594 return werr;
1595 } else {
1596 has_get_all_changes = true;
1600 allowed:
1601 /* for non-administrator replications, check that they have
1602 given the correct source_dsa_invocation_id */
1603 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1604 samdb_domain_sid(sam_ctx));
1605 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1606 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1607 /* we rely on this flag being unset for RODC requests */
1608 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1612 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1613 /* Ignore the _in_ uptpdateness vector*/
1614 req10->uptodateness_vector = NULL;
1617 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1618 req10->source_dsa_invocation_id = invocation_id;
1621 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1623 * The given highwatermark is only valid relative to the
1624 * specified source_dsa_invocation_id.
1626 ZERO_STRUCT(req10->highwatermark);
1629 getnc_state = b_state->getncchanges_state;
1631 /* see if a previous replication has been abandoned */
1632 if (getnc_state) {
1633 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1634 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1635 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1636 ldb_dn_get_linearized(new_dn),
1637 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1638 ldb_dn_get_linearized(getnc_state->last_dn)));
1639 talloc_free(getnc_state);
1640 getnc_state = NULL;
1644 if (getnc_state) {
1645 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1646 &req10->highwatermark);
1647 if (ret != 0) {
1648 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1649 "on DN %s %s highwatermark (last_dn %s)\n",
1650 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1651 (ret > 0) ? "older" : "newer",
1652 ldb_dn_get_linearized(getnc_state->last_dn)));
1653 talloc_free(getnc_state);
1654 getnc_state = NULL;
1658 if (getnc_state == NULL) {
1659 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1660 if (getnc_state == NULL) {
1661 return WERR_NOMEM;
1663 b_state->getncchanges_state = getnc_state;
1664 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1666 /* find out if we are to replicate Schema NC */
1667 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1668 ldb_get_schema_basedn(b_state->sam_ctx));
1669 getnc_state->is_schema_nc = (0 == ret);
1671 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1672 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1676 * This is the first replication cycle and it is
1677 * a good place to handle extended operations
1679 * FIXME: we don't fully support extended operations yet
1681 switch (req10->extended_op) {
1682 case DRSUAPI_EXOP_NONE:
1683 break;
1684 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1685 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1686 W_ERROR_NOT_OK_RETURN(werr);
1687 search_dn = ldb_get_default_basedn(sam_ctx);
1688 break;
1689 case DRSUAPI_EXOP_REPL_SECRET:
1690 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1691 user_sid,
1692 &r->out.ctr->ctr6,
1693 has_get_all_changes);
1694 r->out.result = werr;
1695 W_ERROR_NOT_OK_RETURN(werr);
1696 break;
1697 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1698 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1699 W_ERROR_NOT_OK_RETURN(werr);
1700 break;
1701 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1702 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1703 W_ERROR_NOT_OK_RETURN(werr);
1704 break;
1705 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1706 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1707 W_ERROR_NOT_OK_RETURN(werr);
1708 break;
1709 case DRSUAPI_EXOP_REPL_OBJ:
1710 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1711 r->out.result = werr;
1712 W_ERROR_NOT_OK_RETURN(werr);
1713 break;
1715 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1717 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1718 (unsigned)req10->extended_op));
1719 return WERR_DS_DRA_NOT_SUPPORTED;
1723 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1724 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1725 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1726 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1727 return WERR_DS_DRA_INVALID_PARAMETER;
1730 /* we need the session key for encrypting password attributes */
1731 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1732 if (!NT_STATUS_IS_OK(status)) {
1733 DEBUG(0,(__location__ ": Failed to get session key\n"));
1734 return WERR_DS_DRA_INTERNAL_ERROR;
1738 TODO: MS-DRSR section 4.1.10.1.1
1739 Work out if this is the start of a new cycle */
1741 if (getnc_state->guids == NULL) {
1742 const char *extra_filter;
1743 struct ldb_result *search_res = NULL;
1745 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1747 getnc_state->min_usn = req10->highwatermark.highest_usn;
1748 getnc_state->max_usn = getnc_state->min_usn;
1750 getnc_state->final_udv = talloc_zero(getnc_state,
1751 struct drsuapi_DsReplicaCursor2CtrEx);
1752 if (getnc_state->final_udv == NULL) {
1753 return WERR_NOMEM;
1755 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1756 getnc_state->final_udv);
1757 if (!W_ERROR_IS_OK(werr)) {
1758 return werr;
1761 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1762 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1763 search_dn, extra_filter,
1764 &search_res);
1765 } else {
1766 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1767 &r->out.ctr->ctr6,
1768 search_dn, extra_filter,
1769 &search_res);
1771 W_ERROR_NOT_OK_RETURN(werr);
1773 /* extract out the GUIDs list */
1774 getnc_state->num_records = search_res ? search_res->count : 0;
1775 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1776 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1778 changes = talloc_array(getnc_state,
1779 struct drsuapi_changed_objects,
1780 getnc_state->num_records);
1781 W_ERROR_HAVE_NO_MEMORY(changes);
1783 for (i=0; i<getnc_state->num_records; i++) {
1784 changes[i].dn = search_res->msgs[i]->dn;
1785 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1786 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1788 if (changes[i].usn > getnc_state->max_usn) {
1789 getnc_state->max_usn = changes[i].usn;
1793 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1794 LDB_TYPESAFE_QSORT(changes,
1795 getnc_state->num_records,
1796 getnc_state,
1797 site_res_cmp_anc_order);
1798 } else {
1799 LDB_TYPESAFE_QSORT(changes,
1800 getnc_state->num_records,
1801 getnc_state,
1802 site_res_cmp_usn_order);
1805 for (i=0; i < getnc_state->num_records; i++) {
1806 getnc_state->guids[i] = changes[i].guid;
1807 if (GUID_all_zero(&getnc_state->guids[i])) {
1808 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1809 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1810 return WERR_DS_DRA_INTERNAL_ERROR;
1814 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
1815 getnc_state->final_hwm.reserved_usn = 0;
1816 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
1818 talloc_free(search_res);
1819 talloc_free(changes);
1822 if (req10->uptodateness_vector) {
1823 /* make sure its sorted */
1824 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
1825 req10->uptodateness_vector->count,
1826 drsuapi_DsReplicaCursor_compare);
1829 /* Prefix mapping */
1830 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1831 if (!schema) {
1832 DEBUG(0,("No schema in sam_ctx\n"));
1833 return WERR_DS_DRA_INTERNAL_ERROR;
1836 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1837 *r->out.ctr->ctr6.naming_context = *ncRoot;
1839 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1840 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1841 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1842 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1843 return WERR_DS_DRA_INTERNAL_ERROR;
1846 /* find the SID if there is one */
1847 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1849 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1850 r->out.ctr->ctr6.mapping_ctr = *ctr;
1852 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1853 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1855 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1856 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1858 r->out.ctr->ctr6.first_object = NULL;
1859 currentObject = &r->out.ctr->ctr6.first_object;
1861 /* use this to force single objects at a time, which is useful
1862 * for working out what object is giving problems
1864 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1865 if (req10->max_object_count < max_objects) {
1866 max_objects = req10->max_object_count;
1869 * TODO: work out how the maximum should be calculated
1871 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1874 * Maximum time that we can spend in a getncchanges
1875 * in order to avoid timeout of the other part.
1876 * 10 seconds by default.
1878 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1879 for (i=getnc_state->num_processed;
1880 i<getnc_state->num_records &&
1881 !null_scope &&
1882 (r->out.ctr->ctr6.object_count < max_objects)
1883 && !max_wait_reached;
1884 i++) {
1885 int uSN;
1886 struct drsuapi_DsReplicaObjectListItemEx *obj;
1887 struct ldb_message *msg;
1888 static const char * const msg_attrs[] = {
1889 "*",
1890 "nTSecurityDescriptor",
1891 "parentGUID",
1892 "replPropertyMetaData",
1893 DSDB_SECRET_ATTRIBUTES,
1894 NULL };
1895 struct ldb_result *msg_res;
1896 struct ldb_dn *msg_dn;
1898 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1899 W_ERROR_HAVE_NO_MEMORY(obj);
1901 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1902 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1905 /* by re-searching here we avoid having a lot of full
1906 * records in memory between calls to getncchanges
1908 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1909 msg_dn,
1910 LDB_SCOPE_BASE, msg_attrs, NULL);
1911 if (ret != LDB_SUCCESS) {
1912 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1913 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1914 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1916 talloc_free(obj);
1917 continue;
1920 msg = msg_res->msgs[0];
1922 max_wait_reached = (time(NULL) - start > max_wait);
1924 werr = get_nc_changes_build_object(obj, msg,
1925 sam_ctx, getnc_state->ncRoot_dn,
1926 getnc_state->is_schema_nc,
1927 schema, &session_key, getnc_state->min_usn,
1928 req10->replica_flags,
1929 req10->partial_attribute_set,
1930 req10->uptodateness_vector,
1931 req10->extended_op,
1932 max_wait_reached);
1933 if (!W_ERROR_IS_OK(werr)) {
1934 return werr;
1937 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1938 getnc_state->ncRoot_dn,
1939 schema, getnc_state->min_usn,
1940 req10->replica_flags,
1941 msg,
1942 &getnc_state->la_list,
1943 &getnc_state->la_count,
1944 req10->uptodateness_vector);
1945 if (!W_ERROR_IS_OK(werr)) {
1946 return werr;
1949 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1950 if (uSN > getnc_state->max_usn) {
1952 * Only report the max_usn we had at the start
1953 * of the replication cycle.
1955 * If this object has changed lately we better
1956 * let the destination dsa refetch the change.
1957 * This is better than the risk of loosing some
1958 * objects or linked attributes.
1960 uSN = 0;
1962 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1963 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1964 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
1967 if (obj->meta_data_ctr == NULL) {
1968 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1969 ldb_dn_get_linearized(msg->dn)));
1970 /* no attributes to send */
1971 talloc_free(obj);
1972 continue;
1975 r->out.ctr->ctr6.object_count++;
1977 *currentObject = obj;
1978 currentObject = &obj->next_object;
1980 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1982 talloc_free(getnc_state->last_dn);
1983 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
1985 talloc_free(msg_res);
1986 talloc_free(msg_dn);
1989 getnc_state->num_processed = i;
1991 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1993 /* the client can us to call UpdateRefs on its behalf to
1994 re-establish monitoring of the NC */
1995 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1996 !GUID_all_zero(&req10->destination_dsa_guid)) {
1997 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1998 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1999 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2000 ureq.naming_context = ncRoot;
2001 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2002 &req10->destination_dsa_guid);
2003 if (!ureq.dest_dsa_dns_name) {
2004 return WERR_NOMEM;
2006 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2007 ureq.options = DRSUAPI_DRS_ADD_REF |
2008 DRSUAPI_DRS_ASYNC_OP |
2009 DRSUAPI_DRS_GETCHG_CHECK;
2011 /* we also need to pass through the
2012 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2013 to send notifies using the GC SPN */
2014 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2016 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2017 if (!W_ERROR_IS_OK(werr)) {
2018 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
2019 win_errstr(werr)));
2024 * TODO:
2025 * This is just a guess, how to calculate the
2026 * number of linked attributes to send, we need to
2027 * find out how to do this right.
2029 if (r->out.ctr->ctr6.object_count >= max_links) {
2030 max_links = 0;
2031 } else {
2032 max_links -= r->out.ctr->ctr6.object_count;
2035 link_total = getnc_state->la_count;
2037 if (i < getnc_state->num_records) {
2038 r->out.ctr->ctr6.more_data = true;
2039 } else {
2040 /* sort the whole array the first time */
2041 if (!getnc_state->la_sorted) {
2042 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
2043 sam_ctx, linked_attribute_compare);
2044 getnc_state->la_sorted = true;
2047 link_count = getnc_state->la_count - getnc_state->la_idx;
2048 link_count = MIN(max_links, link_count);
2050 r->out.ctr->ctr6.linked_attributes_count = link_count;
2051 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
2053 getnc_state->la_idx += link_count;
2054 link_given = getnc_state->la_idx;
2056 if (getnc_state->la_idx < getnc_state->la_count) {
2057 r->out.ctr->ctr6.more_data = true;
2061 if (!r->out.ctr->ctr6.more_data) {
2062 talloc_steal(mem_ctx, getnc_state->la_list);
2064 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2065 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2066 &getnc_state->final_udv);
2068 talloc_free(getnc_state);
2069 b_state->getncchanges_state = NULL;
2070 } else {
2071 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2072 &r->out.ctr->ctr6.new_highwatermark);
2073 if (ret == 0) {
2075 * We need to make sure that we never return the
2076 * same highwatermark within the same replication
2077 * cycle more than once. Otherwise we cannot detect
2078 * when the client uses an unexptected highwatermark.
2080 * This is a HACK which is needed because our
2081 * object ordering is wrong and set tmp_highest_usn
2082 * to a value that is higher than what we already
2083 * sent to the client (destination dsa).
2085 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2088 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2091 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2092 r->out.ctr->ctr6.uptodateness_vector = NULL;
2093 r->out.ctr->ctr6.nc_object_count = 0;
2094 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2097 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2098 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2099 (unsigned long long)(req10->highwatermark.highest_usn+1),
2100 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2101 r->out.ctr->ctr6.object_count,
2102 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2103 r->out.ctr->ctr6.linked_attributes_count,
2104 link_given, link_total,
2105 dom_sid_string(mem_ctx, user_sid)));
2107 #if 0
2108 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2109 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2111 #endif
2113 return WERR_OK;