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