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