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: converted %d, %d of %d objects left to be converted.\n",
154 pass_no, converted_obj_count, failed_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 struct drsuapi_DsReplicaAttribute *name_a = NULL;
213 struct drsuapi_DsReplicaMetaData *name_d = NULL;
214 struct replPropertyMetaData1 *rdn_m = NULL;
215 struct dom_sid *sid = NULL;
220 if (!in->object.identifier) {
224 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
228 if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
232 if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
236 sid = &in->object.identifier->sid;
237 if (sid->num_auths > 0) {
238 rid = sid->sub_auths[sid->num_auths - 1];
241 msg = ldb_msg_new(mem_ctx);
242 W_ERROR_HAVE_NO_MEMORY(msg);
244 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
245 W_ERROR_HAVE_NO_MEMORY(msg->dn);
247 msg->num_elements = in->object.attribute_ctr.num_attributes;
248 msg->elements = talloc_array(msg, struct ldb_message_element,
249 msg->num_elements + 1); /* +1 because of the RDN attribute */
250 W_ERROR_HAVE_NO_MEMORY(msg->elements);
252 md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
253 W_ERROR_HAVE_NO_MEMORY(md);
257 md->ctr.ctr1.count = in->meta_data_ctr->count;
258 md->ctr.ctr1.reserved = 0;
259 md->ctr.ctr1.array = talloc_array(mem_ctx,
260 struct replPropertyMetaData1,
261 md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
262 W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
264 for (i=0, attr_count=0; i < in->meta_data_ctr->count; i++, attr_count++) {
265 struct drsuapi_DsReplicaAttribute *a;
266 struct drsuapi_DsReplicaMetaData *d;
267 struct replPropertyMetaData1 *m;
268 struct ldb_message_element *e;
271 a = &in->object.attribute_ctr.attributes[i];
272 d = &in->meta_data_ctr->meta_data[i];
273 m = &md->ctr.ctr1.array[attr_count];
274 e = &msg->elements[attr_count];
276 if (dsdb_attid_in_list(ignore_attids, a->attid)) {
281 if (a->attid == DRSUAPI_ATTID_instanceType) {
282 if (instanceType_e != NULL) {
288 for (j=0; j<a->value_ctr.num_values; j++) {
289 status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob, gensec_skey, rid, a);
290 W_ERROR_NOT_OK_RETURN(status);
293 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
294 a, msg->elements, e);
295 W_ERROR_NOT_OK_RETURN(status);
298 m->version = d->version;
299 m->originating_change_time = d->originating_change_time;
300 m->originating_invocation_id = d->originating_invocation_id;
301 m->originating_usn = d->originating_usn;
304 if (d->originating_change_time > whenChanged) {
305 whenChanged = d->originating_change_time;
308 if (a->attid == DRSUAPI_ATTID_name) {
314 msg->num_elements = attr_count;
315 md->ctr.ctr1.count = attr_count;
317 rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
321 struct ldb_message_element *el;
322 const char *rdn_name = NULL;
323 const struct ldb_val *rdn_value = NULL;
324 const struct dsdb_attribute *rdn_attr = NULL;
328 * We only need the schema calls for the RDN in this
329 * codepath, and by doing this we avoid needing to
330 * have the dsdb_attribute_by_lDAPDisplayName accessor
331 * working during the schema load.
333 rdn_name = ldb_dn_get_rdn_name(msg->dn);
334 rdn_attr = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
338 rdn_attid = rdn_attr->attributeID_id;
339 rdn_value = ldb_dn_get_rdn_val(msg->dn);
341 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
343 ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
344 if (ret != LDB_SUCCESS) {
348 if (el->num_values != 1) {
349 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
353 if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
354 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
355 (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
356 (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
361 rdn_m->attid = rdn_attid;
362 rdn_m->version = name_d->version;
363 rdn_m->originating_change_time = name_d->originating_change_time;
364 rdn_m->originating_invocation_id = name_d->originating_invocation_id;
365 rdn_m->originating_usn = name_d->originating_usn;
366 rdn_m->local_usn = 0;
367 md->ctr.ctr1.count++;
371 if (instanceType_e == NULL) {
375 instanceType = ldb_msg_find_attr_as_int(msg, "instanceType", 0);
376 if (dsdb_repl_flags & DSDB_REPL_FLAG_PARTIAL_REPLICA) {
377 /* the instanceType type for partial_replica
378 replication is sent via DRS with TYPE_WRITE set, but
379 must be used on the client with TYPE_WRITE removed
381 if (instanceType & INSTANCE_TYPE_WRITE) {
383 * Make sure we do not change the order
387 * instanceType_e->num_values = 0
389 * ldb_msg_remove_attr(msg, "instanceType");
391 struct ldb_message_element *e;
393 e = ldb_msg_find_element(msg, "instanceType");
394 if (e != instanceType_e) {
395 DEBUG(0,("instanceType_e[%p] changed to e[%p]\n",
400 instanceType_e->num_values = 0;
402 instanceType &= ~INSTANCE_TYPE_WRITE;
403 if (ldb_msg_add_fmt(msg, "instanceType", "%d", instanceType) != LDB_SUCCESS) {
404 return WERR_INTERNAL_ERROR;
408 if (!(instanceType & INSTANCE_TYPE_WRITE)) {
409 DEBUG(0, ("Refusing to replicate %s from a read-only repilca into a read-write replica!\n",
410 ldb_dn_get_linearized(msg->dn)));
411 return WERR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA;
415 whenChanged_t = nt_time_to_unix(whenChanged);
416 whenChanged_s = ldb_timestring(msg, whenChanged_t);
417 W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
419 nt_status = GUID_to_ndr_blob(&in->object.identifier->guid, msg, &guid_value);
420 if (!NT_STATUS_IS_OK(nt_status)) {
421 return ntstatus_to_werror(nt_status);
424 if (in->parent_object_guid) {
425 nt_status = GUID_to_ndr_blob(in->parent_object_guid, msg, &parent_guid_value);
426 if (!NT_STATUS_IS_OK(nt_status)) {
427 return ntstatus_to_werror(nt_status);
430 parent_guid_value = data_blob_null;
434 out->guid_value = guid_value;
435 out->parent_guid_value = parent_guid_value;
436 out->when_changed = whenChanged_s;
441 WERROR dsdb_replicated_objects_convert(struct ldb_context *ldb,
442 const struct dsdb_schema *schema,
443 const char *partition_dn_str,
444 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
445 uint32_t object_count,
446 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
447 uint32_t linked_attributes_count,
448 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
449 const struct repsFromTo1 *source_dsa,
450 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
451 const DATA_BLOB *gensec_skey,
452 uint32_t dsdb_repl_flags,
454 struct dsdb_extended_replicated_objects **objects)
457 struct ldb_dn *partition_dn;
458 struct dsdb_schema_prefixmap *pfm_remote;
459 struct dsdb_extended_replicated_objects *out;
460 const struct drsuapi_DsReplicaObjectListItemEx *cur;
463 out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
464 W_ERROR_HAVE_NO_MEMORY(out);
465 out->version = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
466 out->dsdb_repl_flags = dsdb_repl_flags;
469 * Ensure schema is kept valid for as long as 'out'
470 * which may contain pointers to it
472 schema = talloc_reference(out, schema);
473 W_ERROR_HAVE_NO_MEMORY(schema);
475 partition_dn = ldb_dn_new(out, ldb, partition_dn_str);
476 W_ERROR_HAVE_NO_MEMORY_AND_FREE(partition_dn, out);
478 status = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
479 out, &pfm_remote, NULL);
480 if (!W_ERROR_IS_OK(status)) {
481 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s",
482 win_errstr(status)));
487 if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
489 * check for schema changes in case
490 * we are not replicating Schema NC
492 status = dsdb_schema_info_cmp(schema, mapping_ctr);
493 if (!W_ERROR_IS_OK(status)) {
494 DEBUG(1,("Remote schema has changed while replicating %s\n",
501 out->partition_dn = partition_dn;
503 out->source_dsa = source_dsa;
504 out->uptodateness_vector= uptodateness_vector;
506 out->num_objects = object_count;
507 out->objects = talloc_array(out,
508 struct dsdb_extended_replicated_object,
510 W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
512 /* pass the linked attributes down to the repl_meta_data
514 out->linked_attributes_count = linked_attributes_count;
515 out->linked_attributes = linked_attributes;
517 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
518 if (i == out->num_objects) {
523 status = dsdb_convert_object_ex(ldb, schema, pfm_remote,
527 out->objects, &out->objects[i]);
528 if (!W_ERROR_IS_OK(status)) {
530 DEBUG(0,("Failed to convert object %s: %s\n",
531 cur->object.identifier->dn,
532 win_errstr(status)));
536 if (i != out->num_objects) {
541 /* free pfm_remote, we won't need it anymore */
542 talloc_free(pfm_remote);
549 * Commits a list of replicated objects.
551 * @param working_schema dsdb_schema to be used for resolving
552 * Classes/Attributes during Schema replication. If not NULL,
553 * it will be set on ldb and used while committing replicated objects
555 WERROR dsdb_replicated_objects_commit(struct ldb_context *ldb,
556 struct dsdb_schema *working_schema,
557 struct dsdb_extended_replicated_objects *objects,
558 uint64_t *notify_uSN)
561 struct ldb_result *ext_res;
562 struct dsdb_schema *cur_schema = NULL;
563 struct dsdb_schema *new_schema = NULL;
565 uint64_t seq_num1, seq_num2;
566 bool used_global_schema = false;
568 TALLOC_CTX *tmp_ctx = talloc_new(objects);
570 DEBUG(0,("Failed to start talloc\n"));
574 /* TODO: handle linked attributes */
576 /* wrap the extended operation in a transaction
577 See [MS-DRSR] 3.3.2 Transactions
579 ret = ldb_transaction_start(ldb);
580 if (ret != LDB_SUCCESS) {
581 DEBUG(0,(__location__ " Failed to start transaction\n"));
585 ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
586 if (ret != LDB_SUCCESS) {
587 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
588 ldb_transaction_cancel(ldb);
589 TALLOC_FREE(tmp_ctx);
594 * Set working_schema for ldb in case we are replicating from Schema NC.
595 * Schema won't be reloaded during Replicated Objects commit, as it is
596 * done in a transaction. So we need some way to search for newly
597 * added Classes and Attributes
599 if (working_schema) {
600 /* store current schema so we can fall back in case of failure */
601 cur_schema = dsdb_get_schema(ldb, tmp_ctx);
602 used_global_schema = dsdb_uses_global_schema(ldb);
604 ret = dsdb_reference_schema(ldb, working_schema, false);
605 if (ret != LDB_SUCCESS) {
606 DEBUG(0,(__location__ "Failed to reference working schema - %s\n",
608 /* TODO: Map LDB Error to NTSTATUS? */
609 ldb_transaction_cancel(ldb);
610 TALLOC_FREE(tmp_ctx);
611 return WERR_INTERNAL_ERROR;
615 ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
616 if (ret != LDB_SUCCESS) {
617 /* restore previous schema */
618 if (used_global_schema) {
619 dsdb_set_global_schema(ldb);
620 } else if (cur_schema) {
621 dsdb_reference_schema(ldb, cur_schema, false);
624 DEBUG(0,("Failed to apply records: %s: %s\n",
625 ldb_errstring(ldb), ldb_strerror(ret)));
626 ldb_transaction_cancel(ldb);
627 TALLOC_FREE(tmp_ctx);
630 talloc_free(ext_res);
632 /* Save our updated prefixMap */
633 if (working_schema) {
634 werr = dsdb_write_prefixes_from_schema_to_ldb(working_schema,
637 if (!W_ERROR_IS_OK(werr)) {
638 /* restore previous schema */
639 if (used_global_schema) {
640 dsdb_set_global_schema(ldb);
641 } else if (cur_schema ) {
642 dsdb_reference_schema(ldb, cur_schema, false);
644 DEBUG(0,("Failed to save updated prefixMap: %s\n",
646 TALLOC_FREE(tmp_ctx);
651 ret = ldb_transaction_prepare_commit(ldb);
652 if (ret != LDB_SUCCESS) {
653 /* restore previous schema */
654 if (used_global_schema) {
655 dsdb_set_global_schema(ldb);
656 } else if (cur_schema ) {
657 dsdb_reference_schema(ldb, cur_schema, false);
659 DEBUG(0,(__location__ " Failed to prepare commit of transaction: %s\n",
660 ldb_errstring(ldb)));
661 TALLOC_FREE(tmp_ctx);
665 ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
666 if (ret != LDB_SUCCESS) {
667 /* restore previous schema */
668 if (used_global_schema) {
669 dsdb_set_global_schema(ldb);
670 } else if (cur_schema ) {
671 dsdb_reference_schema(ldb, cur_schema, false);
673 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
674 ldb_transaction_cancel(ldb);
675 TALLOC_FREE(tmp_ctx);
679 /* if this replication partner didn't need to be notified
680 before this transaction then it still doesn't need to be
681 notified, as the changes came from this server */
682 if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
683 *notify_uSN = seq_num2;
686 ret = ldb_transaction_commit(ldb);
687 if (ret != LDB_SUCCESS) {
688 /* restore previous schema */
689 if (used_global_schema) {
690 dsdb_set_global_schema(ldb);
691 } else if (cur_schema ) {
692 dsdb_reference_schema(ldb, cur_schema, false);
694 DEBUG(0,(__location__ " Failed to commit transaction\n"));
695 TALLOC_FREE(tmp_ctx);
700 * Reset the Schema used by ldb. This will lead to
701 * a schema cache being refreshed from database.
703 if (working_schema) {
704 struct ldb_message *msg;
705 struct ldb_request *req;
708 working_schema->last_refresh = 0;
709 new_schema = dsdb_get_schema(ldb, tmp_ctx);
711 * If dsdb_get_schema() fails, we just fall back
712 * to what we had. However, the database is probably
713 * unable to operate for other users from this
715 if (new_schema && used_global_schema) {
716 dsdb_make_schema_global(ldb, new_schema);
717 } else if (used_global_schema) {
718 DEBUG(0,("Failed to re-load schema after commit of transaction\n"));
719 dsdb_set_global_schema(ldb);
720 TALLOC_FREE(tmp_ctx);
721 return WERR_INTERNAL_ERROR;
723 DEBUG(0,("Failed to re-load schema after commit of transaction\n"));
724 dsdb_reference_schema(ldb, cur_schema, false);
725 TALLOC_FREE(tmp_ctx);
726 return WERR_INTERNAL_ERROR;
728 msg = ldb_msg_new(tmp_ctx);
730 TALLOC_FREE(tmp_ctx);
733 msg->dn = ldb_dn_new(msg, ldb, "");
734 if (msg->dn == NULL) {
735 TALLOC_FREE(tmp_ctx);
739 ret = ldb_msg_add_string(msg, "schemaUpdateNow", "1");
740 if (ret != LDB_SUCCESS) {
741 TALLOC_FREE(tmp_ctx);
742 return WERR_INTERNAL_ERROR;
745 ret = ldb_build_mod_req(&req, ldb, objects,
749 ldb_op_default_callback,
752 if (ret != LDB_SUCCESS) {
753 TALLOC_FREE(tmp_ctx);
754 return WERR_DS_DRA_INTERNAL_ERROR;
757 ret = ldb_transaction_start(ldb);
758 if (ret != LDB_SUCCESS) {
759 TALLOC_FREE(tmp_ctx);
760 DEBUG(0, ("Autotransaction start failed\n"));
761 return WERR_DS_DRA_INTERNAL_ERROR;
764 ret = ldb_request(ldb, req);
765 if (ret == LDB_SUCCESS) {
766 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
769 if (ret == LDB_SUCCESS) {
770 ret = ldb_transaction_commit(ldb);
772 DEBUG(0, ("Schema update now failed: %s\n",
773 ldb_errstring(ldb)));
774 ldb_transaction_cancel(ldb);
777 if (ret != LDB_SUCCESS) {
778 DEBUG(0, ("Commit failed: %s\n", ldb_errstring(ldb)));
779 TALLOC_FREE(tmp_ctx);
780 return WERR_DS_INTERNAL_FAILURE;
784 DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
785 objects->num_objects, objects->linked_attributes_count,
786 ldb_dn_get_linearized(objects->partition_dn)));
788 TALLOC_FREE(tmp_ctx);
792 static WERROR dsdb_origin_object_convert(struct ldb_context *ldb,
793 const struct dsdb_schema *schema,
794 const struct drsuapi_DsReplicaObjectListItem *in,
796 struct ldb_message **_msg)
800 struct ldb_message *msg;
802 if (!in->object.identifier) {
806 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
810 msg = ldb_msg_new(mem_ctx);
811 W_ERROR_HAVE_NO_MEMORY(msg);
813 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
814 W_ERROR_HAVE_NO_MEMORY(msg->dn);
816 msg->num_elements = in->object.attribute_ctr.num_attributes;
817 msg->elements = talloc_array(msg, struct ldb_message_element,
819 W_ERROR_HAVE_NO_MEMORY(msg->elements);
821 for (i=0; i < msg->num_elements; i++) {
822 struct drsuapi_DsReplicaAttribute *a;
823 struct ldb_message_element *e;
825 a = &in->object.attribute_ctr.attributes[i];
826 e = &msg->elements[i];
828 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, schema->prefixmap,
829 a, msg->elements, e);
830 W_ERROR_NOT_OK_RETURN(status);
839 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
841 const struct drsuapi_DsReplicaObjectListItem *first_object,
843 uint32_t dsdb_repl_flags,
844 struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
847 const struct dsdb_schema *schema;
848 const struct drsuapi_DsReplicaObjectListItem *cur;
849 struct ldb_message **objects;
850 struct drsuapi_DsReplicaObjectIdentifier2 *ids;
852 uint32_t num_objects = 0;
853 const char * const attrs[] = {
858 struct ldb_result *res;
861 for (cur = first_object; cur; cur = cur->next_object) {
865 if (num_objects == 0) {
869 ret = ldb_transaction_start(ldb);
870 if (ret != LDB_SUCCESS) {
871 return WERR_DS_INTERNAL_FAILURE;
874 objects = talloc_array(mem_ctx, struct ldb_message *,
876 if (objects == NULL) {
881 schema = dsdb_get_schema(ldb, objects);
883 return WERR_DS_SCHEMA_NOT_LOADED;
886 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
887 status = dsdb_origin_object_convert(ldb, schema, cur,
888 objects, &objects[i]);
889 if (!W_ERROR_IS_OK(status)) {
894 ids = talloc_array(mem_ctx,
895 struct drsuapi_DsReplicaObjectIdentifier2,
902 if (dsdb_repl_flags & DSDB_REPL_FLAG_ADD_NCNAME) {
903 /* check for possible NC creation */
904 for (i=0; i < num_objects; i++) {
905 struct ldb_message *msg = objects[i];
906 struct ldb_message_element *el;
907 struct ldb_dn *nc_dn;
909 if (ldb_msg_check_string_attribute(msg, "objectClass", "crossRef") == 0) {
912 el = ldb_msg_find_element(msg, "nCName");
913 if (el == NULL || el->num_values != 1) {
916 nc_dn = ldb_dn_from_ldb_val(objects, ldb, &el->values[0]);
917 if (!ldb_dn_validate(nc_dn)) {
920 ret = dsdb_create_partial_replica_NC(ldb, nc_dn);
921 if (ret != LDB_SUCCESS) {
922 status = WERR_DS_INTERNAL_FAILURE;
928 for (i=0; i < num_objects; i++) {
929 struct dom_sid *sid = NULL;
930 struct ldb_request *add_req;
932 DEBUG(6,(__location__ ": adding %s\n",
933 ldb_dn_get_linearized(objects[i]->dn)));
935 ret = ldb_build_add_req(&add_req,
941 ldb_op_default_callback,
943 if (ret != LDB_SUCCESS) {
944 status = WERR_DS_INTERNAL_FAILURE;
948 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
949 if (ret != LDB_SUCCESS) {
950 status = WERR_DS_INTERNAL_FAILURE;
954 ret = ldb_request(ldb, add_req);
955 if (ret == LDB_SUCCESS) {
956 ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
958 if (ret != LDB_SUCCESS) {
959 DEBUG(0,(__location__ ": Failed add of %s - %s\n",
960 ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
961 status = WERR_DS_INTERNAL_FAILURE;
965 talloc_free(add_req);
967 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
968 LDB_SCOPE_BASE, attrs,
970 if (ret != LDB_SUCCESS) {
971 status = WERR_DS_INTERNAL_FAILURE;
974 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
975 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
979 ZERO_STRUCT(ids[i].sid);
983 ret = ldb_transaction_commit(ldb);
984 if (ret != LDB_SUCCESS) {
985 return WERR_DS_INTERNAL_FAILURE;
988 talloc_free(objects);
995 talloc_free(objects);
996 ldb_transaction_cancel(ldb);