r23762: Fix DN renames over LDAP, and instrument the partition module. Add a
[samba.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 2 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, write to the Free Software
24    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27 /*
28  *  Name: ldb
29  *
30  *  Component: ldb partitions module
31  *
32  *  Description: Implement LDAP partitions
33  *
34  *  Author: Andrew Bartlett
35  *  Author: Stefan Metzmacher
36  */
37
38 #include "includes.h"
39 #include "ldb/include/ldb_includes.h"
40 #include "dsdb/samdb/samdb.h"
41
42 struct partition_private_data {
43         struct dsdb_control_current_partition **partitions;
44         struct ldb_dn **replicate;
45 };
46
47 struct partition_context {
48         struct ldb_module *module;
49         struct ldb_handle *handle;
50         struct ldb_request *orig_req;
51
52         struct ldb_request **down_req;
53         int num_requests;
54         int finished_requests;
55 };
56
57 static struct partition_context *partition_init_handle(struct ldb_request *req, struct ldb_module *module)
58 {
59         struct partition_context *ac;
60         struct ldb_handle *h;
61
62         h = talloc_zero(req, struct ldb_handle);
63         if (h == NULL) {
64                 ldb_set_errstring(module->ldb, "Out of Memory");
65                 return NULL;
66         }
67
68         h->module = module;
69
70         ac = talloc_zero(h, struct partition_context);
71         if (ac == NULL) {
72                 ldb_set_errstring(module->ldb, "Out of Memory");
73                 talloc_free(h);
74                 return NULL;
75         }
76
77         h->private_data = ac;
78
79         ac->module = module;
80         ac->handle = h;
81         ac->orig_req = req;
82
83         req->handle = h;
84
85         return ac;
86 }
87
88 static struct ldb_module *make_module_for_next_request(TALLOC_CTX *mem_ctx, 
89                                                        struct ldb_context *ldb,
90                                                        struct ldb_module *module)
91 {
92         struct ldb_module *current;
93         static const struct ldb_module_ops ops; /* zero */
94         current = talloc_zero(mem_ctx, struct ldb_module);
95         if (current == NULL) {
96                 return module;
97         }
98         
99         current->ldb = ldb;
100         current->ops = &ops;
101         current->prev = NULL;
102         current->next = module;
103         return current;
104 }
105
106 static struct dsdb_control_current_partition *find_partition(struct partition_private_data *data,
107                                                              struct ldb_dn *dn)
108 {
109         int i;
110
111         /* Look at base DN */
112         /* Figure out which partition it is under */
113         /* Skip the lot if 'data' isn't here yet (initialistion) */
114         for (i=0; data && data->partitions && data->partitions[i]; i++) {
115                 if (ldb_dn_compare_base(data->partitions[i]->dn, dn) == 0) {
116                         return data->partitions[i];
117                 }
118         }
119
120         return NULL;
121 };
122
123 /*
124   fire the caller's callback for every entry, but only send 'done' once.
125 */
126 static int partition_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
127 {
128         struct partition_context *ac;
129
130         if (!context || !ares) {
131                 ldb_set_errstring(ldb, "partition_search_callback: NULL Context or Result in 'search' callback");
132                 goto error;
133         }
134
135         ac = talloc_get_type(context, struct partition_context);
136
137         if (ares->type == LDB_REPLY_ENTRY) {
138                 return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
139         } else {
140                 ac->finished_requests++;
141                 if (ac->finished_requests == ac->num_requests) {
142                         return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
143                 } else {
144                         talloc_free(ares);
145                         return LDB_SUCCESS;
146                 }
147         }
148 error:
149         talloc_free(ares);
150         return LDB_ERR_OPERATIONS_ERROR;
151 }
152
153 /*
154   only fire the 'last' callback, and only for START-TLS for now 
155 */
156 static int partition_other_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
157 {
158         struct partition_context *ac;
159
160         if (!context) {
161                 ldb_set_errstring(ldb, "partition_other_callback: NULL Context in 'other' callback");
162                 goto error;
163         }
164
165         ac = talloc_get_type(context, struct partition_context);
166
167         if (!ac->orig_req->callback) {
168                 talloc_free(ares);
169                 return LDB_SUCCESS;
170         }
171
172         if (!ares 
173             || (ares->type == LDB_REPLY_EXTENDED 
174                 && strcmp(ares->response->oid, LDB_EXTENDED_START_TLS_OID))) {
175                 ac->finished_requests++;
176                 if (ac->finished_requests == ac->num_requests) {
177                         return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
178                 }
179                 talloc_free(ares);
180                 return LDB_SUCCESS;
181         }
182         ldb_set_errstring(ldb, "partition_other_callback: Unknown reply type, only supports START_TLS");
183 error:
184         talloc_free(ares);
185         return LDB_ERR_OPERATIONS_ERROR;
186 }
187
188
189 static int partition_send_request(struct partition_context *ac, struct ldb_control *remove_control, 
190                                   struct dsdb_control_current_partition *partition)
191 {
192         int ret;
193         struct ldb_module *backend;
194         struct ldb_request *req;
195         struct ldb_control **saved_controls;
196
197         if (partition) {
198                 backend = make_module_for_next_request(ac, ac->module->ldb, partition->module);
199         } else {
200                 backend = ac->module;
201         }
202
203         ac->down_req = talloc_realloc(ac, ac->down_req, 
204                                         struct ldb_request *, ac->num_requests + 1);
205         if (!ac->down_req) {
206                 ldb_oom(ac->module->ldb);
207                 return LDB_ERR_OPERATIONS_ERROR;
208         }
209         req = ac->down_req[ac->num_requests] = talloc(ac, struct ldb_request);
210         if (req == NULL) {
211                 ldb_oom(ac->module->ldb);
212                 return LDB_ERR_OPERATIONS_ERROR;
213         }
214         
215         *req = *ac->orig_req; /* copy the request */
216
217         if (req->controls) {
218                 req->controls
219                         = talloc_memdup(req,
220                                         ac->orig_req->controls, talloc_get_size(ac->orig_req->controls));
221                 if (req->controls == NULL) {
222                         ldb_oom(ac->module->ldb);
223                         return LDB_ERR_OPERATIONS_ERROR;
224                 }
225         }
226
227         if (req->operation == LDB_SEARCH) {
228                 /* If the search is for 'more' than this partition,
229                  * then change the basedn, so a remote LDAP server
230                  * doesn't object */
231                 if (partition) {
232                         if (ldb_dn_compare_base(partition->dn, req->op.search.base) != 0) {
233                                 req->op.search.base = partition->dn;
234                         }
235                 } else {
236                         req->op.search.base = NULL;
237                 }
238                 req->callback = partition_search_callback;
239                 req->context = ac;
240         } else {
241                 req->callback = partition_other_callback;
242                 req->context = ac;
243         }
244
245         /* Remove a control, so we don't confuse a backend server */
246         if (remove_control && !save_controls(remove_control, req, &saved_controls)) {
247                 ldb_oom(ac->module->ldb);
248                 return LDB_ERR_OPERATIONS_ERROR;
249         }
250         
251         if (partition) {
252                 ret = ldb_request_add_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID, false, partition);
253                 if (ret != LDB_SUCCESS) {
254                         return ret;
255                 }
256         }
257
258         /* Spray off search requests to all backends */
259         ret = ldb_next_request(backend, req);
260         if (ret != LDB_SUCCESS) {
261                 return ret;
262         }
263
264         ac->num_requests++;
265         return LDB_SUCCESS;
266 }
267
268 /* Send a request down to all the partitions */
269 static int partition_send_all(struct ldb_module *module, 
270                               struct partition_context *ac, 
271                               struct ldb_control *remove_control, 
272                               struct ldb_request *req) 
273 {
274         int i;
275         struct partition_private_data *data = talloc_get_type(module->private_data, 
276                                                               struct partition_private_data);
277         int ret = partition_send_request(ac, remove_control, NULL);
278         if (ret != LDB_SUCCESS) {
279                 return ret;
280         }
281         for (i=0; data && data->partitions && data->partitions[i]; i++) {
282                 ret = partition_send_request(ac, remove_control, data->partitions[i]);
283                 if (ret != LDB_SUCCESS) {
284                         return ret;
285                 }
286         }
287         return LDB_SUCCESS;
288 }
289
290 /* Figure out which backend a request needs to be aimed at.  Some
291  * requests must be replicated to all backends */
292 static int partition_replicate(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn) 
293 {
294         unsigned i;
295         int ret;
296         struct dsdb_control_current_partition *partition;
297         struct ldb_module *backend;
298         struct partition_private_data *data = talloc_get_type(module->private_data, 
299                                                               struct partition_private_data);
300         
301         if (req->operation != LDB_SEARCH) {
302                 /* Is this a special DN, we need to replicate to every backend? */
303                 for (i=0; data->replicate && data->replicate[i]; i++) {
304                         if (ldb_dn_compare(data->replicate[i], 
305                                            dn) == 0) {
306                                 struct partition_context *ac;
307                                 
308                                 ac = partition_init_handle(req, module);
309                                 if (!ac) {
310                                         return LDB_ERR_OPERATIONS_ERROR;
311                                 }
312                                 
313                                 return partition_send_all(module, ac, NULL, req);
314                         }
315                 }
316         }
317
318         /* Otherwise, we need to find the partition to fire it to */
319
320         /* Find partition */
321         partition = find_partition(data, dn);
322         if (!partition) {
323                 /*
324                  * if we haven't found a matching partition
325                  * pass the request to the main ldb
326                  *
327                  * TODO: we should maybe return an error here
328                  *       if it's not a special dn
329                  */
330                 return ldb_next_request(module, req);
331         }
332
333         backend = make_module_for_next_request(req, module->ldb, partition->module);
334         if (!backend) {
335                 return LDB_ERR_OPERATIONS_ERROR;
336         }
337
338         ret = ldb_request_add_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID, false, partition);
339         if (ret != LDB_SUCCESS) {
340                 return ret;
341         }
342
343         /* issue request */
344         return ldb_next_request(backend, req);
345 }
346
347 /* search */
348 static int partition_search(struct ldb_module *module, struct ldb_request *req)
349 {
350         /* Find backend */
351         struct partition_private_data *data = talloc_get_type(module->private_data, 
352                                                               struct partition_private_data);
353         /* issue request */
354
355         /* (later) consider if we should be searching multiple
356          * partitions (for 'invisible' partition behaviour */
357         struct ldb_control *search_control = ldb_request_get_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID);
358         
359         struct ldb_search_options_control *search_options = NULL;
360         if (search_control) {
361                 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
362         }
363
364         if (search_options && (search_options->search_options & LDB_SEARCH_OPTION_PHANTOM_ROOT)) {
365                 int ret, i;
366                 struct partition_context *ac;
367                 struct ldb_control *remove_control = NULL;
368                 if ((search_options->search_options & ~LDB_SEARCH_OPTION_PHANTOM_ROOT) == 0) {
369                         /* We have processed this flag, so we are done with this control now */
370                         remove_control = search_control;
371                 }
372                 ac = partition_init_handle(req, module);
373                 if (!ac) {
374                         return LDB_ERR_OPERATIONS_ERROR;
375                 }
376
377                 /* Search from the base DN */
378                 if (!req->op.search.base || ldb_dn_is_null(req->op.search.base)) {
379                         return partition_send_all(module, ac, remove_control, req);
380                 }
381                 for (i=0; data && data->partitions && data->partitions[i]; i++) {
382                         /* Find all partitions under the search base */
383                         if (ldb_dn_compare_base(req->op.search.base, data->partitions[i]->dn) == 0) {
384                                 ret = partition_send_request(ac, remove_control, data->partitions[i]);
385                                 if (ret != LDB_SUCCESS) {
386                                         return ret;
387                                 }
388                         }
389                 }
390
391                 /* Perhaps we didn't match any partitions.  Try the main partition, only */
392                 if (ac->num_requests == 0) {
393                         talloc_free(ac);
394                         return ldb_next_request(module, req);
395                 }
396                 
397                 return LDB_SUCCESS;
398         } else {
399                 /* Handle this like all other requests */
400                 return partition_replicate(module, req, req->op.search.base);
401         }
402 }
403
404 /* add */
405 static int partition_add(struct ldb_module *module, struct ldb_request *req)
406 {
407         return partition_replicate(module, req, req->op.add.message->dn);
408 }
409
410 /* modify */
411 static int partition_modify(struct ldb_module *module, struct ldb_request *req)
412 {
413         return partition_replicate(module, req, req->op.mod.message->dn);
414 }
415
416 /* delete */
417 static int partition_delete(struct ldb_module *module, struct ldb_request *req)
418 {
419         return partition_replicate(module, req, req->op.del.dn);
420 }
421
422 /* rename */
423 static int partition_rename(struct ldb_module *module, struct ldb_request *req)
424 {
425         /* Find backend */
426         struct dsdb_control_current_partition *backend, *backend2;
427         
428         struct partition_private_data *data = talloc_get_type(module->private_data, 
429                                                               struct partition_private_data);
430
431         /* Skip the lot if 'data' isn't here yet (initialistion) */
432         if (!data) {
433                 return LDB_ERR_OPERATIONS_ERROR;
434         }
435
436         backend = find_partition(data, req->op.rename.olddn);
437         backend2 = find_partition(data, req->op.rename.newdn);
438
439         if ((backend && !backend2) || (!backend && backend2)) {
440                 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
441         }
442
443         if (backend != backend2) {
444                 ldb_asprintf_errstring(module->ldb, 
445                                        "Cannot rename from %s in %s to %s in %s: %s",
446                                        ldb_dn_get_linearized(req->op.rename.olddn),
447                                        ldb_dn_get_linearized(backend->dn),
448                                        ldb_dn_get_linearized(req->op.rename.newdn),
449                                        ldb_dn_get_linearized(backend2->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 = void1;
699         struct dsdb_control_current_partition **pp2 = void2;
700         struct dsdb_control_current_partition *partition1 = talloc_get_type(*pp1,
701                                                             struct dsdb_control_current_partition);
702         struct dsdb_control_current_partition *partition2 = talloc_get_type(*pp2,
703                                                             struct dsdb_control_current_partition);
704
705         return ldb_dn_compare(partition1->dn, partition2->dn);
706 }
707
708 static const char *relative_path(struct ldb_module *module, 
709                                  TALLOC_CTX *mem_ctx, 
710                                  const char *name) 
711 {
712         const char *base_url = ldb_get_opaque(module->ldb, "ldb_url");
713         char *path, *p, *full_name;
714         if (name == NULL) {
715                 return NULL;
716         }
717         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
718                 return talloc_strdup(mem_ctx, name);
719         }
720         path = talloc_strdup(mem_ctx, base_url);
721         if (path == NULL) {
722                 return NULL;
723         }
724         if ( (p = strrchr(path, '/')) != NULL) {
725                 p[0] = '\0';
726         } else {
727                 talloc_free(path);
728                 return NULL;
729         }
730         full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
731         talloc_free(path);
732         return full_name;
733 }
734
735 static int partition_init(struct ldb_module *module)
736 {
737         int ret, i;
738         TALLOC_CTX *mem_ctx = talloc_new(module);
739         static const char *attrs[] = { "partition", "replicateEntries", "modules", NULL };
740         struct ldb_result *res;
741         struct ldb_message *msg;
742         struct ldb_message_element *partition_attributes;
743         struct ldb_message_element *replicate_attributes;
744         struct ldb_message_element *modules_attributes;
745
746         struct partition_private_data *data;
747
748         if (!mem_ctx) {
749                 return LDB_ERR_OPERATIONS_ERROR;
750         }
751
752         data = talloc(mem_ctx, struct partition_private_data);
753         if (data == NULL) {
754                 return LDB_ERR_OPERATIONS_ERROR;
755         }
756
757         ret = ldb_search(module->ldb, ldb_dn_new(mem_ctx, module->ldb, "@PARTITION"),
758                          LDB_SCOPE_BASE,
759                          NULL, attrs,
760                          &res);
761         if (ret != LDB_SUCCESS) {
762                 talloc_free(mem_ctx);
763                 return ret;
764         }
765         talloc_steal(mem_ctx, res);
766         if (res->count == 0) {
767                 talloc_free(mem_ctx);
768                 return ldb_next_init(module);
769         }
770
771         if (res->count > 1) {
772                 talloc_free(mem_ctx);
773                 return LDB_ERR_CONSTRAINT_VIOLATION;
774         }
775
776         msg = res->msgs[0];
777
778         partition_attributes = ldb_msg_find_element(msg, "partition");
779         if (!partition_attributes) {
780                 ldb_set_errstring(module->ldb, "partition_init: no partitions specified");
781                 talloc_free(mem_ctx);
782                 return LDB_ERR_CONSTRAINT_VIOLATION;
783         }
784         data->partitions = talloc_array(data, struct dsdb_control_current_partition *, partition_attributes->num_values + 1);
785         if (!data->partitions) {
786                 talloc_free(mem_ctx);
787                 return LDB_ERR_OPERATIONS_ERROR;
788         }
789         for (i=0; i < partition_attributes->num_values; i++) {
790                 char *base = talloc_strdup(data->partitions, (char *)partition_attributes->values[i].data);
791                 char *p = strchr(base, ':');
792                 if (!p) {
793                         ldb_asprintf_errstring(module->ldb, 
794                                                 "partition_init: "
795                                                 "invalid form for partition record (missing ':'): %s", base);
796                         talloc_free(mem_ctx);
797                         return LDB_ERR_CONSTRAINT_VIOLATION;
798                 }
799                 p[0] = '\0';
800                 p++;
801                 if (!p[0]) {
802                         ldb_asprintf_errstring(module->ldb, 
803                                                 "partition_init: "
804                                                 "invalid form for partition record (missing backend database): %s", base);
805                         talloc_free(mem_ctx);
806                         return LDB_ERR_CONSTRAINT_VIOLATION;
807                 }
808                 data->partitions[i] = talloc(data->partitions, struct dsdb_control_current_partition);
809                 if (!data->partitions[i]) {
810                         talloc_free(mem_ctx);
811                         return LDB_ERR_OPERATIONS_ERROR;
812                 }
813                 data->partitions[i]->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
814
815                 data->partitions[i]->dn = ldb_dn_new(data->partitions[i], module->ldb, base);
816                 if (!data->partitions[i]->dn) {
817                         ldb_asprintf_errstring(module->ldb, 
818                                                 "partition_init: invalid DN in partition record: %s", base);
819                         talloc_free(mem_ctx);
820                         return LDB_ERR_CONSTRAINT_VIOLATION;
821                 }
822
823                 data->partitions[i]->backend = relative_path(module, 
824                                                              data->partitions[i], 
825                                                              p);
826                 ret = ldb_connect_backend(module->ldb, data->partitions[i]->backend, NULL, &data->partitions[i]->module);
827                 if (ret != LDB_SUCCESS) {
828                         talloc_free(mem_ctx);
829                         return ret;
830                 }
831         }
832         data->partitions[i] = NULL;
833
834         /* sort these into order, most to least specific */
835         ldb_qsort(data->partitions, partition_attributes->num_values, sizeof(*data->partitions), 
836                   module->ldb, sort_compare);
837
838         for (i=0; data->partitions[i]; i++) {
839                 struct ldb_request *req;
840                 req = talloc_zero(mem_ctx, struct ldb_request);
841                 if (req == NULL) {
842                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Out of memory!\n");
843                         talloc_free(mem_ctx);
844                         return LDB_ERR_OPERATIONS_ERROR;
845                 }
846                 
847                 req->operation = LDB_REQ_REGISTER_PARTITION;
848                 req->op.reg_partition.dn = data->partitions[i]->dn;
849                 
850                 ret = ldb_request(module->ldb, req);
851                 if (ret != LDB_SUCCESS) {
852                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Unable to register partition with rootdse!\n");
853                         talloc_free(mem_ctx);
854                         return LDB_ERR_OTHER;
855                 }
856                 talloc_free(req);
857         }
858
859         replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
860         if (!replicate_attributes) {
861                 data->replicate = NULL;
862         } else {
863                 data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
864                 if (!data->replicate) {
865                         talloc_free(mem_ctx);
866                         return LDB_ERR_OPERATIONS_ERROR;
867                 }
868                 
869                 for (i=0; i < replicate_attributes->num_values; i++) {
870                         data->replicate[i] = ldb_dn_new(data->replicate, module->ldb, (const char *)replicate_attributes->values[i].data);
871                         if (!ldb_dn_validate(data->replicate[i])) {
872                                 ldb_asprintf_errstring(module->ldb, 
873                                                         "partition_init: "
874                                                         "invalid DN in partition replicate record: %s", 
875                                                         replicate_attributes->values[i].data);
876                                 talloc_free(mem_ctx);
877                                 return LDB_ERR_CONSTRAINT_VIOLATION;
878                         }
879                 }
880                 data->replicate[i] = NULL;
881         }
882
883         /* Make the private data available to any searches the modules may trigger in initialisation */
884         module->private_data = data;
885         talloc_steal(module, data);
886         
887         modules_attributes = ldb_msg_find_element(msg, "modules");
888         if (modules_attributes) {
889                 for (i=0; i < modules_attributes->num_values; i++) {
890                         struct ldb_dn *base_dn;
891                         int partition_idx;
892                         struct dsdb_control_current_partition *partition = NULL;
893                         const char **modules = NULL;
894
895                         char *base = talloc_strdup(data->partitions, (char *)modules_attributes->values[i].data);
896                         char *p = strchr(base, ':');
897                         if (!p) {
898                                 ldb_asprintf_errstring(module->ldb, 
899                                                         "partition_init: "
900                                                         "invalid form for partition module record (missing ':'): %s", base);
901                                 talloc_free(mem_ctx);
902                                 return LDB_ERR_CONSTRAINT_VIOLATION;
903                         }
904                         p[0] = '\0';
905                         p++;
906                         if (!p[0]) {
907                                 ldb_asprintf_errstring(module->ldb, 
908                                                         "partition_init: "
909                                                         "invalid form for partition module record (missing backend database): %s", base);
910                                 talloc_free(mem_ctx);
911                                 return LDB_ERR_CONSTRAINT_VIOLATION;
912                         }
913
914                         modules = ldb_modules_list_from_string(module->ldb, mem_ctx,
915                                                                p);
916                         
917                         base_dn = ldb_dn_new(mem_ctx, module->ldb, base);
918                         if (!ldb_dn_validate(base_dn)) {
919                                 talloc_free(mem_ctx);
920                                 return LDB_ERR_OPERATIONS_ERROR;
921                         }
922                         
923                         for (partition_idx = 0; data->partitions[partition_idx]; partition_idx++) {
924                                 if (ldb_dn_compare(data->partitions[partition_idx]->dn, base_dn) == 0) {
925                                         partition = data->partitions[partition_idx];
926                                         break;
927                                 }
928                         }
929                         
930                         if (!partition) {
931                                 ldb_asprintf_errstring(module->ldb, 
932                                                         "partition_init: "
933                                                         "invalid form for partition module record (no such partition): %s", base);
934                                 talloc_free(mem_ctx);
935                                 return LDB_ERR_CONSTRAINT_VIOLATION;
936                         }
937                         
938                         ret = ldb_load_modules_list(module->ldb, modules, partition->module, &partition->module);
939                         if (ret != LDB_SUCCESS) {
940                                 ldb_asprintf_errstring(module->ldb, 
941                                                        "partition_init: "
942                                                        "loading backend for %s failed: %s", 
943                                                        base, ldb_errstring(module->ldb));
944                                 talloc_free(mem_ctx);
945                                 return ret;
946                         }
947                         ret = ldb_init_module_chain(module->ldb, partition->module);
948                         if (ret != LDB_SUCCESS) {
949                                 ldb_asprintf_errstring(module->ldb, 
950                                                        "partition_init: "
951                                                        "initialising backend for %s failed: %s", 
952                                                        base, ldb_errstring(module->ldb));
953                                 talloc_free(mem_ctx);
954                                 return ret;
955                         }
956                 }
957         }
958
959         talloc_free(mem_ctx);
960         return ldb_next_init(module);
961 }
962
963 static int partition_wait_none(struct ldb_handle *handle) {
964         struct partition_context *ac;
965         int ret;
966         int i;
967     
968         if (!handle || !handle->private_data) {
969                 return LDB_ERR_OPERATIONS_ERROR;
970         }
971
972         if (handle->state == LDB_ASYNC_DONE) {
973                 return handle->status;
974         }
975
976         handle->state = LDB_ASYNC_PENDING;
977         handle->status = LDB_SUCCESS;
978
979         ac = talloc_get_type(handle->private_data, struct partition_context);
980
981         for (i=0; i < ac->num_requests; i++) {
982                 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
983                 
984                 if (ret != LDB_SUCCESS) {
985                         handle->status = ret;
986                         goto done;
987                 }
988                 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
989                         handle->status = ac->down_req[i]->handle->status;
990                         goto done;
991                 }
992                 
993                 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
994                         return LDB_SUCCESS;
995                 }
996         }
997
998         ret = LDB_SUCCESS;
999
1000 done:
1001         handle->state = LDB_ASYNC_DONE;
1002         return ret;
1003 }
1004
1005
1006 static int partition_wait_all(struct ldb_handle *handle) {
1007
1008         int ret;
1009
1010         while (handle->state != LDB_ASYNC_DONE) {
1011                 ret = partition_wait_none(handle);
1012                 if (ret != LDB_SUCCESS) {
1013                         return ret;
1014                 }
1015         }
1016
1017         return handle->status;
1018 }
1019
1020 static int partition_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1021 {
1022         if (type == LDB_WAIT_ALL) {
1023                 return partition_wait_all(handle);
1024         } else {
1025                 return partition_wait_none(handle);
1026         }
1027 }
1028
1029 static const struct ldb_module_ops partition_ops = {
1030         .name              = "partition",
1031         .init_context      = partition_init,
1032         .search            = partition_search,
1033         .add               = partition_add,
1034         .modify            = partition_modify,
1035         .del               = partition_delete,
1036         .rename            = partition_rename,
1037         .extended          = partition_extended,
1038         .sequence_number   = partition_sequence_number,
1039         .start_transaction = partition_start_trans,
1040         .end_transaction   = partition_end_trans,
1041         .del_transaction   = partition_del_trans,
1042         .wait              = partition_wait
1043 };
1044
1045 int ldb_partition_init(void)
1046 {
1047         return ldb_register_module(&partition_ops);
1048 }