s3: Simplify check_reduced_name a bit
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / getncchanges.c
blob07e64d321de4a1539eb6b6cdb2ca79585d9488b6
1 /*
2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "libcli/security/security.h"
31 #include "libcli/security/session.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33 #include "rpc_server/dcerpc_server_proto.h"
34 #include "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
41 build a DsReplicaObjectIdentifier from a ldb msg
43 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
44 struct ldb_message *msg)
46 struct drsuapi_DsReplicaObjectIdentifier *identifier;
47 struct dom_sid *sid;
49 identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
50 if (identifier == NULL) {
51 return NULL;
54 identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
55 identifier->guid = samdb_result_guid(msg, "objectGUID");
57 sid = samdb_result_dom_sid(identifier, msg, "objectSid");
58 if (sid) {
59 identifier->sid = *sid;
60 } else {
61 ZERO_STRUCT(identifier->sid);
63 return identifier;
66 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
68 return GUID_compare(guid1, &guid2);
72 see if we can filter an attribute using the uptodateness_vector
74 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
75 const struct GUID *originating_invocation_id,
76 uint64_t originating_usn)
78 const struct drsuapi_DsReplicaCursor *c;
79 if (udv == NULL) return false;
80 BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
81 originating_invocation_id, udv_compare, c);
82 if (c && originating_usn <= c->highest_usn) {
83 return true;
85 return false;
89 static int attid_cmp(enum drsuapi_DsAttributeId a1, enum drsuapi_DsAttributeId a2)
91 if (a1 == a2) return 0;
92 return ((uint32_t)a1) > ((uint32_t)a2) ? 1 : -1;
96 check if an attribute is in a partial_attribute_set
98 static bool check_partial_attribute_set(const struct dsdb_attribute *sa,
99 struct drsuapi_DsPartialAttributeSet *pas)
101 enum drsuapi_DsAttributeId *result;
102 BINARY_ARRAY_SEARCH_V(pas->attids, pas->num_attids, (enum drsuapi_DsAttributeId)sa->attributeID_id,
103 attid_cmp, result);
104 return result != NULL;
109 drsuapi_DsGetNCChanges for one object
111 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
112 struct ldb_message *msg,
113 struct ldb_context *sam_ctx,
114 struct ldb_dn *ncRoot_dn,
115 bool is_schema_nc,
116 struct dsdb_schema *schema,
117 DATA_BLOB *session_key,
118 uint64_t highest_usn,
119 uint32_t replica_flags,
120 struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
121 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
122 enum drsuapi_DsExtendedOperation extended_op,
123 bool force_object_return)
125 const struct ldb_val *md_value;
126 uint32_t i, n;
127 struct replPropertyMetaDataBlob md;
128 uint32_t rid = 0;
129 enum ndr_err_code ndr_err;
130 uint32_t *attids;
131 const char *rdn;
132 const struct dsdb_attribute *rdn_sa;
133 unsigned int instanceType;
134 struct dsdb_syntax_ctx syntax_ctx;
136 /* make dsdb sytanx context for conversions */
137 dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
138 syntax_ctx.is_schema_nc = is_schema_nc;
140 instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
141 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
142 obj->is_nc_prefix = true;
143 obj->parent_object_guid = NULL;
144 } else {
145 obj->is_nc_prefix = false;
146 obj->parent_object_guid = talloc(obj, struct GUID);
147 if (obj->parent_object_guid == NULL) {
148 return WERR_DS_DRA_INTERNAL_ERROR;
150 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
151 if (GUID_all_zero(obj->parent_object_guid)) {
152 DEBUG(0,(__location__ ": missing parentGUID for %s\n",
153 ldb_dn_get_linearized(msg->dn)));
154 return WERR_DS_DRA_INTERNAL_ERROR;
157 obj->next_object = NULL;
159 md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
160 if (!md_value) {
161 /* nothing to send */
162 return WERR_OK;
165 if (instanceType & INSTANCE_TYPE_UNINSTANT) {
166 /* don't send uninstantiated objects */
167 return WERR_OK;
170 ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
171 (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
172 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
173 return WERR_DS_DRA_INTERNAL_ERROR;
176 if (md.version != 1) {
177 return WERR_DS_DRA_INTERNAL_ERROR;
180 rdn = ldb_dn_get_rdn_name(msg->dn);
181 if (rdn == NULL) {
182 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
183 return WERR_DS_DRA_INTERNAL_ERROR;
186 rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
187 if (rdn_sa == NULL) {
188 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
189 rdn, ldb_dn_get_linearized(msg->dn)));
190 return WERR_DS_DRA_INTERNAL_ERROR;
193 obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
194 attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
196 obj->object.identifier = get_object_identifier(obj, msg);
197 if (obj->object.identifier == NULL) {
198 return WERR_NOMEM;
200 dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
202 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
203 for (n=i=0; i<md.ctr.ctr1.count; i++) {
204 const struct dsdb_attribute *sa;
205 bool force_attribute = false;
207 /* if the attribute has not changed, and it is not the
208 instanceType then don't include it */
209 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
210 extended_op != DRSUAPI_EXOP_REPL_SECRET &&
211 md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
213 /* don't include the rDN */
214 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
216 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
217 if (!sa) {
218 DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
219 (unsigned int)md.ctr.ctr1.array[i].attid,
220 ldb_dn_get_linearized(msg->dn)));
221 return WERR_DS_DRA_INTERNAL_ERROR;
224 if (sa->linkID) {
225 struct ldb_message_element *el;
226 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
227 if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
228 /* don't send upgraded links inline */
229 continue;
233 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
234 !dsdb_attr_in_rodc_fas(sa)) {
235 force_attribute = true;
236 DEBUG(4,("Forcing attribute %s in %s\n",
237 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
240 /* filter by uptodateness_vector */
241 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
242 !force_attribute &&
243 udv_filter(uptodateness_vector,
244 &md.ctr.ctr1.array[i].originating_invocation_id,
245 md.ctr.ctr1.array[i].originating_usn)) {
246 continue;
249 /* filter by partial_attribute_set */
250 if (partial_attribute_set && !check_partial_attribute_set(sa, partial_attribute_set)) {
251 continue;
254 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
255 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
256 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
257 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
258 attids[n] = md.ctr.ctr1.array[i].attid;
259 n++;
262 /* ignore it if its an empty change. Note that renames always
263 * change the 'name' attribute, so they won't be ignored by
264 * this
266 * the force_object_return check is used to force an empty
267 * object return when we timeout in the getncchanges loop.
268 * This allows us to return an empty object, which keeps the
269 * client happy while preventing timeouts
271 if (n == 0 ||
272 (n == 1 &&
273 attids[0] == DRSUAPI_ATTID_instanceType &&
274 !force_object_return)) {
275 talloc_free(obj->meta_data_ctr);
276 obj->meta_data_ctr = NULL;
277 return WERR_OK;
280 obj->meta_data_ctr->count = n;
282 obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
283 obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
284 obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
285 obj->object.attribute_ctr.num_attributes);
286 if (obj->object.attribute_ctr.attributes == NULL) {
287 return WERR_NOMEM;
291 * Note that the meta_data array and the attributes array must
292 * be the same size and in the same order
294 for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
295 struct ldb_message_element *el;
296 WERROR werr;
297 const struct dsdb_attribute *sa;
299 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
300 if (!sa) {
301 DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
302 return WERR_DS_DRA_INTERNAL_ERROR;
305 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
306 if (el == NULL) {
307 /* this happens for attributes that have been removed */
308 DEBUG(5,("No element '%s' for attributeID %u in message\n",
309 sa->lDAPDisplayName, attids[i]));
310 ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
311 obj->object.attribute_ctr.attributes[i].attid =
312 dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
313 } else {
314 werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
315 &obj->object.attribute_ctr.attributes[i]);
316 if (!W_ERROR_IS_OK(werr)) {
317 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
318 sa->lDAPDisplayName, win_errstr(werr)));
319 return werr;
321 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
322 * check if attribute is secret and send a null value
324 if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
325 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
326 &obj->meta_data_ctr->meta_data[i]);
328 /* some attributes needs to be encrypted
329 before being sent */
330 werr = drsuapi_encrypt_attribute(obj, session_key, rid,
331 &obj->object.attribute_ctr.attributes[i]);
332 if (!W_ERROR_IS_OK(werr)) {
333 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
334 sa->lDAPDisplayName, win_errstr(werr)));
335 return werr;
340 return WERR_OK;
344 add one linked attribute from an object to the list of linked
345 attributes in a getncchanges request
347 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
348 struct ldb_context *sam_ctx,
349 const struct dsdb_schema *schema,
350 const struct dsdb_attribute *sa,
351 struct ldb_message *msg,
352 struct dsdb_dn *dsdb_dn,
353 struct drsuapi_DsReplicaLinkedAttribute **la_list,
354 uint32_t *la_count)
356 struct drsuapi_DsReplicaLinkedAttribute *la;
357 bool active;
358 NTSTATUS status;
359 WERROR werr;
361 (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
362 W_ERROR_HAVE_NO_MEMORY(*la_list);
364 la = &(*la_list)[*la_count];
366 la->identifier = get_object_identifier(*la_list, msg);
367 W_ERROR_HAVE_NO_MEMORY(la->identifier);
369 active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
371 if (!active) {
372 /* We have to check that the inactive link still point to an existing object */
373 struct GUID guid;
374 struct ldb_dn *tdn;
375 int ret;
376 const char *v;
378 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
379 if (strncmp(v, "TRUE", 4) == 0) {
381 * Note: we skip the transmition of the deleted link even if the other part used to
382 * know about it because when we transmit the deletion of the object, the link will
383 * be deleted too due to deletion of object where link points and Windows do so.
385 if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
386 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
388 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
389 * if it join an existing domain with deleted objets, it firsts impose to have a
390 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
391 * either during initial replication or after the getNCChanges.
392 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
394 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
395 * If FL >=2K8R2 we are sure that this attribute will be here.
396 * For this kind of forest level we do not return the link if the object is recycled
397 * (isRecycled = true).
399 if (strncmp(v, "TRUE", 4) == 0) {
400 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
401 ldb_dn_get_linearized(msg->dn)));
402 return WERR_OK;
404 } else {
405 return WERR_OK;
408 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
409 if (!NT_STATUS_IS_OK(status)) {
410 DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
411 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
412 return ntstatus_to_werror(status);
414 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, &tdn);
415 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
416 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
417 GUID_string(mem_ctx, &guid)));
418 return WERR_OK;
419 } else if (ret != LDB_SUCCESS) {
420 DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
421 GUID_string(mem_ctx, &guid),
422 ret));
423 return WERR_OK;
426 la->attid = sa->attributeID_id;
427 la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
429 status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
430 if (!NT_STATUS_IS_OK(status)) {
431 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
432 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
433 return ntstatus_to_werror(status);
435 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
436 if (!NT_STATUS_IS_OK(status)) {
437 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
438 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
439 return ntstatus_to_werror(status);
441 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
442 if (!NT_STATUS_IS_OK(status)) {
443 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
444 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
445 return ntstatus_to_werror(status);
447 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
448 if (!NT_STATUS_IS_OK(status)) {
449 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
450 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
451 return ntstatus_to_werror(status);
454 status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
455 if (!NT_STATUS_IS_OK(status)) {
456 /* this is possible for upgraded links */
457 la->originating_add_time = la->meta_data.originating_change_time;
460 werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
461 W_ERROR_NOT_OK_RETURN(werr);
463 (*la_count)++;
464 return WERR_OK;
469 add linked attributes from an object to the list of linked
470 attributes in a getncchanges request
472 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
473 TALLOC_CTX *mem_ctx,
474 struct ldb_dn *ncRoot_dn,
475 struct dsdb_schema *schema,
476 uint64_t highest_usn,
477 uint32_t replica_flags,
478 struct ldb_message *msg,
479 struct drsuapi_DsReplicaLinkedAttribute **la_list,
480 uint32_t *la_count,
481 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
483 unsigned int i;
484 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
485 uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
487 for (i=0; i<msg->num_elements; i++) {
488 struct ldb_message_element *el = &msg->elements[i];
489 const struct dsdb_attribute *sa;
490 unsigned int j;
492 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
494 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
495 /* we only want forward links */
496 continue;
499 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
500 /* its an old style link, it will have been
501 * sent in the main replication data */
502 continue;
505 for (j=0; j<el->num_values; j++) {
506 struct dsdb_dn *dsdb_dn;
507 uint64_t local_usn;
508 NTSTATUS status;
509 WERROR werr;
511 dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
512 if (dsdb_dn == NULL) {
513 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
514 el->name, ldb_dn_get_linearized(msg->dn)));
515 talloc_free(tmp_ctx);
516 return WERR_DS_DRA_INTERNAL_ERROR;
519 status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
520 if (!NT_STATUS_IS_OK(status)) {
521 /* this can happen for attributes
522 given to us with old style meta
523 data */
524 continue;
527 if (local_usn > uSNChanged) {
528 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
529 el->name, ldb_dn_get_linearized(msg->dn)));
530 talloc_free(tmp_ctx);
531 return WERR_DS_DRA_INTERNAL_ERROR;
534 if (local_usn < highest_usn) {
535 continue;
538 werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
539 dsdb_dn, la_list, la_count);
540 if (!W_ERROR_IS_OK(werr)) {
541 talloc_free(tmp_ctx);
542 return werr;
547 talloc_free(tmp_ctx);
548 return WERR_OK;
552 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
554 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
555 struct ldb_dn *ncRoot_dn,
556 struct drsuapi_DsReplicaCursor2CtrEx *udv)
558 int ret;
560 udv->version = 2;
561 udv->reserved1 = 0;
562 udv->reserved2 = 0;
564 ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
565 if (ret != LDB_SUCCESS) {
566 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
567 ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
568 return WERR_DS_DRA_INTERNAL_ERROR;
571 return WERR_OK;
575 /* comparison function for linked attributes - see CompareLinks() in
576 * MS-DRSR section 4.1.10.5.17 */
577 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
578 const struct drsuapi_DsReplicaLinkedAttribute *la2,
579 struct ldb_context *sam_ctx)
581 int c;
582 WERROR werr;
583 TALLOC_CTX *tmp_ctx;
584 const struct dsdb_schema *schema;
585 const struct dsdb_attribute *schema_attrib;
586 struct dsdb_dn *dn1, *dn2;
587 struct GUID guid1, guid2;
588 NTSTATUS status;
590 c = GUID_compare(&la1->identifier->guid,
591 &la2->identifier->guid);
592 if (c != 0) return c;
594 if (la1->attid != la2->attid) {
595 return la1->attid < la2->attid? -1:1;
598 if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
599 (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
600 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
603 /* we need to get the target GUIDs to compare */
604 tmp_ctx = talloc_new(sam_ctx);
606 schema = dsdb_get_schema(sam_ctx, tmp_ctx);
607 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
609 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
610 if (!W_ERROR_IS_OK(werr)) {
611 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
612 talloc_free(tmp_ctx);
613 return 0;
616 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
617 if (!W_ERROR_IS_OK(werr)) {
618 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
619 talloc_free(tmp_ctx);
620 return 0;
623 status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
624 if (!NT_STATUS_IS_OK(status)) {
625 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
626 talloc_free(tmp_ctx);
627 return 0;
629 status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
630 if (!NT_STATUS_IS_OK(status)) {
631 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
632 talloc_free(tmp_ctx);
633 return 0;
636 talloc_free(tmp_ctx);
638 return GUID_compare(&guid1, &guid2);
641 struct drsuapi_changed_objects {
642 struct ldb_dn *dn;
643 struct GUID guid;
644 uint64_t usn;
648 sort the objects we send by tree order
650 static int site_res_cmp_parent_order(struct drsuapi_changed_objects *m1,
651 struct drsuapi_changed_objects *m2)
653 return ldb_dn_compare(m2->dn, m1->dn);
657 sort the objects we send first by uSNChanged
659 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects *m1,
660 struct drsuapi_changed_objects *m2)
662 unsigned usnchanged1, usnchanged2;
663 unsigned cn1, cn2;
665 cn1 = ldb_dn_get_comp_num(m1->dn);
666 cn2 = ldb_dn_get_comp_num(m2->dn);
667 if (cn1 != cn2) {
668 return cn1 > cn2 ? 1 : -1;
670 usnchanged1 = m1->usn;
671 usnchanged2 = m2->usn;
672 if (usnchanged1 == usnchanged2) {
673 return 0;
675 return usnchanged1 > usnchanged2 ? 1 : -1;
680 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
682 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
683 TALLOC_CTX *mem_ctx,
684 struct drsuapi_DsGetNCChangesRequest10 *req10,
685 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
687 struct ldb_dn *rid_manager_dn, *fsmo_role_dn, *req_dn;
688 int ret;
689 struct ldb_context *ldb = b_state->sam_ctx;
690 struct ldb_result *ext_res;
691 struct ldb_dn *base_dn;
692 struct dsdb_fsmo_extended_op *exop;
695 steps:
696 - verify that the DN being asked for is the RID Manager DN
697 - verify that we are the RID Manager
700 /* work out who is the RID Manager */
701 ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
702 if (ret != LDB_SUCCESS) {
703 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
704 return WERR_DS_DRA_INTERNAL_ERROR;
707 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
708 if (!ldb_dn_validate(req_dn) ||
709 ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
710 /* that isn't the RID Manager DN */
711 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
712 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
713 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
714 return WERR_OK;
717 /* find the DN of the RID Manager */
718 ret = samdb_reference_dn(ldb, mem_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
719 if (ret != LDB_SUCCESS) {
720 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s\n",
721 ldb_errstring(ldb)));
722 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
723 return WERR_DS_DRA_INTERNAL_ERROR;
726 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
727 /* we're not the RID Manager - go away */
728 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
729 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
730 return WERR_OK;
733 exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
734 W_ERROR_HAVE_NO_MEMORY(exop);
736 exop->fsmo_info = req10->fsmo_info;
737 exop->destination_dsa_guid = req10->destination_dsa_guid;
739 ret = ldb_transaction_start(ldb);
740 if (ret != LDB_SUCCESS) {
741 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
742 ldb_errstring(ldb)));
743 return WERR_DS_DRA_INTERNAL_ERROR;
747 * FIXME (kim): this is a temp hack to return just few object,
748 * but not the whole domain NC.
749 * We should remove this hack and implement a 'scope'
750 * building function to return just the set of object
751 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
753 ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req10->highwatermark.highest_usn);
755 ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
756 if (ret != LDB_SUCCESS) {
757 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
758 ldb_errstring(ldb)));
759 ldb_transaction_cancel(ldb);
760 return WERR_DS_DRA_INTERNAL_ERROR;
763 ret = ldb_transaction_commit(ldb);
764 if (ret != LDB_SUCCESS) {
765 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
766 ldb_errstring(ldb)));
767 return WERR_DS_DRA_INTERNAL_ERROR;
770 talloc_free(ext_res);
772 base_dn = ldb_get_default_basedn(ldb);
774 DEBUG(2,("Allocated RID pool for server %s\n",
775 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
777 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
779 return WERR_OK;
783 return an array of SIDs from a ldb_message given an attribute name
784 assumes the SIDs are in extended DN format
786 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
787 struct ldb_message *msg,
788 TALLOC_CTX *mem_ctx,
789 const char *attr,
790 const struct dom_sid ***sids)
792 struct ldb_message_element *el;
793 unsigned int i;
795 el = ldb_msg_find_element(msg, attr);
796 if (!el) {
797 *sids = NULL;
798 return WERR_OK;
801 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
802 W_ERROR_HAVE_NO_MEMORY(*sids);
804 for (i=0; i<el->num_values; i++) {
805 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
806 NTSTATUS status;
807 struct dom_sid *sid;
809 sid = talloc(*sids, struct dom_sid);
810 W_ERROR_HAVE_NO_MEMORY(sid);
811 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
812 if (!NT_STATUS_IS_OK(status)) {
813 return WERR_INTERNAL_DB_CORRUPTION;
815 (*sids)[i] = sid;
817 (*sids)[i] = NULL;
819 return WERR_OK;
824 return an array of SIDs from a ldb_message given an attribute name
825 assumes the SIDs are in NDR form
827 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
828 struct ldb_message *msg,
829 TALLOC_CTX *mem_ctx,
830 const char *attr,
831 const struct dom_sid ***sids)
833 struct ldb_message_element *el;
834 unsigned int i;
836 el = ldb_msg_find_element(msg, attr);
837 if (!el) {
838 *sids = NULL;
839 return WERR_OK;
842 (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
843 W_ERROR_HAVE_NO_MEMORY(*sids);
845 for (i=0; i<el->num_values; i++) {
846 enum ndr_err_code ndr_err;
847 struct dom_sid *sid;
849 sid = talloc(*sids, struct dom_sid);
850 W_ERROR_HAVE_NO_MEMORY(sid);
852 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
853 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
854 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
855 return WERR_INTERNAL_DB_CORRUPTION;
857 (*sids)[i] = sid;
859 (*sids)[i] = NULL;
861 return WERR_OK;
865 see if any SIDs in list1 are in list2
867 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
869 unsigned int i, j;
870 /* do we ever have enough SIDs here to worry about O(n^2) ? */
871 for (i=0; list1[i]; i++) {
872 for (j=0; list2[j]; j++) {
873 if (dom_sid_equal(list1[i], list2[j])) {
874 return true;
878 return false;
882 handle a DRSUAPI_EXOP_REPL_SECRET call
884 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
885 TALLOC_CTX *mem_ctx,
886 struct drsuapi_DsGetNCChangesRequest10 *req10,
887 struct dom_sid *user_sid,
888 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
890 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
891 struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
892 int ret;
893 const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
894 const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
895 struct ldb_result *rodc_res, *obj_res;
896 const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
897 WERROR werr;
899 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
900 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
903 * we need to work out if we will allow this RODC to
904 * replicate the secrets for this object
906 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
907 * of this function
910 if (b_state->sam_ctx_system == NULL) {
911 /* this operation needs system level access */
912 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
913 return WERR_DS_DRA_SOURCE_DISABLED;
916 obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
917 if (!ldb_dn_validate(obj_dn)) goto failed;
919 rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
920 dom_sid_string(mem_ctx, user_sid));
921 if (!ldb_dn_validate(rodc_dn)) goto failed;
923 /* do the two searches we need */
924 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
925 DSDB_SEARCH_SHOW_EXTENDED_DN);
926 if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
928 ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
929 if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
931 /* if the object SID is equal to the user_sid, allow */
932 if (dom_sid_equal(user_sid,
933 samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
934 goto allowed;
937 /* an RODC is allowed to get its own krbtgt account secrets */
938 krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
939 rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
940 if (krbtgt_link_dn != NULL &&
941 ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
942 goto allowed;
945 /* but it isn't allowed to get anyone elses krbtgt secrets */
946 if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
947 obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
948 goto denied;
951 if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
952 "userAccountControl", 0) &
953 UF_INTERDOMAIN_TRUST_ACCOUNT) {
954 goto denied;
957 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
958 mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
959 if (!W_ERROR_IS_OK(werr)) {
960 goto denied;
963 werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
964 mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
965 if (!W_ERROR_IS_OK(werr)) {
966 goto denied;
969 werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
970 mem_ctx, "tokenGroups", &token_sids);
971 if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
972 goto denied;
975 if (never_reveal_sids &&
976 sid_list_match(token_sids, never_reveal_sids)) {
977 goto denied;
980 if (reveal_sids &&
981 sid_list_match(token_sids, reveal_sids)) {
982 goto allowed;
985 /* default deny */
986 denied:
987 DEBUG(2,(__location__ ": Denied RODC secret replication for %s by RODC %s\n",
988 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
989 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
990 return WERR_DS_DRA_ACCESS_DENIED;
992 allowed:
993 DEBUG(2,(__location__ ": Allowed RODC secret replication for %s by RODC %s\n",
994 ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
995 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
996 req10->highwatermark.highest_usn = 0;
997 return WERR_OK;
999 failed:
1000 DEBUG(2,(__location__ ": Failed RODC secret replication for %s by RODC %s\n",
1001 ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1002 ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1003 return WERR_DS_DRA_BAD_DN;
1008 handle a DRSUAPI_EXOP_REPL_OBJ call
1010 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1011 TALLOC_CTX *mem_ctx,
1012 struct drsuapi_DsGetNCChangesRequest10 *req10,
1013 struct dom_sid *user_sid,
1014 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1016 struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1018 DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1019 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1021 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1022 req10->highwatermark.highest_usn = 0;
1023 return WERR_OK;
1028 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1029 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1030 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1032 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1033 TALLOC_CTX *mem_ctx,
1034 struct drsuapi_DsGetNCChangesRequest10 *req10,
1035 struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1037 struct ldb_dn *fsmo_role_dn, *req_dn, *ntds_dn;
1038 int ret;
1039 unsigned int i;
1040 struct ldb_context *ldb = b_state->sam_ctx;
1041 struct ldb_message *msg;
1044 steps:
1045 - verify that the client dn exists
1046 - verify that we are the current master
1049 req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1050 if (!ldb_dn_validate(req_dn)) {
1051 /* that is not a valid dn */
1052 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1053 drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1054 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1055 return WERR_OK;
1058 /* retrieve the current role owner */
1059 ret = samdb_reference_dn(ldb, mem_ctx, req_dn, "fSMORoleOwner", &fsmo_role_dn);
1060 if (ret != LDB_SUCCESS) {
1061 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in context - %s\n",
1062 ldb_errstring(ldb)));
1063 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1064 return WERR_DS_DRA_INTERNAL_ERROR;
1067 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
1068 /* we're not the current owner - go away */
1069 DEBUG(0,(__location__ ": FSMO transfer request when not owner\n"));
1070 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1071 return WERR_OK;
1074 /* change the current master */
1075 msg = ldb_msg_new(ldb);
1076 W_ERROR_HAVE_NO_MEMORY(msg);
1077 msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1078 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1080 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1081 ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, &ntds_dn);
1082 if (ret != LDB_SUCCESS) {
1083 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1084 GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1085 talloc_free(msg);
1086 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1087 return WERR_OK;
1090 ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1091 if (ret != 0) {
1092 talloc_free(msg);
1093 return WERR_DS_DRA_INTERNAL_ERROR;
1096 for (i=0;i<msg->num_elements;i++) {
1097 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1100 ret = ldb_transaction_start(ldb);
1101 if (ret != LDB_SUCCESS) {
1102 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1103 ldb_errstring(ldb)));
1104 return WERR_DS_DRA_INTERNAL_ERROR;
1107 ret = ldb_modify(ldb, msg);
1108 if (ret != LDB_SUCCESS) {
1109 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1110 ldb_errstring(ldb)));
1111 ldb_transaction_cancel(ldb);
1112 return WERR_DS_DRA_INTERNAL_ERROR;
1115 ret = ldb_transaction_commit(ldb);
1116 if (ret != LDB_SUCCESS) {
1117 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1118 ldb_errstring(ldb)));
1119 return WERR_DS_DRA_INTERNAL_ERROR;
1122 ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1124 return WERR_OK;
1127 /* state of a partially completed getncchanges call */
1128 struct drsuapi_getncchanges_state {
1129 struct GUID *guids;
1130 uint32_t num_records;
1131 uint32_t num_processed;
1132 struct ldb_dn *ncRoot_dn;
1133 bool is_schema_nc;
1134 uint64_t min_usn;
1135 uint64_t highest_usn;
1136 struct ldb_dn *last_dn;
1137 struct drsuapi_DsReplicaLinkedAttribute *la_list;
1138 uint32_t la_count;
1139 bool la_sorted;
1140 uint32_t la_idx;
1141 struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
1145 see if this getncchanges request includes a request to reveal secret information
1147 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1148 struct drsuapi_DsGetNCChangesRequest10 *req10,
1149 bool *is_secret_request)
1151 enum drsuapi_DsExtendedOperation exop;
1152 uint32_t i;
1153 struct dsdb_schema *schema;
1155 *is_secret_request = true;
1157 exop = req10->extended_op;
1159 switch (exop) {
1160 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1161 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1162 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1163 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1164 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1165 /* FSMO exops can reveal secrets */
1166 *is_secret_request = true;
1167 return WERR_OK;
1168 case DRSUAPI_EXOP_REPL_SECRET:
1169 case DRSUAPI_EXOP_REPL_OBJ:
1170 case DRSUAPI_EXOP_NONE:
1171 break;
1174 if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1175 *is_secret_request = false;
1176 return WERR_OK;
1179 if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1180 req10->partial_attribute_set == NULL) {
1181 /* they want secrets */
1182 *is_secret_request = true;
1183 return WERR_OK;
1186 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1188 /* check the attributes they asked for */
1189 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1190 const struct dsdb_attribute *sa;
1191 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1192 if (sa == NULL) {
1193 return WERR_DS_DRA_SCHEMA_MISMATCH;
1195 if (!dsdb_attr_in_rodc_fas(sa)) {
1196 *is_secret_request = true;
1197 return WERR_OK;
1201 if (req10->partial_attribute_set_ex) {
1202 /* check the extended attributes they asked for */
1203 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1204 const struct dsdb_attribute *sa;
1205 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1206 if (sa == NULL) {
1207 return WERR_DS_DRA_SCHEMA_MISMATCH;
1209 if (!dsdb_attr_in_rodc_fas(sa)) {
1210 *is_secret_request = true;
1211 return WERR_OK;
1216 *is_secret_request = false;
1217 return WERR_OK;
1221 see if this getncchanges request is only for attributes in the GC
1222 partial attribute set
1224 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1225 struct drsuapi_DsGetNCChangesRequest10 *req10,
1226 bool *is_gc_pas_request)
1228 enum drsuapi_DsExtendedOperation exop;
1229 uint32_t i;
1230 struct dsdb_schema *schema;
1232 exop = req10->extended_op;
1234 switch (exop) {
1235 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1236 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1237 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1238 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1239 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1240 case DRSUAPI_EXOP_REPL_SECRET:
1241 *is_gc_pas_request = false;
1242 return WERR_OK;
1243 case DRSUAPI_EXOP_REPL_OBJ:
1244 case DRSUAPI_EXOP_NONE:
1245 break;
1248 if (req10->partial_attribute_set == NULL) {
1249 /* they want it all */
1250 *is_gc_pas_request = false;
1251 return WERR_OK;
1254 schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1256 /* check the attributes they asked for */
1257 for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1258 const struct dsdb_attribute *sa;
1259 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set->attids[i]);
1260 if (sa == NULL) {
1261 return WERR_DS_DRA_SCHEMA_MISMATCH;
1263 if (!sa->isMemberOfPartialAttributeSet) {
1264 *is_gc_pas_request = false;
1265 return WERR_OK;
1269 if (req10->partial_attribute_set_ex) {
1270 /* check the extended attributes they asked for */
1271 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1272 const struct dsdb_attribute *sa;
1273 sa = dsdb_attribute_by_attributeID_id(schema, req10->partial_attribute_set_ex->attids[i]);
1274 if (sa == NULL) {
1275 return WERR_DS_DRA_SCHEMA_MISMATCH;
1277 if (!sa->isMemberOfPartialAttributeSet) {
1278 *is_gc_pas_request = false;
1279 return WERR_OK;
1284 *is_gc_pas_request = true;
1285 return WERR_OK;
1290 map from req8 to req10
1292 static struct drsuapi_DsGetNCChangesRequest10 *
1293 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1294 struct drsuapi_DsGetNCChangesRequest8 *req8)
1296 struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1297 struct drsuapi_DsGetNCChangesRequest10);
1298 if (req10 == NULL) {
1299 return NULL;
1302 req10->destination_dsa_guid = req8->destination_dsa_guid;
1303 req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1304 req10->naming_context = req8->naming_context;
1305 req10->highwatermark = req8->highwatermark;
1306 req10->uptodateness_vector = req8->uptodateness_vector;
1307 req10->replica_flags = req8->replica_flags;
1308 req10->max_object_count = req8->max_object_count;
1309 req10->max_ndr_size = req8->max_ndr_size;
1310 req10->extended_op = req8->extended_op;
1311 req10->fsmo_info = req8->fsmo_info;
1312 req10->partial_attribute_set = req8->partial_attribute_set;
1313 req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1314 req10->mapping_ctr = req8->mapping_ctr;
1316 return req10;
1321 * Collects object for normal replication cycle.
1323 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1324 TALLOC_CTX *mem_ctx,
1325 struct drsuapi_DsGetNCChangesRequest10 *req10,
1326 struct ldb_dn *search_dn,
1327 const char *extra_filter,
1328 struct ldb_result **search_res)
1330 int ret;
1331 char* search_filter;
1332 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1333 //const char *extra_filter;
1334 struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1335 const char *attrs[] = { "uSNChanged",
1336 "objectGUID" ,
1337 NULL };
1339 if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1340 req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1341 scope = LDB_SCOPE_BASE;
1344 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1346 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1348 /* Construct response. */
1349 search_filter = talloc_asprintf(mem_ctx,
1350 "(uSNChanged>=%llu)",
1351 (unsigned long long)(getnc_state->min_usn+1));
1353 if (extra_filter) {
1354 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1357 if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1358 search_filter = talloc_asprintf(mem_ctx,
1359 "(&%s(isCriticalSystemObject=TRUE))",
1360 search_filter);
1363 if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1364 scope = LDB_SCOPE_BASE;
1367 if (!search_dn) {
1368 search_dn = getnc_state->ncRoot_dn;
1371 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1372 ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1373 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1374 search_dn, scope, attrs,
1375 search_filter);
1376 if (ret != LDB_SUCCESS) {
1377 return WERR_DS_DRA_INTERNAL_ERROR;
1380 return WERR_OK;
1384 * Collects object for normal replication cycle.
1386 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1387 TALLOC_CTX *mem_ctx,
1388 struct drsuapi_DsGetNCChangesRequest10 *req10,
1389 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1390 struct ldb_dn *search_dn,
1391 const char *extra_filter,
1392 struct ldb_result **search_res)
1394 /* we have nothing to do in case of ex-op failure */
1395 if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1396 return WERR_OK;
1399 /* TODO: implement extended op specific collection
1400 * of objects. Right now we just normal procedure
1401 * for collecting objects */
1402 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1406 drsuapi_DsGetNCChanges
1408 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1410 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1411 struct drsuapi_DsGetNCChanges *r)
1413 struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1414 int ret;
1415 uint32_t i;
1416 struct dsdb_schema *schema;
1417 struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1418 struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1419 NTSTATUS status;
1420 DATA_BLOB session_key;
1421 WERROR werr;
1422 struct dcesrv_handle *h;
1423 struct drsuapi_bind_state *b_state;
1424 struct drsuapi_getncchanges_state *getnc_state;
1425 struct drsuapi_DsGetNCChangesRequest10 *req10;
1426 uint32_t options;
1427 uint32_t max_objects;
1428 uint32_t max_links;
1429 uint32_t link_count = 0;
1430 uint32_t link_total = 0;
1431 uint32_t link_given = 0;
1432 struct ldb_dn *search_dn = NULL;
1433 bool am_rodc, null_scope=false;
1434 enum security_user_level security_level;
1435 struct ldb_context *sam_ctx;
1436 struct dom_sid *user_sid;
1437 bool is_secret_request;
1438 bool is_gc_pas_request;
1439 struct drsuapi_changed_objects *changes;
1440 time_t max_wait;
1441 time_t start = time(NULL);
1442 bool max_wait_reached = false;
1444 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1445 b_state = h->data;
1447 sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1449 *r->out.level_out = 6;
1450 /* TODO: linked attributes*/
1451 r->out.ctr->ctr6.linked_attributes_count = 0;
1452 r->out.ctr->ctr6.linked_attributes = NULL;
1454 r->out.ctr->ctr6.object_count = 0;
1455 r->out.ctr->ctr6.nc_object_count = 0;
1456 r->out.ctr->ctr6.more_data = false;
1457 r->out.ctr->ctr6.uptodateness_vector = NULL;
1459 /* a RODC doesn't allow for any replication */
1460 ret = samdb_rodc(sam_ctx, &am_rodc);
1461 if (ret == LDB_SUCCESS && am_rodc) {
1462 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1463 return WERR_DS_DRA_SOURCE_DISABLED;
1466 /* Check request revision.
1468 switch (r->in.level) {
1469 case 8:
1470 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1471 if (req10 == NULL) {
1472 return WERR_NOMEM;
1474 break;
1475 case 10:
1476 req10 = &r->in.req->req10;
1477 break;
1478 default:
1479 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1480 r->in.level));
1481 return WERR_REVISION_MISMATCH;
1485 /* Perform access checks. */
1486 /* TODO: we need to support a sync on a specific non-root
1487 * DN. We'll need to find the real partition root here */
1488 ncRoot = req10->naming_context;
1489 if (ncRoot == NULL) {
1490 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1491 return WERR_DS_DRA_INVALID_PARAMETER;
1494 if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1495 return WERR_DS_DRA_INTERNAL_ERROR;
1498 if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1499 !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1500 return WERR_DS_DRA_SOURCE_DISABLED;
1503 user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1505 /* all clients must have GUID_DRS_GET_CHANGES */
1506 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1507 mem_ctx,
1508 dce_call->conn->auth_state.session_info->security_token,
1509 req10->naming_context,
1510 GUID_DRS_GET_CHANGES);
1511 if (!W_ERROR_IS_OK(werr)) {
1512 return werr;
1515 /* allowed if the GC PAS and client has
1516 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1517 werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, &is_gc_pas_request);
1518 if (!W_ERROR_IS_OK(werr)) {
1519 return werr;
1521 if (is_gc_pas_request) {
1522 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1523 mem_ctx,
1524 dce_call->conn->auth_state.session_info->security_token,
1525 req10->naming_context,
1526 GUID_DRS_GET_FILTERED_ATTRIBUTES);
1527 if (W_ERROR_IS_OK(werr)) {
1528 goto allowed;
1532 werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10, &is_secret_request);
1533 if (!W_ERROR_IS_OK(werr)) {
1534 return werr;
1536 if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1537 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1538 mem_ctx,
1539 dce_call->conn->auth_state.session_info->security_token,
1540 req10->naming_context,
1541 GUID_DRS_GET_ALL_CHANGES);
1542 if (!W_ERROR_IS_OK(werr)) {
1543 return werr;
1547 allowed:
1548 /* for non-administrator replications, check that they have
1549 given the correct source_dsa_invocation_id */
1550 security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1551 samdb_domain_sid(sam_ctx));
1552 if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1553 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1554 /* we rely on this flag being unset for RODC requests */
1555 req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1559 if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1560 /* Ignore the _in_ uptpdateness vector*/
1561 req10->uptodateness_vector = NULL;
1564 getnc_state = b_state->getncchanges_state;
1566 /* see if a previous replication has been abandoned */
1567 if (getnc_state) {
1568 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1569 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1570 DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1571 ldb_dn_get_linearized(new_dn),
1572 ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1573 ldb_dn_get_linearized(getnc_state->last_dn)));
1574 talloc_free(getnc_state);
1575 getnc_state = NULL;
1579 if (getnc_state == NULL) {
1580 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1581 if (getnc_state == NULL) {
1582 return WERR_NOMEM;
1584 b_state->getncchanges_state = getnc_state;
1585 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1587 /* find out if we are to replicate Schema NC */
1588 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1589 ldb_get_schema_basedn(b_state->sam_ctx));
1590 getnc_state->is_schema_nc = (0 == ret);
1592 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1593 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1597 * This is the first replication cycle and it is
1598 * a good place to handle extended operations
1600 * FIXME: we don't fully support extended operations yet
1602 switch (req10->extended_op) {
1603 case DRSUAPI_EXOP_NONE:
1604 break;
1605 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1606 werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1607 W_ERROR_NOT_OK_RETURN(werr);
1608 search_dn = ldb_get_default_basedn(sam_ctx);
1609 break;
1610 case DRSUAPI_EXOP_REPL_SECRET:
1611 werr = getncchanges_repl_secret(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1612 r->out.result = werr;
1613 W_ERROR_NOT_OK_RETURN(werr);
1614 break;
1615 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1616 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1617 W_ERROR_NOT_OK_RETURN(werr);
1618 break;
1619 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1620 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1621 W_ERROR_NOT_OK_RETURN(werr);
1622 break;
1623 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1624 werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1625 W_ERROR_NOT_OK_RETURN(werr);
1626 break;
1627 case DRSUAPI_EXOP_REPL_OBJ:
1628 werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1629 r->out.result = werr;
1630 W_ERROR_NOT_OK_RETURN(werr);
1631 break;
1633 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1635 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1636 (unsigned)req10->extended_op));
1637 return WERR_DS_DRA_NOT_SUPPORTED;
1641 if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1642 ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1643 DEBUG(0,(__location__ ": Bad DN '%s'\n",
1644 drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1645 return WERR_DS_DRA_INVALID_PARAMETER;
1648 /* we need the session key for encrypting password attributes */
1649 status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1650 if (!NT_STATUS_IS_OK(status)) {
1651 DEBUG(0,(__location__ ": Failed to get session key\n"));
1652 return WERR_DS_DRA_INTERNAL_ERROR;
1656 TODO: MS-DRSR section 4.1.10.1.1
1657 Work out if this is the start of a new cycle */
1659 if (getnc_state->guids == NULL) {
1660 const char *extra_filter;
1661 struct ldb_result *search_res = NULL;
1663 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1665 getnc_state->min_usn = req10->highwatermark.highest_usn;
1667 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
1668 werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
1669 search_dn, extra_filter,
1670 &search_res);
1671 } else {
1672 werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
1673 &r->out.ctr->ctr6,
1674 search_dn, extra_filter,
1675 &search_res);
1677 W_ERROR_NOT_OK_RETURN(werr);
1679 /* extract out the GUIDs list */
1680 getnc_state->num_records = search_res ? search_res->count : 0;
1681 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
1682 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
1684 changes = talloc_array(getnc_state,
1685 struct drsuapi_changed_objects,
1686 getnc_state->num_records);
1687 W_ERROR_HAVE_NO_MEMORY(changes);
1689 for (i=0; i<getnc_state->num_records; i++) {
1690 changes[i].dn = search_res->msgs[i]->dn;
1691 changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
1692 changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
1695 if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
1696 TYPESAFE_QSORT(changes,
1697 getnc_state->num_records,
1698 site_res_cmp_parent_order);
1699 } else {
1700 TYPESAFE_QSORT(changes,
1701 getnc_state->num_records,
1702 site_res_cmp_dn_usn_order);
1705 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req10->uptodateness_vector);
1706 if (getnc_state->uptodateness_vector) {
1707 /* make sure its sorted */
1708 TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1709 getnc_state->uptodateness_vector->count,
1710 drsuapi_DsReplicaCursor_compare);
1713 for (i=0; i < getnc_state->num_records; i++) {
1714 getnc_state->guids[i] = changes[i].guid;
1715 if (GUID_all_zero(&getnc_state->guids[i])) {
1716 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1717 ldb_dn_get_linearized(search_res->msgs[i]->dn)));
1718 return WERR_DS_DRA_INTERNAL_ERROR;
1722 talloc_free(search_res);
1723 talloc_free(changes);
1726 /* Prefix mapping */
1727 schema = dsdb_get_schema(sam_ctx, mem_ctx);
1728 if (!schema) {
1729 DEBUG(0,("No schema in sam_ctx\n"));
1730 return WERR_DS_DRA_INTERNAL_ERROR;
1733 r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1734 *r->out.ctr->ctr6.naming_context = *ncRoot;
1736 if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1737 &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1738 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1739 ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1740 return WERR_DS_DRA_INTERNAL_ERROR;
1743 /* find the SID if there is one */
1744 dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1746 dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1747 r->out.ctr->ctr6.mapping_ctr = *ctr;
1749 r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1750 r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1752 r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
1753 r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
1755 r->out.ctr->ctr6.first_object = NULL;
1756 currentObject = &r->out.ctr->ctr6.first_object;
1758 /* use this to force single objects at a time, which is useful
1759 * for working out what object is giving problems
1761 max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1762 if (req10->max_object_count < max_objects) {
1763 max_objects = req10->max_object_count;
1766 * TODO: work out how the maximum should be calculated
1768 max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1771 * Maximum time that we can spend in a getncchanges
1772 * in order to avoid timeout of the other part.
1773 * 10 seconds by default.
1775 max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
1776 for (i=getnc_state->num_processed;
1777 i<getnc_state->num_records &&
1778 !null_scope &&
1779 (r->out.ctr->ctr6.object_count < max_objects)
1780 && !max_wait_reached;
1781 i++) {
1782 int uSN;
1783 struct drsuapi_DsReplicaObjectListItemEx *obj;
1784 struct ldb_message *msg;
1785 static const char * const msg_attrs[] = {
1786 "*",
1787 "nTSecurityDescriptor",
1788 "parentGUID",
1789 "replPropertyMetaData",
1790 DSDB_SECRET_ATTRIBUTES,
1791 NULL };
1792 struct ldb_result *msg_res;
1793 struct ldb_dn *msg_dn;
1795 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1796 W_ERROR_HAVE_NO_MEMORY(obj);
1798 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
1799 W_ERROR_HAVE_NO_MEMORY(msg_dn);
1802 /* by re-searching here we avoid having a lot of full
1803 * records in memory between calls to getncchanges
1805 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
1806 msg_dn,
1807 LDB_SCOPE_BASE, msg_attrs, NULL);
1808 if (ret != LDB_SUCCESS) {
1809 if (ret != LDB_ERR_NO_SUCH_OBJECT) {
1810 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1811 ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
1813 talloc_free(obj);
1814 continue;
1817 msg = msg_res->msgs[0];
1819 max_wait_reached = (time(NULL) - start > max_wait);
1821 werr = get_nc_changes_build_object(obj, msg,
1822 sam_ctx, getnc_state->ncRoot_dn,
1823 getnc_state->is_schema_nc,
1824 schema, &session_key, getnc_state->min_usn,
1825 req10->replica_flags,
1826 req10->partial_attribute_set,
1827 getnc_state->uptodateness_vector,
1828 req10->extended_op,
1829 max_wait_reached);
1830 if (!W_ERROR_IS_OK(werr)) {
1831 return werr;
1834 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1835 getnc_state->ncRoot_dn,
1836 schema, getnc_state->min_usn,
1837 req10->replica_flags,
1838 msg,
1839 &getnc_state->la_list,
1840 &getnc_state->la_count,
1841 getnc_state->uptodateness_vector);
1842 if (!W_ERROR_IS_OK(werr)) {
1843 return werr;
1846 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1847 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1848 r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1850 if (uSN > getnc_state->highest_usn) {
1851 getnc_state->highest_usn = uSN;
1854 if (obj->meta_data_ctr == NULL) {
1855 DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1856 ldb_dn_get_linearized(msg->dn)));
1857 /* no attributes to send */
1858 talloc_free(obj);
1859 continue;
1862 r->out.ctr->ctr6.object_count++;
1864 *currentObject = obj;
1865 currentObject = &obj->next_object;
1867 talloc_free(getnc_state->last_dn);
1868 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1870 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1872 talloc_free(msg_res);
1873 talloc_free(msg_dn);
1876 getnc_state->num_processed = i;
1878 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
1880 /* the client can us to call UpdateRefs on its behalf to
1881 re-establish monitoring of the NC */
1882 if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1883 !GUID_all_zero(&req10->destination_dsa_guid)) {
1884 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1885 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1886 GUID_string(mem_ctx, &req10->destination_dsa_guid)));
1887 ureq.naming_context = ncRoot;
1888 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
1889 &req10->destination_dsa_guid);
1890 if (!ureq.dest_dsa_dns_name) {
1891 return WERR_NOMEM;
1893 ureq.dest_dsa_guid = req10->destination_dsa_guid;
1894 ureq.options = DRSUAPI_DRS_ADD_REF |
1895 DRSUAPI_DRS_ASYNC_OP |
1896 DRSUAPI_DRS_GETCHG_CHECK;
1898 /* we also need to pass through the
1899 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1900 to send notifies using the GC SPN */
1901 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
1903 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1904 if (!W_ERROR_IS_OK(werr)) {
1905 DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1906 win_errstr(werr)));
1911 * TODO:
1912 * This is just a guess, how to calculate the
1913 * number of linked attributes to send, we need to
1914 * find out how to do this right.
1916 if (r->out.ctr->ctr6.object_count >= max_links) {
1917 max_links = 0;
1918 } else {
1919 max_links -= r->out.ctr->ctr6.object_count;
1922 link_total = getnc_state->la_count;
1924 if (i < getnc_state->num_records) {
1925 r->out.ctr->ctr6.more_data = true;
1926 } else {
1927 /* sort the whole array the first time */
1928 if (!getnc_state->la_sorted) {
1929 LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1930 sam_ctx, linked_attribute_compare);
1931 getnc_state->la_sorted = true;
1934 link_count = getnc_state->la_count - getnc_state->la_idx;
1935 link_count = MIN(max_links, link_count);
1937 r->out.ctr->ctr6.linked_attributes_count = link_count;
1938 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1940 getnc_state->la_idx += link_count;
1941 link_given = getnc_state->la_idx;
1943 if (getnc_state->la_idx < getnc_state->la_count) {
1944 r->out.ctr->ctr6.more_data = true;
1948 if (!r->out.ctr->ctr6.more_data) {
1949 talloc_steal(mem_ctx, getnc_state->la_list);
1951 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1952 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1954 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1955 r->out.ctr->ctr6.uptodateness_vector);
1956 if (!W_ERROR_IS_OK(werr)) {
1957 return werr;
1960 talloc_free(getnc_state);
1961 b_state->getncchanges_state = NULL;
1964 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1965 r->out.ctr->ctr6.uptodateness_vector = NULL;
1966 r->out.ctr->ctr6.nc_object_count = 0;
1967 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1970 DEBUG(r->out.ctr->ctr6.more_data?4:2,
1971 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1972 (unsigned long long)(req10->highwatermark.highest_usn+1),
1973 req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
1974 r->out.ctr->ctr6.object_count,
1975 i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
1976 r->out.ctr->ctr6.linked_attributes_count,
1977 link_given, link_total,
1978 dom_sid_string(mem_ctx, user_sid)));
1980 #if 0
1981 if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
1982 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
1984 #endif
1986 return WERR_OK;