*/
#include "dsdb/samdb/ldb_modules/partition.h"
+#include "lib/util/tsort.h"
+#include "lib/ldb-samba/ldb_wrap.h"
+#include "system/filesys.h"
+
static int partition_sort_compare(const void *v1, const void *v2)
{
const struct dsdb_partition *p1;
}
/* Load the list of DNs that we must replicate to all partitions */
-static int partition_load_replicate_dns(struct ldb_context *ldb, struct partition_private_data *data, struct ldb_message *msg)
+static int partition_load_replicate_dns(struct ldb_context *ldb,
+ struct partition_private_data *data,
+ struct ldb_message *msg)
{
struct ldb_message_element *replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
if (!replicate_attributes) {
data->replicate = NULL;
} else {
- int i;
+ unsigned int i;
data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
if (!data->replicate) {
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
for (i=0; i < replicate_attributes->num_values; i++) {
static int partition_load_modules(struct ldb_context *ldb,
struct partition_private_data *data, struct ldb_message *msg)
{
- int i;
+ unsigned int i;
struct ldb_message_element *modules_attributes = ldb_msg_find_element(msg, "modules");
talloc_free(data->modules);
if (!modules_attributes) {
data->modules = talloc_array(data, struct partition_module *, modules_attributes->num_values + 1);
if (!data->modules) {
- ldb_oom(ldb);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
for (i=0; i < modules_attributes->num_values; i++) {
DATA_BLOB dn_blob;
data->modules[i] = talloc(data->modules, struct partition_module);
if (!data->modules[i]) {
- ldb_oom(ldb);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
dn_blob = modules_attributes->values[i];
} else {
data->modules[i]->dn = ldb_dn_from_ldb_val(data->modules[i], ldb, &dn_blob);
if (!data->modules[i]->dn || !ldb_dn_validate(data->modules[i]->dn)) {
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_operr(ldb);
}
}
}
return LDB_SUCCESS;
}
-static int partition_reload_metadata(struct ldb_module *module, struct partition_private_data *data, TALLOC_CTX *mem_ctx, struct ldb_message **_msg)
+static int partition_reload_metadata(struct ldb_module *module, struct partition_private_data *data,
+ TALLOC_CTX *mem_ctx, struct ldb_message **_msg,
+ struct ldb_request *parent)
{
int ret;
- struct ldb_message *msg;
+ struct ldb_message *msg, *module_msg;
struct ldb_result *res;
struct ldb_context *ldb = ldb_module_get_ctx(module);
- const char *attrs[] = { "partition", "replicateEntries", "modules", NULL };
+ const char *attrs[] = { "partition", "replicateEntries", "modules", "ldapBackend", NULL };
/* perform search for @PARTITION, looking for module, replicateEntries and ldapBackend */
ret = dsdb_module_search_dn(module, mem_ctx, &res,
ldb_dn_new(mem_ctx, ldb, DSDB_PARTITION_DN),
- attrs);
+ attrs,
+ DSDB_FLAG_NEXT_MODULE, parent);
if (ret != LDB_SUCCESS) {
return ret;
}
return ret;
}
- ret = partition_load_modules(ldb, data, msg);
+ /* When used from Samba4, this message is set by the samba4
+ * module, as a fixed value not read from the DB. This avoids
+ * listing modules in the DB */
+ if (data->forced_module_msg) {
+ module_msg = data->forced_module_msg;
+ } else {
+ module_msg = msg;
+ }
+
+ ret = partition_load_modules(ldb, data, module_msg);
if (ret != LDB_SUCCESS) {
return ret;
}
static const char **find_modules_for_dn(struct partition_private_data *data, struct ldb_dn *dn)
{
- int i;
+ unsigned int i;
struct partition_module *default_mod = NULL;
for (i=0; data->modules && data->modules[i]; i++) {
if (!data->modules[i]->dn) {
static int new_partition_from_dn(struct ldb_context *ldb, struct partition_private_data *data,
TALLOC_CTX *mem_ctx,
- struct ldb_dn *dn, const char *filename_base,
+ struct ldb_dn *dn, const char *filename,
struct dsdb_partition **partition) {
const char *backend_url;
struct dsdb_control_current_partition *ctrl;
(*partition) = talloc(mem_ctx, struct dsdb_partition);
if (!*partition) {
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
(*partition)->ctrl = ctrl = talloc((*partition), struct dsdb_control_current_partition);
if (!ctrl) {
talloc_free(*partition);
- ldb_oom(ldb);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
/* See if an LDAP backend has been specified */
if (data->ldapBackend) {
- backend_url = data->ldapBackend;
+ (*partition)->backend_url = data->ldapBackend;
} else {
-
/* the backend LDB is the DN (base64 encoded if not 'plain') followed by .ldb */
- const char *p;
- char *backend_path;
- char *base64_dn = NULL;
- for (p = filename_base; *p; p++) {
- /* We have such a strict check because I don't want shell metacharacters in the file name, nor ../ */
- if (!(isalnum(*p) || *p == ' ' || *p == '=' || *p == ',')) {
- break;
- }
- }
- if (*p) {
- filename_base = base64_dn = ldb_base64_encode(data, filename_base, strlen(filename_base));
- }
-
- backend_path = samdb_relative_path(ldb,
- *partition,
- filename_base);
- if (base64_dn) {
- talloc_free(base64_dn);
- }
- if (!backend_path) {
+ backend_url = ldb_relative_path(ldb,
+ *partition,
+ filename);
+ if (!backend_url) {
ldb_asprintf_errstring(ldb,
- "partition_init: unable to determine an relative path for partition: %s", filename_base);
+ "partition_init: unable to determine an relative path for partition: %s", filename);
talloc_free(*partition);
- return LDB_ERR_OPERATIONS_ERROR;
+ return LDB_ERR_OPERATIONS_ERROR;
}
- backend_url = talloc_asprintf(*partition, "tdb://%s.ldb",
- backend_path);
- talloc_free(backend_path);
- if (!backend_url) {
- ldb_oom(ldb);
- talloc_free(*partition);
- return LDB_ERR_OPERATIONS_ERROR;
+ (*partition)->backend_url = talloc_steal((*partition), backend_url);
+
+ if (!(ldb_module_flags(ldb) & LDB_FLG_RDONLY)) {
+ char *p;
+ char *backend_dir = talloc_strdup(*partition, backend_url);
+
+ p = strrchr(backend_dir, '/');
+ if (p) {
+ p[0] = '\0';
+ }
+
+ /* Failure is quite reasonable, it might alredy exist */
+ mkdir(backend_dir, 0700);
+ talloc_free(backend_dir);
}
+
}
- (*partition)->backend_url = backend_url;
ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
ctrl->dn = talloc_steal(ctrl, dn);
- ret = ldb_connect_backend(ldb, backend_url, NULL, &backend_module);
+ ret = ldb_module_connect_backend(ldb, (*partition)->backend_url, NULL, &backend_module);
if (ret != LDB_SUCCESS) {
return ret;
}
talloc_free(*partition);
return LDB_ERR_CONSTRAINT_VIOLATION;
}
- ret = ldb_load_modules_list(ldb, modules, backend_module, &module_chain);
+ ret = ldb_module_load_list(ldb, modules, backend_module, &module_chain);
if (ret != LDB_SUCCESS) {
ldb_asprintf_errstring(ldb,
"partition_init: "
talloc_free(*partition);
return ret;
}
- ret = ldb_init_module_chain(ldb, module_chain);
+ ret = ldb_module_init_chain(ldb, module_chain);
if (ret != LDB_SUCCESS) {
ldb_asprintf_errstring(ldb,
"partition_init: "
/* This weirdness allows us to use ldb_next_request() in partition.c */
(*partition)->module = ldb_module_new(*partition, ldb, "partition_next", NULL);
if (!(*partition)->module) {
- ldb_oom(ldb);
talloc_free(*partition);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
- (*partition)->module->next = talloc_steal((*partition)->module, module_chain);
+ ldb_module_set_next((*partition)->module, talloc_steal((*partition)->module, module_chain));
/* if we were in a transaction then we need to start a
transaction on this new partition, otherwise we'll get a
transaction mismatch when we end the transaction */
if (data->in_transaction) {
- if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
+ if (ldb_module_flags(ldb) & LDB_FLG_ENABLE_TRACING) {
ldb_debug(ldb, LDB_DEBUG_TRACE, "partition_start_trans() -> %s (new partition)",
ldb_dn_get_linearized((*partition)->ctrl->dn));
}
req = talloc_zero(NULL, struct ldb_request);
if (req == NULL) {
- ldb_oom(ldb);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
req->operation = LDB_REQ_REGISTER_PARTITION;
req->handle = ldb_handle_new(req, ldb);
if (req->handle == NULL) {
talloc_free(req);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_operr(ldb);
}
ret = ldb_request(ldb, req);
static int add_partition_to_data(struct ldb_context *ldb, struct partition_private_data *data,
struct dsdb_partition *partition)
{
- int i, ret;
+ unsigned int i;
+ int ret;
+
/* Count the partitions */
for (i=0; data->partitions && data->partitions[i]; i++) { /* noop */};
/* Add partition to list of partitions */
data->partitions = talloc_realloc(data, data->partitions, struct dsdb_partition *, i + 2);
if (!data->partitions) {
- ldb_oom(ldb);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
data->partitions[i] = talloc_steal(data->partitions, partition);
data->partitions[i+1] = NULL;
/* Sort again (should use binary insert) */
- qsort(data->partitions, i+1,
- sizeof(*data->partitions), partition_sort_compare);
+ TYPESAFE_QSORT(data->partitions, i+1, partition_sort_compare);
ret = partition_register(ldb, partition->ctrl);
if (ret != LDB_SUCCESS) {
}
int partition_reload_if_required(struct ldb_module *module,
- struct partition_private_data *data)
+ struct partition_private_data *data,
+ struct ldb_request *parent)
{
uint64_t seq;
- int ret, i;
+ int ret;
+ unsigned int i;
struct ldb_context *ldb = ldb_module_get_ctx(module);
struct ldb_message *msg;
struct ldb_message_element *partition_attributes;
mem_ctx = talloc_new(data);
if (!mem_ctx) {
- ldb_oom(ldb);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
ret = partition_primary_sequence_number(module, mem_ctx, LDB_SEQ_HIGHEST_SEQ, &seq);
return LDB_SUCCESS;
}
- ret = partition_reload_metadata(module, data, mem_ctx, &msg);
+ ret = partition_reload_metadata(module, data, mem_ctx, &msg, parent);
if (ret != LDB_SUCCESS) {
talloc_free(mem_ctx);
return ret;
partition_attributes = ldb_msg_find_element(msg, "partition");
for (i=0; partition_attributes && i < partition_attributes->num_values; i++) {
- int j;
+ unsigned int j;
bool new_partition = true;
- const char *filename_base = NULL;
+ const char *filename = NULL;
DATA_BLOB dn_blob;
struct ldb_dn *dn;
struct dsdb_partition *partition;
const char *no_attrs[] = { NULL };
for (j=0; data->partitions && data->partitions[j]; j++) {
- DATA_BLOB casefold = data_blob_string_const(ldb_dn_get_casefold(data->partitions[j]->ctrl->dn));
- if (data_blob_cmp(&casefold, &partition_attributes->values[i]) == 0) {
+ if (data_blob_cmp(&data->partitions[j]->orig_record, &partition_attributes->values[i]) == 0) {
new_partition = false;
break;
}
(strncmp((const char *)&dn_blob.data[dn_blob.length-4], ".ldb", 4) == 0)) {
/* Look for DN:filename.ldb */
- char *p = strchr((const char *)dn_blob.data, ':');
+ char *p = strrchr((const char *)dn_blob.data, ':');
if (!p) {
ldb_asprintf_errstring(ldb,
- "partition_init: invalid DN in attempting to parse old-style partition record: %s", (const char *)dn_blob.data);
+ "partition_init: invalid DN in attempting to parse partition record: %s", (const char *)dn_blob.data);
talloc_free(mem_ctx);
return LDB_ERR_CONSTRAINT_VIOLATION;
}
- filename_base = p+1;
-
- /* Trim off the .ldb */
- dn_blob.data[dn_blob.length-4] = '\0';
+ filename = p+1;
/* Now trim off the filename */
dn_blob.length = ((uint8_t *)p - dn_blob.data);
return LDB_ERR_CONSTRAINT_VIOLATION;
}
- if (!filename_base) {
- filename_base = ldb_dn_get_linearized(dn);
+ /* Now do a slow check with the DN compare */
+ for (j=0; data->partitions && data->partitions[j]; j++) {
+ if (ldb_dn_compare(dn, data->partitions[j]->ctrl->dn) == 0) {
+ new_partition = false;
+ break;
+ }
+ }
+ if (new_partition == false) {
+ continue;
+ }
+
+ if (!filename) {
+ char *base64_dn = NULL;
+ const char *p;
+ for (p = ldb_dn_get_linearized(dn); *p; p++) {
+ /* We have such a strict check because I don't want shell metacharacters in the file name, nor ../ */
+ if (!(isalnum(*p) || *p == ' ' || *p == '=' || *p == ',')) {
+ break;
+ }
+ }
+ if (*p) {
+ base64_dn = ldb_base64_encode(data, ldb_dn_get_linearized(dn), strlen(ldb_dn_get_linearized(dn)));
+ filename = talloc_asprintf(mem_ctx, "%s.ldb", base64_dn);
+ } else {
+ filename = talloc_asprintf(mem_ctx, "%s.ldb", ldb_dn_get_linearized(dn));
+ }
}
/* We call ldb_dn_get_linearized() because the DN in
* correctly. We don't want to mess that up as the
* schema isn't loaded yet */
ret = new_partition_from_dn(ldb, data, data->partitions, dn,
- filename_base,
+ filename,
&partition);
if (ret != LDB_SUCCESS) {
talloc_free(mem_ctx);
return ret;
}
+ talloc_steal(partition, partition_attributes->values[i].data);
+ partition->orig_record = partition_attributes->values[i];
+
/* Get the 'correct' case of the partition DNs from the database */
ret = dsdb_module_search_dn(partition->module, data, &dn_res,
- dn, no_attrs);
+ dn, no_attrs,
+ DSDB_FLAG_NEXT_MODULE, parent);
if (ret == LDB_SUCCESS) {
talloc_free(partition->ctrl->dn);
partition->ctrl->dn = talloc_steal(partition->ctrl, dn_res->msgs[0]->dn);
struct partition_private_data *data,
struct dsdb_partition *partition)
{
- int i, ret;
+ unsigned int i;
+ int ret;
/* for each replicate, copy from main partition. If we get an error, we report it up the chain */
for (i=0; data->replicate && data->replicate[i]; i++) {
struct ldb_result *replicate_res;
struct ldb_request *add_req;
ret = dsdb_module_search_dn(module, last_req, &replicate_res,
data->replicate[i],
- NULL);
+ NULL,
+ DSDB_FLAG_NEXT_MODULE, NULL);
if (ret == LDB_ERR_NO_SUCH_OBJECT) {
continue;
}
ret = ldb_build_add_req(&add_req, ldb, replicate_res,
replicate_res->msgs[0], NULL, NULL,
ldb_op_default_callback, last_req);
+ LDB_REQ_SET_LOCATION(add_req);
last_req = add_req;
if (ret != LDB_SUCCESS) {
/* return directly, this is a very unlikely error */
/* Build del request */
ret = ldb_build_del_req(&del_req, ldb, replicate_res, replicate_res->msgs[0]->dn, NULL, NULL,
ldb_op_default_callback, last_req);
+ LDB_REQ_SET_LOCATION(del_req);
last_req = del_req;
if (ret != LDB_SUCCESS) {
/* return directly, this is a very unlikely error */
/* Build add request */
ret = ldb_build_add_req(&add_req, ldb, replicate_res, replicate_res->msgs[0], NULL, NULL,
ldb_op_default_callback, last_req);
+ LDB_REQ_SET_LOCATION(add_req);
last_req = add_req;
if (ret != LDB_SUCCESS) {
/* return directly, this is a very unlikely error */
* instanceType */
int partition_create(struct ldb_module *module, struct ldb_request *req)
{
- int i, ret;
+ unsigned int i;
+ int ret;
struct ldb_context *ldb = ldb_module_get_ctx(module);
struct ldb_request *mod_req, *last_req = req;
struct ldb_message *mod_msg;
struct dsdb_create_partition_exop *ex_op = talloc_get_type(req->op.extended.data, struct dsdb_create_partition_exop);
struct ldb_dn *dn = ex_op->new_dn;
- data = talloc_get_type(module->private_data, struct partition_private_data);
+ data = talloc_get_type(ldb_module_get_private(module), struct partition_private_data);
if (!data) {
/* We are not going to create a partition before we are even set up */
return LDB_ERR_UNWILLING_TO_PERFORM;
}
if (!partition) {
+ char *filename;
+ char *partition_record;
new_partition = true;
mod_msg = ldb_msg_new(req);
if (!mod_msg) {
- ldb_oom(ldb);
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_oom(ldb);
}
mod_msg->dn = ldb_dn_new(mod_msg, ldb, DSDB_PARTITION_DN);
casefold_dn = ldb_dn_get_casefold(dn);
- ret = ldb_msg_add_string(mod_msg, DSDB_PARTITION_ATTR, casefold_dn);
+ {
+ char *escaped;
+ const char *p, *sam_name;
+ sam_name = strrchr((const char *)ldb_get_opaque(ldb, "ldb_url"), '/');
+ if (!sam_name) {
+ return ldb_operr(ldb);
+ }
+ sam_name++;
+
+ for (p = casefold_dn; *p; p++) {
+ /* We have such a strict check because
+ * I don't want shell metacharacters
+ * in the file name, nor ../, but I do
+ * want it to be easily typed if SAFE
+ * to do so */
+ if (!(isalnum(*p) || *p == ' ' || *p == '=' || *p == ',')) {
+ break;
+ }
+ }
+ if (*p) {
+ escaped = rfc1738_escape_part(mod_msg, casefold_dn);
+ if (!escaped) {
+ return ldb_oom(ldb);
+ }
+ filename = talloc_asprintf(mod_msg, "%s.d/%s.ldb", sam_name, escaped);
+ talloc_free(escaped);
+ } else {
+ filename = talloc_asprintf(mod_msg, "%s.d/%s.ldb", sam_name, casefold_dn);
+ }
+
+ if (!filename) {
+ return ldb_oom(ldb);
+ }
+ }
+ partition_record = talloc_asprintf(mod_msg, "%s:%s", casefold_dn, filename);
+
+ ret = ldb_msg_add_steal_string(mod_msg, DSDB_PARTITION_ATTR, partition_record);
if (ret != LDB_SUCCESS) {
return ret;
}
/* Perform modify on @PARTITION record */
ret = ldb_build_mod_req(&mod_req, ldb, req, mod_msg, NULL, NULL,
ldb_op_default_callback, req);
-
+ LDB_REQ_SET_LOCATION(mod_req);
if (ret != LDB_SUCCESS) {
return ret;
}
}
/* Make a partition structure for this new partition, so we can copy in the template structure */
- ret = new_partition_from_dn(ldb, data, req, ldb_dn_copy(req, dn), casefold_dn, &partition);
+ ret = new_partition_from_dn(ldb, data, req, ldb_dn_copy(req, dn), filename, &partition);
if (ret != LDB_SUCCESS) {
return ret;
}
+ talloc_steal(partition, partition_record);
+ partition->orig_record = data_blob_string_const(partition_record);
}
ret = new_partition_set_replicated_metadata(ldb, module, last_req, data, partition);
{
int ret;
TALLOC_CTX *mem_ctx = talloc_new(module);
-
+ struct ldb_context *ldb = ldb_module_get_ctx(module);
struct partition_private_data *data;
if (!mem_ctx) {
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_operr(ldb);
}
data = talloc_zero(mem_ctx, struct partition_private_data);
if (data == NULL) {
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_operr(ldb);
}
+ /* When used from Samba4, this message is set by the samba4
+ * module, as a fixed value not read from the DB. This avoids
+ * listing modules in the DB */
+ data->forced_module_msg = talloc_get_type(
+ ldb_get_opaque(ldb,
+ DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME),
+ struct ldb_message);
+
/* This loads the partitions */
- ret = partition_reload_if_required(module, data);
+ ret = partition_reload_if_required(module, data, NULL);
if (ret != LDB_SUCCESS) {
return ret;
}
- module->private_data = talloc_steal(module, data);
+ ldb_module_set_private(module, talloc_steal(module, data));
talloc_free(mem_ctx);
ret = ldb_mod_register_control(module, LDB_CONTROL_DOMAIN_SCOPE_OID);
if (ret != LDB_SUCCESS) {
- ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
+ ldb_debug(ldb, LDB_DEBUG_ERROR,
"partition: Unable to register control with rootdse!\n");
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_operr(ldb);
}
ret = ldb_mod_register_control(module, LDB_CONTROL_SEARCH_OPTIONS_OID);
if (ret != LDB_SUCCESS) {
- ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
+ ldb_debug(ldb, LDB_DEBUG_ERROR,
"partition: Unable to register control with rootdse!\n");
- return LDB_ERR_OPERATIONS_ERROR;
+ return ldb_operr(ldb);
}
return ldb_next_init(module);