s4-repl: Use ldb_dn_new() to create the rootDSE DN
[Samba/reqa.git] / source4 / dsdb / repl / replicated_objects.c
blobdadb9a189120e1c247f4959b48c89dfa9767cd79
1 /*
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/>.
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 "../lib/crypto/crypto.h"
30 #include "../libcli/drsuapi/drsuapi.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "param/param.h"
34 /**
35 * Multi-pass working schema creation
36 * Function will:
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,
50 TALLOC_CTX *mem_ctx,
51 struct dsdb_schema **_schema_out)
53 struct schema_list {
54 struct schema_list *next, *prev;
55 const struct drsuapi_DsReplicaObjectListItemEx *obj;
58 WERROR werr;
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;
63 int ret, pass_no;
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,
70 DRSUAPI_ATTID_INVALID
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"));
77 return WERR_NOMEM;
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",
85 win_errstr(werr)));
86 return werr;
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 */
97 pass_no = 1;
99 while (schema_list) {
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
112 * safely */
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,
121 cur, gensec_skey,
122 ignore_attids,
124 tmp_ctx, &object);
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));
129 failed_obj_count++;
130 } else {
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,
137 working_schema,
138 object.msg);
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),
142 win_errstr(werr)));
143 failed_obj_count++;
144 } else {
145 DLIST_REMOVE(schema_list, schema_list_item);
146 talloc_free(schema_list_item);
147 converted_obj_count++;
151 talloc_free(tmp_ctx);
153 DEBUG(4,("Schema load pass %d: %d/%d of %d objects left to be converted.\n",
154 pass_no, failed_obj_count, converted_obj_count, object_count));
155 pass_no++;
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;
173 return WERR_OK;
176 static bool dsdb_attid_in_list(const uint32_t attid_list[], uint32_t attid)
178 const uint32_t *cur;
179 if (!attid_list) {
180 return false;
182 for (cur = attid_list; *cur != DRSUAPI_ATTID_INVALID; cur++) {
183 if (*cur == attid) {
184 return true;
187 return false;
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,
197 TALLOC_CTX *mem_ctx,
198 struct dsdb_extended_replicated_object *out)
200 NTSTATUS nt_status;
201 WERROR status;
202 uint32_t i;
203 struct ldb_message *msg;
204 struct replPropertyMetaDataBlob *md;
205 int instanceType;
206 struct ldb_val guid_value;
207 struct ldb_val parent_guid_value;
208 NTTIME whenChanged = 0;
209 time_t whenChanged_t;
210 const char *whenChanged_s;
211 const char *rdn_name = NULL;
212 const struct ldb_val *rdn_value = NULL;
213 const struct dsdb_attribute *rdn_attr = NULL;
214 uint32_t rdn_attid;
215 struct drsuapi_DsReplicaAttribute *name_a = NULL;
216 struct drsuapi_DsReplicaMetaData *name_d = NULL;
217 struct replPropertyMetaData1 *rdn_m = NULL;
218 struct dom_sid *sid = NULL;
219 uint32_t rid = 0;
220 uint32_t attr_count;
221 int ret;
223 if (!in->object.identifier) {
224 return WERR_FOOBAR;
227 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
228 return WERR_FOOBAR;
231 if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
232 return WERR_FOOBAR;
235 if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
236 return WERR_FOOBAR;
239 sid = &in->object.identifier->sid;
240 if (sid->num_auths > 0) {
241 rid = sid->sub_auths[sid->num_auths - 1];
244 msg = ldb_msg_new(mem_ctx);
245 W_ERROR_HAVE_NO_MEMORY(msg);
247 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
248 W_ERROR_HAVE_NO_MEMORY(msg->dn);
250 rdn_name = ldb_dn_get_rdn_name(msg->dn);
251 rdn_attr = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
252 if (!rdn_attr) {
253 return WERR_FOOBAR;
255 rdn_attid = rdn_attr->attributeID_id;
256 rdn_value = ldb_dn_get_rdn_val(msg->dn);
258 msg->num_elements = in->object.attribute_ctr.num_attributes;
259 msg->elements = talloc_array(msg, struct ldb_message_element,
260 msg->num_elements);
261 W_ERROR_HAVE_NO_MEMORY(msg->elements);
263 md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
264 W_ERROR_HAVE_NO_MEMORY(md);
266 md->version = 1;
267 md->reserved = 0;
268 md->ctr.ctr1.count = in->meta_data_ctr->count;
269 md->ctr.ctr1.reserved = 0;
270 md->ctr.ctr1.array = talloc_array(mem_ctx,
271 struct replPropertyMetaData1,
272 md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
273 W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
275 for (i=0, attr_count=0; i < in->meta_data_ctr->count; i++, attr_count++) {
276 struct drsuapi_DsReplicaAttribute *a;
277 struct drsuapi_DsReplicaMetaData *d;
278 struct replPropertyMetaData1 *m;
279 struct ldb_message_element *e;
280 uint32_t j;
282 a = &in->object.attribute_ctr.attributes[i];
283 d = &in->meta_data_ctr->meta_data[i];
284 m = &md->ctr.ctr1.array[attr_count];
285 e = &msg->elements[attr_count];
287 if (dsdb_attid_in_list(ignore_attids, a->attid)) {
288 attr_count--;
289 continue;
292 for (j=0; j<a->value_ctr.num_values; j++) {
293 status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob, gensec_skey, rid, a);
294 W_ERROR_NOT_OK_RETURN(status);
297 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
298 a, msg->elements, e);
299 W_ERROR_NOT_OK_RETURN(status);
301 m->attid = a->attid;
302 m->version = d->version;
303 m->originating_change_time = d->originating_change_time;
304 m->originating_invocation_id = d->originating_invocation_id;
305 m->originating_usn = d->originating_usn;
306 m->local_usn = 0;
308 if (d->originating_change_time > whenChanged) {
309 whenChanged = d->originating_change_time;
312 if (a->attid == DRSUAPI_ATTID_name) {
313 name_a = a;
314 name_d = d;
318 msg->num_elements = attr_count;
319 md->ctr.ctr1.count = attr_count;
320 if (name_a) {
321 rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
324 if (rdn_m) {
325 struct ldb_message_element *el;
326 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
327 if (!el) {
328 ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
329 if (ret != LDB_SUCCESS) {
330 return WERR_FOOBAR;
332 } else {
333 if (el->num_values != 1) {
334 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
335 el->num_values));
336 return WERR_FOOBAR;
338 if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
339 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
340 (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
341 (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
342 return WERR_FOOBAR;
346 rdn_m->attid = rdn_attid;
347 rdn_m->version = name_d->version;
348 rdn_m->originating_change_time = name_d->originating_change_time;
349 rdn_m->originating_invocation_id = name_d->originating_invocation_id;
350 rdn_m->originating_usn = name_d->originating_usn;
351 rdn_m->local_usn = 0;
352 md->ctr.ctr1.count++;
356 instanceType = ldb_msg_find_attr_as_int(msg, "instanceType", 0);
357 if (dsdb_repl_flags & DSDB_REPL_FLAG_PARTIAL_REPLICA) {
358 /* the instanceType type for partial_replica
359 replication is sent via DRS with TYPE_WRITE set, but
360 must be used on the client with TYPE_WRITE removed
362 if (instanceType & INSTANCE_TYPE_WRITE) {
363 instanceType &= ~INSTANCE_TYPE_WRITE;
364 ldb_msg_remove_attr(msg, "instanceType");
365 if (ldb_msg_add_fmt(msg, "instanceType", "%d", instanceType) != LDB_SUCCESS) {
366 return WERR_INTERNAL_ERROR;
369 } else {
370 if (!(instanceType & INSTANCE_TYPE_WRITE)) {
371 DEBUG(0, ("Refusing to replicate %s from a read-only repilca into a read-write replica!\n",
372 ldb_dn_get_linearized(msg->dn)));
373 return WERR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA;
377 whenChanged_t = nt_time_to_unix(whenChanged);
378 whenChanged_s = ldb_timestring(msg, whenChanged_t);
379 W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
381 nt_status = GUID_to_ndr_blob(&in->object.identifier->guid, msg, &guid_value);
382 if (!NT_STATUS_IS_OK(nt_status)) {
383 return ntstatus_to_werror(nt_status);
386 if (in->parent_object_guid) {
387 nt_status = GUID_to_ndr_blob(in->parent_object_guid, msg, &parent_guid_value);
388 if (!NT_STATUS_IS_OK(nt_status)) {
389 return ntstatus_to_werror(nt_status);
391 } else {
392 parent_guid_value = data_blob_null;
395 out->msg = msg;
396 out->guid_value = guid_value;
397 out->parent_guid_value = parent_guid_value;
398 out->when_changed = whenChanged_s;
399 out->meta_data = md;
400 return WERR_OK;
403 WERROR dsdb_replicated_objects_convert(struct ldb_context *ldb,
404 const struct dsdb_schema *schema,
405 const char *partition_dn_str,
406 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
407 uint32_t object_count,
408 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
409 uint32_t linked_attributes_count,
410 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
411 const struct repsFromTo1 *source_dsa,
412 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
413 const DATA_BLOB *gensec_skey,
414 uint32_t dsdb_repl_flags,
415 TALLOC_CTX *mem_ctx,
416 struct dsdb_extended_replicated_objects **objects)
418 WERROR status;
419 struct ldb_dn *partition_dn;
420 struct dsdb_schema_prefixmap *pfm_remote;
421 struct dsdb_extended_replicated_objects *out;
422 const struct drsuapi_DsReplicaObjectListItemEx *cur;
423 uint32_t i;
425 out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
426 W_ERROR_HAVE_NO_MEMORY(out);
427 out->version = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
428 out->dsdb_repl_flags = dsdb_repl_flags;
431 * Ensure schema is kept valid for as long as 'out'
432 * which may contain pointers to it
434 schema = talloc_reference(out, schema);
435 W_ERROR_HAVE_NO_MEMORY(schema);
437 partition_dn = ldb_dn_new(out, ldb, partition_dn_str);
438 W_ERROR_HAVE_NO_MEMORY_AND_FREE(partition_dn, out);
440 status = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
441 out, &pfm_remote, NULL);
442 if (!W_ERROR_IS_OK(status)) {
443 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s",
444 win_errstr(status)));
445 talloc_free(out);
446 return status;
449 if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
451 * check for schema changes in case
452 * we are not replicating Schema NC
454 status = dsdb_schema_info_cmp(schema, mapping_ctr);
455 if (!W_ERROR_IS_OK(status)) {
456 DEBUG(1,("Remote schema has changed while replicating %s\n",
457 partition_dn_str));
458 talloc_free(out);
459 return status;
463 out->partition_dn = partition_dn;
465 out->source_dsa = source_dsa;
466 out->uptodateness_vector= uptodateness_vector;
468 out->num_objects = object_count;
469 out->objects = talloc_array(out,
470 struct dsdb_extended_replicated_object,
471 out->num_objects);
472 W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
474 /* pass the linked attributes down to the repl_meta_data
475 module */
476 out->linked_attributes_count = linked_attributes_count;
477 out->linked_attributes = linked_attributes;
479 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
480 if (i == out->num_objects) {
481 talloc_free(out);
482 return WERR_FOOBAR;
485 status = dsdb_convert_object_ex(ldb, schema, pfm_remote,
486 cur, gensec_skey,
487 NULL,
488 dsdb_repl_flags,
489 out->objects, &out->objects[i]);
490 if (!W_ERROR_IS_OK(status)) {
491 talloc_free(out);
492 DEBUG(0,("Failed to convert object %s: %s\n",
493 cur->object.identifier->dn,
494 win_errstr(status)));
495 return status;
498 if (i != out->num_objects) {
499 talloc_free(out);
500 return WERR_FOOBAR;
503 /* free pfm_remote, we won't need it anymore */
504 talloc_free(pfm_remote);
506 *objects = out;
507 return WERR_OK;
511 * Commits a list of replicated objects.
513 * @param working_schema dsdb_schema to be used for resolving
514 * Classes/Attributes during Schema replication. If not NULL,
515 * it will be set on ldb and used while committing replicated objects
517 WERROR dsdb_replicated_objects_commit(struct ldb_context *ldb,
518 struct dsdb_schema *working_schema,
519 struct dsdb_extended_replicated_objects *objects,
520 uint64_t *notify_uSN)
522 WERROR werr;
523 struct ldb_result *ext_res;
524 struct dsdb_schema *cur_schema = NULL;
525 int ret;
526 uint64_t seq_num1, seq_num2;
528 /* TODO: handle linked attributes */
530 /* wrap the extended operation in a transaction
531 See [MS-DRSR] 3.3.2 Transactions
533 ret = ldb_transaction_start(ldb);
534 if (ret != LDB_SUCCESS) {
535 DEBUG(0,(__location__ " Failed to start transaction\n"));
536 return WERR_FOOBAR;
539 ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
540 if (ret != LDB_SUCCESS) {
541 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
542 ldb_transaction_cancel(ldb);
543 return WERR_FOOBAR;
547 * Set working_schema for ldb in case we are replicating from Schema NC.
548 * Schema won't be reloaded during Replicated Objects commit, as it is
549 * done in a transaction. So we need some way to search for newly
550 * added Classes and Attributes
552 if (working_schema) {
553 /* store current schema so we can fall back in case of failure */
554 cur_schema = dsdb_get_schema(ldb, working_schema);
556 ret = dsdb_reference_schema(ldb, working_schema, false);
557 if (ret != LDB_SUCCESS) {
558 DEBUG(0,(__location__ "Failed to reference working schema - %s\n",
559 ldb_strerror(ret)));
560 /* TODO: Map LDB Error to NTSTATUS? */
561 ldb_transaction_cancel(ldb);
562 return WERR_INTERNAL_ERROR;
566 ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
567 if (ret != LDB_SUCCESS) {
568 /* restore previous schema */
569 if (cur_schema ) {
570 dsdb_reference_schema(ldb, cur_schema, false);
571 dsdb_make_schema_global(ldb, cur_schema);
574 DEBUG(0,("Failed to apply records: %s: %s\n",
575 ldb_errstring(ldb), ldb_strerror(ret)));
576 ldb_transaction_cancel(ldb);
577 return WERR_FOOBAR;
579 talloc_free(ext_res);
581 /* Save our updated prefixMap */
582 if (working_schema) {
583 werr = dsdb_write_prefixes_from_schema_to_ldb(working_schema,
584 ldb,
585 working_schema);
586 if (!W_ERROR_IS_OK(werr)) {
587 /* restore previous schema */
588 if (cur_schema ) {
589 dsdb_reference_schema(ldb, cur_schema, false);
590 dsdb_make_schema_global(ldb, cur_schema);
592 DEBUG(0,("Failed to save updated prefixMap: %s\n",
593 win_errstr(werr)));
594 return werr;
598 ret = ldb_transaction_prepare_commit(ldb);
599 if (ret != LDB_SUCCESS) {
600 /* restore previous schema */
601 if (cur_schema ) {
602 dsdb_reference_schema(ldb, cur_schema, false);
603 dsdb_make_schema_global(ldb, cur_schema);
605 DEBUG(0,(__location__ " Failed to prepare commit of transaction: %s\n",
606 ldb_errstring(ldb)));
607 return WERR_FOOBAR;
610 ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
611 if (ret != LDB_SUCCESS) {
612 /* restore previous schema */
613 if (cur_schema ) {
614 dsdb_reference_schema(ldb, cur_schema, false);
615 dsdb_make_schema_global(ldb, cur_schema);
617 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
618 ldb_transaction_cancel(ldb);
619 return WERR_FOOBAR;
622 /* if this replication partner didn't need to be notified
623 before this transaction then it still doesn't need to be
624 notified, as the changes came from this server */
625 if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
626 *notify_uSN = seq_num2;
629 ret = ldb_transaction_commit(ldb);
630 if (ret != LDB_SUCCESS) {
631 /* restore previous schema */
632 if (cur_schema ) {
633 dsdb_reference_schema(ldb, cur_schema, false);
634 dsdb_make_schema_global(ldb, cur_schema);
636 DEBUG(0,(__location__ " Failed to commit transaction\n"));
637 return WERR_FOOBAR;
641 * Reset the Schema used by ldb. This will lead to
642 * a schema cache being refreshed from database.
644 if (working_schema) {
645 struct ldb_message *msg;
646 struct ldb_request *req;
648 /* Force a reload */
649 working_schema->last_refresh = 0;
650 cur_schema = dsdb_get_schema(ldb, NULL);
651 /* TODO: What we do in case dsdb_get_schema() fail?
652 * We can't fallback at this point anymore */
653 if (cur_schema) {
654 dsdb_make_schema_global(ldb, cur_schema);
656 msg = ldb_msg_new(ldb);
657 if (msg == NULL) {
658 return WERR_NOMEM;
660 msg->dn = ldb_dn_new(msg, ldb, "");
661 if (msg->dn == NULL) {
662 talloc_free(msg);
663 return WERR_NOMEM;
666 ret = ldb_msg_add_string(msg, "schemaUpdateNow", "1");
667 if (ret != LDB_SUCCESS) {
668 talloc_free(msg);
669 return WERR_INTERNAL_ERROR;
672 ret = ldb_build_mod_req(&req, ldb, ldb,
673 msg,
674 LDB_SCOPE_BASE,
675 NULL,
676 ldb_op_default_callback,
677 NULL);
679 if (ret != LDB_SUCCESS) {
680 talloc_free(msg);
681 return WERR_DS_DRA_INTERNAL_ERROR;
684 ret = ldb_transaction_start(ldb);
685 if (ret != LDB_SUCCESS) {
686 talloc_free(msg);
687 talloc_free(req);
688 DEBUG(0, ("Autotransaction start failed\n"));
689 return WERR_DS_DRA_INTERNAL_ERROR;
692 ret = ldb_request(ldb, req);
693 if (ret == LDB_SUCCESS) {
694 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
697 if (ret == LDB_SUCCESS) {
698 ret = ldb_transaction_commit(ldb);
699 } else {
700 DEBUG(0, ("Schema update now failed: %s\n", ldb_strerror(ret)));
701 ldb_transaction_cancel(ldb);
704 talloc_free(msg);
705 talloc_free(req);
706 if (ret != LDB_SUCCESS) {
707 DEBUG(0, ("Commit failed: %s\n", ldb_strerror(ret)));
708 return WERR_DS_INTERNAL_FAILURE;
712 DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
713 objects->num_objects, objects->linked_attributes_count,
714 ldb_dn_get_linearized(objects->partition_dn)));
716 return WERR_OK;
719 static WERROR dsdb_origin_object_convert(struct ldb_context *ldb,
720 const struct dsdb_schema *schema,
721 const struct drsuapi_DsReplicaObjectListItem *in,
722 TALLOC_CTX *mem_ctx,
723 struct ldb_message **_msg)
725 WERROR status;
726 unsigned int i;
727 struct ldb_message *msg;
729 if (!in->object.identifier) {
730 return WERR_FOOBAR;
733 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
734 return WERR_FOOBAR;
737 msg = ldb_msg_new(mem_ctx);
738 W_ERROR_HAVE_NO_MEMORY(msg);
740 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
741 W_ERROR_HAVE_NO_MEMORY(msg->dn);
743 msg->num_elements = in->object.attribute_ctr.num_attributes;
744 msg->elements = talloc_array(msg, struct ldb_message_element,
745 msg->num_elements);
746 W_ERROR_HAVE_NO_MEMORY(msg->elements);
748 for (i=0; i < msg->num_elements; i++) {
749 struct drsuapi_DsReplicaAttribute *a;
750 struct ldb_message_element *e;
752 a = &in->object.attribute_ctr.attributes[i];
753 e = &msg->elements[i];
755 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, schema->prefixmap,
756 a, msg->elements, e);
757 W_ERROR_NOT_OK_RETURN(status);
761 *_msg = msg;
763 return WERR_OK;
766 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
767 TALLOC_CTX *mem_ctx,
768 const struct drsuapi_DsReplicaObjectListItem *first_object,
769 uint32_t *_num,
770 uint32_t dsdb_repl_flags,
771 struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
773 WERROR status;
774 const struct dsdb_schema *schema;
775 const struct drsuapi_DsReplicaObjectListItem *cur;
776 struct ldb_message **objects;
777 struct drsuapi_DsReplicaObjectIdentifier2 *ids;
778 uint32_t i;
779 uint32_t num_objects = 0;
780 const char * const attrs[] = {
781 "objectGUID",
782 "objectSid",
783 NULL
785 struct ldb_result *res;
786 int ret;
788 for (cur = first_object; cur; cur = cur->next_object) {
789 num_objects++;
792 if (num_objects == 0) {
793 return WERR_OK;
796 ret = ldb_transaction_start(ldb);
797 if (ret != LDB_SUCCESS) {
798 return WERR_DS_INTERNAL_FAILURE;
801 objects = talloc_array(mem_ctx, struct ldb_message *,
802 num_objects);
803 if (objects == NULL) {
804 status = WERR_NOMEM;
805 goto cancel;
808 schema = dsdb_get_schema(ldb, objects);
809 if (!schema) {
810 return WERR_DS_SCHEMA_NOT_LOADED;
813 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
814 status = dsdb_origin_object_convert(ldb, schema, cur,
815 objects, &objects[i]);
816 if (!W_ERROR_IS_OK(status)) {
817 goto cancel;
821 ids = talloc_array(mem_ctx,
822 struct drsuapi_DsReplicaObjectIdentifier2,
823 num_objects);
824 if (ids == NULL) {
825 status = WERR_NOMEM;
826 goto cancel;
829 if (dsdb_repl_flags & DSDB_REPL_FLAG_ADD_NCNAME) {
830 /* check for possible NC creation */
831 for (i=0; i < num_objects; i++) {
832 struct ldb_message *msg = objects[i];
833 struct ldb_message_element *el;
834 struct ldb_dn *nc_dn;
836 if (ldb_msg_check_string_attribute(msg, "objectClass", "crossRef") == 0) {
837 continue;
839 el = ldb_msg_find_element(msg, "nCName");
840 if (el == NULL || el->num_values != 1) {
841 continue;
843 nc_dn = ldb_dn_from_ldb_val(objects, ldb, &el->values[0]);
844 if (!ldb_dn_validate(nc_dn)) {
845 continue;
847 ret = dsdb_create_partial_replica_NC(ldb, nc_dn);
848 if (ret != LDB_SUCCESS) {
849 status = WERR_DS_INTERNAL_FAILURE;
850 goto cancel;
855 for (i=0; i < num_objects; i++) {
856 struct dom_sid *sid = NULL;
857 struct ldb_request *add_req;
859 DEBUG(6,(__location__ ": adding %s\n",
860 ldb_dn_get_linearized(objects[i]->dn)));
862 ret = ldb_build_add_req(&add_req,
863 ldb,
864 objects,
865 objects[i],
866 NULL,
867 NULL,
868 ldb_op_default_callback,
869 NULL);
870 if (ret != LDB_SUCCESS) {
871 status = WERR_DS_INTERNAL_FAILURE;
872 goto cancel;
875 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
876 if (ret != LDB_SUCCESS) {
877 status = WERR_DS_INTERNAL_FAILURE;
878 goto cancel;
881 ret = ldb_request(ldb, add_req);
882 if (ret == LDB_SUCCESS) {
883 ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
885 if (ret != LDB_SUCCESS) {
886 DEBUG(0,(__location__ ": Failed add of %s - %s\n",
887 ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
888 status = WERR_DS_INTERNAL_FAILURE;
889 goto cancel;
892 talloc_free(add_req);
894 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
895 LDB_SCOPE_BASE, attrs,
896 "(objectClass=*)");
897 if (ret != LDB_SUCCESS) {
898 status = WERR_DS_INTERNAL_FAILURE;
899 goto cancel;
901 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
902 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
903 if (sid) {
904 ids[i].sid = *sid;
905 } else {
906 ZERO_STRUCT(ids[i].sid);
910 ret = ldb_transaction_commit(ldb);
911 if (ret != LDB_SUCCESS) {
912 return WERR_DS_INTERNAL_FAILURE;
915 talloc_free(objects);
917 *_num = num_objects;
918 *_ids = ids;
919 return WERR_OK;
921 cancel:
922 talloc_free(objects);
923 ldb_transaction_cancel(ldb);
924 return status;