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