summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-06-18 07:42:21 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:18:24 -0500
commited3d8091ce2b2014350a2f7f22202dde6846a130 (patch)
tree8c1d6ee74907c2c0c2e82b9476ad1db08d7a2179
parentd4d6d0d2e54510690fa3f868ad02625bc24e5b9d (diff)
downloadsamba-ed3d8091ce2b2014350a2f7f22202dde6846a130.tar.gz
samba-ed3d8091ce2b2014350a2f7f22202dde6846a130.tar.bz2
samba-ed3d8091ce2b2014350a2f7f22202dde6846a130.zip
r7709: - convert ldb to use popt, so that it can interact with the samba
cmdline credentials code (which will be done soon) - added a ldb_init() call, and changed ldb_connect() to take a ldb context. This allows for much better error handling in ldb_connect(), and also made the popt conversion easier - fixed up all the existing backends with the new syntax - improved error handling in *_connect() - fixed a crash bug in the new case_fold_required() code - ensured that ltdb_rename() and all ltdb_search() paths get the read lock - added a ldb_oom() macro to make it easier to report out of memory situations in ldb code (This used to be commit f648fdf187669d6d87d01dd4e786b03cd420f220)
-rw-r--r--source4/lib/ldb/Makefile.in34
-rw-r--r--source4/lib/ldb/common/ldb.c48
-rw-r--r--source4/lib/ldb/common/ldb_utf8.c2
-rw-r--r--source4/lib/ldb/common/util.c57
-rw-r--r--source4/lib/ldb/config.mk23
-rw-r--r--source4/lib/ldb/configure.in3
-rw-r--r--source4/lib/ldb/include/ldb.h7
-rw-r--r--source4/lib/ldb/include/ldb_private.h33
-rw-r--r--source4/lib/ldb/ldb_ildap/ldb_ildap.c28
-rw-r--r--source4/lib/ldb/ldb_ldap/ldb_ldap.c82
-rw-r--r--source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c23
-rw-r--r--source4/lib/ldb/ldb_tdb/ldb_search.c38
-rw-r--r--source4/lib/ldb/ldb_tdb/ldb_tdb.c43
-rw-r--r--source4/lib/ldb/ldb_tdb/ldb_tdb.h2
-rwxr-xr-xsource4/lib/ldb/tests/test-extended.sh4
-rwxr-xr-xsource4/lib/ldb/tests/test-generic.sh4
-rw-r--r--source4/lib/ldb/tools/ldbadd.c78
-rw-r--r--source4/lib/ldb/tools/ldbdel.c64
-rw-r--r--source4/lib/ldb/tools/ldbedit.c103
-rw-r--r--source4/lib/ldb/tools/ldbmodify.c59
-rw-r--r--source4/lib/ldb/tools/ldbrename.c58
-rw-r--r--source4/lib/ldb/tools/ldbsearch.c87
-rw-r--r--source4/lib/ldb/tools/ldbtest.c152
23 files changed, 343 insertions, 689 deletions
diff --git a/source4/lib/ldb/Makefile.in b/source4/lib/ldb/Makefile.in
index 88935b6f3a..a633320d06 100644
--- a/source4/lib/ldb/Makefile.in
+++ b/source4/lib/ldb/Makefile.in
@@ -39,7 +39,7 @@ endif
CFLAGS = $(CFLAGS1) $(GCOV_FLAGS) @CFLAGS@
-LIB_FLAGS=-Llib -lldb $(LDAP_LIBS) $(SQLITE3_LIBS) $(GCOV_LIBS)
+LIB_FLAGS=-Llib -lldb $(LDAP_LIBS) $(SQLITE3_LIBS) $(GCOV_LIBS) @LIBS@
TDB_OBJ=$(TDBDIR)/common/tdb.o $(TDBDIR)/common/spinlock.o
TALLOC_OBJ=$(TALLOCDIR)/talloc.o
@@ -49,7 +49,7 @@ LDB_TDB_OBJ=ldb_tdb/ldb_match.o ldb_tdb/ldb_tdb.o \
ldb_tdb/ldb_cache.o
-COMMON_OBJ=common/ldb.o common/ldb_ldif.o common/util.o \
+COMMON_OBJ=common/ldb.o common/ldb_ldif.o \
common/ldb_parse.o common/ldb_msg.o common/ldb_utf8.o \
common/ldb_debug.o common/ldb_modules.o \
common/ldb_explode_dn.o
@@ -80,26 +80,26 @@ bin:
lib/libldb.a: $(OBJS)
-bin/ldbadd: tools/ldbadd.o $(LIBS)
- $(CC) -o bin/ldbadd tools/ldbadd.o $(LIB_FLAGS)
+bin/ldbadd: tools/ldbadd.o tools/cmdline.o $(LIBS)
+ $(CC) -o bin/ldbadd tools/ldbadd.o tools/cmdline.o $(LIB_FLAGS)
-bin/ldbsearch: tools/ldbsearch.o $(LIBS)
- $(CC) -o bin/ldbsearch tools/ldbsearch.o $(LIB_FLAGS)
+bin/ldbsearch: tools/ldbsearch.o tools/cmdline.o $(LIBS)
+ $(CC) -o bin/ldbsearch tools/ldbsearch.o tools/cmdline.o $(LIB_FLAGS)
-bin/ldbdel: tools/ldbdel.o $(LIBS)
- $(CC) -o bin/ldbdel tools/ldbdel.o $(LIB_FLAGS)
+bin/ldbdel: tools/ldbdel.o tools/cmdline.o $(LIBS)
+ $(CC) -o bin/ldbdel tools/ldbdel.o tools/cmdline.o $(LIB_FLAGS)
-bin/ldbmodify: tools/ldbmodify.o $(LIBS)
- $(CC) -o bin/ldbmodify tools/ldbmodify.o $(LIB_FLAGS)
+bin/ldbmodify: tools/ldbmodify.o tools/cmdline.o $(LIBS)
+ $(CC) -o bin/ldbmodify tools/ldbmodify.o tools/cmdline.o $(LIB_FLAGS)
-bin/ldbedit: tools/ldbedit.o $(LIBS)
- $(CC) -o bin/ldbedit tools/ldbedit.o $(LIB_FLAGS)
+bin/ldbedit: tools/ldbedit.o tools/cmdline.o $(LIBS)
+ $(CC) -o bin/ldbedit tools/ldbedit.o tools/cmdline.o $(LIB_FLAGS)
-bin/ldbrename: tools/ldbrename.o $(LIBS)
- $(CC) -o bin/ldbrename tools/ldbrename.o $(LIB_FLAGS)
+bin/ldbrename: tools/ldbrename.o tools/cmdline.o $(LIBS)
+ $(CC) -o bin/ldbrename tools/ldbrename.o tools/cmdline.o $(LIB_FLAGS)
-bin/ldbtest: tools/ldbtest.o $(LIBS)
- $(CC) -o bin/ldbtest tools/ldbtest.o $(LIB_FLAGS)
+bin/ldbtest: tools/ldbtest.o tools/cmdline.o $(LIBS)
+ $(CC) -o bin/ldbtest tools/ldbtest.o tools/cmdline.o $(LIB_FLAGS)
.SUFFIXES: .1 .2 .3 .yo
@@ -126,7 +126,7 @@ distclean: clean
etags:
etags */*.[ch]
-test-tdb:
+test-tdb: $(BINS)
@echo "STARTING TDB BACKEND TEST"
tests/test-tdb.sh
diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c
index 2d0d09de3e..d2dbac95ea 100644
--- a/source4/lib/ldb/common/ldb.c
+++ b/source4/lib/ldb/common/ldb.c
@@ -37,6 +37,16 @@
#include "ldb/include/ldb_private.h"
/*
+ initialise a ldb context
+ The mem_ctx is optional
+*/
+struct ldb_context *ldb_init(void *mem_ctx)
+{
+ struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
+ return ldb;
+}
+
+/*
connect to a database. The URL can either be one of the following forms
ldb://path
ldapi://path
@@ -46,45 +56,45 @@
the options are passed uninterpreted to the backend, and are
backend specific
*/
-struct ldb_context *ldb_connect(const char *url, unsigned int flags,
- const char *options[])
+int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
{
- struct ldb_context *ldb_ctx = NULL;
+ int ret;
if (strncmp(url, "tdb:", 4) == 0 ||
strchr(url, ':') == NULL) {
- ldb_ctx = ltdb_connect(url, flags, options);
+ ret = ltdb_connect(ldb, url, flags, options);
}
#if HAVE_ILDAP
- if (strncmp(url, "ldap", 4) == 0) {
- ldb_ctx = ildb_connect(url, flags, options);
+ else if (strncmp(url, "ldap", 4) == 0) {
+ ret = ildb_connect(ldb, url, flags, options);
}
#elif HAVE_LDAP
- if (strncmp(url, "ldap", 4) == 0) {
- ldb_ctx = lldb_connect(url, flags, options);
+ else if (strncmp(url, "ldap", 4) == 0) {
+ ret = lldb_connect(ldb, url, flags, options);
}
#endif
-
#if HAVE_SQLITE3
- if (strncmp(url, "sqlite:", 7) == 0) {
+ else if (strncmp(url, "sqlite:", 7) == 0) {
ldb_ctx = lsqlite3_connect(url, flags, options);
}
#endif
+ else {
+ ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'", url);
+ return -1;
+ }
-
- if (!ldb_ctx) {
- errno = EINVAL;
- return NULL;
+ if (ret != 0) {
+ ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'", url);
+ return ret;
}
- if (ldb_load_modules(ldb_ctx, options) != 0) {
- talloc_free(ldb_ctx);
- errno = EINVAL;
- return NULL;
+ if (ldb_load_modules(ldb, options) != 0) {
+ ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'", url);
+ return -1;
}
- return ldb_ctx;
+ return 0;
}
/*
diff --git a/source4/lib/ldb/common/ldb_utf8.c b/source4/lib/ldb/common/ldb_utf8.c
index 35249c4b70..ae35ad81b6 100644
--- a/source4/lib/ldb/common/ldb_utf8.c
+++ b/source4/lib/ldb/common/ldb_utf8.c
@@ -135,7 +135,7 @@ char *ldb_dn_fold(void * mem_ctx,
}
if (*value == 0) goto failed;
- case_fold_required = (* case_fold_attr_fn)(user_data, attr);
+ case_fold_required = case_fold_attr_fn(user_data, attr);
attr = ldb_casefold(tmp_ctx, attr);
if (attr == NULL) goto failed;
diff --git a/source4/lib/ldb/common/util.c b/source4/lib/ldb/common/util.c
index c2fd72acd1..e69de29bb2 100644
--- a/source4/lib/ldb/common/util.c
+++ b/source4/lib/ldb/common/util.c
@@ -1,57 +0,0 @@
-/*
- ldb database library
-
- Copyright (C) Andrew Tridgell 2004
-
- ** NOTE! The following LGPL license applies to the ldb
- ** library. This does NOT imply that all of Samba is released
- ** under the LGPL
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-*/
-
-/*
- * Name: ldb
- *
- * Component: ldb utility functions
- *
- * Description: miscellanous utility functions for ldb
- *
- * Author: Andrew Tridgell
- */
-
-#include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
-
-
-/*
- common code for parsing -o options in ldb tools
-*/
-const char **ldb_options_parse(const char **options, int *ldbopts, const char *arg)
-{
- if (*ldbopts == 0) {
- options = malloc(sizeof(char *) * 2);
- } else {
- options = realloc(options, sizeof(char *)*((*ldbopts)+2));
- }
- if (options == NULL) {
- fprintf(stderr, "Out of memory in options parsing!\n");
- exit(-1);
- }
- options[(*ldbopts)++] = arg;
- options[*ldbopts] = NULL;
- return options;
-}
diff --git a/source4/lib/ldb/config.mk b/source4/lib/ldb/config.mk
index 05255fdec5..4d4dc121e1 100644
--- a/source4/lib/ldb/config.mk
+++ b/source4/lib/ldb/config.mk
@@ -92,12 +92,21 @@ REQUIRED_SUBSYSTEMS = \
################################################
################################################
+# Start SUBSYSTEM LIBLDB_CMDLINE
+[SUBSYSTEM::LIBLDB_CMDLINE]
+OBJ_FILES= \
+ lib/ldb/tools/cmdline.o
+REQUIRED_SUBSYSTEMS = LIBLDB LIBCMDLINE
+# End SUBSYSTEM LIBLDB_CMDLINE
+################################################
+
+################################################
# Start BINARY ldbadd
[BINARY::ldbadd]
OBJ_FILES= \
lib/ldb/tools/ldbadd.o
REQUIRED_SUBSYSTEMS = \
- LIBLDB
+ LIBLDB_CMDLINE
# End BINARY ldbadd
################################################
@@ -107,7 +116,7 @@ REQUIRED_SUBSYSTEMS = \
OBJ_FILES= \
lib/ldb/tools/ldbdel.o
REQUIRED_SUBSYSTEMS = \
- LIBLDB
+ LIBLDB_CMDLINE
# End BINARY ldbdel
################################################
@@ -117,7 +126,7 @@ REQUIRED_SUBSYSTEMS = \
OBJ_FILES= \
lib/ldb/tools/ldbmodify.o
REQUIRED_SUBSYSTEMS = \
- LIBLDB
+ LIBLDB_CMDLINE
# End BINARY ldbmodify
################################################
@@ -127,7 +136,7 @@ REQUIRED_SUBSYSTEMS = \
OBJ_FILES= \
lib/ldb/tools/ldbsearch.o
REQUIRED_SUBSYSTEMS = \
- LIBLDB
+ LIBLDB_CMDLINE
# End BINARY ldbsearch
################################################
@@ -137,7 +146,7 @@ REQUIRED_SUBSYSTEMS = \
OBJ_FILES= \
lib/ldb/tools/ldbedit.o
REQUIRED_SUBSYSTEMS = \
- LIBLDB
+ LIBLDB_CMDLINE
# End BINARY ldbedit
################################################
@@ -147,7 +156,7 @@ REQUIRED_SUBSYSTEMS = \
OBJ_FILES= \
lib/ldb/tools/ldbrename.o
REQUIRED_SUBSYSTEMS = \
- LIBLDB
+ LIBLDB_CMDLINE
# End BINARY ldbrename
################################################
@@ -157,6 +166,6 @@ REQUIRED_SUBSYSTEMS = \
OBJ_FILES= \
lib/ldb/tools/ldbtest.o
REQUIRED_SUBSYSTEMS = \
- LIBLDB
+ LIBLDB_CMDLINE
# End BINARY ldbtest
################################################
diff --git a/source4/lib/ldb/configure.in b/source4/lib/ldb/configure.in
index fcb87cdb3a..7ae339c103 100644
--- a/source4/lib/ldb/configure.in
+++ b/source4/lib/ldb/configure.in
@@ -23,6 +23,9 @@ AC_PATH_PROG(XSLTPROC,xsltproc)
AC_PATH_PROG(GCOV,gcov)
AC_CHECK_HEADERS(stdint.h)
AC_CONFIG_HEADER(include/config.h)
+
+AC_CHECK_LIB(popt, poptGetContext)
+
sinclude(ldap.m4)
WITH_LDAP=$with_ldap_support
AC_SUBST(WITH_LDAP)
diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h
index fabaec5da2..ecfa77f436 100644
--- a/source4/lib/ldb/include/ldb.h
+++ b/source4/lib/ldb/include/ldb.h
@@ -184,6 +184,10 @@ struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s);
char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree);
char *ldb_binary_encode(void *ctx, struct ldb_val val);
+/*
+ initialise a ldb context
+*/
+struct ldb_context *ldb_init(void *mem_ctx);
/*
connect to a database. The URL can either be one of the following forms
@@ -195,8 +199,7 @@ char *ldb_binary_encode(void *ctx, struct ldb_val val);
the options are passed uninterpreted to the backend, and are
backend specific
*/
-struct ldb_context *ldb_connect(const char *url, unsigned int flags,
- const char *options[]);
+int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[]);
/*
search the database given a LDAP-like search expression
diff --git a/source4/lib/ldb/include/ldb_private.h b/source4/lib/ldb/include/ldb_private.h
index 4acdd543f2..76a43cdbc2 100644
--- a/source4/lib/ldb/include/ldb_private.h
+++ b/source4/lib/ldb/include/ldb_private.h
@@ -86,6 +86,11 @@ typedef struct ldb_module *(*ldb_module_init_function)(struct ldb_context *ldb,
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
+/*
+ simplify out of memory handling
+*/
+#define ldb_oom(ldb) ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb out of memory at %s:%d\n", __FILE__, __LINE__)
+
/* The following definitions come from lib/ldb/common/ldb_modules.c */
int ldb_load_modules(struct ldb_context *ldb, const char *options[]);
@@ -114,21 +119,21 @@ void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *
char *ldb_base64_encode(struct ldb_context *ldb, const char *buf, int len);
int ldb_should_b64_encode(const struct ldb_val *val);
-struct ldb_context *ltdb_connect(const char *url,
- unsigned int flags,
- const char *options[]);
-struct ldb_context *lldb_connect(const char *url,
- unsigned int flags,
- const char *options[]);
-struct ldb_context *ildb_connect(const char *url,
- unsigned int flags,
- const char *options[]);
-struct ldb_context *lsqlite3_connect(const char *url,
- unsigned int flags,
- const char *options[]);
+int ltdb_connect(struct ldb_context *ldb, const char *url,
+ unsigned int flags,
+ const char *options[]);
+int lldb_connect(struct ldb_context *ldb, const char *url,
+ unsigned int flags,
+ const char *options[]);
+int ildb_connect(struct ldb_context *ldb,
+ const char *url,
+ unsigned int flags,
+ const char *options[]);
+int lsqlite3_connect(struct ldb_context *ldb,
+ const char *url,
+ unsigned int flags,
+ const char *options[]);
struct ldb_module *timestamps_module_init(struct ldb_context *ldb, const char *options[]);
struct ldb_module *schema_module_init(struct ldb_context *ldb, const char *options[]);
-const char **ldb_options_parse(const char **options, int *ldbopts, const char *arg);
-
#endif
diff --git a/source4/lib/ldb/ldb_ildap/ldb_ildap.c b/source4/lib/ldb/ldb_ildap/ldb_ildap.c
index c35d9de6b1..aa0efee481 100644
--- a/source4/lib/ldb/ldb_ildap/ldb_ildap.c
+++ b/source4/lib/ldb/ldb_ildap/ldb_ildap.c
@@ -349,42 +349,34 @@ static const struct ldb_module_ops ildb_ops = {
/*
connect to the database
*/
-struct ldb_context *ildb_connect(const char *url,
- unsigned int flags,
- const char *options[])
+int ildb_connect(struct ldb_context *ldb, const char *url,
+ unsigned int flags, const char *options[])
{
- struct ldb_context *ldb = NULL;
struct ildb_private *ildb = NULL;
NTSTATUS status;
- ldb = talloc(NULL, struct ldb_context);
- if (!ldb) {
- errno = ENOMEM;
- goto failed;
- }
-
- ldb->debug_ops.debug = NULL;
-
ildb = talloc(ldb, struct ildb_private);
if (!ildb) {
- errno = ENOMEM;
+ ldb_oom(ldb);
goto failed;
}
ildb->ldap = ldap_new_connection(ildb, NULL);
if (!ildb->ldap) {
- errno = ENOMEM;
+ ldb_oom(ldb);
goto failed;
}
status = ldap_connect(ildb->ldap, url);
if (!NT_STATUS_IS_OK(status)) {
+ ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
+ url, ldap_errstr(ildb->ldap, status));
goto failed;
}
ldb->modules = talloc(ldb, struct ldb_module);
if (!ldb->modules) {
- errno = ENOMEM;
+ ldb_oom(ldb);
goto failed;
}
ldb->modules->ldb = ldb;
@@ -392,10 +384,10 @@ struct ldb_context *ildb_connect(const char *url,
ldb->modules->private_data = ildb;
ldb->modules->ops = &ildb_ops;
- return ldb;
+ return 0;
failed:
- talloc_free(ldb);
- return NULL;
+ talloc_free(ildb);
+ return -1;
}
diff --git a/source4/lib/ldb/ldb_ldap/ldb_ldap.c b/source4/lib/ldb/ldb_ldap/ldb_ldap.c
index 16beed58f8..d7f589e2e5 100644
--- a/source4/lib/ldb/ldb_ldap/ldb_ldap.c
+++ b/source4/lib/ldb/ldb_ldap/ldb_ldap.c
@@ -37,36 +37,6 @@
#include "ldb/include/ldb_private.h"
#include "ldb/ldb_ldap/ldb_ldap.h"
-#if 0
-/*
- we don't need this right now, but will once we add some backend
- options
-*/
-
-/*
- find an option in an option list (a null terminated list of strings)
-
- this assumes the list is short. If it ever gets long then we really
- should do this in some smarter way
- */
-static const char *lldb_option_find(const struct lldb_private *lldb, const char *name)
-{
- int i;
- size_t len = strlen(name);
-
- if (!lldb->options) return NULL;
-
- for (i=0;lldb->options[i];i++) {
- if (strncmp(lldb->options[i], name, len) == 0 &&
- lldb->options[i][len] == '=') {
- return &lldb->options[i][len+1];
- }
- }
-
- return NULL;
-}
-#endif
-
/*
rename a record
*/
@@ -494,25 +464,17 @@ static int lldb_destructor(void *p)
/*
connect to the database
*/
-struct ldb_context *lldb_connect(const char *url,
- unsigned int flags,
- const char *options[])
+int lldb_connect(struct ldb_context *ldb,
+ const char *url,
+ unsigned int flags,
+ const char *options[])
{
- struct ldb_context *ldb = NULL;
struct lldb_private *lldb = NULL;
- int i, version = 3;
-
- ldb = talloc(NULL, struct ldb_context);
- if (!ldb) {
- errno = ENOMEM;
- goto failed;
- }
-
- ldb->debug_ops.debug = NULL;
+ int version = 3;
lldb = talloc(ldb, struct lldb_private);
if (!lldb) {
- errno = ENOMEM;
+ ldb_oom(ldb);
goto failed;
}
@@ -521,6 +483,8 @@ struct ldb_context *lldb_connect(const char *url,
lldb->last_rc = ldap_initialize(&lldb->ldap, url);
if (lldb->last_rc != LDAP_SUCCESS) {
+ ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_initialize failed for URL '%s' - %s\n",
+ url, ldap_err2string(lldb->last_rc));
goto failed;
}
@@ -528,12 +492,13 @@ struct ldb_context *lldb_connect(const char *url,
lldb->last_rc = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
if (lldb->last_rc != LDAP_SUCCESS) {
- goto failed;
+ ldb_debug(ldb, LDB_DEBUG_FATAL, "ldap_set_option failed - %s\n",
+ ldap_err2string(lldb->last_rc));
}
ldb->modules = talloc(ldb, struct ldb_module);
if (!ldb->modules) {
- errno = ENOMEM;
+ ldb_oom(ldb);
goto failed;
}
ldb->modules->ldb = ldb;
@@ -541,29 +506,10 @@ struct ldb_context *lldb_connect(const char *url,
ldb->modules->private_data = lldb;
ldb->modules->ops = &lldb_ops;
- if (options) {
- /* take a copy of the options array, so we don't have to rely
- on the caller keeping it around (it might be dynamic) */
- for (i=0;options[i];i++) ;
-
- lldb->options = talloc_array(lldb, char *, i+1);
- if (!lldb->options) {
- goto failed;
- }
-
- for (i=0;options[i];i++) {
- lldb->options[i+1] = NULL;
- lldb->options[i] = talloc_strdup(lldb->options, options[i]);
- if (!lldb->options[i]) {
- goto failed;
- }
- }
- }
-
- return ldb;
+ return 0;
failed:
- talloc_free(ldb);
- return NULL;
+ talloc_free(lldb);
+ return -1;
}
diff --git a/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c b/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c
index 51830db94c..3d36043044 100644
--- a/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c
+++ b/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c
@@ -216,21 +216,16 @@ static const struct ldb_module_ops lsqlite3_ops = {
/*
* connect to the database
*/
-struct ldb_context *
-lsqlite3_connect(const char *url,
- unsigned int flags,
- const char *options[])
+int lsqlite3_connect(struct ldb_context *ldb,
+ const char *url,
+ unsigned int flags,
+ const char *options[])
{
int i;
int ret;
struct ldb_context * ldb = NULL;
struct lsqlite3_private * lsqlite3 = NULL;
- ldb = talloc(NULL, struct ldb_context);
- if (!ldb) {
- goto failed;
- }
-
lsqlite3 = talloc(ldb, struct lsqlite3_private);
if (!lsqlite3) {
goto failed;
@@ -279,14 +274,14 @@ lsqlite3_connect(const char *url,
}
}
- return ldb;
+ return 0;
failed:
if (lsqlite3->sqlite != NULL) {
(void) sqlite3_close(lsqlite3->sqlite);
}
- talloc_free(ldb);
- return NULL;
+ talloc_free(lsqlite3);
+ return -1;
}
@@ -1204,7 +1199,9 @@ destructor(void *p)
{
struct lsqlite3_private * lsqlite3 = p;
- (void) sqlite3_close(lsqlite3->sqlite);
+ if (lsqlite3->sqlite) {
+ sqlite3_close(lsqlite3->sqlite);
+ }
return 0;
}
diff --git a/source4/lib/ldb/ldb_tdb/ldb_search.c b/source4/lib/ldb/ldb_tdb/ldb_search.c
index 3644d046e0..d6e7d66f68 100644
--- a/source4/lib/ldb/ldb_tdb/ldb_search.c
+++ b/source4/lib/ldb/ldb_tdb/ldb_search.c
@@ -272,44 +272,63 @@ int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_messag
/*
search the database for a single simple dn
*/
-int ltdb_search_dn(struct ldb_module *module, const char *dn,
- const char * const attrs[], struct ldb_message ***res)
+static int ltdb_search_dn(struct ldb_module *module, const char *dn,
+ const char * const attrs[], struct ldb_message ***res)
{
struct ldb_context *ldb = module->ldb;
+ struct ltdb_private *ltdb = module->private_data;
int ret;
struct ldb_message *msg, *msg2;
+ *res = NULL;
+
+ if (ltdb_lock_read(module) != 0) {
+ return -1;
+ }
+
+ ltdb->last_err_string = NULL;
+
+ if (ltdb_cache_load(module) != 0) {
+ ltdb_unlock_read(module);
+ return -1;
+ }
+
*res = talloc_array(ldb, struct ldb_message *, 2);
if (! *res) {
- return -1;
+ goto failed;
}
msg = talloc(*res, struct ldb_message);
if (msg == NULL) {
- talloc_free(*res);
- *res = NULL;
- return -1;
+ goto failed;
}
ret = ltdb_search_dn1(module, dn, msg);
if (ret != 1) {
talloc_free(*res);
*res = NULL;
- return ret;
+ ltdb_unlock_read(module);
+ return 0;
}
msg2 = ltdb_pull_attrs(module, msg, attrs);
talloc_free(msg);
-
if (!msg2) {
- return -1;
+ goto failed;
}
(*res)[0] = talloc_steal(*res, msg2);
(*res)[1] = NULL;
+ ltdb_unlock_read(module);
+
return 1;
+
+failed:
+ talloc_free(*res);
+ ltdb_unlock_read(module);
+ return -1;
}
@@ -508,7 +527,6 @@ int ltdb_search(struct ldb_module *module, const char *base,
/* check if we are looking for a simple dn */
if (scope == LDB_SCOPE_BASE && (expression == NULL || expression[0] == '\0')) {
ret = ltdb_search_dn(module, base, attrs, res);
- ltdb_unlock_read(module);
return ret;
}
diff --git a/source4/lib/ldb/ldb_tdb/ldb_tdb.c b/source4/lib/ldb/ldb_tdb/ldb_tdb.c
index 6516787a5a..596ede8a4e 100644
--- a/source4/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/source4/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -49,9 +49,9 @@
*/
static int ltdb_case_fold_attr_required(void * user_data, char *attr)
{
- struct ldb_module *module = user_data;
+ struct ldb_module *module = talloc_get_type(user_data, struct ldb_module);
- return ltdb_attribute_flags(module, attr) & LTDB_FLAG_CASE_INSENSITIVE;
+ return ltdb_attribute_flags(module, attr) & LTDB_FLAG_CASE_INSENSITIVE;
}
/*
@@ -746,6 +746,11 @@ static int ltdb_rename(struct ldb_module *module, const char *olddn, const char
return -1;
}
+ if (ltdb_cache_load(module) != 0) {
+ ltdb_unlock(module, LDBLOCK);
+ return -1;
+ }
+
msg = talloc(module, struct ldb_message);
if (msg == NULL) {
goto failed;
@@ -829,28 +834,19 @@ static int ltdb_destructor(void *p)
/*
connect to the database
*/
-struct ldb_context *ltdb_connect(const char *url,
- unsigned int flags,
- const char *options[])
+int ltdb_connect(struct ldb_context *ldb, const char *url,
+ unsigned int flags, const char *options[])
{
const char *path;
int tdb_flags, open_flags;
struct ltdb_private *ltdb;
TDB_CONTEXT *tdb;
- struct ldb_context *ldb;
-
- ldb = talloc_zero(NULL, struct ldb_context);
- if (!ldb) {
- errno = ENOMEM;
- return NULL;
- }
/* parse the url */
if (strchr(url, ':')) {
if (strncmp(url, "tdb://", 6) != 0) {
- errno = EINVAL;
- talloc_free(ldb);
- return NULL;
+ ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid tdb URL '%s'", url);
+ return -1;
}
path = url+6;
} else {
@@ -868,16 +864,15 @@ struct ldb_context *ltdb_connect(const char *url,
/* note that we use quite a large default hash size */
tdb = tdb_open(path, 10000, tdb_flags, open_flags, 0666);
if (!tdb) {
- talloc_free(ldb);
- return NULL;
+ ldb_debug(ldb, LDB_DEBUG_ERROR, "Unable to open tdb '%s'", path);
+ return -1;
}
ltdb = talloc_zero(ldb, struct ltdb_private);
if (!ltdb) {
tdb_close(tdb);
- talloc_free(ldb);
- errno = ENOMEM;
- return NULL;
+ ldb_oom(ldb);
+ return -1;
}
ltdb->tdb = tdb;
@@ -887,14 +882,14 @@ struct ldb_context *ltdb_connect(const char *url,
ldb->modules = talloc(ldb, struct ldb_module);
if (!ldb->modules) {
- talloc_free(ldb);
- errno = ENOMEM;
- return NULL;
+ ldb_oom(ldb);
+ talloc_free(ltdb);
+ return -1;
}
ldb->modules->ldb = ldb;
ldb->modules->prev = ldb->modules->next = NULL;
ldb->modules->private_data = ltdb;
ldb->modules->ops = &ltdb_ops;
- return ldb;
+ return 0;
}
diff --git a/source4/lib/ldb/ldb_tdb/ldb_tdb.h b/source4/lib/ldb/ldb_tdb/ldb_tdb.h
index b77e02fba5..6377092a21 100644
--- a/source4/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/source4/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -91,8 +91,6 @@ int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name,
const struct ldb_val *val);
void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg);
int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_message *msg);
-int ltdb_search_dn(struct ldb_module *module, const char *dn,
- const char * const attrs[], struct ldb_message ***res);
int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
const char * const attrs[],
int *count,
diff --git a/source4/lib/ldb/tests/test-extended.sh b/source4/lib/ldb/tests/test-extended.sh
index ede66ec567..93ac2db6bd 100755
--- a/source4/lib/ldb/tests/test-extended.sh
+++ b/source4/lib/ldb/tests/test-extended.sh
@@ -1,8 +1,10 @@
#!/bin/sh
+echo "Running extended search tests"
+
rm -f $LDB_URL
-cat <<EOF | bin/ldbadd - || exit 1
+cat <<EOF | bin/ldbadd || exit 1
dn: testrec1
i1: 1
i2: 0
diff --git a/source4/lib/ldb/tests/test-generic.sh b/source4/lib/ldb/tests/test-generic.sh
index 7b82c672be..5e0cc9cec6 100755
--- a/source4/lib/ldb/tests/test-generic.sh
+++ b/source4/lib/ldb/tests/test-generic.sh
@@ -20,7 +20,7 @@ echo "Showing renamed record"
$VALGRIND bin/ldbsearch '(uid=uham)' || exit 1
echo "Starting ldbtest"
-time $VALGRIND bin/ldbtest -r 1000 -s 10 || exit 1
+time $VALGRIND bin/ldbtest --num-records 1000 --num-searches 10 || exit 1
echo "Adding index"
$VALGRIND bin/ldbadd tests/test-index.ldif || exit 1
@@ -42,7 +42,7 @@ $VALGRIND bin/ldbsearch '(objectclass=)' uid || exit 1
$VALGRIND bin/ldbsearch -b 'cn=Hampster Ursula,ou=Alumni Association,ou=People,o=University of Michigan,c=US' -s base "" sn || exit 1
echo "Starting ldbtest indexed"
-time $VALGRIND bin/ldbtest -r 1000 -s 5000 || exit 1
+time $VALGRIND bin/ldbtest --num-records 1000 --num-searches 5000 || exit 1
echo "Testing one level search"
count=`$VALGRIND bin/ldbsearch -b 'ou=Groups,o=University of Michigan,c=US' -s one 'objectclass=*' none |grep ^dn | wc -l`
diff --git a/source4/lib/ldb/tools/ldbadd.c b/source4/lib/ldb/tools/ldbadd.c
index 818c33a11d..35a41527be 100644
--- a/source4/lib/ldb/tools/ldbadd.c
+++ b/source4/lib/ldb/tools/ldbadd.c
@@ -35,6 +35,7 @@
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
+#include "ldb/tools/cmdline.h"
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
@@ -86,69 +87,36 @@ static int process_file(struct ldb_context *ldb, FILE *f)
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
{
struct ldb_context *ldb;
- int count=0;
- const char *ldb_url;
- const char **options = NULL;
- int ldbopts;
- int opt, i;
-
- ldb_url = getenv("LDB_URL");
-
- ldbopts = 0;
- while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
- switch (opt) {
- case 'H':
- ldb_url = optarg;
- break;
-
- case 'o':
- options = ldb_options_parse(options, &ldbopts, optarg);
- break;
-
- case 'h':
- default:
- usage();
- break;
- }
- }
+ int i, ret, count=0;
+ struct ldb_cmdline *options;
- if (!ldb_url) {
- fprintf(stderr, "You must specify an ldb URL\n\n");
- usage();
- }
+ ldb = ldb_init(NULL);
- argc -= optind;
- argv += optind;
+ options = ldb_cmdline_process(ldb, argc, argv, usage);
- ldb = ldb_connect(ldb_url, 0, options);
-
- if (!ldb) {
- perror("ldb_connect");
+ ret = ldb_connect(ldb, options->url, 0, options->options);
+ if (ret != 0) {
+ fprintf(stderr, "Failed to connect to %s - %s\n",
+ options->url, ldb_errstring(ldb));
+ talloc_free(ldb);
exit(1);
}
- ldb_set_debug_stderr(ldb);
-
- if (argc == 0) {
- usage();
- }
-
- for (i=0;i<argc;i++) {
- FILE *f;
- if (strcmp(argv[i],"-") == 0) {
- f = stdin;
- } else {
- f = fopen(argv[i], "r");
- }
- if (!f) {
- perror(argv[i]);
- exit(1);
- }
- count += process_file(ldb, f);
- if (f != stdin) {
+ if (options->argc == 0) {
+ count += process_file(ldb, stdin);
+ } else {
+ for (i=0;i<options->argc;i++) {
+ const char *fname = options->argv[i];
+ FILE *f;
+ f = fopen(fname, "r");
+ if (!f) {
+ perror(fname);
+ exit(1);
+ }
+ count += process_file(ldb, f);
fclose(f);
}
}
diff --git a/source4/lib/ldb/tools/ldbdel.c b/source4/lib/ldb/tools/ldbdel.c
index ec2e302b20..fcf1d26d53 100644
--- a/source4/lib/ldb/tools/ldbdel.c
+++ b/source4/lib/ldb/tools/ldbdel.c
@@ -35,6 +35,7 @@
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
+#include "ldb/tools/cmdline.h"
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
@@ -77,72 +78,41 @@ static void usage(void)
exit(1);
}
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
{
struct ldb_context *ldb;
- const char **options = NULL;
- int ldbopts;
int ret, i;
- const char *ldb_url;
- int opt, recursive=0;
-
- ldb_url = getenv("LDB_URL");
-
- ldbopts = 0;
- while ((opt = getopt(argc, argv, "hH:ro:")) != EOF) {
- switch (opt) {
- case 'H':
- ldb_url = optarg;
- break;
-
- case 'r':
- recursive=1;
- break;
-
- case 'o':
- options = ldb_options_parse(options, &ldbopts, optarg);
- break;
-
- case 'h':
- default:
- usage();
- break;
- }
- }
+ struct ldb_cmdline *options;
- if (!ldb_url) {
- fprintf(stderr, "You must specify a ldb URL\n\n");
- usage();
- }
+ ldb = ldb_init(NULL);
- argc -= optind;
- argv += optind;
+ options = ldb_cmdline_process(ldb, argc, argv, usage);
- if (argc < 1) {
+ if (options->argc < 1) {
usage();
exit(1);
}
- ldb = ldb_connect(ldb_url, 0, options);
- if (!ldb) {
- perror("ldb_connect");
+ ret = ldb_connect(ldb, options->url, 0, options->options);
+ if (ret != 0) {
+ fprintf(stderr, "Failed to connect to %s - %s\n",
+ options->url, ldb_errstring(ldb));
+ talloc_free(ldb);
exit(1);
}
- ldb_set_debug_stderr(ldb);
-
- for (i=0;i<argc;i++) {
- if (recursive) {
- ret = ldb_delete_recursive(ldb, argv[i]);
+ for (i=0;i<options->argc;i++) {
+ const char *dn = options->argv[i];
+ if (options->recursive) {
+ ret = ldb_delete_recursive(ldb, dn);
} else {
- ret = ldb_delete(ldb, argv[i]);
+ ret = ldb_delete(ldb, dn);
if (ret == 0) {
printf("Deleted 1 record\n");
}
}
if (ret != 0) {
- printf("delete of '%s' failed - %s\n",
- argv[i], ldb_errstring(ldb));
+ printf("delete of '%s' failed - %s\n", dn, ldb_errstring(ldb));
}
}
diff --git a/source4/lib/ldb/tools/ldbedit.c b/source4/lib/ldb/tools/ldbedit.c
index 851eaeebdf..6c599ee2ec 100644
--- a/source4/lib/ldb/tools/ldbedit.c
+++ b/source4/lib/ldb/tools/ldbedit.c
@@ -35,6 +35,7 @@
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
+#include "ldb/tools/cmdline.h"
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
@@ -272,109 +273,45 @@ static void usage(void)
exit(1);
}
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
{
struct ldb_context *ldb;
struct ldb_message **msgs;
int ret;
const char *expression = NULL;
- const char *ldb_url;
- const char *basedn = NULL;
- const char **options = NULL;
- int ldbopts;
- int opt;
- enum ldb_scope scope = LDB_SCOPE_SUBTREE;
- const char *editor;
const char * const * attrs = NULL;
+ struct ldb_cmdline *options;
- ldb_url = getenv("LDB_URL");
+ ldb = ldb_init(NULL);
- /* build the editor command to run -
- use the same editor priorities as vipw */
- editor = getenv("VISUAL");
- if (!editor) {
- editor = getenv("EDITOR");
- }
- if (!editor) {
- editor = "vi";
- }
-
- ldbopts = 0;
- while ((opt = getopt(argc, argv, "hab:e:H:s:vo:")) != EOF) {
- switch (opt) {
- case 'b':
- basedn = optarg;
- break;
-
- case 'H':
- ldb_url = optarg;
- break;
-
- case 's':
- if (strcmp(optarg, "base") == 0) {
- scope = LDB_SCOPE_BASE;
- } else if (strcmp(optarg, "sub") == 0) {
- scope = LDB_SCOPE_SUBTREE;
- } else if (strcmp(optarg, "one") == 0) {
- scope = LDB_SCOPE_ONELEVEL;
- }
- break;
-
- case 'e':
- editor = optarg;
- break;
-
- case 'a':
- expression = "(|(objectclass=*)(dn=*))";
- break;
-
- case 'v':
- verbose++;
- break;
-
- case 'o':
- options = ldb_options_parse(options, &ldbopts, optarg);
- break;
-
- case 'h':
- default:
- usage();
- break;
- }
- }
+ options = ldb_cmdline_process(ldb, argc, argv, usage);
- if (!ldb_url) {
- fprintf(stderr, "You must specify a ldb URL\n\n");
- usage();
+ if (options->all_records) {
+ expression = "(|(objectclass=*)(dn=*))";
}
- argc -= optind;
- argv += optind;
-
if (!expression) {
- if (argc == 0) {
+ if (options->argc == 0) {
usage();
}
- expression = argv[0];
- argc--;
- argv++;
+ expression = options->argv[0];
+ options->argc--;
+ options->argv++;
}
- if (argc > 0) {
- attrs = (const char * const *)argv;
+ if (options->argc > 0) {
+ attrs = (const char * const *)options->argv;
}
- ldb = ldb_connect(ldb_url, 0, options);
-
- if (!ldb) {
- perror("ldb_connect");
+ ret = ldb_connect(ldb, options->url, LDB_FLG_RDONLY, options->options);
+ if (ret != 0) {
+ fprintf(stderr, "Failed to connect to %s - %s\n",
+ options->url, ldb_errstring(ldb));
+ talloc_free(ldb);
exit(1);
}
- ldb_set_debug_stderr(ldb);
-
- ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
-
+ ret = ldb_search(ldb, options->basedn, options->scope, expression, attrs, &msgs);
if (ret == -1) {
printf("search failed - %s\n", ldb_errstring(ldb));
exit(1);
@@ -385,7 +322,7 @@ static void usage(void)
return 0;
}
- do_edit(ldb, msgs, ret, editor);
+ do_edit(ldb, msgs, ret, options->editor);
if (ret > 0) {
ret = talloc_free(msgs);
diff --git a/source4/lib/ldb/tools/ldbmodify.c b/source4/lib/ldb/tools/ldbmodify.c
index 78baa0e36c..c54c573ab0 100644
--- a/source4/lib/ldb/tools/ldbmodify.c
+++ b/source4/lib/ldb/tools/ldbmodify.c
@@ -35,6 +35,7 @@
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
+#include "ldb/tools/cmdline.h"
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
@@ -88,66 +89,40 @@ static int process_file(struct ldb_context *ldb, FILE *f)
return count;
}
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
{
struct ldb_context *ldb;
int count=0;
- const char *ldb_url;
- const char **options = NULL;
- int ldbopts;
- int opt, i;
-
- ldb_url = getenv("LDB_URL");
-
- ldbopts = 0;
- while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
- switch (opt) {
- case 'H':
- ldb_url = optarg;
- break;
+ int i, ret;
+ struct ldb_cmdline *options;
- case 'o':
- options = ldb_options_parse(options, &ldbopts, optarg);
- break;
+ ldb = ldb_init(NULL);
- case 'h':
- default:
- usage();
- break;
- }
- }
+ options = ldb_cmdline_process(ldb, argc, argv, usage);
- if (!ldb_url) {
- fprintf(stderr, "You must specify a ldb URL\n\n");
- usage();
- }
-
- argc -= optind;
- argv += optind;
-
- ldb = ldb_connect(ldb_url, 0, options);
-
- if (!ldb) {
- perror("ldb_connect");
+ ret = ldb_connect(ldb, options->url, 0, options->options);
+ if (ret != 0) {
+ fprintf(stderr, "Failed to connect to %s - %s\n",
+ options->url, ldb_errstring(ldb));
+ talloc_free(ldb);
exit(1);
}
- ldb_set_debug_stderr(ldb);
-
- if (argc == 0) {
+ if (options->argc == 0) {
usage();
exit(1);
}
- for (i=0;i<argc;i++) {
+ for (i=0;i<options->argc;i++) {
+ const char *fname = options->argv[i];
FILE *f;
- if (strcmp(argv[i],"-") == 0) {
+ if (strcmp(fname,"-") == 0) {
f = stdin;
} else {
- f = fopen(argv[i], "r");
+ f = fopen(fname, "r");
}
if (!f) {
- perror(argv[i]);
+ perror(fname);
exit(1);
}
count += process_file(ldb, f);
diff --git a/source4/lib/ldb/tools/ldbrename.c b/source4/lib/ldb/tools/ldbrename.c
index 273c792584..5566c3d7d7 100644
--- a/source4/lib/ldb/tools/ldbrename.c
+++ b/source4/lib/ldb/tools/ldbrename.c
@@ -37,6 +37,7 @@
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
+#include "ldb/tools/cmdline.h"
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
@@ -55,61 +56,38 @@ static void usage(void)
}
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
{
struct ldb_context *ldb;
- const char *ldb_url;
- const char **options = NULL;
- int ldbopts;
- int opt, ret;
-
- ldb_url = getenv("LDB_URL");
-
- ldbopts = 0;
- while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
- switch (opt) {
- case 'H':
- ldb_url = optarg;
- break;
-
- case 'o':
- options = ldb_options_parse(options, &ldbopts, optarg);
- break;
-
- case 'h':
- default:
- usage();
- break;
- }
- }
-
- if (!ldb_url) {
- fprintf(stderr, "You must specify a ldb URL\n\n");
- usage();
- }
+ int ret;
+ struct ldb_cmdline *options;
+ const char *dn1, *dn2;
- argc -= optind;
- argv += optind;
+ ldb = ldb_init(NULL);
- ldb = ldb_connect(ldb_url, 0, options);
+ options = ldb_cmdline_process(ldb, argc, argv, usage);
- if (!ldb) {
- perror("ldb_connect");
+ ret = ldb_connect(ldb, options->url, 0, options->options);
+ if (ret != 0) {
+ fprintf(stderr, "Failed to connect to %s - %s\n",
+ options->url, ldb_errstring(ldb));
+ talloc_free(ldb);
exit(1);
}
- ldb_set_debug_stderr(ldb);
-
- if (argc < 2) {
+ if (options->argc < 2) {
usage();
}
- ret = ldb_rename(ldb, argv[0], argv[1]);
+ dn1 = options->argv[0];
+ dn2 = options->argv[1];
+
+ ret = ldb_rename(ldb, dn1, dn2);
if (ret == 0) {
printf("Renamed 1 record\n");
} else {
printf("rename of '%s' to '%s' failed - %s\n",
- argv[0], argv[1], ldb_errstring(ldb));
+ dn1, dn2, ldb_errstring(ldb));
}
talloc_free(ldb);
diff --git a/source4/lib/ldb/tools/ldbsearch.c b/source4/lib/ldb/tools/ldbsearch.c
index 4bf9db8d90..04f83ca366 100644
--- a/source4/lib/ldb/tools/ldbsearch.c
+++ b/source4/lib/ldb/tools/ldbsearch.c
@@ -35,6 +35,7 @@
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
+#include "ldb/tools/cmdline.h"
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
@@ -113,93 +114,45 @@ static int do_search(struct ldb_context *ldb,
return 0;
}
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
{
struct ldb_context *ldb;
const char * const * attrs = NULL;
- const char *ldb_url;
- const char *basedn = NULL;
- const char **options = NULL;
- int opt, ldbopts;
- enum ldb_scope scope = LDB_SCOPE_SUBTREE;
- int interactive = 0, sort_attribs=0, ret=0;
-
- ldb_url = getenv("LDB_URL");
-
- ldbopts = 0;
- while ((opt = getopt(argc, argv, "b:H:s:o:hiS")) != EOF) {
- switch (opt) {
- case 'b':
- basedn = optarg;
- break;
-
- case 'H':
- ldb_url = optarg;
- break;
-
- case 's':
- if (strcmp(optarg, "base") == 0) {
- scope = LDB_SCOPE_BASE;
- } else if (strcmp(optarg, "sub") == 0) {
- scope = LDB_SCOPE_SUBTREE;
- } else if (strcmp(optarg, "one") == 0) {
- scope = LDB_SCOPE_ONELEVEL;
- }
- break;
-
- case 'i':
- interactive = 1;
- break;
-
- case 'S':
- sort_attribs = 1;
- break;
+ struct ldb_cmdline *options;
+ int ret;
- case 'o':
- options = ldb_options_parse(options, &ldbopts, optarg);
- break;
-
- case 'h':
- default:
- usage();
- break;
- }
- }
+ ldb = ldb_init(NULL);
- if (!ldb_url) {
- fprintf(stderr, "You must specify a ldb URL\n\n");
- usage();
- }
-
- argc -= optind;
- argv += optind;
-
- if (argc < 1 && !interactive) {
+ options = ldb_cmdline_process(ldb, argc, argv, usage);
+
+ if (options->argc < 1 && !options->interactive) {
usage();
exit(1);
}
- if (argc > 1) {
- attrs = (const char * const *)(argv+1);
+ if (options->argc > 1) {
+ attrs = (const char * const *)(options->argv+1);
}
- ldb = ldb_connect(ldb_url, LDB_FLG_RDONLY, options);
- if (!ldb) {
- perror("ldb_connect");
+ ret = ldb_connect(ldb, options->url, LDB_FLG_RDONLY, options->options);
+ if (ret != 0) {
+ fprintf(stderr, "Failed to connect to %s - %s\n",
+ options->url, ldb_errstring(ldb));
+ talloc_free(ldb);
exit(1);
}
- ldb_set_debug_stderr(ldb);
-
- if (interactive) {
+ if (options->interactive) {
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
- if (do_search(ldb, basedn, scope, sort_attribs, line, attrs) == -1) {
+ if (do_search(ldb, options->basedn,
+ options->scope, options->sorted, line, attrs) == -1) {
ret = -1;
}
}
} else {
- ret = do_search(ldb, basedn, scope, sort_attribs, argv[0], attrs);
+ ret = do_search(ldb, options->basedn, options->scope, options->sorted,
+ options->argv[0], attrs);
}
talloc_free(ldb);
diff --git a/source4/lib/ldb/tools/ldbtest.c b/source4/lib/ldb/tools/ldbtest.c
index fc1f3e3098..a7c9a3123a 100644
--- a/source4/lib/ldb/tools/ldbtest.c
+++ b/source4/lib/ldb/tools/ldbtest.c
@@ -35,16 +35,15 @@
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
+#include "ldb/tools/cmdline.h"
#ifdef _SAMBA_BUILD_
#include "system/filesys.h"
#include "system/time.h"
#endif
-static const char *ldb_url;
-static const char *base_dn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US";
-
static struct timeval tp1,tp2;
+static struct ldb_cmdline *options;
static void _start_timer(void)
{
@@ -69,51 +68,51 @@ static void add_records(struct ldb_context *ldb,
struct ldb_message_element el[6];
struct ldb_val vals[6][1];
char *name;
- int j;
+ TALLOC_CTX *tmp_ctx = talloc_new(ldb);
asprintf(&name, "Test%d", i);
- asprintf(&msg.dn, "cn=%s,%s", name, basedn);
+ msg.dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, basedn);
msg.num_elements = 6;
msg.elements = el;
el[0].flags = 0;
- el[0].name = strdup("cn");
+ el[0].name = talloc_strdup(tmp_ctx, "cn");
el[0].num_values = 1;
el[0].values = vals[0];
vals[0][0].data = name;
vals[0][0].length = strlen(name);
el[1].flags = 0;
- el[1].name = strdup("title");
+ el[1].name = talloc_strdup(tmp_ctx, "title");
el[1].num_values = 1;
el[1].values = vals[1];
- asprintf((char **)&vals[1][0].data, "The title of %s", name);
+ vals[1][0].data = talloc_asprintf(tmp_ctx, "The title of %s", name);
vals[1][0].length = strlen(vals[1][0].data);
el[2].flags = 0;
- el[2].name = strdup("uid");
+ el[2].name = talloc_strdup(tmp_ctx, "uid");
el[2].num_values = 1;
el[2].values = vals[2];
- vals[2][0].data = ldb_casefold(ldb, name);
+ vals[2][0].data = ldb_casefold(tmp_ctx, name);
vals[2][0].length = strlen(vals[2][0].data);
el[3].flags = 0;
- el[3].name = strdup("mail");
+ el[3].name = talloc_strdup(tmp_ctx, "mail");
el[3].num_values = 1;
el[3].values = vals[3];
- asprintf((char **)&vals[3][0].data, "%s@example.com", name);
+ vals[3][0].data = talloc_asprintf(tmp_ctx, "%s@example.com", name);
vals[3][0].length = strlen(vals[3][0].data);
el[4].flags = 0;
- el[4].name = strdup("objectClass");
+ el[4].name = talloc_strdup(tmp_ctx, "objectClass");
el[4].num_values = 1;
el[4].values = vals[4];
- vals[4][0].data = strdup("OpenLDAPperson");
+ vals[4][0].data = talloc_strdup(tmp_ctx, "OpenLDAPperson");
vals[4][0].length = strlen(vals[4][0].data);
el[5].flags = 0;
- el[5].name = strdup("sn");
+ el[5].name = talloc_strdup(tmp_ctx, "sn");
el[5].num_values = 1;
el[5].values = vals[5];
vals[5][0].data = name;
@@ -129,15 +128,7 @@ static void add_records(struct ldb_context *ldb,
printf("adding uid %s\r", name);
fflush(stdout);
- for (j=0;j<msg.num_elements;j++) {
- free(el[j].name);
- }
- free(name);
- free(msg.dn);
- free(vals[1][0].data);
- talloc_free(vals[2][0].data);
- free(vals[3][0].data);
- free(vals[4][0].data);
+ talloc_free(tmp_ctx);
}
printf("\n");
@@ -154,30 +145,30 @@ static void modify_records(struct ldb_context *ldb,
struct ldb_message_element el[3];
struct ldb_val vals[3];
char *name;
- int j;
+ TALLOC_CTX *tmp_ctx = talloc_new(ldb);
- asprintf(&name, "Test%d", i);
- asprintf(&msg.dn, "cn=%s,%s", name, basedn);
+ name = talloc_asprintf(tmp_ctx, "Test%d", i);
+ msg.dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, basedn);
msg.num_elements = 3;
msg.elements = el;
el[0].flags = LDB_FLAG_MOD_DELETE;
- el[0].name = strdup("mail");
+ el[0].name = talloc_strdup(tmp_ctx, "mail");
el[0].num_values = 0;
el[1].flags = LDB_FLAG_MOD_ADD;
- el[1].name = strdup("mail");
+ el[1].name = talloc_strdup(tmp_ctx, "mail");
el[1].num_values = 1;
el[1].values = &vals[1];
- asprintf((char **)&vals[1].data, "%s@other.example.com", name);
+ vals[1].data = talloc_asprintf(tmp_ctx, "%s@other.example.com", name);
vals[1].length = strlen(vals[1].data);
el[2].flags = LDB_FLAG_MOD_REPLACE;
- el[2].name = strdup("mail");
+ el[2].name = talloc_strdup(tmp_ctx, "mail");
el[2].num_values = 1;
el[2].values = &vals[2];
- asprintf((char **)&vals[2].data, "%s@other2.example.com", name);
+ vals[2].data = talloc_asprintf(tmp_ctx, "%s@other2.example.com", name);
vals[2].length = strlen(vals[2].data);
if (ldb_modify(ldb, &msg) != 0) {
@@ -188,13 +179,7 @@ static void modify_records(struct ldb_context *ldb,
printf("Modifying uid %s\r", name);
fflush(stdout);
- for (j=0;j<msg.num_elements;j++) {
- free(el[j].name);
- }
- free(name);
- free(msg.dn);
- free(vals[1].data);
- free(vals[2].data);
+ talloc_free(tmp_ctx);
}
printf("\n");
@@ -235,7 +220,7 @@ static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
int ret;
asprintf(&expr, "(uid=TEST%d)", uid);
- ret = ldb_search(ldb, base_dn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
+ ret = ldb_search(ldb, options->basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res);
if (uid < nrecords && ret != 1) {
printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
@@ -263,7 +248,7 @@ static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches)
static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
{
printf("Adding %d records\n", nrecords);
- add_records(ldb, base_dn, nrecords);
+ add_records(ldb, options->basedn, nrecords);
printf("Starting search on uid\n");
_start_timer();
@@ -271,10 +256,10 @@ static void start_test(struct ldb_context *ldb, int nrecords, int nsearches)
printf("uid search took %.2f seconds\n", _end_timer());
printf("Modifying records\n");
- modify_records(ldb, base_dn, nrecords);
+ modify_records(ldb, options->basedn, nrecords);
printf("Deleting records\n");
- delete_records(ldb, base_dn, nrecords);
+ delete_records(ldb, options->basedn, nrecords);
}
@@ -312,7 +297,7 @@ static void start_test_index(struct ldb_context **ldb)
}
memset(msg, 0, sizeof(*msg));
- asprintf(&msg->dn, "cn=%s,%s", "test", base_dn);
+ asprintf(&msg->dn, "cn=%s,%s", "test", options->basedn);
ldb_msg_add_string(*ldb, msg, "cn", strdup("test"));
ldb_msg_add_string(*ldb, msg, "sn", strdup("test"));
ldb_msg_add_string(*ldb, msg, "uid", strdup("test"));
@@ -328,14 +313,15 @@ static void start_test_index(struct ldb_context **ldb)
exit(1);
}
- *ldb = ldb_connect(ldb_url, 0, NULL);
-
- if (!*ldb) {
- perror("ldb_connect");
+ (*ldb) = ldb_init(options);
+
+ ret = ldb_connect(*ldb, options->url, 0, NULL);
+ if (ret != 0) {
+ printf("failed to connect to %s\n", options->url);
exit(1);
}
- ret = ldb_search(*ldb, base_dn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
+ ret = ldb_search(*ldb, options->basedn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res);
if (ret != 1) {
printf("Should have found 1 record - found %d\n", ret);
exit(1);
@@ -363,71 +349,37 @@ static void usage(void)
exit(1);
}
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
{
+ TALLOC_CTX *mem_ctx = talloc_new(NULL);
struct ldb_context *ldb;
- const char **options = NULL;
- int ldbopts;
- int opt;
- int nrecords = 5000;
- int nsearches = 2000;
-
- ldb_url = getenv("LDB_URL");
-
- ldbopts = 0;
- while ((opt = getopt(argc, argv, "hH:r:s:b:o:")) != EOF) {
- switch (opt) {
- case 'H':
- ldb_url = optarg;
- break;
-
- case 'r':
- nrecords = atoi(optarg);
- break;
-
- case 'b':
- base_dn = optarg;
- break;
-
- case 's':
- nsearches = atoi(optarg);
- break;
-
- case 'o':
- options = ldb_options_parse(options, &ldbopts, optarg);
- break;
-
- case 'h':
- default:
- usage();
- break;
- }
- }
+ int ret;
- if (!ldb_url) {
- fprintf(stderr, "You must specify a ldb URL\n\n");
- usage();
- }
+ ldb = ldb_init(mem_ctx);
- argc -= optind;
- argv += optind;
+ options = ldb_cmdline_process(ldb, argc, argv, usage);
- ldb = ldb_connect(ldb_url, 0, options);
+ talloc_steal(mem_ctx, options);
- if (!ldb) {
- perror("ldb_connect");
- exit(1);
+ if (options->basedn == NULL) {
+ options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US";
}
- ldb_set_debug_stderr(ldb);
+ ret = ldb_connect(ldb, options->url, 0, options->options);
+ if (ret != 0) {
+ fprintf(stderr, "Failed to connect to %s - %s\n",
+ options->url, ldb_errstring(ldb));
+ talloc_free(ldb);
+ exit(1);
+ }
srandom(1);
- start_test(ldb, nrecords, nsearches);
+ start_test(ldb, options->num_records, options->num_searches);
start_test_index(&ldb);
- talloc_free(ldb);
+ talloc_free(mem_ctx);
return 0;
}