dsdb-repl: make message more clearer
[mat/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         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
71         };
72
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;
78         }
79
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;
87         }
88
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);
94         }
95
96         /* resolve objects until all are resolved and in local schema */
97         pass_no = 1;
98
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);
104
105                 for (schema_list_item = schema_list; schema_list_item; schema_list_item=schema_list_next_item) {
106                         struct dsdb_extended_replicated_object object;
107
108                         cur = schema_list_item->obj;
109
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;
114
115                         /*
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.
119                          */
120                         werr = dsdb_convert_object_ex(ldb, working_schema, pfm_remote,
121                                                       cur, gensec_skey,
122                                                       ignore_attids,
123                                                       0,
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));
128
129                                 failed_obj_count++;
130                         } else {
131                                 /*
132                                  * Convert the schema from ldb_message format
133                                  * (OIDs as OID strings) into schema, using
134                                  * the remote prefixMap
135                                  */
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++;
148                                 }
149                         }
150                 }
151                 talloc_free(tmp_ctx);
152
153                 DEBUG(4,("Schema load pass %d: converted %d, %d of %d objects left to be converted.\n",
154                          pass_no, converted_obj_count, failed_obj_count, object_count));
155                 pass_no++;
156
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;
161                 }
162
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;
168                 }
169         };
170
171         *_schema_out = working_schema;
172
173         return WERR_OK;
174 }
175
176 static bool dsdb_attid_in_list(const uint32_t attid_list[], uint32_t attid)
177 {
178         const uint32_t *cur;
179         if (!attid_list) {
180                 return false;
181         }
182         for (cur = attid_list; *cur != DRSUAPI_ATTID_INVALID; cur++) {
183                 if (*cur == attid) {
184                         return true;
185                 }
186         }
187         return false;
188 }
189
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)
199 {
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_message_element *instanceType_e = NULL;
207         struct ldb_val guid_value;
208         struct ldb_val parent_guid_value;
209         NTTIME whenChanged = 0;
210         time_t whenChanged_t;
211         const char *whenChanged_s;
212         const char *rdn_name = NULL;
213         const struct ldb_val *rdn_value = NULL;
214         const struct dsdb_attribute *rdn_attr = NULL;
215         uint32_t rdn_attid;
216         struct drsuapi_DsReplicaAttribute *name_a = NULL;
217         struct drsuapi_DsReplicaMetaData *name_d = NULL;
218         struct replPropertyMetaData1 *rdn_m = NULL;
219         struct dom_sid *sid = NULL;
220         uint32_t rid = 0;
221         uint32_t attr_count;
222         int ret;
223
224         if (!in->object.identifier) {
225                 return WERR_FOOBAR;
226         }
227
228         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
229                 return WERR_FOOBAR;
230         }
231
232         if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
233                 return WERR_FOOBAR;
234         }
235
236         if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
237                 return WERR_FOOBAR;
238         }
239
240         sid = &in->object.identifier->sid;
241         if (sid->num_auths > 0) {
242                 rid = sid->sub_auths[sid->num_auths - 1];
243         }
244
245         msg = ldb_msg_new(mem_ctx);
246         W_ERROR_HAVE_NO_MEMORY(msg);
247
248         msg->dn                 = ldb_dn_new(msg, ldb, in->object.identifier->dn);
249         W_ERROR_HAVE_NO_MEMORY(msg->dn);
250
251         rdn_name        = ldb_dn_get_rdn_name(msg->dn);
252         rdn_attr        = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
253         if (!rdn_attr) {
254                 return WERR_FOOBAR;
255         }
256         rdn_attid       = rdn_attr->attributeID_id;
257         rdn_value       = ldb_dn_get_rdn_val(msg->dn);
258
259         msg->num_elements       = in->object.attribute_ctr.num_attributes;
260         msg->elements           = talloc_array(msg, struct ldb_message_element,
261                                                msg->num_elements + 1); /* +1 because of the RDN attribute */
262         W_ERROR_HAVE_NO_MEMORY(msg->elements);
263
264         md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
265         W_ERROR_HAVE_NO_MEMORY(md);
266
267         md->version             = 1;
268         md->reserved            = 0;
269         md->ctr.ctr1.count      = in->meta_data_ctr->count;
270         md->ctr.ctr1.reserved   = 0;
271         md->ctr.ctr1.array      = talloc_array(mem_ctx,
272                                                struct replPropertyMetaData1,
273                                                md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
274         W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
275
276         for (i=0, attr_count=0; i < in->meta_data_ctr->count; i++, attr_count++) {
277                 struct drsuapi_DsReplicaAttribute *a;
278                 struct drsuapi_DsReplicaMetaData *d;
279                 struct replPropertyMetaData1 *m;
280                 struct ldb_message_element *e;
281                 uint32_t j;
282
283                 a = &in->object.attribute_ctr.attributes[i];
284                 d = &in->meta_data_ctr->meta_data[i];
285                 m = &md->ctr.ctr1.array[attr_count];
286                 e = &msg->elements[attr_count];
287
288                 if (dsdb_attid_in_list(ignore_attids, a->attid)) {
289                         attr_count--;
290                         continue;
291                 }
292
293                 if (a->attid == DRSUAPI_ATTID_instanceType) {
294                         if (instanceType_e != NULL) {
295                                 return WERR_FOOBAR;
296                         }
297                         instanceType_e = e;
298                 }
299
300                 for (j=0; j<a->value_ctr.num_values; j++) {
301                         status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob, gensec_skey, rid, a);
302                         W_ERROR_NOT_OK_RETURN(status);
303                 }
304
305                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
306                                                        a, msg->elements, e);
307                 W_ERROR_NOT_OK_RETURN(status);
308
309                 m->attid                        = a->attid;
310                 m->version                      = d->version;
311                 m->originating_change_time      = d->originating_change_time;
312                 m->originating_invocation_id    = d->originating_invocation_id;
313                 m->originating_usn              = d->originating_usn;
314                 m->local_usn                    = 0;
315
316                 if (d->originating_change_time > whenChanged) {
317                         whenChanged = d->originating_change_time;
318                 }
319
320                 if (a->attid == DRSUAPI_ATTID_name) {
321                         name_a = a;
322                         name_d = d;
323                 }
324         }
325
326         msg->num_elements = attr_count;
327         md->ctr.ctr1.count = attr_count;
328         if (name_a) {
329                 rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
330         }
331
332         if (rdn_m) {
333                 struct ldb_message_element *el;
334                 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
335                 if (!el) {
336                         ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
337                         if (ret != LDB_SUCCESS) {
338                                 return WERR_FOOBAR;
339                         }
340                 } else {
341                         if (el->num_values != 1) {
342                                 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
343                                          el->num_values));
344                                 return WERR_FOOBAR;                             
345                         }
346                         if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
347                                 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
348                                          (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
349                                          (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
350                                 return WERR_FOOBAR;                             
351                         }
352                 }
353
354                 rdn_m->attid                            = rdn_attid;
355                 rdn_m->version                          = name_d->version;
356                 rdn_m->originating_change_time          = name_d->originating_change_time;
357                 rdn_m->originating_invocation_id        = name_d->originating_invocation_id;
358                 rdn_m->originating_usn                  = name_d->originating_usn;
359                 rdn_m->local_usn                        = 0;
360                 md->ctr.ctr1.count++;
361
362         }
363
364         if (instanceType_e == NULL) {
365                 return WERR_FOOBAR;
366         }
367
368         instanceType = ldb_msg_find_attr_as_int(msg, "instanceType", 0);
369         if (dsdb_repl_flags & DSDB_REPL_FLAG_PARTIAL_REPLICA) {
370                 /* the instanceType type for partial_replica
371                    replication is sent via DRS with TYPE_WRITE set, but
372                    must be used on the client with TYPE_WRITE removed
373                 */
374                 if (instanceType & INSTANCE_TYPE_WRITE) {
375                         /*
376                          * Make sure we do not change the order
377                          * of msg->elements!
378                          *
379                          * That's why we use
380                          * instanceType_e->num_values = 0
381                          * instead of
382                          * ldb_msg_remove_attr(msg, "instanceType");
383                          */
384                         struct ldb_message_element *e;
385
386                         e = ldb_msg_find_element(msg, "instanceType");
387                         if (e != instanceType_e) {
388                                 DEBUG(0,("instanceType_e[%p] changed to e[%p]\n",
389                                          instanceType_e, e));
390                                 return WERR_FOOBAR;
391                         }
392
393                         instanceType_e->num_values = 0;
394
395                         instanceType &= ~INSTANCE_TYPE_WRITE;
396                         if (ldb_msg_add_fmt(msg, "instanceType", "%d", instanceType) != LDB_SUCCESS) {
397                                 return WERR_INTERNAL_ERROR;
398                         }
399                 }
400         } else {
401                 if (!(instanceType & INSTANCE_TYPE_WRITE)) {
402                         DEBUG(0, ("Refusing to replicate %s from a read-only repilca into a read-write replica!\n",
403                                   ldb_dn_get_linearized(msg->dn)));
404                         return WERR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA;
405                 }
406         }
407
408         whenChanged_t = nt_time_to_unix(whenChanged);
409         whenChanged_s = ldb_timestring(msg, whenChanged_t);
410         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
411
412         nt_status = GUID_to_ndr_blob(&in->object.identifier->guid, msg, &guid_value);
413         if (!NT_STATUS_IS_OK(nt_status)) {
414                 return ntstatus_to_werror(nt_status);
415         }
416
417         if (in->parent_object_guid) {
418                 nt_status = GUID_to_ndr_blob(in->parent_object_guid, msg, &parent_guid_value);
419                 if (!NT_STATUS_IS_OK(nt_status)) {
420                         return ntstatus_to_werror(nt_status);
421                 }
422         } else {
423                 parent_guid_value = data_blob_null;
424         }
425
426         out->msg                = msg;
427         out->guid_value         = guid_value;
428         out->parent_guid_value  = parent_guid_value;
429         out->when_changed       = whenChanged_s;
430         out->meta_data          = md;
431         return WERR_OK;
432 }
433
434 WERROR dsdb_replicated_objects_convert(struct ldb_context *ldb,
435                                        const struct dsdb_schema *schema,
436                                        const char *partition_dn_str,
437                                        const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
438                                        uint32_t object_count,
439                                        const struct drsuapi_DsReplicaObjectListItemEx *first_object,
440                                        uint32_t linked_attributes_count,
441                                        const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
442                                        const struct repsFromTo1 *source_dsa,
443                                        const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
444                                        const DATA_BLOB *gensec_skey,
445                                        uint32_t dsdb_repl_flags,
446                                        TALLOC_CTX *mem_ctx,
447                                        struct dsdb_extended_replicated_objects **objects)
448 {
449         WERROR status;
450         struct ldb_dn *partition_dn;
451         struct dsdb_schema_prefixmap *pfm_remote;
452         struct dsdb_extended_replicated_objects *out;
453         const struct drsuapi_DsReplicaObjectListItemEx *cur;
454         uint32_t i;
455
456         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
457         W_ERROR_HAVE_NO_MEMORY(out);
458         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
459         out->dsdb_repl_flags    = dsdb_repl_flags;
460
461         /*
462          * Ensure schema is kept valid for as long as 'out'
463          * which may contain pointers to it
464          */
465         schema = talloc_reference(out, schema);
466         W_ERROR_HAVE_NO_MEMORY(schema);
467
468         partition_dn = ldb_dn_new(out, ldb, partition_dn_str);
469         W_ERROR_HAVE_NO_MEMORY_AND_FREE(partition_dn, out);
470
471         status = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
472                                                   out, &pfm_remote, NULL);
473         if (!W_ERROR_IS_OK(status)) {
474                 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s",
475                          win_errstr(status)));
476                 talloc_free(out);
477                 return status;
478         }
479
480         if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
481                 /*
482                  * check for schema changes in case
483                  * we are not replicating Schema NC
484                  */
485                 status = dsdb_schema_info_cmp(schema, mapping_ctr);
486                 if (!W_ERROR_IS_OK(status)) {
487                         DEBUG(1,("Remote schema has changed while replicating %s\n",
488                                  partition_dn_str));
489                         talloc_free(out);
490                         return status;
491                 }
492         }
493
494         out->partition_dn       = partition_dn;
495
496         out->source_dsa         = source_dsa;
497         out->uptodateness_vector= uptodateness_vector;
498
499         out->num_objects        = object_count;
500         out->objects            = talloc_array(out,
501                                                struct dsdb_extended_replicated_object,
502                                                out->num_objects);
503         W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
504
505         /* pass the linked attributes down to the repl_meta_data
506            module */
507         out->linked_attributes_count = linked_attributes_count;
508         out->linked_attributes       = linked_attributes;
509
510         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
511                 if (i == out->num_objects) {
512                         talloc_free(out);
513                         return WERR_FOOBAR;
514                 }
515
516                 status = dsdb_convert_object_ex(ldb, schema, pfm_remote,
517                                                 cur, gensec_skey,
518                                                 NULL,
519                                                 dsdb_repl_flags,
520                                                 out->objects, &out->objects[i]);
521                 if (!W_ERROR_IS_OK(status)) {
522                         talloc_free(out);
523                         DEBUG(0,("Failed to convert object %s: %s\n",
524                                  cur->object.identifier->dn,
525                                  win_errstr(status)));
526                         return status;
527                 }
528         }
529         if (i != out->num_objects) {
530                 talloc_free(out);
531                 return WERR_FOOBAR;
532         }
533
534         /* free pfm_remote, we won't need it anymore */
535         talloc_free(pfm_remote);
536
537         *objects = out;
538         return WERR_OK;
539 }
540
541 /**
542  * Commits a list of replicated objects.
543  *
544  * @param working_schema dsdb_schema to be used for resolving
545  *                       Classes/Attributes during Schema replication. If not NULL,
546  *                       it will be set on ldb and used while committing replicated objects
547  */
548 WERROR dsdb_replicated_objects_commit(struct ldb_context *ldb,
549                                       struct dsdb_schema *working_schema,
550                                       struct dsdb_extended_replicated_objects *objects,
551                                       uint64_t *notify_uSN)
552 {
553         WERROR werr;
554         struct ldb_result *ext_res;
555         struct dsdb_schema *cur_schema = NULL;
556         struct dsdb_schema *new_schema = NULL;
557         int ret;
558         uint64_t seq_num1, seq_num2;
559         bool used_global_schema = false;
560
561         TALLOC_CTX *tmp_ctx = talloc_new(objects);
562         if (!tmp_ctx) {
563                 DEBUG(0,("Failed to start talloc\n"));
564                 return WERR_NOMEM;
565         }
566
567         /* TODO: handle linked attributes */
568
569         /* wrap the extended operation in a transaction 
570            See [MS-DRSR] 3.3.2 Transactions
571          */
572         ret = ldb_transaction_start(ldb);
573         if (ret != LDB_SUCCESS) {
574                 DEBUG(0,(__location__ " Failed to start transaction\n"));
575                 return WERR_FOOBAR;
576         }
577
578         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
579         if (ret != LDB_SUCCESS) {
580                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
581                 ldb_transaction_cancel(ldb);
582                 TALLOC_FREE(tmp_ctx);
583                 return WERR_FOOBAR;             
584         }
585
586         /*
587          * Set working_schema for ldb in case we are replicating from Schema NC.
588          * Schema won't be reloaded during Replicated Objects commit, as it is
589          * done in a transaction. So we need some way to search for newly
590          * added Classes and Attributes
591          */
592         if (working_schema) {
593                 /* store current schema so we can fall back in case of failure */
594                 cur_schema = dsdb_get_schema(ldb, tmp_ctx);
595                 used_global_schema = dsdb_uses_global_schema(ldb);
596
597                 ret = dsdb_reference_schema(ldb, working_schema, false);
598                 if (ret != LDB_SUCCESS) {
599                         DEBUG(0,(__location__ "Failed to reference working schema - %s\n",
600                                  ldb_strerror(ret)));
601                         /* TODO: Map LDB Error to NTSTATUS? */
602                         ldb_transaction_cancel(ldb);
603                         TALLOC_FREE(tmp_ctx);
604                         return WERR_INTERNAL_ERROR;
605                 }
606         }
607
608         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
609         if (ret != LDB_SUCCESS) {
610                 /* restore previous schema */
611                 if (used_global_schema) { 
612                         dsdb_set_global_schema(ldb);
613                 } else if (cur_schema) {
614                         dsdb_reference_schema(ldb, cur_schema, false);
615                 }
616
617                 DEBUG(0,("Failed to apply records: %s: %s\n",
618                          ldb_errstring(ldb), ldb_strerror(ret)));
619                 ldb_transaction_cancel(ldb);
620                 TALLOC_FREE(tmp_ctx);
621                 return WERR_FOOBAR;
622         }
623         talloc_free(ext_res);
624
625         /* Save our updated prefixMap */
626         if (working_schema) {
627                 werr = dsdb_write_prefixes_from_schema_to_ldb(working_schema,
628                                                               ldb,
629                                                               working_schema);
630                 if (!W_ERROR_IS_OK(werr)) {
631                         /* restore previous schema */
632                         if (used_global_schema) { 
633                                 dsdb_set_global_schema(ldb);
634                         } else if (cur_schema ) {
635                                 dsdb_reference_schema(ldb, cur_schema, false);
636                         }
637                         DEBUG(0,("Failed to save updated prefixMap: %s\n",
638                                  win_errstr(werr)));
639                         TALLOC_FREE(tmp_ctx);
640                         return werr;
641                 }
642         }
643
644         ret = ldb_transaction_prepare_commit(ldb);
645         if (ret != LDB_SUCCESS) {
646                 /* restore previous schema */
647                 if (used_global_schema) { 
648                         dsdb_set_global_schema(ldb);
649                 } else if (cur_schema ) {
650                         dsdb_reference_schema(ldb, cur_schema, false);
651                 }
652                 DEBUG(0,(__location__ " Failed to prepare commit of transaction: %s\n",
653                          ldb_errstring(ldb)));
654                 TALLOC_FREE(tmp_ctx);
655                 return WERR_FOOBAR;
656         }
657
658         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
659         if (ret != LDB_SUCCESS) {
660                 /* restore previous schema */
661                 if (used_global_schema) { 
662                         dsdb_set_global_schema(ldb);
663                 } else if (cur_schema ) {
664                         dsdb_reference_schema(ldb, cur_schema, false);
665                 }
666                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
667                 ldb_transaction_cancel(ldb);
668                 TALLOC_FREE(tmp_ctx);
669                 return WERR_FOOBAR;             
670         }
671
672         /* if this replication partner didn't need to be notified
673            before this transaction then it still doesn't need to be
674            notified, as the changes came from this server */    
675         if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
676                 *notify_uSN = seq_num2;
677         }
678
679         ret = ldb_transaction_commit(ldb);
680         if (ret != LDB_SUCCESS) {
681                 /* restore previous schema */
682                 if (used_global_schema) { 
683                         dsdb_set_global_schema(ldb);
684                 } else if (cur_schema ) {
685                         dsdb_reference_schema(ldb, cur_schema, false);
686                 }
687                 DEBUG(0,(__location__ " Failed to commit transaction\n"));
688                 TALLOC_FREE(tmp_ctx);
689                 return WERR_FOOBAR;
690         }
691
692         /*
693          * Reset the Schema used by ldb. This will lead to
694          * a schema cache being refreshed from database.
695          */
696         if (working_schema) {
697                 struct ldb_message *msg;
698                 struct ldb_request *req;
699
700                 /* Force a reload */
701                 working_schema->last_refresh = 0;
702                 new_schema = dsdb_get_schema(ldb, tmp_ctx);
703                 /* TODO: 
704                  * If dsdb_get_schema() fails, we just fall back
705                  * to what we had.  However, the database is probably
706                  * unable to operate for other users from this
707                  * point... */
708                 if (new_schema && used_global_schema) {
709                         dsdb_make_schema_global(ldb, new_schema);
710                 } else if (used_global_schema) { 
711                         DEBUG(0,("Failed to re-load schema after commit of transaction\n"));
712                         dsdb_set_global_schema(ldb);
713                         TALLOC_FREE(tmp_ctx);
714                         return WERR_INTERNAL_ERROR;
715                 } else {
716                         DEBUG(0,("Failed to re-load schema after commit of transaction\n"));
717                         dsdb_reference_schema(ldb, cur_schema, false);
718                         TALLOC_FREE(tmp_ctx);
719                         return WERR_INTERNAL_ERROR;
720                 }
721                 msg = ldb_msg_new(tmp_ctx);
722                 if (msg == NULL) {
723                         TALLOC_FREE(tmp_ctx);
724                         return WERR_NOMEM;
725                 }
726                 msg->dn = ldb_dn_new(msg, ldb, "");
727                 if (msg->dn == NULL) {
728                         TALLOC_FREE(tmp_ctx);
729                         return WERR_NOMEM;
730                 }
731
732                 ret = ldb_msg_add_string(msg, "schemaUpdateNow", "1");
733                 if (ret != LDB_SUCCESS) {
734                         TALLOC_FREE(tmp_ctx);
735                         return WERR_INTERNAL_ERROR;
736                 }
737
738                 ret = ldb_build_mod_req(&req, ldb, objects,
739                                 msg,
740                                 LDB_SCOPE_BASE,
741                                 NULL,
742                                 ldb_op_default_callback,
743                                 NULL);
744
745                 if (ret != LDB_SUCCESS) {
746                         TALLOC_FREE(tmp_ctx);
747                         return WERR_DS_DRA_INTERNAL_ERROR;
748                 }
749
750                 ret = ldb_transaction_start(ldb);
751                 if (ret != LDB_SUCCESS) {
752                         TALLOC_FREE(tmp_ctx);
753                         DEBUG(0, ("Autotransaction start failed\n"));
754                         return WERR_DS_DRA_INTERNAL_ERROR;
755                 }
756
757                 ret = ldb_request(ldb, req);
758                 if (ret == LDB_SUCCESS) {
759                         ret = ldb_wait(req->handle, LDB_WAIT_ALL);
760                 }
761
762                 if (ret == LDB_SUCCESS) {
763                         ret = ldb_transaction_commit(ldb);
764                 } else {
765                         DEBUG(0, ("Schema update now failed: %s\n",
766                                   ldb_errstring(ldb)));
767                         ldb_transaction_cancel(ldb);
768                 }
769
770                 if (ret != LDB_SUCCESS) {
771                         DEBUG(0, ("Commit failed: %s\n", ldb_errstring(ldb)));
772                         TALLOC_FREE(tmp_ctx);
773                         return WERR_DS_INTERNAL_FAILURE;
774                 }
775         }
776
777         DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
778                  objects->num_objects, objects->linked_attributes_count,
779                  ldb_dn_get_linearized(objects->partition_dn)));
780                  
781         TALLOC_FREE(tmp_ctx);
782         return WERR_OK;
783 }
784
785 static WERROR dsdb_origin_object_convert(struct ldb_context *ldb,
786                                          const struct dsdb_schema *schema,
787                                          const struct drsuapi_DsReplicaObjectListItem *in,
788                                          TALLOC_CTX *mem_ctx,
789                                          struct ldb_message **_msg)
790 {
791         WERROR status;
792         unsigned int i;
793         struct ldb_message *msg;
794
795         if (!in->object.identifier) {
796                 return WERR_FOOBAR;
797         }
798
799         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
800                 return WERR_FOOBAR;
801         }
802
803         msg = ldb_msg_new(mem_ctx);
804         W_ERROR_HAVE_NO_MEMORY(msg);
805
806         msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
807         W_ERROR_HAVE_NO_MEMORY(msg->dn);
808
809         msg->num_elements       = in->object.attribute_ctr.num_attributes;
810         msg->elements           = talloc_array(msg, struct ldb_message_element,
811                                                msg->num_elements);
812         W_ERROR_HAVE_NO_MEMORY(msg->elements);
813
814         for (i=0; i < msg->num_elements; i++) {
815                 struct drsuapi_DsReplicaAttribute *a;
816                 struct ldb_message_element *e;
817
818                 a = &in->object.attribute_ctr.attributes[i];
819                 e = &msg->elements[i];
820
821                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, schema->prefixmap,
822                                                        a, msg->elements, e);
823                 W_ERROR_NOT_OK_RETURN(status);
824         }
825
826
827         *_msg = msg;
828
829         return WERR_OK;
830 }
831
832 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
833                                   TALLOC_CTX *mem_ctx,
834                                   const struct drsuapi_DsReplicaObjectListItem *first_object,
835                                   uint32_t *_num,
836                                   uint32_t dsdb_repl_flags,
837                                   struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
838 {
839         WERROR status;
840         const struct dsdb_schema *schema;
841         const struct drsuapi_DsReplicaObjectListItem *cur;
842         struct ldb_message **objects;
843         struct drsuapi_DsReplicaObjectIdentifier2 *ids;
844         uint32_t i;
845         uint32_t num_objects = 0;
846         const char * const attrs[] = {
847                 "objectGUID",
848                 "objectSid",
849                 NULL
850         };
851         struct ldb_result *res;
852         int ret;
853
854         for (cur = first_object; cur; cur = cur->next_object) {
855                 num_objects++;
856         }
857
858         if (num_objects == 0) {
859                 return WERR_OK;
860         }
861
862         ret = ldb_transaction_start(ldb);
863         if (ret != LDB_SUCCESS) {
864                 return WERR_DS_INTERNAL_FAILURE;
865         }
866
867         objects = talloc_array(mem_ctx, struct ldb_message *,
868                                num_objects);
869         if (objects == NULL) {
870                 status = WERR_NOMEM;
871                 goto cancel;
872         }
873
874         schema = dsdb_get_schema(ldb, objects);
875         if (!schema) {
876                 return WERR_DS_SCHEMA_NOT_LOADED;
877         }
878
879         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
880                 status = dsdb_origin_object_convert(ldb, schema, cur,
881                                                     objects, &objects[i]);
882                 if (!W_ERROR_IS_OK(status)) {
883                         goto cancel;
884                 }
885         }
886
887         ids = talloc_array(mem_ctx,
888                            struct drsuapi_DsReplicaObjectIdentifier2,
889                            num_objects);
890         if (ids == NULL) {
891                 status = WERR_NOMEM;
892                 goto cancel;
893         }
894
895         if (dsdb_repl_flags & DSDB_REPL_FLAG_ADD_NCNAME) {
896                 /* check for possible NC creation */
897                 for (i=0; i < num_objects; i++) {
898                         struct ldb_message *msg = objects[i];
899                         struct ldb_message_element *el;
900                         struct ldb_dn *nc_dn;
901
902                         if (ldb_msg_check_string_attribute(msg, "objectClass", "crossRef") == 0) {
903                                 continue;
904                         }
905                         el = ldb_msg_find_element(msg, "nCName");
906                         if (el == NULL || el->num_values != 1) {
907                                 continue;
908                         }
909                         nc_dn = ldb_dn_from_ldb_val(objects, ldb, &el->values[0]);
910                         if (!ldb_dn_validate(nc_dn)) {
911                                 continue;
912                         }
913                         ret = dsdb_create_partial_replica_NC(ldb, nc_dn);
914                         if (ret != LDB_SUCCESS) {
915                                 status = WERR_DS_INTERNAL_FAILURE;
916                                 goto cancel;
917                         }
918                 }
919         }
920
921         for (i=0; i < num_objects; i++) {
922                 struct dom_sid *sid = NULL;
923                 struct ldb_request *add_req;
924
925                 DEBUG(6,(__location__ ": adding %s\n", 
926                          ldb_dn_get_linearized(objects[i]->dn)));
927
928                 ret = ldb_build_add_req(&add_req,
929                                         ldb,
930                                         objects,
931                                         objects[i],
932                                         NULL,
933                                         NULL,
934                                         ldb_op_default_callback,
935                                         NULL);
936                 if (ret != LDB_SUCCESS) {
937                         status = WERR_DS_INTERNAL_FAILURE;
938                         goto cancel;
939                 }
940
941                 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
942                 if (ret != LDB_SUCCESS) {
943                         status = WERR_DS_INTERNAL_FAILURE;
944                         goto cancel;
945                 }
946                 
947                 ret = ldb_request(ldb, add_req);
948                 if (ret == LDB_SUCCESS) {
949                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
950                 }
951                 if (ret != LDB_SUCCESS) {
952                         DEBUG(0,(__location__ ": Failed add of %s - %s\n",
953                                  ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
954                         status = WERR_DS_INTERNAL_FAILURE;
955                         goto cancel;
956                 }
957
958                 talloc_free(add_req);
959
960                 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
961                                  LDB_SCOPE_BASE, attrs,
962                                  "(objectClass=*)");
963                 if (ret != LDB_SUCCESS) {
964                         status = WERR_DS_INTERNAL_FAILURE;
965                         goto cancel;
966                 }
967                 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
968                 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
969                 if (sid) {
970                         ids[i].sid = *sid;
971                 } else {
972                         ZERO_STRUCT(ids[i].sid);
973                 }
974         }
975
976         ret = ldb_transaction_commit(ldb);
977         if (ret != LDB_SUCCESS) {
978                 return WERR_DS_INTERNAL_FAILURE;
979         }
980
981         talloc_free(objects);
982
983         *_num = num_objects;
984         *_ids = ids;
985         return WERR_OK;
986
987 cancel:
988         talloc_free(objects);
989         ldb_transaction_cancel(ldb);
990         return status;
991 }