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