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