b0abc1acbeec622c51e1dc87ff028c735353f430
[metze/samba/wip.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 static WERROR dsdb_repl_merge_working_schema(struct ldb_context *ldb,
35                                              struct dsdb_schema *dest_schema,
36                                              const struct dsdb_schema *ref_schema)
37 {
38         const struct dsdb_class *cur_class = NULL;
39         const struct dsdb_attribute *cur_attr = NULL;
40         int ret;
41
42         for (cur_class = ref_schema->classes;
43              cur_class;
44              cur_class = cur_class->next)
45         {
46                 const struct dsdb_class *tmp1;
47                 struct dsdb_class *tmp2;
48
49                 tmp1 = dsdb_class_by_governsID_id(dest_schema,
50                                                   cur_class->governsID_id);
51                 if (tmp1 != NULL) {
52                         continue;
53                 }
54
55                 /*
56                  * Do a shallow copy so that original next and prev are
57                  * not modified, we don't need to do a deep copy
58                  * as the rest won't be modified and this is for
59                  * a short lived object.
60                  */
61                 tmp2 = talloc(dest_schema->classes, struct dsdb_class);
62                 if (tmp2 == NULL) {
63                         return WERR_NOMEM;
64                 }
65                 *tmp2 = *cur_class;
66                 DLIST_ADD(dest_schema->classes, tmp2);
67         }
68
69         for (cur_attr = ref_schema->attributes;
70              cur_attr;
71              cur_attr = cur_attr->next)
72         {
73                 const struct dsdb_attribute *tmp1;
74                 struct dsdb_attribute *tmp2;
75
76                 tmp1 = dsdb_attribute_by_attributeID_id(dest_schema,
77                                                 cur_attr->attributeID_id);
78                 if (tmp1 != NULL) {
79                         continue;
80                 }
81
82                 /*
83                  * Do a shallow copy so that original next and prev are
84                  * not modified, we don't need to do a deep copy
85                  * as the rest won't be modified and this is for
86                  * a short lived object.
87                  */
88                 tmp2 = talloc(dest_schema->attributes, struct dsdb_attribute);
89                 if (tmp2 == NULL) {
90                         return WERR_NOMEM;
91                 }
92                 *tmp2 = *cur_attr;
93                 DLIST_ADD(dest_schema->attributes, tmp2);
94         }
95
96         ret = dsdb_setup_sorted_accessors(ldb, dest_schema);
97         if (LDB_SUCCESS != ret) {
98                 DEBUG(0,("Failed to add new attribute to reference schema!\n"));
99                 return WERR_INTERNAL_ERROR;
100         }
101
102         return WERR_OK;
103 }
104
105 WERROR dsdb_repl_resolve_working_schema(struct ldb_context *ldb,
106                                         TALLOC_CTX *mem_ctx,
107                                         struct dsdb_schema_prefixmap *pfm_remote,
108                                         uint32_t cycle_before_switching,
109                                         struct dsdb_schema *initial_schema,
110                                         struct dsdb_schema *resulting_schema,
111                                         uint32_t object_count,
112                                         const struct drsuapi_DsReplicaObjectListItemEx *first_object)
113 {
114         struct schema_list {
115                 struct schema_list *next, *prev;
116                 const struct drsuapi_DsReplicaObjectListItemEx *obj;
117         };
118         struct schema_list *schema_list = NULL, *schema_list_item, *schema_list_next_item;
119         WERROR werr;
120         struct dsdb_schema *working_schema;
121         const struct drsuapi_DsReplicaObjectListItemEx *cur;
122         DATA_BLOB empty_key = data_blob_null;
123         int ret, pass_no;
124         uint32_t ignore_attids[] = {
125                         DRSUAPI_ATTID_auxiliaryClass,
126                         DRSUAPI_ATTID_mayContain,
127                         DRSUAPI_ATTID_mustContain,
128                         DRSUAPI_ATTID_possSuperiors,
129                         DRSUAPI_ATTID_systemPossSuperiors,
130                         DRSUAPI_ATTID_INVALID
131         };
132
133         /* create a list of objects yet to be converted */
134         for (cur = first_object; cur; cur = cur->next_object) {
135                 schema_list_item = talloc(mem_ctx, struct schema_list);
136                 if (schema_list_item == NULL) {
137                         return WERR_NOMEM;
138                 }
139
140                 schema_list_item->obj = cur;
141                 DLIST_ADD_END(schema_list, schema_list_item, struct schema_list);
142         }
143
144         /* resolve objects until all are resolved and in local schema */
145         pass_no = 1;
146         working_schema = initial_schema;
147
148         while (schema_list) {
149                 uint32_t converted_obj_count = 0;
150                 uint32_t failed_obj_count = 0;
151
152                 if (resulting_schema != working_schema) {
153                         /*
154                          * If the selfmade schema is not the schema used to
155                          * translate and validate replicated object,
156                          * Which means that we are using the bootstrap schema
157                          * Then we add attributes and classes that were already
158                          * translated to the working schema, the idea is that
159                          * we might need to add new attributes and classes
160                          * to be able to translate critical replicated objects
161                          * and without that we wouldn't be able to translate them
162                          */
163                         werr = dsdb_repl_merge_working_schema(ldb,
164                                                         working_schema,
165                                                         resulting_schema);
166                         if (!W_ERROR_IS_OK(werr)) {
167                                 return werr;
168                         }
169                 }
170
171                 for (schema_list_item = schema_list;
172                      schema_list_item;
173                      schema_list_item=schema_list_next_item) {
174                         struct dsdb_extended_replicated_object object;
175
176                         cur = schema_list_item->obj;
177
178                         /*
179                          * Save the next item, now we have saved out
180                          * the current one, so we can DLIST_REMOVE it
181                          * safely
182                          */
183                         schema_list_next_item = schema_list_item->next;
184
185                         /*
186                          * Convert the objects into LDB messages using the
187                          * schema we have so far. It's ok if we fail to convert
188                          * an object. We should convert more objects on next pass.
189                          */
190                         werr = dsdb_convert_object_ex(ldb, working_schema,
191                                                       pfm_remote,
192                                                       cur, &empty_key,
193                                                       ignore_attids,
194                                                       0,
195                                                       schema_list_item, &object);
196                         if (!W_ERROR_IS_OK(werr)) {
197                                 DEBUG(4,("debug: Failed to convert schema "
198                                          "object %s into ldb msg, "
199                                          "will try during next loop\n",
200                                          cur->object.identifier->dn));
201
202                                 failed_obj_count++;
203                         } else {
204                                 /*
205                                  * Convert the schema from ldb_message format
206                                  * (OIDs as OID strings) into schema, using
207                                  * the remote prefixMap
208                                  *
209                                  * It's not likely, but possible to get the
210                                  * same object twice and we should keep
211                                  * the last instance.
212                                  */
213                                 werr = dsdb_schema_set_el_from_ldb_msg_dups(ldb,
214                                                                 resulting_schema,
215                                                                 object.msg,
216                                                                 true);
217                                 if (!W_ERROR_IS_OK(werr)) {
218                                         DEBUG(4,("debug: failed to convert "
219                                                  "object %s into a schema element, "
220                                                  "will try during next loop: %s\n",
221                                                  ldb_dn_get_linearized(object.msg->dn),
222                                                  win_errstr(werr)));
223                                         failed_obj_count++;
224                                 } else {
225                                         DEBUG(8,("Converted object %s into a schema element\n",
226                                                  ldb_dn_get_linearized(object.msg->dn)));
227                                         DLIST_REMOVE(schema_list, schema_list_item);
228                                         TALLOC_FREE(schema_list_item);
229                                         converted_obj_count++;
230                                 }
231                         }
232                 }
233
234                 DEBUG(4,("Schema load pass %d: converted %d, %d of %d objects left to be converted.\n",
235                          pass_no, converted_obj_count, failed_obj_count, object_count));
236
237                 /* check if we converted any objects in this pass */
238                 if (converted_obj_count == 0) {
239                         DEBUG(0,("Can't continue Schema load: "
240                                  "didn't manage to convert any objects: "
241                                  "all %d remaining of %d objects "
242                                  "failed to convert\n",
243                                  failed_obj_count, object_count));
244                         return WERR_INTERNAL_ERROR;
245                 }
246
247                 /*
248                  * Don't try to load the schema if there is missing object
249                  * _and_ we are on the first pass as some critical objects
250                  * might be missing.
251                  */
252                 if (failed_obj_count == 0 || pass_no > cycle_before_switching) {
253                         /* prepare for another cycle */
254                         working_schema = resulting_schema;
255
256                         ret = dsdb_setup_sorted_accessors(ldb, working_schema);
257                         if (LDB_SUCCESS != ret) {
258                                 DEBUG(0,("Failed to create schema-cache indexes!\n"));
259                                 return WERR_INTERNAL_ERROR;
260                         }
261                 }
262                 pass_no++;
263         }
264
265         return WERR_OK;
266 }
267
268 /**
269  * Multi-pass working schema creation
270  * Function will:
271  *  - shallow copy initial schema supplied
272  *  - create a working schema in multiple passes
273  *    until all objects are resolved
274  * Working schema is a schema with Attributes, Classes
275  * and indexes, but w/o subClassOf, possibleSupperiors etc.
276  * It is to be used just us cache for converting attribute values.
277  */
278 WERROR dsdb_repl_make_working_schema(struct ldb_context *ldb,
279                                      const struct dsdb_schema *initial_schema,
280                                      const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
281                                      uint32_t object_count,
282                                      const struct drsuapi_DsReplicaObjectListItemEx *first_object,
283                                      const DATA_BLOB *gensec_skey,
284                                      TALLOC_CTX *mem_ctx,
285                                      struct dsdb_schema **_schema_out)
286 {
287         WERROR werr;
288         struct dsdb_schema_prefixmap *pfm_remote;
289         struct dsdb_schema *working_schema;
290
291         /* make a copy of the iniatial_scheam so we don't mess with it */
292         working_schema = dsdb_schema_copy_shallow(mem_ctx, ldb, initial_schema);
293         if (!working_schema) {
294                 DEBUG(0,(__location__ ": schema copy failed!\n"));
295                 return WERR_NOMEM;
296         }
297
298         /* we are going to need remote prefixMap for decoding */
299         werr = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
300                                                 mem_ctx, &pfm_remote, NULL);
301         if (!W_ERROR_IS_OK(werr)) {
302                 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s",
303                          win_errstr(werr)));
304                 return werr;
305         }
306
307         werr = dsdb_repl_resolve_working_schema(ldb, mem_ctx,
308                                                 pfm_remote,
309                                                 0, /* cycle_before_switching */
310                                                 working_schema,
311                                                 working_schema,
312                                                 object_count,
313                                                 first_object);
314         if (!W_ERROR_IS_OK(werr)) {
315                 DEBUG(0, ("%s: dsdb_repl_resolve_working_schema() failed: %s",
316                           __location__, win_errstr(werr)));
317                 return werr;
318         }
319
320         *_schema_out = working_schema;
321
322         return WERR_OK;
323 }
324
325 static bool dsdb_attid_in_list(const uint32_t attid_list[], uint32_t attid)
326 {
327         const uint32_t *cur;
328         if (!attid_list) {
329                 return false;
330         }
331         for (cur = attid_list; *cur != DRSUAPI_ATTID_INVALID; cur++) {
332                 if (*cur == attid) {
333                         return true;
334                 }
335         }
336         return false;
337 }
338
339 WERROR dsdb_convert_object_ex(struct ldb_context *ldb,
340                               const struct dsdb_schema *schema,
341                               const struct dsdb_schema_prefixmap *pfm_remote,
342                               const struct drsuapi_DsReplicaObjectListItemEx *in,
343                               const DATA_BLOB *gensec_skey,
344                               const uint32_t *ignore_attids,
345                               uint32_t dsdb_repl_flags,
346                               TALLOC_CTX *mem_ctx,
347                               struct dsdb_extended_replicated_object *out)
348 {
349         NTSTATUS nt_status;
350         WERROR status;
351         uint32_t i;
352         struct ldb_message *msg;
353         struct replPropertyMetaDataBlob *md;
354         int instanceType;
355         struct ldb_message_element *instanceType_e = NULL;
356         struct ldb_val guid_value;
357         struct ldb_val parent_guid_value;
358         NTTIME whenChanged = 0;
359         time_t whenChanged_t;
360         const char *whenChanged_s;
361         struct drsuapi_DsReplicaAttribute *name_a = NULL;
362         struct drsuapi_DsReplicaMetaData *name_d = NULL;
363         struct replPropertyMetaData1 *rdn_m = NULL;
364         struct dom_sid *sid = NULL;
365         uint32_t rid = 0;
366         uint32_t attr_count;
367         int ret;
368
369         if (!in->object.identifier) {
370                 return WERR_FOOBAR;
371         }
372
373         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
374                 return WERR_FOOBAR;
375         }
376
377         if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
378                 return WERR_FOOBAR;
379         }
380
381         if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
382                 return WERR_FOOBAR;
383         }
384
385         sid = &in->object.identifier->sid;
386         if (sid->num_auths > 0) {
387                 rid = sid->sub_auths[sid->num_auths - 1];
388         }
389
390         msg = ldb_msg_new(mem_ctx);
391         W_ERROR_HAVE_NO_MEMORY(msg);
392
393         msg->dn                 = ldb_dn_new(msg, ldb, in->object.identifier->dn);
394         W_ERROR_HAVE_NO_MEMORY(msg->dn);
395
396         msg->num_elements       = in->object.attribute_ctr.num_attributes;
397         msg->elements           = talloc_array(msg, struct ldb_message_element,
398                                                msg->num_elements + 1); /* +1 because of the RDN attribute */
399         W_ERROR_HAVE_NO_MEMORY(msg->elements);
400
401         md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
402         W_ERROR_HAVE_NO_MEMORY(md);
403
404         md->version             = 1;
405         md->reserved            = 0;
406         md->ctr.ctr1.count      = in->meta_data_ctr->count;
407         md->ctr.ctr1.reserved   = 0;
408         md->ctr.ctr1.array      = talloc_array(mem_ctx,
409                                                struct replPropertyMetaData1,
410                                                md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
411         W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
412
413         for (i=0, attr_count=0; i < in->meta_data_ctr->count; i++, attr_count++) {
414                 struct drsuapi_DsReplicaAttribute *a;
415                 struct drsuapi_DsReplicaMetaData *d;
416                 struct replPropertyMetaData1 *m;
417                 struct ldb_message_element *e;
418                 uint32_t j;
419
420                 a = &in->object.attribute_ctr.attributes[i];
421                 d = &in->meta_data_ctr->meta_data[i];
422                 m = &md->ctr.ctr1.array[attr_count];
423                 e = &msg->elements[attr_count];
424
425                 if (dsdb_attid_in_list(ignore_attids, a->attid)) {
426                         attr_count--;
427                         continue;
428                 }
429
430                 if (a->attid == DRSUAPI_ATTID_instanceType) {
431                         if (instanceType_e != NULL) {
432                                 return WERR_FOOBAR;
433                         }
434                         instanceType_e = e;
435                 }
436
437                 for (j=0; j<a->value_ctr.num_values; j++) {
438                         status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob, gensec_skey, rid, a);
439                         W_ERROR_NOT_OK_RETURN(status);
440                 }
441
442                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
443                                                        a, msg->elements, e);
444                 W_ERROR_NOT_OK_RETURN(status);
445
446                 m->attid                        = a->attid;
447                 m->version                      = d->version;
448                 m->originating_change_time      = d->originating_change_time;
449                 m->originating_invocation_id    = d->originating_invocation_id;
450                 m->originating_usn              = d->originating_usn;
451                 m->local_usn                    = 0;
452
453                 if (d->originating_change_time > whenChanged) {
454                         whenChanged = d->originating_change_time;
455                 }
456
457                 if (a->attid == DRSUAPI_ATTID_name) {
458                         name_a = a;
459                         name_d = d;
460                 }
461         }
462
463         msg->num_elements = attr_count;
464         md->ctr.ctr1.count = attr_count;
465         if (name_a) {
466                 rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
467         }
468
469         if (rdn_m) {
470                 struct ldb_message_element *el;
471                 const char *rdn_name = NULL;
472                 const struct ldb_val *rdn_value = NULL;
473                 const struct dsdb_attribute *rdn_attr = NULL;
474                 uint32_t rdn_attid;
475
476                 /*
477                  * We only need the schema calls for the RDN in this
478                  * codepath, and by doing this we avoid needing to
479                  * have the dsdb_attribute_by_lDAPDisplayName accessor
480                  * working during the schema load.
481                  */
482                 rdn_name        = ldb_dn_get_rdn_name(msg->dn);
483                 rdn_attr        = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
484                 if (!rdn_attr) {
485                         return WERR_FOOBAR;
486                 }
487                 rdn_attid       = rdn_attr->attributeID_id;
488                 rdn_value       = ldb_dn_get_rdn_val(msg->dn);
489
490                 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
491                 if (!el) {
492                         ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
493                         if (ret != LDB_SUCCESS) {
494                                 return WERR_FOOBAR;
495                         }
496                 } else {
497                         if (el->num_values != 1) {
498                                 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
499                                          el->num_values));
500                                 return WERR_FOOBAR;                             
501                         }
502                         if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
503                                 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
504                                          (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
505                                          (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
506                                 return WERR_FOOBAR;                             
507                         }
508                 }
509
510                 rdn_m->attid                            = rdn_attid;
511                 rdn_m->version                          = name_d->version;
512                 rdn_m->originating_change_time          = name_d->originating_change_time;
513                 rdn_m->originating_invocation_id        = name_d->originating_invocation_id;
514                 rdn_m->originating_usn                  = name_d->originating_usn;
515                 rdn_m->local_usn                        = 0;
516                 md->ctr.ctr1.count++;
517
518         }
519
520         if (instanceType_e == NULL) {
521                 return WERR_FOOBAR;
522         }
523
524         instanceType = ldb_msg_find_attr_as_int(msg, "instanceType", 0);
525         if (dsdb_repl_flags & DSDB_REPL_FLAG_PARTIAL_REPLICA) {
526                 /* the instanceType type for partial_replica
527                    replication is sent via DRS with TYPE_WRITE set, but
528                    must be used on the client with TYPE_WRITE removed
529                 */
530                 if (instanceType & INSTANCE_TYPE_WRITE) {
531                         /*
532                          * Make sure we do not change the order
533                          * of msg->elements!
534                          *
535                          * That's why we use
536                          * instanceType_e->num_values = 0
537                          * instead of
538                          * ldb_msg_remove_attr(msg, "instanceType");
539                          */
540                         struct ldb_message_element *e;
541
542                         e = ldb_msg_find_element(msg, "instanceType");
543                         if (e != instanceType_e) {
544                                 DEBUG(0,("instanceType_e[%p] changed to e[%p]\n",
545                                          instanceType_e, e));
546                                 return WERR_FOOBAR;
547                         }
548
549                         instanceType_e->num_values = 0;
550
551                         instanceType &= ~INSTANCE_TYPE_WRITE;
552                         if (ldb_msg_add_fmt(msg, "instanceType", "%d", instanceType) != LDB_SUCCESS) {
553                                 return WERR_INTERNAL_ERROR;
554                         }
555                 }
556         } else {
557                 if (!(instanceType & INSTANCE_TYPE_WRITE)) {
558                         DEBUG(0, ("Refusing to replicate %s from a read-only repilca into a read-write replica!\n",
559                                   ldb_dn_get_linearized(msg->dn)));
560                         return WERR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA;
561                 }
562         }
563
564         whenChanged_t = nt_time_to_unix(whenChanged);
565         whenChanged_s = ldb_timestring(msg, whenChanged_t);
566         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
567
568         nt_status = GUID_to_ndr_blob(&in->object.identifier->guid, msg, &guid_value);
569         if (!NT_STATUS_IS_OK(nt_status)) {
570                 return ntstatus_to_werror(nt_status);
571         }
572
573         if (in->parent_object_guid) {
574                 nt_status = GUID_to_ndr_blob(in->parent_object_guid, msg, &parent_guid_value);
575                 if (!NT_STATUS_IS_OK(nt_status)) {
576                         return ntstatus_to_werror(nt_status);
577                 }
578         } else {
579                 parent_guid_value = data_blob_null;
580         }
581
582         out->msg                = msg;
583         out->guid_value         = guid_value;
584         out->parent_guid_value  = parent_guid_value;
585         out->when_changed       = whenChanged_s;
586         out->meta_data          = md;
587         return WERR_OK;
588 }
589
590 WERROR dsdb_replicated_objects_convert(struct ldb_context *ldb,
591                                        const struct dsdb_schema *schema,
592                                        const char *partition_dn_str,
593                                        const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
594                                        uint32_t object_count,
595                                        const struct drsuapi_DsReplicaObjectListItemEx *first_object,
596                                        uint32_t linked_attributes_count,
597                                        const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
598                                        const struct repsFromTo1 *source_dsa,
599                                        const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
600                                        const DATA_BLOB *gensec_skey,
601                                        uint32_t dsdb_repl_flags,
602                                        TALLOC_CTX *mem_ctx,
603                                        struct dsdb_extended_replicated_objects **objects)
604 {
605         WERROR status;
606         struct ldb_dn *partition_dn;
607         struct dsdb_schema_prefixmap *pfm_remote;
608         struct dsdb_extended_replicated_objects *out;
609         const struct drsuapi_DsReplicaObjectListItemEx *cur;
610         uint32_t i;
611
612         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
613         W_ERROR_HAVE_NO_MEMORY(out);
614         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
615         out->dsdb_repl_flags    = dsdb_repl_flags;
616
617         /*
618          * Ensure schema is kept valid for as long as 'out'
619          * which may contain pointers to it
620          */
621         schema = talloc_reference(out, schema);
622         W_ERROR_HAVE_NO_MEMORY(schema);
623
624         partition_dn = ldb_dn_new(out, ldb, partition_dn_str);
625         W_ERROR_HAVE_NO_MEMORY_AND_FREE(partition_dn, out);
626
627         status = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
628                                                   out, &pfm_remote, NULL);
629         if (!W_ERROR_IS_OK(status)) {
630                 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s",
631                          win_errstr(status)));
632                 talloc_free(out);
633                 return status;
634         }
635
636         if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
637                 /*
638                  * check for schema changes in case
639                  * we are not replicating Schema NC
640                  */
641                 status = dsdb_schema_info_cmp(schema, mapping_ctr);
642                 if (!W_ERROR_IS_OK(status)) {
643                         DEBUG(1,("Remote schema has changed while replicating %s\n",
644                                  partition_dn_str));
645                         talloc_free(out);
646                         return status;
647                 }
648         }
649
650         out->partition_dn       = partition_dn;
651
652         out->source_dsa         = source_dsa;
653         out->uptodateness_vector= uptodateness_vector;
654
655         out->num_objects        = object_count;
656         out->objects            = talloc_array(out,
657                                                struct dsdb_extended_replicated_object,
658                                                out->num_objects);
659         W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
660
661         /* pass the linked attributes down to the repl_meta_data
662            module */
663         out->linked_attributes_count = linked_attributes_count;
664         out->linked_attributes       = linked_attributes;
665
666         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
667                 if (i == out->num_objects) {
668                         talloc_free(out);
669                         return WERR_FOOBAR;
670                 }
671
672                 status = dsdb_convert_object_ex(ldb, schema, pfm_remote,
673                                                 cur, gensec_skey,
674                                                 NULL,
675                                                 dsdb_repl_flags,
676                                                 out->objects, &out->objects[i]);
677                 if (!W_ERROR_IS_OK(status)) {
678                         talloc_free(out);
679                         DEBUG(0,("Failed to convert object %s: %s\n",
680                                  cur->object.identifier->dn,
681                                  win_errstr(status)));
682                         return status;
683                 }
684         }
685         if (i != out->num_objects) {
686                 talloc_free(out);
687                 return WERR_FOOBAR;
688         }
689
690         /* free pfm_remote, we won't need it anymore */
691         talloc_free(pfm_remote);
692
693         *objects = out;
694         return WERR_OK;
695 }
696
697 /**
698  * Commits a list of replicated objects.
699  *
700  * @param working_schema dsdb_schema to be used for resolving
701  *                       Classes/Attributes during Schema replication. If not NULL,
702  *                       it will be set on ldb and used while committing replicated objects
703  */
704 WERROR dsdb_replicated_objects_commit(struct ldb_context *ldb,
705                                       struct dsdb_schema *working_schema,
706                                       struct dsdb_extended_replicated_objects *objects,
707                                       uint64_t *notify_uSN)
708 {
709         WERROR werr;
710         struct ldb_result *ext_res;
711         struct dsdb_schema *cur_schema = NULL;
712         struct dsdb_schema *new_schema = NULL;
713         int ret;
714         uint64_t seq_num1, seq_num2;
715         bool used_global_schema = false;
716
717         TALLOC_CTX *tmp_ctx = talloc_new(objects);
718         if (!tmp_ctx) {
719                 DEBUG(0,("Failed to start talloc\n"));
720                 return WERR_NOMEM;
721         }
722
723         /* TODO: handle linked attributes */
724
725         /* wrap the extended operation in a transaction 
726            See [MS-DRSR] 3.3.2 Transactions
727          */
728         ret = ldb_transaction_start(ldb);
729         if (ret != LDB_SUCCESS) {
730                 DEBUG(0,(__location__ " Failed to start transaction\n"));
731                 return WERR_FOOBAR;
732         }
733
734         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
735         if (ret != LDB_SUCCESS) {
736                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
737                 ldb_transaction_cancel(ldb);
738                 TALLOC_FREE(tmp_ctx);
739                 return WERR_FOOBAR;             
740         }
741
742         /*
743          * Set working_schema for ldb in case we are replicating from Schema NC.
744          * Schema won't be reloaded during Replicated Objects commit, as it is
745          * done in a transaction. So we need some way to search for newly
746          * added Classes and Attributes
747          */
748         if (working_schema) {
749                 /* store current schema so we can fall back in case of failure */
750                 cur_schema = dsdb_get_schema(ldb, tmp_ctx);
751                 used_global_schema = dsdb_uses_global_schema(ldb);
752
753                 ret = dsdb_reference_schema(ldb, working_schema, false);
754                 if (ret != LDB_SUCCESS) {
755                         DEBUG(0,(__location__ "Failed to reference working schema - %s\n",
756                                  ldb_strerror(ret)));
757                         /* TODO: Map LDB Error to NTSTATUS? */
758                         ldb_transaction_cancel(ldb);
759                         TALLOC_FREE(tmp_ctx);
760                         return WERR_INTERNAL_ERROR;
761                 }
762         }
763
764         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
765         if (ret != LDB_SUCCESS) {
766                 /* restore previous schema */
767                 if (used_global_schema) { 
768                         dsdb_set_global_schema(ldb);
769                 } else if (cur_schema) {
770                         dsdb_reference_schema(ldb, cur_schema, false);
771                 }
772
773                 DEBUG(0,("Failed to apply records: %s: %s\n",
774                          ldb_errstring(ldb), ldb_strerror(ret)));
775                 ldb_transaction_cancel(ldb);
776                 TALLOC_FREE(tmp_ctx);
777                 return WERR_FOOBAR;
778         }
779         talloc_free(ext_res);
780
781         /* Save our updated prefixMap */
782         if (working_schema) {
783                 werr = dsdb_write_prefixes_from_schema_to_ldb(working_schema,
784                                                               ldb,
785                                                               working_schema);
786                 if (!W_ERROR_IS_OK(werr)) {
787                         /* restore previous schema */
788                         if (used_global_schema) { 
789                                 dsdb_set_global_schema(ldb);
790                         } else if (cur_schema ) {
791                                 dsdb_reference_schema(ldb, cur_schema, false);
792                         }
793                         DEBUG(0,("Failed to save updated prefixMap: %s\n",
794                                  win_errstr(werr)));
795                         TALLOC_FREE(tmp_ctx);
796                         return werr;
797                 }
798         }
799
800         ret = ldb_transaction_prepare_commit(ldb);
801         if (ret != LDB_SUCCESS) {
802                 /* restore previous schema */
803                 if (used_global_schema) { 
804                         dsdb_set_global_schema(ldb);
805                 } else if (cur_schema ) {
806                         dsdb_reference_schema(ldb, cur_schema, false);
807                 }
808                 DEBUG(0,(__location__ " Failed to prepare commit of transaction: %s\n",
809                          ldb_errstring(ldb)));
810                 TALLOC_FREE(tmp_ctx);
811                 return WERR_FOOBAR;
812         }
813
814         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
815         if (ret != LDB_SUCCESS) {
816                 /* restore previous schema */
817                 if (used_global_schema) { 
818                         dsdb_set_global_schema(ldb);
819                 } else if (cur_schema ) {
820                         dsdb_reference_schema(ldb, cur_schema, false);
821                 }
822                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
823                 ldb_transaction_cancel(ldb);
824                 TALLOC_FREE(tmp_ctx);
825                 return WERR_FOOBAR;             
826         }
827
828         /* if this replication partner didn't need to be notified
829            before this transaction then it still doesn't need to be
830            notified, as the changes came from this server */    
831         if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
832                 *notify_uSN = seq_num2;
833         }
834
835         ret = ldb_transaction_commit(ldb);
836         if (ret != LDB_SUCCESS) {
837                 /* restore previous schema */
838                 if (used_global_schema) { 
839                         dsdb_set_global_schema(ldb);
840                 } else if (cur_schema ) {
841                         dsdb_reference_schema(ldb, cur_schema, false);
842                 }
843                 DEBUG(0,(__location__ " Failed to commit transaction\n"));
844                 TALLOC_FREE(tmp_ctx);
845                 return WERR_FOOBAR;
846         }
847
848         /*
849          * Reset the Schema used by ldb. This will lead to
850          * a schema cache being refreshed from database.
851          */
852         if (working_schema) {
853                 struct ldb_message *msg;
854                 struct ldb_request *req;
855
856                 /* Force a reload */
857                 working_schema->last_refresh = 0;
858                 new_schema = dsdb_get_schema(ldb, tmp_ctx);
859                 /* TODO: 
860                  * If dsdb_get_schema() fails, we just fall back
861                  * to what we had.  However, the database is probably
862                  * unable to operate for other users from this
863                  * point... */
864                 if (new_schema && used_global_schema) {
865                         dsdb_make_schema_global(ldb, new_schema);
866                 } else if (used_global_schema) { 
867                         DEBUG(0,("Failed to re-load schema after commit of transaction\n"));
868                         dsdb_set_global_schema(ldb);
869                         TALLOC_FREE(tmp_ctx);
870                         return WERR_INTERNAL_ERROR;
871                 } else {
872                         DEBUG(0,("Failed to re-load schema after commit of transaction\n"));
873                         dsdb_reference_schema(ldb, cur_schema, false);
874                         TALLOC_FREE(tmp_ctx);
875                         return WERR_INTERNAL_ERROR;
876                 }
877                 msg = ldb_msg_new(tmp_ctx);
878                 if (msg == NULL) {
879                         TALLOC_FREE(tmp_ctx);
880                         return WERR_NOMEM;
881                 }
882                 msg->dn = ldb_dn_new(msg, ldb, "");
883                 if (msg->dn == NULL) {
884                         TALLOC_FREE(tmp_ctx);
885                         return WERR_NOMEM;
886                 }
887
888                 ret = ldb_msg_add_string(msg, "schemaUpdateNow", "1");
889                 if (ret != LDB_SUCCESS) {
890                         TALLOC_FREE(tmp_ctx);
891                         return WERR_INTERNAL_ERROR;
892                 }
893
894                 ret = ldb_build_mod_req(&req, ldb, objects,
895                                 msg,
896                                 LDB_SCOPE_BASE,
897                                 NULL,
898                                 ldb_op_default_callback,
899                                 NULL);
900
901                 if (ret != LDB_SUCCESS) {
902                         TALLOC_FREE(tmp_ctx);
903                         return WERR_DS_DRA_INTERNAL_ERROR;
904                 }
905
906                 ret = ldb_transaction_start(ldb);
907                 if (ret != LDB_SUCCESS) {
908                         TALLOC_FREE(tmp_ctx);
909                         DEBUG(0, ("Autotransaction start failed\n"));
910                         return WERR_DS_DRA_INTERNAL_ERROR;
911                 }
912
913                 ret = ldb_request(ldb, req);
914                 if (ret == LDB_SUCCESS) {
915                         ret = ldb_wait(req->handle, LDB_WAIT_ALL);
916                 }
917
918                 if (ret == LDB_SUCCESS) {
919                         ret = ldb_transaction_commit(ldb);
920                 } else {
921                         DEBUG(0, ("Schema update now failed: %s\n",
922                                   ldb_errstring(ldb)));
923                         ldb_transaction_cancel(ldb);
924                 }
925
926                 if (ret != LDB_SUCCESS) {
927                         DEBUG(0, ("Commit failed: %s\n", ldb_errstring(ldb)));
928                         TALLOC_FREE(tmp_ctx);
929                         return WERR_DS_INTERNAL_FAILURE;
930                 }
931         }
932
933         DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
934                  objects->num_objects, objects->linked_attributes_count,
935                  ldb_dn_get_linearized(objects->partition_dn)));
936                  
937         TALLOC_FREE(tmp_ctx);
938         return WERR_OK;
939 }
940
941 static WERROR dsdb_origin_object_convert(struct ldb_context *ldb,
942                                          const struct dsdb_schema *schema,
943                                          const struct drsuapi_DsReplicaObjectListItem *in,
944                                          TALLOC_CTX *mem_ctx,
945                                          struct ldb_message **_msg)
946 {
947         WERROR status;
948         unsigned int i;
949         struct ldb_message *msg;
950
951         if (!in->object.identifier) {
952                 return WERR_FOOBAR;
953         }
954
955         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
956                 return WERR_FOOBAR;
957         }
958
959         msg = ldb_msg_new(mem_ctx);
960         W_ERROR_HAVE_NO_MEMORY(msg);
961
962         msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
963         W_ERROR_HAVE_NO_MEMORY(msg->dn);
964
965         msg->num_elements       = in->object.attribute_ctr.num_attributes;
966         msg->elements           = talloc_array(msg, struct ldb_message_element,
967                                                msg->num_elements);
968         W_ERROR_HAVE_NO_MEMORY(msg->elements);
969
970         for (i=0; i < msg->num_elements; i++) {
971                 struct drsuapi_DsReplicaAttribute *a;
972                 struct ldb_message_element *e;
973
974                 a = &in->object.attribute_ctr.attributes[i];
975                 e = &msg->elements[i];
976
977                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, schema->prefixmap,
978                                                        a, msg->elements, e);
979                 W_ERROR_NOT_OK_RETURN(status);
980         }
981
982
983         *_msg = msg;
984
985         return WERR_OK;
986 }
987
988 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
989                                   TALLOC_CTX *mem_ctx,
990                                   const struct drsuapi_DsReplicaObjectListItem *first_object,
991                                   uint32_t *_num,
992                                   uint32_t dsdb_repl_flags,
993                                   struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
994 {
995         WERROR status;
996         const struct dsdb_schema *schema;
997         const struct drsuapi_DsReplicaObjectListItem *cur;
998         struct ldb_message **objects;
999         struct drsuapi_DsReplicaObjectIdentifier2 *ids;
1000         uint32_t i;
1001         uint32_t num_objects = 0;
1002         const char * const attrs[] = {
1003                 "objectGUID",
1004                 "objectSid",
1005                 NULL
1006         };
1007         struct ldb_result *res;
1008         int ret;
1009
1010         for (cur = first_object; cur; cur = cur->next_object) {
1011                 num_objects++;
1012         }
1013
1014         if (num_objects == 0) {
1015                 return WERR_OK;
1016         }
1017
1018         ret = ldb_transaction_start(ldb);
1019         if (ret != LDB_SUCCESS) {
1020                 return WERR_DS_INTERNAL_FAILURE;
1021         }
1022
1023         objects = talloc_array(mem_ctx, struct ldb_message *,
1024                                num_objects);
1025         if (objects == NULL) {
1026                 status = WERR_NOMEM;
1027                 goto cancel;
1028         }
1029
1030         schema = dsdb_get_schema(ldb, objects);
1031         if (!schema) {
1032                 return WERR_DS_SCHEMA_NOT_LOADED;
1033         }
1034
1035         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
1036                 status = dsdb_origin_object_convert(ldb, schema, cur,
1037                                                     objects, &objects[i]);
1038                 if (!W_ERROR_IS_OK(status)) {
1039                         goto cancel;
1040                 }
1041         }
1042
1043         ids = talloc_array(mem_ctx,
1044                            struct drsuapi_DsReplicaObjectIdentifier2,
1045                            num_objects);
1046         if (ids == NULL) {
1047                 status = WERR_NOMEM;
1048                 goto cancel;
1049         }
1050
1051         if (dsdb_repl_flags & DSDB_REPL_FLAG_ADD_NCNAME) {
1052                 /* check for possible NC creation */
1053                 for (i=0; i < num_objects; i++) {
1054                         struct ldb_message *msg = objects[i];
1055                         struct ldb_message_element *el;
1056                         struct ldb_dn *nc_dn;
1057
1058                         if (ldb_msg_check_string_attribute(msg, "objectClass", "crossRef") == 0) {
1059                                 continue;
1060                         }
1061                         el = ldb_msg_find_element(msg, "nCName");
1062                         if (el == NULL || el->num_values != 1) {
1063                                 continue;
1064                         }
1065                         nc_dn = ldb_dn_from_ldb_val(objects, ldb, &el->values[0]);
1066                         if (!ldb_dn_validate(nc_dn)) {
1067                                 continue;
1068                         }
1069                         ret = dsdb_create_partial_replica_NC(ldb, nc_dn);
1070                         if (ret != LDB_SUCCESS) {
1071                                 status = WERR_DS_INTERNAL_FAILURE;
1072                                 goto cancel;
1073                         }
1074                 }
1075         }
1076
1077         for (i=0; i < num_objects; i++) {
1078                 struct dom_sid *sid = NULL;
1079                 struct ldb_request *add_req;
1080
1081                 DEBUG(6,(__location__ ": adding %s\n", 
1082                          ldb_dn_get_linearized(objects[i]->dn)));
1083
1084                 ret = ldb_build_add_req(&add_req,
1085                                         ldb,
1086                                         objects,
1087                                         objects[i],
1088                                         NULL,
1089                                         NULL,
1090                                         ldb_op_default_callback,
1091                                         NULL);
1092                 if (ret != LDB_SUCCESS) {
1093                         status = WERR_DS_INTERNAL_FAILURE;
1094                         goto cancel;
1095                 }
1096
1097                 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
1098                 if (ret != LDB_SUCCESS) {
1099                         status = WERR_DS_INTERNAL_FAILURE;
1100                         goto cancel;
1101                 }
1102                 
1103                 ret = ldb_request(ldb, add_req);
1104                 if (ret == LDB_SUCCESS) {
1105                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
1106                 }
1107                 if (ret != LDB_SUCCESS) {
1108                         DEBUG(0,(__location__ ": Failed add of %s - %s\n",
1109                                  ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
1110                         status = WERR_DS_INTERNAL_FAILURE;
1111                         goto cancel;
1112                 }
1113
1114                 talloc_free(add_req);
1115
1116                 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
1117                                  LDB_SCOPE_BASE, attrs,
1118                                  "(objectClass=*)");
1119                 if (ret != LDB_SUCCESS) {
1120                         status = WERR_DS_INTERNAL_FAILURE;
1121                         goto cancel;
1122                 }
1123                 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
1124                 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
1125                 if (sid) {
1126                         ids[i].sid = *sid;
1127                 } else {
1128                         ZERO_STRUCT(ids[i].sid);
1129                 }
1130         }
1131
1132         ret = ldb_transaction_commit(ldb);
1133         if (ret != LDB_SUCCESS) {
1134                 return WERR_DS_INTERNAL_FAILURE;
1135         }
1136
1137         talloc_free(objects);
1138
1139         *_num = num_objects;
1140         *_ids = ids;
1141         return WERR_OK;
1142
1143 cancel:
1144         talloc_free(objects);
1145         ldb_transaction_cancel(ldb);
1146         return status;
1147 }