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