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