1
0
mirror of https://github.com/samba-team/samba.git synced 2025-11-27 08:23:49 +03:00

r5585: LDB interfaces change:

changes:
- ldb_wrap disappears from code and become a private structure of db_wrap.c
  thanks to our move to talloc in ldb code, we do not need to expose it anymore

- removal of ldb_close() function form the code
  thanks to our move to talloc in ldb code, we do not need it anymore
  use talloc_free() to close and free an ldb database

- some minor updates to ldb modules code to cope with the change and fix some
  bugs I found out during the process
This commit is contained in:
Simo Sorce
2005-02-27 11:35:47 +00:00
committed by Gerald (Jerry) Carter
parent e77a070c84
commit d58be9e74b
30 changed files with 438 additions and 428 deletions

View File

@@ -69,7 +69,7 @@ struct ldb_context *ldb_connect(const char *url, unsigned int flags,
}
if (ldb_load_modules(ldb_ctx, options) != 0) {
ldb_close(ldb_ctx);
talloc_free(ldb_ctx);
errno = EINVAL;
return NULL;
}
@@ -77,15 +77,6 @@ struct ldb_context *ldb_connect(const char *url, unsigned int flags,
return ldb_ctx;
}
/*
close the connection to the database
*/
int ldb_close(struct ldb_context *ldb)
{
return ldb->modules->ops->close(ldb->modules);
}
/*
search the database given a LDAP-like search expression

View File

@@ -41,6 +41,10 @@
#include <sys/stat.h>
#include <unistd.h>
#ifdef HAVE_DLOPEN_DISABLED
#include <dlfcn.h>
#endif
#define LDB_MODULE_PREFIX "modules"
#define LDB_MODULE_PREFIX_LEN 7
#define LDB_MODULE_SEP ':'
@@ -49,14 +53,15 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
{
struct ldb_module *current;
char **modules;
char *p, *q;
int pn, i;
int mnum, i;
/* find out which modules we are requested to activate */
modules = NULL;
pn = 0;
mnum = 0;
if (options) {
char *q, *p;
for (i = 0; options[i] != NULL; i++) {
if (strncmp(options[i], LDB_MODULE_PREFIX,
LDB_MODULE_PREFIX_LEN) == 0) {
@@ -68,13 +73,13 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
do {
*p = '\0';
q = p + 1;
pn++;
modules = talloc_realloc(ldb, modules, char *, pn);
mnum++;
modules = talloc_realloc(ldb, modules, char *, mnum);
if (!modules) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_load_modules()\n");
return -1;
}
modules[pn - 1] = q;
modules[mnum - 1] = q;
} while ((p = strchr(q, LDB_MODULE_SEP)));
}
}
@@ -83,9 +88,10 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
if (!modules && strcmp("ldap", ldb->modules->ops->name)) {
/* no modules in the options, look for @MODULES in the
db (not for ldap) */
int ret, j, k;
const char * const attrs[] = { "@MODULE" , NULL};
int ret;
const char * const attrs[] = { "@LIST" , NULL};
struct ldb_message **msg = NULL;
char *modstr, *c, *p;
ret = ldb_search(ldb, "", LDB_SCOPE_BASE, "dn=@MODULES", attrs, &msg);
if (ret == 0) {
@@ -100,6 +106,7 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
return -1;
}
/*
for (j = 0; j < msg[0]->num_elements; j++) {
for (k = 0; k < msg[0]->elements[j].num_values; k++) {
pn++;
@@ -115,21 +122,47 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
}
}
}
*/
modstr = msg[0]->elements[0].values[0].data;
for (c = modstr, mnum = 0; c != NULL; mnum++) {
c = strchr(c, ',');
if (c != NULL) {
c++;
if (*c == '\0') { /* avoid failing if the modules string lasts with ',' */
break;
}
}
}
modules = talloc_array(ldb, char *, mnum);
if ( ! modules ) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_load_modules()\n");
return -1;
}
for (p = c = modstr, i = 0; mnum > i; i++) {
c = strchr(p, ',');
if (c) {
*c = '\0';
}
/* modules are seeked in inverse order. Lets place them as an admin would think the right order is */
modules[mnum - i - 1] = talloc_strdup(modules, p);
p = c + 1;
}
}
talloc_free(msg);
}
if (modules) {
for (i = 0; i < pn; i++) {
if (strcmp(modules[i], "timestamps") == 0) {
current = timestamps_module_init(ldb, options);
if (!current) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
return -1;
}
DLIST_ADD(ldb->modules, current);
continue;
}
for (i = 0; i < mnum; i++) {
#ifdef HAVE_DLOPEN_DISABLED
void *handle;
ldb_module_init_function init;
struct stat st;
char *filename;
const char *errstr;
#endif
if (strcmp(modules[i], "schema") == 0) {
current = schema_module_init(ldb, options);
@@ -141,19 +174,39 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
continue;
}
#ifdef HAVE_DLOPEN_DISABLED
{
void *handle;
ldb_module_init_function init;
struct stat st;
const char *errstr;
if (strcmp(modules[i], "timestamps") == 0) {
current = timestamps_module_init(ldb, options);
if (!current) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
return -1;
}
DLIST_ADD(ldb->modules, current);
continue;
}
if (stat(modules[i], &st) < 0) {
if (strcmp(modules[i], "samldb") == 0) {
current = samldb_module_init(ldb, options);
if (!current) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
return -1;
}
DLIST_ADD(ldb->modules, current);
continue;
}
#ifdef HAVE_DLOPEN_DISABLED
filename = talloc_asprintf(ldb, "%s.so", modules[i]);
if (!filename) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "Talloc failed!\n");
return -1;
}
if (stat(filename, &st) < 0) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
return -1;
}
handle = dlopen(modules[i], RTLD_LAZY);
handle = dlopen(filename, RTLD_LAZY);
if (!handle) {
ldb_debug(ldb, LDB_DEBUG_FATAL, "Error loading module %s [%s]\n", modules[i], dlerror());
@@ -174,10 +227,9 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
return -1;
}
DLIST_ADD(ldb->modules, current);
}
#else
ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
return -1;
ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
return -1;
#endif
}
}
@@ -188,13 +240,6 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
/*
helper functions to call the next module in chain
*/
int ldb_next_close(struct ldb_module *module)
{
if (!module->next) {
return -1;
}
return module->next->ops->close(module->next);
}
int ldb_next_search(struct ldb_module *module,
const char *base,

View File

@@ -158,12 +158,6 @@ struct ldb_debug_ops {
struct ldb_context *ldb_connect(const char *url, unsigned int flags,
const char *options[]);
/*
close the connection to the database
*/
int ldb_close(struct ldb_context *ldb);
/*
search the database given a LDAP-like search expression

View File

@@ -55,7 +55,6 @@ struct ldb_module {
*/
struct ldb_module_ops {
const char *name;
int (*close)(struct ldb_module *);
int (*search)(struct ldb_module *, const char *, enum ldb_scope,
const char *, const char * const [], struct ldb_message ***);
int (*search_free)(struct ldb_module *, struct ldb_message **);
@@ -68,9 +67,6 @@ struct ldb_module_ops {
const char * (*errstring)(struct ldb_module *);
};
/* the modules init function */
typedef struct ldb_module *(*ldb_module_init_function)(void);
/*
every ldb connection is started by establishing a ldb_context
*/
@@ -82,10 +78,12 @@ struct ldb_context {
struct ldb_debug_ops debug_ops;
};
/* the modules init function */
typedef struct ldb_module *(*ldb_module_init_function)(struct ldb_context *ldb, const char *options[]);
/* The following definitions come from lib/ldb/common/ldb_modules.c */
int ldb_load_modules(struct ldb_context *ldb, const char *options[]);
int ldb_next_close(struct ldb_module *module);
int ldb_next_search(struct ldb_module *module,
const char *base,
enum ldb_scope scope,

View File

@@ -67,16 +67,6 @@ static const char *lldb_option_find(const struct lldb_private *lldb, const char
}
#endif
/*
close/free the connection
*/
static int lldb_close(struct ldb_module *module)
{
struct ldb_context *ldb = module->ldb;
talloc_free(ldb);
return 0;
}
/*
rename a record
*/
@@ -468,7 +458,6 @@ static const char *lldb_errstring(struct ldb_module *module)
static const struct ldb_module_ops lldb_ops = {
"ldap",
lldb_close,
lldb_search,
lldb_search_free,
lldb_add,

View File

@@ -780,16 +780,6 @@ failed:
return -1;
}
/*
close database
*/
static int ltdb_close(struct ldb_module *module)
{
struct ldb_context *ldb = module->ldb;
talloc_free(ldb);
return 0;
}
/*
return extended error information
@@ -806,7 +796,6 @@ static const char *ltdb_errstring(struct ldb_module *module)
static const struct ldb_module_ops ltdb_ops = {
"tdb",
ltdb_close,
ltdb_search,
ltdb_search_free,
ltdb_add,

View File

@@ -64,8 +64,6 @@ formatted input
dit(bf(ldb_connect(3))) connect to a ldb backend
dit(bf(ldb_close(3))) close a connection to a ldb backend
dit(bf(ldb_search(3))) perform a database search
dit(bf(ldb_search_free(3))) free the results of a ldb_search

View File

@@ -297,12 +297,6 @@ static int get_attr_list_recursive(struct ldb_module *module, struct schema_stru
return 0;
}
/* close */
static int schema_close(struct ldb_module *module)
{
return ldb_next_close(module);
}
/* search */
static int schema_search(struct ldb_module *module, const char *base,
enum ldb_scope scope, const char *expression,
@@ -371,18 +365,6 @@ static int schema_add_record(struct ldb_module *module, const struct ldb_message
return -1;
}
/* check we are not trying to delete a required attribute */
/* TODO: consider multivalued attrs */
if ((attr->flags & SCHEMA_FLAG_MOD_DELETE) != 0) {
ldb_debug(module->ldb, LDB_DEBUG_ERROR,
"Trying to delete the required attribute %s.\n",
attr->name);
data->error_string = "Objectclass violation, a required attribute cannot be removed";
talloc_free(entry_structs);
return -1;
}
/* mark the attribute as checked */
attr->flags = SCHEMA_FLAG_CHECKED;
}
@@ -477,6 +459,18 @@ static int schema_modify_record(struct ldb_module *module, const struct ldb_mess
return -1;
}
/* check we are not trying to delete a required attribute */
/* TODO: consider multivalued attrs */
if ((attr->flags & SCHEMA_FLAG_MOD_DELETE) != 0) {
ldb_debug(module->ldb, LDB_DEBUG_ERROR,
"Trying to delete the required attribute %s.\n",
attr->name);
data->error_string = "Objectclass violation, a required attribute cannot be removed";
talloc_free(entry_structs);
return -1;
}
/* mark the attribute as checked */
attr->flags = SCHEMA_FLAG_CHECKED;
}
@@ -544,9 +538,15 @@ static const char *schema_errstring(struct ldb_module *module)
return ldb_next_errstring(module);
}
static int schema_destructor(void *module_ctx)
{
struct ldb_module *ctx = module_ctx;
/* put your clean-up functions here */
return 0;
}
static const struct ldb_module_ops schema_ops = {
"schema",
schema_close,
schema_search,
schema_search_free,
schema_add_record,
@@ -584,5 +584,7 @@ struct ldb_module *schema_module_init(struct ldb_context *ldb, const char *optio
ctx->prev = ctx->next = NULL;
ctx->ops = &schema_ops;
talloc_set_destructor (ctx, schema_destructor);
return ctx;
}

View File

@@ -36,12 +36,6 @@
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
/* close */
static int skel_close(struct ldb_module *module)
{
return ldb_next_close(module);
}
/* search */
static int skel_search(struct ldb_module *module, const char *base,
enum ldb_scope scope, const char *expression,
@@ -98,9 +92,15 @@ static const char *skel_errstring(struct ldb_module *module)
return ldb_next_errstring(module);
}
static int skel_destructor(void *module_ctx)
{
struct ldb_module *ctx = module_ctx;
/* put your clean-up functions here */
return 0;
}
static const struct ldb_module_ops skel_ops = {
"skel",
skel_close,
skel_search,
skel_search_free,
skel_add_record,
@@ -129,5 +129,7 @@ struct ldb_module *skel_plugin_init(struct ldb_context *ldb, const char *options
ctx->private_data = NULL;
ctx->ops = &skel_ops;
talloc_set_destructor (ctx, skel_destructor);
return ctx;
}

View File

@@ -41,12 +41,6 @@ struct private_data {
const char *error_string;
};
static int timestamps_close(struct ldb_module *module)
{
ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_close\n");
return ldb_next_close(module);
}
static int timestamps_search(struct ldb_module *module, const char *base,
enum ldb_scope scope, const char *expression,
const char * const *attrs, struct ldb_message ***res)
@@ -106,41 +100,43 @@ static int timestamps_add_record(struct ldb_module *module, const struct ldb_mes
ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_add_record\n");
if (msg->dn[0] != '@') { /* do not manipulate our control entries */
timeval = time(NULL);
tm = gmtime(&timeval);
if (!tm) {
return -1;
}
msg2 = talloc(module, struct ldb_message);
if (!msg2) {
return -1;
}
/* formatted like: 20040408072012.0Z */
timestr = talloc_asprintf(msg2, "%04u%02u%02u%02u%02u%02u.0Z",
tm->tm_year+1900, tm->tm_mon+1,
tm->tm_mday, tm->tm_hour, tm->tm_min,
tm->tm_sec);
if (!timestr) {
return -1;
}
msg2->dn = msg->dn;
msg2->num_elements = msg->num_elements;
msg2->private_data = msg->private_data;
msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
for (i = 0; i < msg2->num_elements; i++) {
msg2->elements[i] = msg->elements[i];
}
add_time_element(module, msg2, "createTimestamp", timestr, LDB_FLAG_MOD_ADD);
add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_ADD);
add_time_element(module, msg2, "whenCreated", timestr, LDB_FLAG_MOD_ADD);
add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_ADD);
if (msg->dn[0] == '@') { /* do not manipulate our control entries */
return ldb_next_add_record(module, msg);
}
timeval = time(NULL);
tm = gmtime(&timeval);
if (!tm) {
return -1;
}
msg2 = talloc(module, struct ldb_message);
if (!msg2) {
return -1;
}
/* formatted like: 20040408072012.0Z */
timestr = talloc_asprintf(msg2, "%04u%02u%02u%02u%02u%02u.0Z",
tm->tm_year+1900, tm->tm_mon+1,
tm->tm_mday, tm->tm_hour, tm->tm_min,
tm->tm_sec);
if (!timestr) {
return -1;
}
msg2->dn = msg->dn;
msg2->num_elements = msg->num_elements;
msg2->private_data = msg->private_data;
msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
for (i = 0; i < msg2->num_elements; i++) {
msg2->elements[i] = msg->elements[i];
}
add_time_element(module, msg2, "createTimestamp", timestr, LDB_FLAG_MOD_ADD);
add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_ADD);
add_time_element(module, msg2, "whenCreated", timestr, LDB_FLAG_MOD_ADD);
add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_ADD);
if (msg2) {
ret = ldb_next_add_record(module, msg2);
talloc_free(msg2);
@@ -162,40 +158,42 @@ static int timestamps_modify_record(struct ldb_module *module, const struct ldb_
ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_modify_record\n");
if (msg->dn[0] != '@') { /* do not manipulate our control entries */
timeval = time(NULL);
tm = gmtime(&timeval);
if (!tm) {
return -1;
}
msg2 = talloc(module, struct ldb_message);
if (!msg2) {
return -1;
}
/* formatted like: 20040408072012.0Z */
timestr = talloc_asprintf(msg2,
"%04u%02u%02u%02u%02u%02u.0Z",
tm->tm_year+1900, tm->tm_mon+1,
tm->tm_mday, tm->tm_hour, tm->tm_min,
tm->tm_sec);
if (!timestr) {
return -1;
}
msg2->dn = msg->dn;
msg2->num_elements = msg->num_elements;
msg2->private_data = msg->private_data;
msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
for (i = 0; i < msg2->num_elements; i++) {
msg2->elements[i] = msg->elements[i];
}
add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_REPLACE);
add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_REPLACE);
if (msg->dn[0] == '@') { /* do not manipulate our control entries */
return ldb_next_modify_record(module, msg);
}
timeval = time(NULL);
tm = gmtime(&timeval);
if (!tm) {
return -1;
}
msg2 = talloc(module, struct ldb_message);
if (!msg2) {
return -1;
}
/* formatted like: 20040408072012.0Z */
timestr = talloc_asprintf(msg2,
"%04u%02u%02u%02u%02u%02u.0Z",
tm->tm_year+1900, tm->tm_mon+1,
tm->tm_mday, tm->tm_hour, tm->tm_min,
tm->tm_sec);
if (!timestr) {
return -1;
}
msg2->dn = msg->dn;
msg2->num_elements = msg->num_elements;
msg2->private_data = msg->private_data;
msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
for (i = 0; i < msg2->num_elements; i++) {
msg2->elements[i] = msg->elements[i];
}
add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_REPLACE);
add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_REPLACE);
if (msg2) {
ret = ldb_next_modify_record(module, msg2);
talloc_free(msg2);
@@ -247,9 +245,15 @@ static const char *timestamps_errstring(struct ldb_module *module)
return ldb_next_errstring(module);
}
static int timestamps_destructor(void *module_ctx)
{
struct ldb_module *ctx = module_ctx;
/* put your clean-up functions here */
return 0;
}
static const struct ldb_module_ops timestamps_ops = {
"timestamps",
timestamps_close,
timestamps_search,
timestamps_search_free,
timestamps_add_record,
@@ -288,5 +292,7 @@ struct ldb_module *timestamps_module_init(struct ldb_context *ldb, const char *o
ctx->prev = ctx->next = NULL;
ctx->ops = &timestamps_ops;
talloc_set_destructor (ctx, timestamps_destructor);
return ctx;
}

View File

@@ -153,7 +153,7 @@ static int process_file(struct ldb_context *ldb, FILE *f)
}
}
ldb_close(ldb);
talloc_free(ldb);
printf("Added %d records with %d failures\n", count, failures);

