4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldbsearch
30 * Description: utility for ldb search - modelled on ldapsearch
32 * Author: Andrew Tridgell
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
39 #include "ldb/tools/cmdline.h"
42 #include "system/filesys.h"
45 static void usage(void)
47 printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
49 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
50 printf(" -s base|sub|one choose search scope\n");
51 printf(" -b basedn choose baseDN\n");
52 printf(" -i read search expressions from stdin\n");
53 printf(" -S sort returned attributes\n");
54 printf(" -o options pass options like modules to activate\n");
55 printf(" e.g: -o modules:timestamps\n");
59 static int do_compare_msg(struct ldb_message **el1,
60 struct ldb_message **el2,
63 struct ldb_context *ldb = talloc_get_type(opaque, struct ldb_context);
64 return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
67 static struct ldb_control **parse_controls(void *mem_ctx, char **control_strings)
70 struct ldb_control **ctrl;
72 if (control_strings == NULL || control_strings[0] == NULL)
75 for (i = 0; control_strings[i]; i++);
77 ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
79 for (i = 0; control_strings[i]; i++) {
80 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
81 struct ldb_extended_dn_control *control;
83 ctrl[i] = talloc(ctrl, struct ldb_control);
84 ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
85 ctrl[i]->critical = control_strings[i][12]=='1'?1:0;
86 control = talloc(ctrl[i], struct ldb_extended_dn_control);
87 control->type = atoi(&control_strings[i][14]);
88 ctrl[i]->data = control;
91 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
92 struct ldb_paged_control *control;
94 ctrl[i] = talloc(ctrl, struct ldb_control);
95 ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
96 ctrl[i]->critical = control_strings[i][14]=='1'?1:0;
97 control = talloc(ctrl[i], struct ldb_paged_control);
98 control->size = atoi(&control_strings[i][16]);
99 control->cookie = NULL;
100 control->cookie_len = 0;
101 ctrl[i]->data = control;
104 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
105 struct ldb_server_sort_control **control;
107 ctrl[i] = talloc(ctrl, struct ldb_control);
108 ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
109 ctrl[i]->critical = control_strings[i][12]=='1'?1:0;
110 control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
111 control[0] = talloc(control, struct ldb_server_sort_control);
112 control[0]->attributeName = talloc_strdup(control, &control_strings[i][16]);
113 control[0]->orderingRule = NULL;
114 control[0]->reverse = control_strings[i][14]=='1'?1:0;
116 ctrl[i]->data = control;
125 static int do_search(struct ldb_context *ldb,
126 const struct ldb_dn *basedn,
127 struct ldb_cmdline *options,
128 const char *expression,
129 const char * const *attrs)
132 struct ldb_request req;
133 struct ldb_result *result = NULL;
135 req.operation = LDB_REQ_SEARCH;
136 req.op.search.base = basedn;
137 req.op.search.scope = options->scope;
138 req.op.search.tree = ldb_parse_tree(ldb, expression);
139 req.op.search.attrs = attrs;
140 req.op.search.res = NULL;
141 req.controls = parse_controls(ldb, options->controls);
144 ret = ldb_request(ldb, &req);
145 if (ret != LDB_SUCCESS) {
146 printf("search failed - %s\n", ldb_errstring(ldb));
150 result = req.op.search.res;
151 printf("# returned %d records\n", ret);
153 if (options->sorted) {
154 ldb_qsort(result->msgs, ret, sizeof(struct ldb_message *),
155 ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
158 for (i = 0; i < result->count; i++) {
159 struct ldb_ldif ldif;
160 printf("# record %d\n", i+1);
162 ldif.changetype = LDB_CHANGETYPE_NONE;
163 ldif.msg = result->msgs[i];
165 if (options->sorted) {
167 * Ensure attributes are always returned in the same
168 * order. For testing, this makes comparison of old
169 * vs. new much easier.
171 ldb_msg_sort_elements(ldif.msg);
174 ldb_ldif_write_file(ldb, stdout, &ldif);
178 ret = talloc_free(result);
180 fprintf(stderr, "talloc_free failed\n");
188 int main(int argc, const char **argv)
190 struct ldb_context *ldb;
191 struct ldb_dn *basedn = NULL;
192 const char * const * attrs = NULL;
193 struct ldb_cmdline *options;
195 const char *expression = "(objectclass=*)";
197 ldb = ldb_init(NULL);
199 options = ldb_cmdline_process(ldb, argc, argv, usage);
201 /* the check for '=' is for compatibility with ldapsearch */
202 if (!options->interactive &&
204 strchr(options->argv[0], '=')) {
205 expression = options->argv[0];
210 if (options->argc > 0) {
211 attrs = (const char * const *)(options->argv);
214 if (options->basedn != NULL) {
215 basedn = ldb_dn_explode(ldb, options->basedn);
216 if (basedn == NULL) {
217 fprintf(stderr, "Invalid Base DN format\n");
222 if (options->interactive) {
224 while (fgets(line, sizeof(line), stdin)) {
225 if (do_search(ldb, basedn, options, line, attrs) == -1) {
230 ret = do_search(ldb, basedn, options, expression, attrs);