s4:drsuapi: try to behave more like windows for usn order (bug #9508)
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / getncchanges.c
blobc3fd000e8a24d1612800b2a1b317aaadc62e345d
1 /*
2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "libcli/security/security.h"
31 #include "libcli/security/session.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33 #include "rpc_server/dcerpc_server_proto.h"
34 #include "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
40 /* state of a partially completed getncchanges call */
41 struct drsuapi_getncchanges_state {
42 struct GUID *guids;
43 uint32_t num_records;
44 uint32_t num_processed;
45 struct ldb_dn *ncRoot_dn;
46 bool is_schema_nc;
47 uint64_t min_usn;
48 uint64_t max_usn;
49 struct drsuapi_DsReplicaHighWaterMark last_hwm;
50 struct ldb_dn *last_dn;
51 struct drsuapi_DsReplicaHighWaterMark final_hwm;
52 struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
53 struct drsuapi_DsReplicaLinkedAttribute *la_list;
54 uint32_t la_count;
55 bool la_sorted;
56 uint32_t la_idx;
59 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
60 const struct drsuapi_DsReplicaHighWaterMark *h2)
62 if (h1->highest_usn < h2->highest_usn) {
63 return -1;
64 } else if (h1->highest_usn > h2->highest_usn) {
65 return 1;
66 } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
67 return -1;
68 } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
69 return 1;
70 } else if (h1->reserved_usn < h2->reserved_usn) {
71 return -1;
72 } else if (h1->reserved_usn > h2->reserved_usn) {
73 return 1;
76 return 0;
80 build a DsReplicaObjectIdentifier from a ldb msg
82 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
83 struct ldb_message *msg)
85 struct drsuapi_DsReplicaObjectIdentifier *identifier;
86 struct dom_sid *sid;
88 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
89 if (identifier == NULL) {
90 return NULL;
93 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
94 identifier->guid = samdb_result_guid(msg, "objectGUID");
96 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
97 if (sid) {
98 identifier->sid = *sid;
99 } else {
100 ZERO_STRUCT(identifier->sid);
102 return identifier;
105 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
107 return GUID_compare(guid1, &guid2);
111 see if we can filter an attribute using the uptodateness_vector
113 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
114 const struct GUID *originating_invocation_id,
115 uint64_t originating_usn)
117 const struct drsuapi_DsReplicaCursor *c;
118 if (udv == NULL) return false;
119 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
120 originating_invocation_id, udv_compare, c);
121 if (c && originating_usn <= c->highest_usn) {
122 return true;
124 return false;
128 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
130 if (a1 == a2) return 0;
131 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
135 check if an attribute is in a partial_attribute_set
137 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
138 struct drsuapi_DsPartialAttributeSet *pas)
140 enum drsuapi_DsAttributeId *result;
141 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
142 attid_cmp, result);
143 return result != NULL;
148 drsuapi_DsGetNCChanges for one object
150 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
151 struct ldb_message *msg,
152 struct ldb_context *sam_ctx,
153 struct ldb_dn *ncRoot_dn,
154 bool is_schema_nc,
155 struct dsdb_schema *schema,
156 DATA_BLOB *session_key,
157 uint64_t highest_usn,
158 uint32_t replica_flags,
159 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
160 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
161 enum drsuapi_DsExtendedOperation extended_op,
162 bool force_object_return)
164 const struct ldb_val *md_value;
165 uint32_t i, n;
166 struct replPropertyMetaDataBlob md;
167 uint32_t rid = 0;
168 enum ndr_err_code ndr_err;
169 uint32_t *attids;
170 const char *rdn;
171 const struct dsdb_attribute *rdn_sa;
172 unsigned int instanceType;
173 struct dsdb_syntax_ctx syntax_ctx;
175 /* make dsdb sytanx context for conversions */
176 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
177 syntax_ctx.is_schema_nc = is_schema_nc;
179 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
180 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
181 obj->is_nc_prefix = true;
182 obj->parent_object_guid = NULL;
183 } else {
184 obj->is_nc_prefix = false;
185 obj->parent_object_guid = talloc(obj, struct GUID);
186 if (obj->parent_object_guid == NULL) {
187 return WERR_DS_DRA_INTERNAL_ERROR;
189 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
190 if (GUID_all_zero(obj->parent_object_guid)) {
191 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
192 ldb_dn_get_linearized(msg->dn)));
193 return WERR_DS_DRA_INTERNAL_ERROR;
196 obj->next_object = NULL;
198 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
199 if (!md_value) {
200 /* nothing to send */
201 return WERR_OK;
204 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
205 /* don't send uninstantiated objects */
206 return WERR_OK;
209 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
210 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
211 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
212 return WERR_DS_DRA_INTERNAL_ERROR;
215 if (md.version != 1) {
216 return WERR_DS_DRA_INTERNAL_ERROR;
219 rdn = ldb_dn_get_rdn_name(msg->dn);
220 if (rdn == NULL) {
221 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
222 return WERR_DS_DRA_INTERNAL_ERROR;
225 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
226 if (rdn_sa == NULL) {
227 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
228 rdn, ldb_dn_get_linearized(msg->dn)));
229 return WERR_DS_DRA_INTERNAL_ERROR;
232 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
233 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
235 obj->object.identifier = get_object_identifier(obj, msg);
236 if (obj->object.identifier == NULL) {
237 return WERR_NOMEM;
239 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
241 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
242 for (n=i=0; i<md.ctr.ctr1.count; i++) {
243 const struct dsdb_attribute *sa;
244 bool force_attribute = false;
246 /* if the attribute has not changed, and it is not the
247 instanceType then don't include it */
248 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
249 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
250 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
252 /* don't include the rDN */
253 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
255 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
256 if (!sa) {
257 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
258 (unsigned int)md.ctr.ctr1.array[i].attid,
259 ldb_dn_get_linearized(msg->dn)));
260 return WERR_DS_DRA_INTERNAL_ERROR;
263 if (sa->linkID) {
264 struct ldb_message_element *el;
265 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
266 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
267 /* don't send upgraded links inline */
268 continue;
272 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
273 !dsdb_attr_in_rodc_fas(sa)) {
274 force_attribute = true;
275 DEBUG(4,("Forcing attribute %s in %s\n",
276 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
279 /* filter by uptodateness_vector */
280 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
281 !force_attribute &&
282 udv_filter(uptodateness_vector,
283 &md.ctr.ctr1.array[i].originating_invocation_id,
284 md.ctr.ctr1.array[i].originating_usn)) {
285 continue;
288 /* filter by partial_attribute_set */
289 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
290 continue;
293 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
294 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
295 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
296 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
297 attids[n] = md.ctr.ctr1.array[i].attid;
298 n++;
301 /* ignore it if its an empty change. Note that renames always
302 * change the 'name' attribute, so they won't be ignored by
303 * this
305 * the force_object_return check is used to force an empty
306 * object return when we timeout in the getncchanges loop.
307 * This allows us to return an empty object, which keeps the
308 * client happy while preventing timeouts
310 if (n == 0 ||
311 (n == 1 &&
312 attids[0] == DRSUAPI_ATTID_instanceType &&
313 !force_object_return)) {
314 talloc_free(obj->meta_data_ctr);
315 obj->meta_data_ctr = NULL;
316 return WERR_OK;
319 obj->meta_data_ctr->count = n;
321 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
322 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
323 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
324 obj->object.attribute_ctr.num_attributes);
325 if (obj->object.attribute_ctr.attributes == NULL) {
326 return WERR_NOMEM;
330 * Note that the meta_data array and the attributes array must
331 * be the same size and in the same order
333 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
334 struct ldb_message_element *el;
335 WERROR werr;
336 const struct dsdb_attribute *sa;
338 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
339 if (!sa) {
340 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
341 return WERR_DS_DRA_INTERNAL_ERROR;
344 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
345 if (el == NULL) {
346 /* this happens for attributes that have been removed */
347 DEBUG(5,("No element '%s' for attributeID %u in message\n",
348 sa->lDAPDisplayName, attids[i]));
349 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
350 obj->object.attribute_ctr.attributes[i].attid =
351 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
352 } else {
353 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
354 &obj->object.attribute_ctr.attributes[i]);
355 if (!W_ERROR_IS_OK(werr)) {
356 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
357 sa->lDAPDisplayName, win_errstr(werr)));
358 return werr;
360 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
361 * check if attribute is secret and send a null value
363 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
364 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
365 &obj->meta_data_ctr->meta_data[i]);
367 /* some attributes needs to be encrypted
368 before being sent */
369 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
370 &obj->object.attribute_ctr.attributes[i]);
371 if (!W_ERROR_IS_OK(werr)) {
372 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
373 sa->lDAPDisplayName, win_errstr(werr)));
374 return werr;
379 return WERR_OK;
383 add one linked attribute from an object to the list of linked
384 attributes in a getncchanges request
386 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
387 struct ldb_context *sam_ctx,
388 const struct dsdb_schema *schema,
389 const struct dsdb_attribute *sa,
390 struct ldb_message *msg,
391 struct dsdb_dn *dsdb_dn,
392 struct drsuapi_DsReplicaLinkedAttribute **la_list,
393 uint32_t *la_count)
395 struct drsuapi_DsReplicaLinkedAttribute *la;
396 bool active;
397 NTSTATUS status;
398 WERROR werr;
400 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
401 W_ERROR_HAVE_NO_MEMORY(*la_list);
403 la = &(*la_list)[*la_count];
405 la->identifier = get_object_identifier(*la_list, msg);
406 W_ERROR_HAVE_NO_MEMORY(la->identifier);
408 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
410 if (!active) {
411 /* We have to check that the inactive link still point to an existing object */
412 struct GUID guid;
413 struct ldb_dn *tdn;
414 int ret;
415 const char *v;
417 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
418 if (strncmp(v, "TRUE", 4) == 0) {
420 * Note: we skip the transmition of the deleted link even if the other part used to
421 * know about it because when we transmit the deletion of the object, the link will
422 * be deleted too due to deletion of object where link points and Windows do so.
424 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
425 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
427 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
428 * if it join an existing domain with deleted objets, it firsts impose to have a
429 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
430 * either during initial replication or after the getNCChanges.
431 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
433 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
434 * If FL >=2K8R2 we are sure that this attribute will be here.
435 * For this kind of forest level we do not return the link if the object is recycled
436 * (isRecycled = true).
438 if (strncmp(v, "TRUE", 4) == 0) {
439 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
440 ldb_dn_get_linearized(msg->dn)));
441 return WERR_OK;
443 } else {
444 return WERR_OK;
447 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
448 if (!NT_STATUS_IS_OK(status)) {
449 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
450 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
451 return ntstatus_to_werror(status);
453 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
454 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
455 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
456 GUID_string(mem_ctx, &guid)));
457 return WERR_OK;
458 } else if (ret != LDB_SUCCESS) {
459 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
460 GUID_string(mem_ctx, &guid),
461 ret));
462 return WERR_OK;
465 la->attid = sa->attributeID_id;
466 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
468 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
469 if (!NT_STATUS_IS_OK(status)) {
470 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
471 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
472 return ntstatus_to_werror(status);
474 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
475 if (!NT_STATUS_IS_OK(status)) {
476 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
477 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
478 return ntstatus_to_werror(status);
480 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
481 if (!NT_STATUS_IS_OK(status)) {
482 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
483 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
484 return ntstatus_to_werror(status);
486 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
487 if (!NT_STATUS_IS_OK(status)) {
488 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
489 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
490 return ntstatus_to_werror(status);
493 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
494 if (!NT_STATUS_IS_OK(status)) {
495 /* this is possible for upgraded links */
496 la->originating_add_time = la->meta_data.originating_change_time;
499 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
500 W_ERROR_NOT_OK_RETURN(werr);
502 (*la_count)++;
503 return WERR_OK;
508 add linked attributes from an object to the list of linked
509 attributes in a getncchanges request
511 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
512 TALLOC_CTX *mem_ctx,
513 struct ldb_dn *ncRoot_dn,
514 struct dsdb_schema *schema,
515 uint64_t highest_usn,
516 uint32_t replica_flags,
517 struct ldb_message *msg,
518 struct drsuapi_DsReplicaLinkedAttribute **la_list,
519 uint32_t *la_count,
520 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
522 unsigned int i;
523 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
524 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
526 for (i=0; i<msg->num_elements; i++) {
527 struct ldb_message_element *el = &msg->elements[i];
528 const struct dsdb_attribute *sa;
529 unsigned int j;
531 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
533 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
534 /* we only want forward links */
535 continue;
538 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
539 /* its an old style link, it will have been
540 * sent in the main replication data */
541 continue;
544 for (j=0; j<el->num_values; j++) {
545 struct dsdb_dn *dsdb_dn;
546 uint64_t local_usn;
547 NTSTATUS status;
548 WERROR werr;
550 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
551 if (dsdb_dn == NULL) {
552 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
553 el->name, ldb_dn_get_linearized(msg->dn)));
554 talloc_free(tmp_ctx);
555 return WERR_DS_DRA_INTERNAL_ERROR;
558 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
559 if (!NT_STATUS_IS_OK(status)) {
560 /* this can happen for attributes
561 given to us with old style meta
562 data */
563 continue;
566 if (local_usn > uSNChanged) {
567 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
568 el->name, ldb_dn_get_linearized(msg->dn)));
569 talloc_free(tmp_ctx);
570 return WERR_DS_DRA_INTERNAL_ERROR;
573 if (local_usn < highest_usn) {
574 continue;
577 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
578 dsdb_dn, la_list, la_count);
579 if (!W_ERROR_IS_OK(werr)) {
580 talloc_free(tmp_ctx);
581 return werr;
586 talloc_free(tmp_ctx);
587 return WERR_OK;
591 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
593 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
594 struct ldb_dn *ncRoot_dn,
595 struct drsuapi_DsReplicaCursor2CtrEx *udv)
597 int ret;
599 udv->version = 2;
600 udv->reserved1 = 0;
601 udv->reserved2 = 0;
603 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
604 if (ret != LDB_SUCCESS) {
605 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
606 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
607 return WERR_DS_DRA_INTERNAL_ERROR;
610 return WERR_OK;
614 /* comparison function for linked attributes - see CompareLinks() in
615 * MS-DRSR section 4.1.10.5.17 */
616 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
617 const struct drsuapi_DsReplicaLinkedAttribute *la2,
618 struct ldb_context *sam_ctx)
620 int c;
621 WERROR werr;
622 TALLOC_CTX *tmp_ctx;
623 const struct dsdb_schema *schema;
624 const struct dsdb_attribute *schema_attrib;
625 struct dsdb_dn *dn1, *dn2;
626 struct GUID guid1, guid2;
627 NTSTATUS status;
629 c = GUID_compare(&la1->identifier->guid,
630 &la2->identifier->guid);
631 if (c != 0) return c;
633 if (la1->attid != la2->attid) {
634 return la1->attid < la2->attid? -1:1;
637 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
638 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
639 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
642 /* we need to get the target GUIDs to compare */
643 tmp_ctx = talloc_new(sam_ctx);
645 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
646 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
648 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
649 if (!W_ERROR_IS_OK(werr)) {
650 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
651 talloc_free(tmp_ctx);
652 return 0;
655 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
656 if (!W_ERROR_IS_OK(werr)) {
657 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
658 talloc_free(tmp_ctx);
659 return 0;
662 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
663 if (!NT_STATUS_IS_OK(status)) {
664 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
665 talloc_free(tmp_ctx);
666 return 0;
668 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
669 if (!NT_STATUS_IS_OK(status)) {
670 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
671 talloc_free(tmp_ctx);
672 return 0;
675 talloc_free(tmp_ctx);
677 return GUID_compare(&guid1, &guid2);
680 struct drsuapi_changed_objects {
681 struct ldb_dn *dn;
682 struct GUID guid;
683 uint64_t usn;
687 sort the objects we send by tree order
689 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
690 struct drsuapi_changed_objects *m2,
691 struct drsuapi_getncchanges_state *getnc_state)
693 return ldb_dn_compare(m2->dn, m1->dn);
697 sort the objects we send first by uSNChanged
699 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
700 struct drsuapi_changed_objects *m2,
701 struct drsuapi_getncchanges_state *getnc_state)
703 int ret;
705 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
706 if (ret == 0) {
707 return -1;
710 ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
711 if (ret == 0) {
712 return 1;
715 if (m1->usn == m2->usn) {
716 return ldb_dn_compare(m2->dn, m1->dn);
719 if (m1->usn < m2->usn) {
720 return -1;
723 return 1;
728 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
730 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
731 TALLOC_CTX *mem_ctx,
732 struct drsuapi_DsGetNCChangesRequest10 *req10,
733 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
735 struct ldb_dn *rid_manager_dn, *req_dn;
736 int ret;
737 struct ldb_context *ldb = b_state->sam_ctx;
738 struct ldb_result *ext_res;
739 struct dsdb_fsmo_extended_op *exop;
740 bool is_us;
743 steps:
744 - verify that the DN being asked for is the RID Manager DN
745 - verify that we are the RID Manager
748 /* work out who is the RID Manager */
749 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
750 if (ret != LDB_SUCCESS) {
751 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
752 return WERR_DS_DRA_INTERNAL_ERROR;
755 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
756 if (!ldb_dn_validate(req_dn) ||
757 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
758 /* that isn't the RID Manager DN */
759 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
760 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
761 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
762 return WERR_OK;
765 /* find the DN of the RID Manager */
766 ret = samdb_reference_dn_is_our_ntdsa(ldb, rid_manager_dn, "fSMORoleOwner", &is_us);
767 if (ret != LDB_SUCCESS) {
768 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
769 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
770 return WERR_DS_DRA_INTERNAL_ERROR;
773 if (!is_us) {
774 /* we're not the RID Manager - go away */
775 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
776 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
777 return WERR_OK;
780 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
781 W_ERROR_HAVE_NO_MEMORY(exop);
783 exop->fsmo_info = req10->fsmo_info;
784 exop->destination_dsa_guid = req10->destination_dsa_guid;
786 ret = ldb_transaction_start(ldb);
787 if (ret != LDB_SUCCESS) {
788 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
789 ldb_errstring(ldb)));
790 return WERR_DS_DRA_INTERNAL_ERROR;
794 * FIXME (kim): this is a temp hack to return just few object,
795 * but not the whole domain NC.
796 * We should remove this hack and implement a 'scope'
797 * building function to return just the set of object
798 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
800 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
802 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
803 if (ret != LDB_SUCCESS) {
804 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
805 ldb_errstring(ldb)));
806 ldb_transaction_cancel(ldb);
807 return WERR_DS_DRA_INTERNAL_ERROR;
810 ret = ldb_transaction_commit(ldb);
811 if (ret != LDB_SUCCESS) {
812 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
813 ldb_errstring(ldb)));
814 return WERR_DS_DRA_INTERNAL_ERROR;
817 talloc_free(ext_res);
819 DEBUG(2,("Allocated RID pool for server %s\n",
820 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
822 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
824 return WERR_OK;
828 return an array of SIDs from a ldb_message given an attribute name
829 assumes the SIDs are in extended DN format
831 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
832 struct ldb_message *msg,
833 TALLOC_CTX *mem_ctx,
834 const char *attr,
835 const struct dom_sid ***sids)
837 struct ldb_message_element *el;
838 unsigned int i;
840 el = ldb_msg_find_element(msg, attr);
841 if (!el) {
842 *sids = NULL;
843 return WERR_OK;
846 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
847 W_ERROR_HAVE_NO_MEMORY(*sids);
849 for (i=0; i<el->num_values; i++) {
850 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
851 NTSTATUS status;
852 struct dom_sid *sid;
854 sid = talloc(*sids, struct dom_sid);
855 W_ERROR_HAVE_NO_MEMORY(sid);
856 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
857 if (!NT_STATUS_IS_OK(status)) {
858 return WERR_INTERNAL_DB_CORRUPTION;
860 (*sids)[i] = sid;
862 (*sids)[i] = NULL;
864 return WERR_OK;
869 return an array of SIDs from a ldb_message given an attribute name
870 assumes the SIDs are in NDR form
872 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
873 struct ldb_message *msg,
874 TALLOC_CTX *mem_ctx,
875 const char *attr,
876 const struct dom_sid ***sids)
878 struct ldb_message_element *el;
879 unsigned int i;
881 el = ldb_msg_find_element(msg, attr);
882 if (!el) {
883 *sids = NULL;
884 return WERR_OK;
887 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
888 W_ERROR_HAVE_NO_MEMORY(*sids);
890 for (i=0; i<el->num_values; i++) {
891 enum ndr_err_code ndr_err;
892 struct dom_sid *sid;
894 sid = talloc(*sids, struct dom_sid);
895 W_ERROR_HAVE_NO_MEMORY(sid);
897 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
898 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
899 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
900 return WERR_INTERNAL_DB_CORRUPTION;
902 (*sids)[i] = sid;
904 (*sids)[i] = NULL;
906 return WERR_OK;
910 see if any SIDs in list1 are in list2
912 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
914 unsigned int i, j;
915 /* do we ever have enough SIDs here to worry about O(n^2) ? */
916 for (i=0; list1[i]; i++) {
917 for (j=0; list2[j]; j++) {
918 if (dom_sid_equal(list1[i], list2[j])) {
919 return true;
923 return false;
927 handle a DRSUAPI_EXOP_REPL_SECRET call
929 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
930 TALLOC_CTX *mem_ctx,
931 struct drsuapi_DsGetNCChangesRequest10 *req10,
932 struct dom_sid *user_sid,
933 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
934 bool has_get_all_changes)
936 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
937 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
938 int ret;
939 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
940 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
941 struct ldb_result *rodc_res, *obj_res;
942 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
943 WERROR werr;
945 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
946 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
949 * we need to work out if we will allow this DC to
950 * replicate the secrets for this object
952 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
953 * of this function
956 if (b_state->sam_ctx_system == NULL) {
957 /* this operation needs system level access */
958 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
959 return WERR_DS_DRA_SOURCE_DISABLED;
963 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
965 * The pseudo code indicate
966 * revealsecrets = true
967 * if IsRevealSecretRequest(msgIn) then
968 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
969 * then
970 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
971 * <... check if this account is ok to be replicated on this DC ...>
972 * <... and if not reveal secrets = no ...>
973 * else
974 * reveal secrets = false
975 * endif
976 * endif
977 * endif
979 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
980 * then you can do EXOP_REPL_SECRETS
982 if (has_get_all_changes) {
983 goto allowed;
986 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
987 if (!ldb_dn_validate(obj_dn)) goto failed;
989 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
990 dom_sid_string(mem_ctx, user_sid));
991 if (!ldb_dn_validate(rodc_dn)) goto failed;
993 /* do the two searches we need */
994 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
995 DSDB_SEARCH_SHOW_EXTENDED_DN);
996 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
998 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
999 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1001 /* if the object SID is equal to the user_sid, allow */
1002 if (dom_sid_equal(user_sid,
1003 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1004 goto allowed;
1007 /* an RODC is allowed to get its own krbtgt account secrets */
1008 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1009 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1010 if (krbtgt_link_dn != NULL &&
1011 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1012 goto allowed;
1015 /* but it isn't allowed to get anyone elses krbtgt secrets */
1016 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1017 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1018 goto denied;
1021 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1022 "userAccountControl", 0) &
1023 UF_INTERDOMAIN_TRUST_ACCOUNT) {
1024 goto denied;
1027 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1028 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1029 if (!W_ERROR_IS_OK(werr)) {
1030 goto denied;
1033 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1034 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1035 if (!W_ERROR_IS_OK(werr)) {
1036 goto denied;
1039 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1040 mem_ctx, "tokenGroups", &token_sids);
1041 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1042 goto denied;
1045 if (never_reveal_sids &&
1046 sid_list_match(token_sids, never_reveal_sids)) {
1047 goto denied;
1050 if (reveal_sids &&
1051 sid_list_match(token_sids, reveal_sids)) {
1052 goto allowed;
1055 /* default deny */
1056 denied:
1057 DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1058 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1059 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1060 return WERR_DS_DRA_ACCESS_DENIED;
1062 allowed:
1063 DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1064 ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1065 ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1066 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1067 req10->highwatermark.highest_usn = 0;
1068 return WERR_OK;
1070 failed:
1071 DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1072 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1073 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1074 return WERR_DS_DRA_BAD_DN;
1079 handle a DRSUAPI_EXOP_REPL_OBJ call
1081 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1082 TALLOC_CTX *mem_ctx,
1083 struct drsuapi_DsGetNCChangesRequest10 *req10,
1084 struct dom_sid *user_sid,
1085 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1087 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1089 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1090 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1092 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1093 return WERR_OK;
1098 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1099 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1100 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1102 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1103 TALLOC_CTX *mem_ctx,
1104 struct drsuapi_DsGetNCChangesRequest10 *req10,
1105 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1107 struct ldb_dn *req_dn, *ntds_dn;
1108 int ret;
1109 unsigned int i;
1110 struct ldb_context *ldb = b_state->sam_ctx;
1111 struct ldb_message *msg;
1112 bool is_us;
1115 steps:
1116 - verify that the client dn exists
1117 - verify that we are the current master
1120 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1121 if (!ldb_dn_validate(req_dn)) {
1122 /* that is not a valid dn */
1123 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1124 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1125 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1126 return WERR_OK;
1129 /* retrieve the current role owner */
1130 /* find the DN of the RID Manager */
1131 ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1132 if (ret != LDB_SUCCESS) {
1133 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1134 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1135 return WERR_DS_DRA_INTERNAL_ERROR;
1138 if (!is_us) {
1139 /* we're not the RID Manager - go away */
1140 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
1141 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1142 return WERR_OK;
1145 /* change the current master */
1146 msg = ldb_msg_new(ldb);
1147 W_ERROR_HAVE_NO_MEMORY(msg);
1148 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1149 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1151 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1152 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1153 if (ret != LDB_SUCCESS) {
1154 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1155 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1156 talloc_free(msg);
1157 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1158 return WERR_OK;
1161 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1162 if (ret != 0) {
1163 talloc_free(msg);
1164 return WERR_DS_DRA_INTERNAL_ERROR;
1167 for (i=0;i<msg->num_elements;i++) {
1168 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1171 ret = ldb_transaction_start(ldb);
1172 if (ret != LDB_SUCCESS) {
1173 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1174 ldb_errstring(ldb)));
1175 return WERR_DS_DRA_INTERNAL_ERROR;
1178 ret = ldb_modify(ldb, msg);
1179 if (ret != LDB_SUCCESS) {
1180 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1181 ldb_errstring(ldb)));
1182 ldb_transaction_cancel(ldb);
1183 return WERR_DS_DRA_INTERNAL_ERROR;
1186 ret = ldb_transaction_commit(ldb);
1187 if (ret != LDB_SUCCESS) {
1188 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1189 ldb_errstring(ldb)));
1190 return WERR_DS_DRA_INTERNAL_ERROR;
1193 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1195 return WERR_OK;
1199 see if this getncchanges request includes a request to reveal secret information
1201 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1202 struct drsuapi_DsGetNCChangesRequest10 *req10,
1203 bool *is_secret_request)
1205 enum drsuapi_DsExtendedOperation exop;
1206 uint32_t i;
1207 struct dsdb_schema *schema;
1209 *is_secret_request = true;
1211 exop = req10->extended_op;
1213 switch (exop) {
1214 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1215 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1216 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1217 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1218 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1219 /* FSMO exops can reveal secrets */
1220 *is_secret_request = true;
1221 return WERR_OK;
1222 case DRSUAPI_EXOP_REPL_SECRET:
1223 case DRSUAPI_EXOP_REPL_OBJ:
1224 case DRSUAPI_EXOP_NONE:
1225 break;
1228 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1229 *is_secret_request = false;
1230 return WERR_OK;
1233 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1234 req10->partial_attribute_set == NULL) {
1235 /* they want secrets */
1236 *is_secret_request = true;
1237 return WERR_OK;
1240 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1242 /* check the attributes they asked for */
1243 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1244 const struct dsdb_attribute *sa;
1245 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1246 if (sa == NULL) {
1247 return WERR_DS_DRA_SCHEMA_MISMATCH;
1249 if (!dsdb_attr_in_rodc_fas(sa)) {
1250 *is_secret_request = true;
1251 return WERR_OK;
1255 if (req10->partial_attribute_set_ex) {
1256 /* check the extended attributes they asked for */
1257 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1258 const struct dsdb_attribute *sa;
1259 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1260 if (sa == NULL) {
1261 return WERR_DS_DRA_SCHEMA_MISMATCH;
1263 if (!dsdb_attr_in_rodc_fas(sa)) {
1264 *is_secret_request = true;
1265 return WERR_OK;
1270 *is_secret_request = false;
1271 return WERR_OK;
1275 see if this getncchanges request is only for attributes in the GC
1276 partial attribute set
1278 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1279 struct drsuapi_DsGetNCChangesRequest10 *req10,
1280 bool *is_gc_pas_request)
1282 enum drsuapi_DsExtendedOperation exop;
1283 uint32_t i;
1284 struct dsdb_schema *schema;
1286 exop = req10->extended_op;
1288 switch (exop) {
1289 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1290 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1291 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1292 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1293 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1294 case DRSUAPI_EXOP_REPL_SECRET:
1295 *is_gc_pas_request = false;
1296 return WERR_OK;
1297 case DRSUAPI_EXOP_REPL_OBJ:
1298 case DRSUAPI_EXOP_NONE:
1299 break;
1302 if (req10->partial_attribute_set == NULL) {
1303 /* they want it all */
1304 *is_gc_pas_request = false;
1305 return WERR_OK;
1308 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1310 /* check the attributes they asked for */
1311 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1312 const struct dsdb_attribute *sa;
1313 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1314 if (sa == NULL) {
1315 return WERR_DS_DRA_SCHEMA_MISMATCH;
1317 if (!sa->isMemberOfPartialAttributeSet) {
1318 *is_gc_pas_request = false;
1319 return WERR_OK;
1323 if (req10->partial_attribute_set_ex) {
1324 /* check the extended attributes they asked for */
1325 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1326 const struct dsdb_attribute *sa;
1327 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1328 if (sa == NULL) {
1329 return WERR_DS_DRA_SCHEMA_MISMATCH;
1331 if (!sa->isMemberOfPartialAttributeSet) {
1332 *is_gc_pas_request = false;
1333 return WERR_OK;
1338 *is_gc_pas_request = true;
1339 return WERR_OK;
1344 map from req8 to req10
1346 static struct drsuapi_DsGetNCChangesRequest10 *
1347 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1348 struct drsuapi_DsGetNCChangesRequest8 *req8)
1350 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1351 struct drsuapi_DsGetNCChangesRequest10);
1352 if (req10 == NULL) {
1353 return NULL;
1356 req10->destination_dsa_guid = req8->destination_dsa_guid;
1357 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1358 req10->naming_context = req8->naming_context;
1359 req10->highwatermark = req8->highwatermark;
1360 req10->uptodateness_vector = req8->uptodateness_vector;
1361 req10->replica_flags = req8->replica_flags;
1362 req10->max_object_count = req8->max_object_count;
1363 req10->max_ndr_size = req8->max_ndr_size;
1364 req10->extended_op = req8->extended_op;
1365 req10->fsmo_info = req8->fsmo_info;
1366 req10->partial_attribute_set = req8->partial_attribute_set;
1367 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1368 req10->mapping_ctr = req8->mapping_ctr;
1370 return req10;
1375 * Collects object for normal replication cycle.
1377 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1378 TALLOC_CTX *mem_ctx,
1379 struct drsuapi_DsGetNCChangesRequest10 *req10,
1380 struct ldb_dn *search_dn,
1381 const char *extra_filter,
1382 struct ldb_result **search_res)
1384 int ret;
1385 char* search_filter;
1386 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1387 //const char *extra_filter;
1388 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1389 const char *attrs[] = { "uSNChanged",
1390 "objectGUID" ,
1391 NULL };
1393 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1394 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1395 scope = LDB_SCOPE_BASE;
1398 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1400 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1402 /* Construct response. */
1403 search_filter = talloc_asprintf(mem_ctx,
1404 "(uSNChanged>=%llu)",
1405 (unsigned long long)(getnc_state->min_usn+1));
1407 if (extra_filter) {
1408 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1411 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1412 search_filter = talloc_asprintf(mem_ctx,
1413 "(&%s(isCriticalSystemObject=TRUE))",
1414 search_filter);
1417 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1418 scope = LDB_SCOPE_BASE;
1421 if (!search_dn) {
1422 search_dn = getnc_state->ncRoot_dn;
1425 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1426 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1427 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1428 search_dn, scope, attrs,
1429 search_filter);
1430 if (ret != LDB_SUCCESS) {
1431 return WERR_DS_DRA_INTERNAL_ERROR;
1434 return WERR_OK;
1438 * Collects object for normal replication cycle.
1440 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1441 TALLOC_CTX *mem_ctx,
1442 struct drsuapi_DsGetNCChangesRequest10 *req10,
1443 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1444 struct ldb_dn *search_dn,
1445 const char *extra_filter,
1446 struct ldb_result **search_res)
1448 /* we have nothing to do in case of ex-op failure */
1449 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1450 return WERR_OK;
1453 /* TODO: implement extended op specific collection
1454 * of objects. Right now we just normal procedure
1455 * for collecting objects */
1456 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1460 drsuapi_DsGetNCChanges
1462 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1464 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1465 struct drsuapi_DsGetNCChanges *r)
1467 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1468 int ret;
1469 uint32_t i;
1470 struct dsdb_schema *schema;
1471 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1472 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1473 NTSTATUS status;
1474 DATA_BLOB session_key;
1475 WERROR werr;
1476 struct dcesrv_handle *h;
1477 struct drsuapi_bind_state *b_state;
1478 struct drsuapi_getncchanges_state *getnc_state;
1479 struct drsuapi_DsGetNCChangesRequest10 *req10;
1480 uint32_t options;
1481 uint32_t max_objects;
1482 uint32_t max_links;
1483 uint32_t link_count = 0;
1484 uint32_t link_total = 0;
1485 uint32_t link_given = 0;
1486 struct ldb_dn *search_dn = NULL;
1487 bool am_rodc, null_scope=false;
1488 enum security_user_level security_level;
1489 struct ldb_context *sam_ctx;
1490 struct dom_sid *user_sid;
1491 bool is_secret_request;
1492 bool is_gc_pas_request;
1493 struct drsuapi_changed_objects *changes;
1494 time_t max_wait;
1495 time_t start = time(NULL);
1496 bool max_wait_reached = false;
1497 bool has_get_all_changes = false;
1498 struct GUID invocation_id;
1500 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1501 b_state = h->data;
1503 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1505 invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1507 *r->out.level_out = 6;
1508 /* TODO: linked attributes*/
1509 r->out.ctr->ctr6.linked_attributes_count = 0;
1510 r->out.ctr->ctr6.linked_attributes = NULL;
1512 r->out.ctr->ctr6.object_count = 0;
1513 r->out.ctr->ctr6.nc_object_count = 0;
1514 r->out.ctr->ctr6.more_data = false;
1515 r->out.ctr->ctr6.uptodateness_vector = NULL;
1517 /* a RODC doesn't allow for any replication */
1518 ret = samdb_rodc(sam_ctx, &am_rodc);
1519 if (ret == LDB_SUCCESS && am_rodc) {
1520 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1521 return WERR_DS_DRA_SOURCE_DISABLED;
1524 /* Check request revision.
1526 switch (r->in.level) {
1527 case 8:
1528 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1529 if (req10 == NULL) {
1530 return WERR_NOMEM;
1532 break;
1533 case 10:
1534 req10 = &r->in.req->req10;
1535 break;
1536 default:
1537 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1538 r->in.level));
1539 return WERR_REVISION_MISMATCH;
1543 /* Perform access checks. */
1544 /* TODO: we need to support a sync on a specific non-root
1545 * DN. We'll need to find the real partition root here */
1546 ncRoot = req10->naming_context;
1547 if (ncRoot == NULL) {
1548 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1549 return WERR_DS_DRA_INVALID_PARAMETER;
1552 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1553 return WERR_DS_DRA_INTERNAL_ERROR;
1556 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1557 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1558 return WERR_DS_DRA_SOURCE_DISABLED;
1561 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1563 /* all clients must have GUID_DRS_GET_CHANGES */
1564 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1565 mem_ctx,
1566 dce_call->conn->auth_state.session_info->security_token,
1567 req10->naming_context,
1568 GUID_DRS_GET_CHANGES);
1569 if (!W_ERROR_IS_OK(werr)) {
1570 return werr;
1573 /* allowed if the GC PAS and client has
1574 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1575 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1576 if (!W_ERROR_IS_OK(werr)) {
1577 return werr;
1579 if (is_gc_pas_request) {
1580 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1581 mem_ctx,
1582 dce_call->conn->auth_state.session_info->security_token,
1583 req10->naming_context,
1584 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1585 if (W_ERROR_IS_OK(werr)) {
1586 goto allowed;
1590 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1591 if (!W_ERROR_IS_OK(werr)) {
1592 return werr;
1594 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1595 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1596 mem_ctx,
1597 dce_call->conn->auth_state.session_info->security_token,
1598 req10->naming_context,
1599 GUID_DRS_GET_ALL_CHANGES);
1600 if (!W_ERROR_IS_OK(werr)) {
1601 return werr;
1602 } else {
1603 has_get_all_changes = true;
1607 allowed:
1608 /* for non-administrator replications, check that they have
1609 given the correct source_dsa_invocation_id */
1610 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1611 samdb_domain_sid(sam_ctx));
1612 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1613 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1614 /* we rely on this flag being unset for RODC requests */
1615 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1619 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1620 /* Ignore the _in_ uptpdateness vector*/
1621 req10->uptodateness_vector = NULL;
1624 if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1625 req10->source_dsa_invocation_id = invocation_id;
1628 if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1630 * The given highwatermark is only valid relative to the
1631 * specified source_dsa_invocation_id.
1633 ZERO_STRUCT(req10->highwatermark);
1636 getnc_state = b_state->getncchanges_state;
1638 /* see if a previous replication has been abandoned */
1639 if (getnc_state) {
1640 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1641 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1642 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1643 ldb_dn_get_linearized(new_dn),
1644 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1645 ldb_dn_get_linearized(getnc_state->last_dn)));
1646 talloc_free(getnc_state);
1647 getnc_state = NULL;
1651 if (getnc_state) {
1652 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1653 &req10->highwatermark);
1654 if (ret != 0) {
1655 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1656 "on DN %s %s highwatermark (last_dn %s)\n",
1657 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1658 (ret > 0) ? "older" : "newer",
1659 ldb_dn_get_linearized(getnc_state->last_dn)));
1660 talloc_free(getnc_state);
1661 getnc_state = NULL;
1665 if (getnc_state == NULL) {
1666 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1667 if (getnc_state == NULL) {
1668 return WERR_NOMEM;
1670 b_state->getncchanges_state = getnc_state;
1671 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1673 /* find out if we are to replicate Schema NC */
1674 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1675 ldb_get_schema_basedn(b_state->sam_ctx));
1676 getnc_state->is_schema_nc = (0 == ret);
1678 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1679 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1683 * This is the first replication cycle and it is
1684 * a good place to handle extended operations
1686 * FIXME: we don't fully support extended operations yet
1688 switch (req10->extended_op) {
1689 case DRSUAPI_EXOP_NONE:
1690 break;
1691 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1692 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1693 W_ERROR_NOT_OK_RETURN(werr);
1694 search_dn = ldb_get_default_basedn(sam_ctx);
1695 break;
1696 case DRSUAPI_EXOP_REPL_SECRET:
1697 werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1698 user_sid,
1699 &r->out.ctr->ctr6,
1700 has_get_all_changes);
1701 r->out.result = werr;
1702 W_ERROR_NOT_OK_RETURN(werr);
1703 break;
1704 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1705 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1706 W_ERROR_NOT_OK_RETURN(werr);
1707 break;
1708 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1709 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1710 W_ERROR_NOT_OK_RETURN(werr);
1711 break;
1712 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1713 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1714 W_ERROR_NOT_OK_RETURN(werr);
1715 break;
1716 case DRSUAPI_EXOP_REPL_OBJ:
1717 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1718 r->out.result = werr;
1719 W_ERROR_NOT_OK_RETURN(werr);
1720 break;
1722 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1724 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1725 (unsigned)req10->extended_op));
1726 return WERR_DS_DRA_NOT_SUPPORTED;
1730 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1731 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1732 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1733 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1734 return WERR_DS_DRA_INVALID_PARAMETER;
1737 /* we need the session key for encrypting password attributes */
1738 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1739 if (!NT_STATUS_IS_OK(status)) {
1740 DEBUG(0,(__location__ ": Failed to get session key\n"));
1741 return WERR_DS_DRA_INTERNAL_ERROR;
1745 TODO: MS-DRSR section 4.1.10.1.1
1746 Work out if this is the start of a new cycle */
1748 if (getnc_state->guids == NULL) {
1749 const char *extra_filter;
1750 struct ldb_result *search_res = NULL;
1752 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1754 getnc_state->min_usn = req10->highwatermark.highest_usn;
1755 getnc_state->max_usn = getnc_state->min_usn;
1757 getnc_state->final_udv = talloc_zero(getnc_state,
1758 struct drsuapi_DsReplicaCursor2CtrEx);
1759 if (getnc_state->final_udv == NULL) {
1760 return WERR_NOMEM;
1762 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1763 getnc_state->final_udv);
1764 if (!W_ERROR_IS_OK(werr)) {
1765 return werr;
1768 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1769 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1770 search_dn, extra_filter,
1771 &search_res);
1772 } else {
1773 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1774 &r->out.ctr->ctr6,
1775 search_dn, extra_filter,
1776 &search_res);
1778 W_ERROR_NOT_OK_RETURN(werr);
1780 /* extract out the GUIDs list */
1781 getnc_state->num_records = search_res ? search_res->count : 0;
1782 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1783 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1785 changes = talloc_array(getnc_state,
1786 struct drsuapi_changed_objects,
1787 getnc_state->num_records);
1788 W_ERROR_HAVE_NO_MEMORY(changes);
1790 for (i=0; i<getnc_state->num_records; i++) {
1791 changes[i].dn = search_res->msgs[i]->dn;
1792 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1793 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1795 if (changes[i].usn > getnc_state->max_usn) {
1796 getnc_state->max_usn = changes[i].usn;
1800 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1801 LDB_TYPESAFE_QSORT(changes,
1802 getnc_state->num_records,
1803 getnc_state,
1804 site_res_cmp_anc_order);
1805 } else {
1806 LDB_TYPESAFE_QSORT(changes,
1807 getnc_state->num_records,
1808 getnc_state,
1809 site_res_cmp_usn_order);
1812 for (i=0; i < getnc_state->num_records; i++) {
1813 getnc_state->guids[i] = changes[i].guid;
1814 if (GUID_all_zero(&getnc_state->guids[i])) {
1815 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1816 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1817 return WERR_DS_DRA_INTERNAL_ERROR;
1821 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
1822 getnc_state->final_hwm.reserved_usn = 0;
1823 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
1825 talloc_free(search_res);
1826 talloc_free(changes);
1829 if (req10->uptodateness_vector) {
1830 /* make sure its sorted */
1831 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
1832 req10->uptodateness_vector->count,
1833 drsuapi_DsReplicaCursor_compare);
1836 /* Prefix mapping */
1837 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1838 if (!schema) {
1839 DEBUG(0,("No schema in sam_ctx\n"));
1840 return WERR_DS_DRA_INTERNAL_ERROR;
1843 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1844 *r->out.ctr->ctr6.naming_context = *ncRoot;
1846 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1847 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1848 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1849 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1850 return WERR_DS_DRA_INTERNAL_ERROR;
1853 /* find the SID if there is one */
1854 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1856 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1857 r->out.ctr->ctr6.mapping_ctr = *ctr;
1859 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1860 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1862 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1863 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1865 r->out.ctr->ctr6.first_object = NULL;
1866 currentObject = &r->out.ctr->ctr6.first_object;
1868 /* use this to force single objects at a time, which is useful
1869 * for working out what object is giving problems
1871 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1872 if (req10->max_object_count < max_objects) {
1873 max_objects = req10->max_object_count;
1876 * TODO: work out how the maximum should be calculated
1878 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1881 * Maximum time that we can spend in a getncchanges
1882 * in order to avoid timeout of the other part.
1883 * 10 seconds by default.
1885 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1886 for (i=getnc_state->num_processed;
1887 i<getnc_state->num_records &&
1888 !null_scope &&
1889 (r->out.ctr->ctr6.object_count < max_objects)
1890 && !max_wait_reached;
1891 i++) {
1892 int uSN;
1893 struct drsuapi_DsReplicaObjectListItemEx *obj;
1894 struct ldb_message *msg;
1895 static const char * const msg_attrs[] = {
1896 "*",
1897 "nTSecurityDescriptor",
1898 "parentGUID",
1899 "replPropertyMetaData",
1900 DSDB_SECRET_ATTRIBUTES,
1901 NULL };
1902 struct ldb_result *msg_res;
1903 struct ldb_dn *msg_dn;
1905 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1906 W_ERROR_HAVE_NO_MEMORY(obj);
1908 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1909 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1912 /* by re-searching here we avoid having a lot of full
1913 * records in memory between calls to getncchanges
1915 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1916 msg_dn,
1917 LDB_SCOPE_BASE, msg_attrs, NULL);
1918 if (ret != LDB_SUCCESS) {
1919 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1920 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1921 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1923 talloc_free(obj);
1924 continue;
1927 msg = msg_res->msgs[0];
1929 max_wait_reached = (time(NULL) - start > max_wait);
1931 werr = get_nc_changes_build_object(obj, msg,
1932 sam_ctx, getnc_state->ncRoot_dn,
1933 getnc_state->is_schema_nc,
1934 schema, &session_key, getnc_state->min_usn,
1935 req10->replica_flags,
1936 req10->partial_attribute_set,
1937 req10->uptodateness_vector,
1938 req10->extended_op,
1939 max_wait_reached);
1940 if (!W_ERROR_IS_OK(werr)) {
1941 return werr;
1944 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1945 getnc_state->ncRoot_dn,
1946 schema, getnc_state->min_usn,
1947 req10->replica_flags,
1948 msg,
1949 &getnc_state->la_list,
1950 &getnc_state->la_count,
1951 req10->uptodateness_vector);
1952 if (!W_ERROR_IS_OK(werr)) {
1953 return werr;
1956 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1957 if (uSN > getnc_state->max_usn) {
1959 * Only report the max_usn we had at the start
1960 * of the replication cycle.
1962 * If this object has changed lately we better
1963 * let the destination dsa refetch the change.
1964 * This is better than the risk of loosing some
1965 * objects or linked attributes.
1967 uSN = 0;
1969 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1970 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1971 r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
1974 if (obj->meta_data_ctr == NULL) {
1975 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1976 ldb_dn_get_linearized(msg->dn)));
1977 /* no attributes to send */
1978 talloc_free(obj);
1979 continue;
1982 r->out.ctr->ctr6.object_count++;
1984 *currentObject = obj;
1985 currentObject = &obj->next_object;
1987 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1989 talloc_free(getnc_state->last_dn);
1990 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
1992 talloc_free(msg_res);
1993 talloc_free(msg_dn);
1996 getnc_state->num_processed = i;
1998 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2000 /* the client can us to call UpdateRefs on its behalf to
2001 re-establish monitoring of the NC */
2002 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2003 !GUID_all_zero(&req10->destination_dsa_guid)) {
2004 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2005 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2006 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2007 ureq.naming_context = ncRoot;
2008 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2009 &req10->destination_dsa_guid);
2010 if (!ureq.dest_dsa_dns_name) {
2011 return WERR_NOMEM;
2013 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2014 ureq.options = DRSUAPI_DRS_ADD_REF |
2015 DRSUAPI_DRS_ASYNC_OP |
2016 DRSUAPI_DRS_GETCHG_CHECK;
2018 /* we also need to pass through the
2019 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2020 to send notifies using the GC SPN */
2021 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2023 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2024 if (!W_ERROR_IS_OK(werr)) {
2025 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
2026 win_errstr(werr)));
2031 * TODO:
2032 * This is just a guess, how to calculate the
2033 * number of linked attributes to send, we need to
2034 * find out how to do this right.
2036 if (r->out.ctr->ctr6.object_count >= max_links) {
2037 max_links = 0;
2038 } else {
2039 max_links -= r->out.ctr->ctr6.object_count;
2042 link_total = getnc_state->la_count;
2044 if (i < getnc_state->num_records) {
2045 r->out.ctr->ctr6.more_data = true;
2046 } else {
2047 /* sort the whole array the first time */
2048 if (!getnc_state->la_sorted) {
2049 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
2050 sam_ctx, linked_attribute_compare);
2051 getnc_state->la_sorted = true;
2054 link_count = getnc_state->la_count - getnc_state->la_idx;
2055 link_count = MIN(max_links, link_count);
2057 r->out.ctr->ctr6.linked_attributes_count = link_count;
2058 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
2060 getnc_state->la_idx += link_count;
2061 link_given = getnc_state->la_idx;
2063 if (getnc_state->la_idx < getnc_state->la_count) {
2064 r->out.ctr->ctr6.more_data = true;
2068 if (!r->out.ctr->ctr6.more_data) {
2069 talloc_steal(mem_ctx, getnc_state->la_list);
2071 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2072 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2073 &getnc_state->final_udv);
2075 talloc_free(getnc_state);
2076 b_state->getncchanges_state = NULL;
2077 } else {
2078 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2079 &r->out.ctr->ctr6.new_highwatermark);
2080 if (ret == 0) {
2082 * We need to make sure that we never return the
2083 * same highwatermark within the same replication
2084 * cycle more than once. Otherwise we cannot detect
2085 * when the client uses an unexptected highwatermark.
2087 * This is a HACK which is needed because our
2088 * object ordering is wrong and set tmp_highest_usn
2089 * to a value that is higher than what we already
2090 * sent to the client (destination dsa).
2092 r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2095 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2098 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2099 r->out.ctr->ctr6.uptodateness_vector = NULL;
2100 r->out.ctr->ctr6.nc_object_count = 0;
2101 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2104 DEBUG(r->out.ctr->ctr6.more_data?4:2,
2105 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2106 (unsigned long long)(req10->highwatermark.highest_usn+1),
2107 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2108 r->out.ctr->ctr6.object_count,
2109 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2110 r->out.ctr->ctr6.linked_attributes_count,
2111 link_given, link_total,
2112 dom_sid_string(mem_ctx, user_sid)));
2114 #if 0
2115 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2116 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2118 #endif
2120 return WERR_OK;