s4-drs: check if we have a domain level >= 2k8r2 as before the isRecycled do not...
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / getncchanges.c
blob4217e223f9c0593c5891b00d1486e500d4552e4d
1 /*
2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "libcli/security/security.h"
31 #include "libcli/security/session.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33 #include "rpc_server/dcerpc_server_proto.h"
34 #include "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
41 build a DsReplicaObjectIdentifier from a ldb msg
43 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
44 struct ldb_message *msg)
46 struct drsuapi_DsReplicaObjectIdentifier *identifier;
47 struct dom_sid *sid;
49 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
50 if (identifier == NULL) {
51 return NULL;
54 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
55 identifier->guid = samdb_result_guid(msg, "objectGUID");
57 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
58 if (sid) {
59 identifier->sid = *sid;
60 } else {
61 ZERO_STRUCT(identifier->sid);
63 return identifier;
66 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
68 return GUID_compare(guid1, &guid2);
72 see if we can filter an attribute using the uptodateness_vector
74 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
75 const struct GUID *originating_invocation_id,
76 uint64_t originating_usn)
78 const struct drsuapi_DsReplicaCursor *c;
79 if (udv == NULL) return false;
80 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
81 originating_invocation_id, udv_compare, c);
82 if (c && originating_usn <= c->highest_usn) {
83 return true;
85 return false;
89 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
91 if (a1 == a2) return 0;
92 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
96 check if an attribute is in a partial_attribute_set
98 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
99 struct drsuapi_DsPartialAttributeSet *pas)
101 enum drsuapi_DsAttributeId *result;
102 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
103 attid_cmp, result);
104 return result != NULL;
109 drsuapi_DsGetNCChanges for one object
111 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
112 struct ldb_message *msg,
113 struct ldb_context *sam_ctx,
114 struct ldb_dn *ncRoot_dn,
115 bool is_schema_nc,
116 struct dsdb_schema *schema,
117 DATA_BLOB *session_key,
118 uint64_t highest_usn,
119 uint32_t replica_flags,
120 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
121 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
122 enum drsuapi_DsExtendedOperation extended_op)
124 const struct ldb_val *md_value;
125 uint32_t i, n;
126 struct replPropertyMetaDataBlob md;
127 uint32_t rid = 0;
128 enum ndr_err_code ndr_err;
129 uint32_t *attids;
130 const char *rdn;
131 const struct dsdb_attribute *rdn_sa;
132 unsigned int instanceType;
133 struct dsdb_syntax_ctx syntax_ctx;
135 /* make dsdb sytanx context for conversions */
136 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
137 syntax_ctx.is_schema_nc = is_schema_nc;
139 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
140 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
141 obj->is_nc_prefix = true;
142 obj->parent_object_guid = NULL;
143 } else {
144 obj->is_nc_prefix = false;
145 obj->parent_object_guid = talloc(obj, struct GUID);
146 if (obj->parent_object_guid == NULL) {
147 return WERR_DS_DRA_INTERNAL_ERROR;
149 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
150 if (GUID_all_zero(obj->parent_object_guid)) {
151 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
152 ldb_dn_get_linearized(msg->dn)));
153 return WERR_DS_DRA_INTERNAL_ERROR;
156 obj->next_object = NULL;
158 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
159 if (!md_value) {
160 /* nothing to send */
161 return WERR_OK;
164 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
165 /* don't send uninstantiated objects */
166 return WERR_OK;
169 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
170 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
171 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
172 return WERR_DS_DRA_INTERNAL_ERROR;
175 if (md.version != 1) {
176 return WERR_DS_DRA_INTERNAL_ERROR;
179 rdn = ldb_dn_get_rdn_name(msg->dn);
180 if (rdn == NULL) {
181 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
182 return WERR_DS_DRA_INTERNAL_ERROR;
185 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
186 if (rdn_sa == NULL) {
187 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
188 rdn, ldb_dn_get_linearized(msg->dn)));
189 return WERR_DS_DRA_INTERNAL_ERROR;
192 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
193 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
195 obj->object.identifier = get_object_identifier(obj, msg);
196 if (obj->object.identifier == NULL) {
197 return WERR_NOMEM;
199 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
201 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
202 for (n=i=0; i<md.ctr.ctr1.count; i++) {
203 const struct dsdb_attribute *sa;
204 bool force_attribute = false;
206 /* if the attribute has not changed, and it is not the
207 instanceType then don't include it */
208 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
209 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
210 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
212 /* don't include the rDN */
213 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
215 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
216 if (!sa) {
217 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
218 (unsigned int)md.ctr.ctr1.array[i].attid,
219 ldb_dn_get_linearized(msg->dn)));
220 return WERR_DS_DRA_INTERNAL_ERROR;
223 if (sa->linkID) {
224 struct ldb_message_element *el;
225 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
226 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
227 /* don't send upgraded links inline */
228 continue;
232 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
233 !dsdb_attr_in_rodc_fas(sa)) {
234 force_attribute = true;
235 DEBUG(4,("Forcing attribute %s in %s\n",
236 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
239 /* filter by uptodateness_vector */
240 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
241 !force_attribute &&
242 udv_filter(uptodateness_vector,
243 &md.ctr.ctr1.array[i].originating_invocation_id,
244 md.ctr.ctr1.array[i].originating_usn)) {
245 continue;
248 /* filter by partial_attribute_set */
249 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
250 continue;
253 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
254 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
255 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
256 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
257 attids[n] = md.ctr.ctr1.array[i].attid;
258 n++;
261 /* ignore it if its an empty change. Note that renames always
262 * change the 'name' attribute, so they won't be ignored by
263 * this */
264 if (n == 0 ||
265 (n == 1 && attids[0] == DRSUAPI_ATTID_instanceType)) {
266 talloc_free(obj->meta_data_ctr);
267 obj->meta_data_ctr = NULL;
268 return WERR_OK;
271 obj->meta_data_ctr->count = n;
273 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
274 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
275 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
276 obj->object.attribute_ctr.num_attributes);
279 * Note that the meta_data array and the attributes array must
280 * be the same size and in the same order
282 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
283 struct ldb_message_element *el;
284 WERROR werr;
285 const struct dsdb_attribute *sa;
287 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
288 if (!sa) {
289 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
290 return WERR_DS_DRA_INTERNAL_ERROR;
293 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
294 if (el == NULL) {
295 /* this happens for attributes that have been removed */
296 DEBUG(5,("No element '%s' for attributeID %u in message\n",
297 sa->lDAPDisplayName, attids[i]));
298 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
299 obj->object.attribute_ctr.attributes[i].attid =
300 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
301 } else {
302 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
303 &obj->object.attribute_ctr.attributes[i]);
304 if (!W_ERROR_IS_OK(werr)) {
305 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
306 sa->lDAPDisplayName, win_errstr(werr)));
307 return werr;
309 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
310 * check if attribute is secret and send a null value
312 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
313 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
314 &obj->meta_data_ctr->meta_data[i]);
316 /* some attributes needs to be encrypted
317 before being sent */
318 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
319 &obj->object.attribute_ctr.attributes[i]);
320 if (!W_ERROR_IS_OK(werr)) {
321 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
322 sa->lDAPDisplayName, win_errstr(werr)));
323 return werr;
328 return WERR_OK;
333 add one linked attribute from an object to the list of linked
334 attributes in a getncchanges request
336 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
337 struct ldb_context *sam_ctx,
338 const struct dsdb_schema *schema,
339 const struct dsdb_attribute *sa,
340 struct ldb_message *msg,
341 struct dsdb_dn *dsdb_dn,
342 struct drsuapi_DsReplicaLinkedAttribute **la_list,
343 uint32_t *la_count)
345 struct drsuapi_DsReplicaLinkedAttribute *la;
346 bool active;
347 NTSTATUS status;
348 WERROR werr;
350 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
351 W_ERROR_HAVE_NO_MEMORY(*la_list);
353 la = &(*la_list)[*la_count];
355 la->identifier = get_object_identifier(*la_list, msg);
356 W_ERROR_HAVE_NO_MEMORY(la->identifier);
358 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
360 if (!active) {
361 /* We have to check that the inactive link still point to an existing object */
362 struct GUID guid;
363 struct ldb_dn *tdn;
364 int ret;
365 const char *v;
367 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "false");
368 if (strncasecmp(v, "true", 4) == 0) {
370 * Note: we skip the transmition of the deleted link even if the other part used to
371 * know about it because when we transmit the deletion of the object, the link will
372 * be deleted too due to deletion of object where link points and Windows do so.
374 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
375 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "true");
377 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
378 * if it join an existing domain with deleted objets, it firsts impose to have a
379 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
380 * either during initial replication or after the getNCChanges.
381 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
383 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
384 * If FL >=2K8R2 we are sure that this attribute will be here.
385 * For this kind of forest level we do not return the link if the object is recycled
386 * (isRecycled = true).
388 if (strncasecmp(v, "true", 4) == 0) {
389 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
390 ldb_dn_get_linearized(msg->dn)));
391 return WERR_OK;
393 } else {
394 return WERR_OK;
397 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
398 if (!NT_STATUS_IS_OK(status)) {
399 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
400 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
401 return ntstatus_to_werror(status);
403 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
404 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
405 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
406 GUID_string(mem_ctx, &guid)));
407 return WERR_OK;
408 } else if (ret != LDB_SUCCESS) {
409 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
410 GUID_string(mem_ctx, &guid),
411 ret));
412 return WERR_OK;
415 la->attid = sa->attributeID_id;
416 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
418 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
419 if (!NT_STATUS_IS_OK(status)) {
420 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
421 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
422 return ntstatus_to_werror(status);
424 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
425 if (!NT_STATUS_IS_OK(status)) {
426 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
427 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
428 return ntstatus_to_werror(status);
430 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
431 if (!NT_STATUS_IS_OK(status)) {
432 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
433 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
434 return ntstatus_to_werror(status);
436 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
437 if (!NT_STATUS_IS_OK(status)) {
438 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
439 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
440 return ntstatus_to_werror(status);
443 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
444 if (!NT_STATUS_IS_OK(status)) {
445 /* this is possible for upgraded links */
446 la->originating_add_time = la->meta_data.originating_change_time;
449 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
450 W_ERROR_NOT_OK_RETURN(werr);
452 (*la_count)++;
453 return WERR_OK;
458 add linked attributes from an object to the list of linked
459 attributes in a getncchanges request
461 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
462 TALLOC_CTX *mem_ctx,
463 struct ldb_dn *ncRoot_dn,
464 struct dsdb_schema *schema,
465 uint64_t highest_usn,
466 uint32_t replica_flags,
467 struct ldb_message *msg,
468 struct drsuapi_DsReplicaLinkedAttribute **la_list,
469 uint32_t *la_count,
470 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
472 unsigned int i;
473 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
474 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
476 for (i=0; i<msg->num_elements; i++) {
477 struct ldb_message_element *el = &msg->elements[i];
478 const struct dsdb_attribute *sa;
479 unsigned int j;
481 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
483 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
484 /* we only want forward links */
485 continue;
488 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
489 /* its an old style link, it will have been
490 * sent in the main replication data */
491 continue;
494 for (j=0; j<el->num_values; j++) {
495 struct dsdb_dn *dsdb_dn;
496 uint64_t local_usn;
497 NTSTATUS status;
498 WERROR werr;
500 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
501 if (dsdb_dn == NULL) {
502 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
503 el->name, ldb_dn_get_linearized(msg->dn)));
504 talloc_free(tmp_ctx);
505 return WERR_DS_DRA_INTERNAL_ERROR;
508 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
509 if (!NT_STATUS_IS_OK(status)) {
510 /* this can happen for attributes
511 given to us with old style meta
512 data */
513 continue;
516 if (local_usn > uSNChanged) {
517 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
518 el->name, ldb_dn_get_linearized(msg->dn)));
519 talloc_free(tmp_ctx);
520 return WERR_DS_DRA_INTERNAL_ERROR;
523 if (local_usn < highest_usn) {
524 continue;
527 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
528 dsdb_dn, la_list, la_count);
529 if (!W_ERROR_IS_OK(werr)) {
530 talloc_free(tmp_ctx);
531 return werr;
536 talloc_free(tmp_ctx);
537 return WERR_OK;
541 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
543 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
544 struct ldb_dn *ncRoot_dn,
545 struct drsuapi_DsReplicaCursor2CtrEx *udv)
547 int ret;
549 udv->version = 2;
550 udv->reserved1 = 0;
551 udv->reserved2 = 0;
553 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
554 if (ret != LDB_SUCCESS) {
555 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
556 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
557 return WERR_DS_DRA_INTERNAL_ERROR;
560 return WERR_OK;
564 /* comparison function for linked attributes - see CompareLinks() in
565 * MS-DRSR section 4.1.10.5.17 */
566 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
567 const struct drsuapi_DsReplicaLinkedAttribute *la2,
568 struct ldb_context *sam_ctx)
570 int c;
571 WERROR werr;
572 TALLOC_CTX *tmp_ctx;
573 const struct dsdb_schema *schema;
574 const struct dsdb_attribute *schema_attrib;
575 struct dsdb_dn *dn1, *dn2;
576 struct GUID guid1, guid2;
577 NTSTATUS status;
579 c = GUID_compare(&la1->identifier->guid,
580 &la2->identifier->guid);
581 if (c != 0) return c;
583 if (la1->attid != la2->attid) {
584 return la1->attid < la2->attid? -1:1;
587 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
588 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
589 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
592 /* we need to get the target GUIDs to compare */
593 tmp_ctx = talloc_new(sam_ctx);
595 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
596 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
598 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
599 if (!W_ERROR_IS_OK(werr)) {
600 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
601 talloc_free(tmp_ctx);
602 return 0;
605 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
606 if (!W_ERROR_IS_OK(werr)) {
607 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
608 talloc_free(tmp_ctx);
609 return 0;
612 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
613 if (!NT_STATUS_IS_OK(status)) {
614 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
615 talloc_free(tmp_ctx);
616 return 0;
618 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
619 if (!NT_STATUS_IS_OK(status)) {
620 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
621 talloc_free(tmp_ctx);
622 return 0;
625 talloc_free(tmp_ctx);
627 return GUID_compare(&guid1, &guid2);
632 sort the objects we send by tree order
634 static int site_res_cmp_parent_order(struct ldb_message **m1, struct ldb_message **m2)
636 return ldb_dn_compare((*m2)->dn, (*m1)->dn);
640 sort the objects we send first by uSNChanged
642 static int site_res_cmp_usn_order(struct ldb_message **m1, struct ldb_message **m2)
644 unsigned usnchanged1, usnchanged2;
645 unsigned cn1, cn2;
646 cn1 = ldb_dn_get_comp_num((*m1)->dn);
647 cn2 = ldb_dn_get_comp_num((*m2)->dn);
648 if (cn1 != cn2) {
649 return cn1 > cn2 ? 1 : -1;
651 usnchanged1 = ldb_msg_find_attr_as_uint(*m1, "uSNChanged", 0);
652 usnchanged2 = ldb_msg_find_attr_as_uint(*m2, "uSNChanged", 0);
653 if (usnchanged1 == usnchanged2) {
654 return 0;
656 return usnchanged1 > usnchanged2 ? 1 : -1;
661 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
663 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
664 TALLOC_CTX *mem_ctx,
665 struct drsuapi_DsGetNCChangesRequest10 *req10,
666 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
668 struct ldb_dn *rid_manager_dn, *fsmo_role_dn, *req_dn;
669 int ret;
670 struct ldb_context *ldb = b_state->sam_ctx;
671 struct ldb_result *ext_res;
672 struct ldb_dn *base_dn;
673 struct dsdb_fsmo_extended_op *exop;
676 steps:
677 - verify that the DN being asked for is the RID Manager DN
678 - verify that we are the RID Manager
681 /* work out who is the RID Manager */
682 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
683 if (ret != LDB_SUCCESS) {
684 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
685 return WERR_DS_DRA_INTERNAL_ERROR;
688 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
689 if (!ldb_dn_validate(req_dn) ||
690 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
691 /* that isn't the RID Manager DN */
692 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
693 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
694 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
695 return WERR_OK;
698 /* find the DN of the RID Manager */
699 ret = samdb_reference_dn(ldb, mem_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
700 if (ret != LDB_SUCCESS) {
701 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s\n",
702 ldb_errstring(ldb)));
703 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
704 return WERR_DS_DRA_INTERNAL_ERROR;
707 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
708 /* we're not the RID Manager - go away */
709 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
710 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
711 return WERR_OK;
714 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
715 W_ERROR_HAVE_NO_MEMORY(exop);
717 exop->fsmo_info = req10->fsmo_info;
718 exop->destination_dsa_guid = req10->destination_dsa_guid;
720 ret = ldb_transaction_start(ldb);
721 if (ret != LDB_SUCCESS) {
722 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
723 ldb_errstring(ldb)));
724 return WERR_DS_DRA_INTERNAL_ERROR;
728 * FIXME (kim): this is a temp hack to return just few object,
729 * but not the whole domain NC.
730 * We should remove this hack and implement a 'scope'
731 * building function to return just the set of object
732 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
734 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
736 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
737 if (ret != LDB_SUCCESS) {
738 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
739 ldb_errstring(ldb)));
740 ldb_transaction_cancel(ldb);
741 return WERR_DS_DRA_INTERNAL_ERROR;
744 ret = ldb_transaction_commit(ldb);
745 if (ret != LDB_SUCCESS) {
746 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
747 ldb_errstring(ldb)));
748 return WERR_DS_DRA_INTERNAL_ERROR;
751 talloc_free(ext_res);
753 base_dn = ldb_get_default_basedn(ldb);
755 DEBUG(2,("Allocated RID pool for server %s\n",
756 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
758 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
760 return WERR_OK;
764 return an array of SIDs from a ldb_message given an attribute name
765 assumes the SIDs are in extended DN format
767 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
768 struct ldb_message *msg,
769 TALLOC_CTX *mem_ctx,
770 const char *attr,
771 const struct dom_sid ***sids)
773 struct ldb_message_element *el;
774 unsigned int i;
776 el = ldb_msg_find_element(msg, attr);
777 if (!el) {
778 *sids = NULL;
779 return WERR_OK;
782 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
783 W_ERROR_HAVE_NO_MEMORY(*sids);
785 for (i=0; i<el->num_values; i++) {
786 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
787 NTSTATUS status;
788 struct dom_sid *sid;
790 sid = talloc(*sids, struct dom_sid);
791 W_ERROR_HAVE_NO_MEMORY(sid);
792 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
793 if (!NT_STATUS_IS_OK(status)) {
794 return WERR_INTERNAL_DB_CORRUPTION;
796 (*sids)[i] = sid;
798 (*sids)[i] = NULL;
800 return WERR_OK;
805 return an array of SIDs from a ldb_message given an attribute name
806 assumes the SIDs are in NDR form
808 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
809 struct ldb_message *msg,
810 TALLOC_CTX *mem_ctx,
811 const char *attr,
812 const struct dom_sid ***sids)
814 struct ldb_message_element *el;
815 unsigned int i;
817 el = ldb_msg_find_element(msg, attr);
818 if (!el) {
819 *sids = NULL;
820 return WERR_OK;
823 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
824 W_ERROR_HAVE_NO_MEMORY(*sids);
826 for (i=0; i<el->num_values; i++) {
827 enum ndr_err_code ndr_err;
828 struct dom_sid *sid;
830 sid = talloc(*sids, struct dom_sid);
831 W_ERROR_HAVE_NO_MEMORY(sid);
833 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
834 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
835 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
836 return WERR_INTERNAL_DB_CORRUPTION;
838 (*sids)[i] = sid;
840 (*sids)[i] = NULL;
842 return WERR_OK;
846 see if any SIDs in list1 are in list2
848 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
850 unsigned int i, j;
851 /* do we ever have enough SIDs here to worry about O(n^2) ? */
852 for (i=0; list1[i]; i++) {
853 for (j=0; list2[j]; j++) {
854 if (dom_sid_equal(list1[i], list2[j])) {
855 return true;
859 return false;
863 handle a DRSUAPI_EXOP_REPL_SECRET call
865 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
866 TALLOC_CTX *mem_ctx,
867 struct drsuapi_DsGetNCChangesRequest10 *req10,
868 struct dom_sid *user_sid,
869 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
871 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
872 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
873 int ret;
874 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
875 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
876 struct ldb_result *rodc_res, *obj_res;
877 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
878 WERROR werr;
880 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
881 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
884 * we need to work out if we will allow this RODC to
885 * replicate the secrets for this object
887 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
888 * of this function
891 if (b_state->sam_ctx_system == NULL) {
892 /* this operation needs system level access */
893 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
894 return WERR_DS_DRA_SOURCE_DISABLED;
897 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
898 if (!ldb_dn_validate(obj_dn)) goto failed;
900 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
901 dom_sid_string(mem_ctx, user_sid));
902 if (!ldb_dn_validate(rodc_dn)) goto failed;
904 /* do the two searches we need */
905 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
906 DSDB_SEARCH_SHOW_EXTENDED_DN);
907 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
909 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
910 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
912 /* if the object SID is equal to the user_sid, allow */
913 if (dom_sid_equal(user_sid,
914 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
915 goto allowed;
918 /* an RODC is allowed to get its own krbtgt account secrets */
919 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
920 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
921 if (krbtgt_link_dn != NULL &&
922 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
923 goto allowed;
926 /* but it isn't allowed to get anyone elses krbtgt secrets */
927 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
928 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
929 goto denied;
932 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
933 "userAccountControl", 0) &
934 UF_INTERDOMAIN_TRUST_ACCOUNT) {
935 goto denied;
938 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
939 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
940 if (!W_ERROR_IS_OK(werr)) {
941 goto denied;
944 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
945 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
946 if (!W_ERROR_IS_OK(werr)) {
947 goto denied;
950 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
951 mem_ctx, "tokenGroups", &token_sids);
952 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
953 goto denied;
956 if (never_reveal_sids &&
957 sid_list_match(token_sids, never_reveal_sids)) {
958 goto denied;
961 if (reveal_sids &&
962 sid_list_match(token_sids, reveal_sids)) {
963 goto allowed;
966 /* default deny */
967 denied:
968 DEBUG(2,(__location__ ": Denied RODC secret replication for %s by RODC %s\n",
969 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
970 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
971 return WERR_DS_DRA_ACCESS_DENIED;
973 allowed:
974 DEBUG(2,(__location__ ": Allowed RODC secret replication for %s by RODC %s\n",
975 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
976 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
977 req10->highwatermark.highest_usn = 0;
978 return WERR_OK;
980 failed:
981 DEBUG(2,(__location__ ": Failed RODC secret replication for %s by RODC %s\n",
982 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
983 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
984 return WERR_DS_DRA_BAD_DN;
989 handle a DRSUAPI_EXOP_REPL_OBJ call
991 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
992 TALLOC_CTX *mem_ctx,
993 struct drsuapi_DsGetNCChangesRequest10 *req10,
994 struct dom_sid *user_sid,
995 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
997 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
999 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1000 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1002 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1003 req10->highwatermark.highest_usn = 0;
1004 return WERR_OK;
1009 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1010 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1011 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1013 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1014 TALLOC_CTX *mem_ctx,
1015 struct drsuapi_DsGetNCChangesRequest10 *req10,
1016 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1018 struct ldb_dn *fsmo_role_dn, *req_dn, *ntds_dn;
1019 int ret;
1020 unsigned int i;
1021 struct ldb_context *ldb = b_state->sam_ctx;
1022 struct ldb_message *msg;
1025 steps:
1026 - verify that the client dn exists
1027 - verify that we are the current master
1030 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1031 if (!ldb_dn_validate(req_dn)) {
1032 /* that is not a valid dn */
1033 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1034 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1035 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1036 return WERR_OK;
1039 /* retrieve the current role owner */
1040 ret = samdb_reference_dn(ldb, mem_ctx, req_dn, "fSMORoleOwner", &fsmo_role_dn);
1041 if (ret != LDB_SUCCESS) {
1042 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in context - %s\n",
1043 ldb_errstring(ldb)));
1044 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1045 return WERR_DS_DRA_INTERNAL_ERROR;
1048 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
1049 /* we're not the current owner - go away */
1050 DEBUG(0,(__location__ ": FSMO transfer request when not owner\n"));
1051 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1052 return WERR_OK;
1055 /* change the current master */
1056 msg = ldb_msg_new(ldb);
1057 W_ERROR_HAVE_NO_MEMORY(msg);
1058 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1059 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1061 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1062 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1063 if (ret != LDB_SUCCESS) {
1064 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1065 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1066 talloc_free(msg);
1067 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1068 return WERR_OK;
1071 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1072 if (ret != 0) {
1073 talloc_free(msg);
1074 return WERR_DS_DRA_INTERNAL_ERROR;
1077 for (i=0;i<msg->num_elements;i++) {
1078 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1081 ret = ldb_transaction_start(ldb);
1082 if (ret != LDB_SUCCESS) {
1083 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1084 ldb_errstring(ldb)));
1085 return WERR_DS_DRA_INTERNAL_ERROR;
1088 ret = ldb_modify(ldb, msg);
1089 if (ret != LDB_SUCCESS) {
1090 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1091 ldb_errstring(ldb)));
1092 ldb_transaction_cancel(ldb);
1093 return WERR_DS_DRA_INTERNAL_ERROR;
1096 ret = ldb_transaction_commit(ldb);
1097 if (ret != LDB_SUCCESS) {
1098 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1099 ldb_errstring(ldb)));
1100 return WERR_DS_DRA_INTERNAL_ERROR;
1103 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1105 return WERR_OK;
1108 /* state of a partially completed getncchanges call */
1109 struct drsuapi_getncchanges_state {
1110 struct GUID *guids;
1111 uint32_t num_records;
1112 uint32_t num_processed;
1113 struct ldb_dn *ncRoot_dn;
1114 bool is_schema_nc;
1115 uint64_t min_usn;
1116 uint64_t highest_usn;
1117 struct ldb_dn *last_dn;
1118 struct drsuapi_DsReplicaLinkedAttribute *la_list;
1119 uint32_t la_count;
1120 bool la_sorted;
1121 uint32_t la_idx;
1122 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
1126 see if this getncchanges request includes a request to reveal secret information
1128 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1129 struct drsuapi_DsGetNCChangesRequest10 *req10,
1130 bool *is_secret_request)
1132 enum drsuapi_DsExtendedOperation exop;
1133 uint32_t i;
1134 struct dsdb_schema *schema;
1136 *is_secret_request = true;
1138 exop = req10->extended_op;
1140 switch (exop) {
1141 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1142 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1143 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1144 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1145 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1146 /* FSMO exops can reveal secrets */
1147 *is_secret_request = true;
1148 return WERR_OK;
1149 case DRSUAPI_EXOP_REPL_SECRET:
1150 case DRSUAPI_EXOP_REPL_OBJ:
1151 case DRSUAPI_EXOP_NONE:
1152 break;
1155 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1156 *is_secret_request = false;
1157 return WERR_OK;
1160 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1161 req10->partial_attribute_set == NULL) {
1162 /* they want secrets */
1163 *is_secret_request = true;
1164 return WERR_OK;
1167 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1169 /* check the attributes they asked for */
1170 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1171 const struct dsdb_attribute *sa;
1172 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1173 if (sa == NULL) {
1174 return WERR_DS_DRA_SCHEMA_MISMATCH;
1176 if (!dsdb_attr_in_rodc_fas(sa)) {
1177 *is_secret_request = true;
1178 return WERR_OK;
1182 if (req10->partial_attribute_set_ex) {
1183 /* check the extended attributes they asked for */
1184 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1185 const struct dsdb_attribute *sa;
1186 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1187 if (sa == NULL) {
1188 return WERR_DS_DRA_SCHEMA_MISMATCH;
1190 if (!dsdb_attr_in_rodc_fas(sa)) {
1191 *is_secret_request = true;
1192 return WERR_OK;
1197 *is_secret_request = false;
1198 return WERR_OK;
1202 see if this getncchanges request is only for attributes in the GC
1203 partial attribute set
1205 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1206 struct drsuapi_DsGetNCChangesRequest10 *req10,
1207 bool *is_gc_pas_request)
1209 enum drsuapi_DsExtendedOperation exop;
1210 uint32_t i;
1211 struct dsdb_schema *schema;
1213 exop = req10->extended_op;
1215 switch (exop) {
1216 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1217 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1218 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1219 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1220 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1221 case DRSUAPI_EXOP_REPL_SECRET:
1222 *is_gc_pas_request = false;
1223 return WERR_OK;
1224 case DRSUAPI_EXOP_REPL_OBJ:
1225 case DRSUAPI_EXOP_NONE:
1226 break;
1229 if (req10->partial_attribute_set == NULL) {
1230 /* they want it all */
1231 *is_gc_pas_request = false;
1232 return WERR_OK;
1235 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1237 /* check the attributes they asked for */
1238 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1239 const struct dsdb_attribute *sa;
1240 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1241 if (sa == NULL) {
1242 return WERR_DS_DRA_SCHEMA_MISMATCH;
1244 if (!sa->isMemberOfPartialAttributeSet) {
1245 *is_gc_pas_request = false;
1246 return WERR_OK;
1250 if (req10->partial_attribute_set_ex) {
1251 /* check the extended attributes they asked for */
1252 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1253 const struct dsdb_attribute *sa;
1254 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1255 if (sa == NULL) {
1256 return WERR_DS_DRA_SCHEMA_MISMATCH;
1258 if (!sa->isMemberOfPartialAttributeSet) {
1259 *is_gc_pas_request = false;
1260 return WERR_OK;
1265 *is_gc_pas_request = true;
1266 return WERR_OK;
1271 map from req8 to req10
1273 static struct drsuapi_DsGetNCChangesRequest10 *
1274 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1275 struct drsuapi_DsGetNCChangesRequest8 *req8)
1277 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1278 struct drsuapi_DsGetNCChangesRequest10);
1279 if (req10 == NULL) {
1280 return NULL;
1283 req10->destination_dsa_guid = req8->destination_dsa_guid;
1284 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1285 req10->naming_context = req8->naming_context;
1286 req10->highwatermark = req8->highwatermark;
1287 req10->uptodateness_vector = req8->uptodateness_vector;
1288 req10->replica_flags = req8->replica_flags;
1289 req10->max_object_count = req8->max_object_count;
1290 req10->max_ndr_size = req8->max_ndr_size;
1291 req10->extended_op = req8->extended_op;
1292 req10->fsmo_info = req8->fsmo_info;
1293 req10->partial_attribute_set = req8->partial_attribute_set;
1294 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1295 req10->mapping_ctr = req8->mapping_ctr;
1297 return req10;
1302 * Collects object for normal replication cycle.
1304 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1305 TALLOC_CTX *mem_ctx,
1306 struct drsuapi_DsGetNCChangesRequest10 *req10,
1307 struct ldb_dn *search_dn,
1308 const char *extra_filter,
1309 struct ldb_result **search_res)
1311 int ret;
1312 char* search_filter;
1313 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1314 //const char *extra_filter;
1315 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1316 const char *attrs[] = { "uSNChanged",
1317 "objectGUID" ,
1318 NULL };
1320 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1321 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1322 scope = LDB_SCOPE_BASE;
1325 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1327 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1329 /* Construct response. */
1330 search_filter = talloc_asprintf(mem_ctx,
1331 "(uSNChanged>=%llu)",
1332 (unsigned long long)(getnc_state->min_usn+1));
1334 if (extra_filter) {
1335 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1338 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1339 search_filter = talloc_asprintf(mem_ctx,
1340 "(&%s(isCriticalSystemObject=TRUE))",
1341 search_filter);
1344 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1345 scope = LDB_SCOPE_BASE;
1348 if (!search_dn) {
1349 search_dn = getnc_state->ncRoot_dn;
1352 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1353 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1354 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1355 search_dn, scope, attrs,
1356 search_filter);
1357 if (ret != LDB_SUCCESS) {
1358 return WERR_DS_DRA_INTERNAL_ERROR;
1361 return WERR_OK;
1365 * Collects object for normal replication cycle.
1367 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1368 TALLOC_CTX *mem_ctx,
1369 struct drsuapi_DsGetNCChangesRequest10 *req10,
1370 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1371 struct ldb_dn *search_dn,
1372 const char *extra_filter,
1373 struct ldb_result **search_res)
1375 /* we have nothing to do in case of ex-op failure */
1376 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1377 return WERR_OK;
1380 /* TODO: implement extended op specific collection
1381 * of objects. Right now we just normal procedure
1382 * for collecting objects */
1383 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1387 drsuapi_DsGetNCChanges
1389 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1391 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1392 struct drsuapi_DsGetNCChanges *r)
1394 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1395 int ret;
1396 uint32_t i;
1397 struct dsdb_schema *schema;
1398 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1399 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1400 NTSTATUS status;
1401 DATA_BLOB session_key;
1402 WERROR werr;
1403 struct dcesrv_handle *h;
1404 struct drsuapi_bind_state *b_state;
1405 struct drsuapi_getncchanges_state *getnc_state;
1406 struct drsuapi_DsGetNCChangesRequest10 *req10;
1407 uint32_t options;
1408 uint32_t max_objects;
1409 uint32_t max_links;
1410 uint32_t link_count = 0;
1411 uint32_t link_total = 0;
1412 uint32_t link_given = 0;
1413 struct ldb_dn *search_dn = NULL;
1414 bool am_rodc, null_scope=false;
1415 enum security_user_level security_level;
1416 struct ldb_context *sam_ctx;
1417 struct dom_sid *user_sid;
1418 bool is_secret_request;
1419 bool is_gc_pas_request;
1421 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1422 b_state = h->data;
1424 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1426 *r->out.level_out = 6;
1427 /* TODO: linked attributes*/
1428 r->out.ctr->ctr6.linked_attributes_count = 0;
1429 r->out.ctr->ctr6.linked_attributes = NULL;
1431 r->out.ctr->ctr6.object_count = 0;
1432 r->out.ctr->ctr6.nc_object_count = 0;
1433 r->out.ctr->ctr6.more_data = false;
1434 r->out.ctr->ctr6.uptodateness_vector = NULL;
1436 /* a RODC doesn't allow for any replication */
1437 ret = samdb_rodc(sam_ctx, &am_rodc);
1438 if (ret == LDB_SUCCESS && am_rodc) {
1439 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1440 return WERR_DS_DRA_SOURCE_DISABLED;
1443 /* Check request revision.
1445 switch (r->in.level) {
1446 case 8:
1447 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1448 if (req10 == NULL) {
1449 return WERR_NOMEM;
1451 break;
1452 case 10:
1453 req10 = &r->in.req->req10;
1454 break;
1455 default:
1456 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1457 r->in.level));
1458 return WERR_REVISION_MISMATCH;
1462 /* Perform access checks. */
1463 /* TODO: we need to support a sync on a specific non-root
1464 * DN. We'll need to find the real partition root here */
1465 ncRoot = req10->naming_context;
1466 if (ncRoot == NULL) {
1467 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1468 return WERR_DS_DRA_INVALID_PARAMETER;
1471 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1472 return WERR_DS_DRA_INTERNAL_ERROR;
1475 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1476 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1477 return WERR_DS_DRA_SOURCE_DISABLED;
1480 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1482 /* all clients must have GUID_DRS_GET_CHANGES */
1483 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1484 mem_ctx,
1485 dce_call->conn->auth_state.session_info->security_token,
1486 req10->naming_context,
1487 GUID_DRS_GET_CHANGES);
1488 if (!W_ERROR_IS_OK(werr)) {
1489 return werr;
1492 /* allowed if the GC PAS and client has
1493 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1494 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1495 if (!W_ERROR_IS_OK(werr)) {
1496 return werr;
1498 if (is_gc_pas_request) {
1499 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1500 mem_ctx,
1501 dce_call->conn->auth_state.session_info->security_token,
1502 req10->naming_context,
1503 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1504 if (W_ERROR_IS_OK(werr)) {
1505 goto allowed;
1509 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1510 if (!W_ERROR_IS_OK(werr)) {
1511 return werr;
1513 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1514 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1515 mem_ctx,
1516 dce_call->conn->auth_state.session_info->security_token,
1517 req10->naming_context,
1518 GUID_DRS_GET_ALL_CHANGES);
1519 if (!W_ERROR_IS_OK(werr)) {
1520 return werr;
1524 allowed:
1525 /* for non-administrator replications, check that they have
1526 given the correct source_dsa_invocation_id */
1527 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1528 samdb_domain_sid(sam_ctx));
1529 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1530 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1531 /* we rely on this flag being unset for RODC requests */
1532 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1536 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1537 /* Ignore the _in_ uptpdateness vector*/
1538 req10->uptodateness_vector = NULL;
1541 getnc_state = b_state->getncchanges_state;
1543 /* see if a previous replication has been abandoned */
1544 if (getnc_state) {
1545 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1546 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1547 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1548 ldb_dn_get_linearized(new_dn),
1549 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1550 ldb_dn_get_linearized(getnc_state->last_dn)));
1551 talloc_free(getnc_state);
1552 getnc_state = NULL;
1556 if (getnc_state == NULL) {
1557 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1558 if (getnc_state == NULL) {
1559 return WERR_NOMEM;
1561 b_state->getncchanges_state = getnc_state;
1562 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1564 /* find out if we are to replicate Schema NC */
1565 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1566 ldb_get_schema_basedn(b_state->sam_ctx));
1567 getnc_state->is_schema_nc = (0 == ret);
1569 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1570 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1574 * This is the first replication cycle and it is
1575 * a good place to handle extended operations
1577 * FIXME: we don't fully support extended operations yet
1579 switch (req10->extended_op) {
1580 case DRSUAPI_EXOP_NONE:
1581 break;
1582 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1583 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1584 W_ERROR_NOT_OK_RETURN(werr);
1585 search_dn = ldb_get_default_basedn(sam_ctx);
1586 break;
1587 case DRSUAPI_EXOP_REPL_SECRET:
1588 werr = getncchanges_repl_secret(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1589 r->out.result = werr;
1590 W_ERROR_NOT_OK_RETURN(werr);
1591 break;
1592 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1593 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1594 W_ERROR_NOT_OK_RETURN(werr);
1595 break;
1596 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1597 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1598 W_ERROR_NOT_OK_RETURN(werr);
1599 break;
1600 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1601 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1602 W_ERROR_NOT_OK_RETURN(werr);
1603 break;
1604 case DRSUAPI_EXOP_REPL_OBJ:
1605 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1606 r->out.result = werr;
1607 W_ERROR_NOT_OK_RETURN(werr);
1608 break;
1610 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1612 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1613 (unsigned)req10->extended_op));
1614 return WERR_DS_DRA_NOT_SUPPORTED;
1618 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1619 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1620 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1621 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1622 return WERR_DS_DRA_INVALID_PARAMETER;
1625 /* we need the session key for encrypting password attributes */
1626 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1627 if (!NT_STATUS_IS_OK(status)) {
1628 DEBUG(0,(__location__ ": Failed to get session key\n"));
1629 return WERR_DS_DRA_INTERNAL_ERROR;
1633 TODO: MS-DRSR section 4.1.10.1.1
1634 Work out if this is the start of a new cycle */
1636 if (getnc_state->guids == NULL) {
1637 const char *extra_filter;
1638 struct ldb_result *search_res = NULL;
1640 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1642 getnc_state->min_usn = req10->highwatermark.highest_usn;
1644 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1645 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1646 search_dn, extra_filter,
1647 &search_res);
1648 } else {
1649 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1650 &r->out.ctr->ctr6,
1651 search_dn, extra_filter,
1652 &search_res);
1654 W_ERROR_NOT_OK_RETURN(werr);
1656 if (search_res) {
1657 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1658 TYPESAFE_QSORT(search_res->msgs,
1659 search_res->count,
1660 site_res_cmp_parent_order);
1661 } else {
1662 TYPESAFE_QSORT(search_res->msgs,
1663 search_res->count,
1664 site_res_cmp_usn_order);
1668 /* extract out the GUIDs list */
1669 getnc_state->num_records = search_res ? search_res->count : 0;
1670 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1671 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1673 for (i=0; i<getnc_state->num_records; i++) {
1674 getnc_state->guids[i] = samdb_result_guid(search_res->msgs[i], "objectGUID");
1675 if (GUID_all_zero(&getnc_state->guids[i])) {
1676 DEBUG(2,("getncchanges: bad objectGUID from %s\n", ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1677 return WERR_DS_DRA_INTERNAL_ERROR;
1682 talloc_free(search_res);
1684 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req10->uptodateness_vector);
1685 if (getnc_state->uptodateness_vector) {
1686 /* make sure its sorted */
1687 TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1688 getnc_state->uptodateness_vector->count,
1689 drsuapi_DsReplicaCursor_compare);
1693 /* Prefix mapping */
1694 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1695 if (!schema) {
1696 DEBUG(0,("No schema in sam_ctx\n"));
1697 return WERR_DS_DRA_INTERNAL_ERROR;
1700 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1701 *r->out.ctr->ctr6.naming_context = *ncRoot;
1703 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1704 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1705 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1706 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1707 return WERR_DS_DRA_INTERNAL_ERROR;
1710 /* find the SID if there is one */
1711 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1713 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1714 r->out.ctr->ctr6.mapping_ctr = *ctr;
1716 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1717 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1719 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1720 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1722 r->out.ctr->ctr6.first_object = NULL;
1723 currentObject = &r->out.ctr->ctr6.first_object;
1725 /* use this to force single objects at a time, which is useful
1726 * for working out what object is giving problems
1728 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1729 if (req10->max_object_count < max_objects) {
1730 max_objects = req10->max_object_count;
1733 * TODO: work out how the maximum should be calculated
1735 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1737 for (i=getnc_state->num_processed;
1738 i<getnc_state->num_records &&
1739 !null_scope &&
1740 (r->out.ctr->ctr6.object_count < max_objects);
1741 i++) {
1742 int uSN;
1743 struct drsuapi_DsReplicaObjectListItemEx *obj;
1744 struct ldb_message *msg;
1745 static const char * const msg_attrs[] = {
1746 "*",
1747 "nTSecurityDescriptor",
1748 "parentGUID",
1749 "replPropertyMetaData",
1750 DSDB_SECRET_ATTRIBUTES,
1751 NULL };
1752 struct ldb_result *msg_res;
1753 struct ldb_dn *msg_dn;
1755 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1756 W_ERROR_HAVE_NO_MEMORY(obj);
1758 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1759 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1762 /* by re-searching here we avoid having a lot of full
1763 * records in memory between calls to getncchanges
1765 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1766 msg_dn,
1767 LDB_SCOPE_BASE, msg_attrs, NULL);
1768 if (ret != LDB_SUCCESS) {
1769 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1770 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1771 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1773 talloc_free(obj);
1774 continue;
1777 msg = msg_res->msgs[0];
1779 werr = get_nc_changes_build_object(obj, msg,
1780 sam_ctx, getnc_state->ncRoot_dn,
1781 getnc_state->is_schema_nc,
1782 schema, &session_key, getnc_state->min_usn,
1783 req10->replica_flags,
1784 req10->partial_attribute_set,
1785 getnc_state->uptodateness_vector,
1786 req10->extended_op);
1787 if (!W_ERROR_IS_OK(werr)) {
1788 return werr;
1791 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1792 getnc_state->ncRoot_dn,
1793 schema, getnc_state->min_usn,
1794 req10->replica_flags,
1795 msg,
1796 &getnc_state->la_list,
1797 &getnc_state->la_count,
1798 getnc_state->uptodateness_vector);
1799 if (!W_ERROR_IS_OK(werr)) {
1800 return werr;
1803 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1804 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1805 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1807 if (uSN > getnc_state->highest_usn) {
1808 getnc_state->highest_usn = uSN;
1811 if (obj->meta_data_ctr == NULL) {
1812 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1813 ldb_dn_get_linearized(msg->dn)));
1814 /* no attributes to send */
1815 talloc_free(obj);
1816 continue;
1819 r->out.ctr->ctr6.object_count++;
1821 *currentObject = obj;
1822 currentObject = &obj->next_object;
1824 talloc_free(getnc_state->last_dn);
1825 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1827 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1829 talloc_free(msg_res);
1830 talloc_free(msg_dn);
1833 getnc_state->num_processed = i;
1835 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1837 /* the client can us to call UpdateRefs on its behalf to
1838 re-establish monitoring of the NC */
1839 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1840 !GUID_all_zero(&req10->destination_dsa_guid)) {
1841 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1842 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1843 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1844 ureq.naming_context = ncRoot;
1845 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
1846 &req10->destination_dsa_guid);
1847 if (!ureq.dest_dsa_dns_name) {
1848 return WERR_NOMEM;
1850 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1851 ureq.options = DRSUAPI_DRS_ADD_REF |
1852 DRSUAPI_DRS_ASYNC_OP |
1853 DRSUAPI_DRS_GETCHG_CHECK;
1855 /* we also need to pass through the
1856 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1857 to send notifies using the GC SPN */
1858 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1860 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1861 if (!W_ERROR_IS_OK(werr)) {
1862 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1863 win_errstr(werr)));
1868 * TODO:
1869 * This is just a guess, how to calculate the
1870 * number of linked attributes to send, we need to
1871 * find out how to do this right.
1873 if (r->out.ctr->ctr6.object_count >= max_links) {
1874 max_links = 0;
1875 } else {
1876 max_links -= r->out.ctr->ctr6.object_count;
1879 link_total = getnc_state->la_count;
1881 if (i < getnc_state->num_records) {
1882 r->out.ctr->ctr6.more_data = true;
1883 } else {
1884 /* sort the whole array the first time */
1885 if (!getnc_state->la_sorted) {
1886 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1887 sam_ctx, linked_attribute_compare);
1888 getnc_state->la_sorted = true;
1891 link_count = getnc_state->la_count - getnc_state->la_idx;
1892 link_count = MIN(max_links, link_count);
1894 r->out.ctr->ctr6.linked_attributes_count = link_count;
1895 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1897 getnc_state->la_idx += link_count;
1898 link_given = getnc_state->la_idx;
1900 if (getnc_state->la_idx < getnc_state->la_count) {
1901 r->out.ctr->ctr6.more_data = true;
1905 if (!r->out.ctr->ctr6.more_data) {
1906 talloc_steal(mem_ctx, getnc_state->la_list);
1908 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1909 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1911 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1912 r->out.ctr->ctr6.uptodateness_vector);
1913 if (!W_ERROR_IS_OK(werr)) {
1914 return werr;
1917 talloc_free(getnc_state);
1918 b_state->getncchanges_state = NULL;
1921 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1922 r->out.ctr->ctr6.uptodateness_vector = NULL;
1923 r->out.ctr->ctr6.nc_object_count = 0;
1924 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1927 DEBUG(r->out.ctr->ctr6.more_data?4:2,
1928 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1929 (unsigned long long)(req10->highwatermark.highest_usn+1),
1930 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
1931 r->out.ctr->ctr6.object_count,
1932 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
1933 r->out.ctr->ctr6.linked_attributes_count,
1934 link_given, link_total,
1935 dom_sid_string(mem_ctx, user_sid)));
1937 #if 0
1938 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
1939 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
1941 #endif
1943 return WERR_OK;