View File

@@ -146,7 +146,7 @@ static void usage(void)
}
}
ldb_close(ldb);
talloc_free(ldb);
return 0;
}

View File

@@ -434,6 +434,6 @@ static void usage(void)
}
}
ldb_close(ldb);
talloc_free(ldb);
return 0;
}

View File

@@ -156,7 +156,7 @@ static int process_file(struct ldb_context *ldb, FILE *f)
}
}
ldb_close(ldb);
talloc_free(ldb);
printf("Modified %d records with %d failures\n", count, failures);

View File

@@ -112,7 +112,7 @@ static void usage(void)
argv[0], argv[1], ldb_errstring(ldb));
}
ldb_close(ldb);
talloc_free(ldb);
return ret;
}

View File

@@ -176,6 +176,6 @@ static int do_search(struct ldb_context *ldb,
ret = do_search(ldb, basedn, scope, argv[0], attrs);
}
ldb_close(ldb);
talloc_free(ldb);
return ret;
}

View File

@@ -323,8 +323,8 @@ static void start_test_index(struct ldb_context **ldb)
exit(1);
}
if (ldb_close(*ldb) != 0) {
printf("ldb_close failed - %s\n", ldb_errstring(*ldb));
if (talloc_free(*ldb) != 0) {
printf("failed to free/close ldb database");
exit(1);
}
@@ -427,7 +427,7 @@ static void usage(void)
start_test_index(&ldb);
ldb_close(ldb);
talloc_free(ldb);
return 0;
}