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