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