s4:dsdb:common: Fix code spelling
[Samba.git] / source4 / dsdb / repl / replicated_objects.c
blob83d1b421c458428194f95109b01cf43e84603107
1 /*
2 Unix SMB/CIFS Implementation.
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/>.
22 #include "includes.h"
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 "../libcli/drsuapi/drsuapi.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "param/param.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_DRS_REPL
36 static WERROR dsdb_repl_merge_working_schema(struct ldb_context *ldb,
37 struct dsdb_schema *dest_schema,
38 const struct dsdb_schema *ref_schema)
40 const struct dsdb_class *cur_class = NULL;
41 const struct dsdb_attribute *cur_attr = NULL;
42 int ret;
44 for (cur_class = ref_schema->classes;
45 cur_class;
46 cur_class = cur_class->next)
48 const struct dsdb_class *tmp1;
49 struct dsdb_class *tmp2;
51 tmp1 = dsdb_class_by_governsID_id(dest_schema,
52 cur_class->governsID_id);
53 if (tmp1 != NULL) {
54 continue;
58 * Do a shallow copy so that original next and prev are
59 * not modified, we don't need to do a deep copy
60 * as the rest won't be modified and this is for
61 * a short lived object.
63 tmp2 = talloc(dest_schema, struct dsdb_class);
64 if (tmp2 == NULL) {
65 return WERR_NOT_ENOUGH_MEMORY;
67 *tmp2 = *cur_class;
68 DLIST_ADD(dest_schema->classes, tmp2);
71 for (cur_attr = ref_schema->attributes;
72 cur_attr;
73 cur_attr = cur_attr->next)
75 const struct dsdb_attribute *tmp1;
76 struct dsdb_attribute *tmp2;
78 tmp1 = dsdb_attribute_by_attributeID_id(dest_schema,
79 cur_attr->attributeID_id);
80 if (tmp1 != NULL) {
81 continue;
85 * Do a shallow copy so that original next and prev are
86 * not modified, we don't need to do a deep copy
87 * as the rest won't be modified and this is for
88 * a short lived object.
90 tmp2 = talloc(dest_schema, struct dsdb_attribute);
91 if (tmp2 == NULL) {
92 return WERR_NOT_ENOUGH_MEMORY;
94 *tmp2 = *cur_attr;
95 DLIST_ADD(dest_schema->attributes, tmp2);
98 ret = dsdb_setup_sorted_accessors(ldb, dest_schema);
99 if (LDB_SUCCESS != ret) {
100 DEBUG(0,("Failed to add new attribute to reference schema!\n"));
101 return WERR_INTERNAL_ERROR;
104 return WERR_OK;
107 WERROR dsdb_repl_resolve_working_schema(struct ldb_context *ldb,
108 struct dsdb_schema_prefixmap *pfm_remote,
109 uint32_t cycle_before_switching,
110 struct dsdb_schema *initial_schema,
111 struct dsdb_schema *resulting_schema,
112 uint32_t object_count,
113 const struct drsuapi_DsReplicaObjectListItemEx *first_object)
115 struct schema_list {
116 struct schema_list *next, *prev;
117 const struct drsuapi_DsReplicaObjectListItemEx *obj;
119 struct schema_list *schema_list = NULL, *schema_list_item, *schema_list_next_item;
120 WERROR werr;
121 struct dsdb_schema *working_schema;
122 const struct drsuapi_DsReplicaObjectListItemEx *cur;
123 DATA_BLOB empty_key = data_blob_null;
124 int ret, pass_no;
125 uint32_t ignore_attids[] = {
126 DRSUAPI_ATTID_auxiliaryClass,
127 DRSUAPI_ATTID_mayContain,
128 DRSUAPI_ATTID_mustContain,
129 DRSUAPI_ATTID_possSuperiors,
130 DRSUAPI_ATTID_systemPossSuperiors,
131 DRSUAPI_ATTID_INVALID
133 TALLOC_CTX *frame = talloc_stackframe();
135 /* create a list of objects yet to be converted */
136 for (cur = first_object; cur; cur = cur->next_object) {
137 schema_list_item = talloc(frame, struct schema_list);
138 if (schema_list_item == NULL) {
139 return WERR_NOT_ENOUGH_MEMORY;
142 schema_list_item->obj = cur;
143 DLIST_ADD_END(schema_list, schema_list_item);
146 /* resolve objects until all are resolved and in local schema */
147 pass_no = 1;
148 working_schema = initial_schema;
150 while (schema_list) {
151 uint32_t converted_obj_count = 0;
152 uint32_t failed_obj_count = 0;
154 if (resulting_schema != working_schema) {
156 * If the selfmade schema is not the schema used to
157 * translate and validate replicated object,
158 * Which means that we are using the bootstrap schema
159 * Then we add attributes and classes that were already
160 * translated to the working schema, the idea is that
161 * we might need to add new attributes and classes
162 * to be able to translate critical replicated objects
163 * and without that we wouldn't be able to translate them
165 werr = dsdb_repl_merge_working_schema(ldb,
166 working_schema,
167 resulting_schema);
168 if (!W_ERROR_IS_OK(werr)) {
169 talloc_free(frame);
170 return werr;
174 for (schema_list_item = schema_list;
175 schema_list_item;
176 schema_list_item=schema_list_next_item) {
177 struct dsdb_extended_replicated_object object;
179 cur = schema_list_item->obj;
182 * Save the next item, now we have saved out
183 * the current one, so we can DLIST_REMOVE it
184 * safely
186 schema_list_next_item = schema_list_item->next;
189 * Convert the objects into LDB messages using the
190 * schema we have so far. It's ok if we fail to convert
191 * an object. We should convert more objects on next pass.
193 werr = dsdb_convert_object_ex(ldb, working_schema,
194 NULL,
195 pfm_remote,
196 cur, &empty_key,
197 ignore_attids,
199 schema_list_item, &object);
200 if (!W_ERROR_IS_OK(werr)) {
201 DEBUG(4,("debug: Failed to convert schema "
202 "object %s into ldb msg, "
203 "will try during next loop\n",
204 cur->object.identifier->dn));
206 failed_obj_count++;
207 } else {
209 * Convert the schema from ldb_message format
210 * (OIDs as OID strings) into schema, using
211 * the remote prefixMap
213 * It's not likely, but possible to get the
214 * same object twice and we should keep
215 * the last instance.
217 werr = dsdb_schema_set_el_from_ldb_msg_dups(ldb,
218 resulting_schema,
219 object.msg,
220 true);
221 if (!W_ERROR_IS_OK(werr)) {
222 DEBUG(4,("debug: failed to convert "
223 "object %s into a schema element, "
224 "will try during next loop: %s\n",
225 ldb_dn_get_linearized(object.msg->dn),
226 win_errstr(werr)));
227 failed_obj_count++;
228 } else {
229 DEBUG(8,("Converted object %s into a schema element\n",
230 ldb_dn_get_linearized(object.msg->dn)));
231 DLIST_REMOVE(schema_list, schema_list_item);
232 TALLOC_FREE(schema_list_item);
233 converted_obj_count++;
238 DEBUG(4,("Schema load pass %d: converted %d, %d of %d objects left to be converted.\n",
239 pass_no, converted_obj_count, failed_obj_count, object_count));
241 /* check if we converted any objects in this pass */
242 if (converted_obj_count == 0) {
243 DEBUG(0,("Can't continue Schema load: "
244 "didn't manage to convert any objects: "
245 "all %d remaining of %d objects "
246 "failed to convert\n",
247 failed_obj_count, object_count));
248 talloc_free(frame);
249 return WERR_INTERNAL_ERROR;
253 * Don't try to load the schema if there is missing object
254 * _and_ we are on the first pass as some critical objects
255 * might be missing.
257 if (failed_obj_count == 0 || pass_no > cycle_before_switching) {
258 /* prepare for another cycle */
259 working_schema = resulting_schema;
261 ret = dsdb_setup_sorted_accessors(ldb, working_schema);
262 if (LDB_SUCCESS != ret) {
263 DEBUG(0,("Failed to create schema-cache indexes!\n"));
264 talloc_free(frame);
265 return WERR_INTERNAL_ERROR;
268 pass_no++;
271 talloc_free(frame);
272 return WERR_OK;
276 * Multi-pass working schema creation
277 * Function will:
278 * - shallow copy initial schema supplied
279 * - create a working schema in multiple passes
280 * until all objects are resolved
281 * Working schema is a schema with Attributes, Classes
282 * and indexes, but w/o subClassOf, possibleSupperiors etc.
283 * It is to be used just us cache for converting attribute values.
285 WERROR dsdb_repl_make_working_schema(struct ldb_context *ldb,
286 const struct dsdb_schema *initial_schema,
287 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
288 uint32_t object_count,
289 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
290 const DATA_BLOB *gensec_skey,
291 TALLOC_CTX *mem_ctx,
292 struct dsdb_schema **_schema_out)
294 WERROR werr;
295 struct dsdb_schema_prefixmap *pfm_remote;
296 uint32_t r;
297 struct dsdb_schema *working_schema;
299 /* make a copy of the iniatial_scheam so we don't mess with it */
300 working_schema = dsdb_schema_copy_shallow(mem_ctx, ldb, initial_schema);
301 if (!working_schema) {
302 DEBUG(0,(__location__ ": schema copy failed!\n"));
303 return WERR_NOT_ENOUGH_MEMORY;
305 working_schema->resolving_in_progress = true;
307 /* we are going to need remote prefixMap for decoding */
308 werr = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
309 working_schema, &pfm_remote, NULL);
310 if (!W_ERROR_IS_OK(werr)) {
311 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
312 win_errstr(werr)));
313 talloc_free(working_schema);
314 return werr;
317 for (r=0; r < pfm_remote->length; r++) {
318 const struct dsdb_schema_prefixmap_oid *rm = &pfm_remote->prefixes[r];
319 bool found_oid = false;
320 uint32_t l;
322 for (l=0; l < working_schema->prefixmap->length; l++) {
323 const struct dsdb_schema_prefixmap_oid *lm = &working_schema->prefixmap->prefixes[l];
324 int cmp;
326 cmp = data_blob_cmp(&rm->bin_oid, &lm->bin_oid);
327 if (cmp == 0) {
328 found_oid = true;
329 break;
333 if (found_oid) {
334 continue;
338 * We prefer the same is as we got from the remote peer
339 * if there's no conflict.
341 werr = dsdb_schema_pfm_add_entry(working_schema->prefixmap,
342 rm->bin_oid, &rm->id, NULL);
343 if (!W_ERROR_IS_OK(werr)) {
344 DEBUG(0,(__location__ ": Failed to merge remote prefixMap: %s",
345 win_errstr(werr)));
346 talloc_free(working_schema);
347 return werr;
351 werr = dsdb_repl_resolve_working_schema(ldb,
352 pfm_remote,
353 0, /* cycle_before_switching */
354 working_schema,
355 working_schema,
356 object_count,
357 first_object);
358 if (!W_ERROR_IS_OK(werr)) {
359 DEBUG(0, ("%s: dsdb_repl_resolve_working_schema() failed: %s",
360 __location__, win_errstr(werr)));
361 talloc_free(working_schema);
362 return werr;
365 working_schema->resolving_in_progress = false;
367 *_schema_out = working_schema;
369 return WERR_OK;
372 static bool dsdb_attid_in_list(const uint32_t attid_list[], uint32_t attid)
374 const uint32_t *cur;
375 if (!attid_list) {
376 return false;
378 for (cur = attid_list; *cur != DRSUAPI_ATTID_INVALID; cur++) {
379 if (*cur == attid) {
380 return true;
383 return false;
386 WERROR dsdb_convert_object_ex(struct ldb_context *ldb,
387 const struct dsdb_schema *schema,
388 struct ldb_dn *partition_dn,
389 const struct dsdb_schema_prefixmap *pfm_remote,
390 const struct drsuapi_DsReplicaObjectListItemEx *in,
391 const DATA_BLOB *gensec_skey,
392 const uint32_t *ignore_attids,
393 uint32_t dsdb_repl_flags,
394 TALLOC_CTX *mem_ctx,
395 struct dsdb_extended_replicated_object *out)
397 WERROR status = WERR_OK;
398 uint32_t i;
399 struct ldb_message *msg;
400 struct replPropertyMetaDataBlob *md;
401 int instanceType;
402 struct ldb_message_element *instanceType_e = NULL;
403 NTTIME whenChanged = 0;
404 time_t whenChanged_t;
405 const char *whenChanged_s;
406 struct dom_sid *sid = NULL;
407 uint32_t rid = 0;
408 uint32_t attr_count;
410 if (!in->object.identifier) {
411 return WERR_FOOBAR;
414 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
415 return WERR_FOOBAR;
418 if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
419 return WERR_FOOBAR;
422 if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
423 return WERR_FOOBAR;
426 sid = &in->object.identifier->sid;
427 if (sid->num_auths > 0) {
428 rid = sid->sub_auths[sid->num_auths - 1];
431 msg = ldb_msg_new(mem_ctx);
432 W_ERROR_HAVE_NO_MEMORY(msg);
434 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
435 W_ERROR_HAVE_NO_MEMORY(msg->dn);
437 msg->num_elements = in->object.attribute_ctr.num_attributes;
438 msg->elements = talloc_array(msg, struct ldb_message_element,
439 msg->num_elements);
440 W_ERROR_HAVE_NO_MEMORY(msg->elements);
442 md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
443 W_ERROR_HAVE_NO_MEMORY(md);
445 md->version = 1;
446 md->reserved = 0;
447 md->ctr.ctr1.count = in->meta_data_ctr->count;
448 md->ctr.ctr1.reserved = 0;
449 md->ctr.ctr1.array = talloc_array(mem_ctx,
450 struct replPropertyMetaData1,
451 md->ctr.ctr1.count);
452 W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
454 for (i=0, attr_count=0; i < in->meta_data_ctr->count; i++, attr_count++) {
455 struct drsuapi_DsReplicaAttribute *a;
456 struct drsuapi_DsReplicaMetaData *d;
457 struct replPropertyMetaData1 *m;
458 struct ldb_message_element *e;
459 uint32_t j;
461 a = &in->object.attribute_ctr.attributes[i];
462 d = &in->meta_data_ctr->meta_data[i];
463 m = &md->ctr.ctr1.array[attr_count];
464 e = &msg->elements[attr_count];
466 if (dsdb_attid_in_list(ignore_attids, a->attid)) {
467 attr_count--;
468 continue;
471 if (GUID_all_zero(&d->originating_invocation_id)) {
472 status = WERR_DS_SRC_GUID_MISMATCH;
473 DEBUG(0, ("Refusing replication of object containing invalid zero invocationID on attribute %d of %s: %s\n",
474 a->attid,
475 ldb_dn_get_linearized(msg->dn),
476 win_errstr(status)));
477 return status;
480 if (a->attid == DRSUAPI_ATTID_instanceType) {
481 if (instanceType_e != NULL) {
482 return WERR_FOOBAR;
484 instanceType_e = e;
487 for (j=0; j<a->value_ctr.num_values; j++) {
488 status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob,
489 gensec_skey, rid,
490 dsdb_repl_flags, a);
491 if (!W_ERROR_IS_OK(status)) {
492 break;
495 if (W_ERROR_EQUAL(status, WERR_TOO_MANY_SECRETS)) {
496 WERROR get_name_status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
497 a, msg->elements, e, NULL);
498 if (W_ERROR_IS_OK(get_name_status)) {
499 DEBUG(0, ("Unxpectedly got secret value %s on %s from DRS server\n",
500 e->name, ldb_dn_get_linearized(msg->dn)));
501 } else {
502 DEBUG(0, ("Unxpectedly got secret value on %s from DRS server",
503 ldb_dn_get_linearized(msg->dn)));
505 } else if (!W_ERROR_IS_OK(status)) {
506 return status;
510 * This function also fills in the local attid value,
511 * based on comparing the remote and local prefixMap
512 * tables. If we don't convert the value, then we can
513 * have invalid values in the replPropertyMetaData we
514 * store on disk, as the prefixMap is per host, not
515 * per-domain. This may be why Microsoft added the
516 * msDS-IntID feature, however this is not used for
517 * extra attributes in the schema partition itself.
519 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
520 a, msg->elements, e,
521 &m->attid);
522 W_ERROR_NOT_OK_RETURN(status);
524 m->version = d->version;
525 m->originating_change_time = d->originating_change_time;
526 m->originating_invocation_id = d->originating_invocation_id;
527 m->originating_usn = d->originating_usn;
528 m->local_usn = 0;
530 if (a->attid == DRSUAPI_ATTID_name) {
531 const struct ldb_val *rdn_val = ldb_dn_get_rdn_val(msg->dn);
532 if (rdn_val == NULL) {
533 DEBUG(0, ("Unxpectedly unable to get RDN from %s for validation",
534 ldb_dn_get_linearized(msg->dn)));
535 return WERR_FOOBAR;
537 if (e->num_values != 1) {
538 DEBUG(0, ("Unxpectedly got wrong number of attribute values (got %u, expected 1) when checking RDN against name of %s",
539 e->num_values,
540 ldb_dn_get_linearized(msg->dn)));
541 return WERR_FOOBAR;
543 if (data_blob_cmp(rdn_val,
544 &e->values[0]) != 0) {
545 DEBUG(0, ("Unxpectedly got mismatching RDN values when checking RDN against name of %s",
546 ldb_dn_get_linearized(msg->dn)));
547 return WERR_FOOBAR;
550 if (d->originating_change_time > whenChanged) {
551 whenChanged = d->originating_change_time;
556 msg->num_elements = attr_count;
557 md->ctr.ctr1.count = attr_count;
559 if (instanceType_e == NULL) {
560 return WERR_FOOBAR;
563 instanceType = ldb_msg_find_attr_as_int(msg, "instanceType", 0);
565 if ((instanceType & INSTANCE_TYPE_IS_NC_HEAD)
566 && partition_dn != NULL) {
567 int partition_dn_cmp = ldb_dn_compare(partition_dn, msg->dn);
568 if (partition_dn_cmp != 0) {
569 DEBUG(4, ("Remote server advised us of a new partition %s while processing %s, ignoring\n",
570 ldb_dn_get_linearized(msg->dn),
571 ldb_dn_get_linearized(partition_dn)));
572 return WERR_DS_ADD_REPLICA_INHIBITED;
576 if (dsdb_repl_flags & DSDB_REPL_FLAG_PARTIAL_REPLICA) {
577 /* the instanceType type for partial_replica
578 replication is sent via DRS with TYPE_WRITE set, but
579 must be used on the client with TYPE_WRITE removed
581 if (instanceType & INSTANCE_TYPE_WRITE) {
583 * Make sure we do not change the order
584 * of msg->elements!
586 * That's why we use
587 * instanceType_e->num_values = 0
588 * instead of
589 * ldb_msg_remove_attr(msg, "instanceType");
591 struct ldb_message_element *e;
593 e = ldb_msg_find_element(msg, "instanceType");
594 if (e != instanceType_e) {
595 DEBUG(0,("instanceType_e[%p] changed to e[%p]\n",
596 instanceType_e, e));
597 return WERR_FOOBAR;
600 instanceType_e->num_values = 0;
602 instanceType &= ~INSTANCE_TYPE_WRITE;
603 if (ldb_msg_add_fmt(msg, "instanceType", "%d", instanceType) != LDB_SUCCESS) {
604 return WERR_INTERNAL_ERROR;
607 } else {
608 if (!(instanceType & INSTANCE_TYPE_WRITE)) {
609 DBG_ERR("Refusing to replicate %s from a read-only "
610 "replica into a read-write replica!\n",
611 ldb_dn_get_linearized(msg->dn));
612 return WERR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA;
616 whenChanged_t = nt_time_to_unix(whenChanged);
617 whenChanged_s = ldb_timestring(msg, whenChanged_t);
618 W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
620 out->object_guid = in->object.identifier->guid;
622 if (in->parent_object_guid == NULL) {
623 out->parent_guid = NULL;
624 } else {
625 out->parent_guid = talloc(mem_ctx, struct GUID);
626 W_ERROR_HAVE_NO_MEMORY(out->parent_guid);
627 *out->parent_guid = *in->parent_object_guid;
630 out->msg = msg;
631 out->when_changed = whenChanged_s;
632 out->meta_data = md;
633 return WERR_OK;
636 WERROR dsdb_replicated_objects_convert(struct ldb_context *ldb,
637 const struct dsdb_schema *schema,
638 struct ldb_dn *partition_dn,
639 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
640 uint32_t object_count,
641 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
642 uint32_t linked_attributes_count,
643 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
644 const struct repsFromTo1 *source_dsa,
645 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
646 const DATA_BLOB *gensec_skey,
647 uint32_t dsdb_repl_flags,
648 TALLOC_CTX *mem_ctx,
649 struct dsdb_extended_replicated_objects **objects)
651 WERROR status;
652 struct dsdb_schema_prefixmap *pfm_remote;
653 struct dsdb_extended_replicated_objects *out;
654 const struct drsuapi_DsReplicaObjectListItemEx *cur;
655 struct dsdb_syntax_ctx syntax_ctx;
656 uint32_t i;
658 out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
659 W_ERROR_HAVE_NO_MEMORY(out);
660 out->version = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
661 out->dsdb_repl_flags = dsdb_repl_flags;
664 * Ensure schema is kept valid for as long as 'out'
665 * which may contain pointers to it
667 schema = talloc_reference(out, schema);
668 W_ERROR_HAVE_NO_MEMORY(schema);
670 status = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
671 out, &pfm_remote, NULL);
672 if (!W_ERROR_IS_OK(status)) {
673 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
674 win_errstr(status)));
675 talloc_free(out);
676 return status;
679 /* use default syntax conversion context */
680 dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
681 syntax_ctx.pfm_remote = pfm_remote;
683 if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
685 * check for schema changes in case
686 * we are not replicating Schema NC
688 status = dsdb_schema_info_cmp(schema, mapping_ctr);
689 if (!W_ERROR_IS_OK(status)) {
690 DEBUG(4,("Can't replicate %s because remote schema has changed since we last replicated the schema\n",
691 ldb_dn_get_linearized(partition_dn)));
692 talloc_free(out);
693 return status;
697 out->partition_dn = partition_dn;
699 out->source_dsa = source_dsa;
700 out->uptodateness_vector= uptodateness_vector;
702 out->num_objects = 0;
703 out->objects = talloc_array(out,
704 struct dsdb_extended_replicated_object,
705 object_count);
706 W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
708 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
709 if (i == object_count) {
710 talloc_free(out);
711 return WERR_FOOBAR;
714 status = dsdb_convert_object_ex(ldb, schema, out->partition_dn,
715 pfm_remote,
716 cur, gensec_skey,
717 NULL,
718 dsdb_repl_flags,
719 out->objects,
720 &out->objects[out->num_objects]);
723 * Check to see if we have been advised of a
724 * subdomain or new application partition. We don't
725 * want to start on that here, instead the caller
726 * should consider if it would like to replicate it
727 * based on the cross-ref object.
729 if (W_ERROR_EQUAL(status, WERR_DS_ADD_REPLICA_INHIBITED)) {
730 struct GUID_txt_buf guid_str;
731 DBG_ERR("Ignoring object outside partition %s %s: %s\n",
732 GUID_buf_string(&cur->object.identifier->guid,
733 &guid_str),
734 cur->object.identifier->dn,
735 win_errstr(status));
736 continue;
739 if (!W_ERROR_IS_OK(status)) {
740 talloc_free(out);
741 DEBUG(0,("Failed to convert object %s: %s\n",
742 cur->object.identifier->dn,
743 win_errstr(status)));
744 return status;
747 /* Assuming we didn't skip or error, increment the number of objects */
748 out->num_objects++;
751 DBG_INFO("Processed %"PRIu32" DRS objects, saw %"PRIu32" objects "
752 "and expected %"PRIu32" objects\n",
753 out->num_objects, i, object_count);
755 out->objects = talloc_realloc(out, out->objects,
756 struct dsdb_extended_replicated_object,
757 out->num_objects);
758 if (out->num_objects != 0 && out->objects == NULL) {
759 DBG_ERR("FAILURE: talloc_realloc() failed after "
760 "processing %"PRIu32" DRS objects!\n",
761 out->num_objects);
762 talloc_free(out);
763 return WERR_FOOBAR;
765 if (i != object_count) {
766 DBG_ERR("FAILURE: saw %"PRIu32" DRS objects, server said we "
767 "should expect to see %"PRIu32" objects!\n",
768 i, object_count);
769 talloc_free(out);
770 return WERR_FOOBAR;
773 out->linked_attributes = talloc_array(out,
774 struct drsuapi_DsReplicaLinkedAttribute,
775 linked_attributes_count);
776 W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->linked_attributes, out);
778 for (i=0; i < linked_attributes_count; i++) {
779 const struct drsuapi_DsReplicaLinkedAttribute *ra = &linked_attributes[i];
780 struct drsuapi_DsReplicaLinkedAttribute *la = &out->linked_attributes[i];
782 if (ra->identifier == NULL) {
783 talloc_free(out);
784 return WERR_BAD_NET_RESP;
787 *la = *ra;
789 la->identifier = talloc_zero(out->linked_attributes,
790 struct drsuapi_DsReplicaObjectIdentifier);
791 W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->identifier, out);
794 * We typically only get the guid filled
795 * and the repl_meta_data module only cares abouf
796 * the guid.
798 la->identifier->guid = ra->identifier->guid;
800 if (ra->value.blob != NULL) {
801 la->value.blob = talloc_zero(out->linked_attributes,
802 DATA_BLOB);
803 W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->value.blob, out);
805 if (ra->value.blob->length != 0) {
806 *la->value.blob = data_blob_dup_talloc(la->value.blob,
807 *ra->value.blob);
808 W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->value.blob->data, out);
812 status = dsdb_attribute_drsuapi_remote_to_local(&syntax_ctx,
813 ra->attid,
814 &la->attid,
815 NULL);
816 if (!W_ERROR_IS_OK(status)) {
817 DEBUG(0,(__location__": linked_attribute[%u] attid 0x%08X not found: %s\n",
818 i, ra->attid, win_errstr(status)));
819 return status;
823 out->linked_attributes_count = linked_attributes_count;
825 /* free pfm_remote, we won't need it anymore */
826 talloc_free(pfm_remote);
828 *objects = out;
829 return WERR_OK;
833 * Commits a list of replicated objects.
835 * @param working_schema dsdb_schema to be used for resolving
836 * Classes/Attributes during Schema replication. If not NULL,
837 * it will be set on ldb and used while committing replicated objects
839 WERROR dsdb_replicated_objects_commit(struct ldb_context *ldb,
840 struct dsdb_schema *working_schema,
841 struct dsdb_extended_replicated_objects *objects,
842 uint64_t *notify_uSN)
844 WERROR werr;
845 struct ldb_result *ext_res;
846 struct dsdb_schema *cur_schema = NULL;
847 struct dsdb_schema *new_schema = NULL;
848 int ret;
849 uint64_t seq_num1, seq_num2;
850 bool used_global_schema = false;
852 TALLOC_CTX *tmp_ctx = talloc_new(objects);
853 if (!tmp_ctx) {
854 DEBUG(0,("Failed to start talloc\n"));
855 return WERR_NOT_ENOUGH_MEMORY;
858 /* wrap the extended operation in a transaction
859 See [MS-DRSR] 3.3.2 Transactions
861 ret = ldb_transaction_start(ldb);
862 if (ret != LDB_SUCCESS) {
863 DEBUG(0,(__location__ " Failed to start transaction: %s\n",
864 ldb_errstring(ldb)));
865 return WERR_FOOBAR;
868 ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
869 if (ret != LDB_SUCCESS) {
870 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
871 ldb_transaction_cancel(ldb);
872 TALLOC_FREE(tmp_ctx);
873 return WERR_FOOBAR;
877 * Set working_schema for ldb in case we are replicating from Schema NC.
878 * Schema won't be reloaded during Replicated Objects commit, as it is
879 * done in a transaction. So we need some way to search for newly
880 * added Classes and Attributes
882 if (working_schema) {
883 /* store current schema so we can fall back in case of failure */
884 cur_schema = dsdb_get_schema(ldb, tmp_ctx);
885 used_global_schema = dsdb_uses_global_schema(ldb);
887 ret = dsdb_reference_schema(ldb, working_schema, SCHEMA_MEMORY_ONLY);
888 if (ret != LDB_SUCCESS) {
889 DEBUG(0,(__location__ "Failed to reference working schema - %s\n",
890 ldb_strerror(ret)));
891 /* TODO: Map LDB Error to NTSTATUS? */
892 ldb_transaction_cancel(ldb);
893 TALLOC_FREE(tmp_ctx);
894 return WERR_INTERNAL_ERROR;
898 ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
899 if (ret != LDB_SUCCESS) {
900 /* restore previous schema */
901 if (used_global_schema) {
902 dsdb_set_global_schema(ldb);
903 } else if (cur_schema) {
904 dsdb_reference_schema(ldb, cur_schema, SCHEMA_MEMORY_ONLY);
907 if (W_ERROR_EQUAL(objects->error, WERR_DS_DRA_RECYCLED_TARGET)) {
908 DEBUG(3,("Missing target while attempting to apply records: %s\n",
909 ldb_errstring(ldb)));
910 } else if (W_ERROR_EQUAL(objects->error, WERR_DS_DRA_MISSING_PARENT)) {
911 DEBUG(3,("Missing parent while attempting to apply records: %s\n",
912 ldb_errstring(ldb)));
913 } else {
914 DEBUG(1,("Failed to apply records: %s: %s\n",
915 ldb_errstring(ldb), ldb_strerror(ret)));
917 ldb_transaction_cancel(ldb);
918 TALLOC_FREE(tmp_ctx);
920 if (!W_ERROR_IS_OK(objects->error)) {
921 return objects->error;
923 return WERR_FOOBAR;
925 talloc_free(ext_res);
927 /* Save our updated prefixMap and check the schema is good. */
928 if (working_schema) {
929 struct ldb_result *ext_res_2;
931 werr = dsdb_write_prefixes_from_schema_to_ldb(working_schema,
932 ldb,
933 working_schema);
934 if (!W_ERROR_IS_OK(werr)) {
935 /* restore previous schema */
936 if (used_global_schema) {
937 dsdb_set_global_schema(ldb);
938 } else if (cur_schema ) {
939 dsdb_reference_schema(ldb,
940 cur_schema,
941 SCHEMA_MEMORY_ONLY);
943 DEBUG(0,("Failed to save updated prefixMap: %s\n",
944 win_errstr(werr)));
945 ldb_transaction_cancel(ldb);
946 TALLOC_FREE(tmp_ctx);
947 return werr;
951 * Use dsdb_schema_from_db through dsdb extended to check we
952 * can load the schema currently sitting in the transaction.
953 * We need this check because someone might have written to
954 * the schema or prefixMap before we started the transaction,
955 * which may have caused corruption.
957 ret = ldb_extended(ldb, DSDB_EXTENDED_SCHEMA_LOAD,
958 NULL, &ext_res_2);
960 if (ret != LDB_SUCCESS) {
961 if (used_global_schema) {
962 dsdb_set_global_schema(ldb);
963 } else if (cur_schema) {
964 dsdb_reference_schema(ldb, cur_schema, SCHEMA_MEMORY_ONLY);
966 DEBUG(0,("Corrupt schema write attempt detected, "
967 "aborting schema modification operation.\n"
968 "This probably happened due to bad timing of "
969 "another schema edit: %s (%s)\n",
970 ldb_errstring(ldb),
971 ldb_strerror(ret)));
972 ldb_transaction_cancel(ldb);
973 TALLOC_FREE(tmp_ctx);
974 return WERR_FOOBAR;
978 ret = ldb_transaction_prepare_commit(ldb);
979 if (ret != LDB_SUCCESS) {
980 /* restore previous schema */
981 if (used_global_schema) {
982 dsdb_set_global_schema(ldb);
983 } else if (cur_schema ) {
984 dsdb_reference_schema(ldb, cur_schema, SCHEMA_MEMORY_ONLY);
986 DBG_ERR(" Failed to prepare commit of transaction: %s (%s)\n",
987 ldb_errstring(ldb),
988 ldb_strerror(ret));
989 TALLOC_FREE(tmp_ctx);
990 return WERR_FOOBAR;
993 ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
994 if (ret != LDB_SUCCESS) {
995 /* restore previous schema */
996 if (used_global_schema) {
997 dsdb_set_global_schema(ldb);
998 } else if (cur_schema ) {
999 dsdb_reference_schema(ldb, cur_schema, SCHEMA_MEMORY_ONLY);
1001 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
1002 ldb_transaction_cancel(ldb);
1003 TALLOC_FREE(tmp_ctx);
1004 return WERR_FOOBAR;
1007 ret = ldb_transaction_commit(ldb);
1008 if (ret != LDB_SUCCESS) {
1009 /* restore previous schema */
1010 if (used_global_schema) {
1011 dsdb_set_global_schema(ldb);
1012 } else if (cur_schema ) {
1013 dsdb_reference_schema(ldb, cur_schema, SCHEMA_MEMORY_ONLY);
1015 DEBUG(0,(__location__ " Failed to commit transaction\n"));
1016 TALLOC_FREE(tmp_ctx);
1017 return WERR_FOOBAR;
1020 if (seq_num1 > *notify_uSN) {
1022 * A notify was already required before
1023 * the current transaction.
1025 } else if (objects->originating_updates) {
1027 * Applying the replicated changes
1028 * required originating updates,
1029 * so a notify is required.
1031 } else {
1033 * There's no need to notify the
1034 * server about the change we just from it.
1036 *notify_uSN = seq_num2;
1040 * Reset the Schema used by ldb. This will lead to
1041 * a schema cache being refreshed from database.
1043 if (working_schema) {
1044 /* Reload the schema */
1045 new_schema = dsdb_get_schema(ldb, tmp_ctx);
1046 /* TODO:
1047 * If dsdb_get_schema() fails, we just fall back
1048 * to what we had. However, the database is probably
1049 * unable to operate for other users from this
1050 * point... */
1051 if (new_schema == NULL || new_schema == working_schema) {
1052 DBG_ERR("Failed to re-load schema after commit of "
1053 "transaction (working: %p/%"PRIu64", new: "
1054 "%p/%"PRIu64")\n", new_schema,
1055 new_schema != NULL ?
1056 new_schema->metadata_usn : 0,
1057 working_schema, working_schema->metadata_usn);
1058 dsdb_reference_schema(ldb, cur_schema, SCHEMA_MEMORY_ONLY);
1059 if (used_global_schema) {
1060 dsdb_set_global_schema(ldb);
1062 TALLOC_FREE(tmp_ctx);
1063 return WERR_INTERNAL_ERROR;
1064 } else if (used_global_schema) {
1065 dsdb_make_schema_global(ldb, new_schema);
1069 DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
1070 objects->num_objects, objects->linked_attributes_count,
1071 ldb_dn_get_linearized(objects->partition_dn)));
1073 TALLOC_FREE(tmp_ctx);
1074 return WERR_OK;
1077 static WERROR dsdb_origin_object_convert(struct ldb_context *ldb,
1078 const struct dsdb_schema *schema,
1079 const struct drsuapi_DsReplicaObjectListItem *in,
1080 TALLOC_CTX *mem_ctx,
1081 struct ldb_message **_msg)
1083 WERROR status;
1084 unsigned int i;
1085 struct ldb_message *msg;
1087 if (!in->object.identifier) {
1088 return WERR_FOOBAR;
1091 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
1092 return WERR_FOOBAR;
1095 msg = ldb_msg_new(mem_ctx);
1096 W_ERROR_HAVE_NO_MEMORY(msg);
1098 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
1099 W_ERROR_HAVE_NO_MEMORY(msg->dn);
1101 msg->num_elements = in->object.attribute_ctr.num_attributes;
1102 msg->elements = talloc_array(msg, struct ldb_message_element,
1103 msg->num_elements);
1104 W_ERROR_HAVE_NO_MEMORY(msg->elements);
1106 for (i=0; i < msg->num_elements; i++) {
1107 struct drsuapi_DsReplicaAttribute *a;
1108 struct ldb_message_element *e;
1110 a = &in->object.attribute_ctr.attributes[i];
1111 e = &msg->elements[i];
1113 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, schema->prefixmap,
1114 a, msg->elements, e, NULL);
1115 W_ERROR_NOT_OK_RETURN(status);
1119 *_msg = msg;
1121 return WERR_OK;
1124 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
1125 TALLOC_CTX *mem_ctx,
1126 const struct drsuapi_DsReplicaObjectListItem *first_object,
1127 uint32_t *_num,
1128 uint32_t dsdb_repl_flags,
1129 struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
1131 WERROR status;
1132 const struct dsdb_schema *schema;
1133 const struct drsuapi_DsReplicaObjectListItem *cur;
1134 struct ldb_message **objects;
1135 struct drsuapi_DsReplicaObjectIdentifier2 *ids;
1136 uint32_t i;
1137 uint32_t num_objects = 0;
1138 const char * const attrs[] = {
1139 "objectGUID",
1140 "objectSid",
1141 NULL
1143 struct ldb_result *res;
1144 int ret;
1146 for (cur = first_object; cur; cur = cur->next_object) {
1147 num_objects++;
1150 if (num_objects == 0) {
1151 return WERR_OK;
1154 ret = ldb_transaction_start(ldb);
1155 if (ret != LDB_SUCCESS) {
1156 return WERR_DS_INTERNAL_FAILURE;
1159 objects = talloc_array(mem_ctx, struct ldb_message *,
1160 num_objects);
1161 if (objects == NULL) {
1162 status = WERR_NOT_ENOUGH_MEMORY;
1163 goto cancel;
1166 schema = dsdb_get_schema(ldb, objects);
1167 if (!schema) {
1168 return WERR_DS_SCHEMA_NOT_LOADED;
1171 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
1172 status = dsdb_origin_object_convert(ldb, schema, cur,
1173 objects, &objects[i]);
1174 if (!W_ERROR_IS_OK(status)) {
1175 goto cancel;
1179 ids = talloc_array(mem_ctx,
1180 struct drsuapi_DsReplicaObjectIdentifier2,
1181 num_objects);
1182 if (ids == NULL) {
1183 status = WERR_NOT_ENOUGH_MEMORY;
1184 goto cancel;
1187 if (dsdb_repl_flags & DSDB_REPL_FLAG_ADD_NCNAME) {
1188 /* check for possible NC creation */
1189 for (i=0; i < num_objects; i++) {
1190 struct ldb_message *msg = objects[i];
1191 struct ldb_message_element *el;
1192 struct ldb_dn *nc_dn;
1194 if (ldb_msg_check_string_attribute(msg, "objectClass", "crossRef") == 0) {
1195 continue;
1197 el = ldb_msg_find_element(msg, "nCName");
1198 if (el == NULL || el->num_values != 1) {
1199 continue;
1201 nc_dn = ldb_dn_from_ldb_val(objects, ldb, &el->values[0]);
1202 if (!ldb_dn_validate(nc_dn)) {
1203 continue;
1205 ret = dsdb_create_partial_replica_NC(ldb, nc_dn);
1206 if (ret != LDB_SUCCESS) {
1207 status = WERR_DS_INTERNAL_FAILURE;
1208 goto cancel;
1213 for (i=0; i < num_objects; i++) {
1214 struct dom_sid *sid = NULL;
1215 struct ldb_request *add_req;
1217 DEBUG(6,(__location__ ": adding %s\n",
1218 ldb_dn_get_linearized(objects[i]->dn)));
1220 ret = ldb_build_add_req(&add_req,
1221 ldb,
1222 objects,
1223 objects[i],
1224 NULL,
1225 NULL,
1226 ldb_op_default_callback,
1227 NULL);
1228 if (ret != LDB_SUCCESS) {
1229 status = WERR_DS_INTERNAL_FAILURE;
1230 goto cancel;
1233 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
1234 if (ret != LDB_SUCCESS) {
1235 status = WERR_DS_INTERNAL_FAILURE;
1236 goto cancel;
1239 ret = ldb_request(ldb, add_req);
1240 if (ret == LDB_SUCCESS) {
1241 ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
1243 if (ret != LDB_SUCCESS) {
1244 DEBUG(0,(__location__ ": Failed add of %s - %s\n",
1245 ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
1246 status = WERR_DS_INTERNAL_FAILURE;
1247 goto cancel;
1250 talloc_free(add_req);
1252 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
1253 LDB_SCOPE_BASE, attrs,
1254 "(objectClass=*)");
1255 if (ret != LDB_SUCCESS) {
1256 status = WERR_DS_INTERNAL_FAILURE;
1257 goto cancel;
1259 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
1260 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
1261 if (sid) {
1262 ids[i].sid = *sid;
1263 } else {
1264 ZERO_STRUCT(ids[i].sid);
1268 ret = ldb_transaction_commit(ldb);
1269 if (ret != LDB_SUCCESS) {
1270 return WERR_DS_INTERNAL_FAILURE;
1273 talloc_free(objects);
1275 *_num = num_objects;
1276 *_ids = ids;
1277 return WERR_OK;
1279 cancel:
1280 talloc_free(objects);
1281 ldb_transaction_cancel(ldb);
1282 return status;