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