2 Unix SMB/CIFS implementation.
4 implement the DSGetNCChanges call
6 Copyright (C) Anatoliy Atanasov 2009
7 Copyright (C) Andrew Tridgell 2009
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.
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.
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/>.
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "param/param.h"
27 #include "librpc/gen_ndr/ndr_drsblobs.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "libcli/security/security.h"
31 #include "libcli/security/session.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33 #include "rpc_server/dcerpc_server_proto.h"
34 #include "../libcli/drsuapi/drsuapi.h"
35 #include "lib/util/binsearch.h"
36 #include "lib/util/tsort.h"
37 #include "auth/session.h"
38 #include "dsdb/common/util.h"
41 build a DsReplicaObjectIdentifier from a ldb msg
43 static struct drsuapi_DsReplicaObjectIdentifier
*get_object_identifier(TALLOC_CTX
*mem_ctx
,
44 struct ldb_message
*msg
)
46 struct drsuapi_DsReplicaObjectIdentifier
*identifier
;
49 identifier
= talloc(mem_ctx
, struct drsuapi_DsReplicaObjectIdentifier
);
50 if (identifier
== NULL
) {
54 identifier
->dn
= ldb_dn_alloc_linearized(identifier
, msg
->dn
);
55 identifier
->guid
= samdb_result_guid(msg
, "objectGUID");
57 sid
= samdb_result_dom_sid(identifier
, msg
, "objectSid");
59 identifier
->sid
= *sid
;
61 ZERO_STRUCT(identifier
->sid
);
66 static int udv_compare(const struct GUID
*guid1
, struct GUID guid2
)
68 return GUID_compare(guid1
, &guid2
);
72 see if we can filter an attribute using the uptodateness_vector
74 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx
*udv
,
75 const struct GUID
*originating_invocation_id
,
76 uint64_t originating_usn
)
78 const struct drsuapi_DsReplicaCursor
*c
;
79 if (udv
== NULL
) return false;
80 BINARY_ARRAY_SEARCH(udv
->cursors
, udv
->count
, source_dsa_invocation_id
,
81 originating_invocation_id
, udv_compare
, c
);
82 if (c
&& originating_usn
<= c
->highest_usn
) {
89 static int attid_cmp(enum drsuapi_DsAttributeId a1
, enum drsuapi_DsAttributeId a2
)
91 if (a1
== a2
) return 0;
92 return ((uint32_t)a1
) > ((uint32_t)a2
) ? 1 : -1;
96 check if an attribute is in a partial_attribute_set
98 static bool check_partial_attribute_set(const struct dsdb_attribute
*sa
,
99 struct drsuapi_DsPartialAttributeSet
*pas
)
101 enum drsuapi_DsAttributeId
*result
;
102 BINARY_ARRAY_SEARCH_V(pas
->attids
, pas
->num_attids
, (enum drsuapi_DsAttributeId
)sa
->attributeID_id
,
104 return result
!= NULL
;
109 drsuapi_DsGetNCChanges for one object
111 static WERROR
get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx
*obj
,
112 struct ldb_message
*msg
,
113 struct ldb_context
*sam_ctx
,
114 struct ldb_dn
*ncRoot_dn
,
116 struct dsdb_schema
*schema
,
117 DATA_BLOB
*session_key
,
118 uint64_t highest_usn
,
119 uint32_t replica_flags
,
120 struct drsuapi_DsPartialAttributeSet
*partial_attribute_set
,
121 struct drsuapi_DsReplicaCursorCtrEx
*uptodateness_vector
,
122 enum drsuapi_DsExtendedOperation extended_op
)
124 const struct ldb_val
*md_value
;
126 struct replPropertyMetaDataBlob md
;
128 enum ndr_err_code ndr_err
;
131 const struct dsdb_attribute
*rdn_sa
;
132 unsigned int instanceType
;
133 struct dsdb_syntax_ctx syntax_ctx
;
135 /* make dsdb sytanx context for conversions */
136 dsdb_syntax_ctx_init(&syntax_ctx
, sam_ctx
, schema
);
137 syntax_ctx
.is_schema_nc
= is_schema_nc
;
139 instanceType
= ldb_msg_find_attr_as_uint(msg
, "instanceType", 0);
140 if (instanceType
& INSTANCE_TYPE_IS_NC_HEAD
) {
141 obj
->is_nc_prefix
= true;
142 obj
->parent_object_guid
= NULL
;
144 obj
->is_nc_prefix
= false;
145 obj
->parent_object_guid
= talloc(obj
, struct GUID
);
146 if (obj
->parent_object_guid
== NULL
) {
147 return WERR_DS_DRA_INTERNAL_ERROR
;
149 *obj
->parent_object_guid
= samdb_result_guid(msg
, "parentGUID");
150 if (GUID_all_zero(obj
->parent_object_guid
)) {
151 DEBUG(0,(__location__
": missing parentGUID for %s\n",
152 ldb_dn_get_linearized(msg
->dn
)));
153 return WERR_DS_DRA_INTERNAL_ERROR
;
156 obj
->next_object
= NULL
;
158 md_value
= ldb_msg_find_ldb_val(msg
, "replPropertyMetaData");
160 /* nothing to send */
164 if (instanceType
& INSTANCE_TYPE_UNINSTANT
) {
165 /* don't send uninstantiated objects */
169 ndr_err
= ndr_pull_struct_blob(md_value
, obj
, &md
,
170 (ndr_pull_flags_fn_t
)ndr_pull_replPropertyMetaDataBlob
);
171 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
172 return WERR_DS_DRA_INTERNAL_ERROR
;
175 if (md
.version
!= 1) {
176 return WERR_DS_DRA_INTERNAL_ERROR
;
179 rdn
= ldb_dn_get_rdn_name(msg
->dn
);
181 DEBUG(0,(__location__
": No rDN for %s\n", ldb_dn_get_linearized(msg
->dn
)));
182 return WERR_DS_DRA_INTERNAL_ERROR
;
185 rdn_sa
= dsdb_attribute_by_lDAPDisplayName(schema
, rdn
);
186 if (rdn_sa
== NULL
) {
187 DEBUG(0,(__location__
": Can't find dsds_attribute for rDN %s in %s\n",
188 rdn
, ldb_dn_get_linearized(msg
->dn
)));
189 return WERR_DS_DRA_INTERNAL_ERROR
;
192 obj
->meta_data_ctr
= talloc(obj
, struct drsuapi_DsReplicaMetaDataCtr
);
193 attids
= talloc_array(obj
, uint32_t, md
.ctr
.ctr1
.count
);
195 obj
->object
.identifier
= get_object_identifier(obj
, msg
);
196 if (obj
->object
.identifier
== NULL
) {
199 dom_sid_split_rid(NULL
, &obj
->object
.identifier
->sid
, NULL
, &rid
);
201 obj
->meta_data_ctr
->meta_data
= talloc_array(obj
, struct drsuapi_DsReplicaMetaData
, md
.ctr
.ctr1
.count
);
202 for (n
=i
=0; i
<md
.ctr
.ctr1
.count
; i
++) {
203 const struct dsdb_attribute
*sa
;
204 bool force_attribute
= false;
206 /* if the attribute has not changed, and it is not the
207 instanceType then don't include it */
208 if (md
.ctr
.ctr1
.array
[i
].local_usn
< highest_usn
&&
209 extended_op
!= DRSUAPI_EXOP_REPL_SECRET
&&
210 md
.ctr
.ctr1
.array
[i
].attid
!= DRSUAPI_ATTID_instanceType
) continue;
212 /* don't include the rDN */
213 if (md
.ctr
.ctr1
.array
[i
].attid
== rdn_sa
->attributeID_id
) continue;
215 sa
= dsdb_attribute_by_attributeID_id(schema
, md
.ctr
.ctr1
.array
[i
].attid
);
217 DEBUG(0,(__location__
": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
218 (unsigned int)md
.ctr
.ctr1
.array
[i
].attid
,
219 ldb_dn_get_linearized(msg
->dn
)));
220 return WERR_DS_DRA_INTERNAL_ERROR
;
224 struct ldb_message_element
*el
;
225 el
= ldb_msg_find_element(msg
, sa
->lDAPDisplayName
);
226 if (el
&& el
->num_values
&& dsdb_dn_is_upgraded_link_val(&el
->values
[0])) {
227 /* don't send upgraded links inline */
232 if (extended_op
== DRSUAPI_EXOP_REPL_SECRET
&&
233 !dsdb_attr_in_rodc_fas(sa
)) {
234 force_attribute
= true;
235 DEBUG(4,("Forcing attribute %s in %s\n",
236 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
239 /* filter by uptodateness_vector */
240 if (md
.ctr
.ctr1
.array
[i
].attid
!= DRSUAPI_ATTID_instanceType
&&
242 udv_filter(uptodateness_vector
,
243 &md
.ctr
.ctr1
.array
[i
].originating_invocation_id
,
244 md
.ctr
.ctr1
.array
[i
].originating_usn
)) {
248 /* filter by partial_attribute_set */
249 if (partial_attribute_set
&& !check_partial_attribute_set(sa
, partial_attribute_set
)) {
253 obj
->meta_data_ctr
->meta_data
[n
].originating_change_time
= md
.ctr
.ctr1
.array
[i
].originating_change_time
;
254 obj
->meta_data_ctr
->meta_data
[n
].version
= md
.ctr
.ctr1
.array
[i
].version
;
255 obj
->meta_data_ctr
->meta_data
[n
].originating_invocation_id
= md
.ctr
.ctr1
.array
[i
].originating_invocation_id
;
256 obj
->meta_data_ctr
->meta_data
[n
].originating_usn
= md
.ctr
.ctr1
.array
[i
].originating_usn
;
257 attids
[n
] = md
.ctr
.ctr1
.array
[i
].attid
;
261 /* ignore it if its an empty change. Note that renames always
262 * change the 'name' attribute, so they won't be ignored by
265 (n
== 1 && attids
[0] == DRSUAPI_ATTID_instanceType
)) {
266 talloc_free(obj
->meta_data_ctr
);
267 obj
->meta_data_ctr
= NULL
;
271 obj
->meta_data_ctr
->count
= n
;
273 obj
->object
.flags
= DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER
;
274 obj
->object
.attribute_ctr
.num_attributes
= obj
->meta_data_ctr
->count
;
275 obj
->object
.attribute_ctr
.attributes
= talloc_array(obj
, struct drsuapi_DsReplicaAttribute
,
276 obj
->object
.attribute_ctr
.num_attributes
);
279 * Note that the meta_data array and the attributes array must
280 * be the same size and in the same order
282 for (i
=0; i
<obj
->object
.attribute_ctr
.num_attributes
; i
++) {
283 struct ldb_message_element
*el
;
285 const struct dsdb_attribute
*sa
;
287 sa
= dsdb_attribute_by_attributeID_id(schema
, attids
[i
]);
289 DEBUG(0,("Unable to find attributeID %u in schema\n", attids
[i
]));
290 return WERR_DS_DRA_INTERNAL_ERROR
;
293 el
= ldb_msg_find_element(msg
, sa
->lDAPDisplayName
);
295 /* this happens for attributes that have been removed */
296 DEBUG(5,("No element '%s' for attributeID %u in message\n",
297 sa
->lDAPDisplayName
, attids
[i
]));
298 ZERO_STRUCT(obj
->object
.attribute_ctr
.attributes
[i
]);
299 obj
->object
.attribute_ctr
.attributes
[i
].attid
=
300 dsdb_attribute_get_attid(sa
, syntax_ctx
.is_schema_nc
);
302 werr
= sa
->syntax
->ldb_to_drsuapi(&syntax_ctx
, sa
, el
, obj
,
303 &obj
->object
.attribute_ctr
.attributes
[i
]);
304 if (!W_ERROR_IS_OK(werr
)) {
305 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
306 sa
->lDAPDisplayName
, win_errstr(werr
)));
309 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
310 * check if attribute is secret and send a null value
312 if (replica_flags
& DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING
) {
313 drsuapi_process_secret_attribute(&obj
->object
.attribute_ctr
.attributes
[i
],
314 &obj
->meta_data_ctr
->meta_data
[i
]);
316 /* some attributes needs to be encrypted
318 werr
= drsuapi_encrypt_attribute(obj
, session_key
, rid
,
319 &obj
->object
.attribute_ctr
.attributes
[i
]);
320 if (!W_ERROR_IS_OK(werr
)) {
321 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
322 sa
->lDAPDisplayName
, win_errstr(werr
)));
333 add one linked attribute from an object to the list of linked
334 attributes in a getncchanges request
336 static WERROR
get_nc_changes_add_la(TALLOC_CTX
*mem_ctx
,
337 struct ldb_context
*sam_ctx
,
338 const struct dsdb_schema
*schema
,
339 const struct dsdb_attribute
*sa
,
340 struct ldb_message
*msg
,
341 struct dsdb_dn
*dsdb_dn
,
342 struct drsuapi_DsReplicaLinkedAttribute
**la_list
,
345 struct drsuapi_DsReplicaLinkedAttribute
*la
;
350 (*la_list
) = talloc_realloc(mem_ctx
, *la_list
, struct drsuapi_DsReplicaLinkedAttribute
, (*la_count
)+1);
351 W_ERROR_HAVE_NO_MEMORY(*la_list
);
353 la
= &(*la_list
)[*la_count
];
355 la
->identifier
= get_object_identifier(*la_list
, msg
);
356 W_ERROR_HAVE_NO_MEMORY(la
->identifier
);
358 active
= (dsdb_dn_rmd_flags(dsdb_dn
->dn
) & DSDB_RMD_FLAG_DELETED
) == 0;
361 /* We have to check that the inactive link still point to an existing object */
367 v
= ldb_msg_find_attr_as_string(msg
, "isDeleted", "false");
368 if (strncasecmp(v
, "true", 4) == 0) {
370 * Note: we skip the transmition of the deleted link even if the other part used to
371 * know about it because when we transmit the deletion of the object, the link will
372 * be deleted too due to deletion of object where link points and Windows do so.
374 if (dsdb_functional_level(sam_ctx
) >= DS_DOMAIN_FUNCTION_2008_R2
) {
375 v
= ldb_msg_find_attr_as_string(msg
, "isRecycled", "true");
377 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
378 * if it join an existing domain with deleted objets, it firsts impose to have a
379 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
380 * either during initial replication or after the getNCChanges.
381 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
383 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
384 * If FL >=2K8R2 we are sure that this attribute will be here.
385 * For this kind of forest level we do not return the link if the object is recycled
386 * (isRecycled = true).
388 if (strncasecmp(v
, "true", 4) == 0) {
389 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
390 ldb_dn_get_linearized(msg
->dn
)));
397 status
= dsdb_get_extended_dn_guid(dsdb_dn
->dn
, &guid
, "GUID");
398 if (!NT_STATUS_IS_OK(status
)) {
399 DEBUG(0,(__location__
" Unable to extract GUID in linked attribute '%s' in '%s'\n",
400 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
401 return ntstatus_to_werror(status
);
403 ret
= dsdb_find_dn_by_guid(sam_ctx
, mem_ctx
, &guid
, &tdn
);
404 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
405 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
406 GUID_string(mem_ctx
, &guid
)));
408 } else if (ret
!= LDB_SUCCESS
) {
409 DEBUG(0, (__location__
" Search of guid %s failed with error code %d\n",
410 GUID_string(mem_ctx
, &guid
),
415 la
->attid
= sa
->attributeID_id
;
416 la
->flags
= active
?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
:0;
418 status
= dsdb_get_extended_dn_uint32(dsdb_dn
->dn
, &la
->meta_data
.version
, "RMD_VERSION");
419 if (!NT_STATUS_IS_OK(status
)) {
420 DEBUG(0,(__location__
" No RMD_VERSION in linked attribute '%s' in '%s'\n",
421 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
422 return ntstatus_to_werror(status
);
424 status
= dsdb_get_extended_dn_nttime(dsdb_dn
->dn
, &la
->meta_data
.originating_change_time
, "RMD_CHANGETIME");
425 if (!NT_STATUS_IS_OK(status
)) {
426 DEBUG(0,(__location__
" No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
427 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
428 return ntstatus_to_werror(status
);
430 status
= dsdb_get_extended_dn_guid(dsdb_dn
->dn
, &la
->meta_data
.originating_invocation_id
, "RMD_INVOCID");
431 if (!NT_STATUS_IS_OK(status
)) {
432 DEBUG(0,(__location__
" No RMD_INVOCID in linked attribute '%s' in '%s'\n",
433 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
434 return ntstatus_to_werror(status
);
436 status
= dsdb_get_extended_dn_uint64(dsdb_dn
->dn
, &la
->meta_data
.originating_usn
, "RMD_ORIGINATING_USN");
437 if (!NT_STATUS_IS_OK(status
)) {
438 DEBUG(0,(__location__
" No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
439 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
440 return ntstatus_to_werror(status
);
443 status
= dsdb_get_extended_dn_nttime(dsdb_dn
->dn
, &la
->originating_add_time
, "RMD_ADDTIME");
444 if (!NT_STATUS_IS_OK(status
)) {
445 /* this is possible for upgraded links */
446 la
->originating_add_time
= la
->meta_data
.originating_change_time
;
449 werr
= dsdb_dn_la_to_blob(sam_ctx
, sa
, schema
, *la_list
, dsdb_dn
, &la
->value
.blob
);
450 W_ERROR_NOT_OK_RETURN(werr
);
458 add linked attributes from an object to the list of linked
459 attributes in a getncchanges request
461 static WERROR
get_nc_changes_add_links(struct ldb_context
*sam_ctx
,
463 struct ldb_dn
*ncRoot_dn
,
464 struct dsdb_schema
*schema
,
465 uint64_t highest_usn
,
466 uint32_t replica_flags
,
467 struct ldb_message
*msg
,
468 struct drsuapi_DsReplicaLinkedAttribute
**la_list
,
470 struct drsuapi_DsReplicaCursorCtrEx
*uptodateness_vector
)
473 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
474 uint64_t uSNChanged
= ldb_msg_find_attr_as_int(msg
, "uSNChanged", -1);
476 for (i
=0; i
<msg
->num_elements
; i
++) {
477 struct ldb_message_element
*el
= &msg
->elements
[i
];
478 const struct dsdb_attribute
*sa
;
481 sa
= dsdb_attribute_by_lDAPDisplayName(schema
, el
->name
);
483 if (!sa
|| sa
->linkID
== 0 || (sa
->linkID
& 1)) {
484 /* we only want forward links */
488 if (el
->num_values
&& !dsdb_dn_is_upgraded_link_val(&el
->values
[0])) {
489 /* its an old style link, it will have been
490 * sent in the main replication data */
494 for (j
=0; j
<el
->num_values
; j
++) {
495 struct dsdb_dn
*dsdb_dn
;
500 dsdb_dn
= dsdb_dn_parse(tmp_ctx
, sam_ctx
, &el
->values
[j
], sa
->syntax
->ldap_oid
);
501 if (dsdb_dn
== NULL
) {
502 DEBUG(1,(__location__
": Failed to parse DN for %s in %s\n",
503 el
->name
, ldb_dn_get_linearized(msg
->dn
)));
504 talloc_free(tmp_ctx
);
505 return WERR_DS_DRA_INTERNAL_ERROR
;
508 status
= dsdb_get_extended_dn_uint64(dsdb_dn
->dn
, &local_usn
, "RMD_LOCAL_USN");
509 if (!NT_STATUS_IS_OK(status
)) {
510 /* this can happen for attributes
511 given to us with old style meta
516 if (local_usn
> uSNChanged
) {
517 DEBUG(1,(__location__
": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
518 el
->name
, ldb_dn_get_linearized(msg
->dn
)));
519 talloc_free(tmp_ctx
);
520 return WERR_DS_DRA_INTERNAL_ERROR
;
523 if (local_usn
< highest_usn
) {
527 werr
= get_nc_changes_add_la(mem_ctx
, sam_ctx
, schema
, sa
, msg
,
528 dsdb_dn
, la_list
, la_count
);
529 if (!W_ERROR_IS_OK(werr
)) {
530 talloc_free(tmp_ctx
);
536 talloc_free(tmp_ctx
);
541 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
543 static WERROR
get_nc_changes_udv(struct ldb_context
*sam_ctx
,
544 struct ldb_dn
*ncRoot_dn
,
545 struct drsuapi_DsReplicaCursor2CtrEx
*udv
)
553 ret
= dsdb_load_udv_v2(sam_ctx
, ncRoot_dn
, udv
, &udv
->cursors
, &udv
->count
);
554 if (ret
!= LDB_SUCCESS
) {
555 DEBUG(0,(__location__
": Failed to load UDV for %s - %s\n",
556 ldb_dn_get_linearized(ncRoot_dn
), ldb_errstring(sam_ctx
)));
557 return WERR_DS_DRA_INTERNAL_ERROR
;
564 /* comparison function for linked attributes - see CompareLinks() in
565 * MS-DRSR section 4.1.10.5.17 */
566 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute
*la1
,
567 const struct drsuapi_DsReplicaLinkedAttribute
*la2
,
568 struct ldb_context
*sam_ctx
)
573 const struct dsdb_schema
*schema
;
574 const struct dsdb_attribute
*schema_attrib
;
575 struct dsdb_dn
*dn1
, *dn2
;
576 struct GUID guid1
, guid2
;
579 c
= GUID_compare(&la1
->identifier
->guid
,
580 &la2
->identifier
->guid
);
581 if (c
!= 0) return c
;
583 if (la1
->attid
!= la2
->attid
) {
584 return la1
->attid
< la2
->attid
? -1:1;
587 if ((la1
->flags
& DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
) !=
588 (la2
->flags
& DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
)) {
589 return (la1
->flags
& DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
)? 1:-1;
592 /* we need to get the target GUIDs to compare */
593 tmp_ctx
= talloc_new(sam_ctx
);
595 schema
= dsdb_get_schema(sam_ctx
, tmp_ctx
);
596 schema_attrib
= dsdb_attribute_by_attributeID_id(schema
, la1
->attid
);
598 werr
= dsdb_dn_la_from_blob(sam_ctx
, schema_attrib
, schema
, tmp_ctx
, la1
->value
.blob
, &dn1
);
599 if (!W_ERROR_IS_OK(werr
)) {
600 DEBUG(0,(__location__
": Bad la1 blob in sort\n"));
601 talloc_free(tmp_ctx
);
605 werr
= dsdb_dn_la_from_blob(sam_ctx
, schema_attrib
, schema
, tmp_ctx
, la2
->value
.blob
, &dn2
);
606 if (!W_ERROR_IS_OK(werr
)) {
607 DEBUG(0,(__location__
": Bad la2 blob in sort\n"));
608 talloc_free(tmp_ctx
);
612 status
= dsdb_get_extended_dn_guid(dn1
->dn
, &guid1
, "GUID");
613 if (!NT_STATUS_IS_OK(status
)) {
614 DEBUG(0,(__location__
": Bad la1 guid in sort\n"));
615 talloc_free(tmp_ctx
);
618 status
= dsdb_get_extended_dn_guid(dn2
->dn
, &guid2
, "GUID");
619 if (!NT_STATUS_IS_OK(status
)) {
620 DEBUG(0,(__location__
": Bad la2 guid in sort\n"));
621 talloc_free(tmp_ctx
);
625 talloc_free(tmp_ctx
);
627 return GUID_compare(&guid1
, &guid2
);
630 struct drsuapi_changed_objects
{
637 sort the objects we send by tree order
639 static int site_res_cmp_parent_order(struct drsuapi_changed_objects
*m1
,
640 struct drsuapi_changed_objects
*m2
)
642 return ldb_dn_compare(m2
->dn
, m1
->dn
);
646 sort the objects we send first by uSNChanged
648 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects
*m1
,
649 struct drsuapi_changed_objects
*m2
)
651 unsigned usnchanged1
, usnchanged2
;
654 cn1
= ldb_dn_get_comp_num(m1
->dn
);
655 cn2
= ldb_dn_get_comp_num(m2
->dn
);
657 return cn1
> cn2
? 1 : -1;
659 usnchanged1
= m1
->usn
;
660 usnchanged2
= m2
->usn
;
661 if (usnchanged1
== usnchanged2
) {
664 return usnchanged1
> usnchanged2
? 1 : -1;
669 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
671 static WERROR
getncchanges_rid_alloc(struct drsuapi_bind_state
*b_state
,
673 struct drsuapi_DsGetNCChangesRequest10
*req10
,
674 struct drsuapi_DsGetNCChangesCtr6
*ctr6
)
676 struct ldb_dn
*rid_manager_dn
, *fsmo_role_dn
, *req_dn
;
678 struct ldb_context
*ldb
= b_state
->sam_ctx
;
679 struct ldb_result
*ext_res
;
680 struct ldb_dn
*base_dn
;
681 struct dsdb_fsmo_extended_op
*exop
;
685 - verify that the DN being asked for is the RID Manager DN
686 - verify that we are the RID Manager
689 /* work out who is the RID Manager */
690 ret
= samdb_rid_manager_dn(ldb
, mem_ctx
, &rid_manager_dn
);
691 if (ret
!= LDB_SUCCESS
) {
692 DEBUG(0, (__location__
": Failed to find RID Manager object - %s\n", ldb_errstring(ldb
)));
693 return WERR_DS_DRA_INTERNAL_ERROR
;
696 req_dn
= drs_ObjectIdentifier_to_dn(mem_ctx
, ldb
, req10
->naming_context
);
697 if (!ldb_dn_validate(req_dn
) ||
698 ldb_dn_compare(req_dn
, rid_manager_dn
) != 0) {
699 /* that isn't the RID Manager DN */
700 DEBUG(0,(__location__
": RID Alloc request for wrong DN %s\n",
701 drs_ObjectIdentifier_to_string(mem_ctx
, req10
->naming_context
)));
702 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_MISMATCH
;
706 /* find the DN of the RID Manager */
707 ret
= samdb_reference_dn(ldb
, mem_ctx
, rid_manager_dn
, "fSMORoleOwner", &fsmo_role_dn
);
708 if (ret
!= LDB_SUCCESS
) {
709 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in RID Manager object - %s\n",
710 ldb_errstring(ldb
)));
711 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
;
712 return WERR_DS_DRA_INTERNAL_ERROR
;
715 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb
), fsmo_role_dn
) != 0) {
716 /* we're not the RID Manager - go away */
717 DEBUG(0,(__location__
": RID Alloc request when not RID Manager\n"));
718 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
;
722 exop
= talloc(mem_ctx
, struct dsdb_fsmo_extended_op
);
723 W_ERROR_HAVE_NO_MEMORY(exop
);
725 exop
->fsmo_info
= req10
->fsmo_info
;
726 exop
->destination_dsa_guid
= req10
->destination_dsa_guid
;
728 ret
= ldb_transaction_start(ldb
);
729 if (ret
!= LDB_SUCCESS
) {
730 DEBUG(0,(__location__
": Failed transaction start - %s\n",
731 ldb_errstring(ldb
)));
732 return WERR_DS_DRA_INTERNAL_ERROR
;
736 * FIXME (kim): this is a temp hack to return just few object,
737 * but not the whole domain NC.
738 * We should remove this hack and implement a 'scope'
739 * building function to return just the set of object
740 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
742 ldb_sequence_number(ldb
, LDB_SEQ_HIGHEST_SEQ
, &req10
->highwatermark
.highest_usn
);
744 ret
= ldb_extended(ldb
, DSDB_EXTENDED_ALLOCATE_RID_POOL
, exop
, &ext_res
);
745 if (ret
!= LDB_SUCCESS
) {
746 DEBUG(0,(__location__
": Failed extended allocation RID pool operation - %s\n",
747 ldb_errstring(ldb
)));
748 ldb_transaction_cancel(ldb
);
749 return WERR_DS_DRA_INTERNAL_ERROR
;
752 ret
= ldb_transaction_commit(ldb
);
753 if (ret
!= LDB_SUCCESS
) {
754 DEBUG(0,(__location__
": Failed transaction commit - %s\n",
755 ldb_errstring(ldb
)));
756 return WERR_DS_DRA_INTERNAL_ERROR
;
759 talloc_free(ext_res
);
761 base_dn
= ldb_get_default_basedn(ldb
);
763 DEBUG(2,("Allocated RID pool for server %s\n",
764 GUID_string(mem_ctx
, &req10
->destination_dsa_guid
)));
766 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
772 return an array of SIDs from a ldb_message given an attribute name
773 assumes the SIDs are in extended DN format
775 static WERROR
samdb_result_sid_array_dn(struct ldb_context
*sam_ctx
,
776 struct ldb_message
*msg
,
779 const struct dom_sid
***sids
)
781 struct ldb_message_element
*el
;
784 el
= ldb_msg_find_element(msg
, attr
);
790 (*sids
) = talloc_array(mem_ctx
, const struct dom_sid
*, el
->num_values
+ 1);
791 W_ERROR_HAVE_NO_MEMORY(*sids
);
793 for (i
=0; i
<el
->num_values
; i
++) {
794 struct ldb_dn
*dn
= ldb_dn_from_ldb_val(mem_ctx
, sam_ctx
, &el
->values
[i
]);
798 sid
= talloc(*sids
, struct dom_sid
);
799 W_ERROR_HAVE_NO_MEMORY(sid
);
800 status
= dsdb_get_extended_dn_sid(dn
, sid
, "SID");
801 if (!NT_STATUS_IS_OK(status
)) {
802 return WERR_INTERNAL_DB_CORRUPTION
;
813 return an array of SIDs from a ldb_message given an attribute name
814 assumes the SIDs are in NDR form
816 static WERROR
samdb_result_sid_array_ndr(struct ldb_context
*sam_ctx
,
817 struct ldb_message
*msg
,
820 const struct dom_sid
***sids
)
822 struct ldb_message_element
*el
;
825 el
= ldb_msg_find_element(msg
, attr
);
831 (*sids
) = talloc_array(mem_ctx
, const struct dom_sid
*, el
->num_values
+ 1);
832 W_ERROR_HAVE_NO_MEMORY(*sids
);
834 for (i
=0; i
<el
->num_values
; i
++) {
835 enum ndr_err_code ndr_err
;
838 sid
= talloc(*sids
, struct dom_sid
);
839 W_ERROR_HAVE_NO_MEMORY(sid
);
841 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], sid
, sid
,
842 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
843 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
844 return WERR_INTERNAL_DB_CORRUPTION
;
854 see if any SIDs in list1 are in list2
856 static bool sid_list_match(const struct dom_sid
**list1
, const struct dom_sid
**list2
)
859 /* do we ever have enough SIDs here to worry about O(n^2) ? */
860 for (i
=0; list1
[i
]; i
++) {
861 for (j
=0; list2
[j
]; j
++) {
862 if (dom_sid_equal(list1
[i
], list2
[j
])) {
871 handle a DRSUAPI_EXOP_REPL_SECRET call
873 static WERROR
getncchanges_repl_secret(struct drsuapi_bind_state
*b_state
,
875 struct drsuapi_DsGetNCChangesRequest10
*req10
,
876 struct dom_sid
*user_sid
,
877 struct drsuapi_DsGetNCChangesCtr6
*ctr6
)
879 struct drsuapi_DsReplicaObjectIdentifier
*ncRoot
= req10
->naming_context
;
880 struct ldb_dn
*obj_dn
, *rodc_dn
, *krbtgt_link_dn
;
882 const char *rodc_attrs
[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL
};
883 const char *obj_attrs
[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL
};
884 struct ldb_result
*rodc_res
, *obj_res
;
885 const struct dom_sid
**never_reveal_sids
, **reveal_sids
, **token_sids
;
888 DEBUG(3,(__location__
": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
889 drs_ObjectIdentifier_to_string(mem_ctx
, ncRoot
)));
892 * we need to work out if we will allow this RODC to
893 * replicate the secrets for this object
895 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
899 if (b_state
->sam_ctx_system
== NULL
) {
900 /* this operation needs system level access */
901 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_ACCESS_DENIED
;
902 return WERR_DS_DRA_SOURCE_DISABLED
;
905 obj_dn
= drs_ObjectIdentifier_to_dn(mem_ctx
, b_state
->sam_ctx_system
, ncRoot
);
906 if (!ldb_dn_validate(obj_dn
)) goto failed
;
908 rodc_dn
= ldb_dn_new_fmt(mem_ctx
, b_state
->sam_ctx_system
, "<SID=%s>",
909 dom_sid_string(mem_ctx
, user_sid
));
910 if (!ldb_dn_validate(rodc_dn
)) goto failed
;
912 /* do the two searches we need */
913 ret
= dsdb_search_dn(b_state
->sam_ctx_system
, mem_ctx
, &rodc_res
, rodc_dn
, rodc_attrs
,
914 DSDB_SEARCH_SHOW_EXTENDED_DN
);
915 if (ret
!= LDB_SUCCESS
|| rodc_res
->count
!= 1) goto failed
;
917 ret
= dsdb_search_dn(b_state
->sam_ctx_system
, mem_ctx
, &obj_res
, obj_dn
, obj_attrs
, 0);
918 if (ret
!= LDB_SUCCESS
|| obj_res
->count
!= 1) goto failed
;
920 /* if the object SID is equal to the user_sid, allow */
921 if (dom_sid_equal(user_sid
,
922 samdb_result_dom_sid(mem_ctx
, obj_res
->msgs
[0], "objectSid"))) {
926 /* an RODC is allowed to get its own krbtgt account secrets */
927 krbtgt_link_dn
= samdb_result_dn(b_state
->sam_ctx_system
, mem_ctx
,
928 rodc_res
->msgs
[0], "msDS-KrbTgtLink", NULL
);
929 if (krbtgt_link_dn
!= NULL
&&
930 ldb_dn_compare(obj_dn
, krbtgt_link_dn
) == 0) {
934 /* but it isn't allowed to get anyone elses krbtgt secrets */
935 if (samdb_result_dn(b_state
->sam_ctx_system
, mem_ctx
,
936 obj_res
->msgs
[0], "msDS-KrbTgtLinkBL", NULL
)) {
940 if (ldb_msg_find_attr_as_uint(obj_res
->msgs
[0],
941 "userAccountControl", 0) &
942 UF_INTERDOMAIN_TRUST_ACCOUNT
) {
946 werr
= samdb_result_sid_array_dn(b_state
->sam_ctx_system
, rodc_res
->msgs
[0],
947 mem_ctx
, "msDS-NeverRevealGroup", &never_reveal_sids
);
948 if (!W_ERROR_IS_OK(werr
)) {
952 werr
= samdb_result_sid_array_dn(b_state
->sam_ctx_system
, rodc_res
->msgs
[0],
953 mem_ctx
, "msDS-RevealOnDemandGroup", &reveal_sids
);
954 if (!W_ERROR_IS_OK(werr
)) {
958 werr
= samdb_result_sid_array_ndr(b_state
->sam_ctx_system
, obj_res
->msgs
[0],
959 mem_ctx
, "tokenGroups", &token_sids
);
960 if (!W_ERROR_IS_OK(werr
) || token_sids
==NULL
) {
964 if (never_reveal_sids
&&
965 sid_list_match(token_sids
, never_reveal_sids
)) {
970 sid_list_match(token_sids
, reveal_sids
)) {
976 DEBUG(2,(__location__
": Denied RODC secret replication for %s by RODC %s\n",
977 ldb_dn_get_linearized(obj_dn
), ldb_dn_get_linearized(rodc_res
->msgs
[0]->dn
)));
978 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_NONE
;
979 return WERR_DS_DRA_ACCESS_DENIED
;
982 DEBUG(2,(__location__
": Allowed RODC secret replication for %s by RODC %s\n",
983 ldb_dn_get_linearized(obj_dn
), ldb_dn_get_linearized(rodc_res
->msgs
[0]->dn
)));
984 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
985 req10
->highwatermark
.highest_usn
= 0;
989 DEBUG(2,(__location__
": Failed RODC secret replication for %s by RODC %s\n",
990 ldb_dn_get_linearized(obj_dn
), dom_sid_string(mem_ctx
, user_sid
)));
991 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_NONE
;
992 return WERR_DS_DRA_BAD_DN
;
997 handle a DRSUAPI_EXOP_REPL_OBJ call
999 static WERROR
getncchanges_repl_obj(struct drsuapi_bind_state
*b_state
,
1000 TALLOC_CTX
*mem_ctx
,
1001 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1002 struct dom_sid
*user_sid
,
1003 struct drsuapi_DsGetNCChangesCtr6
*ctr6
)
1005 struct drsuapi_DsReplicaObjectIdentifier
*ncRoot
= req10
->naming_context
;
1007 DEBUG(3,(__location__
": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1008 drs_ObjectIdentifier_to_string(mem_ctx
, ncRoot
)));
1010 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
1011 req10
->highwatermark
.highest_usn
= 0;
1017 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1018 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1019 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1021 static WERROR
getncchanges_change_master(struct drsuapi_bind_state
*b_state
,
1022 TALLOC_CTX
*mem_ctx
,
1023 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1024 struct drsuapi_DsGetNCChangesCtr6
*ctr6
)
1026 struct ldb_dn
*fsmo_role_dn
, *req_dn
, *ntds_dn
;
1029 struct ldb_context
*ldb
= b_state
->sam_ctx
;
1030 struct ldb_message
*msg
;
1034 - verify that the client dn exists
1035 - verify that we are the current master
1038 req_dn
= drs_ObjectIdentifier_to_dn(mem_ctx
, ldb
, req10
->naming_context
);
1039 if (!ldb_dn_validate(req_dn
)) {
1040 /* that is not a valid dn */
1041 DEBUG(0,(__location__
": FSMO role transfer request for invalid DN %s\n",
1042 drs_ObjectIdentifier_to_string(mem_ctx
, req10
->naming_context
)));
1043 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_MISMATCH
;
1047 /* retrieve the current role owner */
1048 ret
= samdb_reference_dn(ldb
, mem_ctx
, req_dn
, "fSMORoleOwner", &fsmo_role_dn
);
1049 if (ret
!= LDB_SUCCESS
) {
1050 DEBUG(0,(__location__
": Failed to find fSMORoleOwner in context - %s\n",
1051 ldb_errstring(ldb
)));
1052 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
;
1053 return WERR_DS_DRA_INTERNAL_ERROR
;
1056 if (ldb_dn_compare(samdb_ntds_settings_dn(ldb
), fsmo_role_dn
) != 0) {
1057 /* we're not the current owner - go away */
1058 DEBUG(0,(__location__
": FSMO transfer request when not owner\n"));
1059 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
;
1063 /* change the current master */
1064 msg
= ldb_msg_new(ldb
);
1065 W_ERROR_HAVE_NO_MEMORY(msg
);
1066 msg
->dn
= drs_ObjectIdentifier_to_dn(msg
, ldb
, req10
->naming_context
);
1067 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
1069 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1070 ret
= dsdb_find_dn_by_guid(ldb
, msg
, &req10
->destination_dsa_guid
, &ntds_dn
);
1071 if (ret
!= LDB_SUCCESS
) {
1072 DEBUG(0, (__location__
": Unable to find NTDS object for guid %s - %s\n",
1073 GUID_string(mem_ctx
, &req10
->destination_dsa_guid
), ldb_errstring(ldb
)));
1075 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_UNKNOWN_CALLER
;
1079 ret
= ldb_msg_add_string(msg
, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn
));
1082 return WERR_DS_DRA_INTERNAL_ERROR
;
1085 for (i
=0;i
<msg
->num_elements
;i
++) {
1086 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
1089 ret
= ldb_transaction_start(ldb
);
1090 if (ret
!= LDB_SUCCESS
) {
1091 DEBUG(0,(__location__
": Failed transaction start - %s\n",
1092 ldb_errstring(ldb
)));
1093 return WERR_DS_DRA_INTERNAL_ERROR
;
1096 ret
= ldb_modify(ldb
, msg
);
1097 if (ret
!= LDB_SUCCESS
) {
1098 DEBUG(0,(__location__
": Failed to change current owner - %s\n",
1099 ldb_errstring(ldb
)));
1100 ldb_transaction_cancel(ldb
);
1101 return WERR_DS_DRA_INTERNAL_ERROR
;
1104 ret
= ldb_transaction_commit(ldb
);
1105 if (ret
!= LDB_SUCCESS
) {
1106 DEBUG(0,(__location__
": Failed transaction commit - %s\n",
1107 ldb_errstring(ldb
)));
1108 return WERR_DS_DRA_INTERNAL_ERROR
;
1111 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
1116 /* state of a partially completed getncchanges call */
1117 struct drsuapi_getncchanges_state
{
1119 uint32_t num_records
;
1120 uint32_t num_processed
;
1121 struct ldb_dn
*ncRoot_dn
;
1124 uint64_t highest_usn
;
1125 struct ldb_dn
*last_dn
;
1126 struct drsuapi_DsReplicaLinkedAttribute
*la_list
;
1130 struct drsuapi_DsReplicaCursorCtrEx
*uptodateness_vector
;
1134 see if this getncchanges request includes a request to reveal secret information
1136 static WERROR
dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state
*b_state
,
1137 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1138 bool *is_secret_request
)
1140 enum drsuapi_DsExtendedOperation exop
;
1142 struct dsdb_schema
*schema
;
1144 *is_secret_request
= true;
1146 exop
= req10
->extended_op
;
1149 case DRSUAPI_EXOP_FSMO_REQ_ROLE
:
1150 case DRSUAPI_EXOP_FSMO_RID_ALLOC
:
1151 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE
:
1152 case DRSUAPI_EXOP_FSMO_REQ_PDC
:
1153 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE
:
1154 /* FSMO exops can reveal secrets */
1155 *is_secret_request
= true;
1157 case DRSUAPI_EXOP_REPL_SECRET
:
1158 case DRSUAPI_EXOP_REPL_OBJ
:
1159 case DRSUAPI_EXOP_NONE
:
1163 if (req10
->replica_flags
& DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING
) {
1164 *is_secret_request
= false;
1168 if (exop
== DRSUAPI_EXOP_REPL_SECRET
||
1169 req10
->partial_attribute_set
== NULL
) {
1170 /* they want secrets */
1171 *is_secret_request
= true;
1175 schema
= dsdb_get_schema(b_state
->sam_ctx
, NULL
);
1177 /* check the attributes they asked for */
1178 for (i
=0; i
<req10
->partial_attribute_set
->num_attids
; i
++) {
1179 const struct dsdb_attribute
*sa
;
1180 sa
= dsdb_attribute_by_attributeID_id(schema
, req10
->partial_attribute_set
->attids
[i
]);
1182 return WERR_DS_DRA_SCHEMA_MISMATCH
;
1184 if (!dsdb_attr_in_rodc_fas(sa
)) {
1185 *is_secret_request
= true;
1190 if (req10
->partial_attribute_set_ex
) {
1191 /* check the extended attributes they asked for */
1192 for (i
=0; i
<req10
->partial_attribute_set_ex
->num_attids
; i
++) {
1193 const struct dsdb_attribute
*sa
;
1194 sa
= dsdb_attribute_by_attributeID_id(schema
, req10
->partial_attribute_set_ex
->attids
[i
]);
1196 return WERR_DS_DRA_SCHEMA_MISMATCH
;
1198 if (!dsdb_attr_in_rodc_fas(sa
)) {
1199 *is_secret_request
= true;
1205 *is_secret_request
= false;
1210 see if this getncchanges request is only for attributes in the GC
1211 partial attribute set
1213 static WERROR
dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state
*b_state
,
1214 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1215 bool *is_gc_pas_request
)
1217 enum drsuapi_DsExtendedOperation exop
;
1219 struct dsdb_schema
*schema
;
1221 exop
= req10
->extended_op
;
1224 case DRSUAPI_EXOP_FSMO_REQ_ROLE
:
1225 case DRSUAPI_EXOP_FSMO_RID_ALLOC
:
1226 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE
:
1227 case DRSUAPI_EXOP_FSMO_REQ_PDC
:
1228 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE
:
1229 case DRSUAPI_EXOP_REPL_SECRET
:
1230 *is_gc_pas_request
= false;
1232 case DRSUAPI_EXOP_REPL_OBJ
:
1233 case DRSUAPI_EXOP_NONE
:
1237 if (req10
->partial_attribute_set
== NULL
) {
1238 /* they want it all */
1239 *is_gc_pas_request
= false;
1243 schema
= dsdb_get_schema(b_state
->sam_ctx
, NULL
);
1245 /* check the attributes they asked for */
1246 for (i
=0; i
<req10
->partial_attribute_set
->num_attids
; i
++) {
1247 const struct dsdb_attribute
*sa
;
1248 sa
= dsdb_attribute_by_attributeID_id(schema
, req10
->partial_attribute_set
->attids
[i
]);
1250 return WERR_DS_DRA_SCHEMA_MISMATCH
;
1252 if (!sa
->isMemberOfPartialAttributeSet
) {
1253 *is_gc_pas_request
= false;
1258 if (req10
->partial_attribute_set_ex
) {
1259 /* check the extended attributes they asked for */
1260 for (i
=0; i
<req10
->partial_attribute_set_ex
->num_attids
; i
++) {
1261 const struct dsdb_attribute
*sa
;
1262 sa
= dsdb_attribute_by_attributeID_id(schema
, req10
->partial_attribute_set_ex
->attids
[i
]);
1264 return WERR_DS_DRA_SCHEMA_MISMATCH
;
1266 if (!sa
->isMemberOfPartialAttributeSet
) {
1267 *is_gc_pas_request
= false;
1273 *is_gc_pas_request
= true;
1279 map from req8 to req10
1281 static struct drsuapi_DsGetNCChangesRequest10
*
1282 getncchanges_map_req8(TALLOC_CTX
*mem_ctx
,
1283 struct drsuapi_DsGetNCChangesRequest8
*req8
)
1285 struct drsuapi_DsGetNCChangesRequest10
*req10
= talloc_zero(mem_ctx
,
1286 struct drsuapi_DsGetNCChangesRequest10
);
1287 if (req10
== NULL
) {
1291 req10
->destination_dsa_guid
= req8
->destination_dsa_guid
;
1292 req10
->source_dsa_invocation_id
= req8
->source_dsa_invocation_id
;
1293 req10
->naming_context
= req8
->naming_context
;
1294 req10
->highwatermark
= req8
->highwatermark
;
1295 req10
->uptodateness_vector
= req8
->uptodateness_vector
;
1296 req10
->replica_flags
= req8
->replica_flags
;
1297 req10
->max_object_count
= req8
->max_object_count
;
1298 req10
->max_ndr_size
= req8
->max_ndr_size
;
1299 req10
->extended_op
= req8
->extended_op
;
1300 req10
->fsmo_info
= req8
->fsmo_info
;
1301 req10
->partial_attribute_set
= req8
->partial_attribute_set
;
1302 req10
->partial_attribute_set_ex
= req8
->partial_attribute_set_ex
;
1303 req10
->mapping_ctr
= req8
->mapping_ctr
;
1310 * Collects object for normal replication cycle.
1312 static WERROR
getncchanges_collect_objects(struct drsuapi_bind_state
*b_state
,
1313 TALLOC_CTX
*mem_ctx
,
1314 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1315 struct ldb_dn
*search_dn
,
1316 const char *extra_filter
,
1317 struct ldb_result
**search_res
)
1320 char* search_filter
;
1321 enum ldb_scope scope
= LDB_SCOPE_SUBTREE
;
1322 //const char *extra_filter;
1323 struct drsuapi_getncchanges_state
*getnc_state
= b_state
->getncchanges_state
;
1324 const char *attrs
[] = { "uSNChanged",
1328 if (req10
->extended_op
== DRSUAPI_EXOP_REPL_OBJ
||
1329 req10
->extended_op
== DRSUAPI_EXOP_REPL_SECRET
) {
1330 scope
= LDB_SCOPE_BASE
;
1333 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1335 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1337 /* Construct response. */
1338 search_filter
= talloc_asprintf(mem_ctx
,
1339 "(uSNChanged>=%llu)",
1340 (unsigned long long)(getnc_state
->min_usn
+1));
1343 search_filter
= talloc_asprintf(mem_ctx
, "(&%s(%s))", search_filter
, extra_filter
);
1346 if (req10
->replica_flags
& DRSUAPI_DRS_CRITICAL_ONLY
) {
1347 search_filter
= talloc_asprintf(mem_ctx
,
1348 "(&%s(isCriticalSystemObject=TRUE))",
1352 if (req10
->replica_flags
& DRSUAPI_DRS_ASYNC_REP
) {
1353 scope
= LDB_SCOPE_BASE
;
1357 search_dn
= getnc_state
->ncRoot_dn
;
1360 DEBUG(2,(__location__
": getncchanges on %s using filter %s\n",
1361 ldb_dn_get_linearized(getnc_state
->ncRoot_dn
), search_filter
));
1362 ret
= drsuapi_search_with_extended_dn(b_state
->sam_ctx
, getnc_state
, search_res
,
1363 search_dn
, scope
, attrs
,
1365 if (ret
!= LDB_SUCCESS
) {
1366 return WERR_DS_DRA_INTERNAL_ERROR
;
1373 * Collects object for normal replication cycle.
1375 static WERROR
getncchanges_collect_objects_exop(struct drsuapi_bind_state
*b_state
,
1376 TALLOC_CTX
*mem_ctx
,
1377 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1378 struct drsuapi_DsGetNCChangesCtr6
*ctr6
,
1379 struct ldb_dn
*search_dn
,
1380 const char *extra_filter
,
1381 struct ldb_result
**search_res
)
1383 /* we have nothing to do in case of ex-op failure */
1384 if (ctr6
->extended_ret
!= DRSUAPI_EXOP_ERR_SUCCESS
) {
1388 /* TODO: implement extended op specific collection
1389 * of objects. Right now we just normal procedure
1390 * for collecting objects */
1391 return getncchanges_collect_objects(b_state
, mem_ctx
, req10
, search_dn
, extra_filter
, search_res
);
1395 drsuapi_DsGetNCChanges
1397 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1399 WERROR
dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
,
1400 struct drsuapi_DsGetNCChanges
*r
)
1402 struct drsuapi_DsReplicaObjectIdentifier
*ncRoot
;
1405 struct dsdb_schema
*schema
;
1406 struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
;
1407 struct drsuapi_DsReplicaObjectListItemEx
**currentObject
;
1409 DATA_BLOB session_key
;
1411 struct dcesrv_handle
*h
;
1412 struct drsuapi_bind_state
*b_state
;
1413 struct drsuapi_getncchanges_state
*getnc_state
;
1414 struct drsuapi_DsGetNCChangesRequest10
*req10
;
1416 uint32_t max_objects
;
1418 uint32_t link_count
= 0;
1419 uint32_t link_total
= 0;
1420 uint32_t link_given
= 0;
1421 struct ldb_dn
*search_dn
= NULL
;
1422 bool am_rodc
, null_scope
=false;
1423 enum security_user_level security_level
;
1424 struct ldb_context
*sam_ctx
;
1425 struct dom_sid
*user_sid
;
1426 bool is_secret_request
;
1427 bool is_gc_pas_request
;
1428 struct drsuapi_changed_objects
*changes
;
1430 DCESRV_PULL_HANDLE_WERR(h
, r
->in
.bind_handle
, DRSUAPI_BIND_HANDLE
);
1433 sam_ctx
= b_state
->sam_ctx_system
?b_state
->sam_ctx_system
:b_state
->sam_ctx
;
1435 *r
->out
.level_out
= 6;
1436 /* TODO: linked attributes*/
1437 r
->out
.ctr
->ctr6
.linked_attributes_count
= 0;
1438 r
->out
.ctr
->ctr6
.linked_attributes
= NULL
;
1440 r
->out
.ctr
->ctr6
.object_count
= 0;
1441 r
->out
.ctr
->ctr6
.nc_object_count
= 0;
1442 r
->out
.ctr
->ctr6
.more_data
= false;
1443 r
->out
.ctr
->ctr6
.uptodateness_vector
= NULL
;
1445 /* a RODC doesn't allow for any replication */
1446 ret
= samdb_rodc(sam_ctx
, &am_rodc
);
1447 if (ret
== LDB_SUCCESS
&& am_rodc
) {
1448 DEBUG(0,(__location__
": DsGetNCChanges attempt on RODC\n"));
1449 return WERR_DS_DRA_SOURCE_DISABLED
;
1452 /* Check request revision.
1454 switch (r
->in
.level
) {
1456 req10
= getncchanges_map_req8(mem_ctx
, &r
->in
.req
->req8
);
1457 if (req10
== NULL
) {
1462 req10
= &r
->in
.req
->req10
;
1465 DEBUG(0,(__location__
": Request for DsGetNCChanges with unsupported level %u\n",
1467 return WERR_REVISION_MISMATCH
;
1471 /* Perform access checks. */
1472 /* TODO: we need to support a sync on a specific non-root
1473 * DN. We'll need to find the real partition root here */
1474 ncRoot
= req10
->naming_context
;
1475 if (ncRoot
== NULL
) {
1476 DEBUG(0,(__location__
": Request for DsGetNCChanges with no NC\n"));
1477 return WERR_DS_DRA_INVALID_PARAMETER
;
1480 if (samdb_ntds_options(sam_ctx
, &options
) != LDB_SUCCESS
) {
1481 return WERR_DS_DRA_INTERNAL_ERROR
;
1484 if ((options
& DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL
) &&
1485 !(req10
->replica_flags
& DRSUAPI_DRS_SYNC_FORCED
)) {
1486 return WERR_DS_DRA_SOURCE_DISABLED
;
1489 user_sid
= &dce_call
->conn
->auth_state
.session_info
->security_token
->sids
[PRIMARY_USER_SID_INDEX
];
1491 /* all clients must have GUID_DRS_GET_CHANGES */
1492 werr
= drs_security_access_check_nc_root(b_state
->sam_ctx
,
1494 dce_call
->conn
->auth_state
.session_info
->security_token
,
1495 req10
->naming_context
,
1496 GUID_DRS_GET_CHANGES
);
1497 if (!W_ERROR_IS_OK(werr
)) {
1501 /* allowed if the GC PAS and client has
1502 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1503 werr
= dcesrv_drsuapi_is_gc_pas_request(b_state
, req10
, &is_gc_pas_request
);
1504 if (!W_ERROR_IS_OK(werr
)) {
1507 if (is_gc_pas_request
) {
1508 werr
= drs_security_access_check_nc_root(b_state
->sam_ctx
,
1510 dce_call
->conn
->auth_state
.session_info
->security_token
,
1511 req10
->naming_context
,
1512 GUID_DRS_GET_FILTERED_ATTRIBUTES
);
1513 if (W_ERROR_IS_OK(werr
)) {
1518 werr
= dcesrv_drsuapi_is_reveal_secrets_request(b_state
, req10
, &is_secret_request
);
1519 if (!W_ERROR_IS_OK(werr
)) {
1522 if (is_secret_request
&& req10
->extended_op
!= DRSUAPI_EXOP_REPL_SECRET
) {
1523 werr
= drs_security_access_check_nc_root(b_state
->sam_ctx
,
1525 dce_call
->conn
->auth_state
.session_info
->security_token
,
1526 req10
->naming_context
,
1527 GUID_DRS_GET_ALL_CHANGES
);
1528 if (!W_ERROR_IS_OK(werr
)) {
1534 /* for non-administrator replications, check that they have
1535 given the correct source_dsa_invocation_id */
1536 security_level
= security_session_user_level(dce_call
->conn
->auth_state
.session_info
,
1537 samdb_domain_sid(sam_ctx
));
1538 if (security_level
== SECURITY_RO_DOMAIN_CONTROLLER
) {
1539 if (req10
->replica_flags
& DRSUAPI_DRS_WRIT_REP
) {
1540 /* we rely on this flag being unset for RODC requests */
1541 req10
->replica_flags
&= ~DRSUAPI_DRS_WRIT_REP
;
1545 if (req10
->replica_flags
& DRSUAPI_DRS_FULL_SYNC_PACKET
) {
1546 /* Ignore the _in_ uptpdateness vector*/
1547 req10
->uptodateness_vector
= NULL
;
1550 getnc_state
= b_state
->getncchanges_state
;
1552 /* see if a previous replication has been abandoned */
1554 struct ldb_dn
*new_dn
= drs_ObjectIdentifier_to_dn(getnc_state
, sam_ctx
, ncRoot
);
1555 if (ldb_dn_compare(new_dn
, getnc_state
->ncRoot_dn
) != 0) {
1556 DEBUG(0,(__location__
": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1557 ldb_dn_get_linearized(new_dn
),
1558 ldb_dn_get_linearized(getnc_state
->ncRoot_dn
),
1559 ldb_dn_get_linearized(getnc_state
->last_dn
)));
1560 talloc_free(getnc_state
);
1565 if (getnc_state
== NULL
) {
1566 getnc_state
= talloc_zero(b_state
, struct drsuapi_getncchanges_state
);
1567 if (getnc_state
== NULL
) {
1570 b_state
->getncchanges_state
= getnc_state
;
1571 getnc_state
->ncRoot_dn
= drs_ObjectIdentifier_to_dn(getnc_state
, sam_ctx
, ncRoot
);
1573 /* find out if we are to replicate Schema NC */
1574 ret
= ldb_dn_compare(getnc_state
->ncRoot_dn
,
1575 ldb_get_schema_basedn(b_state
->sam_ctx
));
1576 getnc_state
->is_schema_nc
= (0 == ret
);
1578 if (req10
->extended_op
!= DRSUAPI_EXOP_NONE
) {
1579 r
->out
.ctr
->ctr6
.extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
1583 * This is the first replication cycle and it is
1584 * a good place to handle extended operations
1586 * FIXME: we don't fully support extended operations yet
1588 switch (req10
->extended_op
) {
1589 case DRSUAPI_EXOP_NONE
:
1591 case DRSUAPI_EXOP_FSMO_RID_ALLOC
:
1592 werr
= getncchanges_rid_alloc(b_state
, mem_ctx
, req10
, &r
->out
.ctr
->ctr6
);
1593 W_ERROR_NOT_OK_RETURN(werr
);
1594 search_dn
= ldb_get_default_basedn(sam_ctx
);
1596 case DRSUAPI_EXOP_REPL_SECRET
:
1597 werr
= getncchanges_repl_secret(b_state
, mem_ctx
, req10
, user_sid
, &r
->out
.ctr
->ctr6
);
1598 r
->out
.result
= werr
;
1599 W_ERROR_NOT_OK_RETURN(werr
);
1601 case DRSUAPI_EXOP_FSMO_REQ_ROLE
:
1602 werr
= getncchanges_change_master(b_state
, mem_ctx
, req10
, &r
->out
.ctr
->ctr6
);
1603 W_ERROR_NOT_OK_RETURN(werr
);
1605 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE
:
1606 werr
= getncchanges_change_master(b_state
, mem_ctx
, req10
, &r
->out
.ctr
->ctr6
);
1607 W_ERROR_NOT_OK_RETURN(werr
);
1609 case DRSUAPI_EXOP_FSMO_REQ_PDC
:
1610 werr
= getncchanges_change_master(b_state
, mem_ctx
, req10
, &r
->out
.ctr
->ctr6
);
1611 W_ERROR_NOT_OK_RETURN(werr
);
1613 case DRSUAPI_EXOP_REPL_OBJ
:
1614 werr
= getncchanges_repl_obj(b_state
, mem_ctx
, req10
, user_sid
, &r
->out
.ctr
->ctr6
);
1615 r
->out
.result
= werr
;
1616 W_ERROR_NOT_OK_RETURN(werr
);
1619 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE
:
1621 DEBUG(0,(__location__
": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1622 (unsigned)req10
->extended_op
));
1623 return WERR_DS_DRA_NOT_SUPPORTED
;
1627 if (!ldb_dn_validate(getnc_state
->ncRoot_dn
) ||
1628 ldb_dn_is_null(getnc_state
->ncRoot_dn
)) {
1629 DEBUG(0,(__location__
": Bad DN '%s'\n",
1630 drs_ObjectIdentifier_to_string(mem_ctx
, ncRoot
)));
1631 return WERR_DS_DRA_INVALID_PARAMETER
;
1634 /* we need the session key for encrypting password attributes */
1635 status
= dcesrv_inherited_session_key(dce_call
->conn
, &session_key
);
1636 if (!NT_STATUS_IS_OK(status
)) {
1637 DEBUG(0,(__location__
": Failed to get session key\n"));
1638 return WERR_DS_DRA_INTERNAL_ERROR
;
1642 TODO: MS-DRSR section 4.1.10.1.1
1643 Work out if this is the start of a new cycle */
1645 if (getnc_state
->guids
== NULL
) {
1646 const char *extra_filter
;
1647 struct ldb_result
*search_res
= NULL
;
1649 extra_filter
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "drs", "object filter");
1651 getnc_state
->min_usn
= req10
->highwatermark
.highest_usn
;
1653 if (req10
->extended_op
== DRSUAPI_EXOP_NONE
) {
1654 werr
= getncchanges_collect_objects(b_state
, mem_ctx
, req10
,
1655 search_dn
, extra_filter
,
1658 werr
= getncchanges_collect_objects_exop(b_state
, mem_ctx
, req10
,
1660 search_dn
, extra_filter
,
1663 W_ERROR_NOT_OK_RETURN(werr
);
1665 /* extract out the GUIDs list */
1666 getnc_state
->num_records
= search_res
? search_res
->count
: 0;
1667 getnc_state
->guids
= talloc_array(getnc_state
, struct GUID
, getnc_state
->num_records
);
1668 W_ERROR_HAVE_NO_MEMORY(getnc_state
->guids
);
1670 changes
= talloc_array(getnc_state
,
1671 struct drsuapi_changed_objects
,
1672 getnc_state
->num_records
);
1673 W_ERROR_HAVE_NO_MEMORY(changes
);
1675 for (i
=0; i
<getnc_state
->num_records
; i
++) {
1676 changes
[i
].dn
= search_res
->msgs
[i
]->dn
;
1677 changes
[i
].guid
= samdb_result_guid(search_res
->msgs
[i
], "objectGUID");
1678 changes
[i
].usn
= ldb_msg_find_attr_as_uint64(search_res
->msgs
[i
], "uSNChanged", 0);
1681 if (req10
->replica_flags
& DRSUAPI_DRS_GET_ANC
) {
1682 TYPESAFE_QSORT(changes
,
1683 getnc_state
->num_records
,
1684 site_res_cmp_parent_order
);
1686 TYPESAFE_QSORT(changes
,
1687 getnc_state
->num_records
,
1688 site_res_cmp_dn_usn_order
);
1691 getnc_state
->uptodateness_vector
= talloc_steal(getnc_state
, req10
->uptodateness_vector
);
1692 if (getnc_state
->uptodateness_vector
) {
1693 /* make sure its sorted */
1694 TYPESAFE_QSORT(getnc_state
->uptodateness_vector
->cursors
,
1695 getnc_state
->uptodateness_vector
->count
,
1696 drsuapi_DsReplicaCursor_compare
);
1699 for (i
=0; i
< getnc_state
->num_records
; i
++) {
1700 getnc_state
->guids
[i
] = changes
[i
].guid
;
1701 if (GUID_all_zero(&getnc_state
->guids
[i
])) {
1702 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1703 ldb_dn_get_linearized(search_res
->msgs
[i
]->dn
)));
1704 return WERR_DS_DRA_INTERNAL_ERROR
;
1708 talloc_free(search_res
);
1709 talloc_free(changes
);
1712 /* Prefix mapping */
1713 schema
= dsdb_get_schema(sam_ctx
, mem_ctx
);
1715 DEBUG(0,("No schema in sam_ctx\n"));
1716 return WERR_DS_DRA_INTERNAL_ERROR
;
1719 r
->out
.ctr
->ctr6
.naming_context
= talloc(mem_ctx
, struct drsuapi_DsReplicaObjectIdentifier
);
1720 *r
->out
.ctr
->ctr6
.naming_context
= *ncRoot
;
1722 if (dsdb_find_guid_by_dn(sam_ctx
, getnc_state
->ncRoot_dn
,
1723 &r
->out
.ctr
->ctr6
.naming_context
->guid
) != LDB_SUCCESS
) {
1724 DEBUG(0,(__location__
": Failed to find GUID of ncRoot_dn %s\n",
1725 ldb_dn_get_linearized(getnc_state
->ncRoot_dn
)));
1726 return WERR_DS_DRA_INTERNAL_ERROR
;
1729 /* find the SID if there is one */
1730 dsdb_find_sid_by_dn(sam_ctx
, getnc_state
->ncRoot_dn
, &r
->out
.ctr
->ctr6
.naming_context
->sid
);
1732 dsdb_get_oid_mappings_drsuapi(schema
, true, mem_ctx
, &ctr
);
1733 r
->out
.ctr
->ctr6
.mapping_ctr
= *ctr
;
1735 r
->out
.ctr
->ctr6
.source_dsa_guid
= *(samdb_ntds_objectGUID(sam_ctx
));
1736 r
->out
.ctr
->ctr6
.source_dsa_invocation_id
= *(samdb_ntds_invocation_id(sam_ctx
));
1738 r
->out
.ctr
->ctr6
.old_highwatermark
= req10
->highwatermark
;
1739 r
->out
.ctr
->ctr6
.new_highwatermark
= req10
->highwatermark
;
1741 r
->out
.ctr
->ctr6
.first_object
= NULL
;
1742 currentObject
= &r
->out
.ctr
->ctr6
.first_object
;
1744 /* use this to force single objects at a time, which is useful
1745 * for working out what object is giving problems
1747 max_objects
= lpcfg_parm_int(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "drs", "max object sync", 1000);
1748 if (req10
->max_object_count
< max_objects
) {
1749 max_objects
= req10
->max_object_count
;
1752 * TODO: work out how the maximum should be calculated
1754 max_links
= lpcfg_parm_int(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "drs", "max link sync", 1500);
1756 for (i
=getnc_state
->num_processed
;
1757 i
<getnc_state
->num_records
&&
1759 (r
->out
.ctr
->ctr6
.object_count
< max_objects
);
1762 struct drsuapi_DsReplicaObjectListItemEx
*obj
;
1763 struct ldb_message
*msg
;
1764 static const char * const msg_attrs
[] = {
1766 "nTSecurityDescriptor",
1768 "replPropertyMetaData",
1769 DSDB_SECRET_ATTRIBUTES
,
1771 struct ldb_result
*msg_res
;
1772 struct ldb_dn
*msg_dn
;
1774 obj
= talloc_zero(mem_ctx
, struct drsuapi_DsReplicaObjectListItemEx
);
1775 W_ERROR_HAVE_NO_MEMORY(obj
);
1777 msg_dn
= ldb_dn_new_fmt(obj
, sam_ctx
, "<GUID=%s>", GUID_string(obj
, &getnc_state
->guids
[i
]));
1778 W_ERROR_HAVE_NO_MEMORY(msg_dn
);
1781 /* by re-searching here we avoid having a lot of full
1782 * records in memory between calls to getncchanges
1784 ret
= drsuapi_search_with_extended_dn(sam_ctx
, obj
, &msg_res
,
1786 LDB_SCOPE_BASE
, msg_attrs
, NULL
);
1787 if (ret
!= LDB_SUCCESS
) {
1788 if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
1789 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1790 ldb_dn_get_extended_linearized(obj
, msg_dn
, 1), ldb_errstring(sam_ctx
)));
1796 msg
= msg_res
->msgs
[0];
1798 werr
= get_nc_changes_build_object(obj
, msg
,
1799 sam_ctx
, getnc_state
->ncRoot_dn
,
1800 getnc_state
->is_schema_nc
,
1801 schema
, &session_key
, getnc_state
->min_usn
,
1802 req10
->replica_flags
,
1803 req10
->partial_attribute_set
,
1804 getnc_state
->uptodateness_vector
,
1805 req10
->extended_op
);
1806 if (!W_ERROR_IS_OK(werr
)) {
1810 werr
= get_nc_changes_add_links(sam_ctx
, getnc_state
,
1811 getnc_state
->ncRoot_dn
,
1812 schema
, getnc_state
->min_usn
,
1813 req10
->replica_flags
,
1815 &getnc_state
->la_list
,
1816 &getnc_state
->la_count
,
1817 getnc_state
->uptodateness_vector
);
1818 if (!W_ERROR_IS_OK(werr
)) {
1822 uSN
= ldb_msg_find_attr_as_int(msg
, "uSNChanged", -1);
1823 if (uSN
> r
->out
.ctr
->ctr6
.new_highwatermark
.tmp_highest_usn
) {
1824 r
->out
.ctr
->ctr6
.new_highwatermark
.tmp_highest_usn
= uSN
;
1826 if (uSN
> getnc_state
->highest_usn
) {
1827 getnc_state
->highest_usn
= uSN
;
1830 if (obj
->meta_data_ctr
== NULL
) {
1831 DEBUG(8,(__location__
": getncchanges skipping send of object %s\n",
1832 ldb_dn_get_linearized(msg
->dn
)));
1833 /* no attributes to send */
1838 r
->out
.ctr
->ctr6
.object_count
++;
1840 *currentObject
= obj
;
1841 currentObject
= &obj
->next_object
;
1843 talloc_free(getnc_state
->last_dn
);
1844 getnc_state
->last_dn
= ldb_dn_copy(getnc_state
, msg
->dn
);
1846 DEBUG(8,(__location__
": replicating object %s\n", ldb_dn_get_linearized(msg
->dn
)));
1848 talloc_free(msg_res
);
1849 talloc_free(msg_dn
);
1852 getnc_state
->num_processed
= i
;
1854 r
->out
.ctr
->ctr6
.nc_object_count
= getnc_state
->num_records
;
1856 /* the client can us to call UpdateRefs on its behalf to
1857 re-establish monitoring of the NC */
1858 if ((req10
->replica_flags
& (DRSUAPI_DRS_ADD_REF
| DRSUAPI_DRS_REF_GCSPN
)) &&
1859 !GUID_all_zero(&req10
->destination_dsa_guid
)) {
1860 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq
;
1861 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1862 GUID_string(mem_ctx
, &req10
->destination_dsa_guid
)));
1863 ureq
.naming_context
= ncRoot
;
1864 ureq
.dest_dsa_dns_name
= samdb_ntds_msdcs_dns_name(b_state
->sam_ctx
, mem_ctx
,
1865 &req10
->destination_dsa_guid
);
1866 if (!ureq
.dest_dsa_dns_name
) {
1869 ureq
.dest_dsa_guid
= req10
->destination_dsa_guid
;
1870 ureq
.options
= DRSUAPI_DRS_ADD_REF
|
1871 DRSUAPI_DRS_ASYNC_OP
|
1872 DRSUAPI_DRS_GETCHG_CHECK
;
1874 /* we also need to pass through the
1875 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1876 to send notifies using the GC SPN */
1877 ureq
.options
|= (req10
->replica_flags
& DRSUAPI_DRS_REF_GCSPN
);
1879 werr
= drsuapi_UpdateRefs(b_state
, mem_ctx
, &ureq
);
1880 if (!W_ERROR_IS_OK(werr
)) {
1881 DEBUG(0,(__location__
": Failed UpdateRefs in DsGetNCChanges - %s\n",
1888 * This is just a guess, how to calculate the
1889 * number of linked attributes to send, we need to
1890 * find out how to do this right.
1892 if (r
->out
.ctr
->ctr6
.object_count
>= max_links
) {
1895 max_links
-= r
->out
.ctr
->ctr6
.object_count
;
1898 link_total
= getnc_state
->la_count
;
1900 if (i
< getnc_state
->num_records
) {
1901 r
->out
.ctr
->ctr6
.more_data
= true;
1903 /* sort the whole array the first time */
1904 if (!getnc_state
->la_sorted
) {
1905 LDB_TYPESAFE_QSORT(getnc_state
->la_list
, getnc_state
->la_count
,
1906 sam_ctx
, linked_attribute_compare
);
1907 getnc_state
->la_sorted
= true;
1910 link_count
= getnc_state
->la_count
- getnc_state
->la_idx
;
1911 link_count
= MIN(max_links
, link_count
);
1913 r
->out
.ctr
->ctr6
.linked_attributes_count
= link_count
;
1914 r
->out
.ctr
->ctr6
.linked_attributes
= getnc_state
->la_list
+ getnc_state
->la_idx
;
1916 getnc_state
->la_idx
+= link_count
;
1917 link_given
= getnc_state
->la_idx
;
1919 if (getnc_state
->la_idx
< getnc_state
->la_count
) {
1920 r
->out
.ctr
->ctr6
.more_data
= true;
1924 if (!r
->out
.ctr
->ctr6
.more_data
) {
1925 talloc_steal(mem_ctx
, getnc_state
->la_list
);
1927 r
->out
.ctr
->ctr6
.uptodateness_vector
= talloc(mem_ctx
, struct drsuapi_DsReplicaCursor2CtrEx
);
1928 r
->out
.ctr
->ctr6
.new_highwatermark
.highest_usn
= r
->out
.ctr
->ctr6
.new_highwatermark
.tmp_highest_usn
;
1930 werr
= get_nc_changes_udv(sam_ctx
, getnc_state
->ncRoot_dn
,
1931 r
->out
.ctr
->ctr6
.uptodateness_vector
);
1932 if (!W_ERROR_IS_OK(werr
)) {
1936 talloc_free(getnc_state
);
1937 b_state
->getncchanges_state
= NULL
;
1940 if (req10
->extended_op
!= DRSUAPI_EXOP_NONE
) {
1941 r
->out
.ctr
->ctr6
.uptodateness_vector
= NULL
;
1942 r
->out
.ctr
->ctr6
.nc_object_count
= 0;
1943 ZERO_STRUCT(r
->out
.ctr
->ctr6
.new_highwatermark
);
1946 DEBUG(r
->out
.ctr
->ctr6
.more_data
?4:2,
1947 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1948 (unsigned long long)(req10
->highwatermark
.highest_usn
+1),
1949 req10
->replica_flags
, drs_ObjectIdentifier_to_string(mem_ctx
, ncRoot
),
1950 r
->out
.ctr
->ctr6
.object_count
,
1951 i
, r
->out
.ctr
->ctr6
.more_data
?getnc_state
->num_records
:i
,
1952 r
->out
.ctr
->ctr6
.linked_attributes_count
,
1953 link_given
, link_total
,
1954 dom_sid_string(mem_ctx
, user_sid
)));
1957 if (!r
->out
.ctr
->ctr6
.more_data
&& req10
->extended_op
!= DRSUAPI_EXOP_NONE
) {
1958 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges
, NDR_BOTH
, r
);