s4-drs: make getncchanges debug less verbose
[samba.git] / source4 / rpc_server / drsuapi / getncchanges.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implement the DSGetNCChanges call
5
6    Copyright (C) Anatoliy Atanasov 2009
7    Copyright (C) Andrew Tridgell 2009
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         /*
656          * FIXME (kim): this is a temp hack to return just few object,
657          * but not the whole domain NC.
658          * We should remove this hack and implement a 'scope'
659          * building function to return just the set of object
660          * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
661          */
662         ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &req8->highwatermark.highest_usn);
663
664         ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
665         if (ret != LDB_SUCCESS) {
666                 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
667                          ldb_errstring(ldb)));
668                 ldb_transaction_cancel(ldb);
669                 return WERR_DS_DRA_INTERNAL_ERROR;
670         }
671
672         ret = ldb_transaction_commit(ldb);
673         if (ret != LDB_SUCCESS) {
674                 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
675                          ldb_errstring(ldb)));
676                 return WERR_DS_DRA_INTERNAL_ERROR;
677         }
678
679         talloc_free(ext_res);
680
681         base_dn = ldb_get_default_basedn(ldb);
682
683         DEBUG(2,("Allocated RID pool for server %s\n",
684                  GUID_string(mem_ctx, &req8->destination_dsa_guid)));
685
686         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
687
688         return WERR_OK;
689 }
690
691 /*
692   return an array of SIDs from a ldb_message given an attribute name
693   assumes the SIDs are in extended DN format
694  */
695 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
696                                         struct ldb_message *msg,
697                                         TALLOC_CTX *mem_ctx,
698                                         const char *attr,
699                                         const struct dom_sid ***sids)
700 {
701         struct ldb_message_element *el;
702         unsigned int i;
703
704         el = ldb_msg_find_element(msg, attr);
705         if (!el) {
706                 *sids = NULL;
707                 return WERR_OK;
708         }
709
710         (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
711         W_ERROR_HAVE_NO_MEMORY(*sids);
712
713         for (i=0; i<el->num_values; i++) {
714                 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
715                 NTSTATUS status;
716                 struct dom_sid *sid;
717
718                 sid = talloc(*sids, struct dom_sid);
719                 W_ERROR_HAVE_NO_MEMORY(sid);
720                 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
721                 if (!NT_STATUS_IS_OK(status)) {
722                         return WERR_INTERNAL_DB_CORRUPTION;
723                 }
724                 (*sids)[i] = sid;
725         }
726         (*sids)[i] = NULL;
727
728         return WERR_OK;
729 }
730
731
732 /*
733   return an array of SIDs from a ldb_message given an attribute name
734   assumes the SIDs are in NDR form
735  */
736 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
737                                          struct ldb_message *msg,
738                                          TALLOC_CTX *mem_ctx,
739                                          const char *attr,
740                                          const struct dom_sid ***sids)
741 {
742         struct ldb_message_element *el;
743         unsigned int i;
744
745         el = ldb_msg_find_element(msg, attr);
746         if (!el) {
747                 *sids = NULL;
748                 return WERR_OK;
749         }
750
751         (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
752         W_ERROR_HAVE_NO_MEMORY(*sids);
753
754         for (i=0; i<el->num_values; i++) {
755                 enum ndr_err_code ndr_err;
756                 struct dom_sid *sid;
757
758                 sid = talloc(*sids, struct dom_sid);
759                 W_ERROR_HAVE_NO_MEMORY(sid);
760
761                 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
762                                                (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
763                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
764                         return WERR_INTERNAL_DB_CORRUPTION;
765                 }
766                 (*sids)[i] = sid;
767         }
768         (*sids)[i] = NULL;
769
770         return WERR_OK;
771 }
772
773 /*
774   see if any SIDs in list1 are in list2
775  */
776 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
777 {
778         unsigned int i, j;
779         /* do we ever have enough SIDs here to worry about O(n^2) ? */
780         for (i=0; list1[i]; i++) {
781                 for (j=0; list2[j]; j++) {
782                         if (dom_sid_equal(list1[i], list2[j])) {
783                                 return true;
784                         }
785                 }
786         }
787         return false;
788 }
789
790 /*
791   handle a DRSUAPI_EXOP_REPL_SECRET call
792  */
793 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
794                                        TALLOC_CTX *mem_ctx,
795                                        struct drsuapi_DsGetNCChangesRequest8 *req8,
796                                        struct dom_sid *user_sid,
797                                        struct drsuapi_DsGetNCChangesCtr6 *ctr6)
798 {
799         struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req8->naming_context;
800         struct ldb_dn *obj_dn, *rodc_dn, *krbtgt_link_dn;
801         int ret;
802         const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
803         const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
804         struct ldb_result *rodc_res, *obj_res;
805         const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
806         WERROR werr;
807
808         DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n", ncRoot->dn));
809
810         /*
811          * we need to work out if we will allow this RODC to
812          * replicate the secrets for this object
813          *
814          * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
815          * of this function
816          */
817
818         if (b_state->sam_ctx_system == NULL) {
819                 /* this operation needs system level access */
820                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
821                 return WERR_DS_DRA_SOURCE_DISABLED;
822         }
823
824         obj_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx_system, ncRoot->dn);
825         if (!ldb_dn_validate(obj_dn)) goto failed;
826
827         rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
828                                  dom_sid_string(mem_ctx, user_sid));
829         if (!ldb_dn_validate(rodc_dn)) goto failed;
830
831         /* do the two searches we need */
832         ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
833                              DSDB_SEARCH_SHOW_EXTENDED_DN);
834         if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
835
836         ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
837         if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
838
839         /* if the object SID is equal to the user_sid, allow */
840         if (dom_sid_equal(user_sid,
841                           samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
842                 goto allowed;
843         }
844
845         /* an RODC is allowed to get its own krbtgt account secrets */
846         krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
847                                          rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
848         if (krbtgt_link_dn != NULL &&
849             ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
850                 goto allowed;
851         }
852
853         /* but it isn't allowed to get anyone elses krbtgt secrets */
854         if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
855                             obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
856                 goto denied;
857         }
858
859         if (samdb_result_uint(obj_res->msgs[0], "UserAccountControl", 0) &
860             UF_INTERDOMAIN_TRUST_ACCOUNT) {
861                 goto denied;
862         }
863
864         werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
865                                          mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
866         if (!W_ERROR_IS_OK(werr)) {
867                 goto denied;
868         }
869
870         werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
871                                          mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
872         if (!W_ERROR_IS_OK(werr)) {
873                 goto denied;
874         }
875
876         werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
877                                          mem_ctx, "tokenGroups", &token_sids);
878         if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
879                 goto denied;
880         }
881
882         if (never_reveal_sids &&
883             sid_list_match(token_sids, never_reveal_sids)) {
884                 goto denied;
885         }
886
887         if (reveal_sids &&
888             sid_list_match(token_sids, reveal_sids)) {
889                 goto allowed;
890         }
891
892         /* default deny */
893 denied:
894         DEBUG(2,(__location__ ": Denied RODC secret replication for %s by RODC %s\n",
895                  ncRoot->dn, ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
896         ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
897         return WERR_DS_DRA_ACCESS_DENIED;
898
899 allowed:
900         DEBUG(2,(__location__ ": Allowed RODC secret replication for %s by RODC %s\n",
901                  ncRoot->dn, ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
902         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
903         req8->highwatermark.highest_usn = 0;
904         return WERR_OK;
905
906 failed:
907         DEBUG(2,(__location__ ": Failed RODC secret replication for %s by RODC %s\n",
908                  ncRoot->dn, dom_sid_string(mem_ctx, user_sid)));
909         ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
910         return WERR_DS_DRA_BAD_DN;
911 }
912
913 /*
914   handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
915   DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
916   and DRSUAPI_EXOP_FSMO_REQ_PDC calls
917  */
918 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
919                                          TALLOC_CTX *mem_ctx,
920                                          struct drsuapi_DsGetNCChangesRequest8 *req8,
921                                          struct drsuapi_DsGetNCChangesCtr6 *ctr6)
922 {
923         struct ldb_dn *fsmo_role_dn, *req_dn, *ntds_dn;
924         int ret;
925         unsigned int i;
926         struct ldb_context *ldb = b_state->sam_ctx;
927         struct ldb_message *msg;
928
929         /*
930           steps:
931             - verify that the client dn exists
932             - verify that we are the current master
933          */
934
935         req_dn = ldb_dn_new(mem_ctx, ldb, req8->naming_context->dn);
936         if (!req_dn ||
937             !ldb_dn_validate(req_dn)) {
938                 /* that is not a valid dn */
939                 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
940                          req8->naming_context->dn));
941                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
942                 return WERR_OK;
943         }
944
945         /* retrieve the current role owner */
946         ret = samdb_reference_dn(ldb, mem_ctx, req_dn, "fSMORoleOwner", &fsmo_role_dn);
947         if (ret != LDB_SUCCESS) {
948                 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in context - %s\n",
949                          ldb_errstring(ldb)));
950                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
951                 return WERR_DS_DRA_INTERNAL_ERROR;
952         }
953
954         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
955                 /* we're not the current owner - go away */
956                 DEBUG(0,(__location__ ": FSMO transfer request when not owner\n"));
957                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
958                 return WERR_OK;
959         }
960
961         /* change the current master */
962         msg = ldb_msg_new(ldb);
963         W_ERROR_HAVE_NO_MEMORY(msg);
964         msg->dn = ldb_dn_new(msg, ldb, req8->naming_context->dn);
965         W_ERROR_HAVE_NO_MEMORY(msg->dn);
966
967         ret = dsdb_find_dn_by_guid(ldb, msg, &req8->destination_dsa_guid, &ntds_dn);
968         if (ret != LDB_SUCCESS) {
969                 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
970                           GUID_string(mem_ctx, &req8->destination_dsa_guid), ldb_errstring(ldb)));
971                 talloc_free(msg);
972                 return WERR_DS_DRA_INTERNAL_ERROR;
973         }
974
975         ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
976         if (ret != 0) {
977                 talloc_free(msg);
978                 return WERR_DS_DRA_INTERNAL_ERROR;
979         }
980
981         for (i=0;i<msg->num_elements;i++) {
982                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
983         }
984
985         ret = ldb_transaction_start(ldb);
986         if (ret != LDB_SUCCESS) {
987                 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
988                          ldb_errstring(ldb)));
989                 return WERR_DS_DRA_INTERNAL_ERROR;
990         }
991
992         ret = ldb_modify(ldb, msg);
993         if (ret != LDB_SUCCESS) {
994                 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
995                          ldb_errstring(ldb)));
996                 ldb_transaction_cancel(ldb);
997                 return WERR_DS_DRA_INTERNAL_ERROR;
998         }
999
1000         ret = ldb_transaction_commit(ldb);
1001         if (ret != LDB_SUCCESS) {
1002                 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1003                          ldb_errstring(ldb)));
1004                 return WERR_DS_DRA_INTERNAL_ERROR;
1005         }
1006
1007         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1008
1009         return WERR_OK;
1010 }
1011
1012 /* state of a partially completed getncchanges call */
1013 struct drsuapi_getncchanges_state {
1014         struct ldb_result *site_res;
1015         uint32_t num_sent;
1016         struct ldb_dn *ncRoot_dn;
1017         bool is_schema_nc;
1018         uint64_t min_usn;
1019         uint64_t highest_usn;
1020         struct ldb_dn *last_dn;
1021         struct drsuapi_DsReplicaLinkedAttribute *la_list;
1022         uint32_t la_count;
1023         bool la_sorted;
1024         uint32_t la_idx;
1025         struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
1026 };
1027
1028 /* 
1029   drsuapi_DsGetNCChanges
1030
1031   see MS-DRSR 4.1.10.5.2 for basic logic of this function
1032 */
1033 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1034                                      struct drsuapi_DsGetNCChanges *r)
1035 {
1036         struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1037         int ret;
1038         unsigned int i;
1039         struct dsdb_schema *schema;
1040         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1041         struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1042         NTSTATUS status;
1043         DATA_BLOB session_key;
1044         const char *attrs[] = { "*", "distinguishedName",
1045                                 "nTSecurityDescriptor",
1046                                 "parentGUID",
1047                                 "replPropertyMetaData",
1048                                 "unicodePwd",
1049                                 "dBCSPwd",
1050                                 "ntPwdHistory",
1051                                 "lmPwdHistory",
1052                                 "supplementalCredentials",
1053                                 NULL };
1054         WERROR werr;
1055         struct dcesrv_handle *h;
1056         struct drsuapi_bind_state *b_state;     
1057         struct drsuapi_getncchanges_state *getnc_state;
1058         struct drsuapi_DsGetNCChangesRequest8 *req8;
1059         uint32_t options;
1060         uint32_t max_objects;
1061         uint32_t max_links;
1062         uint32_t link_count = 0;
1063         uint32_t link_total = 0;
1064         uint32_t link_given = 0;
1065         struct ldb_dn *search_dn = NULL;
1066         bool am_rodc, null_scope=false;
1067         enum security_user_level security_level;
1068         struct ldb_context *sam_ctx;
1069         struct dom_sid *user_sid;
1070
1071         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1072         b_state = h->data;
1073
1074         sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1075
1076         *r->out.level_out = 6;
1077         /* TODO: linked attributes*/
1078         r->out.ctr->ctr6.linked_attributes_count = 0;
1079         r->out.ctr->ctr6.linked_attributes = NULL;
1080
1081         r->out.ctr->ctr6.object_count = 0;
1082         r->out.ctr->ctr6.nc_object_count = 0;
1083         r->out.ctr->ctr6.more_data = false;
1084         r->out.ctr->ctr6.uptodateness_vector = NULL;
1085
1086         /* a RODC doesn't allow for any replication */
1087         ret = samdb_rodc(sam_ctx, &am_rodc);
1088         if (ret == LDB_SUCCESS && am_rodc) {
1089                 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1090                 return WERR_DS_DRA_SOURCE_DISABLED;
1091         }
1092
1093         /* Check request revision. 
1094            TODO: Adding mappings to req8 from the other levels
1095          */
1096         if (r->in.level != 8) {
1097                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1098                          r->in.level));
1099                 return WERR_REVISION_MISMATCH;
1100         }
1101
1102         req8 = &r->in.req->req8;
1103
1104         /* Perform access checks. */
1105         /* TODO: we need to support a sync on a specific non-root
1106          * DN. We'll need to find the real partition root here */
1107         ncRoot = req8->naming_context;
1108         if (ncRoot == NULL) {
1109                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1110                 return WERR_DS_DRA_INVALID_PARAMETER;
1111         }
1112
1113         if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1114                 return WERR_DS_DRA_INTERNAL_ERROR;
1115         }
1116         
1117         if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1118             !(req8->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1119                 return WERR_DS_DRA_SOURCE_DISABLED;
1120         }
1121
1122         werr = drs_security_level_check(dce_call, "DsGetNCChanges", SECURITY_RO_DOMAIN_CONTROLLER,
1123                                         samdb_domain_sid(sam_ctx));
1124         if (!W_ERROR_IS_OK(werr)) {
1125                 return werr;
1126         }
1127
1128         user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1129
1130
1131         /* for non-administrator replications, check that they have
1132            given the correct source_dsa_invocation_id */
1133         security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1134                                                      samdb_domain_sid(sam_ctx));
1135         if (security_level == SECURITY_RO_DOMAIN_CONTROLLER &&
1136             req8->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1137                 /* we rely on this flag being unset for RODC requests */
1138                 req8->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1139         }
1140
1141
1142         if (req8->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1143                 /* Ignore the _in_ uptpdateness vector*/
1144                 req8->uptodateness_vector = NULL;
1145         } 
1146
1147         getnc_state = b_state->getncchanges_state;
1148
1149         /* see if a previous replication has been abandoned */
1150         if (getnc_state) {
1151                 struct ldb_dn *new_dn = ldb_dn_new(getnc_state, sam_ctx, ncRoot->dn);
1152                 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1153                         DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1154                                  ldb_dn_get_linearized(new_dn),
1155                                  ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1156                                  ldb_dn_get_linearized(getnc_state->last_dn)));
1157                         talloc_free(getnc_state);
1158                         getnc_state = NULL;
1159                 }
1160         }
1161
1162         if (getnc_state == NULL) {
1163                 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1164                 if (getnc_state == NULL) {
1165                         return WERR_NOMEM;
1166                 }
1167                 b_state->getncchanges_state = getnc_state;
1168                 getnc_state->ncRoot_dn = ldb_dn_new(getnc_state, sam_ctx, ncRoot->dn);
1169
1170                 /* find out if we are to replicate Schema NC */
1171                 ret = ldb_dn_compare(getnc_state->ncRoot_dn,
1172                                      ldb_get_schema_basedn(b_state->sam_ctx));
1173                 getnc_state->is_schema_nc = (0 == ret);
1174
1175                 /*
1176                  * This is the first replication cycle and it is
1177                  * a good place to handle extended operations
1178                  *
1179                  * FIXME: we don't fully support extended operations yet
1180                  */
1181                 switch (req8->extended_op) {
1182                 case DRSUAPI_EXOP_NONE:
1183                         break;
1184                 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1185                         werr = getncchanges_rid_alloc(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1186                         W_ERROR_NOT_OK_RETURN(werr);
1187                         search_dn = ldb_get_default_basedn(sam_ctx);
1188                         break;
1189                 case DRSUAPI_EXOP_REPL_SECRET:
1190                         werr = getncchanges_repl_secret(b_state, mem_ctx, req8, user_sid, &r->out.ctr->ctr6);
1191                         r->out.result = werr;
1192                         W_ERROR_NOT_OK_RETURN(werr);
1193                         break;
1194                 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1195                         werr = getncchanges_change_master(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1196                         W_ERROR_NOT_OK_RETURN(werr);
1197                         break;
1198                 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1199                         werr = getncchanges_change_master(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1200                         W_ERROR_NOT_OK_RETURN(werr);
1201                         break;
1202                 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1203                         werr = getncchanges_change_master(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
1204                         W_ERROR_NOT_OK_RETURN(werr);
1205                         break;
1206                 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1207                 case DRSUAPI_EXOP_REPL_OBJ:
1208                         DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1209                                  (unsigned)req8->extended_op));
1210                         return WERR_DS_DRA_NOT_SUPPORTED;
1211                 }
1212         }
1213
1214         if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
1215             ldb_dn_is_null(getnc_state->ncRoot_dn)) {
1216                 DEBUG(0,(__location__ ": Bad DN '%s'\n", ncRoot->dn));
1217                 return WERR_DS_DRA_INVALID_PARAMETER;
1218         }
1219
1220         /* we need the session key for encrypting password attributes */
1221         status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
1222         if (!NT_STATUS_IS_OK(status)) {
1223                 DEBUG(0,(__location__ ": Failed to get session key\n"));
1224                 return WERR_DS_DRA_INTERNAL_ERROR;              
1225         }
1226
1227         /* 
1228            TODO: MS-DRSR section 4.1.10.1.1
1229            Work out if this is the start of a new cycle */
1230
1231         if (getnc_state->site_res == NULL) {
1232                 char* search_filter;
1233                 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1234                 const char *extra_filter;
1235
1236                 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1237
1238                 getnc_state->min_usn = req8->highwatermark.highest_usn;
1239
1240                 /* Construct response. */
1241                 search_filter = talloc_asprintf(mem_ctx,
1242                                                 "(uSNChanged>=%llu)",
1243                                                 (unsigned long long)(getnc_state->min_usn+1));
1244         
1245                 if (extra_filter) {
1246                         search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1247                 }
1248
1249                 if (req8->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1250                         search_filter = talloc_asprintf(mem_ctx,
1251                                                         "(&%s(isCriticalSystemObject=TRUE))",
1252                                                         search_filter);
1253                 }
1254                 
1255                 if (req8->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1256                         scope = LDB_SCOPE_BASE;
1257                 }
1258                 
1259                 if (!search_dn) {
1260                         search_dn = getnc_state->ncRoot_dn;
1261                 }
1262
1263                 DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1264                          ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1265                 ret = drsuapi_search_with_extended_dn(sam_ctx, getnc_state, &getnc_state->site_res,
1266                                                       search_dn, scope, attrs,
1267                                                       search_filter);
1268                 if (ret != LDB_SUCCESS) {
1269                         return WERR_DS_DRA_INTERNAL_ERROR;
1270                 }
1271
1272                 if (req8->replica_flags & DRSUAPI_DRS_GET_ANC) {
1273                         TYPESAFE_QSORT(getnc_state->site_res->msgs,
1274                                        getnc_state->site_res->count,
1275                                        site_res_cmp_parent_order);
1276                 } else {
1277                         TYPESAFE_QSORT(getnc_state->site_res->msgs,
1278                                        getnc_state->site_res->count,
1279                                        site_res_cmp_usn_order);
1280                 }
1281
1282                 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req8->uptodateness_vector);
1283                 if (getnc_state->uptodateness_vector) {
1284                         /* make sure its sorted */
1285                         TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
1286                                        getnc_state->uptodateness_vector->count,
1287                                        drsuapi_DsReplicaCursor_compare);
1288                 }
1289         }
1290
1291         /* Prefix mapping */
1292         schema = dsdb_get_schema(sam_ctx, mem_ctx);
1293         if (!schema) {
1294                 DEBUG(0,("No schema in sam_ctx\n"));
1295                 return WERR_DS_DRA_INTERNAL_ERROR;
1296         }
1297
1298         r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
1299         *r->out.ctr->ctr6.naming_context = *ncRoot;
1300
1301         if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
1302                                  &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
1303                 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
1304                          ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
1305                 return WERR_DS_DRA_INTERNAL_ERROR;
1306         }
1307
1308         /* find the SID if there is one */
1309         dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
1310
1311         dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
1312         r->out.ctr->ctr6.mapping_ctr = *ctr;
1313
1314         r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1315         r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1316
1317         r->out.ctr->ctr6.old_highwatermark = req8->highwatermark;
1318         r->out.ctr->ctr6.new_highwatermark = req8->highwatermark;
1319
1320         r->out.ctr->ctr6.first_object = NULL;
1321         currentObject = &r->out.ctr->ctr6.first_object;
1322
1323         /* use this to force single objects at a time, which is useful
1324          * for working out what object is giving problems
1325          */
1326         max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
1327         if (req8->max_object_count < max_objects) {
1328                 max_objects = req8->max_object_count;
1329         }
1330         /*
1331          * TODO: work out how the maximum should be calculated
1332          */
1333         max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
1334
1335         for (i=getnc_state->num_sent;
1336              i<getnc_state->site_res->count &&
1337                      !null_scope &&
1338                      (r->out.ctr->ctr6.object_count < max_objects);
1339             i++) {
1340                 int uSN;
1341                 struct drsuapi_DsReplicaObjectListItemEx *obj;
1342                 struct ldb_message *msg = getnc_state->site_res->msgs[i];
1343
1344                 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
1345
1346                 werr = get_nc_changes_build_object(obj, msg,
1347                                                    sam_ctx, getnc_state->ncRoot_dn,
1348                                                    getnc_state->is_schema_nc,
1349                                                    schema, &session_key, getnc_state->min_usn,
1350                                                    req8->replica_flags, getnc_state->uptodateness_vector,
1351                                                    req8->extended_op);
1352                 if (!W_ERROR_IS_OK(werr)) {
1353                         return werr;
1354                 }
1355
1356                 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
1357                                                 getnc_state->ncRoot_dn,
1358                                                 schema, getnc_state->min_usn,
1359                                                 req8->replica_flags,
1360                                                 msg,
1361                                                 &getnc_state->la_list,
1362                                                 &getnc_state->la_count,
1363                                                 getnc_state->uptodateness_vector);
1364                 if (!W_ERROR_IS_OK(werr)) {
1365                         return werr;
1366                 }
1367
1368                 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
1369                 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
1370                         r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
1371                 }
1372                 if (uSN > getnc_state->highest_usn) {
1373                         getnc_state->highest_usn = uSN;
1374                 }
1375
1376                 if (obj->meta_data_ctr == NULL) {
1377                         DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
1378                                  ldb_dn_get_linearized(msg->dn)));
1379                         /* no attributes to send */
1380                         talloc_free(obj);
1381                         continue;
1382                 }
1383
1384                 r->out.ctr->ctr6.object_count++;
1385                 
1386                 *currentObject = obj;
1387                 currentObject = &obj->next_object;
1388
1389                 talloc_free(getnc_state->last_dn);
1390                 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1391
1392                 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1393         }
1394
1395         getnc_state->num_sent += r->out.ctr->ctr6.object_count;
1396
1397         r->out.ctr->ctr6.nc_object_count = getnc_state->site_res->count;
1398
1399         /* the client can us to call UpdateRefs on its behalf to
1400            re-establish monitoring of the NC */
1401         if ((req8->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1402             !GUID_all_zero(&req8->destination_dsa_guid)) {
1403                 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1404                 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1405                          GUID_string(mem_ctx, &req8->destination_dsa_guid)));
1406                 ureq.naming_context = ncRoot;
1407                 ureq.dest_dsa_dns_name = talloc_asprintf(mem_ctx, "%s._msdcs.%s",
1408                                                          GUID_string(mem_ctx, &req8->destination_dsa_guid),
1409                                                          lpcfg_dnsdomain(dce_call->conn->dce_ctx->lp_ctx));
1410                 if (!ureq.dest_dsa_dns_name) {
1411                         return WERR_NOMEM;
1412                 }
1413                 ureq.dest_dsa_guid = req8->destination_dsa_guid;
1414                 ureq.options = DRSUAPI_DRS_ADD_REF |
1415                         DRSUAPI_DRS_ASYNC_OP |
1416                         DRSUAPI_DRS_GETCHG_CHECK;
1417                 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1418                 if (!W_ERROR_IS_OK(werr)) {
1419                         DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1420                                  win_errstr(werr)));
1421                 }
1422         }
1423
1424         /*
1425          * TODO:
1426          * This is just a guess, how to calculate the
1427          * number of linked attributes to send, we need to
1428          * find out how to do this right.
1429          */
1430         if (r->out.ctr->ctr6.object_count >= max_links) {
1431                 max_links = 0;
1432         } else {
1433                 max_links -= r->out.ctr->ctr6.object_count;
1434         }
1435
1436         link_total = getnc_state->la_count;
1437
1438         if (i < getnc_state->site_res->count) {
1439                 r->out.ctr->ctr6.more_data = true;
1440         } else {
1441                 /* sort the whole array the first time */
1442                 if (!getnc_state->la_sorted) {
1443                         LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1444                                            sam_ctx, linked_attribute_compare);
1445                         getnc_state->la_sorted = true;
1446                 }
1447
1448                 link_count = getnc_state->la_count - getnc_state->la_idx;
1449                 link_count = MIN(max_links, link_count);
1450
1451                 r->out.ctr->ctr6.linked_attributes_count = link_count;
1452                 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1453
1454                 getnc_state->la_idx += link_count;
1455                 link_given = getnc_state->la_idx;
1456
1457                 if (getnc_state->la_idx < getnc_state->la_count) {
1458                         r->out.ctr->ctr6.more_data = true;
1459                 }
1460         }
1461
1462         if (!r->out.ctr->ctr6.more_data) {
1463                 talloc_steal(mem_ctx, getnc_state->la_list);
1464
1465                 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1466                 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1467
1468                 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
1469                                           r->out.ctr->ctr6.uptodateness_vector);
1470                 if (!W_ERROR_IS_OK(werr)) {
1471                         return werr;
1472                 }
1473
1474                 talloc_free(getnc_state);
1475                 b_state->getncchanges_state = NULL;
1476         }
1477
1478         if (req8->extended_op != DRSUAPI_EXOP_NONE) {
1479                 r->out.ctr->ctr6.uptodateness_vector = NULL;
1480                 r->out.ctr->ctr6.nc_object_count = 0;
1481                 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1482                 r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1483         }
1484
1485         DEBUG(r->out.ctr->ctr6.more_data?4:2,
1486               ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1487                (unsigned long long)(req8->highwatermark.highest_usn+1),
1488                req8->replica_flags, ncRoot->dn,
1489                r->out.ctr->ctr6.object_count,
1490                i, r->out.ctr->ctr6.more_data?getnc_state->site_res->count:i,
1491                r->out.ctr->ctr6.linked_attributes_count,
1492                link_given, link_total,
1493                dom_sid_string(mem_ctx, user_sid)));
1494
1495 #if 0
1496         if (!r->out.ctr->ctr6.more_data && req8->extended_op != DRSUAPI_EXOP_NONE) {
1497                 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
1498         }
1499 #endif
1500
1501         return WERR_OK;
1502 }