s4-drs: allow getncchanges from RODC with WRIT_REP set
[samba.git] / source4 / rpc_server / drsuapi / getncchanges.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implement the DRSUpdateRefs call
5
6    Copyright (C) Anatoliy Atanasov 2009
7    Copyright (C) Andrew Tridgell 2009
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
30 #include "rpc_server/dcerpc_server_proto.h"
31 #include "../libcli/drsuapi/drsuapi.h"
32 #include "libcli/security/security.h"
33 #include "lib/util/binsearch.h"
34 #include "lib/util/tsort.h"
35 #include "auth/session.h"
36
37 /*
38   build a DsReplicaObjectIdentifier from a ldb msg
39  */
40 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
41                                                                        struct ldb_message *msg)
42 {
43         struct drsuapi_DsReplicaObjectIdentifier *identifier;
44         struct dom_sid *sid;
45
46         identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
47         if (identifier == NULL) {
48                 return NULL;
49         }
50
51         identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
52         identifier->guid = samdb_result_guid(msg, "objectGUID");
53
54         sid = samdb_result_dom_sid(identifier, msg, "objectSid");
55         if (sid) {
56                 identifier->sid = *sid;
57         } else {
58                 ZERO_STRUCT(identifier->sid);
59         }
60         return identifier;
61 }
62
63 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
64 {
65         return GUID_compare(guid1, &guid2);
66 }
67
68 /*
69   see if we can filter an attribute using the uptodateness_vector
70  */
71 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
72                        const struct GUID *originating_invocation_id,
73                        uint64_t originating_usn)
74 {
75         const struct drsuapi_DsReplicaCursor *c;
76         if (udv == NULL) return false;
77         BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id, 
78                             originating_invocation_id, udv_compare, c);
79         if (c && originating_usn <= c->highest_usn) {
80                 return true;
81         }
82         return false;
83         
84 }
85
86 /* 
87   drsuapi_DsGetNCChanges for one object
88 */
89 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
90                                           struct ldb_message *msg,
91                                           struct ldb_context *sam_ctx,
92                                           struct ldb_dn *ncRoot_dn,
93                                           struct dsdb_schema *schema,
94                                           DATA_BLOB *session_key,
95                                           uint64_t highest_usn,
96                                           uint32_t replica_flags,
97                                           struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
98 {
99         const struct ldb_val *md_value;
100         unsigned int i, n;
101         struct replPropertyMetaDataBlob md;
102         uint32_t rid = 0;
103         enum ndr_err_code ndr_err;
104         uint32_t *attids;
105         const char *rdn;
106         const struct dsdb_attribute *rdn_sa;
107         unsigned int instanceType;
108
109         instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
110         if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
111                 obj->is_nc_prefix = true;
112                 obj->parent_object_guid = NULL;
113         } else {
114                 obj->is_nc_prefix = false;
115                 obj->parent_object_guid = talloc(obj, struct GUID);
116                 if (obj->parent_object_guid == NULL) {
117                         return WERR_DS_DRA_INTERNAL_ERROR;
118                 }
119                 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
120                 if (GUID_all_zero(obj->parent_object_guid)) {
121                         DEBUG(0,(__location__ ": missing parentGUID for %s\n",
122                                  ldb_dn_get_linearized(msg->dn)));
123                         return WERR_DS_DRA_INTERNAL_ERROR;
124                 }
125         }
126         obj->next_object = NULL;
127         
128         md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
129         if (!md_value) {
130                 /* nothing to send */
131                 return WERR_OK;
132         }
133
134         if (instanceType & INSTANCE_TYPE_UNINSTANT) {
135                 /* don't send uninstantiated objects */
136                 return WERR_OK;
137         }
138
139         ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
140                                        (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
141         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
142                 return WERR_DS_DRA_INTERNAL_ERROR;
143         }
144         
145         if (md.version != 1) {
146                 return WERR_DS_DRA_INTERNAL_ERROR;
147         }
148
149         rdn = ldb_dn_get_rdn_name(msg->dn);
150         if (rdn == NULL) {
151                 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
152                 return WERR_DS_DRA_INTERNAL_ERROR;
153         }
154
155         rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
156         if (rdn_sa == NULL) {
157                 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n", 
158                          rdn, ldb_dn_get_linearized(msg->dn)));
159                 return WERR_DS_DRA_INTERNAL_ERROR;
160         }
161
162         obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
163         attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
164
165         obj->object.identifier = get_object_identifier(obj, msg);
166         if (obj->object.identifier == NULL) {
167                 return WERR_NOMEM;
168         }
169         dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
170         
171         obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
172         for (n=i=0; i<md.ctr.ctr1.count; i++) {
173                 const struct dsdb_attribute *sa;
174                 /* if the attribute has not changed, and it is not the
175                    instanceType then don't include it */
176                 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
177                     md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType) continue;
178
179                 /* don't include the rDN */
180                 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
181
182                 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
183                 if (!sa) {
184                         DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n", 
185                                  (unsigned int)md.ctr.ctr1.array[i].attid, 
186                                  ldb_dn_get_linearized(msg->dn)));
187                         return WERR_DS_DRA_INTERNAL_ERROR;              
188                 }
189
190                 if (sa->linkID) {
191                         struct ldb_message_element *el;
192                         el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
193                         if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
194                                 /* don't send upgraded links inline */
195                                 continue;
196                         }
197                 }
198
199                 /* filter by uptodateness_vector */
200                 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTRIBUTE_instanceType &&
201                     udv_filter(uptodateness_vector,
202                                &md.ctr.ctr1.array[i].originating_invocation_id, 
203                                md.ctr.ctr1.array[i].originating_usn)) {
204                         continue;
205                 }
206
207                 /*
208                  * If the recipient is a RODC, then we should not add any
209                  * RODC filtered attribute
210                  *
211                  * TODO: This is not strictly correct, as it doesn't allow for administrators
212                  * to setup some users to transfer passwords to specific RODCs. To support that
213                  * we would instead remove this check and rely on extended ACL checking in the dsdb
214                  * acl module.
215                  */
216                 if (dsdb_attr_in_rodc_fas(replica_flags, sa)) {
217                         continue;
218                 }
219
220                 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
221                 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
222                 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
223                 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
224                 attids[n] = md.ctr.ctr1.array[i].attid;
225                 n++;
226         }
227
228         /* ignore it if its an empty change. Note that renames always
229          * change the 'name' attribute, so they won't be ignored by
230          * this */
231         if (n == 0 ||
232             (n == 1 && attids[0] == DRSUAPI_ATTRIBUTE_instanceType)) {
233                 talloc_free(obj->meta_data_ctr);
234                 obj->meta_data_ctr = NULL;
235                 return WERR_OK;
236         }
237
238         obj->meta_data_ctr->count = n;
239
240         obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
241         obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
242         obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
243                                                             obj->object.attribute_ctr.num_attributes);
244
245         /*
246          * Note that the meta_data array and the attributes array must
247          * be the same size and in the same order
248          */
249         for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
250                 struct ldb_message_element *el;
251                 WERROR werr;
252                 const struct dsdb_attribute *sa;
253         
254                 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
255                 if (!sa) {
256                         DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
257                         return WERR_DS_DRA_INTERNAL_ERROR;
258                 }
259
260                 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
261                 if (el == NULL) {
262                         /* this happens for attributes that have been removed */
263                         DEBUG(5,("No element '%s' for attributeID %u in message\n",
264                                  sa->lDAPDisplayName, attids[i]));
265                         ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
266                         obj->object.attribute_ctr.attributes[i].attid = attids[i];
267                 } else {
268                         werr = dsdb_attribute_ldb_to_drsuapi(sam_ctx, schema, el, obj,
269                                                              &obj->object.attribute_ctr.attributes[i]);
270                         if (!W_ERROR_IS_OK(werr)) {
271                                 DEBUG(0,("Unable to convert %s to DRS object - %s\n", 
272                                          sa->lDAPDisplayName, win_errstr(werr)));
273                                 return werr;
274                         }
275                         /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
276                          * check if attribute is secret and send a null value
277                          */
278                         if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
279                                 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
280                                                                  &obj->meta_data_ctr->meta_data[i]);
281                         }
282                         /* some attributes needs to be encrypted
283                            before being sent */
284                         werr = drsuapi_encrypt_attribute(obj, session_key, rid, 
285                                                          &obj->object.attribute_ctr.attributes[i]);
286                         if (!W_ERROR_IS_OK(werr)) {
287                                 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n", 
288                                          sa->lDAPDisplayName, win_errstr(werr)));
289                                 return werr;
290                         }
291                 }
292         }
293
294         return WERR_OK;
295 }
296
297
298 /*
299   add one linked attribute from an object to the list of linked
300   attributes in a getncchanges request
301  */
302 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
303                                     struct ldb_context *sam_ctx,
304                                     const struct dsdb_schema *schema,
305                                     const struct dsdb_attribute *sa,
306                                     struct ldb_message *msg,
307                                     struct dsdb_dn *dsdb_dn,
308                                     struct drsuapi_DsReplicaLinkedAttribute **la_list,
309                                     uint32_t *la_count)
310 {
311         struct drsuapi_DsReplicaLinkedAttribute *la;
312         bool active;
313         NTSTATUS status;
314         WERROR werr;
315
316         (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
317         W_ERROR_HAVE_NO_MEMORY(*la_list);
318
319         la = &(*la_list)[*la_count];
320
321         la->identifier = get_object_identifier(*la_list, msg);
322         W_ERROR_HAVE_NO_MEMORY(la->identifier);
323
324         active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
325
326         la->attid = sa->attributeID_id;
327         la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
328
329         status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
330         if (!NT_STATUS_IS_OK(status)) {
331                 return ntstatus_to_werror(status);
332         }
333         status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
334         if (!NT_STATUS_IS_OK(status)) {
335                 return ntstatus_to_werror(status);
336         }
337         status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
338         if (!NT_STATUS_IS_OK(status)) {
339                 return ntstatus_to_werror(status);
340         }
341         status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
342         if (!NT_STATUS_IS_OK(status)) {
343                 return ntstatus_to_werror(status);
344         }
345         status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
346         if (!NT_STATUS_IS_OK(status)) {
347                 return ntstatus_to_werror(status);
348         }
349
350         werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
351         W_ERROR_NOT_OK_RETURN(werr);
352
353         (*la_count)++;
354         return WERR_OK;
355 }
356
357
358 /*
359   add linked attributes from an object to the list of linked
360   attributes in a getncchanges request
361  */
362 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
363                                        TALLOC_CTX *mem_ctx,
364                                        struct ldb_dn *ncRoot_dn,
365                                        struct dsdb_schema *schema,
366                                        uint64_t highest_usn,
367                                        uint32_t replica_flags,
368                                        struct ldb_message *msg,
369                                        struct drsuapi_DsReplicaLinkedAttribute **la_list,
370                                        uint32_t *la_count,
371                                        struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
372 {
373         unsigned int i;
374         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
375         uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
376
377         for (i=0; i<msg->num_elements; i++) {
378                 struct ldb_message_element *el = &msg->elements[i];
379                 const struct dsdb_attribute *sa;
380                 unsigned int j;
381
382                 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
383
384                 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
385                         /* we only want forward links */
386                         continue;
387                 }
388
389                 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
390                         /* its an old style link, it will have been
391                          * sent in the main replication data */
392                         continue;
393                 }
394
395                 for (j=0; j<el->num_values; j++) {
396                         struct dsdb_dn *dsdb_dn;
397                         uint64_t local_usn;
398                         NTSTATUS status;
399                         WERROR werr;
400
401                         dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
402                         if (dsdb_dn == NULL) {
403                                 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
404                                          el->name, ldb_dn_get_linearized(msg->dn)));
405                                 talloc_free(tmp_ctx);
406                                 return WERR_DS_DRA_INTERNAL_ERROR;
407                         }
408
409                         status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
410                         if (!NT_STATUS_IS_OK(status)) {
411                                 /* this can happen for attributes
412                                    given to us with old style meta
413                                    data */
414                                 continue;
415                         }
416
417                         if (local_usn > uSNChanged) {
418                                 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
419                                          el->name, ldb_dn_get_linearized(msg->dn)));
420                                 talloc_free(tmp_ctx);
421                                 return WERR_DS_DRA_INTERNAL_ERROR;
422                         }
423
424                         if (local_usn < highest_usn) {
425                                 continue;
426                         }
427
428                         werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema, sa, msg,
429                                                      dsdb_dn, la_list, la_count);
430                         if (!W_ERROR_IS_OK(werr)) {
431                                 talloc_free(tmp_ctx);
432                                 return werr;
433                         }
434                 }
435         }
436
437         talloc_free(tmp_ctx);
438         return WERR_OK;
439 }
440
441 /*
442   fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
443  */
444 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
445                                  struct ldb_dn *ncRoot_dn,
446                                  struct drsuapi_DsReplicaCursor2CtrEx *udv)
447 {
448         int ret;
449
450         udv->version = 2;
451         udv->reserved1 = 0;
452         udv->reserved2 = 0;
453
454         ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
455         if (ret != LDB_SUCCESS) {
456                 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
457                          ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
458                 return WERR_DS_DRA_INTERNAL_ERROR;
459         }
460         
461         return WERR_OK;
462 }
463
464
465 /* comparison function for linked attributes - see CompareLinks() in
466  * MS-DRSR section 4.1.10.5.17 */
467 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute *la1,
468                                     const struct drsuapi_DsReplicaLinkedAttribute *la2,
469                                     struct ldb_context *sam_ctx)
470 {
471         int c;
472         WERROR werr;
473         TALLOC_CTX *tmp_ctx;
474         const struct dsdb_schema *schema;
475         const struct dsdb_attribute *schema_attrib;
476         struct dsdb_dn *dn1, *dn2;
477         struct GUID guid1, guid2;
478         NTSTATUS status;
479
480         c = GUID_compare(&la1->identifier->guid,
481                          &la2->identifier->guid);
482         if (c != 0) return c;
483
484         if (la1->attid != la2->attid) {
485                 return la1->attid < la2->attid? -1:1;
486         }
487
488         if ((la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
489             (la2->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
490                 return (la1->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
491         }
492
493         /* we need to get the target GUIDs to compare */
494         tmp_ctx = talloc_new(sam_ctx);
495
496         schema = dsdb_get_schema(sam_ctx, tmp_ctx);
497         schema_attrib = dsdb_attribute_by_attributeID_id(schema, la1->attid);
498
499         werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la1->value.blob, &dn1);
500         if (!W_ERROR_IS_OK(werr)) {
501                 DEBUG(0,(__location__ ": Bad la1 blob in sort\n"));
502                 talloc_free(tmp_ctx);
503                 return 0;
504         }
505
506         werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, tmp_ctx, la2->value.blob, &dn2);
507         if (!W_ERROR_IS_OK(werr)) {
508                 DEBUG(0,(__location__ ": Bad la2 blob in sort\n"));
509                 talloc_free(tmp_ctx);
510                 return 0;
511         }
512
513         status = dsdb_get_extended_dn_guid(dn1->dn, &guid1, "GUID");
514         if (!NT_STATUS_IS_OK(status)) {
515                 DEBUG(0,(__location__ ": Bad la1 guid in sort\n"));
516                 talloc_free(tmp_ctx);
517                 return 0;
518         }
519         status = dsdb_get_extended_dn_guid(dn2->dn, &guid2, "GUID");
520         if (!NT_STATUS_IS_OK(status)) {
521                 DEBUG(0,(__location__ ": Bad la2 guid in sort\n"));
522                 talloc_free(tmp_ctx);
523                 return 0;
524         }
525
526         talloc_free(tmp_ctx);
527
528         return GUID_compare(&guid1, &guid2);
529 }
530
531
532 /*
533   sort the objects we send by tree order
534  */
535 static int site_res_cmp_parent_order(struct ldb_message **m1, struct ldb_message **m2)
536 {
537         return ldb_dn_compare((*m2)->dn, (*m1)->dn);
538 }
539
540 /*
541   sort the objects we send first by uSNChanged
542  */
543 static int site_res_cmp_usn_order(struct ldb_message **m1, struct ldb_message **m2)
544 {
545         unsigned usnchanged1, usnchanged2;
546         unsigned cn1, cn2;
547         cn1 = ldb_dn_get_comp_num((*m1)->dn);
548         cn2 = ldb_dn_get_comp_num((*m2)->dn);
549         if (cn1 != cn2) {
550                 return cn1 > cn2 ? 1 : -1;
551         }
552         usnchanged1 = ldb_msg_find_attr_as_uint(*m1, "uSNChanged", 0);
553         usnchanged2 = ldb_msg_find_attr_as_uint(*m2, "uSNChanged", 0);
554         if (usnchanged1 == usnchanged2) {
555                 return 0;
556         }
557         return usnchanged1 > usnchanged2 ? 1 : -1;
558 }
559
560
561 /*
562   handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
563  */
564 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
565                                      TALLOC_CTX *mem_ctx,
566                                      struct drsuapi_DsGetNCChangesRequest8 *req8,
567                                      struct drsuapi_DsGetNCChangesCtr6 *ctr6)
568 {
569         struct ldb_dn *rid_manager_dn, *fsmo_role_dn, *req_dn;
570         int ret;
571         struct ldb_context *ldb = b_state->sam_ctx;
572         struct ldb_result *ext_res;
573         struct ldb_dn *base_dn;
574         struct dsdb_fsmo_extended_op *exop;
575
576         /*
577           steps:
578             - verify that the DN being asked for is the RID Manager DN
579             - verify that we are the RID Manager
580          */
581
582         /* work out who is the RID Manager */
583         ret = samdb_rid_manager_dn(ldb, mem_ctx, &rid_manager_dn);
584         if (ret != LDB_SUCCESS) {
585                 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
586                 return WERR_DS_DRA_INTERNAL_ERROR;
587         }
588
589         req_dn = ldb_dn_new(mem_ctx, ldb, req8->naming_context->dn);
590         if (!req_dn ||
591             !ldb_dn_validate(req_dn) ||
592             ldb_dn_compare(req_dn, rid_manager_dn) != 0) {
593                 /* that isn't the RID Manager DN */
594                 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
595                          req8->naming_context->dn));
596                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
597                 return WERR_OK;
598         }
599
600         /* find the DN of the RID Manager */
601         ret = samdb_reference_dn(ldb, mem_ctx, rid_manager_dn, "fSMORoleOwner", &fsmo_role_dn);
602         if (ret != LDB_SUCCESS) {
603                 DEBUG(0,(__location__ ": Failed to find fSMORoleOwner in RID Manager object - %s\n",
604                          ldb_errstring(ldb)));
605                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
606                 return WERR_DS_DRA_INTERNAL_ERROR;
607         }
608
609         if (ldb_dn_compare(samdb_ntds_settings_dn(ldb), fsmo_role_dn) != 0) {
610                 /* we're not the RID Manager - go away */
611                 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
612                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
613                 return WERR_OK;
614         }
615
616         exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
617         W_ERROR_HAVE_NO_MEMORY(exop);
618
619         exop->fsmo_info = req8->fsmo_info;
620         exop->destination_dsa_guid = req8->destination_dsa_guid;
621
622         ret = ldb_transaction_start(ldb);
623         if (ret != LDB_SUCCESS) {
624                 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
625                          ldb_errstring(ldb)));
626                 return WERR_DS_DRA_INTERNAL_ERROR;
627         }
628
629         ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
630         if (ret != LDB_SUCCESS) {
631                 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
632                          ldb_errstring(ldb)));
633                 ldb_transaction_cancel(ldb);
634                 return WERR_DS_DRA_INTERNAL_ERROR;
635         }
636
637         ret = ldb_transaction_commit(ldb);
638         if (ret != LDB_SUCCESS) {
639                 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
640                          ldb_errstring(ldb)));
641                 return WERR_DS_DRA_INTERNAL_ERROR;
642         }
643
644         talloc_free(ext_res);
645
646         base_dn = ldb_get_default_basedn(ldb);
647
648         DEBUG(2,("Allocated RID pool for server %s\n",
649                  GUID_string(mem_ctx, &req8->destination_dsa_guid)));
650
651         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
652
653         return WERR_OK;
654 }
655
656
657
658 /* state of a partially completed getncchanges call */
659 struct drsuapi_getncchanges_state {
660         struct ldb_result *site_res;
661         uint32_t num_sent;
662         struct ldb_dn *ncRoot_dn;
663         uint64_t min_usn;
664         uint64_t highest_usn;
665         struct ldb_dn *last_dn;
666         struct drsuapi_DsReplicaLinkedAttribute *la_list;
667         uint32_t la_count;
668         bool la_sorted;
669         uint32_t la_idx;
670         struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector;
671 };
672
673 /* 
674   drsuapi_DsGetNCChanges
675
676   see MS-DRSR 4.1.10.5.2 for basic logic of this function
677 */
678 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
679                                      struct drsuapi_DsGetNCChanges *r)
680 {
681         struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
682         int ret;
683         unsigned int i;
684         struct dsdb_schema *schema;
685         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
686         struct drsuapi_DsReplicaObjectListItemEx **currentObject;
687         NTSTATUS status;
688         DATA_BLOB session_key;
689         const char *attrs[] = { "*", "distinguishedName",
690                                 "nTSecurityDescriptor",
691                                 "parentGUID",
692                                 "replPropertyMetaData",
693                                 "unicodePwd",
694                                 "dBCSPwd",
695                                 "ntPwdHistory",
696                                 "lmPwdHistory",
697                                 "supplementalCredentials",
698                                 NULL };
699         WERROR werr;
700         struct dcesrv_handle *h;
701         struct drsuapi_bind_state *b_state;     
702         struct drsuapi_getncchanges_state *getnc_state;
703         struct drsuapi_DsGetNCChangesRequest8 *req8;
704         uint32_t options;
705         uint32_t max_objects;
706         uint32_t max_links;
707         uint32_t link_count = 0;
708         uint32_t link_total = 0;
709         uint32_t link_given = 0;
710         struct ldb_dn *search_dn = NULL;
711         bool am_rodc;
712         enum security_user_level security_level;
713
714         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
715         b_state = h->data;
716
717         *r->out.level_out = 6;
718         /* TODO: linked attributes*/
719         r->out.ctr->ctr6.linked_attributes_count = 0;
720         r->out.ctr->ctr6.linked_attributes = NULL;
721
722         r->out.ctr->ctr6.object_count = 0;
723         r->out.ctr->ctr6.nc_object_count = 0;
724         r->out.ctr->ctr6.more_data = false;
725         r->out.ctr->ctr6.uptodateness_vector = NULL;
726
727         /* a RODC doesn't allow for any replication */
728         ret = samdb_rodc(b_state->sam_ctx, &am_rodc);
729         if (ret == LDB_SUCCESS && am_rodc) {
730                 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
731                 return WERR_DS_DRA_SOURCE_DISABLED;
732         }
733
734         /* Check request revision. 
735            TODO: Adding mappings to req8 from the other levels
736          */
737         if (r->in.level != 8) {
738                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
739                          r->in.level));
740                 return WERR_REVISION_MISMATCH;
741         }
742
743         req8 = &r->in.req->req8;
744
745         /* Perform access checks. */
746         /* TODO: we need to support a sync on a specific non-root
747          * DN. We'll need to find the real partition root here */
748         ncRoot = req8->naming_context;
749         if (ncRoot == NULL) {
750                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
751                 return WERR_DS_DRA_INVALID_PARAMETER;
752         }
753
754         if (samdb_ntds_options(b_state->sam_ctx, &options) != LDB_SUCCESS) {
755                 return WERR_DS_DRA_INTERNAL_ERROR;
756         }
757         
758         if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
759             !(req8->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
760                 return WERR_DS_DRA_SOURCE_DISABLED;
761         }
762
763         werr = drs_security_level_check(dce_call, "DsGetNCChanges", SECURITY_RO_DOMAIN_CONTROLLER,
764                                         samdb_domain_sid(b_state->sam_ctx));
765         if (!W_ERROR_IS_OK(werr)) {
766                 return werr;
767         }
768
769         /* for non-administrator replications, check that they have
770            given the correct source_dsa_invocation_id */
771         security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
772                                                      samdb_domain_sid(b_state->sam_ctx));
773         if (security_level == SECURITY_RO_DOMAIN_CONTROLLER &&
774             (req8->replica_flags & DRSUAPI_DRS_WRIT_REP)) {
775                 DEBUG(3,(__location__ ": Removing WRIT_REP flag for replication by RODC %s\n",
776                          dom_sid_string(mem_ctx,
777                                         dce_call->conn->auth_state.session_info->security_token->user_sid)));
778                 req8->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
779         }
780
781
782         if (req8->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
783                 /* Ignore the _in_ uptpdateness vector*/
784                 req8->uptodateness_vector = NULL;
785         } 
786
787         /* we don't yet support extended operations */
788         switch (req8->extended_op) {
789         case DRSUAPI_EXOP_NONE:
790                 break;
791
792         case DRSUAPI_EXOP_FSMO_RID_ALLOC:
793                 werr = getncchanges_rid_alloc(b_state, mem_ctx, req8, &r->out.ctr->ctr6);
794                 W_ERROR_NOT_OK_RETURN(werr);
795                 search_dn = ldb_get_default_basedn(b_state->sam_ctx);
796                 break;
797
798         case DRSUAPI_EXOP_FSMO_REQ_ROLE:
799         case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
800         case DRSUAPI_EXOP_FSMO_REQ_PDC:
801         case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
802         case DRSUAPI_EXOP_REPL_OBJ:
803         case DRSUAPI_EXOP_REPL_SECRET:
804                 DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
805                          (unsigned)req8->extended_op));
806                 return WERR_DS_DRA_NOT_SUPPORTED;
807         }
808
809         getnc_state = b_state->getncchanges_state;
810
811         /* see if a previous replication has been abandoned */
812         if (getnc_state) {
813                 struct ldb_dn *new_dn = ldb_dn_new(getnc_state, b_state->sam_ctx, ncRoot->dn);
814                 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
815                         DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
816                                  ldb_dn_get_linearized(new_dn),
817                                  ldb_dn_get_linearized(getnc_state->ncRoot_dn),
818                                  ldb_dn_get_linearized(getnc_state->last_dn)));
819                         talloc_free(getnc_state);
820                         getnc_state = NULL;
821                 }
822         }
823
824         if (getnc_state == NULL) {
825                 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
826                 if (getnc_state == NULL) {
827                         return WERR_NOMEM;
828                 }
829                 b_state->getncchanges_state = getnc_state;
830                 getnc_state->ncRoot_dn = ldb_dn_new(getnc_state, b_state->sam_ctx, ncRoot->dn);
831         }
832
833         if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
834             ldb_dn_is_null(getnc_state->ncRoot_dn)) {
835                 DEBUG(0,(__location__ ": Bad DN '%s'\n", ncRoot->dn));
836                 return WERR_DS_DRA_INVALID_PARAMETER;
837         }
838
839         /* we need the session key for encrypting password attributes */
840         status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
841         if (!NT_STATUS_IS_OK(status)) {
842                 DEBUG(0,(__location__ ": Failed to get session key\n"));
843                 return WERR_DS_DRA_INTERNAL_ERROR;              
844         }
845
846         /* 
847            TODO: MS-DRSR section 4.1.10.1.1
848            Work out if this is the start of a new cycle */
849
850         if (getnc_state->site_res == NULL) {
851                 char* search_filter;
852                 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
853                 const char *extra_filter;
854
855                 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
856
857                 getnc_state->min_usn = req8->highwatermark.highest_usn;
858
859                 /* Construct response. */
860                 search_filter = talloc_asprintf(mem_ctx,
861                                                 "(uSNChanged>=%llu)",
862                                                 (unsigned long long)(getnc_state->min_usn+1));
863         
864                 if (extra_filter) {
865                         search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
866                 }
867
868                 if (req8->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
869                         search_filter = talloc_asprintf(mem_ctx,
870                                                         "(&%s(isCriticalSystemObject=TRUE))",
871                                                         search_filter);
872                 }
873                 
874                 if (req8->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
875                         scope = LDB_SCOPE_BASE;
876                 }
877                 
878                 if (!search_dn) {
879                         search_dn = getnc_state->ncRoot_dn;
880                 }
881
882                 DEBUG(1,(__location__ ": getncchanges on %s using filter %s\n",
883                          ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
884                 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, &getnc_state->site_res,
885                                                       search_dn, scope, attrs,
886                                                       search_filter);
887                 if (ret != LDB_SUCCESS) {
888                         return WERR_DS_DRA_INTERNAL_ERROR;
889                 }
890
891                 if (req8->replica_flags & DRSUAPI_DRS_GET_ANC) {
892                         TYPESAFE_QSORT(getnc_state->site_res->msgs,
893                                        getnc_state->site_res->count,
894                                        site_res_cmp_parent_order);
895                 } else {
896                         TYPESAFE_QSORT(getnc_state->site_res->msgs,
897                                        getnc_state->site_res->count,
898                                        site_res_cmp_usn_order);
899                 }
900
901                 getnc_state->uptodateness_vector = talloc_steal(getnc_state, req8->uptodateness_vector);
902                 if (getnc_state->uptodateness_vector) {
903                         /* make sure its sorted */
904                         TYPESAFE_QSORT(getnc_state->uptodateness_vector->cursors,
905                                        getnc_state->uptodateness_vector->count,
906                                        drsuapi_DsReplicaCursor_compare);
907                 }
908         }
909
910         /* Prefix mapping */
911         schema = dsdb_get_schema(b_state->sam_ctx, mem_ctx);
912         if (!schema) {
913                 DEBUG(0,("No schema in sam_ctx\n"));
914                 return WERR_DS_DRA_INTERNAL_ERROR;
915         }
916
917         r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
918         *r->out.ctr->ctr6.naming_context = *ncRoot;
919
920         if (dsdb_find_guid_by_dn(b_state->sam_ctx, getnc_state->ncRoot_dn, 
921                                  &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
922                 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
923                          ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
924                 return WERR_DS_DRA_INTERNAL_ERROR;
925         }
926
927         /* find the SID if there is one */
928         dsdb_find_sid_by_dn(b_state->sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
929
930         dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
931         r->out.ctr->ctr6.mapping_ctr = *ctr;
932
933         r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(b_state->sam_ctx));
934         r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(b_state->sam_ctx));
935
936         r->out.ctr->ctr6.old_highwatermark = req8->highwatermark;
937         r->out.ctr->ctr6.new_highwatermark = req8->highwatermark;
938
939         r->out.ctr->ctr6.first_object = NULL;
940         currentObject = &r->out.ctr->ctr6.first_object;
941
942         /* use this to force single objects at a time, which is useful
943          * for working out what object is giving problems
944          */
945         max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
946         if (req8->max_object_count < max_objects) {
947                 max_objects = req8->max_object_count;
948         }
949         /*
950          * TODO: work out how the maximum should be calculated
951          */
952         max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
953
954         for(i=getnc_state->num_sent; 
955             i<getnc_state->site_res->count && 
956                     (r->out.ctr->ctr6.object_count < max_objects);
957             i++) {
958                 int uSN;
959                 struct drsuapi_DsReplicaObjectListItemEx *obj;
960                 struct ldb_message *msg = getnc_state->site_res->msgs[i];
961
962                 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
963
964                 werr = get_nc_changes_build_object(obj, msg,
965                                                    b_state->sam_ctx, getnc_state->ncRoot_dn, 
966                                                    schema, &session_key, getnc_state->min_usn,
967                                                    req8->replica_flags, getnc_state->uptodateness_vector);
968                 if (!W_ERROR_IS_OK(werr)) {
969                         return werr;
970                 }
971
972                 werr = get_nc_changes_add_links(b_state->sam_ctx, getnc_state,
973                                                 getnc_state->ncRoot_dn,
974                                                 schema, getnc_state->min_usn,
975                                                 req8->replica_flags,
976                                                 msg,
977                                                 &getnc_state->la_list,
978                                                 &getnc_state->la_count,
979                                                 getnc_state->uptodateness_vector);
980                 if (!W_ERROR_IS_OK(werr)) {
981                         return werr;
982                 }
983
984                 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
985                 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
986                         r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
987                 }
988                 if (uSN > getnc_state->highest_usn) {
989                         getnc_state->highest_usn = uSN;
990                 }
991
992                 if (obj->meta_data_ctr == NULL) {
993                         DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
994                                  ldb_dn_get_linearized(msg->dn)));
995                         /* no attributes to send */
996                         talloc_free(obj);
997                         continue;
998                 }
999
1000                 r->out.ctr->ctr6.object_count++;
1001                 
1002                 *currentObject = obj;
1003                 currentObject = &obj->next_object;
1004
1005                 talloc_free(getnc_state->last_dn);
1006                 getnc_state->last_dn = ldb_dn_copy(getnc_state, msg->dn);
1007
1008                 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
1009         }
1010
1011         getnc_state->num_sent += r->out.ctr->ctr6.object_count;
1012
1013         r->out.ctr->ctr6.nc_object_count = getnc_state->site_res->count;
1014
1015         /* the client can us to call UpdateRefs on its behalf to
1016            re-establish monitoring of the NC */
1017         if ((req8->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
1018             !GUID_all_zero(&req8->destination_dsa_guid)) {
1019                 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
1020                 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1021                          GUID_string(mem_ctx, &req8->destination_dsa_guid)));
1022                 ureq.naming_context = ncRoot;
1023                 ureq.dest_dsa_dns_name = talloc_asprintf(mem_ctx, "%s._msdcs.%s",
1024                                                          GUID_string(mem_ctx, &req8->destination_dsa_guid),
1025                                                          lpcfg_realm(dce_call->conn->dce_ctx->lp_ctx));
1026                 if (!ureq.dest_dsa_dns_name) {
1027                         return WERR_NOMEM;
1028                 }
1029                 ureq.dest_dsa_guid = req8->destination_dsa_guid;
1030                 ureq.options = DRSUAPI_DRS_ADD_REF |
1031                         DRSUAPI_DRS_ASYNC_OP |
1032                         DRSUAPI_DRS_GETCHG_CHECK;
1033                 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
1034                 if (!W_ERROR_IS_OK(werr)) {
1035                         DEBUG(0,(__location__ ": Failed UpdateRefs in DsGetNCChanges - %s\n",
1036                                  win_errstr(werr)));
1037                 }
1038         }
1039
1040         /*
1041          * TODO:
1042          * This is just a guess, how to calculate the
1043          * number of linked attributes to send, we need to
1044          * find out how to do this right.
1045          */
1046         if (r->out.ctr->ctr6.object_count >= max_links) {
1047                 max_links = 0;
1048         } else {
1049                 max_links -= r->out.ctr->ctr6.object_count;
1050         }
1051
1052         link_total = getnc_state->la_count;
1053
1054         if (i < getnc_state->site_res->count) {
1055                 r->out.ctr->ctr6.more_data = true;
1056         } else {
1057                 /* sort the whole array the first time */
1058                 if (!getnc_state->la_sorted) {
1059                         LDB_TYPESAFE_QSORT(getnc_state->la_list, getnc_state->la_count,
1060                                            b_state->sam_ctx, linked_attribute_compare);
1061                         getnc_state->la_sorted = true;
1062                 }
1063
1064                 link_count = getnc_state->la_count - getnc_state->la_idx;
1065                 link_count = MIN(max_links, link_count);
1066
1067                 r->out.ctr->ctr6.linked_attributes_count = link_count;
1068                 r->out.ctr->ctr6.linked_attributes = getnc_state->la_list + getnc_state->la_idx;
1069
1070                 getnc_state->la_idx += link_count;
1071                 link_given = getnc_state->la_idx;
1072
1073                 if (getnc_state->la_idx < getnc_state->la_count) {
1074                         r->out.ctr->ctr6.more_data = true;
1075                 }
1076         }
1077
1078         if (!r->out.ctr->ctr6.more_data) {
1079                 talloc_steal(mem_ctx, getnc_state->la_list);
1080
1081                 r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
1082                 r->out.ctr->ctr6.new_highwatermark.highest_usn = r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn;
1083
1084                 werr = get_nc_changes_udv(b_state->sam_ctx, getnc_state->ncRoot_dn, 
1085                                           r->out.ctr->ctr6.uptodateness_vector);
1086                 if (!W_ERROR_IS_OK(werr)) {
1087                         return werr;
1088                 }
1089
1090                 talloc_free(getnc_state);
1091                 b_state->getncchanges_state = NULL;
1092         }
1093
1094         if (req8->extended_op != DRSUAPI_EXOP_NONE) {
1095                 r->out.ctr->ctr6.uptodateness_vector = NULL;
1096                 r->out.ctr->ctr6.nc_object_count = 0;
1097                 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
1098         }
1099
1100         DEBUG(r->out.ctr->ctr6.more_data?2:1,
1101               ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u)\n",
1102                (unsigned long long)(req8->highwatermark.highest_usn+1),
1103                req8->replica_flags, ncRoot->dn,
1104                r->out.ctr->ctr6.object_count,
1105                i, r->out.ctr->ctr6.more_data?getnc_state->site_res->count:i,
1106                r->out.ctr->ctr6.linked_attributes_count,
1107                link_given, link_total));
1108
1109 #if 0
1110         if (!r->out.ctr->ctr6.more_data) {
1111                 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
1112         }
1113 #endif
1114
1115         return WERR_OK;
1116 }