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/includes.h"
37 #include "ldb/tools/cmdline.h"
39 static void usage(void)
41 printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
43 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
44 printf(" -s base|sub|one choose search scope\n");
45 printf(" -b basedn choose baseDN\n");
46 printf(" -i read search expressions from stdin\n");
47 printf(" -S sort returned attributes\n");
48 printf(" -o options pass options like modules to activate\n");
49 printf(" e.g: -o modules:timestamps\n");
53 static int do_compare_msg(struct ldb_message **el1,
54 struct ldb_message **el2,
57 struct ldb_context *ldb = talloc_get_type(opaque, struct ldb_context);
58 return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
61 struct async_context {
62 struct ldb_control **req_ctrls;
66 struct ldb_message **store;
76 static int store_message(struct ldb_message *msg, struct async_context *actx) {
78 actx->store = talloc_realloc(actx, actx->store, struct ldb_message *, actx->num_stored + 2);
80 fprintf(stderr, "talloc_realloc failed while storing messages\n");
84 actx->store[actx->num_stored] = talloc_steal(actx->store, msg);
85 if (!actx->store[actx->num_stored]) {
86 fprintf(stderr, "talloc_steal failed while storing messages\n");
91 actx->store[actx->num_stored] = NULL;
96 static int store_referral(char *referral, struct async_context *actx) {
98 actx->refs_store = talloc_realloc(actx, actx->refs_store, char *, actx->refs + 2);
99 if (!actx->refs_store) {
100 fprintf(stderr, "talloc_realloc failed while storing referrals\n");
104 actx->refs_store[actx->refs] = talloc_steal(actx->refs_store, referral);
105 if (!actx->refs_store[actx->refs]) {
106 fprintf(stderr, "talloc_steal failed while storing referrals\n");
111 actx->refs_store[actx->refs] = NULL;
116 static int display_message(struct ldb_context *ldb, struct ldb_message *msg, struct async_context *actx) {
117 struct ldb_ldif ldif;
120 printf("# record %d\n", actx->entries);
122 ldif.changetype = LDB_CHANGETYPE_NONE;
127 * Ensure attributes are always returned in the same
128 * order. For testing, this makes comparison of old
129 * vs. new much easier.
131 ldb_msg_sort_elements(ldif.msg);
134 ldb_ldif_write_file(ldb, stdout, &ldif);
139 static int display_referral(char *referral, struct async_context *actx) {
142 printf("# Referral\nref: %s\n\n", referral);
147 static int search_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
149 struct async_context *actx = talloc_get_type(context, struct async_context);
152 switch (ares->type) {
154 case LDB_REPLY_ENTRY:
156 ret = store_message(ares->message, actx);
158 ret = display_message(ldb, ares->message, actx);
162 case LDB_REPLY_REFERRAL:
164 ret = store_referral(ares->referral, actx);
166 ret = display_referral(ares->referral, actx);
171 if (ares->controls) {
172 if (handle_controls_reply(ares->controls, actx->req_ctrls) == 1)
179 fprintf(stderr, "unknown Reply Type\n");
180 return LDB_ERR_OTHER;
183 if (talloc_free(ares) == -1) {
184 fprintf(stderr, "talloc_free failed\n");
186 return LDB_ERR_OPERATIONS_ERROR;
190 return LDB_ERR_OPERATIONS_ERROR;
196 static int do_search(struct ldb_context *ldb,
197 const struct ldb_dn *basedn,
198 struct ldb_cmdline *options,
199 const char *expression,
200 const char * const *attrs)
202 struct ldb_request *req;
203 struct async_context *actx;
206 req = talloc(ldb, struct ldb_request);
209 actx = talloc(req, struct async_context);
210 if (!actx) return -1;
212 actx->sort = options->sorted;
213 actx->num_stored = 0;
215 actx->req_ctrls = parse_controls(ldb, options->controls);
216 if (options->controls != NULL && actx->req_ctrls== NULL) return -1;
220 req->operation = LDB_ASYNC_SEARCH;
221 req->op.search.base = basedn;
222 req->op.search.scope = options->scope;
223 req->op.search.tree = ldb_parse_tree(ldb, expression);
224 if (req->op.search.tree == NULL) return -1;
225 req->op.search.attrs = attrs;
226 req->controls = actx->req_ctrls;
227 req->async.context = actx;
228 req->async.callback = &search_callback;
229 req->async.timeout = 3600; /* TODO: make this settable by command line */
234 ret = ldb_request(ldb, req);
235 if (ret != LDB_SUCCESS) {
236 printf("search failed - %s\n", ldb_errstring(ldb));
240 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
241 if (ret != LDB_SUCCESS) {
242 printf("search error - %s\n", ldb_errstring(ldb));
249 if (actx->sort && actx->num_stored != 0) {
252 ldb_qsort(actx->store, ret, sizeof(struct ldb_message *),
253 ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
256 fprintf(stderr, "An error occurred while sorting messages\n");
260 for (i = 0; i < actx->num_stored; i++) {
261 display_message(ldb, actx->store[i], actx);
264 for (i = 0; i < actx->refs; i++) {
265 display_referral(actx->refs_store[i], actx);
269 printf("# returned %d records\n# %d entries\n# %d referrals\n",
270 actx->entries + actx->refs, actx->entries, actx->refs);
277 int main(int argc, const char **argv)
279 struct ldb_context *ldb;
280 struct ldb_dn *basedn = NULL;
281 const char * const * attrs = NULL;
282 struct ldb_cmdline *options;
284 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
288 ldb = ldb_init(NULL);
290 options = ldb_cmdline_process(ldb, argc, argv, usage);
292 /* the check for '=' is for compatibility with ldapsearch */
293 if (!options->interactive &&
295 strchr(options->argv[0], '=')) {
296 expression = options->argv[0];
301 if (options->argc > 0) {
302 attrs = (const char * const *)(options->argv);
305 if (options->basedn != NULL) {
306 basedn = ldb_dn_explode(ldb, options->basedn);
307 if (basedn == NULL) {
308 fprintf(stderr, "Invalid Base DN format\n");
313 if (options->interactive) {
315 while (fgets(line, sizeof(line), stdin)) {
316 if (do_search(ldb, basedn, options, line, attrs) == -1) {
321 ret = do_search(ldb, basedn, options, expression, attrs);