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, 75 insertions, 9 deletions
diff --git a/source4/lib/ldb/tools/ldbsearch.c b/source4/lib/ldb/tools/ldbsearch.c
index a340e16d08..406c81f765 100644
--- a/source4/lib/ldb/tools/ldbsearch.c
+++ b/source4/lib/ldb/tools/ldbsearch.c
@@ -64,25 +64,93 @@ static int do_compare_msg(struct ldb_message **el1,
return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
}
+static struct ldb_control **parse_controls(void *mem_ctx, char **control_strings)
+{
+ int i;
+ struct ldb_control **ctrl;
+
+ if (control_strings == NULL || control_strings[0] == NULL)
+ return NULL;
+
+ for (i = 0; control_strings[i]; i++);
+
+ ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
+
+ for (i = 0; control_strings[i]; i++) {
+ if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
+ struct ldb_extended_dn_control *control;
+
+ ctrl[i] = talloc(ctrl, struct ldb_control);
+ ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
+ ctrl[i]->critical = control_strings[i][12]=='1'?1:0;
+ control = talloc(ctrl[i], struct ldb_extended_dn_control);
+ control->type = atoi(&control_strings[i][14]);
+ ctrl[i]->data = control;
+ }
+
+ if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
+ struct ldb_paged_control *control;
+
+ ctrl[i] = talloc(ctrl, struct ldb_control);
+ ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
+ ctrl[i]->critical = control_strings[i][14]=='1'?1:0;
+ control = talloc(ctrl[i], struct ldb_paged_control);
+ control->size = atoi(&control_strings[i][16]);
+ control->cookie = NULL;
+ control->cookie_len = 0;
+ ctrl[i]->data = control;
+ }
+
+ if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
+ struct ldb_server_sort_control **control;
+
+ ctrl[i] = talloc(ctrl, struct ldb_control);
+ ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
+ ctrl[i]->critical = control_strings[i][12]=='1'?1:0;
+ control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
+ control[0] = talloc(control, struct ldb_server_sort_control);
+ control[0]->attributeName = talloc_strdup(control, &control_strings[i][16]);
+ control[0]->orderingRule = NULL;
+ control[0]->reverse = control_strings[i][14]=='1'?1:0;
+ control[1] = NULL;
+ ctrl[i]->data = control;
+ }
+ }
+
+ ctrl[i + 1] = NULL;
+
+ return ctrl;
+}
+
static int do_search(struct ldb_context *ldb,
const struct ldb_dn *basedn,
- int scope,
- int sort_attribs,
+ struct ldb_cmdline *options,
const char *expression,
const char * const *attrs)
{
int ret, i;
+ struct ldb_request req;
struct ldb_result *result = NULL;
- ret = ldb_search(ldb, basedn, scope, expression, attrs, &result);
+ req.operation = LDB_REQ_SEARCH;
+ req.op.search.base = basedn;
+ req.op.search.scope = options->scope;
+ req.op.search.tree = ldb_parse_tree(ldb, expression);
+ req.op.search.attrs = attrs;
+ req.op.search.res = NULL;
+ req.controls = parse_controls(ldb, options->controls);
+ req.creds = NULL;
+
+ ret = ldb_request(ldb, &req);
if (ret != LDB_SUCCESS) {
printf("search failed - %s\n", ldb_errstring(ldb));
return -1;
}
+ result = req.op.search.res;
printf("# returned %d records\n", ret);
- if (sort_attribs) {
+ if (options->sorted) {
ldb_qsort(result->msgs, ret, sizeof(struct ldb_message *),
ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
}
@@ -94,7 +162,7 @@ static int do_search(struct ldb_context *ldb,
ldif.changetype = LDB_CHANGETYPE_NONE;
ldif.msg = result->msgs[i];
- if (sort_attribs) {
+ if (options->sorted) {
/*
* Ensure attributes are always returned in the same
* order. For testing, this makes comparison of old
@@ -154,14 +222,12 @@ static int do_search(struct ldb_context *ldb,
if (options->interactive) {
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
- if (do_search(ldb, basedn,
- options->scope, options->sorted, line, attrs) == -1) {
+ if (do_search(ldb, basedn, options, line, attrs) == -1) {
ret = -1;
}
}
} else {
- ret = do_search(ldb, basedn, options->scope, options->sorted,
- expression, attrs);
+ ret = do_search(ldb, basedn, options, expression, attrs);
}
talloc_free(ldb);