s4-drs: treat a zero GUID as not present in replmd_add_fix_la
[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 #include "dsdb/samdb/ldb_modules/util.h"
49 #include "lib/util/binsearch.h"
50
51 #define W2K3_LINKED_ATTRIBUTES 1
52
53 struct replmd_private {
54         TALLOC_CTX *la_ctx;
55         struct la_entry *la_list;
56         TALLOC_CTX *bl_ctx;
57         struct la_backlink *la_backlinks;
58         struct nc_entry {
59                 struct nc_entry *prev, *next;
60                 struct ldb_dn *dn;
61                 uint64_t mod_usn;
62         } *ncs;
63 };
64
65 struct la_entry {
66         struct la_entry *next, *prev;
67         struct drsuapi_DsReplicaLinkedAttribute *la;
68 };
69
70 struct replmd_replicated_request {
71         struct ldb_module *module;
72         struct ldb_request *req;
73
74         const struct dsdb_schema *schema;
75
76         /* the controls we pass down */
77         struct ldb_control **controls;
78
79         /* details for the mode where we apply a bunch of inbound replication meessages */
80         bool apply_mode;
81         uint32_t index_current;
82         struct dsdb_extended_replicated_objects *objs;
83
84         struct ldb_message *search_msg;
85
86         uint64_t seq_num;
87
88 };
89
90 static int replmd_replicated_apply_next(struct replmd_replicated_request *ar);
91
92
93 /*
94   initialise the module
95   allocate the private structure and build the list
96   of partition DNs for use by replmd_notify()
97  */
98 static int replmd_init(struct ldb_module *module)
99 {
100         struct replmd_private *replmd_private;
101         struct ldb_context *ldb = ldb_module_get_ctx(module);
102
103         replmd_private = talloc_zero(module, struct replmd_private);
104         if (replmd_private == NULL) {
105                 ldb_oom(ldb);
106                 return LDB_ERR_OPERATIONS_ERROR;
107         }
108         ldb_module_set_private(module, replmd_private);
109
110         return ldb_next_init(module);
111 }
112
113 /*
114   cleanup our per-transaction contexts
115  */
116 static void replmd_txn_cleanup(struct replmd_private *replmd_private)
117 {
118         talloc_free(replmd_private->la_ctx);
119         replmd_private->la_list = NULL;
120         replmd_private->la_ctx = NULL;
121
122         talloc_free(replmd_private->bl_ctx);
123         replmd_private->la_backlinks = NULL;
124         replmd_private->bl_ctx = NULL;
125 }
126
127
128 struct la_backlink {
129         struct la_backlink *next, *prev;
130         const char *attr_name;
131         struct GUID forward_guid, target_guid;
132         bool active;
133 };
134
135 /*
136   process a backlinks we accumulated during a transaction, adding and
137   deleting the backlinks from the target objects
138  */
139 static int replmd_process_backlink(struct ldb_module *module, struct la_backlink *bl)
140 {
141         struct ldb_dn *target_dn, *source_dn;
142         int ret;
143         struct ldb_context *ldb = ldb_module_get_ctx(module);
144         struct ldb_message *msg;
145         TALLOC_CTX *tmp_ctx = talloc_new(bl);
146         char *dn_string;
147
148         /*
149           - find DN of target
150           - find DN of source
151           - construct ldb_message
152               - either an add or a delete
153          */
154         ret = dsdb_module_dn_by_guid(module, tmp_ctx, &bl->target_guid, &target_dn);
155         if (ret != LDB_SUCCESS) {
156                 ldb_asprintf_errstring(ldb, "Failed to find target DN for linked attribute with GUID %s\n",
157                                        GUID_string(bl, &bl->target_guid));
158                 talloc_free(tmp_ctx);
159                 return ret;
160         }
161
162         ret = dsdb_module_dn_by_guid(module, tmp_ctx, &bl->forward_guid, &source_dn);
163         if (ret != LDB_SUCCESS) {
164                 ldb_asprintf_errstring(ldb, "Failed to find source DN for linked attribute with GUID %s\n",
165                                        GUID_string(bl, &bl->forward_guid));
166                 talloc_free(tmp_ctx);
167                 return ret;
168         }
169
170         msg = ldb_msg_new(tmp_ctx);
171         if (msg == NULL) {
172                 ldb_module_oom(module);
173                 talloc_free(tmp_ctx);
174                 return LDB_ERR_OPERATIONS_ERROR;
175         }
176
177         /* construct a ldb_message for adding/deleting the backlink */
178         msg->dn = target_dn;
179         dn_string = ldb_dn_get_extended_linearized(tmp_ctx, source_dn, 1);
180         if (!dn_string) {
181                 ldb_module_oom(module);
182                 talloc_free(tmp_ctx);
183                 return LDB_ERR_OPERATIONS_ERROR;
184         }
185         ret = ldb_msg_add_steal_string(msg, bl->attr_name, dn_string);
186         if (ret != LDB_SUCCESS) {
187                 talloc_free(tmp_ctx);
188                 return ret;
189         }
190         msg->elements[0].flags = bl->active?LDB_FLAG_MOD_ADD:LDB_FLAG_MOD_DELETE;
191
192         ret = dsdb_module_modify(module, msg, 0);
193         if (ret != LDB_SUCCESS) {
194                 ldb_asprintf_errstring(ldb, "Failed to %s backlink from %s to %s - %s",
195                                        bl->active?"add":"remove",
196                                        ldb_dn_get_linearized(source_dn),
197                                        ldb_dn_get_linearized(target_dn),
198                                        ldb_errstring(ldb));
199                 talloc_free(tmp_ctx);
200                 return ret;
201         }
202         talloc_free(tmp_ctx);
203         return ret;
204 }
205
206 /*
207   add a backlink to the list of backlinks to add/delete in the prepare
208   commit
209  */
210 static int replmd_add_backlink(struct ldb_module *module, const struct dsdb_schema *schema,
211                                struct GUID *forward_guid, struct GUID *target_guid,
212                                bool active, const struct dsdb_attribute *schema_attr, bool immediate)
213 {
214         const struct dsdb_attribute *target_attr;
215         struct la_backlink *bl;
216         struct replmd_private *replmd_private =
217                 talloc_get_type_abort(ldb_module_get_private(module), struct replmd_private);
218
219         target_attr = dsdb_attribute_by_linkID(schema, schema_attr->linkID ^ 1);
220         if (!target_attr) {
221                 /*
222                  * windows 2003 has a broken schema where the
223                  * definition of msDS-IsDomainFor is missing (which is
224                  * supposed to be the backlink of the
225                  * msDS-HasDomainNCs attribute
226                  */
227                 return LDB_SUCCESS;
228         }
229
230         /* see if its already in the list */
231         for (bl=replmd_private->la_backlinks; bl; bl=bl->next) {
232                 if (GUID_equal(forward_guid, &bl->forward_guid) &&
233                     GUID_equal(target_guid, &bl->target_guid) &&
234                     (target_attr->lDAPDisplayName == bl->attr_name ||
235                      strcmp(target_attr->lDAPDisplayName, bl->attr_name) == 0)) {
236                         break;
237                 }
238         }
239
240         if (bl) {
241                 /* we found an existing one */
242                 if (bl->active == active) {
243                         return LDB_SUCCESS;
244                 }
245                 DLIST_REMOVE(replmd_private->la_backlinks, bl);
246                 talloc_free(bl);
247                 return LDB_SUCCESS;
248         }
249
250         if (replmd_private->bl_ctx == NULL) {
251                 replmd_private->bl_ctx = talloc_new(replmd_private);
252                 if (replmd_private->bl_ctx == NULL) {
253                         ldb_module_oom(module);
254                         return LDB_ERR_OPERATIONS_ERROR;
255                 }
256         }
257
258         /* its a new one */
259         bl = talloc(replmd_private->bl_ctx, struct la_backlink);
260         if (bl == NULL) {
261                 ldb_module_oom(module);
262                 return LDB_ERR_OPERATIONS_ERROR;
263         }
264
265         bl->attr_name = target_attr->lDAPDisplayName;
266         bl->forward_guid = *forward_guid;
267         bl->target_guid = *target_guid;
268         bl->active = active;
269
270         /* the caller may ask for this backlink to be processed
271            immediately */
272         if (immediate) {
273                 int ret = replmd_process_backlink(module, bl);
274                 talloc_free(bl);
275                 return ret;
276         }
277
278         DLIST_ADD(replmd_private->la_backlinks, bl);
279
280         return LDB_SUCCESS;
281 }
282
283
284 /*
285  * Callback for most write operations in this module:
286  * 
287  * notify the repl task that a object has changed. The notifies are
288  * gathered up in the replmd_private structure then written to the
289  * @REPLCHANGED object in each partition during the prepare_commit
290  */
291 static int replmd_op_callback(struct ldb_request *req, struct ldb_reply *ares)
292 {
293         int ret;
294         struct replmd_replicated_request *ac = 
295                 talloc_get_type_abort(req->context, struct replmd_replicated_request);
296         struct replmd_private *replmd_private = 
297                 talloc_get_type_abort(ldb_module_get_private(ac->module), struct replmd_private);
298         struct nc_entry *modified_partition;
299         struct ldb_control *partition_ctrl;
300         const struct dsdb_control_current_partition *partition;
301
302         struct ldb_control **controls;
303
304         partition_ctrl = ldb_reply_get_control(ares, DSDB_CONTROL_CURRENT_PARTITION_OID);
305
306         /* Remove the 'partition' control from what we pass up the chain */
307         controls = controls_except_specified(ares->controls, ares, partition_ctrl);
308
309         if (ares->error != LDB_SUCCESS) {
310                 return ldb_module_done(ac->req, controls,
311                                         ares->response, ares->error);
312         }
313
314         if (ares->type != LDB_REPLY_DONE) {
315                 ldb_set_errstring(ldb_module_get_ctx(ac->module), "Invalid reply type for notify\n!");
316                 return ldb_module_done(ac->req, NULL,
317                                        NULL, LDB_ERR_OPERATIONS_ERROR);
318         }
319
320         if (!partition_ctrl) {
321                 return ldb_module_done(ac->req, NULL,
322                                        NULL, LDB_ERR_OPERATIONS_ERROR);
323         }
324
325         partition = talloc_get_type_abort(partition_ctrl->data,
326                                     struct dsdb_control_current_partition);
327         
328         if (ac->seq_num > 0) {
329                 for (modified_partition = replmd_private->ncs; modified_partition; 
330                      modified_partition = modified_partition->next) {
331                         if (ldb_dn_compare(modified_partition->dn, partition->dn) == 0) {
332                                 break;
333                         }
334                 }
335                 
336                 if (modified_partition == NULL) {
337                         modified_partition = talloc_zero(replmd_private, struct nc_entry);
338                         if (!modified_partition) {
339                                 ldb_oom(ldb_module_get_ctx(ac->module));
340                                 return ldb_module_done(ac->req, NULL,
341                                                        NULL, LDB_ERR_OPERATIONS_ERROR);
342                         }
343                         modified_partition->dn = ldb_dn_copy(modified_partition, partition->dn);
344                         if (!modified_partition->dn) {
345                                 ldb_oom(ldb_module_get_ctx(ac->module));
346                                 return ldb_module_done(ac->req, NULL,
347                                                        NULL, LDB_ERR_OPERATIONS_ERROR);
348                         }
349                         DLIST_ADD(replmd_private->ncs, modified_partition);
350                 }
351
352                 if (ac->seq_num > modified_partition->mod_usn) {
353                         modified_partition->mod_usn = ac->seq_num;
354                 }
355         }
356
357         if (ac->apply_mode) {
358                 talloc_free(ares);
359                 ac->index_current++;
360                 
361                 ret = replmd_replicated_apply_next(ac);
362                 if (ret != LDB_SUCCESS) {
363                         return ldb_module_done(ac->req, NULL, NULL, ret);
364                 }
365                 return ret;
366         } else {
367                 /* free the partition control container here, for the
368                  * common path.  Other cases will have it cleaned up
369                  * eventually with the ares */
370                 talloc_free(partition_ctrl);
371                 return ldb_module_done(ac->req, 
372                                        controls_except_specified(controls, ares, partition_ctrl),
373                                        ares->response, LDB_SUCCESS);
374         }
375 }
376
377
378 /*
379  * update a @REPLCHANGED record in each partition if there have been
380  * any writes of replicated data in the partition
381  */
382 static int replmd_notify_store(struct ldb_module *module)
383 {
384         struct replmd_private *replmd_private = 
385                 talloc_get_type(ldb_module_get_private(module), struct replmd_private);
386         struct ldb_context *ldb = ldb_module_get_ctx(module);
387
388         while (replmd_private->ncs) {
389                 int ret;
390                 struct nc_entry *modified_partition = replmd_private->ncs;
391
392                 ret = dsdb_save_partition_usn(ldb, modified_partition->dn, modified_partition->mod_usn);
393                 if (ret != LDB_SUCCESS) {
394                         DEBUG(0,(__location__ ": Failed to save partition uSN for %s\n",
395                                  ldb_dn_get_linearized(modified_partition->dn)));
396                         return ret;
397                 }
398                 DLIST_REMOVE(replmd_private->ncs, modified_partition);
399                 talloc_free(modified_partition);
400         }
401
402         return LDB_SUCCESS;
403 }
404
405
406 /*
407   created a replmd_replicated_request context
408  */
409 static struct replmd_replicated_request *replmd_ctx_init(struct ldb_module *module,
410                                                          struct ldb_request *req)
411 {
412         struct ldb_context *ldb;
413         struct replmd_replicated_request *ac;
414
415         ldb = ldb_module_get_ctx(module);
416
417         ac = talloc_zero(req, struct replmd_replicated_request);
418         if (ac == NULL) {
419                 ldb_oom(ldb);
420                 return NULL;
421         }
422
423         ac->module = module;
424         ac->req = req;
425
426         ac->schema = dsdb_get_schema(ldb);
427         if (!ac->schema) {
428                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
429                               "replmd_modify: no dsdb_schema loaded");
430                 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
431                 return NULL;
432         }
433
434         return ac;
435 }
436
437 /*
438   add a time element to a record
439 */
440 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
441 {
442         struct ldb_message_element *el;
443         char *s;
444
445         if (ldb_msg_find_element(msg, attr) != NULL) {
446                 return LDB_SUCCESS;
447         }
448
449         s = ldb_timestring(msg, t);
450         if (s == NULL) {
451                 return LDB_ERR_OPERATIONS_ERROR;
452         }
453
454         if (ldb_msg_add_string(msg, attr, s) != LDB_SUCCESS) {
455                 return LDB_ERR_OPERATIONS_ERROR;
456         }
457
458         el = ldb_msg_find_element(msg, attr);
459         /* always set as replace. This works because on add ops, the flag
460            is ignored */
461         el->flags = LDB_FLAG_MOD_REPLACE;
462
463         return LDB_SUCCESS;
464 }
465
466 /*
467   add a uint64_t element to a record
468 */
469 static int add_uint64_element(struct ldb_message *msg, const char *attr, uint64_t v)
470 {
471         struct ldb_message_element *el;
472
473         if (ldb_msg_find_element(msg, attr) != NULL) {
474                 return LDB_SUCCESS;
475         }
476
477         if (ldb_msg_add_fmt(msg, attr, "%llu", (unsigned long long)v) != LDB_SUCCESS) {
478                 return LDB_ERR_OPERATIONS_ERROR;
479         }
480
481         el = ldb_msg_find_element(msg, attr);
482         /* always set as replace. This works because on add ops, the flag
483            is ignored */
484         el->flags = LDB_FLAG_MOD_REPLACE;
485
486         return LDB_SUCCESS;
487 }
488
489 static int replmd_replPropertyMetaData1_attid_sort(const struct replPropertyMetaData1 *m1,
490                                                    const struct replPropertyMetaData1 *m2,
491                                                    const uint32_t *rdn_attid)
492 {
493         if (m1->attid == m2->attid) {
494                 return 0;
495         }
496
497         /*
498          * the rdn attribute should be at the end!
499          * so we need to return a value greater than zero
500          * which means m1 is greater than m2
501          */
502         if (m1->attid == *rdn_attid) {
503                 return 1;
504         }
505
506         /*
507          * the rdn attribute should be at the end!
508          * so we need to return a value less than zero
509          * which means m2 is greater than m1
510          */
511         if (m2->attid == *rdn_attid) {
512                 return -1;
513         }
514
515         return m1->attid > m2->attid ? 1 : -1;
516 }
517
518 static int replmd_replPropertyMetaDataCtr1_sort(struct replPropertyMetaDataCtr1 *ctr1,
519                                                 const struct dsdb_schema *schema,
520                                                 struct ldb_dn *dn)
521 {
522         const char *rdn_name;
523         const struct dsdb_attribute *rdn_sa;
524
525         rdn_name = ldb_dn_get_rdn_name(dn);
526         if (!rdn_name) {
527                 DEBUG(0,(__location__ ": No rDN for %s?\n", ldb_dn_get_linearized(dn)));
528                 return LDB_ERR_OPERATIONS_ERROR;
529         }
530
531         rdn_sa = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
532         if (rdn_sa == NULL) {
533                 DEBUG(0,(__location__ ": No sa found for rDN %s for %s\n", rdn_name, ldb_dn_get_linearized(dn)));
534                 return LDB_ERR_OPERATIONS_ERROR;                
535         }
536
537         DEBUG(6,("Sorting rpmd with attid exception %u rDN=%s DN=%s\n", 
538                  rdn_sa->attributeID_id, rdn_name, ldb_dn_get_linearized(dn)));
539
540         ldb_qsort(ctr1->array, ctr1->count, sizeof(struct replPropertyMetaData1),
541                   discard_const_p(void, &rdn_sa->attributeID_id), 
542                   (ldb_qsort_cmp_fn_t)replmd_replPropertyMetaData1_attid_sort);
543
544         return LDB_SUCCESS;
545 }
546
547 static int replmd_ldb_message_element_attid_sort(const struct ldb_message_element *e1,
548                                                  const struct ldb_message_element *e2,
549                                                  const struct dsdb_schema *schema)
550 {
551         const struct dsdb_attribute *a1;
552         const struct dsdb_attribute *a2;
553
554         /* 
555          * TODO: make this faster by caching the dsdb_attribute pointer
556          *       on the ldb_messag_element
557          */
558
559         a1 = dsdb_attribute_by_lDAPDisplayName(schema, e1->name);
560         a2 = dsdb_attribute_by_lDAPDisplayName(schema, e2->name);
561
562         /*
563          * TODO: remove this check, we should rely on e1 and e2 having valid attribute names
564          *       in the schema
565          */
566         if (!a1 || !a2) {
567                 return strcasecmp(e1->name, e2->name);
568         }
569         if (a1->attributeID_id == a2->attributeID_id) {
570                 return 0;
571         }
572         return a1->attributeID_id > a2->attributeID_id ? 1 : -1;
573 }
574
575 static void replmd_ldb_message_sort(struct ldb_message *msg,
576                                     const struct dsdb_schema *schema)
577 {
578         ldb_qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element),
579                   discard_const_p(void, schema), (ldb_qsort_cmp_fn_t)replmd_ldb_message_element_attid_sort);
580 }
581
582 static int replmd_build_la_val(TALLOC_CTX *mem_ctx, struct ldb_val *v, struct dsdb_dn *dsdb_dn,
583                                const struct GUID *invocation_id, uint64_t seq_num,
584                                uint64_t local_usn, NTTIME nttime, uint32_t version, bool deleted);
585
586
587 /*
588   fix up linked attributes in replmd_add.
589   This involves setting up the right meta-data in extended DN
590   components, and creating backlinks to the object
591  */
592 static int replmd_add_fix_la(struct ldb_module *module, struct ldb_message_element *el,
593                              uint64_t seq_num, const struct GUID *invocationId, time_t t,
594                              struct GUID *guid, const struct dsdb_attribute *sa)
595 {
596         int i;
597         TALLOC_CTX *tmp_ctx = talloc_new(el->values);
598         struct ldb_context *ldb = ldb_module_get_ctx(module);
599         struct dsdb_schema *schema = dsdb_get_schema(ldb);
600         NTTIME now;
601
602         unix_to_nt_time(&now, t);
603
604         for (i=0; i<el->num_values; i++) {
605                 struct ldb_val *v = &el->values[i];
606                 struct dsdb_dn *dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, v, sa->syntax->ldap_oid);
607                 struct GUID target_guid;
608                 NTSTATUS status;
609                 int ret;
610
611                 ret = replmd_build_la_val(el->values, v, dsdb_dn, invocationId,
612                                           seq_num, seq_num, now, 0, false);
613                 if (ret != LDB_SUCCESS) {
614                         talloc_free(tmp_ctx);
615                         return ret;
616                 }
617
618                 /* note that the DN already has the extended
619                    components from the extended_dn_store module */
620                 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &target_guid, "GUID");
621                 if (!NT_STATUS_IS_OK(status) || GUID_all_zero(&target_guid)) {
622                         ret = dsdb_module_guid_by_dn(module, dsdb_dn->dn, &target_guid);
623                         if (ret != LDB_SUCCESS) {
624                                 talloc_free(tmp_ctx);
625                                 return ret;
626                         }
627                 }
628
629                 ret = replmd_add_backlink(module, schema, guid, &target_guid, true, sa, false);
630                 if (ret != LDB_SUCCESS) {
631                         talloc_free(tmp_ctx);
632                         return ret;
633                 }
634         }
635
636         talloc_free(tmp_ctx);
637         return LDB_SUCCESS;
638 }
639
640
641 /*
642   intercept add requests
643  */
644 static int replmd_add(struct ldb_module *module, struct ldb_request *req)
645 {
646         struct ldb_context *ldb;
647         struct ldb_control *control;
648         struct replmd_replicated_request *ac;
649         enum ndr_err_code ndr_err;
650         struct ldb_request *down_req;
651         struct ldb_message *msg;
652         const DATA_BLOB *guid_blob;
653         struct GUID guid;
654         struct replPropertyMetaDataBlob nmd;
655         struct ldb_val nmd_value;
656         const struct GUID *our_invocation_id;
657         time_t t = time(NULL);
658         NTTIME now;
659         char *time_str;
660         int ret;
661         uint32_t i, ni=0;
662         bool allow_add_guid = false;
663         bool remove_current_guid = false;
664
665         /* check if there's a show relax control (used by provision to say 'I know what I'm doing') */
666         control = ldb_request_get_control(req, LDB_CONTROL_RELAX_OID);
667         if (control) {
668                 allow_add_guid = 1;
669         }
670
671         /* do not manipulate our control entries */
672         if (ldb_dn_is_special(req->op.add.message->dn)) {
673                 return ldb_next_request(module, req);
674         }
675
676         ldb = ldb_module_get_ctx(module);
677
678         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_add\n");
679
680         ac = replmd_ctx_init(module, req);
681         if (!ac) {
682                 return LDB_ERR_OPERATIONS_ERROR;
683         }
684
685         guid_blob = ldb_msg_find_ldb_val(req->op.add.message, "objectGUID");
686         if ( guid_blob != NULL ) {
687                 if( !allow_add_guid ) {
688                         ldb_debug_set(ldb, LDB_DEBUG_ERROR,
689                               "replmd_add: it's not allowed to add an object with objectGUID\n");
690                         talloc_free(ac);
691                         return LDB_ERR_UNWILLING_TO_PERFORM;
692                 } else {
693                         NTSTATUS status = GUID_from_data_blob(guid_blob,&guid);
694                         if ( !NT_STATUS_IS_OK(status)) {
695                                 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
696                                       "replmd_add: Unable to parse as a GUID the attribute objectGUID\n");
697                                 talloc_free(ac);
698                                 return LDB_ERR_UNWILLING_TO_PERFORM;
699                         }
700                         /* we remove this attribute as it can be a string and will not be treated 
701                         correctly and then we will readd it latter on in the good format*/
702                         remove_current_guid = true;
703                 }
704         } else {
705                 /* a new GUID */
706                 guid = GUID_random();
707         }
708
709         /* Get a sequence number from the backend */
710         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &ac->seq_num);
711         if (ret != LDB_SUCCESS) {
712                 talloc_free(ac);
713                 return ret;
714         }
715
716         /* get our invocationId */
717         our_invocation_id = samdb_ntds_invocation_id(ldb);
718         if (!our_invocation_id) {
719                 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
720                               "replmd_add: unable to find invocationId\n");
721                 talloc_free(ac);
722                 return LDB_ERR_OPERATIONS_ERROR;
723         }
724
725         /* we have to copy the message as the caller might have it as a const */
726         msg = ldb_msg_copy_shallow(ac, req->op.add.message);
727         if (msg == NULL) {
728                 ldb_oom(ldb);
729                 talloc_free(ac);
730                 return LDB_ERR_OPERATIONS_ERROR;
731         }
732
733         /* generated times */
734         unix_to_nt_time(&now, t);
735         time_str = ldb_timestring(msg, t);
736         if (!time_str) {
737                 ldb_oom(ldb);
738                 talloc_free(ac);
739                 return LDB_ERR_OPERATIONS_ERROR;
740         }
741         if (remove_current_guid) {
742                 ldb_msg_remove_attr(msg,"objectGUID");
743         }
744
745         /* 
746          * remove autogenerated attributes
747          */
748         ldb_msg_remove_attr(msg, "whenCreated");
749         ldb_msg_remove_attr(msg, "whenChanged");
750         ldb_msg_remove_attr(msg, "uSNCreated");
751         ldb_msg_remove_attr(msg, "uSNChanged");
752         ldb_msg_remove_attr(msg, "replPropertyMetaData");
753
754         /*
755          * readd replicated attributes
756          */
757         ret = ldb_msg_add_string(msg, "whenCreated", time_str);
758         if (ret != LDB_SUCCESS) {
759                 ldb_oom(ldb);
760                 talloc_free(ac);
761                 return ret;
762         }
763
764         /* build the replication meta_data */
765         ZERO_STRUCT(nmd);
766         nmd.version             = 1;
767         nmd.ctr.ctr1.count      = msg->num_elements;
768         nmd.ctr.ctr1.array      = talloc_array(msg,
769                                                struct replPropertyMetaData1,
770                                                nmd.ctr.ctr1.count);
771         if (!nmd.ctr.ctr1.array) {
772                 ldb_oom(ldb);
773                 talloc_free(ac);
774                 return LDB_ERR_OPERATIONS_ERROR;
775         }
776
777         for (i=0; i < msg->num_elements; i++) {
778                 struct ldb_message_element *e = &msg->elements[i];
779                 struct replPropertyMetaData1 *m = &nmd.ctr.ctr1.array[ni];
780                 const struct dsdb_attribute *sa;
781
782                 if (e->name[0] == '@') continue;
783
784                 sa = dsdb_attribute_by_lDAPDisplayName(ac->schema, e->name);
785                 if (!sa) {
786                         ldb_debug_set(ldb, LDB_DEBUG_ERROR,
787                                       "replmd_add: attribute '%s' not defined in schema\n",
788                                       e->name);
789                         talloc_free(ac);
790                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
791                 }
792
793                 if ((sa->systemFlags & DS_FLAG_ATTR_NOT_REPLICATED) || (sa->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED)) {
794                         /* if the attribute is not replicated (0x00000001)
795                          * or constructed (0x00000004) it has no metadata
796                          */
797                         continue;
798                 }
799
800 #if W2K3_LINKED_ATTRIBUTES
801                 if (sa->linkID != 0 && dsdb_functional_level(ldb) > DS_DOMAIN_FUNCTION_2000) {
802                         ret = replmd_add_fix_la(module, e, ac->seq_num, our_invocation_id, t, &guid, sa);
803                         if (ret != LDB_SUCCESS) {
804                                 talloc_free(ac);
805                                 return ret;
806                         }
807                         /* linked attributes are not stored in
808                            replPropertyMetaData in FL above w2k */
809                         continue;
810                 }
811 #endif
812
813                 m->attid                        = sa->attributeID_id;
814                 m->version                      = 1;
815                 m->originating_change_time      = now;
816                 m->originating_invocation_id    = *our_invocation_id;
817                 m->originating_usn              = ac->seq_num;
818                 m->local_usn                    = ac->seq_num;
819                 ni++;
820         }
821
822         /* fix meta data count */
823         nmd.ctr.ctr1.count = ni;
824
825         /*
826          * sort meta data array, and move the rdn attribute entry to the end
827          */
828         ret = replmd_replPropertyMetaDataCtr1_sort(&nmd.ctr.ctr1, ac->schema, msg->dn);
829         if (ret != LDB_SUCCESS) {
830                 talloc_free(ac);
831                 return ret;
832         }
833
834         /* generated NDR encoded values */
835         ndr_err = ndr_push_struct_blob(&nmd_value, msg, 
836                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
837                                        &nmd,
838                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
839         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
840                 ldb_oom(ldb);
841                 talloc_free(ac);
842                 return LDB_ERR_OPERATIONS_ERROR;
843         }
844
845         /*
846          * add the autogenerated values
847          */
848         ret = dsdb_msg_add_guid(msg, &guid, "objectGUID");
849         if (ret != LDB_SUCCESS) {
850                 ldb_oom(ldb);
851                 talloc_free(ac);
852                 return ret;
853         }
854         ret = ldb_msg_add_string(msg, "whenChanged", time_str);
855         if (ret != LDB_SUCCESS) {
856                 ldb_oom(ldb);
857                 talloc_free(ac);
858                 return ret;
859         }
860         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNCreated", ac->seq_num);
861         if (ret != LDB_SUCCESS) {
862                 ldb_oom(ldb);
863                 talloc_free(ac);
864                 return ret;
865         }
866         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", ac->seq_num);
867         if (ret != LDB_SUCCESS) {
868                 ldb_oom(ldb);
869                 talloc_free(ac);
870                 return ret;
871         }
872         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &nmd_value, NULL);
873         if (ret != LDB_SUCCESS) {
874                 ldb_oom(ldb);
875                 talloc_free(ac);
876                 return ret;
877         }
878
879         /*
880          * sort the attributes by attid before storing the object
881          */
882         replmd_ldb_message_sort(msg, ac->schema);
883
884         ret = ldb_build_add_req(&down_req, ldb, ac,
885                                 msg,
886                                 req->controls,
887                                 ac, replmd_op_callback,
888                                 req);
889         if (ret != LDB_SUCCESS) {
890                 talloc_free(ac);
891                 return ret;
892         }
893
894         /* mark the control done */
895         if (control) {
896                 control->critical = 0;
897         }
898
899         /* go on with the call chain */
900         return ldb_next_request(module, down_req);
901 }
902
903
904 /*
905  * update the replPropertyMetaData for one element
906  */
907 static int replmd_update_rpmd_element(struct ldb_context *ldb, 
908                                       struct ldb_message *msg,
909                                       struct ldb_message_element *el,
910                                       struct replPropertyMetaDataBlob *omd,
911                                       const struct dsdb_schema *schema,
912                                       uint64_t *seq_num,
913                                       const struct GUID *our_invocation_id,
914                                       NTTIME now)
915 {
916         int i;
917         const struct dsdb_attribute *a;
918         struct replPropertyMetaData1 *md1;
919
920         a = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
921         if (a == NULL) {
922                 DEBUG(0,(__location__ ": Unable to find attribute %s in schema\n",
923                          el->name));
924                 return LDB_ERR_OPERATIONS_ERROR;
925         }
926
927         if ((a->systemFlags & DS_FLAG_ATTR_NOT_REPLICATED) || (a->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED)) {
928                 return LDB_SUCCESS;
929         }
930
931         for (i=0; i<omd->ctr.ctr1.count; i++) {
932                 if (a->attributeID_id == omd->ctr.ctr1.array[i].attid) break;
933         }
934
935 #if W2K3_LINKED_ATTRIBUTES
936         if (a->linkID != 0 && dsdb_functional_level(ldb) > DS_DOMAIN_FUNCTION_2000) {
937                 /* linked attributes are not stored in
938                    replPropertyMetaData in FL above w2k, but we do
939                    raise the seqnum for the object  */
940                 if (*seq_num == 0 &&
941                     ldb_sequence_number(ldb, LDB_SEQ_NEXT, seq_num) != LDB_SUCCESS) {
942                         return LDB_ERR_OPERATIONS_ERROR;
943                 }
944                 return LDB_SUCCESS;
945         }
946 #endif
947
948         if (i == omd->ctr.ctr1.count) {
949                 /* we need to add a new one */
950                 omd->ctr.ctr1.array = talloc_realloc(msg, omd->ctr.ctr1.array, 
951                                                      struct replPropertyMetaData1, omd->ctr.ctr1.count+1);
952                 if (omd->ctr.ctr1.array == NULL) {
953                         ldb_oom(ldb);
954                         return LDB_ERR_OPERATIONS_ERROR;
955                 }
956                 omd->ctr.ctr1.count++;
957                 ZERO_STRUCT(omd->ctr.ctr1.array[i]);
958         }
959
960         /* Get a new sequence number from the backend. We only do this
961          * if we have a change that requires a new
962          * replPropertyMetaData element 
963          */
964         if (*seq_num == 0) {
965                 int ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, seq_num);
966                 if (ret != LDB_SUCCESS) {
967                         return LDB_ERR_OPERATIONS_ERROR;
968                 }
969         }
970
971         md1 = &omd->ctr.ctr1.array[i];
972         md1->version++;
973         md1->attid                     = a->attributeID_id;
974         md1->originating_change_time   = now;
975         md1->originating_invocation_id = *our_invocation_id;
976         md1->originating_usn           = *seq_num;
977         md1->local_usn                 = *seq_num;
978         
979         return LDB_SUCCESS;
980 }
981
982 /*
983  * update the replPropertyMetaData object each time we modify an
984  * object. This is needed for DRS replication, as the merge on the
985  * client is based on this object 
986  */
987 static int replmd_update_rpmd(struct ldb_module *module, 
988                               const struct dsdb_schema *schema, 
989                               struct ldb_message *msg, uint64_t *seq_num,
990                               time_t t)
991 {
992         const struct ldb_val *omd_value;
993         enum ndr_err_code ndr_err;
994         struct replPropertyMetaDataBlob omd;
995         int i;
996         NTTIME now;
997         const struct GUID *our_invocation_id;
998         int ret;
999         const char *attrs[] = { "replPropertyMetaData" , NULL };
1000         struct ldb_result *res;
1001         struct ldb_context *ldb;
1002
1003         ldb = ldb_module_get_ctx(module);
1004
1005         our_invocation_id = samdb_ntds_invocation_id(ldb);
1006         if (!our_invocation_id) {
1007                 /* this happens during an initial vampire while
1008                    updating the schema */
1009                 DEBUG(5,("No invocationID - skipping replPropertyMetaData update\n"));
1010                 return LDB_SUCCESS;
1011         }
1012
1013         unix_to_nt_time(&now, t);
1014
1015         /* search for the existing replPropertyMetaDataBlob */
1016         ret = dsdb_search_dn_with_deleted(ldb, msg, &res, msg->dn, attrs);
1017         if (ret != LDB_SUCCESS || res->count != 1) {
1018                 DEBUG(0,(__location__ ": Object %s failed to find replPropertyMetaData\n",
1019                          ldb_dn_get_linearized(msg->dn)));
1020                 return LDB_ERR_OPERATIONS_ERROR;
1021         }
1022                 
1023
1024         omd_value = ldb_msg_find_ldb_val(res->msgs[0], "replPropertyMetaData");
1025         if (!omd_value) {
1026                 DEBUG(0,(__location__ ": Object %s does not have a replPropertyMetaData attribute\n",
1027                          ldb_dn_get_linearized(msg->dn)));
1028                 return LDB_ERR_OPERATIONS_ERROR;
1029         }
1030
1031         ndr_err = ndr_pull_struct_blob(omd_value, msg,
1032                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &omd,
1033                                        (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
1034         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1035                 DEBUG(0,(__location__ ": Failed to parse replPropertyMetaData for %s\n",
1036                          ldb_dn_get_linearized(msg->dn)));
1037                 return LDB_ERR_OPERATIONS_ERROR;
1038         }
1039
1040         if (omd.version != 1) {
1041                 DEBUG(0,(__location__ ": bad version %u in replPropertyMetaData for %s\n",
1042                          omd.version, ldb_dn_get_linearized(msg->dn)));
1043                 return LDB_ERR_OPERATIONS_ERROR;
1044         }
1045
1046         for (i=0; i<msg->num_elements; i++) {
1047                 ret = replmd_update_rpmd_element(ldb, msg, &msg->elements[i], &omd, schema, seq_num, 
1048                                                  our_invocation_id, now);
1049                 if (ret != LDB_SUCCESS) {
1050                         return ret;
1051                 }
1052         }
1053
1054         /*
1055          * replmd_update_rpmd_element has done an update if the
1056          * seq_num is set
1057          */
1058         if (*seq_num != 0) {
1059                 struct ldb_val *md_value;
1060                 struct ldb_message_element *el;
1061
1062                 md_value = talloc(msg, struct ldb_val);
1063                 if (md_value == NULL) {
1064                         ldb_oom(ldb);
1065                         return LDB_ERR_OPERATIONS_ERROR;
1066                 }
1067
1068                 ret = replmd_replPropertyMetaDataCtr1_sort(&omd.ctr.ctr1, schema, msg->dn);
1069                 if (ret != LDB_SUCCESS) {
1070                         return ret;
1071                 }
1072
1073                 ndr_err = ndr_push_struct_blob(md_value, msg, 
1074                                                lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
1075                                                &omd,
1076                                                (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
1077                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1078                         DEBUG(0,(__location__ ": Failed to marshall replPropertyMetaData for %s\n",
1079                                  ldb_dn_get_linearized(msg->dn)));
1080                         return LDB_ERR_OPERATIONS_ERROR;
1081                 }
1082
1083                 ret = ldb_msg_add_empty(msg, "replPropertyMetaData", LDB_FLAG_MOD_REPLACE, &el);
1084                 if (ret != LDB_SUCCESS) {
1085                         DEBUG(0,(__location__ ": Failed to add updated replPropertyMetaData %s\n",
1086                                  ldb_dn_get_linearized(msg->dn)));
1087                         return ret;
1088                 }
1089
1090                 el->num_values = 1;
1091                 el->values = md_value;
1092         }
1093
1094         return LDB_SUCCESS;     
1095 }
1096
1097
1098 struct parsed_dn {
1099         struct dsdb_dn *dsdb_dn;
1100         struct GUID *guid;
1101         struct ldb_val *v;
1102 };
1103
1104 static int parsed_dn_compare(struct parsed_dn *pdn1, struct parsed_dn *pdn2)
1105 {
1106         return GUID_compare(pdn1->guid, pdn2->guid);
1107 }
1108
1109 static struct parsed_dn *parsed_dn_find(struct parsed_dn *pdn, int count, struct GUID *guid)
1110 {
1111         struct parsed_dn *ret;
1112         BINARY_ARRAY_SEARCH(pdn, count, guid, guid, GUID_compare, ret);
1113         return ret;
1114 }
1115
1116 /*
1117   get a series of message element values as an array of DNs and GUIDs
1118   the result is sorted by GUID
1119  */
1120 static int get_parsed_dns(struct ldb_module *module, TALLOC_CTX *mem_ctx,
1121                           struct ldb_message_element *el, struct parsed_dn **pdn,
1122                           const char *ldap_oid)
1123 {
1124         int i;
1125         struct ldb_context *ldb = ldb_module_get_ctx(module);
1126
1127         if (el == NULL) {
1128                 *pdn = NULL;
1129                 return LDB_SUCCESS;
1130         }
1131
1132         (*pdn) = talloc_array(mem_ctx, struct parsed_dn, el->num_values);
1133         if (!*pdn) {
1134                 ldb_module_oom(module);
1135                 return LDB_ERR_OPERATIONS_ERROR;
1136         }
1137
1138         for (i=0; i<el->num_values; i++) {
1139                 struct ldb_val *v = &el->values[i];
1140                 NTSTATUS status;
1141                 struct ldb_dn *dn;
1142                 struct parsed_dn *p;
1143
1144                 p = &(*pdn)[i];
1145
1146                 p->dsdb_dn = dsdb_dn_parse(*pdn, ldb, v, ldap_oid);
1147                 if (p->dsdb_dn == NULL) {
1148                         return LDB_ERR_INVALID_DN_SYNTAX;
1149                 }
1150
1151                 dn = p->dsdb_dn->dn;
1152
1153                 p->guid = talloc(*pdn, struct GUID);
1154                 if (p->guid == NULL) {
1155                         ldb_module_oom(module);
1156                         return LDB_ERR_OPERATIONS_ERROR;
1157                 }
1158
1159                 status = dsdb_get_extended_dn_guid(dn, p->guid, "GUID");
1160                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1161                         /* we got a DN without a GUID - go find the GUID */
1162                         int ret = dsdb_find_guid_by_dn(ldb, dn, p->guid);
1163                         if (ret != LDB_SUCCESS) {
1164                                 ldb_asprintf_errstring(ldb, "Unable to find GUID for DN %s\n",
1165                                                        ldb_dn_get_linearized(dn));
1166                                 return ret;
1167                         }
1168                 } else if (!NT_STATUS_IS_OK(status)) {
1169                         return LDB_ERR_OPERATIONS_ERROR;
1170                 }
1171
1172                 /* keep a pointer to the original ldb_val */
1173                 p->v = v;
1174         }
1175
1176         qsort(*pdn, el->num_values, sizeof((*pdn)[0]), (comparison_fn_t)parsed_dn_compare);
1177
1178         return LDB_SUCCESS;
1179 }
1180
1181 /*
1182   build a new extended DN, including all meta data fields
1183
1184   DELETED             = 1 or missing
1185   RMD_ADDTIME         = originating_add_time
1186   RMD_INVOCID         = originating_invocation_id
1187   RMD_CHANGETIME      = originating_change_time
1188   RMD_ORIGINATING_USN = originating_usn
1189   RMD_LOCAL_USN       = local_usn
1190   RMD_VERSION         = version
1191  */
1192 static int replmd_build_la_val(TALLOC_CTX *mem_ctx, struct ldb_val *v, struct dsdb_dn *dsdb_dn,
1193                                const struct GUID *invocation_id, uint64_t seq_num,
1194                                uint64_t local_usn, NTTIME nttime, uint32_t version, bool deleted)
1195 {
1196         struct ldb_dn *dn = dsdb_dn->dn;
1197         const char *tstring, *usn_string;
1198         struct ldb_val tval;
1199         struct ldb_val iid;
1200         struct ldb_val usnv, local_usnv;
1201         struct ldb_val vers;
1202         NTSTATUS status;
1203         int ret;
1204         const char *dnstring;
1205         char *vstring;
1206
1207         tstring = talloc_asprintf(mem_ctx, "%llu", (unsigned long long)nttime);
1208         if (!tstring) {
1209                 return LDB_ERR_OPERATIONS_ERROR;
1210         }
1211         tval = data_blob_string_const(tstring);
1212
1213         usn_string = talloc_asprintf(mem_ctx, "%llu", (unsigned long long)seq_num);
1214         if (!usn_string) {
1215                 return LDB_ERR_OPERATIONS_ERROR;
1216         }
1217         usnv = data_blob_string_const(usn_string);
1218
1219         usn_string = talloc_asprintf(mem_ctx, "%llu", (unsigned long long)local_usn);
1220         if (!usn_string) {
1221                 return LDB_ERR_OPERATIONS_ERROR;
1222         }
1223         local_usnv = data_blob_string_const(usn_string);
1224
1225         vstring = talloc_asprintf(mem_ctx, "%lu", (unsigned long)version);
1226         vers = data_blob_string_const(vstring);
1227
1228         status = GUID_to_ndr_blob(invocation_id, dn, &iid);
1229         if (!NT_STATUS_IS_OK(status)) {
1230                 return LDB_ERR_OPERATIONS_ERROR;
1231         }
1232
1233         if (deleted) {
1234                 struct ldb_val dv;
1235                 dv = data_blob_string_const("1");
1236                 ret = ldb_dn_set_extended_component(dn, "DELETED", &dv);
1237         } else {
1238                 ret = ldb_dn_set_extended_component(dn, "DELETED", NULL);
1239         }
1240         if (ret != LDB_SUCCESS) return ret;
1241         ret = ldb_dn_set_extended_component(dn, "RMD_ADDTIME", &tval);
1242         if (ret != LDB_SUCCESS) return ret;
1243         ret = ldb_dn_set_extended_component(dn, "RMD_INVOCID", &iid);
1244         if (ret != LDB_SUCCESS) return ret;
1245         ret = ldb_dn_set_extended_component(dn, "RMD_CHANGETIME", &tval);
1246         if (ret != LDB_SUCCESS) return ret;
1247         ret = ldb_dn_set_extended_component(dn, "RMD_LOCAL_USN", &local_usnv);
1248         if (ret != LDB_SUCCESS) return ret;
1249         ret = ldb_dn_set_extended_component(dn, "RMD_ORIGINATING_USN", &usnv);
1250         if (ret != LDB_SUCCESS) return ret;
1251         ret = ldb_dn_set_extended_component(dn, "RMD_VERSION", &vers);
1252         if (ret != LDB_SUCCESS) return ret;
1253
1254         dnstring = dsdb_dn_get_extended_linearized(mem_ctx, dsdb_dn, 1);
1255         if (dnstring == NULL) {
1256                 return LDB_ERR_OPERATIONS_ERROR;
1257         }
1258         *v = data_blob_string_const(dnstring);
1259
1260         return LDB_SUCCESS;
1261 }
1262
1263 static int replmd_update_la_val(TALLOC_CTX *mem_ctx, struct ldb_val *v, struct dsdb_dn *dsdb_dn,
1264                                 struct dsdb_dn *old_dsdb_dn, const struct GUID *invocation_id,
1265                                 uint64_t seq_num, uint64_t local_usn, NTTIME nttime,
1266                                 uint32_t version, bool deleted);
1267
1268 /*
1269   check if any links need upgrading from w2k format
1270  */
1271 static int replmd_check_upgrade_links(struct parsed_dn *dns, uint32_t count, const struct GUID *invocation_id)
1272 {
1273         int i;
1274         for (i=0; i<count; i++) {
1275                 NTSTATUS status;
1276                 uint32_t version;
1277                 int ret;
1278
1279                 status = dsdb_get_extended_dn_uint32(dns[i].dsdb_dn->dn, &version, "RMD_VERSION");
1280                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1281                         continue;
1282                 }
1283
1284                 /* it's an old one that needs upgrading */
1285                 ret = replmd_update_la_val(dns, dns[i].v, dns[i].dsdb_dn, dns[i].dsdb_dn, invocation_id,
1286                                            1, 1, 0, 0, false);
1287                 if (ret != LDB_SUCCESS) {
1288                         return ret;
1289                 }
1290         }
1291         return LDB_SUCCESS;
1292 }
1293
1294 /*
1295   update an extended DN, including all meta data fields
1296
1297   see replmd_build_la_val for value names
1298  */
1299 static int replmd_update_la_val(TALLOC_CTX *mem_ctx, struct ldb_val *v, struct dsdb_dn *dsdb_dn,
1300                                 struct dsdb_dn *old_dsdb_dn, const struct GUID *invocation_id,
1301                                 uint64_t seq_num, uint64_t local_usn, NTTIME nttime,
1302                                 uint32_t version, bool deleted)
1303 {
1304         struct ldb_dn *dn = dsdb_dn->dn;
1305         const char *tstring, *usn_string;
1306         struct ldb_val tval;
1307         struct ldb_val iid;
1308         struct ldb_val usnv, local_usnv;
1309         struct ldb_val vers;
1310         const struct ldb_val *old_addtime;
1311         uint32_t old_version;
1312         NTSTATUS status;
1313         int ret;
1314         const char *dnstring;
1315         char *vstring;
1316
1317         tstring = talloc_asprintf(mem_ctx, "%llu", (unsigned long long)nttime);
1318         if (!tstring) {
1319                 return LDB_ERR_OPERATIONS_ERROR;
1320         }
1321         tval = data_blob_string_const(tstring);
1322
1323         usn_string = talloc_asprintf(mem_ctx, "%llu", (unsigned long long)seq_num);
1324         if (!usn_string) {
1325                 return LDB_ERR_OPERATIONS_ERROR;
1326         }
1327         usnv = data_blob_string_const(usn_string);
1328
1329         usn_string = talloc_asprintf(mem_ctx, "%llu", (unsigned long long)local_usn);
1330         if (!usn_string) {
1331                 return LDB_ERR_OPERATIONS_ERROR;
1332         }
1333         local_usnv = data_blob_string_const(usn_string);
1334
1335         status = GUID_to_ndr_blob(invocation_id, dn, &iid);
1336         if (!NT_STATUS_IS_OK(status)) {
1337                 return LDB_ERR_OPERATIONS_ERROR;
1338         }
1339
1340         if (deleted) {
1341                 struct ldb_val dv;
1342                 dv = data_blob_string_const("1");
1343                 ret = ldb_dn_set_extended_component(dn, "DELETED", &dv);
1344         } else {
1345                 ret = ldb_dn_set_extended_component(dn, "DELETED", NULL);
1346         }
1347         if (ret != LDB_SUCCESS) return ret;
1348
1349         /* get the ADDTIME from the original */
1350         old_addtime = ldb_dn_get_extended_component(old_dsdb_dn->dn, "RMD_ADDTIME");
1351         if (old_addtime == NULL) {
1352                 old_addtime = &tval;
1353         }
1354         if (dsdb_dn != old_dsdb_dn) {
1355                 ret = ldb_dn_set_extended_component(dn, "RMD_ADDTIME", old_addtime);
1356                 if (ret != LDB_SUCCESS) return ret;
1357         }
1358
1359         /* use our invocation id */
1360         ret = ldb_dn_set_extended_component(dn, "RMD_INVOCID", &iid);
1361         if (ret != LDB_SUCCESS) return ret;
1362
1363         /* changetime is the current time */
1364         ret = ldb_dn_set_extended_component(dn, "RMD_CHANGETIME", &tval);
1365         if (ret != LDB_SUCCESS) return ret;
1366
1367         /* update the USN */
1368         ret = ldb_dn_set_extended_component(dn, "RMD_ORIGINATING_USN", &usnv);
1369         if (ret != LDB_SUCCESS) return ret;
1370
1371         ret = ldb_dn_set_extended_component(dn, "RMD_LOCAL_USN", &local_usnv);
1372         if (ret != LDB_SUCCESS) return ret;
1373
1374         /* increase the version by 1 */
1375         status = dsdb_get_extended_dn_uint32(old_dsdb_dn->dn, &old_version, "RMD_VERSION");
1376         if (NT_STATUS_IS_OK(status) && old_version >= version) {
1377                 version = old_version+1;
1378         }
1379         vstring = talloc_asprintf(dn, "%lu", (unsigned long)version);
1380         vers = data_blob_string_const(vstring);
1381         ret = ldb_dn_set_extended_component(dn, "RMD_VERSION", &vers);
1382         if (ret != LDB_SUCCESS) return ret;
1383
1384         dnstring = dsdb_dn_get_extended_linearized(mem_ctx, dsdb_dn, 1);
1385         if (dnstring == NULL) {
1386                 return LDB_ERR_OPERATIONS_ERROR;
1387         }
1388         *v = data_blob_string_const(dnstring);
1389
1390         return LDB_SUCCESS;
1391 }
1392
1393 /*
1394   handle adding a linked attribute
1395  */
1396 static int replmd_modify_la_add(struct ldb_module *module,
1397                                 struct dsdb_schema *schema,
1398                                 struct ldb_message *msg,
1399                                 struct ldb_message_element *el,
1400                                 struct ldb_message_element *old_el,
1401                                 const struct dsdb_attribute *schema_attr,
1402                                 uint64_t seq_num,
1403                                 time_t t,
1404                                 struct GUID *msg_guid)
1405 {
1406         int i;
1407         struct parsed_dn *dns, *old_dns;
1408         TALLOC_CTX *tmp_ctx = talloc_new(msg);
1409         int ret;
1410         struct ldb_val *new_values = NULL;
1411         unsigned int num_new_values = 0;
1412         unsigned old_num_values = old_el?old_el->num_values:0;
1413         const struct GUID *invocation_id;
1414         struct ldb_context *ldb = ldb_module_get_ctx(module);
1415         NTTIME now;
1416
1417         unix_to_nt_time(&now, t);
1418
1419         ret = get_parsed_dns(module, tmp_ctx, el, &dns, schema_attr->syntax->ldap_oid);
1420         if (ret != LDB_SUCCESS) {
1421                 talloc_free(tmp_ctx);
1422                 return ret;
1423         }
1424
1425         ret = get_parsed_dns(module, tmp_ctx, old_el, &old_dns, schema_attr->syntax->ldap_oid);
1426         if (ret != LDB_SUCCESS) {
1427                 talloc_free(tmp_ctx);
1428                 return ret;
1429         }
1430
1431         invocation_id = samdb_ntds_invocation_id(ldb);
1432         if (!invocation_id) {
1433                 talloc_free(tmp_ctx);
1434                 return LDB_ERR_OPERATIONS_ERROR;
1435         }
1436
1437         ret = replmd_check_upgrade_links(old_dns, old_num_values, invocation_id);
1438         if (ret != LDB_SUCCESS) {
1439                 talloc_free(tmp_ctx);
1440                 return ret;
1441         }
1442
1443         /* for each new value, see if it exists already with the same GUID */
1444         for (i=0; i<el->num_values; i++) {
1445                 struct parsed_dn *p = parsed_dn_find(old_dns, old_num_values, dns[i].guid);
1446                 if (p == NULL) {
1447                         /* this is a new linked attribute value */
1448                         new_values = talloc_realloc(tmp_ctx, new_values, struct ldb_val, num_new_values+1);
1449                         if (new_values == NULL) {
1450                                 ldb_module_oom(module);
1451                                 talloc_free(tmp_ctx);
1452                                 return LDB_ERR_OPERATIONS_ERROR;
1453                         }
1454                         ret = replmd_build_la_val(new_values, &new_values[num_new_values], dns[i].dsdb_dn,
1455                                                   invocation_id, seq_num, seq_num, now, 0, false);
1456                         if (ret != LDB_SUCCESS) {
1457                                 talloc_free(tmp_ctx);
1458                                 return ret;
1459                         }
1460                         num_new_values++;
1461                 } else {
1462                         /* this is only allowed if the GUID was
1463                            previously deleted. */
1464                         const struct ldb_val *v;
1465                         v = ldb_dn_get_extended_component(p->dsdb_dn->dn, "DELETED");
1466                         if (v == NULL) {
1467                                 ldb_asprintf_errstring(ldb, "Attribute %s already exists for target GUID %s",
1468                                                        el->name, GUID_string(tmp_ctx, p->guid));
1469                                 talloc_free(tmp_ctx);
1470                                 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
1471                         }
1472                         ret = replmd_update_la_val(old_el->values, p->v, dns[i].dsdb_dn, p->dsdb_dn,
1473                                                    invocation_id, seq_num, seq_num, now, 0, false);
1474                         if (ret != LDB_SUCCESS) {
1475                                 talloc_free(tmp_ctx);
1476                                 return ret;
1477                         }
1478                 }
1479
1480                 ret = replmd_add_backlink(module, schema, msg_guid, dns[i].guid, true, schema_attr, true);
1481                 if (ret != LDB_SUCCESS) {
1482                         talloc_free(tmp_ctx);
1483                         return ret;
1484                 }
1485         }
1486
1487         /* add the new ones on to the end of the old values, constructing a new el->values */
1488         el->values = talloc_realloc(msg->elements, old_el?old_el->values:NULL,
1489                                     struct ldb_val,
1490                                     old_num_values+num_new_values);
1491         if (el->values == NULL) {
1492                 ldb_module_oom(module);
1493                 return LDB_ERR_OPERATIONS_ERROR;
1494         }
1495
1496         memcpy(&el->values[old_num_values], new_values, num_new_values*sizeof(struct ldb_val));
1497         el->num_values = old_num_values + num_new_values;
1498
1499         talloc_steal(msg->elements, el->values);
1500         talloc_steal(el->values, new_values);
1501
1502         talloc_free(tmp_ctx);
1503
1504         /* we now tell the backend to replace all existing values
1505            with the one we have constructed */
1506         el->flags = LDB_FLAG_MOD_REPLACE;
1507
1508         return LDB_SUCCESS;
1509 }
1510
1511
1512 /*
1513   handle deleting all active linked attributes
1514  */
1515 static int replmd_modify_la_delete(struct ldb_module *module,
1516                                    struct dsdb_schema *schema,
1517                                    struct ldb_message *msg,
1518                                    struct ldb_message_element *el,
1519                                    struct ldb_message_element *old_el,
1520                                    const struct dsdb_attribute *schema_attr,
1521                                    uint64_t seq_num,
1522                                    time_t t,
1523                                    struct GUID *msg_guid)
1524 {
1525         int i;
1526         struct parsed_dn *dns, *old_dns;
1527         TALLOC_CTX *tmp_ctx = talloc_new(msg);
1528         int ret;
1529         const struct GUID *invocation_id;
1530         struct ldb_context *ldb = ldb_module_get_ctx(module);
1531         NTTIME now;
1532
1533         unix_to_nt_time(&now, t);
1534
1535         /* check if there is nothing to delete */
1536         if ((!old_el || old_el->num_values == 0) &&
1537             el->num_values == 0) {
1538                 return LDB_SUCCESS;
1539         }
1540
1541         if (!old_el || old_el->num_values == 0) {
1542                 return LDB_ERR_NO_SUCH_ATTRIBUTE;
1543         }
1544
1545         ret = get_parsed_dns(module, tmp_ctx, el, &dns, schema_attr->syntax->ldap_oid);
1546         if (ret != LDB_SUCCESS) {
1547                 talloc_free(tmp_ctx);
1548                 return ret;
1549         }
1550
1551         ret = get_parsed_dns(module, tmp_ctx, old_el, &old_dns, schema_attr->syntax->ldap_oid);
1552         if (ret != LDB_SUCCESS) {
1553                 talloc_free(tmp_ctx);
1554                 return ret;
1555         }
1556
1557         invocation_id = samdb_ntds_invocation_id(ldb);
1558         if (!invocation_id) {
1559                 return LDB_ERR_OPERATIONS_ERROR;
1560         }
1561
1562         ret = replmd_check_upgrade_links(old_dns, old_el->num_values, invocation_id);
1563         if (ret != LDB_SUCCESS) {
1564                 talloc_free(tmp_ctx);
1565                 return ret;
1566         }
1567
1568         el->values = NULL;
1569
1570         /* see if we are being asked to delete any links that
1571            don't exist or are already deleted */
1572         for (i=0; i<el->num_values; i++) {
1573                 struct parsed_dn *p = &dns[i];
1574                 struct parsed_dn *p2;
1575                 const struct ldb_val *v;
1576
1577                 p2 = parsed_dn_find(old_dns, old_el->num_values, p->guid);
1578                 if (!p2) {
1579                         ldb_asprintf_errstring(ldb, "Attribute %s doesn't exist for target GUID %s",
1580                                                el->name, GUID_string(tmp_ctx, p->guid));
1581                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
1582                 }
1583                 v = ldb_dn_get_extended_component(p2->dsdb_dn->dn, "DELETED");
1584                 if (v) {
1585                         ldb_asprintf_errstring(ldb, "Attribute %s already deleted for target GUID %s",
1586                                                el->name, GUID_string(tmp_ctx, p->guid));
1587                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
1588                 }
1589         }
1590
1591         /* for each new value, see if it exists already with the same GUID
1592            if it is not already deleted and matches the delete list then delete it
1593         */
1594         for (i=0; i<old_el->num_values; i++) {
1595                 struct parsed_dn *p = &old_dns[i];
1596                 const struct ldb_val *v;
1597
1598                 if (dns && parsed_dn_find(dns, el->num_values, p->guid) == NULL) {
1599                         continue;
1600                 }
1601
1602                 v = ldb_dn_get_extended_component(p->dsdb_dn->dn, "DELETED");
1603                 if (v != NULL) continue;
1604
1605                 ret = replmd_update_la_val(old_el->values, p->v, p->dsdb_dn, p->dsdb_dn,
1606                                            invocation_id, seq_num, seq_num, now, 0, true);
1607                 if (ret != LDB_SUCCESS) {
1608                         talloc_free(tmp_ctx);
1609                         return ret;
1610                 }
1611
1612                 ret = replmd_add_backlink(module, schema, msg_guid, old_dns[i].guid, false, schema_attr, true);
1613                 if (ret != LDB_SUCCESS) {
1614                         talloc_free(tmp_ctx);
1615                         return ret;
1616                 }
1617         }
1618
1619         el->values = talloc_steal(msg->elements, old_el->values);
1620         el->num_values = old_el->num_values;
1621
1622         talloc_free(tmp_ctx);
1623
1624         /* we now tell the backend to replace all existing values
1625            with the one we have constructed */
1626         el->flags = LDB_FLAG_MOD_REPLACE;
1627
1628         return LDB_SUCCESS;
1629 }
1630
1631 /*
1632   handle replacing a linked attribute
1633  */
1634 static int replmd_modify_la_replace(struct ldb_module *module,
1635                                     struct dsdb_schema *schema,
1636                                     struct ldb_message *msg,
1637                                     struct ldb_message_element *el,
1638                                     struct ldb_message_element *old_el,
1639                                     const struct dsdb_attribute *schema_attr,
1640                                     uint64_t seq_num,
1641                                     time_t t,
1642                                     struct GUID *msg_guid)
1643 {
1644         int i;
1645         struct parsed_dn *dns, *old_dns;
1646         TALLOC_CTX *tmp_ctx = talloc_new(msg);
1647         int ret;
1648         const struct GUID *invocation_id;
1649         struct ldb_context *ldb = ldb_module_get_ctx(module);
1650         struct ldb_val *new_values = NULL;
1651         uint32_t num_new_values = 0;
1652         unsigned old_num_values = old_el?old_el->num_values:0;
1653         NTTIME now;
1654
1655         unix_to_nt_time(&now, t);
1656
1657         /* check if there is nothing to replace */
1658         if ((!old_el || old_el->num_values == 0) &&
1659             el->num_values == 0) {
1660                 return LDB_SUCCESS;
1661         }
1662
1663         ret = get_parsed_dns(module, tmp_ctx, el, &dns, schema_attr->syntax->ldap_oid);
1664         if (ret != LDB_SUCCESS) {
1665                 talloc_free(tmp_ctx);
1666                 return ret;
1667         }
1668
1669         ret = get_parsed_dns(module, tmp_ctx, old_el, &old_dns, schema_attr->syntax->ldap_oid);
1670         if (ret != LDB_SUCCESS) {
1671                 talloc_free(tmp_ctx);
1672                 return ret;
1673         }
1674
1675         invocation_id = samdb_ntds_invocation_id(ldb);
1676         if (!invocation_id) {
1677                 return LDB_ERR_OPERATIONS_ERROR;
1678         }
1679
1680         ret = replmd_check_upgrade_links(old_dns, old_num_values, invocation_id);
1681         if (ret != LDB_SUCCESS) {
1682                 talloc_free(tmp_ctx);
1683                 return ret;
1684         }
1685
1686         /* mark all the old ones as deleted */
1687         for (i=0; i<old_num_values; i++) {
1688                 struct parsed_dn *old_p = &old_dns[i];
1689                 struct parsed_dn *p;
1690                 const struct ldb_val *v;
1691
1692                 v = ldb_dn_get_extended_component(old_p->dsdb_dn->dn, "DELETED");
1693                 if (v) continue;
1694
1695                 ret = replmd_add_backlink(module, schema, msg_guid, old_dns[i].guid, false, schema_attr, false);
1696                 if (ret != LDB_SUCCESS) {
1697                         talloc_free(tmp_ctx);
1698                         return ret;
1699                 }
1700
1701                 p = parsed_dn_find(dns, el->num_values, old_p->guid);
1702                 if (p) {
1703                         /* we don't delete it if we are re-adding it */
1704                         continue;
1705                 }
1706
1707                 ret = replmd_update_la_val(old_el->values, old_p->v, old_p->dsdb_dn, old_p->dsdb_dn,
1708                                            invocation_id, seq_num, seq_num, now, 0, true);
1709                 if (ret != LDB_SUCCESS) {
1710                         talloc_free(tmp_ctx);
1711                         return ret;
1712                 }
1713         }
1714
1715         /* for each new value, either update its meta-data, or add it
1716          * to old_el
1717         */
1718         for (i=0; i<el->num_values; i++) {
1719                 struct parsed_dn *p = &dns[i], *old_p;
1720
1721                 if (old_dns &&
1722                     (old_p = parsed_dn_find(old_dns,
1723                                             old_num_values, p->guid)) != NULL) {
1724                         /* update in place */
1725                         ret = replmd_update_la_val(old_el->values, old_p->v, old_p->dsdb_dn,
1726                                                    old_p->dsdb_dn, invocation_id,
1727                                                    seq_num, seq_num, now, 0, false);
1728                         if (ret != LDB_SUCCESS) {
1729                                 talloc_free(tmp_ctx);
1730                                 return ret;
1731                         }
1732                 } else {
1733                         /* add a new one */
1734                         new_values = talloc_realloc(tmp_ctx, new_values, struct ldb_val,
1735                                                     num_new_values+1);
1736                         if (new_values == NULL) {
1737                                 ldb_module_oom(module);
1738                                 talloc_free(tmp_ctx);
1739                                 return LDB_ERR_OPERATIONS_ERROR;
1740                         }
1741                         ret = replmd_build_la_val(new_values, &new_values[num_new_values], dns[i].dsdb_dn,
1742                                                   invocation_id, seq_num, seq_num, now, 0, false);
1743                         if (ret != LDB_SUCCESS) {
1744                                 talloc_free(tmp_ctx);
1745                                 return ret;
1746                         }
1747                         num_new_values++;
1748                 }
1749
1750                 ret = replmd_add_backlink(module, schema, msg_guid, dns[i].guid, true, schema_attr, false);
1751                 if (ret != LDB_SUCCESS) {
1752                         talloc_free(tmp_ctx);
1753                         return ret;
1754                 }
1755         }
1756
1757         /* add the new values to the end of old_el */
1758         if (num_new_values != 0) {
1759                 el->values = talloc_realloc(msg->elements, old_el?old_el->values:NULL,
1760                                             struct ldb_val, old_num_values+num_new_values);
1761                 if (el->values == NULL) {
1762                         ldb_module_oom(module);
1763                         return LDB_ERR_OPERATIONS_ERROR;
1764                 }
1765                 memcpy(&el->values[old_num_values], &new_values[0],
1766                        sizeof(struct ldb_val)*num_new_values);
1767                 el->num_values = old_num_values + num_new_values;
1768                 talloc_steal(msg->elements, new_values);
1769         } else {
1770                 el->values = old_el->values;
1771                 el->num_values = old_el->num_values;
1772                 talloc_steal(msg->elements, el->values);
1773         }
1774
1775         talloc_free(tmp_ctx);
1776
1777         /* we now tell the backend to replace all existing values
1778            with the one we have constructed */
1779         el->flags = LDB_FLAG_MOD_REPLACE;
1780
1781         return LDB_SUCCESS;
1782 }
1783
1784
1785 /*
1786   handle linked attributes in modify requests
1787  */
1788 static int replmd_modify_handle_linked_attribs(struct ldb_module *module,
1789                                                struct ldb_message *msg,
1790                                                uint64_t seq_num, time_t t)
1791 {
1792         struct ldb_result *res;
1793         int ret, i;
1794         struct ldb_context *ldb = ldb_module_get_ctx(module);
1795         struct ldb_message *old_msg;
1796         struct dsdb_schema *schema = dsdb_get_schema(ldb);
1797         struct GUID old_guid;
1798
1799         if (seq_num == 0) {
1800                 /* there the replmd_update_rpmd code has already
1801                  * checked and saw that there are no linked
1802                  * attributes */
1803                 return LDB_SUCCESS;
1804         }
1805
1806 #if !W2K3_LINKED_ATTRIBUTES
1807         return LDB_SUCCESS;
1808 #endif
1809
1810         if (dsdb_functional_level(ldb) == DS_DOMAIN_FUNCTION_2000) {
1811                 /* don't do anything special for linked attributes */
1812                 return LDB_SUCCESS;
1813         }
1814
1815         ret = dsdb_module_search_dn(module, msg, &res, msg->dn, NULL,
1816                                     DSDB_SEARCH_SHOW_DELETED |
1817                                     DSDB_SEARCH_REVEAL_INTERNALS |
1818                                     DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
1819         if (ret != LDB_SUCCESS) {
1820                 return ret;
1821         }
1822         old_msg = res->msgs[0];
1823
1824         old_guid = samdb_result_guid(old_msg, "objectGUID");
1825
1826         for (i=0; i<msg->num_elements; i++) {
1827                 struct ldb_message_element *el = &msg->elements[i];
1828                 struct ldb_message_element *old_el, *new_el;
1829                 const struct dsdb_attribute *schema_attr
1830                         = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
1831                 if (!schema_attr) {
1832                         ldb_asprintf_errstring(ldb,
1833                                                "attribute %s is not a valid attribute in schema", el->name);
1834                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
1835                 }
1836                 if (schema_attr->linkID == 0) {
1837                         continue;
1838                 }
1839                 if ((schema_attr->linkID & 1) == 1) {
1840                         /* Odd is for the target.  Illegal to modify */
1841                         ldb_asprintf_errstring(ldb,
1842                                                "attribute %s must not be modified directly, it is a linked attribute", el->name);
1843                         return LDB_ERR_UNWILLING_TO_PERFORM;
1844                 }
1845                 old_el = ldb_msg_find_element(old_msg, el->name);
1846                 switch (el->flags & LDB_FLAG_MOD_MASK) {
1847                 case LDB_FLAG_MOD_REPLACE:
1848                         ret = replmd_modify_la_replace(module, schema, msg, el, old_el, schema_attr, seq_num, t, &old_guid);
1849                         break;
1850                 case LDB_FLAG_MOD_DELETE:
1851                         ret = replmd_modify_la_delete(module, schema, msg, el, old_el, schema_attr, seq_num, t, &old_guid);
1852                         break;
1853                 case LDB_FLAG_MOD_ADD:
1854                         ret = replmd_modify_la_add(module, schema, msg, el, old_el, schema_attr, seq_num, t, &old_guid);
1855                         break;
1856                 default:
1857                         ldb_asprintf_errstring(ldb,
1858                                                "invalid flags 0x%x for %s linked attribute",
1859                                                el->flags, el->name);
1860                         return LDB_ERR_UNWILLING_TO_PERFORM;
1861                 }
1862                 if (ret != LDB_SUCCESS) {
1863                         return ret;
1864                 }
1865                 if (old_el) {
1866                         ldb_msg_remove_attr(old_msg, el->name);
1867                 }
1868                 ldb_msg_add_empty(old_msg, el->name, 0, &new_el);
1869                 new_el->num_values = el->num_values;
1870                 new_el->values = el->values;
1871
1872                 /* TODO: this relises a bit too heavily on the exact
1873                    behaviour of ldb_msg_find_element and
1874                    ldb_msg_remove_element */
1875                 old_el = ldb_msg_find_element(msg, el->name);
1876                 if (old_el != el) {
1877                         ldb_msg_remove_element(msg, old_el);
1878                         i--;
1879                 }
1880         }
1881
1882         talloc_free(res);
1883         return ret;
1884 }
1885
1886
1887
1888 static int replmd_modify(struct ldb_module *module, struct ldb_request *req)
1889 {
1890         struct ldb_context *ldb;
1891         struct replmd_replicated_request *ac;
1892         struct ldb_request *down_req;
1893         struct ldb_message *msg;
1894         time_t t = time(NULL);
1895         int ret;
1896
1897         /* do not manipulate our control entries */
1898         if (ldb_dn_is_special(req->op.mod.message->dn)) {
1899                 return ldb_next_request(module, req);
1900         }
1901
1902         ldb = ldb_module_get_ctx(module);
1903
1904         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_modify\n");
1905
1906         ac = replmd_ctx_init(module, req);
1907         if (!ac) {
1908                 return LDB_ERR_OPERATIONS_ERROR;
1909         }
1910
1911         /* we have to copy the message as the caller might have it as a const */
1912         msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
1913         if (msg == NULL) {
1914                 ldb_oom(ldb);
1915                 talloc_free(ac);
1916                 return LDB_ERR_OPERATIONS_ERROR;
1917         }
1918
1919         ret = replmd_update_rpmd(module, ac->schema, msg, &ac->seq_num, t);
1920         if (ret != LDB_SUCCESS) {
1921                 talloc_free(ac);
1922                 return ret;
1923         }
1924
1925         ret = replmd_modify_handle_linked_attribs(module, msg, ac->seq_num, t);
1926         if (ret != LDB_SUCCESS) {
1927                 talloc_free(ac);
1928                 return ret;
1929         }
1930
1931         /* TODO:
1932          * - replace the old object with the newly constructed one
1933          */
1934
1935         ret = ldb_build_mod_req(&down_req, ldb, ac,
1936                                 msg,
1937                                 req->controls,
1938                                 ac, replmd_op_callback,
1939                                 req);
1940         if (ret != LDB_SUCCESS) {
1941                 talloc_free(ac);
1942                 return ret;
1943         }
1944         talloc_steal(down_req, msg);
1945
1946         /* we only change whenChanged and uSNChanged if the seq_num
1947            has changed */
1948         if (ac->seq_num != 0) {
1949                 if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
1950                         talloc_free(ac);
1951                         return ret;
1952                 }
1953
1954                 if (add_uint64_element(msg, "uSNChanged", ac->seq_num) != LDB_SUCCESS) {
1955                         talloc_free(ac);
1956                         return ret;
1957                 }
1958         }
1959
1960         /* go on with the call chain */
1961         return ldb_next_request(module, down_req);
1962 }
1963
1964 static int replmd_rename_callback(struct ldb_request *req, struct ldb_reply *ares);
1965
1966 /*
1967   handle a rename request
1968
1969   On a rename we need to do an extra ldb_modify which sets the
1970   whenChanged and uSNChanged attributes.  We do this in a callback after the success.
1971  */
1972 static int replmd_rename(struct ldb_module *module, struct ldb_request *req)
1973 {
1974         struct ldb_context *ldb;
1975         struct replmd_replicated_request *ac;
1976         int ret;
1977         struct ldb_request *down_req;
1978
1979         /* do not manipulate our control entries */
1980         if (ldb_dn_is_special(req->op.mod.message->dn)) {
1981                 return ldb_next_request(module, req);
1982         }
1983
1984         ldb = ldb_module_get_ctx(module);
1985
1986         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_rename\n");
1987
1988         ac = replmd_ctx_init(module, req);
1989         if (!ac) {
1990                 return LDB_ERR_OPERATIONS_ERROR;
1991         }
1992         ret = ldb_build_rename_req(&down_req, ldb, ac,
1993                                    ac->req->op.rename.olddn,
1994                                    ac->req->op.rename.newdn,
1995                                    ac->req->controls,
1996                                    ac, replmd_rename_callback,
1997                                    ac->req);
1998
1999         if (ret != LDB_SUCCESS) {
2000                 talloc_free(ac);
2001                 return ret;
2002         }
2003
2004         /* go on with the call chain */
2005         return ldb_next_request(module, down_req);
2006 }
2007
2008 /* After the rename is compleated, update the whenchanged etc */
2009 static int replmd_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
2010 {
2011         struct ldb_context *ldb;
2012         struct replmd_replicated_request *ac;
2013         struct ldb_request *down_req;
2014         struct ldb_message *msg;
2015         time_t t = time(NULL);
2016         int ret;
2017
2018         ac = talloc_get_type(req->context, struct replmd_replicated_request);
2019         ldb = ldb_module_get_ctx(ac->module);
2020
2021         if (ares->error != LDB_SUCCESS) {
2022                 return ldb_module_done(ac->req, ares->controls,
2023                                         ares->response, ares->error);
2024         }
2025
2026         if (ares->type != LDB_REPLY_DONE) {
2027                 ldb_set_errstring(ldb,
2028                                   "invalid ldb_reply_type in callback");
2029                 talloc_free(ares);
2030                 return ldb_module_done(ac->req, NULL, NULL,
2031                                         LDB_ERR_OPERATIONS_ERROR);
2032         }
2033
2034         /* Get a sequence number from the backend */
2035         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &ac->seq_num);
2036         if (ret != LDB_SUCCESS) {
2037                 return ret;
2038         }
2039
2040         /* TODO:
2041          * - replace the old object with the newly constructed one
2042          */
2043
2044         msg = ldb_msg_new(ac);
2045         if (msg == NULL) {
2046                 ldb_oom(ldb);
2047                 return LDB_ERR_OPERATIONS_ERROR;
2048         }
2049
2050         msg->dn = ac->req->op.rename.newdn;
2051
2052         ret = ldb_build_mod_req(&down_req, ldb, ac,
2053                                 msg,
2054                                 req->controls,
2055                                 ac, replmd_op_callback,
2056                                 req);
2057
2058         if (ret != LDB_SUCCESS) {
2059                 talloc_free(ac);
2060                 return ret;
2061         }
2062         talloc_steal(down_req, msg);
2063
2064         if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
2065                 talloc_free(ac);
2066                 return ret;
2067         }
2068         
2069         if (add_uint64_element(msg, "uSNChanged", ac->seq_num) != LDB_SUCCESS) {
2070                 talloc_free(ac);
2071                 return ret;
2072         }
2073
2074         /* go on with the call chain - do the modify after the rename */
2075         return ldb_next_request(ac->module, down_req);
2076 }
2077
2078 /* remove forwards and backlinks as needed when an object
2079    is deleted */
2080 static int replmd_delete_remove_link(struct ldb_module *module,
2081                                      struct dsdb_schema *schema,
2082                                      struct ldb_dn *dn,
2083                                      struct ldb_message_element *el,
2084                                      const struct dsdb_attribute *sa)
2085 {
2086         int i;
2087         TALLOC_CTX *tmp_ctx = talloc_new(module);
2088         struct ldb_context *ldb = ldb_module_get_ctx(module);
2089
2090         for (i=0; i<el->num_values; i++) {
2091                 struct dsdb_dn *dsdb_dn;
2092                 NTSTATUS status;
2093                 int ret;
2094                 struct GUID guid2;
2095                 struct ldb_message *msg;
2096                 const struct dsdb_attribute *target_attr;
2097                 struct ldb_message_element *el2;
2098                 struct ldb_val dn_val;
2099
2100                 if (dsdb_dn_is_deleted_val(&el->values[i])) {
2101                         continue;
2102                 }
2103
2104                 dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, &el->values[i], sa->syntax->ldap_oid);
2105                 if (!dsdb_dn) {
2106                         talloc_free(tmp_ctx);
2107                         return LDB_ERR_OPERATIONS_ERROR;
2108                 }
2109
2110                 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid2, "GUID");
2111                 if (!NT_STATUS_IS_OK(status)) {
2112                         talloc_free(tmp_ctx);
2113                         return LDB_ERR_OPERATIONS_ERROR;
2114                 }
2115
2116                 /* remove the link */
2117                 msg = ldb_msg_new(tmp_ctx);
2118                 if (!msg) {
2119                         ldb_module_oom(module);
2120                         talloc_free(tmp_ctx);
2121                         return LDB_ERR_OPERATIONS_ERROR;
2122                 }
2123
2124
2125                 msg->dn = dsdb_dn->dn;
2126
2127                 target_attr = dsdb_attribute_by_linkID(schema, sa->linkID ^ 1);
2128                 if (target_attr == NULL) {
2129                         continue;
2130                 }
2131
2132                 ret = ldb_msg_add_empty(msg, target_attr->lDAPDisplayName, LDB_FLAG_MOD_DELETE, &el2);
2133                 if (ret != LDB_SUCCESS) {
2134                         ldb_module_oom(module);
2135                         talloc_free(tmp_ctx);
2136                         return LDB_ERR_OPERATIONS_ERROR;
2137                 }
2138                 dn_val = data_blob_string_const(ldb_dn_get_linearized(dn));
2139                 el2->values = &dn_val;
2140                 el2->num_values = 1;
2141
2142                 ret = dsdb_module_modify(module, msg, 0);
2143                 if (ret != LDB_SUCCESS) {
2144                         talloc_free(tmp_ctx);
2145                         return ret;
2146                 }
2147         }
2148         talloc_free(tmp_ctx);
2149         return LDB_SUCCESS;
2150 }
2151
2152
2153 /*
2154   handle update of replication meta data for deletion of objects
2155
2156   This also handles the mapping of delete to a rename operation
2157   to allow deletes to be replicated.
2158  */
2159 static int replmd_delete(struct ldb_module *module, struct ldb_request *req)
2160 {
2161         int ret = LDB_ERR_OTHER;
2162         bool retb;
2163         struct ldb_dn *old_dn, *new_dn;
2164         const char *rdn_name;
2165         const struct ldb_val *rdn_value, *new_rdn_value;
2166         struct GUID guid;
2167         struct ldb_context *ldb = ldb_module_get_ctx(module);
2168         struct dsdb_schema *schema = dsdb_get_schema(ldb);
2169         struct ldb_message *msg, *old_msg;
2170         struct ldb_message_element *el;
2171         TALLOC_CTX *tmp_ctx;
2172         struct ldb_result *res, *parent_res;
2173         const char *preserved_attrs[] = {
2174                 /* yes, this really is a hard coded list. See MS-ADTS
2175                    section 3.1.1.5.5.1.1 */
2176                 "nTSecurityDescriptor", "attributeID", "attributeSyntax", "dNReferenceUpdate", "dNSHostName",
2177                 "flatName", "governsID", "groupType", "instanceType", "lDAPDisplayName", "legacyExchangeDN",
2178                 "isDeleted", "isRecycled", "lastKnownParent", "msDS-LastKnownRDN", "mS-DS-CreatorSID",
2179                 "mSMQOwnerID", "nCName", "objectClass", "distinguishedName", "objectGUID", "objectSid",
2180                 "oMSyntax", "proxiedObjectName", "name", "replPropertyMetaData", "sAMAccountName",
2181                 "securityIdentifier", "sIDHistory", "subClassOf", "systemFlags", "trustPartner", "trustDirection",
2182                 "trustType", "trustAttributes", "userAccountControl", "uSNChanged", "uSNCreated", "whenCreate",
2183                 NULL};
2184         uint32_t el_count = 0;
2185         int i;
2186
2187         tmp_ctx = talloc_new(ldb);
2188
2189         old_dn = ldb_dn_copy(tmp_ctx, req->op.del.dn);
2190
2191         /* we need the complete msg off disk, so we can work out which
2192            attributes need to be removed */
2193         ret = dsdb_module_search_dn(module, tmp_ctx, &res, old_dn, NULL,
2194                                     DSDB_SEARCH_SHOW_DELETED |
2195                                     DSDB_SEARCH_REVEAL_INTERNALS |
2196                                     DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
2197         if (ret != LDB_SUCCESS) {
2198                 talloc_free(tmp_ctx);
2199                 return ret;
2200         }
2201         old_msg = res->msgs[0];
2202
2203         /* work out where we will be renaming this object to */
2204         ret = dsdb_get_deleted_objects_dn(ldb, tmp_ctx, old_dn, &new_dn);
2205         if (ret != LDB_SUCCESS) {
2206                 /* this is probably an attempted delete on a partition
2207                  * that doesn't allow delete operations, such as the
2208                  * schema partition */
2209                 ldb_asprintf_errstring(ldb, "No Deleted Objects container for DN %s",
2210                                        ldb_dn_get_linearized(old_dn));
2211                 talloc_free(tmp_ctx);
2212                 return LDB_ERR_UNWILLING_TO_PERFORM;
2213         }
2214
2215         rdn_name = ldb_dn_get_rdn_name(old_dn);
2216         rdn_value = ldb_dn_get_rdn_val(old_dn);
2217
2218         /* get the objects GUID from the search we just did */
2219         guid = samdb_result_guid(old_msg, "objectGUID");
2220
2221         /* Add a formatted child */
2222         retb = ldb_dn_add_child_fmt(new_dn, "%s=%s\\0ADEL:%s",
2223                                     rdn_name,
2224                                     rdn_value->data,
2225                                     GUID_string(tmp_ctx, &guid));
2226         if (!retb) {
2227                 DEBUG(0,(__location__ ": Unable to add a formatted child to dn: %s",
2228                                 ldb_dn_get_linearized(new_dn)));
2229                 talloc_free(tmp_ctx);
2230                 return LDB_ERR_OPERATIONS_ERROR;
2231         }
2232
2233         /*
2234           now we need to modify the object in the following ways:
2235
2236           - add isDeleted=TRUE
2237           - update rDN and name, with new rDN
2238           - remove linked attributes
2239           - remove objectCategory and sAMAccountType
2240           - remove attribs not on the preserved list
2241              - preserved if in above list, or is rDN
2242           - remove all linked attribs from this object
2243           - remove all links from other objects to this object
2244           - add lastKnownParent
2245           - update replPropertyMetaData?
2246
2247           see MS-ADTS "Tombstone Requirements" section 3.1.1.5.5.1.1
2248          */
2249
2250         msg = ldb_msg_new(tmp_ctx);
2251         if (msg == NULL) {
2252                 ldb_module_oom(module);
2253                 talloc_free(tmp_ctx);
2254                 return LDB_ERR_OPERATIONS_ERROR;
2255         }
2256
2257         msg->dn = old_dn;
2258
2259         ret = ldb_msg_add_string(msg, "isDeleted", "TRUE");
2260         if (ret != LDB_SUCCESS) {
2261                 DEBUG(0,(__location__ ": Failed to add isDeleted string to the msg\n"));
2262                 ldb_module_oom(module);
2263                 talloc_free(tmp_ctx);
2264                 return ret;
2265         }
2266         msg->elements[el_count++].flags = LDB_FLAG_MOD_ADD;
2267
2268         /* we need the storage form of the parent GUID */
2269         ret = dsdb_module_search_dn(module, tmp_ctx, &parent_res,
2270                                     ldb_dn_get_parent(tmp_ctx, old_dn), NULL,
2271                                     DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT |
2272                                     DSDB_SEARCH_REVEAL_INTERNALS);
2273         if (ret != LDB_SUCCESS) {
2274                 talloc_free(tmp_ctx);
2275                 return ret;
2276         }
2277
2278         ret = ldb_msg_add_steal_string(msg, "lastKnownParent",
2279                                        ldb_dn_get_extended_linearized(tmp_ctx, parent_res->msgs[0]->dn, 1));
2280         if (ret != LDB_SUCCESS) {
2281                 DEBUG(0,(__location__ ": Failed to add lastKnownParent string to the msg\n"));
2282                 ldb_module_oom(module);
2283                 talloc_free(tmp_ctx);
2284                 return ret;
2285         }
2286         msg->elements[el_count++].flags = LDB_FLAG_MOD_ADD;
2287
2288         /* work out which of the old attributes we will be removing */
2289         for (i=0; i<old_msg->num_elements; i++) {
2290                 const struct dsdb_attribute *sa;
2291                 el = &old_msg->elements[i];
2292                 sa = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
2293                 if (!sa) {
2294                         talloc_free(tmp_ctx);
2295                         return LDB_ERR_OPERATIONS_ERROR;
2296                 }
2297                 if (ldb_attr_cmp(el->name, rdn_name) == 0) {
2298                         /* don't remove the rDN */
2299                         continue;
2300                 }
2301
2302                 if (sa->linkID) {
2303                         ret = replmd_delete_remove_link(module, schema, old_dn, el, sa);
2304                         if (ret != LDB_SUCCESS) {
2305                                 talloc_free(tmp_ctx);
2306                                 return LDB_ERR_OPERATIONS_ERROR;
2307                         }
2308                 }
2309
2310                 if (!sa->linkID && ldb_attr_in_list(preserved_attrs, el->name)) {
2311                         continue;
2312                 }
2313
2314                 ret = ldb_msg_add_empty(msg, el->name, LDB_FLAG_MOD_DELETE, &el);
2315                 if (ret != LDB_SUCCESS) {
2316                         talloc_free(tmp_ctx);
2317                         ldb_module_oom(module);
2318                         return ret;
2319                 }
2320         }
2321
2322         /* work out what the new rdn value is, for updating the
2323            rDN and name fields */
2324         new_rdn_value = ldb_dn_get_rdn_val(new_dn);
2325         ret = ldb_msg_add_value(msg, rdn_name, new_rdn_value, &el);
2326         if (ret != LDB_SUCCESS) {
2327                 talloc_free(tmp_ctx);
2328                 return ret;
2329         }
2330         el->flags = LDB_FLAG_MOD_REPLACE;
2331
2332         el = ldb_msg_find_element(old_msg, "name");
2333         if (el) {
2334                 ret = ldb_msg_add_value(msg, "name", new_rdn_value, &el);
2335                 if (ret != LDB_SUCCESS) {
2336                         talloc_free(tmp_ctx);
2337                         return ret;
2338                 }
2339                 el->flags = LDB_FLAG_MOD_REPLACE;
2340         }
2341
2342         ret = dsdb_module_modify(module, msg, 0);
2343         if (ret != LDB_SUCCESS){
2344                 ldb_asprintf_errstring(ldb, "replmd_delete: Failed to modify object %s in delete - %s",
2345                                        ldb_dn_get_linearized(old_dn), ldb_errstring(ldb));
2346                 talloc_free(tmp_ctx);
2347                 return ret;
2348         }
2349
2350         /* now rename onto the new DN */
2351         ret = dsdb_module_rename(module, old_dn, new_dn, 0);
2352         if (ret != LDB_SUCCESS){
2353                 DEBUG(0,(__location__ ": Failed to rename object from '%s' to '%s' - %s\n",
2354                          ldb_dn_get_linearized(old_dn),
2355                          ldb_dn_get_linearized(new_dn),
2356                          ldb_errstring(ldb)));
2357                 talloc_free(tmp_ctx);
2358                 return ret;
2359         }
2360
2361         talloc_free(tmp_ctx);
2362
2363         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2364 }
2365
2366
2367
2368 static int replmd_replicated_request_error(struct replmd_replicated_request *ar, int ret)
2369 {
2370         return ret;
2371 }
2372
2373 static int replmd_replicated_request_werror(struct replmd_replicated_request *ar, WERROR status)
2374 {
2375         int ret = LDB_ERR_OTHER;
2376         /* TODO: do some error mapping */
2377         return ret;
2378 }
2379
2380 static int replmd_replicated_apply_add(struct replmd_replicated_request *ar)
2381 {
2382         struct ldb_context *ldb;
2383         struct ldb_request *change_req;
2384         enum ndr_err_code ndr_err;
2385         struct ldb_message *msg;
2386         struct replPropertyMetaDataBlob *md;
2387         struct ldb_val md_value;
2388         uint32_t i;
2389         int ret;
2390
2391         /*
2392          * TODO: check if the parent object exist
2393          */
2394
2395         /*
2396          * TODO: handle the conflict case where an object with the
2397          *       same name exist
2398          */
2399
2400         ldb = ldb_module_get_ctx(ar->module);
2401         msg = ar->objs->objects[ar->index_current].msg;
2402         md = ar->objs->objects[ar->index_current].meta_data;
2403
2404         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &ar->seq_num);
2405         if (ret != LDB_SUCCESS) {
2406                 return replmd_replicated_request_error(ar, ret);
2407         }
2408
2409         ret = ldb_msg_add_value(msg, "objectGUID", &ar->objs->objects[ar->index_current].guid_value, NULL);
2410         if (ret != LDB_SUCCESS) {
2411                 return replmd_replicated_request_error(ar, ret);
2412         }
2413
2414         ret = ldb_msg_add_string(msg, "whenChanged", ar->objs->objects[ar->index_current].when_changed);
2415         if (ret != LDB_SUCCESS) {
2416                 return replmd_replicated_request_error(ar, ret);
2417         }
2418
2419         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNCreated", ar->seq_num);
2420         if (ret != LDB_SUCCESS) {
2421                 return replmd_replicated_request_error(ar, ret);
2422         }
2423
2424         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", ar->seq_num);
2425         if (ret != LDB_SUCCESS) {
2426                 return replmd_replicated_request_error(ar, ret);
2427         }
2428
2429         /* remove any message elements that have zero values */
2430         for (i=0; i<msg->num_elements; i++) {
2431                 struct ldb_message_element *el = &msg->elements[i];
2432
2433                 if (el->num_values == 0) {
2434                         DEBUG(4,(__location__ ": Removing attribute %s with num_values==0\n",
2435                                  el->name));
2436                         memmove(el, el+1, sizeof(*el)*(msg->num_elements - (i+1)));
2437                         msg->num_elements--;
2438                         i--;
2439                         continue;
2440                 }
2441         }
2442         
2443         /*
2444          * the meta data array is already sorted by the caller
2445          */
2446         for (i=0; i < md->ctr.ctr1.count; i++) {
2447                 md->ctr.ctr1.array[i].local_usn = ar->seq_num;
2448         }
2449         ndr_err = ndr_push_struct_blob(&md_value, msg, 
2450                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
2451                                        md,
2452                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
2453         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2454                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
2455                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
2456         }
2457         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &md_value, NULL);
2458         if (ret != LDB_SUCCESS) {
2459                 return replmd_replicated_request_error(ar, ret);
2460         }
2461
2462         replmd_ldb_message_sort(msg, ar->schema);
2463
2464         if (DEBUGLVL(4)) {
2465                 char *s = ldb_ldif_message_string(ldb, ar, LDB_CHANGETYPE_ADD, msg);
2466                 DEBUG(4, ("DRS replication add message:\n%s\n", s));
2467                 talloc_free(s);
2468         }
2469
2470         ret = ldb_build_add_req(&change_req,
2471                                 ldb,
2472                                 ar,
2473                                 msg,
2474                                 ar->controls,
2475                                 ar,
2476                                 replmd_op_callback,
2477                                 ar->req);
2478         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
2479
2480         return ldb_next_request(ar->module, change_req);
2481 }
2482
2483 /*
2484    return true if an update is newer than an existing entry
2485    see section 5.11 of MS-ADTS
2486 */
2487 static bool replmd_update_is_newer(const struct GUID *current_invocation_id,
2488                                    const struct GUID *update_invocation_id,
2489                                    uint32_t current_version,
2490                                    uint32_t update_version,
2491                                    NTTIME current_change_time,
2492                                    NTTIME update_change_time)
2493 {
2494         if (update_version != current_version) {
2495                 return update_version > current_version;
2496         }
2497         if (update_change_time > current_change_time) {
2498                 return true;
2499         }
2500         if (update_change_time == current_change_time) {
2501                 return GUID_compare(update_invocation_id, current_invocation_id) > 0;
2502         }
2503         return false;
2504 }
2505
2506 static bool replmd_replPropertyMetaData1_is_newer(struct replPropertyMetaData1 *cur_m,
2507                                                   struct replPropertyMetaData1 *new_m)
2508 {
2509         return replmd_update_is_newer(&cur_m->originating_invocation_id,
2510                                       &new_m->originating_invocation_id,
2511                                       cur_m->version,
2512                                       new_m->version,
2513                                       cur_m->originating_change_time,
2514                                       new_m->originating_change_time);
2515 }
2516
2517 static int replmd_replicated_apply_merge(struct replmd_replicated_request *ar)
2518 {
2519         struct ldb_context *ldb;
2520         struct ldb_request *change_req;
2521         enum ndr_err_code ndr_err;
2522         struct ldb_message *msg;
2523         struct replPropertyMetaDataBlob *rmd;
2524         struct replPropertyMetaDataBlob omd;
2525         const struct ldb_val *omd_value;
2526         struct replPropertyMetaDataBlob nmd;
2527         struct ldb_val nmd_value;
2528         uint32_t i,j,ni=0;
2529         uint32_t removed_attrs = 0;
2530         int ret;
2531
2532         ldb = ldb_module_get_ctx(ar->module);
2533         msg = ar->objs->objects[ar->index_current].msg;
2534         rmd = ar->objs->objects[ar->index_current].meta_data;
2535         ZERO_STRUCT(omd);
2536         omd.version = 1;
2537
2538         /*
2539          * TODO: check repl data is correct after a rename
2540          */
2541         if (ldb_dn_compare(msg->dn, ar->search_msg->dn) != 0) {
2542                 ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_request rename %s => %s\n",
2543                           ldb_dn_get_linearized(ar->search_msg->dn),
2544                           ldb_dn_get_linearized(msg->dn));
2545                 /* we can't use dsdb_module_rename() here as we need
2546                    the rename call to be intercepted by this module, to
2547                    allow it to process linked attribute changes */
2548                 if (ldb_rename(ldb, ar->search_msg->dn, msg->dn) != LDB_SUCCESS) {
2549                         ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_replicated_request rename %s => %s failed - %s\n",
2550                                   ldb_dn_get_linearized(ar->search_msg->dn),
2551                                   ldb_dn_get_linearized(msg->dn),
2552                                   ldb_errstring(ldb));
2553                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_DB_ERROR);
2554                 }
2555         }
2556
2557         /* find existing meta data */
2558         omd_value = ldb_msg_find_ldb_val(ar->search_msg, "replPropertyMetaData");
2559         if (omd_value) {
2560                 ndr_err = ndr_pull_struct_blob(omd_value, ar,
2561                                                lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &omd,
2562                                                (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
2563                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2564                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
2565                         return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
2566                 }
2567
2568                 if (omd.version != 1) {
2569                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
2570                 }
2571         }
2572
2573         ZERO_STRUCT(nmd);
2574         nmd.version = 1;
2575         nmd.ctr.ctr1.count = omd.ctr.ctr1.count + rmd->ctr.ctr1.count;
2576         nmd.ctr.ctr1.array = talloc_array(ar,
2577                                           struct replPropertyMetaData1,
2578                                           nmd.ctr.ctr1.count);
2579         if (!nmd.ctr.ctr1.array) return replmd_replicated_request_werror(ar, WERR_NOMEM);
2580
2581         /* first copy the old meta data */
2582         for (i=0; i < omd.ctr.ctr1.count; i++) {
2583                 nmd.ctr.ctr1.array[ni]  = omd.ctr.ctr1.array[i];
2584                 ni++;
2585         }
2586
2587         /* now merge in the new meta data */
2588         for (i=0; i < rmd->ctr.ctr1.count; i++) {
2589                 bool found = false;
2590
2591                 for (j=0; j < ni; j++) {
2592                         bool cmp;
2593
2594                         if (rmd->ctr.ctr1.array[i].attid != nmd.ctr.ctr1.array[j].attid) {
2595                                 continue;
2596                         }
2597
2598                         cmp = replmd_replPropertyMetaData1_is_newer(&nmd.ctr.ctr1.array[j],
2599                                                                     &rmd->ctr.ctr1.array[i]);
2600                         if (cmp) {
2601                                 /* replace the entry */
2602                                 nmd.ctr.ctr1.array[j] = rmd->ctr.ctr1.array[i];
2603                                 found = true;
2604                                 break;
2605                         }
2606
2607                         DEBUG(1,("Discarding older DRS attribute update to %s on %s from %s\n",
2608                                  msg->elements[i-removed_attrs].name,
2609                                  ldb_dn_get_linearized(msg->dn),
2610                                  GUID_string(ar, &rmd->ctr.ctr1.array[i].originating_invocation_id)));
2611
2612                         /* we don't want to apply this change so remove the attribute */
2613                         ldb_msg_remove_element(msg, &msg->elements[i-removed_attrs]);
2614                         removed_attrs++;
2615
2616                         found = true;
2617                         break;
2618                 }
2619
2620                 if (found) continue;
2621
2622                 nmd.ctr.ctr1.array[ni] = rmd->ctr.ctr1.array[i];
2623                 ni++;
2624         }
2625
2626         /*
2627          * finally correct the size of the meta_data array
2628          */
2629         nmd.ctr.ctr1.count = ni;
2630
2631         /*
2632          * the rdn attribute (the alias for the name attribute),
2633          * 'cn' for most objects is the last entry in the meta data array
2634          * we have stored
2635          *
2636          * sort the new meta data array
2637          */
2638         ret = replmd_replPropertyMetaDataCtr1_sort(&nmd.ctr.ctr1, ar->schema, msg->dn);
2639         if (ret != LDB_SUCCESS) {
2640                 return ret;
2641         }
2642
2643         /*
2644          * check if some replicated attributes left, otherwise skip the ldb_modify() call
2645          */
2646         if (msg->num_elements == 0) {
2647                 ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_apply_merge[%u]: skip replace\n",
2648                           ar->index_current);
2649
2650                 ar->index_current++;
2651                 return replmd_replicated_apply_next(ar);
2652         }
2653
2654         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_replicated_apply_merge[%u]: replace %u attributes\n",
2655                   ar->index_current, msg->num_elements);
2656
2657         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &ar->seq_num);
2658         if (ret != LDB_SUCCESS) {
2659                 return replmd_replicated_request_error(ar, ret);
2660         }
2661
2662         for (i=0; i<ni; i++) {
2663                 nmd.ctr.ctr1.array[i].local_usn = ar->seq_num;
2664         }
2665
2666         /* create the meta data value */
2667         ndr_err = ndr_push_struct_blob(&nmd_value, msg, 
2668                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
2669                                        &nmd,
2670                                        (ndr_push_flags_fn_t)ndr_push_replPropertyMetaDataBlob);
2671         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2672                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
2673                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
2674         }
2675
2676         /*
2677          * when we know that we'll modify the record, add the whenChanged, uSNChanged
2678          * and replPopertyMetaData attributes
2679          */
2680         ret = ldb_msg_add_string(msg, "whenChanged", ar->objs->objects[ar->index_current].when_changed);
2681         if (ret != LDB_SUCCESS) {
2682                 return replmd_replicated_request_error(ar, ret);
2683         }
2684         ret = samdb_msg_add_uint64(ldb, msg, msg, "uSNChanged", ar->seq_num);
2685         if (ret != LDB_SUCCESS) {
2686                 return replmd_replicated_request_error(ar, ret);
2687         }
2688         ret = ldb_msg_add_value(msg, "replPropertyMetaData", &nmd_value, NULL);
2689         if (ret != LDB_SUCCESS) {
2690                 return replmd_replicated_request_error(ar, ret);
2691         }
2692
2693         replmd_ldb_message_sort(msg, ar->schema);
2694
2695         /* we want to replace the old values */
2696         for (i=0; i < msg->num_elements; i++) {
2697                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
2698         }
2699
2700         if (DEBUGLVL(4)) {
2701                 char *s = ldb_ldif_message_string(ldb, ar, LDB_CHANGETYPE_MODIFY, msg);
2702                 DEBUG(4, ("DRS replication modify message:\n%s\n", s));
2703                 talloc_free(s);
2704         }
2705
2706         ret = ldb_build_mod_req(&change_req,
2707                                 ldb,
2708                                 ar,
2709                                 msg,
2710                                 ar->controls,
2711                                 ar,
2712                                 replmd_op_callback,
2713                                 ar->req);
2714         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
2715
2716         return ldb_next_request(ar->module, change_req);
2717 }
2718
2719 static int replmd_replicated_apply_search_callback(struct ldb_request *req,
2720                                                    struct ldb_reply *ares)
2721 {
2722         struct replmd_replicated_request *ar = talloc_get_type(req->context,
2723                                                struct replmd_replicated_request);
2724         int ret;
2725
2726         if (!ares) {
2727                 return ldb_module_done(ar->req, NULL, NULL,
2728                                         LDB_ERR_OPERATIONS_ERROR);
2729         }
2730         if (ares->error != LDB_SUCCESS &&
2731             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
2732                 return ldb_module_done(ar->req, ares->controls,
2733                                         ares->response, ares->error);
2734         }
2735
2736         switch (ares->type) {
2737         case LDB_REPLY_ENTRY:
2738                 ar->search_msg = talloc_steal(ar, ares->message);
2739                 break;
2740
2741         case LDB_REPLY_REFERRAL:
2742                 /* we ignore referrals */
2743                 break;
2744
2745         case LDB_REPLY_DONE:
2746                 if (ar->search_msg != NULL) {
2747                         ret = replmd_replicated_apply_merge(ar);
2748                 } else {
2749                         ret = replmd_replicated_apply_add(ar);
2750                 }
2751                 if (ret != LDB_SUCCESS) {
2752                         return ldb_module_done(ar->req, NULL, NULL, ret);
2753                 }
2754         }
2755
2756         talloc_free(ares);
2757         return LDB_SUCCESS;
2758 }
2759
2760 static int replmd_replicated_uptodate_vector(struct replmd_replicated_request *ar);
2761
2762 static int replmd_replicated_apply_next(struct replmd_replicated_request *ar)
2763 {
2764         struct ldb_context *ldb;
2765         int ret;
2766         char *tmp_str;
2767         char *filter;
2768         struct ldb_request *search_req;
2769         struct ldb_search_options_control *options;
2770
2771         if (ar->index_current >= ar->objs->num_objects) {
2772                 /* done with it, go to next stage */
2773                 return replmd_replicated_uptodate_vector(ar);
2774         }
2775
2776         ldb = ldb_module_get_ctx(ar->module);
2777         ar->search_msg = NULL;
2778
2779         tmp_str = ldb_binary_encode(ar, ar->objs->objects[ar->index_current].guid_value);
2780         if (!tmp_str) return replmd_replicated_request_werror(ar, WERR_NOMEM);
2781
2782         filter = talloc_asprintf(ar, "(objectGUID=%s)", tmp_str);
2783         if (!filter) return replmd_replicated_request_werror(ar, WERR_NOMEM);
2784         talloc_free(tmp_str);
2785
2786         ret = ldb_build_search_req(&search_req,
2787                                    ldb,
2788                                    ar,
2789                                    NULL,
2790                                    LDB_SCOPE_SUBTREE,
2791                                    filter,
2792                                    NULL,
2793                                    NULL,
2794                                    ar,
2795                                    replmd_replicated_apply_search_callback,
2796                                    ar->req);
2797
2798         ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
2799         if (ret != LDB_SUCCESS) {
2800                 return ret;
2801         }
2802
2803         /* we need to cope with cross-partition links, so search for
2804            the GUID over all partitions */
2805         options = talloc(search_req, struct ldb_search_options_control);
2806         if (options == NULL) {
2807                 DEBUG(0, (__location__ ": out of memory\n"));
2808                 return LDB_ERR_OPERATIONS_ERROR;
2809         }
2810         options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
2811
2812         ret = ldb_request_add_control(search_req,
2813                                       LDB_CONTROL_SEARCH_OPTIONS_OID,
2814                                       true, options);
2815         if (ret != LDB_SUCCESS) {
2816                 return ret;
2817         }
2818
2819         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
2820
2821         return ldb_next_request(ar->module, search_req);
2822 }
2823
2824 static int replmd_replicated_uptodate_modify_callback(struct ldb_request *req,
2825                                                       struct ldb_reply *ares)
2826 {
2827         struct ldb_context *ldb;
2828         struct replmd_replicated_request *ar = talloc_get_type(req->context,
2829                                                struct replmd_replicated_request);
2830         ldb = ldb_module_get_ctx(ar->module);
2831
2832         if (!ares) {
2833                 return ldb_module_done(ar->req, NULL, NULL,
2834                                         LDB_ERR_OPERATIONS_ERROR);
2835         }
2836         if (ares->error != LDB_SUCCESS) {
2837                 return ldb_module_done(ar->req, ares->controls,
2838                                         ares->response, ares->error);
2839         }
2840
2841         if (ares->type != LDB_REPLY_DONE) {
2842                 ldb_set_errstring(ldb, "Invalid reply type\n!");
2843                 return ldb_module_done(ar->req, NULL, NULL,
2844                                         LDB_ERR_OPERATIONS_ERROR);
2845         }
2846
2847         talloc_free(ares);
2848
2849         return ldb_module_done(ar->req, NULL, NULL, LDB_SUCCESS);
2850 }
2851
2852 static int replmd_replicated_uptodate_modify(struct replmd_replicated_request *ar)
2853 {
2854         struct ldb_context *ldb;
2855         struct ldb_request *change_req;
2856         enum ndr_err_code ndr_err;
2857         struct ldb_message *msg;
2858         struct replUpToDateVectorBlob ouv;
2859         const struct ldb_val *ouv_value;
2860         const struct drsuapi_DsReplicaCursor2CtrEx *ruv;
2861         struct replUpToDateVectorBlob nuv;
2862         struct ldb_val nuv_value;
2863         struct ldb_message_element *nuv_el = NULL;
2864         const struct GUID *our_invocation_id;
2865         struct ldb_message_element *orf_el = NULL;
2866         struct repsFromToBlob nrf;
2867         struct ldb_val *nrf_value = NULL;
2868         struct ldb_message_element *nrf_el = NULL;
2869         uint32_t i,j,ni=0;
2870         bool found = false;
2871         time_t t = time(NULL);
2872         NTTIME now;
2873         int ret;
2874
2875         ldb = ldb_module_get_ctx(ar->module);
2876         ruv = ar->objs->uptodateness_vector;
2877         ZERO_STRUCT(ouv);
2878         ouv.version = 2;
2879         ZERO_STRUCT(nuv);
2880         nuv.version = 2;
2881
2882         unix_to_nt_time(&now, t);
2883
2884         /*
2885          * first create the new replUpToDateVector
2886          */
2887         ouv_value = ldb_msg_find_ldb_val(ar->search_msg, "replUpToDateVector");
2888         if (ouv_value) {
2889                 ndr_err = ndr_pull_struct_blob(ouv_value, ar,
2890                                                lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &ouv,
2891                                                (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
2892                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2893                         NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
2894                         return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
2895                 }
2896
2897                 if (ouv.version != 2) {
2898                         return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
2899                 }
2900         }
2901
2902         /*
2903          * the new uptodateness vector will at least
2904          * contain 1 entry, one for the source_dsa
2905          *
2906          * plus optional values from our old vector and the one from the source_dsa
2907          */
2908         nuv.ctr.ctr2.count = 1 + ouv.ctr.ctr2.count;
2909         if (ruv) nuv.ctr.ctr2.count += ruv->count;
2910         nuv.ctr.ctr2.cursors = talloc_array(ar,
2911                                             struct drsuapi_DsReplicaCursor2,
2912                                             nuv.ctr.ctr2.count);
2913         if (!nuv.ctr.ctr2.cursors) return replmd_replicated_request_werror(ar, WERR_NOMEM);
2914
2915         /* first copy the old vector */
2916         for (i=0; i < ouv.ctr.ctr2.count; i++) {
2917                 nuv.ctr.ctr2.cursors[ni] = ouv.ctr.ctr2.cursors[i];
2918                 ni++;
2919         }
2920
2921         /* get our invocation_id if we have one already attached to the ldb */
2922         our_invocation_id = samdb_ntds_invocation_id(ldb);
2923
2924         /* merge in the source_dsa vector is available */
2925         for (i=0; (ruv && i < ruv->count); i++) {
2926                 found = false;
2927
2928                 if (our_invocation_id &&
2929                     GUID_equal(&ruv->cursors[i].source_dsa_invocation_id,
2930                                our_invocation_id)) {
2931                         continue;
2932                 }
2933
2934                 for (j=0; j < ni; j++) {
2935                         if (!GUID_equal(&ruv->cursors[i].source_dsa_invocation_id,
2936                                         &nuv.ctr.ctr2.cursors[j].source_dsa_invocation_id)) {
2937                                 continue;
2938                         }
2939
2940                         found = true;
2941
2942                         /*
2943                          * we update only the highest_usn and not the latest_sync_success time,
2944                          * because the last success stands for direct replication
2945                          */
2946                         if (ruv->cursors[i].highest_usn > nuv.ctr.ctr2.cursors[j].highest_usn) {
2947                                 nuv.ctr.ctr2.cursors[j].highest_usn = ruv->cursors[i].highest_usn;
2948                         }
2949                         break;                  
2950                 }
2951
2952                 if (found) continue;
2953
2954                 /* if it's not there yet, add it */
2955                 nuv.ctr.ctr2.cursors[ni] = ruv->cursors[i];
2956                 ni++;
2957         }
2958
2959         /*
2960          * merge in the current highwatermark for the source_dsa
2961          */
2962         found = false;
2963         for (j=0; j < ni; j++) {
2964                 if (!GUID_equal(&ar->objs->source_dsa->source_dsa_invocation_id,
2965                                 &nuv.ctr.ctr2.cursors[j].source_dsa_invocation_id)) {
2966                         continue;
2967                 }
2968
2969                 found = true;
2970
2971                 /*
2972                  * here we update the highest_usn and last_sync_success time
2973                  * because we're directly replicating from the source_dsa
2974                  *
2975                  * and use the tmp_highest_usn because this is what we have just applied
2976                  * to our ldb
2977                  */
2978                 nuv.ctr.ctr2.cursors[j].highest_usn             = ar->objs->source_dsa->highwatermark.tmp_highest_usn;
2979                 nuv.ctr.ctr2.cursors[j].last_sync_success       = now;
2980                 break;
2981         }
2982         if (!found) {
2983                 /*
2984                  * here we update the highest_usn and last_sync_success time
2985                  * because we're directly replicating from the source_dsa
2986                  *
2987                  * and use the tmp_highest_usn because this is what we have just applied
2988                  * to our ldb
2989                  */
2990                 nuv.ctr.ctr2.cursors[ni].source_dsa_invocation_id= ar->objs->source_dsa->source_dsa_invocation_id;
2991                 nuv.ctr.ctr2.cursors[ni].highest_usn            = ar->objs->source_dsa->highwatermark.tmp_highest_usn;
2992                 nuv.ctr.ctr2.cursors[ni].last_sync_success      = now;
2993                 ni++;
2994         }
2995
2996         /*
2997          * finally correct the size of the cursors array
2998          */
2999         nuv.ctr.ctr2.count = ni;
3000
3001         /*
3002          * sort the cursors
3003          */
3004         qsort(nuv.ctr.ctr2.cursors, nuv.ctr.ctr2.count,
3005               sizeof(struct drsuapi_DsReplicaCursor2),
3006               (comparison_fn_t)drsuapi_DsReplicaCursor2_compare);
3007
3008         /*
3009          * create the change ldb_message
3010          */
3011         msg = ldb_msg_new(ar);
3012         if (!msg) return replmd_replicated_request_werror(ar, WERR_NOMEM);
3013         msg->dn = ar->search_msg->dn;
3014
3015         ndr_err = ndr_push_struct_blob(&nuv_value, msg, 
3016                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
3017                                        &nuv,
3018                                        (ndr_push_flags_fn_t)ndr_push_replUpToDateVectorBlob);
3019         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3020                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
3021                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
3022         }
3023         ret = ldb_msg_add_value(msg, "replUpToDateVector", &nuv_value, &nuv_el);
3024         if (ret != LDB_SUCCESS) {
3025                 return replmd_replicated_request_error(ar, ret);
3026         }
3027         nuv_el->flags = LDB_FLAG_MOD_REPLACE;
3028
3029         /*
3030          * now create the new repsFrom value from the given repsFromTo1 structure
3031          */
3032         ZERO_STRUCT(nrf);
3033         nrf.version                                     = 1;
3034         nrf.ctr.ctr1                                    = *ar->objs->source_dsa;
3035         /* and fix some values... */
3036         nrf.ctr.ctr1.consecutive_sync_failures          = 0;
3037         nrf.ctr.ctr1.last_success                       = now;
3038         nrf.ctr.ctr1.last_attempt                       = now;
3039         nrf.ctr.ctr1.result_last_attempt                = WERR_OK;
3040         nrf.ctr.ctr1.highwatermark.highest_usn          = nrf.ctr.ctr1.highwatermark.tmp_highest_usn;
3041
3042         /*
3043          * first see if we already have a repsFrom value for the current source dsa
3044          * if so we'll later replace this value
3045          */
3046         orf_el = ldb_msg_find_element(ar->search_msg, "repsFrom");
3047         if (orf_el) {
3048                 for (i=0; i < orf_el->num_values; i++) {
3049                         struct repsFromToBlob *trf;
3050
3051                         trf = talloc(ar, struct repsFromToBlob);
3052                         if (!trf) return replmd_replicated_request_werror(ar, WERR_NOMEM);
3053
3054                         ndr_err = ndr_pull_struct_blob(&orf_el->values[i], trf, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), trf,
3055                                                        (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob);
3056                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3057                                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
3058                                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
3059                         }
3060
3061                         if (trf->version != 1) {
3062                                 return replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
3063                         }
3064
3065                         /*
3066                          * we compare the source dsa objectGUID not the invocation_id
3067                          * because we want only one repsFrom value per source dsa
3068                          * and when the invocation_id of the source dsa has changed we don't need 
3069                          * the old repsFrom with the old invocation_id
3070                          */
3071                         if (!GUID_equal(&trf->ctr.ctr1.source_dsa_obj_guid,
3072                                         &ar->objs->source_dsa->source_dsa_obj_guid)) {
3073                                 talloc_free(trf);
3074                                 continue;
3075                         }
3076
3077                         talloc_free(trf);
3078                         nrf_value = &orf_el->values[i];
3079                         break;
3080                 }
3081
3082                 /*
3083                  * copy over all old values to the new ldb_message
3084                  */
3085                 ret = ldb_msg_add_empty(msg, "repsFrom", 0, &nrf_el);
3086                 if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
3087                 *nrf_el = *orf_el;
3088         }
3089
3090         /*
3091          * if we haven't found an old repsFrom value for the current source dsa
3092          * we'll add a new value
3093          */
3094         if (!nrf_value) {
3095                 struct ldb_val zero_value;
3096                 ZERO_STRUCT(zero_value);
3097                 ret = ldb_msg_add_value(msg, "repsFrom", &zero_value, &nrf_el);
3098                 if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
3099
3100                 nrf_value = &nrf_el->values[nrf_el->num_values - 1];
3101         }
3102
3103         /* we now fill the value which is already attached to ldb_message */
3104         ndr_err = ndr_push_struct_blob(nrf_value, msg, 
3105                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
3106                                        &nrf,
3107                                        (ndr_push_flags_fn_t)ndr_push_repsFromToBlob);
3108         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3109                 NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
3110                 return replmd_replicated_request_werror(ar, ntstatus_to_werror(nt_status));
3111         }
3112
3113         /* 
3114          * the ldb_message_element for the attribute, has all the old values and the new one
3115          * so we'll replace the whole attribute with all values
3116          */
3117         nrf_el->flags = LDB_FLAG_MOD_REPLACE;
3118
3119         if (DEBUGLVL(4)) {
3120                 char *s = ldb_ldif_message_string(ldb, ar, LDB_CHANGETYPE_MODIFY, msg);
3121                 DEBUG(4, ("DRS replication uptodate modify message:\n%s\n", s));
3122                 talloc_free(s);
3123         }
3124
3125         /* prepare the ldb_modify() request */
3126         ret = ldb_build_mod_req(&change_req,
3127                                 ldb,
3128                                 ar,
3129                                 msg,
3130                                 ar->controls,
3131                                 ar,
3132                                 replmd_replicated_uptodate_modify_callback,
3133                                 ar->req);
3134         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
3135
3136         return ldb_next_request(ar->module, change_req);
3137 }
3138
3139 static int replmd_replicated_uptodate_search_callback(struct ldb_request *req,
3140                                                       struct ldb_reply *ares)
3141 {
3142         struct replmd_replicated_request *ar = talloc_get_type(req->context,
3143                                                struct replmd_replicated_request);
3144         int ret;
3145
3146         if (!ares) {
3147                 return ldb_module_done(ar->req, NULL, NULL,
3148                                         LDB_ERR_OPERATIONS_ERROR);
3149         }
3150         if (ares->error != LDB_SUCCESS &&
3151             ares->error != LDB_ERR_NO_SUCH_OBJECT) {
3152                 return ldb_module_done(ar->req, ares->controls,
3153                                         ares->response, ares->error);
3154         }
3155
3156         switch (ares->type) {
3157         case LDB_REPLY_ENTRY:
3158                 ar->search_msg = talloc_steal(ar, ares->message);
3159                 break;
3160
3161         case LDB_REPLY_REFERRAL:
3162                 /* we ignore referrals */
3163                 break;
3164
3165         case LDB_REPLY_DONE:
3166                 if (ar->search_msg == NULL) {
3167                         ret = replmd_replicated_request_werror(ar, WERR_DS_DRA_INTERNAL_ERROR);
3168                 } else {
3169                         ret = replmd_replicated_uptodate_modify(ar);
3170                 }
3171                 if (ret != LDB_SUCCESS) {
3172                         return ldb_module_done(ar->req, NULL, NULL, ret);
3173                 }
3174         }
3175
3176         talloc_free(ares);
3177         return LDB_SUCCESS;
3178 }
3179
3180
3181 static int replmd_replicated_uptodate_vector(struct replmd_replicated_request *ar)
3182 {
3183         struct ldb_context *ldb;
3184         int ret;
3185         static const char *attrs[] = {
3186                 "replUpToDateVector",
3187                 "repsFrom",
3188                 NULL
3189         };
3190         struct ldb_request *search_req;
3191
3192         ldb = ldb_module_get_ctx(ar->module);
3193         ar->search_msg = NULL;
3194
3195         ret = ldb_build_search_req(&search_req,
3196                                    ldb,
3197                                    ar,
3198                                    ar->objs->partition_dn,
3199                                    LDB_SCOPE_BASE,
3200                                    "(objectClass=*)",
3201                                    attrs,
3202                                    NULL,
3203                                    ar,
3204                                    replmd_replicated_uptodate_search_callback,
3205                                    ar->req);
3206         if (ret != LDB_SUCCESS) return replmd_replicated_request_error(ar, ret);
3207
3208         return ldb_next_request(ar->module, search_req);
3209 }
3210
3211
3212
3213 static int replmd_extended_replicated_objects(struct ldb_module *module, struct ldb_request *req)
3214 {
3215         struct ldb_context *ldb;
3216         struct dsdb_extended_replicated_objects *objs;
3217         struct replmd_replicated_request *ar;
3218         struct ldb_control **ctrls;
3219         int ret, i;
3220         struct replmd_private *replmd_private = 
3221                 talloc_get_type(ldb_module_get_private(module), struct replmd_private);
3222
3223         ldb = ldb_module_get_ctx(module);
3224
3225         ldb_debug(ldb, LDB_DEBUG_TRACE, "replmd_extended_replicated_objects\n");
3226
3227         objs = talloc_get_type(req->op.extended.data, struct dsdb_extended_replicated_objects);
3228         if (!objs) {
3229                 ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_extended_replicated_objects: invalid extended data\n");
3230                 return LDB_ERR_PROTOCOL_ERROR;
3231         }
3232
3233         if (objs->version != DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION) {
3234                 ldb_debug(ldb, LDB_DEBUG_FATAL, "replmd_extended_replicated_objects: extended data invalid version [%u != %u]\n",
3235                           objs->version, DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION);
3236                 return LDB_ERR_PROTOCOL_ERROR;
3237         }
3238
3239         ar = replmd_ctx_init(module, req);
3240         if (!ar)
3241                 return LDB_ERR_OPERATIONS_ERROR;
3242
3243         /* Set the flags to have the replmd_op_callback run over the full set of objects */
3244         ar->apply_mode = true;
3245         ar->objs = objs;
3246         ar->schema = dsdb_get_schema(ldb);
3247         if (!ar->schema) {
3248                 ldb_debug_set(ldb, LDB_DEBUG_FATAL, "replmd_ctx_init: no loaded schema found\n");
3249                 talloc_free(ar);
3250                 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
3251                 return LDB_ERR_CONSTRAINT_VIOLATION;
3252         }
3253
3254         ctrls = req->controls;
3255
3256         if (req->controls) {
3257                 req->controls = talloc_memdup(ar, req->controls,
3258                                               talloc_get_size(req->controls));
3259                 if (!req->controls) return replmd_replicated_request_werror(ar, WERR_NOMEM);
3260         }
3261
3262         ret = ldb_request_add_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID, false, NULL);
3263         if (ret != LDB_SUCCESS) {
3264                 return ret;
3265         }
3266
3267         ar->controls = req->controls;
3268         req->controls = ctrls;
3269
3270         DEBUG(4,("linked_attributes_count=%u\n", objs->linked_attributes_count));
3271
3272         /* save away the linked attributes for the end of the
3273            transaction */
3274         for (i=0; i<ar->objs->linked_attributes_count; i++) {
3275                 struct la_entry *la_entry;
3276
3277                 if (replmd_private->la_ctx == NULL) {
3278                         replmd_private->la_ctx = talloc_new(replmd_private);
3279                 }
3280                 la_entry = talloc(replmd_private->la_ctx, struct la_entry);
3281                 if (la_entry == NULL) {
3282                         ldb_oom(ldb);
3283                         return LDB_ERR_OPERATIONS_ERROR;
3284                 }
3285                 la_entry->la = talloc(la_entry, struct drsuapi_DsReplicaLinkedAttribute);
3286                 if (la_entry->la == NULL) {
3287                         talloc_free(la_entry);
3288                         ldb_oom(ldb);
3289                         return LDB_ERR_OPERATIONS_ERROR;
3290                 }
3291                 *la_entry->la = ar->objs->linked_attributes[i];
3292
3293                 /* we need to steal the non-scalars so they stay
3294                    around until the end of the transaction */
3295                 talloc_steal(la_entry->la, la_entry->la->identifier);
3296                 talloc_steal(la_entry->la, la_entry->la->value.blob);
3297
3298                 DLIST_ADD(replmd_private->la_list, la_entry);
3299         }
3300
3301         return replmd_replicated_apply_next(ar);
3302 }
3303
3304 /*
3305   process one linked attribute structure
3306  */
3307 static int replmd_process_linked_attribute(struct ldb_module *module,
3308                                            struct la_entry *la_entry)
3309 {                                          
3310         struct drsuapi_DsReplicaLinkedAttribute *la = la_entry->la;
3311         struct ldb_context *ldb = ldb_module_get_ctx(module);
3312         struct dsdb_schema *schema = dsdb_get_schema(ldb);
3313         struct ldb_message *msg;
3314         TALLOC_CTX *tmp_ctx = talloc_new(la_entry);
3315         int ret;
3316         const struct dsdb_attribute *attr;
3317         struct dsdb_dn *dsdb_dn;
3318         uint64_t seq_num = 0;
3319         struct drsuapi_DsReplicaAttribute drs;
3320         struct drsuapi_DsAttributeValue val;
3321         struct ldb_message_element new_el, *old_el;
3322         WERROR status;
3323         time_t t = time(NULL);
3324         struct ldb_result *res;
3325         const char *attrs[2];
3326         struct parsed_dn *pdn_list, *pdn;
3327         struct GUID guid;
3328         NTSTATUS ntstatus;
3329         bool active = (la->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)?true:false;
3330         const struct GUID *our_invocation_id;
3331
3332         drs.value_ctr.num_values = 1;
3333         drs.value_ctr.values = &val;
3334         val.blob = la->value.blob;
3335
3336 /*
3337 linked_attributes[0]:                                                     
3338      &objs->linked_attributes[i]: struct drsuapi_DsReplicaLinkedAttribute 
3339         identifier               : *                                      
3340             identifier: struct drsuapi_DsReplicaObjectIdentifier          
3341                 __ndr_size               : 0x0000003a (58)                
3342                 __ndr_size_sid           : 0x00000000 (0)                 
3343                 guid                     : 8e95b6a9-13dd-4158-89db-3220a5be5cc7
3344                 sid                      : S-0-0                               
3345                 __ndr_size_dn            : 0x00000000 (0)                      
3346                 dn                       : ''                                  
3347         attid                    : DRSUAPI_ATTRIBUTE_member (0x1F)             
3348         value: struct drsuapi_DsAttributeValue                                 
3349             __ndr_size               : 0x0000007e (126)                        
3350             blob                     : *                                       
3351                 blob                     : DATA_BLOB length=126                
3352         flags                    : 0x00000001 (1)                              
3353                1: DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE                      
3354         originating_add_time     : Wed Sep  2 22:20:01 2009 EST                
3355         meta_data: struct drsuapi_DsReplicaMetaData                            
3356             version                  : 0x00000015 (21)                         
3357             originating_change_time  : Wed Sep  2 23:39:07 2009 EST            
3358             originating_invocation_id: 794640f3-18cf-40ee-a211-a93992b67a64    
3359             originating_usn          : 0x000000000001e19c (123292)             
3360
3361 (for cases where the link is to a normal DN)
3362      &target: struct drsuapi_DsReplicaObjectIdentifier3                        
3363         __ndr_size               : 0x0000007e (126)                            
3364         __ndr_size_sid           : 0x0000001c (28)                             
3365         guid                     : 7639e594-db75-4086-b0d4-67890ae46031        
3366         sid                      : S-1-5-21-2848215498-2472035911-1947525656-19924
3367         __ndr_size_dn            : 0x00000022 (34)                                
3368         dn                       : 'CN=UOne,OU=TestOU,DC=vsofs8,DC=com'           
3369  */
3370         
3371         /* find the attribute being modified */
3372         attr = dsdb_attribute_by_attributeID_id(schema, la->attid);
3373         if (attr == NULL) {
3374                 DEBUG(0, (__location__ ": Unable to find attributeID 0x%x\n", la->attid));
3375                 talloc_free(tmp_ctx);
3376                 return LDB_ERR_OPERATIONS_ERROR;
3377         }
3378
3379         attrs[0] = attr->lDAPDisplayName;
3380         attrs[1] = NULL;
3381
3382         /* get the existing message from the db for the object with
3383            this GUID, returning attribute being modified. We will then
3384            use this msg as the basis for a modify call */
3385         ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
3386                                  DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
3387                                  DSDB_SEARCH_SHOW_DELETED |
3388                                  DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT |
3389                                  DSDB_SEARCH_REVEAL_INTERNALS,
3390                                  "objectGUID=%s", GUID_string(tmp_ctx, &la->identifier->guid));
3391         if (ret != LDB_SUCCESS) {
3392                 talloc_free(tmp_ctx);
3393                 return ret;
3394         }
3395         if (res->count != 1) {
3396                 ldb_asprintf_errstring(ldb, "DRS linked attribute for GUID %s - DN not found",
3397                                        GUID_string(tmp_ctx, &la->identifier->guid));
3398                 talloc_free(tmp_ctx);
3399                 return LDB_ERR_NO_SUCH_OBJECT;
3400         }
3401         msg = res->msgs[0];
3402
3403         if (msg->num_elements == 0) {
3404                 ret = ldb_msg_add_empty(msg, attr->lDAPDisplayName, LDB_FLAG_MOD_REPLACE, &old_el);
3405                 if (ret != LDB_SUCCESS) {
3406                         ldb_module_oom(module);
3407                         talloc_free(tmp_ctx);
3408                         return LDB_ERR_OPERATIONS_ERROR;
3409                 }
3410         } else {
3411                 old_el = &msg->elements[0];
3412                 old_el->flags = LDB_FLAG_MOD_REPLACE;
3413         }
3414
3415         /* parse the existing links */
3416         ret = get_parsed_dns(module, tmp_ctx, old_el, &pdn_list, attr->syntax->ldap_oid);
3417         if (ret != LDB_SUCCESS) {
3418                 talloc_free(tmp_ctx);
3419                 return ret;
3420         }
3421
3422         /* get our invocationId */
3423         our_invocation_id = samdb_ntds_invocation_id(ldb);
3424         if (!our_invocation_id) {
3425                 ldb_debug_set(ldb, LDB_DEBUG_ERROR, __location__ ": unable to find invocationId\n");
3426                 talloc_free(tmp_ctx);
3427                 return LDB_ERR_OPERATIONS_ERROR;
3428         }
3429
3430         ret = replmd_check_upgrade_links(pdn_list, old_el->num_values, our_invocation_id);
3431         if (ret != LDB_SUCCESS) {
3432                 talloc_free(tmp_ctx);
3433                 return ret;
3434         }
3435
3436         status = attr->syntax->drsuapi_to_ldb(ldb, schema, attr, &drs, tmp_ctx, &new_el);
3437         if (!W_ERROR_IS_OK(status)) {
3438                 ldb_asprintf_errstring(ldb, "Failed to parsed linked attribute blob for %s on %s\n",
3439                                        old_el->name, ldb_dn_get_linearized(msg->dn));
3440                 return LDB_ERR_OPERATIONS_ERROR;
3441         }
3442
3443         if (new_el.num_values != 1) {
3444                 ldb_asprintf_errstring(ldb, "Failed to find value in linked attribute blob for %s on %s\n",
3445                                        old_el->name, ldb_dn_get_linearized(msg->dn));
3446                 return LDB_ERR_OPERATIONS_ERROR;
3447         }
3448
3449         dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, &new_el.values[0], attr->syntax->ldap_oid);
3450         if (!dsdb_dn) {
3451                 ldb_asprintf_errstring(ldb, "Failed to parse DN in linked attribute blob for %s on %s\n",
3452                                        old_el->name, ldb_dn_get_linearized(msg->dn));
3453                 return LDB_ERR_OPERATIONS_ERROR;
3454         }
3455
3456         ntstatus = dsdb_get_extended_dn_guid(dsdb_dn->dn, &guid, "GUID");
3457         if (!NT_STATUS_IS_OK(ntstatus)) {
3458                 ldb_asprintf_errstring(ldb, "Failed to find GUID in linked attribute blob for %s on %s\n",
3459                                        old_el->name, ldb_dn_get_linearized(msg->dn));
3460                 return LDB_ERR_OPERATIONS_ERROR;
3461         }
3462
3463         /* see if this link already exists */
3464         pdn = parsed_dn_find(pdn_list, old_el->num_values, &guid);
3465         if (pdn != NULL) {
3466                 /* see if this update is newer than what we have already */
3467                 struct GUID invocation_id = GUID_zero();
3468                 uint32_t version = 0;
3469                 NTTIME change_time = 0;
3470                 bool was_active = ldb_dn_get_extended_component(pdn->dsdb_dn->dn, "DELETED") == NULL;
3471
3472                 dsdb_get_extended_dn_guid(pdn->dsdb_dn->dn, &invocation_id, "RMD_INVOCID");
3473                 dsdb_get_extended_dn_uint32(pdn->dsdb_dn->dn, &version, "RMD_VERSION");
3474                 dsdb_get_extended_dn_nttime(pdn->dsdb_dn->dn, &change_time, "RMD_CHANGETIME");
3475
3476                 if (!replmd_update_is_newer(&invocation_id,
3477                                             &la->meta_data.originating_invocation_id,
3478                                             version,
3479                                             la->meta_data.version,
3480                                             change_time,
3481                                             la->meta_data.originating_change_time)) {
3482                         DEBUG(1,("Discarding older DRS linked attribute update to %s on %s from %s\n",
3483                                  old_el->name, ldb_dn_get_linearized(msg->dn),
3484                                  GUID_string(tmp_ctx, &la->meta_data.originating_invocation_id)));
3485                         talloc_free(tmp_ctx);
3486                         return LDB_SUCCESS;
3487                 }
3488
3489                 /* get a seq_num for this change */
3490                 ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
3491                 if (ret != LDB_SUCCESS) {
3492                         talloc_free(tmp_ctx);
3493                         return ret;
3494                 }
3495
3496                 if (was_active) {
3497                         /* remove the existing backlink */
3498                         ret = replmd_add_backlink(module, schema, &la->identifier->guid, &guid, false, attr, false);
3499                         if (ret != LDB_SUCCESS) {
3500                                 talloc_free(tmp_ctx);
3501                                 return ret;
3502                         }
3503                 }
3504
3505                 ret = replmd_update_la_val(tmp_ctx, pdn->v, dsdb_dn, pdn->dsdb_dn,
3506                                            &la->meta_data.originating_invocation_id,
3507                                            la->meta_data.originating_usn, seq_num,
3508                                            la->meta_data.originating_change_time,
3509                                            la->meta_data.version,
3510                                            !active);
3511                 if (ret != LDB_SUCCESS) {
3512                         talloc_free(tmp_ctx);
3513                         return ret;
3514                 }
3515
3516                 if (active) {
3517                         /* add the new backlink */
3518                         ret = replmd_add_backlink(module, schema, &la->identifier->guid, &guid, true, attr, false);
3519                         if (ret != LDB_SUCCESS) {
3520                                 talloc_free(tmp_ctx);
3521                                 return ret;
3522                         }
3523                 }
3524         } else {
3525                 /* get a seq_num for this change */
3526                 ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
3527                 if (ret != LDB_SUCCESS) {
3528                         talloc_free(tmp_ctx);
3529                         return ret;
3530                 }
3531
3532                 old_el->values = talloc_realloc(msg->elements, old_el->values,
3533                                                 struct ldb_val, old_el->num_values+1);
3534                 if (!old_el->values) {
3535                         ldb_module_oom(module);
3536                         return LDB_ERR_OPERATIONS_ERROR;
3537                 }
3538                 old_el->num_values++;
3539
3540                 ret = replmd_build_la_val(tmp_ctx, &old_el->values[old_el->num_values-1], dsdb_dn,
3541                                           &la->meta_data.originating_invocation_id,
3542                                           la->meta_data.originating_usn, seq_num,
3543                                           la->meta_data.originating_change_time,
3544                                           la->meta_data.version,
3545                                           (la->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE)?false:true);
3546                 if (ret != LDB_SUCCESS) {
3547                         talloc_free(tmp_ctx);
3548                         return ret;
3549                 }
3550
3551                 if (active) {
3552                         ret = replmd_add_backlink(module, schema, &la->identifier->guid, &guid,
3553                                                   true, attr, false);
3554                         if (ret != LDB_SUCCESS) {
3555                                 talloc_free(tmp_ctx);
3556                                 return ret;
3557                         }
3558                 }
3559         }
3560
3561         /* we only change whenChanged and uSNChanged if the seq_num
3562            has changed */
3563         if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
3564                 talloc_free(tmp_ctx);
3565                 return LDB_ERR_OPERATIONS_ERROR;
3566         }
3567
3568         if (add_uint64_element(msg, "uSNChanged", seq_num) != LDB_SUCCESS) {
3569                 talloc_free(tmp_ctx);
3570                 return LDB_ERR_OPERATIONS_ERROR;
3571         }
3572
3573         ret = dsdb_check_single_valued_link(attr, old_el);
3574         if (ret != LDB_SUCCESS) {
3575                 talloc_free(tmp_ctx);
3576                 return ret;
3577         }
3578
3579         ret = dsdb_module_modify(module, msg, DSDB_MODIFY_RELAX);
3580         if (ret != LDB_SUCCESS) {
3581                 ldb_debug(ldb, LDB_DEBUG_WARNING, "Failed to apply linked attribute change '%s'\n%s\n",
3582                           ldb_errstring(ldb),
3583                           ldb_ldif_message_string(ldb, tmp_ctx, LDB_CHANGETYPE_MODIFY, msg));
3584                 talloc_free(tmp_ctx);
3585                 return ret;
3586         }
3587         
3588         talloc_free(tmp_ctx);
3589
3590         return ret;     
3591 }
3592
3593 static int replmd_extended(struct ldb_module *module, struct ldb_request *req)
3594 {
3595         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_REPLICATED_OBJECTS_OID) == 0) {
3596                 return replmd_extended_replicated_objects(module, req);
3597         }
3598
3599         return ldb_next_request(module, req);
3600 }
3601
3602
3603 /*
3604   we hook into the transaction operations to allow us to 
3605   perform the linked attribute updates at the end of the whole
3606   transaction. This allows a forward linked attribute to be created
3607   before the object is created. During a vampire, w2k8 sends us linked
3608   attributes before the objects they are part of.
3609  */
3610 static int replmd_start_transaction(struct ldb_module *module)
3611 {
3612         /* create our private structure for this transaction */
3613         struct replmd_private *replmd_private = talloc_get_type(ldb_module_get_private(module),
3614                                                                 struct replmd_private);
3615         replmd_txn_cleanup(replmd_private);
3616
3617         /* free any leftover mod_usn records from cancelled
3618            transactions */
3619         while (replmd_private->ncs) {
3620                 struct nc_entry *e = replmd_private->ncs;
3621                 DLIST_REMOVE(replmd_private->ncs, e);
3622                 talloc_free(e);
3623         }
3624
3625         return ldb_next_start_trans(module);
3626 }
3627
3628 /*
3629   on prepare commit we loop over our queued la_context structures and
3630   apply each of them  
3631  */
3632 static int replmd_prepare_commit(struct ldb_module *module)
3633 {
3634         struct replmd_private *replmd_private = 
3635                 talloc_get_type(ldb_module_get_private(module), struct replmd_private);
3636         struct la_entry *la, *prev;
3637         struct la_backlink *bl;
3638         int ret;
3639
3640         /* walk the list backwards, to do the first entry first, as we
3641          * added the entries with DLIST_ADD() which puts them at the
3642          * start of the list */
3643         for (la = replmd_private->la_list; la && la->next; la=la->next) ;
3644
3645         for (; la; la=prev) {
3646                 prev = la->prev;
3647                 DLIST_REMOVE(replmd_private->la_list, la);
3648                 ret = replmd_process_linked_attribute(module, la);
3649                 if (ret != LDB_SUCCESS) {
3650                         replmd_txn_cleanup(replmd_private);
3651                         return ret;
3652                 }
3653         }
3654
3655         /* process our backlink list, creating and deleting backlinks
3656            as necessary */
3657         for (bl=replmd_private->la_backlinks; bl; bl=bl->next) {
3658                 ret = replmd_process_backlink(module, bl);
3659                 if (ret != LDB_SUCCESS) {
3660                         replmd_txn_cleanup(replmd_private);
3661                         return ret;
3662                 }
3663         }
3664
3665         replmd_txn_cleanup(replmd_private);
3666
3667         /* possibly change @REPLCHANGED */
3668         ret = replmd_notify_store(module);
3669         if (ret != LDB_SUCCESS) {
3670                 return ret;
3671         }
3672         
3673         return ldb_next_prepare_commit(module);
3674 }
3675
3676 static int replmd_del_transaction(struct ldb_module *module)
3677 {
3678         struct replmd_private *replmd_private = 
3679                 talloc_get_type(ldb_module_get_private(module), struct replmd_private);
3680         replmd_txn_cleanup(replmd_private);
3681
3682         return ldb_next_del_trans(module);
3683 }
3684
3685
3686 _PUBLIC_ const struct ldb_module_ops ldb_repl_meta_data_module_ops = {
3687         .name          = "repl_meta_data",
3688         .init_context      = replmd_init,
3689         .add               = replmd_add,
3690         .modify            = replmd_modify,
3691         .rename            = replmd_rename,
3692         .del               = replmd_delete,
3693         .extended          = replmd_extended,
3694         .start_transaction = replmd_start_transaction,
3695         .prepare_commit    = replmd_prepare_commit,
3696         .del_transaction   = replmd_del_transaction,
3697 };