summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/tools/ldbsearch.c
diff options
context:
space:
mode:
Diffstat (limited to 'source4/lib/ldb/tools/ldbsearch.c')
-rw-r--r--source4/lib/ldb/tools/ldbsearch.c84
1 files changed, 51 insertions, 33 deletions
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);