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