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"
34 static WERROR dsdb_repl_merge_working_schema(struct ldb_context *ldb,
35 struct dsdb_schema *dest_schema,
36 const struct dsdb_schema *ref_schema)
38 const struct dsdb_class *cur_class = NULL;
39 const struct dsdb_attribute *cur_attr = NULL;
42 for (cur_class = ref_schema->classes;
44 cur_class = cur_class->next)
46 const struct dsdb_class *tmp1;
47 struct dsdb_class *tmp2;
49 tmp1 = dsdb_class_by_governsID_id(dest_schema,
50 cur_class->governsID_id);
56 * Do a shallow copy so that original next and prev are
57 * not modified, we don't need to do a deep copy
58 * as the rest won't be modified and this is for
59 * a short lived object.
61 tmp2 = talloc(dest_schema, struct dsdb_class);
63 return WERR_NOT_ENOUGH_MEMORY;
66 DLIST_ADD(dest_schema->classes, tmp2);
69 for (cur_attr = ref_schema->attributes;
71 cur_attr = cur_attr->next)
73 const struct dsdb_attribute *tmp1;
74 struct dsdb_attribute *tmp2;
76 tmp1 = dsdb_attribute_by_attributeID_id(dest_schema,
77 cur_attr->attributeID_id);
83 * Do a shallow copy so that original next and prev are
84 * not modified, we don't need to do a deep copy
85 * as the rest won't be modified and this is for
86 * a short lived object.
88 tmp2 = talloc(dest_schema, struct dsdb_attribute);
90 return WERR_NOT_ENOUGH_MEMORY;
93 DLIST_ADD(dest_schema->attributes, tmp2);
96 ret = dsdb_setup_sorted_accessors(ldb, dest_schema);
97 if (LDB_SUCCESS != ret) {
98 DEBUG(0,("Failed to add new attribute to reference schema!\n"));
99 return WERR_INTERNAL_ERROR;
105 WERROR dsdb_repl_resolve_working_schema(struct ldb_context *ldb,
106 struct dsdb_schema_prefixmap *pfm_remote,
107 uint32_t cycle_before_switching,
108 struct dsdb_schema *initial_schema,
109 struct dsdb_schema *resulting_schema,
110 uint32_t object_count,
111 const struct drsuapi_DsReplicaObjectListItemEx *first_object)
114 struct schema_list *next, *prev;
115 const struct drsuapi_DsReplicaObjectListItemEx *obj;
117 struct schema_list *schema_list = NULL, *schema_list_item, *schema_list_next_item;
119 struct dsdb_schema *working_schema;
120 const struct drsuapi_DsReplicaObjectListItemEx *cur;
121 DATA_BLOB empty_key = data_blob_null;
123 uint32_t ignore_attids[] = {
124 DRSUAPI_ATTID_auxiliaryClass,
125 DRSUAPI_ATTID_mayContain,
126 DRSUAPI_ATTID_mustContain,
127 DRSUAPI_ATTID_possSuperiors,
128 DRSUAPI_ATTID_systemPossSuperiors,
129 DRSUAPI_ATTID_INVALID
131 TALLOC_CTX *frame = talloc_stackframe();
133 /* create a list of objects yet to be converted */
134 for (cur = first_object; cur; cur = cur->next_object) {
135 schema_list_item = talloc(frame, struct schema_list);
136 if (schema_list_item == NULL) {
137 return WERR_NOT_ENOUGH_MEMORY;
140 schema_list_item->obj = cur;
141 DLIST_ADD_END(schema_list, schema_list_item);
144 /* resolve objects until all are resolved and in local schema */
146 working_schema = initial_schema;
148 while (schema_list) {
149 uint32_t converted_obj_count = 0;
150 uint32_t failed_obj_count = 0;
152 if (resulting_schema != working_schema) {
154 * If the selfmade schema is not the schema used to
155 * translate and validate replicated object,
156 * Which means that we are using the bootstrap schema
157 * Then we add attributes and classes that were already
158 * translated to the working schema, the idea is that
159 * we might need to add new attributes and classes
160 * to be able to translate critical replicated objects
161 * and without that we wouldn't be able to translate them
163 werr = dsdb_repl_merge_working_schema(ldb,
166 if (!W_ERROR_IS_OK(werr)) {
172 for (schema_list_item = schema_list;
174 schema_list_item=schema_list_next_item) {
175 struct dsdb_extended_replicated_object object;
177 cur = schema_list_item->obj;
180 * Save the next item, now we have saved out
181 * the current one, so we can DLIST_REMOVE it
184 schema_list_next_item = schema_list_item->next;
187 * Convert the objects into LDB messages using the
188 * schema we have so far. It's ok if we fail to convert
189 * an object. We should convert more objects on next pass.
191 werr = dsdb_convert_object_ex(ldb, working_schema,
197 schema_list_item, &object);
198 if (!W_ERROR_IS_OK(werr)) {
199 DEBUG(4,("debug: Failed to convert schema "
200 "object %s into ldb msg, "
201 "will try during next loop\n",
202 cur->object.identifier->dn));
207 * Convert the schema from ldb_message format
208 * (OIDs as OID strings) into schema, using
209 * the remote prefixMap
211 * It's not likely, but possible to get the
212 * same object twice and we should keep
215 werr = dsdb_schema_set_el_from_ldb_msg_dups(ldb,
219 if (!W_ERROR_IS_OK(werr)) {
220 DEBUG(4,("debug: failed to convert "
221 "object %s into a schema element, "
222 "will try during next loop: %s\n",
223 ldb_dn_get_linearized(object.msg->dn),
227 DEBUG(8,("Converted object %s into a schema element\n",
228 ldb_dn_get_linearized(object.msg->dn)));
229 DLIST_REMOVE(schema_list, schema_list_item);
230 TALLOC_FREE(schema_list_item);
231 converted_obj_count++;
236 DEBUG(4,("Schema load pass %d: converted %d, %d of %d objects left to be converted.\n",
237 pass_no, converted_obj_count, failed_obj_count, object_count));
239 /* check if we converted any objects in this pass */
240 if (converted_obj_count == 0) {
241 DEBUG(0,("Can't continue Schema load: "
242 "didn't manage to convert any objects: "
243 "all %d remaining of %d objects "
244 "failed to convert\n",
245 failed_obj_count, object_count));
247 return WERR_INTERNAL_ERROR;
251 * Don't try to load the schema if there is missing object
252 * _and_ we are on the first pass as some critical objects
255 if (failed_obj_count == 0 || pass_no > cycle_before_switching) {
256 /* prepare for another cycle */
257 working_schema = resulting_schema;
259 ret = dsdb_setup_sorted_accessors(ldb, working_schema);
260 if (LDB_SUCCESS != ret) {
261 DEBUG(0,("Failed to create schema-cache indexes!\n"));
263 return WERR_INTERNAL_ERROR;
274 * Multi-pass working schema creation
276 * - shallow copy initial schema supplied
277 * - create a working schema in multiple passes
278 * until all objects are resolved
279 * Working schema is a schema with Attributes, Classes
280 * and indexes, but w/o subClassOf, possibleSupperiors etc.
281 * It is to be used just us cache for converting attribute values.
283 WERROR dsdb_repl_make_working_schema(struct ldb_context *ldb,
284 const struct dsdb_schema *initial_schema,
285 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
286 uint32_t object_count,
287 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
288 const DATA_BLOB *gensec_skey,
290 struct dsdb_schema **_schema_out)
293 struct dsdb_schema_prefixmap *pfm_remote;
295 struct dsdb_schema *working_schema;
297 /* make a copy of the iniatial_scheam so we don't mess with it */
298 working_schema = dsdb_schema_copy_shallow(mem_ctx, ldb, initial_schema);
299 if (!working_schema) {
300 DEBUG(0,(__location__ ": schema copy failed!\n"));
301 return WERR_NOT_ENOUGH_MEMORY;
303 working_schema->resolving_in_progress = true;
305 /* we are going to need remote prefixMap for decoding */
306 werr = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
307 working_schema, &pfm_remote, NULL);
308 if (!W_ERROR_IS_OK(werr)) {
309 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
311 talloc_free(working_schema);
315 for (r=0; r < pfm_remote->length; r++) {
316 const struct dsdb_schema_prefixmap_oid *rm = &pfm_remote->prefixes[r];
317 bool found_oid = false;
320 for (l=0; l < working_schema->prefixmap->length; l++) {
321 const struct dsdb_schema_prefixmap_oid *lm = &working_schema->prefixmap->prefixes[l];
324 cmp = data_blob_cmp(&rm->bin_oid, &lm->bin_oid);
336 * We prefer the same is as we got from the remote peer
337 * if there's no conflict.
339 werr = dsdb_schema_pfm_add_entry(working_schema->prefixmap,
340 rm->bin_oid, &rm->id, NULL);
341 if (!W_ERROR_IS_OK(werr)) {
342 DEBUG(0,(__location__ ": Failed to merge remote prefixMap: %s",
344 talloc_free(working_schema);
349 werr = dsdb_repl_resolve_working_schema(ldb,
351 0, /* cycle_before_switching */
356 if (!W_ERROR_IS_OK(werr)) {
357 DEBUG(0, ("%s: dsdb_repl_resolve_working_schema() failed: %s",
358 __location__, win_errstr(werr)));
359 talloc_free(working_schema);
363 working_schema->resolving_in_progress = false;
365 *_schema_out = working_schema;
370 static bool dsdb_attid_in_list(const uint32_t attid_list[], uint32_t attid)
376 for (cur = attid_list; *cur != DRSUAPI_ATTID_INVALID; cur++) {
384 WERROR dsdb_convert_object_ex(struct ldb_context *ldb,
385 const struct dsdb_schema *schema,
386 struct ldb_dn *partition_dn,
387 const struct dsdb_schema_prefixmap *pfm_remote,
388 const struct drsuapi_DsReplicaObjectListItemEx *in,
389 const DATA_BLOB *gensec_skey,
390 const uint32_t *ignore_attids,
391 uint32_t dsdb_repl_flags,
393 struct dsdb_extended_replicated_object *out)
395 WERROR status = WERR_OK;
397 struct ldb_message *msg;
398 struct replPropertyMetaDataBlob *md;
400 struct ldb_message_element *instanceType_e = NULL;
401 NTTIME whenChanged = 0;
402 time_t whenChanged_t;
403 const char *whenChanged_s;
404 struct dom_sid *sid = NULL;
408 if (!in->object.identifier) {
412 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
416 if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
420 if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
424 sid = &in->object.identifier->sid;
425 if (sid->num_auths > 0) {
426 rid = sid->sub_auths[sid->num_auths - 1];
429 msg = ldb_msg_new(mem_ctx);
430 W_ERROR_HAVE_NO_MEMORY(msg);
432 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
433 W_ERROR_HAVE_NO_MEMORY(msg->dn);
435 msg->num_elements = in->object.attribute_ctr.num_attributes;
436 msg->elements = talloc_array(msg, struct ldb_message_element,
438 W_ERROR_HAVE_NO_MEMORY(msg->elements);
440 md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
441 W_ERROR_HAVE_NO_MEMORY(md);
445 md->ctr.ctr1.count = in->meta_data_ctr->count;
446 md->ctr.ctr1.reserved = 0;
447 md->ctr.ctr1.array = talloc_array(mem_ctx,
448 struct replPropertyMetaData1,
450 W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
452 for (i=0, attr_count=0; i < in->meta_data_ctr->count; i++, attr_count++) {
453 struct drsuapi_DsReplicaAttribute *a;
454 struct drsuapi_DsReplicaMetaData *d;
455 struct replPropertyMetaData1 *m;
456 struct ldb_message_element *e;
459 a = &in->object.attribute_ctr.attributes[i];
460 d = &in->meta_data_ctr->meta_data[i];
461 m = &md->ctr.ctr1.array[attr_count];
462 e = &msg->elements[attr_count];
464 if (dsdb_attid_in_list(ignore_attids, a->attid)) {
469 if (GUID_all_zero(&d->originating_invocation_id)) {
470 status = WERR_DS_SRC_GUID_MISMATCH;
471 DEBUG(0, ("Refusing replication of object containing invalid zero invocationID on attribute %d of %s: %s\n",
473 ldb_dn_get_linearized(msg->dn),
474 win_errstr(status)));
478 if (a->attid == DRSUAPI_ATTID_instanceType) {
479 if (instanceType_e != NULL) {
485 for (j=0; j<a->value_ctr.num_values; j++) {
486 status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob,
489 if (!W_ERROR_IS_OK(status)) {
493 if (W_ERROR_EQUAL(status, WERR_TOO_MANY_SECRETS)) {
494 WERROR get_name_status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
495 a, msg->elements, e, NULL);
496 if (W_ERROR_IS_OK(get_name_status)) {
497 DEBUG(0, ("Unxpectedly got secret value %s on %s from DRS server\n",
498 e->name, ldb_dn_get_linearized(msg->dn)));
500 DEBUG(0, ("Unxpectedly got secret value on %s from DRS server",
501 ldb_dn_get_linearized(msg->dn)));
503 } else if (!W_ERROR_IS_OK(status)) {
508 * This function also fills in the local attid value,
509 * based on comparing the remote and local prefixMap
510 * tables. If we don't convert the value, then we can
511 * have invalid values in the replPropertyMetaData we
512 * store on disk, as the prefixMap is per host, not
513 * per-domain. This may be why Microsoft added the
514 * msDS-IntID feature, however this is not used for
515 * extra attributes in the schema partition itself.
517 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
520 W_ERROR_NOT_OK_RETURN(status);
522 m->version = d->version;
523 m->originating_change_time = d->originating_change_time;
524 m->originating_invocation_id = d->originating_invocation_id;
525 m->originating_usn = d->originating_usn;
528 if (a->attid == DRSUAPI_ATTID_name) {
529 const struct ldb_val *rdn_val = ldb_dn_get_rdn_val(msg->dn);
530 if (rdn_val == NULL) {
531 DEBUG(0, ("Unxpectedly unable to get RDN from %s for validation",
532 ldb_dn_get_linearized(msg->dn)));
535 if (e->num_values != 1) {
536 DEBUG(0, ("Unxpectedly got wrong number of attribute values (got %u, expected 1) when checking RDN against name of %s",
538 ldb_dn_get_linearized(msg->dn)));
541 if (data_blob_cmp(rdn_val,
542 &e->values[0]) != 0) {
543 DEBUG(0, ("Unxpectedly got mismatching RDN values when checking RDN against name of %s",
544 ldb_dn_get_linearized(msg->dn)));
548 if (d->originating_change_time > whenChanged) {
549 whenChanged = d->originating_change_time;
554 msg->num_elements = attr_count;
555 md->ctr.ctr1.count = attr_count;
557 if (instanceType_e == NULL) {
561 instanceType = ldb_msg_find_attr_as_int(msg, "instanceType", 0);
563 if ((instanceType & INSTANCE_TYPE_IS_NC_HEAD)
564 && partition_dn != NULL) {
565 int partition_dn_cmp = ldb_dn_compare(partition_dn, msg->dn);
566 if (partition_dn_cmp != 0) {
567 DEBUG(4, ("Remote server advised us of a new partition %s while processing %s, ignoring\n",
568 ldb_dn_get_linearized(msg->dn),
569 ldb_dn_get_linearized(partition_dn)));
570 return WERR_DS_ADD_REPLICA_INHIBITED;
574 if (dsdb_repl_flags & DSDB_REPL_FLAG_PARTIAL_REPLICA) {
575 /* the instanceType type for partial_replica
576 replication is sent via DRS with TYPE_WRITE set, but
577 must be used on the client with TYPE_WRITE removed
579 if (instanceType & INSTANCE_TYPE_WRITE) {
581 * Make sure we do not change the order
585 * instanceType_e->num_values = 0
587 * ldb_msg_remove_attr(msg, "instanceType");
589 struct ldb_message_element *e;
591 e = ldb_msg_find_element(msg, "instanceType");
592 if (e != instanceType_e) {
593 DEBUG(0,("instanceType_e[%p] changed to e[%p]\n",
598 instanceType_e->num_values = 0;
600 instanceType &= ~INSTANCE_TYPE_WRITE;
601 if (ldb_msg_add_fmt(msg, "instanceType", "%d", instanceType) != LDB_SUCCESS) {
602 return WERR_INTERNAL_ERROR;
606 if (!(instanceType & INSTANCE_TYPE_WRITE)) {
607 DEBUG(0, ("Refusing to replicate %s from a read-only repilca into a read-write replica!\n",
608 ldb_dn_get_linearized(msg->dn)));
609 return WERR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA;
613 whenChanged_t = nt_time_to_unix(whenChanged);
614 whenChanged_s = ldb_timestring(msg, whenChanged_t);
615 W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
617 out->object_guid = in->object.identifier->guid;
619 if (in->parent_object_guid == NULL) {
620 out->parent_guid = NULL;
622 out->parent_guid = talloc(mem_ctx, struct GUID);
623 W_ERROR_HAVE_NO_MEMORY(out->parent_guid);
624 *out->parent_guid = *in->parent_object_guid;
628 out->when_changed = whenChanged_s;
633 WERROR dsdb_replicated_objects_convert(struct ldb_context *ldb,
634 const struct dsdb_schema *schema,
635 struct ldb_dn *partition_dn,
636 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
637 uint32_t object_count,
638 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
639 uint32_t linked_attributes_count,
640 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
641 const struct repsFromTo1 *source_dsa,
642 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
643 const DATA_BLOB *gensec_skey,
644 uint32_t dsdb_repl_flags,
646 struct dsdb_extended_replicated_objects **objects)
649 struct dsdb_schema_prefixmap *pfm_remote;
650 struct dsdb_extended_replicated_objects *out;
651 const struct drsuapi_DsReplicaObjectListItemEx *cur;
652 struct dsdb_syntax_ctx syntax_ctx;
655 out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
656 W_ERROR_HAVE_NO_MEMORY(out);
657 out->version = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
658 out->dsdb_repl_flags = dsdb_repl_flags;
661 * Ensure schema is kept valid for as long as 'out'
662 * which may contain pointers to it
664 schema = talloc_reference(out, schema);
665 W_ERROR_HAVE_NO_MEMORY(schema);
667 status = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
668 out, &pfm_remote, NULL);
669 if (!W_ERROR_IS_OK(status)) {
670 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
671 win_errstr(status)));
676 /* use default syntax conversion context */
677 dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
678 syntax_ctx.pfm_remote = pfm_remote;
680 if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
682 * check for schema changes in case
683 * we are not replicating Schema NC
685 status = dsdb_schema_info_cmp(schema, mapping_ctr);
686 if (!W_ERROR_IS_OK(status)) {
687 DEBUG(4,("Can't replicate %s because remote schema has changed since we last replicated the schema\n",
688 ldb_dn_get_linearized(partition_dn)));
694 out->partition_dn = partition_dn;
696 out->source_dsa = source_dsa;
697 out->uptodateness_vector= uptodateness_vector;
699 out->num_objects = 0;
700 out->objects = talloc_array(out,
701 struct dsdb_extended_replicated_object,
703 W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
705 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
706 if (i == object_count) {
711 status = dsdb_convert_object_ex(ldb, schema, out->partition_dn,
717 &out->objects[out->num_objects]);
720 * Check to see if we have been advised of a
721 * subdomain or new application partition. We don't
722 * want to start on that here, instead the caller
723 * should consider if it would like to replicate it
724 * based on the cross-ref object.
726 if (W_ERROR_EQUAL(status, WERR_DS_ADD_REPLICA_INHIBITED)) {
730 if (!W_ERROR_IS_OK(status)) {
732 DEBUG(0,("Failed to convert object %s: %s\n",
733 cur->object.identifier->dn,
734 win_errstr(status)));
738 /* Assuming we didn't skip or error, increment the number of objects */
741 out->objects = talloc_realloc(out, out->objects,
742 struct dsdb_extended_replicated_object,
744 if (out->num_objects != 0 && out->objects == NULL) {
748 if (i != object_count) {
753 out->linked_attributes = talloc_array(out,
754 struct drsuapi_DsReplicaLinkedAttribute,
755 linked_attributes_count);
756 W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->linked_attributes, out);
758 for (i=0; i < linked_attributes_count; i++) {
759 const struct drsuapi_DsReplicaLinkedAttribute *ra = &linked_attributes[i];
760 struct drsuapi_DsReplicaLinkedAttribute *la = &out->linked_attributes[i];
762 if (ra->identifier == NULL) {
764 return WERR_BAD_NET_RESP;
769 la->identifier = talloc_zero(out->linked_attributes,
770 struct drsuapi_DsReplicaObjectIdentifier);
771 W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->identifier, out);
774 * We typically only get the guid filled
775 * and the repl_meta_data module only cares abouf
778 la->identifier->guid = ra->identifier->guid;
780 if (ra->value.blob != NULL) {
781 la->value.blob = talloc_zero(out->linked_attributes,
783 W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->value.blob, out);
785 if (ra->value.blob->length != 0) {
786 *la->value.blob = data_blob_dup_talloc(la->value.blob,
788 W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->value.blob->data, out);
792 status = dsdb_attribute_drsuapi_remote_to_local(&syntax_ctx,
796 if (!W_ERROR_IS_OK(status)) {
797 DEBUG(0,(__location__": linked_attribute[%u] attid 0x%08X not found: %s\n",
798 i, ra->attid, win_errstr(status)));
803 out->linked_attributes_count = linked_attributes_count;
805 /* free pfm_remote, we won't need it anymore */
806 talloc_free(pfm_remote);
813 * Commits a list of replicated objects.
815 * @param working_schema dsdb_schema to be used for resolving
816 * Classes/Attributes during Schema replication. If not NULL,
817 * it will be set on ldb and used while committing replicated objects
819 WERROR dsdb_replicated_objects_commit(struct ldb_context *ldb,
820 struct dsdb_schema *working_schema,
821 struct dsdb_extended_replicated_objects *objects,
822 uint64_t *notify_uSN)
825 struct ldb_result *ext_res;
826 struct dsdb_schema *cur_schema = NULL;
827 struct dsdb_schema *new_schema = NULL;
829 uint64_t seq_num1, seq_num2;
830 bool used_global_schema = false;
832 TALLOC_CTX *tmp_ctx = talloc_new(objects);
834 DEBUG(0,("Failed to start talloc\n"));
835 return WERR_NOT_ENOUGH_MEMORY;
838 /* wrap the extended operation in a transaction
839 See [MS-DRSR] 3.3.2 Transactions
841 ret = ldb_transaction_start(ldb);
842 if (ret != LDB_SUCCESS) {
843 DEBUG(0,(__location__ " Failed to start transaction: %s\n",
844 ldb_errstring(ldb)));
848 ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
849 if (ret != LDB_SUCCESS) {
850 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
851 ldb_transaction_cancel(ldb);
852 TALLOC_FREE(tmp_ctx);
857 * Set working_schema for ldb in case we are replicating from Schema NC.
858 * Schema won't be reloaded during Replicated Objects commit, as it is
859 * done in a transaction. So we need some way to search for newly
860 * added Classes and Attributes
862 if (working_schema) {
863 /* store current schema so we can fall back in case of failure */
864 cur_schema = dsdb_get_schema(ldb, tmp_ctx);
865 used_global_schema = dsdb_uses_global_schema(ldb);
867 ret = dsdb_reference_schema(ldb, working_schema, false);
868 if (ret != LDB_SUCCESS) {
869 DEBUG(0,(__location__ "Failed to reference working schema - %s\n",
871 /* TODO: Map LDB Error to NTSTATUS? */
872 ldb_transaction_cancel(ldb);
873 TALLOC_FREE(tmp_ctx);
874 return WERR_INTERNAL_ERROR;
878 ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
879 if (ret != LDB_SUCCESS) {
880 /* restore previous schema */
881 if (used_global_schema) {
882 dsdb_set_global_schema(ldb);
883 } else if (cur_schema) {
884 dsdb_reference_schema(ldb, cur_schema, false);
887 if (!W_ERROR_EQUAL(objects->error, WERR_DS_DRA_MISSING_PARENT)) {
888 DEBUG(1,("Failed to apply records: %s: %s\n",
889 ldb_errstring(ldb), ldb_strerror(ret)));
891 DEBUG(3,("Missing parent while attempting to apply records: %s\n",
892 ldb_errstring(ldb)));
894 ldb_transaction_cancel(ldb);
895 TALLOC_FREE(tmp_ctx);
897 if (!W_ERROR_IS_OK(objects->error)) {
898 return objects->error;
902 talloc_free(ext_res);
904 /* Save our updated prefixMap */
905 if (working_schema) {
906 werr = dsdb_write_prefixes_from_schema_to_ldb(working_schema,
909 if (!W_ERROR_IS_OK(werr)) {
910 /* restore previous schema */
911 if (used_global_schema) {
912 dsdb_set_global_schema(ldb);
913 } else if (cur_schema ) {
914 dsdb_reference_schema(ldb, cur_schema, false);
916 DEBUG(0,("Failed to save updated prefixMap: %s\n",
918 TALLOC_FREE(tmp_ctx);
923 ret = ldb_transaction_prepare_commit(ldb);
924 if (ret != LDB_SUCCESS) {
925 /* restore previous schema */
926 if (used_global_schema) {
927 dsdb_set_global_schema(ldb);
928 } else if (cur_schema ) {
929 dsdb_reference_schema(ldb, cur_schema, false);
931 DEBUG(0,(__location__ " Failed to prepare commit of transaction: %s\n",
932 ldb_errstring(ldb)));
933 TALLOC_FREE(tmp_ctx);
937 ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
938 if (ret != LDB_SUCCESS) {
939 /* restore previous schema */
940 if (used_global_schema) {
941 dsdb_set_global_schema(ldb);
942 } else if (cur_schema ) {
943 dsdb_reference_schema(ldb, cur_schema, false);
945 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
946 ldb_transaction_cancel(ldb);
947 TALLOC_FREE(tmp_ctx);
951 ret = ldb_transaction_commit(ldb);
952 if (ret != LDB_SUCCESS) {
953 /* restore previous schema */
954 if (used_global_schema) {
955 dsdb_set_global_schema(ldb);
956 } else if (cur_schema ) {
957 dsdb_reference_schema(ldb, cur_schema, false);
959 DEBUG(0,(__location__ " Failed to commit transaction\n"));
960 TALLOC_FREE(tmp_ctx);
964 if (seq_num1 > *notify_uSN) {
966 * A notify was already required before
967 * the current transaction.
969 } else if (objects->originating_updates) {
971 * Applying the replicated changes
972 * required originating updates,
973 * so a notify is required.
977 * There's no need to notify the
978 * server about the change we just from it.
980 *notify_uSN = seq_num2;
984 * Reset the Schema used by ldb. This will lead to
985 * a schema cache being refreshed from database.
987 if (working_schema) {
988 /* Reload the schema */
989 new_schema = dsdb_get_schema(ldb, tmp_ctx);
991 * If dsdb_get_schema() fails, we just fall back
992 * to what we had. However, the database is probably
993 * unable to operate for other users from this
995 if (new_schema == NULL || new_schema == working_schema) {
996 DBG_ERR("Failed to re-load schema after commit of "
997 "transaction (working: %p/%"PRIu64", new: "
998 "%p/%"PRIu64")\n", new_schema,
1000 new_schema->metadata_usn : 0,
1001 working_schema, working_schema->metadata_usn);
1002 dsdb_reference_schema(ldb, cur_schema, false);
1003 if (used_global_schema) {
1004 dsdb_set_global_schema(ldb);
1006 TALLOC_FREE(tmp_ctx);
1007 return WERR_INTERNAL_ERROR;
1008 } else if (used_global_schema) {
1009 dsdb_make_schema_global(ldb, new_schema);
1013 DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
1014 objects->num_objects, objects->linked_attributes_count,
1015 ldb_dn_get_linearized(objects->partition_dn)));
1017 TALLOC_FREE(tmp_ctx);
1021 static WERROR dsdb_origin_object_convert(struct ldb_context *ldb,
1022 const struct dsdb_schema *schema,
1023 const struct drsuapi_DsReplicaObjectListItem *in,
1024 TALLOC_CTX *mem_ctx,
1025 struct ldb_message **_msg)
1029 struct ldb_message *msg;
1031 if (!in->object.identifier) {
1035 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
1039 msg = ldb_msg_new(mem_ctx);
1040 W_ERROR_HAVE_NO_MEMORY(msg);
1042 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
1043 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1045 msg->num_elements = in->object.attribute_ctr.num_attributes;
1046 msg->elements = talloc_array(msg, struct ldb_message_element,
1048 W_ERROR_HAVE_NO_MEMORY(msg->elements);
1050 for (i=0; i < msg->num_elements; i++) {
1051 struct drsuapi_DsReplicaAttribute *a;
1052 struct ldb_message_element *e;
1054 a = &in->object.attribute_ctr.attributes[i];
1055 e = &msg->elements[i];
1057 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, schema->prefixmap,
1058 a, msg->elements, e, NULL);
1059 W_ERROR_NOT_OK_RETURN(status);
1068 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
1069 TALLOC_CTX *mem_ctx,
1070 const struct drsuapi_DsReplicaObjectListItem *first_object,
1072 uint32_t dsdb_repl_flags,
1073 struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
1076 const struct dsdb_schema *schema;
1077 const struct drsuapi_DsReplicaObjectListItem *cur;
1078 struct ldb_message **objects;
1079 struct drsuapi_DsReplicaObjectIdentifier2 *ids;
1081 uint32_t num_objects = 0;
1082 const char * const attrs[] = {
1087 struct ldb_result *res;
1090 for (cur = first_object; cur; cur = cur->next_object) {
1094 if (num_objects == 0) {
1098 ret = ldb_transaction_start(ldb);
1099 if (ret != LDB_SUCCESS) {
1100 return WERR_DS_INTERNAL_FAILURE;
1103 objects = talloc_array(mem_ctx, struct ldb_message *,
1105 if (objects == NULL) {
1106 status = WERR_NOT_ENOUGH_MEMORY;
1110 schema = dsdb_get_schema(ldb, objects);
1112 return WERR_DS_SCHEMA_NOT_LOADED;
1115 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
1116 status = dsdb_origin_object_convert(ldb, schema, cur,
1117 objects, &objects[i]);
1118 if (!W_ERROR_IS_OK(status)) {
1123 ids = talloc_array(mem_ctx,
1124 struct drsuapi_DsReplicaObjectIdentifier2,
1127 status = WERR_NOT_ENOUGH_MEMORY;
1131 if (dsdb_repl_flags & DSDB_REPL_FLAG_ADD_NCNAME) {
1132 /* check for possible NC creation */
1133 for (i=0; i < num_objects; i++) {
1134 struct ldb_message *msg = objects[i];
1135 struct ldb_message_element *el;
1136 struct ldb_dn *nc_dn;
1138 if (ldb_msg_check_string_attribute(msg, "objectClass", "crossRef") == 0) {
1141 el = ldb_msg_find_element(msg, "nCName");
1142 if (el == NULL || el->num_values != 1) {
1145 nc_dn = ldb_dn_from_ldb_val(objects, ldb, &el->values[0]);
1146 if (!ldb_dn_validate(nc_dn)) {
1149 ret = dsdb_create_partial_replica_NC(ldb, nc_dn);
1150 if (ret != LDB_SUCCESS) {
1151 status = WERR_DS_INTERNAL_FAILURE;
1157 for (i=0; i < num_objects; i++) {
1158 struct dom_sid *sid = NULL;
1159 struct ldb_request *add_req;
1161 DEBUG(6,(__location__ ": adding %s\n",
1162 ldb_dn_get_linearized(objects[i]->dn)));
1164 ret = ldb_build_add_req(&add_req,
1170 ldb_op_default_callback,
1172 if (ret != LDB_SUCCESS) {
1173 status = WERR_DS_INTERNAL_FAILURE;
1177 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
1178 if (ret != LDB_SUCCESS) {
1179 status = WERR_DS_INTERNAL_FAILURE;
1183 ret = ldb_request(ldb, add_req);
1184 if (ret == LDB_SUCCESS) {
1185 ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
1187 if (ret != LDB_SUCCESS) {
1188 DEBUG(0,(__location__ ": Failed add of %s - %s\n",
1189 ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
1190 status = WERR_DS_INTERNAL_FAILURE;
1194 talloc_free(add_req);
1196 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
1197 LDB_SCOPE_BASE, attrs,
1199 if (ret != LDB_SUCCESS) {
1200 status = WERR_DS_INTERNAL_FAILURE;
1203 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
1204 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
1208 ZERO_STRUCT(ids[i].sid);
1212 ret = ldb_transaction_commit(ldb);
1213 if (ret != LDB_SUCCESS) {
1214 return WERR_DS_INTERNAL_FAILURE;
1217 talloc_free(objects);
1219 *_num = num_objects;
1224 talloc_free(objects);
1225 ldb_transaction_cancel(ldb);