s4-drs: bring us much closer to the docs for DRS secret replication
[samba.git] / source4 / rpc_server / drsuapi / getncchanges.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implement the DRSUpdateRefs 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 "rpc_server/drsuapi/dcesrv_drsuapi.h"
31 #include "rpc_server/dcerpc_server_proto.h"
32 #include "../libcli/drsuapi/drsuapi.h"
33 #include "libcli/security/security.h"
34 #include "lib/util/binsearch.h"
35 #include "lib/util/tsort.h"
36 #include "auth/session.h"
37 #include "dsdb/common/util.h"
38
39 /*
40   build a DsReplicaObjectIdentifier from a ldb msg
41  */
42 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
43                                                                        struct ldb_message *msg)
44 {
45         struct drsuapi_DsReplicaObjectIdentifier *identifier;
46         struct dom_sid *sid;
47
48         identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
49         if (identifier == NULL) {
50                 return NULL;
51         }
52
53         identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
54         identifier->guid = samdb_result_guid(msg, "objectGUID");
55
56         sid = samdb_result_dom_sid(identifier, msg, "objectSid");
57         if (sid) {
58                 identifier->sid = *sid;
59         } else {
60                 ZERO_STRUCT(identifier->sid);
61         }
62         return identifier;
63 }
64
65 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
66 {
67         return GUID_compare(guid1, &guid2);
68 }
69
70 /*
71   see if we can filter an attribute using the uptodateness_vector
72  */
73 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
74                        const struct GUID *originating_invocation_id,
75                        uint64_t originating_usn)
76 {
77         const struct drsuapi_DsReplicaCursor *c;
78         if (udv == NULL) return false;
79         BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id, 
80                             originating_invocation_id, udv_compare, c);
81         if (c && originating_usn <= c->highest_usn) {
82                 return true;
83         }
84         return false;
85         
86 }
87
88 /* 
89   drsuapi_DsGetNCChanges for one object
90 */
91 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
92                                           struct ldb_message *msg,
93                                           struct ldb_context *sam_ctx,
94                                           struct ldb_dn *ncRoot_dn,
95                                           bool   is_schema_nc,
96                                           struct dsdb_schema *schema,
97                                           DATA_BLOB *session_key,
98                                           uint64_t highest_usn,
99                                           uint32_t replica_flags,
100                                           struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
101                                           enum drsuapi_DsExtendedOperation extended_op)
102 {
103         const struct ldb_val *md_value;
104         unsigned int i, n;
105         struct replPropertyMetaDataBlob md;
106         uint32_t rid = 0;
107         enum ndr_err_code ndr_err;
108         uint32_t *attids;
109         const char *rdn;
110         const struct dsdb_attribute *rdn_sa;
111         unsigned int instanceType;
112         struct dsdb_syntax_ctx syntax_ctx;
113
114         /* make dsdb sytanx context for conversions */
115         dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
116         syntax_ctx.is_schema_nc = is_schema_nc;
117
118         instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
119         if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
120                 obj->is_nc_prefix = true;
121                 obj->parent_object_guid = NULL;
122         } else {
123                 obj->is_nc_prefix = false;
124                 obj->parent_object_guid = talloc(obj, struct GUID);
125                 if (obj->parent_object_guid == NULL) {
126                         return WERR_DS_DRA_INTERNAL_ERROR;
127                 }
128                 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
129                 if (GUID_all_zero(obj->parent_object_guid)) {
130                         DEBUG(0,(__location__ ": missing parentGUID for %s\n",
131                                  ldb_dn_get_linearized(msg->dn)));
132                         return WERR_DS_DRA_INTERNAL_ERROR;
133                 }
134         }
135         obj->next_object = NULL;
136         
137         md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
138         if (!md_value) {
139                 /* nothing to send */
140                 return WERR_OK;
141         }
142
143         if (instanceType & INSTANCE_TYPE_UNINSTANT) {
144                 /* don't send uninstantiated objects */
145                 return WERR_OK;
146         }
147
148         ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
149                                        (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
150         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
151                 return WERR_DS_DRA_INTERNAL_ERROR;
152         }
153         
154         if (md.version != 1) {
155                 return WERR_DS_DRA_INTERNAL_ERROR;
156         }
157
158         rdn = ldb_dn_get_rdn_name(msg->dn);
159         if (rdn == NULL) {
160                 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
161                 return WERR_DS_DRA_INTERNAL_ERROR;
162         }
163
164         rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
165         if (rdn_sa == NULL) {
166                 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n", 
167                          rdn, ldb_dn_get_linearized(msg->dn)));
168                 return WERR_DS_DRA_INTERNAL_ERROR;
169         }
170
171         obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
172         attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
173
174         obj->object.identifier = get_object_identifier(obj, msg);
175         if (obj->object.identifier == NULL) {
176                 return WERR_NOMEM;
177         }
178         dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
179         
180         obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
181         for (n=i=0; i<md.ctr.ctr1.count; i++) {
182                 const struct dsdb_attribute *sa;
183                 bool force_attribute = false;
184
185                 /* if the attribute has not changed, and it is not the
186                    instanceType then don't include it */
187                 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
188                     extended_op != DRSUAPI_EXOP_REPL_SECRET &&
189                     md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType) continue;
190
191                 /* don't include the rDN */
192                 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
193
194                 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
195                 if (!sa) {
196                         DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n", 
197                                  (unsigned int)md.ctr.ctr1.array[i].attid, 
198                                  ldb_dn_get_linearized(msg->dn)));
199                         return WERR_DS_DRA_INTERNAL_ERROR;              
200                 }
201
202                 if (sa->linkID) {
203                         struct ldb_message_element *el;
204                         el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
205                         if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
206                                 /* don't send upgraded links inline */
207                                 continue;
208                         }
209                 }
210
211                 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
212                     !dsdb_attr_in_rodc_fas(sa)) {
213                         force_attribute = true;
214                         DEBUG(4,("Forcing attribute %s in %s\n",
215                                  sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
216                 }
217
218                 /* filter by uptodateness_vector */
219                 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType &&
220                     !force_attribute &&
221                     udv_filter(uptodateness_vector,
222                                &md.ctr.ctr1.array[i].originating_invocation_id, 
223                                md.ctr.ctr1.array[i].originating_usn)) {
224                         continue;
225                 }
226
227                 /*
228                  * If the recipient is a RODC, then we should only give
229                  * attributes from the RODC filtered attribute set
230                  *
231                  * TODO: This is not strictly correct, as it doesn't allow for administrators
232                  * to setup some users to transfer passwords to specific RODCs. To support that
233                  * we would instead remove this check and rely on extended ACL checking in the dsdb
234                  * acl module.
235                  */
236                 if (!(replica_flags & DRSUAPI_DRS_WRIT_REP) &&
237                     !force_attribute &&
238                     !dsdb_attr_in_rodc_fas(sa)) {
239                         DEBUG(4,("Skipping non-FAS attr %s in %s\n",
240                                  sa->lDAPDisplayName,
241                                  ldb_dn_get_linearized(msg->dn)));
242                         continue;
243                 }
244
245                 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
246                 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
247                 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
248                 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
249                 attids[n] = md.ctr.ctr1.array[i].attid;
250                 n++;
251         }
252
253         /* ignore it if its an empty change. Note that renames always
254          * change the 'name' attribute, so they won't be ignored by
255          * this */
256         if (n == 0 ||
257             (n == 1 && attids[0] == DRSUAPI_ATTRIBUTE_instanceType)) {
258                 talloc_free(obj->meta_data_ctr);
259                 obj->meta_data_ctr = NULL;
260                 return WERR_OK;
261         }
262
263         obj->meta_data_ctr->count = n;
264
265         obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
266         obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
267         obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
268                                                             obj->object.attribute_ctr.num_attributes);
269
270         /*
271          * Note that the meta_data array and the attributes array must
272          * be the same size and in the same order
273          */
274         for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
275                 struct ldb_message_element *el;
276                 WERROR werr;
277                 const struct dsdb_attribute *sa;
278         
279                 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
280                 if (!sa) {
281                         DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
282                         return WERR_DS_DRA_INTERNAL_ERROR;
283                 }
284
285                 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
286                 if (el == NULL) {
287                         /* this happens for attributes that have been removed */
288                         DEBUG(5,("No element '%s' for attributeID %u in message\n",
289                                  sa->lDAPDisplayName, attids[i]));
290                         ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
291                         obj->object.attribute_ctr.attributes[i].attid =
292                                         dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
293                 } else {
294                         werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
295                                                           &obj->object.attribute_ctr.attributes[i]);
296                         if (!W_ERROR_IS_OK(werr)) {
297                                 DEBUG(0,("Unable to convert %s to DRS object - %s\n", 
298                                          sa->lDAPDisplayName, win_errstr(werr)));
299                                 return werr;
300                         }
301                         /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
302                          * check if attribute is secret and send a null value
303                          */
304                         if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
305                                 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
306                                                                  &obj->meta_data_ctr->meta_data[i]);
307                         }
308                         /* some attributes needs to be encrypted
309                            before being sent */
310                         werr = drsuapi_encrypt_attribute(obj, session_key, rid, 
311                                                          &obj->object.attribute_ctr.attributes[i]);
312                         if (!W_ERROR_IS_OK(werr)) {
313                                 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n", 
314                                          sa->lDAPDisplayName, win_errstr(werr)));
315                                 return werr;
316                         }
317                 }
318         }
319
320         return WERR_OK;
321 }
322
323
324 /*
325   add one linked attribute from an object to the list of linked
326   attributes in a getncchanges request
327  */
328 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
329                                     struct ldb_context *sam_ctx,
330                                     const struct dsdb_schema *schema,
331                                     const struct dsdb_attribute *sa,
332                                     struct ldb_message *msg,
333                                     struct dsdb_dn *dsdb_dn,
334                                     struct drsuapi_DsReplicaLinkedAttribute **la_list,
335                                     uint32_t *la_count)
336 {
337         struct drsuapi_DsReplicaLinkedAttribute *la;
338         bool active;
339         NTSTATUS status;
340         WERROR werr;
341
342         (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
343         W_ERROR_HAVE_NO_MEMORY(*la_list);
344
345         la = &(*la_list)[*la_count];
346
347         la->identifier = get_object_identifier(*la_list, msg);
348         W_ERROR_HAVE_NO_MEMORY(la->identifier);
349
350         active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
351
352         la->attid = sa->attributeID_id;
353         la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
354
355         status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
356         if (!NT_STATUS_IS_OK(status)) {
357                 return ntstatus_to_werror(status);
358         }
359         status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
360         if (!NT_STATUS_IS_OK(status)) {
361                 return ntstatus_to_werror(status);
362         }
363         status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
364         if (!NT_STATUS_IS_OK(status)) {
365                 return ntstatus_to_werror(status);
366         }
367         status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
368         if (!NT_STATUS_IS_OK(status)) {
369                 return ntstatus_to_werror(status);
370         }
371         status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
372         if (!NT_STATUS_IS_OK(status)) {
373                 return ntstatus_to_werror(status);
374         }
375
376         werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
377         W_ERROR_NOT_OK_RETURN(werr);
378
379         (*la_count)++;
380         return WERR_OK;
381 }
382
383
384 /*
385   add linked attributes from an object to the list of linked
386   attributes in a getncchanges request
387  */
388 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
389                                        TALLOC_CTX *mem_ctx,
390                                        struct ldb_dn *ncRoot_dn,
391                                        struct dsdb_schema *schema,
392                                        uint64_t highest_usn,
393                                        uint32_t replica_flags,
394                                        struct ldb_message *msg,
395                                        struct drsuapi_DsReplicaLinkedAttribute **la_list,
396                                        uint32_t *la_count,
397                                        struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
398 {
399         unsigned int i;
400         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
401         uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
402
403         for (i=0; i<msg->num_elements; i++) {
404                 struct ldb_message_element *el = &msg->elements[i];
405                 const struct dsdb_attribute *sa;
406                 unsigned int j;
407
408                 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
409
410                 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
411                         /* we only want forward links */
412                         continue;
413                 }
414
415                 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
416                         /* its an old style link, it will have been
417                          * sent in the main replication data */
418                         continue;
419                 }
420
421                 for (j=0; j<el->num_values; j++) {
422                         struct dsdb_dn *dsdb_dn;
423                         uint64_t local_usn;
424                         NTSTATUS status;
425                         WERROR werr;
426
427                         dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
428                         if (dsdb_dn == NULL) {
429                                 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
430                                          el->name, ldb_dn_get_linearized(msg->dn)));
431                                 talloc_free(tmp_ctx);
432                                 return WERR_DS_DRA_INTERNAL_ERROR;
433                         }
434
435                         status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
436                         if (!NT_STATUS_IS_OK(status)) {
437                                 /* this can happen for attributes
438                                    given to us with old style meta
439                                    data */
440                                 continue;
441                         }
442
443                         if (local_usn > uSNChanged) {
444                                 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
445                                          el->name, ldb_dn_get_linearized(msg->dn)));
446                                 talloc_free(tmp_ctx);
447                                 return WERR_DS_DRA_INTERNAL_ERROR;
448                         }
449
450                         if (local_usn < highest_usn) {
451                                 continue;
452                         }
453
454                         werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
455                                                      dsdb_dn, la_list, la_count);
456                         if (!W_ERROR_IS_OK(werr)) {
457                                 talloc_free(tmp_ctx);
458                                 return werr;
459                         }
460                 }
461         }
462
463         talloc_free(tmp_ctx);
464         return WERR_OK;
465 }
466
467 /*
468   fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
469  */
470 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
471                                  struct ldb_dn *ncRoot_dn,
472                                  struct drsuapi_DsReplicaCursor2CtrEx *udv)
473 {
474         int ret;
475
476         udv->version = 2;
477         udv->reserved1 = 0;
478         udv->reserved2 = 0;
479
480         ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
481         if (ret != LDB_SUCCESS) {
482                 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
483                          ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
484                 return WERR_DS_DRA_INTERNAL_ERROR;
485         }
486         
487         return WERR_OK;
488 }
489
490
491 /* comparison function for linked attributes - see CompareLinks() in
492  * MS-DRSR section 4.1.10.5.17 */
493 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
494                                     const struct drsuapi_DsReplicaLinkedAttribute *la2,
495                                     struct ldb_context *sam_ctx)
496 {
497         int c;
498         WERROR werr;
499         TALLOC_CTX *tmp_ctx;
500         const struct dsdb_schema *schema;
501         const struct dsdb_attribute *schema_attrib;
502         struct dsdb_dn *dn1, *dn2;
503         struct GUID guid1, guid2;
504         NTSTATUS status;
505
506         c = GUID_compare(&la1->identifier->guid,
507                          &la2->identifier->guid);
508         if (c != 0) return c;
509
510         if (la1->attid != la2->attid) {
511                 return la1->attid < la2->attid? -1:1;
512         }
513
514         if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
515             (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
516                 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
517         }
518
519         /* we need to get the target GUIDs to compare */
520         tmp_ctx = talloc_new(sam_ctx);
521
522         schema = dsdb_get_schema(sam_ctx, tmp_ctx);
523         schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
524
525         werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
526         if (!W_ERROR_IS_OK(werr)) {
527                 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
528                 talloc_free(tmp_ctx);
529                 return 0;
530         }
531
532         werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
533         if (!W_ERROR_IS_OK(werr)) {
534                 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
535                 talloc_free(tmp_ctx);
536                 return 0;
537         }
538
539         status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
540         if (!NT_STATUS_IS_OK(status)) {
541                 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
542                 talloc_free(tmp_ctx);
543                 return 0;
544         }
545         status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
546         if (!NT_STATUS_IS_OK(status)) {
547                 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
548                 talloc_free(tmp_ctx);
549                 return 0;
550         }
551
552         talloc_free(tmp_ctx);
553
554         return GUID_compare(&guid1, &guid2);
555 }
556
557
558 /*
559   sort the objects we send by tree order
560  */
561 static int site_res_cmp_parent_order(struct ldb_message **m1, struct ldb_message **m2)
562 {
563         return ldb_dn_compare((*m2)->dn, (*m1)->dn);
564 }
565
566 /*
567   sort the objects we send first by uSNChanged
568  */
569 static int site_res_cmp_usn_order(struct ldb_message **m1, struct ldb_message **m2)
570 {
571         unsigned usnchanged1, usnchanged2;
572         unsigned cn1, cn2;
573         cn1 = ldb_dn_get_comp_num((*m1)->dn);
574         cn2 = ldb_dn_get_comp_num((*m2)->dn);
575         if (cn1 != cn2) {
576                 return cn1 > cn2 ? 1 : -1;
577         }
578         usnchanged1 = ldb_msg_find_attr_as_uint(*m1, "uSNChanged", 0);
579         usnchanged2 = ldb_msg_find_attr_as_uint(*m2, "uSNChanged", 0);
580         if (usnchanged1 == usnchanged2) {
581                 return 0;
582         }
583         return usnchanged1 > usnchanged2 ? 1 : -1;
584 }
585
586
587 /*
588   handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
589  */
590 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
591                                      TALLOC_CTX *mem_ctx,
592                                      struct drsuapi_DsGetNCChangesRequest8 *req8,
593                                      struct drsuapi_DsGetNCChangesCtr6 *ctr6)
594 {
595         struct ldb_dn *rid_manager_dn, *fsmo_role_dn, *req_dn;
596         int ret;
597         struct ldb_context *ldb = b_state->sam_ctx;
598         struct ldb_result *ext_res;
599         struct ldb_dn *base_dn;
600         struct dsdb_fsmo_extended_op *exop;
601
602         /*
603           steps:
604             - verify that the DN being asked for is the RID Manager DN
605             - verify that we are the RID Manager
606          */
607
608         /* work out who is the RID Manager */
609         ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
610         if (ret != LDB_SUCCESS) {
611                 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
612                 return WERR_DS_DRA_INTERNAL_ERROR;
613         }
614
615         req_dn = ldb_dn_new(mem_ctx, ldb, req8->naming_context->dn);
616         if (!req_dn ||
617             !ldb_dn_validate(req_dn) ||
618             ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
619                 /* that isn't the RID Manager DN */
620                 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
621                          req8->naming_context->dn));
622                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
623                 return WERR_OK;
624         }
625
626         /* find the DN of the RID Manager */
627         ret = samdb_reference_dn(ldb, mem_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
628         if (ret != LDB_SUCCESS) {
629                 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s\n",
630                          ldb_errstring(ldb)));
631                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
632                 return WERR_DS_DRA_INTERNAL_ERROR;
633         }
634
635         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
636                 /* we're not the RID Manager - go away */
637                 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
638                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
639                 return WERR_OK;
640         }
641
642         exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
643         W_ERROR_HAVE_NO_MEMORY(exop);
644
645         exop->fsmo_info = req8->fsmo_info;
646         exop->destination_dsa_guid = req8->destination_dsa_guid;
647
648         ret = ldb_transaction_start(ldb);
649         if (ret != LDB_SUCCESS) {
650                 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
651                          ldb_errstring(ldb)));
652                 return WERR_DS_DRA_INTERNAL_ERROR;
653         }
654
655         ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
656         if (ret != LDB_SUCCESS) {
657                 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
658                          ldb_errstring(ldb)));
659                 ldb_transaction_cancel(ldb);
660                 return WERR_DS_DRA_INTERNAL_ERROR;
661         }
662
663         ret = ldb_transaction_commit(ldb);
664         if (ret != LDB_SUCCESS) {
665                 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
666                          ldb_errstring(ldb)));
667                 return WERR_DS_DRA_INTERNAL_ERROR;
668         }
669
670         talloc_free(ext_res);
671
672         base_dn = ldb_get_default_basedn(ldb);
673
674         DEBUG(2,("Allocated RID pool for server %s\n",
675                  GUID_string(mem_ctx, &req8->destination_dsa_guid)));
676
677         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
678
679         return WERR_OK;
680 }
681
682 /*
683   return an array of SIDs from a ldb_message given an attribute name
684   assumes the SIDs are in extended DN format
685  */
686 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
687                                         struct ldb_message *msg,
688                                         TALLOC_CTX *mem_ctx,
689                                         const char *attr,
690                                         const struct dom_sid ***sids)
691 {
692         struct ldb_message_element *el;
693         int i;
694
695         el = ldb_msg_find_element(msg, attr);
696         if (!el) {
697                 *sids = NULL;
698                 return WERR_OK;
699         }
700
701         (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
702         W_ERROR_HAVE_NO_MEMORY(*sids);
703
704         for (i=0; i<el->num_values; i++) {
705                 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
706                 NTSTATUS status;
707                 struct dom_sid *sid;
708
709                 sid = talloc(*sids, struct dom_sid);
710                 W_ERROR_HAVE_NO_MEMORY(sid);
711                 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
712                 if (!NT_STATUS_IS_OK(status)) {
713                         return WERR_INTERNAL_DB_CORRUPTION;
714                 }
715                 (*sids)[i] = sid;
716         }
717         (*sids)[i] = NULL;
718
719         return WERR_OK;
720 }
721
722
723 /*
724   return an array of SIDs from a ldb_message given an attribute name
725   assumes the SIDs are in NDR form
726  */
727 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
728                                          struct ldb_message *msg,
729                                          TALLOC_CTX *mem_ctx,
730                                          const char *attr,
731                                          const struct dom_sid ***sids)
732 {
733         struct ldb_message_element *el;
734         int i;
735
736         el = ldb_msg_find_element(msg, attr);
737         if (!el) {
738                 *sids = NULL;
739                 return WERR_OK;
740         }
741
742         (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
743         W_ERROR_HAVE_NO_MEMORY(*sids);
744
745         for (i=0; i<el->num_values; i++) {
746                 enum ndr_err_code ndr_err;
747                 struct dom_sid *sid;
748
749                 sid = talloc(*sids, struct dom_sid);
750                 W_ERROR_HAVE_NO_MEMORY(sid);
751
752                 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
753                                                (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
754                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
755                         return WERR_INTERNAL_DB_CORRUPTION;
756                 }
757                 (*sids)[i] = sid;
758         }
759         (*sids)[i] = NULL;
760
761         return WERR_OK;
762 }
763
764 /*
765   see if any SIDs in list1 are in list2
766  */
767 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
768 {
769         int i, j;
770         /* do we ever have enough SIDs here to worry about O(n^2) ? */
771         for (i=0; list1[i]; i++) {
772                 for (j=0; list2[j]; j++) {
773                         if (dom_sid_equal(list1[i], list2[j])) {
774                                 return true;
775                         }
776                 }
777         }
778         return false;
779 }
780
781 /*
782   handle a DRSUAPI_EXOP_REPL_SECRET call
783  */
784 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
785                                        TALLOC_CTX *mem_ctx,
786                                        struct drsuapi_DsGetNCChangesRequest8 *req8,
787                                        struct dom_sid *user_sid,
788                                        struct drsuapi_DsGetNCChangesCtr6 *ctr6)
789 {
790         struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req8->naming_context;
791         struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
792         int ret;
793         const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
794         const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
795         struct ldb_result *rodc_res, *obj_res;
796         const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
797         WERROR werr;
798
799         DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n", ncRoot->dn));
800
801         /*
802          * we need to work out if we will allow this RODC to
803          * replicate the secrets for this object
804          *
805          * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
806          * of this function
807          */
808
809         if (b_state->sam_ctx_system == NULL) {
810                 /* this operation needs system level access */
811                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
812                 return WERR_DS_DRA_SOURCE_DISABLED;
813         }
814
815         obj_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx_system, ncRoot->dn);
816         if (!ldb_dn_validate(obj_dn)) goto failed;
817
818         rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
819                                  dom_sid_string(mem_ctx, user_sid));
820         if (!ldb_dn_validate(rodc_dn)) goto failed;
821
822         /* do the two searches we need */
823         ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
824                              DSDB_SEARCH_SHOW_EXTENDED_DN);
825         if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
826
827         ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
828         if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
829
830         /* if the object SID is equal to the user_sid, allow */
831         if (dom_sid_equal(user_sid,
832                           samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
833                 goto allowed;
834         }
835
836         /* an RODC is allowed to get its own krbtgt account secrets */
837         krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
838                                          rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
839         if (krbtgt_link_dn != NULL &&
840             ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
841                 goto allowed;
842         }
843
844         /* but it isn't allowed to get anyone elses krbtgt secrets */
845         if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
846                             obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
847                 goto denied;
848         }
849
850         if (samdb_result_uint(obj_res->msgs[0], "UserAccountControl", 0) &
851             UF_INTERDOMAIN_TRUST_ACCOUNT) {
852                 goto denied;
853         }
854
855         werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
856                                          mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
857         if (!W_ERROR_IS_OK(werr)) {
858                 goto denied;
859         }
860
861         werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
862                                          mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
863         if (!W_ERROR_IS_OK(werr)) {
864                 goto denied;
865         }
866
867         werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
868                                          mem_ctx, "tokenGroups", &token_sids);
869         if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
870                 goto denied;
871         }
872
873         if (never_reveal_sids &&
874             sid_list_match(token_sids, never_reveal_sids)) {
875                 goto denied;
876         }
877
878         if (reveal_sids &&
879             sid_list_match(token_sids, reveal_sids)) {
880                 goto allowed;
881         }
882
883         /* default deny */
884 denied:
885         DEBUG(2,(__location__ ": Denied RODC secret replication for %s by RODC %s\n",
886                  ncRoot->dn, ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
887         ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
888         return WERR_ACCESS_DENIED;
889
890 allowed:
891         DEBUG(2,(__location__ ": Allowed RODC secret replication for %s by RODC %s\n",
892                  ncRoot->dn, ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
893         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
894         req8->highwatermark.highest_usn = 0;
895         return WERR_OK;
896
897 failed:
898         DEBUG(2,(__location__ ": Failed RODC secret replication for %s by RODC %s\n",
899                  ncRoot->dn, dom_sid_string(mem_ctx, user_sid)));
900         ctr6->extended_ret = DRSUAPI_EXOP_ERR_DIR_ERROR;
901         return WERR_DS_DRA_SOURCE_DISABLED;
902 }
903
904
905
906 /* state of a partially completed getncchanges call */
907 struct drsuapi_getncchanges_state {
908         struct ldb_result *site_res;
909         uint32_t num_sent;
910         struct ldb_dn *ncRoot_dn;
911         bool is_schema_nc;
912         uint64_t min_usn;
913         uint64_t highest_usn;
914         struct ldb_dn *last_dn;
915         struct drsuapi_DsReplicaLinkedAttribute *la_list;
916         uint32_t la_count;
917         bool la_sorted;
918         uint32_t la_idx;
919         struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
920 };
921
922 /* 
923   drsuapi_DsGetNCChanges
924
925   see MS-DRSR 4.1.10.5.2 for basic logic of this function
926 */
927 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
928                                      struct drsuapi_DsGetNCChanges *r)
929 {
930         struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
931         int ret;
932         unsigned int i;
933         struct dsdb_schema *schema;
934         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
935         struct drsuapi_DsReplicaObjectListItemEx **currentObject;
936         NTSTATUS status;
937         DATA_BLOB session_key;
938         const char *attrs[] = { "*", "distinguishedName",
939                                 "nTSecurityDescriptor",
940                                 "parentGUID",
941                                 "replPropertyMetaData",
942                                 "unicodePwd",
943                                 "dBCSPwd",
944                                 "ntPwdHistory",
945                                 "lmPwdHistory",
946                                 "supplementalCredentials",
947                                 NULL };
948         WERROR werr;
949         struct dcesrv_handle *h;
950         struct drsuapi_bind_state *b_state;     
951         struct drsuapi_getncchanges_state *getnc_state;
952         struct drsuapi_DsGetNCChangesRequest8 *req8;
953         uint32_t options;
954         uint32_t max_objects;
955         uint32_t max_links;
956         uint32_t link_count = 0;
957         uint32_t link_total = 0;
958         uint32_t link_given = 0;
959         struct ldb_dn *search_dn = NULL;
960         bool am_rodc, null_scope=false;
961         enum security_user_level security_level;
962         struct ldb_context *sam_ctx;
963         struct dom_sid *user_sid;
964
965         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
966         b_state = h->data;
967
968         sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
969
970         *r->out.level_out = 6;
971         /* TODO: linked attributes*/
972         r->out.ctr->ctr6.linked_attributes_count = 0;
973         r->out.ctr->ctr6.linked_attributes = NULL;
974
975         r->out.ctr->ctr6.object_count = 0;
976         r->out.ctr->ctr6.nc_object_count = 0;
977         r->out.ctr->ctr6.more_data = false;
978         r->out.ctr->ctr6.uptodateness_vector = NULL;
979
980         /* a RODC doesn't allow for any replication */
981         ret = samdb_rodc(sam_ctx, &am_rodc);
982         if (ret == LDB_SUCCESS && am_rodc) {
983                 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
984                 return WERR_DS_DRA_SOURCE_DISABLED;
985         }
986
987         /* Check request revision. 
988            TODO: Adding mappings to req8 from the other levels
989          */
990         if (r->in.level != 8) {
991                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
992                          r->in.level));
993                 return WERR_REVISION_MISMATCH;
994         }
995
996         req8 = &r->in.req->req8;
997
998         /* Perform access checks. */
999         /* TODO: we need to support a sync on a specific non-root
1000          * DN. We'll need to find the real partition root here */
1001         ncRoot = req8->naming_context;
1002         if (ncRoot == NULL) {
1003                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1004                 return WERR_DS_DRA_INVALID_PARAMETER;
1005         }
1006
1007         if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1008                 return WERR_DS_DRA_INTERNAL_ERROR;
1009         }
1010         
1011         if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1012             !(req8->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1013                 return WERR_DS_DRA_SOURCE_DISABLED;
1014         }
1015
1016         werr = drs_security_level_check(dce_call, "DsGetNCChanges", SECURITY_RO_DOMAIN_CONTROLLER,
1017                                         samdb_domain_sid(sam_ctx));
1018         if (!W_ERROR_IS_OK(werr)) {
1019                 return werr;
1020         }
1021
1022         user_sid = dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1023
1024
1025         /* for non-administrator replications, check that they have
1026            given the correct source_dsa_invocation_id */
1027         security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1028                                                      samdb_domain_sid(sam_ctx));
1029         if (security_level == SECURITY_RO_DOMAIN_CONTROLLER &&
1030             (req8->replica_flags & DRSUAPI_DRS_WRIT_REP) &&
1031             req8->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1032                 DEBUG(3,(__location__ ": Removing WRIT_REP flag for replication by RODC %s\n",
1033                          dom_sid_string(mem_ctx, user_sid)));
1034                 req8->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1035         }
1036
1037
1038         if (req8->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1039                 /* Ignore the _in_ uptpdateness vector*/
1040                 req8->uptodateness_vector = NULL;
1041         } 
1042
1043         /* we don't yet support extended operations */
1044         switch (req8->extended_op) {
1045         case DRSUAPI_EXOP_NONE:
1046                 break;
1047
1048         case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1049                 werr = getncchanges_rid_alloc(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1050                 W_ERROR_NOT_OK_RETURN(werr);
1051                 search_dn = ldb_get_default_basedn(sam_ctx);
1052                 break;
1053
1054         case DRSUAPI_EXOP_REPL_SECRET:
1055                 werr = getncchanges_repl_secret(b_state, mem_ctx, req8, user_sid, &r->out.ctr->ctr6);
1056                 if (W_ERROR_EQUAL(werr, WERR_ACCESS_DENIED)) {
1057                         null_scope = true;
1058                 } else {
1059                         W_ERROR_NOT_OK_RETURN(werr);
1060                 }
1061                 break;
1062
1063         case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1064         case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1065         case DRSUAPI_EXOP_FSMO_REQ_PDC:
1066         case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1067         case DRSUAPI_EXOP_REPL_OBJ:
1068                 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1069                          (unsigned)req8->extended_op));
1070                 return WERR_DS_DRA_NOT_SUPPORTED;
1071         }
1072
1073         getnc_state = b_state->getncchanges_state;
1074
1075         /* see if a previous replication has been abandoned */
1076         if (getnc_state) {
1077                 struct ldb_dn *new_dn = ldb_dn_new(getnc_state, sam_ctx, ncRoot->dn);
1078                 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1079                         DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1080                                  ldb_dn_get_linearized(new_dn),
1081                                  ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1082                                  ldb_dn_get_linearized(getnc_state->last_dn)));
1083                         talloc_free(getnc_state);
1084                         getnc_state = NULL;
1085                 }
1086         }
1087
1088         if (getnc_state == NULL) {
1089                 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1090                 if (getnc_state == NULL) {
1091                         return WERR_NOMEM;
1092                 }
1093                 b_state->getncchanges_state = getnc_state;
1094                 getnc_state->ncRoot_dn = ldb_dn_new(getnc_state, sam_ctx, ncRoot->dn);
1095
1096                 /* find out if we are to replicate Schema NC */
1097                 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1098                                      ldb_get_schema_basedn(b_state->sam_ctx));
1099                 getnc_state->is_schema_nc = (0 == ret);
1100         }
1101
1102         if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1103             ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1104                 DEBUG(0,(__location__ ": Bad DN '%s'\n", ncRoot->dn));
1105                 return WERR_DS_DRA_INVALID_PARAMETER;
1106         }
1107
1108         /* we need the session key for encrypting password attributes */
1109         status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1110         if (!NT_STATUS_IS_OK(status)) {
1111                 DEBUG(0,(__location__ ": Failed to get session key\n"));
1112                 return WERR_DS_DRA_INTERNAL_ERROR;              
1113         }
1114
1115         /* 
1116            TODO: MS-DRSR section 4.1.10.1.1
1117            Work out if this is the start of a new cycle */
1118
1119         if (getnc_state->site_res == NULL) {
1120                 char* search_filter;
1121                 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1122                 const char *extra_filter;
1123
1124                 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1125
1126                 getnc_state->min_usn = req8->highwatermark.highest_usn;
1127
1128                 /* Construct response. */
1129                 search_filter = talloc_asprintf(mem_ctx,
1130                                                 "(uSNChanged>=%llu)",
1131                                                 (unsigned long long)(getnc_state->min_usn+1));
1132         
1133                 if (extra_filter) {
1134                         search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1135                 }
1136
1137                 if (req8->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1138                         search_filter = talloc_asprintf(mem_ctx,
1139                                                         "(&%s(isCriticalSystemObject=TRUE))",
1140                                                         search_filter);
1141                 }
1142                 
1143                 if (req8->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1144                         scope = LDB_SCOPE_BASE;
1145                 }
1146                 
1147                 if (!search_dn) {
1148                         search_dn = getnc_state->ncRoot_dn;
1149                 }
1150
1151                 DEBUG(1,(__location__ ": getncchanges on %s using filter %s\n",
1152                          ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1153                 ret = drsuapi_search_with_extended_dn(sam_ctx, getnc_state, &getnc_state->site_res,
1154                                                       search_dn, scope, attrs,
1155                                                       search_filter);
1156                 if (ret != LDB_SUCCESS) {
1157                         return WERR_DS_DRA_INTERNAL_ERROR;
1158                 }
1159
1160                 if (req8->replica_flags & DRSUAPI_DRS_GET_ANC) {
1161                         TYPESAFE_QSORT(getnc_state->site_res->msgs,
1162                                        getnc_state->site_res->count,
1163                                        site_res_cmp_parent_order);
1164                 } else {
1165                         TYPESAFE_QSORT(getnc_state->site_res->msgs,
1166                                        getnc_state->site_res->count,
1167                                        site_res_cmp_usn_order);
1168                 }
1169
1170                 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req8->uptodateness_vector);
1171                 if (getnc_state->uptodateness_vector) {
1172                         /* make sure its sorted */
1173                         TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1174                                        getnc_state->uptodateness_vector->count,
1175                                        drsuapi_DsReplicaCursor_compare);
1176                 }
1177         }
1178
1179         /* Prefix mapping */
1180         schema = dsdb_get_schema(sam_ctx, mem_ctx);
1181         if (!schema) {
1182                 DEBUG(0,("No schema in sam_ctx\n"));
1183                 return WERR_DS_DRA_INTERNAL_ERROR;
1184         }
1185
1186         r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1187         *r->out.ctr->ctr6.naming_context = *ncRoot;
1188
1189         if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1190                                  &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1191                 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1192                          ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1193                 return WERR_DS_DRA_INTERNAL_ERROR;
1194         }
1195
1196         /* find the SID if there is one */
1197         dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1198
1199         dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1200         r->out.ctr->ctr6.mapping_ctr = *ctr;
1201
1202         r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1203         r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1204
1205         r->out.ctr->ctr6.old_highwatermark = req8->highwatermark;
1206         r->out.ctr->ctr6.new_highwatermark = req8->highwatermark;
1207
1208         r->out.ctr->ctr6.first_object = NULL;
1209         currentObject = &r->out.ctr->ctr6.first_object;
1210
1211         /* use this to force single objects at a time, which is useful
1212          * for working out what object is giving problems
1213          */
1214         max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1215         if (req8->max_object_count < max_objects) {
1216                 max_objects = req8->max_object_count;
1217         }
1218         /*
1219          * TODO: work out how the maximum should be calculated
1220          */
1221         max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1222
1223         for (i=getnc_state->num_sent;
1224              i<getnc_state->site_res->count &&
1225                      !null_scope &&
1226                      (r->out.ctr->ctr6.object_count < max_objects);
1227             i++) {
1228                 int uSN;
1229                 struct drsuapi_DsReplicaObjectListItemEx *obj;
1230                 struct ldb_message *msg = getnc_state->site_res->msgs[i];
1231
1232                 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1233
1234                 werr = get_nc_changes_build_object(obj, msg,
1235                                                    sam_ctx, getnc_state->ncRoot_dn,
1236                                                    getnc_state->is_schema_nc,
1237                                                    schema, &session_key, getnc_state->min_usn,
1238                                                    req8->replica_flags, getnc_state->uptodateness_vector,
1239                                                    req8->extended_op);
1240                 if (!W_ERROR_IS_OK(werr)) {
1241                         return werr;
1242                 }
1243
1244                 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1245                                                 getnc_state->ncRoot_dn,
1246                                                 schema, getnc_state->min_usn,
1247                                                 req8->replica_flags,
1248                                                 msg,
1249                                                 &getnc_state->la_list,
1250                                                 &getnc_state->la_count,
1251                                                 getnc_state->uptodateness_vector);
1252                 if (!W_ERROR_IS_OK(werr)) {
1253                         return werr;
1254                 }
1255
1256                 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1257                 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1258                         r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1259                 }
1260                 if (uSN > getnc_state->highest_usn) {
1261                         getnc_state->highest_usn = uSN;
1262                 }
1263
1264                 if (obj->meta_data_ctr == NULL) {
1265                         DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1266                                  ldb_dn_get_linearized(msg->dn)));
1267                         /* no attributes to send */
1268                         talloc_free(obj);
1269                         continue;
1270                 }
1271
1272                 r->out.ctr->ctr6.object_count++;
1273                 
1274                 *currentObject = obj;
1275                 currentObject = &obj->next_object;
1276
1277                 talloc_free(getnc_state->last_dn);
1278                 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1279
1280                 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1281         }
1282
1283         getnc_state->num_sent += r->out.ctr->ctr6.object_count;
1284
1285         r->out.ctr->ctr6.nc_object_count = getnc_state->site_res->count;
1286
1287         /* the client can us to call UpdateRefs on its behalf to
1288            re-establish monitoring of the NC */
1289         if ((req8->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1290             !GUID_all_zero(&req8->destination_dsa_guid)) {
1291                 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1292                 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1293                          GUID_string(mem_ctx, &req8->destination_dsa_guid)));
1294                 ureq.naming_context = ncRoot;
1295                 ureq.dest_dsa_dns_name = talloc_asprintf(mem_ctx, "%s._msdcs.%s",
1296                                                          GUID_string(mem_ctx, &req8->destination_dsa_guid),
1297                                                          lpcfg_realm(dce_call->conn->dce_ctx->lp_ctx));
1298                 if (!ureq.dest_dsa_dns_name) {
1299                         return WERR_NOMEM;
1300                 }
1301                 ureq.dest_dsa_guid = req8->destination_dsa_guid;
1302                 ureq.options = DRSUAPI_DRS_ADD_REF |
1303                         DRSUAPI_DRS_ASYNC_OP |
1304                         DRSUAPI_DRS_GETCHG_CHECK;
1305                 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1306                 if (!W_ERROR_IS_OK(werr)) {
1307                         DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1308                                  win_errstr(werr)));
1309                 }
1310         }
1311
1312         /*
1313          * TODO:
1314          * This is just a guess, how to calculate the
1315          * number of linked attributes to send, we need to
1316          * find out how to do this right.
1317          */
1318         if (r->out.ctr->ctr6.object_count >= max_links) {
1319                 max_links = 0;
1320         } else {
1321                 max_links -= r->out.ctr->ctr6.object_count;
1322         }
1323
1324         link_total = getnc_state->la_count;
1325
1326         if (i < getnc_state->site_res->count) {
1327                 r->out.ctr->ctr6.more_data = true;
1328         } else {
1329                 /* sort the whole array the first time */
1330                 if (!getnc_state->la_sorted) {
1331                         LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1332                                            sam_ctx, linked_attribute_compare);
1333                         getnc_state->la_sorted = true;
1334                 }
1335
1336                 link_count = getnc_state->la_count - getnc_state->la_idx;
1337                 link_count = MIN(max_links, link_count);
1338
1339                 r->out.ctr->ctr6.linked_attributes_count = link_count;
1340                 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1341
1342                 getnc_state->la_idx += link_count;
1343                 link_given = getnc_state->la_idx;
1344
1345                 if (getnc_state->la_idx < getnc_state->la_count) {
1346                         r->out.ctr->ctr6.more_data = true;
1347                 }
1348         }
1349
1350         if (!r->out.ctr->ctr6.more_data) {
1351                 talloc_steal(mem_ctx, getnc_state->la_list);
1352
1353                 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1354                 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1355
1356                 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1357                                           r->out.ctr->ctr6.uptodateness_vector);
1358                 if (!W_ERROR_IS_OK(werr)) {
1359                         return werr;
1360                 }
1361
1362                 talloc_free(getnc_state);
1363                 b_state->getncchanges_state = NULL;
1364         }
1365
1366         if (req8->extended_op != DRSUAPI_EXOP_NONE) {
1367                 r->out.ctr->ctr6.uptodateness_vector = NULL;
1368                 r->out.ctr->ctr6.nc_object_count = 0;
1369                 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1370         }
1371
1372         DEBUG(r->out.ctr->ctr6.more_data?2:1,
1373               ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u)\n",
1374                (unsigned long long)(req8->highwatermark.highest_usn+1),
1375                req8->replica_flags, ncRoot->dn,
1376                r->out.ctr->ctr6.object_count,
1377                i, r->out.ctr->ctr6.more_data?getnc_state->site_res->count:i,
1378                r->out.ctr->ctr6.linked_attributes_count,
1379                link_given, link_total));
1380
1381 #if 0
1382         if (!r->out.ctr->ctr6.more_data) {
1383                 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
1384         }
1385 #endif
1386
1387         return WERR_OK;
1388 }