added basic support for rename in DRS replication
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / repl_meta_data.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004-2008
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
8
9      ** NOTE! The following LGPL license applies to the ldb
10      ** library. This does NOT imply that all of Samba is released
11      ** under the LGPL
12    
13    This library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 3 of the License, or (at your option) any later version.
17
18    This library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22
23    You should have received a copy of the GNU Lesser General Public
24    License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 */
26
27 /*
28  *  Name: ldb
29  *
30  *  Component: ldb repl_meta_data module
31  *
32  *  Description: - add a unique objectGUID onto every new record,
33  *               - handle whenCreated, whenChanged timestamps
34  *               - handle uSNCreated, uSNChanged numbers
35  *               - handle replPropertyMetaData attribute
36  *
37  *  Author: Simo Sorce
38  *  Author: Stefan Metzmacher
39  */
40
41 #include "includes.h"
42 #include "ldb_module.h"
43 #include "dsdb/samdb/samdb.h"
44 #include "../libds/common/flags.h"
45 #include "librpc/gen_ndr/ndr_misc.h"
46 #include "librpc/gen_ndr/ndr_drsuapi.h"
47 #include "librpc/gen_ndr/ndr_drsblobs.h"
48 #include "param/param.h"
49
50 struct replmd_replicated_request {
51         struct ldb_module *module;
52         struct ldb_request *req;
53
54         const struct dsdb_schema *schema;
55
56         struct dsdb_extended_replicated_objects *objs;
57
58         /* the controls we pass down */
59         struct ldb_control **controls;
60
61         uint32_t index_current;
62
63         struct ldb_message *search_msg;
64 };
65
66 static struct replmd_replicated_request *replmd_ctx_init(struct ldb_module *module,
67                                           struct ldb_request *req)
68 {
69         struct ldb_context *ldb;
70         struct replmd_replicated_request *ac;
71
72         ldb = ldb_module_get_ctx(module);
73
74         ac = talloc_zero(req, struct replmd_replicated_request);
75         if (ac == NULL) {
76                 ldb_oom(ldb);
77                 return NULL;
78         }
79
80         ac->module = module;
81         ac->req = req;
82         return ac;
83 }
84
85 /*
86   add a time element to a record
87 */
88 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
89 {
90         struct ldb_message_element *el;
91         char *s;
92
93         if (ldb_msg_find_element(msg, attr) != NULL) {
94                 return LDB_SUCCESS;
95         }
96
97         s = ldb_timestring(msg, t);
98         if (s == NULL) {
99                 return LDB_ERR_OPERATIONS_ERROR;
100         }
101
102         if (ldb_msg_add_string(msg, attr, s) != LDB_SUCCESS) {
103                 return LDB_ERR_OPERATIONS_ERROR;
104         }
105
106         el = ldb_msg_find_element(msg, attr);
107         /* always set as replace. This works because on add ops, the flag
108            is ignored */
109         el->flags = LDB_FLAG_MOD_REPLACE;
110
111         return LDB_SUCCESS;
112 }
113
114 /*
115   add a uint64_t element to a record
116 */
117 static int add_uint64_element(struct ldb_message *msg, const char *attr, uint64_t v)
118 {
119         struct ldb_message_element *el;
120
121         if (ldb_msg_find_element(msg, attr) != NULL) {
122                 return LDB_SUCCESS;
123         }
124
125         if (ldb_msg_add_fmt(msg, attr, "%llu", (unsigned long long)v) != LDB_SUCCESS) {
126                 return LDB_ERR_OPERATIONS_ERROR;
127         }
128
129         el = ldb_msg_find_element(msg, attr);
130         /* always set as replace. This works because on add ops, the flag
131            is ignored */
132         el->flags = LDB_FLAG_MOD_REPLACE;
133
134         return LDB_SUCCESS;
135 }
136
137 static int replmd_replPropertyMetaData1_attid_sort(const struct replPropertyMetaData1 *m1,
138                                                    const struct replPropertyMetaData1 *m2,
139                                                    const uint32_t *rdn_attid)
140 {
141         if (m1->attid == m2->attid) {
142                 return 0;
143         }
144
145         /*
146          * the rdn attribute should be at the end!
147          * so we need to return a value greater than zero
148          * which means m1 is greater than m2
149          */
150         if (m1->attid == *rdn_attid) {
151                 return 1;
152         }
153
154         /*
155          * the rdn attribute should be at the end!
156          * so we need to return a value less than zero
157          * which means m2 is greater than m1
158          */
159         if (m2->attid == *rdn_attid) {
160                 return -1;
161         }
162
163         return m1->attid - m2->attid;
164 }
165
166 static void replmd_replPropertyMetaDataCtr1_sort(struct replPropertyMetaDataCtr1 *ctr1,
167                                                  const uint32_t *rdn_attid)
168 {
169         ldb_qsort(ctr1->array, ctr1->count, sizeof(struct replPropertyMetaData1),
170                   discard_const_p(void, rdn_attid), (ldb_qsort_cmp_fn_t)replmd_replPropertyMetaData1_attid_sort);
171 }
172
173 static int replmd_ldb_message_element_attid_sort(const struct ldb_message_element *e1,
174                                                  const struct ldb_message_element *e2,
175                                                  const struct dsdb_schema *schema)
176 {
177         const struct dsdb_attribute *a1;
178         const struct dsdb_attribute *a2;
179
180         /* 
181          * TODO: make this faster by caching the dsdb_attribute pointer
182          *       on the ldb_messag_element
183          */
184
185         a1 = dsdb_attribute_by_lDAPDisplayName(schema, e1->name);
186         a2 = dsdb_attribute_by_lDAPDisplayName(schema, e2->name);
187
188         /*
189          * TODO: remove this check, we should rely on e1 and e2 having valid attribute names
190          *       in the schema
191          */
192         if (!a1 || !a2) {
193                 return strcasecmp(e1->name, e2->name);
194         }
195
196         return a1->attributeID_id - a2->attributeID_id;
197 }
198
199 static void replmd_ldb_message_sort(struct ldb_message *msg,
200                                     const struct dsdb_schema *schema)
201 {
202         ldb_qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element),
203                   discard_const_p(void, schema), (ldb_qsort_cmp_fn_t)replmd_ldb_message_element_attid_sort);
204 }
205
206 static int replmd_op_callback(struct ldb_request *req, struct ldb_reply *ares)
207 {
208         struct ldb_context *ldb;
209         struct replmd_replicated_request *ac;
210
211         ac = talloc_get_type(req->context, struct replmd_replicated_request);
212         ldb = ldb_module_get_ctx(ac->module);
213
214         if (!ares) {
215                 return ldb_module_done(ac->req, NULL, NULL,
216                                         LDB_ERR_OPERATIONS_ERROR);
217         }
218         if (ares->error != LDB_SUCCESS) {
219                 return ldb_module_done(ac->req, ares->controls,
220                                         ares->response, ares->error);
221         }
222
223         if (ares->type != LDB_REPLY_DONE) {
224                 ldb_set_errstring(ldb,
225                                   "invalid ldb_reply_type in callback");
226                 talloc_free(ares);
227                 return ldb_module_done(ac->req, NULL, NULL,
228                                         LDB_ERR_OPERATIONS_ERROR);
229         }
230
231         return ldb_module_done(ac->req, ares->controls,
232                                 ares->response, LDB_SUCCESS);
233 }
234
235 static int replmd_add(struct ldb_module *module, struct ldb_request *req)
236 {
237         struct ldb_context *ldb;
238         struct replmd_replicated_request *ac;
239         const struct dsdb_schema *schema;
240         enum ndr_err_code ndr_err;
241         struct ldb_request *down_req;
242         struct ldb_message *msg;
243         const struct dsdb_attribute *rdn_attr = NULL;
244         struct GUID guid;
245         struct ldb_val guid_value;
246         struct replPropertyMetaDataBlob nmd;
247         struct ldb_val nmd_value;
248         uint64_t seq_num;
249         const struct GUID *our_invocation_id;
250         time_t t = time(NULL);
251         NTTIME now;
252         char *time_str;
253         int ret;
254         uint32_t i, ni=0;
255
256         /* do not manipulate our control entries */
257         if (ldb_dn_is_special(req->op.add.message->dn)) {
258                 return ldb_next_request(module, req);
259         }
260
261         ldb = ldb_module_get_ctx(module);
262
263         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_add\n");
264
265         schema = dsdb_get_schema(ldb);
266         if (!schema) {
267                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
268                               "replmd_modify: no dsdb_schema loaded");
269                 return LDB_ERR_CONSTRAINT_VIOLATION;
270         }
271
272         ac = replmd_ctx_init(module, req);
273         if (!ac) {
274                 return LDB_ERR_OPERATIONS_ERROR;
275         }
276
277         ac->schema = schema;
278
279         if (ldb_msg_find_element(req->op.add.message, "objectGUID") != NULL) {
280                 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
281                               "replmd_add: it's not allowed to add an object with objectGUID\n");
282                 return LDB_ERR_UNWILLING_TO_PERFORM;
283         }
284
285         /* Get a sequence number from the backend */
286         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
287         if (ret != LDB_SUCCESS) {
288                 return ret;
289         }
290
291         /* a new GUID */
292         guid = GUID_random();
293
294         /* get our invicationId */
295         our_invocation_id = samdb_ntds_invocation_id(ldb);
296         if (!our_invocation_id) {
297                 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
298                               "replmd_add: unable to find invocationId\n");
299                 return LDB_ERR_OPERATIONS_ERROR;
300         }
301
302         /* we have to copy the message as the caller might have it as a const */
303         msg = ldb_msg_copy_shallow(ac, req->op.add.message);
304         if (msg == NULL) {
305                 ldb_oom(ldb);
306                 return LDB_ERR_OPERATIONS_ERROR;
307         }
308
309         /* generated times */
310         unix_to_nt_time(&now, t);
311         time_str = ldb_timestring(msg, t);
312         if (!time_str) {
313                 return LDB_ERR_OPERATIONS_ERROR;
314         }
315
316         /* 
317          * remove autogenerated attributes
318          */
319         ldb_msg_remove_attr(msg, "whenCreated");
320         ldb_msg_remove_attr(msg, "whenChanged");
321         ldb_msg_remove_attr(msg, "uSNCreated");
322         ldb_msg_remove_attr(msg, "uSNChanged");
323         ldb_msg_remove_attr(msg, "replPropertyMetaData");
324
325         /*
326          * readd replicated attributes
327          */
328         ret = ldb_msg_add_string(msg, "whenCreated", time_str);
329         if (ret != LDB_SUCCESS) {
330                 ldb_oom(ldb);
331                 return LDB_ERR_OPERATIONS_ERROR;
332         }
333
334         /* build the replication meta_data */
335         ZERO_STRUCT(nmd);
336         nmd.version             = 1;
337         nmd.ctr.ctr1.count      = msg->num_elements;
338         nmd.ctr.ctr1.array      = talloc_array(msg,
339                                                struct replPropertyMetaData1,
340                                                nmd.ctr.ctr1.count);
341         if (!nmd.ctr.ctr1.array) {
342                 ldb_oom(ldb);
343                 return LDB_ERR_OPERATIONS_ERROR;
344         }
345
346         for (i=0; i < msg->num_elements; i++) {
347                 struct ldb_message_element *e = &msg->elements[i];
348                 struct replPropertyMetaData1 *m = &nmd.ctr.ctr1.array[ni];
349                 const struct dsdb_attribute *sa;
350
351                 if (e->name[0] == '@') continue;
352
353                 sa = dsdb_attribute_by_lDAPDisplayName(schema, e->name);
354                 if (!sa) {
355                         ldb_debug_set(ldb, LDB_DEBUG_ERROR,
356                                       "replmd_add: attribute '%s' not defined in schema\n",
357                                       e->name);
358                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
359                 }
360
361                 if ((sa->systemFlags & 0x00000001) || (sa->systemFlags & 0x00000004)) {
362                         /* if the attribute is not replicated (0x00000001)
363                          * or constructed (0x00000004) it has no metadata
364                          */
365                         continue;
366                 }
367
368                 m->attid                        = sa->attributeID_id;
369                 m->version                      = 1;
370                 m->originating_change_time      = now;
371                 m->originating_invocation_id    = *our_invocation_id;
372                 m->originating_usn              = seq_num;
373                 m->local_usn                    = seq_num;
374                 ni++;
375
376                 if (ldb_attr_cmp(e->name, ldb_dn_get_rdn_name(msg->dn))) {
377                         rdn_attr = sa;
378                 }
379         }
380
381         /* fix meta data count */
382         nmd.ctr.ctr1.count = ni;
383
384         /*
385          * sort meta data array, and move the rdn attribute entry to the end
386          */
387         replmd_replPropertyMetaDataCtr1_sort(&nmd.ctr.ctr1, &rdn_attr->attributeID_id);
388
389         /* generated NDR encoded values */
390         ndr_err = ndr_push_struct_blob(&guid_value, msg, 
391                                        NULL,
392                                        &guid,
393                                        (ndr_push_flags_fn_t)ndr_push_GUID);
394         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
395                 ldb_oom(ldb);
396                 return LDB_ERR_OPERATIONS_ERROR;
397         }
398         ndr_err = ndr_push_struct_blob(&nmd_value, msg, 
399                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
400                                        &nmd,
401                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
402         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
403                 ldb_oom(ldb);
404                 return LDB_ERR_OPERATIONS_ERROR;
405         }
406
407         /*
408          * add the autogenerated values
409          */
410         ret = ldb_msg_add_value(msg, "objectGUID", &guid_value, NULL);
411         if (ret != LDB_SUCCESS) {
412                 ldb_oom(ldb);
413                 return LDB_ERR_OPERATIONS_ERROR;
414         }
415         ret = ldb_msg_add_string(msg, "whenChanged", time_str);
416         if (ret != LDB_SUCCESS) {
417                 ldb_oom(ldb);
418                 return LDB_ERR_OPERATIONS_ERROR;
419         }
420         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNCreated", seq_num);
421         if (ret != LDB_SUCCESS) {
422                 ldb_oom(ldb);
423                 return LDB_ERR_OPERATIONS_ERROR;
424         }
425         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", seq_num);
426         if (ret != LDB_SUCCESS) {
427                 ldb_oom(ldb);
428                 return LDB_ERR_OPERATIONS_ERROR;
429         }
430         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &nmd_value, NULL);
431         if (ret != LDB_SUCCESS) {
432                 ldb_oom(ldb);
433                 return LDB_ERR_OPERATIONS_ERROR;
434         }
435
436         /*
437          * sort the attributes by attid before storing the object
438          */
439         replmd_ldb_message_sort(msg, schema);
440
441         ret = ldb_build_add_req(&down_req, ldb, ac,
442                                 msg,
443                                 req->controls,
444                                 ac, replmd_op_callback,
445                                 req);
446         if (ret != LDB_SUCCESS) {
447                 return ret;
448         }
449
450         /* go on with the call chain */
451         return ldb_next_request(module, down_req);
452 }
453
454 static int replmd_modify(struct ldb_module *module, struct ldb_request *req)
455 {
456         struct ldb_context *ldb;
457         struct replmd_replicated_request *ac;
458         const struct dsdb_schema *schema;
459         struct ldb_request *down_req;
460         struct ldb_message *msg;
461         int ret;
462         time_t t = time(NULL);
463         uint64_t seq_num;
464
465         /* do not manipulate our control entries */
466         if (ldb_dn_is_special(req->op.mod.message->dn)) {
467                 return ldb_next_request(module, req);
468         }
469
470         ldb = ldb_module_get_ctx(module);
471
472         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_modify\n");
473
474         schema = dsdb_get_schema(ldb);
475         if (!schema) {
476                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
477                               "replmd_modify: no dsdb_schema loaded");
478                 return LDB_ERR_CONSTRAINT_VIOLATION;
479         }
480
481         ac = replmd_ctx_init(module, req);
482         if (!ac) {
483                 return LDB_ERR_OPERATIONS_ERROR;
484         }
485
486         ac->schema = schema;
487
488         /* we have to copy the message as the caller might have it as a const */
489         msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
490         if (msg == NULL) {
491                 talloc_free(ac);
492                 return LDB_ERR_OPERATIONS_ERROR;
493         }
494
495         /* TODO:
496          * - get the whole old object
497          * - if the old object doesn't exist report an error
498          * - give an error when a readonly attribute should
499          *   be modified
500          * - merge the changed into the old object
501          *   if the caller set values to the same value
502          *   ignore the attribute, return success when no
503          *   attribute was changed
504          * - calculate the new replPropertyMetaData attribute
505          */
506
507         if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
508                 talloc_free(ac);
509                 return LDB_ERR_OPERATIONS_ERROR;
510         }
511
512         /* Get a sequence number from the backend */
513         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
514         if (ret == LDB_SUCCESS) {
515                 if (add_uint64_element(msg, "uSNChanged", seq_num) != LDB_SUCCESS) {
516                         talloc_free(ac);
517                         return LDB_ERR_OPERATIONS_ERROR;
518                 }
519         }
520
521         /* TODO:
522          * - sort the attributes by attid with replmd_ldb_message_sort()
523          * - replace the old object with the newly constructed one
524          */
525
526         ret = ldb_build_mod_req(&down_req, ldb, ac,
527                                 msg,
528                                 req->controls,
529                                 ac, replmd_op_callback,
530                                 req);
531         if (ret != LDB_SUCCESS) {
532                 return ret;
533         }
534         talloc_steal(down_req, msg);
535
536         /* go on with the call chain */
537         return ldb_next_request(module, down_req);
538 }
539
540 static int replmd_replicated_request_error(struct replmd_replicated_request *ar, int ret)
541 {
542         return ret;
543 }
544
545 static int replmd_replicated_request_werror(struct replmd_replicated_request *ar, WERROR status)
546 {
547         int ret = LDB_ERR_OTHER;
548         /* TODO: do some error mapping */
549         return ret;
550 }
551
552 static int replmd_replicated_apply_next(struct replmd_replicated_request *ar);
553
554 static int replmd_replicated_apply_add_callback(struct ldb_request *req,
555                                                 struct ldb_reply *ares)
556 {
557         struct ldb_context *ldb;
558         struct replmd_replicated_request *ar = talloc_get_type(req->context,
559                                                struct replmd_replicated_request);
560         int ret;
561
562         ldb = ldb_module_get_ctx(ar->module);
563
564         if (!ares) {
565                 return ldb_module_done(ar->req, NULL, NULL,
566                                         LDB_ERR_OPERATIONS_ERROR);
567         }
568         if (ares->error != LDB_SUCCESS) {
569                 return ldb_module_done(ar->req, ares->controls,
570                                         ares->response, ares->error);
571         }
572
573         if (ares->type != LDB_REPLY_DONE) {
574                 ldb_set_errstring(ldb, "Invalid reply type\n!");
575                 return ldb_module_done(ar->req, NULL, NULL,
576                                         LDB_ERR_OPERATIONS_ERROR);
577         }
578
579         talloc_free(ares);
580         ar->index_current++;
581
582         ret = replmd_replicated_apply_next(ar);
583         if (ret != LDB_SUCCESS) {
584                 return ldb_module_done(ar->req, NULL, NULL, ret);
585         }
586
587         return LDB_SUCCESS;
588 }
589
590 static int replmd_replicated_apply_add(struct replmd_replicated_request *ar)
591 {
592         struct ldb_context *ldb;
593         struct ldb_request *change_req;
594         enum ndr_err_code ndr_err;
595         struct ldb_message *msg;
596         struct replPropertyMetaDataBlob *md;
597         struct ldb_val md_value;
598         uint32_t i;
599         uint64_t seq_num;
600         int ret;
601
602         /*
603          * TODO: check if the parent object exist
604          */
605
606         /*
607          * TODO: handle the conflict case where an object with the
608          *       same name exist
609          */
610
611         ldb = ldb_module_get_ctx(ar->module);
612         msg = ar->objs->objects[ar->index_current].msg;
613         md = ar->objs->objects[ar->index_current].meta_data;
614
615         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
616         if (ret != LDB_SUCCESS) {
617                 return replmd_replicated_request_error(ar, ret);
618         }
619
620         ret = ldb_msg_add_value(msg, "objectGUID", &ar->objs->objects[ar->index_current].guid_value, NULL);
621         if (ret != LDB_SUCCESS) {
622                 return replmd_replicated_request_error(ar, ret);
623         }
624
625         ret = ldb_msg_add_string(msg, "whenChanged", ar->objs->objects[ar->index_current].when_changed);
626         if (ret != LDB_SUCCESS) {
627                 return replmd_replicated_request_error(ar, ret);
628         }
629
630         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNCreated", seq_num);
631         if (ret != LDB_SUCCESS) {
632                 return replmd_replicated_request_error(ar, ret);
633         }
634
635         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", seq_num);
636         if (ret != LDB_SUCCESS) {
637                 return replmd_replicated_request_error(ar, ret);
638         }
639
640         /*
641          * the meta data array is already sorted by the caller
642          */
643         for (i=0; i < md->ctr.ctr1.count; i++) {
644                 md->ctr.ctr1.array[i].local_usn = seq_num;
645         }
646         ndr_err = ndr_push_struct_blob(&md_value, msg, 
647                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
648                                        md,
649                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
650         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
651                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
652                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
653         }
654         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &md_value, NULL);
655         if (ret != LDB_SUCCESS) {
656                 return replmd_replicated_request_error(ar, ret);
657         }
658
659         replmd_ldb_message_sort(msg, ar->schema);
660
661         ret = ldb_build_add_req(&change_req,
662                                 ldb,
663                                 ar,
664                                 msg,
665                                 ar->controls,
666                                 ar,
667                                 replmd_replicated_apply_add_callback,
668                                 ar->req);
669         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
670
671         return ldb_next_request(ar->module, change_req);
672 }
673
674 static int replmd_replPropertyMetaData1_conflict_compare(struct replPropertyMetaData1 *m1,
675                                                          struct replPropertyMetaData1 *m2)
676 {
677         int ret;
678
679         if (m1->version != m2->version) {
680                 return m1->version - m2->version;
681         }
682
683         if (m1->originating_change_time != m2->originating_change_time) {
684                 return m1->originating_change_time - m2->originating_change_time;
685         }
686
687         ret = GUID_compare(&m1->originating_invocation_id, &m2->originating_invocation_id);
688         if (ret != 0) {
689                 return ret;
690         }
691
692         return m1->originating_usn - m2->originating_usn;
693 }
694
695 static int replmd_replicated_apply_merge_callback(struct ldb_request *req,
696                                                   struct ldb_reply *ares)
697 {
698         struct ldb_context *ldb;
699         struct replmd_replicated_request *ar = talloc_get_type(req->context,
700                                                struct replmd_replicated_request);
701         int ret;
702
703         ldb = ldb_module_get_ctx(ar->module);
704
705         if (!ares) {
706                 return ldb_module_done(ar->req, NULL, NULL,
707                                         LDB_ERR_OPERATIONS_ERROR);
708         }
709         if (ares->error != LDB_SUCCESS) {
710                 return ldb_module_done(ar->req, ares->controls,
711                                         ares->response, ares->error);
712         }
713
714         if (ares->type != LDB_REPLY_DONE) {
715                 ldb_set_errstring(ldb, "Invalid reply type\n!");
716                 return ldb_module_done(ar->req, NULL, NULL,
717                                         LDB_ERR_OPERATIONS_ERROR);
718         }
719
720         talloc_free(ares);
721         ar->index_current++;
722
723         ret = replmd_replicated_apply_next(ar);
724         if (ret != LDB_SUCCESS) {
725                 return ldb_module_done(ar->req, NULL, NULL, ret);
726         }
727
728         return LDB_SUCCESS;
729 }
730
731 static int replmd_replicated_apply_merge(struct replmd_replicated_request *ar)
732 {
733         struct ldb_context *ldb;
734         struct ldb_request *change_req;
735         enum ndr_err_code ndr_err;
736         struct ldb_message *msg;
737         struct replPropertyMetaDataBlob *rmd;
738         struct replPropertyMetaDataBlob omd;
739         const struct ldb_val *omd_value;
740         struct replPropertyMetaDataBlob nmd;
741         struct ldb_val nmd_value;
742         uint32_t i,j,ni=0;
743         uint32_t removed_attrs = 0;
744         uint64_t seq_num;
745         int ret;
746
747         ldb = ldb_module_get_ctx(ar->module);
748         msg = ar->objs->objects[ar->index_current].msg;
749         rmd = ar->objs->objects[ar->index_current].meta_data;
750         ZERO_STRUCT(omd);
751         omd.version = 1;
752
753         /*
754          * TODO: check repl data is correct after a rename
755          */
756         if (ldb_dn_compare(msg->dn, ar->search_msg->dn) != 0) {
757                 ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_request rename %s => %s\n",
758                           ldb_dn_get_linearized(ar->search_msg->dn),
759                           ldb_dn_get_linearized(msg->dn));
760                 if (ldb_rename(ldb, ar->search_msg->dn, msg->dn) != LDB_SUCCESS) {
761                         ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_replicated_request rename %s => %s failed - %s\n",
762                                   ldb_dn_get_linearized(ar->search_msg->dn),
763                                   ldb_dn_get_linearized(msg->dn),
764                                   ldb_errstring(ldb));
765                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_DB_ERROR);
766                 }
767         }
768
769         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
770         if (ret != LDB_SUCCESS) {
771                 return replmd_replicated_request_error(ar, ret);
772         }
773
774         /* find existing meta data */
775         omd_value = ldb_msg_find_ldb_val(ar->search_msg, "replPropertyMetaData");
776         if (omd_value) {
777                 ndr_err = ndr_pull_struct_blob(omd_value, ar,
778                                                lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &omd,
779                                                (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
780                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
781                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
782                         return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
783                 }
784
785                 if (omd.version != 1) {
786                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
787                 }
788         }
789
790         ZERO_STRUCT(nmd);
791         nmd.version = 1;
792         nmd.ctr.ctr1.count = omd.ctr.ctr1.count + rmd->ctr.ctr1.count;
793         nmd.ctr.ctr1.array = talloc_array(ar,
794                                           struct replPropertyMetaData1,
795                                           nmd.ctr.ctr1.count);
796         if (!nmd.ctr.ctr1.array) return replmd_replicated_request_werror(ar, WERR_NOMEM);
797
798         /* first copy the old meta data */
799         for (i=0; i < omd.ctr.ctr1.count; i++) {
800                 nmd.ctr.ctr1.array[ni]  = omd.ctr.ctr1.array[i];
801                 ni++;
802         }
803
804         /* now merge in the new meta data */
805         for (i=0; i < rmd->ctr.ctr1.count; i++) {
806                 bool found = false;
807
808                 rmd->ctr.ctr1.array[i].local_usn = seq_num;
809
810                 for (j=0; j < ni; j++) {
811                         int cmp;
812
813                         if (rmd->ctr.ctr1.array[i].attid != nmd.ctr.ctr1.array[j].attid) {
814                                 continue;
815                         }
816
817                         cmp = replmd_replPropertyMetaData1_conflict_compare(&rmd->ctr.ctr1.array[i],
818                                                                             &nmd.ctr.ctr1.array[j]);
819                         if (cmp > 0) {
820                                 /* replace the entry */
821                                 nmd.ctr.ctr1.array[j] = rmd->ctr.ctr1.array[i];
822                                 found = true;
823                                 break;
824                         }
825
826                         /* we don't want to apply this change so remove the attribute */
827                         ldb_msg_remove_element(msg, &msg->elements[i-removed_attrs]);
828                         removed_attrs++;
829
830                         found = true;
831                         break;
832                 }
833
834                 if (found) continue;
835
836                 nmd.ctr.ctr1.array[ni] = rmd->ctr.ctr1.array[i];
837                 ni++;
838         }
839
840         /*
841          * finally correct the size of the meta_data array
842          */
843         nmd.ctr.ctr1.count = ni;
844
845         /*
846          * the rdn attribute (the alias for the name attribute),
847          * 'cn' for most objects is the last entry in the meta data array
848          * we have stored
849          *
850          * sort the new meta data array
851          */
852         {
853                 struct replPropertyMetaData1 *rdn_p;
854                 uint32_t rdn_idx = omd.ctr.ctr1.count - 1;
855
856                 rdn_p = &nmd.ctr.ctr1.array[rdn_idx];
857                 replmd_replPropertyMetaDataCtr1_sort(&nmd.ctr.ctr1, &rdn_p->attid);
858         }
859
860         /* create the meta data value */
861         ndr_err = ndr_push_struct_blob(&nmd_value, msg, 
862                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
863                                        &nmd,
864                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
865         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
866                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
867                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
868         }
869
870         /*
871          * check if some replicated attributes left, otherwise skip the ldb_modify() call
872          */
873         if (msg->num_elements == 0) {
874                 ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_apply_merge[%u]: skip replace\n",
875                           ar->index_current);
876
877                 ar->index_current++;
878                 return replmd_replicated_apply_next(ar);
879         }
880
881         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_apply_merge[%u]: replace %u attributes\n",
882                   ar->index_current, msg->num_elements);
883
884         /*
885          * when we know that we'll modify the record, add the whenChanged, uSNChanged
886          * and replPopertyMetaData attributes
887          */
888         ret = ldb_msg_add_string(msg, "whenChanged", ar->objs->objects[ar->index_current].when_changed);
889         if (ret != LDB_SUCCESS) {
890                 return replmd_replicated_request_error(ar, ret);
891         }
892         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", seq_num);
893         if (ret != LDB_SUCCESS) {
894                 return replmd_replicated_request_error(ar, ret);
895         }
896         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &nmd_value, NULL);
897         if (ret != LDB_SUCCESS) {
898                 return replmd_replicated_request_error(ar, ret);
899         }
900
901         replmd_ldb_message_sort(msg, ar->schema);
902
903         /* we want to replace the old values */
904         for (i=0; i < msg->num_elements; i++) {
905                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
906         }
907
908         ret = ldb_build_mod_req(&change_req,
909                                 ldb,
910                                 ar,
911                                 msg,
912                                 ar->controls,
913                                 ar,
914                                 replmd_replicated_apply_merge_callback,
915                                 ar->req);
916         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
917
918         return ldb_next_request(ar->module, change_req);
919 }
920
921 static int replmd_replicated_apply_search_callback(struct ldb_request *req,
922                                                    struct ldb_reply *ares)
923 {
924         struct replmd_replicated_request *ar = talloc_get_type(req->context,
925                                                struct replmd_replicated_request);
926         int ret;
927
928         if (!ares) {
929                 return ldb_module_done(ar->req, NULL, NULL,
930                                         LDB_ERR_OPERATIONS_ERROR);
931         }
932         if (ares->error != LDB_SUCCESS &&
933             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
934                 return ldb_module_done(ar->req, ares->controls,
935                                         ares->response, ares->error);
936         }
937
938         switch (ares->type) {
939         case LDB_REPLY_ENTRY:
940                 ar->search_msg = talloc_steal(ar, ares->message);
941                 break;
942
943         case LDB_REPLY_REFERRAL:
944                 /* we ignore referrals */
945                 break;
946
947         case LDB_REPLY_DONE:
948                 if (ar->search_msg != NULL) {
949                         ret = replmd_replicated_apply_merge(ar);
950                 } else {
951                         ret = replmd_replicated_apply_add(ar);
952                 }
953                 if (ret != LDB_SUCCESS) {
954                         return ldb_module_done(ar->req, NULL, NULL, ret);
955                 }
956         }
957
958         talloc_free(ares);
959         return LDB_SUCCESS;
960 }
961
962 static int replmd_replicated_uptodate_vector(struct replmd_replicated_request *ar);
963
964 static int replmd_replicated_apply_next(struct replmd_replicated_request *ar)
965 {
966         struct ldb_context *ldb;
967         int ret;
968         char *tmp_str;
969         char *filter;
970         struct ldb_request *search_req;
971
972         if (ar->index_current >= ar->objs->num_objects) {
973                 /* done with it, go to the last op */
974                 return replmd_replicated_uptodate_vector(ar);
975         }
976
977         ldb = ldb_module_get_ctx(ar->module);
978         ar->search_msg = NULL;
979
980         tmp_str = ldb_binary_encode(ar, ar->objs->objects[ar->index_current].guid_value);
981         if (!tmp_str) return replmd_replicated_request_werror(ar, WERR_NOMEM);
982
983         filter = talloc_asprintf(ar, "(objectGUID=%s)", tmp_str);
984         if (!filter) return replmd_replicated_request_werror(ar, WERR_NOMEM);
985         talloc_free(tmp_str);
986
987         ret = ldb_build_search_req(&search_req,
988                                    ldb,
989                                    ar,
990                                    ar->objs->partition_dn,
991                                    LDB_SCOPE_SUBTREE,
992                                    filter,
993                                    NULL,
994                                    NULL,
995                                    ar,
996                                    replmd_replicated_apply_search_callback,
997                                    ar->req);
998         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
999
1000         return ldb_next_request(ar->module, search_req);
1001 }
1002
1003 static int replmd_replicated_uptodate_modify_callback(struct ldb_request *req,
1004                                                       struct ldb_reply *ares)
1005 {
1006         struct ldb_context *ldb;
1007         struct replmd_replicated_request *ar = talloc_get_type(req->context,
1008                                                struct replmd_replicated_request);
1009         ldb = ldb_module_get_ctx(ar->module);
1010
1011         if (!ares) {
1012                 return ldb_module_done(ar->req, NULL, NULL,
1013                                         LDB_ERR_OPERATIONS_ERROR);
1014         }
1015         if (ares->error != LDB_SUCCESS) {
1016                 return ldb_module_done(ar->req, ares->controls,
1017                                         ares->response, ares->error);
1018         }
1019
1020         if (ares->type != LDB_REPLY_DONE) {
1021                 ldb_set_errstring(ldb, "Invalid reply type\n!");
1022                 return ldb_module_done(ar->req, NULL, NULL,
1023                                         LDB_ERR_OPERATIONS_ERROR);
1024         }
1025
1026         talloc_free(ares);
1027
1028         return ldb_module_done(ar->req, NULL, NULL, LDB_SUCCESS);
1029 }
1030
1031 static int replmd_drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
1032                                                    const struct drsuapi_DsReplicaCursor2 *c2)
1033 {
1034         return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
1035 }
1036
1037 static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *ar)
1038 {
1039         struct ldb_context *ldb;
1040         struct ldb_request *change_req;
1041         enum ndr_err_code ndr_err;
1042         struct ldb_message *msg;
1043         struct replUpToDateVectorBlob ouv;
1044         const struct ldb_val *ouv_value;
1045         const struct drsuapi_DsReplicaCursor2CtrEx *ruv;
1046         struct replUpToDateVectorBlob nuv;
1047         struct ldb_val nuv_value;
1048         struct ldb_message_element *nuv_el = NULL;
1049         const struct GUID *our_invocation_id;
1050         struct ldb_message_element *orf_el = NULL;
1051         struct repsFromToBlob nrf;
1052         struct ldb_val *nrf_value = NULL;
1053         struct ldb_message_element *nrf_el = NULL;
1054         uint32_t i,j,ni=0;
1055         uint64_t seq_num;
1056         bool found = false;
1057         time_t t = time(NULL);
1058         NTTIME now;
1059         int ret;
1060
1061         ldb = ldb_module_get_ctx(ar->module);
1062         ruv = ar->objs->uptodateness_vector;
1063         ZERO_STRUCT(ouv);
1064         ouv.version = 2;
1065         ZERO_STRUCT(nuv);
1066         nuv.version = 2;
1067
1068         unix_to_nt_time(&now, t);
1069
1070         /* 
1071          * we use the next sequence number for our own highest_usn
1072          * because we will do a modify request and this will increment
1073          * our highest_usn
1074          */
1075         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
1076         if (ret != LDB_SUCCESS) {
1077                 return replmd_replicated_request_error(ar, ret);
1078         }
1079
1080         /*
1081          * first create the new replUpToDateVector
1082          */
1083         ouv_value = ldb_msg_find_ldb_val(ar->search_msg, "replUpToDateVector");
1084         if (ouv_value) {
1085                 ndr_err = ndr_pull_struct_blob(ouv_value, ar,
1086                                                lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &ouv,
1087                                                (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
1088                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1089                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1090                         return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1091                 }
1092
1093                 if (ouv.version != 2) {
1094                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
1095                 }
1096         }
1097
1098         /*
1099          * the new uptodateness vector will at least
1100          * contain 1 entry, one for the source_dsa
1101          *
1102          * plus optional values from our old vector and the one from the source_dsa
1103          */
1104         nuv.ctr.ctr2.count = 1 + ouv.ctr.ctr2.count;
1105         if (ruv) nuv.ctr.ctr2.count += ruv->count;
1106         nuv.ctr.ctr2.cursors = talloc_array(ar,
1107                                             struct drsuapi_DsReplicaCursor2,
1108                                             nuv.ctr.ctr2.count);
1109         if (!nuv.ctr.ctr2.cursors) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1110
1111         /* first copy the old vector */
1112         for (i=0; i < ouv.ctr.ctr2.count; i++) {
1113                 nuv.ctr.ctr2.cursors[ni] = ouv.ctr.ctr2.cursors[i];
1114                 ni++;
1115         }
1116
1117         /* get our invocation_id if we have one already attached to the ldb */
1118         our_invocation_id = samdb_ntds_invocation_id(ldb);
1119
1120         /* merge in the source_dsa vector is available */
1121         for (i=0; (ruv && i < ruv->count); i++) {
1122                 found = false;
1123
1124                 if (our_invocation_id &&
1125                     GUID_equal(&ruv->cursors[i].source_dsa_invocation_id,
1126                                our_invocation_id)) {
1127                         continue;
1128                 }
1129
1130                 for (j=0; j < ni; j++) {
1131                         if (!GUID_equal(&ruv->cursors[i].source_dsa_invocation_id,
1132                                         &nuv.ctr.ctr2.cursors[j].source_dsa_invocation_id)) {
1133                                 continue;
1134                         }
1135
1136                         found = true;
1137
1138                         /*
1139                          * we update only the highest_usn and not the latest_sync_success time,
1140                          * because the last success stands for direct replication
1141                          */
1142                         if (ruv->cursors[i].highest_usn > nuv.ctr.ctr2.cursors[j].highest_usn) {
1143                                 nuv.ctr.ctr2.cursors[j].highest_usn = ruv->cursors[i].highest_usn;
1144                         }
1145                         break;                  
1146                 }
1147
1148                 if (found) continue;
1149
1150                 /* if it's not there yet, add it */
1151                 nuv.ctr.ctr2.cursors[ni] = ruv->cursors[i];
1152                 ni++;
1153         }
1154
1155         /*
1156          * merge in the current highwatermark for the source_dsa
1157          */
1158         found = false;
1159         for (j=0; j < ni; j++) {
1160                 if (!GUID_equal(&ar->objs->source_dsa->source_dsa_invocation_id,
1161                                 &nuv.ctr.ctr2.cursors[j].source_dsa_invocation_id)) {
1162                         continue;
1163                 }
1164
1165                 found = true;
1166
1167                 /*
1168                  * here we update the highest_usn and last_sync_success time
1169                  * because we're directly replicating from the source_dsa
1170                  *
1171                  * and use the tmp_highest_usn because this is what we have just applied
1172                  * to our ldb
1173                  */
1174                 nuv.ctr.ctr2.cursors[j].highest_usn             = ar->objs->source_dsa->highwatermark.tmp_highest_usn;
1175                 nuv.ctr.ctr2.cursors[j].last_sync_success       = now;
1176                 break;
1177         }
1178         if (!found) {
1179                 /*
1180                  * here we update the highest_usn and last_sync_success time
1181                  * because we're directly replicating from the source_dsa
1182                  *
1183                  * and use the tmp_highest_usn because this is what we have just applied
1184                  * to our ldb
1185                  */
1186                 nuv.ctr.ctr2.cursors[ni].source_dsa_invocation_id= ar->objs->source_dsa->source_dsa_invocation_id;
1187                 nuv.ctr.ctr2.cursors[ni].highest_usn            = ar->objs->source_dsa->highwatermark.tmp_highest_usn;
1188                 nuv.ctr.ctr2.cursors[ni].last_sync_success      = now;
1189                 ni++;
1190         }
1191
1192         /*
1193          * finally correct the size of the cursors array
1194          */
1195         nuv.ctr.ctr2.count = ni;
1196
1197         /*
1198          * sort the cursors
1199          */
1200         qsort(nuv.ctr.ctr2.cursors, nuv.ctr.ctr2.count,
1201               sizeof(struct drsuapi_DsReplicaCursor2),
1202               (comparison_fn_t)replmd_drsuapi_DsReplicaCursor2_compare);
1203
1204         /*
1205          * create the change ldb_message
1206          */
1207         msg = ldb_msg_new(ar);
1208         if (!msg) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1209         msg->dn = ar->search_msg->dn;
1210
1211         ndr_err = ndr_push_struct_blob(&nuv_value, msg, 
1212                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
1213                                        &nuv,
1214                                        (ndr_push_flags_fn_t)ndr_push_replUpToDateVectorBlob);
1215         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1216                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1217                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1218         }
1219         ret = ldb_msg_add_value(msg, "replUpToDateVector", &nuv_value, &nuv_el);
1220         if (ret != LDB_SUCCESS) {
1221                 return replmd_replicated_request_error(ar, ret);
1222         }
1223         nuv_el->flags = LDB_FLAG_MOD_REPLACE;
1224
1225         /*
1226          * now create the new repsFrom value from the given repsFromTo1 structure
1227          */
1228         ZERO_STRUCT(nrf);
1229         nrf.version                                     = 1;
1230         nrf.ctr.ctr1                                    = *ar->objs->source_dsa;
1231         /* and fix some values... */
1232         nrf.ctr.ctr1.consecutive_sync_failures          = 0;
1233         nrf.ctr.ctr1.last_success                       = now;
1234         nrf.ctr.ctr1.last_attempt                       = now;
1235         nrf.ctr.ctr1.result_last_attempt                = WERR_OK;
1236         nrf.ctr.ctr1.highwatermark.highest_usn          = nrf.ctr.ctr1.highwatermark.tmp_highest_usn;
1237
1238         /*
1239          * first see if we already have a repsFrom value for the current source dsa
1240          * if so we'll later replace this value
1241          */
1242         orf_el = ldb_msg_find_element(ar->search_msg, "repsFrom");
1243         if (orf_el) {
1244                 for (i=0; i < orf_el->num_values; i++) {
1245                         struct repsFromToBlob *trf;
1246
1247                         trf = talloc(ar, struct repsFromToBlob);
1248                         if (!trf) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1249
1250                         ndr_err = ndr_pull_struct_blob(&orf_el->values[i], trf, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), trf,
1251                                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
1252                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1253                                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1254                                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1255                         }
1256
1257                         if (trf->version != 1) {
1258                                 return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
1259                         }
1260
1261                         /*
1262                          * we compare the source dsa objectGUID not the invocation_id
1263                          * because we want only one repsFrom value per source dsa
1264                          * and when the invocation_id of the source dsa has changed we don't need 
1265                          * the old repsFrom with the old invocation_id
1266                          */
1267                         if (!GUID_equal(&trf->ctr.ctr1.source_dsa_obj_guid,
1268                                         &ar->objs->source_dsa->source_dsa_obj_guid)) {
1269                                 talloc_free(trf);
1270                                 continue;
1271                         }
1272
1273                         talloc_free(trf);
1274                         nrf_value = &orf_el->values[i];
1275                         break;
1276                 }
1277
1278                 /*
1279                  * copy over all old values to the new ldb_message
1280                  */
1281                 ret = ldb_msg_add_empty(msg, "repsFrom", 0, &nrf_el);
1282                 if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1283                 *nrf_el = *orf_el;
1284         }
1285
1286         /*
1287          * if we haven't found an old repsFrom value for the current source dsa
1288          * we'll add a new value
1289          */
1290         if (!nrf_value) {
1291                 struct ldb_val zero_value;
1292                 ZERO_STRUCT(zero_value);
1293                 ret = ldb_msg_add_value(msg, "repsFrom", &zero_value, &nrf_el);
1294                 if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1295
1296                 nrf_value = &nrf_el->values[nrf_el->num_values - 1];
1297         }
1298
1299         /* we now fill the value which is already attached to ldb_message */
1300         ndr_err = ndr_push_struct_blob(nrf_value, msg, 
1301                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1302                                        &nrf,
1303                                        (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
1304         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1305                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1306                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1307         }
1308
1309         /* 
1310          * the ldb_message_element for the attribute, has all the old values and the new one
1311          * so we'll replace the whole attribute with all values
1312          */
1313         nrf_el->flags = LDB_FLAG_MOD_REPLACE;
1314
1315         /* prepare the ldb_modify() request */
1316         ret = ldb_build_mod_req(&change_req,
1317                                 ldb,
1318                                 ar,
1319                                 msg,
1320                                 ar->controls,
1321                                 ar,
1322                                 replmd_replicated_uptodate_modify_callback,
1323                                 ar->req);
1324         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1325
1326         return ldb_next_request(ar->module, change_req);
1327 }
1328
1329 static int replmd_replicated_uptodate_search_callback(struct ldb_request *req,
1330                                                       struct ldb_reply *ares)
1331 {
1332         struct replmd_replicated_request *ar = talloc_get_type(req->context,
1333                                                struct replmd_replicated_request);
1334         int ret;
1335
1336         if (!ares) {
1337                 return ldb_module_done(ar->req, NULL, NULL,
1338                                         LDB_ERR_OPERATIONS_ERROR);
1339         }
1340         if (ares->error != LDB_SUCCESS &&
1341             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
1342                 return ldb_module_done(ar->req, ares->controls,
1343                                         ares->response, ares->error);
1344         }
1345
1346         switch (ares->type) {
1347         case LDB_REPLY_ENTRY:
1348                 ar->search_msg = talloc_steal(ar, ares->message);
1349                 break;
1350
1351         case LDB_REPLY_REFERRAL:
1352                 /* we ignore referrals */
1353                 break;
1354
1355         case LDB_REPLY_DONE:
1356                 if (ar->search_msg == NULL) {
1357                         ret = replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
1358                 } else {
1359                         ret = replmd_replicated_uptodate_modify(ar);
1360                 }
1361                 if (ret != LDB_SUCCESS) {
1362                         return ldb_module_done(ar->req, NULL, NULL, ret);
1363                 }
1364         }
1365
1366         talloc_free(ares);
1367         return LDB_SUCCESS;
1368 }
1369
1370
1371 static int replmd_replicated_uptodate_vector(struct replmd_replicated_request *ar)
1372 {
1373         struct ldb_context *ldb;
1374         int ret;
1375         static const char *attrs[] = {
1376                 "replUpToDateVector",
1377                 "repsFrom",
1378                 NULL
1379         };
1380         struct ldb_request *search_req;
1381
1382         ldb = ldb_module_get_ctx(ar->module);
1383         ar->search_msg = NULL;
1384
1385         ret = ldb_build_search_req(&search_req,
1386                                    ldb,
1387                                    ar,
1388                                    ar->objs->partition_dn,
1389                                    LDB_SCOPE_BASE,
1390                                    "(objectClass=*)",
1391                                    attrs,
1392                                    NULL,
1393                                    ar,
1394                                    replmd_replicated_uptodate_search_callback,
1395                                    ar->req);
1396         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1397
1398         return ldb_next_request(ar->module, search_req);
1399 }
1400
1401 static int replmd_extended_replicated_objects(struct ldb_module *module, struct ldb_request *req)
1402 {
1403         struct ldb_context *ldb;
1404         struct dsdb_extended_replicated_objects *objs;
1405         struct replmd_replicated_request *ar;
1406         struct ldb_control **ctrls;
1407         int ret;
1408
1409         ldb = ldb_module_get_ctx(module);
1410
1411         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_extended_replicated_objects\n");
1412
1413         objs = talloc_get_type(req->op.extended.data, struct dsdb_extended_replicated_objects);
1414         if (!objs) {
1415                 ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_extended_replicated_objects: invalid extended data\n");
1416                 return LDB_ERR_PROTOCOL_ERROR;
1417         }
1418
1419         if (objs->version != DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION) {
1420                 ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_extended_replicated_objects: extended data invalid version [%u != %u]\n",
1421                           objs->version, DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION);
1422                 return LDB_ERR_PROTOCOL_ERROR;
1423         }
1424
1425         ar = replmd_ctx_init(module, req);
1426         if (!ar)
1427                 return LDB_ERR_OPERATIONS_ERROR;
1428
1429         ar->objs = objs;
1430         ar->schema = dsdb_get_schema(ldb);
1431         if (!ar->schema) {
1432                 ldb_debug_set(ldb, LDB_DEBUG_FATAL, "replmd_ctx_init: no loaded schema found\n");
1433                 talloc_free(ar);
1434                 return LDB_ERR_CONSTRAINT_VIOLATION;
1435         }
1436
1437         ctrls = req->controls;
1438
1439         if (req->controls) {
1440                 req->controls = talloc_memdup(ar, req->controls,
1441                                               talloc_get_size(req->controls));
1442                 if (!req->controls) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1443         }
1444
1445         ret = ldb_request_add_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID, false, NULL);
1446         if (ret != LDB_SUCCESS) {
1447                 return ret;
1448         }
1449
1450         ar->controls = req->controls;
1451         req->controls = ctrls;
1452
1453         return replmd_replicated_apply_next(ar);
1454 }
1455
1456 static int replmd_extended(struct ldb_module *module, struct ldb_request *req)
1457 {
1458         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_REPLICATED_OBJECTS_OID) == 0) {
1459                 return replmd_extended_replicated_objects(module, req);
1460         }
1461
1462         return ldb_next_request(module, req);
1463 }
1464
1465 _PUBLIC_ const struct ldb_module_ops ldb_repl_meta_data_module_ops = {
1466         .name          = "repl_meta_data",
1467         .add           = replmd_add,
1468         .modify        = replmd_modify,
1469         .extended      = replmd_extended,
1470 };