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