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