getncchanges: Let security of RWDC+ manually replicate secrets to RODCs
[nivanova/samba-autobuild/.git] / source4 / rpc_server / drsuapi / getncchanges.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implement the DSGetNCChanges call
5
6    Copyright (C) Anatoliy Atanasov 2009
7    Copyright (C) Andrew Tridgell 2009-2010
8    Copyright (C) Andrew Bartlett 2010-2016
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "param/param.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "libcli/security/security.h"
32 #include "libcli/security/session.h"
33 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
34 #include "rpc_server/dcerpc_server_proto.h"
35 #include "../libcli/drsuapi/drsuapi.h"
36 #include "lib/util/binsearch.h"
37 #include "lib/util/tsort.h"
38 #include "auth/session.h"
39 #include "dsdb/common/util.h"
40 #include "lib/dbwrap/dbwrap.h"
41 #include "lib/dbwrap/dbwrap_rbt.h"
42 #include "librpc/gen_ndr/ndr_misc.h"
43
44 /* state of a partially completed getncchanges call */
45 struct drsuapi_getncchanges_state {
46         struct db_context *anc_cache;
47         struct GUID *guids;
48         uint32_t num_records;
49         uint32_t num_processed;
50         struct ldb_dn *ncRoot_dn;
51         struct GUID ncRoot_guid;
52         bool is_schema_nc;
53         uint64_t min_usn;
54         uint64_t max_usn;
55         struct drsuapi_DsReplicaHighWaterMark last_hwm;
56         struct ldb_dn *last_dn;
57         struct drsuapi_DsReplicaHighWaterMark final_hwm;
58         struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
59         struct drsuapi_DsReplicaLinkedAttribute *la_list;
60         uint32_t la_count;
61         struct la_for_sorting *la_sorted;
62         uint32_t la_idx;
63 };
64
65 /* We must keep the GUIDs in NDR form for sorting */
66 struct la_for_sorting {
67         struct drsuapi_DsReplicaLinkedAttribute *link;
68         uint8_t target_guid[16];
69         uint8_t source_guid[16];
70 };
71
72 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
73                                               const struct drsuapi_DsReplicaHighWaterMark *h2)
74 {
75         if (h1->highest_usn < h2->highest_usn) {
76                 return -1;
77         } else if (h1->highest_usn > h2->highest_usn) {
78                 return 1;
79         } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
80                 return -1;
81         } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
82                 return 1;
83         } else if (h1->reserved_usn < h2->reserved_usn) {
84                 return -1;
85         } else if (h1->reserved_usn > h2->reserved_usn) {
86                 return 1;
87         }
88
89         return 0;
90 }
91
92 /*
93   build a DsReplicaObjectIdentifier from a ldb msg
94  */
95 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
96                                                                        const struct ldb_message *msg)
97 {
98         struct drsuapi_DsReplicaObjectIdentifier *identifier;
99         struct dom_sid *sid;
100
101         identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
102         if (identifier == NULL) {
103                 return NULL;
104         }
105
106         identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
107         identifier->guid = samdb_result_guid(msg, "objectGUID");
108
109         sid = samdb_result_dom_sid(identifier, msg, "objectSid");
110         if (sid) {
111                 identifier->sid = *sid;
112         } else {
113                 ZERO_STRUCT(identifier->sid);
114         }
115         return identifier;
116 }
117
118 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
119 {
120         return GUID_compare(guid1, &guid2);
121 }
122
123 /*
124   see if we can filter an attribute using the uptodateness_vector
125  */
126 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
127                        const struct GUID *originating_invocation_id,
128                        uint64_t originating_usn)
129 {
130         const struct drsuapi_DsReplicaCursor *c;
131         if (udv == NULL) return false;
132         BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
133                             originating_invocation_id, udv_compare, c);
134         if (c && originating_usn <= c->highest_usn) {
135                 return true;
136         }
137         return false;
138
139 }
140
141 static int uint32_t_cmp(uint32_t a1, uint32_t a2)
142 {
143         if (a1 == a2) return 0;
144         return a1 > a2 ? 1 : -1;
145 }
146
147 static int uint32_t_ptr_cmp(uint32_t *a1, uint32_t *a2, void *unused)
148 {
149         if (*a1 == *a2) return 0;
150         return *a1 > *a2 ? 1 : -1;
151 }
152
153 static WERROR getncchanges_attid_remote_to_local(const struct dsdb_schema *schema,
154                                                  const struct dsdb_syntax_ctx *ctx,
155                                                  enum drsuapi_DsAttributeId remote_attid_as_enum,
156                                                  enum drsuapi_DsAttributeId *local_attid_as_enum,
157                                                  const struct dsdb_attribute **_sa)
158 {
159         WERROR werr;
160         const struct dsdb_attribute *sa = NULL;
161
162         if (ctx->pfm_remote == NULL) {
163                 DEBUG(7, ("No prefixMap supplied, falling back to local prefixMap.\n"));
164                 goto fail;
165         }
166
167         werr = dsdb_attribute_drsuapi_remote_to_local(ctx,
168                                                       remote_attid_as_enum,
169                                                       local_attid_as_enum,
170                                                       _sa);
171         if (!W_ERROR_IS_OK(werr)) {
172                 DEBUG(3, ("WARNING: Unable to resolve remote attid, falling back to local prefixMap.\n"));
173                 goto fail;
174         }
175
176         return werr;
177 fail:
178
179         sa = dsdb_attribute_by_attributeID_id(schema, remote_attid_as_enum);
180         if (sa == NULL) {
181                 return WERR_DS_DRA_SCHEMA_MISMATCH;
182         } else {
183                 if (local_attid_as_enum != NULL) {
184                         *local_attid_as_enum = sa->attributeID_id;
185                 }
186                 if (_sa != NULL) {
187                         *_sa = sa;
188                 }
189                 return WERR_OK;
190         }
191 }
192
193 /* 
194   drsuapi_DsGetNCChanges for one object
195 */
196 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
197                                           const struct ldb_message *msg,
198                                           struct ldb_context *sam_ctx,
199                                           struct ldb_dn *ncRoot_dn,
200                                           bool   is_schema_nc,
201                                           struct dsdb_schema *schema,
202                                           DATA_BLOB *session_key,
203                                           uint64_t highest_usn,
204                                           uint32_t replica_flags,
205                                           struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
206                                           struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
207                                           enum drsuapi_DsExtendedOperation extended_op,
208                                           bool force_object_return,
209                                           uint32_t *local_pas)
210 {
211         const struct ldb_val *md_value;
212         uint32_t i, n;
213         struct replPropertyMetaDataBlob md;
214         uint32_t rid = 0;
215         enum ndr_err_code ndr_err;
216         uint32_t *attids;
217         const char *rdn;
218         const struct dsdb_attribute *rdn_sa;
219         uint64_t uSNChanged;
220         unsigned int instanceType;
221         struct dsdb_syntax_ctx syntax_ctx;
222
223         /* make dsdb sytanx context for conversions */
224         dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
225         syntax_ctx.is_schema_nc = is_schema_nc;
226
227         uSNChanged = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
228         instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
229         if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
230                 obj->is_nc_prefix = true;
231                 obj->parent_object_guid = NULL;
232         } else {
233                 obj->is_nc_prefix = false;
234                 obj->parent_object_guid = talloc(obj, struct GUID);
235                 if (obj->parent_object_guid == NULL) {
236                         return WERR_DS_DRA_INTERNAL_ERROR;
237                 }
238                 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
239                 if (GUID_all_zero(obj->parent_object_guid)) {
240                         DEBUG(0,(__location__ ": missing parentGUID for %s\n",
241                                  ldb_dn_get_linearized(msg->dn)));
242                         return WERR_DS_DRA_INTERNAL_ERROR;
243                 }
244         }
245         obj->next_object = NULL;
246
247         md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
248         if (!md_value) {
249                 /* nothing to send */
250                 return WERR_OK;
251         }
252
253         if (instanceType & INSTANCE_TYPE_UNINSTANT) {
254                 /* don't send uninstantiated objects */
255                 return WERR_OK;
256         }
257
258         if (uSNChanged <= highest_usn) {
259                 /* nothing to send */
260                 return WERR_OK;
261         }
262
263         ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
264                                        (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
265         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
266                 return WERR_DS_DRA_INTERNAL_ERROR;
267         }
268
269         if (md.version != 1) {
270                 return WERR_DS_DRA_INTERNAL_ERROR;
271         }
272
273         rdn = ldb_dn_get_rdn_name(msg->dn);
274         if (rdn == NULL) {
275                 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
276                 return WERR_DS_DRA_INTERNAL_ERROR;
277         }
278
279         rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
280         if (rdn_sa == NULL) {
281                 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
282                          rdn, ldb_dn_get_linearized(msg->dn)));
283                 return WERR_DS_DRA_INTERNAL_ERROR;
284         }
285
286         obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
287         attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
288
289         obj->object.identifier = get_object_identifier(obj, msg);
290         if (obj->object.identifier == NULL) {
291                 return WERR_NOT_ENOUGH_MEMORY;
292         }
293         dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
294
295         obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
296         for (n=i=0; i<md.ctr.ctr1.count; i++) {
297                 const struct dsdb_attribute *sa;
298                 bool force_attribute = false;
299
300                 /* if the attribute has not changed, and it is not the
301                    instanceType then don't include it */
302                 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
303                     extended_op != DRSUAPI_EXOP_REPL_SECRET &&
304                     md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
305
306                 /* don't include the rDN */
307                 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
308
309                 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
310                 if (!sa) {
311                         DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
312                                  (unsigned int)md.ctr.ctr1.array[i].attid,
313                                  ldb_dn_get_linearized(msg->dn)));
314                         return WERR_DS_DRA_INTERNAL_ERROR;
315                 }
316
317                 if (sa->linkID) {
318                         struct ldb_message_element *el;
319                         el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
320                         if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
321                                 /* don't send upgraded links inline */
322                                 continue;
323                         }
324                 }
325
326                 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
327                     !dsdb_attr_in_rodc_fas(sa)) {
328                         force_attribute = true;
329                         DEBUG(4,("Forcing attribute %s in %s\n",
330                                  sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
331                 }
332
333                 /* filter by uptodateness_vector */
334                 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
335                     !force_attribute &&
336                     udv_filter(uptodateness_vector,
337                                &md.ctr.ctr1.array[i].originating_invocation_id,
338                                md.ctr.ctr1.array[i].originating_usn)) {
339                         continue;
340                 }
341
342                 /* filter by partial_attribute_set */
343                 if (partial_attribute_set) {
344                         uint32_t *result = NULL;
345                         BINARY_ARRAY_SEARCH_V(local_pas, partial_attribute_set->num_attids, sa->attributeID_id,
346                                               uint32_t_cmp, result);
347                         if (result == NULL) {
348                                 continue;
349                         }
350                 }
351
352                 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
353                 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
354                 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
355                 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
356                 attids[n] = md.ctr.ctr1.array[i].attid;
357                 n++;
358         }
359
360         /* ignore it if its an empty change. Note that renames always
361          * change the 'name' attribute, so they won't be ignored by
362          * this
363
364          * the force_object_return check is used to force an empty
365          * object return when we timeout in the getncchanges loop.
366          * This allows us to return an empty object, which keeps the
367          * client happy while preventing timeouts
368          */
369         if (n == 0 ||
370             (n == 1 &&
371              attids[0] == DRSUAPI_ATTID_instanceType &&
372              !force_object_return)) {
373                 talloc_free(obj->meta_data_ctr);
374                 obj->meta_data_ctr = NULL;
375                 return WERR_OK;
376         }
377
378         obj->meta_data_ctr->count = n;
379
380         obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
381         obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
382         obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
383                                                             obj->object.attribute_ctr.num_attributes);
384         if (obj->object.attribute_ctr.attributes == NULL) {
385                 return WERR_NOT_ENOUGH_MEMORY;
386         }
387
388         /*
389          * Note that the meta_data array and the attributes array must
390          * be the same size and in the same order
391          */
392         for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
393                 struct ldb_message_element *el;
394                 WERROR werr;
395                 const struct dsdb_attribute *sa;
396
397                 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
398                 if (!sa) {
399                         DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
400                         return WERR_DS_DRA_INTERNAL_ERROR;
401                 }
402
403                 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
404                 if (el == NULL) {
405                         /* this happens for attributes that have been removed */
406                         DEBUG(5,("No element '%s' for attributeID %u in message\n",
407                                  sa->lDAPDisplayName, attids[i]));
408                         ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
409                         obj->object.attribute_ctr.attributes[i].attid =
410                                         dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
411                 } else {
412                         werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
413                                                           &obj->object.attribute_ctr.attributes[i]);
414                         if (!W_ERROR_IS_OK(werr)) {
415                                 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
416                                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
417                                          win_errstr(werr)));
418                                 return werr;
419                         }
420                         /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
421                          * check if attribute is secret and send a null value
422                          */
423                         if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
424                                 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
425                                                                  &obj->meta_data_ctr->meta_data[i]);
426                         }
427                         /* some attributes needs to be encrypted
428                            before being sent */
429                         werr = drsuapi_encrypt_attribute(obj, session_key, rid, 
430                                                          &obj->object.attribute_ctr.attributes[i]);
431                         if (!W_ERROR_IS_OK(werr)) {
432                                 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
433                                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
434                                          win_errstr(werr)));
435                                 return werr;
436                         }
437                 }
438                 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
439                         DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID:  "
440                                   "0x%08x vs 0x%08x "
441                                   "Run dbcheck!\n",
442                                   sa->lDAPDisplayName,
443                                   ldb_dn_get_linearized(msg->dn),
444                                   attids[i],
445                                   obj->object.attribute_ctr.attributes[i].attid));
446                         return WERR_DS_DATABASE_ERROR;
447                 }
448         }
449
450         return WERR_OK;
451 }
452
453 /*
454   add one linked attribute from an object to the list of linked
455   attributes in a getncchanges request
456  */
457 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
458                                     struct ldb_context *sam_ctx,
459                                     const struct dsdb_schema *schema,
460                                     const struct dsdb_attribute *sa,
461                                     const struct ldb_message *msg,
462                                     struct dsdb_dn *dsdb_dn,
463                                     struct drsuapi_DsReplicaLinkedAttribute **la_list,
464                                     uint32_t *la_count,
465                                     bool is_schema_nc)
466 {
467         struct drsuapi_DsReplicaLinkedAttribute *la;
468         bool active;
469         NTSTATUS status;
470         WERROR werr;
471
472         (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
473         W_ERROR_HAVE_NO_MEMORY(*la_list);
474
475         la = &(*la_list)[*la_count];
476
477         la->identifier = get_object_identifier(*la_list, msg);
478         W_ERROR_HAVE_NO_MEMORY(la->identifier);
479
480         active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
481
482         if (!active) {
483                 /* We have to check that the inactive link still point to an existing object */
484                 struct GUID guid;
485                 struct ldb_dn *tdn;
486                 int ret;
487                 const char *v;
488
489                 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
490                 if (strncmp(v, "TRUE", 4) == 0) {
491                         /*
492                           * Note: we skip the transmition of the deleted link even if the other part used to
493                           * know about it because when we transmit the deletion of the object, the link will
494                           * be deleted too due to deletion of object where link points and Windows do so.
495                           */
496                         if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
497                                 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
498                                 /*
499                                  * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
500                                  * if it join an existing domain with deleted objets, it firsts impose to have a
501                                  * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
502                                  * either during initial replication or after the getNCChanges.
503                                  * Behavior of samba has been changed to always have this attribute if it's present in the schema.
504                                  *
505                                  * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
506                                  * If FL >=2K8R2 we are sure that this attribute will be here.
507                                  * For this kind of forest level we do not return the link if the object is recycled
508                                  * (isRecycled = true).
509                                  */
510                                 if (strncmp(v, "TRUE", 4) == 0) {
511                                         DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
512                                                                 ldb_dn_get_linearized(msg->dn)));
513                                         return WERR_OK;
514                                 }
515                         } else {
516                                 return WERR_OK;
517                         }
518                 }
519                 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
520                 if (!NT_STATUS_IS_OK(status)) {
521                         DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
522                                 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
523                         return ntstatus_to_werror(status);
524                 }
525                 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
526                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
527                         DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
528                                                 GUID_string(mem_ctx, &guid)));
529                         return WERR_OK;
530                 } else if (ret != LDB_SUCCESS) {
531                         DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
532                                                 GUID_string(mem_ctx, &guid),
533                                                 ret));
534                         return WERR_OK;
535                 }
536         }
537         la->attid = dsdb_attribute_get_attid(sa, is_schema_nc);
538         la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
539
540         status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
541         if (!NT_STATUS_IS_OK(status)) {
542                 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
543                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
544                 return ntstatus_to_werror(status);
545         }
546         status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
547         if (!NT_STATUS_IS_OK(status)) {
548                 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
549                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
550                 return ntstatus_to_werror(status);
551         }
552         status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
553         if (!NT_STATUS_IS_OK(status)) {
554                 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
555                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
556                 return ntstatus_to_werror(status);
557         }
558         status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
559         if (!NT_STATUS_IS_OK(status)) {
560                 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
561                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
562                 return ntstatus_to_werror(status);
563         }
564
565         status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
566         if (!NT_STATUS_IS_OK(status)) {
567                 /* this is possible for upgraded links */
568                 la->originating_add_time = la->meta_data.originating_change_time;
569         }
570
571         werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
572         W_ERROR_NOT_OK_RETURN(werr);
573
574         (*la_count)++;
575         return WERR_OK;
576 }
577
578
579 /*
580   add linked attributes from an object to the list of linked
581   attributes in a getncchanges request
582  */
583 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
584                                        TALLOC_CTX *mem_ctx,
585                                        struct ldb_dn *ncRoot_dn,
586                                        bool is_schema_nc,
587                                        struct dsdb_schema *schema,
588                                        uint64_t highest_usn,
589                                        uint32_t replica_flags,
590                                        const struct ldb_message *msg,
591                                        struct drsuapi_DsReplicaLinkedAttribute **la_list,
592                                        uint32_t *la_count,
593                                        struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
594 {
595         unsigned int i;
596         TALLOC_CTX *tmp_ctx = NULL;
597         uint64_t uSNChanged = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
598         bool is_critical = ldb_msg_find_attr_as_bool(msg, "isCriticalSystemObject", false);
599
600         if (replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
601                 if (!is_critical) {
602                         return WERR_OK;
603                 }
604         }
605
606         if (uSNChanged <= highest_usn) {
607                 return WERR_OK;
608         }
609
610         tmp_ctx = talloc_new(mem_ctx);
611         if (tmp_ctx == NULL) {
612                 return WERR_NOT_ENOUGH_MEMORY;
613         }
614
615         for (i=0; i<msg->num_elements; i++) {
616                 struct ldb_message_element *el = &msg->elements[i];
617                 const struct dsdb_attribute *sa;
618                 unsigned int j;
619
620                 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
621
622                 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
623                         /* we only want forward links */
624                         continue;
625                 }
626
627                 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
628                         /* its an old style link, it will have been
629                          * sent in the main replication data */
630                         continue;
631                 }
632
633                 for (j=0; j<el->num_values; j++) {
634                         struct dsdb_dn *dsdb_dn;
635                         uint64_t local_usn;
636                         uint64_t originating_usn;
637                         NTSTATUS status, status2;
638                         WERROR werr;
639                         struct GUID originating_invocation_id;
640
641                         dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
642                         if (dsdb_dn == NULL) {
643                                 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
644                                          el->name, ldb_dn_get_linearized(msg->dn)));
645                                 talloc_free(tmp_ctx);
646                                 return WERR_DS_DRA_INTERNAL_ERROR;
647                         }
648
649                         status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
650                         if (!NT_STATUS_IS_OK(status)) {
651                                 /* this can happen for attributes
652                                    given to us with old style meta
653                                    data */
654                                 continue;
655                         }
656
657                         if (local_usn > uSNChanged) {
658                                 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
659                                          el->name, ldb_dn_get_linearized(msg->dn)));
660                                 talloc_free(tmp_ctx);
661                                 return WERR_DS_DRA_INTERNAL_ERROR;
662                         }
663
664                         if (local_usn <= highest_usn) {
665                                 continue;
666                         }
667
668                         status = dsdb_get_extended_dn_guid(dsdb_dn->dn,
669                                                            &originating_invocation_id,
670                                                            "RMD_INVOCID");
671                         status2 = dsdb_get_extended_dn_uint64(dsdb_dn->dn,
672                                                               &originating_usn,
673                                                               "RMD_ORIGINATING_USN");
674
675                         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2)) {
676                                 if (udv_filter(uptodateness_vector,
677                                                &originating_invocation_id,
678                                                originating_usn)) {
679                                         continue;
680                                 }
681                         }
682
683                         werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema,
684                                                      sa, msg, dsdb_dn, la_list,
685                                                      la_count, is_schema_nc);
686                         if (!W_ERROR_IS_OK(werr)) {
687                                 talloc_free(tmp_ctx);
688                                 return werr;
689                         }
690                 }
691         }
692
693         talloc_free(tmp_ctx);
694         return WERR_OK;
695 }
696
697 /*
698   fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
699  */
700 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
701                                  struct ldb_dn *ncRoot_dn,
702                                  struct drsuapi_DsReplicaCursor2CtrEx *udv)
703 {
704         int ret;
705
706         udv->version = 2;
707         udv->reserved1 = 0;
708         udv->reserved2 = 0;
709
710         ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
711         if (ret != LDB_SUCCESS) {
712                 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
713                          ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
714                 return WERR_DS_DRA_INTERNAL_ERROR;
715         }
716
717         return WERR_OK;
718 }
719
720
721 /* comparison function for linked attributes - see CompareLinks() in
722  * MS-DRSR section 4.1.10.5.17 */
723 static int linked_attribute_compare(const struct la_for_sorting *la1,
724                                     const struct la_for_sorting *la2,
725                                     void *opaque)
726 {
727         int c;
728         c = memcmp(la1->source_guid,
729                    la2->source_guid, sizeof(la2->source_guid));
730         if (c != 0) {
731                 return c;
732         }
733
734         if (la1->link->attid != la2->link->attid) {
735                 return la1->link->attid < la2->link->attid? -1:1;
736         }
737
738         if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
739             (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
740                 return (la1->link->flags &
741                         DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
742         }
743
744         return memcmp(la1->target_guid,
745                       la2->target_guid, sizeof(la2->target_guid));
746 }
747
748 struct drsuapi_changed_objects {
749         struct ldb_dn *dn;
750         struct GUID guid;
751         uint64_t usn;
752 };
753
754 /*
755   sort the objects we send first by uSNChanged
756  */
757 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
758                                   struct drsuapi_changed_objects *m2,
759                                   struct drsuapi_getncchanges_state *getnc_state)
760 {
761         int ret;
762
763         ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
764         if (ret == 0) {
765                 return -1;
766         }
767
768         ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
769         if (ret == 0) {
770                 return 1;
771         }
772
773         if (m1->usn == m2->usn) {
774                 return ldb_dn_compare(m2->dn, m1->dn);
775         }
776
777         if (m1->usn < m2->usn) {
778                 return -1;
779         }
780
781         return 1;
782 }
783
784
785 /*
786   handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
787  */
788 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
789                                      TALLOC_CTX *mem_ctx,
790                                      struct drsuapi_DsGetNCChangesRequest10 *req10,
791                                      struct drsuapi_DsGetNCChangesCtr6 *ctr6,
792                                      struct ldb_dn **rid_manager_dn)
793 {
794         struct ldb_dn *req_dn, *ntds_dn = NULL;
795         int ret;
796         struct ldb_context *ldb = b_state->sam_ctx;
797         struct ldb_result *ext_res;
798         struct dsdb_fsmo_extended_op *exop;
799         bool is_us;
800
801         /*
802           steps:
803             - verify that the DN being asked for is the RID Manager DN
804             - verify that we are the RID Manager
805          */
806
807         /* work out who is the RID Manager, also return to caller */
808         ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
809         if (ret != LDB_SUCCESS) {
810                 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
811                 return WERR_DS_DRA_INTERNAL_ERROR;
812         }
813
814         req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
815         if (!ldb_dn_validate(req_dn) ||
816             ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
817                 /* that isn't the RID Manager DN */
818                 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
819                          drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
820                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
821                 return WERR_OK;
822         }
823
824         /* TODO: make sure ntds_dn is a valid nTDSDSA object */
825         ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
826         if (ret != LDB_SUCCESS) {
827                 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
828                           GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
829                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
830                 return WERR_OK;
831         }
832
833         /* find the DN of the RID Manager */
834         ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
835         if (ret != LDB_SUCCESS) {
836                 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
837                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
838                 return WERR_DS_DRA_INTERNAL_ERROR;
839         }
840
841         if (!is_us) {
842                 /* we're not the RID Manager - go away */
843                 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
844                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
845                 return WERR_OK;
846         }
847
848         exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
849         W_ERROR_HAVE_NO_MEMORY(exop);
850
851         exop->fsmo_info = req10->fsmo_info;
852         exop->destination_dsa_guid = req10->destination_dsa_guid;
853
854         ret = ldb_transaction_start(ldb);
855         if (ret != LDB_SUCCESS) {
856                 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
857                          ldb_errstring(ldb)));
858                 return WERR_DS_DRA_INTERNAL_ERROR;
859         }
860
861         ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
862         if (ret != LDB_SUCCESS) {
863                 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
864                          ldb_errstring(ldb)));
865                 ldb_transaction_cancel(ldb);
866                 return WERR_DS_DRA_INTERNAL_ERROR;
867         }
868
869         ret = ldb_transaction_commit(ldb);
870         if (ret != LDB_SUCCESS) {
871                 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
872                          ldb_errstring(ldb)));
873                 return WERR_DS_DRA_INTERNAL_ERROR;
874         }
875
876         talloc_free(ext_res);
877
878         DEBUG(2,("Allocated RID pool for server %s\n",
879                  GUID_string(mem_ctx, &req10->destination_dsa_guid)));
880
881         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
882
883         return WERR_OK;
884 }
885
886 /*
887   return an array of SIDs from a ldb_message given an attribute name
888   assumes the SIDs are in extended DN format
889  */
890 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
891                                         struct ldb_message *msg,
892                                         TALLOC_CTX *mem_ctx,
893                                         const char *attr,
894                                         const struct dom_sid ***sids)
895 {
896         struct ldb_message_element *el;
897         unsigned int i;
898
899         el = ldb_msg_find_element(msg, attr);
900         if (!el) {
901                 *sids = NULL;
902                 return WERR_OK;
903         }
904
905         (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
906         W_ERROR_HAVE_NO_MEMORY(*sids);
907
908         for (i=0; i<el->num_values; i++) {
909                 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
910                 NTSTATUS status;
911                 struct dom_sid *sid;
912
913                 sid = talloc(*sids, struct dom_sid);
914                 W_ERROR_HAVE_NO_MEMORY(sid);
915                 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
916                 if (!NT_STATUS_IS_OK(status)) {
917                         return WERR_INTERNAL_DB_CORRUPTION;
918                 }
919                 (*sids)[i] = sid;
920         }
921         (*sids)[i] = NULL;
922
923         return WERR_OK;
924 }
925
926
927 /*
928   return an array of SIDs from a ldb_message given an attribute name
929   assumes the SIDs are in NDR form
930  */
931 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
932                                          struct ldb_message *msg,
933                                          TALLOC_CTX *mem_ctx,
934                                          const char *attr,
935                                          const struct dom_sid ***sids)
936 {
937         struct ldb_message_element *el;
938         unsigned int i;
939
940         el = ldb_msg_find_element(msg, attr);
941         if (!el) {
942                 *sids = NULL;
943                 return WERR_OK;
944         }
945
946         (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
947         W_ERROR_HAVE_NO_MEMORY(*sids);
948
949         for (i=0; i<el->num_values; i++) {
950                 enum ndr_err_code ndr_err;
951                 struct dom_sid *sid;
952
953                 sid = talloc(*sids, struct dom_sid);
954                 W_ERROR_HAVE_NO_MEMORY(sid);
955
956                 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
957                                                (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
958                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
959                         return WERR_INTERNAL_DB_CORRUPTION;
960                 }
961                 (*sids)[i] = sid;
962         }
963         (*sids)[i] = NULL;
964
965         return WERR_OK;
966 }
967
968 /*
969   see if any SIDs in list1 are in list2
970  */
971 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
972 {
973         unsigned int i, j;
974         /* do we ever have enough SIDs here to worry about O(n^2) ? */
975         for (i=0; list1[i]; i++) {
976                 for (j=0; list2[j]; j++) {
977                         if (dom_sid_equal(list1[i], list2[j])) {
978                                 return true;
979                         }
980                 }
981         }
982         return false;
983 }
984
985 /*
986   handle a DRSUAPI_EXOP_REPL_SECRET call
987  */
988 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
989                                        TALLOC_CTX *mem_ctx,
990                                        struct drsuapi_DsGetNCChangesRequest10 *req10,
991                                        struct dom_sid *user_sid,
992                                        struct drsuapi_DsGetNCChangesCtr6 *ctr6,
993                                        bool has_get_all_changes)
994 {
995         struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
996         struct ldb_dn *obj_dn = NULL;
997         struct ldb_dn *rodc_dn, *krbtgt_link_dn;
998         int ret;
999         const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
1000         const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
1001         struct ldb_result *rodc_res, *obj_res;
1002         const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
1003         WERROR werr;
1004
1005         DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
1006                  drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1007
1008         /*
1009          * we need to work out if we will allow this DC to
1010          * replicate the secrets for this object
1011          *
1012          * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
1013          * of this function
1014          */
1015
1016         if (b_state->sam_ctx_system == NULL) {
1017                 /* this operation needs system level access */
1018                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
1019                 return WERR_DS_DRA_SOURCE_DISABLED;
1020         }
1021
1022         /*
1023          * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
1024          *
1025          * The pseudo code indicate
1026          * revealsecrets = true
1027          * if IsRevealSecretRequest(msgIn) then
1028          *   if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
1029          *   then
1030          *     if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
1031          *     <... check if this account is ok to be replicated on this DC ...>
1032          *     <... and if not reveal secrets = no ...>
1033          *     else
1034          *       reveal secrets = false
1035          *     endif
1036          *   endif
1037          * endif
1038          *
1039          * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
1040          * then you can do EXOP_REPL_SECRETS
1041          */
1042         if (has_get_all_changes) {
1043                 goto allowed;
1044         }
1045
1046         obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
1047         if (!ldb_dn_validate(obj_dn)) goto failed;
1048
1049         rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1050                                  dom_sid_string(mem_ctx, user_sid));
1051         if (!ldb_dn_validate(rodc_dn)) goto failed;
1052
1053         /* do the two searches we need */
1054         ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1055                              DSDB_SEARCH_SHOW_EXTENDED_DN);
1056         if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1057
1058         ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1059         if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1060
1061         /* if the object SID is equal to the user_sid, allow */
1062         if (dom_sid_equal(user_sid,
1063                           samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1064                 goto allowed;
1065         }
1066
1067         /* an RODC is allowed to get its own krbtgt account secrets */
1068         krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1069                                          rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1070         if (krbtgt_link_dn != NULL &&
1071             ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1072                 goto allowed;
1073         }
1074
1075         /* but it isn't allowed to get anyone elses krbtgt secrets */
1076         if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1077                             obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1078                 goto denied;
1079         }
1080
1081         if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1082                                       "userAccountControl", 0) &
1083             UF_INTERDOMAIN_TRUST_ACCOUNT) {
1084                 goto denied;
1085         }
1086
1087         werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1088                                          mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1089         if (!W_ERROR_IS_OK(werr)) {
1090                 goto denied;
1091         }
1092
1093         werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1094                                          mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1095         if (!W_ERROR_IS_OK(werr)) {
1096                 goto denied;
1097         }
1098
1099         werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1100                                          mem_ctx, "tokenGroups", &token_sids);
1101         if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1102                 goto denied;
1103         }
1104
1105         if (never_reveal_sids &&
1106             sid_list_match(token_sids, never_reveal_sids)) {
1107                 goto denied;
1108         }
1109
1110         if (reveal_sids &&
1111             sid_list_match(token_sids, reveal_sids)) {
1112                 goto allowed;
1113         }
1114
1115         /* default deny */
1116 denied:
1117         DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1118                  ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1119         ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1120         return WERR_DS_DRA_SECRETS_DENIED;
1121
1122 allowed:
1123         DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1124                  ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1125                  ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1126         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1127         req10->highwatermark.highest_usn = 0;
1128         return WERR_OK;
1129
1130 failed:
1131         DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1132                  ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1133         ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1134         return WERR_DS_DRA_BAD_DN;
1135 }
1136
1137
1138 /*
1139   handle a DRSUAPI_EXOP_REPL_OBJ call
1140  */
1141 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1142                                     TALLOC_CTX *mem_ctx,
1143                                     struct drsuapi_DsGetNCChangesRequest10 *req10,
1144                                     struct dom_sid *user_sid,
1145                                     struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1146 {
1147         struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1148
1149         DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1150                  drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1151
1152         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1153         return WERR_OK;
1154 }
1155
1156
1157 /*
1158   handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1159   DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1160   and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1161  */
1162 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1163                                          TALLOC_CTX *mem_ctx,
1164                                          struct drsuapi_DsGetNCChangesRequest10 *req10,
1165                                          struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1166 {
1167         struct ldb_dn *req_dn, *ntds_dn;
1168         int ret;
1169         unsigned int i;
1170         struct ldb_context *ldb = b_state->sam_ctx;
1171         struct ldb_message *msg;
1172         bool is_us;
1173
1174         /*
1175           steps:
1176             - verify that the client dn exists
1177             - verify that we are the current master
1178          */
1179
1180         req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1181         if (!ldb_dn_validate(req_dn)) {
1182                 /* that is not a valid dn */
1183                 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1184                          drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1185                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1186                 return WERR_OK;
1187         }
1188
1189         /* find the DN of the current role owner */
1190         ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1191         if (ret != LDB_SUCCESS) {
1192                 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1193                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1194                 return WERR_DS_DRA_INTERNAL_ERROR;
1195         }
1196
1197         if (!is_us) {
1198                 /* we're not the RID Manager or role owner - go away */
1199                 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1200                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1201                 return WERR_OK;
1202         }
1203
1204         /* change the current master */
1205         msg = ldb_msg_new(ldb);
1206         W_ERROR_HAVE_NO_MEMORY(msg);
1207         msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1208         W_ERROR_HAVE_NO_MEMORY(msg->dn);
1209
1210         /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1211         ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1212         if (ret != LDB_SUCCESS) {
1213                 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1214                           GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1215                 talloc_free(msg);
1216                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1217                 return WERR_OK;
1218         }
1219
1220         ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1221         if (ret != 0) {
1222                 talloc_free(msg);
1223                 return WERR_DS_DRA_INTERNAL_ERROR;
1224         }
1225
1226         for (i=0;i<msg->num_elements;i++) {
1227                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1228         }
1229
1230         ret = ldb_transaction_start(ldb);
1231         if (ret != LDB_SUCCESS) {
1232                 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1233                          ldb_errstring(ldb)));
1234                 return WERR_DS_DRA_INTERNAL_ERROR;
1235         }
1236
1237         ret = ldb_modify(ldb, msg);
1238         if (ret != LDB_SUCCESS) {
1239                 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1240                          ldb_errstring(ldb)));
1241                 ldb_transaction_cancel(ldb);
1242                 return WERR_DS_DRA_INTERNAL_ERROR;
1243         }
1244
1245         ret = ldb_transaction_commit(ldb);
1246         if (ret != LDB_SUCCESS) {
1247                 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1248                          ldb_errstring(ldb)));
1249                 return WERR_DS_DRA_INTERNAL_ERROR;
1250         }
1251
1252         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1253
1254         return WERR_OK;
1255 }
1256
1257 /*
1258   see if this getncchanges request includes a request to reveal secret information
1259  */
1260 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1261                                                        struct drsuapi_DsGetNCChangesRequest10 *req10,
1262                                                        struct dsdb_schema_prefixmap *pfm_remote,
1263                                                        bool *is_secret_request)
1264 {
1265         enum drsuapi_DsExtendedOperation exop;
1266         uint32_t i;
1267         struct dsdb_schema *schema;
1268         struct dsdb_syntax_ctx syntax_ctx;
1269
1270         *is_secret_request = true;
1271
1272         exop = req10->extended_op;
1273
1274         switch (exop) {
1275         case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1276         case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1277         case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1278         case DRSUAPI_EXOP_FSMO_REQ_PDC:
1279         case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1280                 /* FSMO exops can reveal secrets */
1281                 *is_secret_request = true;
1282                 return WERR_OK;
1283         case DRSUAPI_EXOP_REPL_SECRET:
1284         case DRSUAPI_EXOP_REPL_OBJ:
1285         case DRSUAPI_EXOP_NONE:
1286                 break;
1287         }
1288
1289         if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1290                 *is_secret_request = false;
1291                 return WERR_OK;
1292         }
1293
1294         if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1295             req10->partial_attribute_set == NULL) {
1296                 /* they want secrets */
1297                 *is_secret_request = true;
1298                 return WERR_OK;
1299         }
1300
1301         schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1302         dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1303         syntax_ctx.pfm_remote = pfm_remote;
1304
1305         /* check the attributes they asked for */
1306         for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1307                 const struct dsdb_attribute *sa;
1308                 WERROR werr = getncchanges_attid_remote_to_local(schema,
1309                                                                  &syntax_ctx,
1310                                                                  req10->partial_attribute_set->attids[i],
1311                                                                  NULL,
1312                                                                  &sa);
1313
1314                 if (!W_ERROR_IS_OK(werr)) {
1315                         DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1316                                  req10->partial_attribute_set->attids[i], win_errstr(werr)));
1317                         return werr;
1318                 }
1319
1320                 if (!dsdb_attr_in_rodc_fas(sa)) {
1321                         *is_secret_request = true;
1322                         return WERR_OK;
1323                 }
1324         }
1325
1326         if (req10->partial_attribute_set_ex) {
1327                 /* check the extended attributes they asked for */
1328                 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1329                         const struct dsdb_attribute *sa;
1330                         WERROR werr = getncchanges_attid_remote_to_local(schema,
1331                                                                          &syntax_ctx,
1332                                                                          req10->partial_attribute_set_ex->attids[i],
1333                                                                          NULL,
1334                                                                          &sa);
1335
1336                         if (!W_ERROR_IS_OK(werr)) {
1337                                 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1338                                          req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1339                                 return werr;
1340                         }
1341
1342                         if (!dsdb_attr_in_rodc_fas(sa)) {
1343                                 *is_secret_request = true;
1344                                 return WERR_OK;
1345                         }
1346                 }
1347         }
1348
1349         *is_secret_request = false;
1350         return WERR_OK;
1351 }
1352
1353 /*
1354   see if this getncchanges request is only for attributes in the GC
1355   partial attribute set
1356  */
1357 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1358                                                struct drsuapi_DsGetNCChangesRequest10 *req10,
1359                                                struct dsdb_schema_prefixmap *pfm_remote,
1360                                                bool *is_gc_pas_request)
1361 {
1362         enum drsuapi_DsExtendedOperation exop;
1363         uint32_t i;
1364         struct dsdb_schema *schema;
1365         struct dsdb_syntax_ctx syntax_ctx;
1366
1367         exop = req10->extended_op;
1368
1369         switch (exop) {
1370         case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1371         case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1372         case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1373         case DRSUAPI_EXOP_FSMO_REQ_PDC:
1374         case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1375         case DRSUAPI_EXOP_REPL_SECRET:
1376                 *is_gc_pas_request = false;
1377                 return WERR_OK;
1378         case DRSUAPI_EXOP_REPL_OBJ:
1379         case DRSUAPI_EXOP_NONE:
1380                 break;
1381         }
1382
1383         if (req10->partial_attribute_set == NULL) {
1384                 /* they want it all */
1385                 *is_gc_pas_request = false;
1386                 return WERR_OK;
1387         }
1388
1389         schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1390         dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1391         syntax_ctx.pfm_remote = pfm_remote;
1392
1393         /* check the attributes they asked for */
1394         for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1395                 const struct dsdb_attribute *sa;
1396                 WERROR werr = getncchanges_attid_remote_to_local(schema,
1397                                                                  &syntax_ctx,
1398                                                                  req10->partial_attribute_set->attids[i],
1399                                                                  NULL,
1400                                                                  &sa);
1401
1402                 if (!W_ERROR_IS_OK(werr)) {
1403                         DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1404                                  req10->partial_attribute_set->attids[i], win_errstr(werr)));
1405                         return werr;
1406                 }
1407
1408                 if (!sa->isMemberOfPartialAttributeSet) {
1409                         *is_gc_pas_request = false;
1410                         return WERR_OK;
1411                 }
1412         }
1413
1414         if (req10->partial_attribute_set_ex) {
1415                 /* check the extended attributes they asked for */
1416                 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1417                         const struct dsdb_attribute *sa;
1418                         WERROR werr = getncchanges_attid_remote_to_local(schema,
1419                                                                          &syntax_ctx,
1420                                                                          req10->partial_attribute_set_ex->attids[i],
1421                                                                          NULL,
1422                                                                          &sa);
1423
1424                         if (!W_ERROR_IS_OK(werr)) {
1425                                 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1426                                          req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1427                                 return werr;
1428                         }
1429
1430                         if (!sa->isMemberOfPartialAttributeSet) {
1431                                 *is_gc_pas_request = false;
1432                                 return WERR_OK;
1433                         }
1434                 }
1435         }
1436
1437         *is_gc_pas_request = true;
1438         return WERR_OK;
1439 }
1440
1441
1442 /*
1443   map from req8 to req10
1444  */
1445 static struct drsuapi_DsGetNCChangesRequest10 *
1446 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1447                       struct drsuapi_DsGetNCChangesRequest8 *req8)
1448 {
1449         struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1450                                                                     struct drsuapi_DsGetNCChangesRequest10);
1451         if (req10 == NULL) {
1452                 return NULL;
1453         }
1454
1455         req10->destination_dsa_guid = req8->destination_dsa_guid;
1456         req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1457         req10->naming_context = req8->naming_context;
1458         req10->highwatermark = req8->highwatermark;
1459         req10->uptodateness_vector = req8->uptodateness_vector;
1460         req10->replica_flags = req8->replica_flags;
1461         req10->max_object_count = req8->max_object_count;
1462         req10->max_ndr_size = req8->max_ndr_size;
1463         req10->extended_op = req8->extended_op;
1464         req10->fsmo_info = req8->fsmo_info;
1465         req10->partial_attribute_set = req8->partial_attribute_set;
1466         req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1467         req10->mapping_ctr = req8->mapping_ctr;
1468
1469         return req10;
1470 }
1471
1472 static const char *collect_objects_attrs[] = { "uSNChanged",
1473                                                "objectGUID" ,
1474                                                NULL };
1475
1476 /**
1477  * Collects object for normal replication cycle.
1478  */
1479 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1480                                            TALLOC_CTX *mem_ctx,
1481                                            struct drsuapi_DsGetNCChangesRequest10 *req10,
1482                                            struct ldb_dn *search_dn,
1483                                            const char *extra_filter,
1484                                            struct ldb_result **search_res)
1485 {
1486         int ret;
1487         char* search_filter;
1488         enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1489         struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1490         bool critical_only = false;
1491
1492         if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1493                 critical_only = true;
1494         }
1495
1496         if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1497             req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1498                 scope = LDB_SCOPE_BASE;
1499                 critical_only = false;
1500         }
1501
1502         /* Construct response. */
1503         search_filter = talloc_asprintf(mem_ctx,
1504                                         "(uSNChanged>=%llu)",
1505                                         (unsigned long long)(getnc_state->min_usn+1));
1506
1507         if (extra_filter) {
1508                 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1509         }
1510
1511         if (critical_only) {
1512                 search_filter = talloc_asprintf(mem_ctx,
1513                                                 "(&%s(isCriticalSystemObject=TRUE))",
1514                                                 search_filter);
1515         }
1516
1517         if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1518                 scope = LDB_SCOPE_BASE;
1519         }
1520
1521         if (!search_dn) {
1522                 search_dn = getnc_state->ncRoot_dn;
1523         }
1524
1525         DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1526                  ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1527         ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1528                                               search_dn, scope,
1529                                               collect_objects_attrs,
1530                                               search_filter);
1531         if (ret != LDB_SUCCESS) {
1532                 return WERR_DS_DRA_INTERNAL_ERROR;
1533         }
1534
1535         return WERR_OK;
1536 }
1537
1538 /**
1539  * Collects object for normal replication cycle.
1540  */
1541 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1542                                                 TALLOC_CTX *mem_ctx,
1543                                                 struct drsuapi_DsGetNCChangesRequest10 *req10,
1544                                                 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1545                                                 struct ldb_dn *search_dn,
1546                                                 const char *extra_filter,
1547                                                 struct ldb_result **search_res)
1548 {
1549         /* we have nothing to do in case of ex-op failure */
1550         if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1551                 return WERR_OK;
1552         }
1553
1554         switch (req10->extended_op) {
1555         case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1556         {
1557                 int ret;
1558                 struct ldb_dn *ntds_dn = NULL;
1559                 struct ldb_dn *server_dn = NULL;
1560                 struct ldb_dn *machine_dn = NULL;
1561                 struct ldb_dn *rid_set_dn = NULL;
1562                 struct ldb_result *search_res2 = NULL;
1563                 struct ldb_result *search_res3 = NULL;
1564                 TALLOC_CTX *frame = talloc_stackframe();
1565                 /* get RID manager, RID set and server DN (in that order) */
1566
1567                 /* This first search will get the RID Manager */
1568                 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1569                                                       search_res,
1570                                                       search_dn, LDB_SCOPE_BASE,
1571                                                       collect_objects_attrs,
1572                                                       NULL);
1573                 if (ret != LDB_SUCCESS) {
1574                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1575                                   ldb_dn_get_linearized(search_dn),
1576                                   ldb_errstring(b_state->sam_ctx)));
1577                         TALLOC_FREE(frame);
1578                         return WERR_DS_DRA_INTERNAL_ERROR;
1579                 }
1580
1581                 if ((*search_res)->count != 1) {
1582                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1583                                   ldb_dn_get_linearized(search_dn),
1584                                   (*search_res)->count));
1585                         TALLOC_FREE(frame);
1586                         return WERR_DS_DRA_INTERNAL_ERROR;
1587                 }
1588
1589                 /* Now extend it to the RID set */
1590
1591                 /* Find the computer account DN for the destination
1592                  * dsa GUID specified */
1593
1594                 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1595                                            &req10->destination_dsa_guid, 0,
1596                                            &ntds_dn);
1597                 if (ret != LDB_SUCCESS) {
1598                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1599                                   GUID_string(frame,
1600                                               &req10->destination_dsa_guid),
1601                                   ldb_errstring(b_state->sam_ctx)));
1602                         TALLOC_FREE(frame);
1603                         return WERR_DS_DRA_INTERNAL_ERROR;
1604                 }
1605
1606                 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1607                 if (!server_dn) {
1608                         TALLOC_FREE(frame);
1609                         return WERR_DS_DRA_INTERNAL_ERROR;
1610                 }
1611
1612                 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1613                                          "serverReference", &machine_dn);
1614                 if (ret != LDB_SUCCESS) {
1615                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1616                                   ldb_dn_get_linearized(server_dn),
1617                                   ldb_errstring(b_state->sam_ctx)));
1618                         TALLOC_FREE(frame);
1619                         return WERR_DS_DRA_INTERNAL_ERROR;
1620                 }
1621
1622                 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1623                                          "rIDSetReferences", &rid_set_dn);
1624                 if (ret != LDB_SUCCESS) {
1625                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1626                                   ldb_dn_get_linearized(server_dn),
1627                                   ldb_errstring(b_state->sam_ctx)));
1628                         TALLOC_FREE(frame);
1629                         return WERR_DS_DRA_INTERNAL_ERROR;
1630                 }
1631
1632
1633                 /* This first search will get the RID Manager, now get the RID set */
1634                 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1635                                                       &search_res2,
1636                                                       rid_set_dn, LDB_SCOPE_BASE,
1637                                                       collect_objects_attrs,
1638                                                       NULL);
1639                 if (ret != LDB_SUCCESS) {
1640                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1641                                   ldb_dn_get_linearized(rid_set_dn),
1642                                   ldb_errstring(b_state->sam_ctx)));
1643                         TALLOC_FREE(frame);
1644                         return WERR_DS_DRA_INTERNAL_ERROR;
1645                 }
1646
1647                 if (search_res2->count != 1) {
1648                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1649                                   ldb_dn_get_linearized(rid_set_dn),
1650                                   search_res2->count));
1651                         TALLOC_FREE(frame);
1652                         return WERR_DS_DRA_INTERNAL_ERROR;
1653                 }
1654
1655                 /* Finally get the server DN */
1656                 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1657                                                       &search_res3,
1658                                                       machine_dn, LDB_SCOPE_BASE,
1659                                                       collect_objects_attrs,
1660                                                       NULL);
1661                 if (ret != LDB_SUCCESS) {
1662                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1663                                   ldb_dn_get_linearized(server_dn),
1664                                   ldb_errstring(b_state->sam_ctx)));
1665                         TALLOC_FREE(frame);
1666                         return WERR_DS_DRA_INTERNAL_ERROR;
1667                 }
1668
1669                 if (search_res3->count != 1) {
1670                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1671                                   ldb_dn_get_linearized(server_dn),
1672                                   search_res3->count));
1673                         TALLOC_FREE(frame);
1674                         return WERR_DS_DRA_INTERNAL_ERROR;
1675                 }
1676
1677                 /* Now extend the original search_res with these answers */
1678                 (*search_res)->count = 3;
1679
1680                 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1681                                                      struct ldb_message *,
1682                                                      (*search_res)->count);
1683                 if ((*search_res)->msgs == NULL) {
1684                         TALLOC_FREE(frame);
1685                         return WERR_NOT_ENOUGH_MEMORY;
1686                 }
1687
1688
1689                 talloc_steal(mem_ctx, *search_res);
1690                 (*search_res)->msgs[1] =
1691                         talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1692                 (*search_res)->msgs[2] =
1693                         talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1694
1695                 TALLOC_FREE(frame);
1696                 return WERR_OK;
1697         }
1698         default:
1699                 /* TODO: implement extended op specific collection
1700                  * of objects. Right now we just normal procedure
1701                  * for collecting objects */
1702                 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1703         }
1704 }
1705
1706 static void dcesrv_drsuapi_update_highwatermark(const struct ldb_message *msg,
1707                                                 uint64_t max_usn,
1708                                                 struct drsuapi_DsReplicaHighWaterMark *hwm)
1709 {
1710         uint64_t uSN = ldb_msg_find_attr_as_uint64(msg, "uSNChanged", 0);
1711
1712         if (uSN > max_usn) {
1713                 /*
1714                  * Only report the max_usn we had at the start
1715                  * of the replication cycle.
1716                  *
1717                  * If this object has changed lately we better
1718                  * let the destination dsa refetch the change.
1719                  * This is better than the risk of loosing some
1720                  * objects or linked attributes.
1721                  */
1722                 return;
1723         }
1724
1725         if (uSN <= hwm->tmp_highest_usn) {
1726                 return;
1727         }
1728
1729         hwm->tmp_highest_usn = uSN;
1730         hwm->reserved_usn = 0;
1731 }
1732
1733 static WERROR dcesrv_drsuapi_anc_cache_add(struct db_context *anc_cache,
1734                                            const struct GUID *guid)
1735 {
1736         enum ndr_err_code ndr_err;
1737         uint8_t guid_buf[16] = { 0, };
1738         DATA_BLOB b = {
1739                 .data = guid_buf,
1740                 .length = sizeof(guid_buf),
1741         };
1742         TDB_DATA key = {
1743                 .dptr = b.data,
1744                 .dsize = b.length,
1745         };
1746         TDB_DATA val = {
1747                 .dptr = NULL,
1748                 .dsize = 0,
1749         };
1750         NTSTATUS status;
1751
1752         ndr_err = ndr_push_struct_into_fixed_blob(&b, guid,
1753                         (ndr_push_flags_fn_t)ndr_push_GUID);
1754         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1755                 return WERR_DS_DRA_INTERNAL_ERROR;
1756         }
1757
1758         status = dbwrap_store(anc_cache, key, val, TDB_REPLACE);
1759         if (!NT_STATUS_IS_OK(status)) {
1760                 return WERR_DS_DRA_INTERNAL_ERROR;
1761         }
1762
1763         return WERR_OK;
1764 }
1765
1766 static WERROR dcesrv_drsuapi_anc_cache_exists(struct db_context *anc_cache,
1767                                               const struct GUID *guid)
1768 {
1769         enum ndr_err_code ndr_err;
1770         uint8_t guid_buf[16] = { 0, };
1771         DATA_BLOB b = {
1772                 .data = guid_buf,
1773                 .length = sizeof(guid_buf),
1774         };
1775         TDB_DATA key = {
1776                 .dptr = b.data,
1777                 .dsize = b.length,
1778         };
1779         bool exists;
1780
1781         ndr_err = ndr_push_struct_into_fixed_blob(&b, guid,
1782                         (ndr_push_flags_fn_t)ndr_push_GUID);
1783         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1784                 return WERR_DS_DRA_INTERNAL_ERROR;
1785         }
1786
1787         exists = dbwrap_exists(anc_cache, key);
1788         if (!exists) {
1789                 return WERR_OBJECT_NOT_FOUND;
1790         }
1791
1792         return WERR_OBJECT_NAME_EXISTS;
1793 }
1794
1795 /* 
1796   drsuapi_DsGetNCChanges
1797
1798   see MS-DRSR 4.1.10.5.2 for basic logic of this function
1799 */
1800 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1801                                      struct drsuapi_DsGetNCChanges *r)
1802 {
1803         struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1804         int ret;
1805         uint32_t i, k;
1806         struct dsdb_schema *schema;
1807         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1808         struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1809         NTSTATUS status;
1810         DATA_BLOB session_key;
1811         WERROR werr;
1812         struct dcesrv_handle *h;
1813         struct drsuapi_bind_state *b_state;
1814         struct drsuapi_getncchanges_state *getnc_state;
1815         struct drsuapi_DsGetNCChangesRequest10 *req10;
1816         uint32_t options;
1817         uint32_t max_objects;
1818         uint32_t max_links;
1819         uint32_t link_count = 0;
1820         uint32_t link_total = 0;
1821         uint32_t link_given = 0;
1822         struct ldb_dn *search_dn = NULL;
1823         bool am_rodc, null_scope=false;
1824         enum security_user_level security_level;
1825         struct ldb_context *sam_ctx;
1826         struct dom_sid *user_sid;
1827         bool is_secret_request;
1828         bool is_gc_pas_request;
1829         struct drsuapi_changed_objects *changes;
1830         time_t max_wait;
1831         time_t start = time(NULL);
1832         bool max_wait_reached = false;
1833         bool has_get_all_changes = false;
1834         struct GUID invocation_id;
1835         static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
1836         struct dsdb_schema_prefixmap *pfm_remote = NULL;
1837         bool full = true;
1838         uint32_t *local_pas = NULL;
1839
1840         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1841         b_state = h->data;
1842
1843         sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1844
1845         invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1846
1847         *r->out.level_out = 6;
1848         /* TODO: linked attributes*/
1849         r->out.ctr->ctr6.linked_attributes_count = 0;
1850         r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
1851
1852         r->out.ctr->ctr6.object_count = 0;
1853         r->out.ctr->ctr6.nc_object_count = 0;
1854         r->out.ctr->ctr6.more_data = false;
1855         r->out.ctr->ctr6.uptodateness_vector = NULL;
1856         r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1857         r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1858         r->out.ctr->ctr6.first_object = NULL;
1859
1860         /* a RODC doesn't allow for any replication */
1861         ret = samdb_rodc(sam_ctx, &am_rodc);
1862         if (ret == LDB_SUCCESS && am_rodc) {
1863                 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1864                 return WERR_DS_DRA_SOURCE_DISABLED;
1865         }
1866
1867         /* Check request revision. 
1868          */
1869         switch (r->in.level) {
1870         case 8:
1871                 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1872                 if (req10 == NULL) {
1873                         return WERR_NOT_ENOUGH_MEMORY;
1874                 }
1875                 break;
1876         case 10:
1877                 req10 = &r->in.req->req10;
1878                 break;
1879         default:
1880                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1881                          r->in.level));
1882                 return WERR_REVISION_MISMATCH;
1883         }
1884
1885
1886         /* Perform access checks. */
1887         /* TODO: we need to support a sync on a specific non-root
1888          * DN. We'll need to find the real partition root here */
1889         ncRoot = req10->naming_context;
1890         if (ncRoot == NULL) {
1891                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1892                 return WERR_DS_DRA_INVALID_PARAMETER;
1893         }
1894
1895         if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1896                 return WERR_DS_DRA_INTERNAL_ERROR;
1897         }
1898
1899         if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1900             !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1901                 return WERR_DS_DRA_SOURCE_DISABLED;
1902         }
1903
1904         user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1905
1906         /* all clients must have GUID_DRS_GET_CHANGES */
1907         werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1908                                                  mem_ctx,
1909                                                  dce_call->conn->auth_state.session_info->security_token,
1910                                                  req10->naming_context,
1911                                                  GUID_DRS_GET_CHANGES);
1912         if (!W_ERROR_IS_OK(werr)) {
1913                 return werr;
1914         }
1915
1916         if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
1917                 full = req10->partial_attribute_set == NULL &&
1918                        req10->partial_attribute_set_ex == NULL;
1919         } else {
1920                 full = (options & DRSUAPI_DRS_WRIT_REP) != 0;
1921         }
1922
1923         werr = dsdb_schema_pfm_from_drsuapi_pfm(&req10->mapping_ctr, true,
1924                                                 mem_ctx, &pfm_remote, NULL);
1925
1926         /* We were supplied a partial attribute set, without the prefix map! */
1927         if (!full && !W_ERROR_IS_OK(werr)) {
1928                 if (req10->mapping_ctr.num_mappings == 0) {
1929                         /*
1930                          * Despite the fact MS-DRSR specifies that this shouldn't
1931                          * happen, Windows RODCs will in fact not provide a prefixMap.
1932                          */
1933                         DEBUG(5,(__location__ ": Failed to provide a remote prefixMap,"
1934                                  " falling back to local prefixMap\n"));
1935                 } else {
1936                         DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
1937                                  win_errstr(werr)));
1938                         return werr;
1939                 }
1940         }
1941
1942         /* allowed if the GC PAS and client has
1943            GUID_DRS_GET_FILTERED_ATTRIBUTES */
1944         werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, pfm_remote, &is_gc_pas_request);
1945         if (!W_ERROR_IS_OK(werr)) {
1946                 return werr;
1947         }
1948         if (is_gc_pas_request) {
1949                 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1950                                                          mem_ctx,
1951                                                          dce_call->conn->auth_state.session_info->security_token,
1952                                                          req10->naming_context,
1953                                                          GUID_DRS_GET_FILTERED_ATTRIBUTES);
1954                 if (W_ERROR_IS_OK(werr)) {
1955                         goto allowed;
1956                 }
1957         }
1958
1959         werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10,
1960                                                         pfm_remote,
1961                                                         &is_secret_request);
1962         if (!W_ERROR_IS_OK(werr)) {
1963                 return werr;
1964         }
1965         if (is_secret_request) {
1966                 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1967                                                          mem_ctx,
1968                                                          dce_call->conn->auth_state.session_info->security_token,
1969                                                          req10->naming_context,
1970                                                          GUID_DRS_GET_ALL_CHANGES);
1971                 if (!W_ERROR_IS_OK(werr)) {
1972                         /* Only bail if this is not a EXOP_REPL_SECRET */
1973                         if (req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1974                                 return werr;
1975                         }
1976                 } else {
1977                         has_get_all_changes = true;
1978                 }
1979         }
1980
1981 allowed:
1982         /* for non-administrator replications, check that they have
1983            given the correct source_dsa_invocation_id */
1984         security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1985                                                      samdb_domain_sid(sam_ctx));
1986         if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1987                 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1988                         /* we rely on this flag being unset for RODC requests */
1989                         req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1990                 }
1991         }
1992
1993         if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1994                 /* Ignore the _in_ uptpdateness vector*/
1995                 req10->uptodateness_vector = NULL;
1996         }
1997
1998         if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1999                 req10->source_dsa_invocation_id = invocation_id;
2000         }
2001
2002         if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
2003                 /*
2004                  * The given highwatermark is only valid relative to the
2005                  * specified source_dsa_invocation_id.
2006                  */
2007                 ZERO_STRUCT(req10->highwatermark);
2008         }
2009
2010         getnc_state = b_state->getncchanges_state;
2011
2012         /* see if a previous replication has been abandoned */
2013         if (getnc_state) {
2014                 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
2015                 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
2016                         DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
2017                                  ldb_dn_get_linearized(new_dn),
2018                                  ldb_dn_get_linearized(getnc_state->ncRoot_dn),
2019                                  ldb_dn_get_linearized(getnc_state->last_dn)));
2020                         talloc_free(getnc_state);
2021                         getnc_state = NULL;
2022                 }
2023         }
2024
2025         if (getnc_state) {
2026                 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
2027                                                          &req10->highwatermark);
2028                 if (ret != 0) {
2029                         DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
2030                                  "on DN %s %s highwatermark (last_dn %s)\n",
2031                                  ldb_dn_get_linearized(getnc_state->ncRoot_dn),
2032                                  (ret > 0) ? "older" : "newer",
2033                                  ldb_dn_get_linearized(getnc_state->last_dn)));
2034                         talloc_free(getnc_state);
2035                         getnc_state = NULL;
2036                 }
2037         }
2038
2039         if (getnc_state == NULL) {
2040                 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
2041                 if (getnc_state == NULL) {
2042                         return WERR_NOT_ENOUGH_MEMORY;
2043                 }
2044                 b_state->getncchanges_state = getnc_state;
2045                 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
2046                 if (getnc_state->ncRoot_dn == NULL) {
2047                         return WERR_NOT_ENOUGH_MEMORY;
2048                 }
2049
2050                 ret = dsdb_find_guid_by_dn(b_state->sam_ctx_system,
2051                                            getnc_state->ncRoot_dn,
2052                                            &getnc_state->ncRoot_guid);
2053                 if (ret != LDB_SUCCESS) {
2054                         DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
2055                                  ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
2056                         return WERR_DS_DRA_INTERNAL_ERROR;
2057                 }
2058                 ncRoot->guid = getnc_state->ncRoot_guid;
2059
2060                 /* find out if we are to replicate Schema NC */
2061                 ret = ldb_dn_compare_base(ldb_get_schema_basedn(b_state->sam_ctx),
2062                                           getnc_state->ncRoot_dn);
2063
2064                 getnc_state->is_schema_nc = (0 == ret);
2065
2066                 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2067                         r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
2068                 }
2069
2070                 /*
2071                  * This is the first replication cycle and it is
2072                  * a good place to handle extended operations
2073                  *
2074                  * FIXME: we don't fully support extended operations yet
2075                  */
2076                 switch (req10->extended_op) {
2077                 case DRSUAPI_EXOP_NONE:
2078                         break;
2079                 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
2080                         werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
2081                         W_ERROR_NOT_OK_RETURN(werr);
2082                         if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2083                                 return WERR_OK;
2084                         }
2085                         break;
2086                 case DRSUAPI_EXOP_REPL_SECRET:
2087                         werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
2088                                                         user_sid,
2089                                                         &r->out.ctr->ctr6,
2090                                                         has_get_all_changes);
2091                         r->out.result = werr;
2092                         W_ERROR_NOT_OK_RETURN(werr);
2093                         break;
2094                 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
2095                         werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2096                         W_ERROR_NOT_OK_RETURN(werr);
2097                         if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2098                                 return WERR_OK;
2099                         }
2100                         break;
2101                 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
2102                         werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2103                         W_ERROR_NOT_OK_RETURN(werr);
2104                         if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2105                                 return WERR_OK;
2106                         }
2107                         break;
2108                 case DRSUAPI_EXOP_FSMO_REQ_PDC:
2109                         werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
2110                         W_ERROR_NOT_OK_RETURN(werr);
2111                         if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
2112                                 return WERR_OK;
2113                         }
2114                         break;
2115                 case DRSUAPI_EXOP_REPL_OBJ:
2116                         werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
2117                         r->out.result = werr;
2118                         W_ERROR_NOT_OK_RETURN(werr);
2119                         break;
2120
2121                 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
2122
2123                         DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
2124                                  (unsigned)req10->extended_op));
2125                         return WERR_DS_DRA_NOT_SUPPORTED;
2126                 }
2127         }
2128
2129         if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
2130             ldb_dn_is_null(getnc_state->ncRoot_dn)) {
2131                 DEBUG(0,(__location__ ": Bad DN '%s'\n",
2132                          drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
2133                 return WERR_DS_DRA_INVALID_PARAMETER;
2134         }
2135
2136         ncRoot->guid = getnc_state->ncRoot_guid;
2137
2138         /* we need the session key for encrypting password attributes */
2139         status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
2140         if (!NT_STATUS_IS_OK(status)) {
2141                 DEBUG(0,(__location__ ": Failed to get session key\n"));
2142                 return WERR_DS_DRA_INTERNAL_ERROR;
2143         }
2144
2145         /* 
2146            TODO: MS-DRSR section 4.1.10.1.1
2147            Work out if this is the start of a new cycle */
2148
2149         if (getnc_state->guids == NULL) {
2150                 const char *extra_filter;
2151                 struct ldb_result *search_res = NULL;
2152                 static const struct drsuapi_DsReplicaCursorCtrEx empty_udv;
2153                 const struct drsuapi_DsReplicaCursorCtrEx *udv = NULL;
2154
2155                 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
2156
2157                 if (req10->uptodateness_vector != NULL) {
2158                         udv = req10->uptodateness_vector;
2159                 } else {
2160                         udv = &empty_udv;
2161                 }
2162
2163                 getnc_state->min_usn = req10->highwatermark.highest_usn;
2164                 for (i = 0; i < udv->count; i++) {
2165                         bool match;
2166                         const struct drsuapi_DsReplicaCursor *cur =
2167                                 &udv->cursors[i];
2168
2169                         match = GUID_equal(&invocation_id,
2170                                            &cur->source_dsa_invocation_id);
2171                         if (!match) {
2172                                 continue;
2173                         }
2174                         if (cur->highest_usn > getnc_state->min_usn) {
2175                                 getnc_state->min_usn = cur->highest_usn;
2176                         }
2177                         break;
2178                 }
2179                 getnc_state->max_usn = getnc_state->min_usn;
2180
2181                 getnc_state->final_udv = talloc_zero(getnc_state,
2182                                         struct drsuapi_DsReplicaCursor2CtrEx);
2183                 if (getnc_state->final_udv == NULL) {
2184                         return WERR_NOT_ENOUGH_MEMORY;
2185                 }
2186                 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
2187                                           getnc_state->final_udv);
2188                 if (!W_ERROR_IS_OK(werr)) {
2189                         return werr;
2190                 }
2191
2192                 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2193                         werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
2194                                                             search_dn, extra_filter,
2195                                                             &search_res);
2196                 } else {
2197                         werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
2198                                                                  &r->out.ctr->ctr6,
2199                                                                  search_dn, extra_filter,
2200                                                                  &search_res);
2201                 }
2202                 W_ERROR_NOT_OK_RETURN(werr);
2203
2204                 /* extract out the GUIDs list */
2205                 getnc_state->num_records = search_res ? search_res->count : 0;
2206                 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
2207                 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
2208
2209                 changes = talloc_array(getnc_state,
2210                                        struct drsuapi_changed_objects,
2211                                        getnc_state->num_records);
2212                 W_ERROR_HAVE_NO_MEMORY(changes);
2213
2214                 for (i=0; i<getnc_state->num_records; i++) {
2215                         changes[i].dn = search_res->msgs[i]->dn;
2216                         changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
2217                         changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
2218
2219                         if (changes[i].usn > getnc_state->max_usn) {
2220                                 getnc_state->max_usn = changes[i].usn;
2221                         }
2222                 }
2223
2224                 /* RID_ALLOC returns 3 objects in a fixed order */
2225                 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
2226                         /* Do nothing */
2227                 } else {
2228                         LDB_TYPESAFE_QSORT(changes,
2229                                            getnc_state->num_records,
2230                                            getnc_state,
2231                                            site_res_cmp_usn_order);
2232                 }
2233
2234                 for (i=0; i < getnc_state->num_records; i++) {
2235                         getnc_state->guids[i] = changes[i].guid;
2236                         if (GUID_all_zero(&getnc_state->guids[i])) {
2237                                 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
2238                                          ldb_dn_get_linearized(search_res->msgs[i]->dn)));
2239                                 return WERR_DS_DRA_INTERNAL_ERROR;
2240                         }
2241                 }
2242
2243                 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
2244                 getnc_state->final_hwm.reserved_usn = 0;
2245                 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
2246
2247                 talloc_free(search_res);
2248                 talloc_free(changes);
2249
2250                 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2251                         /* Do nothing */
2252                 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
2253                         getnc_state->anc_cache = db_open_rbt(getnc_state);
2254                         if (getnc_state->anc_cache == NULL) {
2255                                 return WERR_NOT_ENOUGH_MEMORY;
2256                         }
2257                 }
2258         }
2259
2260         if (req10->uptodateness_vector) {
2261                 /* make sure its sorted */
2262                 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
2263                                req10->uptodateness_vector->count,
2264                                drsuapi_DsReplicaCursor_compare);
2265         }
2266
2267         /* Prefix mapping */
2268         schema = dsdb_get_schema(sam_ctx, mem_ctx);
2269         if (!schema) {
2270                 DEBUG(0,("No schema in sam_ctx\n"));
2271                 return WERR_DS_DRA_INTERNAL_ERROR;
2272         }
2273
2274         r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
2275         if (r->out.ctr->ctr6.naming_context == NULL) {
2276                 return WERR_NOT_ENOUGH_MEMORY;
2277         }
2278         *r->out.ctr->ctr6.naming_context = *ncRoot;
2279
2280         /* find the SID if there is one */
2281         dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2282
2283         dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2284         r->out.ctr->ctr6.mapping_ctr = *ctr;
2285
2286         r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2287         r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2288
2289         r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2290         r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2291
2292         currentObject = &r->out.ctr->ctr6.first_object;
2293
2294         max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2295         /*
2296          * The client control here only applies in normal replication, not extended
2297          * operations, which return a fixed set, even if the caller
2298          * sets max_object_count == 0
2299          */
2300         if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2301                 /* use this to force single objects at a time, which is useful
2302                  * for working out what object is giving problems
2303                  */
2304                 if (req10->max_object_count < max_objects) {
2305                         max_objects = req10->max_object_count;
2306                 }
2307         }
2308         /*
2309          * TODO: work out how the maximum should be calculated
2310          */
2311         max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2312
2313         /*
2314          * Maximum time that we can spend in a getncchanges
2315          * in order to avoid timeout of the other part.
2316          * 10 seconds by default.
2317          */
2318         max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2319
2320         if (req10->partial_attribute_set != NULL) {
2321                 struct dsdb_syntax_ctx syntax_ctx;
2322                 uint32_t j = 0;
2323
2324                 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
2325                 syntax_ctx.pfm_remote = pfm_remote;
2326
2327                 local_pas = talloc_array(b_state, uint32_t, req10->partial_attribute_set->num_attids);
2328
2329                 for (j = 0; j < req10->partial_attribute_set->num_attids; j++) {
2330                         getncchanges_attid_remote_to_local(schema,
2331                                                            &syntax_ctx,
2332                                                            req10->partial_attribute_set->attids[j],
2333                                                            (enum drsuapi_DsAttributeId *)&local_pas[j],
2334                                                            NULL);
2335                 }
2336
2337                 LDB_TYPESAFE_QSORT(local_pas,
2338                                    req10->partial_attribute_set->num_attids,
2339                                    NULL,
2340                                    uint32_t_ptr_cmp);
2341         }
2342
2343         for (i=getnc_state->num_processed;
2344              i<getnc_state->num_records &&
2345                      !null_scope &&
2346                      (r->out.ctr->ctr6.object_count < max_objects)
2347                      && !max_wait_reached;
2348             i++) {
2349                 struct drsuapi_DsReplicaObjectListItemEx *new_objs = NULL;
2350                 struct drsuapi_DsReplicaObjectListItemEx *obj;
2351                 struct ldb_message *msg;
2352                 static const char * const msg_attrs[] = {
2353                                             "*",
2354                                             "nTSecurityDescriptor",
2355                                             "parentGUID",
2356                                             "replPropertyMetaData",
2357                                             DSDB_SECRET_ATTRIBUTES,
2358                                             NULL };
2359                 struct ldb_result *msg_res;
2360                 struct ldb_dn *msg_dn;
2361                 const struct GUID *next_anc_guid = NULL;
2362
2363                 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2364                 W_ERROR_HAVE_NO_MEMORY(obj);
2365
2366                 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2367                 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2368
2369
2370                 /* by re-searching here we avoid having a lot of full
2371                  * records in memory between calls to getncchanges
2372                  */
2373                 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2374                                                       msg_dn,
2375                                                       LDB_SCOPE_BASE, msg_attrs, NULL);
2376                 if (ret != LDB_SUCCESS) {
2377                         if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2378                                 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2379                                          ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2380                         }
2381                         talloc_free(obj);
2382                         continue;
2383                 }
2384
2385                 msg = msg_res->msgs[0];
2386
2387                 /*
2388                  * If it has already been added as an ancestor of
2389                  * an object, we don't need to do anything more,
2390                  * as we've already added the links.
2391                  */
2392                 if (getnc_state->anc_cache != NULL) {
2393                         werr = dcesrv_drsuapi_anc_cache_exists(getnc_state->anc_cache,
2394                                                                &getnc_state->guids[i]);
2395                         if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
2396                                 dcesrv_drsuapi_update_highwatermark(msg,
2397                                                 getnc_state->max_usn,
2398                                                 &r->out.ctr->ctr6.new_highwatermark);
2399                                 /* no attributes to send */
2400                                 talloc_free(obj);
2401                                 continue;
2402                         }
2403                 }
2404
2405                 max_wait_reached = (time(NULL) - start > max_wait);
2406
2407                 werr = get_nc_changes_build_object(obj, msg,
2408                                                    sam_ctx, getnc_state->ncRoot_dn,
2409                                                    getnc_state->is_schema_nc,
2410                                                    schema, &session_key, getnc_state->min_usn,
2411                                                    req10->replica_flags,
2412                                                    req10->partial_attribute_set,
2413                                                    req10->uptodateness_vector,
2414                                                    req10->extended_op,
2415                                                    max_wait_reached,
2416                                                    local_pas);
2417                 if (!W_ERROR_IS_OK(werr)) {
2418                         return werr;
2419                 }
2420
2421                 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2422                                                 getnc_state->ncRoot_dn,
2423                                                 getnc_state->is_schema_nc,
2424                                                 schema, getnc_state->min_usn,
2425                                                 req10->replica_flags,
2426                                                 msg,
2427                                                 &getnc_state->la_list,
2428                                                 &getnc_state->la_count,
2429                                                 req10->uptodateness_vector);
2430                 if (!W_ERROR_IS_OK(werr)) {
2431                         return werr;
2432                 }
2433
2434                 dcesrv_drsuapi_update_highwatermark(msg,
2435                                         getnc_state->max_usn,
2436                                         &r->out.ctr->ctr6.new_highwatermark);
2437
2438                 if (obj->meta_data_ctr == NULL) {
2439                         DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2440                                  ldb_dn_get_linearized(msg->dn)));
2441                         /* no attributes to send */
2442                         talloc_free(obj);
2443                         continue;
2444                 }
2445
2446                 new_objs = obj;
2447
2448                 if (getnc_state->anc_cache != NULL) {
2449                         werr = dcesrv_drsuapi_anc_cache_add(getnc_state->anc_cache,
2450                                                             &getnc_state->guids[i]);
2451                         if (!W_ERROR_IS_OK(werr)) {
2452                                 return werr;
2453                         }
2454
2455                         next_anc_guid = obj->parent_object_guid;
2456                 }
2457
2458                 while (next_anc_guid != NULL) {
2459                         struct drsuapi_DsReplicaObjectListItemEx *anc_obj = NULL;
2460                         struct ldb_message *anc_msg = NULL;
2461                         struct ldb_result *anc_res = NULL;
2462                         struct ldb_dn *anc_dn = NULL;
2463
2464                         werr = dcesrv_drsuapi_anc_cache_exists(getnc_state->anc_cache,
2465                                                                next_anc_guid);
2466                         if (W_ERROR_EQUAL(werr, WERR_OBJECT_NAME_EXISTS)) {
2467                                 /*
2468                                  * We don't need to send it twice.
2469                                  */
2470                                 break;
2471                         }
2472                         if (W_ERROR_IS_OK(werr)) {
2473                                 return WERR_INTERNAL_ERROR;
2474                         }
2475                         if (!W_ERROR_EQUAL(werr, WERR_OBJECT_NOT_FOUND)) {
2476                                 return werr;
2477                         }
2478                         werr = WERR_OK;
2479
2480                         anc_obj = talloc_zero(mem_ctx,
2481                                         struct drsuapi_DsReplicaObjectListItemEx);
2482                         if (anc_obj == NULL) {
2483                                 return WERR_NOT_ENOUGH_MEMORY;
2484                         }
2485
2486                         anc_dn = ldb_dn_new_fmt(anc_obj, sam_ctx, "<GUID=%s>",
2487                                         GUID_string(anc_obj, next_anc_guid));
2488                         if (anc_dn == NULL) {
2489                                 return WERR_NOT_ENOUGH_MEMORY;
2490                         }
2491
2492                         ret = drsuapi_search_with_extended_dn(sam_ctx, anc_obj,
2493                                                               &anc_res, anc_dn,
2494                                                               LDB_SCOPE_BASE,
2495                                                               msg_attrs, NULL);
2496                         if (ret != LDB_SUCCESS) {
2497                                 const char *anc_str = NULL;
2498                                 const char *obj_str = NULL;
2499
2500                                 anc_str = ldb_dn_get_extended_linearized(anc_obj,
2501                                                                          anc_dn,
2502                                                                          1);
2503                                 obj_str = ldb_dn_get_extended_linearized(anc_obj,
2504                                                                          msg->dn,
2505                                                                          1),
2506
2507                                 DBG_ERR("getncchanges: failed to fetch ANC "
2508                                         "DN %s for DN %s - %s\n",
2509                                         anc_str, obj_str,
2510                                         ldb_errstring(sam_ctx));
2511                                 return WERR_DS_DRA_INCONSISTENT_DIT;
2512                         }
2513
2514                         anc_msg = anc_res->msgs[0];
2515
2516                         werr = get_nc_changes_build_object(anc_obj, anc_msg,
2517                                                            sam_ctx,
2518                                                            getnc_state->ncRoot_dn,
2519                                                            getnc_state->is_schema_nc,
2520                                                            schema, &session_key,
2521                                                            getnc_state->min_usn,
2522                                                            req10->replica_flags,
2523                                                            req10->partial_attribute_set,
2524                                                            req10->uptodateness_vector,
2525                                                            req10->extended_op,
2526                                                            false, /* force_object_return */
2527                                                            local_pas);
2528                         if (!W_ERROR_IS_OK(werr)) {
2529                                 return werr;
2530                         }
2531
2532                         werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2533                                                         getnc_state->ncRoot_dn,
2534                                                         getnc_state->is_schema_nc,
2535                                                         schema, getnc_state->min_usn,
2536                                                         req10->replica_flags,
2537                                                         anc_msg,
2538                                                         &getnc_state->la_list,
2539                                                         &getnc_state->la_count,
2540                                                         req10->uptodateness_vector);
2541                         if (!W_ERROR_IS_OK(werr)) {
2542                                 return werr;
2543                         }
2544
2545                         /*
2546                          * Regardless of if we actually use it or not,
2547                          * we add it to the cache so we don't look at it again
2548                          */
2549                         werr = dcesrv_drsuapi_anc_cache_add(getnc_state->anc_cache,
2550                                                             next_anc_guid);
2551                         if (!W_ERROR_IS_OK(werr)) {
2552                                 return werr;
2553                         }
2554
2555                         /*
2556                          * Any ancestors which are below the highwatermark
2557                          * or uptodateness_vector shouldn't be added,
2558                          * but we still look further up the
2559                          * tree for ones which have been changed recently.
2560                          */
2561                         if (anc_obj->meta_data_ctr != NULL) {
2562                                 /*
2563                                  * prepend it to the list
2564                                  */
2565                                 anc_obj->next_object = new_objs;
2566                                 new_objs = anc_obj;
2567                         }
2568
2569                         anc_msg = NULL;
2570                         TALLOC_FREE(anc_res);
2571                         TALLOC_FREE(anc_dn);
2572
2573                         /*
2574                          * We may need to resolve more...
2575                          */
2576                         next_anc_guid = anc_obj->parent_object_guid;
2577                 }
2578
2579                 *currentObject = new_objs;
2580                 while (new_objs != NULL) {
2581                         r->out.ctr->ctr6.object_count += 1;
2582                         if (new_objs->next_object == NULL) {
2583                                 currentObject = &new_objs->next_object;
2584                         }
2585                         new_objs = new_objs->next_object;
2586                 }
2587
2588                 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
2589
2590                 talloc_free(getnc_state->last_dn);
2591                 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
2592
2593                 talloc_free(msg_res);
2594                 talloc_free(msg_dn);
2595         }
2596
2597         getnc_state->num_processed = i;
2598
2599         /* the client can us to call UpdateRefs on its behalf to
2600            re-establish monitoring of the NC */
2601         if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2602             !GUID_all_zero(&req10->destination_dsa_guid)) {
2603                 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2604                 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2605                          GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2606                 ureq.naming_context = ncRoot;
2607                 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2608                                                                    &req10->destination_dsa_guid);
2609                 if (!ureq.dest_dsa_dns_name) {
2610                         return WERR_NOT_ENOUGH_MEMORY;
2611                 }
2612                 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2613                 ureq.options = DRSUAPI_DRS_ADD_REF |
2614                         DRSUAPI_DRS_ASYNC_OP |
2615                         DRSUAPI_DRS_GETCHG_CHECK;
2616
2617                 /* we also need to pass through the
2618                    DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2619                    to send notifies using the GC SPN */
2620                 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2621
2622                 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2623                 if (!W_ERROR_IS_OK(werr)) {
2624                         DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2625                                  drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2626                                  win_errstr(werr)));
2627                 }
2628         }
2629
2630         /*
2631          * TODO:
2632          * This is just a guess, how to calculate the
2633          * number of linked attributes to send, we need to
2634          * find out how to do this right.
2635          */
2636         if (r->out.ctr->ctr6.object_count >= max_links) {
2637                 max_links = 0;
2638         } else {
2639                 max_links -= r->out.ctr->ctr6.object_count;
2640         }
2641
2642         link_total = getnc_state->la_count;
2643
2644         if (i < getnc_state->num_records) {
2645                 r->out.ctr->ctr6.more_data = true;
2646         } else {
2647                 /* sort the whole array the first time */
2648                 if (getnc_state->la_sorted == NULL) {
2649                         int j;
2650                         struct la_for_sorting *guid_array = talloc_array(getnc_state, struct la_for_sorting, getnc_state->la_count);
2651                         if (guid_array == NULL) {
2652                                 DEBUG(0, ("Out of memory allocating %u linked attributes for sorting", getnc_state->la_count));
2653                                 return WERR_NOT_ENOUGH_MEMORY;
2654                         }
2655                         for (j = 0; j < getnc_state->la_count; j++) {
2656                                 /* we need to get the target GUIDs to compare */
2657                                 struct dsdb_dn *dn;
2658                                 const struct drsuapi_DsReplicaLinkedAttribute *la = &getnc_state->la_list[j];
2659                                 const struct dsdb_attribute *schema_attrib;
2660                                 const struct ldb_val *target_guid;
2661                                 DATA_BLOB source_guid;
2662                                 TALLOC_CTX *frame = talloc_stackframe();
2663
2664                                 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2665
2666                                 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, frame, la->value.blob, &dn);
2667                                 if (!W_ERROR_IS_OK(werr)) {
2668                                         DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2669                                         TALLOC_FREE(frame);
2670                                         return werr;
2671                                 }
2672
2673                                 /* Extract the target GUID in NDR form */
2674                                 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2675                                 if (target_guid == NULL
2676                                     || target_guid->length != sizeof(guid_array[0].target_guid)) {
2677                                         status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2678                                 } else {
2679                                         /* Repack the source GUID as NDR for sorting */
2680                                         status = GUID_to_ndr_blob(&la->identifier->guid,
2681                                                                   frame,
2682                                                                   &source_guid);
2683                                 }
2684
2685                                 if (!NT_STATUS_IS_OK(status)
2686                                     || source_guid.length != sizeof(guid_array[0].source_guid)) {
2687                                         DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2688                                         TALLOC_FREE(frame);
2689                                         return ntstatus_to_werror(status);
2690                                 }
2691
2692                                 guid_array[j].link = &getnc_state->la_list[j];
2693                                 memcpy(guid_array[j].target_guid, target_guid->data,
2694                                        sizeof(guid_array[j].target_guid));
2695                                 memcpy(guid_array[j].source_guid, source_guid.data,
2696                                        sizeof(guid_array[j].source_guid));
2697                                 TALLOC_FREE(frame);
2698                         }
2699
2700                         LDB_TYPESAFE_QSORT(guid_array, getnc_state->la_count, NULL, linked_attribute_compare);
2701                         getnc_state->la_sorted = guid_array;
2702                 }
2703
2704                 link_count = getnc_state->la_count - getnc_state->la_idx;
2705                 link_count = MIN(max_links, link_count);
2706
2707                 r->out.ctr->ctr6.linked_attributes_count = link_count;
2708                 r->out.ctr->ctr6.linked_attributes = talloc_array(r->out.ctr, struct drsuapi_DsReplicaLinkedAttribute, link_count);
2709                 if (r->out.ctr->ctr6.linked_attributes == NULL) {
2710                         DEBUG(0, ("Out of memory allocating %u linked attributes for output", link_count));
2711                         return WERR_NOT_ENOUGH_MEMORY;
2712                 }
2713
2714                 for (k = 0; k < link_count; k++) {
2715                         r->out.ctr->ctr6.linked_attributes[k]
2716                                 = *getnc_state->la_sorted[getnc_state->la_idx + k].link;
2717                 }
2718
2719                 getnc_state->la_idx += link_count;
2720                 link_given = getnc_state->la_idx;
2721
2722                 if (getnc_state->la_idx < getnc_state->la_count) {
2723                         r->out.ctr->ctr6.more_data = true;
2724                 }
2725         }
2726
2727         if (req10->replica_flags & DRSUAPI_DRS_GET_NC_SIZE) {
2728                 /*
2729                  * TODO: This implementation is wrong
2730                  * we should find out the total number of
2731                  * objects and links in the whole naming context
2732                  * at the start of the cycle and return these
2733                  * values in each message.
2734                  *
2735                  * For now we keep our current strategy and return
2736                  * the number of objects for this cycle and the number
2737                  * of links we found so far during the cycle.
2738                  */
2739                 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2740                 r->out.ctr->ctr6.nc_linked_attributes_count = getnc_state->la_count;
2741         }
2742
2743         if (!r->out.ctr->ctr6.more_data) {
2744                 talloc_steal(mem_ctx, getnc_state->la_list);
2745
2746                 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2747                 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2748                                                         &getnc_state->final_udv);
2749
2750                 talloc_free(getnc_state);
2751                 b_state->getncchanges_state = NULL;
2752         } else {
2753                 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2754                                                          &r->out.ctr->ctr6.new_highwatermark);
2755                 if (ret == 0) {
2756                         /*
2757                          * We need to make sure that we never return the
2758                          * same highwatermark within the same replication
2759                          * cycle more than once. Otherwise we cannot detect
2760                          * when the client uses an unexptected highwatermark.
2761                          *
2762                          * This is a HACK which is needed because our
2763                          * object ordering is wrong and set tmp_highest_usn
2764                          * to a value that is higher than what we already
2765                          * sent to the client (destination dsa).
2766                          */
2767                         r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2768                 }
2769
2770                 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2771         }
2772
2773         if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2774                 r->out.ctr->ctr6.uptodateness_vector = NULL;
2775                 r->out.ctr->ctr6.nc_object_count = 0;
2776                 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2777         }
2778
2779         DEBUG(r->out.ctr->ctr6.more_data?4:2,
2780               ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2781                (unsigned long long)(req10->highwatermark.highest_usn+1),
2782                req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2783                r->out.ctr->ctr6.object_count,
2784                i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2785                r->out.ctr->ctr6.linked_attributes_count,
2786                link_given, link_total,
2787                dom_sid_string(mem_ctx, user_sid)));
2788
2789 #if 0
2790         if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2791                 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2792         }
2793 #endif
2794
2795         return WERR_OK;
2796 }