repl: Remove old TODO
[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 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, struct dsdb_class);
62                 if (tmp2 == NULL) {
63                         return WERR_NOT_ENOUGH_MEMORY;
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, struct dsdb_attribute);
89                 if (tmp2 == NULL) {
90                         return WERR_NOT_ENOUGH_MEMORY;
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                                         struct dsdb_schema_prefixmap *pfm_remote,
107                                         uint32_t cycle_before_switching,
108                                         struct dsdb_schema *initial_schema,
109                                         struct dsdb_schema *resulting_schema,
110                                         uint32_t object_count,
111                                         const struct drsuapi_DsReplicaObjectListItemEx *first_object)
112 {
113         struct schema_list {
114                 struct schema_list *next, *prev;
115                 const struct drsuapi_DsReplicaObjectListItemEx *obj;
116         };
117         struct schema_list *schema_list = NULL, *schema_list_item, *schema_list_next_item;
118         WERROR werr;
119         struct dsdb_schema *working_schema;
120         const struct drsuapi_DsReplicaObjectListItemEx *cur;
121         DATA_BLOB empty_key = data_blob_null;
122         int ret, pass_no;
123         uint32_t ignore_attids[] = {
124                         DRSUAPI_ATTID_auxiliaryClass,
125                         DRSUAPI_ATTID_mayContain,
126                         DRSUAPI_ATTID_mustContain,
127                         DRSUAPI_ATTID_possSuperiors,
128                         DRSUAPI_ATTID_systemPossSuperiors,
129                         DRSUAPI_ATTID_INVALID
130         };
131         TALLOC_CTX *frame = talloc_stackframe();
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(frame, struct schema_list);
136                 if (schema_list_item == NULL) {
137                         return WERR_NOT_ENOUGH_MEMORY;
138                 }
139
140                 schema_list_item->obj = cur;
141                 DLIST_ADD_END(schema_list, schema_list_item);
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                                 talloc_free(frame);
168                                 return werr;
169                         }
170                 }
171
172                 for (schema_list_item = schema_list;
173                      schema_list_item;
174                      schema_list_item=schema_list_next_item) {
175                         struct dsdb_extended_replicated_object object;
176
177                         cur = schema_list_item->obj;
178
179                         /*
180                          * Save the next item, now we have saved out
181                          * the current one, so we can DLIST_REMOVE it
182                          * safely
183                          */
184                         schema_list_next_item = schema_list_item->next;
185
186                         /*
187                          * Convert the objects into LDB messages using the
188                          * schema we have so far. It's ok if we fail to convert
189                          * an object. We should convert more objects on next pass.
190                          */
191                         werr = dsdb_convert_object_ex(ldb, working_schema,
192                                                       NULL,
193                                                       pfm_remote,
194                                                       cur, &empty_key,
195                                                       ignore_attids,
196                                                       0,
197                                                       schema_list_item, &object);
198                         if (!W_ERROR_IS_OK(werr)) {
199                                 DEBUG(4,("debug: Failed to convert schema "
200                                          "object %s into ldb msg, "
201                                          "will try during next loop\n",
202                                          cur->object.identifier->dn));
203
204                                 failed_obj_count++;
205                         } else {
206                                 /*
207                                  * Convert the schema from ldb_message format
208                                  * (OIDs as OID strings) into schema, using
209                                  * the remote prefixMap
210                                  *
211                                  * It's not likely, but possible to get the
212                                  * same object twice and we should keep
213                                  * the last instance.
214                                  */
215                                 werr = dsdb_schema_set_el_from_ldb_msg_dups(ldb,
216                                                                 resulting_schema,
217                                                                 object.msg,
218                                                                 true);
219                                 if (!W_ERROR_IS_OK(werr)) {
220                                         DEBUG(4,("debug: failed to convert "
221                                                  "object %s into a schema element, "
222                                                  "will try during next loop: %s\n",
223                                                  ldb_dn_get_linearized(object.msg->dn),
224                                                  win_errstr(werr)));
225                                         failed_obj_count++;
226                                 } else {
227                                         DEBUG(8,("Converted object %s into a schema element\n",
228                                                  ldb_dn_get_linearized(object.msg->dn)));
229                                         DLIST_REMOVE(schema_list, schema_list_item);
230                                         TALLOC_FREE(schema_list_item);
231                                         converted_obj_count++;
232                                 }
233                         }
234                 }
235
236                 DEBUG(4,("Schema load pass %d: converted %d, %d of %d objects left to be converted.\n",
237                          pass_no, converted_obj_count, failed_obj_count, object_count));
238
239                 /* check if we converted any objects in this pass */
240                 if (converted_obj_count == 0) {
241                         DEBUG(0,("Can't continue Schema load: "
242                                  "didn't manage to convert any objects: "
243                                  "all %d remaining of %d objects "
244                                  "failed to convert\n",
245                                  failed_obj_count, object_count));
246                         talloc_free(frame);
247                         return WERR_INTERNAL_ERROR;
248                 }
249
250                 /*
251                  * Don't try to load the schema if there is missing object
252                  * _and_ we are on the first pass as some critical objects
253                  * might be missing.
254                  */
255                 if (failed_obj_count == 0 || pass_no > cycle_before_switching) {
256                         /* prepare for another cycle */
257                         working_schema = resulting_schema;
258
259                         ret = dsdb_setup_sorted_accessors(ldb, working_schema);
260                         if (LDB_SUCCESS != ret) {
261                                 DEBUG(0,("Failed to create schema-cache indexes!\n"));
262                                 talloc_free(frame);
263                                 return WERR_INTERNAL_ERROR;
264                         }
265                 }
266                 pass_no++;
267         }
268
269         talloc_free(frame);
270         return WERR_OK;
271 }
272
273 /**
274  * Multi-pass working schema creation
275  * Function will:
276  *  - shallow copy initial schema supplied
277  *  - create a working schema in multiple passes
278  *    until all objects are resolved
279  * Working schema is a schema with Attributes, Classes
280  * and indexes, but w/o subClassOf, possibleSupperiors etc.
281  * It is to be used just us cache for converting attribute values.
282  */
283 WERROR dsdb_repl_make_working_schema(struct ldb_context *ldb,
284                                      const struct dsdb_schema *initial_schema,
285                                      const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
286                                      uint32_t object_count,
287                                      const struct drsuapi_DsReplicaObjectListItemEx *first_object,
288                                      const DATA_BLOB *gensec_skey,
289                                      TALLOC_CTX *mem_ctx,
290                                      struct dsdb_schema **_schema_out)
291 {
292         WERROR werr;
293         struct dsdb_schema_prefixmap *pfm_remote;
294         uint32_t r;
295         struct dsdb_schema *working_schema;
296
297         /* make a copy of the iniatial_scheam so we don't mess with it */
298         working_schema = dsdb_schema_copy_shallow(mem_ctx, ldb, initial_schema);
299         if (!working_schema) {
300                 DEBUG(0,(__location__ ": schema copy failed!\n"));
301                 return WERR_NOT_ENOUGH_MEMORY;
302         }
303         working_schema->resolving_in_progress = true;
304
305         /* we are going to need remote prefixMap for decoding */
306         werr = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
307                                                 working_schema, &pfm_remote, NULL);
308         if (!W_ERROR_IS_OK(werr)) {
309                 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
310                          win_errstr(werr)));
311                 talloc_free(working_schema);
312                 return werr;
313         }
314
315         for (r=0; r < pfm_remote->length; r++) {
316                 const struct dsdb_schema_prefixmap_oid *rm = &pfm_remote->prefixes[r];
317                 bool found_oid = false;
318                 uint32_t l;
319
320                 for (l=0; l < working_schema->prefixmap->length; l++) {
321                         const struct dsdb_schema_prefixmap_oid *lm = &working_schema->prefixmap->prefixes[l];
322                         int cmp;
323
324                         cmp = data_blob_cmp(&rm->bin_oid, &lm->bin_oid);
325                         if (cmp == 0) {
326                                 found_oid = true;
327                                 break;
328                         }
329                 }
330
331                 if (found_oid) {
332                         continue;
333                 }
334
335                 /*
336                  * We prefer the same is as we got from the remote peer
337                  * if there's no conflict.
338                  */
339                 werr = dsdb_schema_pfm_add_entry(working_schema->prefixmap,
340                                                  rm->bin_oid, &rm->id, NULL);
341                 if (!W_ERROR_IS_OK(werr)) {
342                         DEBUG(0,(__location__ ": Failed to merge remote prefixMap: %s",
343                                  win_errstr(werr)));
344                         talloc_free(working_schema);
345                         return werr;
346                 }
347         }
348
349         werr = dsdb_repl_resolve_working_schema(ldb,
350                                                 pfm_remote,
351                                                 0, /* cycle_before_switching */
352                                                 working_schema,
353                                                 working_schema,
354                                                 object_count,
355                                                 first_object);
356         if (!W_ERROR_IS_OK(werr)) {
357                 DEBUG(0, ("%s: dsdb_repl_resolve_working_schema() failed: %s",
358                           __location__, win_errstr(werr)));
359                 talloc_free(working_schema);
360                 return werr;
361         }
362
363         working_schema->resolving_in_progress = false;
364
365         *_schema_out = working_schema;
366
367         return WERR_OK;
368 }
369
370 static bool dsdb_attid_in_list(const uint32_t attid_list[], uint32_t attid)
371 {
372         const uint32_t *cur;
373         if (!attid_list) {
374                 return false;
375         }
376         for (cur = attid_list; *cur != DRSUAPI_ATTID_INVALID; cur++) {
377                 if (*cur == attid) {
378                         return true;
379                 }
380         }
381         return false;
382 }
383
384 WERROR dsdb_convert_object_ex(struct ldb_context *ldb,
385                               const struct dsdb_schema *schema,
386                               struct ldb_dn *partition_dn,
387                               const struct dsdb_schema_prefixmap *pfm_remote,
388                               const struct drsuapi_DsReplicaObjectListItemEx *in,
389                               const DATA_BLOB *gensec_skey,
390                               const uint32_t *ignore_attids,
391                               uint32_t dsdb_repl_flags,
392                               TALLOC_CTX *mem_ctx,
393                               struct dsdb_extended_replicated_object *out)
394 {
395         WERROR status = WERR_OK;
396         uint32_t i;
397         struct ldb_message *msg;
398         struct replPropertyMetaDataBlob *md;
399         int instanceType;
400         struct ldb_message_element *instanceType_e = NULL;
401         NTTIME whenChanged = 0;
402         time_t whenChanged_t;
403         const char *whenChanged_s;
404         struct dom_sid *sid = NULL;
405         uint32_t rid = 0;
406         uint32_t attr_count;
407
408         if (!in->object.identifier) {
409                 return WERR_FOOBAR;
410         }
411
412         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
413                 return WERR_FOOBAR;
414         }
415
416         if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
417                 return WERR_FOOBAR;
418         }
419
420         if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
421                 return WERR_FOOBAR;
422         }
423
424         sid = &in->object.identifier->sid;
425         if (sid->num_auths > 0) {
426                 rid = sid->sub_auths[sid->num_auths - 1];
427         }
428
429         msg = ldb_msg_new(mem_ctx);
430         W_ERROR_HAVE_NO_MEMORY(msg);
431
432         msg->dn                 = ldb_dn_new(msg, ldb, in->object.identifier->dn);
433         W_ERROR_HAVE_NO_MEMORY(msg->dn);
434
435         msg->num_elements       = in->object.attribute_ctr.num_attributes;
436         msg->elements           = talloc_array(msg, struct ldb_message_element,
437                                                msg->num_elements);
438         W_ERROR_HAVE_NO_MEMORY(msg->elements);
439
440         md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
441         W_ERROR_HAVE_NO_MEMORY(md);
442
443         md->version             = 1;
444         md->reserved            = 0;
445         md->ctr.ctr1.count      = in->meta_data_ctr->count;
446         md->ctr.ctr1.reserved   = 0;
447         md->ctr.ctr1.array      = talloc_array(mem_ctx,
448                                                struct replPropertyMetaData1,
449                                                md->ctr.ctr1.count);
450         W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
451
452         for (i=0, attr_count=0; i < in->meta_data_ctr->count; i++, attr_count++) {
453                 struct drsuapi_DsReplicaAttribute *a;
454                 struct drsuapi_DsReplicaMetaData *d;
455                 struct replPropertyMetaData1 *m;
456                 struct ldb_message_element *e;
457                 uint32_t j;
458
459                 a = &in->object.attribute_ctr.attributes[i];
460                 d = &in->meta_data_ctr->meta_data[i];
461                 m = &md->ctr.ctr1.array[attr_count];
462                 e = &msg->elements[attr_count];
463
464                 if (dsdb_attid_in_list(ignore_attids, a->attid)) {
465                         attr_count--;
466                         continue;
467                 }
468
469                 if (GUID_all_zero(&d->originating_invocation_id)) {
470                         status = WERR_DS_SRC_GUID_MISMATCH;
471                         DEBUG(0, ("Refusing replication of object containing invalid zero invocationID on attribute %d of %s: %s\n",
472                                   a->attid,
473                                   ldb_dn_get_linearized(msg->dn),
474                                   win_errstr(status)));
475                         return status;
476                 }
477
478                 if (a->attid == DRSUAPI_ATTID_instanceType) {
479                         if (instanceType_e != NULL) {
480                                 return WERR_FOOBAR;
481                         }
482                         instanceType_e = e;
483                 }
484
485                 for (j=0; j<a->value_ctr.num_values; j++) {
486                         status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob,
487                                                            gensec_skey, rid,
488                                                            dsdb_repl_flags, a);
489                         if (!W_ERROR_IS_OK(status)) {
490                                 break;
491                         }
492                 }
493                 if (W_ERROR_EQUAL(status, WERR_TOO_MANY_SECRETS)) {
494                         WERROR get_name_status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
495                                                                                a, msg->elements, e, NULL);
496                         if (W_ERROR_IS_OK(get_name_status)) {
497                                 DEBUG(0, ("Unxpectedly got secret value %s on %s from DRS server\n",
498                                           e->name, ldb_dn_get_linearized(msg->dn)));
499                         } else {
500                                 DEBUG(0, ("Unxpectedly got secret value on %s from DRS server",
501                                           ldb_dn_get_linearized(msg->dn)));
502                         }
503                 } else if (!W_ERROR_IS_OK(status)) {
504                         return status;
505                 }
506
507                 /*
508                  * This function also fills in the local attid value,
509                  * based on comparing the remote and local prefixMap
510                  * tables.  If we don't convert the value, then we can
511                  * have invalid values in the replPropertyMetaData we
512                  * store on disk, as the prefixMap is per host, not
513                  * per-domain.  This may be why Microsoft added the
514                  * msDS-IntID feature, however this is not used for
515                  * extra attributes in the schema partition itself.
516                  */
517                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, pfm_remote,
518                                                        a, msg->elements, e,
519                                                        &m->attid);
520                 W_ERROR_NOT_OK_RETURN(status);
521
522                 m->version                      = d->version;
523                 m->originating_change_time      = d->originating_change_time;
524                 m->originating_invocation_id    = d->originating_invocation_id;
525                 m->originating_usn              = d->originating_usn;
526                 m->local_usn                    = 0;
527
528                 if (a->attid == DRSUAPI_ATTID_name) {
529                         const struct ldb_val *rdn_val = ldb_dn_get_rdn_val(msg->dn);
530                         if (rdn_val == NULL) {
531                                 DEBUG(0, ("Unxpectedly unable to get RDN from %s for validation",
532                                           ldb_dn_get_linearized(msg->dn)));
533                                 return WERR_FOOBAR;
534                         }
535                         if (e->num_values != 1) {
536                                 DEBUG(0, ("Unxpectedly got wrong number of attribute values (got %u, expected 1) when checking RDN against name of %s",
537                                           e->num_values,
538                                           ldb_dn_get_linearized(msg->dn)));
539                                 return WERR_FOOBAR;
540                         }
541                         if (data_blob_cmp(rdn_val,
542                                           &e->values[0]) != 0) {
543                                 DEBUG(0, ("Unxpectedly got mismatching RDN values when checking RDN against name of %s",
544                                           ldb_dn_get_linearized(msg->dn)));
545                                 return WERR_FOOBAR;
546                         }
547                 }
548                 if (d->originating_change_time > whenChanged) {
549                         whenChanged = d->originating_change_time;
550                 }
551
552         }
553
554         msg->num_elements = attr_count;
555         md->ctr.ctr1.count = attr_count;
556
557         if (instanceType_e == NULL) {
558                 return WERR_FOOBAR;
559         }
560
561         instanceType = ldb_msg_find_attr_as_int(msg, "instanceType", 0);
562
563         if ((instanceType & INSTANCE_TYPE_IS_NC_HEAD)
564             && partition_dn != NULL) {
565                 int partition_dn_cmp = ldb_dn_compare(partition_dn, msg->dn);
566                 if (partition_dn_cmp != 0) {
567                         DEBUG(4, ("Remote server advised us of a new partition %s while processing %s, ignoring\n",
568                                   ldb_dn_get_linearized(msg->dn),
569                                   ldb_dn_get_linearized(partition_dn)));
570                         return WERR_DS_ADD_REPLICA_INHIBITED;
571                 }
572         }
573
574         if (dsdb_repl_flags & DSDB_REPL_FLAG_PARTIAL_REPLICA) {
575                 /* the instanceType type for partial_replica
576                    replication is sent via DRS with TYPE_WRITE set, but
577                    must be used on the client with TYPE_WRITE removed
578                 */
579                 if (instanceType & INSTANCE_TYPE_WRITE) {
580                         /*
581                          * Make sure we do not change the order
582                          * of msg->elements!
583                          *
584                          * That's why we use
585                          * instanceType_e->num_values = 0
586                          * instead of
587                          * ldb_msg_remove_attr(msg, "instanceType");
588                          */
589                         struct ldb_message_element *e;
590
591                         e = ldb_msg_find_element(msg, "instanceType");
592                         if (e != instanceType_e) {
593                                 DEBUG(0,("instanceType_e[%p] changed to e[%p]\n",
594                                          instanceType_e, e));
595                                 return WERR_FOOBAR;
596                         }
597
598                         instanceType_e->num_values = 0;
599
600                         instanceType &= ~INSTANCE_TYPE_WRITE;
601                         if (ldb_msg_add_fmt(msg, "instanceType", "%d", instanceType) != LDB_SUCCESS) {
602                                 return WERR_INTERNAL_ERROR;
603                         }
604                 }
605         } else {
606                 if (!(instanceType & INSTANCE_TYPE_WRITE)) {
607                         DEBUG(0, ("Refusing to replicate %s from a read-only repilca into a read-write replica!\n",
608                                   ldb_dn_get_linearized(msg->dn)));
609                         return WERR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA;
610                 }
611         }
612
613         whenChanged_t = nt_time_to_unix(whenChanged);
614         whenChanged_s = ldb_timestring(msg, whenChanged_t);
615         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
616
617         out->object_guid = in->object.identifier->guid;
618
619         if (in->parent_object_guid == NULL) {
620                 out->parent_guid = NULL;
621         } else {
622                 out->parent_guid = talloc(mem_ctx, struct GUID);
623                 W_ERROR_HAVE_NO_MEMORY(out->parent_guid);
624                 *out->parent_guid = *in->parent_object_guid;
625         }
626
627         out->msg                = msg;
628         out->when_changed       = whenChanged_s;
629         out->meta_data          = md;
630         return WERR_OK;
631 }
632
633 WERROR dsdb_replicated_objects_convert(struct ldb_context *ldb,
634                                        const struct dsdb_schema *schema,
635                                        struct ldb_dn *partition_dn,
636                                        const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
637                                        uint32_t object_count,
638                                        const struct drsuapi_DsReplicaObjectListItemEx *first_object,
639                                        uint32_t linked_attributes_count,
640                                        const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
641                                        const struct repsFromTo1 *source_dsa,
642                                        const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
643                                        const DATA_BLOB *gensec_skey,
644                                        uint32_t dsdb_repl_flags,
645                                        TALLOC_CTX *mem_ctx,
646                                        struct dsdb_extended_replicated_objects **objects)
647 {
648         WERROR status;
649         struct dsdb_schema_prefixmap *pfm_remote;
650         struct dsdb_extended_replicated_objects *out;
651         const struct drsuapi_DsReplicaObjectListItemEx *cur;
652         struct dsdb_syntax_ctx syntax_ctx;
653         uint32_t i;
654
655         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
656         W_ERROR_HAVE_NO_MEMORY(out);
657         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
658         out->dsdb_repl_flags    = dsdb_repl_flags;
659
660         /*
661          * Ensure schema is kept valid for as long as 'out'
662          * which may contain pointers to it
663          */
664         schema = talloc_reference(out, schema);
665         W_ERROR_HAVE_NO_MEMORY(schema);
666
667         status = dsdb_schema_pfm_from_drsuapi_pfm(mapping_ctr, true,
668                                                   out, &pfm_remote, NULL);
669         if (!W_ERROR_IS_OK(status)) {
670                 DEBUG(0,(__location__ ": Failed to decode remote prefixMap: %s\n",
671                          win_errstr(status)));
672                 talloc_free(out);
673                 return status;
674         }
675
676         /* use default syntax conversion context */
677         dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
678         syntax_ctx.pfm_remote = pfm_remote;
679
680         if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
681                 /*
682                  * check for schema changes in case
683                  * we are not replicating Schema NC
684                  */
685                 status = dsdb_schema_info_cmp(schema, mapping_ctr);
686                 if (!W_ERROR_IS_OK(status)) {
687                         DEBUG(4,("Can't replicate %s because remote schema has changed since we last replicated the schema\n",
688                                  ldb_dn_get_linearized(partition_dn)));
689                         talloc_free(out);
690                         return status;
691                 }
692         }
693
694         out->partition_dn       = partition_dn;
695
696         out->source_dsa         = source_dsa;
697         out->uptodateness_vector= uptodateness_vector;
698
699         out->num_objects        = 0;
700         out->objects            = talloc_array(out,
701                                                struct dsdb_extended_replicated_object,
702                                                object_count);
703         W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
704
705         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
706                 if (i == object_count) {
707                         talloc_free(out);
708                         return WERR_FOOBAR;
709                 }
710
711                 status = dsdb_convert_object_ex(ldb, schema, out->partition_dn,
712                                                 pfm_remote,
713                                                 cur, gensec_skey,
714                                                 NULL,
715                                                 dsdb_repl_flags,
716                                                 out->objects,
717                                                 &out->objects[out->num_objects]);
718
719                 /*
720                  * Check to see if we have been advised of a
721                  * subdomain or new application partition.  We don't
722                  * want to start on that here, instead the caller
723                  * should consider if it would like to replicate it
724                  * based on the cross-ref object.
725                  */
726                 if (W_ERROR_EQUAL(status, WERR_DS_ADD_REPLICA_INHIBITED)) {
727                         continue;
728                 }
729
730                 if (!W_ERROR_IS_OK(status)) {
731                         talloc_free(out);
732                         DEBUG(0,("Failed to convert object %s: %s\n",
733                                  cur->object.identifier->dn,
734                                  win_errstr(status)));
735                         return status;
736                 }
737
738                 /* Assuming we didn't skip or error, increment the number of objects */
739                 out->num_objects++;
740         }
741         out->objects = talloc_realloc(out, out->objects,
742                                       struct dsdb_extended_replicated_object,
743                                       out->num_objects);
744         if (out->num_objects != 0 && out->objects == NULL) {
745                 talloc_free(out);
746                 return WERR_FOOBAR;
747         }
748         if (i != object_count) {
749                 talloc_free(out);
750                 return WERR_FOOBAR;
751         }
752
753         out->linked_attributes = talloc_array(out,
754                                               struct drsuapi_DsReplicaLinkedAttribute,
755                                               linked_attributes_count);
756         W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->linked_attributes, out);
757
758         for (i=0; i < linked_attributes_count; i++) {
759                 const struct drsuapi_DsReplicaLinkedAttribute *ra = &linked_attributes[i];
760                 struct drsuapi_DsReplicaLinkedAttribute *la = &out->linked_attributes[i];
761
762                 if (ra->identifier == NULL) {
763                         talloc_free(out);
764                         return WERR_BAD_NET_RESP;
765                 }
766
767                 *la = *ra;
768
769                 la->identifier = talloc_zero(out->linked_attributes,
770                                              struct drsuapi_DsReplicaObjectIdentifier);
771                 W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->identifier, out);
772
773                 /*
774                  * We typically only get the guid filled
775                  * and the repl_meta_data module only cares abouf
776                  * the guid.
777                  */
778                 la->identifier->guid = ra->identifier->guid;
779
780                 if (ra->value.blob != NULL) {
781                         la->value.blob = talloc_zero(out->linked_attributes,
782                                                      DATA_BLOB);
783                         W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->value.blob, out);
784
785                         if (ra->value.blob->length != 0) {
786                                 *la->value.blob = data_blob_dup_talloc(la->value.blob,
787                                                                        *ra->value.blob);
788                                 W_ERROR_HAVE_NO_MEMORY_AND_FREE(la->value.blob->data, out);
789                         }
790                 }
791
792                 status = dsdb_attribute_drsuapi_remote_to_local(&syntax_ctx,
793                                                                 ra->attid,
794                                                                 &la->attid,
795                                                                 NULL);
796                 if (!W_ERROR_IS_OK(status)) {
797                         DEBUG(0,(__location__": linked_attribute[%u] attid 0x%08X not found: %s\n",
798                                  i, ra->attid, win_errstr(status)));
799                         return status;
800                 }
801         }
802
803         out->linked_attributes_count = linked_attributes_count;
804
805         /* free pfm_remote, we won't need it anymore */
806         talloc_free(pfm_remote);
807
808         *objects = out;
809         return WERR_OK;
810 }
811
812 /**
813  * Commits a list of replicated objects.
814  *
815  * @param working_schema dsdb_schema to be used for resolving
816  *                       Classes/Attributes during Schema replication. If not NULL,
817  *                       it will be set on ldb and used while committing replicated objects
818  */
819 WERROR dsdb_replicated_objects_commit(struct ldb_context *ldb,
820                                       struct dsdb_schema *working_schema,
821                                       struct dsdb_extended_replicated_objects *objects,
822                                       uint64_t *notify_uSN)
823 {
824         WERROR werr;
825         struct ldb_result *ext_res;
826         struct dsdb_schema *cur_schema = NULL;
827         struct dsdb_schema *new_schema = NULL;
828         int ret;
829         uint64_t seq_num1, seq_num2;
830         bool used_global_schema = false;
831
832         TALLOC_CTX *tmp_ctx = talloc_new(objects);
833         if (!tmp_ctx) {
834                 DEBUG(0,("Failed to start talloc\n"));
835                 return WERR_NOT_ENOUGH_MEMORY;
836         }
837
838         /* wrap the extended operation in a transaction 
839            See [MS-DRSR] 3.3.2 Transactions
840          */
841         ret = ldb_transaction_start(ldb);
842         if (ret != LDB_SUCCESS) {
843                 DEBUG(0,(__location__ " Failed to start transaction: %s\n",
844                          ldb_errstring(ldb)));
845                 return WERR_FOOBAR;
846         }
847
848         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
849         if (ret != LDB_SUCCESS) {
850                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
851                 ldb_transaction_cancel(ldb);
852                 TALLOC_FREE(tmp_ctx);
853                 return WERR_FOOBAR;             
854         }
855
856         /*
857          * Set working_schema for ldb in case we are replicating from Schema NC.
858          * Schema won't be reloaded during Replicated Objects commit, as it is
859          * done in a transaction. So we need some way to search for newly
860          * added Classes and Attributes
861          */
862         if (working_schema) {
863                 /* store current schema so we can fall back in case of failure */
864                 cur_schema = dsdb_get_schema(ldb, tmp_ctx);
865                 used_global_schema = dsdb_uses_global_schema(ldb);
866
867                 ret = dsdb_reference_schema(ldb, working_schema, false);
868                 if (ret != LDB_SUCCESS) {
869                         DEBUG(0,(__location__ "Failed to reference working schema - %s\n",
870                                  ldb_strerror(ret)));
871                         /* TODO: Map LDB Error to NTSTATUS? */
872                         ldb_transaction_cancel(ldb);
873                         TALLOC_FREE(tmp_ctx);
874                         return WERR_INTERNAL_ERROR;
875                 }
876         }
877
878         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
879         if (ret != LDB_SUCCESS) {
880                 /* restore previous schema */
881                 if (used_global_schema) { 
882                         dsdb_set_global_schema(ldb);
883                 } else if (cur_schema) {
884                         dsdb_reference_schema(ldb, cur_schema, false);
885                 }
886
887                 if (!W_ERROR_EQUAL(objects->error, WERR_DS_DRA_MISSING_PARENT)) {
888                         DEBUG(1,("Failed to apply records: %s: %s\n",
889                                  ldb_errstring(ldb), ldb_strerror(ret)));
890                 } else {
891                         DEBUG(3,("Missing parent while attempting to apply records: %s\n",
892                                  ldb_errstring(ldb)));
893                 }
894                 ldb_transaction_cancel(ldb);
895                 TALLOC_FREE(tmp_ctx);
896
897                 if (!W_ERROR_IS_OK(objects->error)) {
898                         return objects->error;
899                 }
900                 return WERR_FOOBAR;
901         }
902         talloc_free(ext_res);
903
904         /* Save our updated prefixMap */
905         if (working_schema) {
906                 werr = dsdb_write_prefixes_from_schema_to_ldb(working_schema,
907                                                               ldb,
908                                                               working_schema);
909                 if (!W_ERROR_IS_OK(werr)) {
910                         /* restore previous schema */
911                         if (used_global_schema) { 
912                                 dsdb_set_global_schema(ldb);
913                         } else if (cur_schema ) {
914                                 dsdb_reference_schema(ldb, cur_schema, false);
915                         }
916                         DEBUG(0,("Failed to save updated prefixMap: %s\n",
917                                  win_errstr(werr)));
918                         TALLOC_FREE(tmp_ctx);
919                         return werr;
920                 }
921         }
922
923         ret = ldb_transaction_prepare_commit(ldb);
924         if (ret != LDB_SUCCESS) {
925                 /* restore previous schema */
926                 if (used_global_schema) { 
927                         dsdb_set_global_schema(ldb);
928                 } else if (cur_schema ) {
929                         dsdb_reference_schema(ldb, cur_schema, false);
930                 }
931                 DEBUG(0,(__location__ " Failed to prepare commit of transaction: %s\n",
932                          ldb_errstring(ldb)));
933                 TALLOC_FREE(tmp_ctx);
934                 return WERR_FOOBAR;
935         }
936
937         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
938         if (ret != LDB_SUCCESS) {
939                 /* restore previous schema */
940                 if (used_global_schema) { 
941                         dsdb_set_global_schema(ldb);
942                 } else if (cur_schema ) {
943                         dsdb_reference_schema(ldb, cur_schema, false);
944                 }
945                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
946                 ldb_transaction_cancel(ldb);
947                 TALLOC_FREE(tmp_ctx);
948                 return WERR_FOOBAR;             
949         }
950
951         ret = ldb_transaction_commit(ldb);
952         if (ret != LDB_SUCCESS) {
953                 /* restore previous schema */
954                 if (used_global_schema) { 
955                         dsdb_set_global_schema(ldb);
956                 } else if (cur_schema ) {
957                         dsdb_reference_schema(ldb, cur_schema, false);
958                 }
959                 DEBUG(0,(__location__ " Failed to commit transaction\n"));
960                 TALLOC_FREE(tmp_ctx);
961                 return WERR_FOOBAR;
962         }
963
964         if (seq_num1 > *notify_uSN) {
965                 /*
966                  * A notify was already required before
967                  * the current transaction.
968                  */
969         } else if (objects->originating_updates) {
970                 /*
971                  * Applying the replicated changes
972                  * required originating updates,
973                  * so a notify is required.
974                  */
975         } else {
976                 /*
977                  * There's no need to notify the
978                  * server about the change we just from it.
979                  */
980                 *notify_uSN = seq_num2;
981         }
982
983         /*
984          * Reset the Schema used by ldb. This will lead to
985          * a schema cache being refreshed from database.
986          */
987         if (working_schema) {
988                 /* Reload the schema */
989                 new_schema = dsdb_get_schema(ldb, tmp_ctx);
990                 /* TODO:
991                  * If dsdb_get_schema() fails, we just fall back
992                  * to what we had.  However, the database is probably
993                  * unable to operate for other users from this
994                  * point... */
995                 if (new_schema == NULL || new_schema == working_schema) {
996                         DBG_ERR("Failed to re-load schema after commit of "
997                                 "transaction (working: %p/%"PRIu64", new: "
998                                 "%p/%"PRIu64")\n", new_schema,
999                                 new_schema != NULL ?
1000                                 new_schema->metadata_usn : 0,
1001                                 working_schema, working_schema->metadata_usn);
1002                         dsdb_reference_schema(ldb, cur_schema, false);
1003                         if (used_global_schema) {
1004                                 dsdb_set_global_schema(ldb);
1005                         }
1006                         TALLOC_FREE(tmp_ctx);
1007                         return WERR_INTERNAL_ERROR;
1008                 } else if (used_global_schema) {
1009                         dsdb_make_schema_global(ldb, new_schema);
1010                 }
1011         }
1012
1013         DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
1014                  objects->num_objects, objects->linked_attributes_count,
1015                  ldb_dn_get_linearized(objects->partition_dn)));
1016                  
1017         TALLOC_FREE(tmp_ctx);
1018         return WERR_OK;
1019 }
1020
1021 static WERROR dsdb_origin_object_convert(struct ldb_context *ldb,
1022                                          const struct dsdb_schema *schema,
1023                                          const struct drsuapi_DsReplicaObjectListItem *in,
1024                                          TALLOC_CTX *mem_ctx,
1025                                          struct ldb_message **_msg)
1026 {
1027         WERROR status;
1028         unsigned int i;
1029         struct ldb_message *msg;
1030
1031         if (!in->object.identifier) {
1032                 return WERR_FOOBAR;
1033         }
1034
1035         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
1036                 return WERR_FOOBAR;
1037         }
1038
1039         msg = ldb_msg_new(mem_ctx);
1040         W_ERROR_HAVE_NO_MEMORY(msg);
1041
1042         msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
1043         W_ERROR_HAVE_NO_MEMORY(msg->dn);
1044
1045         msg->num_elements       = in->object.attribute_ctr.num_attributes;
1046         msg->elements           = talloc_array(msg, struct ldb_message_element,
1047                                                msg->num_elements);
1048         W_ERROR_HAVE_NO_MEMORY(msg->elements);
1049
1050         for (i=0; i < msg->num_elements; i++) {
1051                 struct drsuapi_DsReplicaAttribute *a;
1052                 struct ldb_message_element *e;
1053
1054                 a = &in->object.attribute_ctr.attributes[i];
1055                 e = &msg->elements[i];
1056
1057                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, schema->prefixmap,
1058                                                        a, msg->elements, e, NULL);
1059                 W_ERROR_NOT_OK_RETURN(status);
1060         }
1061
1062
1063         *_msg = msg;
1064
1065         return WERR_OK;
1066 }
1067
1068 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
1069                                   TALLOC_CTX *mem_ctx,
1070                                   const struct drsuapi_DsReplicaObjectListItem *first_object,
1071                                   uint32_t *_num,
1072                                   uint32_t dsdb_repl_flags,
1073                                   struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
1074 {
1075         WERROR status;
1076         const struct dsdb_schema *schema;
1077         const struct drsuapi_DsReplicaObjectListItem *cur;
1078         struct ldb_message **objects;
1079         struct drsuapi_DsReplicaObjectIdentifier2 *ids;
1080         uint32_t i;
1081         uint32_t num_objects = 0;
1082         const char * const attrs[] = {
1083                 "objectGUID",
1084                 "objectSid",
1085                 NULL
1086         };
1087         struct ldb_result *res;
1088         int ret;
1089
1090         for (cur = first_object; cur; cur = cur->next_object) {
1091                 num_objects++;
1092         }
1093
1094         if (num_objects == 0) {
1095                 return WERR_OK;
1096         }
1097
1098         ret = ldb_transaction_start(ldb);
1099         if (ret != LDB_SUCCESS) {
1100                 return WERR_DS_INTERNAL_FAILURE;
1101         }
1102
1103         objects = talloc_array(mem_ctx, struct ldb_message *,
1104                                num_objects);
1105         if (objects == NULL) {
1106                 status = WERR_NOT_ENOUGH_MEMORY;
1107                 goto cancel;
1108         }
1109
1110         schema = dsdb_get_schema(ldb, objects);
1111         if (!schema) {
1112                 return WERR_DS_SCHEMA_NOT_LOADED;
1113         }
1114
1115         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
1116                 status = dsdb_origin_object_convert(ldb, schema, cur,
1117                                                     objects, &objects[i]);
1118                 if (!W_ERROR_IS_OK(status)) {
1119                         goto cancel;
1120                 }
1121         }
1122
1123         ids = talloc_array(mem_ctx,
1124                            struct drsuapi_DsReplicaObjectIdentifier2,
1125                            num_objects);
1126         if (ids == NULL) {
1127                 status = WERR_NOT_ENOUGH_MEMORY;
1128                 goto cancel;
1129         }
1130
1131         if (dsdb_repl_flags & DSDB_REPL_FLAG_ADD_NCNAME) {
1132                 /* check for possible NC creation */
1133                 for (i=0; i < num_objects; i++) {
1134                         struct ldb_message *msg = objects[i];
1135                         struct ldb_message_element *el;
1136                         struct ldb_dn *nc_dn;
1137
1138                         if (ldb_msg_check_string_attribute(msg, "objectClass", "crossRef") == 0) {
1139                                 continue;
1140                         }
1141                         el = ldb_msg_find_element(msg, "nCName");
1142                         if (el == NULL || el->num_values != 1) {
1143                                 continue;
1144                         }
1145                         nc_dn = ldb_dn_from_ldb_val(objects, ldb, &el->values[0]);
1146                         if (!ldb_dn_validate(nc_dn)) {
1147                                 continue;
1148                         }
1149                         ret = dsdb_create_partial_replica_NC(ldb, nc_dn);
1150                         if (ret != LDB_SUCCESS) {
1151                                 status = WERR_DS_INTERNAL_FAILURE;
1152                                 goto cancel;
1153                         }
1154                 }
1155         }
1156
1157         for (i=0; i < num_objects; i++) {
1158                 struct dom_sid *sid = NULL;
1159                 struct ldb_request *add_req;
1160
1161                 DEBUG(6,(__location__ ": adding %s\n", 
1162                          ldb_dn_get_linearized(objects[i]->dn)));
1163
1164                 ret = ldb_build_add_req(&add_req,
1165                                         ldb,
1166                                         objects,
1167                                         objects[i],
1168                                         NULL,
1169                                         NULL,
1170                                         ldb_op_default_callback,
1171                                         NULL);
1172                 if (ret != LDB_SUCCESS) {
1173                         status = WERR_DS_INTERNAL_FAILURE;
1174                         goto cancel;
1175                 }
1176
1177                 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
1178                 if (ret != LDB_SUCCESS) {
1179                         status = WERR_DS_INTERNAL_FAILURE;
1180                         goto cancel;
1181                 }
1182                 
1183                 ret = ldb_request(ldb, add_req);
1184                 if (ret == LDB_SUCCESS) {
1185                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
1186                 }
1187                 if (ret != LDB_SUCCESS) {
1188                         DEBUG(0,(__location__ ": Failed add of %s - %s\n",
1189                                  ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
1190                         status = WERR_DS_INTERNAL_FAILURE;
1191                         goto cancel;
1192                 }
1193
1194                 talloc_free(add_req);
1195
1196                 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
1197                                  LDB_SCOPE_BASE, attrs,
1198                                  "(objectClass=*)");
1199                 if (ret != LDB_SUCCESS) {
1200                         status = WERR_DS_INTERNAL_FAILURE;
1201                         goto cancel;
1202                 }
1203                 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
1204                 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
1205                 if (sid) {
1206                         ids[i].sid = *sid;
1207                 } else {
1208                         ZERO_STRUCT(ids[i].sid);
1209                 }
1210         }
1211
1212         ret = ldb_transaction_commit(ldb);
1213         if (ret != LDB_SUCCESS) {
1214                 return WERR_DS_INTERNAL_FAILURE;
1215         }
1216
1217         talloc_free(objects);
1218
1219         *_num = num_objects;
1220         *_ids = ids;
1221         return WERR_OK;
1222
1223 cancel:
1224         talloc_free(objects);
1225         ldb_transaction_cancel(ldb);
1226         return status;
1227 }