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