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 static int do_search(struct ldb_context *ldb,
62 const struct ldb_dn *basedn,
63 struct ldb_cmdline *options,
64 const char *expression,
65 const char * const *attrs)
71 struct ldb_request req;
72 struct ldb_result *result = NULL;
74 req.operation = LDB_REQ_SEARCH;
75 req.op.search.base = basedn;
76 req.op.search.scope = options->scope;
77 req.op.search.tree = ldb_parse_tree(ldb, expression);
78 if (req.op.search.tree == NULL) return -1;
79 req.op.search.attrs = attrs;
80 req.op.search.res = NULL;
81 req.controls = parse_controls(ldb, options->controls);
82 if (options->controls != NULL && req.controls == NULL) return -1;
88 ret = ldb_request(ldb, &req);
89 if (ret != LDB_SUCCESS) {
90 printf("search failed - %s\n", ldb_errstring(ldb));
91 if (req.op.search.res && req.op.search.res->controls) {
92 handle_controls_reply(req.op.search.res->controls, req.controls);
97 result = req.op.search.res;
99 if (options->sorted) {
100 ldb_qsort(result->msgs, result->count, sizeof(struct ldb_message *),
101 ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
104 for (i = 0; i < result->count; i++, total++) {
105 struct ldb_ldif ldif;
106 printf("# record %d\n", total + 1);
108 ldif.changetype = LDB_CHANGETYPE_NONE;
109 ldif.msg = result->msgs[i];
111 if (options->sorted) {
113 * Ensure attributes are always returned in the same
114 * order. For testing, this makes comparison of old
115 * vs. new much easier.
117 ldb_msg_sort_elements(ldif.msg);
120 ldb_ldif_write_file(ldb, stdout, &ldif);
124 for(n = 0;result->refs[n]; n++, refs++) {
125 printf("# referral %d\nref: %s\n\n", refs + 1, result->refs[n]);
129 if (result->controls) {
130 if (handle_controls_reply(result->controls, req.controls) == 1)
135 ret = talloc_free(result);
137 fprintf(stderr, "talloc_free failed\n");
142 req.op.search.res = NULL;
146 printf("# returned %d records\n# %d entries\n# %d referrals\n", total + refs, total, refs);
151 int main(int argc, const char **argv)
153 struct ldb_context *ldb;
154 struct ldb_dn *basedn = NULL;
155 const char * const * attrs = NULL;
156 struct ldb_cmdline *options;
158 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
160 ldb = ldb_init(NULL);
162 options = ldb_cmdline_process(ldb, argc, argv, usage);
164 /* the check for '=' is for compatibility with ldapsearch */
165 if (!options->interactive &&
167 strchr(options->argv[0], '=')) {
168 expression = options->argv[0];
173 if (options->argc > 0) {
174 attrs = (const char * const *)(options->argv);
177 if (options->basedn != NULL) {
178 basedn = ldb_dn_explode(ldb, options->basedn);
179 if (basedn == NULL) {
180 fprintf(stderr, "Invalid Base DN format\n");
185 if (options->interactive) {
187 while (fgets(line, sizeof(line), stdin)) {
188 if (do_search(ldb, basedn, options, line, attrs) == -1) {
193 ret = do_search(ldb, basedn, options, expression, attrs);