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