d3400f8e085dfcac4e1f4bdcb56b0609474e56b0
[abartlet/samba.git/.git] / source4 / dsdb / samdb / ldb_modules / partition_init.c
1 /* 
2    Partitions ldb module
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb partitions module
25  *
26  *  Description: Implement LDAP partitions
27  *
28  *  Author: Andrew Bartlett
29  *  Author: Stefan Metzmacher
30  */
31
32 #include "dsdb/samdb/ldb_modules/partition.h"
33 static int partition_sort_compare(const void *v1, const void *v2)
34 {
35         const struct dsdb_partition *p1;
36         const struct dsdb_partition *p2;
37
38         p1 = *((struct dsdb_partition * const*)v1);
39         p2 = *((struct dsdb_partition * const*)v2);
40
41         return ldb_dn_compare(p1->ctrl->dn, p2->ctrl->dn);
42 }
43
44 /* Load the list of DNs that we must replicate to all partitions */
45 static int partition_load_replicate_dns(struct ldb_context *ldb, struct partition_private_data *data, struct ldb_message *msg) 
46 {
47         struct ldb_message_element *replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
48
49         talloc_free(data->replicate);
50         if (!replicate_attributes) {
51                 data->replicate = NULL;
52         } else {
53                 int i;
54                 data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
55                 if (!data->replicate) {
56                         return LDB_ERR_OPERATIONS_ERROR;
57                 }
58
59                 for (i=0; i < replicate_attributes->num_values; i++) {
60                         data->replicate[i] = ldb_dn_from_ldb_val(data->replicate, ldb, &replicate_attributes->values[i]);
61                         if (!ldb_dn_validate(data->replicate[i])) {
62                                 ldb_asprintf_errstring(ldb,
63                                                         "partition_init: "
64                                                         "invalid DN in partition replicate record: %s", 
65                                                         replicate_attributes->values[i].data);
66                                 return LDB_ERR_CONSTRAINT_VIOLATION;
67                         }
68                 }
69                 data->replicate[i] = NULL;
70         }
71         return LDB_SUCCESS;
72 }
73
74 /* Load the list of modules for the partitions */
75 static int partition_load_modules(struct ldb_context *ldb, 
76                                   struct partition_private_data *data, struct ldb_message *msg) 
77 {
78         int i;
79         struct ldb_message_element *modules_attributes = ldb_msg_find_element(msg, "modules");
80         talloc_free(data->modules);
81         if (!modules_attributes) {
82                 return LDB_SUCCESS;
83         }
84         
85         data->modules = talloc_array(data, struct partition_module *, modules_attributes->num_values + 1);
86         if (!data->modules) {
87                 ldb_oom(ldb);
88                 return LDB_ERR_OPERATIONS_ERROR;
89         }
90         
91         for (i=0; i < modules_attributes->num_values; i++) {
92                 char *p;
93                 DATA_BLOB dn_blob;
94                 data->modules[i] = talloc(data->modules, struct partition_module);
95                 if (!data->modules[i]) {
96                         ldb_oom(ldb);
97                         return LDB_ERR_OPERATIONS_ERROR;
98                 }
99
100                 dn_blob = modules_attributes->values[i];
101
102                 p = strchr((const char *)dn_blob.data, ':');
103                 if (!p) {
104                         ldb_asprintf_errstring(ldb, 
105                                                "partition_load_modules: "
106                                                "invalid form for partition module record (missing ':'): %s", (const char *)dn_blob.data);
107                         return LDB_ERR_CONSTRAINT_VIOLATION;
108                 }
109                 /* Now trim off the filename */
110                 dn_blob.length = ((uint8_t *)p - dn_blob.data);
111
112                 p++;
113                 data->modules[i]->modules = ldb_modules_list_from_string(ldb, data->modules[i],
114                                                                          p);
115                 
116                 if (dn_blob.length == 1 && dn_blob.data[0] == '*') {
117                         data->modules[i]->dn = NULL;
118                 } else {
119                         data->modules[i]->dn = ldb_dn_from_ldb_val(data->modules[i], ldb, &dn_blob);
120                         if (!data->modules[i]->dn || !ldb_dn_validate(data->modules[i]->dn)) {
121                                 return LDB_ERR_OPERATIONS_ERROR;
122                         }
123                 }
124         }
125         data->modules[i] = NULL;
126         return LDB_SUCCESS;
127 }
128
129 static int partition_reload_metadata(struct ldb_module *module, struct partition_private_data *data, TALLOC_CTX *mem_ctx, struct ldb_message **_msg) 
130 {
131         int ret;
132         struct ldb_message *msg;
133         struct ldb_result *res;
134         struct ldb_context *ldb = ldb_module_get_ctx(module);
135         const char *attrs[] = { "partition", "replicateEntries", "modules", NULL };
136         /* perform search for @PARTITION, looking for module, replicateEntries and ldapBackend */
137         ret = dsdb_module_search_dn(module, mem_ctx, &res, 
138                                     ldb_dn_new(mem_ctx, ldb, DSDB_PARTITION_DN),
139                                     attrs);
140         if (ret != LDB_SUCCESS) {
141                 return ret;
142         }
143
144         msg = res->msgs[0];
145
146         ret = partition_load_replicate_dns(ldb, data, msg);
147         if (ret != LDB_SUCCESS) {
148                 return ret;
149         }
150
151         ret = partition_load_modules(ldb, data, msg);                   
152         if (ret != LDB_SUCCESS) {
153                 return ret;
154         }
155
156         data->ldapBackend = talloc_steal(data, ldb_msg_find_attr_as_string(msg, "ldapBackend", NULL));
157         if (_msg) {
158                 *_msg = msg;
159         } else {
160                 talloc_free(msg);
161         }
162
163         return LDB_SUCCESS;
164 }
165
166 static const char **find_modules_for_dn(struct partition_private_data *data, struct ldb_dn *dn) 
167 {
168         int i;
169         struct partition_module *default_mod = NULL;
170         for (i=0; data->modules && data->modules[i]; i++) {
171                 if (!data->modules[i]->dn) {
172                         default_mod = data->modules[i];
173                 } else if (ldb_dn_compare(dn, data->modules[i]->dn) == 0) {
174                         return data->modules[i]->modules;
175                 }
176         }
177         if (default_mod) {
178                 return default_mod->modules;
179         } else {
180                 return NULL;
181         }
182 }
183
184 static int new_partition_from_dn(struct ldb_context *ldb, struct partition_private_data *data, 
185                                  TALLOC_CTX *mem_ctx, 
186                                  struct ldb_dn *dn, const char *filename_base,
187                                  struct dsdb_partition **partition) {
188         const char *backend_url;
189         struct dsdb_control_current_partition *ctrl;
190         struct ldb_module *backend_module;
191         struct ldb_module *module_chain;
192         const char **modules;
193         int ret;
194
195         (*partition) = talloc(mem_ctx, struct dsdb_partition);
196         if (!*partition) {
197                 return LDB_ERR_OPERATIONS_ERROR;
198         }
199
200         (*partition)->ctrl = ctrl = talloc((*partition), struct dsdb_control_current_partition);
201         if (!ctrl) {
202                 talloc_free(*partition);
203                 ldb_oom(ldb);
204                 return LDB_ERR_OPERATIONS_ERROR;
205         }
206
207         /* See if an LDAP backend has been specified */
208         if (data->ldapBackend) {
209                 backend_url = data->ldapBackend;
210         } else {
211
212                 /* the backend LDB is the DN (base64 encoded if not 'plain') followed by .ldb */
213                 const char *p;
214                 char *backend_path;
215                 char *base64_dn = NULL;
216                 for (p = filename_base; *p; p++) {
217                         /* We have such a strict check because I don't want shell metacharacters in the file name, nor ../ */
218                         if (!(isalnum(*p) || *p == ' ' || *p == '=' || *p == ',')) {
219                                 break;
220                         }
221                 }
222                 if (*p) {
223                         filename_base = base64_dn = ldb_base64_encode(data, filename_base, strlen(filename_base));
224                 }
225                 
226                 backend_path = samdb_relative_path(ldb, 
227                                                    *partition, 
228                                                    filename_base);
229                 if (base64_dn) {
230                         talloc_free(base64_dn);
231                 }
232                 if (!backend_path) {
233                         ldb_asprintf_errstring(ldb, 
234                                                "partition_init: unable to determine an relative path for partition: %s", filename_base);
235                         talloc_free(*partition);
236                         return LDB_ERR_OPERATIONS_ERROR;                
237                 }
238                 backend_url = talloc_asprintf(*partition, "tdb://%s.ldb", 
239                                               backend_path); 
240                 talloc_free(backend_path);
241                 if (!backend_url) {
242                         ldb_oom(ldb);
243                         talloc_free(*partition);
244                         return LDB_ERR_OPERATIONS_ERROR;                
245                 }
246         }
247
248         (*partition)->backend_url = backend_url;
249         ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
250         ctrl->dn = talloc_steal(ctrl, dn);
251         
252         ret = ldb_connect_backend(ldb, backend_url, NULL, &backend_module);
253         if (ret != LDB_SUCCESS) {
254                 return ret;
255         }
256         talloc_steal((*partition), backend_module);
257
258         modules = find_modules_for_dn(data, dn);
259
260         if (!modules) {
261                 DEBUG(0, ("Unable to load partition modules for new DN %s, perhaps you need to reprovision?  See partition-upgrade.txt for instructions\n", ldb_dn_get_linearized(dn)));
262                 talloc_free(*partition);
263                 return LDB_ERR_CONSTRAINT_VIOLATION;
264         }
265         ret = ldb_load_modules_list(ldb, modules, backend_module, &module_chain);
266         if (ret != LDB_SUCCESS) {
267                 ldb_asprintf_errstring(ldb, 
268                                        "partition_init: "
269                                        "loading backend for %s failed: %s", 
270                                        ldb_dn_get_linearized(dn), ldb_errstring(ldb));
271                 talloc_free(*partition);
272                 return ret;
273         }
274         ret = ldb_init_module_chain(ldb, module_chain);
275         if (ret != LDB_SUCCESS) {
276                 ldb_asprintf_errstring(ldb,
277                                        "partition_init: "
278                                        "initialising backend for %s failed: %s", 
279                                        ldb_dn_get_linearized(dn), ldb_errstring(ldb));
280                 talloc_free(*partition);
281                 return ret;
282         }
283
284         /* This weirdness allows us to use ldb_next_request() in partition.c */
285         (*partition)->module = ldb_module_new(*partition, ldb, "partition_next", NULL);
286         if (!(*partition)->module) {
287                 ldb_oom(ldb);
288                 talloc_free(*partition);
289                 return LDB_ERR_OPERATIONS_ERROR;
290         }
291         (*partition)->module->next = talloc_steal((*partition)->module, module_chain);
292
293         /* if we were in a transaction then we need to start a
294            transaction on this new partition, otherwise we'll get a
295            transaction mismatch when we end the transaction */
296         if (data->in_transaction) {
297                 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
298                         ldb_debug(ldb, LDB_DEBUG_TRACE, "partition_start_trans() -> %s (new partition)", 
299                                   ldb_dn_get_linearized((*partition)->ctrl->dn));
300                 }
301                 ret = ldb_next_start_trans((*partition)->module);
302         }
303
304         return ret;
305 }
306
307 /* Tell the rootDSE about the new partition */
308 static int partition_register(struct ldb_context *ldb, struct dsdb_control_current_partition *ctrl) 
309 {
310         struct ldb_request *req;
311         int ret;
312
313         req = talloc_zero(NULL, struct ldb_request);
314         if (req == NULL) {
315                 ldb_oom(ldb);
316                 return LDB_ERR_OPERATIONS_ERROR;
317         }
318                 
319         req->operation = LDB_REQ_REGISTER_PARTITION;
320         req->op.reg_partition.dn = ctrl->dn;
321         req->callback = ldb_op_default_callback;
322
323         ldb_set_timeout(ldb, req, 0);
324         
325         req->handle = ldb_handle_new(req, ldb);
326         if (req->handle == NULL) {
327                 talloc_free(req);
328                 return LDB_ERR_OPERATIONS_ERROR;
329         }
330         
331         ret = ldb_request(ldb, req);
332         if (ret == LDB_SUCCESS) {
333                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
334         }
335         if (ret != LDB_SUCCESS) {
336                 ldb_debug(ldb, LDB_DEBUG_ERROR, "partition: Unable to register partition with rootdse!\n");
337                 talloc_free(req);
338                 return LDB_ERR_OTHER;
339         }
340         talloc_free(req);
341
342         return LDB_SUCCESS;
343 }
344
345 /* Add a newly found partition to the global data */
346 static int add_partition_to_data(struct ldb_context *ldb, struct partition_private_data *data,
347                                  struct dsdb_partition *partition)
348 {
349         int i, ret;
350         /* Count the partitions */
351         for (i=0; data->partitions && data->partitions[i]; i++) { /* noop */};
352         
353         /* Add partition to list of partitions */
354         data->partitions = talloc_realloc(data, data->partitions, struct dsdb_partition *, i + 2);
355         if (!data->partitions) {
356                 ldb_oom(ldb);
357                 return LDB_ERR_OPERATIONS_ERROR;
358         }
359         data->partitions[i] = talloc_steal(data->partitions, partition);
360         data->partitions[i+1] = NULL;
361         
362         /* Sort again (should use binary insert) */
363         qsort(data->partitions, i+1,
364               sizeof(*data->partitions), partition_sort_compare);
365         
366         ret = partition_register(ldb, partition->ctrl);
367         if (ret != LDB_SUCCESS) {
368                 return ret;
369         }
370         return LDB_SUCCESS;
371 }
372
373 int partition_reload_if_required(struct ldb_module *module, 
374                                  struct partition_private_data *data)
375 {
376         uint64_t seq;
377         int ret, i;
378         struct ldb_context *ldb = ldb_module_get_ctx(module);
379         struct ldb_message *msg;
380         struct ldb_message_element *partition_attributes;
381         TALLOC_CTX *mem_ctx;
382
383         if (!data) {
384                 /* Not initilised yet */
385                 return LDB_SUCCESS;
386         }
387
388         mem_ctx = talloc_new(data);
389         if (!mem_ctx) {
390                 ldb_oom(ldb);
391                 return LDB_ERR_OPERATIONS_ERROR;
392         }
393
394         ret = partition_primary_sequence_number(module, mem_ctx, LDB_SEQ_HIGHEST_SEQ, &seq);
395         if (ret != LDB_SUCCESS) {
396                 talloc_free(mem_ctx);
397                 return ret;
398         }
399         if (seq == data->metadata_seq) {
400                 talloc_free(mem_ctx);
401                 return LDB_SUCCESS;
402         }
403
404         ret = partition_reload_metadata(module, data, mem_ctx, &msg);
405         if (ret != LDB_SUCCESS) {
406                 talloc_free(mem_ctx);
407                 return ret;
408         }
409
410         data->metadata_seq = seq;
411
412         partition_attributes = ldb_msg_find_element(msg, "partition");
413
414         for (i=0; partition_attributes && i < partition_attributes->num_values; i++) {
415                 int j;
416                 bool new_partition = true;
417                 const char *filename_base = NULL;
418                 DATA_BLOB dn_blob;
419                 struct ldb_dn *dn;
420                 struct dsdb_partition *partition;
421                 struct ldb_result *dn_res;
422                 const char *no_attrs[] = { NULL };
423
424                 for (j=0; data->partitions && data->partitions[j]; j++) {
425                         DATA_BLOB casefold = data_blob_string_const(ldb_dn_get_casefold(data->partitions[j]->ctrl->dn));
426                         if (data_blob_cmp(&casefold, &partition_attributes->values[i]) == 0) {
427                                 new_partition = false;
428                                 break;
429                         }
430                 }
431                 if (new_partition == false) {
432                         continue;
433                 }
434
435                 dn_blob = partition_attributes->values[i];
436                 
437                 if (dn_blob.length > 4 && 
438                     (strncmp((const char *)&dn_blob.data[dn_blob.length-4], ".ldb", 4) == 0)) {
439
440                         /* Look for DN:filename.ldb */
441                         char *p = strchr((const char *)dn_blob.data, ':');
442                         if (!p) {
443                                 ldb_asprintf_errstring(ldb, 
444                                                        "partition_init: invalid DN in attempting to parse old-style partition record: %s", (const char *)dn_blob.data);
445                                 talloc_free(mem_ctx);
446                                 return LDB_ERR_CONSTRAINT_VIOLATION;
447                         }
448                         filename_base = p+1;
449                         
450                         /* Trim off the .ldb */
451                         dn_blob.data[dn_blob.length-4] = '\0';
452                         
453                         /* Now trim off the filename */
454                         dn_blob.length = ((uint8_t *)p - dn_blob.data);
455                 }
456
457                 dn = ldb_dn_from_ldb_val(mem_ctx, ldb, &dn_blob);
458                 if (!dn) {
459                         ldb_asprintf_errstring(ldb, 
460                                                "partition_init: invalid DN in partition record: %s", (const char *)dn_blob.data);
461                         talloc_free(mem_ctx);
462                         return LDB_ERR_CONSTRAINT_VIOLATION;
463                 }
464                 
465                 if (!filename_base) {
466                         filename_base = ldb_dn_get_linearized(dn);
467                 }
468                         
469                 /* We call ldb_dn_get_linearized() because the DN in
470                  * partition_attributes is already casefolded
471                  * correctly.  We don't want to mess that up as the
472                  * schema isn't loaded yet */
473                 ret = new_partition_from_dn(ldb, data, data->partitions, dn, 
474                                             filename_base,
475                                             &partition);
476                 if (ret != LDB_SUCCESS) {
477                         talloc_free(mem_ctx);
478                         return ret;
479                 }
480
481                 /* Get the 'correct' case of the partition DNs from the database */
482                 ret = dsdb_module_search_dn(partition->module, data, &dn_res, 
483                                             dn, no_attrs);
484                 if (ret == LDB_SUCCESS) {
485                         talloc_free(partition->ctrl->dn);
486                         partition->ctrl->dn = talloc_steal(partition->ctrl, dn_res->msgs[0]->dn);
487                         talloc_free(dn_res);
488                 } else if (ret != LDB_ERR_NO_SUCH_OBJECT) {
489                         ldb_asprintf_errstring(ldb,
490                                                "Failed to search for partition base %s in new partition at %s: %s", 
491                                                ldb_dn_get_linearized(dn), 
492                                                partition->backend_url, 
493                                                ldb_errstring(ldb));
494                         talloc_free(mem_ctx);
495                         return ret;
496                 }
497
498                 ret = add_partition_to_data(ldb, data, partition);
499                 if (ret != LDB_SUCCESS) {
500                         talloc_free(mem_ctx);
501                         return ret;
502                 }
503         }
504
505         talloc_free(mem_ctx);
506         return LDB_SUCCESS;
507 }
508
509 /* Copy the metadata (@OPTIONS etc) for the new partition into the partition */
510
511 static int new_partition_set_replicated_metadata(struct ldb_context *ldb, 
512                                                  struct ldb_module *module, struct ldb_request *last_req, 
513                                                  struct partition_private_data *data, 
514                                                  struct dsdb_partition *partition)
515 {
516         int i, ret;
517         /* for each replicate, copy from main partition.  If we get an error, we report it up the chain */
518         for (i=0; data->replicate && data->replicate[i]; i++) {
519                 struct ldb_result *replicate_res;
520                 struct ldb_request *add_req;
521                 ret = dsdb_module_search_dn(module, last_req, &replicate_res, 
522                                             data->replicate[i],
523                                             NULL);
524                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
525                         continue;
526                 }
527                 if (ret != LDB_SUCCESS) {
528                         ldb_asprintf_errstring(ldb,
529                                                "Failed to search for %s from " DSDB_PARTITION_DN 
530                                                " replicateEntries for new partition at %s on %s: %s", 
531                                                ldb_dn_get_linearized(data->replicate[i]), 
532                                                partition->backend_url,
533                                                ldb_dn_get_linearized(partition->ctrl->dn), 
534                                                ldb_errstring(ldb));
535                         return ret;
536                 }
537
538                 /* Build add request */
539                 ret = ldb_build_add_req(&add_req, ldb, replicate_res, 
540                                         replicate_res->msgs[0], NULL, NULL, 
541                                         ldb_op_default_callback, last_req);
542                 last_req = add_req;
543                 if (ret != LDB_SUCCESS) {
544                         /* return directly, this is a very unlikely error */
545                         return ret;
546                 }
547                 /* do request */
548                 ret = ldb_next_request(partition->module, add_req);
549                 /* wait */
550                 if (ret == LDB_SUCCESS) {
551                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
552                 }
553                 
554                 switch (ret) {
555                 case LDB_SUCCESS:
556                         break;
557
558                 case LDB_ERR_ENTRY_ALREADY_EXISTS:
559                         /* Handle this case specially - if the
560                          * metadata already exists, replace it */
561                 {
562                         struct ldb_request *del_req;
563                         
564                         /* Don't leave a confusing string in the ldb_errstring() */
565                         ldb_reset_err_string(ldb);
566                         /* Build del request */
567                         ret = ldb_build_del_req(&del_req, ldb, replicate_res, replicate_res->msgs[0]->dn, NULL, NULL, 
568                                                 ldb_op_default_callback, last_req);
569                         last_req = del_req;
570                         if (ret != LDB_SUCCESS) {
571                                 /* return directly, this is a very unlikely error */
572                                 return ret;
573                         }
574                         /* do request */
575                         ret = ldb_next_request(partition->module, del_req);
576                         
577                         /* wait */
578                         if (ret == LDB_SUCCESS) {
579                                 ret = ldb_wait(del_req->handle, LDB_WAIT_ALL);
580                         }
581                         if (ret != LDB_SUCCESS) {
582                                 ldb_asprintf_errstring(ldb,
583                                                        "Failed to delete  (for re-add) %s from " DSDB_PARTITION_DN 
584                                                        " replicateEntries in new partition at %s on %s: %s", 
585                                                        ldb_dn_get_linearized(data->replicate[i]), 
586                                                        partition->backend_url,
587                                                        ldb_dn_get_linearized(partition->ctrl->dn), 
588                                                        ldb_errstring(ldb));
589                                 return ret;
590                         }
591                         
592                         /* Build add request */
593                         ret = ldb_build_add_req(&add_req, ldb, replicate_res, replicate_res->msgs[0], NULL, NULL, 
594                                                 ldb_op_default_callback, last_req);
595                         last_req = add_req;
596                         if (ret != LDB_SUCCESS) {
597                                 /* return directly, this is a very unlikely error */
598                                 return ret;
599                         }
600                         
601                         /* do the add again */
602                         ret = ldb_next_request(partition->module, add_req);
603                         
604                         /* wait */
605                         if (ret == LDB_SUCCESS) {
606                                 ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
607                         }
608
609                         if (ret != LDB_SUCCESS) {
610                                 ldb_asprintf_errstring(ldb,
611                                                        "Failed to add (after delete) %s from " DSDB_PARTITION_DN 
612                                                        " replicateEntries to new partition at %s on %s: %s", 
613                                                        ldb_dn_get_linearized(data->replicate[i]), 
614                                                        partition->backend_url,
615                                                        ldb_dn_get_linearized(partition->ctrl->dn), 
616                                                        ldb_errstring(ldb));
617                                 return ret;
618                         }
619                         break;
620                 }
621                 default: 
622                 {
623                         ldb_asprintf_errstring(ldb,
624                                                "Failed to add %s from " DSDB_PARTITION_DN 
625                                                " replicateEntries to new partition at %s on %s: %s", 
626                                                ldb_dn_get_linearized(data->replicate[i]), 
627                                                partition->backend_url,
628                                                ldb_dn_get_linearized(partition->ctrl->dn), 
629                                                ldb_errstring(ldb));
630                         return ret;
631                 }
632                 }
633
634                 /* And around again, for the next thing we must merge */
635         }
636         return LDB_SUCCESS;
637 }
638
639 /* Extended operation to create a new partition, called when
640  * 'new_partition' detects that one is being added based on it's
641  * instanceType */
642 int partition_create(struct ldb_module *module, struct ldb_request *req)
643 {
644         int i, ret;
645         struct ldb_context *ldb = ldb_module_get_ctx(module);
646         struct ldb_request *mod_req, *last_req = req;
647         struct ldb_message *mod_msg;
648         struct partition_private_data *data;
649         struct dsdb_partition *partition = NULL;
650         const char *casefold_dn;
651         bool new_partition = false;
652
653         /* Check if this is already a partition */
654
655         struct dsdb_create_partition_exop *ex_op = talloc_get_type(req->op.extended.data, struct dsdb_create_partition_exop);
656         struct ldb_dn *dn = ex_op->new_dn;
657
658         data = talloc_get_type(module->private_data, struct partition_private_data);
659         if (!data) {
660                 /* We are not going to create a partition before we are even set up */
661                 return LDB_ERR_UNWILLING_TO_PERFORM;
662         }
663
664         for (i=0; data->partitions && data->partitions[i]; i++) {
665                 if (ldb_dn_compare(data->partitions[i]->ctrl->dn, dn) == 0) {
666                         partition = data->partitions[i];
667                 }
668         }
669
670         if (!partition) {
671                 new_partition = true;
672                 mod_msg = ldb_msg_new(req);
673                 if (!mod_msg) {
674                         ldb_oom(ldb);
675                         return LDB_ERR_OPERATIONS_ERROR;
676                 }
677                 
678                 mod_msg->dn = ldb_dn_new(mod_msg, ldb, DSDB_PARTITION_DN);
679                 ret = ldb_msg_add_empty(mod_msg, DSDB_PARTITION_ATTR, LDB_FLAG_MOD_ADD, NULL);
680                 if (ret != LDB_SUCCESS) {
681                         return ret;
682                 }
683                 
684                 casefold_dn = ldb_dn_get_casefold(dn);
685                 
686                 ret = ldb_msg_add_string(mod_msg, DSDB_PARTITION_ATTR, casefold_dn);
687                 if (ret != LDB_SUCCESS) {
688                         return ret;
689                 }
690                 
691                 /* Perform modify on @PARTITION record */
692                 ret = ldb_build_mod_req(&mod_req, ldb, req, mod_msg, NULL, NULL, 
693                                         ldb_op_default_callback, req);
694                 
695                 if (ret != LDB_SUCCESS) {
696                         return ret;
697                 }
698                 
699                 last_req = mod_req;
700
701                 ret = ldb_next_request(module, mod_req);
702                 if (ret == LDB_SUCCESS) {
703                         ret = ldb_wait(mod_req->handle, LDB_WAIT_ALL);
704                 }
705                 
706                 if (ret != LDB_SUCCESS) {
707                         return ret;
708                 }
709                 
710                 /* Make a partition structure for this new partition, so we can copy in the template structure */ 
711                 ret = new_partition_from_dn(ldb, data, req, ldb_dn_copy(req, dn), casefold_dn, &partition);
712                 if (ret != LDB_SUCCESS) {
713                         return ret;
714                 }
715         }
716         
717         ret = new_partition_set_replicated_metadata(ldb, module, last_req, data, partition);
718         if (ret != LDB_SUCCESS) {
719                 return ret;
720         }
721         
722         if (new_partition) {
723                 ret = add_partition_to_data(ldb, data, partition);
724                 if (ret != LDB_SUCCESS) {
725                         return ret;
726                 }
727         }
728
729         /* send request done */
730         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
731 }
732
733
734 int partition_init(struct ldb_module *module)
735 {
736         int ret;
737         TALLOC_CTX *mem_ctx = talloc_new(module);
738
739         struct partition_private_data *data;
740
741         if (!mem_ctx) {
742                 return LDB_ERR_OPERATIONS_ERROR;
743         }
744
745         data = talloc_zero(mem_ctx, struct partition_private_data);
746         if (data == NULL) {
747                 return LDB_ERR_OPERATIONS_ERROR;
748         }
749
750         /* This loads the partitions */
751         ret = partition_reload_if_required(module, data);
752         if (ret != LDB_SUCCESS) {
753                 return ret;
754         }
755
756         module->private_data = talloc_steal(module, data);
757         talloc_free(mem_ctx);
758
759         ret = ldb_mod_register_control(module, LDB_CONTROL_DOMAIN_SCOPE_OID);
760         if (ret != LDB_SUCCESS) {
761                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
762                         "partition: Unable to register control with rootdse!\n");
763                 return LDB_ERR_OPERATIONS_ERROR;
764         }
765
766         ret = ldb_mod_register_control(module, LDB_CONTROL_SEARCH_OPTIONS_OID);
767         if (ret != LDB_SUCCESS) {
768                 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
769                         "partition: Unable to register control with rootdse!\n");
770                 return LDB_ERR_OPERATIONS_ERROR;
771         }
772
773         return ldb_next_init(module);
774 }