08a959cabb5b751cf53f1774b8a22ae47418af37
[sfrench/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / partition.c
1 /* 
2    Partitions ldb module
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb partitions module
25  *
26  *  Description: Implement LDAP partitions
27  *
28  *  Author: Andrew Bartlett
29  *  Author: Stefan Metzmacher
30  */
31
32 #include "dsdb/samdb/ldb_modules/partition.h"
33
34 struct part_request {
35         struct ldb_module *module;
36         struct ldb_request *req;
37 };
38
39 struct partition_context {
40         struct ldb_module *module;
41         struct ldb_request *req;
42
43         struct part_request *part_req;
44         unsigned int num_requests;
45         unsigned int finished_requests;
46
47         const char **referrals;
48 };
49
50 static struct partition_context *partition_init_ctx(struct ldb_module *module, struct ldb_request *req)
51 {
52         struct partition_context *ac;
53
54         ac = talloc_zero(req, struct partition_context);
55         if (ac == NULL) {
56                 ldb_set_errstring(ldb_module_get_ctx(module), "Out of Memory");
57                 return NULL;
58         }
59
60         ac->module = module;
61         ac->req = req;
62
63         return ac;
64 }
65
66 /*
67  * helper functions to call the next module in chain
68  */
69 int partition_request(struct ldb_module *module, struct ldb_request *request)
70 {
71         if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) { \
72                 const struct dsdb_control_current_partition *partition = NULL;
73                 struct ldb_control *partition_ctrl = ldb_request_get_control(request, DSDB_CONTROL_CURRENT_PARTITION_OID);
74                 if (partition_ctrl) {
75                         partition = talloc_get_type(partition_ctrl->data,
76                                                     struct dsdb_control_current_partition);
77                 }
78
79                 if (partition != NULL) {
80                         ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_request() -> %s",
81                                   ldb_dn_get_linearized(partition->dn));                        
82                 } else {
83                         ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_request() -> (metadata partition)");
84                 }
85         }
86
87         return ldb_next_request(module, request);
88 }
89
90 static struct dsdb_partition *find_partition(struct partition_private_data *data,
91                                              struct ldb_dn *dn,
92                                              struct ldb_request *req)
93 {
94         unsigned int i;
95         struct ldb_control *partition_ctrl;
96
97         /* see if the request has the partition DN specified in a
98          * control. The repl_meta_data module can specify this to
99          * ensure that replication happens to the right partition
100          */
101         partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
102         if (partition_ctrl) {
103                 const struct dsdb_control_current_partition *partition;
104                 partition = talloc_get_type(partition_ctrl->data,
105                                             struct dsdb_control_current_partition);
106                 if (partition != NULL) {
107                         dn = partition->dn;
108                 }
109         }
110
111         if (dn == NULL) {
112                 return NULL;
113         }
114
115         /* Look at base DN */
116         /* Figure out which partition it is under */
117         /* Skip the lot if 'data' isn't here yet (initialisation) */
118         for (i=0; data && data->partitions && data->partitions[i]; i++) {
119                 if (ldb_dn_compare_base(data->partitions[i]->ctrl->dn, dn) == 0) {
120                         return data->partitions[i];
121                 }
122         }
123
124         return NULL;
125 }
126
127 /**
128  * fire the caller's callback for every entry, but only send 'done' once.
129  */
130 static int partition_req_callback(struct ldb_request *req,
131                                   struct ldb_reply *ares)
132 {
133         struct partition_context *ac;
134         struct ldb_module *module;
135         struct ldb_request *nreq;
136         int ret;
137         struct ldb_control *partition_ctrl;
138
139         ac = talloc_get_type(req->context, struct partition_context);
140
141         if (!ares) {
142                 return ldb_module_done(ac->req, NULL, NULL,
143                                         LDB_ERR_OPERATIONS_ERROR);
144         }
145
146         partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
147         if (partition_ctrl && (ac->num_requests == 1 || ares->type == LDB_REPLY_ENTRY)) {
148                 /* If we didn't fan this request out to mulitple partitions,
149                  * or this is an individual search result, we can
150                  * deterministically tell the caller what partition this was
151                  * written to (repl_meta_data likes to know) */
152                 ret = ldb_reply_add_control(ares,
153                                             DSDB_CONTROL_CURRENT_PARTITION_OID,
154                                             false, partition_ctrl->data);
155                 if (ret != LDB_SUCCESS) {
156                         return ldb_module_done(ac->req, NULL, NULL,
157                                                ret);
158                 }
159         }
160
161         if (ares->error != LDB_SUCCESS) {
162                 return ldb_module_done(ac->req, ares->controls,
163                                         ares->response, ares->error);
164         }
165
166         switch (ares->type) {
167         case LDB_REPLY_REFERRAL:
168                 return ldb_module_send_referral(ac->req, ares->referral);
169
170         case LDB_REPLY_ENTRY:
171                 if (ac->req->operation != LDB_SEARCH) {
172                         ldb_set_errstring(ldb_module_get_ctx(ac->module),
173                                 "partition_req_callback:"
174                                 " Unsupported reply type for this request");
175                         return ldb_module_done(ac->req, NULL, NULL,
176                                                 LDB_ERR_OPERATIONS_ERROR);
177                 }
178                 
179                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
180
181         case LDB_REPLY_DONE:
182                 if (ac->req->operation == LDB_EXTENDED) {
183                         /* FIXME: check for ares->response, replmd does not fill it ! */
184                         if (ares->response) {
185                                 if (strcmp(ares->response->oid, LDB_EXTENDED_START_TLS_OID) != 0) {
186                                         ldb_set_errstring(ldb_module_get_ctx(ac->module),
187                                                           "partition_req_callback:"
188                                                           " Unknown extended reply, "
189                                                           "only supports START_TLS");
190                                         talloc_free(ares);
191                                         return ldb_module_done(ac->req, NULL, NULL,
192                                                                 LDB_ERR_OPERATIONS_ERROR);
193                                 }
194                         }
195                 }
196
197                 ac->finished_requests++;
198                 if (ac->finished_requests == ac->num_requests) {
199                         /* Send back referrals if they do exist (search ops) */
200                         if (ac->referrals != NULL) {
201                                 const char **ref;
202                                 for (ref = ac->referrals; *ref != NULL; ++ref) {
203                                         ret = ldb_module_send_referral(ac->req,
204                                                                        talloc_strdup(ac->req, *ref));
205                                         if (ret != LDB_SUCCESS) {
206                                                 return ldb_module_done(ac->req, NULL, NULL,
207                                                                        ret);
208                                         }
209                                 }
210                         }
211
212                         /* this was the last one, call callback */
213                         return ldb_module_done(ac->req, ares->controls,
214                                                ares->response, 
215                                                ares->error);
216                 }
217
218                 /* not the last, now call the next one */
219                 module = ac->part_req[ac->finished_requests].module;
220                 nreq = ac->part_req[ac->finished_requests].req;
221
222                 ret = partition_request(module, nreq);
223                 if (ret != LDB_SUCCESS) {
224                         talloc_free(ares);
225                         return ldb_module_done(ac->req, NULL, NULL, ret);
226                 }
227
228                 break;
229         }
230
231         talloc_free(ares);
232         return LDB_SUCCESS;
233 }
234
235 static int partition_prep_request(struct partition_context *ac,
236                                   struct dsdb_partition *partition)
237 {
238         int ret;
239         struct ldb_request *req;
240         struct ldb_control *partition_ctrl = NULL;
241
242         ac->part_req = talloc_realloc(ac, ac->part_req,
243                                         struct part_request,
244                                         ac->num_requests + 1);
245         if (ac->part_req == NULL) {
246                 return ldb_oom(ldb_module_get_ctx(ac->module));
247         }
248
249         switch (ac->req->operation) {
250         case LDB_SEARCH:
251                 ret = ldb_build_search_req_ex(&req, ldb_module_get_ctx(ac->module),
252                                         ac->part_req,
253                                         ac->req->op.search.base,
254                                         ac->req->op.search.scope,
255                                         ac->req->op.search.tree,
256                                         ac->req->op.search.attrs,
257                                         ac->req->controls,
258                                         ac, partition_req_callback,
259                                         ac->req);
260                 LDB_REQ_SET_LOCATION(req);
261                 break;
262         case LDB_ADD:
263                 ret = ldb_build_add_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
264                                         ac->req->op.add.message,
265                                         ac->req->controls,
266                                         ac, partition_req_callback,
267                                         ac->req);
268                 LDB_REQ_SET_LOCATION(req);
269                 break;
270         case LDB_MODIFY:
271                 ret = ldb_build_mod_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
272                                         ac->req->op.mod.message,
273                                         ac->req->controls,
274                                         ac, partition_req_callback,
275                                         ac->req);
276                 LDB_REQ_SET_LOCATION(req);
277                 break;
278         case LDB_DELETE:
279                 ret = ldb_build_del_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
280                                         ac->req->op.del.dn,
281                                         ac->req->controls,
282                                         ac, partition_req_callback,
283                                         ac->req);
284                 LDB_REQ_SET_LOCATION(req);
285                 break;
286         case LDB_RENAME:
287                 ret = ldb_build_rename_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
288                                         ac->req->op.rename.olddn,
289                                         ac->req->op.rename.newdn,
290                                         ac->req->controls,
291                                         ac, partition_req_callback,
292                                         ac->req);
293                 LDB_REQ_SET_LOCATION(req);
294                 break;
295         case LDB_EXTENDED:
296                 ret = ldb_build_extended_req(&req, ldb_module_get_ctx(ac->module),
297                                         ac->part_req,
298                                         ac->req->op.extended.oid,
299                                         ac->req->op.extended.data,
300                                         ac->req->controls,
301                                         ac, partition_req_callback,
302                                         ac->req);
303                 LDB_REQ_SET_LOCATION(req);
304                 break;
305         default:
306                 ldb_set_errstring(ldb_module_get_ctx(ac->module),
307                                   "Unsupported request type!");
308                 ret = LDB_ERR_UNWILLING_TO_PERFORM;
309         }
310
311         if (ret != LDB_SUCCESS) {
312                 return ret;
313         }
314
315         ac->part_req[ac->num_requests].req = req;
316
317         if (ac->req->controls) {
318                 /* Duplicate everything beside the current partition control */
319                 partition_ctrl = ldb_request_get_control(ac->req,
320                                                          DSDB_CONTROL_CURRENT_PARTITION_OID);
321                 if (!ldb_save_controls(partition_ctrl, req, NULL)) {
322                         return ldb_module_oom(ac->module);
323                 }
324         }
325
326         if (partition) {
327                 void *part_data = partition->ctrl;
328
329                 ac->part_req[ac->num_requests].module = partition->module;
330
331                 if (partition_ctrl != NULL) {
332                         if (partition_ctrl->data != NULL) {
333                                 part_data = partition_ctrl->data;
334                         }
335
336                         /*
337                          * If the provided current partition control is without
338                          * data then use the calculated one.
339                          */
340                         ret = ldb_request_add_control(req,
341                                                       DSDB_CONTROL_CURRENT_PARTITION_OID,
342                                                       false, part_data);
343                         if (ret != LDB_SUCCESS) {
344                                 return ret;
345                         }
346                 }
347
348                 if (req->operation == LDB_SEARCH) {
349                         /* If the search is for 'more' than this partition,
350                          * then change the basedn, so a remote LDAP server
351                          * doesn't object */
352                         if (ldb_dn_compare_base(partition->ctrl->dn,
353                                                 req->op.search.base) != 0) {
354                                 req->op.search.base = partition->ctrl->dn;
355                         }
356                 }
357
358         } else {
359                 /* make sure you put the module here, or
360                  * or ldb_next_request() will skip a module */
361                 ac->part_req[ac->num_requests].module = ac->module;
362         }
363
364         ac->num_requests++;
365
366         return LDB_SUCCESS;
367 }
368
369 static int partition_call_first(struct partition_context *ac)
370 {
371         return partition_request(ac->part_req[0].module, ac->part_req[0].req);
372 }
373
374 /**
375  * Send a request down to all the partitions (but not the sam.ldb file)
376  */
377 static int partition_send_all(struct ldb_module *module, 
378                               struct partition_context *ac, 
379                               struct ldb_request *req) 
380 {
381         unsigned int i;
382         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
383                                                               struct partition_private_data);
384         int ret;
385
386         for (i=0; data && data->partitions && data->partitions[i]; i++) {
387                 ret = partition_prep_request(ac, data->partitions[i]);
388                 if (ret != LDB_SUCCESS) {
389                         return ret;
390                 }
391         }
392
393         /* fire the first one */
394         return partition_call_first(ac);
395 }
396
397
398 /**
399  * send an operation to the top partition, then copy the resulting
400  * object to all other partitions
401  */
402 static int partition_copy_all(struct ldb_module *module,
403                               struct partition_context *ac,
404                               struct ldb_request *req,
405                               struct ldb_dn *dn)
406 {
407         unsigned int i;
408         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
409                                                               struct partition_private_data);
410         int ret, search_ret;
411         struct ldb_result *res;
412
413         /* do the request on the top level sam.ldb synchronously */
414         ret = ldb_next_request(module, req);
415         if (ret != LDB_SUCCESS) {
416                 return ret;
417         }
418         ret = ldb_wait(req->handle, LDB_WAIT_ALL);
419         if (ret != LDB_SUCCESS) {
420                 return ret;
421         }
422
423         /* now fetch the resulting object, and then copy it to all the
424          * other partitions. We need this approach to cope with the
425          * partitions getting out of sync. If for example the
426          * @ATTRIBUTES object exists on one partition but not the
427          * others then just doing each of the partitions in turn will
428          * lead to an error
429          */
430         search_ret = dsdb_module_search_dn(module, ac, &res, dn, NULL, DSDB_FLAG_NEXT_MODULE, req);
431         if (search_ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
432                 return search_ret;
433         }
434
435         /* now delete the object in the other partitions. Once that is
436            done we will re-add the object, if search_ret was not
437            LDB_ERR_NO_SUCH_OBJECT
438         */
439         for (i=0; data->partitions && data->partitions[i]; i++) {
440                 int pret;
441                 pret = dsdb_module_del(data->partitions[i]->module, dn, DSDB_FLAG_NEXT_MODULE, req);
442                 if (pret != LDB_SUCCESS && pret != LDB_ERR_NO_SUCH_OBJECT) {
443                         /* we should only get success or no
444                            such object from the other partitions */
445                         return pret;
446                 }
447         }
448
449
450         if (search_ret != LDB_ERR_NO_SUCH_OBJECT) {
451                 /* now re-add in the other partitions */
452                 for (i=0; data->partitions && data->partitions[i]; i++) {
453                         int pret;
454                         pret = dsdb_module_add(data->partitions[i]->module, res->msgs[0], DSDB_FLAG_NEXT_MODULE, req);
455                         if (pret != LDB_SUCCESS) {
456                                 return pret;
457                         }
458                 }
459         }
460
461         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
462 }
463
464 /**
465  * Figure out which backend a request needs to be aimed at.  Some
466  * requests must be replicated to all backends
467  */
468 static int partition_replicate(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn) 
469 {
470         struct partition_context *ac;
471         unsigned int i;
472         int ret;
473         struct dsdb_partition *partition;
474         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
475                                                               struct partition_private_data);
476
477         /* if we aren't initialised yet go further */
478         if (!data || !data->partitions) {
479                 return ldb_next_request(module, req);
480         }
481
482         if (ldb_dn_is_special(dn)) {
483                 /* Is this a special DN, we need to replicate to every backend? */
484                 for (i=0; data->replicate && data->replicate[i]; i++) {
485                         if (ldb_dn_compare(data->replicate[i], 
486                                            dn) == 0) {
487                                 
488                                 ac = partition_init_ctx(module, req);
489                                 if (!ac) {
490                                         return ldb_operr(ldb_module_get_ctx(module));
491                                 }
492                                 
493                                 return partition_copy_all(module, ac, req, dn);
494                         }
495                 }
496         }
497
498         /* Otherwise, we need to find the partition to fire it to */
499
500         /* Find partition */
501         partition = find_partition(data, dn, req);
502         if (!partition) {
503                 /*
504                  * if we haven't found a matching partition
505                  * pass the request to the main ldb
506                  *
507                  * TODO: we should maybe return an error here
508                  *       if it's not a special dn
509                  */
510
511                 return ldb_next_request(module, req);
512         }
513
514         ac = partition_init_ctx(module, req);
515         if (!ac) {
516                 return ldb_operr(ldb_module_get_ctx(module));
517         }
518
519         /* we need to add a control but we never touch the original request */
520         ret = partition_prep_request(ac, partition);
521         if (ret != LDB_SUCCESS) {
522                 return ret;
523         }
524
525         /* fire the first one */
526         return partition_call_first(ac);
527 }
528
529 /* search */
530 static int partition_search(struct ldb_module *module, struct ldb_request *req)
531 {
532         struct ldb_control **saved_controls;
533         /* Find backend */
534         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
535                                                               struct partition_private_data);
536         struct partition_context *ac;
537         struct ldb_context *ldb;
538         struct loadparm_context *lp_ctx;
539
540         struct ldb_control *search_control = ldb_request_get_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID);
541         struct ldb_control *domain_scope_control = ldb_request_get_control(req, LDB_CONTROL_DOMAIN_SCOPE_OID);
542         struct ldb_control *no_gc_control = ldb_request_get_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG);
543         
544         struct ldb_search_options_control *search_options = NULL;
545         struct dsdb_partition *p;
546         unsigned int i, j;
547         int ret;
548         bool domain_scope = false, phantom_root = false;
549
550         p = find_partition(data, NULL, req);
551         if (p != NULL) {
552                 /* the caller specified what partition they want the
553                  * search - just pass it on
554                  */
555                 return ldb_next_request(p->module, req);
556         }
557
558         /* Get back the search options from the search control, and mark it as
559          * non-critical (to make backends and also dcpromo happy).
560          */
561         if (search_control) {
562                 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
563                 search_control->critical = 0;
564
565         }
566
567         /* Remove the "domain_scope" control, so we don't confuse a backend
568          * server */
569         if (domain_scope_control && !ldb_save_controls(domain_scope_control, req, &saved_controls)) {
570                 return ldb_oom(ldb_module_get_ctx(module));
571         }
572
573         /* if we aren't initialised yet go further */
574         if (!data || !data->partitions) {
575                 return ldb_next_request(module, req);
576         }
577
578         /* Special DNs without specified partition should go further */
579         if (ldb_dn_is_special(req->op.search.base)) {
580                 return ldb_next_request(module, req);
581         }
582
583         /* Locate the options */
584         domain_scope = (search_options
585                 && (search_options->search_options & LDB_SEARCH_OPTION_DOMAIN_SCOPE))
586                 || domain_scope_control;
587         phantom_root = search_options
588                 && (search_options->search_options & LDB_SEARCH_OPTION_PHANTOM_ROOT);
589
590         /* Remove handled options from the search control flag */
591         if (search_options) {
592                 search_options->search_options = search_options->search_options
593                         & ~LDB_SEARCH_OPTION_DOMAIN_SCOPE
594                         & ~LDB_SEARCH_OPTION_PHANTOM_ROOT;
595         }
596
597         ac = partition_init_ctx(module, req);
598         if (!ac) {
599                 return ldb_operr(ldb_module_get_ctx(module));
600         }
601
602         ldb = ldb_module_get_ctx(ac->module);
603         lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
604                                                 struct loadparm_context);
605
606         /* Search from the base DN */
607         if (ldb_dn_is_null(req->op.search.base)) {
608                 if (!phantom_root) {
609                         return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, "empty base DN");
610                 }
611                 return partition_send_all(module, ac, req);
612         }
613
614         for (i=0; data->partitions[i]; i++) {
615                 bool match = false, stop = false;
616
617                 if (data->partitions[i]->partial_replica && no_gc_control != NULL) {
618                         if (ldb_dn_compare_base(data->partitions[i]->ctrl->dn,
619                                                 req->op.search.base) == 0) {
620                                 /* base DN is in a partial replica
621                                    with the NO_GLOBAL_CATALOG
622                                    control. This partition is invisible */
623                                 /* DEBUG(0,("DENYING NON-GC OP: %s\n", ldb_module_call_chain(req, req))); */
624                                 continue;
625                         }
626                 }
627
628                 if (phantom_root) {
629                         /* Phantom root: Find all partitions under the
630                          * search base. We match if:
631                          *
632                          * 1) the DN we are looking for exactly matches a
633                          *    certain partition and always stop
634                          * 2) the DN we are looking for is a parent of certain
635                          *    partitions and it isn't a scope base search
636                          * 3) the DN we are looking for is a child of a certain
637                          *    partition and always stop
638                          *    - we don't need to go any further up in the
639                          *    hierarchy!
640                          */
641                         if (ldb_dn_compare(data->partitions[i]->ctrl->dn,
642                                            req->op.search.base) == 0) {
643                                 match = true;
644                                 stop = true;
645                         }
646                         if (!match &&
647                             (ldb_dn_compare_base(req->op.search.base,
648                                                  data->partitions[i]->ctrl->dn) == 0 &&
649                              req->op.search.scope != LDB_SCOPE_BASE)) {
650                                 match = true;
651                         }
652                         if (!match &&
653                             ldb_dn_compare_base(data->partitions[i]->ctrl->dn,
654                                                 req->op.search.base) == 0) {
655                                 match = true;
656                                 stop = true; /* note that this relies on partition ordering */
657                         }
658                 } else {
659                         /* Domain scope: Find all partitions under the search
660                          * base.
661                          *
662                          * We generate referral candidates if we haven't
663                          * specified the domain scope control, haven't a base
664                          * search* scope and the DN we are looking for is a real
665                          * predecessor of certain partitions. When a new
666                          * referral candidate is nearer to the DN than an
667                          * existing one delete the latter (we want to have only
668                          * the closest ones). When we checked this for all
669                          * candidates we have the final referrals.
670                          *
671                          * We match if the DN we are looking for is a child of
672                          * a certain partition or the partition
673                          * DN itself - we don't need to go any further
674                          * up in the hierarchy!
675                          */
676                         if ((!domain_scope) &&
677                             (req->op.search.scope != LDB_SCOPE_BASE) &&
678                             (ldb_dn_compare_base(req->op.search.base,
679                                                  data->partitions[i]->ctrl->dn) == 0) &&
680                             (ldb_dn_compare(req->op.search.base,
681                                             data->partitions[i]->ctrl->dn) != 0)) {
682                                 char *ref = talloc_asprintf(ac,
683                                                             "ldap://%s/%s%s",
684                                                             lpcfg_dnsdomain(lp_ctx),
685                                                             ldb_dn_get_linearized(data->partitions[i]->ctrl->dn),
686                                                             req->op.search.scope == LDB_SCOPE_ONELEVEL ? "??base" : "");
687
688                                 if (ref == NULL) {
689                                         return ldb_oom(ldb);
690                                 }
691
692                                 /* Initialise the referrals list */
693                                 if (ac->referrals == NULL) {
694                                         char **l = str_list_make_empty(ac);
695                                         ac->referrals = discard_const_p(const char *, l);
696                                         if (ac->referrals == NULL) {
697                                                 return ldb_oom(ldb);
698                                         }
699                                 }
700
701                                 /* Check if the new referral candidate is
702                                  * closer to the base DN than already
703                                  * saved ones and delete the latters */
704                                 j = 0;
705                                 while (ac->referrals[j] != NULL) {
706                                         if (strstr(ac->referrals[j],
707                                                    ldb_dn_get_linearized(data->partitions[i]->ctrl->dn)) != NULL) {
708                                                 str_list_remove(ac->referrals,
709                                                                 ac->referrals[j]);
710                                         } else {
711                                                 ++j;
712                                         }
713                                 }
714
715                                 /* Add our new candidate */
716                                 ac->referrals = str_list_add(ac->referrals, ref);
717
718                                 talloc_free(ref);
719
720                                 if (ac->referrals == NULL) {
721                                         return ldb_oom(ldb);
722                                 }
723                         }
724                         if (ldb_dn_compare_base(data->partitions[i]->ctrl->dn, req->op.search.base) == 0) {
725                                 match = true;
726                                 stop = true; /* note that this relies on partition ordering */
727                         }
728                 }
729
730                 if (match) {
731                         ret = partition_prep_request(ac, data->partitions[i]);
732                         if (ret != LDB_SUCCESS) {
733                                 return ret;
734                         }
735                 }
736
737                 if (stop) break;
738         }
739
740         /* Perhaps we didn't match any partitions. Try the main partition */
741         if (ac->num_requests == 0) {
742                 talloc_free(ac);
743                 return ldb_next_request(module, req);
744         }
745
746         /* fire the first one */
747         return partition_call_first(ac);
748 }
749
750 /* add */
751 static int partition_add(struct ldb_module *module, struct ldb_request *req)
752 {
753         return partition_replicate(module, req, req->op.add.message->dn);
754 }
755
756 /* modify */
757 static int partition_modify(struct ldb_module *module, struct ldb_request *req)
758 {
759         return partition_replicate(module, req, req->op.mod.message->dn);
760 }
761
762 /* delete */
763 static int partition_delete(struct ldb_module *module, struct ldb_request *req)
764 {
765         return partition_replicate(module, req, req->op.del.dn);
766 }
767
768 /* rename */
769 static int partition_rename(struct ldb_module *module, struct ldb_request *req)
770 {
771         /* Find backend */
772         struct dsdb_partition *backend, *backend2;
773         
774         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
775                                                               struct partition_private_data);
776
777         /* Skip the lot if 'data' isn't here yet (initialisation) */
778         if (!data) {
779                 return ldb_operr(ldb_module_get_ctx(module));
780         }
781
782         backend = find_partition(data, req->op.rename.olddn, req);
783         backend2 = find_partition(data, req->op.rename.newdn, req);
784
785         if ((backend && !backend2) || (!backend && backend2)) {
786                 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
787         }
788
789         if (backend != backend2) {
790                 ldb_asprintf_errstring(ldb_module_get_ctx(module), 
791                                        "Cannot rename from %s in %s to %s in %s: %s",
792                                        ldb_dn_get_linearized(req->op.rename.olddn),
793                                        ldb_dn_get_linearized(backend->ctrl->dn),
794                                        ldb_dn_get_linearized(req->op.rename.newdn),
795                                        ldb_dn_get_linearized(backend2->ctrl->dn),
796                                        ldb_strerror(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
797                 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
798         }
799
800         return partition_replicate(module, req, req->op.rename.olddn);
801 }
802
803 /* start a transaction */
804 static int partition_start_trans(struct ldb_module *module)
805 {
806         int i;
807         int ret;
808         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
809                                                               struct partition_private_data);
810         /* Look at base DN */
811         /* Figure out which partition it is under */
812         /* Skip the lot if 'data' isn't here yet (initialization) */
813         if (ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING) {
814                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_start_trans() -> (metadata partition)");
815         }
816
817         /* This order must match that in prepare_commit() */
818         ret = ldb_next_start_trans(module);
819         if (ret != LDB_SUCCESS) {
820                 return ret;
821         }
822
823         ret = partition_reload_if_required(module, data, NULL);
824         if (ret != LDB_SUCCESS) {
825                 ldb_next_del_trans(module);
826                 return ret;
827         }
828
829         for (i=0; data && data->partitions && data->partitions[i]; i++) {
830                 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
831                         ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_start_trans() -> %s",
832                                   ldb_dn_get_linearized(data->partitions[i]->ctrl->dn));
833                 }
834                 ret = ldb_next_start_trans(data->partitions[i]->module);
835                 if (ret != LDB_SUCCESS) {
836                         /* Back it out, if it fails on one */
837                         for (i--; i >= 0; i--) {
838                                 ldb_next_del_trans(data->partitions[i]->module);
839                         }
840                         ldb_next_del_trans(module);
841                         partition_metadata_del_trans(module);
842                         return ret;
843                 }
844         }
845
846         /*
847          * Because in prepare_commit this must come last, to ensure
848          * lock ordering we have to do this last here also 
849          */
850         ret = partition_metadata_start_trans(module);
851         if (ret != LDB_SUCCESS) {
852                 /* Back it out, if it fails on one */
853                 for (i--; i >= 0; i--) {
854                         ldb_next_del_trans(data->partitions[i]->module);
855                 }
856                 ldb_next_del_trans(module);
857                 return ret;
858         }
859
860         data->in_transaction++;
861
862         return LDB_SUCCESS;
863 }
864
865 /* prepare for a commit */
866 static int partition_prepare_commit(struct ldb_module *module)
867 {
868         unsigned int i;
869         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
870                                                               struct partition_private_data);
871         int ret;
872
873         ret = ldb_next_prepare_commit(module);
874         if (ret != LDB_SUCCESS) {
875                 return ret;
876         }
877
878         for (i=0; data && data->partitions && data->partitions[i]; i++) {
879                 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
880                         ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_prepare_commit() -> %s",
881                                   ldb_dn_get_linearized(data->partitions[i]->ctrl->dn));
882                 }
883                 ret = ldb_next_prepare_commit(data->partitions[i]->module);
884                 if (ret != LDB_SUCCESS) {
885                         ldb_asprintf_errstring(ldb_module_get_ctx(module), "prepare_commit error on %s: %s",
886                                                ldb_dn_get_linearized(data->partitions[i]->ctrl->dn),
887                                                ldb_errstring(ldb_module_get_ctx(module)));
888                         return ret;
889                 }
890         }
891
892         if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
893                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_prepare_commit() -> (metadata partition)");
894         }
895
896         /* metadata prepare commit must come last, as other partitions could modify
897          * the database inside the prepare commit method of a module */
898         return partition_metadata_prepare_commit(module);
899 }
900
901
902 /* end a transaction */
903 static int partition_end_trans(struct ldb_module *module)
904 {
905         int ret, ret2;
906         unsigned int i;
907         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
908                                                               struct partition_private_data);
909
910         ret = LDB_SUCCESS;
911
912         if (data->in_transaction == 0) {
913                 DEBUG(0,("partition end transaction mismatch\n"));
914                 ret = LDB_ERR_OPERATIONS_ERROR;
915         } else {
916                 data->in_transaction--;
917         }
918
919         ret2 = partition_metadata_end_trans(module);
920         if (ret2 != LDB_SUCCESS) {
921                 ret = ret2;
922         }
923
924         for (i=0; data && data->partitions && data->partitions[i]; i++) {
925                 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
926                         ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_end_trans() -> %s",
927                                   ldb_dn_get_linearized(data->partitions[i]->ctrl->dn));
928                 }
929                 ret2 = ldb_next_end_trans(data->partitions[i]->module);
930                 if (ret2 != LDB_SUCCESS) {
931                         ldb_asprintf_errstring(ldb_module_get_ctx(module), "end_trans error on %s: %s",
932                                                ldb_dn_get_linearized(data->partitions[i]->ctrl->dn),
933                                                ldb_errstring(ldb_module_get_ctx(module)));
934                         ret = ret2;
935                 }
936         }
937
938         if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
939                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_end_trans() -> (metadata partition)");
940         }
941         ret2 = ldb_next_end_trans(module);
942         if (ret2 != LDB_SUCCESS) {
943                 ret = ret2;
944         }
945         return ret;
946 }
947
948 /* delete a transaction */
949 static int partition_del_trans(struct ldb_module *module)
950 {
951         int ret, final_ret = LDB_SUCCESS;
952         unsigned int i;
953         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
954                                                               struct partition_private_data);
955         ret = partition_metadata_del_trans(module);
956         if (ret != LDB_SUCCESS) {
957                 final_ret = ret;
958         }
959
960         for (i=0; data && data->partitions && data->partitions[i]; i++) {
961                 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
962                         ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_del_trans() -> %s",
963                                   ldb_dn_get_linearized(data->partitions[i]->ctrl->dn));
964                 }
965                 ret = ldb_next_del_trans(data->partitions[i]->module);
966                 if (ret != LDB_SUCCESS) {
967                         ldb_asprintf_errstring(ldb_module_get_ctx(module), "del_trans error on %s: %s",
968                                                ldb_dn_get_linearized(data->partitions[i]->ctrl->dn),
969                                                ldb_errstring(ldb_module_get_ctx(module)));
970                         final_ret = ret;
971                 }
972         }       
973
974         if (data->in_transaction == 0) {
975                 DEBUG(0,("partition del transaction mismatch\n"));
976                 return ldb_operr(ldb_module_get_ctx(module));
977         }
978         data->in_transaction--;
979
980         if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
981                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_del_trans() -> (metadata partition)");
982         }
983         ret = ldb_next_del_trans(module);
984         if (ret != LDB_SUCCESS) {
985                 final_ret = ret;
986         }
987         return final_ret;
988 }
989
990 int partition_primary_sequence_number(struct ldb_module *module, TALLOC_CTX *mem_ctx, 
991                                       uint64_t *seq_number,
992                                       struct ldb_request *parent)
993 {
994         int ret;
995         struct ldb_result *res;
996         struct ldb_seqnum_request *tseq;
997         struct ldb_seqnum_result *seqr;
998
999         tseq = talloc_zero(mem_ctx, struct ldb_seqnum_request);
1000         if (tseq == NULL) {
1001                 return ldb_oom(ldb_module_get_ctx(module));
1002         }
1003         tseq->type = LDB_SEQ_HIGHEST_SEQ;
1004         
1005         ret = dsdb_module_extended(module, tseq, &res,
1006                                    LDB_EXTENDED_SEQUENCE_NUMBER,
1007                                    tseq,
1008                                    DSDB_FLAG_NEXT_MODULE,
1009                                    parent);
1010         if (ret != LDB_SUCCESS) {
1011                 talloc_free(tseq);
1012                 return ret;
1013         }
1014         
1015         seqr = talloc_get_type_abort(res->extended->data,
1016                                      struct ldb_seqnum_result);
1017         if (seqr->flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
1018                 talloc_free(res);
1019                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
1020                         "Primary backend in partition module returned a timestamp based seq");
1021         }
1022
1023         *seq_number = seqr->seq_num;
1024         talloc_free(tseq);
1025         return LDB_SUCCESS;
1026 }
1027
1028
1029 /*
1030  * Older version of sequence number as sum of sequence numbers for each partition
1031  */
1032 int partition_sequence_number_from_partitions(struct ldb_module *module,
1033                                               uint64_t *seqr)
1034 {
1035         int ret;
1036         unsigned int i;
1037         uint64_t seq_number = 0;
1038         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
1039                                                               struct partition_private_data);
1040
1041         ret = partition_primary_sequence_number(module, data, &seq_number, NULL);
1042         if (ret != LDB_SUCCESS) {
1043                 return ret;
1044         }
1045         
1046         /* Skip the lot if 'data' isn't here yet (initialisation) */
1047         for (i=0; data && data->partitions && data->partitions[i]; i++) {
1048                 struct ldb_seqnum_request *tseq;
1049                 struct ldb_seqnum_result *tseqr;
1050                 struct ldb_request *treq;
1051                 struct ldb_result *res = talloc_zero(data, struct ldb_result);
1052                 if (res == NULL) {
1053                         return ldb_oom(ldb_module_get_ctx(module));
1054                 }
1055                 tseq = talloc_zero(res, struct ldb_seqnum_request);
1056                 if (tseq == NULL) {
1057                         talloc_free(res);
1058                         return ldb_oom(ldb_module_get_ctx(module));
1059                 }
1060                 tseq->type = LDB_SEQ_HIGHEST_SEQ;
1061                 
1062                 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
1063                                              LDB_EXTENDED_SEQUENCE_NUMBER,
1064                                              tseq,
1065                                              NULL,
1066                                              res,
1067                                              ldb_extended_default_callback,
1068                                              NULL);
1069                 LDB_REQ_SET_LOCATION(treq);
1070                 if (ret != LDB_SUCCESS) {
1071                         talloc_free(res);
1072                         return ret;
1073                 }
1074                 
1075                 ret = partition_request(data->partitions[i]->module, treq);
1076                 if (ret != LDB_SUCCESS) {
1077                         talloc_free(res);
1078                         return ret;
1079                 }
1080                 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
1081                 if (ret != LDB_SUCCESS) {
1082                         talloc_free(res);
1083                         return ret;
1084                 }
1085                 tseqr = talloc_get_type(res->extended->data,
1086                                         struct ldb_seqnum_result);
1087                 seq_number += tseqr->seq_num;
1088                 talloc_free(res);
1089         }
1090
1091         *seqr = seq_number;
1092         return LDB_SUCCESS;
1093 }
1094
1095
1096 /*
1097  * Newer version of sequence number using metadata tdb
1098  */
1099 static int partition_sequence_number(struct ldb_module *module, struct ldb_request *req)
1100 {
1101         struct ldb_extended *ext;
1102         struct ldb_seqnum_request *seq;
1103         struct ldb_seqnum_result *seqr;
1104         uint64_t seq_number;
1105         int ret;
1106
1107         seq = talloc_get_type_abort(req->op.extended.data, struct ldb_seqnum_request);
1108         switch (seq->type) {
1109         case LDB_SEQ_NEXT:
1110                 ret = partition_metadata_sequence_number_increment(module, &seq_number);
1111                 if (ret != LDB_SUCCESS) {
1112                         return ret;
1113                 }
1114                 break;
1115
1116         case LDB_SEQ_HIGHEST_SEQ:
1117                 ret = partition_metadata_sequence_number(module, &seq_number);
1118                 if (ret != LDB_SUCCESS) {
1119                         return ret;
1120                 }
1121                 break;
1122
1123         case LDB_SEQ_HIGHEST_TIMESTAMP:
1124                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
1125                                         "LDB_SEQ_HIGHEST_TIMESTAMP not supported");
1126         }
1127
1128         ext = talloc_zero(req, struct ldb_extended);
1129         if (!ext) {
1130                 return ldb_module_oom(module);
1131         }
1132         seqr = talloc_zero(ext, struct ldb_seqnum_result);
1133         if (seqr == NULL) {
1134                 talloc_free(ext);
1135                 return ldb_module_oom(module);
1136         }
1137         ext->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
1138         ext->data = seqr;
1139
1140         seqr->seq_num = seq_number;
1141         seqr->flags |= LDB_SEQ_GLOBAL_SEQUENCE;
1142
1143         /* send request done */
1144         return ldb_module_done(req, NULL, ext, LDB_SUCCESS);
1145 }
1146
1147 /* extended */
1148 static int partition_extended(struct ldb_module *module, struct ldb_request *req)
1149 {
1150         struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
1151                                                               struct partition_private_data);
1152         struct partition_context *ac;
1153         int ret;
1154
1155         /* if we aren't initialised yet go further */
1156         if (!data) {
1157                 return ldb_next_request(module, req);
1158         }
1159
1160         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) == 0) {
1161                 /* Update the metadata.tdb to increment the schema version if needed*/
1162                 DEBUG(10, ("Incrementing the sequence_number after schema_update_now\n"));
1163                 ret = partition_metadata_inc_schema_sequence(module);
1164                 return ldb_module_done(req, NULL, NULL, ret);
1165         }
1166         
1167         if (strcmp(req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1168                 return partition_sequence_number(module, req);
1169         }
1170
1171         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_CREATE_PARTITION_OID) == 0) {
1172                 return partition_create(module, req);
1173         }
1174
1175         /* 
1176          * as the extended operation has no dn
1177          * we need to send it to all partitions
1178          */
1179
1180         ac = partition_init_ctx(module, req);
1181         if (!ac) {
1182                 return ldb_operr(ldb_module_get_ctx(module));
1183         }
1184
1185         return partition_send_all(module, ac, req);
1186 }
1187
1188 static const struct ldb_module_ops ldb_partition_module_ops = {
1189         .name              = "partition",
1190         .init_context      = partition_init,
1191         .search            = partition_search,
1192         .add               = partition_add,
1193         .modify            = partition_modify,
1194         .del               = partition_delete,
1195         .rename            = partition_rename,
1196         .extended          = partition_extended,
1197         .start_transaction = partition_start_trans,
1198         .prepare_commit    = partition_prepare_commit,
1199         .end_transaction   = partition_end_trans,
1200         .del_transaction   = partition_del_trans,
1201 };
1202
1203 int ldb_partition_module_init(const char *version)
1204 {
1205         LDB_MODULE_CHECK_VERSION(version);
1206         return ldb_register_module(&ldb_partition_module_ops);
1207 }