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