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