summaryrefslogtreecommitdiff
path: root/source4/lib/ldb
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2004-05-03 14:51:26 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 12:51:43 -0500
commit208e09747c242ab5bd59a658033db49efa8d8696 (patch)
tree46fd31e75aa1e805f3abb9fa572f36da2b70b3da /source4/lib/ldb
parentb96695ca23f8d1d95ed2e038ea66e6a0580356c3 (diff)
downloadsamba-208e09747c242ab5bd59a658033db49efa8d8696.tar.gz
samba-208e09747c242ab5bd59a658033db49efa8d8696.tar.bz2
samba-208e09747c242ab5bd59a658033db49efa8d8696.zip
r456: - added -i option to ldbsearch
- fixed sorting bug in ldb index handing (This used to be commit cdd48e2b9b3ca6be5503eec401e09db162408ac8)
Diffstat (limited to 'source4/lib/ldb')
-rw-r--r--source4/lib/ldb/common/ldb.c2
-rw-r--r--source4/lib/ldb/common/util.c7
-rw-r--r--source4/lib/ldb/include/ldb.h4
-rw-r--r--source4/lib/ldb/ldb_ldap/ldb_ldap.c2
-rw-r--r--source4/lib/ldb/ldb_tdb/ldb_index.c13
-rw-r--r--source4/lib/ldb/ldb_tdb/ldb_search.c12
-rw-r--r--source4/lib/ldb/tools/ldbsearch.c84
7 files changed, 75 insertions, 49 deletions
diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c
index ac77f306e9..ce21d1d9e2 100644
--- a/source4/lib/ldb/common/ldb.c
+++ b/source4/lib/ldb/common/ldb.c
@@ -81,7 +81,7 @@ int ldb_search(struct ldb_context *ldb,
const char *base,
enum ldb_scope scope,
const char *expression,
- char * const attrs[], struct ldb_message ***res)
+ const char * const *attrs, struct ldb_message ***res)
{
return ldb->ops->search(ldb, base, scope, expression, attrs, res);
}
diff --git a/source4/lib/ldb/common/util.c b/source4/lib/ldb/common/util.c
index 534d07c1df..22bbc8334e 100644
--- a/source4/lib/ldb/common/util.c
+++ b/source4/lib/ldb/common/util.c
@@ -85,10 +85,13 @@ int list_find(const void *needle,
}
return test_i;
}
- if (r == -1) {
+ if (r < 0) {
+ if (test_i == 0) {
+ return -1;
+ }
max_i = test_i - 1;
}
- if (r == 1) {
+ if (r > 0) {
min_i = test_i + 1;
}
}
diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h
index 215671c98a..c0521ecffa 100644
--- a/source4/lib/ldb/include/ldb.h
+++ b/source4/lib/ldb/include/ldb.h
@@ -124,7 +124,7 @@ typedef int (*ldb_traverse_fn)(struct ldb_context *, const struct ldb_message *)
struct ldb_backend_ops {
int (*close)(struct ldb_context *);
int (*search)(struct ldb_context *, const char *, enum ldb_scope,
- const char *, char * const [], struct ldb_message ***);
+ const char *, const char * const [], struct ldb_message ***);
int (*search_free)(struct ldb_context *, struct ldb_message **);
int (*add_record)(struct ldb_context *, const struct ldb_message *);
int (*modify_record)(struct ldb_context *, const struct ldb_message *);
@@ -174,7 +174,7 @@ int ldb_search(struct ldb_context *ldb,
const char *base,
enum ldb_scope scope,
const char *expression,
- char * const attrs[], struct ldb_message ***res);
+ const char * const *attrs, struct ldb_message ***res);
/*
free a set of messages returned by ldb_search
diff --git a/source4/lib/ldb/ldb_ldap/ldb_ldap.c b/source4/lib/ldb/ldb_ldap/ldb_ldap.c
index 8723beeadc..26c29122ad 100644
--- a/source4/lib/ldb/ldb_ldap/ldb_ldap.c
+++ b/source4/lib/ldb/ldb_ldap/ldb_ldap.c
@@ -207,7 +207,7 @@ static int lldb_add_msg_attr(struct ldb_message *msg,
*/
static int lldb_search(struct ldb_context *ldb, const char *base,
enum ldb_scope scope, const char *expression,
- char * const *attrs, struct ldb_message ***res)
+ const char * const *attrs, struct ldb_message ***res)
{
struct lldb_private *lldb = ldb->private_data;
int count, msg_count;
diff --git a/source4/lib/ldb/ldb_tdb/ldb_index.c b/source4/lib/ldb/ldb_tdb/ldb_index.c
index 987ee017b6..d250bc10be 100644
--- a/source4/lib/ldb/ldb_tdb/ldb_index.c
+++ b/source4/lib/ldb/ldb_tdb/ldb_index.c
@@ -96,6 +96,11 @@ static int ldb_msg_find_idx(const struct ldb_message *msg, const char *attr,
return -1;
}
+/* used in sorting dn lists */
+static int list_cmp(const char **s1, const char **s2)
+{
+ return strcmp(*s1, *s2);
+}
/*
return a list of dn's that might match a simple indexed search or
@@ -164,7 +169,7 @@ static int ltdb_index_dn_simple(struct ldb_context *ldb,
ltdb_search_dn1_free(ldb, &msg);
- qsort(list->dn, list->count, sizeof(char *), (comparison_fn_t) strcmp);
+ qsort(list->dn, list->count, sizeof(char *), (comparison_fn_t) list_cmp);
return 1;
}
@@ -309,7 +314,7 @@ static int list_union(struct dn_list *list, const struct dn_list *list2)
}
if (list->count != count) {
- qsort(list->dn, list->count, sizeof(char *), (comparison_fn_t)strcmp);
+ qsort(list->dn, list->count, sizeof(char *), (comparison_fn_t)list_cmp);
}
return 0;
@@ -483,7 +488,7 @@ static int ldb_index_filter(struct ldb_context *ldb, struct ldb_parse_tree *tree
const char *base,
enum ldb_scope scope,
const struct dn_list *dn_list,
- char * const attrs[], struct ldb_message ***res)
+ const char * const attrs[], struct ldb_message ***res)
{
int i;
unsigned int count = 0;
@@ -523,7 +528,7 @@ int ltdb_search_indexed(struct ldb_context *ldb,
const char *base,
enum ldb_scope scope,
struct ldb_parse_tree *tree,
- char * const attrs[], struct ldb_message ***res)
+ const char * const attrs[], struct ldb_message ***res)
{
struct ltdb_private *ltdb = ldb->private_data;
struct dn_list dn_list;
diff --git a/source4/lib/ldb/ldb_tdb/ldb_search.c b/source4/lib/ldb/ldb_tdb/ldb_search.c
index 1dce8f83a2..cce865e052 100644
--- a/source4/lib/ldb/ldb_tdb/ldb_search.c
+++ b/source4/lib/ldb/ldb_tdb/ldb_search.c
@@ -148,7 +148,7 @@ static int msg_add_all_elements(struct ldb_message *ret,
*/
static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb,
const struct ldb_message *msg,
- char * const *attrs)
+ const char * const *attrs)
{
struct ldb_message *ret;
int i;
@@ -294,7 +294,7 @@ int ltdb_search_dn1(struct ldb_context *ldb, const char *dn, struct ldb_message
search the database for a single simple dn
*/
int ltdb_search_dn(struct ldb_context *ldb, char *dn,
- char * const attrs[], struct ldb_message ***res)
+ const char * const attrs[], struct ldb_message ***res)
{
int ret;
struct ldb_message msg, *msg2;
@@ -330,7 +330,7 @@ int ltdb_search_dn(struct ldb_context *ldb, char *dn,
return 0 on success, -1 on failure
*/
int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
- char * const attrs[],
+ const char * const attrs[],
unsigned int *count,
struct ldb_message ***res)
{
@@ -368,7 +368,7 @@ struct ltdb_search_info {
struct ldb_parse_tree *tree;
const char *base;
enum ldb_scope scope;
- char * const *attrs;
+ const char * const *attrs;
struct ldb_message **msgs;
int failures;
int count;
@@ -445,7 +445,7 @@ static int ltdb_search_full(struct ldb_context *ldb,
const char *base,
enum ldb_scope scope,
struct ldb_parse_tree *tree,
- char * const attrs[], struct ldb_message ***res)
+ const char * const attrs[], struct ldb_message ***res)
{
struct ltdb_private *ltdb = ldb->private_data;
int ret;
@@ -478,7 +478,7 @@ static int ltdb_search_full(struct ldb_context *ldb,
*/
int ltdb_search(struct ldb_context *ldb, const char *base,
enum ldb_scope scope, const char *expression,
- char * const attrs[], struct ldb_message ***res)
+ const char * const attrs[], struct ldb_message ***res)
{
struct ldb_parse_tree *tree;
int ret;
diff --git a/source4/lib/ldb/tools/ldbsearch.c b/source4/lib/ldb/tools/ldbsearch.c
index 45412f6104..e8275e87c1 100644
--- a/source4/lib/ldb/tools/ldbsearch.c
+++ b/source4/lib/ldb/tools/ldbsearch.c
@@ -41,24 +41,59 @@ static void usage(void)
printf(" -H ldb_url choose the database (or $LDB_URL)\n");
printf(" -s base|sub|one choose search scope\n");
printf(" -b basedn choose baseDN\n");
+ printf(" -i read search expressions from stdin\n");
exit(1);
}
+static void do_search(struct ldb_context *ldb,
+ const char *basedn,
+ int scope,
+ const char *expression,
+ const char * const *attrs)
+{
+ int ret, i;
+ struct ldb_message **msgs;
+
+ ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
+ if (ret == -1) {
+ printf("search failed - %s\n", ldb_errstring(ldb));
+ return;
+ }
+
+ printf("# returned %d records\n", ret);
+
+ for (i=0;i<ret;i++) {
+ struct ldb_ldif ldif;
+ printf("# record %d\n", i+1);
+
+ ldif.changetype = LDB_CHANGETYPE_NONE;
+ ldif.msg = *msgs[i];
+
+ ldif_write_file(stdout, &ldif);
+ }
+
+ if (ret > 0) {
+ ret = ldb_search_free(ldb, msgs);
+ if (ret == -1) {
+ fprintf(stderr, "search_free failed\n");
+ exit(1);
+ }
+ }
+}
+
int main(int argc, char * const argv[])
{
struct ldb_context *ldb;
- struct ldb_message **msgs;
- int ret, i;
- const char *expression;
- char * const *attrs = NULL;
+ const char * const * attrs = NULL;
const char *ldb_url;
const char *basedn = NULL;
int opt;
enum ldb_scope scope = LDB_SCOPE_SUBTREE;
+ int interactive = 0;
ldb_url = getenv("LDB_URL");
- while ((opt = getopt(argc, argv, "b:H:s:h")) != EOF) {
+ while ((opt = getopt(argc, argv, "b:H:s:hi")) != EOF) {
switch (opt) {
case 'b':
basedn = optarg;
@@ -78,6 +113,10 @@ static void usage(void)
}
break;
+ case 'i':
+ interactive = 1;
+ break;
+
case 'h':
default:
usage();
@@ -93,7 +132,7 @@ static void usage(void)
argc -= optind;
argv += optind;
- if (argc < 1) {
+ if (argc < 1 && !interactive) {
usage();
exit(1);
}
@@ -102,40 +141,19 @@ static void usage(void)
attrs = argv+1;
}
- expression = argv[0];
-
ldb = ldb_connect(ldb_url, 0, NULL);
-
if (!ldb) {
perror("ldb_connect");
exit(1);
}
- ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
-
- if (ret == -1) {
- printf("search failed - %s\n", ldb_errstring(ldb));
- exit(1);
- }
-
- printf("# returned %d records\n", ret);
-
- for (i=0;i<ret;i++) {
- struct ldb_ldif ldif;
- printf("# record %d\n", i+1);
-
- ldif.changetype = LDB_CHANGETYPE_NONE;
- ldif.msg = *msgs[i];
-
- ldif_write_file(stdout, &ldif);
- }
-
- if (ret > 0) {
- ret = ldb_search_free(ldb, msgs);
- if (ret == -1) {
- fprintf(stderr, "search_free failed\n");
- exit(1);
+ if (interactive) {
+ char line[1024];
+ while (fgets(line, sizeof(line), stdin)) {
+ do_search(ldb, basedn, scope, line, attrs);
}
+ } else {
+ do_search(ldb, basedn, scope, argv[0], attrs);
}
ldb_close(ldb);