s4:drsuapi: remove unused 'highest_usn' from drsuapi_getncchanges_state
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / getncchanges.c
blobb7b488c2e3bcb37d959b9f99a938253e21e73e19
1 /*
2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "libcli/security/security.h"
31 #include "libcli/security/session.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33 #include "rpc_server/dcerpc_server_proto.h"
34 #include "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
40 /* state of a partially completed getncchanges call */
41 struct drsuapi_getncchanges_state {
42 struct GUID *guids;
43 uint32_t num_records;
44 uint32_t num_processed;
45 struct ldb_dn *ncRoot_dn;
46 bool is_schema_nc;
47 uint64_t min_usn;
48 struct ldb_dn *last_dn;
49 struct drsuapi_DsReplicaLinkedAttribute *la_list;
50 uint32_t la_count;
51 bool la_sorted;
52 uint32_t la_idx;
53 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
57 build a DsReplicaObjectIdentifier from a ldb msg
59 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
60 struct ldb_message *msg)
62 struct drsuapi_DsReplicaObjectIdentifier *identifier;
63 struct dom_sid *sid;
65 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
66 if (identifier == NULL) {
67 return NULL;
70 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
71 identifier->guid = samdb_result_guid(msg, "objectGUID");
73 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
74 if (sid) {
75 identifier->sid = *sid;
76 } else {
77 ZERO_STRUCT(identifier->sid);
79 return identifier;
82 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
84 return GUID_compare(guid1, &guid2);
88 see if we can filter an attribute using the uptodateness_vector
90 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
91 const struct GUID *originating_invocation_id,
92 uint64_t originating_usn)
94 const struct drsuapi_DsReplicaCursor *c;
95 if (udv == NULL) return false;
96 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
97 originating_invocation_id, udv_compare, c);
98 if (c && originating_usn <= c->highest_usn) {
99 return true;
101 return false;
105 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
107 if (a1 == a2) return 0;
108 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
112 check if an attribute is in a partial_attribute_set
114 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
115 struct drsuapi_DsPartialAttributeSet *pas)
117 enum drsuapi_DsAttributeId *result;
118 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
119 attid_cmp, result);
120 return result != NULL;
125 drsuapi_DsGetNCChanges for one object
127 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
128 struct ldb_message *msg,
129 struct ldb_context *sam_ctx,
130 struct ldb_dn *ncRoot_dn,
131 bool is_schema_nc,
132 struct dsdb_schema *schema,
133 DATA_BLOB *session_key,
134 uint64_t highest_usn,
135 uint32_t replica_flags,
136 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
137 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
138 enum drsuapi_DsExtendedOperation extended_op,
139 bool force_object_return)
141 const struct ldb_val *md_value;
142 uint32_t i, n;
143 struct replPropertyMetaDataBlob md;
144 uint32_t rid = 0;
145 enum ndr_err_code ndr_err;
146 uint32_t *attids;
147 const char *rdn;
148 const struct dsdb_attribute *rdn_sa;
149 unsigned int instanceType;
150 struct dsdb_syntax_ctx syntax_ctx;
152 /* make dsdb sytanx context for conversions */
153 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
154 syntax_ctx.is_schema_nc = is_schema_nc;
156 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
157 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
158 obj->is_nc_prefix = true;
159 obj->parent_object_guid = NULL;
160 } else {
161 obj->is_nc_prefix = false;
162 obj->parent_object_guid = talloc(obj, struct GUID);
163 if (obj->parent_object_guid == NULL) {
164 return WERR_DS_DRA_INTERNAL_ERROR;
166 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
167 if (GUID_all_zero(obj->parent_object_guid)) {
168 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
169 ldb_dn_get_linearized(msg->dn)));
170 return WERR_DS_DRA_INTERNAL_ERROR;
173 obj->next_object = NULL;
175 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
176 if (!md_value) {
177 /* nothing to send */
178 return WERR_OK;
181 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
182 /* don't send uninstantiated objects */
183 return WERR_OK;
186 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
187 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
188 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
189 return WERR_DS_DRA_INTERNAL_ERROR;
192 if (md.version != 1) {
193 return WERR_DS_DRA_INTERNAL_ERROR;
196 rdn = ldb_dn_get_rdn_name(msg->dn);
197 if (rdn == NULL) {
198 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
199 return WERR_DS_DRA_INTERNAL_ERROR;
202 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
203 if (rdn_sa == NULL) {
204 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
205 rdn, ldb_dn_get_linearized(msg->dn)));
206 return WERR_DS_DRA_INTERNAL_ERROR;
209 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
210 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
212 obj->object.identifier = get_object_identifier(obj, msg);
213 if (obj->object.identifier == NULL) {
214 return WERR_NOMEM;
216 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
218 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
219 for (n=i=0; i<md.ctr.ctr1.count; i++) {
220 const struct dsdb_attribute *sa;
221 bool force_attribute = false;
223 /* if the attribute has not changed, and it is not the
224 instanceType then don't include it */
225 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
226 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
227 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
229 /* don't include the rDN */
230 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
232 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
233 if (!sa) {
234 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
235 (unsigned int)md.ctr.ctr1.array[i].attid,
236 ldb_dn_get_linearized(msg->dn)));
237 return WERR_DS_DRA_INTERNAL_ERROR;
240 if (sa->linkID) {
241 struct ldb_message_element *el;
242 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
243 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
244 /* don't send upgraded links inline */
245 continue;
249 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
250 !dsdb_attr_in_rodc_fas(sa)) {
251 force_attribute = true;
252 DEBUG(4,("Forcing attribute %s in %s\n",
253 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
256 /* filter by uptodateness_vector */
257 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
258 !force_attribute &&
259 udv_filter(uptodateness_vector,
260 &md.ctr.ctr1.array[i].originating_invocation_id,
261 md.ctr.ctr1.array[i].originating_usn)) {
262 continue;
265 /* filter by partial_attribute_set */
266 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
267 continue;
270 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
271 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
272 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
273 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
274 attids[n] = md.ctr.ctr1.array[i].attid;
275 n++;
278 /* ignore it if its an empty change. Note that renames always
279 * change the 'name' attribute, so they won't be ignored by
280 * this
282 * the force_object_return check is used to force an empty
283 * object return when we timeout in the getncchanges loop.
284 * This allows us to return an empty object, which keeps the
285 * client happy while preventing timeouts
287 if (n == 0 ||
288 (n == 1 &&
289 attids[0] == DRSUAPI_ATTID_instanceType &&
290 !force_object_return)) {
291 talloc_free(obj->meta_data_ctr);
292 obj->meta_data_ctr = NULL;
293 return WERR_OK;
296 obj->meta_data_ctr->count = n;
298 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
299 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
300 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
301 obj->object.attribute_ctr.num_attributes);
302 if (obj->object.attribute_ctr.attributes == NULL) {
303 return WERR_NOMEM;
307 * Note that the meta_data array and the attributes array must
308 * be the same size and in the same order
310 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
311 struct ldb_message_element *el;
312 WERROR werr;
313 const struct dsdb_attribute *sa;
315 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
316 if (!sa) {
317 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
318 return WERR_DS_DRA_INTERNAL_ERROR;
321 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
322 if (el == NULL) {
323 /* this happens for attributes that have been removed */
324 DEBUG(5,("No element '%s' for attributeID %u in message\n",
325 sa->lDAPDisplayName, attids[i]));
326 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
327 obj->object.attribute_ctr.attributes[i].attid =
328 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
329 } else {
330 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
331 &obj->object.attribute_ctr.attributes[i]);
332 if (!W_ERROR_IS_OK(werr)) {
333 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
334 sa->lDAPDisplayName, win_errstr(werr)));
335 return werr;
337 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
338 * check if attribute is secret and send a null value
340 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
341 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
342 &obj->meta_data_ctr->meta_data[i]);
344 /* some attributes needs to be encrypted
345 before being sent */
346 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
347 &obj->object.attribute_ctr.attributes[i]);
348 if (!W_ERROR_IS_OK(werr)) {
349 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
350 sa->lDAPDisplayName, win_errstr(werr)));
351 return werr;
356 return WERR_OK;
360 add one linked attribute from an object to the list of linked
361 attributes in a getncchanges request
363 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
364 struct ldb_context *sam_ctx,
365 const struct dsdb_schema *schema,
366 const struct dsdb_attribute *sa,
367 struct ldb_message *msg,
368 struct dsdb_dn *dsdb_dn,
369 struct drsuapi_DsReplicaLinkedAttribute **la_list,
370 uint32_t *la_count)
372 struct drsuapi_DsReplicaLinkedAttribute *la;
373 bool active;
374 NTSTATUS status;
375 WERROR werr;
377 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
378 W_ERROR_HAVE_NO_MEMORY(*la_list);
380 la = &(*la_list)[*la_count];
382 la->identifier = get_object_identifier(*la_list, msg);
383 W_ERROR_HAVE_NO_MEMORY(la->identifier);
385 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
387 if (!active) {
388 /* We have to check that the inactive link still point to an existing object */
389 struct GUID guid;
390 struct ldb_dn *tdn;
391 int ret;
392 const char *v;
394 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
395 if (strncmp(v, "TRUE", 4) == 0) {
397 * Note: we skip the transmition of the deleted link even if the other part used to
398 * know about it because when we transmit the deletion of the object, the link will
399 * be deleted too due to deletion of object where link points and Windows do so.
401 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
402 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
404 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
405 * if it join an existing domain with deleted objets, it firsts impose to have a
406 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
407 * either during initial replication or after the getNCChanges.
408 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
410 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
411 * If FL >=2K8R2 we are sure that this attribute will be here.
412 * For this kind of forest level we do not return the link if the object is recycled
413 * (isRecycled = true).
415 if (strncmp(v, "TRUE", 4) == 0) {
416 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
417 ldb_dn_get_linearized(msg->dn)));
418 return WERR_OK;
420 } else {
421 return WERR_OK;
424 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
425 if (!NT_STATUS_IS_OK(status)) {
426 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
427 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
428 return ntstatus_to_werror(status);
430 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
431 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
432 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
433 GUID_string(mem_ctx, &guid)));
434 return WERR_OK;
435 } else if (ret != LDB_SUCCESS) {
436 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
437 GUID_string(mem_ctx, &guid),
438 ret));
439 return WERR_OK;
442 la->attid = sa->attributeID_id;
443 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
445 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
446 if (!NT_STATUS_IS_OK(status)) {
447 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
448 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
449 return ntstatus_to_werror(status);
451 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
452 if (!NT_STATUS_IS_OK(status)) {
453 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
454 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
455 return ntstatus_to_werror(status);
457 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
458 if (!NT_STATUS_IS_OK(status)) {
459 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
460 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
461 return ntstatus_to_werror(status);
463 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
464 if (!NT_STATUS_IS_OK(status)) {
465 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
466 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
467 return ntstatus_to_werror(status);
470 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
471 if (!NT_STATUS_IS_OK(status)) {
472 /* this is possible for upgraded links */
473 la->originating_add_time = la->meta_data.originating_change_time;
476 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
477 W_ERROR_NOT_OK_RETURN(werr);
479 (*la_count)++;
480 return WERR_OK;
485 add linked attributes from an object to the list of linked
486 attributes in a getncchanges request
488 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
489 TALLOC_CTX *mem_ctx,
490 struct ldb_dn *ncRoot_dn,
491 struct dsdb_schema *schema,
492 uint64_t highest_usn,
493 uint32_t replica_flags,
494 struct ldb_message *msg,
495 struct drsuapi_DsReplicaLinkedAttribute **la_list,
496 uint32_t *la_count,
497 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
499 unsigned int i;
500 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
501 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
503 for (i=0; i<msg->num_elements; i++) {
504 struct ldb_message_element *el = &msg->elements[i];
505 const struct dsdb_attribute *sa;
506 unsigned int j;
508 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
510 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
511 /* we only want forward links */
512 continue;
515 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
516 /* its an old style link, it will have been
517 * sent in the main replication data */
518 continue;
521 for (j=0; j<el->num_values; j++) {
522 struct dsdb_dn *dsdb_dn;
523 uint64_t local_usn;
524 NTSTATUS status;
525 WERROR werr;
527 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
528 if (dsdb_dn == NULL) {
529 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
530 el->name, ldb_dn_get_linearized(msg->dn)));
531 talloc_free(tmp_ctx);
532 return WERR_DS_DRA_INTERNAL_ERROR;
535 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
536 if (!NT_STATUS_IS_OK(status)) {
537 /* this can happen for attributes
538 given to us with old style meta
539 data */
540 continue;
543 if (local_usn > uSNChanged) {
544 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
545 el->name, ldb_dn_get_linearized(msg->dn)));
546 talloc_free(tmp_ctx);
547 return WERR_DS_DRA_INTERNAL_ERROR;
550 if (local_usn < highest_usn) {
551 continue;
554 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
555 dsdb_dn, la_list, la_count);
556 if (!W_ERROR_IS_OK(werr)) {
557 talloc_free(tmp_ctx);
558 return werr;
563 talloc_free(tmp_ctx);
564 return WERR_OK;
568 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
570 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
571 struct ldb_dn *ncRoot_dn,
572 struct drsuapi_DsReplicaCursor2CtrEx *udv)
574 int ret;
576 udv->version = 2;
577 udv->reserved1 = 0;
578 udv->reserved2 = 0;
580 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
581 if (ret != LDB_SUCCESS) {
582 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
583 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
584 return WERR_DS_DRA_INTERNAL_ERROR;
587 return WERR_OK;
591 /* comparison function for linked attributes - see CompareLinks() in
592 * MS-DRSR section 4.1.10.5.17 */
593 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
594 const struct drsuapi_DsReplicaLinkedAttribute *la2,
595 struct ldb_context *sam_ctx)
597 int c;
598 WERROR werr;
599 TALLOC_CTX *tmp_ctx;
600 const struct dsdb_schema *schema;
601 const struct dsdb_attribute *schema_attrib;
602 struct dsdb_dn *dn1, *dn2;
603 struct GUID guid1, guid2;
604 NTSTATUS status;
606 c = GUID_compare(&la1->identifier->guid,
607 &la2->identifier->guid);
608 if (c != 0) return c;
610 if (la1->attid != la2->attid) {
611 return la1->attid < la2->attid? -1:1;
614 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
615 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
616 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
619 /* we need to get the target GUIDs to compare */
620 tmp_ctx = talloc_new(sam_ctx);
622 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
623 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
625 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
626 if (!W_ERROR_IS_OK(werr)) {
627 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
628 talloc_free(tmp_ctx);
629 return 0;
632 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
633 if (!W_ERROR_IS_OK(werr)) {
634 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
635 talloc_free(tmp_ctx);
636 return 0;
639 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
640 if (!NT_STATUS_IS_OK(status)) {
641 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
642 talloc_free(tmp_ctx);
643 return 0;
645 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
646 if (!NT_STATUS_IS_OK(status)) {
647 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
648 talloc_free(tmp_ctx);
649 return 0;
652 talloc_free(tmp_ctx);
654 return GUID_compare(&guid1, &guid2);
657 struct drsuapi_changed_objects {
658 struct ldb_dn *dn;
659 struct GUID guid;
660 uint64_t usn;
664 sort the objects we send by tree order
666 static int site_res_cmp_parent_order(struct drsuapi_changed_objects *m1,
667 struct drsuapi_changed_objects *m2)
669 return ldb_dn_compare(m2->dn, m1->dn);
673 sort the objects we send first by uSNChanged
675 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects *m1,
676 struct drsuapi_changed_objects *m2)
678 unsigned usnchanged1, usnchanged2;
679 unsigned cn1, cn2;
681 cn1 = ldb_dn_get_comp_num(m1->dn);
682 cn2 = ldb_dn_get_comp_num(m2->dn);
683 if (cn1 != cn2) {
684 return cn1 > cn2 ? 1 : -1;
686 usnchanged1 = m1->usn;
687 usnchanged2 = m2->usn;
688 if (usnchanged1 == usnchanged2) {
689 return 0;
691 return usnchanged1 > usnchanged2 ? 1 : -1;
696 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
698 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
699 TALLOC_CTX *mem_ctx,
700 struct drsuapi_DsGetNCChangesRequest10 *req10,
701 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
703 struct ldb_dn *rid_manager_dn, *req_dn;
704 int ret;
705 struct ldb_context *ldb = b_state->sam_ctx;
706 struct ldb_result *ext_res;
707 struct dsdb_fsmo_extended_op *exop;
708 bool is_us;
711 steps:
712 - verify that the DN being asked for is the RID Manager DN
713 - verify that we are the RID Manager
716 /* work out who is the RID Manager */
717 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
718 if (ret != LDB_SUCCESS) {
719 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
720 return WERR_DS_DRA_INTERNAL_ERROR;
723 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
724 if (!ldb_dn_validate(req_dn) ||
725 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
726 /* that isn't the RID Manager DN */
727 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
728 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
729 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
730 return WERR_OK;
733 /* find the DN of the RID Manager */
734 ret = samdb_reference_dn_is_our_ntdsa(ldb, rid_manager_dn, "fSMORoleOwner", &is_us);
735 if (ret != LDB_SUCCESS) {
736 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
737 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
738 return WERR_DS_DRA_INTERNAL_ERROR;
741 if (!is_us) {
742 /* we're not the RID Manager - go away */
743 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
744 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
745 return WERR_OK;
748 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
749 W_ERROR_HAVE_NO_MEMORY(exop);
751 exop->fsmo_info = req10->fsmo_info;
752 exop->destination_dsa_guid = req10->destination_dsa_guid;
754 ret = ldb_transaction_start(ldb);
755 if (ret != LDB_SUCCESS) {
756 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
757 ldb_errstring(ldb)));
758 return WERR_DS_DRA_INTERNAL_ERROR;
762 * FIXME (kim): this is a temp hack to return just few object,
763 * but not the whole domain NC.
764 * We should remove this hack and implement a 'scope'
765 * building function to return just the set of object
766 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
768 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
770 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
771 if (ret != LDB_SUCCESS) {
772 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
773 ldb_errstring(ldb)));
774 ldb_transaction_cancel(ldb);
775 return WERR_DS_DRA_INTERNAL_ERROR;
778 ret = ldb_transaction_commit(ldb);
779 if (ret != LDB_SUCCESS) {
780 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
781 ldb_errstring(ldb)));
782 return WERR_DS_DRA_INTERNAL_ERROR;
785 talloc_free(ext_res);
787 DEBUG(2,("Allocated RID pool for server %s\n",
788 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
790 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
792 return WERR_OK;
796 return an array of SIDs from a ldb_message given an attribute name
797 assumes the SIDs are in extended DN format
799 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
800 struct ldb_message *msg,
801 TALLOC_CTX *mem_ctx,
802 const char *attr,
803 const struct dom_sid ***sids)
805 struct ldb_message_element *el;
806 unsigned int i;
808 el = ldb_msg_find_element(msg, attr);
809 if (!el) {
810 *sids = NULL;
811 return WERR_OK;
814 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
815 W_ERROR_HAVE_NO_MEMORY(*sids);
817 for (i=0; i<el->num_values; i++) {
818 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
819 NTSTATUS status;
820 struct dom_sid *sid;
822 sid = talloc(*sids, struct dom_sid);
823 W_ERROR_HAVE_NO_MEMORY(sid);
824 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
825 if (!NT_STATUS_IS_OK(status)) {
826 return WERR_INTERNAL_DB_CORRUPTION;
828 (*sids)[i] = sid;
830 (*sids)[i] = NULL;
832 return WERR_OK;
837 return an array of SIDs from a ldb_message given an attribute name
838 assumes the SIDs are in NDR form
840 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
841 struct ldb_message *msg,
842 TALLOC_CTX *mem_ctx,
843 const char *attr,
844 const struct dom_sid ***sids)
846 struct ldb_message_element *el;
847 unsigned int i;
849 el = ldb_msg_find_element(msg, attr);
850 if (!el) {
851 *sids = NULL;
852 return WERR_OK;
855 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
856 W_ERROR_HAVE_NO_MEMORY(*sids);
858 for (i=0; i<el->num_values; i++) {
859 enum ndr_err_code ndr_err;
860 struct dom_sid *sid;
862 sid = talloc(*sids, struct dom_sid);
863 W_ERROR_HAVE_NO_MEMORY(sid);
865 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
866 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
867 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
868 return WERR_INTERNAL_DB_CORRUPTION;
870 (*sids)[i] = sid;
872 (*sids)[i] = NULL;
874 return WERR_OK;
878 see if any SIDs in list1 are in list2
880 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
882 unsigned int i, j;
883 /* do we ever have enough SIDs here to worry about O(n^2) ? */
884 for (i=0; list1[i]; i++) {
885 for (j=0; list2[j]; j++) {
886 if (dom_sid_equal(list1[i], list2[j])) {
887 return true;
891 return false;
895 handle a DRSUAPI_EXOP_REPL_SECRET call
897 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
898 TALLOC_CTX *mem_ctx,
899 struct drsuapi_DsGetNCChangesRequest10 *req10,
900 struct dom_sid *user_sid,
901 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
902 bool has_get_all_changes)
904 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
905 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
906 int ret;
907 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
908 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
909 struct ldb_result *rodc_res, *obj_res;
910 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
911 WERROR werr;
913 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
914 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
917 * we need to work out if we will allow this DC to
918 * replicate the secrets for this object
920 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
921 * of this function
924 if (b_state->sam_ctx_system == NULL) {
925 /* this operation needs system level access */
926 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
927 return WERR_DS_DRA_SOURCE_DISABLED;
931 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
933 * The pseudo code indicate
934 * revealsecrets = true
935 * if IsRevealSecretRequest(msgIn) then
936 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
937 * then
938 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
939 * <... check if this account is ok to be replicated on this DC ...>
940 * <... and if not reveal secrets = no ...>
941 * else
942 * reveal secrets = false
943 * endif
944 * endif
945 * endif
947 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
948 * then you can do EXOP_REPL_SECRETS
950 if (has_get_all_changes) {
951 goto allowed;
954 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
955 if (!ldb_dn_validate(obj_dn)) goto failed;
957 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
958 dom_sid_string(mem_ctx, user_sid));
959 if (!ldb_dn_validate(rodc_dn)) goto failed;
961 /* do the two searches we need */
962 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
963 DSDB_SEARCH_SHOW_EXTENDED_DN);
964 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
966 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
967 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
969 /* if the object SID is equal to the user_sid, allow */
970 if (dom_sid_equal(user_sid,
971 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
972 goto allowed;
975 /* an RODC is allowed to get its own krbtgt account secrets */
976 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
977 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
978 if (krbtgt_link_dn != NULL &&
979 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
980 goto allowed;
983 /* but it isn't allowed to get anyone elses krbtgt secrets */
984 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
985 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
986 goto denied;
989 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
990 "userAccountControl", 0) &
991 UF_INTERDOMAIN_TRUST_ACCOUNT) {
992 goto denied;
995 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
996 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
997 if (!W_ERROR_IS_OK(werr)) {
998 goto denied;
1001 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1002 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1003 if (!W_ERROR_IS_OK(werr)) {
1004 goto denied;
1007 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1008 mem_ctx, "tokenGroups", &token_sids);
1009 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1010 goto denied;
1013 if (never_reveal_sids &&
1014 sid_list_match(token_sids, never_reveal_sids)) {
1015 goto denied;
1018 if (reveal_sids &&
1019 sid_list_match(token_sids, reveal_sids)) {
1020 goto allowed;
1023 /* default deny */
1024 denied:
1025 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1026 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1027 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1028 return WERR_DS_DRA_ACCESS_DENIED;
1030 allowed:
1031 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1032 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1033 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1034 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1035 req10->highwatermark.highest_usn = 0;
1036 return WERR_OK;
1038 failed:
1039 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1040 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1041 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1042 return WERR_DS_DRA_BAD_DN;
1047 handle a DRSUAPI_EXOP_REPL_OBJ call
1049 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1050 TALLOC_CTX *mem_ctx,
1051 struct drsuapi_DsGetNCChangesRequest10 *req10,
1052 struct dom_sid *user_sid,
1053 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1055 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1057 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1058 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1060 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1061 return WERR_OK;
1066 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1067 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1068 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1070 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1071 TALLOC_CTX *mem_ctx,
1072 struct drsuapi_DsGetNCChangesRequest10 *req10,
1073 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1075 struct ldb_dn *req_dn, *ntds_dn;
1076 int ret;
1077 unsigned int i;
1078 struct ldb_context *ldb = b_state->sam_ctx;
1079 struct ldb_message *msg;
1080 bool is_us;
1083 steps:
1084 - verify that the client dn exists
1085 - verify that we are the current master
1088 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1089 if (!ldb_dn_validate(req_dn)) {
1090 /* that is not a valid dn */
1091 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1092 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1093 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1094 return WERR_OK;
1097 /* retrieve the current role owner */
1098 /* find the DN of the RID Manager */
1099 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1100 if (ret != LDB_SUCCESS) {
1101 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1102 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1103 return WERR_DS_DRA_INTERNAL_ERROR;
1106 if (!is_us) {
1107 /* we're not the RID Manager - go away */
1108 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1109 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1110 return WERR_OK;
1113 /* change the current master */
1114 msg = ldb_msg_new(ldb);
1115 W_ERROR_HAVE_NO_MEMORY(msg);
1116 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1117 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1119 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1120 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1121 if (ret != LDB_SUCCESS) {
1122 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1123 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1124 talloc_free(msg);
1125 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1126 return WERR_OK;
1129 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1130 if (ret != 0) {
1131 talloc_free(msg);
1132 return WERR_DS_DRA_INTERNAL_ERROR;
1135 for (i=0;i<msg->num_elements;i++) {
1136 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1139 ret = ldb_transaction_start(ldb);
1140 if (ret != LDB_SUCCESS) {
1141 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1142 ldb_errstring(ldb)));
1143 return WERR_DS_DRA_INTERNAL_ERROR;
1146 ret = ldb_modify(ldb, msg);
1147 if (ret != LDB_SUCCESS) {
1148 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1149 ldb_errstring(ldb)));
1150 ldb_transaction_cancel(ldb);
1151 return WERR_DS_DRA_INTERNAL_ERROR;
1154 ret = ldb_transaction_commit(ldb);
1155 if (ret != LDB_SUCCESS) {
1156 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1157 ldb_errstring(ldb)));
1158 return WERR_DS_DRA_INTERNAL_ERROR;
1161 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1163 return WERR_OK;
1167 see if this getncchanges request includes a request to reveal secret information
1169 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1170 struct drsuapi_DsGetNCChangesRequest10 *req10,
1171 bool *is_secret_request)
1173 enum drsuapi_DsExtendedOperation exop;
1174 uint32_t i;
1175 struct dsdb_schema *schema;
1177 *is_secret_request = true;
1179 exop = req10->extended_op;
1181 switch (exop) {
1182 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1183 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1184 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1185 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1186 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1187 /* FSMO exops can reveal secrets */
1188 *is_secret_request = true;
1189 return WERR_OK;
1190 case DRSUAPI_EXOP_REPL_SECRET:
1191 case DRSUAPI_EXOP_REPL_OBJ:
1192 case DRSUAPI_EXOP_NONE:
1193 break;
1196 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1197 *is_secret_request = false;
1198 return WERR_OK;
1201 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1202 req10->partial_attribute_set == NULL) {
1203 /* they want secrets */
1204 *is_secret_request = true;
1205 return WERR_OK;
1208 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1210 /* check the attributes they asked for */
1211 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1212 const struct dsdb_attribute *sa;
1213 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1214 if (sa == NULL) {
1215 return WERR_DS_DRA_SCHEMA_MISMATCH;
1217 if (!dsdb_attr_in_rodc_fas(sa)) {
1218 *is_secret_request = true;
1219 return WERR_OK;
1223 if (req10->partial_attribute_set_ex) {
1224 /* check the extended attributes they asked for */
1225 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1226 const struct dsdb_attribute *sa;
1227 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1228 if (sa == NULL) {
1229 return WERR_DS_DRA_SCHEMA_MISMATCH;
1231 if (!dsdb_attr_in_rodc_fas(sa)) {
1232 *is_secret_request = true;
1233 return WERR_OK;
1238 *is_secret_request = false;
1239 return WERR_OK;
1243 see if this getncchanges request is only for attributes in the GC
1244 partial attribute set
1246 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1247 struct drsuapi_DsGetNCChangesRequest10 *req10,
1248 bool *is_gc_pas_request)
1250 enum drsuapi_DsExtendedOperation exop;
1251 uint32_t i;
1252 struct dsdb_schema *schema;
1254 exop = req10->extended_op;
1256 switch (exop) {
1257 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1258 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1259 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1260 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1261 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1262 case DRSUAPI_EXOP_REPL_SECRET:
1263 *is_gc_pas_request = false;
1264 return WERR_OK;
1265 case DRSUAPI_EXOP_REPL_OBJ:
1266 case DRSUAPI_EXOP_NONE:
1267 break;
1270 if (req10->partial_attribute_set == NULL) {
1271 /* they want it all */
1272 *is_gc_pas_request = false;
1273 return WERR_OK;
1276 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1278 /* check the attributes they asked for */
1279 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1280 const struct dsdb_attribute *sa;
1281 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1282 if (sa == NULL) {
1283 return WERR_DS_DRA_SCHEMA_MISMATCH;
1285 if (!sa->isMemberOfPartialAttributeSet) {
1286 *is_gc_pas_request = false;
1287 return WERR_OK;
1291 if (req10->partial_attribute_set_ex) {
1292 /* check the extended attributes they asked for */
1293 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1294 const struct dsdb_attribute *sa;
1295 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1296 if (sa == NULL) {
1297 return WERR_DS_DRA_SCHEMA_MISMATCH;
1299 if (!sa->isMemberOfPartialAttributeSet) {
1300 *is_gc_pas_request = false;
1301 return WERR_OK;
1306 *is_gc_pas_request = true;
1307 return WERR_OK;
1312 map from req8 to req10
1314 static struct drsuapi_DsGetNCChangesRequest10 *
1315 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1316 struct drsuapi_DsGetNCChangesRequest8 *req8)
1318 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1319 struct drsuapi_DsGetNCChangesRequest10);
1320 if (req10 == NULL) {
1321 return NULL;
1324 req10->destination_dsa_guid = req8->destination_dsa_guid;
1325 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1326 req10->naming_context = req8->naming_context;
1327 req10->highwatermark = req8->highwatermark;
1328 req10->uptodateness_vector = req8->uptodateness_vector;
1329 req10->replica_flags = req8->replica_flags;
1330 req10->max_object_count = req8->max_object_count;
1331 req10->max_ndr_size = req8->max_ndr_size;
1332 req10->extended_op = req8->extended_op;
1333 req10->fsmo_info = req8->fsmo_info;
1334 req10->partial_attribute_set = req8->partial_attribute_set;
1335 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1336 req10->mapping_ctr = req8->mapping_ctr;
1338 return req10;
1343 * Collects object for normal replication cycle.
1345 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1346 TALLOC_CTX *mem_ctx,
1347 struct drsuapi_DsGetNCChangesRequest10 *req10,
1348 struct ldb_dn *search_dn,
1349 const char *extra_filter,
1350 struct ldb_result **search_res)
1352 int ret;
1353 char* search_filter;
1354 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1355 //const char *extra_filter;
1356 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1357 const char *attrs[] = { "uSNChanged",
1358 "objectGUID" ,
1359 NULL };
1361 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1362 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1363 scope = LDB_SCOPE_BASE;
1366 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1368 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1370 /* Construct response. */
1371 search_filter = talloc_asprintf(mem_ctx,
1372 "(uSNChanged>=%llu)",
1373 (unsigned long long)(getnc_state->min_usn+1));
1375 if (extra_filter) {
1376 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1379 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1380 search_filter = talloc_asprintf(mem_ctx,
1381 "(&%s(isCriticalSystemObject=TRUE))",
1382 search_filter);
1385 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1386 scope = LDB_SCOPE_BASE;
1389 if (!search_dn) {
1390 search_dn = getnc_state->ncRoot_dn;
1393 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1394 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1395 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1396 search_dn, scope, attrs,
1397 search_filter);
1398 if (ret != LDB_SUCCESS) {
1399 return WERR_DS_DRA_INTERNAL_ERROR;
1402 return WERR_OK;
1406 * Collects object for normal replication cycle.
1408 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1409 TALLOC_CTX *mem_ctx,
1410 struct drsuapi_DsGetNCChangesRequest10 *req10,
1411 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1412 struct ldb_dn *search_dn,
1413 const char *extra_filter,
1414 struct ldb_result **search_res)
1416 /* we have nothing to do in case of ex-op failure */
1417 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1418 return WERR_OK;
1421 /* TODO: implement extended op specific collection
1422 * of objects. Right now we just normal procedure
1423 * for collecting objects */
1424 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1428 drsuapi_DsGetNCChanges
1430 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1432 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1433 struct drsuapi_DsGetNCChanges *r)
1435 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1436 int ret;
1437 uint32_t i;
1438 struct dsdb_schema *schema;
1439 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1440 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1441 NTSTATUS status;
1442 DATA_BLOB session_key;
1443 WERROR werr;
1444 struct dcesrv_handle *h;
1445 struct drsuapi_bind_state *b_state;
1446 struct drsuapi_getncchanges_state *getnc_state;
1447 struct drsuapi_DsGetNCChangesRequest10 *req10;
1448 uint32_t options;
1449 uint32_t max_objects;
1450 uint32_t max_links;
1451 uint32_t link_count = 0;
1452 uint32_t link_total = 0;
1453 uint32_t link_given = 0;
1454 struct ldb_dn *search_dn = NULL;
1455 bool am_rodc, null_scope=false;
1456 enum security_user_level security_level;
1457 struct ldb_context *sam_ctx;
1458 struct dom_sid *user_sid;
1459 bool is_secret_request;
1460 bool is_gc_pas_request;
1461 struct drsuapi_changed_objects *changes;
1462 time_t max_wait;
1463 time_t start = time(NULL);
1464 bool max_wait_reached = false;
1465 bool has_get_all_changes = false;
1467 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1468 b_state = h->data;
1470 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1472 *r->out.level_out = 6;
1473 /* TODO: linked attributes*/
1474 r->out.ctr->ctr6.linked_attributes_count = 0;
1475 r->out.ctr->ctr6.linked_attributes = NULL;
1477 r->out.ctr->ctr6.object_count = 0;
1478 r->out.ctr->ctr6.nc_object_count = 0;
1479 r->out.ctr->ctr6.more_data = false;
1480 r->out.ctr->ctr6.uptodateness_vector = NULL;
1482 /* a RODC doesn't allow for any replication */
1483 ret = samdb_rodc(sam_ctx, &am_rodc);
1484 if (ret == LDB_SUCCESS && am_rodc) {
1485 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1486 return WERR_DS_DRA_SOURCE_DISABLED;
1489 /* Check request revision.
1491 switch (r->in.level) {
1492 case 8:
1493 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1494 if (req10 == NULL) {
1495 return WERR_NOMEM;
1497 break;
1498 case 10:
1499 req10 = &r->in.req->req10;
1500 break;
1501 default:
1502 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1503 r->in.level));
1504 return WERR_REVISION_MISMATCH;
1508 /* Perform access checks. */
1509 /* TODO: we need to support a sync on a specific non-root
1510 * DN. We'll need to find the real partition root here */
1511 ncRoot = req10->naming_context;
1512 if (ncRoot == NULL) {
1513 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1514 return WERR_DS_DRA_INVALID_PARAMETER;
1517 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1518 return WERR_DS_DRA_INTERNAL_ERROR;
1521 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1522 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1523 return WERR_DS_DRA_SOURCE_DISABLED;
1526 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1528 /* all clients must have GUID_DRS_GET_CHANGES */
1529 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1530 mem_ctx,
1531 dce_call->conn->auth_state.session_info->security_token,
1532 req10->naming_context,
1533 GUID_DRS_GET_CHANGES);
1534 if (!W_ERROR_IS_OK(werr)) {
1535 return werr;
1538 /* allowed if the GC PAS and client has
1539 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1540 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1541 if (!W_ERROR_IS_OK(werr)) {
1542 return werr;
1544 if (is_gc_pas_request) {
1545 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1546 mem_ctx,
1547 dce_call->conn->auth_state.session_info->security_token,
1548 req10->naming_context,
1549 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1550 if (W_ERROR_IS_OK(werr)) {
1551 goto allowed;
1555 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1556 if (!W_ERROR_IS_OK(werr)) {
1557 return werr;
1559 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1560 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1561 mem_ctx,
1562 dce_call->conn->auth_state.session_info->security_token,
1563 req10->naming_context,
1564 GUID_DRS_GET_ALL_CHANGES);
1565 if (!W_ERROR_IS_OK(werr)) {
1566 return werr;
1567 } else {
1568 has_get_all_changes = true;
1572 allowed:
1573 /* for non-administrator replications, check that they have
1574 given the correct source_dsa_invocation_id */
1575 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1576 samdb_domain_sid(sam_ctx));
1577 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1578 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1579 /* we rely on this flag being unset for RODC requests */
1580 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1584 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1585 /* Ignore the _in_ uptpdateness vector*/
1586 req10->uptodateness_vector = NULL;
1589 getnc_state = b_state->getncchanges_state;
1591 /* see if a previous replication has been abandoned */
1592 if (getnc_state) {
1593 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1594 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1595 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1596 ldb_dn_get_linearized(new_dn),
1597 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1598 ldb_dn_get_linearized(getnc_state->last_dn)));
1599 talloc_free(getnc_state);
1600 getnc_state = NULL;
1604 if (getnc_state == NULL) {
1605 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1606 if (getnc_state == NULL) {
1607 return WERR_NOMEM;
1609 b_state->getncchanges_state = getnc_state;
1610 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1612 /* find out if we are to replicate Schema NC */
1613 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1614 ldb_get_schema_basedn(b_state->sam_ctx));
1615 getnc_state->is_schema_nc = (0 == ret);
1617 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1618 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1622 * This is the first replication cycle and it is
1623 * a good place to handle extended operations
1625 * FIXME: we don't fully support extended operations yet
1627 switch (req10->extended_op) {
1628 case DRSUAPI_EXOP_NONE:
1629 break;
1630 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1631 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1632 W_ERROR_NOT_OK_RETURN(werr);
1633 search_dn = ldb_get_default_basedn(sam_ctx);
1634 break;
1635 case DRSUAPI_EXOP_REPL_SECRET:
1636 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1637 user_sid,
1638 &r->out.ctr->ctr6,
1639 has_get_all_changes);
1640 r->out.result = werr;
1641 W_ERROR_NOT_OK_RETURN(werr);
1642 break;
1643 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1644 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1645 W_ERROR_NOT_OK_RETURN(werr);
1646 break;
1647 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1648 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1649 W_ERROR_NOT_OK_RETURN(werr);
1650 break;
1651 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1652 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1653 W_ERROR_NOT_OK_RETURN(werr);
1654 break;
1655 case DRSUAPI_EXOP_REPL_OBJ:
1656 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1657 r->out.result = werr;
1658 W_ERROR_NOT_OK_RETURN(werr);
1659 break;
1661 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1663 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1664 (unsigned)req10->extended_op));
1665 return WERR_DS_DRA_NOT_SUPPORTED;
1669 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1670 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1671 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1672 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1673 return WERR_DS_DRA_INVALID_PARAMETER;
1676 /* we need the session key for encrypting password attributes */
1677 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1678 if (!NT_STATUS_IS_OK(status)) {
1679 DEBUG(0,(__location__ ": Failed to get session key\n"));
1680 return WERR_DS_DRA_INTERNAL_ERROR;
1684 TODO: MS-DRSR section 4.1.10.1.1
1685 Work out if this is the start of a new cycle */
1687 if (getnc_state->guids == NULL) {
1688 const char *extra_filter;
1689 struct ldb_result *search_res = NULL;
1691 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1693 getnc_state->min_usn = req10->highwatermark.highest_usn;
1695 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1696 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1697 search_dn, extra_filter,
1698 &search_res);
1699 } else {
1700 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1701 &r->out.ctr->ctr6,
1702 search_dn, extra_filter,
1703 &search_res);
1705 W_ERROR_NOT_OK_RETURN(werr);
1707 /* extract out the GUIDs list */
1708 getnc_state->num_records = search_res ? search_res->count : 0;
1709 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1710 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1712 changes = talloc_array(getnc_state,
1713 struct drsuapi_changed_objects,
1714 getnc_state->num_records);
1715 W_ERROR_HAVE_NO_MEMORY(changes);
1717 for (i=0; i<getnc_state->num_records; i++) {
1718 changes[i].dn = search_res->msgs[i]->dn;
1719 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1720 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1723 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1724 TYPESAFE_QSORT(changes,
1725 getnc_state->num_records,
1726 site_res_cmp_parent_order);
1727 } else {
1728 TYPESAFE_QSORT(changes,
1729 getnc_state->num_records,
1730 site_res_cmp_dn_usn_order);
1733 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req10->uptodateness_vector);
1734 if (getnc_state->uptodateness_vector) {
1735 /* make sure its sorted */
1736 TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1737 getnc_state->uptodateness_vector->count,
1738 drsuapi_DsReplicaCursor_compare);
1741 for (i=0; i < getnc_state->num_records; i++) {
1742 getnc_state->guids[i] = changes[i].guid;
1743 if (GUID_all_zero(&getnc_state->guids[i])) {
1744 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1745 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1746 return WERR_DS_DRA_INTERNAL_ERROR;
1750 talloc_free(search_res);
1751 talloc_free(changes);
1754 /* Prefix mapping */
1755 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1756 if (!schema) {
1757 DEBUG(0,("No schema in sam_ctx\n"));
1758 return WERR_DS_DRA_INTERNAL_ERROR;
1761 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1762 *r->out.ctr->ctr6.naming_context = *ncRoot;
1764 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1765 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1766 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1767 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1768 return WERR_DS_DRA_INTERNAL_ERROR;
1771 /* find the SID if there is one */
1772 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1774 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1775 r->out.ctr->ctr6.mapping_ctr = *ctr;
1777 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1778 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1780 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1781 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1783 r->out.ctr->ctr6.first_object = NULL;
1784 currentObject = &r->out.ctr->ctr6.first_object;
1786 /* use this to force single objects at a time, which is useful
1787 * for working out what object is giving problems
1789 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1790 if (req10->max_object_count < max_objects) {
1791 max_objects = req10->max_object_count;
1794 * TODO: work out how the maximum should be calculated
1796 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1799 * Maximum time that we can spend in a getncchanges
1800 * in order to avoid timeout of the other part.
1801 * 10 seconds by default.
1803 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1804 for (i=getnc_state->num_processed;
1805 i<getnc_state->num_records &&
1806 !null_scope &&
1807 (r->out.ctr->ctr6.object_count < max_objects)
1808 && !max_wait_reached;
1809 i++) {
1810 int uSN;
1811 struct drsuapi_DsReplicaObjectListItemEx *obj;
1812 struct ldb_message *msg;
1813 static const char * const msg_attrs[] = {
1814 "*",
1815 "nTSecurityDescriptor",
1816 "parentGUID",
1817 "replPropertyMetaData",
1818 DSDB_SECRET_ATTRIBUTES,
1819 NULL };
1820 struct ldb_result *msg_res;
1821 struct ldb_dn *msg_dn;
1823 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1824 W_ERROR_HAVE_NO_MEMORY(obj);
1826 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1827 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1830 /* by re-searching here we avoid having a lot of full
1831 * records in memory between calls to getncchanges
1833 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1834 msg_dn,
1835 LDB_SCOPE_BASE, msg_attrs, NULL);
1836 if (ret != LDB_SUCCESS) {
1837 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1838 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1839 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1841 talloc_free(obj);
1842 continue;
1845 msg = msg_res->msgs[0];
1847 max_wait_reached = (time(NULL) - start > max_wait);
1849 werr = get_nc_changes_build_object(obj, msg,
1850 sam_ctx, getnc_state->ncRoot_dn,
1851 getnc_state->is_schema_nc,
1852 schema, &session_key, getnc_state->min_usn,
1853 req10->replica_flags,
1854 req10->partial_attribute_set,
1855 getnc_state->uptodateness_vector,
1856 req10->extended_op,
1857 max_wait_reached);
1858 if (!W_ERROR_IS_OK(werr)) {
1859 return werr;
1862 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1863 getnc_state->ncRoot_dn,
1864 schema, getnc_state->min_usn,
1865 req10->replica_flags,
1866 msg,
1867 &getnc_state->la_list,
1868 &getnc_state->la_count,
1869 getnc_state->uptodateness_vector);
1870 if (!W_ERROR_IS_OK(werr)) {
1871 return werr;
1874 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1875 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1876 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1879 if (obj->meta_data_ctr == NULL) {
1880 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1881 ldb_dn_get_linearized(msg->dn)));
1882 /* no attributes to send */
1883 talloc_free(obj);
1884 continue;
1887 r->out.ctr->ctr6.object_count++;
1889 *currentObject = obj;
1890 currentObject = &obj->next_object;
1892 talloc_free(getnc_state->last_dn);
1893 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1895 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1897 talloc_free(msg_res);
1898 talloc_free(msg_dn);
1901 getnc_state->num_processed = i;
1903 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1905 /* the client can us to call UpdateRefs on its behalf to
1906 re-establish monitoring of the NC */
1907 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1908 !GUID_all_zero(&req10->destination_dsa_guid)) {
1909 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1910 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1911 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1912 ureq.naming_context = ncRoot;
1913 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
1914 &req10->destination_dsa_guid);
1915 if (!ureq.dest_dsa_dns_name) {
1916 return WERR_NOMEM;
1918 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1919 ureq.options = DRSUAPI_DRS_ADD_REF |
1920 DRSUAPI_DRS_ASYNC_OP |
1921 DRSUAPI_DRS_GETCHG_CHECK;
1923 /* we also need to pass through the
1924 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1925 to send notifies using the GC SPN */
1926 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1928 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1929 if (!W_ERROR_IS_OK(werr)) {
1930 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1931 win_errstr(werr)));
1936 * TODO:
1937 * This is just a guess, how to calculate the
1938 * number of linked attributes to send, we need to
1939 * find out how to do this right.
1941 if (r->out.ctr->ctr6.object_count >= max_links) {
1942 max_links = 0;
1943 } else {
1944 max_links -= r->out.ctr->ctr6.object_count;
1947 link_total = getnc_state->la_count;
1949 if (i < getnc_state->num_records) {
1950 r->out.ctr->ctr6.more_data = true;
1951 } else {
1952 /* sort the whole array the first time */
1953 if (!getnc_state->la_sorted) {
1954 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1955 sam_ctx, linked_attribute_compare);
1956 getnc_state->la_sorted = true;
1959 link_count = getnc_state->la_count - getnc_state->la_idx;
1960 link_count = MIN(max_links, link_count);
1962 r->out.ctr->ctr6.linked_attributes_count = link_count;
1963 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1965 getnc_state->la_idx += link_count;
1966 link_given = getnc_state->la_idx;
1968 if (getnc_state->la_idx < getnc_state->la_count) {
1969 r->out.ctr->ctr6.more_data = true;
1973 if (!r->out.ctr->ctr6.more_data) {
1974 talloc_steal(mem_ctx, getnc_state->la_list);
1976 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1977 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1979 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1980 r->out.ctr->ctr6.uptodateness_vector);
1981 if (!W_ERROR_IS_OK(werr)) {
1982 return werr;
1985 talloc_free(getnc_state);
1986 b_state->getncchanges_state = NULL;
1989 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1990 r->out.ctr->ctr6.uptodateness_vector = NULL;
1991 r->out.ctr->ctr6.nc_object_count = 0;
1992 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1995 DEBUG(r->out.ctr->ctr6.more_data?4:2,
1996 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1997 (unsigned long long)(req10->highwatermark.highest_usn+1),
1998 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
1999 r->out.ctr->ctr6.object_count,
2000 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2001 r->out.ctr->ctr6.linked_attributes_count,
2002 link_given, link_total,
2003 dom_sid_string(mem_ctx, user_sid)));
2005 #if 0
2006 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2007 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2009 #endif
2011 return WERR_OK;