s4-replicated_objects: Implement a mechanism to relax some attributes conversion
[samba.git] / source4 / dsdb / repl / replicated_objects.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    Helper functions for applying replicated objects
4    
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
6     
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.
11    
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.
16    
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/>.
19    
20 */
21
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"
33
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.
43  */
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)
52 {
53         struct schema_list {
54                 struct schema_list *next, *prev;
55                 const struct drsuapi_DsReplicaObjectListItemEx *obj;
56         };
57
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
65         /* make a copy of the iniatial_scheam so we don't mess with it */
66         working_schema = dsdb_schema_copy_shallow(mem_ctx, ldb, initial_schema);
67         if (!working_schema) {
68                 DEBUG(0,(__location__ ": schema copy failed!\n"));
69                 return WERR_NOMEM;
70         }
71
72         /* we are going to need remote prefixMap for decoding */
73         werr = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
74                                                 mem_ctx, &pfm_remote, NULL);
75         if (!W_ERROR_IS_OK(werr)) {
76                 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s",
77                          win_errstr(werr)));
78                 return werr;
79         }
80
81         /* create a list of objects yet to be converted */
82         for (cur = first_object; cur; cur = cur->next_object) {
83                 schema_list_item = talloc(mem_ctx, struct schema_list);
84                 schema_list_item->obj = cur;
85                 DLIST_ADD_END(schema_list, schema_list_item, struct schema_list);
86         }
87
88         /* resolve objects until all are resolved and in local schema */
89         pass_no = 1;
90
91         while (schema_list) {
92                 uint32_t converted_obj_count = 0;
93                 uint32_t failed_obj_count = 0;
94                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
95                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
96
97                 for (schema_list_item = schema_list; schema_list_item; schema_list_item=schema_list_next_item) {
98                         struct dsdb_extended_replicated_object object;
99
100                         cur = schema_list_item->obj;
101
102                         /* Save the next item, now we have saved out
103                          * the current one, so we can DLIST_REMOVE it
104                          * safely */
105                         schema_list_next_item = schema_list_item->next;
106
107                         /*
108                          * Convert the objects into LDB messages using the
109                          * schema we have so far. It's ok if we fail to convert
110                          * an object. We should convert more objects on next pass.
111                          */
112                         werr = dsdb_convert_object_ex(ldb, working_schema, pfm_remote,
113                                                       cur, gensec_skey,
114                                                       tmp_ctx, &object);
115                         if (!W_ERROR_IS_OK(werr)) {
116                                 DEBUG(1,("Warning: Failed to convert schema object %s into ldb msg\n",
117                                          cur->object.identifier->dn));
118
119                                 failed_obj_count++;
120                         } else {
121                                 /*
122                                  * Convert the schema from ldb_message format
123                                  * (OIDs as OID strings) into schema, using
124                                  * the remote prefixMap
125                                  */
126                                 werr = dsdb_schema_set_el_from_ldb_msg(ldb,
127                                                                        working_schema,
128                                                                        object.msg);
129                                 if (!W_ERROR_IS_OK(werr)) {
130                                         DEBUG(1,("Warning: failed to convert object %s into a schema element: %s\n",
131                                                  ldb_dn_get_linearized(object.msg->dn),
132                                                  win_errstr(werr)));
133                                         failed_obj_count++;
134                                 } else {
135                                         DLIST_REMOVE(schema_list, schema_list_item);
136                                         talloc_free(schema_list_item);
137                                         converted_obj_count++;
138                                 }
139                         }
140                 }
141                 talloc_free(tmp_ctx);
142
143                 DEBUG(4,("Schema load pass %d: %d/%d of %d objects left to be converted.\n",
144                          pass_no, failed_obj_count, converted_obj_count, object_count));
145                 pass_no++;
146
147                 /* check if we converted any objects in this pass */
148                 if (converted_obj_count == 0) {
149                         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));
150                         return WERR_INTERNAL_ERROR;
151                 }
152
153                 /* rebuild indexes */
154                 ret = dsdb_setup_sorted_accessors(ldb, working_schema);
155                 if (LDB_SUCCESS != ret) {
156                         DEBUG(0,("Failed to create schema-cache indexes!\n"));
157                         return WERR_INTERNAL_ERROR;
158                 }
159         };
160
161         *_schema_out = working_schema;
162
163         return WERR_OK;
164 }
165
166 static bool dsdb_attid_in_list(const uint32_t attid_list[], uint32_t attid)
167 {
168         const uint32_t *cur;
169         if (!attid_list) {
170                 return false;
171         }
172         for (cur = attid_list; *cur != DRSUAPI_ATTID_INVALID; cur++) {
173                 if (*cur == attid) {
174                         return true;
175                 }
176         }
177         return false;
178 }
179
180 WERROR dsdb_convert_object_ex(struct ldb_context *ldb,
181                               const struct dsdb_schema *schema,
182                               const struct dsdb_schema_prefixmap *pfm_remote,
183                               const struct drsuapi_DsReplicaObjectListItemEx *in,
184                               const DATA_BLOB *gensec_skey,
185                               const uint32_t *ignore_attids,
186                               TALLOC_CTX *mem_ctx,
187                               struct dsdb_extended_replicated_object *out)
188 {
189         NTSTATUS nt_status;
190         WERROR status;
191         uint32_t i;
192         struct ldb_message *msg;
193         struct replPropertyMetaDataBlob *md;
194         struct ldb_val guid_value;
195         NTTIME whenChanged = 0;
196         time_t whenChanged_t;
197         const char *whenChanged_s;
198         const char *rdn_name = NULL;
199         const struct ldb_val *rdn_value = NULL;
200         const struct dsdb_attribute *rdn_attr = NULL;
201         uint32_t rdn_attid;
202         struct drsuapi_DsReplicaAttribute *name_a = NULL;
203         struct drsuapi_DsReplicaMetaData *name_d = NULL;
204         struct replPropertyMetaData1 *rdn_m = NULL;
205         struct dom_sid *sid = NULL;
206         uint32_t rid = 0;
207         uint32_t attr_count;
208         int ret;
209
210         if (!in->object.identifier) {
211                 return WERR_FOOBAR;
212         }
213
214         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
215                 return WERR_FOOBAR;
216         }
217
218         if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
219                 return WERR_FOOBAR;
220         }
221
222         if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
223                 return WERR_FOOBAR;
224         }
225
226         sid = &in->object.identifier->sid;
227         if (sid->num_auths > 0) {
228                 rid = sid->sub_auths[sid->num_auths - 1];
229         }
230
231         msg = ldb_msg_new(mem_ctx);
232         W_ERROR_HAVE_NO_MEMORY(msg);
233
234         msg->dn                 = ldb_dn_new(msg, ldb, in->object.identifier->dn);
235         W_ERROR_HAVE_NO_MEMORY(msg->dn);
236
237         rdn_name        = ldb_dn_get_rdn_name(msg->dn);
238         rdn_attr        = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
239         if (!rdn_attr) {
240                 return WERR_FOOBAR;
241         }
242         rdn_attid       = rdn_attr->attributeID_id;
243         rdn_value       = ldb_dn_get_rdn_val(msg->dn);
244
245         msg->num_elements       = in->object.attribute_ctr.num_attributes;
246         msg->elements           = talloc_array(msg, struct ldb_message_element,
247                                                msg->num_elements);
248         W_ERROR_HAVE_NO_MEMORY(msg->elements);
249
250         md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
251         W_ERROR_HAVE_NO_MEMORY(md);
252
253         md->version             = 1;
254         md->reserved            = 0;
255         md->ctr.ctr1.count      = in->meta_data_ctr->count;
256         md->ctr.ctr1.reserved   = 0;
257         md->ctr.ctr1.array      = talloc_array(mem_ctx,
258                                                struct replPropertyMetaData1,
259                                                md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
260         W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
261
262         for (i=0, attr_count=0; i < in->meta_data_ctr->count; i++, attr_count++) {
263                 struct drsuapi_DsReplicaAttribute *a;
264                 struct drsuapi_DsReplicaMetaData *d;
265                 struct replPropertyMetaData1 *m;
266                 struct ldb_message_element *e;
267                 uint32_t j;
268
269                 a = &in->object.attribute_ctr.attributes[i];
270                 d = &in->meta_data_ctr->meta_data[i];
271                 m = &md->ctr.ctr1.array[attr_count];
272                 e = &msg->elements[attr_count];
273
274                 if (dsdb_attid_in_list(ignore_attids, a->attid)) {
275                         attr_count--;
276                         continue;
277                 }
278
279                 for (j=0; j<a->value_ctr.num_values; j++) {
280                         status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob, gensec_skey, rid, a);
281                         W_ERROR_NOT_OK_RETURN(status);
282                 }
283
284                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
285                                                        a, msg->elements, e);
286                 W_ERROR_NOT_OK_RETURN(status);
287
288                 m->attid                        = a->attid;
289                 m->version                      = d->version;
290                 m->originating_change_time      = d->originating_change_time;
291                 m->originating_invocation_id    = d->originating_invocation_id;
292                 m->originating_usn              = d->originating_usn;
293                 m->local_usn                    = 0;
294
295                 if (d->originating_change_time > whenChanged) {
296                         whenChanged = d->originating_change_time;
297                 }
298
299                 if (a->attid == DRSUAPI_ATTID_name) {
300                         name_a = a;
301                         name_d = d;
302                 }
303         }
304
305         msg->num_elements = attr_count;
306         md->ctr.ctr1.count = attr_count;
307         if (name_a) {
308                 rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
309         }
310
311         if (rdn_m) {
312                 struct ldb_message_element *el;
313                 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
314                 if (!el) {
315                         ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
316                         if (ret != LDB_SUCCESS) {
317                                 return WERR_FOOBAR;
318                         }
319                 } else {
320                         if (el->num_values != 1) {
321                                 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
322                                          el->num_values));
323                                 return WERR_FOOBAR;                             
324                         }
325                         if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
326                                 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
327                                          (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
328                                          (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
329                                 return WERR_FOOBAR;                             
330                         }
331                 }
332
333                 rdn_m->attid                            = rdn_attid;
334                 rdn_m->version                          = name_d->version;
335                 rdn_m->originating_change_time          = name_d->originating_change_time;
336                 rdn_m->originating_invocation_id        = name_d->originating_invocation_id;
337                 rdn_m->originating_usn                  = name_d->originating_usn;
338                 rdn_m->local_usn                        = 0;
339                 md->ctr.ctr1.count++;
340
341         }
342
343         whenChanged_t = nt_time_to_unix(whenChanged);
344         whenChanged_s = ldb_timestring(msg, whenChanged_t);
345         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
346
347         nt_status = GUID_to_ndr_blob(&in->object.identifier->guid, msg, &guid_value);
348         if (!NT_STATUS_IS_OK(nt_status)) {
349                 return ntstatus_to_werror(nt_status);
350         }
351
352         out->msg                = msg;
353         out->guid_value         = guid_value;
354         out->when_changed       = whenChanged_s;
355         out->meta_data          = md;
356         return WERR_OK;
357 }
358
359 WERROR dsdb_replicated_objects_convert(struct ldb_context *ldb,
360                                        const struct dsdb_schema *schema,
361                                        const char *partition_dn_str,
362                                        const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
363                                        uint32_t object_count,
364                                        const struct drsuapi_DsReplicaObjectListItemEx *first_object,
365                                        uint32_t linked_attributes_count,
366                                        const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
367                                        const struct repsFromTo1 *source_dsa,
368                                        const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
369                                        const DATA_BLOB *gensec_skey,
370                                        TALLOC_CTX *mem_ctx,
371                                        struct dsdb_extended_replicated_objects **objects)
372 {
373         WERROR status;
374         struct ldb_dn *partition_dn;
375         struct dsdb_schema_prefixmap *pfm_remote;
376         struct dsdb_extended_replicated_objects *out;
377         const struct drsuapi_DsReplicaObjectListItemEx *cur;
378         uint32_t i;
379
380         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
381         W_ERROR_HAVE_NO_MEMORY(out);
382         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
383
384         /*
385          * Ensure schema is kept valid for as long as 'out'
386          * which may contain pointers to it
387          */
388         schema = talloc_reference(out, schema);
389         W_ERROR_HAVE_NO_MEMORY(schema);
390
391         partition_dn = ldb_dn_new(out, ldb, partition_dn_str);
392         W_ERROR_HAVE_NO_MEMORY_AND_FREE(partition_dn, out);
393
394         status = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
395                                                   out, &pfm_remote, NULL);
396         if (!W_ERROR_IS_OK(status)) {
397                 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s",
398                          win_errstr(status)));
399                 talloc_free(out);
400                 return status;
401         }
402
403         if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
404                 /*
405                  * check for schema changes in case
406                  * we are not replicating Schema NC
407                  */
408                 status = dsdb_schema_info_cmp(schema, mapping_ctr);
409                 if (!W_ERROR_IS_OK(status)) {
410                         DEBUG(1,("Remote schema has changed while replicating %s\n",
411                                  partition_dn_str));
412                         talloc_free(out);
413                         return status;
414                 }
415         }
416
417         out->partition_dn       = partition_dn;
418
419         out->source_dsa         = source_dsa;
420         out->uptodateness_vector= uptodateness_vector;
421
422         out->num_objects        = object_count;
423         out->objects            = talloc_array(out,
424                                                struct dsdb_extended_replicated_object,
425                                                out->num_objects);
426         W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
427
428         /* pass the linked attributes down to the repl_meta_data
429            module */
430         out->linked_attributes_count = linked_attributes_count;
431         out->linked_attributes       = linked_attributes;
432
433         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
434                 if (i == out->num_objects) {
435                         talloc_free(out);
436                         return WERR_FOOBAR;
437                 }
438
439                 status = dsdb_convert_object_ex(ldb, schema, pfm_remote,
440                                                 cur, gensec_skey,
441                                                 NULL,
442                                                 out->objects, &out->objects[i]);
443                 if (!W_ERROR_IS_OK(status)) {
444                         talloc_free(out);
445                         DEBUG(0,("Failed to convert object %s: %s\n",
446                                  cur->object.identifier->dn,
447                                  win_errstr(status)));
448                         return status;
449                 }
450         }
451         if (i != out->num_objects) {
452                 talloc_free(out);
453                 return WERR_FOOBAR;
454         }
455
456         /* free pfm_remote, we won't need it anymore */
457         talloc_free(pfm_remote);
458
459         *objects = out;
460         return WERR_OK;
461 }
462
463 /**
464  * Commits a list of replicated objects.
465  *
466  * @param working_schema dsdb_schema to be used for resolving
467  *                       Classes/Attributes during Schema replication. If not NULL,
468  *                       it will be set on ldb and used while committing replicated objects
469  */
470 WERROR dsdb_replicated_objects_commit(struct ldb_context *ldb,
471                                       struct dsdb_schema *working_schema,
472                                       struct dsdb_extended_replicated_objects *objects,
473                                       uint64_t *notify_uSN)
474 {
475         WERROR werr;
476         struct ldb_result *ext_res;
477         struct dsdb_schema *cur_schema = NULL;
478         int ret;
479         uint64_t seq_num1, seq_num2;
480
481         /* TODO: handle linked attributes */
482
483         /* wrap the extended operation in a transaction 
484            See [MS-DRSR] 3.3.2 Transactions
485          */
486         ret = ldb_transaction_start(ldb);
487         if (ret != LDB_SUCCESS) {
488                 DEBUG(0,(__location__ " Failed to start transaction\n"));
489                 return WERR_FOOBAR;
490         }
491
492         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
493         if (ret != LDB_SUCCESS) {
494                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
495                 ldb_transaction_cancel(ldb);
496                 return WERR_FOOBAR;             
497         }
498
499         /*
500          * Set working_schema for ldb in case we are replicating from Schema NC.
501          * Schema won't be reloaded during Replicated Objects commit, as it is
502          * done in a transaction. So we need some way to search for newly
503          * added Classes and Attributes
504          */
505         if (working_schema) {
506                 /* store current schema so we can fall back in case of failure */
507                 cur_schema = dsdb_get_schema(ldb, working_schema);
508
509                 ret = dsdb_reference_schema(ldb, working_schema, false);
510                 if (ret != LDB_SUCCESS) {
511                         DEBUG(0,(__location__ "Failed to reference working schema - %s\n",
512                                  ldb_strerror(ret)));
513                         /* TODO: Map LDB Error to NTSTATUS? */
514                         ldb_transaction_cancel(ldb);
515                         return WERR_INTERNAL_ERROR;
516                 }
517         }
518
519         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
520         if (ret != LDB_SUCCESS) {
521                 /* restore previous schema */
522                 if (cur_schema ) {
523                         dsdb_reference_schema(ldb, cur_schema, false);
524                         dsdb_make_schema_global(ldb, cur_schema);
525                 }
526
527                 DEBUG(0,("Failed to apply records: %s: %s\n",
528                          ldb_errstring(ldb), ldb_strerror(ret)));
529                 ldb_transaction_cancel(ldb);
530                 return WERR_FOOBAR;
531         }
532         talloc_free(ext_res);
533
534         /* Save our updated prefixMap */
535         if (working_schema) {
536                 werr = dsdb_write_prefixes_from_schema_to_ldb(working_schema,
537                                                               ldb,
538                                                               working_schema);
539                 if (!W_ERROR_IS_OK(werr)) {
540                         /* restore previous schema */
541                         if (cur_schema ) {
542                                 dsdb_reference_schema(ldb, cur_schema, false);
543                                 dsdb_make_schema_global(ldb, cur_schema);
544                         }
545                         DEBUG(0,("Failed to save updated prefixMap: %s\n",
546                                  win_errstr(werr)));
547                         return werr;
548                 }
549         }
550
551         ret = ldb_transaction_prepare_commit(ldb);
552         if (ret != LDB_SUCCESS) {
553                 /* restore previous schema */
554                 if (cur_schema ) {
555                         dsdb_reference_schema(ldb, cur_schema, false);
556                         dsdb_make_schema_global(ldb, cur_schema);
557                 }
558                 DEBUG(0,(__location__ " Failed to prepare commit of transaction: %s\n",
559                          ldb_errstring(ldb)));
560                 return WERR_FOOBAR;
561         }
562
563         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
564         if (ret != LDB_SUCCESS) {
565                 /* restore previous schema */
566                 if (cur_schema ) {
567                         dsdb_reference_schema(ldb, cur_schema, false);
568                         dsdb_make_schema_global(ldb, cur_schema);
569                 }
570                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
571                 ldb_transaction_cancel(ldb);
572                 return WERR_FOOBAR;             
573         }
574
575         /* if this replication partner didn't need to be notified
576            before this transaction then it still doesn't need to be
577            notified, as the changes came from this server */    
578         if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
579                 *notify_uSN = seq_num2;
580         }
581
582         ret = ldb_transaction_commit(ldb);
583         if (ret != LDB_SUCCESS) {
584                 /* restore previous schema */
585                 if (cur_schema ) {
586                         dsdb_reference_schema(ldb, cur_schema, false);
587                         dsdb_make_schema_global(ldb, cur_schema);
588                 }
589                 DEBUG(0,(__location__ " Failed to commit transaction\n"));
590                 return WERR_FOOBAR;
591         }
592
593         /*
594          * Reset the Schema used by ldb. This will lead to
595          * a schema cache being refreshed from database.
596          */
597         if (working_schema) {
598                 cur_schema = dsdb_get_schema(ldb, NULL);
599                 /* TODO: What we do in case dsdb_get_schema() fail?
600                  *       We can't fallback at this point anymore */
601                 if (cur_schema) {
602                         dsdb_make_schema_global(ldb, cur_schema);
603                 }
604         }
605
606         DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
607                  objects->num_objects, objects->linked_attributes_count,
608                  ldb_dn_get_linearized(objects->partition_dn)));
609                  
610         return WERR_OK;
611 }
612
613 static WERROR dsdb_origin_object_convert(struct ldb_context *ldb,
614                                          const struct dsdb_schema *schema,
615                                          const struct drsuapi_DsReplicaObjectListItem *in,
616                                          TALLOC_CTX *mem_ctx,
617                                          struct ldb_message **_msg)
618 {
619         WERROR status;
620         unsigned int i;
621         struct ldb_message *msg;
622
623         if (!in->object.identifier) {
624                 return WERR_FOOBAR;
625         }
626
627         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
628                 return WERR_FOOBAR;
629         }
630
631         msg = ldb_msg_new(mem_ctx);
632         W_ERROR_HAVE_NO_MEMORY(msg);
633
634         msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
635         W_ERROR_HAVE_NO_MEMORY(msg->dn);
636
637         msg->num_elements       = in->object.attribute_ctr.num_attributes;
638         msg->elements           = talloc_array(msg, struct ldb_message_element,
639                                                msg->num_elements);
640         W_ERROR_HAVE_NO_MEMORY(msg->elements);
641
642         for (i=0; i < msg->num_elements; i++) {
643                 struct drsuapi_DsReplicaAttribute *a;
644                 struct ldb_message_element *e;
645
646                 a = &in->object.attribute_ctr.attributes[i];
647                 e = &msg->elements[i];
648
649                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, schema->prefixmap,
650                                                        a, msg->elements, e);
651                 W_ERROR_NOT_OK_RETURN(status);
652         }
653
654
655         *_msg = msg;
656
657         return WERR_OK;
658 }
659
660 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
661                                   TALLOC_CTX *mem_ctx,
662                                   const struct drsuapi_DsReplicaObjectListItem *first_object,
663                                   uint32_t *_num,
664                                   struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
665 {
666         WERROR status;
667         const struct dsdb_schema *schema;
668         const struct drsuapi_DsReplicaObjectListItem *cur;
669         struct ldb_message **objects;
670         struct drsuapi_DsReplicaObjectIdentifier2 *ids;
671         uint32_t i;
672         uint32_t num_objects = 0;
673         const char * const attrs[] = {
674                 "objectGUID",
675                 "objectSid",
676                 NULL
677         };
678         struct ldb_result *res;
679         int ret;
680
681         for (cur = first_object; cur; cur = cur->next_object) {
682                 num_objects++;
683         }
684
685         if (num_objects == 0) {
686                 return WERR_OK;
687         }
688
689         ret = ldb_transaction_start(ldb);
690         if (ret != LDB_SUCCESS) {
691                 return WERR_DS_INTERNAL_FAILURE;
692         }
693
694         objects = talloc_array(mem_ctx, struct ldb_message *,
695                                num_objects);
696         if (objects == NULL) {
697                 status = WERR_NOMEM;
698                 goto cancel;
699         }
700
701         schema = dsdb_get_schema(ldb, objects);
702         if (!schema) {
703                 return WERR_DS_SCHEMA_NOT_LOADED;
704         }
705
706         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
707                 status = dsdb_origin_object_convert(ldb, schema, cur,
708                                                     objects, &objects[i]);
709                 if (!W_ERROR_IS_OK(status)) {
710                         goto cancel;
711                 }
712         }
713
714         ids = talloc_array(mem_ctx,
715                            struct drsuapi_DsReplicaObjectIdentifier2,
716                            num_objects);
717         if (ids == NULL) {
718                 status = WERR_NOMEM;
719                 goto cancel;
720         }
721
722         for (i=0; i < num_objects; i++) {
723                 struct dom_sid *sid = NULL;
724                 struct ldb_request *add_req;
725
726                 DEBUG(6,(__location__ ": adding %s\n", 
727                          ldb_dn_get_linearized(objects[i]->dn)));
728
729                 ret = ldb_build_add_req(&add_req,
730                                         ldb,
731                                         objects,
732                                         objects[i],
733                                         NULL,
734                                         NULL,
735                                         ldb_op_default_callback,
736                                         NULL);
737                 if (ret != LDB_SUCCESS) {
738                         status = WERR_DS_INTERNAL_FAILURE;
739                         goto cancel;
740                 }
741
742                 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
743                 if (ret != LDB_SUCCESS) {
744                         status = WERR_DS_INTERNAL_FAILURE;
745                         goto cancel;
746                 }
747                 
748                 ret = ldb_request(ldb, add_req);
749                 if (ret == LDB_SUCCESS) {
750                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
751                 }
752                 if (ret != LDB_SUCCESS) {
753                         DEBUG(0,(__location__ ": Failed add of %s - %s\n",
754                                  ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
755                         status = WERR_DS_INTERNAL_FAILURE;
756                         goto cancel;
757                 }
758
759                 talloc_free(add_req);
760
761                 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
762                                  LDB_SCOPE_BASE, attrs,
763                                  "(objectClass=*)");
764                 if (ret != LDB_SUCCESS) {
765                         status = WERR_DS_INTERNAL_FAILURE;
766                         goto cancel;
767                 }
768                 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
769                 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
770                 if (sid) {
771                         ids[i].sid = *sid;
772                 } else {
773                         ZERO_STRUCT(ids[i].sid);
774                 }
775         }
776
777         ret = ldb_transaction_commit(ldb);
778         if (ret != LDB_SUCCESS) {
779                 return WERR_DS_INTERNAL_FAILURE;
780         }
781
782         talloc_free(objects);
783
784         *_num = num_objects;
785         *_ids = ids;
786         return WERR_OK;
787
788 cancel:
789         talloc_free(objects);
790         ldb_transaction_cancel(ldb);
791         return status;
792 }