s4-drs: Using dsdb_msg_add_guid() utility function
[amitay/samba.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    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /*
24  *  Name: ldb
25  *
26  *  Component: ldb repl_meta_data module
27  *
28  *  Description: - add a unique objectGUID onto every new record,
29  *               - handle whenCreated, whenChanged timestamps
30  *               - handle uSNCreated, uSNChanged numbers
31  *               - handle replPropertyMetaData attribute
32  *
33  *  Author: Simo Sorce
34  *  Author: Stefan Metzmacher
35  */
36
37 #include "includes.h"
38 #include "ldb_module.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "dsdb/common/proto.h"
41 #include "../libds/common/flags.h"
42 #include "librpc/gen_ndr/ndr_misc.h"
43 #include "librpc/gen_ndr/ndr_drsuapi.h"
44 #include "librpc/gen_ndr/ndr_drsblobs.h"
45 #include "param/param.h"
46 #include "libcli/security/dom_sid.h"
47 #include "lib/util/dlinklist.h"
48
49 struct replmd_private {
50         TALLOC_CTX *la_ctx;
51         struct la_entry *la_list;
52         struct nc_entry {
53                 struct nc_entry *prev, *next;
54                 struct ldb_dn *dn;
55                 uint64_t mod_usn;
56         } *ncs;
57 };
58
59 struct la_entry {
60         struct la_entry *next, *prev;
61         struct drsuapi_DsReplicaLinkedAttribute *la;
62 };
63
64 struct replmd_replicated_request {
65         struct ldb_module *module;
66         struct ldb_request *req;
67
68         const struct dsdb_schema *schema;
69
70         /* the controls we pass down */
71         struct ldb_control **controls;
72
73         /* details for the mode where we apply a bunch of inbound replication meessages */
74         bool apply_mode;
75         uint32_t index_current;
76         struct dsdb_extended_replicated_objects *objs;
77
78         struct ldb_message *search_msg;
79
80         uint64_t seq_num;
81
82 };
83
84 static int replmd_replicated_apply_next(struct replmd_replicated_request *ar);
85
86 /*
87   initialise the module
88   allocate the private structure and build the list
89   of partition DNs for use by replmd_notify()
90  */
91 static int replmd_init(struct ldb_module *module)
92 {
93         struct replmd_private *replmd_private;
94         struct ldb_context *ldb = ldb_module_get_ctx(module);
95
96         replmd_private = talloc_zero(module, struct replmd_private);
97         if (replmd_private == NULL) {
98                 ldb_oom(ldb);
99                 return LDB_ERR_OPERATIONS_ERROR;
100         }
101         ldb_module_set_private(module, replmd_private);
102
103         return ldb_next_init(module);
104 }
105
106
107 /*
108  * Callback for most write operations in this module:
109  * 
110  * notify the repl task that a object has changed. The notifies are
111  * gathered up in the replmd_private structure then written to the
112  * @REPLCHANGED object in each partition during the prepare_commit
113  */
114 static int replmd_op_callback(struct ldb_request *req, struct ldb_reply *ares)
115 {
116         int ret;
117         struct replmd_replicated_request *ac = 
118                 talloc_get_type_abort(req->context, struct replmd_replicated_request);
119         struct replmd_private *replmd_private = 
120                 talloc_get_type_abort(ldb_module_get_private(ac->module), struct replmd_private);
121         struct nc_entry *modified_partition;
122         struct ldb_control *partition_ctrl;
123         const struct dsdb_control_current_partition *partition;
124
125         struct ldb_control **controls;
126
127         partition_ctrl = ldb_reply_get_control(ares, DSDB_CONTROL_CURRENT_PARTITION_OID);
128
129         /* Remove the 'partition' control from what we pass up the chain */
130         controls = controls_except_specified(ares->controls, ares, partition_ctrl);
131
132         if (ares->error != LDB_SUCCESS) {
133                 return ldb_module_done(ac->req, controls,
134                                         ares->response, ares->error);
135         }
136
137         if (ares->type != LDB_REPLY_DONE) {
138                 ldb_set_errstring(ldb_module_get_ctx(ac->module), "Invalid reply type for notify\n!");
139                 return ldb_module_done(ac->req, NULL,
140                                        NULL, LDB_ERR_OPERATIONS_ERROR);
141         }
142
143         if (!partition_ctrl) {
144                 return ldb_module_done(ac->req, NULL,
145                                        NULL, LDB_ERR_OPERATIONS_ERROR);
146         }
147
148         partition = talloc_get_type_abort(partition_ctrl->data,
149                                     struct dsdb_control_current_partition);
150         
151         if (ac->seq_num > 0) {
152                 for (modified_partition = replmd_private->ncs; modified_partition; 
153                      modified_partition = modified_partition->next) {
154                         if (ldb_dn_compare(modified_partition->dn, partition->dn) == 0) {
155                                 break;
156                         }
157                 }
158                 
159                 if (modified_partition == NULL) {
160                         modified_partition = talloc_zero(replmd_private, struct nc_entry);
161                         if (!modified_partition) {
162                                 ldb_oom(ldb_module_get_ctx(ac->module));
163                                 return ldb_module_done(ac->req, NULL,
164                                                        NULL, LDB_ERR_OPERATIONS_ERROR);
165                         }
166                         modified_partition->dn = ldb_dn_copy(modified_partition, partition->dn);
167                         if (!modified_partition->dn) {
168                                 ldb_oom(ldb_module_get_ctx(ac->module));
169                                 return ldb_module_done(ac->req, NULL,
170                                                        NULL, LDB_ERR_OPERATIONS_ERROR);
171                         }
172                         DLIST_ADD(replmd_private->ncs, modified_partition);
173                 }
174
175                 if (ac->seq_num > modified_partition->mod_usn) {
176                         modified_partition->mod_usn = ac->seq_num;
177                 }
178         }
179
180         if (ac->apply_mode) {
181                 talloc_free(ares);
182                 ac->index_current++;
183                 
184                 ret = replmd_replicated_apply_next(ac);
185                 if (ret != LDB_SUCCESS) {
186                         return ldb_module_done(ac->req, NULL, NULL, ret);
187                 }
188                 return ret;
189         } else {
190                 /* free the partition control container here, for the
191                  * common path.  Other cases will have it cleaned up
192                  * eventually with the ares */
193                 talloc_free(partition_ctrl);
194                 return ldb_module_done(ac->req, 
195                                        controls_except_specified(controls, ares, partition_ctrl),
196                                        ares->response, LDB_SUCCESS);
197         }
198 }
199
200
201 /*
202  * update a @REPLCHANGED record in each partition if there have been
203  * any writes of replicated data in the partition
204  */
205 static int replmd_notify_store(struct ldb_module *module)
206 {
207         struct replmd_private *replmd_private = 
208                 talloc_get_type(ldb_module_get_private(module), struct replmd_private);
209         struct ldb_context *ldb = ldb_module_get_ctx(module);
210
211         while (replmd_private->ncs) {
212                 int ret;
213                 struct nc_entry *modified_partition = replmd_private->ncs;
214
215                 ret = dsdb_save_partition_usn(ldb, modified_partition->dn, modified_partition->mod_usn);
216                 if (ret != LDB_SUCCESS) {
217                         DEBUG(0,(__location__ ": Failed to save partition uSN for %s\n",
218                                  ldb_dn_get_linearized(modified_partition->dn)));
219                         return ret;
220                 }
221                 DLIST_REMOVE(replmd_private->ncs, modified_partition);
222                 talloc_free(modified_partition);
223         }
224
225         return LDB_SUCCESS;
226 }
227
228
229 /*
230   created a replmd_replicated_request context
231  */
232 static struct replmd_replicated_request *replmd_ctx_init(struct ldb_module *module,
233                                                          struct ldb_request *req)
234 {
235         struct ldb_context *ldb;
236         struct replmd_replicated_request *ac;
237
238         ldb = ldb_module_get_ctx(module);
239
240         ac = talloc_zero(req, struct replmd_replicated_request);
241         if (ac == NULL) {
242                 ldb_oom(ldb);
243                 return NULL;
244         }
245
246         ac->module = module;
247         ac->req = req;
248
249         ac->schema = dsdb_get_schema(ldb);
250         if (!ac->schema) {
251                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
252                               "replmd_modify: no dsdb_schema loaded");
253                 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
254                 return NULL;
255         }
256
257         return ac;
258 }
259
260 /*
261   add a time element to a record
262 */
263 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
264 {
265         struct ldb_message_element *el;
266         char *s;
267
268         if (ldb_msg_find_element(msg, attr) != NULL) {
269                 return LDB_SUCCESS;
270         }
271
272         s = ldb_timestring(msg, t);
273         if (s == NULL) {
274                 return LDB_ERR_OPERATIONS_ERROR;
275         }
276
277         if (ldb_msg_add_string(msg, attr, s) != LDB_SUCCESS) {
278                 return LDB_ERR_OPERATIONS_ERROR;
279         }
280
281         el = ldb_msg_find_element(msg, attr);
282         /* always set as replace. This works because on add ops, the flag
283            is ignored */
284         el->flags = LDB_FLAG_MOD_REPLACE;
285
286         return LDB_SUCCESS;
287 }
288
289 /*
290   add a uint64_t element to a record
291 */
292 static int add_uint64_element(struct ldb_message *msg, const char *attr, uint64_t v)
293 {
294         struct ldb_message_element *el;
295
296         if (ldb_msg_find_element(msg, attr) != NULL) {
297                 return LDB_SUCCESS;
298         }
299
300         if (ldb_msg_add_fmt(msg, attr, "%llu", (unsigned long long)v) != LDB_SUCCESS) {
301                 return LDB_ERR_OPERATIONS_ERROR;
302         }
303
304         el = ldb_msg_find_element(msg, attr);
305         /* always set as replace. This works because on add ops, the flag
306            is ignored */
307         el->flags = LDB_FLAG_MOD_REPLACE;
308
309         return LDB_SUCCESS;
310 }
311
312 static int replmd_replPropertyMetaData1_attid_sort(const struct replPropertyMetaData1 *m1,
313                                                    const struct replPropertyMetaData1 *m2,
314                                                    const uint32_t *rdn_attid)
315 {
316         if (m1->attid == m2->attid) {
317                 return 0;
318         }
319
320         /*
321          * the rdn attribute should be at the end!
322          * so we need to return a value greater than zero
323          * which means m1 is greater than m2
324          */
325         if (m1->attid == *rdn_attid) {
326                 return 1;
327         }
328
329         /*
330          * the rdn attribute should be at the end!
331          * so we need to return a value less than zero
332          * which means m2 is greater than m1
333          */
334         if (m2->attid == *rdn_attid) {
335                 return -1;
336         }
337
338         return m1->attid - m2->attid;
339 }
340
341 static int replmd_replPropertyMetaDataCtr1_sort(struct replPropertyMetaDataCtr1 *ctr1,
342                                                 const struct dsdb_schema *schema,
343                                                 struct ldb_dn *dn)
344 {
345         const char *rdn_name;
346         const struct dsdb_attribute *rdn_sa;
347
348         rdn_name = ldb_dn_get_rdn_name(dn);
349         if (!rdn_name) {
350                 DEBUG(0,(__location__ ": No rDN for %s?\n", ldb_dn_get_linearized(dn)));
351                 return LDB_ERR_OPERATIONS_ERROR;
352         }
353
354         rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
355         if (rdn_sa == NULL) {
356                 DEBUG(0,(__location__ ": No sa found for rDN %s for %s\n", rdn_name, ldb_dn_get_linearized(dn)));
357                 return LDB_ERR_OPERATIONS_ERROR;                
358         }
359
360         DEBUG(6,("Sorting rpmd with attid exception %u rDN=%s DN=%s\n", 
361                  rdn_sa->attributeID_id, rdn_name, ldb_dn_get_linearized(dn)));
362
363         ldb_qsort(ctr1->array, ctr1->count, sizeof(struct replPropertyMetaData1),
364                   discard_const_p(void, &rdn_sa->attributeID_id), 
365                   (ldb_qsort_cmp_fn_t)replmd_replPropertyMetaData1_attid_sort);
366
367         return LDB_SUCCESS;
368 }
369
370 static int replmd_ldb_message_element_attid_sort(const struct ldb_message_element *e1,
371                                                  const struct ldb_message_element *e2,
372                                                  const struct dsdb_schema *schema)
373 {
374         const struct dsdb_attribute *a1;
375         const struct dsdb_attribute *a2;
376
377         /* 
378          * TODO: make this faster by caching the dsdb_attribute pointer
379          *       on the ldb_messag_element
380          */
381
382         a1 = dsdb_attribute_by_lDAPDisplayName(schema, e1->name);
383         a2 = dsdb_attribute_by_lDAPDisplayName(schema, e2->name);
384
385         /*
386          * TODO: remove this check, we should rely on e1 and e2 having valid attribute names
387          *       in the schema
388          */
389         if (!a1 || !a2) {
390                 return strcasecmp(e1->name, e2->name);
391         }
392
393         return a1->attributeID_id - a2->attributeID_id;
394 }
395
396 static void replmd_ldb_message_sort(struct ldb_message *msg,
397                                     const struct dsdb_schema *schema)
398 {
399         ldb_qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element),
400                   discard_const_p(void, schema), (ldb_qsort_cmp_fn_t)replmd_ldb_message_element_attid_sort);
401 }
402
403 static int replmd_add(struct ldb_module *module, struct ldb_request *req)
404 {
405         struct ldb_context *ldb;
406         struct ldb_control *control;
407         struct ldb_control **saved_controls;
408         struct replmd_replicated_request *ac;
409         enum ndr_err_code ndr_err;
410         struct ldb_request *down_req;
411         struct ldb_message *msg;
412         const DATA_BLOB *guid_blob;
413         struct GUID guid;
414         struct replPropertyMetaDataBlob nmd;
415         struct ldb_val nmd_value;
416         const struct GUID *our_invocation_id;
417         time_t t = time(NULL);
418         NTTIME now;
419         char *time_str;
420         int ret;
421         uint32_t i, ni=0;
422         bool allow_add_guid = false;
423         bool remove_current_guid = false;
424
425         /* check if there's a show relax control (used by provision to say 'I know what I'm doing') */
426         control = ldb_request_get_control(req, LDB_CONTROL_RELAX_OID);
427         if (control) {
428                 allow_add_guid = 1;
429         }
430
431         /* do not manipulate our control entries */
432         if (ldb_dn_is_special(req->op.add.message->dn)) {
433                 return ldb_next_request(module, req);
434         }
435
436         ldb = ldb_module_get_ctx(module);
437
438         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_add\n");
439
440         ac = replmd_ctx_init(module, req);
441         if (!ac) {
442                 return LDB_ERR_OPERATIONS_ERROR;
443         }
444
445         guid_blob = ldb_msg_find_ldb_val(req->op.add.message, "objectGUID");
446         if ( guid_blob != NULL ) {
447                 if( !allow_add_guid ) {
448                         ldb_debug_set(ldb, LDB_DEBUG_ERROR,
449                               "replmd_add: it's not allowed to add an object with objectGUID\n");
450                         talloc_free(ac);
451                         return LDB_ERR_UNWILLING_TO_PERFORM;
452                 } else {
453                         NTSTATUS status = GUID_from_data_blob(guid_blob,&guid);
454                         if ( !NT_STATUS_IS_OK(status)) {
455                                 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
456                                       "replmd_add: Unable to parse as a GUID the attribute objectGUID\n");
457                                 talloc_free(ac);
458                                 return LDB_ERR_UNWILLING_TO_PERFORM;
459                         }
460                         /* we remove this attribute as it can be a string and will not be treated 
461                         correctly and then we will readd it latter on in the good format*/
462                         remove_current_guid = true;
463                 }
464         } else {
465                 /* a new GUID */
466                 guid = GUID_random();
467         }
468
469         /* Get a sequence number from the backend */
470         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &ac->seq_num);
471         if (ret != LDB_SUCCESS) {
472                 talloc_free(ac);
473                 return ret;
474         }
475
476         /* get our invocationId */
477         our_invocation_id = samdb_ntds_invocation_id(ldb);
478         if (!our_invocation_id) {
479                 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
480                               "replmd_add: unable to find invocationId\n");
481                 talloc_free(ac);
482                 return LDB_ERR_OPERATIONS_ERROR;
483         }
484
485         /* we have to copy the message as the caller might have it as a const */
486         msg = ldb_msg_copy_shallow(ac, req->op.add.message);
487         if (msg == NULL) {
488                 ldb_oom(ldb);
489                 talloc_free(ac);
490                 return LDB_ERR_OPERATIONS_ERROR;
491         }
492
493         /* generated times */
494         unix_to_nt_time(&now, t);
495         time_str = ldb_timestring(msg, t);
496         if (!time_str) {
497                 ldb_oom(ldb);
498                 talloc_free(ac);
499                 return LDB_ERR_OPERATIONS_ERROR;
500         }
501         if (remove_current_guid) {
502                 ldb_msg_remove_attr(msg,"objectGUID");
503         }
504
505         /* 
506          * remove autogenerated attributes
507          */
508         ldb_msg_remove_attr(msg, "whenCreated");
509         ldb_msg_remove_attr(msg, "whenChanged");
510         ldb_msg_remove_attr(msg, "uSNCreated");
511         ldb_msg_remove_attr(msg, "uSNChanged");
512         ldb_msg_remove_attr(msg, "replPropertyMetaData");
513
514         /*
515          * readd replicated attributes
516          */
517         ret = ldb_msg_add_string(msg, "whenCreated", time_str);
518         if (ret != LDB_SUCCESS) {
519                 ldb_oom(ldb);
520                 talloc_free(ac);
521                 return ret;
522         }
523
524         /* build the replication meta_data */
525         ZERO_STRUCT(nmd);
526         nmd.version             = 1;
527         nmd.ctr.ctr1.count      = msg->num_elements;
528         nmd.ctr.ctr1.array      = talloc_array(msg,
529                                                struct replPropertyMetaData1,
530                                                nmd.ctr.ctr1.count);
531         if (!nmd.ctr.ctr1.array) {
532                 ldb_oom(ldb);
533                 talloc_free(ac);
534                 return LDB_ERR_OPERATIONS_ERROR;
535         }
536
537         for (i=0; i < msg->num_elements; i++) {
538                 struct ldb_message_element *e = &msg->elements[i];
539                 struct replPropertyMetaData1 *m = &nmd.ctr.ctr1.array[ni];
540                 const struct dsdb_attribute *sa;
541
542                 if (e->name[0] == '@') continue;
543
544                 sa = dsdb_attribute_by_lDAPDisplayName(ac->schema, e->name);
545                 if (!sa) {
546                         ldb_debug_set(ldb, LDB_DEBUG_ERROR,
547                                       "replmd_add: attribute '%s' not defined in schema\n",
548                                       e->name);
549                         talloc_free(ac);
550                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
551                 }
552
553                 if ((sa->systemFlags & DS_FLAG_ATTR_NOT_REPLICATED) || (sa->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED)) {
554                         /* if the attribute is not replicated (0x00000001)
555                          * or constructed (0x00000004) it has no metadata
556                          */
557                         continue;
558                 }
559
560                 m->attid                        = sa->attributeID_id;
561                 m->version                      = 1;
562                 m->originating_change_time      = now;
563                 m->originating_invocation_id    = *our_invocation_id;
564                 m->originating_usn              = ac->seq_num;
565                 m->local_usn                    = ac->seq_num;
566                 ni++;
567         }
568
569         /* fix meta data count */
570         nmd.ctr.ctr1.count = ni;
571
572         /*
573          * sort meta data array, and move the rdn attribute entry to the end
574          */
575         ret = replmd_replPropertyMetaDataCtr1_sort(&nmd.ctr.ctr1, ac->schema, msg->dn);
576         if (ret != LDB_SUCCESS) {
577                 talloc_free(ac);
578                 return ret;
579         }
580
581         /* generated NDR encoded values */
582         ndr_err = ndr_push_struct_blob(&nmd_value, msg, 
583                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
584                                        &nmd,
585                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
586         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
587                 ldb_oom(ldb);
588                 talloc_free(ac);
589                 return LDB_ERR_OPERATIONS_ERROR;
590         }
591
592         /*
593          * add the autogenerated values
594          */
595         ret = dsdb_msg_add_guid(msg, &guid, "objectGUID");
596         if (ret != LDB_SUCCESS) {
597                 ldb_oom(ldb);
598                 talloc_free(ac);
599                 return ret;
600         }
601         ret = ldb_msg_add_string(msg, "whenChanged", time_str);
602         if (ret != LDB_SUCCESS) {
603                 ldb_oom(ldb);
604                 talloc_free(ac);
605                 return ret;
606         }
607         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNCreated", ac->seq_num);
608         if (ret != LDB_SUCCESS) {
609                 ldb_oom(ldb);
610                 talloc_free(ac);
611                 return ret;
612         }
613         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", ac->seq_num);
614         if (ret != LDB_SUCCESS) {
615                 ldb_oom(ldb);
616                 talloc_free(ac);
617                 return ret;
618         }
619         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &nmd_value, NULL);
620         if (ret != LDB_SUCCESS) {
621                 ldb_oom(ldb);
622                 talloc_free(ac);
623                 return ret;
624         }
625
626         /*
627          * sort the attributes by attid before storing the object
628          */
629         replmd_ldb_message_sort(msg, ac->schema);
630
631         ret = ldb_build_add_req(&down_req, ldb, ac,
632                                 msg,
633                                 req->controls,
634                                 ac, replmd_op_callback,
635                                 req);
636         if (ret != LDB_SUCCESS) {
637                 talloc_free(ac);
638                 return ret;
639         }
640
641         /* if a control is there remove if from the modified request */
642         if (control && !save_controls(control, down_req, &saved_controls)) {
643                 talloc_free(ac);
644                 return LDB_ERR_OPERATIONS_ERROR;
645         }
646
647         /* go on with the call chain */
648         return ldb_next_request(module, down_req);
649 }
650
651
652 /*
653  * update the replPropertyMetaData for one element
654  */
655 static int replmd_update_rpmd_element(struct ldb_context *ldb, 
656                                       struct ldb_message *msg,
657                                       struct ldb_message_element *el,
658                                       struct replPropertyMetaDataBlob *omd,
659                                       const struct dsdb_schema *schema,
660                                       uint64_t *seq_num,
661                                       const struct GUID *our_invocation_id,
662                                       NTTIME now)
663 {
664         int i;
665         const struct dsdb_attribute *a;
666         struct replPropertyMetaData1 *md1;
667
668         a = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
669         if (a == NULL) {
670                 DEBUG(0,(__location__ ": Unable to find attribute %s in schema\n",
671                          el->name));
672                 return LDB_ERR_OPERATIONS_ERROR;
673         }
674
675         if ((a->systemFlags & DS_FLAG_ATTR_NOT_REPLICATED) || (a->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED)) {
676                 return LDB_SUCCESS;
677         }
678
679         for (i=0; i<omd->ctr.ctr1.count; i++) {
680                 if (a->attributeID_id == omd->ctr.ctr1.array[i].attid) break;
681         }
682         if (i == omd->ctr.ctr1.count) {
683                 /* we need to add a new one */
684                 omd->ctr.ctr1.array = talloc_realloc(msg, omd->ctr.ctr1.array, 
685                                                      struct replPropertyMetaData1, omd->ctr.ctr1.count+1);
686                 if (omd->ctr.ctr1.array == NULL) {
687                         ldb_oom(ldb);
688                         return LDB_ERR_OPERATIONS_ERROR;
689                 }
690                 omd->ctr.ctr1.count++;
691                 ZERO_STRUCT(omd->ctr.ctr1.array[i]);
692         }
693
694         /* Get a new sequence number from the backend. We only do this
695          * if we have a change that requires a new
696          * replPropertyMetaData element 
697          */
698         if (*seq_num == 0) {
699                 int ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, seq_num);
700                 if (ret != LDB_SUCCESS) {
701                         return LDB_ERR_OPERATIONS_ERROR;
702                 }
703         }
704
705         md1 = &omd->ctr.ctr1.array[i];
706         md1->version++;
707         md1->attid                     = a->attributeID_id;
708         md1->originating_change_time   = now;
709         md1->originating_invocation_id = *our_invocation_id;
710         md1->originating_usn           = *seq_num;
711         md1->local_usn                 = *seq_num;
712         
713         return LDB_SUCCESS;
714 }
715
716 /*
717  * update the replPropertyMetaData object each time we modify an
718  * object. This is needed for DRS replication, as the merge on the
719  * client is based on this object 
720  */
721 static int replmd_update_rpmd(struct ldb_module *module, 
722                               const struct dsdb_schema *schema, 
723                               struct ldb_message *msg, uint64_t *seq_num)
724 {
725         const struct ldb_val *omd_value;
726         enum ndr_err_code ndr_err;
727         struct replPropertyMetaDataBlob omd;
728         int i;
729         time_t t = time(NULL);
730         NTTIME now;
731         const struct GUID *our_invocation_id;
732         int ret;
733         const char *attrs[] = { "replPropertyMetaData" , NULL };
734         struct ldb_result *res;
735         struct ldb_context *ldb;
736
737         ldb = ldb_module_get_ctx(module);
738
739         our_invocation_id = samdb_ntds_invocation_id(ldb);
740         if (!our_invocation_id) {
741                 /* this happens during an initial vampire while
742                    updating the schema */
743                 DEBUG(5,("No invocationID - skipping replPropertyMetaData update\n"));
744                 return LDB_SUCCESS;
745         }
746
747         unix_to_nt_time(&now, t);
748
749         /* search for the existing replPropertyMetaDataBlob */
750         ret = dsdb_search_dn_with_deleted(ldb, msg, &res, msg->dn, attrs);
751         if (ret != LDB_SUCCESS || res->count != 1) {
752                 DEBUG(0,(__location__ ": Object %s failed to find replPropertyMetaData\n",
753                          ldb_dn_get_linearized(msg->dn)));
754                 return LDB_ERR_OPERATIONS_ERROR;
755         }
756                 
757
758         omd_value = ldb_msg_find_ldb_val(res->msgs[0], "replPropertyMetaData");
759         if (!omd_value) {
760                 DEBUG(0,(__location__ ": Object %s does not have a replPropertyMetaData attribute\n",
761                          ldb_dn_get_linearized(msg->dn)));
762                 return LDB_ERR_OPERATIONS_ERROR;
763         }
764
765         ndr_err = ndr_pull_struct_blob(omd_value, msg,
766                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &omd,
767                                        (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
768         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
769                 DEBUG(0,(__location__ ": Failed to parse replPropertyMetaData for %s\n",
770                          ldb_dn_get_linearized(msg->dn)));
771                 return LDB_ERR_OPERATIONS_ERROR;
772         }
773
774         if (omd.version != 1) {
775                 DEBUG(0,(__location__ ": bad version %u in replPropertyMetaData for %s\n",
776                          omd.version, ldb_dn_get_linearized(msg->dn)));
777                 return LDB_ERR_OPERATIONS_ERROR;
778         }
779
780         for (i=0; i<msg->num_elements; i++) {
781                 ret = replmd_update_rpmd_element(ldb, msg, &msg->elements[i], &omd, schema, seq_num, 
782                                                  our_invocation_id, now);
783                 if (ret != LDB_SUCCESS) {
784                         return ret;
785                 }
786         }
787
788         /*
789          * replmd_update_rpmd_element has done an update if the
790          * seq_num is set
791          */
792         if (*seq_num != 0) {
793                 struct ldb_val *md_value;
794                 struct ldb_message_element *el;
795
796                 md_value = talloc(msg, struct ldb_val);
797                 if (md_value == NULL) {
798                         ldb_oom(ldb);
799                         return LDB_ERR_OPERATIONS_ERROR;
800                 }
801
802                 ret = replmd_replPropertyMetaDataCtr1_sort(&omd.ctr.ctr1, schema, msg->dn);
803                 if (ret != LDB_SUCCESS) {
804                         return ret;
805                 }
806
807                 ndr_err = ndr_push_struct_blob(md_value, msg, 
808                                                lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
809                                                &omd,
810                                                (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
811                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
812                         DEBUG(0,(__location__ ": Failed to marshall replPropertyMetaData for %s\n",
813                                  ldb_dn_get_linearized(msg->dn)));
814                         return LDB_ERR_OPERATIONS_ERROR;
815                 }
816
817                 ret = ldb_msg_add_empty(msg, "replPropertyMetaData", LDB_FLAG_MOD_REPLACE, &el);
818                 if (ret != LDB_SUCCESS) {
819                         DEBUG(0,(__location__ ": Failed to add updated replPropertyMetaData %s\n",
820                                  ldb_dn_get_linearized(msg->dn)));
821                         return ret;
822                 }
823
824                 el->num_values = 1;
825                 el->values = md_value;
826         }
827
828         return LDB_SUCCESS;     
829 }
830
831
832 static int replmd_modify(struct ldb_module *module, struct ldb_request *req)
833 {
834         struct ldb_context *ldb;
835         struct replmd_replicated_request *ac;
836         struct ldb_request *down_req;
837         struct ldb_message *msg;
838         struct ldb_result *res;
839         time_t t = time(NULL);
840         uint64_t seq_num = 0;
841         int ret;
842
843         /* do not manipulate our control entries */
844         if (ldb_dn_is_special(req->op.mod.message->dn)) {
845                 return ldb_next_request(module, req);
846         }
847
848         ldb = ldb_module_get_ctx(module);
849
850         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_modify\n");
851
852         ac = replmd_ctx_init(module, req);
853         if (!ac) {
854                 return LDB_ERR_OPERATIONS_ERROR;
855         }
856
857         /* we have to copy the message as the caller might have it as a const */
858         msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
859         if (msg == NULL) {
860                 ldb_oom(ldb);
861                 talloc_free(ac);
862                 return LDB_ERR_OPERATIONS_ERROR;
863         }
864
865         /* TODO:
866          * - give an error when a readonly attribute should
867          *   be modified
868          * - merge the changed into the old object
869          *   if the caller set values to the same value
870          *   ignore the attribute, return success when no
871          *   attribute was changed
872          */
873
874         ret = dsdb_search_dn_with_deleted(ldb, msg, &res, msg->dn, NULL);
875         if (ret != LDB_SUCCESS) {
876                 talloc_free(ac);
877                 return ret;
878         }
879
880         ret = replmd_update_rpmd(module, ac->schema, msg, &ac->seq_num);
881         if (ret != LDB_SUCCESS) {
882                 talloc_free(ac);
883                 return ret;
884         }
885
886         /* TODO:
887          * - replace the old object with the newly constructed one
888          */
889
890         ret = ldb_build_mod_req(&down_req, ldb, ac,
891                                 msg,
892                                 req->controls,
893                                 ac, replmd_op_callback,
894                                 req);
895         if (ret != LDB_SUCCESS) {
896                 talloc_free(ac);
897                 return ret;
898         }
899         talloc_steal(down_req, msg);
900
901         /* we only change whenChanged and uSNChanged if the seq_num
902            has changed */
903         if (seq_num != 0) {
904                 if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
905                         talloc_free(ac);
906                         return ret;
907                 }
908
909                 if (add_uint64_element(msg, "uSNChanged", seq_num) != LDB_SUCCESS) {
910                         talloc_free(ac);
911                         return ret;
912                 }
913         }
914
915         /* go on with the call chain */
916         return ldb_next_request(module, down_req);
917 }
918
919 static int replmd_rename_callback(struct ldb_request *req, struct ldb_reply *ares);
920
921 /*
922   handle a rename request
923
924   On a rename we need to do an extra ldb_modify which sets the
925   whenChanged and uSNChanged attributes.  We do this in a callback after the success.
926  */
927 static int replmd_rename(struct ldb_module *module, struct ldb_request *req)
928 {
929         struct ldb_context *ldb;
930         struct replmd_replicated_request *ac;
931         int ret;
932         struct ldb_request *down_req;
933
934         /* do not manipulate our control entries */
935         if (ldb_dn_is_special(req->op.mod.message->dn)) {
936                 return ldb_next_request(module, req);
937         }
938
939         ldb = ldb_module_get_ctx(module);
940
941         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_rename\n");
942
943         ac = replmd_ctx_init(module, req);
944         if (!ac) {
945                 return LDB_ERR_OPERATIONS_ERROR;
946         }
947         ret = ldb_build_rename_req(&down_req, ldb, ac,
948                                    ac->req->op.rename.olddn,
949                                    ac->req->op.rename.newdn,
950                                    ac->req->controls,
951                                    ac, replmd_rename_callback,
952                                    ac->req);
953
954         if (ret != LDB_SUCCESS) {
955                 talloc_free(ac);
956                 return ret;
957         }
958
959         /* go on with the call chain */
960         return ldb_next_request(module, down_req);
961 }
962
963 /* After the rename is compleated, update the whenchanged etc */
964 static int replmd_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
965 {
966         struct ldb_context *ldb;
967         struct replmd_replicated_request *ac;
968         struct ldb_request *down_req;
969         struct ldb_message *msg;
970         time_t t = time(NULL);
971         int ret;
972
973         ac = talloc_get_type(req->context, struct replmd_replicated_request);
974         ldb = ldb_module_get_ctx(ac->module);
975
976         if (ares->error != LDB_SUCCESS) {
977                 return ldb_module_done(ac->req, ares->controls,
978                                         ares->response, ares->error);
979         }
980
981         if (ares->type != LDB_REPLY_DONE) {
982                 ldb_set_errstring(ldb,
983                                   "invalid ldb_reply_type in callback");
984                 talloc_free(ares);
985                 return ldb_module_done(ac->req, NULL, NULL,
986                                         LDB_ERR_OPERATIONS_ERROR);
987         }
988
989         /* Get a sequence number from the backend */
990         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &ac->seq_num);
991         if (ret != LDB_SUCCESS) {
992                 return ret;
993         }
994
995         /* TODO:
996          * - replace the old object with the newly constructed one
997          */
998
999         msg = ldb_msg_new(ac);
1000         if (msg == NULL) {
1001                 ldb_oom(ldb);
1002                 return LDB_ERR_OPERATIONS_ERROR;
1003         }
1004
1005         msg->dn = ac->req->op.rename.newdn;
1006
1007         ret = ldb_build_mod_req(&down_req, ldb, ac,
1008                                 msg,
1009                                 req->controls,
1010                                 ac, replmd_op_callback,
1011                                 req);
1012
1013         if (ret != LDB_SUCCESS) {
1014                 talloc_free(ac);
1015                 return ret;
1016         }
1017         talloc_steal(down_req, msg);
1018
1019         if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
1020                 talloc_free(ac);
1021                 return ret;
1022         }
1023         
1024         if (add_uint64_element(msg, "uSNChanged", ac->seq_num) != LDB_SUCCESS) {
1025                 talloc_free(ac);
1026                 return ret;
1027         }
1028
1029         /* go on with the call chain - do the modify after the rename */
1030         return ldb_next_request(ac->module, down_req);
1031 }
1032
1033
1034 static int replmd_replicated_request_error(struct replmd_replicated_request *ar, int ret)
1035 {
1036         return ret;
1037 }
1038
1039 static int replmd_replicated_request_werror(struct replmd_replicated_request *ar, WERROR status)
1040 {
1041         int ret = LDB_ERR_OTHER;
1042         /* TODO: do some error mapping */
1043         return ret;
1044 }
1045
1046 static int replmd_replicated_apply_add(struct replmd_replicated_request *ar)
1047 {
1048         struct ldb_context *ldb;
1049         struct ldb_request *change_req;
1050         enum ndr_err_code ndr_err;
1051         struct ldb_message *msg;
1052         struct replPropertyMetaDataBlob *md;
1053         struct ldb_val md_value;
1054         uint32_t i;
1055         int ret;
1056
1057         /*
1058          * TODO: check if the parent object exist
1059          */
1060
1061         /*
1062          * TODO: handle the conflict case where an object with the
1063          *       same name exist
1064          */
1065
1066         ldb = ldb_module_get_ctx(ar->module);
1067         msg = ar->objs->objects[ar->index_current].msg;
1068         md = ar->objs->objects[ar->index_current].meta_data;
1069
1070         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &ar->seq_num);
1071         if (ret != LDB_SUCCESS) {
1072                 return replmd_replicated_request_error(ar, ret);
1073         }
1074
1075         ret = ldb_msg_add_value(msg, "objectGUID", &ar->objs->objects[ar->index_current].guid_value, NULL);
1076         if (ret != LDB_SUCCESS) {
1077                 return replmd_replicated_request_error(ar, ret);
1078         }
1079
1080         ret = ldb_msg_add_string(msg, "whenChanged", ar->objs->objects[ar->index_current].when_changed);
1081         if (ret != LDB_SUCCESS) {
1082                 return replmd_replicated_request_error(ar, ret);
1083         }
1084
1085         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNCreated", ar->seq_num);
1086         if (ret != LDB_SUCCESS) {
1087                 return replmd_replicated_request_error(ar, ret);
1088         }
1089
1090         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", ar->seq_num);
1091         if (ret != LDB_SUCCESS) {
1092                 return replmd_replicated_request_error(ar, ret);
1093         }
1094
1095         /* remove any message elements that have zero values */
1096         for (i=0; i<msg->num_elements; i++) {
1097                 if (msg->elements[i].num_values == 0) {
1098                         DEBUG(4,(__location__ ": Removing attribute %s with num_values==0\n",
1099                                  msg->elements[i].name));
1100                         memmove(&msg->elements[i], 
1101                                 &msg->elements[i+1], 
1102                                 sizeof(msg->elements[i])*(msg->num_elements - (i+1)));
1103                         msg->num_elements--;
1104                         i--;
1105                 }
1106         }
1107         
1108         /*
1109          * the meta data array is already sorted by the caller
1110          */
1111         for (i=0; i < md->ctr.ctr1.count; i++) {
1112                 md->ctr.ctr1.array[i].local_usn = ar->seq_num;
1113         }
1114         ndr_err = ndr_push_struct_blob(&md_value, msg, 
1115                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1116                                        md,
1117                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
1118         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1119                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1120                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1121         }
1122         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &md_value, NULL);
1123         if (ret != LDB_SUCCESS) {
1124                 return replmd_replicated_request_error(ar, ret);
1125         }
1126
1127         replmd_ldb_message_sort(msg, ar->schema);
1128
1129         if (DEBUGLVL(4)) {
1130                 char *s = ldb_ldif_message_string(ldb, ar, LDB_CHANGETYPE_ADD, msg);
1131                 DEBUG(4, ("DRS replication add message:\n%s\n", s));
1132                 talloc_free(s);
1133         }
1134
1135         ret = ldb_build_add_req(&change_req,
1136                                 ldb,
1137                                 ar,
1138                                 msg,
1139                                 ar->controls,
1140                                 ar,
1141                                 replmd_op_callback,
1142                                 ar->req);
1143         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1144
1145         return ldb_next_request(ar->module, change_req);
1146 }
1147
1148 static int replmd_replPropertyMetaData1_conflict_compare(struct replPropertyMetaData1 *m1,
1149                                                          struct replPropertyMetaData1 *m2)
1150 {
1151         int ret;
1152
1153         if (m1->version != m2->version) {
1154                 return m1->version - m2->version;
1155         }
1156
1157         if (m1->originating_change_time != m2->originating_change_time) {
1158                 return m1->originating_change_time - m2->originating_change_time;
1159         }
1160
1161         ret = GUID_compare(&m1->originating_invocation_id, &m2->originating_invocation_id);
1162         if (ret != 0) {
1163                 return ret;
1164         }
1165
1166         return m1->originating_usn - m2->originating_usn;
1167 }
1168
1169 static int replmd_replicated_apply_merge(struct replmd_replicated_request *ar)
1170 {
1171         struct ldb_context *ldb;
1172         struct ldb_request *change_req;
1173         enum ndr_err_code ndr_err;
1174         struct ldb_message *msg;
1175         struct replPropertyMetaDataBlob *rmd;
1176         struct replPropertyMetaDataBlob omd;
1177         const struct ldb_val *omd_value;
1178         struct replPropertyMetaDataBlob nmd;
1179         struct ldb_val nmd_value;
1180         uint32_t i,j,ni=0;
1181         uint32_t removed_attrs = 0;
1182         int ret;
1183
1184         ldb = ldb_module_get_ctx(ar->module);
1185         msg = ar->objs->objects[ar->index_current].msg;
1186         rmd = ar->objs->objects[ar->index_current].meta_data;
1187         ZERO_STRUCT(omd);
1188         omd.version = 1;
1189
1190         /*
1191          * TODO: check repl data is correct after a rename
1192          */
1193         if (ldb_dn_compare(msg->dn, ar->search_msg->dn) != 0) {
1194                 ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_request rename %s => %s\n",
1195                           ldb_dn_get_linearized(ar->search_msg->dn),
1196                           ldb_dn_get_linearized(msg->dn));
1197                 if (ldb_rename(ldb, ar->search_msg->dn, msg->dn) != LDB_SUCCESS) {
1198                         ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_replicated_request rename %s => %s failed - %s\n",
1199                                   ldb_dn_get_linearized(ar->search_msg->dn),
1200                                   ldb_dn_get_linearized(msg->dn),
1201                                   ldb_errstring(ldb));
1202                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_DB_ERROR);
1203                 }
1204         }
1205
1206         /* find existing meta data */
1207         omd_value = ldb_msg_find_ldb_val(ar->search_msg, "replPropertyMetaData");
1208         if (omd_value) {
1209                 ndr_err = ndr_pull_struct_blob(omd_value, ar,
1210                                                lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &omd,
1211                                                (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
1212                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1213                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1214                         return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1215                 }
1216
1217                 if (omd.version != 1) {
1218                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
1219                 }
1220         }
1221
1222         ZERO_STRUCT(nmd);
1223         nmd.version = 1;
1224         nmd.ctr.ctr1.count = omd.ctr.ctr1.count + rmd->ctr.ctr1.count;
1225         nmd.ctr.ctr1.array = talloc_array(ar,
1226                                           struct replPropertyMetaData1,
1227                                           nmd.ctr.ctr1.count);
1228         if (!nmd.ctr.ctr1.array) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1229
1230         /* first copy the old meta data */
1231         for (i=0; i < omd.ctr.ctr1.count; i++) {
1232                 nmd.ctr.ctr1.array[ni]  = omd.ctr.ctr1.array[i];
1233                 ni++;
1234         }
1235
1236         /* now merge in the new meta data */
1237         for (i=0; i < rmd->ctr.ctr1.count; i++) {
1238                 bool found = false;
1239
1240                 for (j=0; j < ni; j++) {
1241                         int cmp;
1242
1243                         if (rmd->ctr.ctr1.array[i].attid != nmd.ctr.ctr1.array[j].attid) {
1244                                 continue;
1245                         }
1246
1247                         cmp = replmd_replPropertyMetaData1_conflict_compare(&rmd->ctr.ctr1.array[i],
1248                                                                             &nmd.ctr.ctr1.array[j]);
1249                         if (cmp > 0) {
1250                                 /* replace the entry */
1251                                 nmd.ctr.ctr1.array[j] = rmd->ctr.ctr1.array[i];
1252                                 found = true;
1253                                 break;
1254                         }
1255
1256                         /* we don't want to apply this change so remove the attribute */
1257                         ldb_msg_remove_element(msg, &msg->elements[i-removed_attrs]);
1258                         removed_attrs++;
1259
1260                         found = true;
1261                         break;
1262                 }
1263
1264                 if (found) continue;
1265
1266                 nmd.ctr.ctr1.array[ni] = rmd->ctr.ctr1.array[i];
1267                 ni++;
1268         }
1269
1270         /*
1271          * finally correct the size of the meta_data array
1272          */
1273         nmd.ctr.ctr1.count = ni;
1274
1275         /*
1276          * the rdn attribute (the alias for the name attribute),
1277          * 'cn' for most objects is the last entry in the meta data array
1278          * we have stored
1279          *
1280          * sort the new meta data array
1281          */
1282         ret = replmd_replPropertyMetaDataCtr1_sort(&nmd.ctr.ctr1, ar->schema, msg->dn);
1283         if (ret != LDB_SUCCESS) {
1284                 return ret;
1285         }
1286
1287         /*
1288          * check if some replicated attributes left, otherwise skip the ldb_modify() call
1289          */
1290         if (msg->num_elements == 0) {
1291                 ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_apply_merge[%u]: skip replace\n",
1292                           ar->index_current);
1293
1294                 ar->index_current++;
1295                 return replmd_replicated_apply_next(ar);
1296         }
1297
1298         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_apply_merge[%u]: replace %u attributes\n",
1299                   ar->index_current, msg->num_elements);
1300
1301         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &ar->seq_num);
1302         if (ret != LDB_SUCCESS) {
1303                 return replmd_replicated_request_error(ar, ret);
1304         }
1305
1306         for (i=0; i<ni; i++) {
1307                 nmd.ctr.ctr1.array[i].local_usn = ar->seq_num;
1308         }
1309
1310         /* create the meta data value */
1311         ndr_err = ndr_push_struct_blob(&nmd_value, msg, 
1312                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1313                                        &nmd,
1314                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
1315         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1316                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1317                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1318         }
1319
1320         /*
1321          * when we know that we'll modify the record, add the whenChanged, uSNChanged
1322          * and replPopertyMetaData attributes
1323          */
1324         ret = ldb_msg_add_string(msg, "whenChanged", ar->objs->objects[ar->index_current].when_changed);
1325         if (ret != LDB_SUCCESS) {
1326                 return replmd_replicated_request_error(ar, ret);
1327         }
1328         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", ar->seq_num);
1329         if (ret != LDB_SUCCESS) {
1330                 return replmd_replicated_request_error(ar, ret);
1331         }
1332         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &nmd_value, NULL);
1333         if (ret != LDB_SUCCESS) {
1334                 return replmd_replicated_request_error(ar, ret);
1335         }
1336
1337         replmd_ldb_message_sort(msg, ar->schema);
1338
1339         /* we want to replace the old values */
1340         for (i=0; i < msg->num_elements; i++) {
1341                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1342         }
1343
1344         if (DEBUGLVL(4)) {
1345                 char *s = ldb_ldif_message_string(ldb, ar, LDB_CHANGETYPE_MODIFY, msg);
1346                 DEBUG(4, ("DRS replication modify message:\n%s\n", s));
1347                 talloc_free(s);
1348         }
1349
1350         ret = ldb_build_mod_req(&change_req,
1351                                 ldb,
1352                                 ar,
1353                                 msg,
1354                                 ar->controls,
1355                                 ar,
1356                                 replmd_op_callback,
1357                                 ar->req);
1358         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1359
1360         return ldb_next_request(ar->module, change_req);
1361 }
1362
1363 static int replmd_replicated_apply_search_callback(struct ldb_request *req,
1364                                                    struct ldb_reply *ares)
1365 {
1366         struct replmd_replicated_request *ar = talloc_get_type(req->context,
1367                                                struct replmd_replicated_request);
1368         int ret;
1369
1370         if (!ares) {
1371                 return ldb_module_done(ar->req, NULL, NULL,
1372                                         LDB_ERR_OPERATIONS_ERROR);
1373         }
1374         if (ares->error != LDB_SUCCESS &&
1375             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
1376                 return ldb_module_done(ar->req, ares->controls,
1377                                         ares->response, ares->error);
1378         }
1379
1380         switch (ares->type) {
1381         case LDB_REPLY_ENTRY:
1382                 ar->search_msg = talloc_steal(ar, ares->message);
1383                 break;
1384
1385         case LDB_REPLY_REFERRAL:
1386                 /* we ignore referrals */
1387                 break;
1388
1389         case LDB_REPLY_DONE:
1390                 if (ar->search_msg != NULL) {
1391                         ret = replmd_replicated_apply_merge(ar);
1392                 } else {
1393                         ret = replmd_replicated_apply_add(ar);
1394                 }
1395                 if (ret != LDB_SUCCESS) {
1396                         return ldb_module_done(ar->req, NULL, NULL, ret);
1397                 }
1398         }
1399
1400         talloc_free(ares);
1401         return LDB_SUCCESS;
1402 }
1403
1404 static int replmd_replicated_uptodate_vector(struct replmd_replicated_request *ar);
1405
1406 static int replmd_replicated_apply_next(struct replmd_replicated_request *ar)
1407 {
1408         struct ldb_context *ldb;
1409         int ret;
1410         char *tmp_str;
1411         char *filter;
1412         struct ldb_request *search_req;
1413         struct ldb_search_options_control *options;
1414
1415         if (ar->index_current >= ar->objs->num_objects) {
1416                 /* done with it, go to next stage */
1417                 return replmd_replicated_uptodate_vector(ar);
1418         }
1419
1420         ldb = ldb_module_get_ctx(ar->module);
1421         ar->search_msg = NULL;
1422
1423         tmp_str = ldb_binary_encode(ar, ar->objs->objects[ar->index_current].guid_value);
1424         if (!tmp_str) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1425
1426         filter = talloc_asprintf(ar, "(objectGUID=%s)", tmp_str);
1427         if (!filter) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1428         talloc_free(tmp_str);
1429
1430         ret = ldb_build_search_req(&search_req,
1431                                    ldb,
1432                                    ar,
1433                                    NULL,
1434                                    LDB_SCOPE_SUBTREE,
1435                                    filter,
1436                                    NULL,
1437                                    NULL,
1438                                    ar,
1439                                    replmd_replicated_apply_search_callback,
1440                                    ar->req);
1441
1442         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
1443         if (ret != LDB_SUCCESS) {
1444                 return ret;
1445         }
1446
1447         /* we need to cope with cross-partition links, so search for
1448            the GUID over all partitions */
1449         options = talloc(search_req, struct ldb_search_options_control);
1450         if (options == NULL) {
1451                 DEBUG(0, (__location__ ": out of memory\n"));
1452                 return LDB_ERR_OPERATIONS_ERROR;
1453         }
1454         options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
1455
1456         ret = ldb_request_add_control(search_req,
1457                                       LDB_CONTROL_SEARCH_OPTIONS_OID,
1458                                       true, options);
1459         if (ret != LDB_SUCCESS) {
1460                 return ret;
1461         }
1462
1463         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1464
1465         return ldb_next_request(ar->module, search_req);
1466 }
1467
1468 static int replmd_replicated_uptodate_modify_callback(struct ldb_request *req,
1469                                                       struct ldb_reply *ares)
1470 {
1471         struct ldb_context *ldb;
1472         struct replmd_replicated_request *ar = talloc_get_type(req->context,
1473                                                struct replmd_replicated_request);
1474         ldb = ldb_module_get_ctx(ar->module);
1475
1476         if (!ares) {
1477                 return ldb_module_done(ar->req, NULL, NULL,
1478                                         LDB_ERR_OPERATIONS_ERROR);
1479         }
1480         if (ares->error != LDB_SUCCESS) {
1481                 return ldb_module_done(ar->req, ares->controls,
1482                                         ares->response, ares->error);
1483         }
1484
1485         if (ares->type != LDB_REPLY_DONE) {
1486                 ldb_set_errstring(ldb, "Invalid reply type\n!");
1487                 return ldb_module_done(ar->req, NULL, NULL,
1488                                         LDB_ERR_OPERATIONS_ERROR);
1489         }
1490
1491         talloc_free(ares);
1492
1493         return ldb_module_done(ar->req, NULL, NULL, LDB_SUCCESS);
1494 }
1495
1496 static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *ar)
1497 {
1498         struct ldb_context *ldb;
1499         struct ldb_request *change_req;
1500         enum ndr_err_code ndr_err;
1501         struct ldb_message *msg;
1502         struct replUpToDateVectorBlob ouv;
1503         const struct ldb_val *ouv_value;
1504         const struct drsuapi_DsReplicaCursor2CtrEx *ruv;
1505         struct replUpToDateVectorBlob nuv;
1506         struct ldb_val nuv_value;
1507         struct ldb_message_element *nuv_el = NULL;
1508         const struct GUID *our_invocation_id;
1509         struct ldb_message_element *orf_el = NULL;
1510         struct repsFromToBlob nrf;
1511         struct ldb_val *nrf_value = NULL;
1512         struct ldb_message_element *nrf_el = NULL;
1513         uint32_t i,j,ni=0;
1514         bool found = false;
1515         time_t t = time(NULL);
1516         NTTIME now;
1517         int ret;
1518
1519         ldb = ldb_module_get_ctx(ar->module);
1520         ruv = ar->objs->uptodateness_vector;
1521         ZERO_STRUCT(ouv);
1522         ouv.version = 2;
1523         ZERO_STRUCT(nuv);
1524         nuv.version = 2;
1525
1526         unix_to_nt_time(&now, t);
1527
1528         /*
1529          * first create the new replUpToDateVector
1530          */
1531         ouv_value = ldb_msg_find_ldb_val(ar->search_msg, "replUpToDateVector");
1532         if (ouv_value) {
1533                 ndr_err = ndr_pull_struct_blob(ouv_value, ar,
1534                                                lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &ouv,
1535                                                (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
1536                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1537                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1538                         return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1539                 }
1540
1541                 if (ouv.version != 2) {
1542                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
1543                 }
1544         }
1545
1546         /*
1547          * the new uptodateness vector will at least
1548          * contain 1 entry, one for the source_dsa
1549          *
1550          * plus optional values from our old vector and the one from the source_dsa
1551          */
1552         nuv.ctr.ctr2.count = 1 + ouv.ctr.ctr2.count;
1553         if (ruv) nuv.ctr.ctr2.count += ruv->count;
1554         nuv.ctr.ctr2.cursors = talloc_array(ar,
1555                                             struct drsuapi_DsReplicaCursor2,
1556                                             nuv.ctr.ctr2.count);
1557         if (!nuv.ctr.ctr2.cursors) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1558
1559         /* first copy the old vector */
1560         for (i=0; i < ouv.ctr.ctr2.count; i++) {
1561                 nuv.ctr.ctr2.cursors[ni] = ouv.ctr.ctr2.cursors[i];
1562                 ni++;
1563         }
1564
1565         /* get our invocation_id if we have one already attached to the ldb */
1566         our_invocation_id = samdb_ntds_invocation_id(ldb);
1567
1568         /* merge in the source_dsa vector is available */
1569         for (i=0; (ruv && i < ruv->count); i++) {
1570                 found = false;
1571
1572                 if (our_invocation_id &&
1573                     GUID_equal(&ruv->cursors[i].source_dsa_invocation_id,
1574                                our_invocation_id)) {
1575                         continue;
1576                 }
1577
1578                 for (j=0; j < ni; j++) {
1579                         if (!GUID_equal(&ruv->cursors[i].source_dsa_invocation_id,
1580                                         &nuv.ctr.ctr2.cursors[j].source_dsa_invocation_id)) {
1581                                 continue;
1582                         }
1583
1584                         found = true;
1585
1586                         /*
1587                          * we update only the highest_usn and not the latest_sync_success time,
1588                          * because the last success stands for direct replication
1589                          */
1590                         if (ruv->cursors[i].highest_usn > nuv.ctr.ctr2.cursors[j].highest_usn) {
1591                                 nuv.ctr.ctr2.cursors[j].highest_usn = ruv->cursors[i].highest_usn;
1592                         }
1593                         break;                  
1594                 }
1595
1596                 if (found) continue;
1597
1598                 /* if it's not there yet, add it */
1599                 nuv.ctr.ctr2.cursors[ni] = ruv->cursors[i];
1600                 ni++;
1601         }
1602
1603         /*
1604          * merge in the current highwatermark for the source_dsa
1605          */
1606         found = false;
1607         for (j=0; j < ni; j++) {
1608                 if (!GUID_equal(&ar->objs->source_dsa->source_dsa_invocation_id,
1609                                 &nuv.ctr.ctr2.cursors[j].source_dsa_invocation_id)) {
1610                         continue;
1611                 }
1612
1613                 found = true;
1614
1615                 /*
1616                  * here we update the highest_usn and last_sync_success time
1617                  * because we're directly replicating from the source_dsa
1618                  *
1619                  * and use the tmp_highest_usn because this is what we have just applied
1620                  * to our ldb
1621                  */
1622                 nuv.ctr.ctr2.cursors[j].highest_usn             = ar->objs->source_dsa->highwatermark.tmp_highest_usn;
1623                 nuv.ctr.ctr2.cursors[j].last_sync_success       = now;
1624                 break;
1625         }
1626         if (!found) {
1627                 /*
1628                  * here we update the highest_usn and last_sync_success time
1629                  * because we're directly replicating from the source_dsa
1630                  *
1631                  * and use the tmp_highest_usn because this is what we have just applied
1632                  * to our ldb
1633                  */
1634                 nuv.ctr.ctr2.cursors[ni].source_dsa_invocation_id= ar->objs->source_dsa->source_dsa_invocation_id;
1635                 nuv.ctr.ctr2.cursors[ni].highest_usn            = ar->objs->source_dsa->highwatermark.tmp_highest_usn;
1636                 nuv.ctr.ctr2.cursors[ni].last_sync_success      = now;
1637                 ni++;
1638         }
1639
1640         /*
1641          * finally correct the size of the cursors array
1642          */
1643         nuv.ctr.ctr2.count = ni;
1644
1645         /*
1646          * sort the cursors
1647          */
1648         qsort(nuv.ctr.ctr2.cursors, nuv.ctr.ctr2.count,
1649               sizeof(struct drsuapi_DsReplicaCursor2),
1650               (comparison_fn_t)drsuapi_DsReplicaCursor2_compare);
1651
1652         /*
1653          * create the change ldb_message
1654          */
1655         msg = ldb_msg_new(ar);
1656         if (!msg) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1657         msg->dn = ar->search_msg->dn;
1658
1659         ndr_err = ndr_push_struct_blob(&nuv_value, msg, 
1660                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
1661                                        &nuv,
1662                                        (ndr_push_flags_fn_t)ndr_push_replUpToDateVectorBlob);
1663         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1664                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1665                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1666         }
1667         ret = ldb_msg_add_value(msg, "replUpToDateVector", &nuv_value, &nuv_el);
1668         if (ret != LDB_SUCCESS) {
1669                 return replmd_replicated_request_error(ar, ret);
1670         }
1671         nuv_el->flags = LDB_FLAG_MOD_REPLACE;
1672
1673         /*
1674          * now create the new repsFrom value from the given repsFromTo1 structure
1675          */
1676         ZERO_STRUCT(nrf);
1677         nrf.version                                     = 1;
1678         nrf.ctr.ctr1                                    = *ar->objs->source_dsa;
1679         /* and fix some values... */
1680         nrf.ctr.ctr1.consecutive_sync_failures          = 0;
1681         nrf.ctr.ctr1.last_success                       = now;
1682         nrf.ctr.ctr1.last_attempt                       = now;
1683         nrf.ctr.ctr1.result_last_attempt                = WERR_OK;
1684         nrf.ctr.ctr1.highwatermark.highest_usn          = nrf.ctr.ctr1.highwatermark.tmp_highest_usn;
1685
1686         /*
1687          * first see if we already have a repsFrom value for the current source dsa
1688          * if so we'll later replace this value
1689          */
1690         orf_el = ldb_msg_find_element(ar->search_msg, "repsFrom");
1691         if (orf_el) {
1692                 for (i=0; i < orf_el->num_values; i++) {
1693                         struct repsFromToBlob *trf;
1694
1695                         trf = talloc(ar, struct repsFromToBlob);
1696                         if (!trf) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1697
1698                         ndr_err = ndr_pull_struct_blob(&orf_el->values[i], trf, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), trf,
1699                                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
1700                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1701                                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1702                                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1703                         }
1704
1705                         if (trf->version != 1) {
1706                                 return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
1707                         }
1708
1709                         /*
1710                          * we compare the source dsa objectGUID not the invocation_id
1711                          * because we want only one repsFrom value per source dsa
1712                          * and when the invocation_id of the source dsa has changed we don't need 
1713                          * the old repsFrom with the old invocation_id
1714                          */
1715                         if (!GUID_equal(&trf->ctr.ctr1.source_dsa_obj_guid,
1716                                         &ar->objs->source_dsa->source_dsa_obj_guid)) {
1717                                 talloc_free(trf);
1718                                 continue;
1719                         }
1720
1721                         talloc_free(trf);
1722                         nrf_value = &orf_el->values[i];
1723                         break;
1724                 }
1725
1726                 /*
1727                  * copy over all old values to the new ldb_message
1728                  */
1729                 ret = ldb_msg_add_empty(msg, "repsFrom", 0, &nrf_el);
1730                 if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1731                 *nrf_el = *orf_el;
1732         }
1733
1734         /*
1735          * if we haven't found an old repsFrom value for the current source dsa
1736          * we'll add a new value
1737          */
1738         if (!nrf_value) {
1739                 struct ldb_val zero_value;
1740                 ZERO_STRUCT(zero_value);
1741                 ret = ldb_msg_add_value(msg, "repsFrom", &zero_value, &nrf_el);
1742                 if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1743
1744                 nrf_value = &nrf_el->values[nrf_el->num_values - 1];
1745         }
1746
1747         /* we now fill the value which is already attached to ldb_message */
1748         ndr_err = ndr_push_struct_blob(nrf_value, msg, 
1749                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1750                                        &nrf,
1751                                        (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
1752         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1753                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
1754                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
1755         }
1756
1757         /* 
1758          * the ldb_message_element for the attribute, has all the old values and the new one
1759          * so we'll replace the whole attribute with all values
1760          */
1761         nrf_el->flags = LDB_FLAG_MOD_REPLACE;
1762
1763         if (DEBUGLVL(4)) {
1764                 char *s = ldb_ldif_message_string(ldb, ar, LDB_CHANGETYPE_MODIFY, msg);
1765                 DEBUG(4, ("DRS replication uptodate modify message:\n%s\n", s));
1766                 talloc_free(s);
1767         }
1768
1769         /* prepare the ldb_modify() request */
1770         ret = ldb_build_mod_req(&change_req,
1771                                 ldb,
1772                                 ar,
1773                                 msg,
1774                                 ar->controls,
1775                                 ar,
1776                                 replmd_replicated_uptodate_modify_callback,
1777                                 ar->req);
1778         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1779
1780         return ldb_next_request(ar->module, change_req);
1781 }
1782
1783 static int replmd_replicated_uptodate_search_callback(struct ldb_request *req,
1784                                                       struct ldb_reply *ares)
1785 {
1786         struct replmd_replicated_request *ar = talloc_get_type(req->context,
1787                                                struct replmd_replicated_request);
1788         int ret;
1789
1790         if (!ares) {
1791                 return ldb_module_done(ar->req, NULL, NULL,
1792                                         LDB_ERR_OPERATIONS_ERROR);
1793         }
1794         if (ares->error != LDB_SUCCESS &&
1795             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
1796                 return ldb_module_done(ar->req, ares->controls,
1797                                         ares->response, ares->error);
1798         }
1799
1800         switch (ares->type) {
1801         case LDB_REPLY_ENTRY:
1802                 ar->search_msg = talloc_steal(ar, ares->message);
1803                 break;
1804
1805         case LDB_REPLY_REFERRAL:
1806                 /* we ignore referrals */
1807                 break;
1808
1809         case LDB_REPLY_DONE:
1810                 if (ar->search_msg == NULL) {
1811                         ret = replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
1812                 } else {
1813                         ret = replmd_replicated_uptodate_modify(ar);
1814                 }
1815                 if (ret != LDB_SUCCESS) {
1816                         return ldb_module_done(ar->req, NULL, NULL, ret);
1817                 }
1818         }
1819
1820         talloc_free(ares);
1821         return LDB_SUCCESS;
1822 }
1823
1824
1825 static int replmd_replicated_uptodate_vector(struct replmd_replicated_request *ar)
1826 {
1827         struct ldb_context *ldb;
1828         int ret;
1829         static const char *attrs[] = {
1830                 "replUpToDateVector",
1831                 "repsFrom",
1832                 NULL
1833         };
1834         struct ldb_request *search_req;
1835
1836         ldb = ldb_module_get_ctx(ar->module);
1837         ar->search_msg = NULL;
1838
1839         ret = ldb_build_search_req(&search_req,
1840                                    ldb,
1841                                    ar,
1842                                    ar->objs->partition_dn,
1843                                    LDB_SCOPE_BASE,
1844                                    "(objectClass=*)",
1845                                    attrs,
1846                                    NULL,
1847                                    ar,
1848                                    replmd_replicated_uptodate_search_callback,
1849                                    ar->req);
1850         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
1851
1852         return ldb_next_request(ar->module, search_req);
1853 }
1854
1855
1856
1857 static int replmd_extended_replicated_objects(struct ldb_module *module, struct ldb_request *req)
1858 {
1859         struct ldb_context *ldb;
1860         struct dsdb_extended_replicated_objects *objs;
1861         struct replmd_replicated_request *ar;
1862         struct ldb_control **ctrls;
1863         int ret, i;
1864         struct replmd_private *replmd_private = 
1865                 talloc_get_type(ldb_module_get_private(module), struct replmd_private);
1866
1867         ldb = ldb_module_get_ctx(module);
1868
1869         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_extended_replicated_objects\n");
1870
1871         objs = talloc_get_type(req->op.extended.data, struct dsdb_extended_replicated_objects);
1872         if (!objs) {
1873                 ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_extended_replicated_objects: invalid extended data\n");
1874                 return LDB_ERR_PROTOCOL_ERROR;
1875         }
1876
1877         if (objs->version != DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION) {
1878                 ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_extended_replicated_objects: extended data invalid version [%u != %u]\n",
1879                           objs->version, DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION);
1880                 return LDB_ERR_PROTOCOL_ERROR;
1881         }
1882
1883         ar = replmd_ctx_init(module, req);
1884         if (!ar)
1885                 return LDB_ERR_OPERATIONS_ERROR;
1886
1887         /* Set the flags to have the replmd_op_callback run over the full set of objects */
1888         ar->apply_mode = true;
1889         ar->objs = objs;
1890         ar->schema = dsdb_get_schema(ldb);
1891         if (!ar->schema) {
1892                 ldb_debug_set(ldb, LDB_DEBUG_FATAL, "replmd_ctx_init: no loaded schema found\n");
1893                 talloc_free(ar);
1894                 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1895                 return LDB_ERR_CONSTRAINT_VIOLATION;
1896         }
1897
1898         ctrls = req->controls;
1899
1900         if (req->controls) {
1901                 req->controls = talloc_memdup(ar, req->controls,
1902                                               talloc_get_size(req->controls));
1903                 if (!req->controls) return replmd_replicated_request_werror(ar, WERR_NOMEM);
1904         }
1905
1906         ret = ldb_request_add_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID, false, NULL);
1907         if (ret != LDB_SUCCESS) {
1908                 return ret;
1909         }
1910
1911         ar->controls = req->controls;
1912         req->controls = ctrls;
1913
1914         DEBUG(4,("linked_attributes_count=%u\n", objs->linked_attributes_count));
1915
1916         /* save away the linked attributes for the end of the
1917            transaction */
1918         for (i=0; i<ar->objs->linked_attributes_count; i++) {
1919                 struct la_entry *la_entry;
1920
1921                 if (replmd_private->la_ctx == NULL) {
1922                         replmd_private->la_ctx = talloc_new(replmd_private);
1923                 }
1924                 la_entry = talloc(replmd_private->la_ctx, struct la_entry);
1925                 if (la_entry == NULL) {
1926                         ldb_oom(ldb);
1927                         return LDB_ERR_OPERATIONS_ERROR;
1928                 }
1929                 la_entry->la = talloc(la_entry, struct drsuapi_DsReplicaLinkedAttribute);
1930                 if (la_entry->la == NULL) {
1931                         talloc_free(la_entry);
1932                         ldb_oom(ldb);
1933                         return LDB_ERR_OPERATIONS_ERROR;
1934                 }
1935                 *la_entry->la = ar->objs->linked_attributes[i];
1936
1937                 /* we need to steal the non-scalars so they stay
1938                    around until the end of the transaction */
1939                 talloc_steal(la_entry->la, la_entry->la->identifier);
1940                 talloc_steal(la_entry->la, la_entry->la->value.blob);
1941
1942                 DLIST_ADD(replmd_private->la_list, la_entry);
1943         }
1944
1945         return replmd_replicated_apply_next(ar);
1946 }
1947
1948 /*
1949   process one linked attribute structure
1950  */
1951 static int replmd_process_linked_attribute(struct ldb_module *module,
1952                                            struct la_entry *la_entry)
1953 {                                          
1954         struct drsuapi_DsReplicaLinkedAttribute *la = la_entry->la;
1955         struct ldb_context *ldb = ldb_module_get_ctx(module);
1956         struct dsdb_schema *schema = dsdb_get_schema(ldb);
1957         struct drsuapi_DsReplicaObjectIdentifier3 target;
1958         struct ldb_message *msg;
1959         TALLOC_CTX *tmp_ctx = talloc_new(la_entry);
1960         struct ldb_request *mod_req;
1961         int ret;
1962         const struct dsdb_attribute *attr;
1963         struct ldb_dn *target_dn;
1964         struct dsdb_dn *dsdb_dn;
1965         uint64_t seq_num = 0;
1966         struct drsuapi_DsReplicaAttribute drs;
1967         struct drsuapi_DsAttributeValue val;
1968         struct ldb_message_element el;
1969         const struct ldb_val *guid;
1970         WERROR status;
1971
1972         drs.value_ctr.num_values = 1;
1973         drs.value_ctr.values = &val;
1974         val.blob = la->value.blob;
1975
1976 /*
1977 linked_attributes[0]:                                                     
1978      &objs->linked_attributes[i]: struct drsuapi_DsReplicaLinkedAttribute 
1979         identifier               : *                                      
1980             identifier: struct drsuapi_DsReplicaObjectIdentifier          
1981                 __ndr_size               : 0x0000003a (58)                
1982                 __ndr_size_sid           : 0x00000000 (0)                 
1983                 guid                     : 8e95b6a9-13dd-4158-89db-3220a5be5cc7
1984                 sid                      : S-0-0                               
1985                 __ndr_size_dn            : 0x00000000 (0)                      
1986                 dn                       : ''                                  
1987         attid                    : DRSUAPI_ATTRIBUTE_member (0x1F)             
1988         value: struct drsuapi_DsAttributeValue                                 
1989             __ndr_size               : 0x0000007e (126)                        
1990             blob                     : *                                       
1991                 blob                     : DATA_BLOB length=126                
1992         flags                    : 0x00000001 (1)                              
1993                1: DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE                      
1994         originating_add_time     : Wed Sep  2 22:20:01 2009 EST                
1995         meta_data: struct drsuapi_DsReplicaMetaData                            
1996             version                  : 0x00000015 (21)                         
1997             originating_change_time  : Wed Sep  2 23:39:07 2009 EST            
1998             originating_invocation_id: 794640f3-18cf-40ee-a211-a93992b67a64    
1999             originating_usn          : 0x000000000001e19c (123292)             
2000
2001 (for cases where the link is to a normal DN)
2002      &target: struct drsuapi_DsReplicaObjectIdentifier3                        
2003         __ndr_size               : 0x0000007e (126)                            
2004         __ndr_size_sid           : 0x0000001c (28)                             
2005         guid                     : 7639e594-db75-4086-b0d4-67890ae46031        
2006         sid                      : S-1-5-21-2848215498-2472035911-1947525656-19924
2007         __ndr_size_dn            : 0x00000022 (34)                                
2008         dn                       : 'CN=UOne,OU=TestOU,DC=vsofs8,DC=com'           
2009  */
2010         
2011         /* find the attribute being modified */
2012         attr = dsdb_attribute_by_attributeID_id(schema, la->attid);
2013         if (attr == NULL) {
2014                 DEBUG(0, (__location__ ": Unable to find attributeID 0x%x\n", la->attid));
2015                 talloc_free(tmp_ctx);
2016                 return LDB_ERR_OPERATIONS_ERROR;
2017         }
2018
2019         status = attr->syntax->drsuapi_to_ldb(ldb, schema, attr, &drs, tmp_ctx, &el);
2020
2021         /* construct a modify request for this attribute change */
2022         msg = ldb_msg_new(tmp_ctx);
2023         if (!msg) {
2024                 ldb_oom(ldb);
2025                 talloc_free(tmp_ctx);
2026                 return LDB_ERR_OPERATIONS_ERROR;
2027         }
2028
2029         ret = dsdb_find_dn_by_guid(ldb, tmp_ctx, 
2030                                    GUID_string(tmp_ctx, &la->identifier->guid), &msg->dn);
2031         if (ret != LDB_SUCCESS) {
2032                 talloc_free(tmp_ctx);
2033                 return ret;
2034         }
2035
2036         el.name = attr->lDAPDisplayName;
2037         if (la->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) {
2038                 ret = ldb_msg_add(msg, &el, LDB_FLAG_MOD_ADD);
2039         } else {
2040                 ret = ldb_msg_add(msg, &el, LDB_FLAG_MOD_DELETE);
2041         }
2042         if (ret != LDB_SUCCESS) {
2043                 talloc_free(tmp_ctx);
2044                 return ret;
2045         }
2046
2047         dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, &el.values[0], attr->syntax->ldap_oid);
2048         if (!dsdb_dn) {
2049                 DEBUG(0,(__location__ ": Failed to parse just-generated DN\n"));
2050                 talloc_free(tmp_ctx);
2051                 return LDB_ERR_INVALID_DN_SYNTAX;
2052         }
2053
2054         guid = ldb_dn_get_extended_component(dsdb_dn->dn, "GUID");
2055         if (!guid) {
2056                 DEBUG(0,(__location__ ": Failed to parse GUID from just-generated DN\n"));
2057                 talloc_free(tmp_ctx);
2058                 return LDB_ERR_INVALID_DN_SYNTAX;
2059         }
2060
2061         ret = dsdb_find_dn_by_guid(ldb, tmp_ctx, ldb_binary_encode(tmp_ctx, *guid), &target_dn);
2062         if (ret != LDB_SUCCESS) {
2063                 /* If this proves to be a problem in the future, then
2064                  * just remove the return - perhaps we can just use
2065                  * the details the replication peer supplied */
2066
2067                 DEBUG(0,(__location__ ": Failed to map GUID %s to DN\n", GUID_string(tmp_ctx, &target.guid)));
2068                 talloc_free(tmp_ctx);
2069                 return LDB_ERR_OPERATIONS_ERROR;
2070         } else {
2071
2072                 /* Now update with full DN we just found in the DB (including extended components) */
2073                 dsdb_dn->dn = target_dn;
2074                 /* Now make a linearized version, using the original binary components (if any) */
2075                 el.values[0] = data_blob_string_const(dsdb_dn_get_extended_linearized(tmp_ctx, dsdb_dn, 1));
2076         }
2077
2078         ret = replmd_update_rpmd(module, schema, msg, &seq_num);
2079         if (ret != LDB_SUCCESS) {
2080                 talloc_free(tmp_ctx);
2081                 return ret;
2082         }
2083
2084         /* we only change whenChanged and uSNChanged if the seq_num
2085            has changed */
2086         if (seq_num != 0) {
2087                 time_t t = time(NULL);
2088
2089                 if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
2090                         talloc_free(tmp_ctx);
2091                         return LDB_ERR_OPERATIONS_ERROR;
2092                 }
2093
2094                 if (add_uint64_element(msg, "uSNChanged", seq_num) != LDB_SUCCESS) {
2095                         talloc_free(tmp_ctx);
2096                         return LDB_ERR_OPERATIONS_ERROR;
2097                 }
2098         }
2099
2100         ret = ldb_build_mod_req(&mod_req, ldb, tmp_ctx,
2101                                 msg,
2102                                 NULL,
2103                                 NULL, 
2104                                 ldb_op_default_callback,
2105                                 NULL);
2106         if (ret != LDB_SUCCESS) {
2107                 talloc_free(tmp_ctx);
2108                 return ret;
2109         }
2110         talloc_steal(mod_req, msg);
2111
2112         if (DEBUGLVL(4)) {
2113                 DEBUG(4,("Applying DRS linked attribute change:\n%s\n",
2114                          ldb_ldif_message_string(ldb, tmp_ctx, LDB_CHANGETYPE_MODIFY, msg)));
2115         }
2116
2117         /* Run the new request */
2118         ret = ldb_next_request(module, mod_req);
2119
2120         /* we need to wait for this to finish, as we are being called
2121            from the synchronous end_transaction hook of this module */
2122         if (ret == LDB_SUCCESS) {
2123                 ret = ldb_wait(mod_req->handle, LDB_WAIT_ALL);
2124         }
2125
2126         if (ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
2127                 /* the link destination exists, we need to update it
2128                  * by deleting the old one for the same DN then adding
2129                  * the new one */
2130                 msg->elements = talloc_realloc(msg, msg->elements,
2131                                                struct ldb_message_element,
2132                                                msg->num_elements+1);
2133                 if (msg->elements == NULL) {
2134                         ldb_oom(ldb);
2135                         talloc_free(tmp_ctx);
2136                         return LDB_ERR_OPERATIONS_ERROR;
2137                 }
2138                 /* this relies on the backend matching the old entry
2139                    only by the DN portion of the extended DN */
2140                 msg->elements[1] = msg->elements[0];
2141                 msg->elements[0].flags = LDB_FLAG_MOD_DELETE;
2142                 msg->num_elements++;
2143
2144                 ret = ldb_build_mod_req(&mod_req, ldb, tmp_ctx,
2145                                         msg,
2146                                         NULL,
2147                                         NULL, 
2148                                         ldb_op_default_callback,
2149                                         NULL);
2150                 if (ret != LDB_SUCCESS) {
2151                         talloc_free(tmp_ctx);
2152                         return ret;
2153                 }
2154
2155                 /* Run the new request */
2156                 ret = ldb_next_request(module, mod_req);
2157                 
2158                 if (ret == LDB_SUCCESS) {
2159                         ret = ldb_wait(mod_req->handle, LDB_WAIT_ALL);
2160                 }
2161         }
2162
2163         if (ret != LDB_SUCCESS) {
2164                 ldb_debug(ldb, LDB_DEBUG_WARNING, "Failed to apply linked attribute change '%s' %s\n",
2165                           ldb_errstring(ldb),
2166                           ldb_ldif_message_string(ldb, tmp_ctx, LDB_CHANGETYPE_MODIFY, msg));
2167                 ret = LDB_SUCCESS;
2168         }
2169         
2170         talloc_free(tmp_ctx);
2171
2172         return ret;     
2173 }
2174
2175 static int replmd_extended(struct ldb_module *module, struct ldb_request *req)
2176 {
2177         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_REPLICATED_OBJECTS_OID) == 0) {
2178                 return replmd_extended_replicated_objects(module, req);
2179         }
2180
2181         return ldb_next_request(module, req);
2182 }
2183
2184
2185 /*
2186   we hook into the transaction operations to allow us to 
2187   perform the linked attribute updates at the end of the whole
2188   transaction. This allows a forward linked attribute to be created
2189   before the object is created. During a vampire, w2k8 sends us linked
2190   attributes before the objects they are part of.
2191  */
2192 static int replmd_start_transaction(struct ldb_module *module)
2193 {
2194         /* create our private structure for this transaction */
2195         struct replmd_private *replmd_private = talloc_get_type(ldb_module_get_private(module),
2196                                                                 struct replmd_private);
2197         talloc_free(replmd_private->la_ctx);
2198         replmd_private->la_list = NULL;
2199         replmd_private->la_ctx = NULL;
2200
2201         /* free any leftover mod_usn records from cancelled
2202            transactions */
2203         while (replmd_private->ncs) {
2204                 struct nc_entry *e = replmd_private->ncs;
2205                 DLIST_REMOVE(replmd_private->ncs, e);
2206                 talloc_free(e);
2207         }
2208
2209         return ldb_next_start_trans(module);
2210 }
2211
2212 /*
2213   on prepare commit we loop over our queued la_context structures and
2214   apply each of them  
2215  */
2216 static int replmd_prepare_commit(struct ldb_module *module)
2217 {
2218         struct replmd_private *replmd_private = 
2219                 talloc_get_type(ldb_module_get_private(module), struct replmd_private);
2220         struct la_entry *la, *prev;
2221         int ret;
2222
2223         /* walk the list backwards, to do the first entry first, as we
2224          * added the entries with DLIST_ADD() which puts them at the
2225          * start of the list */
2226         for (la = replmd_private->la_list; la && la->next; la=la->next) ;
2227
2228         for (; la; la=prev) {
2229                 prev = la->prev;
2230                 DLIST_REMOVE(replmd_private->la_list, la);
2231                 ret = replmd_process_linked_attribute(module, la);
2232                 if (ret != LDB_SUCCESS) {
2233                         talloc_free(replmd_private->la_ctx);
2234                         replmd_private->la_list = NULL;
2235                         replmd_private->la_ctx = NULL;
2236                         return ret;
2237                 }
2238         }
2239
2240         talloc_free(replmd_private->la_ctx);
2241         replmd_private->la_list = NULL;
2242         replmd_private->la_ctx = NULL;
2243
2244         /* possibly change @REPLCHANGED */
2245         ret = replmd_notify_store(module);
2246         if (ret != LDB_SUCCESS) {
2247                 return ret;
2248         }
2249         
2250         return ldb_next_prepare_commit(module);
2251 }
2252
2253 static int replmd_del_transaction(struct ldb_module *module)
2254 {
2255         struct replmd_private *replmd_private = 
2256                 talloc_get_type(ldb_module_get_private(module), struct replmd_private);
2257         talloc_free(replmd_private->la_ctx);
2258         replmd_private->la_list = NULL;
2259         replmd_private->la_ctx = NULL;
2260         return ldb_next_del_trans(module);
2261 }
2262
2263
2264 _PUBLIC_ const struct ldb_module_ops ldb_repl_meta_data_module_ops = {
2265         .name          = "repl_meta_data",
2266         .init_context      = replmd_init,
2267         .add               = replmd_add,
2268         .modify            = replmd_modify,
2269         .rename            = replmd_rename,
2270         .extended          = replmd_extended,
2271         .start_transaction = replmd_start_transaction,
2272         .prepare_commit    = replmd_prepare_commit,
2273         .del_transaction   = replmd_del_transaction,
2274 };