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