s4:drsuapi: move struct drsuapi_getncchanges_state to the top of getncchanges.c
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / getncchanges.c
blob00988fc381d4363594f477735fd45c969d180000
1 /*
2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "libcli/security/security.h"
31 #include "libcli/security/session.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33 #include "rpc_server/dcerpc_server_proto.h"
34 #include "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
40 /* state of a partially completed getncchanges call */
41 struct drsuapi_getncchanges_state {
42 struct GUID *guids;
43 uint32_t num_records;
44 uint32_t num_processed;
45 struct ldb_dn *ncRoot_dn;
46 bool is_schema_nc;
47 uint64_t min_usn;
48 uint64_t highest_usn;
49 struct ldb_dn *last_dn;
50 struct drsuapi_DsReplicaLinkedAttribute *la_list;
51 uint32_t la_count;
52 bool la_sorted;
53 uint32_t la_idx;
54 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
58 build a DsReplicaObjectIdentifier from a ldb msg
60 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
61 struct ldb_message *msg)
63 struct drsuapi_DsReplicaObjectIdentifier *identifier;
64 struct dom_sid *sid;
66 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
67 if (identifier == NULL) {
68 return NULL;
71 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
72 identifier->guid = samdb_result_guid(msg, "objectGUID");
74 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
75 if (sid) {
76 identifier->sid = *sid;
77 } else {
78 ZERO_STRUCT(identifier->sid);
80 return identifier;
83 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
85 return GUID_compare(guid1, &guid2);
89 see if we can filter an attribute using the uptodateness_vector
91 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
92 const struct GUID *originating_invocation_id,
93 uint64_t originating_usn)
95 const struct drsuapi_DsReplicaCursor *c;
96 if (udv == NULL) return false;
97 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
98 originating_invocation_id, udv_compare, c);
99 if (c && originating_usn <= c->highest_usn) {
100 return true;
102 return false;
106 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
108 if (a1 == a2) return 0;
109 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
113 check if an attribute is in a partial_attribute_set
115 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
116 struct drsuapi_DsPartialAttributeSet *pas)
118 enum drsuapi_DsAttributeId *result;
119 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
120 attid_cmp, result);
121 return result != NULL;
126 drsuapi_DsGetNCChanges for one object
128 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
129 struct ldb_message *msg,
130 struct ldb_context *sam_ctx,
131 struct ldb_dn *ncRoot_dn,
132 bool is_schema_nc,
133 struct dsdb_schema *schema,
134 DATA_BLOB *session_key,
135 uint64_t highest_usn,
136 uint32_t replica_flags,
137 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
138 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
139 enum drsuapi_DsExtendedOperation extended_op,
140 bool force_object_return)
142 const struct ldb_val *md_value;
143 uint32_t i, n;
144 struct replPropertyMetaDataBlob md;
145 uint32_t rid = 0;
146 enum ndr_err_code ndr_err;
147 uint32_t *attids;
148 const char *rdn;
149 const struct dsdb_attribute *rdn_sa;
150 unsigned int instanceType;
151 struct dsdb_syntax_ctx syntax_ctx;
153 /* make dsdb sytanx context for conversions */
154 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
155 syntax_ctx.is_schema_nc = is_schema_nc;
157 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
158 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
159 obj->is_nc_prefix = true;
160 obj->parent_object_guid = NULL;
161 } else {
162 obj->is_nc_prefix = false;
163 obj->parent_object_guid = talloc(obj, struct GUID);
164 if (obj->parent_object_guid == NULL) {
165 return WERR_DS_DRA_INTERNAL_ERROR;
167 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
168 if (GUID_all_zero(obj->parent_object_guid)) {
169 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
170 ldb_dn_get_linearized(msg->dn)));
171 return WERR_DS_DRA_INTERNAL_ERROR;
174 obj->next_object = NULL;
176 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
177 if (!md_value) {
178 /* nothing to send */
179 return WERR_OK;
182 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
183 /* don't send uninstantiated objects */
184 return WERR_OK;
187 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
188 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
189 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
190 return WERR_DS_DRA_INTERNAL_ERROR;
193 if (md.version != 1) {
194 return WERR_DS_DRA_INTERNAL_ERROR;
197 rdn = ldb_dn_get_rdn_name(msg->dn);
198 if (rdn == NULL) {
199 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
200 return WERR_DS_DRA_INTERNAL_ERROR;
203 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
204 if (rdn_sa == NULL) {
205 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
206 rdn, ldb_dn_get_linearized(msg->dn)));
207 return WERR_DS_DRA_INTERNAL_ERROR;
210 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
211 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
213 obj->object.identifier = get_object_identifier(obj, msg);
214 if (obj->object.identifier == NULL) {
215 return WERR_NOMEM;
217 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
219 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
220 for (n=i=0; i<md.ctr.ctr1.count; i++) {
221 const struct dsdb_attribute *sa;
222 bool force_attribute = false;
224 /* if the attribute has not changed, and it is not the
225 instanceType then don't include it */
226 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
227 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
228 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
230 /* don't include the rDN */
231 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
233 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
234 if (!sa) {
235 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
236 (unsigned int)md.ctr.ctr1.array[i].attid,
237 ldb_dn_get_linearized(msg->dn)));
238 return WERR_DS_DRA_INTERNAL_ERROR;
241 if (sa->linkID) {
242 struct ldb_message_element *el;
243 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
244 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
245 /* don't send upgraded links inline */
246 continue;
250 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
251 !dsdb_attr_in_rodc_fas(sa)) {
252 force_attribute = true;
253 DEBUG(4,("Forcing attribute %s in %s\n",
254 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
257 /* filter by uptodateness_vector */
258 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
259 !force_attribute &&
260 udv_filter(uptodateness_vector,
261 &md.ctr.ctr1.array[i].originating_invocation_id,
262 md.ctr.ctr1.array[i].originating_usn)) {
263 continue;
266 /* filter by partial_attribute_set */
267 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
268 continue;
271 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
272 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
273 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
274 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
275 attids[n] = md.ctr.ctr1.array[i].attid;
276 n++;
279 /* ignore it if its an empty change. Note that renames always
280 * change the 'name' attribute, so they won't be ignored by
281 * this
283 * the force_object_return check is used to force an empty
284 * object return when we timeout in the getncchanges loop.
285 * This allows us to return an empty object, which keeps the
286 * client happy while preventing timeouts
288 if (n == 0 ||
289 (n == 1 &&
290 attids[0] == DRSUAPI_ATTID_instanceType &&
291 !force_object_return)) {
292 talloc_free(obj->meta_data_ctr);
293 obj->meta_data_ctr = NULL;
294 return WERR_OK;
297 obj->meta_data_ctr->count = n;
299 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
300 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
301 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
302 obj->object.attribute_ctr.num_attributes);
303 if (obj->object.attribute_ctr.attributes == NULL) {
304 return WERR_NOMEM;
308 * Note that the meta_data array and the attributes array must
309 * be the same size and in the same order
311 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
312 struct ldb_message_element *el;
313 WERROR werr;
314 const struct dsdb_attribute *sa;
316 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
317 if (!sa) {
318 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
319 return WERR_DS_DRA_INTERNAL_ERROR;
322 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
323 if (el == NULL) {
324 /* this happens for attributes that have been removed */
325 DEBUG(5,("No element '%s' for attributeID %u in message\n",
326 sa->lDAPDisplayName, attids[i]));
327 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
328 obj->object.attribute_ctr.attributes[i].attid =
329 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
330 } else {
331 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
332 &obj->object.attribute_ctr.attributes[i]);
333 if (!W_ERROR_IS_OK(werr)) {
334 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
335 sa->lDAPDisplayName, win_errstr(werr)));
336 return werr;
338 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
339 * check if attribute is secret and send a null value
341 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
342 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
343 &obj->meta_data_ctr->meta_data[i]);
345 /* some attributes needs to be encrypted
346 before being sent */
347 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
348 &obj->object.attribute_ctr.attributes[i]);
349 if (!W_ERROR_IS_OK(werr)) {
350 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
351 sa->lDAPDisplayName, win_errstr(werr)));
352 return werr;
357 return WERR_OK;
361 add one linked attribute from an object to the list of linked
362 attributes in a getncchanges request
364 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
365 struct ldb_context *sam_ctx,
366 const struct dsdb_schema *schema,
367 const struct dsdb_attribute *sa,
368 struct ldb_message *msg,
369 struct dsdb_dn *dsdb_dn,
370 struct drsuapi_DsReplicaLinkedAttribute **la_list,
371 uint32_t *la_count)
373 struct drsuapi_DsReplicaLinkedAttribute *la;
374 bool active;
375 NTSTATUS status;
376 WERROR werr;
378 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
379 W_ERROR_HAVE_NO_MEMORY(*la_list);
381 la = &(*la_list)[*la_count];
383 la->identifier = get_object_identifier(*la_list, msg);
384 W_ERROR_HAVE_NO_MEMORY(la->identifier);
386 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
388 if (!active) {
389 /* We have to check that the inactive link still point to an existing object */
390 struct GUID guid;
391 struct ldb_dn *tdn;
392 int ret;
393 const char *v;
395 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
396 if (strncmp(v, "TRUE", 4) == 0) {
398 * Note: we skip the transmition of the deleted link even if the other part used to
399 * know about it because when we transmit the deletion of the object, the link will
400 * be deleted too due to deletion of object where link points and Windows do so.
402 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
403 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
405 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
406 * if it join an existing domain with deleted objets, it firsts impose to have a
407 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
408 * either during initial replication or after the getNCChanges.
409 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
411 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
412 * If FL >=2K8R2 we are sure that this attribute will be here.
413 * For this kind of forest level we do not return the link if the object is recycled
414 * (isRecycled = true).
416 if (strncmp(v, "TRUE", 4) == 0) {
417 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
418 ldb_dn_get_linearized(msg->dn)));
419 return WERR_OK;
421 } else {
422 return WERR_OK;
425 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
426 if (!NT_STATUS_IS_OK(status)) {
427 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
428 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
429 return ntstatus_to_werror(status);
431 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
432 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
433 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
434 GUID_string(mem_ctx, &guid)));
435 return WERR_OK;
436 } else if (ret != LDB_SUCCESS) {
437 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
438 GUID_string(mem_ctx, &guid),
439 ret));
440 return WERR_OK;
443 la->attid = sa->attributeID_id;
444 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
446 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
447 if (!NT_STATUS_IS_OK(status)) {
448 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
449 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
450 return ntstatus_to_werror(status);
452 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
453 if (!NT_STATUS_IS_OK(status)) {
454 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
455 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
456 return ntstatus_to_werror(status);
458 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
459 if (!NT_STATUS_IS_OK(status)) {
460 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
461 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
462 return ntstatus_to_werror(status);
464 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
465 if (!NT_STATUS_IS_OK(status)) {
466 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
467 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
468 return ntstatus_to_werror(status);
471 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
472 if (!NT_STATUS_IS_OK(status)) {
473 /* this is possible for upgraded links */
474 la->originating_add_time = la->meta_data.originating_change_time;
477 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
478 W_ERROR_NOT_OK_RETURN(werr);
480 (*la_count)++;
481 return WERR_OK;
486 add linked attributes from an object to the list of linked
487 attributes in a getncchanges request
489 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
490 TALLOC_CTX *mem_ctx,
491 struct ldb_dn *ncRoot_dn,
492 struct dsdb_schema *schema,
493 uint64_t highest_usn,
494 uint32_t replica_flags,
495 struct ldb_message *msg,
496 struct drsuapi_DsReplicaLinkedAttribute **la_list,
497 uint32_t *la_count,
498 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
500 unsigned int i;
501 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
502 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
504 for (i=0; i<msg->num_elements; i++) {
505 struct ldb_message_element *el = &msg->elements[i];
506 const struct dsdb_attribute *sa;
507 unsigned int j;
509 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
511 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
512 /* we only want forward links */
513 continue;
516 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
517 /* its an old style link, it will have been
518 * sent in the main replication data */
519 continue;
522 for (j=0; j<el->num_values; j++) {
523 struct dsdb_dn *dsdb_dn;
524 uint64_t local_usn;
525 NTSTATUS status;
526 WERROR werr;
528 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
529 if (dsdb_dn == NULL) {
530 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
531 el->name, ldb_dn_get_linearized(msg->dn)));
532 talloc_free(tmp_ctx);
533 return WERR_DS_DRA_INTERNAL_ERROR;
536 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
537 if (!NT_STATUS_IS_OK(status)) {
538 /* this can happen for attributes
539 given to us with old style meta
540 data */
541 continue;
544 if (local_usn > uSNChanged) {
545 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
546 el->name, ldb_dn_get_linearized(msg->dn)));
547 talloc_free(tmp_ctx);
548 return WERR_DS_DRA_INTERNAL_ERROR;
551 if (local_usn < highest_usn) {
552 continue;
555 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
556 dsdb_dn, la_list, la_count);
557 if (!W_ERROR_IS_OK(werr)) {
558 talloc_free(tmp_ctx);
559 return werr;
564 talloc_free(tmp_ctx);
565 return WERR_OK;
569 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
571 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
572 struct ldb_dn *ncRoot_dn,
573 struct drsuapi_DsReplicaCursor2CtrEx *udv)
575 int ret;
577 udv->version = 2;
578 udv->reserved1 = 0;
579 udv->reserved2 = 0;
581 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
582 if (ret != LDB_SUCCESS) {
583 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
584 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
585 return WERR_DS_DRA_INTERNAL_ERROR;
588 return WERR_OK;
592 /* comparison function for linked attributes - see CompareLinks() in
593 * MS-DRSR section 4.1.10.5.17 */
594 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
595 const struct drsuapi_DsReplicaLinkedAttribute *la2,
596 struct ldb_context *sam_ctx)
598 int c;
599 WERROR werr;
600 TALLOC_CTX *tmp_ctx;
601 const struct dsdb_schema *schema;
602 const struct dsdb_attribute *schema_attrib;
603 struct dsdb_dn *dn1, *dn2;
604 struct GUID guid1, guid2;
605 NTSTATUS status;
607 c = GUID_compare(&la1->identifier->guid,
608 &la2->identifier->guid);
609 if (c != 0) return c;
611 if (la1->attid != la2->attid) {
612 return la1->attid < la2->attid? -1:1;
615 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
616 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
617 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
620 /* we need to get the target GUIDs to compare */
621 tmp_ctx = talloc_new(sam_ctx);
623 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
624 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
626 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
627 if (!W_ERROR_IS_OK(werr)) {
628 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
629 talloc_free(tmp_ctx);
630 return 0;
633 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
634 if (!W_ERROR_IS_OK(werr)) {
635 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
636 talloc_free(tmp_ctx);
637 return 0;
640 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
641 if (!NT_STATUS_IS_OK(status)) {
642 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
643 talloc_free(tmp_ctx);
644 return 0;
646 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
647 if (!NT_STATUS_IS_OK(status)) {
648 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
649 talloc_free(tmp_ctx);
650 return 0;
653 talloc_free(tmp_ctx);
655 return GUID_compare(&guid1, &guid2);
658 struct drsuapi_changed_objects {
659 struct ldb_dn *dn;
660 struct GUID guid;
661 uint64_t usn;
665 sort the objects we send by tree order
667 static int site_res_cmp_parent_order(struct drsuapi_changed_objects *m1,
668 struct drsuapi_changed_objects *m2)
670 return ldb_dn_compare(m2->dn, m1->dn);
674 sort the objects we send first by uSNChanged
676 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects *m1,
677 struct drsuapi_changed_objects *m2)
679 unsigned usnchanged1, usnchanged2;
680 unsigned cn1, cn2;
682 cn1 = ldb_dn_get_comp_num(m1->dn);
683 cn2 = ldb_dn_get_comp_num(m2->dn);
684 if (cn1 != cn2) {
685 return cn1 > cn2 ? 1 : -1;
687 usnchanged1 = m1->usn;
688 usnchanged2 = m2->usn;
689 if (usnchanged1 == usnchanged2) {
690 return 0;
692 return usnchanged1 > usnchanged2 ? 1 : -1;
697 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
699 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
700 TALLOC_CTX *mem_ctx,
701 struct drsuapi_DsGetNCChangesRequest10 *req10,
702 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
704 struct ldb_dn *rid_manager_dn, *req_dn;
705 int ret;
706 struct ldb_context *ldb = b_state->sam_ctx;
707 struct ldb_result *ext_res;
708 struct dsdb_fsmo_extended_op *exop;
709 bool is_us;
712 steps:
713 - verify that the DN being asked for is the RID Manager DN
714 - verify that we are the RID Manager
717 /* work out who is the RID Manager */
718 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
719 if (ret != LDB_SUCCESS) {
720 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
721 return WERR_DS_DRA_INTERNAL_ERROR;
724 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
725 if (!ldb_dn_validate(req_dn) ||
726 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
727 /* that isn't the RID Manager DN */
728 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
729 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
730 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
731 return WERR_OK;
734 /* find the DN of the RID Manager */
735 ret = samdb_reference_dn_is_our_ntdsa(ldb, rid_manager_dn, "fSMORoleOwner", &is_us);
736 if (ret != LDB_SUCCESS) {
737 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
738 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
739 return WERR_DS_DRA_INTERNAL_ERROR;
742 if (!is_us) {
743 /* we're not the RID Manager - go away */
744 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
745 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
746 return WERR_OK;
749 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
750 W_ERROR_HAVE_NO_MEMORY(exop);
752 exop->fsmo_info = req10->fsmo_info;
753 exop->destination_dsa_guid = req10->destination_dsa_guid;
755 ret = ldb_transaction_start(ldb);
756 if (ret != LDB_SUCCESS) {
757 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
758 ldb_errstring(ldb)));
759 return WERR_DS_DRA_INTERNAL_ERROR;
763 * FIXME (kim): this is a temp hack to return just few object,
764 * but not the whole domain NC.
765 * We should remove this hack and implement a 'scope'
766 * building function to return just the set of object
767 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
769 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
771 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
772 if (ret != LDB_SUCCESS) {
773 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
774 ldb_errstring(ldb)));
775 ldb_transaction_cancel(ldb);
776 return WERR_DS_DRA_INTERNAL_ERROR;
779 ret = ldb_transaction_commit(ldb);
780 if (ret != LDB_SUCCESS) {
781 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
782 ldb_errstring(ldb)));
783 return WERR_DS_DRA_INTERNAL_ERROR;
786 talloc_free(ext_res);
788 DEBUG(2,("Allocated RID pool for server %s\n",
789 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
791 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
793 return WERR_OK;
797 return an array of SIDs from a ldb_message given an attribute name
798 assumes the SIDs are in extended DN format
800 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
801 struct ldb_message *msg,
802 TALLOC_CTX *mem_ctx,
803 const char *attr,
804 const struct dom_sid ***sids)
806 struct ldb_message_element *el;
807 unsigned int i;
809 el = ldb_msg_find_element(msg, attr);
810 if (!el) {
811 *sids = NULL;
812 return WERR_OK;
815 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
816 W_ERROR_HAVE_NO_MEMORY(*sids);
818 for (i=0; i<el->num_values; i++) {
819 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
820 NTSTATUS status;
821 struct dom_sid *sid;
823 sid = talloc(*sids, struct dom_sid);
824 W_ERROR_HAVE_NO_MEMORY(sid);
825 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
826 if (!NT_STATUS_IS_OK(status)) {
827 return WERR_INTERNAL_DB_CORRUPTION;
829 (*sids)[i] = sid;
831 (*sids)[i] = NULL;
833 return WERR_OK;
838 return an array of SIDs from a ldb_message given an attribute name
839 assumes the SIDs are in NDR form
841 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
842 struct ldb_message *msg,
843 TALLOC_CTX *mem_ctx,
844 const char *attr,
845 const struct dom_sid ***sids)
847 struct ldb_message_element *el;
848 unsigned int i;
850 el = ldb_msg_find_element(msg, attr);
851 if (!el) {
852 *sids = NULL;
853 return WERR_OK;
856 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
857 W_ERROR_HAVE_NO_MEMORY(*sids);
859 for (i=0; i<el->num_values; i++) {
860 enum ndr_err_code ndr_err;
861 struct dom_sid *sid;
863 sid = talloc(*sids, struct dom_sid);
864 W_ERROR_HAVE_NO_MEMORY(sid);
866 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
867 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
868 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
869 return WERR_INTERNAL_DB_CORRUPTION;
871 (*sids)[i] = sid;
873 (*sids)[i] = NULL;
875 return WERR_OK;
879 see if any SIDs in list1 are in list2
881 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
883 unsigned int i, j;
884 /* do we ever have enough SIDs here to worry about O(n^2) ? */
885 for (i=0; list1[i]; i++) {
886 for (j=0; list2[j]; j++) {
887 if (dom_sid_equal(list1[i], list2[j])) {
888 return true;
892 return false;
896 handle a DRSUAPI_EXOP_REPL_SECRET call
898 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
899 TALLOC_CTX *mem_ctx,
900 struct drsuapi_DsGetNCChangesRequest10 *req10,
901 struct dom_sid *user_sid,
902 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
903 bool has_get_all_changes)
905 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
906 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
907 int ret;
908 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
909 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
910 struct ldb_result *rodc_res, *obj_res;
911 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
912 WERROR werr;
914 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
915 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
918 * we need to work out if we will allow this DC to
919 * replicate the secrets for this object
921 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
922 * of this function
925 if (b_state->sam_ctx_system == NULL) {
926 /* this operation needs system level access */
927 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
928 return WERR_DS_DRA_SOURCE_DISABLED;
932 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
934 * The pseudo code indicate
935 * revealsecrets = true
936 * if IsRevealSecretRequest(msgIn) then
937 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
938 * then
939 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
940 * <... check if this account is ok to be replicated on this DC ...>
941 * <... and if not reveal secrets = no ...>
942 * else
943 * reveal secrets = false
944 * endif
945 * endif
946 * endif
948 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
949 * then you can do EXOP_REPL_SECRETS
951 if (has_get_all_changes) {
952 goto allowed;
955 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
956 if (!ldb_dn_validate(obj_dn)) goto failed;
958 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
959 dom_sid_string(mem_ctx, user_sid));
960 if (!ldb_dn_validate(rodc_dn)) goto failed;
962 /* do the two searches we need */
963 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
964 DSDB_SEARCH_SHOW_EXTENDED_DN);
965 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
967 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
968 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
970 /* if the object SID is equal to the user_sid, allow */
971 if (dom_sid_equal(user_sid,
972 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
973 goto allowed;
976 /* an RODC is allowed to get its own krbtgt account secrets */
977 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
978 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
979 if (krbtgt_link_dn != NULL &&
980 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
981 goto allowed;
984 /* but it isn't allowed to get anyone elses krbtgt secrets */
985 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
986 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
987 goto denied;
990 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
991 "userAccountControl", 0) &
992 UF_INTERDOMAIN_TRUST_ACCOUNT) {
993 goto denied;
996 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
997 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
998 if (!W_ERROR_IS_OK(werr)) {
999 goto denied;
1002 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1003 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1004 if (!W_ERROR_IS_OK(werr)) {
1005 goto denied;
1008 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1009 mem_ctx, "tokenGroups", &token_sids);
1010 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1011 goto denied;
1014 if (never_reveal_sids &&
1015 sid_list_match(token_sids, never_reveal_sids)) {
1016 goto denied;
1019 if (reveal_sids &&
1020 sid_list_match(token_sids, reveal_sids)) {
1021 goto allowed;
1024 /* default deny */
1025 denied:
1026 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1027 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1028 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1029 return WERR_DS_DRA_ACCESS_DENIED;
1031 allowed:
1032 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1033 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1034 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1035 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1036 req10->highwatermark.highest_usn = 0;
1037 return WERR_OK;
1039 failed:
1040 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1041 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1042 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1043 return WERR_DS_DRA_BAD_DN;
1048 handle a DRSUAPI_EXOP_REPL_OBJ call
1050 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1051 TALLOC_CTX *mem_ctx,
1052 struct drsuapi_DsGetNCChangesRequest10 *req10,
1053 struct dom_sid *user_sid,
1054 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1056 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1058 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1059 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1061 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1062 return WERR_OK;
1067 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1068 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1069 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1071 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1072 TALLOC_CTX *mem_ctx,
1073 struct drsuapi_DsGetNCChangesRequest10 *req10,
1074 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1076 struct ldb_dn *req_dn, *ntds_dn;
1077 int ret;
1078 unsigned int i;
1079 struct ldb_context *ldb = b_state->sam_ctx;
1080 struct ldb_message *msg;
1081 bool is_us;
1084 steps:
1085 - verify that the client dn exists
1086 - verify that we are the current master
1089 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1090 if (!ldb_dn_validate(req_dn)) {
1091 /* that is not a valid dn */
1092 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1093 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1094 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1095 return WERR_OK;
1098 /* retrieve the current role owner */
1099 /* find the DN of the RID Manager */
1100 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1101 if (ret != LDB_SUCCESS) {
1102 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1103 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1104 return WERR_DS_DRA_INTERNAL_ERROR;
1107 if (!is_us) {
1108 /* we're not the RID Manager - go away */
1109 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1110 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1111 return WERR_OK;
1114 /* change the current master */
1115 msg = ldb_msg_new(ldb);
1116 W_ERROR_HAVE_NO_MEMORY(msg);
1117 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1118 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1120 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1121 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1122 if (ret != LDB_SUCCESS) {
1123 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1124 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1125 talloc_free(msg);
1126 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1127 return WERR_OK;
1130 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1131 if (ret != 0) {
1132 talloc_free(msg);
1133 return WERR_DS_DRA_INTERNAL_ERROR;
1136 for (i=0;i<msg->num_elements;i++) {
1137 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1140 ret = ldb_transaction_start(ldb);
1141 if (ret != LDB_SUCCESS) {
1142 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1143 ldb_errstring(ldb)));
1144 return WERR_DS_DRA_INTERNAL_ERROR;
1147 ret = ldb_modify(ldb, msg);
1148 if (ret != LDB_SUCCESS) {
1149 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1150 ldb_errstring(ldb)));
1151 ldb_transaction_cancel(ldb);
1152 return WERR_DS_DRA_INTERNAL_ERROR;
1155 ret = ldb_transaction_commit(ldb);
1156 if (ret != LDB_SUCCESS) {
1157 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1158 ldb_errstring(ldb)));
1159 return WERR_DS_DRA_INTERNAL_ERROR;
1162 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1164 return WERR_OK;
1168 see if this getncchanges request includes a request to reveal secret information
1170 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1171 struct drsuapi_DsGetNCChangesRequest10 *req10,
1172 bool *is_secret_request)
1174 enum drsuapi_DsExtendedOperation exop;
1175 uint32_t i;
1176 struct dsdb_schema *schema;
1178 *is_secret_request = true;
1180 exop = req10->extended_op;
1182 switch (exop) {
1183 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1184 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1185 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1186 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1187 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1188 /* FSMO exops can reveal secrets */
1189 *is_secret_request = true;
1190 return WERR_OK;
1191 case DRSUAPI_EXOP_REPL_SECRET:
1192 case DRSUAPI_EXOP_REPL_OBJ:
1193 case DRSUAPI_EXOP_NONE:
1194 break;
1197 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1198 *is_secret_request = false;
1199 return WERR_OK;
1202 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1203 req10->partial_attribute_set == NULL) {
1204 /* they want secrets */
1205 *is_secret_request = true;
1206 return WERR_OK;
1209 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1211 /* check the attributes they asked for */
1212 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1213 const struct dsdb_attribute *sa;
1214 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1215 if (sa == NULL) {
1216 return WERR_DS_DRA_SCHEMA_MISMATCH;
1218 if (!dsdb_attr_in_rodc_fas(sa)) {
1219 *is_secret_request = true;
1220 return WERR_OK;
1224 if (req10->partial_attribute_set_ex) {
1225 /* check the extended attributes they asked for */
1226 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1227 const struct dsdb_attribute *sa;
1228 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1229 if (sa == NULL) {
1230 return WERR_DS_DRA_SCHEMA_MISMATCH;
1232 if (!dsdb_attr_in_rodc_fas(sa)) {
1233 *is_secret_request = true;
1234 return WERR_OK;
1239 *is_secret_request = false;
1240 return WERR_OK;
1244 see if this getncchanges request is only for attributes in the GC
1245 partial attribute set
1247 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1248 struct drsuapi_DsGetNCChangesRequest10 *req10,
1249 bool *is_gc_pas_request)
1251 enum drsuapi_DsExtendedOperation exop;
1252 uint32_t i;
1253 struct dsdb_schema *schema;
1255 exop = req10->extended_op;
1257 switch (exop) {
1258 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1259 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1260 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1261 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1262 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1263 case DRSUAPI_EXOP_REPL_SECRET:
1264 *is_gc_pas_request = false;
1265 return WERR_OK;
1266 case DRSUAPI_EXOP_REPL_OBJ:
1267 case DRSUAPI_EXOP_NONE:
1268 break;
1271 if (req10->partial_attribute_set == NULL) {
1272 /* they want it all */
1273 *is_gc_pas_request = false;
1274 return WERR_OK;
1277 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1279 /* check the attributes they asked for */
1280 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1281 const struct dsdb_attribute *sa;
1282 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1283 if (sa == NULL) {
1284 return WERR_DS_DRA_SCHEMA_MISMATCH;
1286 if (!sa->isMemberOfPartialAttributeSet) {
1287 *is_gc_pas_request = false;
1288 return WERR_OK;
1292 if (req10->partial_attribute_set_ex) {
1293 /* check the extended attributes they asked for */
1294 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1295 const struct dsdb_attribute *sa;
1296 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1297 if (sa == NULL) {
1298 return WERR_DS_DRA_SCHEMA_MISMATCH;
1300 if (!sa->isMemberOfPartialAttributeSet) {
1301 *is_gc_pas_request = false;
1302 return WERR_OK;
1307 *is_gc_pas_request = true;
1308 return WERR_OK;
1313 map from req8 to req10
1315 static struct drsuapi_DsGetNCChangesRequest10 *
1316 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1317 struct drsuapi_DsGetNCChangesRequest8 *req8)
1319 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1320 struct drsuapi_DsGetNCChangesRequest10);
1321 if (req10 == NULL) {
1322 return NULL;
1325 req10->destination_dsa_guid = req8->destination_dsa_guid;
1326 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1327 req10->naming_context = req8->naming_context;
1328 req10->highwatermark = req8->highwatermark;
1329 req10->uptodateness_vector = req8->uptodateness_vector;
1330 req10->replica_flags = req8->replica_flags;
1331 req10->max_object_count = req8->max_object_count;
1332 req10->max_ndr_size = req8->max_ndr_size;
1333 req10->extended_op = req8->extended_op;
1334 req10->fsmo_info = req8->fsmo_info;
1335 req10->partial_attribute_set = req8->partial_attribute_set;
1336 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1337 req10->mapping_ctr = req8->mapping_ctr;
1339 return req10;
1344 * Collects object for normal replication cycle.
1346 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1347 TALLOC_CTX *mem_ctx,
1348 struct drsuapi_DsGetNCChangesRequest10 *req10,
1349 struct ldb_dn *search_dn,
1350 const char *extra_filter,
1351 struct ldb_result **search_res)
1353 int ret;
1354 char* search_filter;
1355 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1356 //const char *extra_filter;
1357 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1358 const char *attrs[] = { "uSNChanged",
1359 "objectGUID" ,
1360 NULL };
1362 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1363 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1364 scope = LDB_SCOPE_BASE;
1367 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1369 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1371 /* Construct response. */
1372 search_filter = talloc_asprintf(mem_ctx,
1373 "(uSNChanged>=%llu)",
1374 (unsigned long long)(getnc_state->min_usn+1));
1376 if (extra_filter) {
1377 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1380 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1381 search_filter = talloc_asprintf(mem_ctx,
1382 "(&%s(isCriticalSystemObject=TRUE))",
1383 search_filter);
1386 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1387 scope = LDB_SCOPE_BASE;
1390 if (!search_dn) {
1391 search_dn = getnc_state->ncRoot_dn;
1394 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1395 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1396 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1397 search_dn, scope, attrs,
1398 search_filter);
1399 if (ret != LDB_SUCCESS) {
1400 return WERR_DS_DRA_INTERNAL_ERROR;
1403 return WERR_OK;
1407 * Collects object for normal replication cycle.
1409 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1410 TALLOC_CTX *mem_ctx,
1411 struct drsuapi_DsGetNCChangesRequest10 *req10,
1412 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1413 struct ldb_dn *search_dn,
1414 const char *extra_filter,
1415 struct ldb_result **search_res)
1417 /* we have nothing to do in case of ex-op failure */
1418 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1419 return WERR_OK;
1422 /* TODO: implement extended op specific collection
1423 * of objects. Right now we just normal procedure
1424 * for collecting objects */
1425 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1429 drsuapi_DsGetNCChanges
1431 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1433 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1434 struct drsuapi_DsGetNCChanges *r)
1436 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1437 int ret;
1438 uint32_t i;
1439 struct dsdb_schema *schema;
1440 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1441 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1442 NTSTATUS status;
1443 DATA_BLOB session_key;
1444 WERROR werr;
1445 struct dcesrv_handle *h;
1446 struct drsuapi_bind_state *b_state;
1447 struct drsuapi_getncchanges_state *getnc_state;
1448 struct drsuapi_DsGetNCChangesRequest10 *req10;
1449 uint32_t options;
1450 uint32_t max_objects;
1451 uint32_t max_links;
1452 uint32_t link_count = 0;
1453 uint32_t link_total = 0;
1454 uint32_t link_given = 0;
1455 struct ldb_dn *search_dn = NULL;
1456 bool am_rodc, null_scope=false;
1457 enum security_user_level security_level;
1458 struct ldb_context *sam_ctx;
1459 struct dom_sid *user_sid;
1460 bool is_secret_request;
1461 bool is_gc_pas_request;
1462 struct drsuapi_changed_objects *changes;
1463 time_t max_wait;
1464 time_t start = time(NULL);
1465 bool max_wait_reached = false;
1466 bool has_get_all_changes = false;
1468 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1469 b_state = h->data;
1471 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1473 *r->out.level_out = 6;
1474 /* TODO: linked attributes*/
1475 r->out.ctr->ctr6.linked_attributes_count = 0;
1476 r->out.ctr->ctr6.linked_attributes = NULL;
1478 r->out.ctr->ctr6.object_count = 0;
1479 r->out.ctr->ctr6.nc_object_count = 0;
1480 r->out.ctr->ctr6.more_data = false;
1481 r->out.ctr->ctr6.uptodateness_vector = NULL;
1483 /* a RODC doesn't allow for any replication */
1484 ret = samdb_rodc(sam_ctx, &am_rodc);
1485 if (ret == LDB_SUCCESS && am_rodc) {
1486 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1487 return WERR_DS_DRA_SOURCE_DISABLED;
1490 /* Check request revision.
1492 switch (r->in.level) {
1493 case 8:
1494 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1495 if (req10 == NULL) {
1496 return WERR_NOMEM;
1498 break;
1499 case 10:
1500 req10 = &r->in.req->req10;
1501 break;
1502 default:
1503 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1504 r->in.level));
1505 return WERR_REVISION_MISMATCH;
1509 /* Perform access checks. */
1510 /* TODO: we need to support a sync on a specific non-root
1511 * DN. We'll need to find the real partition root here */
1512 ncRoot = req10->naming_context;
1513 if (ncRoot == NULL) {
1514 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1515 return WERR_DS_DRA_INVALID_PARAMETER;
1518 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1519 return WERR_DS_DRA_INTERNAL_ERROR;
1522 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1523 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1524 return WERR_DS_DRA_SOURCE_DISABLED;
1527 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1529 /* all clients must have GUID_DRS_GET_CHANGES */
1530 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1531 mem_ctx,
1532 dce_call->conn->auth_state.session_info->security_token,
1533 req10->naming_context,
1534 GUID_DRS_GET_CHANGES);
1535 if (!W_ERROR_IS_OK(werr)) {
1536 return werr;
1539 /* allowed if the GC PAS and client has
1540 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1541 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1542 if (!W_ERROR_IS_OK(werr)) {
1543 return werr;
1545 if (is_gc_pas_request) {
1546 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1547 mem_ctx,
1548 dce_call->conn->auth_state.session_info->security_token,
1549 req10->naming_context,
1550 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1551 if (W_ERROR_IS_OK(werr)) {
1552 goto allowed;
1556 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1557 if (!W_ERROR_IS_OK(werr)) {
1558 return werr;
1560 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1561 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1562 mem_ctx,
1563 dce_call->conn->auth_state.session_info->security_token,
1564 req10->naming_context,
1565 GUID_DRS_GET_ALL_CHANGES);
1566 if (!W_ERROR_IS_OK(werr)) {
1567 return werr;
1568 } else {
1569 has_get_all_changes = true;
1573 allowed:
1574 /* for non-administrator replications, check that they have
1575 given the correct source_dsa_invocation_id */
1576 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1577 samdb_domain_sid(sam_ctx));
1578 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1579 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1580 /* we rely on this flag being unset for RODC requests */
1581 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1585 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1586 /* Ignore the _in_ uptpdateness vector*/
1587 req10->uptodateness_vector = NULL;
1590 getnc_state = b_state->getncchanges_state;
1592 /* see if a previous replication has been abandoned */
1593 if (getnc_state) {
1594 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1595 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1596 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1597 ldb_dn_get_linearized(new_dn),
1598 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1599 ldb_dn_get_linearized(getnc_state->last_dn)));
1600 talloc_free(getnc_state);
1601 getnc_state = NULL;
1605 if (getnc_state == NULL) {
1606 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1607 if (getnc_state == NULL) {
1608 return WERR_NOMEM;
1610 b_state->getncchanges_state = getnc_state;
1611 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1613 /* find out if we are to replicate Schema NC */
1614 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1615 ldb_get_schema_basedn(b_state->sam_ctx));
1616 getnc_state->is_schema_nc = (0 == ret);
1618 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1619 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1623 * This is the first replication cycle and it is
1624 * a good place to handle extended operations
1626 * FIXME: we don't fully support extended operations yet
1628 switch (req10->extended_op) {
1629 case DRSUAPI_EXOP_NONE:
1630 break;
1631 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1632 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1633 W_ERROR_NOT_OK_RETURN(werr);
1634 search_dn = ldb_get_default_basedn(sam_ctx);
1635 break;
1636 case DRSUAPI_EXOP_REPL_SECRET:
1637 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1638 user_sid,
1639 &r->out.ctr->ctr6,
1640 has_get_all_changes);
1641 r->out.result = werr;
1642 W_ERROR_NOT_OK_RETURN(werr);
1643 break;
1644 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1645 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1646 W_ERROR_NOT_OK_RETURN(werr);
1647 break;
1648 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1649 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1650 W_ERROR_NOT_OK_RETURN(werr);
1651 break;
1652 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1653 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1654 W_ERROR_NOT_OK_RETURN(werr);
1655 break;
1656 case DRSUAPI_EXOP_REPL_OBJ:
1657 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1658 r->out.result = werr;
1659 W_ERROR_NOT_OK_RETURN(werr);
1660 break;
1662 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1664 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1665 (unsigned)req10->extended_op));
1666 return WERR_DS_DRA_NOT_SUPPORTED;
1670 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1671 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1672 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1673 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1674 return WERR_DS_DRA_INVALID_PARAMETER;
1677 /* we need the session key for encrypting password attributes */
1678 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1679 if (!NT_STATUS_IS_OK(status)) {
1680 DEBUG(0,(__location__ ": Failed to get session key\n"));
1681 return WERR_DS_DRA_INTERNAL_ERROR;
1685 TODO: MS-DRSR section 4.1.10.1.1
1686 Work out if this is the start of a new cycle */
1688 if (getnc_state->guids == NULL) {
1689 const char *extra_filter;
1690 struct ldb_result *search_res = NULL;
1692 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1694 getnc_state->min_usn = req10->highwatermark.highest_usn;
1696 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1697 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1698 search_dn, extra_filter,
1699 &search_res);
1700 } else {
1701 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1702 &r->out.ctr->ctr6,
1703 search_dn, extra_filter,
1704 &search_res);
1706 W_ERROR_NOT_OK_RETURN(werr);
1708 /* extract out the GUIDs list */
1709 getnc_state->num_records = search_res ? search_res->count : 0;
1710 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1711 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1713 changes = talloc_array(getnc_state,
1714 struct drsuapi_changed_objects,
1715 getnc_state->num_records);
1716 W_ERROR_HAVE_NO_MEMORY(changes);
1718 for (i=0; i<getnc_state->num_records; i++) {
1719 changes[i].dn = search_res->msgs[i]->dn;
1720 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1721 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1724 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1725 TYPESAFE_QSORT(changes,
1726 getnc_state->num_records,
1727 site_res_cmp_parent_order);
1728 } else {
1729 TYPESAFE_QSORT(changes,
1730 getnc_state->num_records,
1731 site_res_cmp_dn_usn_order);
1734 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req10->uptodateness_vector);
1735 if (getnc_state->uptodateness_vector) {
1736 /* make sure its sorted */
1737 TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1738 getnc_state->uptodateness_vector->count,
1739 drsuapi_DsReplicaCursor_compare);
1742 for (i=0; i < getnc_state->num_records; i++) {
1743 getnc_state->guids[i] = changes[i].guid;
1744 if (GUID_all_zero(&getnc_state->guids[i])) {
1745 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1746 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1747 return WERR_DS_DRA_INTERNAL_ERROR;
1751 talloc_free(search_res);
1752 talloc_free(changes);
1755 /* Prefix mapping */
1756 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1757 if (!schema) {
1758 DEBUG(0,("No schema in sam_ctx\n"));
1759 return WERR_DS_DRA_INTERNAL_ERROR;
1762 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1763 *r->out.ctr->ctr6.naming_context = *ncRoot;
1765 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1766 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1767 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1768 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1769 return WERR_DS_DRA_INTERNAL_ERROR;
1772 /* find the SID if there is one */
1773 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1775 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1776 r->out.ctr->ctr6.mapping_ctr = *ctr;
1778 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1779 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1781 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1782 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1784 r->out.ctr->ctr6.first_object = NULL;
1785 currentObject = &r->out.ctr->ctr6.first_object;
1787 /* use this to force single objects at a time, which is useful
1788 * for working out what object is giving problems
1790 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1791 if (req10->max_object_count < max_objects) {
1792 max_objects = req10->max_object_count;
1795 * TODO: work out how the maximum should be calculated
1797 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1800 * Maximum time that we can spend in a getncchanges
1801 * in order to avoid timeout of the other part.
1802 * 10 seconds by default.
1804 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1805 for (i=getnc_state->num_processed;
1806 i<getnc_state->num_records &&
1807 !null_scope &&
1808 (r->out.ctr->ctr6.object_count < max_objects)
1809 && !max_wait_reached;
1810 i++) {
1811 int uSN;
1812 struct drsuapi_DsReplicaObjectListItemEx *obj;
1813 struct ldb_message *msg;
1814 static const char * const msg_attrs[] = {
1815 "*",
1816 "nTSecurityDescriptor",
1817 "parentGUID",
1818 "replPropertyMetaData",
1819 DSDB_SECRET_ATTRIBUTES,
1820 NULL };
1821 struct ldb_result *msg_res;
1822 struct ldb_dn *msg_dn;
1824 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1825 W_ERROR_HAVE_NO_MEMORY(obj);
1827 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1828 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1831 /* by re-searching here we avoid having a lot of full
1832 * records in memory between calls to getncchanges
1834 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1835 msg_dn,
1836 LDB_SCOPE_BASE, msg_attrs, NULL);
1837 if (ret != LDB_SUCCESS) {
1838 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1839 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1840 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1842 talloc_free(obj);
1843 continue;
1846 msg = msg_res->msgs[0];
1848 max_wait_reached = (time(NULL) - start > max_wait);
1850 werr = get_nc_changes_build_object(obj, msg,
1851 sam_ctx, getnc_state->ncRoot_dn,
1852 getnc_state->is_schema_nc,
1853 schema, &session_key, getnc_state->min_usn,
1854 req10->replica_flags,
1855 req10->partial_attribute_set,
1856 getnc_state->uptodateness_vector,
1857 req10->extended_op,
1858 max_wait_reached);
1859 if (!W_ERROR_IS_OK(werr)) {
1860 return werr;
1863 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1864 getnc_state->ncRoot_dn,
1865 schema, getnc_state->min_usn,
1866 req10->replica_flags,
1867 msg,
1868 &getnc_state->la_list,
1869 &getnc_state->la_count,
1870 getnc_state->uptodateness_vector);
1871 if (!W_ERROR_IS_OK(werr)) {
1872 return werr;
1875 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1876 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1877 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1879 if (uSN > getnc_state->highest_usn) {
1880 getnc_state->highest_usn = uSN;
1883 if (obj->meta_data_ctr == NULL) {
1884 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1885 ldb_dn_get_linearized(msg->dn)));
1886 /* no attributes to send */
1887 talloc_free(obj);
1888 continue;
1891 r->out.ctr->ctr6.object_count++;
1893 *currentObject = obj;
1894 currentObject = &obj->next_object;
1896 talloc_free(getnc_state->last_dn);
1897 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1899 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1901 talloc_free(msg_res);
1902 talloc_free(msg_dn);
1905 getnc_state->num_processed = i;
1907 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1909 /* the client can us to call UpdateRefs on its behalf to
1910 re-establish monitoring of the NC */
1911 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1912 !GUID_all_zero(&req10->destination_dsa_guid)) {
1913 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1914 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1915 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1916 ureq.naming_context = ncRoot;
1917 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
1918 &req10->destination_dsa_guid);
1919 if (!ureq.dest_dsa_dns_name) {
1920 return WERR_NOMEM;
1922 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1923 ureq.options = DRSUAPI_DRS_ADD_REF |
1924 DRSUAPI_DRS_ASYNC_OP |
1925 DRSUAPI_DRS_GETCHG_CHECK;
1927 /* we also need to pass through the
1928 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1929 to send notifies using the GC SPN */
1930 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1932 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1933 if (!W_ERROR_IS_OK(werr)) {
1934 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1935 win_errstr(werr)));
1940 * TODO:
1941 * This is just a guess, how to calculate the
1942 * number of linked attributes to send, we need to
1943 * find out how to do this right.
1945 if (r->out.ctr->ctr6.object_count >= max_links) {
1946 max_links = 0;
1947 } else {
1948 max_links -= r->out.ctr->ctr6.object_count;
1951 link_total = getnc_state->la_count;
1953 if (i < getnc_state->num_records) {
1954 r->out.ctr->ctr6.more_data = true;
1955 } else {
1956 /* sort the whole array the first time */
1957 if (!getnc_state->la_sorted) {
1958 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1959 sam_ctx, linked_attribute_compare);
1960 getnc_state->la_sorted = true;
1963 link_count = getnc_state->la_count - getnc_state->la_idx;
1964 link_count = MIN(max_links, link_count);
1966 r->out.ctr->ctr6.linked_attributes_count = link_count;
1967 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1969 getnc_state->la_idx += link_count;
1970 link_given = getnc_state->la_idx;
1972 if (getnc_state->la_idx < getnc_state->la_count) {
1973 r->out.ctr->ctr6.more_data = true;
1977 if (!r->out.ctr->ctr6.more_data) {
1978 talloc_steal(mem_ctx, getnc_state->la_list);
1980 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1981 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1983 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1984 r->out.ctr->ctr6.uptodateness_vector);
1985 if (!W_ERROR_IS_OK(werr)) {
1986 return werr;
1989 talloc_free(getnc_state);
1990 b_state->getncchanges_state = NULL;
1993 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1994 r->out.ctr->ctr6.uptodateness_vector = NULL;
1995 r->out.ctr->ctr6.nc_object_count = 0;
1996 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1999 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2000 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2001 (unsigned long long)(req10->highwatermark.highest_usn+1),
2002 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2003 r->out.ctr->ctr6.object_count,
2004 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2005 r->out.ctr->ctr6.linked_attributes_count,
2006 link_given, link_total,
2007 dom_sid_string(mem_ctx, user_sid)));
2009 #if 0
2010 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2011 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2013 #endif
2015 return WERR_OK;