2 Unix SMB/CIFS mplementation.
3 Helper functions for applying replicated objects
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "dsdb/samdb/samdb.h"
24 #include <ldb_errors.h>
25 #include "../lib/util/dlinklist.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
27 #include "librpc/gen_ndr/ndr_drsuapi.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "../lib/crypto/crypto.h"
30 #include "../libcli/drsuapi/drsuapi.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "param/param.h"
35 * Multi-pass working schema creation
37 * - shallow copy initial schema supplied
38 * - create a working schema in multiple passes
39 * until all objects are resolved
40 * Working schema is a schema with Attributes, Classes
41 * and indexes, but w/o subClassOf, possibleSupperiors etc.
42 * It is to be used just us cache for converting attribute values.
44 WERROR
dsdb_repl_make_working_schema(struct ldb_context
*ldb
,
45 const struct dsdb_schema
*initial_schema
,
46 const struct drsuapi_DsReplicaOIDMapping_Ctr
*mapping_ctr
,
47 uint32_t object_count
,
48 const struct drsuapi_DsReplicaObjectListItemEx
*first_object
,
49 const DATA_BLOB
*gensec_skey
,
51 struct dsdb_schema
**_schema_out
)
54 struct schema_list
*next
, *prev
;
55 const struct drsuapi_DsReplicaObjectListItemEx
*obj
;
59 struct dsdb_schema_prefixmap
*pfm_remote
;
60 struct schema_list
*schema_list
= NULL
, *schema_list_item
, *schema_list_next_item
;
61 struct dsdb_schema
*working_schema
;
62 const struct drsuapi_DsReplicaObjectListItemEx
*cur
;
64 uint32_t ignore_attids
[] = {
65 DRSUAPI_ATTID_auxiliaryClass
,
66 DRSUAPI_ATTID_mayContain
,
67 DRSUAPI_ATTID_mustContain
,
68 DRSUAPI_ATTID_possSuperiors
,
69 DRSUAPI_ATTID_systemPossSuperiors
,
73 /* make a copy of the iniatial_scheam so we don't mess with it */
74 working_schema
= dsdb_schema_copy_shallow(mem_ctx
, ldb
, initial_schema
);
75 if (!working_schema
) {
76 DEBUG(0,(__location__
": schema copy failed!\n"));
80 /* we are going to need remote prefixMap for decoding */
81 werr
= dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr
, true,
82 mem_ctx
, &pfm_remote
, NULL
);
83 if (!W_ERROR_IS_OK(werr
)) {
84 DEBUG(0,(__location__
": Failed to decode remote prefixMap: %s",
89 /* create a list of objects yet to be converted */
90 for (cur
= first_object
; cur
; cur
= cur
->next_object
) {
91 schema_list_item
= talloc(mem_ctx
, struct schema_list
);
92 schema_list_item
->obj
= cur
;
93 DLIST_ADD_END(schema_list
, schema_list_item
, struct schema_list
);
96 /* resolve objects until all are resolved and in local schema */
100 uint32_t converted_obj_count
= 0;
101 uint32_t failed_obj_count
= 0;
102 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
103 W_ERROR_HAVE_NO_MEMORY(tmp_ctx
);
105 for (schema_list_item
= schema_list
; schema_list_item
; schema_list_item
=schema_list_next_item
) {
106 struct dsdb_extended_replicated_object object
;
108 cur
= schema_list_item
->obj
;
110 /* Save the next item, now we have saved out
111 * the current one, so we can DLIST_REMOVE it
113 schema_list_next_item
= schema_list_item
->next
;
116 * Convert the objects into LDB messages using the
117 * schema we have so far. It's ok if we fail to convert
118 * an object. We should convert more objects on next pass.
120 werr
= dsdb_convert_object_ex(ldb
, working_schema
, pfm_remote
,
125 if (!W_ERROR_IS_OK(werr
)) {
126 DEBUG(4,("debug: Failed to convert schema object %s into ldb msg, will try during next loop\n",
127 cur
->object
.identifier
->dn
));
132 * Convert the schema from ldb_message format
133 * (OIDs as OID strings) into schema, using
134 * the remote prefixMap
136 werr
= dsdb_schema_set_el_from_ldb_msg(ldb
,
139 if (!W_ERROR_IS_OK(werr
)) {
140 DEBUG(4,("debug: failed to convert object %s into a schema element, will try during next loop: %s\n",
141 ldb_dn_get_linearized(object
.msg
->dn
),
145 DLIST_REMOVE(schema_list
, schema_list_item
);
146 talloc_free(schema_list_item
);
147 converted_obj_count
++;
151 talloc_free(tmp_ctx
);
153 DEBUG(4,("Schema load pass %d: %d/%d of %d objects left to be converted.\n",
154 pass_no
, failed_obj_count
, converted_obj_count
, object_count
));
157 /* check if we converted any objects in this pass */
158 if (converted_obj_count
== 0) {
159 DEBUG(0,("Can't continue Schema load: didn't manage to convert any objects: all %d remaining of %d objects failed to convert\n", failed_obj_count
, object_count
));
160 return WERR_INTERNAL_ERROR
;
163 /* rebuild indexes */
164 ret
= dsdb_setup_sorted_accessors(ldb
, working_schema
);
165 if (LDB_SUCCESS
!= ret
) {
166 DEBUG(0,("Failed to create schema-cache indexes!\n"));
167 return WERR_INTERNAL_ERROR
;
171 *_schema_out
= working_schema
;
176 static bool dsdb_attid_in_list(const uint32_t attid_list
[], uint32_t attid
)
182 for (cur
= attid_list
; *cur
!= DRSUAPI_ATTID_INVALID
; cur
++) {
190 WERROR
dsdb_convert_object_ex(struct ldb_context
*ldb
,
191 const struct dsdb_schema
*schema
,
192 const struct dsdb_schema_prefixmap
*pfm_remote
,
193 const struct drsuapi_DsReplicaObjectListItemEx
*in
,
194 const DATA_BLOB
*gensec_skey
,
195 const uint32_t *ignore_attids
,
196 uint32_t dsdb_repl_flags
,
198 struct dsdb_extended_replicated_object
*out
)
203 struct ldb_message
*msg
;
204 struct replPropertyMetaDataBlob
*md
;
206 struct ldb_message_element
*instanceType_e
= NULL
;
207 struct ldb_val guid_value
;
208 struct ldb_val parent_guid_value
;
209 NTTIME whenChanged
= 0;
210 time_t whenChanged_t
;
211 const char *whenChanged_s
;
212 const char *rdn_name
= NULL
;
213 const struct ldb_val
*rdn_value
= NULL
;
214 const struct dsdb_attribute
*rdn_attr
= NULL
;
216 struct drsuapi_DsReplicaAttribute
*name_a
= NULL
;
217 struct drsuapi_DsReplicaMetaData
*name_d
= NULL
;
218 struct replPropertyMetaData1
*rdn_m
= NULL
;
219 struct dom_sid
*sid
= NULL
;
224 if (!in
->object
.identifier
) {
228 if (!in
->object
.identifier
->dn
|| !in
->object
.identifier
->dn
[0]) {
232 if (in
->object
.attribute_ctr
.num_attributes
!= 0 && !in
->meta_data_ctr
) {
236 if (in
->object
.attribute_ctr
.num_attributes
!= in
->meta_data_ctr
->count
) {
240 sid
= &in
->object
.identifier
->sid
;
241 if (sid
->num_auths
> 0) {
242 rid
= sid
->sub_auths
[sid
->num_auths
- 1];
245 msg
= ldb_msg_new(mem_ctx
);
246 W_ERROR_HAVE_NO_MEMORY(msg
);
248 msg
->dn
= ldb_dn_new(msg
, ldb
, in
->object
.identifier
->dn
);
249 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
251 rdn_name
= ldb_dn_get_rdn_name(msg
->dn
);
252 rdn_attr
= dsdb_attribute_by_lDAPDisplayName(schema
, rdn_name
);
256 rdn_attid
= rdn_attr
->attributeID_id
;
257 rdn_value
= ldb_dn_get_rdn_val(msg
->dn
);
259 msg
->num_elements
= in
->object
.attribute_ctr
.num_attributes
;
260 msg
->elements
= talloc_array(msg
, struct ldb_message_element
,
262 W_ERROR_HAVE_NO_MEMORY(msg
->elements
);
264 md
= talloc(mem_ctx
, struct replPropertyMetaDataBlob
);
265 W_ERROR_HAVE_NO_MEMORY(md
);
269 md
->ctr
.ctr1
.count
= in
->meta_data_ctr
->count
;
270 md
->ctr
.ctr1
.reserved
= 0;
271 md
->ctr
.ctr1
.array
= talloc_array(mem_ctx
,
272 struct replPropertyMetaData1
,
273 md
->ctr
.ctr1
.count
+ 1); /* +1 because of the RDN attribute */
274 W_ERROR_HAVE_NO_MEMORY(md
->ctr
.ctr1
.array
);
276 for (i
=0, attr_count
=0; i
< in
->meta_data_ctr
->count
; i
++, attr_count
++) {
277 struct drsuapi_DsReplicaAttribute
*a
;
278 struct drsuapi_DsReplicaMetaData
*d
;
279 struct replPropertyMetaData1
*m
;
280 struct ldb_message_element
*e
;
283 a
= &in
->object
.attribute_ctr
.attributes
[i
];
284 d
= &in
->meta_data_ctr
->meta_data
[i
];
285 m
= &md
->ctr
.ctr1
.array
[attr_count
];
286 e
= &msg
->elements
[attr_count
];
288 if (dsdb_attid_in_list(ignore_attids
, a
->attid
)) {
293 if (a
->attid
== DRSUAPI_ATTID_instanceType
) {
294 if (instanceType_e
!= NULL
) {
300 for (j
=0; j
<a
->value_ctr
.num_values
; j
++) {
301 status
= drsuapi_decrypt_attribute(a
->value_ctr
.values
[j
].blob
, gensec_skey
, rid
, a
);
302 W_ERROR_NOT_OK_RETURN(status
);
305 status
= dsdb_attribute_drsuapi_to_ldb(ldb
, schema
, pfm_remote
,
306 a
, msg
->elements
, e
);
307 W_ERROR_NOT_OK_RETURN(status
);
310 m
->version
= d
->version
;
311 m
->originating_change_time
= d
->originating_change_time
;
312 m
->originating_invocation_id
= d
->originating_invocation_id
;
313 m
->originating_usn
= d
->originating_usn
;
316 if (d
->originating_change_time
> whenChanged
) {
317 whenChanged
= d
->originating_change_time
;
320 if (a
->attid
== DRSUAPI_ATTID_name
) {
326 msg
->num_elements
= attr_count
;
327 md
->ctr
.ctr1
.count
= attr_count
;
329 rdn_m
= &md
->ctr
.ctr1
.array
[md
->ctr
.ctr1
.count
];
333 struct ldb_message_element
*el
;
334 el
= ldb_msg_find_element(msg
, rdn_attr
->lDAPDisplayName
);
336 ret
= ldb_msg_add_value(msg
, rdn_attr
->lDAPDisplayName
, rdn_value
, NULL
);
337 if (ret
!= LDB_SUCCESS
) {
341 if (el
->num_values
!= 1) {
342 DEBUG(0,(__location__
": Unexpected num_values=%u\n",
346 if (!ldb_val_equal_exact(&el
->values
[0], rdn_value
)) {
347 DEBUG(0,(__location__
": RDN value changed? '%*.*s' '%*.*s'\n",
348 (int)el
->values
[0].length
, (int)el
->values
[0].length
, el
->values
[0].data
,
349 (int)rdn_value
->length
, (int)rdn_value
->length
, rdn_value
->data
));
354 rdn_m
->attid
= rdn_attid
;
355 rdn_m
->version
= name_d
->version
;
356 rdn_m
->originating_change_time
= name_d
->originating_change_time
;
357 rdn_m
->originating_invocation_id
= name_d
->originating_invocation_id
;
358 rdn_m
->originating_usn
= name_d
->originating_usn
;
359 rdn_m
->local_usn
= 0;
360 md
->ctr
.ctr1
.count
++;
364 if (instanceType_e
== NULL
) {
368 instanceType
= ldb_msg_find_attr_as_int(msg
, "instanceType", 0);
369 if (dsdb_repl_flags
& DSDB_REPL_FLAG_PARTIAL_REPLICA
) {
370 /* the instanceType type for partial_replica
371 replication is sent via DRS with TYPE_WRITE set, but
372 must be used on the client with TYPE_WRITE removed
374 if (instanceType
& INSTANCE_TYPE_WRITE
) {
375 instanceType
&= ~INSTANCE_TYPE_WRITE
;
377 * Make sure we do not change the order
381 * instanceType_e->num_values = 0
383 * ldb_msg_remove_attr(msg, "instanceType");
385 instanceType_e
->num_values
= 0;
386 if (ldb_msg_add_fmt(msg
, "instanceType", "%d", instanceType
) != LDB_SUCCESS
) {
387 return WERR_INTERNAL_ERROR
;
391 if (!(instanceType
& INSTANCE_TYPE_WRITE
)) {
392 DEBUG(0, ("Refusing to replicate %s from a read-only repilca into a read-write replica!\n",
393 ldb_dn_get_linearized(msg
->dn
)));
394 return WERR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA
;
398 whenChanged_t
= nt_time_to_unix(whenChanged
);
399 whenChanged_s
= ldb_timestring(msg
, whenChanged_t
);
400 W_ERROR_HAVE_NO_MEMORY(whenChanged_s
);
402 nt_status
= GUID_to_ndr_blob(&in
->object
.identifier
->guid
, msg
, &guid_value
);
403 if (!NT_STATUS_IS_OK(nt_status
)) {
404 return ntstatus_to_werror(nt_status
);
407 if (in
->parent_object_guid
) {
408 nt_status
= GUID_to_ndr_blob(in
->parent_object_guid
, msg
, &parent_guid_value
);
409 if (!NT_STATUS_IS_OK(nt_status
)) {
410 return ntstatus_to_werror(nt_status
);
413 parent_guid_value
= data_blob_null
;
417 out
->guid_value
= guid_value
;
418 out
->parent_guid_value
= parent_guid_value
;
419 out
->when_changed
= whenChanged_s
;
424 WERROR
dsdb_replicated_objects_convert(struct ldb_context
*ldb
,
425 const struct dsdb_schema
*schema
,
426 const char *partition_dn_str
,
427 const struct drsuapi_DsReplicaOIDMapping_Ctr
*mapping_ctr
,
428 uint32_t object_count
,
429 const struct drsuapi_DsReplicaObjectListItemEx
*first_object
,
430 uint32_t linked_attributes_count
,
431 const struct drsuapi_DsReplicaLinkedAttribute
*linked_attributes
,
432 const struct repsFromTo1
*source_dsa
,
433 const struct drsuapi_DsReplicaCursor2CtrEx
*uptodateness_vector
,
434 const DATA_BLOB
*gensec_skey
,
435 uint32_t dsdb_repl_flags
,
437 struct dsdb_extended_replicated_objects
**objects
)
440 struct ldb_dn
*partition_dn
;
441 struct dsdb_schema_prefixmap
*pfm_remote
;
442 struct dsdb_extended_replicated_objects
*out
;
443 const struct drsuapi_DsReplicaObjectListItemEx
*cur
;
446 out
= talloc_zero(mem_ctx
, struct dsdb_extended_replicated_objects
);
447 W_ERROR_HAVE_NO_MEMORY(out
);
448 out
->version
= DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION
;
449 out
->dsdb_repl_flags
= dsdb_repl_flags
;
452 * Ensure schema is kept valid for as long as 'out'
453 * which may contain pointers to it
455 schema
= talloc_reference(out
, schema
);
456 W_ERROR_HAVE_NO_MEMORY(schema
);
458 partition_dn
= ldb_dn_new(out
, ldb
, partition_dn_str
);
459 W_ERROR_HAVE_NO_MEMORY_AND_FREE(partition_dn
, out
);
461 status
= dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr
, true,
462 out
, &pfm_remote
, NULL
);
463 if (!W_ERROR_IS_OK(status
)) {
464 DEBUG(0,(__location__
": Failed to decode remote prefixMap: %s",
465 win_errstr(status
)));
470 if (ldb_dn_compare(partition_dn
, ldb_get_schema_basedn(ldb
)) != 0) {
472 * check for schema changes in case
473 * we are not replicating Schema NC
475 status
= dsdb_schema_info_cmp(schema
, mapping_ctr
);
476 if (!W_ERROR_IS_OK(status
)) {
477 DEBUG(1,("Remote schema has changed while replicating %s\n",
484 out
->partition_dn
= partition_dn
;
486 out
->source_dsa
= source_dsa
;
487 out
->uptodateness_vector
= uptodateness_vector
;
489 out
->num_objects
= object_count
;
490 out
->objects
= talloc_array(out
,
491 struct dsdb_extended_replicated_object
,
493 W_ERROR_HAVE_NO_MEMORY_AND_FREE(out
->objects
, out
);
495 /* pass the linked attributes down to the repl_meta_data
497 out
->linked_attributes_count
= linked_attributes_count
;
498 out
->linked_attributes
= linked_attributes
;
500 for (i
=0, cur
= first_object
; cur
; cur
= cur
->next_object
, i
++) {
501 if (i
== out
->num_objects
) {
506 status
= dsdb_convert_object_ex(ldb
, schema
, pfm_remote
,
510 out
->objects
, &out
->objects
[i
]);
511 if (!W_ERROR_IS_OK(status
)) {
513 DEBUG(0,("Failed to convert object %s: %s\n",
514 cur
->object
.identifier
->dn
,
515 win_errstr(status
)));
519 if (i
!= out
->num_objects
) {
524 /* free pfm_remote, we won't need it anymore */
525 talloc_free(pfm_remote
);
532 * Commits a list of replicated objects.
534 * @param working_schema dsdb_schema to be used for resolving
535 * Classes/Attributes during Schema replication. If not NULL,
536 * it will be set on ldb and used while committing replicated objects
538 WERROR
dsdb_replicated_objects_commit(struct ldb_context
*ldb
,
539 struct dsdb_schema
*working_schema
,
540 struct dsdb_extended_replicated_objects
*objects
,
541 uint64_t *notify_uSN
)
544 struct ldb_result
*ext_res
;
545 struct dsdb_schema
*cur_schema
= NULL
;
547 uint64_t seq_num1
, seq_num2
;
549 /* TODO: handle linked attributes */
551 /* wrap the extended operation in a transaction
552 See [MS-DRSR] 3.3.2 Transactions
554 ret
= ldb_transaction_start(ldb
);
555 if (ret
!= LDB_SUCCESS
) {
556 DEBUG(0,(__location__
" Failed to start transaction\n"));
560 ret
= dsdb_load_partition_usn(ldb
, objects
->partition_dn
, &seq_num1
, NULL
);
561 if (ret
!= LDB_SUCCESS
) {
562 DEBUG(0,(__location__
" Failed to load partition uSN\n"));
563 ldb_transaction_cancel(ldb
);
568 * Set working_schema for ldb in case we are replicating from Schema NC.
569 * Schema won't be reloaded during Replicated Objects commit, as it is
570 * done in a transaction. So we need some way to search for newly
571 * added Classes and Attributes
573 if (working_schema
) {
574 /* store current schema so we can fall back in case of failure */
575 cur_schema
= dsdb_get_schema(ldb
, working_schema
);
577 ret
= dsdb_reference_schema(ldb
, working_schema
, false);
578 if (ret
!= LDB_SUCCESS
) {
579 DEBUG(0,(__location__
"Failed to reference working schema - %s\n",
581 /* TODO: Map LDB Error to NTSTATUS? */
582 ldb_transaction_cancel(ldb
);
583 return WERR_INTERNAL_ERROR
;
587 ret
= ldb_extended(ldb
, DSDB_EXTENDED_REPLICATED_OBJECTS_OID
, objects
, &ext_res
);
588 if (ret
!= LDB_SUCCESS
) {
589 /* restore previous schema */
591 dsdb_reference_schema(ldb
, cur_schema
, false);
592 dsdb_make_schema_global(ldb
, cur_schema
);
595 DEBUG(0,("Failed to apply records: %s: %s\n",
596 ldb_errstring(ldb
), ldb_strerror(ret
)));
597 ldb_transaction_cancel(ldb
);
600 talloc_free(ext_res
);
602 /* Save our updated prefixMap */
603 if (working_schema
) {
604 werr
= dsdb_write_prefixes_from_schema_to_ldb(working_schema
,
607 if (!W_ERROR_IS_OK(werr
)) {
608 /* restore previous schema */
610 dsdb_reference_schema(ldb
, cur_schema
, false);
611 dsdb_make_schema_global(ldb
, cur_schema
);
613 DEBUG(0,("Failed to save updated prefixMap: %s\n",
619 ret
= ldb_transaction_prepare_commit(ldb
);
620 if (ret
!= LDB_SUCCESS
) {
621 /* restore previous schema */
623 dsdb_reference_schema(ldb
, cur_schema
, false);
624 dsdb_make_schema_global(ldb
, cur_schema
);
626 DEBUG(0,(__location__
" Failed to prepare commit of transaction: %s\n",
627 ldb_errstring(ldb
)));
631 ret
= dsdb_load_partition_usn(ldb
, objects
->partition_dn
, &seq_num2
, NULL
);
632 if (ret
!= LDB_SUCCESS
) {
633 /* restore previous schema */
635 dsdb_reference_schema(ldb
, cur_schema
, false);
636 dsdb_make_schema_global(ldb
, cur_schema
);
638 DEBUG(0,(__location__
" Failed to load partition uSN\n"));
639 ldb_transaction_cancel(ldb
);
643 /* if this replication partner didn't need to be notified
644 before this transaction then it still doesn't need to be
645 notified, as the changes came from this server */
646 if (seq_num2
> seq_num1
&& seq_num1
<= *notify_uSN
) {
647 *notify_uSN
= seq_num2
;
650 ret
= ldb_transaction_commit(ldb
);
651 if (ret
!= LDB_SUCCESS
) {
652 /* restore previous schema */
654 dsdb_reference_schema(ldb
, cur_schema
, false);
655 dsdb_make_schema_global(ldb
, cur_schema
);
657 DEBUG(0,(__location__
" Failed to commit transaction\n"));
662 * Reset the Schema used by ldb. This will lead to
663 * a schema cache being refreshed from database.
665 if (working_schema
) {
666 struct ldb_message
*msg
;
667 struct ldb_request
*req
;
670 working_schema
->last_refresh
= 0;
671 cur_schema
= dsdb_get_schema(ldb
, NULL
);
672 /* TODO: What we do in case dsdb_get_schema() fail?
673 * We can't fallback at this point anymore */
675 dsdb_make_schema_global(ldb
, cur_schema
);
677 msg
= ldb_msg_new(ldb
);
681 msg
->dn
= ldb_dn_new(msg
, ldb
, "");
682 if (msg
->dn
== NULL
) {
687 ret
= ldb_msg_add_string(msg
, "schemaUpdateNow", "1");
688 if (ret
!= LDB_SUCCESS
) {
690 return WERR_INTERNAL_ERROR
;
693 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
697 ldb_op_default_callback
,
700 if (ret
!= LDB_SUCCESS
) {
702 return WERR_DS_DRA_INTERNAL_ERROR
;
705 ret
= ldb_transaction_start(ldb
);
706 if (ret
!= LDB_SUCCESS
) {
709 DEBUG(0, ("Autotransaction start failed\n"));
710 return WERR_DS_DRA_INTERNAL_ERROR
;
713 ret
= ldb_request(ldb
, req
);
714 if (ret
== LDB_SUCCESS
) {
715 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
718 if (ret
== LDB_SUCCESS
) {
719 ret
= ldb_transaction_commit(ldb
);
721 DEBUG(0, ("Schema update now failed: %s\n", ldb_strerror(ret
)));
722 ldb_transaction_cancel(ldb
);
727 if (ret
!= LDB_SUCCESS
) {
728 DEBUG(0, ("Commit failed: %s\n", ldb_strerror(ret
)));
729 return WERR_DS_INTERNAL_FAILURE
;
733 DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
734 objects
->num_objects
, objects
->linked_attributes_count
,
735 ldb_dn_get_linearized(objects
->partition_dn
)));
740 static WERROR
dsdb_origin_object_convert(struct ldb_context
*ldb
,
741 const struct dsdb_schema
*schema
,
742 const struct drsuapi_DsReplicaObjectListItem
*in
,
744 struct ldb_message
**_msg
)
748 struct ldb_message
*msg
;
750 if (!in
->object
.identifier
) {
754 if (!in
->object
.identifier
->dn
|| !in
->object
.identifier
->dn
[0]) {
758 msg
= ldb_msg_new(mem_ctx
);
759 W_ERROR_HAVE_NO_MEMORY(msg
);
761 msg
->dn
= ldb_dn_new(msg
, ldb
, in
->object
.identifier
->dn
);
762 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
764 msg
->num_elements
= in
->object
.attribute_ctr
.num_attributes
;
765 msg
->elements
= talloc_array(msg
, struct ldb_message_element
,
767 W_ERROR_HAVE_NO_MEMORY(msg
->elements
);
769 for (i
=0; i
< msg
->num_elements
; i
++) {
770 struct drsuapi_DsReplicaAttribute
*a
;
771 struct ldb_message_element
*e
;
773 a
= &in
->object
.attribute_ctr
.attributes
[i
];
774 e
= &msg
->elements
[i
];
776 status
= dsdb_attribute_drsuapi_to_ldb(ldb
, schema
, schema
->prefixmap
,
777 a
, msg
->elements
, e
);
778 W_ERROR_NOT_OK_RETURN(status
);
787 WERROR
dsdb_origin_objects_commit(struct ldb_context
*ldb
,
789 const struct drsuapi_DsReplicaObjectListItem
*first_object
,
791 uint32_t dsdb_repl_flags
,
792 struct drsuapi_DsReplicaObjectIdentifier2
**_ids
)
795 const struct dsdb_schema
*schema
;
796 const struct drsuapi_DsReplicaObjectListItem
*cur
;
797 struct ldb_message
**objects
;
798 struct drsuapi_DsReplicaObjectIdentifier2
*ids
;
800 uint32_t num_objects
= 0;
801 const char * const attrs
[] = {
806 struct ldb_result
*res
;
809 for (cur
= first_object
; cur
; cur
= cur
->next_object
) {
813 if (num_objects
== 0) {
817 ret
= ldb_transaction_start(ldb
);
818 if (ret
!= LDB_SUCCESS
) {
819 return WERR_DS_INTERNAL_FAILURE
;
822 objects
= talloc_array(mem_ctx
, struct ldb_message
*,
824 if (objects
== NULL
) {
829 schema
= dsdb_get_schema(ldb
, objects
);
831 return WERR_DS_SCHEMA_NOT_LOADED
;
834 for (i
=0, cur
= first_object
; cur
; cur
= cur
->next_object
, i
++) {
835 status
= dsdb_origin_object_convert(ldb
, schema
, cur
,
836 objects
, &objects
[i
]);
837 if (!W_ERROR_IS_OK(status
)) {
842 ids
= talloc_array(mem_ctx
,
843 struct drsuapi_DsReplicaObjectIdentifier2
,
850 if (dsdb_repl_flags
& DSDB_REPL_FLAG_ADD_NCNAME
) {
851 /* check for possible NC creation */
852 for (i
=0; i
< num_objects
; i
++) {
853 struct ldb_message
*msg
= objects
[i
];
854 struct ldb_message_element
*el
;
855 struct ldb_dn
*nc_dn
;
857 if (ldb_msg_check_string_attribute(msg
, "objectClass", "crossRef") == 0) {
860 el
= ldb_msg_find_element(msg
, "nCName");
861 if (el
== NULL
|| el
->num_values
!= 1) {
864 nc_dn
= ldb_dn_from_ldb_val(objects
, ldb
, &el
->values
[0]);
865 if (!ldb_dn_validate(nc_dn
)) {
868 ret
= dsdb_create_partial_replica_NC(ldb
, nc_dn
);
869 if (ret
!= LDB_SUCCESS
) {
870 status
= WERR_DS_INTERNAL_FAILURE
;
876 for (i
=0; i
< num_objects
; i
++) {
877 struct dom_sid
*sid
= NULL
;
878 struct ldb_request
*add_req
;
880 DEBUG(6,(__location__
": adding %s\n",
881 ldb_dn_get_linearized(objects
[i
]->dn
)));
883 ret
= ldb_build_add_req(&add_req
,
889 ldb_op_default_callback
,
891 if (ret
!= LDB_SUCCESS
) {
892 status
= WERR_DS_INTERNAL_FAILURE
;
896 ret
= ldb_request_add_control(add_req
, LDB_CONTROL_RELAX_OID
, true, NULL
);
897 if (ret
!= LDB_SUCCESS
) {
898 status
= WERR_DS_INTERNAL_FAILURE
;
902 ret
= ldb_request(ldb
, add_req
);
903 if (ret
== LDB_SUCCESS
) {
904 ret
= ldb_wait(add_req
->handle
, LDB_WAIT_ALL
);
906 if (ret
!= LDB_SUCCESS
) {
907 DEBUG(0,(__location__
": Failed add of %s - %s\n",
908 ldb_dn_get_linearized(objects
[i
]->dn
), ldb_errstring(ldb
)));
909 status
= WERR_DS_INTERNAL_FAILURE
;
913 talloc_free(add_req
);
915 ret
= ldb_search(ldb
, objects
, &res
, objects
[i
]->dn
,
916 LDB_SCOPE_BASE
, attrs
,
918 if (ret
!= LDB_SUCCESS
) {
919 status
= WERR_DS_INTERNAL_FAILURE
;
922 ids
[i
].guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
923 sid
= samdb_result_dom_sid(objects
, res
->msgs
[0], "objectSid");
927 ZERO_STRUCT(ids
[i
].sid
);
931 ret
= ldb_transaction_commit(ldb
);
932 if (ret
!= LDB_SUCCESS
) {
933 return WERR_DS_INTERNAL_FAILURE
;
936 talloc_free(objects
);
943 talloc_free(objects
);
944 ldb_transaction_cancel(ldb
);