ad2901c308bf4671edaa883bf282110622fcc955
[kai/samba.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    * NOTICE: this module is NOT released under the GNU LGPL license as
8    * other ldb code. This module is release under the GNU GPL v3 or
9    * later license.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb partitions module
29  *
30  *  Description: Implement LDAP partitions
31  *
32  *  Author: Andrew Bartlett
33  *  Author: Stefan Metzmacher
34  */
35
36 #include "includes.h"
37 #include "ldb/include/ldb_includes.h"
38 #include "dsdb/samdb/samdb.h"
39
40 struct partition_private_data {
41         struct dsdb_control_current_partition **partitions;
42         struct ldb_dn **replicate;
43 };
44
45 struct part_request {
46         struct ldb_module *module;
47         struct ldb_request *req;
48 };
49
50 struct partition_context {
51         struct ldb_module *module;
52         struct ldb_request *req;
53
54         struct part_request *part_req;
55         int num_requests;
56         int finished_requests;
57 };
58
59 static struct partition_context *partition_init_ctx(struct ldb_module *module, struct ldb_request *req)
60 {
61         struct partition_context *ac;
62
63         ac = talloc_zero(req, struct partition_context);
64         if (ac == NULL) {
65                 ldb_set_errstring(module->ldb, "Out of Memory");
66                 return NULL;
67         }
68
69         ac->module = module;
70         ac->req = req;
71
72         return ac;
73 }
74
75 #define PARTITION_FIND_OP(module, op) do { \
76         struct ldb_context *ldbctx = module->ldb; \
77         while (module && module->ops->op == NULL) module = module->next; \
78         if (module == NULL) { \
79                 ldb_asprintf_errstring(ldbctx, \
80                         "Unable to find backend operation for " #op ); \
81                 return LDB_ERR_OPERATIONS_ERROR; \
82         } \
83 } while (0)
84
85 /*
86  *    helper functions to call the next module in chain
87  *    */
88
89 int partition_request(struct ldb_module *module, struct ldb_request *request)
90 {
91         int ret;
92         switch (request->operation) {
93         case LDB_SEARCH:
94                 PARTITION_FIND_OP(module, search);
95                 ret = module->ops->search(module, request);
96                 break;
97         case LDB_ADD:
98                 PARTITION_FIND_OP(module, add);
99                 ret = module->ops->add(module, request);
100                 break;
101         case LDB_MODIFY:
102                 PARTITION_FIND_OP(module, modify);
103                 ret = module->ops->modify(module, request);
104                 break;
105         case LDB_DELETE:
106                 PARTITION_FIND_OP(module, del);
107                 ret = module->ops->del(module, request);
108                 break;
109         case LDB_RENAME:
110                 PARTITION_FIND_OP(module, rename);
111                 ret = module->ops->rename(module, request);
112                 break;
113         case LDB_EXTENDED:
114                 PARTITION_FIND_OP(module, extended);
115                 ret = module->ops->extended(module, request);
116                 break;
117         case LDB_SEQUENCE_NUMBER:
118                 PARTITION_FIND_OP(module, sequence_number);
119                 ret = module->ops->sequence_number(module, request);
120                 break;
121         default:
122                 PARTITION_FIND_OP(module, request);
123                 ret = module->ops->request(module, request);
124                 break;
125         }
126         if (ret == LDB_SUCCESS) {
127                 return ret;
128         }
129         if (!ldb_errstring(module->ldb)) {
130                 /* Set a default error string, to place the blame somewhere */
131                 ldb_asprintf_errstring(module->ldb,
132                                         "error in module %s: %s (%d)",
133                                         module->ops->name,
134                                         ldb_strerror(ret), ret);
135         }
136         return ret;
137 }
138
139 static struct dsdb_control_current_partition *find_partition(struct partition_private_data *data,
140                                                              struct ldb_dn *dn)
141 {
142         int i;
143
144         /* Look at base DN */
145         /* Figure out which partition it is under */
146         /* Skip the lot if 'data' isn't here yet (initialistion) */
147         for (i=0; data && data->partitions && data->partitions[i]; i++) {
148                 if (ldb_dn_compare_base(data->partitions[i]->dn, dn) == 0) {
149                         return data->partitions[i];
150                 }
151         }
152
153         return NULL;
154 };
155
156 /**
157  * fire the caller's callback for every entry, but only send 'done' once.
158  */
159 static int partition_req_callback(struct ldb_request *req,
160                                   struct ldb_reply *ares)
161 {
162         struct partition_context *ac;
163         struct ldb_module *module;
164         struct ldb_request *nreq;
165         int ret;
166
167         ac = talloc_get_type(req->context, struct partition_context);
168
169         if (!ares) {
170                 return ldb_module_done(ac->req, NULL, NULL,
171                                         LDB_ERR_OPERATIONS_ERROR);
172         }
173         if (ares->error != LDB_SUCCESS) {
174                 return ldb_module_done(ac->req, ares->controls,
175                                         ares->response, ares->error);
176         }
177
178         switch (ares->type) {
179         case LDB_REPLY_REFERRAL:
180                 /* ignore referrals for now */
181                 break;
182
183         case LDB_REPLY_ENTRY:
184                 if (ac->req->operation != LDB_SEARCH) {
185                         ldb_set_errstring(ac->module->ldb,
186                                 "partition_req_callback:"
187                                 " Unsupported reply type for this request");
188                         return ldb_module_done(ac->req, NULL, NULL,
189                                                 LDB_ERR_OPERATIONS_ERROR);
190                 }
191                 return ldb_module_send_entry(ac->req, ares->message);
192
193         case LDB_REPLY_DONE:
194                 if (ac->req->operation == LDB_EXTENDED) {
195                         /* FIXME: check for ares->response, replmd does not fill it ! */
196                         if (ares->response) {
197                                 if (strcmp(ares->response->oid, LDB_EXTENDED_START_TLS_OID) != 0) {
198                                         ldb_set_errstring(ac->module->ldb,
199                                                           "partition_req_callback:"
200                                                           " Unknown extended reply, "
201                                                           "only supports START_TLS");
202                                         talloc_free(ares);
203                                         return ldb_module_done(ac->req, NULL, NULL,
204                                                                 LDB_ERR_OPERATIONS_ERROR);
205                                 }
206                         }
207                 }
208
209                 ac->finished_requests++;
210                 if (ac->finished_requests == ac->num_requests) {
211                         /* this was the last one, call callback */
212                         return ldb_module_done(ac->req, ares->controls,
213                                                 ares->response, ares->error);
214                 }
215
216                 /* not the last, now call the next one */
217                 module = ac->part_req[ac->finished_requests].module;
218                 nreq = ac->part_req[ac->finished_requests].req;
219
220                 ret = partition_request(module, nreq);
221                 if (ret != LDB_SUCCESS) {
222                         talloc_free(ares);
223                         return ldb_module_done(ac->req, NULL, NULL, ret);
224                 }
225
226                 break;
227         }
228
229         talloc_free(ares);
230         return LDB_SUCCESS;
231 }
232
233 static int partition_prep_request(struct partition_context *ac,
234                                   struct dsdb_control_current_partition *partition)
235 {
236         int ret;
237         struct ldb_request *req;
238
239         ac->part_req = talloc_realloc(ac, ac->part_req,
240                                         struct part_request,
241                                         ac->num_requests + 1);
242         if (ac->part_req == NULL) {
243                 ldb_oom(ac->module->ldb);
244                 return LDB_ERR_OPERATIONS_ERROR;
245         }
246
247         switch (ac->req->operation) {
248         case LDB_SEARCH:
249                 ret = ldb_build_search_req_ex(&req, ac->module->ldb,
250                                         ac->part_req,
251                                         ac->req->op.search.base,
252                                         ac->req->op.search.scope,
253                                         ac->req->op.search.tree,
254                                         ac->req->op.search.attrs,
255                                         ac->req->controls,
256                                         ac, partition_req_callback,
257                                         ac->req);
258                 break;
259         case LDB_ADD:
260                 ret = ldb_build_add_req(&req, ac->module->ldb, ac->part_req,
261                                         ac->req->op.add.message,
262                                         ac->req->controls,
263                                         ac, partition_req_callback,
264                                         ac->req);
265                 break;
266         case LDB_MODIFY:
267                 ret = ldb_build_mod_req(&req, ac->module->ldb, ac->part_req,
268                                         ac->req->op.mod.message,
269                                         ac->req->controls,
270                                         ac, partition_req_callback,
271                                         ac->req);
272                 break;
273         case LDB_DELETE:
274                 ret = ldb_build_del_req(&req, ac->module->ldb, ac->part_req,
275                                         ac->req->op.del.dn,
276                                         ac->req->controls,
277                                         ac, partition_req_callback,
278                                         ac->req);
279                 break;
280         case LDB_RENAME:
281                 ret = ldb_build_rename_req(&req, ac->module->ldb, ac->part_req,
282                                         ac->req->op.rename.olddn,
283                                         ac->req->op.rename.newdn,
284                                         ac->req->controls,
285                                         ac, partition_req_callback,
286                                         ac->req);
287                 break;
288         case LDB_EXTENDED:
289                 ret = ldb_build_extended_req(&req, ac->module->ldb,
290                                         ac->part_req,
291                                         ac->req->op.extended.oid,
292                                         ac->req->op.extended.data,
293                                         ac->req->controls,
294                                         ac, partition_req_callback,
295                                         ac->req);
296                 break;
297         default:
298                 ldb_set_errstring(ac->module->ldb,
299                                   "Unsupported request type!");
300                 ret = LDB_ERR_UNWILLING_TO_PERFORM;
301         }
302
303         if (ret != LDB_SUCCESS) {
304                 return ret;
305         }
306
307         ac->part_req[ac->num_requests].req = req;
308
309         if (ac->req->controls) {
310                 req->controls = talloc_memdup(req, ac->req->controls,
311                                         talloc_get_size(ac->req->controls));
312                 if (req->controls == NULL) {
313                         ldb_oom(ac->module->ldb);
314                         return LDB_ERR_OPERATIONS_ERROR;
315                 }
316         }
317
318         if (partition) {
319                 ac->part_req[ac->num_requests].module = partition->module;
320
321                 ret = ldb_request_add_control(req,
322                                         DSDB_CONTROL_CURRENT_PARTITION_OID,
323                                         false, partition);
324                 if (ret != LDB_SUCCESS) {
325                         return ret;
326                 }
327
328                 if (req->operation == LDB_SEARCH) {
329                         /* If the search is for 'more' than this partition,
330                          * then change the basedn, so a remote LDAP server
331                          * doesn't object */
332                         if (ldb_dn_compare_base(partition->dn,
333                                                 req->op.search.base) != 0) {
334                                 req->op.search.base = partition->dn;
335                         }
336                 }
337
338         } else {
339                 /* make sure you put the NEXT module here, or
340                  * partition_request() will simply loop forever on itself */
341                 ac->part_req[ac->num_requests].module = ac->module->next;
342         }
343
344         ac->num_requests++;
345
346         return LDB_SUCCESS;
347 }
348
349 static int partition_call_first(struct partition_context *ac)
350 {
351         return partition_request(ac->part_req[0].module, ac->part_req[0].req);
352 }
353
354 /**
355  * Send a request down to all the partitions
356  */
357 static int partition_send_all(struct ldb_module *module, 
358                               struct partition_context *ac, 
359                               struct ldb_request *req) 
360 {
361         int i;
362         struct partition_private_data *data = talloc_get_type(module->private_data, 
363                                                               struct partition_private_data);
364         int ret = partition_prep_request(ac, NULL);
365         if (ret != LDB_SUCCESS) {
366                 return ret;
367         }
368         for (i=0; data && data->partitions && data->partitions[i]; i++) {
369                 ret = partition_prep_request(ac, data->partitions[i]);
370                 if (ret != LDB_SUCCESS) {
371                         return ret;
372                 }
373         }
374
375         /* fire the first one */
376         return partition_call_first(ac);
377 }
378
379 /**
380  * Figure out which backend a request needs to be aimed at.  Some
381  * requests must be replicated to all backends
382  */
383 static int partition_replicate(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn) 
384 {
385         struct partition_context *ac;
386         unsigned i;
387         int ret;
388         struct dsdb_control_current_partition *partition;
389         struct partition_private_data *data = talloc_get_type(module->private_data, 
390                                                               struct partition_private_data);
391         if (!data || !data->partitions) {
392                 return ldb_next_request(module, req);
393         }
394         
395         if (req->operation != LDB_SEARCH) {
396                 /* Is this a special DN, we need to replicate to every backend? */
397                 for (i=0; data->replicate && data->replicate[i]; i++) {
398                         if (ldb_dn_compare(data->replicate[i], 
399                                            dn) == 0) {
400                                 
401                                 ac = partition_init_ctx(module, req);
402                                 if (!ac) {
403                                         return LDB_ERR_OPERATIONS_ERROR;
404                                 }
405                                 
406                                 return partition_send_all(module, ac, req);
407                         }
408                 }
409         }
410
411         /* Otherwise, we need to find the partition to fire it to */
412
413         /* Find partition */
414         partition = find_partition(data, dn);
415         if (!partition) {
416                 /*
417                  * if we haven't found a matching partition
418                  * pass the request to the main ldb
419                  *
420                  * TODO: we should maybe return an error here
421                  *       if it's not a special dn
422                  */
423
424                 return ldb_next_request(module, req);
425         }
426
427         ac = partition_init_ctx(module, req);
428         if (!ac) {
429                 return LDB_ERR_OPERATIONS_ERROR;
430         }
431
432         /* we need to add a control but we never touch the original request */
433         ret = partition_prep_request(ac, partition);
434         if (ret != LDB_SUCCESS) {
435                 return ret;
436         }
437
438         /* fire the first one */
439         return partition_call_first(ac);
440 }
441
442 /* search */
443 static int partition_search(struct ldb_module *module, struct ldb_request *req)
444 {
445         struct ldb_control **saved_controls;
446         
447         /* Find backend */
448         struct partition_private_data *data = talloc_get_type(module->private_data, 
449                                                               struct partition_private_data);
450         /* issue request */
451
452         /* (later) consider if we should be searching multiple
453          * partitions (for 'invisible' partition behaviour */
454
455         struct ldb_control *search_control = ldb_request_get_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID);
456         struct ldb_control *domain_scope_control = ldb_request_get_control(req, LDB_CONTROL_DOMAIN_SCOPE_OID);
457         
458         struct ldb_search_options_control *search_options = NULL;
459         if (search_control) {
460                 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
461         }
462
463         /* Remove the domain_scope control, so we don't confuse a backend server */
464         if (domain_scope_control && !save_controls(domain_scope_control, req, &saved_controls)) {
465                 ldb_oom(module->ldb);
466                 return LDB_ERR_OPERATIONS_ERROR;
467         }
468
469         /* TODO:
470            Generate referrals (look for a partition under this DN) if we don't have the above control specified
471         */
472         
473         if (search_options && (search_options->search_options & LDB_SEARCH_OPTION_PHANTOM_ROOT)) {
474                 int ret, i;
475                 struct partition_context *ac;
476                 if ((search_options->search_options & ~LDB_SEARCH_OPTION_PHANTOM_ROOT) == 0) {
477                         /* We have processed this flag, so we are done with this control now */
478
479                         /* Remove search control, so we don't confuse a backend server */
480                         if (search_control && !save_controls(search_control, req, &saved_controls)) {
481                                 ldb_oom(module->ldb);
482                                 return LDB_ERR_OPERATIONS_ERROR;
483                         }
484                 }
485                 ac = partition_init_ctx(module, req);
486                 if (!ac) {
487                         return LDB_ERR_OPERATIONS_ERROR;
488                 }
489
490                 /* Search from the base DN */
491                 if (!req->op.search.base || ldb_dn_is_null(req->op.search.base)) {
492                         return partition_send_all(module, ac, req);
493                 }
494                 for (i=0; data && data->partitions && data->partitions[i]; i++) {
495                         /* Find all partitions under the search base */
496                         if (ldb_dn_compare_base(req->op.search.base, data->partitions[i]->dn) == 0) {
497                                 ret = partition_prep_request(ac, data->partitions[i]);
498                                 if (ret != LDB_SUCCESS) {
499                                         return ret;
500                                 }
501                         }
502                 }
503
504                 /* Perhaps we didn't match any partitions.  Try the main partition, only */
505                 if (ac->num_requests == 0) {
506                         talloc_free(ac);
507                         return ldb_next_request(module, req);
508                 }
509
510                 /* fire the first one */
511                 return partition_call_first(ac);
512
513         } else {
514                 /* Handle this like all other requests */
515                 if (search_control && (search_options->search_options & ~LDB_SEARCH_OPTION_PHANTOM_ROOT) == 0) {
516                         /* We have processed this flag, so we are done with this control now */
517
518                         /* Remove search control, so we don't confuse a backend server */
519                         if (search_control && !save_controls(search_control, req, &saved_controls)) {
520                                 ldb_oom(module->ldb);
521                                 return LDB_ERR_OPERATIONS_ERROR;
522                         }
523                 }
524
525                 return partition_replicate(module, req, req->op.search.base);
526         }
527 }
528
529 /* add */
530 static int partition_add(struct ldb_module *module, struct ldb_request *req)
531 {
532         return partition_replicate(module, req, req->op.add.message->dn);
533 }
534
535 /* modify */
536 static int partition_modify(struct ldb_module *module, struct ldb_request *req)
537 {
538         return partition_replicate(module, req, req->op.mod.message->dn);
539 }
540
541 /* delete */
542 static int partition_delete(struct ldb_module *module, struct ldb_request *req)
543 {
544         return partition_replicate(module, req, req->op.del.dn);
545 }
546
547 /* rename */
548 static int partition_rename(struct ldb_module *module, struct ldb_request *req)
549 {
550         int i, matched = -1;
551         /* Find backend */
552         struct dsdb_control_current_partition *backend, *backend2;
553         
554         struct partition_private_data *data = talloc_get_type(module->private_data, 
555                                                               struct partition_private_data);
556
557         /* Skip the lot if 'data' isn't here yet (initialization) */
558         if (!data) {
559                 return LDB_ERR_OPERATIONS_ERROR;
560         }
561
562         backend = find_partition(data, req->op.rename.olddn);
563         backend2 = find_partition(data, req->op.rename.newdn);
564
565         if ((backend && !backend2) || (!backend && backend2)) {
566                 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
567         }
568
569         if (backend != backend2) {
570                 ldb_asprintf_errstring(module->ldb, 
571                                        "Cannot rename from %s in %s to %s in %s: %s",
572                                        ldb_dn_get_linearized(req->op.rename.olddn),
573                                        ldb_dn_get_linearized(backend->dn),
574                                        ldb_dn_get_linearized(req->op.rename.newdn),
575                                        ldb_dn_get_linearized(backend2->dn),
576                                        ldb_strerror(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
577                 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
578         }
579
580         for (i=0; data && data->partitions && data->partitions[i]; i++) {
581                 if (ldb_dn_compare_base(req->op.rename.olddn, data->partitions[i]->dn) == 0) {
582                         matched = i;
583                 }
584         }
585
586         if (matched > 0) {
587                 ldb_asprintf_errstring(module->ldb, 
588                                        "Cannot rename from %s to %s, subtree rename would cross partition %s: %s",
589                                        ldb_dn_get_linearized(req->op.rename.olddn),
590                                        ldb_dn_get_linearized(req->op.rename.newdn),
591                                        ldb_dn_get_linearized(data->partitions[matched]->dn),
592                                        ldb_strerror(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
593                 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
594         }
595
596         return partition_replicate(module, req, req->op.rename.olddn);
597 }
598
599 /* start a transaction */
600 static int partition_start_trans(struct ldb_module *module)
601 {
602         int i, ret;
603         struct partition_private_data *data = talloc_get_type(module->private_data, 
604                                                               struct partition_private_data);
605         /* Look at base DN */
606         /* Figure out which partition it is under */
607         /* Skip the lot if 'data' isn't here yet (initialization) */
608         ret = ldb_next_start_trans(module);
609         if (ret != LDB_SUCCESS) {
610                 return ret;
611         }
612
613         for (i=0; data && data->partitions && data->partitions[i]; i++) {
614                 struct ldb_module *next = data->partitions[i]->module;
615                 PARTITION_FIND_OP(next, start_transaction);
616
617                 ret = next->ops->start_transaction(next);
618                 if (ret != LDB_SUCCESS) {
619                         /* Back it out, if it fails on one */
620                         for (i--; i >= 0; i--) {
621                                 next = data->partitions[i]->module;
622                                 PARTITION_FIND_OP(next, del_transaction);
623
624                                 next->ops->del_transaction(next);
625                         }
626                         ldb_next_del_trans(module);
627                         return ret;
628                 }
629         }
630         return LDB_SUCCESS;
631 }
632
633 /* end a transaction */
634 static int partition_end_trans(struct ldb_module *module)
635 {
636         int i, ret;
637         struct partition_private_data *data = talloc_get_type(module->private_data, 
638                                                               struct partition_private_data);
639         ret = ldb_next_end_trans(module);
640         if (ret != LDB_SUCCESS) {
641                 return ret;
642         }
643
644         /* Look at base DN */
645         /* Figure out which partition it is under */
646         /* Skip the lot if 'data' isn't here yet (initialistion) */
647         for (i=0; data && data->partitions && data->partitions[i]; i++) {
648                 struct ldb_module *next = data->partitions[i]->module;
649                 PARTITION_FIND_OP(next, end_transaction);
650
651                 ret = next->ops->end_transaction(next);
652                 if (ret != LDB_SUCCESS) {
653                         /* Back it out, if it fails on one */
654                         for (i--; i >= 0; i--) {
655                                 next = data->partitions[i]->module;
656                                 PARTITION_FIND_OP(next, del_transaction);
657
658                                 next->ops->del_transaction(next);
659                         }
660                         ldb_next_del_trans(module);
661                         return ret;
662                 }
663         }
664
665         return LDB_SUCCESS;
666 }
667
668 /* delete a transaction */
669 static int partition_del_trans(struct ldb_module *module)
670 {
671         int i, ret, ret2 = LDB_SUCCESS;
672         struct partition_private_data *data = talloc_get_type(module->private_data, 
673                                                               struct partition_private_data);
674         ret = ldb_next_del_trans(module);
675         if (ret != LDB_SUCCESS) {
676                 ret2 = ret;
677         }
678
679         /* Look at base DN */
680         /* Figure out which partition it is under */
681         /* Skip the lot if 'data' isn't here yet (initialistion) */
682         for (i=0; data && data->partitions && data->partitions[i]; i++) {
683                 struct ldb_module *next = data->partitions[i]->module;
684                 PARTITION_FIND_OP(next, del_transaction);
685
686                 ret = next->ops->del_transaction(next);
687                 if (ret != LDB_SUCCESS) {
688                         ret2 = ret;
689                 }
690         }
691         return ret2;
692 }
693
694 /* NOTE: ldb_sequence_number is still a completely SYNCHRONOUS call
695  * implemented only in ldb_rdb. It does not require ldb_wait() to be
696  * called after a request is made */
697 static int partition_sequence_number(struct ldb_module *module, struct ldb_request *req)
698 {
699         int i, ret;
700         uint64_t seq_number = 0;
701         uint64_t timestamp_sequence = 0;
702         uint64_t timestamp = 0;
703         struct partition_private_data *data = talloc_get_type(module->private_data, 
704                                                               struct partition_private_data);
705
706         switch (req->op.seq_num.type) {
707         case LDB_SEQ_NEXT:
708         case LDB_SEQ_HIGHEST_SEQ:
709                 ret = ldb_next_request(module, req);
710                 if (ret != LDB_SUCCESS) {
711                         return ret;
712                 }
713                 if (req->op.seq_num.flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
714                         timestamp_sequence = req->op.seq_num.seq_num;
715                 } else {
716                         seq_number = seq_number + req->op.seq_num.seq_num;
717                 }
718
719                 /* gross hack part1 */
720                 ret = ldb_request_add_control(req,
721                                         DSDB_CONTROL_CURRENT_PARTITION_OID,
722                                         false, NULL);
723                 if (ret != LDB_SUCCESS) {
724                         return ret;
725                 }
726
727                 /* Look at base DN */
728                 /* Figure out which partition it is under */
729                 /* Skip the lot if 'data' isn't here yet (initialistion) */
730                 for (i=0; data && data->partitions && data->partitions[i]; i++) {
731
732                         /* gross hack part2 */
733                         int j;
734                         for (j=0; req->controls[j]; j++) {
735                                 if (strcmp(req->controls[j]->oid, DSDB_CONTROL_CURRENT_PARTITION_OID) == 0) {
736                                         req->controls[j]->data = data->partitions[i];
737                                         break;
738                                 }
739                         }
740
741                         ret = partition_request(data->partitions[i]->module, req);
742                         if (ret != LDB_SUCCESS) {
743                                 return ret;
744                         }
745                         if (req->op.seq_num.flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
746                                 timestamp_sequence = MAX(timestamp_sequence, req->op.seq_num.seq_num);
747                         } else {
748                                 seq_number = seq_number + req->op.seq_num.seq_num;
749                         }
750                 }
751                 /* fall though */
752         case LDB_SEQ_HIGHEST_TIMESTAMP:
753         {
754                 struct ldb_request *date_req = talloc(req, struct ldb_request);
755                 if (!date_req) {
756                         return LDB_ERR_OPERATIONS_ERROR;
757                 }
758                 *date_req = *req;
759                 date_req->op.seq_num.flags = LDB_SEQ_HIGHEST_TIMESTAMP;
760
761                 ret = ldb_next_request(module, date_req);
762                 if (ret != LDB_SUCCESS) {
763                         return ret;
764                 }
765                 timestamp = date_req->op.seq_num.seq_num;
766
767                 /* Look at base DN */
768                 /* Figure out which partition it is under */
769                 /* Skip the lot if 'data' isn't here yet (initialistion) */
770                 for (i=0; data && data->partitions && data->partitions[i]; i++) {
771
772                         ret = partition_request(data->partitions[i]->module, req);
773                         if (ret != LDB_SUCCESS) {
774                                 return ret;
775                         }
776                         timestamp = MAX(timestamp, date_req->op.seq_num.seq_num);
777                 }
778                 break;
779         }
780         }
781
782         switch (req->op.seq_num.flags) {
783         case LDB_SEQ_NEXT:
784         case LDB_SEQ_HIGHEST_SEQ:
785
786                 req->op.seq_num.flags = 0;
787
788                 /* Has someone above set a timebase sequence? */
789                 if (timestamp_sequence) {
790                         req->op.seq_num.seq_num = (((unsigned long long)timestamp << 24) | (seq_number & 0xFFFFFF));
791                 } else {
792                         req->op.seq_num.seq_num = seq_number;
793                 }
794
795                 if (timestamp_sequence > req->op.seq_num.seq_num) {
796                         req->op.seq_num.seq_num = timestamp_sequence;
797                         req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
798                 }
799
800                 req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
801                 break;
802         case LDB_SEQ_HIGHEST_TIMESTAMP:
803                 req->op.seq_num.seq_num = timestamp;
804                 break;
805         }
806
807         switch (req->op.seq_num.flags) {
808         case LDB_SEQ_NEXT:
809                 req->op.seq_num.seq_num++;
810         }
811         return LDB_SUCCESS;
812 }
813
814 static int partition_extended_replicated_objects(struct ldb_module *module, struct ldb_request *req)
815 {
816         struct dsdb_extended_replicated_objects *ext;
817
818         ext = talloc_get_type(req->op.extended.data, struct dsdb_extended_replicated_objects);
819         if (!ext) {
820                 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "partition_extended_replicated_objects: invalid extended data\n");
821                 return LDB_ERR_PROTOCOL_ERROR;
822         }
823
824         if (ext->version != DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION) {
825                 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "partition_extended_replicated_objects: extended data invalid version [%u != %u]\n",
826                           ext->version, DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION);
827                 return LDB_ERR_PROTOCOL_ERROR;
828         }
829
830         return partition_replicate(module, req, ext->partition_dn);
831 }
832
833 static int partition_extended_schema_update_now(struct ldb_module *module, struct ldb_request *req)
834 {
835         struct dsdb_control_current_partition *partition;
836         struct partition_private_data *data;
837         struct ldb_dn *schema_dn;
838         struct partition_context *ac;
839         int ret;
840
841         schema_dn = talloc_get_type(req->op.extended.data, struct ldb_dn);
842         if (!schema_dn) {
843                 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "partition_extended: invalid extended data\n");
844                 return LDB_ERR_PROTOCOL_ERROR;
845         }
846
847         data = talloc_get_type(module->private_data, struct partition_private_data);
848         if (!data) {
849                 return LDB_ERR_OPERATIONS_ERROR;
850         }
851         
852         partition = find_partition( data, schema_dn );
853         if (!partition) {
854                 return ldb_next_request(module, req);
855         }
856
857         ac = partition_init_ctx(module, req);
858         if (!ac) {
859                 return LDB_ERR_OPERATIONS_ERROR;
860         }
861
862         /* we need to add a control but we never touch the original request */
863         ret = partition_prep_request(ac, partition);
864         if (ret != LDB_SUCCESS) {
865                 return ret;
866         }
867
868         /* fire the first one */
869         return partition_call_first(ac);
870 }
871
872
873 /* extended */
874 static int partition_extended(struct ldb_module *module, struct ldb_request *req)
875 {
876         struct partition_private_data *data;
877         struct partition_context *ac;
878
879         data = talloc_get_type(module->private_data, struct partition_private_data);
880         if (!data || !data->partitions) {
881                 return ldb_next_request(module, req);
882         }
883
884         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_REPLICATED_OBJECTS_OID) == 0) {
885                 return partition_extended_replicated_objects(module, req);
886         }
887
888         /* forward schemaUpdateNow operation to schema_fsmo module*/
889         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) == 0) {
890                 return partition_extended_schema_update_now( module, req );
891         }       
892
893         /* 
894          * as the extended operation has no dn
895          * we need to send it to all partitions
896          */
897
898         ac = partition_init_ctx(module, req);
899         if (!ac) {
900                 return LDB_ERR_OPERATIONS_ERROR;
901         }
902
903         return partition_send_all(module, ac, req);
904 }
905
906 static int partition_sort_compare(const void *v1, const void *v2)
907 {
908         struct dsdb_control_current_partition *p1;
909         struct dsdb_control_current_partition *p2;
910
911         p1 = *((struct dsdb_control_current_partition **)v1);
912         p2 = *((struct dsdb_control_current_partition **)v2);
913
914         return ldb_dn_compare(p1->dn, p2->dn);
915 }
916
917 static int partition_init(struct ldb_module *module)
918 {
919         int ret, i;
920         TALLOC_CTX *mem_ctx = talloc_new(module);
921         const char *attrs[] = { "partition", "replicateEntries", "modules", NULL };
922         struct ldb_result *res;
923         struct ldb_message *msg;
924         struct ldb_message_element *partition_attributes;
925         struct ldb_message_element *replicate_attributes;
926         struct ldb_message_element *modules_attributes;
927
928         struct partition_private_data *data;
929
930         if (!mem_ctx) {
931                 return LDB_ERR_OPERATIONS_ERROR;
932         }
933
934         data = talloc(mem_ctx, struct partition_private_data);
935         if (data == NULL) {
936                 return LDB_ERR_OPERATIONS_ERROR;
937         }
938
939         ret = ldb_search(module->ldb, mem_ctx, &res,
940                          ldb_dn_new(mem_ctx, module->ldb, "@PARTITION"),
941                          LDB_SCOPE_BASE, attrs, NULL);
942         if (ret != LDB_SUCCESS) {
943                 talloc_free(mem_ctx);
944                 return ret;
945         }
946         if (res->count == 0) {
947                 talloc_free(mem_ctx);
948                 return ldb_next_init(module);
949         }
950
951         if (res->count > 1) {
952                 talloc_free(mem_ctx);
953                 return LDB_ERR_CONSTRAINT_VIOLATION;
954         }
955
956         msg = res->msgs[0];
957
958         partition_attributes = ldb_msg_find_element(msg, "partition");
959         if (!partition_attributes) {
960                 ldb_set_errstring(module->ldb, "partition_init: no partitions specified");
961                 talloc_free(mem_ctx);
962                 return LDB_ERR_CONSTRAINT_VIOLATION;
963         }
964         data->partitions = talloc_array(data, struct dsdb_control_current_partition *, partition_attributes->num_values + 1);
965         if (!data->partitions) {
966                 talloc_free(mem_ctx);
967                 return LDB_ERR_OPERATIONS_ERROR;
968         }
969         for (i=0; i < partition_attributes->num_values; i++) {
970                 char *base = talloc_strdup(data->partitions, (char *)partition_attributes->values[i].data);
971                 char *p = strchr(base, ':');
972                 if (!p) {
973                         ldb_asprintf_errstring(module->ldb, 
974                                                 "partition_init: "
975                                                 "invalid form for partition record (missing ':'): %s", base);
976                         talloc_free(mem_ctx);
977                         return LDB_ERR_CONSTRAINT_VIOLATION;
978                 }
979                 p[0] = '\0';
980                 p++;
981                 if (!p[0]) {
982                         ldb_asprintf_errstring(module->ldb, 
983                                                 "partition_init: "
984                                                 "invalid form for partition record (missing backend database): %s", base);
985                         talloc_free(mem_ctx);
986                         return LDB_ERR_CONSTRAINT_VIOLATION;
987                 }
988                 data->partitions[i] = talloc(data->partitions, struct dsdb_control_current_partition);
989                 if (!data->partitions[i]) {
990                         talloc_free(mem_ctx);
991                         return LDB_ERR_OPERATIONS_ERROR;
992                 }
993                 data->partitions[i]->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
994
995                 data->partitions[i]->dn = ldb_dn_new(data->partitions[i], module->ldb, base);
996                 if (!data->partitions[i]->dn) {
997                         ldb_asprintf_errstring(module->ldb, 
998                                                 "partition_init: invalid DN in partition record: %s", base);
999                         talloc_free(mem_ctx);
1000                         return LDB_ERR_CONSTRAINT_VIOLATION;
1001                 }
1002
1003                 data->partitions[i]->backend = samdb_relative_path(module->ldb, 
1004                                                                    data->partitions[i], 
1005                                                                    p);
1006                 if (!data->partitions[i]->backend) {
1007                         ldb_asprintf_errstring(module->ldb, 
1008                                                 "partition_init: unable to determine an relative path for partition: %s", base);
1009                         talloc_free(mem_ctx);                   
1010                 }
1011                 ret = ldb_connect_backend(module->ldb, data->partitions[i]->backend, NULL, &data->partitions[i]->module);
1012                 if (ret != LDB_SUCCESS) {
1013                         talloc_free(mem_ctx);
1014                         return ret;
1015                 }
1016         }
1017         data->partitions[i] = NULL;
1018
1019         /* sort these into order, most to least specific */
1020         qsort(data->partitions, partition_attributes->num_values,
1021               sizeof(*data->partitions), partition_sort_compare);
1022
1023         for (i=0; data->partitions[i]; i++) {
1024                 struct ldb_request *req;
1025                 req = talloc_zero(mem_ctx, struct ldb_request);
1026                 if (req == NULL) {
1027                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Out of memory!\n");
1028                         talloc_free(mem_ctx);
1029                         return LDB_ERR_OPERATIONS_ERROR;
1030                 }
1031                 
1032                 req->operation = LDB_REQ_REGISTER_PARTITION;
1033                 req->op.reg_partition.dn = data->partitions[i]->dn;
1034                 req->callback = ldb_op_default_callback;
1035
1036                 ldb_set_timeout(module->ldb, req, 0);
1037
1038                 req->handle = ldb_handle_new(req, module->ldb);
1039                 if (req->handle == NULL) {
1040                         return LDB_ERR_OPERATIONS_ERROR;
1041                 }
1042                 
1043                 ret = ldb_request(module->ldb, req);
1044                 if (ret == LDB_SUCCESS) {
1045                         ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1046                 }
1047                 if (ret != LDB_SUCCESS) {
1048                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Unable to register partition with rootdse!\n");
1049                         talloc_free(mem_ctx);
1050                         return LDB_ERR_OTHER;
1051                 }
1052                 talloc_free(req);
1053         }
1054
1055         replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
1056         if (!replicate_attributes) {
1057                 data->replicate = NULL;
1058         } else {
1059                 data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
1060                 if (!data->replicate) {
1061                         talloc_free(mem_ctx);
1062                         return LDB_ERR_OPERATIONS_ERROR;
1063                 }
1064
1065                 for (i=0; i < replicate_attributes->num_values; i++) {
1066                         data->replicate[i] = ldb_dn_from_ldb_val(data->replicate, module->ldb, &replicate_attributes->values[i]);
1067                         if (!ldb_dn_validate(data->replicate[i])) {
1068                                 ldb_asprintf_errstring(module->ldb, 
1069                                                         "partition_init: "
1070                                                         "invalid DN in partition replicate record: %s", 
1071                                                         replicate_attributes->values[i].data);
1072                                 talloc_free(mem_ctx);
1073                                 return LDB_ERR_CONSTRAINT_VIOLATION;
1074                         }
1075                 }
1076                 data->replicate[i] = NULL;
1077         }
1078
1079         /* Make the private data available to any searches the modules may trigger in initialisation */
1080         module->private_data = data;
1081         talloc_steal(module, data);
1082         
1083         modules_attributes = ldb_msg_find_element(msg, "modules");
1084         if (modules_attributes) {
1085                 for (i=0; i < modules_attributes->num_values; i++) {
1086                         struct ldb_dn *base_dn;
1087                         int partition_idx;
1088                         struct dsdb_control_current_partition *partition = NULL;
1089                         const char **modules = NULL;
1090
1091                         char *base = talloc_strdup(data->partitions, (char *)modules_attributes->values[i].data);
1092                         char *p = strchr(base, ':');
1093                         if (!p) {
1094                                 ldb_asprintf_errstring(module->ldb, 
1095                                                         "partition_init: "
1096                                                         "invalid form for partition module record (missing ':'): %s", base);
1097                                 talloc_free(mem_ctx);
1098                                 return LDB_ERR_CONSTRAINT_VIOLATION;
1099                         }
1100                         p[0] = '\0';
1101                         p++;
1102                         if (!p[0]) {
1103                                 ldb_asprintf_errstring(module->ldb, 
1104                                                         "partition_init: "
1105                                                         "invalid form for partition module record (missing backend database): %s", base);
1106                                 talloc_free(mem_ctx);
1107                                 return LDB_ERR_CONSTRAINT_VIOLATION;
1108                         }
1109
1110                         modules = ldb_modules_list_from_string(module->ldb, mem_ctx,
1111                                                                p);
1112                         
1113                         base_dn = ldb_dn_new(mem_ctx, module->ldb, base);
1114                         if (!ldb_dn_validate(base_dn)) {
1115                                 talloc_free(mem_ctx);
1116                                 return LDB_ERR_OPERATIONS_ERROR;
1117                         }
1118                         
1119                         for (partition_idx = 0; data->partitions[partition_idx]; partition_idx++) {
1120                                 if (ldb_dn_compare(data->partitions[partition_idx]->dn, base_dn) == 0) {
1121                                         partition = data->partitions[partition_idx];
1122                                         break;
1123                                 }
1124                         }
1125                         
1126                         if (!partition) {
1127                                 ldb_asprintf_errstring(module->ldb, 
1128                                                         "partition_init: "
1129                                                         "invalid form for partition module record (no such partition): %s", base);
1130                                 talloc_free(mem_ctx);
1131                                 return LDB_ERR_CONSTRAINT_VIOLATION;
1132                         }
1133                         
1134                         ret = ldb_load_modules_list(module->ldb, modules, partition->module, &partition->module);
1135                         if (ret != LDB_SUCCESS) {
1136                                 ldb_asprintf_errstring(module->ldb, 
1137                                                        "partition_init: "
1138                                                        "loading backend for %s failed: %s", 
1139                                                        base, ldb_errstring(module->ldb));
1140                                 talloc_free(mem_ctx);
1141                                 return ret;
1142                         }
1143                         ret = ldb_init_module_chain(module->ldb, partition->module);
1144                         if (ret != LDB_SUCCESS) {
1145                                 ldb_asprintf_errstring(module->ldb, 
1146                                                        "partition_init: "
1147                                                        "initialising backend for %s failed: %s", 
1148                                                        base, ldb_errstring(module->ldb));
1149                                 talloc_free(mem_ctx);
1150                                 return ret;
1151                         }
1152                 }
1153         }
1154
1155         talloc_free(mem_ctx);
1156         return ldb_next_init(module);
1157 }
1158
1159 _PUBLIC_ const struct ldb_module_ops ldb_partition_module_ops = {
1160         .name              = "partition",
1161         .init_context      = partition_init,
1162         .search            = partition_search,
1163         .add               = partition_add,
1164         .modify            = partition_modify,
1165         .del               = partition_delete,
1166         .rename            = partition_rename,
1167         .extended          = partition_extended,
1168         .sequence_number   = partition_sequence_number,
1169         .start_transaction = partition_start_trans,
1170         .end_transaction   = partition_end_trans,
1171         .del_transaction   = partition_del_trans,
1172 };