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