getncchanges: pass struct ldb_message as const
[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-2010
8    Copyright (C) Andrew Bartlett 2010-2016
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "param/param.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "libcli/security/security.h"
32 #include "libcli/security/session.h"
33 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
34 #include "rpc_server/dcerpc_server_proto.h"
35 #include "../libcli/drsuapi/drsuapi.h"
36 #include "lib/util/binsearch.h"
37 #include "lib/util/tsort.h"
38 #include "auth/session.h"
39 #include "dsdb/common/util.h"
40
41 /* state of a partially completed getncchanges call */
42 struct drsuapi_getncchanges_state {
43         struct GUID *guids;
44         uint32_t num_records;
45         uint32_t num_processed;
46         struct ldb_dn *ncRoot_dn;
47         bool is_schema_nc;
48         uint64_t min_usn;
49         uint64_t max_usn;
50         struct drsuapi_DsReplicaHighWaterMark last_hwm;
51         struct ldb_dn *last_dn;
52         struct drsuapi_DsReplicaHighWaterMark final_hwm;
53         struct drsuapi_DsReplicaCursor2CtrEx *final_udv;
54         struct drsuapi_DsReplicaLinkedAttribute *la_list;
55         uint32_t la_count;
56         struct la_for_sorting *la_sorted;
57         uint32_t la_idx;
58 };
59
60 /* We must keep the GUIDs in NDR form for sorting */
61 struct la_for_sorting {
62         struct drsuapi_DsReplicaLinkedAttribute *link;
63         uint8_t target_guid[16];
64         uint8_t source_guid[16];
65 };
66
67 static int drsuapi_DsReplicaHighWaterMark_cmp(const struct drsuapi_DsReplicaHighWaterMark *h1,
68                                               const struct drsuapi_DsReplicaHighWaterMark *h2)
69 {
70         if (h1->highest_usn < h2->highest_usn) {
71                 return -1;
72         } else if (h1->highest_usn > h2->highest_usn) {
73                 return 1;
74         } else if (h1->tmp_highest_usn < h2->tmp_highest_usn) {
75                 return -1;
76         } else if (h1->tmp_highest_usn > h2->tmp_highest_usn) {
77                 return 1;
78         } else if (h1->reserved_usn < h2->reserved_usn) {
79                 return -1;
80         } else if (h1->reserved_usn > h2->reserved_usn) {
81                 return 1;
82         }
83
84         return 0;
85 }
86
87 /*
88   build a DsReplicaObjectIdentifier from a ldb msg
89  */
90 static struct drsuapi_DsReplicaObjectIdentifier *get_object_identifier(TALLOC_CTX *mem_ctx,
91                                                                        const struct ldb_message *msg)
92 {
93         struct drsuapi_DsReplicaObjectIdentifier *identifier;
94         struct dom_sid *sid;
95
96         identifier = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
97         if (identifier == NULL) {
98                 return NULL;
99         }
100
101         identifier->dn = ldb_dn_alloc_linearized(identifier, msg->dn);
102         identifier->guid = samdb_result_guid(msg, "objectGUID");
103
104         sid = samdb_result_dom_sid(identifier, msg, "objectSid");
105         if (sid) {
106                 identifier->sid = *sid;
107         } else {
108                 ZERO_STRUCT(identifier->sid);
109         }
110         return identifier;
111 }
112
113 static int udv_compare(const struct GUID *guid1, struct GUID guid2)
114 {
115         return GUID_compare(guid1, &guid2);
116 }
117
118 /*
119   see if we can filter an attribute using the uptodateness_vector
120  */
121 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx *udv,
122                        const struct GUID *originating_invocation_id,
123                        uint64_t originating_usn)
124 {
125         const struct drsuapi_DsReplicaCursor *c;
126         if (udv == NULL) return false;
127         BINARY_ARRAY_SEARCH(udv->cursors, udv->count, source_dsa_invocation_id,
128                             originating_invocation_id, udv_compare, c);
129         if (c && originating_usn <= c->highest_usn) {
130                 return true;
131         }
132         return false;
133
134 }
135
136 static int uint32_t_cmp(uint32_t a1, uint32_t a2)
137 {
138         if (a1 == a2) return 0;
139         return a1 > a2 ? 1 : -1;
140 }
141
142 static int uint32_t_ptr_cmp(uint32_t *a1, uint32_t *a2, void *unused)
143 {
144         if (*a1 == *a2) return 0;
145         return *a1 > *a2 ? 1 : -1;
146 }
147
148 static WERROR getncchanges_attid_remote_to_local(const struct dsdb_schema *schema,
149                                                  const struct dsdb_syntax_ctx *ctx,
150                                                  enum drsuapi_DsAttributeId remote_attid_as_enum,
151                                                  enum drsuapi_DsAttributeId *local_attid_as_enum,
152                                                  const struct dsdb_attribute **_sa)
153 {
154         WERROR werr;
155         const struct dsdb_attribute *sa = NULL;
156
157         if (ctx->pfm_remote == NULL) {
158                 DEBUG(7, ("No prefixMap supplied, falling back to local prefixMap.\n"));
159                 goto fail;
160         }
161
162         werr = dsdb_attribute_drsuapi_remote_to_local(ctx,
163                                                       remote_attid_as_enum,
164                                                       local_attid_as_enum,
165                                                       _sa);
166         if (!W_ERROR_IS_OK(werr)) {
167                 DEBUG(3, ("WARNING: Unable to resolve remote attid, falling back to local prefixMap.\n"));
168                 goto fail;
169         }
170
171         return werr;
172 fail:
173
174         sa = dsdb_attribute_by_attributeID_id(schema, remote_attid_as_enum);
175         if (sa == NULL) {
176                 return WERR_DS_DRA_SCHEMA_MISMATCH;
177         } else {
178                 if (local_attid_as_enum != NULL) {
179                         *local_attid_as_enum = sa->attributeID_id;
180                 }
181                 if (_sa != NULL) {
182                         *_sa = sa;
183                 }
184                 return WERR_OK;
185         }
186 }
187
188 /* 
189   drsuapi_DsGetNCChanges for one object
190 */
191 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
192                                           const struct ldb_message *msg,
193                                           struct ldb_context *sam_ctx,
194                                           struct ldb_dn *ncRoot_dn,
195                                           bool   is_schema_nc,
196                                           struct dsdb_schema *schema,
197                                           DATA_BLOB *session_key,
198                                           uint64_t highest_usn,
199                                           uint32_t replica_flags,
200                                           struct drsuapi_DsPartialAttributeSet *partial_attribute_set,
201                                           struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector,
202                                           enum drsuapi_DsExtendedOperation extended_op,
203                                           bool force_object_return,
204                                           uint32_t *local_pas)
205 {
206         const struct ldb_val *md_value;
207         uint32_t i, n;
208         struct replPropertyMetaDataBlob md;
209         uint32_t rid = 0;
210         enum ndr_err_code ndr_err;
211         uint32_t *attids;
212         const char *rdn;
213         const struct dsdb_attribute *rdn_sa;
214         unsigned int instanceType;
215         struct dsdb_syntax_ctx syntax_ctx;
216
217         /* make dsdb sytanx context for conversions */
218         dsdb_syntax_ctx_init(&syntax_ctx, sam_ctx, schema);
219         syntax_ctx.is_schema_nc = is_schema_nc;
220
221         instanceType = ldb_msg_find_attr_as_uint(msg, "instanceType", 0);
222         if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
223                 obj->is_nc_prefix = true;
224                 obj->parent_object_guid = NULL;
225         } else {
226                 obj->is_nc_prefix = false;
227                 obj->parent_object_guid = talloc(obj, struct GUID);
228                 if (obj->parent_object_guid == NULL) {
229                         return WERR_DS_DRA_INTERNAL_ERROR;
230                 }
231                 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
232                 if (GUID_all_zero(obj->parent_object_guid)) {
233                         DEBUG(0,(__location__ ": missing parentGUID for %s\n",
234                                  ldb_dn_get_linearized(msg->dn)));
235                         return WERR_DS_DRA_INTERNAL_ERROR;
236                 }
237         }
238         obj->next_object = NULL;
239
240         md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
241         if (!md_value) {
242                 /* nothing to send */
243                 return WERR_OK;
244         }
245
246         if (instanceType & INSTANCE_TYPE_UNINSTANT) {
247                 /* don't send uninstantiated objects */
248                 return WERR_OK;
249         }
250
251         ndr_err = ndr_pull_struct_blob(md_value, obj, &md,
252                                        (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
253         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
254                 return WERR_DS_DRA_INTERNAL_ERROR;
255         }
256
257         if (md.version != 1) {
258                 return WERR_DS_DRA_INTERNAL_ERROR;
259         }
260
261         rdn = ldb_dn_get_rdn_name(msg->dn);
262         if (rdn == NULL) {
263                 DEBUG(0,(__location__ ": No rDN for %s\n", ldb_dn_get_linearized(msg->dn)));
264                 return WERR_DS_DRA_INTERNAL_ERROR;
265         }
266
267         rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn);
268         if (rdn_sa == NULL) {
269                 DEBUG(0,(__location__ ": Can't find dsds_attribute for rDN %s in %s\n",
270                          rdn, ldb_dn_get_linearized(msg->dn)));
271                 return WERR_DS_DRA_INTERNAL_ERROR;
272         }
273
274         obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
275         attids = talloc_array(obj, uint32_t, md.ctr.ctr1.count);
276
277         obj->object.identifier = get_object_identifier(obj, msg);
278         if (obj->object.identifier == NULL) {
279                 return WERR_NOT_ENOUGH_MEMORY;
280         }
281         dom_sid_split_rid(NULL, &obj->object.identifier->sid, NULL, &rid);
282
283         obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
284         for (n=i=0; i<md.ctr.ctr1.count; i++) {
285                 const struct dsdb_attribute *sa;
286                 bool force_attribute = false;
287
288                 /* if the attribute has not changed, and it is not the
289                    instanceType then don't include it */
290                 if (md.ctr.ctr1.array[i].local_usn < highest_usn &&
291                     extended_op != DRSUAPI_EXOP_REPL_SECRET &&
292                     md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType) continue;
293
294                 /* don't include the rDN */
295                 if (md.ctr.ctr1.array[i].attid == rdn_sa->attributeID_id) continue;
296
297                 sa = dsdb_attribute_by_attributeID_id(schema, md.ctr.ctr1.array[i].attid);
298                 if (!sa) {
299                         DEBUG(0,(__location__ ": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
300                                  (unsigned int)md.ctr.ctr1.array[i].attid,
301                                  ldb_dn_get_linearized(msg->dn)));
302                         return WERR_DS_DRA_INTERNAL_ERROR;
303                 }
304
305                 if (sa->linkID) {
306                         struct ldb_message_element *el;
307                         el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
308                         if (el && el->num_values && dsdb_dn_is_upgraded_link_val(&el->values[0])) {
309                                 /* don't send upgraded links inline */
310                                 continue;
311                         }
312                 }
313
314                 if (extended_op == DRSUAPI_EXOP_REPL_SECRET &&
315                     !dsdb_attr_in_rodc_fas(sa)) {
316                         force_attribute = true;
317                         DEBUG(4,("Forcing attribute %s in %s\n",
318                                  sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
319                 }
320
321                 /* filter by uptodateness_vector */
322                 if (md.ctr.ctr1.array[i].attid != DRSUAPI_ATTID_instanceType &&
323                     !force_attribute &&
324                     udv_filter(uptodateness_vector,
325                                &md.ctr.ctr1.array[i].originating_invocation_id,
326                                md.ctr.ctr1.array[i].originating_usn)) {
327                         continue;
328                 }
329
330                 /* filter by partial_attribute_set */
331                 if (partial_attribute_set) {
332                         uint32_t *result = NULL;
333                         BINARY_ARRAY_SEARCH_V(local_pas, partial_attribute_set->num_attids, sa->attributeID_id,
334                                               uint32_t_cmp, result);
335                         if (result == NULL) {
336                                 continue;
337                         }
338                 }
339
340                 obj->meta_data_ctr->meta_data[n].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
341                 obj->meta_data_ctr->meta_data[n].version = md.ctr.ctr1.array[i].version;
342                 obj->meta_data_ctr->meta_data[n].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
343                 obj->meta_data_ctr->meta_data[n].originating_usn = md.ctr.ctr1.array[i].originating_usn;
344                 attids[n] = md.ctr.ctr1.array[i].attid;
345                 n++;
346         }
347
348         /* ignore it if its an empty change. Note that renames always
349          * change the 'name' attribute, so they won't be ignored by
350          * this
351
352          * the force_object_return check is used to force an empty
353          * object return when we timeout in the getncchanges loop.
354          * This allows us to return an empty object, which keeps the
355          * client happy while preventing timeouts
356          */
357         if (n == 0 ||
358             (n == 1 &&
359              attids[0] == DRSUAPI_ATTID_instanceType &&
360              !force_object_return)) {
361                 talloc_free(obj->meta_data_ctr);
362                 obj->meta_data_ctr = NULL;
363                 return WERR_OK;
364         }
365
366         obj->meta_data_ctr->count = n;
367
368         obj->object.flags = DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER;
369         obj->object.attribute_ctr.num_attributes = obj->meta_data_ctr->count;
370         obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
371                                                             obj->object.attribute_ctr.num_attributes);
372         if (obj->object.attribute_ctr.attributes == NULL) {
373                 return WERR_NOT_ENOUGH_MEMORY;
374         }
375
376         /*
377          * Note that the meta_data array and the attributes array must
378          * be the same size and in the same order
379          */
380         for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
381                 struct ldb_message_element *el;
382                 WERROR werr;
383                 const struct dsdb_attribute *sa;
384
385                 sa = dsdb_attribute_by_attributeID_id(schema, attids[i]);
386                 if (!sa) {
387                         DEBUG(0,("Unable to find attributeID %u in schema\n", attids[i]));
388                         return WERR_DS_DRA_INTERNAL_ERROR;
389                 }
390
391                 el = ldb_msg_find_element(msg, sa->lDAPDisplayName);
392                 if (el == NULL) {
393                         /* this happens for attributes that have been removed */
394                         DEBUG(5,("No element '%s' for attributeID %u in message\n",
395                                  sa->lDAPDisplayName, attids[i]));
396                         ZERO_STRUCT(obj->object.attribute_ctr.attributes[i]);
397                         obj->object.attribute_ctr.attributes[i].attid =
398                                         dsdb_attribute_get_attid(sa, syntax_ctx.is_schema_nc);
399                 } else {
400                         werr = sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, el, obj,
401                                                           &obj->object.attribute_ctr.attributes[i]);
402                         if (!W_ERROR_IS_OK(werr)) {
403                                 DEBUG(0,("Unable to convert %s on %s to DRS object - %s\n",
404                                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
405                                          win_errstr(werr)));
406                                 return werr;
407                         }
408                         /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
409                          * check if attribute is secret and send a null value
410                          */
411                         if (replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
412                                 drsuapi_process_secret_attribute(&obj->object.attribute_ctr.attributes[i],
413                                                                  &obj->meta_data_ctr->meta_data[i]);
414                         }
415                         /* some attributes needs to be encrypted
416                            before being sent */
417                         werr = drsuapi_encrypt_attribute(obj, session_key, rid, 
418                                                          &obj->object.attribute_ctr.attributes[i]);
419                         if (!W_ERROR_IS_OK(werr)) {
420                                 DEBUG(0,("Unable to encrypt %s on %s in DRS object - %s\n",
421                                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn),
422                                          win_errstr(werr)));
423                                 return werr;
424                         }
425                 }
426                 if (attids[i] != obj->object.attribute_ctr.attributes[i].attid) {
427                         DEBUG(0, ("Unable to replicate attribute %s on %s via DRS, incorrect attributeID:  "
428                                   "0x%08x vs 0x%08x "
429                                   "Run dbcheck!\n",
430                                   sa->lDAPDisplayName,
431                                   ldb_dn_get_linearized(msg->dn),
432                                   attids[i],
433                                   obj->object.attribute_ctr.attributes[i].attid));
434                         return WERR_DS_DATABASE_ERROR;
435                 }
436         }
437
438         return WERR_OK;
439 }
440
441 /*
442   add one linked attribute from an object to the list of linked
443   attributes in a getncchanges request
444  */
445 static WERROR get_nc_changes_add_la(TALLOC_CTX *mem_ctx,
446                                     struct ldb_context *sam_ctx,
447                                     const struct dsdb_schema *schema,
448                                     const struct dsdb_attribute *sa,
449                                     const struct ldb_message *msg,
450                                     struct dsdb_dn *dsdb_dn,
451                                     struct drsuapi_DsReplicaLinkedAttribute **la_list,
452                                     uint32_t *la_count,
453                                     bool is_schema_nc)
454 {
455         struct drsuapi_DsReplicaLinkedAttribute *la;
456         bool active;
457         NTSTATUS status;
458         WERROR werr;
459
460         (*la_list) = talloc_realloc(mem_ctx, *la_list, struct drsuapi_DsReplicaLinkedAttribute, (*la_count)+1);
461         W_ERROR_HAVE_NO_MEMORY(*la_list);
462
463         la = &(*la_list)[*la_count];
464
465         la->identifier = get_object_identifier(*la_list, msg);
466         W_ERROR_HAVE_NO_MEMORY(la->identifier);
467
468         active = (dsdb_dn_rmd_flags(dsdb_dn->dn) & DSDB_RMD_FLAG_DELETED) == 0;
469
470         if (!active) {
471                 /* We have to check that the inactive link still point to an existing object */
472                 struct GUID guid;
473                 struct ldb_dn *tdn;
474                 int ret;
475                 const char *v;
476
477                 v = ldb_msg_find_attr_as_string(msg, "isDeleted", "FALSE");
478                 if (strncmp(v, "TRUE", 4) == 0) {
479                         /*
480                           * Note: we skip the transmition of the deleted link even if the other part used to
481                           * know about it because when we transmit the deletion of the object, the link will
482                           * be deleted too due to deletion of object where link points and Windows do so.
483                           */
484                         if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008_R2) {
485                                 v = ldb_msg_find_attr_as_string(msg, "isRecycled", "FALSE");
486                                 /*
487                                  * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
488                                  * if it join an existing domain with deleted objets, it firsts impose to have a
489                                  * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
490                                  * either during initial replication or after the getNCChanges.
491                                  * Behavior of samba has been changed to always have this attribute if it's present in the schema.
492                                  *
493                                  * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
494                                  * If FL >=2K8R2 we are sure that this attribute will be here.
495                                  * For this kind of forest level we do not return the link if the object is recycled
496                                  * (isRecycled = true).
497                                  */
498                                 if (strncmp(v, "TRUE", 4) == 0) {
499                                         DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
500                                                                 ldb_dn_get_linearized(msg->dn)));
501                                         return WERR_OK;
502                                 }
503                         } else {
504                                 return WERR_OK;
505                         }
506                 }
507                 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
508                 if (!NT_STATUS_IS_OK(status)) {
509                         DEBUG(0,(__location__ " Unable to extract GUID in linked attribute '%s' in '%s'\n",
510                                 sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
511                         return ntstatus_to_werror(status);
512                 }
513                 ret = dsdb_find_dn_by_guid(sam_ctx, mem_ctx, &guid, 0, &tdn);
514                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
515                         DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
516                                                 GUID_string(mem_ctx, &guid)));
517                         return WERR_OK;
518                 } else if (ret != LDB_SUCCESS) {
519                         DEBUG(0, (__location__ " Search of guid %s failed with error code %d\n",
520                                                 GUID_string(mem_ctx, &guid),
521                                                 ret));
522                         return WERR_OK;
523                 }
524         }
525         la->attid = dsdb_attribute_get_attid(sa, is_schema_nc);
526         la->flags = active?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE:0;
527
528         status = dsdb_get_extended_dn_uint32(dsdb_dn->dn, &la->meta_data.version, "RMD_VERSION");
529         if (!NT_STATUS_IS_OK(status)) {
530                 DEBUG(0,(__location__ " No RMD_VERSION in linked attribute '%s' in '%s'\n",
531                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
532                 return ntstatus_to_werror(status);
533         }
534         status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->meta_data.originating_change_time, "RMD_CHANGETIME");
535         if (!NT_STATUS_IS_OK(status)) {
536                 DEBUG(0,(__location__ " No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
537                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
538                 return ntstatus_to_werror(status);
539         }
540         status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &la->meta_data.originating_invocation_id, "RMD_INVOCID");
541         if (!NT_STATUS_IS_OK(status)) {
542                 DEBUG(0,(__location__ " No RMD_INVOCID in linked attribute '%s' in '%s'\n",
543                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
544                 return ntstatus_to_werror(status);
545         }
546         status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &la->meta_data.originating_usn, "RMD_ORIGINATING_USN");
547         if (!NT_STATUS_IS_OK(status)) {
548                 DEBUG(0,(__location__ " No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
549                          sa->lDAPDisplayName, ldb_dn_get_linearized(msg->dn)));
550                 return ntstatus_to_werror(status);
551         }
552
553         status = dsdb_get_extended_dn_nttime(dsdb_dn->dn, &la->originating_add_time, "RMD_ADDTIME");
554         if (!NT_STATUS_IS_OK(status)) {
555                 /* this is possible for upgraded links */
556                 la->originating_add_time = la->meta_data.originating_change_time;
557         }
558
559         werr = dsdb_dn_la_to_blob(sam_ctx, sa, schema, *la_list, dsdb_dn, &la->value.blob);
560         W_ERROR_NOT_OK_RETURN(werr);
561
562         (*la_count)++;
563         return WERR_OK;
564 }
565
566
567 /*
568   add linked attributes from an object to the list of linked
569   attributes in a getncchanges request
570  */
571 static WERROR get_nc_changes_add_links(struct ldb_context *sam_ctx,
572                                        TALLOC_CTX *mem_ctx,
573                                        struct ldb_dn *ncRoot_dn,
574                                        bool is_schema_nc,
575                                        struct dsdb_schema *schema,
576                                        uint64_t highest_usn,
577                                        uint32_t replica_flags,
578                                        const struct ldb_message *msg,
579                                        struct drsuapi_DsReplicaLinkedAttribute **la_list,
580                                        uint32_t *la_count,
581                                        struct drsuapi_DsReplicaCursorCtrEx *uptodateness_vector)
582 {
583         unsigned int i;
584         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
585         uint64_t uSNChanged = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
586
587         for (i=0; i<msg->num_elements; i++) {
588                 struct ldb_message_element *el = &msg->elements[i];
589                 const struct dsdb_attribute *sa;
590                 unsigned int j;
591
592                 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
593
594                 if (!sa || sa->linkID == 0 || (sa->linkID & 1)) {
595                         /* we only want forward links */
596                         continue;
597                 }
598
599                 if (el->num_values && !dsdb_dn_is_upgraded_link_val(&el->values[0])) {
600                         /* its an old style link, it will have been
601                          * sent in the main replication data */
602                         continue;
603                 }
604
605                 for (j=0; j<el->num_values; j++) {
606                         struct dsdb_dn *dsdb_dn;
607                         uint64_t local_usn;
608                         uint64_t originating_usn;
609                         NTSTATUS status, status2;
610                         WERROR werr;
611                         struct GUID originating_invocation_id;
612
613                         dsdb_dn = dsdb_dn_parse(tmp_ctx, sam_ctx, &el->values[j], sa->syntax->ldap_oid);
614                         if (dsdb_dn == NULL) {
615                                 DEBUG(1,(__location__ ": Failed to parse DN for %s in %s\n",
616                                          el->name, ldb_dn_get_linearized(msg->dn)));
617                                 talloc_free(tmp_ctx);
618                                 return WERR_DS_DRA_INTERNAL_ERROR;
619                         }
620
621                         status = dsdb_get_extended_dn_uint64(dsdb_dn->dn, &local_usn, "RMD_LOCAL_USN");
622                         if (!NT_STATUS_IS_OK(status)) {
623                                 /* this can happen for attributes
624                                    given to us with old style meta
625                                    data */
626                                 continue;
627                         }
628
629                         if (local_usn > uSNChanged) {
630                                 DEBUG(1,(__location__ ": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
631                                          el->name, ldb_dn_get_linearized(msg->dn)));
632                                 talloc_free(tmp_ctx);
633                                 return WERR_DS_DRA_INTERNAL_ERROR;
634                         }
635
636                         if (local_usn < highest_usn) {
637                                 continue;
638                         }
639
640                         status = dsdb_get_extended_dn_guid(dsdb_dn->dn,
641                                                            &originating_invocation_id,
642                                                            "RMD_INVOCID");
643                         status2 = dsdb_get_extended_dn_uint64(dsdb_dn->dn,
644                                                               &originating_usn,
645                                                               "RMD_ORIGINATING_USN");
646
647                         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2)) {
648                                 if (udv_filter(uptodateness_vector,
649                                                &originating_invocation_id,
650                                                originating_usn)) {
651                                         continue;
652                                 }
653                         }
654
655                         werr = get_nc_changes_add_la(mem_ctx, sam_ctx, schema,
656                                                      sa, msg, dsdb_dn, la_list,
657                                                      la_count, is_schema_nc);
658                         if (!W_ERROR_IS_OK(werr)) {
659                                 talloc_free(tmp_ctx);
660                                 return werr;
661                         }
662                 }
663         }
664
665         talloc_free(tmp_ctx);
666         return WERR_OK;
667 }
668
669 /*
670   fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
671  */
672 static WERROR get_nc_changes_udv(struct ldb_context *sam_ctx,
673                                  struct ldb_dn *ncRoot_dn,
674                                  struct drsuapi_DsReplicaCursor2CtrEx *udv)
675 {
676         int ret;
677
678         udv->version = 2;
679         udv->reserved1 = 0;
680         udv->reserved2 = 0;
681
682         ret = dsdb_load_udv_v2(sam_ctx, ncRoot_dn, udv, &udv->cursors, &udv->count);
683         if (ret != LDB_SUCCESS) {
684                 DEBUG(0,(__location__ ": Failed to load UDV for %s - %s\n",
685                          ldb_dn_get_linearized(ncRoot_dn), ldb_errstring(sam_ctx)));
686                 return WERR_DS_DRA_INTERNAL_ERROR;
687         }
688
689         return WERR_OK;
690 }
691
692
693 /* comparison function for linked attributes - see CompareLinks() in
694  * MS-DRSR section 4.1.10.5.17 */
695 static int linked_attribute_compare(const struct la_for_sorting *la1,
696                                     const struct la_for_sorting *la2,
697                                     void *opaque)
698 {
699         int c;
700         c = memcmp(la1->source_guid,
701                    la2->source_guid, sizeof(la2->source_guid));
702         if (c != 0) {
703                 return c;
704         }
705
706         if (la1->link->attid != la2->link->attid) {
707                 return la1->link->attid < la2->link->attid? -1:1;
708         }
709
710         if ((la1->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) !=
711             (la2->link->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)) {
712                 return (la1->link->flags &
713                         DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)? 1:-1;
714         }
715
716         return memcmp(la1->target_guid,
717                       la2->target_guid, sizeof(la2->target_guid));
718 }
719
720 struct drsuapi_changed_objects {
721         struct ldb_dn *dn;
722         struct GUID guid;
723         uint64_t usn;
724 };
725
726 /*
727   sort the objects we send by tree order
728  */
729 static int site_res_cmp_anc_order(struct drsuapi_changed_objects *m1,
730                                   struct drsuapi_changed_objects *m2,
731                                   struct drsuapi_getncchanges_state *getnc_state)
732 {
733         return ldb_dn_compare(m2->dn, m1->dn);
734 }
735
736 /*
737   sort the objects we send first by uSNChanged
738  */
739 static int site_res_cmp_usn_order(struct drsuapi_changed_objects *m1,
740                                   struct drsuapi_changed_objects *m2,
741                                   struct drsuapi_getncchanges_state *getnc_state)
742 {
743         int ret;
744
745         ret = ldb_dn_compare(getnc_state->ncRoot_dn, m1->dn);
746         if (ret == 0) {
747                 return -1;
748         }
749
750         ret = ldb_dn_compare(getnc_state->ncRoot_dn, m2->dn);
751         if (ret == 0) {
752                 return 1;
753         }
754
755         if (m1->usn == m2->usn) {
756                 return ldb_dn_compare(m2->dn, m1->dn);
757         }
758
759         if (m1->usn < m2->usn) {
760                 return -1;
761         }
762
763         return 1;
764 }
765
766
767 /*
768   handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
769  */
770 static WERROR getncchanges_rid_alloc(struct drsuapi_bind_state *b_state,
771                                      TALLOC_CTX *mem_ctx,
772                                      struct drsuapi_DsGetNCChangesRequest10 *req10,
773                                      struct drsuapi_DsGetNCChangesCtr6 *ctr6,
774                                      struct ldb_dn **rid_manager_dn)
775 {
776         struct ldb_dn *req_dn, *ntds_dn = NULL;
777         int ret;
778         struct ldb_context *ldb = b_state->sam_ctx;
779         struct ldb_result *ext_res;
780         struct dsdb_fsmo_extended_op *exop;
781         bool is_us;
782
783         /*
784           steps:
785             - verify that the DN being asked for is the RID Manager DN
786             - verify that we are the RID Manager
787          */
788
789         /* work out who is the RID Manager, also return to caller */
790         ret = samdb_rid_manager_dn(ldb, mem_ctx, rid_manager_dn);
791         if (ret != LDB_SUCCESS) {
792                 DEBUG(0, (__location__ ": Failed to find RID Manager object - %s\n", ldb_errstring(ldb)));
793                 return WERR_DS_DRA_INTERNAL_ERROR;
794         }
795
796         req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
797         if (!ldb_dn_validate(req_dn) ||
798             ldb_dn_compare(req_dn, *rid_manager_dn) != 0) {
799                 /* that isn't the RID Manager DN */
800                 DEBUG(0,(__location__ ": RID Alloc request for wrong DN %s\n",
801                          drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
802                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
803                 return WERR_OK;
804         }
805
806         /* TODO: make sure ntds_dn is a valid nTDSDSA object */
807         ret = dsdb_find_dn_by_guid(ldb, mem_ctx, &req10->destination_dsa_guid, 0, &ntds_dn);
808         if (ret != LDB_SUCCESS) {
809                 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
810                           GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
811                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
812                 return WERR_OK;
813         }
814
815         /* find the DN of the RID Manager */
816         ret = samdb_reference_dn_is_our_ntdsa(ldb, *rid_manager_dn, "fSMORoleOwner", &is_us);
817         if (ret != LDB_SUCCESS) {
818                 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
819                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
820                 return WERR_DS_DRA_INTERNAL_ERROR;
821         }
822
823         if (!is_us) {
824                 /* we're not the RID Manager - go away */
825                 DEBUG(0,(__location__ ": RID Alloc request when not RID Manager\n"));
826                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
827                 return WERR_OK;
828         }
829
830         exop = talloc(mem_ctx, struct dsdb_fsmo_extended_op);
831         W_ERROR_HAVE_NO_MEMORY(exop);
832
833         exop->fsmo_info = req10->fsmo_info;
834         exop->destination_dsa_guid = req10->destination_dsa_guid;
835
836         ret = ldb_transaction_start(ldb);
837         if (ret != LDB_SUCCESS) {
838                 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
839                          ldb_errstring(ldb)));
840                 return WERR_DS_DRA_INTERNAL_ERROR;
841         }
842
843         ret = ldb_extended(ldb, DSDB_EXTENDED_ALLOCATE_RID_POOL, exop, &ext_res);
844         if (ret != LDB_SUCCESS) {
845                 DEBUG(0,(__location__ ": Failed extended allocation RID pool operation - %s\n",
846                          ldb_errstring(ldb)));
847                 ldb_transaction_cancel(ldb);
848                 return WERR_DS_DRA_INTERNAL_ERROR;
849         }
850
851         ret = ldb_transaction_commit(ldb);
852         if (ret != LDB_SUCCESS) {
853                 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
854                          ldb_errstring(ldb)));
855                 return WERR_DS_DRA_INTERNAL_ERROR;
856         }
857
858         talloc_free(ext_res);
859
860         DEBUG(2,("Allocated RID pool for server %s\n",
861                  GUID_string(mem_ctx, &req10->destination_dsa_guid)));
862
863         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
864
865         return WERR_OK;
866 }
867
868 /*
869   return an array of SIDs from a ldb_message given an attribute name
870   assumes the SIDs are in extended DN format
871  */
872 static WERROR samdb_result_sid_array_dn(struct ldb_context *sam_ctx,
873                                         struct ldb_message *msg,
874                                         TALLOC_CTX *mem_ctx,
875                                         const char *attr,
876                                         const struct dom_sid ***sids)
877 {
878         struct ldb_message_element *el;
879         unsigned int i;
880
881         el = ldb_msg_find_element(msg, attr);
882         if (!el) {
883                 *sids = NULL;
884                 return WERR_OK;
885         }
886
887         (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
888         W_ERROR_HAVE_NO_MEMORY(*sids);
889
890         for (i=0; i<el->num_values; i++) {
891                 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, sam_ctx, &el->values[i]);
892                 NTSTATUS status;
893                 struct dom_sid *sid;
894
895                 sid = talloc(*sids, struct dom_sid);
896                 W_ERROR_HAVE_NO_MEMORY(sid);
897                 status = dsdb_get_extended_dn_sid(dn, sid, "SID");
898                 if (!NT_STATUS_IS_OK(status)) {
899                         return WERR_INTERNAL_DB_CORRUPTION;
900                 }
901                 (*sids)[i] = sid;
902         }
903         (*sids)[i] = NULL;
904
905         return WERR_OK;
906 }
907
908
909 /*
910   return an array of SIDs from a ldb_message given an attribute name
911   assumes the SIDs are in NDR form
912  */
913 static WERROR samdb_result_sid_array_ndr(struct ldb_context *sam_ctx,
914                                          struct ldb_message *msg,
915                                          TALLOC_CTX *mem_ctx,
916                                          const char *attr,
917                                          const struct dom_sid ***sids)
918 {
919         struct ldb_message_element *el;
920         unsigned int i;
921
922         el = ldb_msg_find_element(msg, attr);
923         if (!el) {
924                 *sids = NULL;
925                 return WERR_OK;
926         }
927
928         (*sids) = talloc_array(mem_ctx, const struct dom_sid *, el->num_values + 1);
929         W_ERROR_HAVE_NO_MEMORY(*sids);
930
931         for (i=0; i<el->num_values; i++) {
932                 enum ndr_err_code ndr_err;
933                 struct dom_sid *sid;
934
935                 sid = talloc(*sids, struct dom_sid);
936                 W_ERROR_HAVE_NO_MEMORY(sid);
937
938                 ndr_err = ndr_pull_struct_blob(&el->values[i], sid, sid,
939                                                (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
940                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
941                         return WERR_INTERNAL_DB_CORRUPTION;
942                 }
943                 (*sids)[i] = sid;
944         }
945         (*sids)[i] = NULL;
946
947         return WERR_OK;
948 }
949
950 /*
951   see if any SIDs in list1 are in list2
952  */
953 static bool sid_list_match(const struct dom_sid **list1, const struct dom_sid **list2)
954 {
955         unsigned int i, j;
956         /* do we ever have enough SIDs here to worry about O(n^2) ? */
957         for (i=0; list1[i]; i++) {
958                 for (j=0; list2[j]; j++) {
959                         if (dom_sid_equal(list1[i], list2[j])) {
960                                 return true;
961                         }
962                 }
963         }
964         return false;
965 }
966
967 /*
968   handle a DRSUAPI_EXOP_REPL_SECRET call
969  */
970 static WERROR getncchanges_repl_secret(struct drsuapi_bind_state *b_state,
971                                        TALLOC_CTX *mem_ctx,
972                                        struct drsuapi_DsGetNCChangesRequest10 *req10,
973                                        struct dom_sid *user_sid,
974                                        struct drsuapi_DsGetNCChangesCtr6 *ctr6,
975                                        bool has_get_all_changes)
976 {
977         struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
978         struct ldb_dn *obj_dn = NULL;
979         struct ldb_dn *rodc_dn, *krbtgt_link_dn;
980         int ret;
981         const char *rodc_attrs[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL };
982         const char *obj_attrs[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL };
983         struct ldb_result *rodc_res, *obj_res;
984         const struct dom_sid **never_reveal_sids, **reveal_sids, **token_sids;
985         WERROR werr;
986
987         DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
988                  drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
989
990         /*
991          * we need to work out if we will allow this DC to
992          * replicate the secrets for this object
993          *
994          * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
995          * of this function
996          */
997
998         if (b_state->sam_ctx_system == NULL) {
999                 /* this operation needs system level access */
1000                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_ACCESS_DENIED;
1001                 return WERR_DS_DRA_SOURCE_DISABLED;
1002         }
1003
1004         /*
1005          * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
1006          *
1007          * The pseudo code indicate
1008          * revealsecrets = true
1009          * if IsRevealSecretRequest(msgIn) then
1010          *   if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
1011          *   then
1012          *     if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
1013          *     <... check if this account is ok to be replicated on this DC ...>
1014          *     <... and if not reveal secrets = no ...>
1015          *     else
1016          *       reveal secrets = false
1017          *     endif
1018          *   endif
1019          * endif
1020          *
1021          * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
1022          * then you can do EXOP_REPL_SECRETS
1023          */
1024         if (has_get_all_changes) {
1025                 goto allowed;
1026         }
1027
1028         obj_dn = drs_ObjectIdentifier_to_dn(mem_ctx, b_state->sam_ctx_system, ncRoot);
1029         if (!ldb_dn_validate(obj_dn)) goto failed;
1030
1031         rodc_dn = ldb_dn_new_fmt(mem_ctx, b_state->sam_ctx_system, "<SID=%s>",
1032                                  dom_sid_string(mem_ctx, user_sid));
1033         if (!ldb_dn_validate(rodc_dn)) goto failed;
1034
1035         /* do the two searches we need */
1036         ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &rodc_res, rodc_dn, rodc_attrs,
1037                              DSDB_SEARCH_SHOW_EXTENDED_DN);
1038         if (ret != LDB_SUCCESS || rodc_res->count != 1) goto failed;
1039
1040         ret = dsdb_search_dn(b_state->sam_ctx_system, mem_ctx, &obj_res, obj_dn, obj_attrs, 0);
1041         if (ret != LDB_SUCCESS || obj_res->count != 1) goto failed;
1042
1043         /* if the object SID is equal to the user_sid, allow */
1044         if (dom_sid_equal(user_sid,
1045                           samdb_result_dom_sid(mem_ctx, obj_res->msgs[0], "objectSid"))) {
1046                 goto allowed;
1047         }
1048
1049         /* an RODC is allowed to get its own krbtgt account secrets */
1050         krbtgt_link_dn = samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1051                                          rodc_res->msgs[0], "msDS-KrbTgtLink", NULL);
1052         if (krbtgt_link_dn != NULL &&
1053             ldb_dn_compare(obj_dn, krbtgt_link_dn) == 0) {
1054                 goto allowed;
1055         }
1056
1057         /* but it isn't allowed to get anyone elses krbtgt secrets */
1058         if (samdb_result_dn(b_state->sam_ctx_system, mem_ctx,
1059                             obj_res->msgs[0], "msDS-KrbTgtLinkBL", NULL)) {
1060                 goto denied;
1061         }
1062
1063         if (ldb_msg_find_attr_as_uint(obj_res->msgs[0],
1064                                       "userAccountControl", 0) &
1065             UF_INTERDOMAIN_TRUST_ACCOUNT) {
1066                 goto denied;
1067         }
1068
1069         werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1070                                          mem_ctx, "msDS-NeverRevealGroup", &never_reveal_sids);
1071         if (!W_ERROR_IS_OK(werr)) {
1072                 goto denied;
1073         }
1074
1075         werr = samdb_result_sid_array_dn(b_state->sam_ctx_system, rodc_res->msgs[0],
1076                                          mem_ctx, "msDS-RevealOnDemandGroup", &reveal_sids);
1077         if (!W_ERROR_IS_OK(werr)) {
1078                 goto denied;
1079         }
1080
1081         werr = samdb_result_sid_array_ndr(b_state->sam_ctx_system, obj_res->msgs[0],
1082                                          mem_ctx, "tokenGroups", &token_sids);
1083         if (!W_ERROR_IS_OK(werr) || token_sids==NULL) {
1084                 goto denied;
1085         }
1086
1087         if (never_reveal_sids &&
1088             sid_list_match(token_sids, never_reveal_sids)) {
1089                 goto denied;
1090         }
1091
1092         if (reveal_sids &&
1093             sid_list_match(token_sids, reveal_sids)) {
1094                 goto allowed;
1095         }
1096
1097         /* default deny */
1098 denied:
1099         DEBUG(2,(__location__ ": Denied single object with secret replication for %s by RODC %s\n",
1100                  ldb_dn_get_linearized(obj_dn), ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1101         ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1102         return WERR_DS_DRA_ACCESS_DENIED;
1103
1104 allowed:
1105         DEBUG(2,(__location__ ": Allowed single object with secret replication for %s by %s %s\n",
1106                  ldb_dn_get_linearized(obj_dn), has_get_all_changes?"RWDC":"RODC",
1107                  ldb_dn_get_linearized(rodc_res->msgs[0]->dn)));
1108         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1109         req10->highwatermark.highest_usn = 0;
1110         return WERR_OK;
1111
1112 failed:
1113         DEBUG(2,(__location__ ": Failed single secret replication for %s by RODC %s\n",
1114                  ldb_dn_get_linearized(obj_dn), dom_sid_string(mem_ctx, user_sid)));
1115         ctr6->extended_ret = DRSUAPI_EXOP_ERR_NONE;
1116         return WERR_DS_DRA_BAD_DN;
1117 }
1118
1119
1120 /*
1121   handle a DRSUAPI_EXOP_REPL_OBJ call
1122  */
1123 static WERROR getncchanges_repl_obj(struct drsuapi_bind_state *b_state,
1124                                     TALLOC_CTX *mem_ctx,
1125                                     struct drsuapi_DsGetNCChangesRequest10 *req10,
1126                                     struct dom_sid *user_sid,
1127                                     struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1128 {
1129         struct drsuapi_DsReplicaObjectIdentifier *ncRoot = req10->naming_context;
1130
1131         DEBUG(3,(__location__ ": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1132                  drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
1133
1134         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1135         return WERR_OK;
1136 }
1137
1138
1139 /*
1140   handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1141   DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1142   and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1143  */
1144 static WERROR getncchanges_change_master(struct drsuapi_bind_state *b_state,
1145                                          TALLOC_CTX *mem_ctx,
1146                                          struct drsuapi_DsGetNCChangesRequest10 *req10,
1147                                          struct drsuapi_DsGetNCChangesCtr6 *ctr6)
1148 {
1149         struct ldb_dn *req_dn, *ntds_dn;
1150         int ret;
1151         unsigned int i;
1152         struct ldb_context *ldb = b_state->sam_ctx;
1153         struct ldb_message *msg;
1154         bool is_us;
1155
1156         /*
1157           steps:
1158             - verify that the client dn exists
1159             - verify that we are the current master
1160          */
1161
1162         req_dn = drs_ObjectIdentifier_to_dn(mem_ctx, ldb, req10->naming_context);
1163         if (!ldb_dn_validate(req_dn)) {
1164                 /* that is not a valid dn */
1165                 DEBUG(0,(__location__ ": FSMO role transfer request for invalid DN %s\n",
1166                          drs_ObjectIdentifier_to_string(mem_ctx, req10->naming_context)));
1167                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_MISMATCH;
1168                 return WERR_OK;
1169         }
1170
1171         /* find the DN of the current role owner */
1172         ret = samdb_reference_dn_is_our_ntdsa(ldb, req_dn, "fSMORoleOwner", &is_us);
1173         if (ret != LDB_SUCCESS) {
1174                 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1175                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1176                 return WERR_DS_DRA_INTERNAL_ERROR;
1177         }
1178
1179         if (!is_us) {
1180                 /* we're not the RID Manager or role owner - go away */
1181                 DEBUG(0,(__location__ ": FSMO role or RID manager transfer owner request when not role owner\n"));
1182                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER;
1183                 return WERR_OK;
1184         }
1185
1186         /* change the current master */
1187         msg = ldb_msg_new(ldb);
1188         W_ERROR_HAVE_NO_MEMORY(msg);
1189         msg->dn = drs_ObjectIdentifier_to_dn(msg, ldb, req10->naming_context);
1190         W_ERROR_HAVE_NO_MEMORY(msg->dn);
1191
1192         /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1193         ret = dsdb_find_dn_by_guid(ldb, msg, &req10->destination_dsa_guid, 0, &ntds_dn);
1194         if (ret != LDB_SUCCESS) {
1195                 DEBUG(0, (__location__ ": Unable to find NTDS object for guid %s - %s\n",
1196                           GUID_string(mem_ctx, &req10->destination_dsa_guid), ldb_errstring(ldb)));
1197                 talloc_free(msg);
1198                 ctr6->extended_ret = DRSUAPI_EXOP_ERR_UNKNOWN_CALLER;
1199                 return WERR_OK;
1200         }
1201
1202         ret = ldb_msg_add_string(msg, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn));
1203         if (ret != 0) {
1204                 talloc_free(msg);
1205                 return WERR_DS_DRA_INTERNAL_ERROR;
1206         }
1207
1208         for (i=0;i<msg->num_elements;i++) {
1209                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1210         }
1211
1212         ret = ldb_transaction_start(ldb);
1213         if (ret != LDB_SUCCESS) {
1214                 DEBUG(0,(__location__ ": Failed transaction start - %s\n",
1215                          ldb_errstring(ldb)));
1216                 return WERR_DS_DRA_INTERNAL_ERROR;
1217         }
1218
1219         ret = ldb_modify(ldb, msg);
1220         if (ret != LDB_SUCCESS) {
1221                 DEBUG(0,(__location__ ": Failed to change current owner - %s\n",
1222                          ldb_errstring(ldb)));
1223                 ldb_transaction_cancel(ldb);
1224                 return WERR_DS_DRA_INTERNAL_ERROR;
1225         }
1226
1227         ret = ldb_transaction_commit(ldb);
1228         if (ret != LDB_SUCCESS) {
1229                 DEBUG(0,(__location__ ": Failed transaction commit - %s\n",
1230                          ldb_errstring(ldb)));
1231                 return WERR_DS_DRA_INTERNAL_ERROR;
1232         }
1233
1234         ctr6->extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1235
1236         return WERR_OK;
1237 }
1238
1239 /*
1240   see if this getncchanges request includes a request to reveal secret information
1241  */
1242 static WERROR dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state *b_state,
1243                                                        struct drsuapi_DsGetNCChangesRequest10 *req10,
1244                                                        struct dsdb_schema_prefixmap *pfm_remote,
1245                                                        bool *is_secret_request)
1246 {
1247         enum drsuapi_DsExtendedOperation exop;
1248         uint32_t i;
1249         struct dsdb_schema *schema;
1250         struct dsdb_syntax_ctx syntax_ctx;
1251
1252         *is_secret_request = true;
1253
1254         exop = req10->extended_op;
1255
1256         switch (exop) {
1257         case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1258         case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1259         case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1260         case DRSUAPI_EXOP_FSMO_REQ_PDC:
1261         case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1262                 /* FSMO exops can reveal secrets */
1263                 *is_secret_request = true;
1264                 return WERR_OK;
1265         case DRSUAPI_EXOP_REPL_SECRET:
1266         case DRSUAPI_EXOP_REPL_OBJ:
1267         case DRSUAPI_EXOP_NONE:
1268                 break;
1269         }
1270
1271         if (req10->replica_flags & DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING) {
1272                 *is_secret_request = false;
1273                 return WERR_OK;
1274         }
1275
1276         if (exop == DRSUAPI_EXOP_REPL_SECRET ||
1277             req10->partial_attribute_set == NULL) {
1278                 /* they want secrets */
1279                 *is_secret_request = true;
1280                 return WERR_OK;
1281         }
1282
1283         schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1284         dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1285         syntax_ctx.pfm_remote = pfm_remote;
1286
1287         /* check the attributes they asked for */
1288         for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1289                 const struct dsdb_attribute *sa;
1290                 WERROR werr = getncchanges_attid_remote_to_local(schema,
1291                                                                  &syntax_ctx,
1292                                                                  req10->partial_attribute_set->attids[i],
1293                                                                  NULL,
1294                                                                  &sa);
1295
1296                 if (!W_ERROR_IS_OK(werr)) {
1297                         DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1298                                  req10->partial_attribute_set->attids[i], win_errstr(werr)));
1299                         return werr;
1300                 }
1301
1302                 if (!dsdb_attr_in_rodc_fas(sa)) {
1303                         *is_secret_request = true;
1304                         return WERR_OK;
1305                 }
1306         }
1307
1308         if (req10->partial_attribute_set_ex) {
1309                 /* check the extended attributes they asked for */
1310                 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1311                         const struct dsdb_attribute *sa;
1312                         WERROR werr = getncchanges_attid_remote_to_local(schema,
1313                                                                          &syntax_ctx,
1314                                                                          req10->partial_attribute_set_ex->attids[i],
1315                                                                          NULL,
1316                                                                          &sa);
1317
1318                         if (!W_ERROR_IS_OK(werr)) {
1319                                 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1320                                          req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1321                                 return werr;
1322                         }
1323
1324                         if (!dsdb_attr_in_rodc_fas(sa)) {
1325                                 *is_secret_request = true;
1326                                 return WERR_OK;
1327                         }
1328                 }
1329         }
1330
1331         *is_secret_request = false;
1332         return WERR_OK;
1333 }
1334
1335 /*
1336   see if this getncchanges request is only for attributes in the GC
1337   partial attribute set
1338  */
1339 static WERROR dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state *b_state,
1340                                                struct drsuapi_DsGetNCChangesRequest10 *req10,
1341                                                struct dsdb_schema_prefixmap *pfm_remote,
1342                                                bool *is_gc_pas_request)
1343 {
1344         enum drsuapi_DsExtendedOperation exop;
1345         uint32_t i;
1346         struct dsdb_schema *schema;
1347         struct dsdb_syntax_ctx syntax_ctx;
1348
1349         exop = req10->extended_op;
1350
1351         switch (exop) {
1352         case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1353         case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1354         case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1355         case DRSUAPI_EXOP_FSMO_REQ_PDC:
1356         case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1357         case DRSUAPI_EXOP_REPL_SECRET:
1358                 *is_gc_pas_request = false;
1359                 return WERR_OK;
1360         case DRSUAPI_EXOP_REPL_OBJ:
1361         case DRSUAPI_EXOP_NONE:
1362                 break;
1363         }
1364
1365         if (req10->partial_attribute_set == NULL) {
1366                 /* they want it all */
1367                 *is_gc_pas_request = false;
1368                 return WERR_OK;
1369         }
1370
1371         schema = dsdb_get_schema(b_state->sam_ctx, NULL);
1372         dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
1373         syntax_ctx.pfm_remote = pfm_remote;
1374
1375         /* check the attributes they asked for */
1376         for (i=0; i<req10->partial_attribute_set->num_attids; i++) {
1377                 const struct dsdb_attribute *sa;
1378                 WERROR werr = getncchanges_attid_remote_to_local(schema,
1379                                                                  &syntax_ctx,
1380                                                                  req10->partial_attribute_set->attids[i],
1381                                                                  NULL,
1382                                                                  &sa);
1383
1384                 if (!W_ERROR_IS_OK(werr)) {
1385                         DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1386                                  req10->partial_attribute_set->attids[i], win_errstr(werr)));
1387                         return werr;
1388                 }
1389
1390                 if (!sa->isMemberOfPartialAttributeSet) {
1391                         *is_gc_pas_request = false;
1392                         return WERR_OK;
1393                 }
1394         }
1395
1396         if (req10->partial_attribute_set_ex) {
1397                 /* check the extended attributes they asked for */
1398                 for (i=0; i<req10->partial_attribute_set_ex->num_attids; i++) {
1399                         const struct dsdb_attribute *sa;
1400                         WERROR werr = getncchanges_attid_remote_to_local(schema,
1401                                                                          &syntax_ctx,
1402                                                                          req10->partial_attribute_set_ex->attids[i],
1403                                                                          NULL,
1404                                                                          &sa);
1405
1406                         if (!W_ERROR_IS_OK(werr)) {
1407                                 DEBUG(0,(__location__": attid 0x%08X not found: %s\n",
1408                                          req10->partial_attribute_set_ex->attids[i], win_errstr(werr)));
1409                                 return werr;
1410                         }
1411
1412                         if (!sa->isMemberOfPartialAttributeSet) {
1413                                 *is_gc_pas_request = false;
1414                                 return WERR_OK;
1415                         }
1416                 }
1417         }
1418
1419         *is_gc_pas_request = true;
1420         return WERR_OK;
1421 }
1422
1423
1424 /*
1425   map from req8 to req10
1426  */
1427 static struct drsuapi_DsGetNCChangesRequest10 *
1428 getncchanges_map_req8(TALLOC_CTX *mem_ctx,
1429                       struct drsuapi_DsGetNCChangesRequest8 *req8)
1430 {
1431         struct drsuapi_DsGetNCChangesRequest10 *req10 = talloc_zero(mem_ctx,
1432                                                                     struct drsuapi_DsGetNCChangesRequest10);
1433         if (req10 == NULL) {
1434                 return NULL;
1435         }
1436
1437         req10->destination_dsa_guid = req8->destination_dsa_guid;
1438         req10->source_dsa_invocation_id = req8->source_dsa_invocation_id;
1439         req10->naming_context = req8->naming_context;
1440         req10->highwatermark = req8->highwatermark;
1441         req10->uptodateness_vector = req8->uptodateness_vector;
1442         req10->replica_flags = req8->replica_flags;
1443         req10->max_object_count = req8->max_object_count;
1444         req10->max_ndr_size = req8->max_ndr_size;
1445         req10->extended_op = req8->extended_op;
1446         req10->fsmo_info = req8->fsmo_info;
1447         req10->partial_attribute_set = req8->partial_attribute_set;
1448         req10->partial_attribute_set_ex = req8->partial_attribute_set_ex;
1449         req10->mapping_ctr = req8->mapping_ctr;
1450
1451         return req10;
1452 }
1453
1454 static const char *collect_objects_attrs[] = { "uSNChanged",
1455                                                "objectGUID" ,
1456                                                NULL };
1457
1458 /**
1459  * Collects object for normal replication cycle.
1460  */
1461 static WERROR getncchanges_collect_objects(struct drsuapi_bind_state *b_state,
1462                                            TALLOC_CTX *mem_ctx,
1463                                            struct drsuapi_DsGetNCChangesRequest10 *req10,
1464                                            struct ldb_dn *search_dn,
1465                                            const char *extra_filter,
1466                                            struct ldb_result **search_res)
1467 {
1468         int ret;
1469         char* search_filter;
1470         enum ldb_scope scope = LDB_SCOPE_SUBTREE;
1471         //const char *extra_filter;
1472         struct drsuapi_getncchanges_state *getnc_state = b_state->getncchanges_state;
1473
1474         if (req10->extended_op == DRSUAPI_EXOP_REPL_OBJ ||
1475             req10->extended_op == DRSUAPI_EXOP_REPL_SECRET) {
1476                 scope = LDB_SCOPE_BASE;
1477         }
1478
1479         //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1480
1481         //getnc_state->min_usn = req10->highwatermark.highest_usn;
1482
1483         /* Construct response. */
1484         search_filter = talloc_asprintf(mem_ctx,
1485                                         "(uSNChanged>=%llu)",
1486                                         (unsigned long long)(getnc_state->min_usn+1));
1487
1488         if (extra_filter) {
1489                 search_filter = talloc_asprintf(mem_ctx, "(&%s(%s))", search_filter, extra_filter);
1490         }
1491
1492         if (req10->replica_flags & DRSUAPI_DRS_CRITICAL_ONLY) {
1493                 search_filter = talloc_asprintf(mem_ctx,
1494                                                 "(&%s(isCriticalSystemObject=TRUE))",
1495                                                 search_filter);
1496         }
1497
1498         if (req10->replica_flags & DRSUAPI_DRS_ASYNC_REP) {
1499                 scope = LDB_SCOPE_BASE;
1500         }
1501
1502         if (!search_dn) {
1503                 search_dn = getnc_state->ncRoot_dn;
1504         }
1505
1506         DEBUG(2,(__location__ ": getncchanges on %s using filter %s\n",
1507                  ldb_dn_get_linearized(getnc_state->ncRoot_dn), search_filter));
1508         ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, getnc_state, search_res,
1509                                               search_dn, scope,
1510                                               collect_objects_attrs,
1511                                               search_filter);
1512         if (ret != LDB_SUCCESS) {
1513                 return WERR_DS_DRA_INTERNAL_ERROR;
1514         }
1515
1516         return WERR_OK;
1517 }
1518
1519 /**
1520  * Collects object for normal replication cycle.
1521  */
1522 static WERROR getncchanges_collect_objects_exop(struct drsuapi_bind_state *b_state,
1523                                                 TALLOC_CTX *mem_ctx,
1524                                                 struct drsuapi_DsGetNCChangesRequest10 *req10,
1525                                                 struct drsuapi_DsGetNCChangesCtr6 *ctr6,
1526                                                 struct ldb_dn *search_dn,
1527                                                 const char *extra_filter,
1528                                                 struct ldb_result **search_res)
1529 {
1530         /* we have nothing to do in case of ex-op failure */
1531         if (ctr6->extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1532                 return WERR_OK;
1533         }
1534
1535         switch (req10->extended_op) {
1536         case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1537         {
1538                 int ret;
1539                 struct ldb_dn *ntds_dn = NULL;
1540                 struct ldb_dn *server_dn = NULL;
1541                 struct ldb_dn *machine_dn = NULL;
1542                 struct ldb_dn *rid_set_dn = NULL;
1543                 struct ldb_result *search_res2 = NULL;
1544                 struct ldb_result *search_res3 = NULL;
1545                 TALLOC_CTX *frame = talloc_stackframe();
1546                 /* get RID manager, RID set and server DN (in that order) */
1547
1548                 /* This first search will get the RID Manager */
1549                 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1550                                                       search_res,
1551                                                       search_dn, LDB_SCOPE_BASE,
1552                                                       collect_objects_attrs,
1553                                                       NULL);
1554                 if (ret != LDB_SUCCESS) {
1555                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %s",
1556                                   ldb_dn_get_linearized(search_dn),
1557                                   ldb_errstring(b_state->sam_ctx)));
1558                         TALLOC_FREE(frame);
1559                         return WERR_DS_DRA_INTERNAL_ERROR;
1560                 }
1561
1562                 if ((*search_res)->count != 1) {
1563                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Manager object %s - %u objects returned",
1564                                   ldb_dn_get_linearized(search_dn),
1565                                   (*search_res)->count));
1566                         TALLOC_FREE(frame);
1567                         return WERR_DS_DRA_INTERNAL_ERROR;
1568                 }
1569
1570                 /* Now extend it to the RID set */
1571
1572                 /* Find the computer account DN for the destination
1573                  * dsa GUID specified */
1574
1575                 ret = dsdb_find_dn_by_guid(b_state->sam_ctx, frame,
1576                                            &req10->destination_dsa_guid, 0,
1577                                            &ntds_dn);
1578                 if (ret != LDB_SUCCESS) {
1579                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Unable to find NTDS object for guid %s - %s\n",
1580                                   GUID_string(frame,
1581                                               &req10->destination_dsa_guid),
1582                                   ldb_errstring(b_state->sam_ctx)));
1583                         TALLOC_FREE(frame);
1584                         return WERR_DS_DRA_INTERNAL_ERROR;
1585                 }
1586
1587                 server_dn = ldb_dn_get_parent(frame, ntds_dn);
1588                 if (!server_dn) {
1589                         TALLOC_FREE(frame);
1590                         return WERR_DS_DRA_INTERNAL_ERROR;
1591                 }
1592
1593                 ret = samdb_reference_dn(b_state->sam_ctx, frame, server_dn,
1594                                          "serverReference", &machine_dn);
1595                 if (ret != LDB_SUCCESS) {
1596                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find serverReference in %s - %s",
1597                                   ldb_dn_get_linearized(server_dn),
1598                                   ldb_errstring(b_state->sam_ctx)));
1599                         TALLOC_FREE(frame);
1600                         return WERR_DS_DRA_INTERNAL_ERROR;
1601                 }
1602
1603                 ret = samdb_reference_dn(b_state->sam_ctx, frame, machine_dn,
1604                                          "rIDSetReferences", &rid_set_dn);
1605                 if (ret != LDB_SUCCESS) {
1606                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to find rIDSetReferences in %s - %s",
1607                                   ldb_dn_get_linearized(server_dn),
1608                                   ldb_errstring(b_state->sam_ctx)));
1609                         TALLOC_FREE(frame);
1610                         return WERR_DS_DRA_INTERNAL_ERROR;
1611                 }
1612
1613
1614                 /* This first search will get the RID Manager, now get the RID set */
1615                 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1616                                                       &search_res2,
1617                                                       rid_set_dn, LDB_SCOPE_BASE,
1618                                                       collect_objects_attrs,
1619                                                       NULL);
1620                 if (ret != LDB_SUCCESS) {
1621                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %s",
1622                                   ldb_dn_get_linearized(rid_set_dn),
1623                                   ldb_errstring(b_state->sam_ctx)));
1624                         TALLOC_FREE(frame);
1625                         return WERR_DS_DRA_INTERNAL_ERROR;
1626                 }
1627
1628                 if (search_res2->count != 1) {
1629                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get RID Set object %s - %u objects returned",
1630                                   ldb_dn_get_linearized(rid_set_dn),
1631                                   search_res2->count));
1632                         TALLOC_FREE(frame);
1633                         return WERR_DS_DRA_INTERNAL_ERROR;
1634                 }
1635
1636                 /* Finally get the server DN */
1637                 ret = drsuapi_search_with_extended_dn(b_state->sam_ctx, frame,
1638                                                       &search_res3,
1639                                                       machine_dn, LDB_SCOPE_BASE,
1640                                                       collect_objects_attrs,
1641                                                       NULL);
1642                 if (ret != LDB_SUCCESS) {
1643                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %s",
1644                                   ldb_dn_get_linearized(server_dn),
1645                                   ldb_errstring(b_state->sam_ctx)));
1646                         TALLOC_FREE(frame);
1647                         return WERR_DS_DRA_INTERNAL_ERROR;
1648                 }
1649
1650                 if (search_res3->count != 1) {
1651                         DEBUG(1, ("DRSUAPI_EXOP_FSMO_RID_ALLOC: Failed to get server object %s - %u objects returned",
1652                                   ldb_dn_get_linearized(server_dn),
1653                                   search_res3->count));
1654                         TALLOC_FREE(frame);
1655                         return WERR_DS_DRA_INTERNAL_ERROR;
1656                 }
1657
1658                 /* Now extend the original search_res with these answers */
1659                 (*search_res)->count = 3;
1660
1661                 (*search_res)->msgs = talloc_realloc(frame, (*search_res)->msgs,
1662                                                      struct ldb_message *,
1663                                                      (*search_res)->count);
1664                 if ((*search_res)->msgs == NULL) {
1665                         TALLOC_FREE(frame);
1666                         return WERR_NOT_ENOUGH_MEMORY;
1667                 }
1668
1669
1670                 talloc_steal(mem_ctx, *search_res);
1671                 (*search_res)->msgs[1] =
1672                         talloc_steal((*search_res)->msgs, search_res2->msgs[0]);
1673                 (*search_res)->msgs[2] =
1674                         talloc_steal((*search_res)->msgs, search_res3->msgs[0]);
1675
1676                 TALLOC_FREE(frame);
1677                 return WERR_OK;
1678         }
1679         default:
1680                 /* TODO: implement extended op specific collection
1681                  * of objects. Right now we just normal procedure
1682                  * for collecting objects */
1683                 return getncchanges_collect_objects(b_state, mem_ctx, req10, search_dn, extra_filter, search_res);
1684         }
1685 }
1686
1687 /* 
1688   drsuapi_DsGetNCChanges
1689
1690   see MS-DRSR 4.1.10.5.2 for basic logic of this function
1691 */
1692 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1693                                      struct drsuapi_DsGetNCChanges *r)
1694 {
1695         struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
1696         int ret;
1697         uint32_t i, k;
1698         struct dsdb_schema *schema;
1699         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
1700         struct drsuapi_DsReplicaObjectListItemEx **currentObject;
1701         NTSTATUS status;
1702         DATA_BLOB session_key;
1703         WERROR werr;
1704         struct dcesrv_handle *h;
1705         struct drsuapi_bind_state *b_state;
1706         struct drsuapi_getncchanges_state *getnc_state;
1707         struct drsuapi_DsGetNCChangesRequest10 *req10;
1708         uint32_t options;
1709         uint32_t max_objects;
1710         uint32_t max_links;
1711         uint32_t link_count = 0;
1712         uint32_t link_total = 0;
1713         uint32_t link_given = 0;
1714         struct ldb_dn *search_dn = NULL;
1715         bool am_rodc, null_scope=false;
1716         enum security_user_level security_level;
1717         struct ldb_context *sam_ctx;
1718         struct dom_sid *user_sid;
1719         bool is_secret_request;
1720         bool is_gc_pas_request;
1721         struct drsuapi_changed_objects *changes;
1722         time_t max_wait;
1723         time_t start = time(NULL);
1724         bool max_wait_reached = false;
1725         bool has_get_all_changes = false;
1726         struct GUID invocation_id;
1727         static const struct drsuapi_DsReplicaLinkedAttribute no_linked_attr;
1728         struct dsdb_schema_prefixmap *pfm_remote = NULL;
1729         bool full = true;
1730         uint32_t *local_pas = NULL;
1731
1732         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
1733         b_state = h->data;
1734
1735         sam_ctx = b_state->sam_ctx_system?b_state->sam_ctx_system:b_state->sam_ctx;
1736
1737         invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1738
1739         *r->out.level_out = 6;
1740         /* TODO: linked attributes*/
1741         r->out.ctr->ctr6.linked_attributes_count = 0;
1742         r->out.ctr->ctr6.linked_attributes = discard_const_p(struct drsuapi_DsReplicaLinkedAttribute, &no_linked_attr);
1743
1744         r->out.ctr->ctr6.object_count = 0;
1745         r->out.ctr->ctr6.nc_object_count = 0;
1746         r->out.ctr->ctr6.more_data = false;
1747         r->out.ctr->ctr6.uptodateness_vector = NULL;
1748         r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
1749         r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
1750         r->out.ctr->ctr6.first_object = NULL;
1751
1752         /* a RODC doesn't allow for any replication */
1753         ret = samdb_rodc(sam_ctx, &am_rodc);
1754         if (ret == LDB_SUCCESS && am_rodc) {
1755                 DEBUG(0,(__location__ ": DsGetNCChanges attempt on RODC\n"));
1756                 return WERR_DS_DRA_SOURCE_DISABLED;
1757         }
1758
1759         /* Check request revision. 
1760          */
1761         switch (r->in.level) {
1762         case 8:
1763                 req10 = getncchanges_map_req8(mem_ctx, &r->in.req->req8);
1764                 if (req10 == NULL) {
1765                         return WERR_NOT_ENOUGH_MEMORY;
1766                 }
1767                 break;
1768         case 10:
1769                 req10 = &r->in.req->req10;
1770                 break;
1771         default:
1772                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with unsupported level %u\n",
1773                          r->in.level));
1774                 return WERR_REVISION_MISMATCH;
1775         }
1776
1777
1778         /* Perform access checks. */
1779         /* TODO: we need to support a sync on a specific non-root
1780          * DN. We'll need to find the real partition root here */
1781         ncRoot = req10->naming_context;
1782         if (ncRoot == NULL) {
1783                 DEBUG(0,(__location__ ": Request for DsGetNCChanges with no NC\n"));
1784                 return WERR_DS_DRA_INVALID_PARAMETER;
1785         }
1786
1787         if (samdb_ntds_options(sam_ctx, &options) != LDB_SUCCESS) {
1788                 return WERR_DS_DRA_INTERNAL_ERROR;
1789         }
1790
1791         if ((options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) &&
1792             !(req10->replica_flags & DRSUAPI_DRS_SYNC_FORCED)) {
1793                 return WERR_DS_DRA_SOURCE_DISABLED;
1794         }
1795
1796         user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
1797
1798         /* all clients must have GUID_DRS_GET_CHANGES */
1799         werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1800                                                  mem_ctx,
1801                                                  dce_call->conn->auth_state.session_info->security_token,
1802                                                  req10->naming_context,
1803                                                  GUID_DRS_GET_CHANGES);
1804         if (!W_ERROR_IS_OK(werr)) {
1805                 return werr;
1806         }
1807
1808         if (dsdb_functional_level(sam_ctx) >= DS_DOMAIN_FUNCTION_2008) {
1809                 full = req10->partial_attribute_set == NULL &&
1810                        req10->partial_attribute_set_ex == NULL;
1811         } else {
1812                 full = (options & DRSUAPI_DRS_WRIT_REP) != 0;
1813         }
1814
1815         werr = dsdb_schema_pfm_from_drsuapi_pfm(&req10->mapping_ctr, true,
1816                                                 mem_ctx, &pfm_remote, NULL);
1817
1818         /* We were supplied a partial attribute set, without the prefix map! */
1819         if (!full && !W_ERROR_IS_OK(werr)) {
1820                 if (req10->mapping_ctr.num_mappings == 0) {
1821                         /*
1822                          * Despite the fact MS-DRSR specifies that this shouldn't
1823                          * happen, Windows RODCs will in fact not provide a prefixMap.
1824                          */
1825                         DEBUG(5,(__location__ ": Failed to provide a remote prefixMap,"
1826                                  " falling back to local prefixMap\n"));
1827                 } else {
1828                         DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
1829                                  win_errstr(werr)));
1830                         return werr;
1831                 }
1832         }
1833
1834         /* allowed if the GC PAS and client has
1835            GUID_DRS_GET_FILTERED_ATTRIBUTES */
1836         werr = dcesrv_drsuapi_is_gc_pas_request(b_state, req10, pfm_remote, &is_gc_pas_request);
1837         if (!W_ERROR_IS_OK(werr)) {
1838                 return werr;
1839         }
1840         if (is_gc_pas_request) {
1841                 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1842                                                          mem_ctx,
1843                                                          dce_call->conn->auth_state.session_info->security_token,
1844                                                          req10->naming_context,
1845                                                          GUID_DRS_GET_FILTERED_ATTRIBUTES);
1846                 if (W_ERROR_IS_OK(werr)) {
1847                         goto allowed;
1848                 }
1849         }
1850
1851         werr = dcesrv_drsuapi_is_reveal_secrets_request(b_state, req10,
1852                                                         pfm_remote,
1853                                                         &is_secret_request);
1854         if (!W_ERROR_IS_OK(werr)) {
1855                 return werr;
1856         }
1857         if (is_secret_request && req10->extended_op != DRSUAPI_EXOP_REPL_SECRET) {
1858                 werr = drs_security_access_check_nc_root(b_state->sam_ctx,
1859                                                          mem_ctx,
1860                                                          dce_call->conn->auth_state.session_info->security_token,
1861                                                          req10->naming_context,
1862                                                          GUID_DRS_GET_ALL_CHANGES);
1863                 if (!W_ERROR_IS_OK(werr)) {
1864                         return werr;
1865                 } else {
1866                         has_get_all_changes = true;
1867                 }
1868         }
1869
1870 allowed:
1871         /* for non-administrator replications, check that they have
1872            given the correct source_dsa_invocation_id */
1873         security_level = security_session_user_level(dce_call->conn->auth_state.session_info,
1874                                                      samdb_domain_sid(sam_ctx));
1875         if (security_level == SECURITY_RO_DOMAIN_CONTROLLER) {
1876                 if (req10->replica_flags & DRSUAPI_DRS_WRIT_REP) {
1877                         /* we rely on this flag being unset for RODC requests */
1878                         req10->replica_flags &= ~DRSUAPI_DRS_WRIT_REP;
1879                 }
1880         }
1881
1882         if (req10->replica_flags & DRSUAPI_DRS_FULL_SYNC_PACKET) {
1883                 /* Ignore the _in_ uptpdateness vector*/
1884                 req10->uptodateness_vector = NULL;
1885         }
1886
1887         if (GUID_all_zero(&req10->source_dsa_invocation_id)) {
1888                 req10->source_dsa_invocation_id = invocation_id;
1889         }
1890
1891         if (!GUID_equal(&req10->source_dsa_invocation_id, &invocation_id)) {
1892                 /*
1893                  * The given highwatermark is only valid relative to the
1894                  * specified source_dsa_invocation_id.
1895                  */
1896                 ZERO_STRUCT(req10->highwatermark);
1897         }
1898
1899         getnc_state = b_state->getncchanges_state;
1900
1901         /* see if a previous replication has been abandoned */
1902         if (getnc_state) {
1903                 struct ldb_dn *new_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1904                 if (ldb_dn_compare(new_dn, getnc_state->ncRoot_dn) != 0) {
1905                         DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1906                                  ldb_dn_get_linearized(new_dn),
1907                                  ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1908                                  ldb_dn_get_linearized(getnc_state->last_dn)));
1909                         talloc_free(getnc_state);
1910                         getnc_state = NULL;
1911                 }
1912         }
1913
1914         if (getnc_state) {
1915                 ret = drsuapi_DsReplicaHighWaterMark_cmp(&getnc_state->last_hwm,
1916                                                          &req10->highwatermark);
1917                 if (ret != 0) {
1918                         DEBUG(0,(__location__ ": DsGetNCChanges 2nd replication "
1919                                  "on DN %s %s highwatermark (last_dn %s)\n",
1920                                  ldb_dn_get_linearized(getnc_state->ncRoot_dn),
1921                                  (ret > 0) ? "older" : "newer",
1922                                  ldb_dn_get_linearized(getnc_state->last_dn)));
1923                         talloc_free(getnc_state);
1924                         getnc_state = NULL;
1925                 }
1926         }
1927
1928         if (getnc_state == NULL) {
1929                 getnc_state = talloc_zero(b_state, struct drsuapi_getncchanges_state);
1930                 if (getnc_state == NULL) {
1931                         return WERR_NOT_ENOUGH_MEMORY;
1932                 }
1933                 b_state->getncchanges_state = getnc_state;
1934                 getnc_state->ncRoot_dn = drs_ObjectIdentifier_to_dn(getnc_state, sam_ctx, ncRoot);
1935
1936                 /* find out if we are to replicate Schema NC */
1937                 ret = ldb_dn_compare_base(ldb_get_schema_basedn(b_state->sam_ctx),
1938                                           getnc_state->ncRoot_dn);
1939
1940                 getnc_state->is_schema_nc = (0 == ret);
1941
1942                 if (req10->extended_op != DRSUAPI_EXOP_NONE) {
1943                         r->out.ctr->ctr6.extended_ret = DRSUAPI_EXOP_ERR_SUCCESS;
1944                 }
1945
1946                 /*
1947                  * This is the first replication cycle and it is
1948                  * a good place to handle extended operations
1949                  *
1950                  * FIXME: we don't fully support extended operations yet
1951                  */
1952                 switch (req10->extended_op) {
1953                 case DRSUAPI_EXOP_NONE:
1954                         break;
1955                 case DRSUAPI_EXOP_FSMO_RID_ALLOC:
1956                         werr = getncchanges_rid_alloc(b_state, mem_ctx, req10, &r->out.ctr->ctr6, &search_dn);
1957                         W_ERROR_NOT_OK_RETURN(werr);
1958                         if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1959                                 return WERR_OK;
1960                         }
1961                         break;
1962                 case DRSUAPI_EXOP_REPL_SECRET:
1963                         werr = getncchanges_repl_secret(b_state, mem_ctx, req10,
1964                                                         user_sid,
1965                                                         &r->out.ctr->ctr6,
1966                                                         has_get_all_changes);
1967                         r->out.result = werr;
1968                         W_ERROR_NOT_OK_RETURN(werr);
1969                         break;
1970                 case DRSUAPI_EXOP_FSMO_REQ_ROLE:
1971                         werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1972                         W_ERROR_NOT_OK_RETURN(werr);
1973                         if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1974                                 return WERR_OK;
1975                         }
1976                         break;
1977                 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE:
1978                         werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1979                         W_ERROR_NOT_OK_RETURN(werr);
1980                         if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1981                                 return WERR_OK;
1982                         }
1983                         break;
1984                 case DRSUAPI_EXOP_FSMO_REQ_PDC:
1985                         werr = getncchanges_change_master(b_state, mem_ctx, req10, &r->out.ctr->ctr6);
1986                         W_ERROR_NOT_OK_RETURN(werr);
1987                         if (r->out.ctr->ctr6.extended_ret != DRSUAPI_EXOP_ERR_SUCCESS) {
1988                                 return WERR_OK;
1989                         }
1990                         break;
1991                 case DRSUAPI_EXOP_REPL_OBJ:
1992                         werr = getncchanges_repl_obj(b_state, mem_ctx, req10, user_sid, &r->out.ctr->ctr6);
1993                         r->out.result = werr;
1994                         W_ERROR_NOT_OK_RETURN(werr);
1995                         break;
1996
1997                 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE:
1998
1999                         DEBUG(0,(__location__ ": Request for DsGetNCChanges unsupported extended op 0x%x\n",
2000                                  (unsigned)req10->extended_op));
2001                         return WERR_DS_DRA_NOT_SUPPORTED;
2002                 }
2003         }
2004
2005         if (!ldb_dn_validate(getnc_state->ncRoot_dn) ||
2006             ldb_dn_is_null(getnc_state->ncRoot_dn)) {
2007                 DEBUG(0,(__location__ ": Bad DN '%s'\n",
2008                          drs_ObjectIdentifier_to_string(mem_ctx, ncRoot)));
2009                 return WERR_DS_DRA_INVALID_PARAMETER;
2010         }
2011
2012         /* we need the session key for encrypting password attributes */
2013         status = dcesrv_inherited_session_key(dce_call->conn, &session_key);
2014         if (!NT_STATUS_IS_OK(status)) {
2015                 DEBUG(0,(__location__ ": Failed to get session key\n"));
2016                 return WERR_DS_DRA_INTERNAL_ERROR;
2017         }
2018
2019         /* 
2020            TODO: MS-DRSR section 4.1.10.1.1
2021            Work out if this is the start of a new cycle */
2022
2023         if (getnc_state->guids == NULL) {
2024                 const char *extra_filter;
2025                 struct ldb_result *search_res = NULL;
2026
2027                 extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
2028
2029                 getnc_state->min_usn = req10->highwatermark.highest_usn;
2030                 getnc_state->max_usn = getnc_state->min_usn;
2031
2032                 getnc_state->final_udv = talloc_zero(getnc_state,
2033                                         struct drsuapi_DsReplicaCursor2CtrEx);
2034                 if (getnc_state->final_udv == NULL) {
2035                         return WERR_NOT_ENOUGH_MEMORY;
2036                 }
2037                 werr = get_nc_changes_udv(sam_ctx, getnc_state->ncRoot_dn,
2038                                           getnc_state->final_udv);
2039                 if (!W_ERROR_IS_OK(werr)) {
2040                         return werr;
2041                 }
2042
2043                 if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2044                         werr = getncchanges_collect_objects(b_state, mem_ctx, req10,
2045                                                             search_dn, extra_filter,
2046                                                             &search_res);
2047                 } else {
2048                         werr = getncchanges_collect_objects_exop(b_state, mem_ctx, req10,
2049                                                                  &r->out.ctr->ctr6,
2050                                                                  search_dn, extra_filter,
2051                                                                  &search_res);
2052                 }
2053                 W_ERROR_NOT_OK_RETURN(werr);
2054
2055                 /* extract out the GUIDs list */
2056                 getnc_state->num_records = search_res ? search_res->count : 0;
2057                 getnc_state->guids = talloc_array(getnc_state, struct GUID, getnc_state->num_records);
2058                 W_ERROR_HAVE_NO_MEMORY(getnc_state->guids);
2059
2060                 changes = talloc_array(getnc_state,
2061                                        struct drsuapi_changed_objects,
2062                                        getnc_state->num_records);
2063                 W_ERROR_HAVE_NO_MEMORY(changes);
2064
2065                 for (i=0; i<getnc_state->num_records; i++) {
2066                         changes[i].dn = search_res->msgs[i]->dn;
2067                         changes[i].guid = samdb_result_guid(search_res->msgs[i], "objectGUID");
2068                         changes[i].usn = ldb_msg_find_attr_as_uint64(search_res->msgs[i], "uSNChanged", 0);
2069
2070                         if (changes[i].usn > getnc_state->max_usn) {
2071                                 getnc_state->max_usn = changes[i].usn;
2072                         }
2073                 }
2074
2075                 /* RID_ALLOC returns 3 objects in a fixed order */
2076                 if (req10->extended_op == DRSUAPI_EXOP_FSMO_RID_ALLOC) {
2077                         /* Do nothing */
2078                 } else if (req10->replica_flags & DRSUAPI_DRS_GET_ANC) {
2079                         LDB_TYPESAFE_QSORT(changes,
2080                                            getnc_state->num_records,
2081                                            getnc_state,
2082                                            site_res_cmp_anc_order);
2083                 } else {
2084                         LDB_TYPESAFE_QSORT(changes,
2085                                            getnc_state->num_records,
2086                                            getnc_state,
2087                                            site_res_cmp_usn_order);
2088                 }
2089
2090                 for (i=0; i < getnc_state->num_records; i++) {
2091                         getnc_state->guids[i] = changes[i].guid;
2092                         if (GUID_all_zero(&getnc_state->guids[i])) {
2093                                 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
2094                                          ldb_dn_get_linearized(search_res->msgs[i]->dn)));
2095                                 return WERR_DS_DRA_INTERNAL_ERROR;
2096                         }
2097                 }
2098
2099                 getnc_state->final_hwm.tmp_highest_usn = getnc_state->max_usn;
2100                 getnc_state->final_hwm.reserved_usn = 0;
2101                 getnc_state->final_hwm.highest_usn = getnc_state->max_usn;
2102
2103                 talloc_free(search_res);
2104                 talloc_free(changes);
2105         }
2106
2107         if (req10->uptodateness_vector) {
2108                 /* make sure its sorted */
2109                 TYPESAFE_QSORT(req10->uptodateness_vector->cursors,
2110                                req10->uptodateness_vector->count,
2111                                drsuapi_DsReplicaCursor_compare);
2112         }
2113
2114         /* Prefix mapping */
2115         schema = dsdb_get_schema(sam_ctx, mem_ctx);
2116         if (!schema) {
2117                 DEBUG(0,("No schema in sam_ctx\n"));
2118                 return WERR_DS_DRA_INTERNAL_ERROR;
2119         }
2120
2121         r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
2122         *r->out.ctr->ctr6.naming_context = *ncRoot;
2123
2124         if (dsdb_find_guid_by_dn(sam_ctx, getnc_state->ncRoot_dn,
2125                                  &r->out.ctr->ctr6.naming_context->guid) != LDB_SUCCESS) {
2126                 DEBUG(0,(__location__ ": Failed to find GUID of ncRoot_dn %s\n",
2127                          ldb_dn_get_linearized(getnc_state->ncRoot_dn)));
2128                 return WERR_DS_DRA_INTERNAL_ERROR;
2129         }
2130
2131         /* find the SID if there is one */
2132         dsdb_find_sid_by_dn(sam_ctx, getnc_state->ncRoot_dn, &r->out.ctr->ctr6.naming_context->sid);
2133
2134         dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
2135         r->out.ctr->ctr6.mapping_ctr = *ctr;
2136
2137         r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
2138         r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
2139
2140         r->out.ctr->ctr6.old_highwatermark = req10->highwatermark;
2141         r->out.ctr->ctr6.new_highwatermark = req10->highwatermark;
2142
2143         currentObject = &r->out.ctr->ctr6.first_object;
2144
2145         max_objects = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max object sync", 1000);
2146         /*
2147          * The client control here only applies in normal replication, not extended
2148          * operations, which return a fixed set, even if the caller
2149          * sets max_object_count == 0
2150          */
2151         if (req10->extended_op == DRSUAPI_EXOP_NONE) {
2152                 /* use this to force single objects at a time, which is useful
2153                  * for working out what object is giving problems
2154                  */
2155                 if (req10->max_object_count < max_objects) {
2156                         max_objects = req10->max_object_count;
2157                 }
2158         }
2159         /*
2160          * TODO: work out how the maximum should be calculated
2161          */
2162         max_links = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max link sync", 1500);
2163
2164         /*
2165          * Maximum time that we can spend in a getncchanges
2166          * in order to avoid timeout of the other part.
2167          * 10 seconds by default.
2168          */
2169         max_wait = lpcfg_parm_int(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "max work time", 10);
2170
2171         if (req10->partial_attribute_set != NULL) {
2172                 struct dsdb_syntax_ctx syntax_ctx;
2173                 uint32_t j = 0;
2174
2175                 dsdb_syntax_ctx_init(&syntax_ctx, b_state->sam_ctx, schema);
2176                 syntax_ctx.pfm_remote = pfm_remote;
2177
2178                 local_pas = talloc_array(b_state, uint32_t, req10->partial_attribute_set->num_attids);
2179
2180                 for (j = 0; j < req10->partial_attribute_set->num_attids; j++) {
2181                         getncchanges_attid_remote_to_local(schema,
2182                                                            &syntax_ctx,
2183                                                            req10->partial_attribute_set->attids[j],
2184                                                            (enum drsuapi_DsAttributeId *)&local_pas[j],
2185                                                            NULL);
2186                 }
2187
2188                 LDB_TYPESAFE_QSORT(local_pas,
2189                                    req10->partial_attribute_set->num_attids,
2190                                    NULL,
2191                                    uint32_t_ptr_cmp);
2192         }
2193
2194         for (i=getnc_state->num_processed;
2195              i<getnc_state->num_records &&
2196                      !null_scope &&
2197                      (r->out.ctr->ctr6.object_count < max_objects)
2198                      && !max_wait_reached;
2199             i++) {
2200                 int uSN;
2201                 struct drsuapi_DsReplicaObjectListItemEx *obj;
2202                 struct ldb_message *msg;
2203                 static const char * const msg_attrs[] = {
2204                                             "*",
2205                                             "nTSecurityDescriptor",
2206                                             "parentGUID",
2207                                             "replPropertyMetaData",
2208                                             DSDB_SECRET_ATTRIBUTES,
2209                                             NULL };
2210                 struct ldb_result *msg_res;
2211                 struct ldb_dn *msg_dn;
2212
2213                 obj = talloc_zero(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
2214                 W_ERROR_HAVE_NO_MEMORY(obj);
2215
2216                 msg_dn = ldb_dn_new_fmt(obj, sam_ctx, "<GUID=%s>", GUID_string(obj, &getnc_state->guids[i]));
2217                 W_ERROR_HAVE_NO_MEMORY(msg_dn);
2218
2219
2220                 /* by re-searching here we avoid having a lot of full
2221                  * records in memory between calls to getncchanges
2222                  */
2223                 ret = drsuapi_search_with_extended_dn(sam_ctx, obj, &msg_res,
2224                                                       msg_dn,
2225                                                       LDB_SCOPE_BASE, msg_attrs, NULL);
2226                 if (ret != LDB_SUCCESS) {
2227                         if (ret != LDB_ERR_NO_SUCH_OBJECT) {
2228                                 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
2229                                          ldb_dn_get_extended_linearized(obj, msg_dn, 1), ldb_errstring(sam_ctx)));
2230                         }
2231                         talloc_free(obj);
2232                         continue;
2233                 }
2234
2235                 msg = msg_res->msgs[0];
2236
2237                 max_wait_reached = (time(NULL) - start > max_wait);
2238
2239                 werr = get_nc_changes_build_object(obj, msg,
2240                                                    sam_ctx, getnc_state->ncRoot_dn,
2241                                                    getnc_state->is_schema_nc,
2242                                                    schema, &session_key, getnc_state->min_usn,
2243                                                    req10->replica_flags,
2244                                                    req10->partial_attribute_set,
2245                                                    req10->uptodateness_vector,
2246                                                    req10->extended_op,
2247                                                    max_wait_reached,
2248                                                    local_pas);
2249                 if (!W_ERROR_IS_OK(werr)) {
2250                         return werr;
2251                 }
2252
2253                 werr = get_nc_changes_add_links(sam_ctx, getnc_state,
2254                                                 getnc_state->ncRoot_dn,
2255                                                 getnc_state->is_schema_nc,
2256                                                 schema, getnc_state->min_usn,
2257                                                 req10->replica_flags,
2258                                                 msg,
2259                                                 &getnc_state->la_list,
2260                                                 &getnc_state->la_count,
2261                                                 req10->uptodateness_vector);
2262                 if (!W_ERROR_IS_OK(werr)) {
2263                         return werr;
2264                 }
2265
2266                 uSN = ldb_msg_find_attr_as_int(msg, "uSNChanged", -1);
2267                 if (uSN > getnc_state->max_usn) {
2268                         /*
2269                          * Only report the max_usn we had at the start
2270                          * of the replication cycle.
2271                          *
2272                          * If this object has changed lately we better
2273                          * let the destination dsa refetch the change.
2274                          * This is better than the risk of loosing some
2275                          * objects or linked attributes.
2276                          */
2277                         uSN = 0;
2278                 }
2279                 if (uSN > r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn) {
2280                         r->out.ctr->ctr6.new_highwatermark.tmp_highest_usn = uSN;
2281                         r->out.ctr->ctr6.new_highwatermark.reserved_usn = 0;
2282                 }
2283
2284                 if (obj->meta_data_ctr == NULL) {
2285                         DEBUG(8,(__location__ ": getncchanges skipping send of object %s\n",
2286                                  ldb_dn_get_linearized(msg->dn)));
2287                         /* no attributes to send */
2288                         talloc_free(obj);
2289                         continue;
2290                 }
2291
2292                 r->out.ctr->ctr6.object_count++;
2293
2294                 *currentObject = obj;
2295                 currentObject = &obj->next_object;
2296
2297                 DEBUG(8,(__location__ ": replicating object %s\n", ldb_dn_get_linearized(msg->dn)));
2298
2299                 talloc_free(getnc_state->last_dn);
2300                 getnc_state->last_dn = talloc_move(getnc_state, &msg->dn);
2301
2302                 talloc_free(msg_res);
2303                 talloc_free(msg_dn);
2304         }
2305
2306         getnc_state->num_processed = i;
2307
2308         /* the client can us to call UpdateRefs on its behalf to
2309            re-establish monitoring of the NC */
2310         if ((req10->replica_flags & (DRSUAPI_DRS_ADD_REF | DRSUAPI_DRS_REF_GCSPN)) &&
2311             !GUID_all_zero(&req10->destination_dsa_guid)) {
2312                 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq;
2313                 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
2314                          GUID_string(mem_ctx, &req10->destination_dsa_guid)));
2315                 ureq.naming_context = ncRoot;
2316                 ureq.dest_dsa_dns_name = samdb_ntds_msdcs_dns_name(b_state->sam_ctx, mem_ctx,
2317                                                                    &req10->destination_dsa_guid);
2318                 if (!ureq.dest_dsa_dns_name) {
2319                         return WERR_NOT_ENOUGH_MEMORY;
2320                 }
2321                 ureq.dest_dsa_guid = req10->destination_dsa_guid;
2322                 ureq.options = DRSUAPI_DRS_ADD_REF |
2323                         DRSUAPI_DRS_ASYNC_OP |
2324                         DRSUAPI_DRS_GETCHG_CHECK;
2325
2326                 /* we also need to pass through the
2327                    DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
2328                    to send notifies using the GC SPN */
2329                 ureq.options |= (req10->replica_flags & DRSUAPI_DRS_REF_GCSPN);
2330
2331                 werr = drsuapi_UpdateRefs(b_state, mem_ctx, &ureq);
2332                 if (!W_ERROR_IS_OK(werr)) {
2333                         DEBUG(0,(__location__ ": Failed UpdateRefs on %s for %s in DsGetNCChanges - %s\n",
2334                                  drs_ObjectIdentifier_to_string(mem_ctx, ncRoot), ureq.dest_dsa_dns_name,
2335                                  win_errstr(werr)));
2336                 }
2337         }
2338
2339         /*
2340          * TODO:
2341          * This is just a guess, how to calculate the
2342          * number of linked attributes to send, we need to
2343          * find out how to do this right.
2344          */
2345         if (r->out.ctr->ctr6.object_count >= max_links) {
2346                 max_links = 0;
2347         } else {
2348                 max_links -= r->out.ctr->ctr6.object_count;
2349         }
2350
2351         link_total = getnc_state->la_count;
2352
2353         if (i < getnc_state->num_records) {
2354                 r->out.ctr->ctr6.more_data = true;
2355         } else {
2356                 /* sort the whole array the first time */
2357                 if (getnc_state->la_sorted == NULL) {
2358                         int j;
2359                         struct la_for_sorting *guid_array = talloc_array(getnc_state, struct la_for_sorting, getnc_state->la_count);
2360                         if (guid_array == NULL) {
2361                                 DEBUG(0, ("Out of memory allocating %u linked attributes for sorting", getnc_state->la_count));
2362                                 return WERR_NOT_ENOUGH_MEMORY;
2363                         }
2364                         for (j = 0; j < getnc_state->la_count; j++) {
2365                                 /* we need to get the target GUIDs to compare */
2366                                 struct dsdb_dn *dn;
2367                                 const struct drsuapi_DsReplicaLinkedAttribute *la = &getnc_state->la_list[j];
2368                                 const struct dsdb_attribute *schema_attrib;
2369                                 const struct ldb_val *target_guid;
2370                                 DATA_BLOB source_guid;
2371                                 TALLOC_CTX *frame = talloc_stackframe();
2372
2373                                 schema_attrib = dsdb_attribute_by_attributeID_id(schema, la->attid);
2374
2375                                 werr = dsdb_dn_la_from_blob(sam_ctx, schema_attrib, schema, frame, la->value.blob, &dn);
2376                                 if (!W_ERROR_IS_OK(werr)) {
2377                                         DEBUG(0,(__location__ ": Bad la blob in sort\n"));
2378                                         TALLOC_FREE(frame);
2379                                         return werr;
2380                                 }
2381
2382                                 /* Extract the target GUID in NDR form */
2383                                 target_guid = ldb_dn_get_extended_component(dn->dn, "GUID");
2384                                 if (target_guid == NULL
2385                                     || target_guid->length != sizeof(guid_array[0].target_guid)) {
2386                                         status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2387                                 } else {
2388                                         /* Repack the source GUID as NDR for sorting */
2389                                         status = GUID_to_ndr_blob(&la->identifier->guid,
2390                                                                   frame,
2391                                                                   &source_guid);
2392                                 }
2393
2394                                 if (!NT_STATUS_IS_OK(status)
2395                                     || source_guid.length != sizeof(guid_array[0].source_guid)) {
2396                                         DEBUG(0,(__location__ ": Bad la guid in sort\n"));
2397                                         TALLOC_FREE(frame);
2398                                         return ntstatus_to_werror(status);
2399                                 }
2400
2401                                 guid_array[j].link = &getnc_state->la_list[j];
2402                                 memcpy(guid_array[j].target_guid, target_guid->data,
2403                                        sizeof(guid_array[j].target_guid));
2404                                 memcpy(guid_array[j].source_guid, source_guid.data,
2405                                        sizeof(guid_array[j].source_guid));
2406                                 TALLOC_FREE(frame);
2407                         }
2408
2409                         LDB_TYPESAFE_QSORT(guid_array, getnc_state->la_count, NULL, linked_attribute_compare);
2410                         getnc_state->la_sorted = guid_array;
2411                 }
2412
2413                 link_count = getnc_state->la_count - getnc_state->la_idx;
2414                 link_count = MIN(max_links, link_count);
2415
2416                 r->out.ctr->ctr6.linked_attributes_count = link_count;
2417                 r->out.ctr->ctr6.linked_attributes = talloc_array(r->out.ctr, struct drsuapi_DsReplicaLinkedAttribute, link_count);
2418                 if (r->out.ctr->ctr6.linked_attributes == NULL) {
2419                         DEBUG(0, ("Out of memory allocating %u linked attributes for output", link_count));
2420                         return WERR_NOT_ENOUGH_MEMORY;
2421                 }
2422
2423                 for (k = 0; k < link_count; k++) {
2424                         r->out.ctr->ctr6.linked_attributes[k]
2425                                 = *getnc_state->la_sorted[getnc_state->la_idx + k].link;
2426                 }
2427
2428                 getnc_state->la_idx += link_count;
2429                 link_given = getnc_state->la_idx;
2430
2431                 if (getnc_state->la_idx < getnc_state->la_count) {
2432                         r->out.ctr->ctr6.more_data = true;
2433                 }
2434         }
2435
2436         if (req10->replica_flags & DRSUAPI_DRS_GET_NC_SIZE) {
2437                 /*
2438                  * TODO: This implementation is wrong
2439                  * we should find out the total number of
2440                  * objects and links in the whole naming context
2441                  * at the start of the cycle and return these
2442                  * values in each message.
2443                  *
2444                  * For now we keep our current strategy and return
2445                  * the number of objects for this cycle and the number
2446                  * of links we found so far during the cycle.
2447                  */
2448                 r->out.ctr->ctr6.nc_object_count = getnc_state->num_records;
2449                 r->out.ctr->ctr6.nc_linked_attributes_count = getnc_state->la_count;
2450         }
2451
2452         if (!r->out.ctr->ctr6.more_data) {
2453                 talloc_steal(mem_ctx, getnc_state->la_list);
2454
2455                 r->out.ctr->ctr6.new_highwatermark = getnc_state->final_hwm;
2456                 r->out.ctr->ctr6.uptodateness_vector = talloc_move(mem_ctx,
2457                                                         &getnc_state->final_udv);
2458
2459                 talloc_free(getnc_state);
2460                 b_state->getncchanges_state = NULL;
2461         } else {
2462                 ret = drsuapi_DsReplicaHighWaterMark_cmp(&r->out.ctr->ctr6.old_highwatermark,
2463                                                          &r->out.ctr->ctr6.new_highwatermark);
2464                 if (ret == 0) {
2465                         /*
2466                          * We need to make sure that we never return the
2467                          * same highwatermark within the same replication
2468                          * cycle more than once. Otherwise we cannot detect
2469                          * when the client uses an unexptected highwatermark.
2470                          *
2471                          * This is a HACK which is needed because our
2472                          * object ordering is wrong and set tmp_highest_usn
2473                          * to a value that is higher than what we already
2474                          * sent to the client (destination dsa).
2475                          */
2476                         r->out.ctr->ctr6.new_highwatermark.reserved_usn += 1;
2477                 }
2478
2479                 getnc_state->last_hwm = r->out.ctr->ctr6.new_highwatermark;
2480         }
2481
2482         if (req10->extended_op != DRSUAPI_EXOP_NONE) {
2483                 r->out.ctr->ctr6.uptodateness_vector = NULL;
2484                 r->out.ctr->ctr6.nc_object_count = 0;
2485                 ZERO_STRUCT(r->out.ctr->ctr6.new_highwatermark);
2486         }
2487
2488         DEBUG(r->out.ctr->ctr6.more_data?4:2,
2489               ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
2490                (unsigned long long)(req10->highwatermark.highest_usn+1),
2491                req10->replica_flags, drs_ObjectIdentifier_to_string(mem_ctx, ncRoot),
2492                r->out.ctr->ctr6.object_count,
2493                i, r->out.ctr->ctr6.more_data?getnc_state->num_records:i,
2494                r->out.ctr->ctr6.linked_attributes_count,
2495                link_given, link_total,
2496                dom_sid_string(mem_ctx, user_sid)));
2497
2498 #if 0
2499         if (!r->out.ctr->ctr6.more_data && req10->extended_op != DRSUAPI_EXOP_NONE) {
2500                 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges, NDR_BOTH, r);
2501         }
2502 #endif
2503
2504         return WERR_OK;
2505 }