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"
40 /* state of a partially completed getncchanges call */
41 struct drsuapi_getncchanges_state
{
44 uint32_t num_processed
;
45 struct ldb_dn
*ncRoot_dn
;
48 struct ldb_dn
*last_dn
;
49 struct drsuapi_DsReplicaLinkedAttribute
*la_list
;
53 struct drsuapi_DsReplicaCursorCtrEx
*uptodateness_vector
;
57 build a DsReplicaObjectIdentifier from a ldb msg
59 static struct drsuapi_DsReplicaObjectIdentifier
*get_object_identifier(TALLOC_CTX
*mem_ctx
,
60 struct ldb_message
*msg
)
62 struct drsuapi_DsReplicaObjectIdentifier
*identifier
;
65 identifier
= talloc(mem_ctx
, struct drsuapi_DsReplicaObjectIdentifier
);
66 if (identifier
== NULL
) {
70 identifier
->dn
= ldb_dn_alloc_linearized(identifier
, msg
->dn
);
71 identifier
->guid
= samdb_result_guid(msg
, "objectGUID");
73 sid
= samdb_result_dom_sid(identifier
, msg
, "objectSid");
75 identifier
->sid
= *sid
;
77 ZERO_STRUCT(identifier
->sid
);
82 static int udv_compare(const struct GUID
*guid1
, struct GUID guid2
)
84 return GUID_compare(guid1
, &guid2
);
88 see if we can filter an attribute using the uptodateness_vector
90 static bool udv_filter(const struct drsuapi_DsReplicaCursorCtrEx
*udv
,
91 const struct GUID
*originating_invocation_id
,
92 uint64_t originating_usn
)
94 const struct drsuapi_DsReplicaCursor
*c
;
95 if (udv
== NULL
) return false;
96 BINARY_ARRAY_SEARCH(udv
->cursors
, udv
->count
, source_dsa_invocation_id
,
97 originating_invocation_id
, udv_compare
, c
);
98 if (c
&& originating_usn
<= c
->highest_usn
) {
105 static int attid_cmp(enum drsuapi_DsAttributeId a1
, enum drsuapi_DsAttributeId a2
)
107 if (a1
== a2
) return 0;
108 return ((uint32_t)a1
) > ((uint32_t)a2
) ? 1 : -1;
112 check if an attribute is in a partial_attribute_set
114 static bool check_partial_attribute_set(const struct dsdb_attribute
*sa
,
115 struct drsuapi_DsPartialAttributeSet
*pas
)
117 enum drsuapi_DsAttributeId
*result
;
118 BINARY_ARRAY_SEARCH_V(pas
->attids
, pas
->num_attids
, (enum drsuapi_DsAttributeId
)sa
->attributeID_id
,
120 return result
!= NULL
;
125 drsuapi_DsGetNCChanges for one object
127 static WERROR
get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx
*obj
,
128 struct ldb_message
*msg
,
129 struct ldb_context
*sam_ctx
,
130 struct ldb_dn
*ncRoot_dn
,
132 struct dsdb_schema
*schema
,
133 DATA_BLOB
*session_key
,
134 uint64_t highest_usn
,
135 uint32_t replica_flags
,
136 struct drsuapi_DsPartialAttributeSet
*partial_attribute_set
,
137 struct drsuapi_DsReplicaCursorCtrEx
*uptodateness_vector
,
138 enum drsuapi_DsExtendedOperation extended_op
,
139 bool force_object_return
)
141 const struct ldb_val
*md_value
;
143 struct replPropertyMetaDataBlob md
;
145 enum ndr_err_code ndr_err
;
148 const struct dsdb_attribute
*rdn_sa
;
149 unsigned int instanceType
;
150 struct dsdb_syntax_ctx syntax_ctx
;
152 /* make dsdb sytanx context for conversions */
153 dsdb_syntax_ctx_init(&syntax_ctx
, sam_ctx
, schema
);
154 syntax_ctx
.is_schema_nc
= is_schema_nc
;
156 instanceType
= ldb_msg_find_attr_as_uint(msg
, "instanceType", 0);
157 if (instanceType
& INSTANCE_TYPE_IS_NC_HEAD
) {
158 obj
->is_nc_prefix
= true;
159 obj
->parent_object_guid
= NULL
;
161 obj
->is_nc_prefix
= false;
162 obj
->parent_object_guid
= talloc(obj
, struct GUID
);
163 if (obj
->parent_object_guid
== NULL
) {
164 return WERR_DS_DRA_INTERNAL_ERROR
;
166 *obj
->parent_object_guid
= samdb_result_guid(msg
, "parentGUID");
167 if (GUID_all_zero(obj
->parent_object_guid
)) {
168 DEBUG(0,(__location__
": missing parentGUID for %s\n",
169 ldb_dn_get_linearized(msg
->dn
)));
170 return WERR_DS_DRA_INTERNAL_ERROR
;
173 obj
->next_object
= NULL
;
175 md_value
= ldb_msg_find_ldb_val(msg
, "replPropertyMetaData");
177 /* nothing to send */
181 if (instanceType
& INSTANCE_TYPE_UNINSTANT
) {
182 /* don't send uninstantiated objects */
186 ndr_err
= ndr_pull_struct_blob(md_value
, obj
, &md
,
187 (ndr_pull_flags_fn_t
)ndr_pull_replPropertyMetaDataBlob
);
188 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
189 return WERR_DS_DRA_INTERNAL_ERROR
;
192 if (md
.version
!= 1) {
193 return WERR_DS_DRA_INTERNAL_ERROR
;
196 rdn
= ldb_dn_get_rdn_name(msg
->dn
);
198 DEBUG(0,(__location__
": No rDN for %s\n", ldb_dn_get_linearized(msg
->dn
)));
199 return WERR_DS_DRA_INTERNAL_ERROR
;
202 rdn_sa
= dsdb_attribute_by_lDAPDisplayName(schema
, rdn
);
203 if (rdn_sa
== NULL
) {
204 DEBUG(0,(__location__
": Can't find dsds_attribute for rDN %s in %s\n",
205 rdn
, ldb_dn_get_linearized(msg
->dn
)));
206 return WERR_DS_DRA_INTERNAL_ERROR
;
209 obj
->meta_data_ctr
= talloc(obj
, struct drsuapi_DsReplicaMetaDataCtr
);
210 attids
= talloc_array(obj
, uint32_t, md
.ctr
.ctr1
.count
);
212 obj
->object
.identifier
= get_object_identifier(obj
, msg
);
213 if (obj
->object
.identifier
== NULL
) {
216 dom_sid_split_rid(NULL
, &obj
->object
.identifier
->sid
, NULL
, &rid
);
218 obj
->meta_data_ctr
->meta_data
= talloc_array(obj
, struct drsuapi_DsReplicaMetaData
, md
.ctr
.ctr1
.count
);
219 for (n
=i
=0; i
<md
.ctr
.ctr1
.count
; i
++) {
220 const struct dsdb_attribute
*sa
;
221 bool force_attribute
= false;
223 /* if the attribute has not changed, and it is not the
224 instanceType then don't include it */
225 if (md
.ctr
.ctr1
.array
[i
].local_usn
< highest_usn
&&
226 extended_op
!= DRSUAPI_EXOP_REPL_SECRET
&&
227 md
.ctr
.ctr1
.array
[i
].attid
!= DRSUAPI_ATTID_instanceType
) continue;
229 /* don't include the rDN */
230 if (md
.ctr
.ctr1
.array
[i
].attid
== rdn_sa
->attributeID_id
) continue;
232 sa
= dsdb_attribute_by_attributeID_id(schema
, md
.ctr
.ctr1
.array
[i
].attid
);
234 DEBUG(0,(__location__
": Failed to find attribute in schema for attrid %u mentioned in replPropertyMetaData of %s\n",
235 (unsigned int)md
.ctr
.ctr1
.array
[i
].attid
,
236 ldb_dn_get_linearized(msg
->dn
)));
237 return WERR_DS_DRA_INTERNAL_ERROR
;
241 struct ldb_message_element
*el
;
242 el
= ldb_msg_find_element(msg
, sa
->lDAPDisplayName
);
243 if (el
&& el
->num_values
&& dsdb_dn_is_upgraded_link_val(&el
->values
[0])) {
244 /* don't send upgraded links inline */
249 if (extended_op
== DRSUAPI_EXOP_REPL_SECRET
&&
250 !dsdb_attr_in_rodc_fas(sa
)) {
251 force_attribute
= true;
252 DEBUG(4,("Forcing attribute %s in %s\n",
253 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
256 /* filter by uptodateness_vector */
257 if (md
.ctr
.ctr1
.array
[i
].attid
!= DRSUAPI_ATTID_instanceType
&&
259 udv_filter(uptodateness_vector
,
260 &md
.ctr
.ctr1
.array
[i
].originating_invocation_id
,
261 md
.ctr
.ctr1
.array
[i
].originating_usn
)) {
265 /* filter by partial_attribute_set */
266 if (partial_attribute_set
&& !check_partial_attribute_set(sa
, partial_attribute_set
)) {
270 obj
->meta_data_ctr
->meta_data
[n
].originating_change_time
= md
.ctr
.ctr1
.array
[i
].originating_change_time
;
271 obj
->meta_data_ctr
->meta_data
[n
].version
= md
.ctr
.ctr1
.array
[i
].version
;
272 obj
->meta_data_ctr
->meta_data
[n
].originating_invocation_id
= md
.ctr
.ctr1
.array
[i
].originating_invocation_id
;
273 obj
->meta_data_ctr
->meta_data
[n
].originating_usn
= md
.ctr
.ctr1
.array
[i
].originating_usn
;
274 attids
[n
] = md
.ctr
.ctr1
.array
[i
].attid
;
278 /* ignore it if its an empty change. Note that renames always
279 * change the 'name' attribute, so they won't be ignored by
282 * the force_object_return check is used to force an empty
283 * object return when we timeout in the getncchanges loop.
284 * This allows us to return an empty object, which keeps the
285 * client happy while preventing timeouts
289 attids
[0] == DRSUAPI_ATTID_instanceType
&&
290 !force_object_return
)) {
291 talloc_free(obj
->meta_data_ctr
);
292 obj
->meta_data_ctr
= NULL
;
296 obj
->meta_data_ctr
->count
= n
;
298 obj
->object
.flags
= DRSUAPI_DS_REPLICA_OBJECT_FROM_MASTER
;
299 obj
->object
.attribute_ctr
.num_attributes
= obj
->meta_data_ctr
->count
;
300 obj
->object
.attribute_ctr
.attributes
= talloc_array(obj
, struct drsuapi_DsReplicaAttribute
,
301 obj
->object
.attribute_ctr
.num_attributes
);
302 if (obj
->object
.attribute_ctr
.attributes
== NULL
) {
307 * Note that the meta_data array and the attributes array must
308 * be the same size and in the same order
310 for (i
=0; i
<obj
->object
.attribute_ctr
.num_attributes
; i
++) {
311 struct ldb_message_element
*el
;
313 const struct dsdb_attribute
*sa
;
315 sa
= dsdb_attribute_by_attributeID_id(schema
, attids
[i
]);
317 DEBUG(0,("Unable to find attributeID %u in schema\n", attids
[i
]));
318 return WERR_DS_DRA_INTERNAL_ERROR
;
321 el
= ldb_msg_find_element(msg
, sa
->lDAPDisplayName
);
323 /* this happens for attributes that have been removed */
324 DEBUG(5,("No element '%s' for attributeID %u in message\n",
325 sa
->lDAPDisplayName
, attids
[i
]));
326 ZERO_STRUCT(obj
->object
.attribute_ctr
.attributes
[i
]);
327 obj
->object
.attribute_ctr
.attributes
[i
].attid
=
328 dsdb_attribute_get_attid(sa
, syntax_ctx
.is_schema_nc
);
330 werr
= sa
->syntax
->ldb_to_drsuapi(&syntax_ctx
, sa
, el
, obj
,
331 &obj
->object
.attribute_ctr
.attributes
[i
]);
332 if (!W_ERROR_IS_OK(werr
)) {
333 DEBUG(0,("Unable to convert %s to DRS object - %s\n",
334 sa
->lDAPDisplayName
, win_errstr(werr
)));
337 /* if DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING is set
338 * check if attribute is secret and send a null value
340 if (replica_flags
& DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING
) {
341 drsuapi_process_secret_attribute(&obj
->object
.attribute_ctr
.attributes
[i
],
342 &obj
->meta_data_ctr
->meta_data
[i
]);
344 /* some attributes needs to be encrypted
346 werr
= drsuapi_encrypt_attribute(obj
, session_key
, rid
,
347 &obj
->object
.attribute_ctr
.attributes
[i
]);
348 if (!W_ERROR_IS_OK(werr
)) {
349 DEBUG(0,("Unable to encrypt %s in DRS object - %s\n",
350 sa
->lDAPDisplayName
, win_errstr(werr
)));
360 add one linked attribute from an object to the list of linked
361 attributes in a getncchanges request
363 static WERROR
get_nc_changes_add_la(TALLOC_CTX
*mem_ctx
,
364 struct ldb_context
*sam_ctx
,
365 const struct dsdb_schema
*schema
,
366 const struct dsdb_attribute
*sa
,
367 struct ldb_message
*msg
,
368 struct dsdb_dn
*dsdb_dn
,
369 struct drsuapi_DsReplicaLinkedAttribute
**la_list
,
372 struct drsuapi_DsReplicaLinkedAttribute
*la
;
377 (*la_list
) = talloc_realloc(mem_ctx
, *la_list
, struct drsuapi_DsReplicaLinkedAttribute
, (*la_count
)+1);
378 W_ERROR_HAVE_NO_MEMORY(*la_list
);
380 la
= &(*la_list
)[*la_count
];
382 la
->identifier
= get_object_identifier(*la_list
, msg
);
383 W_ERROR_HAVE_NO_MEMORY(la
->identifier
);
385 active
= (dsdb_dn_rmd_flags(dsdb_dn
->dn
) & DSDB_RMD_FLAG_DELETED
) == 0;
388 /* We have to check that the inactive link still point to an existing object */
394 v
= ldb_msg_find_attr_as_string(msg
, "isDeleted", "FALSE");
395 if (strncmp(v
, "TRUE", 4) == 0) {
397 * Note: we skip the transmition of the deleted link even if the other part used to
398 * know about it because when we transmit the deletion of the object, the link will
399 * be deleted too due to deletion of object where link points and Windows do so.
401 if (dsdb_functional_level(sam_ctx
) >= DS_DOMAIN_FUNCTION_2008_R2
) {
402 v
= ldb_msg_find_attr_as_string(msg
, "isRecycled", "FALSE");
404 * On Windows 2008R2 isRecycled is always present even if FL or DL are < FL 2K8R2
405 * if it join an existing domain with deleted objets, it firsts impose to have a
406 * schema with the is-Recycled object and for all deleted objects it adds the isRecycled
407 * either during initial replication or after the getNCChanges.
408 * Behavior of samba has been changed to always have this attribute if it's present in the schema.
410 * So if FL <2K8R2 isRecycled might be here or not but we don't care, it's meaning less.
411 * If FL >=2K8R2 we are sure that this attribute will be here.
412 * For this kind of forest level we do not return the link if the object is recycled
413 * (isRecycled = true).
415 if (strncmp(v
, "TRUE", 4) == 0) {
416 DEBUG(2, (" object %s is recycled, not returning linked attribute !\n",
417 ldb_dn_get_linearized(msg
->dn
)));
424 status
= dsdb_get_extended_dn_guid(dsdb_dn
->dn
, &guid
, "GUID");
425 if (!NT_STATUS_IS_OK(status
)) {
426 DEBUG(0,(__location__
" Unable to extract GUID in linked attribute '%s' in '%s'\n",
427 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
428 return ntstatus_to_werror(status
);
430 ret
= dsdb_find_dn_by_guid(sam_ctx
, mem_ctx
, &guid
, &tdn
);
431 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
432 DEBUG(2, (" Search of guid %s returned 0 objects, skipping it !\n",
433 GUID_string(mem_ctx
, &guid
)));
435 } else if (ret
!= LDB_SUCCESS
) {
436 DEBUG(0, (__location__
" Search of guid %s failed with error code %d\n",
437 GUID_string(mem_ctx
, &guid
),
442 la
->attid
= sa
->attributeID_id
;
443 la
->flags
= active
?DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
:0;
445 status
= dsdb_get_extended_dn_uint32(dsdb_dn
->dn
, &la
->meta_data
.version
, "RMD_VERSION");
446 if (!NT_STATUS_IS_OK(status
)) {
447 DEBUG(0,(__location__
" No RMD_VERSION in linked attribute '%s' in '%s'\n",
448 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
449 return ntstatus_to_werror(status
);
451 status
= dsdb_get_extended_dn_nttime(dsdb_dn
->dn
, &la
->meta_data
.originating_change_time
, "RMD_CHANGETIME");
452 if (!NT_STATUS_IS_OK(status
)) {
453 DEBUG(0,(__location__
" No RMD_CHANGETIME in linked attribute '%s' in '%s'\n",
454 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
455 return ntstatus_to_werror(status
);
457 status
= dsdb_get_extended_dn_guid(dsdb_dn
->dn
, &la
->meta_data
.originating_invocation_id
, "RMD_INVOCID");
458 if (!NT_STATUS_IS_OK(status
)) {
459 DEBUG(0,(__location__
" No RMD_INVOCID in linked attribute '%s' in '%s'\n",
460 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
461 return ntstatus_to_werror(status
);
463 status
= dsdb_get_extended_dn_uint64(dsdb_dn
->dn
, &la
->meta_data
.originating_usn
, "RMD_ORIGINATING_USN");
464 if (!NT_STATUS_IS_OK(status
)) {
465 DEBUG(0,(__location__
" No RMD_ORIGINATING_USN in linked attribute '%s' in '%s'\n",
466 sa
->lDAPDisplayName
, ldb_dn_get_linearized(msg
->dn
)));
467 return ntstatus_to_werror(status
);
470 status
= dsdb_get_extended_dn_nttime(dsdb_dn
->dn
, &la
->originating_add_time
, "RMD_ADDTIME");
471 if (!NT_STATUS_IS_OK(status
)) {
472 /* this is possible for upgraded links */
473 la
->originating_add_time
= la
->meta_data
.originating_change_time
;
476 werr
= dsdb_dn_la_to_blob(sam_ctx
, sa
, schema
, *la_list
, dsdb_dn
, &la
->value
.blob
);
477 W_ERROR_NOT_OK_RETURN(werr
);
485 add linked attributes from an object to the list of linked
486 attributes in a getncchanges request
488 static WERROR
get_nc_changes_add_links(struct ldb_context
*sam_ctx
,
490 struct ldb_dn
*ncRoot_dn
,
491 struct dsdb_schema
*schema
,
492 uint64_t highest_usn
,
493 uint32_t replica_flags
,
494 struct ldb_message
*msg
,
495 struct drsuapi_DsReplicaLinkedAttribute
**la_list
,
497 struct drsuapi_DsReplicaCursorCtrEx
*uptodateness_vector
)
500 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
501 uint64_t uSNChanged
= ldb_msg_find_attr_as_int(msg
, "uSNChanged", -1);
503 for (i
=0; i
<msg
->num_elements
; i
++) {
504 struct ldb_message_element
*el
= &msg
->elements
[i
];
505 const struct dsdb_attribute
*sa
;
508 sa
= dsdb_attribute_by_lDAPDisplayName(schema
, el
->name
);
510 if (!sa
|| sa
->linkID
== 0 || (sa
->linkID
& 1)) {
511 /* we only want forward links */
515 if (el
->num_values
&& !dsdb_dn_is_upgraded_link_val(&el
->values
[0])) {
516 /* its an old style link, it will have been
517 * sent in the main replication data */
521 for (j
=0; j
<el
->num_values
; j
++) {
522 struct dsdb_dn
*dsdb_dn
;
527 dsdb_dn
= dsdb_dn_parse(tmp_ctx
, sam_ctx
, &el
->values
[j
], sa
->syntax
->ldap_oid
);
528 if (dsdb_dn
== NULL
) {
529 DEBUG(1,(__location__
": Failed to parse DN for %s in %s\n",
530 el
->name
, ldb_dn_get_linearized(msg
->dn
)));
531 talloc_free(tmp_ctx
);
532 return WERR_DS_DRA_INTERNAL_ERROR
;
535 status
= dsdb_get_extended_dn_uint64(dsdb_dn
->dn
, &local_usn
, "RMD_LOCAL_USN");
536 if (!NT_STATUS_IS_OK(status
)) {
537 /* this can happen for attributes
538 given to us with old style meta
543 if (local_usn
> uSNChanged
) {
544 DEBUG(1,(__location__
": uSNChanged less than RMD_LOCAL_USN for %s on %s\n",
545 el
->name
, ldb_dn_get_linearized(msg
->dn
)));
546 talloc_free(tmp_ctx
);
547 return WERR_DS_DRA_INTERNAL_ERROR
;
550 if (local_usn
< highest_usn
) {
554 werr
= get_nc_changes_add_la(mem_ctx
, sam_ctx
, schema
, sa
, msg
,
555 dsdb_dn
, la_list
, la_count
);
556 if (!W_ERROR_IS_OK(werr
)) {
557 talloc_free(tmp_ctx
);
563 talloc_free(tmp_ctx
);
568 fill in the cursors return based on the replUpToDateVector for the ncRoot_dn
570 static WERROR
get_nc_changes_udv(struct ldb_context
*sam_ctx
,
571 struct ldb_dn
*ncRoot_dn
,
572 struct drsuapi_DsReplicaCursor2CtrEx
*udv
)
580 ret
= dsdb_load_udv_v2(sam_ctx
, ncRoot_dn
, udv
, &udv
->cursors
, &udv
->count
);
581 if (ret
!= LDB_SUCCESS
) {
582 DEBUG(0,(__location__
": Failed to load UDV for %s - %s\n",
583 ldb_dn_get_linearized(ncRoot_dn
), ldb_errstring(sam_ctx
)));
584 return WERR_DS_DRA_INTERNAL_ERROR
;
591 /* comparison function for linked attributes - see CompareLinks() in
592 * MS-DRSR section 4.1.10.5.17 */
593 static int linked_attribute_compare(const struct drsuapi_DsReplicaLinkedAttribute
*la1
,
594 const struct drsuapi_DsReplicaLinkedAttribute
*la2
,
595 struct ldb_context
*sam_ctx
)
600 const struct dsdb_schema
*schema
;
601 const struct dsdb_attribute
*schema_attrib
;
602 struct dsdb_dn
*dn1
, *dn2
;
603 struct GUID guid1
, guid2
;
606 c
= GUID_compare(&la1
->identifier
->guid
,
607 &la2
->identifier
->guid
);
608 if (c
!= 0) return c
;
610 if (la1
->attid
!= la2
->attid
) {
611 return la1
->attid
< la2
->attid
? -1:1;
614 if ((la1
->flags
& DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
) !=
615 (la2
->flags
& DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
)) {
616 return (la1
->flags
& DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
)? 1:-1;
619 /* we need to get the target GUIDs to compare */
620 tmp_ctx
= talloc_new(sam_ctx
);
622 schema
= dsdb_get_schema(sam_ctx
, tmp_ctx
);
623 schema_attrib
= dsdb_attribute_by_attributeID_id(schema
, la1
->attid
);
625 werr
= dsdb_dn_la_from_blob(sam_ctx
, schema_attrib
, schema
, tmp_ctx
, la1
->value
.blob
, &dn1
);
626 if (!W_ERROR_IS_OK(werr
)) {
627 DEBUG(0,(__location__
": Bad la1 blob in sort\n"));
628 talloc_free(tmp_ctx
);
632 werr
= dsdb_dn_la_from_blob(sam_ctx
, schema_attrib
, schema
, tmp_ctx
, la2
->value
.blob
, &dn2
);
633 if (!W_ERROR_IS_OK(werr
)) {
634 DEBUG(0,(__location__
": Bad la2 blob in sort\n"));
635 talloc_free(tmp_ctx
);
639 status
= dsdb_get_extended_dn_guid(dn1
->dn
, &guid1
, "GUID");
640 if (!NT_STATUS_IS_OK(status
)) {
641 DEBUG(0,(__location__
": Bad la1 guid in sort\n"));
642 talloc_free(tmp_ctx
);
645 status
= dsdb_get_extended_dn_guid(dn2
->dn
, &guid2
, "GUID");
646 if (!NT_STATUS_IS_OK(status
)) {
647 DEBUG(0,(__location__
": Bad la2 guid in sort\n"));
648 talloc_free(tmp_ctx
);
652 talloc_free(tmp_ctx
);
654 return GUID_compare(&guid1
, &guid2
);
657 struct drsuapi_changed_objects
{
664 sort the objects we send by tree order
666 static int site_res_cmp_parent_order(struct drsuapi_changed_objects
*m1
,
667 struct drsuapi_changed_objects
*m2
)
669 return ldb_dn_compare(m2
->dn
, m1
->dn
);
673 sort the objects we send first by uSNChanged
675 static int site_res_cmp_dn_usn_order(struct drsuapi_changed_objects
*m1
,
676 struct drsuapi_changed_objects
*m2
)
678 unsigned usnchanged1
, usnchanged2
;
681 cn1
= ldb_dn_get_comp_num(m1
->dn
);
682 cn2
= ldb_dn_get_comp_num(m2
->dn
);
684 return cn1
> cn2
? 1 : -1;
686 usnchanged1
= m1
->usn
;
687 usnchanged2
= m2
->usn
;
688 if (usnchanged1
== usnchanged2
) {
691 return usnchanged1
> usnchanged2
? 1 : -1;
696 handle a DRSUAPI_EXOP_FSMO_RID_ALLOC call
698 static WERROR
getncchanges_rid_alloc(struct drsuapi_bind_state
*b_state
,
700 struct drsuapi_DsGetNCChangesRequest10
*req10
,
701 struct drsuapi_DsGetNCChangesCtr6
*ctr6
)
703 struct ldb_dn
*rid_manager_dn
, *req_dn
;
705 struct ldb_context
*ldb
= b_state
->sam_ctx
;
706 struct ldb_result
*ext_res
;
707 struct dsdb_fsmo_extended_op
*exop
;
712 - verify that the DN being asked for is the RID Manager DN
713 - verify that we are the RID Manager
716 /* work out who is the RID Manager */
717 ret
= samdb_rid_manager_dn(ldb
, mem_ctx
, &rid_manager_dn
);
718 if (ret
!= LDB_SUCCESS
) {
719 DEBUG(0, (__location__
": Failed to find RID Manager object - %s\n", ldb_errstring(ldb
)));
720 return WERR_DS_DRA_INTERNAL_ERROR
;
723 req_dn
= drs_ObjectIdentifier_to_dn(mem_ctx
, ldb
, req10
->naming_context
);
724 if (!ldb_dn_validate(req_dn
) ||
725 ldb_dn_compare(req_dn
, rid_manager_dn
) != 0) {
726 /* that isn't the RID Manager DN */
727 DEBUG(0,(__location__
": RID Alloc request for wrong DN %s\n",
728 drs_ObjectIdentifier_to_string(mem_ctx
, req10
->naming_context
)));
729 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_MISMATCH
;
733 /* find the DN of the RID Manager */
734 ret
= samdb_reference_dn_is_our_ntdsa(ldb
, rid_manager_dn
, "fSMORoleOwner", &is_us
);
735 if (ret
!= LDB_SUCCESS
) {
736 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
737 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
;
738 return WERR_DS_DRA_INTERNAL_ERROR
;
742 /* we're not the RID Manager - go away */
743 DEBUG(0,(__location__
": RID Alloc request when not RID Manager\n"));
744 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
;
748 exop
= talloc(mem_ctx
, struct dsdb_fsmo_extended_op
);
749 W_ERROR_HAVE_NO_MEMORY(exop
);
751 exop
->fsmo_info
= req10
->fsmo_info
;
752 exop
->destination_dsa_guid
= req10
->destination_dsa_guid
;
754 ret
= ldb_transaction_start(ldb
);
755 if (ret
!= LDB_SUCCESS
) {
756 DEBUG(0,(__location__
": Failed transaction start - %s\n",
757 ldb_errstring(ldb
)));
758 return WERR_DS_DRA_INTERNAL_ERROR
;
762 * FIXME (kim): this is a temp hack to return just few object,
763 * but not the whole domain NC.
764 * We should remove this hack and implement a 'scope'
765 * building function to return just the set of object
766 * documented for DRSUAPI_EXOP_FSMO_RID_ALLOC extended_op
768 ldb_sequence_number(ldb
, LDB_SEQ_HIGHEST_SEQ
, &req10
->highwatermark
.highest_usn
);
770 ret
= ldb_extended(ldb
, DSDB_EXTENDED_ALLOCATE_RID_POOL
, exop
, &ext_res
);
771 if (ret
!= LDB_SUCCESS
) {
772 DEBUG(0,(__location__
": Failed extended allocation RID pool operation - %s\n",
773 ldb_errstring(ldb
)));
774 ldb_transaction_cancel(ldb
);
775 return WERR_DS_DRA_INTERNAL_ERROR
;
778 ret
= ldb_transaction_commit(ldb
);
779 if (ret
!= LDB_SUCCESS
) {
780 DEBUG(0,(__location__
": Failed transaction commit - %s\n",
781 ldb_errstring(ldb
)));
782 return WERR_DS_DRA_INTERNAL_ERROR
;
785 talloc_free(ext_res
);
787 DEBUG(2,("Allocated RID pool for server %s\n",
788 GUID_string(mem_ctx
, &req10
->destination_dsa_guid
)));
790 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
796 return an array of SIDs from a ldb_message given an attribute name
797 assumes the SIDs are in extended DN format
799 static WERROR
samdb_result_sid_array_dn(struct ldb_context
*sam_ctx
,
800 struct ldb_message
*msg
,
803 const struct dom_sid
***sids
)
805 struct ldb_message_element
*el
;
808 el
= ldb_msg_find_element(msg
, attr
);
814 (*sids
) = talloc_array(mem_ctx
, const struct dom_sid
*, el
->num_values
+ 1);
815 W_ERROR_HAVE_NO_MEMORY(*sids
);
817 for (i
=0; i
<el
->num_values
; i
++) {
818 struct ldb_dn
*dn
= ldb_dn_from_ldb_val(mem_ctx
, sam_ctx
, &el
->values
[i
]);
822 sid
= talloc(*sids
, struct dom_sid
);
823 W_ERROR_HAVE_NO_MEMORY(sid
);
824 status
= dsdb_get_extended_dn_sid(dn
, sid
, "SID");
825 if (!NT_STATUS_IS_OK(status
)) {
826 return WERR_INTERNAL_DB_CORRUPTION
;
837 return an array of SIDs from a ldb_message given an attribute name
838 assumes the SIDs are in NDR form
840 static WERROR
samdb_result_sid_array_ndr(struct ldb_context
*sam_ctx
,
841 struct ldb_message
*msg
,
844 const struct dom_sid
***sids
)
846 struct ldb_message_element
*el
;
849 el
= ldb_msg_find_element(msg
, attr
);
855 (*sids
) = talloc_array(mem_ctx
, const struct dom_sid
*, el
->num_values
+ 1);
856 W_ERROR_HAVE_NO_MEMORY(*sids
);
858 for (i
=0; i
<el
->num_values
; i
++) {
859 enum ndr_err_code ndr_err
;
862 sid
= talloc(*sids
, struct dom_sid
);
863 W_ERROR_HAVE_NO_MEMORY(sid
);
865 ndr_err
= ndr_pull_struct_blob(&el
->values
[i
], sid
, sid
,
866 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
867 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
868 return WERR_INTERNAL_DB_CORRUPTION
;
878 see if any SIDs in list1 are in list2
880 static bool sid_list_match(const struct dom_sid
**list1
, const struct dom_sid
**list2
)
883 /* do we ever have enough SIDs here to worry about O(n^2) ? */
884 for (i
=0; list1
[i
]; i
++) {
885 for (j
=0; list2
[j
]; j
++) {
886 if (dom_sid_equal(list1
[i
], list2
[j
])) {
895 handle a DRSUAPI_EXOP_REPL_SECRET call
897 static WERROR
getncchanges_repl_secret(struct drsuapi_bind_state
*b_state
,
899 struct drsuapi_DsGetNCChangesRequest10
*req10
,
900 struct dom_sid
*user_sid
,
901 struct drsuapi_DsGetNCChangesCtr6
*ctr6
,
902 bool has_get_all_changes
)
904 struct drsuapi_DsReplicaObjectIdentifier
*ncRoot
= req10
->naming_context
;
905 struct ldb_dn
*obj_dn
, *rodc_dn
, *krbtgt_link_dn
;
907 const char *rodc_attrs
[] = { "msDS-KrbTgtLink", "msDS-NeverRevealGroup", "msDS-RevealOnDemandGroup", NULL
};
908 const char *obj_attrs
[] = { "tokenGroups", "objectSid", "UserAccountControl", "msDS-KrbTgtLinkBL", NULL
};
909 struct ldb_result
*rodc_res
, *obj_res
;
910 const struct dom_sid
**never_reveal_sids
, **reveal_sids
, **token_sids
;
913 DEBUG(3,(__location__
": DRSUAPI_EXOP_REPL_SECRET extended op on %s\n",
914 drs_ObjectIdentifier_to_string(mem_ctx
, ncRoot
)));
917 * we need to work out if we will allow this DC to
918 * replicate the secrets for this object
920 * see 4.1.10.5.14 GetRevealSecretsPolicyForUser for details
924 if (b_state
->sam_ctx_system
== NULL
) {
925 /* this operation needs system level access */
926 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_ACCESS_DENIED
;
927 return WERR_DS_DRA_SOURCE_DISABLED
;
931 * In MS-DRSR.pdf 5.99 IsGetNCChangesPermissionGranted
933 * The pseudo code indicate
934 * revealsecrets = true
935 * if IsRevealSecretRequest(msgIn) then
936 * if AccessCheckCAR(ncRoot, Ds-Replication-Get-Changes-All) = false
938 * if (msgIn.ulExtendedOp = EXOP_REPL_SECRETS) then
939 * <... check if this account is ok to be replicated on this DC ...>
940 * <... and if not reveal secrets = no ...>
942 * reveal secrets = false
947 * Which basically means that if you have GET_ALL_CHANGES rights (~== RWDC)
948 * then you can do EXOP_REPL_SECRETS
950 if (has_get_all_changes
) {
954 obj_dn
= drs_ObjectIdentifier_to_dn(mem_ctx
, b_state
->sam_ctx_system
, ncRoot
);
955 if (!ldb_dn_validate(obj_dn
)) goto failed
;
957 rodc_dn
= ldb_dn_new_fmt(mem_ctx
, b_state
->sam_ctx_system
, "<SID=%s>",
958 dom_sid_string(mem_ctx
, user_sid
));
959 if (!ldb_dn_validate(rodc_dn
)) goto failed
;
961 /* do the two searches we need */
962 ret
= dsdb_search_dn(b_state
->sam_ctx_system
, mem_ctx
, &rodc_res
, rodc_dn
, rodc_attrs
,
963 DSDB_SEARCH_SHOW_EXTENDED_DN
);
964 if (ret
!= LDB_SUCCESS
|| rodc_res
->count
!= 1) goto failed
;
966 ret
= dsdb_search_dn(b_state
->sam_ctx_system
, mem_ctx
, &obj_res
, obj_dn
, obj_attrs
, 0);
967 if (ret
!= LDB_SUCCESS
|| obj_res
->count
!= 1) goto failed
;
969 /* if the object SID is equal to the user_sid, allow */
970 if (dom_sid_equal(user_sid
,
971 samdb_result_dom_sid(mem_ctx
, obj_res
->msgs
[0], "objectSid"))) {
975 /* an RODC is allowed to get its own krbtgt account secrets */
976 krbtgt_link_dn
= samdb_result_dn(b_state
->sam_ctx_system
, mem_ctx
,
977 rodc_res
->msgs
[0], "msDS-KrbTgtLink", NULL
);
978 if (krbtgt_link_dn
!= NULL
&&
979 ldb_dn_compare(obj_dn
, krbtgt_link_dn
) == 0) {
983 /* but it isn't allowed to get anyone elses krbtgt secrets */
984 if (samdb_result_dn(b_state
->sam_ctx_system
, mem_ctx
,
985 obj_res
->msgs
[0], "msDS-KrbTgtLinkBL", NULL
)) {
989 if (ldb_msg_find_attr_as_uint(obj_res
->msgs
[0],
990 "userAccountControl", 0) &
991 UF_INTERDOMAIN_TRUST_ACCOUNT
) {
995 werr
= samdb_result_sid_array_dn(b_state
->sam_ctx_system
, rodc_res
->msgs
[0],
996 mem_ctx
, "msDS-NeverRevealGroup", &never_reveal_sids
);
997 if (!W_ERROR_IS_OK(werr
)) {
1001 werr
= samdb_result_sid_array_dn(b_state
->sam_ctx_system
, rodc_res
->msgs
[0],
1002 mem_ctx
, "msDS-RevealOnDemandGroup", &reveal_sids
);
1003 if (!W_ERROR_IS_OK(werr
)) {
1007 werr
= samdb_result_sid_array_ndr(b_state
->sam_ctx_system
, obj_res
->msgs
[0],
1008 mem_ctx
, "tokenGroups", &token_sids
);
1009 if (!W_ERROR_IS_OK(werr
) || token_sids
==NULL
) {
1013 if (never_reveal_sids
&&
1014 sid_list_match(token_sids
, never_reveal_sids
)) {
1019 sid_list_match(token_sids
, reveal_sids
)) {
1025 DEBUG(2,(__location__
": Denied single object with secret replication for %s by RODC %s\n",
1026 ldb_dn_get_linearized(obj_dn
), ldb_dn_get_linearized(rodc_res
->msgs
[0]->dn
)));
1027 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_NONE
;
1028 return WERR_DS_DRA_ACCESS_DENIED
;
1031 DEBUG(2,(__location__
": Allowed single object with secret replication for %s by %s %s\n",
1032 ldb_dn_get_linearized(obj_dn
), has_get_all_changes
?"RWDC":"RODC",
1033 ldb_dn_get_linearized(rodc_res
->msgs
[0]->dn
)));
1034 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
1035 req10
->highwatermark
.highest_usn
= 0;
1039 DEBUG(2,(__location__
": Failed single secret replication for %s by RODC %s\n",
1040 ldb_dn_get_linearized(obj_dn
), dom_sid_string(mem_ctx
, user_sid
)));
1041 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_NONE
;
1042 return WERR_DS_DRA_BAD_DN
;
1047 handle a DRSUAPI_EXOP_REPL_OBJ call
1049 static WERROR
getncchanges_repl_obj(struct drsuapi_bind_state
*b_state
,
1050 TALLOC_CTX
*mem_ctx
,
1051 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1052 struct dom_sid
*user_sid
,
1053 struct drsuapi_DsGetNCChangesCtr6
*ctr6
)
1055 struct drsuapi_DsReplicaObjectIdentifier
*ncRoot
= req10
->naming_context
;
1057 DEBUG(3,(__location__
": DRSUAPI_EXOP_REPL_OBJ extended op on %s\n",
1058 drs_ObjectIdentifier_to_string(mem_ctx
, ncRoot
)));
1060 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
1066 handle DRSUAPI_EXOP_FSMO_REQ_ROLE,
1067 DRSUAPI_EXOP_FSMO_RID_REQ_ROLE,
1068 and DRSUAPI_EXOP_FSMO_REQ_PDC calls
1070 static WERROR
getncchanges_change_master(struct drsuapi_bind_state
*b_state
,
1071 TALLOC_CTX
*mem_ctx
,
1072 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1073 struct drsuapi_DsGetNCChangesCtr6
*ctr6
)
1075 struct ldb_dn
*req_dn
, *ntds_dn
;
1078 struct ldb_context
*ldb
= b_state
->sam_ctx
;
1079 struct ldb_message
*msg
;
1084 - verify that the client dn exists
1085 - verify that we are the current master
1088 req_dn
= drs_ObjectIdentifier_to_dn(mem_ctx
, ldb
, req10
->naming_context
);
1089 if (!ldb_dn_validate(req_dn
)) {
1090 /* that is not a valid dn */
1091 DEBUG(0,(__location__
": FSMO role transfer request for invalid DN %s\n",
1092 drs_ObjectIdentifier_to_string(mem_ctx
, req10
->naming_context
)));
1093 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_MISMATCH
;
1097 /* retrieve the current role owner */
1098 /* find the DN of the RID Manager */
1099 ret
= samdb_reference_dn_is_our_ntdsa(ldb
, req_dn
, "fSMORoleOwner", &is_us
);
1100 if (ret
!= LDB_SUCCESS
) {
1101 DEBUG(0,("Failed to find fSMORoleOwner in RID Manager object\n"));
1102 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
;
1103 return WERR_DS_DRA_INTERNAL_ERROR
;
1107 /* we're not the RID Manager - go away */
1108 DEBUG(0,(__location__
": RID Alloc request when not RID Manager\n"));
1109 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_FSMO_NOT_OWNER
;
1113 /* change the current master */
1114 msg
= ldb_msg_new(ldb
);
1115 W_ERROR_HAVE_NO_MEMORY(msg
);
1116 msg
->dn
= drs_ObjectIdentifier_to_dn(msg
, ldb
, req10
->naming_context
);
1117 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
1119 /* TODO: make sure ntds_dn is a valid nTDSDSA object */
1120 ret
= dsdb_find_dn_by_guid(ldb
, msg
, &req10
->destination_dsa_guid
, &ntds_dn
);
1121 if (ret
!= LDB_SUCCESS
) {
1122 DEBUG(0, (__location__
": Unable to find NTDS object for guid %s - %s\n",
1123 GUID_string(mem_ctx
, &req10
->destination_dsa_guid
), ldb_errstring(ldb
)));
1125 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_UNKNOWN_CALLER
;
1129 ret
= ldb_msg_add_string(msg
, "fSMORoleOwner", ldb_dn_get_linearized(ntds_dn
));
1132 return WERR_DS_DRA_INTERNAL_ERROR
;
1135 for (i
=0;i
<msg
->num_elements
;i
++) {
1136 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
1139 ret
= ldb_transaction_start(ldb
);
1140 if (ret
!= LDB_SUCCESS
) {
1141 DEBUG(0,(__location__
": Failed transaction start - %s\n",
1142 ldb_errstring(ldb
)));
1143 return WERR_DS_DRA_INTERNAL_ERROR
;
1146 ret
= ldb_modify(ldb
, msg
);
1147 if (ret
!= LDB_SUCCESS
) {
1148 DEBUG(0,(__location__
": Failed to change current owner - %s\n",
1149 ldb_errstring(ldb
)));
1150 ldb_transaction_cancel(ldb
);
1151 return WERR_DS_DRA_INTERNAL_ERROR
;
1154 ret
= ldb_transaction_commit(ldb
);
1155 if (ret
!= LDB_SUCCESS
) {
1156 DEBUG(0,(__location__
": Failed transaction commit - %s\n",
1157 ldb_errstring(ldb
)));
1158 return WERR_DS_DRA_INTERNAL_ERROR
;
1161 ctr6
->extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
1167 see if this getncchanges request includes a request to reveal secret information
1169 static WERROR
dcesrv_drsuapi_is_reveal_secrets_request(struct drsuapi_bind_state
*b_state
,
1170 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1171 bool *is_secret_request
)
1173 enum drsuapi_DsExtendedOperation exop
;
1175 struct dsdb_schema
*schema
;
1177 *is_secret_request
= true;
1179 exop
= req10
->extended_op
;
1182 case DRSUAPI_EXOP_FSMO_REQ_ROLE
:
1183 case DRSUAPI_EXOP_FSMO_RID_ALLOC
:
1184 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE
:
1185 case DRSUAPI_EXOP_FSMO_REQ_PDC
:
1186 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE
:
1187 /* FSMO exops can reveal secrets */
1188 *is_secret_request
= true;
1190 case DRSUAPI_EXOP_REPL_SECRET
:
1191 case DRSUAPI_EXOP_REPL_OBJ
:
1192 case DRSUAPI_EXOP_NONE
:
1196 if (req10
->replica_flags
& DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING
) {
1197 *is_secret_request
= false;
1201 if (exop
== DRSUAPI_EXOP_REPL_SECRET
||
1202 req10
->partial_attribute_set
== NULL
) {
1203 /* they want secrets */
1204 *is_secret_request
= true;
1208 schema
= dsdb_get_schema(b_state
->sam_ctx
, NULL
);
1210 /* check the attributes they asked for */
1211 for (i
=0; i
<req10
->partial_attribute_set
->num_attids
; i
++) {
1212 const struct dsdb_attribute
*sa
;
1213 sa
= dsdb_attribute_by_attributeID_id(schema
, req10
->partial_attribute_set
->attids
[i
]);
1215 return WERR_DS_DRA_SCHEMA_MISMATCH
;
1217 if (!dsdb_attr_in_rodc_fas(sa
)) {
1218 *is_secret_request
= true;
1223 if (req10
->partial_attribute_set_ex
) {
1224 /* check the extended attributes they asked for */
1225 for (i
=0; i
<req10
->partial_attribute_set_ex
->num_attids
; i
++) {
1226 const struct dsdb_attribute
*sa
;
1227 sa
= dsdb_attribute_by_attributeID_id(schema
, req10
->partial_attribute_set_ex
->attids
[i
]);
1229 return WERR_DS_DRA_SCHEMA_MISMATCH
;
1231 if (!dsdb_attr_in_rodc_fas(sa
)) {
1232 *is_secret_request
= true;
1238 *is_secret_request
= false;
1243 see if this getncchanges request is only for attributes in the GC
1244 partial attribute set
1246 static WERROR
dcesrv_drsuapi_is_gc_pas_request(struct drsuapi_bind_state
*b_state
,
1247 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1248 bool *is_gc_pas_request
)
1250 enum drsuapi_DsExtendedOperation exop
;
1252 struct dsdb_schema
*schema
;
1254 exop
= req10
->extended_op
;
1257 case DRSUAPI_EXOP_FSMO_REQ_ROLE
:
1258 case DRSUAPI_EXOP_FSMO_RID_ALLOC
:
1259 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE
:
1260 case DRSUAPI_EXOP_FSMO_REQ_PDC
:
1261 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE
:
1262 case DRSUAPI_EXOP_REPL_SECRET
:
1263 *is_gc_pas_request
= false;
1265 case DRSUAPI_EXOP_REPL_OBJ
:
1266 case DRSUAPI_EXOP_NONE
:
1270 if (req10
->partial_attribute_set
== NULL
) {
1271 /* they want it all */
1272 *is_gc_pas_request
= false;
1276 schema
= dsdb_get_schema(b_state
->sam_ctx
, NULL
);
1278 /* check the attributes they asked for */
1279 for (i
=0; i
<req10
->partial_attribute_set
->num_attids
; i
++) {
1280 const struct dsdb_attribute
*sa
;
1281 sa
= dsdb_attribute_by_attributeID_id(schema
, req10
->partial_attribute_set
->attids
[i
]);
1283 return WERR_DS_DRA_SCHEMA_MISMATCH
;
1285 if (!sa
->isMemberOfPartialAttributeSet
) {
1286 *is_gc_pas_request
= false;
1291 if (req10
->partial_attribute_set_ex
) {
1292 /* check the extended attributes they asked for */
1293 for (i
=0; i
<req10
->partial_attribute_set_ex
->num_attids
; i
++) {
1294 const struct dsdb_attribute
*sa
;
1295 sa
= dsdb_attribute_by_attributeID_id(schema
, req10
->partial_attribute_set_ex
->attids
[i
]);
1297 return WERR_DS_DRA_SCHEMA_MISMATCH
;
1299 if (!sa
->isMemberOfPartialAttributeSet
) {
1300 *is_gc_pas_request
= false;
1306 *is_gc_pas_request
= true;
1312 map from req8 to req10
1314 static struct drsuapi_DsGetNCChangesRequest10
*
1315 getncchanges_map_req8(TALLOC_CTX
*mem_ctx
,
1316 struct drsuapi_DsGetNCChangesRequest8
*req8
)
1318 struct drsuapi_DsGetNCChangesRequest10
*req10
= talloc_zero(mem_ctx
,
1319 struct drsuapi_DsGetNCChangesRequest10
);
1320 if (req10
== NULL
) {
1324 req10
->destination_dsa_guid
= req8
->destination_dsa_guid
;
1325 req10
->source_dsa_invocation_id
= req8
->source_dsa_invocation_id
;
1326 req10
->naming_context
= req8
->naming_context
;
1327 req10
->highwatermark
= req8
->highwatermark
;
1328 req10
->uptodateness_vector
= req8
->uptodateness_vector
;
1329 req10
->replica_flags
= req8
->replica_flags
;
1330 req10
->max_object_count
= req8
->max_object_count
;
1331 req10
->max_ndr_size
= req8
->max_ndr_size
;
1332 req10
->extended_op
= req8
->extended_op
;
1333 req10
->fsmo_info
= req8
->fsmo_info
;
1334 req10
->partial_attribute_set
= req8
->partial_attribute_set
;
1335 req10
->partial_attribute_set_ex
= req8
->partial_attribute_set_ex
;
1336 req10
->mapping_ctr
= req8
->mapping_ctr
;
1343 * Collects object for normal replication cycle.
1345 static WERROR
getncchanges_collect_objects(struct drsuapi_bind_state
*b_state
,
1346 TALLOC_CTX
*mem_ctx
,
1347 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1348 struct ldb_dn
*search_dn
,
1349 const char *extra_filter
,
1350 struct ldb_result
**search_res
)
1353 char* search_filter
;
1354 enum ldb_scope scope
= LDB_SCOPE_SUBTREE
;
1355 //const char *extra_filter;
1356 struct drsuapi_getncchanges_state
*getnc_state
= b_state
->getncchanges_state
;
1357 const char *attrs
[] = { "uSNChanged",
1361 if (req10
->extended_op
== DRSUAPI_EXOP_REPL_OBJ
||
1362 req10
->extended_op
== DRSUAPI_EXOP_REPL_SECRET
) {
1363 scope
= LDB_SCOPE_BASE
;
1366 //extra_filter = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "drs", "object filter");
1368 //getnc_state->min_usn = req10->highwatermark.highest_usn;
1370 /* Construct response. */
1371 search_filter
= talloc_asprintf(mem_ctx
,
1372 "(uSNChanged>=%llu)",
1373 (unsigned long long)(getnc_state
->min_usn
+1));
1376 search_filter
= talloc_asprintf(mem_ctx
, "(&%s(%s))", search_filter
, extra_filter
);
1379 if (req10
->replica_flags
& DRSUAPI_DRS_CRITICAL_ONLY
) {
1380 search_filter
= talloc_asprintf(mem_ctx
,
1381 "(&%s(isCriticalSystemObject=TRUE))",
1385 if (req10
->replica_flags
& DRSUAPI_DRS_ASYNC_REP
) {
1386 scope
= LDB_SCOPE_BASE
;
1390 search_dn
= getnc_state
->ncRoot_dn
;
1393 DEBUG(2,(__location__
": getncchanges on %s using filter %s\n",
1394 ldb_dn_get_linearized(getnc_state
->ncRoot_dn
), search_filter
));
1395 ret
= drsuapi_search_with_extended_dn(b_state
->sam_ctx
, getnc_state
, search_res
,
1396 search_dn
, scope
, attrs
,
1398 if (ret
!= LDB_SUCCESS
) {
1399 return WERR_DS_DRA_INTERNAL_ERROR
;
1406 * Collects object for normal replication cycle.
1408 static WERROR
getncchanges_collect_objects_exop(struct drsuapi_bind_state
*b_state
,
1409 TALLOC_CTX
*mem_ctx
,
1410 struct drsuapi_DsGetNCChangesRequest10
*req10
,
1411 struct drsuapi_DsGetNCChangesCtr6
*ctr6
,
1412 struct ldb_dn
*search_dn
,
1413 const char *extra_filter
,
1414 struct ldb_result
**search_res
)
1416 /* we have nothing to do in case of ex-op failure */
1417 if (ctr6
->extended_ret
!= DRSUAPI_EXOP_ERR_SUCCESS
) {
1421 /* TODO: implement extended op specific collection
1422 * of objects. Right now we just normal procedure
1423 * for collecting objects */
1424 return getncchanges_collect_objects(b_state
, mem_ctx
, req10
, search_dn
, extra_filter
, search_res
);
1428 drsuapi_DsGetNCChanges
1430 see MS-DRSR 4.1.10.5.2 for basic logic of this function
1432 WERROR
dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
,
1433 struct drsuapi_DsGetNCChanges
*r
)
1435 struct drsuapi_DsReplicaObjectIdentifier
*ncRoot
;
1438 struct dsdb_schema
*schema
;
1439 struct drsuapi_DsReplicaOIDMapping_Ctr
*ctr
;
1440 struct drsuapi_DsReplicaObjectListItemEx
**currentObject
;
1442 DATA_BLOB session_key
;
1444 struct dcesrv_handle
*h
;
1445 struct drsuapi_bind_state
*b_state
;
1446 struct drsuapi_getncchanges_state
*getnc_state
;
1447 struct drsuapi_DsGetNCChangesRequest10
*req10
;
1449 uint32_t max_objects
;
1451 uint32_t link_count
= 0;
1452 uint32_t link_total
= 0;
1453 uint32_t link_given
= 0;
1454 struct ldb_dn
*search_dn
= NULL
;
1455 bool am_rodc
, null_scope
=false;
1456 enum security_user_level security_level
;
1457 struct ldb_context
*sam_ctx
;
1458 struct dom_sid
*user_sid
;
1459 bool is_secret_request
;
1460 bool is_gc_pas_request
;
1461 struct drsuapi_changed_objects
*changes
;
1463 time_t start
= time(NULL
);
1464 bool max_wait_reached
= false;
1465 bool has_get_all_changes
= false;
1467 DCESRV_PULL_HANDLE_WERR(h
, r
->in
.bind_handle
, DRSUAPI_BIND_HANDLE
);
1470 sam_ctx
= b_state
->sam_ctx_system
?b_state
->sam_ctx_system
:b_state
->sam_ctx
;
1472 *r
->out
.level_out
= 6;
1473 /* TODO: linked attributes*/
1474 r
->out
.ctr
->ctr6
.linked_attributes_count
= 0;
1475 r
->out
.ctr
->ctr6
.linked_attributes
= NULL
;
1477 r
->out
.ctr
->ctr6
.object_count
= 0;
1478 r
->out
.ctr
->ctr6
.nc_object_count
= 0;
1479 r
->out
.ctr
->ctr6
.more_data
= false;
1480 r
->out
.ctr
->ctr6
.uptodateness_vector
= NULL
;
1482 /* a RODC doesn't allow for any replication */
1483 ret
= samdb_rodc(sam_ctx
, &am_rodc
);
1484 if (ret
== LDB_SUCCESS
&& am_rodc
) {
1485 DEBUG(0,(__location__
": DsGetNCChanges attempt on RODC\n"));
1486 return WERR_DS_DRA_SOURCE_DISABLED
;
1489 /* Check request revision.
1491 switch (r
->in
.level
) {
1493 req10
= getncchanges_map_req8(mem_ctx
, &r
->in
.req
->req8
);
1494 if (req10
== NULL
) {
1499 req10
= &r
->in
.req
->req10
;
1502 DEBUG(0,(__location__
": Request for DsGetNCChanges with unsupported level %u\n",
1504 return WERR_REVISION_MISMATCH
;
1508 /* Perform access checks. */
1509 /* TODO: we need to support a sync on a specific non-root
1510 * DN. We'll need to find the real partition root here */
1511 ncRoot
= req10
->naming_context
;
1512 if (ncRoot
== NULL
) {
1513 DEBUG(0,(__location__
": Request for DsGetNCChanges with no NC\n"));
1514 return WERR_DS_DRA_INVALID_PARAMETER
;
1517 if (samdb_ntds_options(sam_ctx
, &options
) != LDB_SUCCESS
) {
1518 return WERR_DS_DRA_INTERNAL_ERROR
;
1521 if ((options
& DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL
) &&
1522 !(req10
->replica_flags
& DRSUAPI_DRS_SYNC_FORCED
)) {
1523 return WERR_DS_DRA_SOURCE_DISABLED
;
1526 user_sid
= &dce_call
->conn
->auth_state
.session_info
->security_token
->sids
[PRIMARY_USER_SID_INDEX
];
1528 /* all clients must have GUID_DRS_GET_CHANGES */
1529 werr
= drs_security_access_check_nc_root(b_state
->sam_ctx
,
1531 dce_call
->conn
->auth_state
.session_info
->security_token
,
1532 req10
->naming_context
,
1533 GUID_DRS_GET_CHANGES
);
1534 if (!W_ERROR_IS_OK(werr
)) {
1538 /* allowed if the GC PAS and client has
1539 GUID_DRS_GET_FILTERED_ATTRIBUTES */
1540 werr
= dcesrv_drsuapi_is_gc_pas_request(b_state
, req10
, &is_gc_pas_request
);
1541 if (!W_ERROR_IS_OK(werr
)) {
1544 if (is_gc_pas_request
) {
1545 werr
= drs_security_access_check_nc_root(b_state
->sam_ctx
,
1547 dce_call
->conn
->auth_state
.session_info
->security_token
,
1548 req10
->naming_context
,
1549 GUID_DRS_GET_FILTERED_ATTRIBUTES
);
1550 if (W_ERROR_IS_OK(werr
)) {
1555 werr
= dcesrv_drsuapi_is_reveal_secrets_request(b_state
, req10
, &is_secret_request
);
1556 if (!W_ERROR_IS_OK(werr
)) {
1559 if (is_secret_request
&& req10
->extended_op
!= DRSUAPI_EXOP_REPL_SECRET
) {
1560 werr
= drs_security_access_check_nc_root(b_state
->sam_ctx
,
1562 dce_call
->conn
->auth_state
.session_info
->security_token
,
1563 req10
->naming_context
,
1564 GUID_DRS_GET_ALL_CHANGES
);
1565 if (!W_ERROR_IS_OK(werr
)) {
1568 has_get_all_changes
= true;
1573 /* for non-administrator replications, check that they have
1574 given the correct source_dsa_invocation_id */
1575 security_level
= security_session_user_level(dce_call
->conn
->auth_state
.session_info
,
1576 samdb_domain_sid(sam_ctx
));
1577 if (security_level
== SECURITY_RO_DOMAIN_CONTROLLER
) {
1578 if (req10
->replica_flags
& DRSUAPI_DRS_WRIT_REP
) {
1579 /* we rely on this flag being unset for RODC requests */
1580 req10
->replica_flags
&= ~DRSUAPI_DRS_WRIT_REP
;
1584 if (req10
->replica_flags
& DRSUAPI_DRS_FULL_SYNC_PACKET
) {
1585 /* Ignore the _in_ uptpdateness vector*/
1586 req10
->uptodateness_vector
= NULL
;
1589 getnc_state
= b_state
->getncchanges_state
;
1591 /* see if a previous replication has been abandoned */
1593 struct ldb_dn
*new_dn
= drs_ObjectIdentifier_to_dn(getnc_state
, sam_ctx
, ncRoot
);
1594 if (ldb_dn_compare(new_dn
, getnc_state
->ncRoot_dn
) != 0) {
1595 DEBUG(0,(__location__
": DsGetNCChanges 2nd replication on different DN %s %s (last_dn %s)\n",
1596 ldb_dn_get_linearized(new_dn
),
1597 ldb_dn_get_linearized(getnc_state
->ncRoot_dn
),
1598 ldb_dn_get_linearized(getnc_state
->last_dn
)));
1599 talloc_free(getnc_state
);
1604 if (getnc_state
== NULL
) {
1605 getnc_state
= talloc_zero(b_state
, struct drsuapi_getncchanges_state
);
1606 if (getnc_state
== NULL
) {
1609 b_state
->getncchanges_state
= getnc_state
;
1610 getnc_state
->ncRoot_dn
= drs_ObjectIdentifier_to_dn(getnc_state
, sam_ctx
, ncRoot
);
1612 /* find out if we are to replicate Schema NC */
1613 ret
= ldb_dn_compare(getnc_state
->ncRoot_dn
,
1614 ldb_get_schema_basedn(b_state
->sam_ctx
));
1615 getnc_state
->is_schema_nc
= (0 == ret
);
1617 if (req10
->extended_op
!= DRSUAPI_EXOP_NONE
) {
1618 r
->out
.ctr
->ctr6
.extended_ret
= DRSUAPI_EXOP_ERR_SUCCESS
;
1622 * This is the first replication cycle and it is
1623 * a good place to handle extended operations
1625 * FIXME: we don't fully support extended operations yet
1627 switch (req10
->extended_op
) {
1628 case DRSUAPI_EXOP_NONE
:
1630 case DRSUAPI_EXOP_FSMO_RID_ALLOC
:
1631 werr
= getncchanges_rid_alloc(b_state
, mem_ctx
, req10
, &r
->out
.ctr
->ctr6
);
1632 W_ERROR_NOT_OK_RETURN(werr
);
1633 search_dn
= ldb_get_default_basedn(sam_ctx
);
1635 case DRSUAPI_EXOP_REPL_SECRET
:
1636 werr
= getncchanges_repl_secret(b_state
, mem_ctx
, req10
,
1639 has_get_all_changes
);
1640 r
->out
.result
= werr
;
1641 W_ERROR_NOT_OK_RETURN(werr
);
1643 case DRSUAPI_EXOP_FSMO_REQ_ROLE
:
1644 werr
= getncchanges_change_master(b_state
, mem_ctx
, req10
, &r
->out
.ctr
->ctr6
);
1645 W_ERROR_NOT_OK_RETURN(werr
);
1647 case DRSUAPI_EXOP_FSMO_RID_REQ_ROLE
:
1648 werr
= getncchanges_change_master(b_state
, mem_ctx
, req10
, &r
->out
.ctr
->ctr6
);
1649 W_ERROR_NOT_OK_RETURN(werr
);
1651 case DRSUAPI_EXOP_FSMO_REQ_PDC
:
1652 werr
= getncchanges_change_master(b_state
, mem_ctx
, req10
, &r
->out
.ctr
->ctr6
);
1653 W_ERROR_NOT_OK_RETURN(werr
);
1655 case DRSUAPI_EXOP_REPL_OBJ
:
1656 werr
= getncchanges_repl_obj(b_state
, mem_ctx
, req10
, user_sid
, &r
->out
.ctr
->ctr6
);
1657 r
->out
.result
= werr
;
1658 W_ERROR_NOT_OK_RETURN(werr
);
1661 case DRSUAPI_EXOP_FSMO_ABANDON_ROLE
:
1663 DEBUG(0,(__location__
": Request for DsGetNCChanges unsupported extended op 0x%x\n",
1664 (unsigned)req10
->extended_op
));
1665 return WERR_DS_DRA_NOT_SUPPORTED
;
1669 if (!ldb_dn_validate(getnc_state
->ncRoot_dn
) ||
1670 ldb_dn_is_null(getnc_state
->ncRoot_dn
)) {
1671 DEBUG(0,(__location__
": Bad DN '%s'\n",
1672 drs_ObjectIdentifier_to_string(mem_ctx
, ncRoot
)));
1673 return WERR_DS_DRA_INVALID_PARAMETER
;
1676 /* we need the session key for encrypting password attributes */
1677 status
= dcesrv_inherited_session_key(dce_call
->conn
, &session_key
);
1678 if (!NT_STATUS_IS_OK(status
)) {
1679 DEBUG(0,(__location__
": Failed to get session key\n"));
1680 return WERR_DS_DRA_INTERNAL_ERROR
;
1684 TODO: MS-DRSR section 4.1.10.1.1
1685 Work out if this is the start of a new cycle */
1687 if (getnc_state
->guids
== NULL
) {
1688 const char *extra_filter
;
1689 struct ldb_result
*search_res
= NULL
;
1691 extra_filter
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "drs", "object filter");
1693 getnc_state
->min_usn
= req10
->highwatermark
.highest_usn
;
1695 if (req10
->extended_op
== DRSUAPI_EXOP_NONE
) {
1696 werr
= getncchanges_collect_objects(b_state
, mem_ctx
, req10
,
1697 search_dn
, extra_filter
,
1700 werr
= getncchanges_collect_objects_exop(b_state
, mem_ctx
, req10
,
1702 search_dn
, extra_filter
,
1705 W_ERROR_NOT_OK_RETURN(werr
);
1707 /* extract out the GUIDs list */
1708 getnc_state
->num_records
= search_res
? search_res
->count
: 0;
1709 getnc_state
->guids
= talloc_array(getnc_state
, struct GUID
, getnc_state
->num_records
);
1710 W_ERROR_HAVE_NO_MEMORY(getnc_state
->guids
);
1712 changes
= talloc_array(getnc_state
,
1713 struct drsuapi_changed_objects
,
1714 getnc_state
->num_records
);
1715 W_ERROR_HAVE_NO_MEMORY(changes
);
1717 for (i
=0; i
<getnc_state
->num_records
; i
++) {
1718 changes
[i
].dn
= search_res
->msgs
[i
]->dn
;
1719 changes
[i
].guid
= samdb_result_guid(search_res
->msgs
[i
], "objectGUID");
1720 changes
[i
].usn
= ldb_msg_find_attr_as_uint64(search_res
->msgs
[i
], "uSNChanged", 0);
1723 if (req10
->replica_flags
& DRSUAPI_DRS_GET_ANC
) {
1724 TYPESAFE_QSORT(changes
,
1725 getnc_state
->num_records
,
1726 site_res_cmp_parent_order
);
1728 TYPESAFE_QSORT(changes
,
1729 getnc_state
->num_records
,
1730 site_res_cmp_dn_usn_order
);
1733 getnc_state
->uptodateness_vector
= talloc_steal(getnc_state
, req10
->uptodateness_vector
);
1734 if (getnc_state
->uptodateness_vector
) {
1735 /* make sure its sorted */
1736 TYPESAFE_QSORT(getnc_state
->uptodateness_vector
->cursors
,
1737 getnc_state
->uptodateness_vector
->count
,
1738 drsuapi_DsReplicaCursor_compare
);
1741 for (i
=0; i
< getnc_state
->num_records
; i
++) {
1742 getnc_state
->guids
[i
] = changes
[i
].guid
;
1743 if (GUID_all_zero(&getnc_state
->guids
[i
])) {
1744 DEBUG(2,("getncchanges: bad objectGUID from %s\n",
1745 ldb_dn_get_linearized(search_res
->msgs
[i
]->dn
)));
1746 return WERR_DS_DRA_INTERNAL_ERROR
;
1750 talloc_free(search_res
);
1751 talloc_free(changes
);
1754 /* Prefix mapping */
1755 schema
= dsdb_get_schema(sam_ctx
, mem_ctx
);
1757 DEBUG(0,("No schema in sam_ctx\n"));
1758 return WERR_DS_DRA_INTERNAL_ERROR
;
1761 r
->out
.ctr
->ctr6
.naming_context
= talloc(mem_ctx
, struct drsuapi_DsReplicaObjectIdentifier
);
1762 *r
->out
.ctr
->ctr6
.naming_context
= *ncRoot
;
1764 if (dsdb_find_guid_by_dn(sam_ctx
, getnc_state
->ncRoot_dn
,
1765 &r
->out
.ctr
->ctr6
.naming_context
->guid
) != LDB_SUCCESS
) {
1766 DEBUG(0,(__location__
": Failed to find GUID of ncRoot_dn %s\n",
1767 ldb_dn_get_linearized(getnc_state
->ncRoot_dn
)));
1768 return WERR_DS_DRA_INTERNAL_ERROR
;
1771 /* find the SID if there is one */
1772 dsdb_find_sid_by_dn(sam_ctx
, getnc_state
->ncRoot_dn
, &r
->out
.ctr
->ctr6
.naming_context
->sid
);
1774 dsdb_get_oid_mappings_drsuapi(schema
, true, mem_ctx
, &ctr
);
1775 r
->out
.ctr
->ctr6
.mapping_ctr
= *ctr
;
1777 r
->out
.ctr
->ctr6
.source_dsa_guid
= *(samdb_ntds_objectGUID(sam_ctx
));
1778 r
->out
.ctr
->ctr6
.source_dsa_invocation_id
= *(samdb_ntds_invocation_id(sam_ctx
));
1780 r
->out
.ctr
->ctr6
.old_highwatermark
= req10
->highwatermark
;
1781 r
->out
.ctr
->ctr6
.new_highwatermark
= req10
->highwatermark
;
1783 r
->out
.ctr
->ctr6
.first_object
= NULL
;
1784 currentObject
= &r
->out
.ctr
->ctr6
.first_object
;
1786 /* use this to force single objects at a time, which is useful
1787 * for working out what object is giving problems
1789 max_objects
= lpcfg_parm_int(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "drs", "max object sync", 1000);
1790 if (req10
->max_object_count
< max_objects
) {
1791 max_objects
= req10
->max_object_count
;
1794 * TODO: work out how the maximum should be calculated
1796 max_links
= lpcfg_parm_int(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "drs", "max link sync", 1500);
1799 * Maximum time that we can spend in a getncchanges
1800 * in order to avoid timeout of the other part.
1801 * 10 seconds by default.
1803 max_wait
= lpcfg_parm_int(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "drs", "max work time", 10);
1804 for (i
=getnc_state
->num_processed
;
1805 i
<getnc_state
->num_records
&&
1807 (r
->out
.ctr
->ctr6
.object_count
< max_objects
)
1808 && !max_wait_reached
;
1811 struct drsuapi_DsReplicaObjectListItemEx
*obj
;
1812 struct ldb_message
*msg
;
1813 static const char * const msg_attrs
[] = {
1815 "nTSecurityDescriptor",
1817 "replPropertyMetaData",
1818 DSDB_SECRET_ATTRIBUTES
,
1820 struct ldb_result
*msg_res
;
1821 struct ldb_dn
*msg_dn
;
1823 obj
= talloc_zero(mem_ctx
, struct drsuapi_DsReplicaObjectListItemEx
);
1824 W_ERROR_HAVE_NO_MEMORY(obj
);
1826 msg_dn
= ldb_dn_new_fmt(obj
, sam_ctx
, "<GUID=%s>", GUID_string(obj
, &getnc_state
->guids
[i
]));
1827 W_ERROR_HAVE_NO_MEMORY(msg_dn
);
1830 /* by re-searching here we avoid having a lot of full
1831 * records in memory between calls to getncchanges
1833 ret
= drsuapi_search_with_extended_dn(sam_ctx
, obj
, &msg_res
,
1835 LDB_SCOPE_BASE
, msg_attrs
, NULL
);
1836 if (ret
!= LDB_SUCCESS
) {
1837 if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
1838 DEBUG(1,("getncchanges: failed to fetch DN %s - %s\n",
1839 ldb_dn_get_extended_linearized(obj
, msg_dn
, 1), ldb_errstring(sam_ctx
)));
1845 msg
= msg_res
->msgs
[0];
1847 max_wait_reached
= (time(NULL
) - start
> max_wait
);
1849 werr
= get_nc_changes_build_object(obj
, msg
,
1850 sam_ctx
, getnc_state
->ncRoot_dn
,
1851 getnc_state
->is_schema_nc
,
1852 schema
, &session_key
, getnc_state
->min_usn
,
1853 req10
->replica_flags
,
1854 req10
->partial_attribute_set
,
1855 getnc_state
->uptodateness_vector
,
1858 if (!W_ERROR_IS_OK(werr
)) {
1862 werr
= get_nc_changes_add_links(sam_ctx
, getnc_state
,
1863 getnc_state
->ncRoot_dn
,
1864 schema
, getnc_state
->min_usn
,
1865 req10
->replica_flags
,
1867 &getnc_state
->la_list
,
1868 &getnc_state
->la_count
,
1869 getnc_state
->uptodateness_vector
);
1870 if (!W_ERROR_IS_OK(werr
)) {
1874 uSN
= ldb_msg_find_attr_as_int(msg
, "uSNChanged", -1);
1875 if (uSN
> r
->out
.ctr
->ctr6
.new_highwatermark
.tmp_highest_usn
) {
1876 r
->out
.ctr
->ctr6
.new_highwatermark
.tmp_highest_usn
= uSN
;
1879 if (obj
->meta_data_ctr
== NULL
) {
1880 DEBUG(8,(__location__
": getncchanges skipping send of object %s\n",
1881 ldb_dn_get_linearized(msg
->dn
)));
1882 /* no attributes to send */
1887 r
->out
.ctr
->ctr6
.object_count
++;
1889 *currentObject
= obj
;
1890 currentObject
= &obj
->next_object
;
1892 DEBUG(8,(__location__
": replicating object %s\n", ldb_dn_get_linearized(msg
->dn
)));
1894 talloc_free(getnc_state
->last_dn
);
1895 getnc_state
->last_dn
= talloc_move(getnc_state
, &msg
->dn
);
1897 talloc_free(msg_res
);
1898 talloc_free(msg_dn
);
1901 getnc_state
->num_processed
= i
;
1903 r
->out
.ctr
->ctr6
.nc_object_count
= getnc_state
->num_records
;
1905 /* the client can us to call UpdateRefs on its behalf to
1906 re-establish monitoring of the NC */
1907 if ((req10
->replica_flags
& (DRSUAPI_DRS_ADD_REF
| DRSUAPI_DRS_REF_GCSPN
)) &&
1908 !GUID_all_zero(&req10
->destination_dsa_guid
)) {
1909 struct drsuapi_DsReplicaUpdateRefsRequest1 ureq
;
1910 DEBUG(3,("UpdateRefs on getncchanges for %s\n",
1911 GUID_string(mem_ctx
, &req10
->destination_dsa_guid
)));
1912 ureq
.naming_context
= ncRoot
;
1913 ureq
.dest_dsa_dns_name
= samdb_ntds_msdcs_dns_name(b_state
->sam_ctx
, mem_ctx
,
1914 &req10
->destination_dsa_guid
);
1915 if (!ureq
.dest_dsa_dns_name
) {
1918 ureq
.dest_dsa_guid
= req10
->destination_dsa_guid
;
1919 ureq
.options
= DRSUAPI_DRS_ADD_REF
|
1920 DRSUAPI_DRS_ASYNC_OP
|
1921 DRSUAPI_DRS_GETCHG_CHECK
;
1923 /* we also need to pass through the
1924 DRSUAPI_DRS_REF_GCSPN bit so that repsTo gets flagged
1925 to send notifies using the GC SPN */
1926 ureq
.options
|= (req10
->replica_flags
& DRSUAPI_DRS_REF_GCSPN
);
1928 werr
= drsuapi_UpdateRefs(b_state
, mem_ctx
, &ureq
);
1929 if (!W_ERROR_IS_OK(werr
)) {
1930 DEBUG(0,(__location__
": Failed UpdateRefs in DsGetNCChanges - %s\n",
1937 * This is just a guess, how to calculate the
1938 * number of linked attributes to send, we need to
1939 * find out how to do this right.
1941 if (r
->out
.ctr
->ctr6
.object_count
>= max_links
) {
1944 max_links
-= r
->out
.ctr
->ctr6
.object_count
;
1947 link_total
= getnc_state
->la_count
;
1949 if (i
< getnc_state
->num_records
) {
1950 r
->out
.ctr
->ctr6
.more_data
= true;
1952 /* sort the whole array the first time */
1953 if (!getnc_state
->la_sorted
) {
1954 LDB_TYPESAFE_QSORT(getnc_state
->la_list
, getnc_state
->la_count
,
1955 sam_ctx
, linked_attribute_compare
);
1956 getnc_state
->la_sorted
= true;
1959 link_count
= getnc_state
->la_count
- getnc_state
->la_idx
;
1960 link_count
= MIN(max_links
, link_count
);
1962 r
->out
.ctr
->ctr6
.linked_attributes_count
= link_count
;
1963 r
->out
.ctr
->ctr6
.linked_attributes
= getnc_state
->la_list
+ getnc_state
->la_idx
;
1965 getnc_state
->la_idx
+= link_count
;
1966 link_given
= getnc_state
->la_idx
;
1968 if (getnc_state
->la_idx
< getnc_state
->la_count
) {
1969 r
->out
.ctr
->ctr6
.more_data
= true;
1973 if (!r
->out
.ctr
->ctr6
.more_data
) {
1974 talloc_steal(mem_ctx
, getnc_state
->la_list
);
1976 r
->out
.ctr
->ctr6
.uptodateness_vector
= talloc(mem_ctx
, struct drsuapi_DsReplicaCursor2CtrEx
);
1977 r
->out
.ctr
->ctr6
.new_highwatermark
.highest_usn
= r
->out
.ctr
->ctr6
.new_highwatermark
.tmp_highest_usn
;
1979 werr
= get_nc_changes_udv(sam_ctx
, getnc_state
->ncRoot_dn
,
1980 r
->out
.ctr
->ctr6
.uptodateness_vector
);
1981 if (!W_ERROR_IS_OK(werr
)) {
1985 talloc_free(getnc_state
);
1986 b_state
->getncchanges_state
= NULL
;
1989 if (req10
->extended_op
!= DRSUAPI_EXOP_NONE
) {
1990 r
->out
.ctr
->ctr6
.uptodateness_vector
= NULL
;
1991 r
->out
.ctr
->ctr6
.nc_object_count
= 0;
1992 ZERO_STRUCT(r
->out
.ctr
->ctr6
.new_highwatermark
);
1995 DEBUG(r
->out
.ctr
->ctr6
.more_data
?4:2,
1996 ("DsGetNCChanges with uSNChanged >= %llu flags 0x%08x on %s gave %u objects (done %u/%u) %u links (done %u/%u (as %s))\n",
1997 (unsigned long long)(req10
->highwatermark
.highest_usn
+1),
1998 req10
->replica_flags
, drs_ObjectIdentifier_to_string(mem_ctx
, ncRoot
),
1999 r
->out
.ctr
->ctr6
.object_count
,
2000 i
, r
->out
.ctr
->ctr6
.more_data
?getnc_state
->num_records
:i
,
2001 r
->out
.ctr
->ctr6
.linked_attributes_count
,
2002 link_given
, link_total
,
2003 dom_sid_string(mem_ctx
, user_sid
)));
2006 if (!r
->out
.ctr
->ctr6
.more_data
&& req10
->extended_op
!= DRSUAPI_EXOP_NONE
) {
2007 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsGetNCChanges
, NDR_BOTH
, r
);