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