4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
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.
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.
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/>.
24 * Component: ldb partitions module
26 * Description: Implement LDAP partitions
28 * Author: Andrew Bartlett
29 * Author: Stefan Metzmacher
32 #include "dsdb/samdb/ldb_modules/partition.h"
33 #include "lib/util/tsort.h"
34 #include "lib/ldb-samba/ldb_wrap.h"
35 #include "system/filesys.h"
37 static int partition_sort_compare(const void *v1
, const void *v2
)
39 const struct dsdb_partition
*p1
;
40 const struct dsdb_partition
*p2
;
42 p1
= *((struct dsdb_partition
* const*)v1
);
43 p2
= *((struct dsdb_partition
* const*)v2
);
45 return ldb_dn_compare(p1
->ctrl
->dn
, p2
->ctrl
->dn
);
48 /* Load the list of DNs that we must replicate to all partitions */
49 static int partition_load_replicate_dns(struct ldb_context
*ldb
,
50 struct partition_private_data
*data
,
51 struct ldb_message
*msg
)
53 struct ldb_message_element
*replicate_attributes
= ldb_msg_find_element(msg
, "replicateEntries");
55 talloc_free(data
->replicate
);
56 if (!replicate_attributes
) {
57 data
->replicate
= NULL
;
60 data
->replicate
= talloc_array(data
, struct ldb_dn
*, replicate_attributes
->num_values
+ 1);
61 if (!data
->replicate
) {
65 for (i
=0; i
< replicate_attributes
->num_values
; i
++) {
66 data
->replicate
[i
] = ldb_dn_from_ldb_val(data
->replicate
, ldb
, &replicate_attributes
->values
[i
]);
67 if (!ldb_dn_validate(data
->replicate
[i
])) {
68 ldb_asprintf_errstring(ldb
,
70 "invalid DN in partition replicate record: %s",
71 replicate_attributes
->values
[i
].data
);
72 return LDB_ERR_CONSTRAINT_VIOLATION
;
75 data
->replicate
[i
] = NULL
;
80 /* Load the list of modules for the partitions */
81 static int partition_load_modules(struct ldb_context
*ldb
,
82 struct partition_private_data
*data
, struct ldb_message
*msg
)
85 struct ldb_message_element
*modules_attributes
= ldb_msg_find_element(msg
, "modules");
86 talloc_free(data
->modules
);
87 if (!modules_attributes
) {
91 data
->modules
= talloc_array(data
, struct partition_module
*, modules_attributes
->num_values
+ 1);
96 for (i
=0; i
< modules_attributes
->num_values
; i
++) {
99 data
->modules
[i
] = talloc(data
->modules
, struct partition_module
);
100 if (!data
->modules
[i
]) {
104 dn_blob
= modules_attributes
->values
[i
];
106 p
= strchr((const char *)dn_blob
.data
, ':');
108 ldb_asprintf_errstring(ldb
,
109 "partition_load_modules: "
110 "invalid form for partition module record (missing ':'): %s", (const char *)dn_blob
.data
);
111 return LDB_ERR_CONSTRAINT_VIOLATION
;
113 /* Now trim off the filename */
114 dn_blob
.length
= ((uint8_t *)p
- dn_blob
.data
);
117 data
->modules
[i
]->modules
= ldb_modules_list_from_string(ldb
, data
->modules
[i
],
120 if (dn_blob
.length
== 1 && dn_blob
.data
[0] == '*') {
121 data
->modules
[i
]->dn
= NULL
;
123 data
->modules
[i
]->dn
= ldb_dn_from_ldb_val(data
->modules
[i
], ldb
, &dn_blob
);
124 if (!data
->modules
[i
]->dn
|| !ldb_dn_validate(data
->modules
[i
]->dn
)) {
125 return ldb_operr(ldb
);
129 data
->modules
[i
] = NULL
;
133 static int partition_reload_metadata(struct ldb_module
*module
, struct partition_private_data
*data
,
134 TALLOC_CTX
*mem_ctx
, struct ldb_message
**_msg
,
135 struct ldb_request
*parent
)
138 struct ldb_message
*msg
, *module_msg
;
139 struct ldb_result
*res
;
140 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
141 const char *attrs
[] = { "partition", "replicateEntries", "modules",
142 "partialReplica", "backendStore", NULL
};
143 /* perform search for @PARTITION, looking for module, replicateEntries and ldapBackend */
144 ret
= dsdb_module_search_dn(module
, mem_ctx
, &res
,
145 ldb_dn_new(mem_ctx
, ldb
, DSDB_PARTITION_DN
),
147 DSDB_FLAG_NEXT_MODULE
, parent
);
148 if (ret
!= LDB_SUCCESS
) {
154 ret
= partition_load_replicate_dns(ldb
, data
, msg
);
155 if (ret
!= LDB_SUCCESS
) {
159 /* When used from Samba4, this message is set by the samba4
160 * module, as a fixed value not read from the DB. This avoids
161 * listing modules in the DB */
162 if (data
->forced_module_msg
) {
163 module_msg
= data
->forced_module_msg
;
168 ret
= partition_load_modules(ldb
, data
, module_msg
);
169 if (ret
!= LDB_SUCCESS
) {
182 static const char **find_modules_for_dn(struct partition_private_data
*data
, struct ldb_dn
*dn
)
185 struct partition_module
*default_mod
= NULL
;
186 for (i
=0; data
->modules
&& data
->modules
[i
]; i
++) {
187 if (!data
->modules
[i
]->dn
) {
188 default_mod
= data
->modules
[i
];
189 } else if (ldb_dn_compare(dn
, data
->modules
[i
]->dn
) == 0) {
190 return data
->modules
[i
]->modules
;
194 return default_mod
->modules
;
200 static int new_partition_from_dn(struct ldb_context
*ldb
, struct partition_private_data
*data
,
202 struct ldb_dn
*dn
, const char *filename
,
203 const char *backend_db_store
,
204 struct dsdb_partition
**partition
) {
205 struct dsdb_control_current_partition
*ctrl
;
206 struct ldb_module
*backend_module
;
208 struct ldb_module
*module_chain
;
209 const char **modules
;
210 const char **options
= NULL
;
213 (*partition
) = talloc_zero(mem_ctx
, struct dsdb_partition
);
218 (*partition
)->ctrl
= ctrl
= talloc((*partition
), struct dsdb_control_current_partition
);
220 talloc_free(*partition
);
224 /* the backend LDB is the DN (base64 encoded if not 'plain') followed by .ldb */
225 backend_path
= ldb_relative_path(ldb
,
229 ldb_asprintf_errstring(ldb
,
230 "partition_init: unable to determine an relative path for partition: %s",
232 talloc_free(*partition
);
233 return LDB_ERR_OPERATIONS_ERROR
;
235 (*partition
)->backend_url
= talloc_asprintf(*partition
, "%s://%s",
239 if (!(ldb_module_flags(ldb
) & LDB_FLG_RDONLY
)) {
243 p
= strrchr(backend_path
, '/');
247 backend_dir
= backend_path
;
249 /* Failure is quite reasonable, it might already exist */
250 mkdir(backend_dir
, 0700);
253 ctrl
->version
= DSDB_CONTROL_CURRENT_PARTITION_VERSION
;
254 ctrl
->dn
= talloc_steal(ctrl
, dn
);
256 options
= ldb_options_get(ldb
);
257 ret
= ldb_module_connect_backend(
258 ldb
, (*partition
)->backend_url
, options
, &backend_module
);
259 if (ret
!= LDB_SUCCESS
) {
262 talloc_steal((*partition
), backend_module
);
264 modules
= find_modules_for_dn(data
, dn
);
267 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
)));
268 talloc_free(*partition
);
269 return LDB_ERR_CONSTRAINT_VIOLATION
;
271 ret
= ldb_module_load_list(ldb
, modules
, backend_module
, &module_chain
);
272 if (ret
!= LDB_SUCCESS
) {
273 ldb_asprintf_errstring(ldb
,
275 "loading backend for %s failed: %s",
276 ldb_dn_get_linearized(dn
), ldb_errstring(ldb
));
277 talloc_free(*partition
);
280 ret
= ldb_module_init_chain(ldb
, module_chain
);
281 if (ret
!= LDB_SUCCESS
) {
282 ldb_asprintf_errstring(ldb
,
284 "initialising backend for %s failed: %s",
285 ldb_dn_get_linearized(dn
), ldb_errstring(ldb
));
286 talloc_free(*partition
);
290 /* This weirdness allows us to use ldb_next_request() in partition.c */
291 (*partition
)->module
= ldb_module_new(*partition
, ldb
, "partition_next", NULL
);
292 if (!(*partition
)->module
) {
293 talloc_free(*partition
);
296 ldb_module_set_next((*partition
)->module
, talloc_steal((*partition
)->module
, module_chain
));
298 /* if we were in a transaction then we need to start a
299 transaction on this new partition, otherwise we'll get a
300 transaction mismatch when we end the transaction */
301 if (data
->in_transaction
) {
302 if (ldb_module_flags(ldb
) & LDB_FLG_ENABLE_TRACING
) {
303 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "partition_start_trans() -> %s (new partition)",
304 ldb_dn_get_linearized((*partition
)->ctrl
->dn
));
306 ret
= ldb_next_start_trans((*partition
)->module
);
312 /* Tell the rootDSE about the new partition */
313 static int partition_register(struct ldb_context
*ldb
, struct dsdb_control_current_partition
*ctrl
)
315 struct ldb_request
*req
;
318 req
= talloc_zero(NULL
, struct ldb_request
);
323 req
->operation
= LDB_REQ_REGISTER_PARTITION
;
324 req
->op
.reg_partition
.dn
= ctrl
->dn
;
325 req
->callback
= ldb_op_default_callback
;
327 ldb_set_timeout(ldb
, req
, 0);
329 req
->handle
= ldb_handle_new(req
, ldb
);
330 if (req
->handle
== NULL
) {
332 return ldb_operr(ldb
);
335 ret
= ldb_request(ldb
, req
);
336 if (ret
== LDB_SUCCESS
) {
337 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
339 if (ret
!= LDB_SUCCESS
) {
340 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "partition: Unable to register partition with rootdse!\n");
342 return LDB_ERR_OTHER
;
349 /* Add a newly found partition to the global data */
350 static int add_partition_to_data(struct ldb_context
*ldb
, struct partition_private_data
*data
,
351 struct dsdb_partition
*partition
)
356 /* Count the partitions */
357 for (i
=0; data
->partitions
&& data
->partitions
[i
]; i
++) { /* noop */};
359 /* Add partition to list of partitions */
360 data
->partitions
= talloc_realloc(data
, data
->partitions
, struct dsdb_partition
*, i
+ 2);
361 if (!data
->partitions
) {
364 data
->partitions
[i
] = talloc_steal(data
->partitions
, partition
);
365 data
->partitions
[i
+1] = NULL
;
367 /* Sort again (should use binary insert) */
368 TYPESAFE_QSORT(data
->partitions
, i
+1, partition_sort_compare
);
370 ret
= partition_register(ldb
, partition
->ctrl
);
371 if (ret
!= LDB_SUCCESS
) {
377 int partition_reload_if_required(struct ldb_module
*module
,
378 struct partition_private_data
*data
,
379 struct ldb_request
*parent
)
384 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
385 struct ldb_message
*msg
;
386 struct ldb_message_element
*partition_attributes
;
387 struct ldb_message_element
*partial_replicas
;
391 /* Not initialised yet */
395 mem_ctx
= talloc_new(data
);
400 ret
= partition_primary_sequence_number(module
, mem_ctx
, &seq
, parent
);
401 if (ret
!= LDB_SUCCESS
) {
402 talloc_free(mem_ctx
);
405 if (seq
== data
->metadata_seq
) {
406 talloc_free(mem_ctx
);
410 /* This loads metadata tdb. If it's missing, creates it */
411 ret
= partition_metadata_init(module
);
412 if (ret
!= LDB_SUCCESS
) {
416 ret
= partition_reload_metadata(module
, data
, mem_ctx
, &msg
, parent
);
417 if (ret
!= LDB_SUCCESS
) {
418 talloc_free(mem_ctx
);
422 data
->metadata_seq
= seq
;
424 partition_attributes
= ldb_msg_find_element(msg
, "partition");
425 partial_replicas
= ldb_msg_find_element(msg
, "partialReplica");
426 data
->backend_db_store
427 = talloc_strdup(data
, ldb_msg_find_attr_as_string(msg
, "backendStore", "tdb"));
429 if (data
->backend_db_store
== NULL
) {
430 talloc_free(mem_ctx
);
431 return ldb_module_oom(module
);
434 for (i
=0; partition_attributes
&& i
< partition_attributes
->num_values
; i
++) {
436 bool new_partition
= true;
437 const char *filename
= NULL
;
440 struct dsdb_partition
*partition
;
441 struct ldb_result
*dn_res
;
442 const char *no_attrs
[] = { NULL
};
444 for (j
=0; data
->partitions
&& data
->partitions
[j
]; j
++) {
445 if (data_blob_cmp(&data
->partitions
[j
]->orig_record
, &partition_attributes
->values
[i
]) == 0) {
446 new_partition
= false;
450 if (new_partition
== false) {
454 dn_blob
= partition_attributes
->values
[i
];
456 if (dn_blob
.length
> 4 &&
457 (strncmp((const char *)&dn_blob
.data
[dn_blob
.length
-4], ".ldb", 4) == 0)) {
459 /* Look for DN:filename.ldb */
460 char *p
= strrchr((const char *)dn_blob
.data
, ':');
462 ldb_asprintf_errstring(ldb
,
463 "partition_init: invalid DN in attempting to parse partition record: %s", (const char *)dn_blob
.data
);
464 talloc_free(mem_ctx
);
465 return LDB_ERR_CONSTRAINT_VIOLATION
;
469 /* Now trim off the filename */
470 dn_blob
.length
= ((uint8_t *)p
- dn_blob
.data
);
473 dn
= ldb_dn_from_ldb_val(mem_ctx
, ldb
, &dn_blob
);
475 ldb_asprintf_errstring(ldb
,
476 "partition_init: invalid DN in partition record: %s", (const char *)dn_blob
.data
);
477 talloc_free(mem_ctx
);
478 return LDB_ERR_CONSTRAINT_VIOLATION
;
481 /* Now do a slow check with the DN compare */
482 for (j
=0; data
->partitions
&& data
->partitions
[j
]; j
++) {
483 if (ldb_dn_compare(dn
, data
->partitions
[j
]->ctrl
->dn
) == 0) {
484 new_partition
= false;
488 if (new_partition
== false) {
493 char *base64_dn
= NULL
;
495 for (p
= ldb_dn_get_linearized(dn
); *p
; p
++) {
496 /* We have such a strict check because I don't want shell metacharacters in the file name, nor ../ */
497 if (!(isalnum(*p
) || *p
== ' ' || *p
== '=' || *p
== ',')) {
502 base64_dn
= ldb_base64_encode(data
, ldb_dn_get_linearized(dn
), strlen(ldb_dn_get_linearized(dn
)));
503 filename
= talloc_asprintf(mem_ctx
, "%s.ldb", base64_dn
);
505 filename
= talloc_asprintf(mem_ctx
, "%s.ldb", ldb_dn_get_linearized(dn
));
509 /* We call ldb_dn_get_linearized() because the DN in
510 * partition_attributes is already casefolded
511 * correctly. We don't want to mess that up as the
512 * schema isn't loaded yet */
513 ret
= new_partition_from_dn(ldb
, data
, data
->partitions
, dn
,
514 filename
, data
->backend_db_store
,
516 if (ret
!= LDB_SUCCESS
) {
517 talloc_free(mem_ctx
);
521 talloc_steal(partition
, partition_attributes
->values
[i
].data
);
522 partition
->orig_record
= partition_attributes
->values
[i
];
524 /* Get the 'correct' case of the partition DNs from the database */
525 ret
= dsdb_module_search_dn(partition
->module
, data
, &dn_res
,
527 DSDB_FLAG_NEXT_MODULE
, parent
);
528 if (ret
== LDB_SUCCESS
) {
529 talloc_free(partition
->ctrl
->dn
);
530 partition
->ctrl
->dn
= talloc_steal(partition
->ctrl
, dn_res
->msgs
[0]->dn
);
532 } else if (ret
!= LDB_ERR_NO_SUCH_OBJECT
) {
533 ldb_asprintf_errstring(ldb
,
534 "Failed to search for partition base %s in new partition at %s: %s",
535 ldb_dn_get_linearized(dn
),
536 partition
->backend_url
,
538 talloc_free(mem_ctx
);
542 /* see if it is a partial replica */
543 for (j
=0; partial_replicas
&& j
<partial_replicas
->num_values
; j
++) {
544 struct ldb_dn
*pa_dn
= ldb_dn_from_ldb_val(mem_ctx
, ldb
, &partial_replicas
->values
[j
]);
545 if (pa_dn
!= NULL
&& ldb_dn_compare(pa_dn
, partition
->ctrl
->dn
) == 0) {
546 partition
->partial_replica
= true;
551 ret
= add_partition_to_data(ldb
, data
, partition
);
552 if (ret
!= LDB_SUCCESS
) {
553 talloc_free(mem_ctx
);
558 talloc_free(mem_ctx
);
562 /* Copy the metadata (@OPTIONS etc) for the new partition into the partition */
564 static int new_partition_set_replicated_metadata(struct ldb_context
*ldb
,
565 struct ldb_module
*module
, struct ldb_request
*last_req
,
566 struct partition_private_data
*data
,
567 struct dsdb_partition
*partition
)
571 /* for each replicate, copy from main partition. If we get an error, we report it up the chain */
572 for (i
=0; data
->replicate
&& data
->replicate
[i
]; i
++) {
573 struct ldb_result
*replicate_res
;
574 struct ldb_request
*add_req
;
575 ret
= dsdb_module_search_dn(module
, last_req
, &replicate_res
,
578 DSDB_FLAG_NEXT_MODULE
, NULL
);
579 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
582 if (ret
!= LDB_SUCCESS
) {
583 ldb_asprintf_errstring(ldb
,
584 "Failed to search for %s from " DSDB_PARTITION_DN
585 " replicateEntries for new partition at %s on %s: %s",
586 ldb_dn_get_linearized(data
->replicate
[i
]),
587 partition
->backend_url
,
588 ldb_dn_get_linearized(partition
->ctrl
->dn
),
593 /* Build add request */
594 ret
= ldb_build_add_req(&add_req
, ldb
, replicate_res
,
595 replicate_res
->msgs
[0], NULL
, NULL
,
596 ldb_op_default_callback
, last_req
);
597 LDB_REQ_SET_LOCATION(add_req
);
599 if (ret
!= LDB_SUCCESS
) {
600 /* return directly, this is a very unlikely error */
604 ret
= ldb_next_request(partition
->module
, add_req
);
606 if (ret
== LDB_SUCCESS
) {
607 ret
= ldb_wait(add_req
->handle
, LDB_WAIT_ALL
);
614 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
615 /* Handle this case specially - if the
616 * metadata already exists, replace it */
618 struct ldb_request
*del_req
;
620 /* Don't leave a confusing string in the ldb_errstring() */
621 ldb_reset_err_string(ldb
);
622 /* Build del request */
623 ret
= ldb_build_del_req(&del_req
, ldb
, replicate_res
, replicate_res
->msgs
[0]->dn
, NULL
, NULL
,
624 ldb_op_default_callback
, last_req
);
625 LDB_REQ_SET_LOCATION(del_req
);
627 if (ret
!= LDB_SUCCESS
) {
628 /* return directly, this is a very unlikely error */
632 ret
= ldb_next_request(partition
->module
, del_req
);
635 if (ret
== LDB_SUCCESS
) {
636 ret
= ldb_wait(del_req
->handle
, LDB_WAIT_ALL
);
638 if (ret
!= LDB_SUCCESS
) {
639 ldb_asprintf_errstring(ldb
,
640 "Failed to delete (for re-add) %s from " DSDB_PARTITION_DN
641 " replicateEntries in new partition at %s on %s: %s",
642 ldb_dn_get_linearized(data
->replicate
[i
]),
643 partition
->backend_url
,
644 ldb_dn_get_linearized(partition
->ctrl
->dn
),
649 /* Build add request */
650 ret
= ldb_build_add_req(&add_req
, ldb
, replicate_res
, replicate_res
->msgs
[0], NULL
, NULL
,
651 ldb_op_default_callback
, last_req
);
652 LDB_REQ_SET_LOCATION(add_req
);
654 if (ret
!= LDB_SUCCESS
) {
655 /* return directly, this is a very unlikely error */
659 /* do the add again */
660 ret
= ldb_next_request(partition
->module
, add_req
);
663 if (ret
== LDB_SUCCESS
) {
664 ret
= ldb_wait(add_req
->handle
, LDB_WAIT_ALL
);
667 if (ret
!= LDB_SUCCESS
) {
668 ldb_asprintf_errstring(ldb
,
669 "Failed to add (after delete) %s from " DSDB_PARTITION_DN
670 " replicateEntries to new partition at %s on %s: %s",
671 ldb_dn_get_linearized(data
->replicate
[i
]),
672 partition
->backend_url
,
673 ldb_dn_get_linearized(partition
->ctrl
->dn
),
681 ldb_asprintf_errstring(ldb
,
682 "Failed to add %s from " DSDB_PARTITION_DN
683 " replicateEntries to new partition at %s on %s: %s",
684 ldb_dn_get_linearized(data
->replicate
[i
]),
685 partition
->backend_url
,
686 ldb_dn_get_linearized(partition
->ctrl
->dn
),
692 /* And around again, for the next thing we must merge */
697 /* Extended operation to create a new partition, called when
698 * 'new_partition' detects that one is being added based on it's
700 int partition_create(struct ldb_module
*module
, struct ldb_request
*req
)
704 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
705 struct ldb_request
*mod_req
, *last_req
= req
;
706 struct ldb_message
*mod_msg
;
707 struct partition_private_data
*data
;
708 struct dsdb_partition
*partition
= NULL
;
709 const char *casefold_dn
;
710 bool new_partition
= false;
712 /* Check if this is already a partition */
714 struct dsdb_create_partition_exop
*ex_op
= talloc_get_type(req
->op
.extended
.data
, struct dsdb_create_partition_exop
);
715 struct ldb_dn
*dn
= ex_op
->new_dn
;
717 data
= talloc_get_type(ldb_module_get_private(module
), struct partition_private_data
);
719 /* We are not going to create a partition before we are even set up */
720 return LDB_ERR_UNWILLING_TO_PERFORM
;
723 /* see if we are still up-to-date */
724 ret
= partition_reload_if_required(module
, data
, req
);
725 if (ret
!= LDB_SUCCESS
) {
729 for (i
=0; data
->partitions
&& data
->partitions
[i
]; i
++) {
730 if (ldb_dn_compare(data
->partitions
[i
]->ctrl
->dn
, dn
) == 0) {
731 partition
= data
->partitions
[i
];
737 char *partition_record
;
738 new_partition
= true;
739 mod_msg
= ldb_msg_new(req
);
744 mod_msg
->dn
= ldb_dn_new(mod_msg
, ldb
, DSDB_PARTITION_DN
);
746 casefold_dn
= ldb_dn_get_casefold(dn
);
750 const char *p
, *sam_name
;
751 sam_name
= strrchr((const char *)ldb_get_opaque(ldb
, "ldb_url"), '/');
753 return ldb_operr(ldb
);
757 for (p
= casefold_dn
; *p
; p
++) {
758 /* We have such a strict check because
759 * I don't want shell metacharacters
760 * in the file name, nor ../, but I do
761 * want it to be easily typed if SAFE
763 if (!(isalnum(*p
) || *p
== ' ' || *p
== '=' || *p
== ',')) {
768 escaped
= rfc1738_escape_part(mod_msg
, casefold_dn
);
772 filename
= talloc_asprintf(mod_msg
, "%s.d/%s.ldb", sam_name
, escaped
);
773 talloc_free(escaped
);
775 filename
= talloc_asprintf(mod_msg
, "%s.d/%s.ldb", sam_name
, casefold_dn
);
782 partition_record
= talloc_asprintf(mod_msg
, "%s:%s", casefold_dn
, filename
);
784 ret
= ldb_msg_append_steal_string(mod_msg
, DSDB_PARTITION_ATTR
, partition_record
,
786 if (ret
!= LDB_SUCCESS
) {
790 if (ldb_request_get_control(req
, DSDB_CONTROL_PARTIAL_REPLICA
)) {
791 /* this new partition is a partial replica */
792 ret
= ldb_msg_append_fmt(mod_msg
, LDB_FLAG_MOD_ADD
,
793 "partialReplica", "%s", ldb_dn_get_linearized(dn
));
794 if (ret
!= LDB_SUCCESS
) {
799 /* Perform modify on @PARTITION record */
800 ret
= ldb_build_mod_req(&mod_req
, ldb
, req
, mod_msg
, NULL
, NULL
,
801 ldb_op_default_callback
, req
);
802 LDB_REQ_SET_LOCATION(mod_req
);
803 if (ret
!= LDB_SUCCESS
) {
809 ret
= ldb_next_request(module
, mod_req
);
810 if (ret
== LDB_SUCCESS
) {
811 ret
= ldb_wait(mod_req
->handle
, LDB_WAIT_ALL
);
814 if (ret
!= LDB_SUCCESS
) {
818 /* Make a partition structure for this new partition, so we can copy in the template structure */
819 ret
= new_partition_from_dn(ldb
, data
, req
, ldb_dn_copy(req
, dn
),
820 filename
, data
->backend_db_store
,
822 if (ret
!= LDB_SUCCESS
) {
825 talloc_steal(partition
, partition_record
);
826 partition
->orig_record
= data_blob_string_const(partition_record
);
829 ret
= new_partition_set_replicated_metadata(ldb
, module
, last_req
, data
, partition
);
830 if (ret
!= LDB_SUCCESS
) {
835 ret
= add_partition_to_data(ldb
, data
, partition
);
836 if (ret
!= LDB_SUCCESS
) {
841 /* send request done */
842 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);
846 int partition_init(struct ldb_module
*module
)
849 TALLOC_CTX
*mem_ctx
= talloc_new(module
);
850 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
851 struct partition_private_data
*data
;
854 return ldb_operr(ldb
);
857 /* We actually got this during the read_lock call */
858 data
= talloc_get_type_abort(ldb_module_get_private(module
),
859 struct partition_private_data
);
861 /* This loads the partitions */
862 ret
= partition_reload_if_required(module
, data
, NULL
);
863 if (ret
!= LDB_SUCCESS
) {
867 ldb_module_set_private(module
, talloc_steal(module
, data
));
868 talloc_free(mem_ctx
);
870 ret
= ldb_mod_register_control(module
, LDB_CONTROL_DOMAIN_SCOPE_OID
);
871 if (ret
!= LDB_SUCCESS
) {
872 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
873 "partition: Unable to register control with rootdse!\n");
874 return ldb_operr(ldb
);
877 ret
= ldb_mod_register_control(module
, LDB_CONTROL_SEARCH_OPTIONS_OID
);
878 if (ret
!= LDB_SUCCESS
) {
879 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
880 "partition: Unable to register control with rootdse!\n");
881 return ldb_operr(ldb
);
884 return ldb_next_init(module
);