s4-drs: introduce a timeout in the getncchanges processing to always return something...
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / getncchanges.c
blobefd2e638f2701fd6265c6961c3ad41a7a3daca06
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"
41 build a DsReplicaObjectIdentifier from a ldb msg
43 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
44 struct ldb_message *msg)
46 struct drsuapi_DsReplicaObjectIdentifier *identifier;
47 struct dom_sid *sid;
49 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
50 if (identifier == NULL) {
51 return NULL;
54 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
55 identifier->guid = samdb_result_guid(msg, "objectGUID");
57 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
58 if (sid) {
59 identifier->sid = *sid;
60 } else {
61 ZERO_STRUCT(identifier->sid);
63 return identifier;
66 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
68 return GUID_compare(guid1, &guid2);
72 see if we can filter an attribute using the uptodateness_vector
74 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
75 const struct GUID *originating_invocation_id,
76 uint64_t originating_usn)
78 const struct drsuapi_DsReplicaCursor *c;
79 if (udv == NULL) return false;
80 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
81 originating_invocation_id, udv_compare, c);
82 if (c && originating_usn <= c->highest_usn) {
83 return true;
85 return false;
89 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
91 if (a1 == a2) return 0;
92 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
96 check if an attribute is in a partial_attribute_set
98 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
99 struct drsuapi_DsPartialAttributeSet *pas)
101 enum drsuapi_DsAttributeId *result;
102 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
103 attid_cmp, result);
104 return result != NULL;
109 drsuapi_DsGetNCChanges for one object
111 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
112 struct ldb_message *msg,
113 struct ldb_context *sam_ctx,
114 struct ldb_dn *ncRoot_dn,
115 bool is_schema_nc,
116 struct dsdb_schema *schema,
117 DATA_BLOB *session_key,
118 uint64_t highest_usn,
119 uint32_t replica_flags,
120 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
121 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
122 enum drsuapi_DsExtendedOperation extended_op,
123 bool force_object_return)
125 const struct ldb_val *md_value;
126 uint32_t i, n;
127 struct replPropertyMetaDataBlob md;
128 uint32_t rid = 0;
129 enum ndr_err_code ndr_err;
130 uint32_t *attids;
131 const char *rdn;
132 const struct dsdb_attribute *rdn_sa;
133 unsigned int instanceType;
134 struct dsdb_syntax_ctx syntax_ctx;
136 /* make dsdb sytanx context for conversions */
137 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
138 syntax_ctx.is_schema_nc = is_schema_nc;
140 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
141 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
142 obj->is_nc_prefix = true;
143 obj->parent_object_guid = NULL;
144 } else {
145 obj->is_nc_prefix = false;
146 obj->parent_object_guid = talloc(obj, struct GUID);
147 if (obj->parent_object_guid == NULL) {
148 return WERR_DS_DRA_INTERNAL_ERROR;
150 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
151 if (GUID_all_zero(obj->parent_object_guid)) {
152 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
153 ldb_dn_get_linearized(msg->dn)));
154 return WERR_DS_DRA_INTERNAL_ERROR;
157 obj->next_object = NULL;
159 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
160 if (!md_value) {
161 /* nothing to send */
162 return WERR_OK;
165 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
166 /* don't send uninstantiated objects */
167 return WERR_OK;
170 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
171 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
172 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
173 return WERR_DS_DRA_INTERNAL_ERROR;
176 if (md.version != 1) {
177 return WERR_DS_DRA_INTERNAL_ERROR;
180 rdn = ldb_dn_get_rdn_name(msg->dn);
181 if (rdn == NULL) {
182 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
183 return WERR_DS_DRA_INTERNAL_ERROR;
186 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
187 if (rdn_sa == NULL) {
188 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
189 rdn, ldb_dn_get_linearized(msg->dn)));
190 return WERR_DS_DRA_INTERNAL_ERROR;
193 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
194 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
196 obj->object.identifier = get_object_identifier(obj, msg);
197 if (obj->object.identifier == NULL) {
198 return WERR_NOMEM;
200 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
202 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
203 for (n=i=0; i<md.ctr.ctr1.count; i++) {
204 const struct dsdb_attribute *sa;
205 bool force_attribute = false;
207 /* if the attribute has not changed, and it is not the
208 instanceType then don't include it */
209 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
210 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
211 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
213 /* don't include the rDN */
214 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
216 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
217 if (!sa) {
218 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
219 (unsigned int)md.ctr.ctr1.array[i].attid,
220 ldb_dn_get_linearized(msg->dn)));
221 return WERR_DS_DRA_INTERNAL_ERROR;
224 if (sa->linkID) {
225 struct ldb_message_element *el;
226 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
227 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
228 /* don't send upgraded links inline */
229 continue;
233 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
234 !dsdb_attr_in_rodc_fas(sa)) {
235 force_attribute = true;
236 DEBUG(4,("Forcing attribute %s in %s\n",
237 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
240 /* filter by uptodateness_vector */
241 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
242 !force_attribute &&
243 udv_filter(uptodateness_vector,
244 &md.ctr.ctr1.array[i].originating_invocation_id,
245 md.ctr.ctr1.array[i].originating_usn)) {
246 continue;
249 /* filter by partial_attribute_set */
250 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
251 continue;
254 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
255 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
256 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
257 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
258 attids[n] = md.ctr.ctr1.array[i].attid;
259 n++;
262 /* ignore it if its an empty change. Note that renames always
263 * change the 'name' attribute, so they won't be ignored by
264 * this
266 * the force_object_return check is used to force an empty
267 * object return when we timeout in the getncchanges loop.
268 * This allows us to return an empty object, which keeps the
269 * client happy while preventing timeouts
271 if (n == 0 ||
272 (n == 1 &&
273 attids[0] == DRSUAPI_ATTID_instanceType &&
274 !force_object_return)) {
275 talloc_free(obj->meta_data_ctr);
276 obj->meta_data_ctr = NULL;
277 return WERR_OK;
280 obj->meta_data_ctr->count = n;
282 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
283 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
284 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
285 obj->object.attribute_ctr.num_attributes);
288 * Note that the meta_data array and the attributes array must
289 * be the same size and in the same order
291 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
292 struct ldb_message_element *el;
293 WERROR werr;
294 const struct dsdb_attribute *sa;
296 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
297 if (!sa) {
298 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
299 return WERR_DS_DRA_INTERNAL_ERROR;
302 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
303 if (el == NULL) {
304 /* this happens for attributes that have been removed */
305 DEBUG(5,("No element '%s' for attributeID %u in message\n",
306 sa->lDAPDisplayName, attids[i]));
307 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
308 obj->object.attribute_ctr.attributes[i].attid =
309 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
310 } else {
311 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
312 &obj->object.attribute_ctr.attributes[i]);
313 if (!W_ERROR_IS_OK(werr)) {
314 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
315 sa->lDAPDisplayName, win_errstr(werr)));
316 return werr;
318 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
319 * check if attribute is secret and send a null value
321 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
322 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
323 &obj->meta_data_ctr->meta_data[i]);
325 /* some attributes needs to be encrypted
326 before being sent */
327 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
328 &obj->object.attribute_ctr.attributes[i]);
329 if (!W_ERROR_IS_OK(werr)) {
330 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
331 sa->lDAPDisplayName, win_errstr(werr)));
332 return werr;
337 return WERR_OK;
341 add one linked attribute from an object to the list of linked
342 attributes in a getncchanges request
344 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
345 struct ldb_context *sam_ctx,
346 const struct dsdb_schema *schema,
347 const struct dsdb_attribute *sa,
348 struct ldb_message *msg,
349 struct dsdb_dn *dsdb_dn,
350 struct drsuapi_DsReplicaLinkedAttribute **la_list,
351 uint32_t *la_count)
353 struct drsuapi_DsReplicaLinkedAttribute *la;
354 bool active;
355 NTSTATUS status;
356 WERROR werr;
358 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
359 W_ERROR_HAVE_NO_MEMORY(*la_list);
361 la = &(*la_list)[*la_count];
363 la->identifier = get_object_identifier(*la_list, msg);
364 W_ERROR_HAVE_NO_MEMORY(la->identifier);
366 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
368 if (!active) {
369 /* We have to check that the inactive link still point to an existing object */
370 struct GUID guid;
371 struct ldb_dn *tdn;
372 int ret;
373 const char *v;
375 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "false");
376 if (strncasecmp(v, "true", 4) == 0) {
378 * Note: we skip the transmition of the deleted link even if the other part used to
379 * know about it because when we transmit the deletion of the object, the link will
380 * be deleted too due to deletion of object where link points and Windows do so.
382 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
383 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "true");
385 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
386 * if it join an existing domain with deleted objets, it firsts impose to have a
387 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
388 * either during initial replication or after the getNCChanges.
389 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
391 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
392 * If FL >=2K8R2 we are sure that this attribute will be here.
393 * For this kind of forest level we do not return the link if the object is recycled
394 * (isRecycled = true).
396 if (strncasecmp(v, "true", 4) == 0) {
397 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
398 ldb_dn_get_linearized(msg->dn)));
399 return WERR_OK;
401 } else {
402 return WERR_OK;
405 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
406 if (!NT_STATUS_IS_OK(status)) {
407 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
408 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
409 return ntstatus_to_werror(status);
411 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
412 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
413 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
414 GUID_string(mem_ctx, &guid)));
415 return WERR_OK;
416 } else if (ret != LDB_SUCCESS) {
417 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
418 GUID_string(mem_ctx, &guid),
419 ret));
420 return WERR_OK;
423 la->attid = sa->attributeID_id;
424 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
426 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
427 if (!NT_STATUS_IS_OK(status)) {
428 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
429 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
430 return ntstatus_to_werror(status);
432 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
433 if (!NT_STATUS_IS_OK(status)) {
434 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
435 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
436 return ntstatus_to_werror(status);
438 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
439 if (!NT_STATUS_IS_OK(status)) {
440 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
441 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
442 return ntstatus_to_werror(status);
444 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
445 if (!NT_STATUS_IS_OK(status)) {
446 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
447 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
448 return ntstatus_to_werror(status);
451 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
452 if (!NT_STATUS_IS_OK(status)) {
453 /* this is possible for upgraded links */
454 la->originating_add_time = la->meta_data.originating_change_time;
457 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
458 W_ERROR_NOT_OK_RETURN(werr);
460 (*la_count)++;
461 return WERR_OK;
466 add linked attributes from an object to the list of linked
467 attributes in a getncchanges request
469 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
470 TALLOC_CTX *mem_ctx,
471 struct ldb_dn *ncRoot_dn,
472 struct dsdb_schema *schema,
473 uint64_t highest_usn,
474 uint32_t replica_flags,
475 struct ldb_message *msg,
476 struct drsuapi_DsReplicaLinkedAttribute **la_list,
477 uint32_t *la_count,
478 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
480 unsigned int i;
481 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
482 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
484 for (i=0; i<msg->num_elements; i++) {
485 struct ldb_message_element *el = &msg->elements[i];
486 const struct dsdb_attribute *sa;
487 unsigned int j;
489 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
491 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
492 /* we only want forward links */
493 continue;
496 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
497 /* its an old style link, it will have been
498 * sent in the main replication data */
499 continue;
502 for (j=0; j<el->num_values; j++) {
503 struct dsdb_dn *dsdb_dn;
504 uint64_t local_usn;
505 NTSTATUS status;
506 WERROR werr;
508 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
509 if (dsdb_dn == NULL) {
510 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
511 el->name, ldb_dn_get_linearized(msg->dn)));
512 talloc_free(tmp_ctx);
513 return WERR_DS_DRA_INTERNAL_ERROR;
516 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
517 if (!NT_STATUS_IS_OK(status)) {
518 /* this can happen for attributes
519 given to us with old style meta
520 data */
521 continue;
524 if (local_usn > uSNChanged) {
525 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
526 el->name, ldb_dn_get_linearized(msg->dn)));
527 talloc_free(tmp_ctx);
528 return WERR_DS_DRA_INTERNAL_ERROR;
531 if (local_usn < highest_usn) {
532 continue;
535 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
536 dsdb_dn, la_list, la_count);
537 if (!W_ERROR_IS_OK(werr)) {
538 talloc_free(tmp_ctx);
539 return werr;
544 talloc_free(tmp_ctx);
545 return WERR_OK;
549 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
551 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
552 struct ldb_dn *ncRoot_dn,
553 struct drsuapi_DsReplicaCursor2CtrEx *udv)
555 int ret;
557 udv->version = 2;
558 udv->reserved1 = 0;
559 udv->reserved2 = 0;
561 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
562 if (ret != LDB_SUCCESS) {
563 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
564 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
565 return WERR_DS_DRA_INTERNAL_ERROR;
568 return WERR_OK;
572 /* comparison function for linked attributes - see CompareLinks() in
573 * MS-DRSR section 4.1.10.5.17 */
574 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
575 const struct drsuapi_DsReplicaLinkedAttribute *la2,
576 struct ldb_context *sam_ctx)
578 int c;
579 WERROR werr;
580 TALLOC_CTX *tmp_ctx;
581 const struct dsdb_schema *schema;
582 const struct dsdb_attribute *schema_attrib;
583 struct dsdb_dn *dn1, *dn2;
584 struct GUID guid1, guid2;
585 NTSTATUS status;
587 c = GUID_compare(&la1->identifier->guid,
588 &la2->identifier->guid);
589 if (c != 0) return c;
591 if (la1->attid != la2->attid) {
592 return la1->attid < la2->attid? -1:1;
595 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
596 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
597 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
600 /* we need to get the target GUIDs to compare */
601 tmp_ctx = talloc_new(sam_ctx);
603 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
604 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
606 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
607 if (!W_ERROR_IS_OK(werr)) {
608 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
609 talloc_free(tmp_ctx);
610 return 0;
613 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
614 if (!W_ERROR_IS_OK(werr)) {
615 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
616 talloc_free(tmp_ctx);
617 return 0;
620 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
621 if (!NT_STATUS_IS_OK(status)) {
622 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
623 talloc_free(tmp_ctx);
624 return 0;
626 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
627 if (!NT_STATUS_IS_OK(status)) {
628 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
629 talloc_free(tmp_ctx);
630 return 0;
633 talloc_free(tmp_ctx);
635 return GUID_compare(&guid1, &guid2);
638 struct drsuapi_changed_objects {
639 struct ldb_dn *dn;
640 struct GUID guid;
641 uint64_t usn;
645 sort the objects we send by tree order
647 static int site_res_cmp_parent_order(struct drsuapi_changed_objects *m1,
648 struct drsuapi_changed_objects *m2)
650 return ldb_dn_compare(m2->dn, m1->dn);
654 sort the objects we send first by uSNChanged
656 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects *m1,
657 struct drsuapi_changed_objects *m2)
659 unsigned usnchanged1, usnchanged2;
660 unsigned cn1, cn2;
662 cn1 = ldb_dn_get_comp_num(m1->dn);
663 cn2 = ldb_dn_get_comp_num(m2->dn);
664 if (cn1 != cn2) {
665 return cn1 > cn2 ? 1 : -1;
667 usnchanged1 = m1->usn;
668 usnchanged2 = m2->usn;
669 if (usnchanged1 == usnchanged2) {
670 return 0;
672 return usnchanged1 > usnchanged2 ? 1 : -1;
677 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
679 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
680 TALLOC_CTX *mem_ctx,
681 struct drsuapi_DsGetNCChangesRequest10 *req10,
682 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
684 struct ldb_dn *rid_manager_dn, *fsmo_role_dn, *req_dn;
685 int ret;
686 struct ldb_context *ldb = b_state->sam_ctx;
687 struct ldb_result *ext_res;
688 struct ldb_dn *base_dn;
689 struct dsdb_fsmo_extended_op *exop;
692 steps:
693 - verify that the DN being asked for is the RID Manager DN
694 - verify that we are the RID Manager
697 /* work out who is the RID Manager */
698 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
699 if (ret != LDB_SUCCESS) {
700 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
701 return WERR_DS_DRA_INTERNAL_ERROR;
704 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
705 if (!ldb_dn_validate(req_dn) ||
706 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
707 /* that isn't the RID Manager DN */
708 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
709 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
710 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
711 return WERR_OK;
714 /* find the DN of the RID Manager */
715 ret = samdb_reference_dn(ldb, mem_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
716 if (ret != LDB_SUCCESS) {
717 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s\n",
718 ldb_errstring(ldb)));
719 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
720 return WERR_DS_DRA_INTERNAL_ERROR;
723 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
724 /* we're not the RID Manager - go away */
725 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
726 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
727 return WERR_OK;
730 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
731 W_ERROR_HAVE_NO_MEMORY(exop);
733 exop->fsmo_info = req10->fsmo_info;
734 exop->destination_dsa_guid = req10->destination_dsa_guid;
736 ret = ldb_transaction_start(ldb);
737 if (ret != LDB_SUCCESS) {
738 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
739 ldb_errstring(ldb)));
740 return WERR_DS_DRA_INTERNAL_ERROR;
744 * FIXME (kim): this is a temp hack to return just few object,
745 * but not the whole domain NC.
746 * We should remove this hack and implement a 'scope'
747 * building function to return just the set of object
748 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
750 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
752 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
753 if (ret != LDB_SUCCESS) {
754 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
755 ldb_errstring(ldb)));
756 ldb_transaction_cancel(ldb);
757 return WERR_DS_DRA_INTERNAL_ERROR;
760 ret = ldb_transaction_commit(ldb);
761 if (ret != LDB_SUCCESS) {
762 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
763 ldb_errstring(ldb)));
764 return WERR_DS_DRA_INTERNAL_ERROR;
767 talloc_free(ext_res);
769 base_dn = ldb_get_default_basedn(ldb);
771 DEBUG(2,("Allocated RID pool for server %s\n",
772 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
774 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
776 return WERR_OK;
780 return an array of SIDs from a ldb_message given an attribute name
781 assumes the SIDs are in extended DN format
783 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
784 struct ldb_message *msg,
785 TALLOC_CTX *mem_ctx,
786 const char *attr,
787 const struct dom_sid ***sids)
789 struct ldb_message_element *el;
790 unsigned int i;
792 el = ldb_msg_find_element(msg, attr);
793 if (!el) {
794 *sids = NULL;
795 return WERR_OK;
798 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
799 W_ERROR_HAVE_NO_MEMORY(*sids);
801 for (i=0; i<el->num_values; i++) {
802 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
803 NTSTATUS status;
804 struct dom_sid *sid;
806 sid = talloc(*sids, struct dom_sid);
807 W_ERROR_HAVE_NO_MEMORY(sid);
808 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
809 if (!NT_STATUS_IS_OK(status)) {
810 return WERR_INTERNAL_DB_CORRUPTION;
812 (*sids)[i] = sid;
814 (*sids)[i] = NULL;
816 return WERR_OK;
821 return an array of SIDs from a ldb_message given an attribute name
822 assumes the SIDs are in NDR form
824 static WERROR samdb_result_sid_array_ndr(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 enum ndr_err_code ndr_err;
844 struct dom_sid *sid;
846 sid = talloc(*sids, struct dom_sid);
847 W_ERROR_HAVE_NO_MEMORY(sid);
849 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
850 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
851 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
852 return WERR_INTERNAL_DB_CORRUPTION;
854 (*sids)[i] = sid;
856 (*sids)[i] = NULL;
858 return WERR_OK;
862 see if any SIDs in list1 are in list2
864 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
866 unsigned int i, j;
867 /* do we ever have enough SIDs here to worry about O(n^2) ? */
868 for (i=0; list1[i]; i++) {
869 for (j=0; list2[j]; j++) {
870 if (dom_sid_equal(list1[i], list2[j])) {
871 return true;
875 return false;
879 handle a DRSUAPI_EXOP_REPL_SECRET call
881 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
882 TALLOC_CTX *mem_ctx,
883 struct drsuapi_DsGetNCChangesRequest10 *req10,
884 struct dom_sid *user_sid,
885 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
887 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
888 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
889 int ret;
890 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
891 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
892 struct ldb_result *rodc_res, *obj_res;
893 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
894 WERROR werr;
896 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
897 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
900 * we need to work out if we will allow this RODC to
901 * replicate the secrets for this object
903 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
904 * of this function
907 if (b_state->sam_ctx_system == NULL) {
908 /* this operation needs system level access */
909 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
910 return WERR_DS_DRA_SOURCE_DISABLED;
913 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
914 if (!ldb_dn_validate(obj_dn)) goto failed;
916 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
917 dom_sid_string(mem_ctx, user_sid));
918 if (!ldb_dn_validate(rodc_dn)) goto failed;
920 /* do the two searches we need */
921 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
922 DSDB_SEARCH_SHOW_EXTENDED_DN);
923 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
925 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
926 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
928 /* if the object SID is equal to the user_sid, allow */
929 if (dom_sid_equal(user_sid,
930 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
931 goto allowed;
934 /* an RODC is allowed to get its own krbtgt account secrets */
935 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
936 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
937 if (krbtgt_link_dn != NULL &&
938 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
939 goto allowed;
942 /* but it isn't allowed to get anyone elses krbtgt secrets */
943 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
944 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
945 goto denied;
948 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
949 "userAccountControl", 0) &
950 UF_INTERDOMAIN_TRUST_ACCOUNT) {
951 goto denied;
954 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
955 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
956 if (!W_ERROR_IS_OK(werr)) {
957 goto denied;
960 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
961 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
962 if (!W_ERROR_IS_OK(werr)) {
963 goto denied;
966 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
967 mem_ctx, "tokenGroups", &token_sids);
968 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
969 goto denied;
972 if (never_reveal_sids &&
973 sid_list_match(token_sids, never_reveal_sids)) {
974 goto denied;
977 if (reveal_sids &&
978 sid_list_match(token_sids, reveal_sids)) {
979 goto allowed;
982 /* default deny */
983 denied:
984 DEBUG(2,(__location__ ": Denied RODC secret replication for %s by RODC %s\n",
985 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
986 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
987 return WERR_DS_DRA_ACCESS_DENIED;
989 allowed:
990 DEBUG(2,(__location__ ": Allowed RODC secret replication for %s by RODC %s\n",
991 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
992 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
993 req10->highwatermark.highest_usn = 0;
994 return WERR_OK;
996 failed:
997 DEBUG(2,(__location__ ": Failed RODC secret replication for %s by RODC %s\n",
998 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
999 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1000 return WERR_DS_DRA_BAD_DN;
1005 handle a DRSUAPI_EXOP_REPL_OBJ call
1007 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1008 TALLOC_CTX *mem_ctx,
1009 struct drsuapi_DsGetNCChangesRequest10 *req10,
1010 struct dom_sid *user_sid,
1011 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1013 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1015 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1016 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1018 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1019 req10->highwatermark.highest_usn = 0;
1020 return WERR_OK;
1025 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1026 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1027 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1029 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1030 TALLOC_CTX *mem_ctx,
1031 struct drsuapi_DsGetNCChangesRequest10 *req10,
1032 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1034 struct ldb_dn *fsmo_role_dn, *req_dn, *ntds_dn;
1035 int ret;
1036 unsigned int i;
1037 struct ldb_context *ldb = b_state->sam_ctx;
1038 struct ldb_message *msg;
1041 steps:
1042 - verify that the client dn exists
1043 - verify that we are the current master
1046 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1047 if (!ldb_dn_validate(req_dn)) {
1048 /* that is not a valid dn */
1049 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1050 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1051 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1052 return WERR_OK;
1055 /* retrieve the current role owner */
1056 ret = samdb_reference_dn(ldb, mem_ctx, req_dn, "fSMORoleOwner", &fsmo_role_dn);
1057 if (ret != LDB_SUCCESS) {
1058 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in context - %s\n",
1059 ldb_errstring(ldb)));
1060 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1061 return WERR_DS_DRA_INTERNAL_ERROR;
1064 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
1065 /* we're not the current owner - go away */
1066 DEBUG(0,(__location__ ": FSMO transfer request when not owner\n"));
1067 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1068 return WERR_OK;
1071 /* change the current master */
1072 msg = ldb_msg_new(ldb);
1073 W_ERROR_HAVE_NO_MEMORY(msg);
1074 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1075 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1077 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1078 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1079 if (ret != LDB_SUCCESS) {
1080 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1081 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1082 talloc_free(msg);
1083 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1084 return WERR_OK;
1087 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1088 if (ret != 0) {
1089 talloc_free(msg);
1090 return WERR_DS_DRA_INTERNAL_ERROR;
1093 for (i=0;i<msg->num_elements;i++) {
1094 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1097 ret = ldb_transaction_start(ldb);
1098 if (ret != LDB_SUCCESS) {
1099 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1100 ldb_errstring(ldb)));
1101 return WERR_DS_DRA_INTERNAL_ERROR;
1104 ret = ldb_modify(ldb, msg);
1105 if (ret != LDB_SUCCESS) {
1106 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1107 ldb_errstring(ldb)));
1108 ldb_transaction_cancel(ldb);
1109 return WERR_DS_DRA_INTERNAL_ERROR;
1112 ret = ldb_transaction_commit(ldb);
1113 if (ret != LDB_SUCCESS) {
1114 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1115 ldb_errstring(ldb)));
1116 return WERR_DS_DRA_INTERNAL_ERROR;
1119 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1121 return WERR_OK;
1124 /* state of a partially completed getncchanges call */
1125 struct drsuapi_getncchanges_state {
1126 struct GUID *guids;
1127 uint32_t num_records;
1128 uint32_t num_processed;
1129 struct ldb_dn *ncRoot_dn;
1130 bool is_schema_nc;
1131 uint64_t min_usn;
1132 uint64_t highest_usn;
1133 struct ldb_dn *last_dn;
1134 struct drsuapi_DsReplicaLinkedAttribute *la_list;
1135 uint32_t la_count;
1136 bool la_sorted;
1137 uint32_t la_idx;
1138 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
1142 see if this getncchanges request includes a request to reveal secret information
1144 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1145 struct drsuapi_DsGetNCChangesRequest10 *req10,
1146 bool *is_secret_request)
1148 enum drsuapi_DsExtendedOperation exop;
1149 uint32_t i;
1150 struct dsdb_schema *schema;
1152 *is_secret_request = true;
1154 exop = req10->extended_op;
1156 switch (exop) {
1157 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1158 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1159 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1160 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1161 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1162 /* FSMO exops can reveal secrets */
1163 *is_secret_request = true;
1164 return WERR_OK;
1165 case DRSUAPI_EXOP_REPL_SECRET:
1166 case DRSUAPI_EXOP_REPL_OBJ:
1167 case DRSUAPI_EXOP_NONE:
1168 break;
1171 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1172 *is_secret_request = false;
1173 return WERR_OK;
1176 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1177 req10->partial_attribute_set == NULL) {
1178 /* they want secrets */
1179 *is_secret_request = true;
1180 return WERR_OK;
1183 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1185 /* check the attributes they asked for */
1186 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1187 const struct dsdb_attribute *sa;
1188 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1189 if (sa == NULL) {
1190 return WERR_DS_DRA_SCHEMA_MISMATCH;
1192 if (!dsdb_attr_in_rodc_fas(sa)) {
1193 *is_secret_request = true;
1194 return WERR_OK;
1198 if (req10->partial_attribute_set_ex) {
1199 /* check the extended attributes they asked for */
1200 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1201 const struct dsdb_attribute *sa;
1202 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1203 if (sa == NULL) {
1204 return WERR_DS_DRA_SCHEMA_MISMATCH;
1206 if (!dsdb_attr_in_rodc_fas(sa)) {
1207 *is_secret_request = true;
1208 return WERR_OK;
1213 *is_secret_request = false;
1214 return WERR_OK;
1218 see if this getncchanges request is only for attributes in the GC
1219 partial attribute set
1221 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1222 struct drsuapi_DsGetNCChangesRequest10 *req10,
1223 bool *is_gc_pas_request)
1225 enum drsuapi_DsExtendedOperation exop;
1226 uint32_t i;
1227 struct dsdb_schema *schema;
1229 exop = req10->extended_op;
1231 switch (exop) {
1232 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1233 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1234 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1235 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1236 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1237 case DRSUAPI_EXOP_REPL_SECRET:
1238 *is_gc_pas_request = false;
1239 return WERR_OK;
1240 case DRSUAPI_EXOP_REPL_OBJ:
1241 case DRSUAPI_EXOP_NONE:
1242 break;
1245 if (req10->partial_attribute_set == NULL) {
1246 /* they want it all */
1247 *is_gc_pas_request = false;
1248 return WERR_OK;
1251 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1253 /* check the attributes they asked for */
1254 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1255 const struct dsdb_attribute *sa;
1256 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1257 if (sa == NULL) {
1258 return WERR_DS_DRA_SCHEMA_MISMATCH;
1260 if (!sa->isMemberOfPartialAttributeSet) {
1261 *is_gc_pas_request = false;
1262 return WERR_OK;
1266 if (req10->partial_attribute_set_ex) {
1267 /* check the extended attributes they asked for */
1268 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1269 const struct dsdb_attribute *sa;
1270 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1271 if (sa == NULL) {
1272 return WERR_DS_DRA_SCHEMA_MISMATCH;
1274 if (!sa->isMemberOfPartialAttributeSet) {
1275 *is_gc_pas_request = false;
1276 return WERR_OK;
1281 *is_gc_pas_request = true;
1282 return WERR_OK;
1287 map from req8 to req10
1289 static struct drsuapi_DsGetNCChangesRequest10 *
1290 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1291 struct drsuapi_DsGetNCChangesRequest8 *req8)
1293 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1294 struct drsuapi_DsGetNCChangesRequest10);
1295 if (req10 == NULL) {
1296 return NULL;
1299 req10->destination_dsa_guid = req8->destination_dsa_guid;
1300 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1301 req10->naming_context = req8->naming_context;
1302 req10->highwatermark = req8->highwatermark;
1303 req10->uptodateness_vector = req8->uptodateness_vector;
1304 req10->replica_flags = req8->replica_flags;
1305 req10->max_object_count = req8->max_object_count;
1306 req10->max_ndr_size = req8->max_ndr_size;
1307 req10->extended_op = req8->extended_op;
1308 req10->fsmo_info = req8->fsmo_info;
1309 req10->partial_attribute_set = req8->partial_attribute_set;
1310 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1311 req10->mapping_ctr = req8->mapping_ctr;
1313 return req10;
1318 * Collects object for normal replication cycle.
1320 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1321 TALLOC_CTX *mem_ctx,
1322 struct drsuapi_DsGetNCChangesRequest10 *req10,
1323 struct ldb_dn *search_dn,
1324 const char *extra_filter,
1325 struct ldb_result **search_res)
1327 int ret;
1328 char* search_filter;
1329 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1330 //const char *extra_filter;
1331 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1332 const char *attrs[] = { "uSNChanged",
1333 "objectGUID" ,
1334 NULL };
1336 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1337 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1338 scope = LDB_SCOPE_BASE;
1341 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1343 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1345 /* Construct response. */
1346 search_filter = talloc_asprintf(mem_ctx,
1347 "(uSNChanged>=%llu)",
1348 (unsigned long long)(getnc_state->min_usn+1));
1350 if (extra_filter) {
1351 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1354 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1355 search_filter = talloc_asprintf(mem_ctx,
1356 "(&%s(isCriticalSystemObject=TRUE))",
1357 search_filter);
1360 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1361 scope = LDB_SCOPE_BASE;
1364 if (!search_dn) {
1365 search_dn = getnc_state->ncRoot_dn;
1368 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1369 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1370 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1371 search_dn, scope, attrs,
1372 search_filter);
1373 if (ret != LDB_SUCCESS) {
1374 return WERR_DS_DRA_INTERNAL_ERROR;
1377 return WERR_OK;
1381 * Collects object for normal replication cycle.
1383 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1384 TALLOC_CTX *mem_ctx,
1385 struct drsuapi_DsGetNCChangesRequest10 *req10,
1386 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1387 struct ldb_dn *search_dn,
1388 const char *extra_filter,
1389 struct ldb_result **search_res)
1391 /* we have nothing to do in case of ex-op failure */
1392 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1393 return WERR_OK;
1396 /* TODO: implement extended op specific collection
1397 * of objects. Right now we just normal procedure
1398 * for collecting objects */
1399 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1403 drsuapi_DsGetNCChanges
1405 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1407 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1408 struct drsuapi_DsGetNCChanges *r)
1410 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1411 int ret;
1412 uint32_t i;
1413 struct dsdb_schema *schema;
1414 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1415 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1416 NTSTATUS status;
1417 DATA_BLOB session_key;
1418 WERROR werr;
1419 struct dcesrv_handle *h;
1420 struct drsuapi_bind_state *b_state;
1421 struct drsuapi_getncchanges_state *getnc_state;
1422 struct drsuapi_DsGetNCChangesRequest10 *req10;
1423 uint32_t options;
1424 uint32_t max_objects;
1425 uint32_t max_links;
1426 uint32_t link_count = 0;
1427 uint32_t link_total = 0;
1428 uint32_t link_given = 0;
1429 struct ldb_dn *search_dn = NULL;
1430 bool am_rodc, null_scope=false;
1431 enum security_user_level security_level;
1432 struct ldb_context *sam_ctx;
1433 struct dom_sid *user_sid;
1434 bool is_secret_request;
1435 bool is_gc_pas_request;
1436 struct drsuapi_changed_objects *changes;
1437 time_t max_wait;
1438 time_t start = time(NULL);
1439 bool max_wait_reached = false;
1441 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1442 b_state = h->data;
1444 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1446 *r->out.level_out = 6;
1447 /* TODO: linked attributes*/
1448 r->out.ctr->ctr6.linked_attributes_count = 0;
1449 r->out.ctr->ctr6.linked_attributes = NULL;
1451 r->out.ctr->ctr6.object_count = 0;
1452 r->out.ctr->ctr6.nc_object_count = 0;
1453 r->out.ctr->ctr6.more_data = false;
1454 r->out.ctr->ctr6.uptodateness_vector = NULL;
1456 /* a RODC doesn't allow for any replication */
1457 ret = samdb_rodc(sam_ctx, &am_rodc);
1458 if (ret == LDB_SUCCESS && am_rodc) {
1459 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1460 return WERR_DS_DRA_SOURCE_DISABLED;
1463 /* Check request revision.
1465 switch (r->in.level) {
1466 case 8:
1467 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1468 if (req10 == NULL) {
1469 return WERR_NOMEM;
1471 break;
1472 case 10:
1473 req10 = &r->in.req->req10;
1474 break;
1475 default:
1476 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1477 r->in.level));
1478 return WERR_REVISION_MISMATCH;
1482 /* Perform access checks. */
1483 /* TODO: we need to support a sync on a specific non-root
1484 * DN. We'll need to find the real partition root here */
1485 ncRoot = req10->naming_context;
1486 if (ncRoot == NULL) {
1487 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1488 return WERR_DS_DRA_INVALID_PARAMETER;
1491 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1492 return WERR_DS_DRA_INTERNAL_ERROR;
1495 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1496 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1497 return WERR_DS_DRA_SOURCE_DISABLED;
1500 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1502 /* all clients must have GUID_DRS_GET_CHANGES */
1503 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1504 mem_ctx,
1505 dce_call->conn->auth_state.session_info->security_token,
1506 req10->naming_context,
1507 GUID_DRS_GET_CHANGES);
1508 if (!W_ERROR_IS_OK(werr)) {
1509 return werr;
1512 /* allowed if the GC PAS and client has
1513 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1514 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1515 if (!W_ERROR_IS_OK(werr)) {
1516 return werr;
1518 if (is_gc_pas_request) {
1519 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1520 mem_ctx,
1521 dce_call->conn->auth_state.session_info->security_token,
1522 req10->naming_context,
1523 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1524 if (W_ERROR_IS_OK(werr)) {
1525 goto allowed;
1529 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1530 if (!W_ERROR_IS_OK(werr)) {
1531 return werr;
1533 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1534 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1535 mem_ctx,
1536 dce_call->conn->auth_state.session_info->security_token,
1537 req10->naming_context,
1538 GUID_DRS_GET_ALL_CHANGES);
1539 if (!W_ERROR_IS_OK(werr)) {
1540 return werr;
1544 allowed:
1545 /* for non-administrator replications, check that they have
1546 given the correct source_dsa_invocation_id */
1547 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1548 samdb_domain_sid(sam_ctx));
1549 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1550 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1551 /* we rely on this flag being unset for RODC requests */
1552 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1556 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1557 /* Ignore the _in_ uptpdateness vector*/
1558 req10->uptodateness_vector = NULL;
1561 getnc_state = b_state->getncchanges_state;
1563 /* see if a previous replication has been abandoned */
1564 if (getnc_state) {
1565 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1566 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1567 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1568 ldb_dn_get_linearized(new_dn),
1569 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1570 ldb_dn_get_linearized(getnc_state->last_dn)));
1571 talloc_free(getnc_state);
1572 getnc_state = NULL;
1576 if (getnc_state == NULL) {
1577 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1578 if (getnc_state == NULL) {
1579 return WERR_NOMEM;
1581 b_state->getncchanges_state = getnc_state;
1582 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1584 /* find out if we are to replicate Schema NC */
1585 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1586 ldb_get_schema_basedn(b_state->sam_ctx));
1587 getnc_state->is_schema_nc = (0 == ret);
1589 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1590 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1594 * This is the first replication cycle and it is
1595 * a good place to handle extended operations
1597 * FIXME: we don't fully support extended operations yet
1599 switch (req10->extended_op) {
1600 case DRSUAPI_EXOP_NONE:
1601 break;
1602 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1603 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1604 W_ERROR_NOT_OK_RETURN(werr);
1605 search_dn = ldb_get_default_basedn(sam_ctx);
1606 break;
1607 case DRSUAPI_EXOP_REPL_SECRET:
1608 werr = getncchanges_repl_secret(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1609 r->out.result = werr;
1610 W_ERROR_NOT_OK_RETURN(werr);
1611 break;
1612 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1613 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1614 W_ERROR_NOT_OK_RETURN(werr);
1615 break;
1616 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1617 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1618 W_ERROR_NOT_OK_RETURN(werr);
1619 break;
1620 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1621 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1622 W_ERROR_NOT_OK_RETURN(werr);
1623 break;
1624 case DRSUAPI_EXOP_REPL_OBJ:
1625 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1626 r->out.result = werr;
1627 W_ERROR_NOT_OK_RETURN(werr);
1628 break;
1630 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1632 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1633 (unsigned)req10->extended_op));
1634 return WERR_DS_DRA_NOT_SUPPORTED;
1638 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1639 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1640 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1641 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1642 return WERR_DS_DRA_INVALID_PARAMETER;
1645 /* we need the session key for encrypting password attributes */
1646 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1647 if (!NT_STATUS_IS_OK(status)) {
1648 DEBUG(0,(__location__ ": Failed to get session key\n"));
1649 return WERR_DS_DRA_INTERNAL_ERROR;
1653 TODO: MS-DRSR section 4.1.10.1.1
1654 Work out if this is the start of a new cycle */
1656 if (getnc_state->guids == NULL) {
1657 const char *extra_filter;
1658 struct ldb_result *search_res = NULL;
1660 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1662 getnc_state->min_usn = req10->highwatermark.highest_usn;
1664 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1665 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1666 search_dn, extra_filter,
1667 &search_res);
1668 } else {
1669 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1670 &r->out.ctr->ctr6,
1671 search_dn, extra_filter,
1672 &search_res);
1674 W_ERROR_NOT_OK_RETURN(werr);
1676 /* extract out the GUIDs list */
1677 getnc_state->num_records = search_res ? search_res->count : 0;
1678 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1679 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1681 changes = talloc_array(getnc_state,
1682 struct drsuapi_changed_objects,
1683 getnc_state->num_records);
1684 W_ERROR_HAVE_NO_MEMORY(changes);
1686 for (i=0; i<getnc_state->num_records; i++) {
1687 changes[i].dn = search_res->msgs[i]->dn;
1688 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1689 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1692 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1693 TYPESAFE_QSORT(changes,
1694 getnc_state->num_records,
1695 site_res_cmp_parent_order);
1696 } else {
1697 TYPESAFE_QSORT(changes,
1698 getnc_state->num_records,
1699 site_res_cmp_dn_usn_order);
1702 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req10->uptodateness_vector);
1703 if (getnc_state->uptodateness_vector) {
1704 /* make sure its sorted */
1705 TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1706 getnc_state->uptodateness_vector->count,
1707 drsuapi_DsReplicaCursor_compare);
1710 for (i=0; i < getnc_state->num_records; i++) {
1711 getnc_state->guids[i] = changes[i].guid;
1712 if (GUID_all_zero(&getnc_state->guids[i])) {
1713 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1714 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1715 return WERR_DS_DRA_INTERNAL_ERROR;
1719 talloc_free(search_res);
1720 talloc_free(changes);
1723 /* Prefix mapping */
1724 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1725 if (!schema) {
1726 DEBUG(0,("No schema in sam_ctx\n"));
1727 return WERR_DS_DRA_INTERNAL_ERROR;
1730 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1731 *r->out.ctr->ctr6.naming_context = *ncRoot;
1733 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1734 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1735 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1736 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1737 return WERR_DS_DRA_INTERNAL_ERROR;
1740 /* find the SID if there is one */
1741 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1743 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1744 r->out.ctr->ctr6.mapping_ctr = *ctr;
1746 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1747 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1749 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1750 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1752 r->out.ctr->ctr6.first_object = NULL;
1753 currentObject = &r->out.ctr->ctr6.first_object;
1755 /* use this to force single objects at a time, which is useful
1756 * for working out what object is giving problems
1758 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1759 if (req10->max_object_count < max_objects) {
1760 max_objects = req10->max_object_count;
1763 * TODO: work out how the maximum should be calculated
1765 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1768 * Maximum time that we can spend in a getncchanges
1769 * in order to avoid timeout of the other part.
1770 * 10 seconds by default.
1772 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1773 for (i=getnc_state->num_processed;
1774 i<getnc_state->num_records &&
1775 !null_scope &&
1776 (r->out.ctr->ctr6.object_count < max_objects)
1777 && !max_wait_reached;
1778 i++) {
1779 int uSN;
1780 struct drsuapi_DsReplicaObjectListItemEx *obj;
1781 struct ldb_message *msg;
1782 static const char * const msg_attrs[] = {
1783 "*",
1784 "nTSecurityDescriptor",
1785 "parentGUID",
1786 "replPropertyMetaData",
1787 DSDB_SECRET_ATTRIBUTES,
1788 NULL };
1789 struct ldb_result *msg_res;
1790 struct ldb_dn *msg_dn;
1792 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1793 W_ERROR_HAVE_NO_MEMORY(obj);
1795 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1796 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1799 /* by re-searching here we avoid having a lot of full
1800 * records in memory between calls to getncchanges
1802 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1803 msg_dn,
1804 LDB_SCOPE_BASE, msg_attrs, NULL);
1805 if (ret != LDB_SUCCESS) {
1806 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1807 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1808 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1810 talloc_free(obj);
1811 continue;
1814 msg = msg_res->msgs[0];
1816 max_wait_reached = (time(NULL) - start > max_wait);
1818 werr = get_nc_changes_build_object(obj, msg,
1819 sam_ctx, getnc_state->ncRoot_dn,
1820 getnc_state->is_schema_nc,
1821 schema, &session_key, getnc_state->min_usn,
1822 req10->replica_flags,
1823 req10->partial_attribute_set,
1824 getnc_state->uptodateness_vector,
1825 req10->extended_op,
1826 max_wait_reached);
1827 if (!W_ERROR_IS_OK(werr)) {
1828 return werr;
1831 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1832 getnc_state->ncRoot_dn,
1833 schema, getnc_state->min_usn,
1834 req10->replica_flags,
1835 msg,
1836 &getnc_state->la_list,
1837 &getnc_state->la_count,
1838 getnc_state->uptodateness_vector);
1839 if (!W_ERROR_IS_OK(werr)) {
1840 return werr;
1843 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1844 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1845 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1847 if (uSN > getnc_state->highest_usn) {
1848 getnc_state->highest_usn = uSN;
1851 if (obj->meta_data_ctr == NULL) {
1852 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1853 ldb_dn_get_linearized(msg->dn)));
1854 /* no attributes to send */
1855 talloc_free(obj);
1856 continue;
1859 r->out.ctr->ctr6.object_count++;
1861 *currentObject = obj;
1862 currentObject = &obj->next_object;
1864 talloc_free(getnc_state->last_dn);
1865 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1867 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1869 talloc_free(msg_res);
1870 talloc_free(msg_dn);
1873 getnc_state->num_processed = i;
1875 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1877 /* the client can us to call UpdateRefs on its behalf to
1878 re-establish monitoring of the NC */
1879 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1880 !GUID_all_zero(&req10->destination_dsa_guid)) {
1881 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1882 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1883 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1884 ureq.naming_context = ncRoot;
1885 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
1886 &req10->destination_dsa_guid);
1887 if (!ureq.dest_dsa_dns_name) {
1888 return WERR_NOMEM;
1890 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1891 ureq.options = DRSUAPI_DRS_ADD_REF |
1892 DRSUAPI_DRS_ASYNC_OP |
1893 DRSUAPI_DRS_GETCHG_CHECK;
1895 /* we also need to pass through the
1896 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1897 to send notifies using the GC SPN */
1898 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1900 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1901 if (!W_ERROR_IS_OK(werr)) {
1902 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1903 win_errstr(werr)));
1908 * TODO:
1909 * This is just a guess, how to calculate the
1910 * number of linked attributes to send, we need to
1911 * find out how to do this right.
1913 if (r->out.ctr->ctr6.object_count >= max_links) {
1914 max_links = 0;
1915 } else {
1916 max_links -= r->out.ctr->ctr6.object_count;
1919 link_total = getnc_state->la_count;
1921 if (i < getnc_state->num_records) {
1922 r->out.ctr->ctr6.more_data = true;
1923 } else {
1924 /* sort the whole array the first time */
1925 if (!getnc_state->la_sorted) {
1926 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1927 sam_ctx, linked_attribute_compare);
1928 getnc_state->la_sorted = true;
1931 link_count = getnc_state->la_count - getnc_state->la_idx;
1932 link_count = MIN(max_links, link_count);
1934 r->out.ctr->ctr6.linked_attributes_count = link_count;
1935 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1937 getnc_state->la_idx += link_count;
1938 link_given = getnc_state->la_idx;
1940 if (getnc_state->la_idx < getnc_state->la_count) {
1941 r->out.ctr->ctr6.more_data = true;
1945 if (!r->out.ctr->ctr6.more_data) {
1946 talloc_steal(mem_ctx, getnc_state->la_list);
1948 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1949 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1951 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1952 r->out.ctr->ctr6.uptodateness_vector);
1953 if (!W_ERROR_IS_OK(werr)) {
1954 return werr;
1957 talloc_free(getnc_state);
1958 b_state->getncchanges_state = NULL;
1961 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1962 r->out.ctr->ctr6.uptodateness_vector = NULL;
1963 r->out.ctr->ctr6.nc_object_count = 0;
1964 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1967 DEBUG(r->out.ctr->ctr6.more_data?4:2,
1968 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1969 (unsigned long long)(req10->highwatermark.highest_usn+1),
1970 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
1971 r->out.ctr->ctr6.object_count,
1972 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
1973 r->out.ctr->ctr6.linked_attributes_count,
1974 link_given, link_total,
1975 dom_sid_string(mem_ctx, user_sid)));
1977 #if 0
1978 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
1979 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
1981 #endif
1983 return WERR_OK;