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 = talloc(ldb, struct ldb_request);
75 req->operation = LDB_REQ_SEARCH;
76 req->op.search.base = basedn;
77 req->op.search.scope = options->scope;
78 req->op.search.tree = ldb_parse_tree(ldb, expression);
79 if (req->op.search.tree == NULL) return -1;
80 req->op.search.attrs = attrs;
81 req->op.search.res = NULL;
82 req->controls = parse_controls(ldb, options->controls);
83 if (options->controls != NULL && req->controls == NULL) return -1;
89 ret = ldb_request(ldb, req);
90 if (ret != LDB_SUCCESS) {
91 printf("search failed - %s\n", ldb_errstring(ldb));
92 if (req->op.search.res && req->op.search.res->controls) {
93 handle_controls_reply(req->op.search.res->controls, req->controls);
98 result = req->op.search.res;
100 if (options->sorted) {
101 ldb_qsort(result->msgs, result->count, sizeof(struct ldb_message *),
102 ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
105 for (i = 0; i < result->count; i++, total++) {
106 struct ldb_ldif ldif;
107 printf("# record %d\n", total + 1);
109 ldif.changetype = LDB_CHANGETYPE_NONE;
110 ldif.msg = result->msgs[i];
112 if (options->sorted) {
114 * Ensure attributes are always returned in the same
115 * order. For testing, this makes comparison of old
116 * vs. new much easier.
118 ldb_msg_sort_elements(ldif.msg);
121 ldb_ldif_write_file(ldb, stdout, &ldif);
125 for(n = 0;result->refs[n]; n++, refs++) {
126 printf("# referral %d\nref: %s\n\n", refs + 1, result->refs[n]);
130 if (result->controls) {
131 if (handle_controls_reply(result->controls, req->controls) == 1)
136 ret = talloc_free(result);
138 fprintf(stderr, "talloc_free failed\n");
143 req->op.search.res = NULL;
147 printf("# returned %d records\n# %d entries\n# %d referrals\n", total + refs, total, refs);
152 int main(int argc, const char **argv)
154 struct ldb_context *ldb;
155 struct ldb_dn *basedn = NULL;
156 const char * const * attrs = NULL;
157 struct ldb_cmdline *options;
159 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
163 ldb = ldb_init(NULL);
165 options = ldb_cmdline_process(ldb, argc, argv, usage);
167 /* the check for '=' is for compatibility with ldapsearch */
168 if (!options->interactive &&
170 strchr(options->argv[0], '=')) {
171 expression = options->argv[0];
176 if (options->argc > 0) {
177 attrs = (const char * const *)(options->argv);
180 if (options->basedn != NULL) {
181 basedn = ldb_dn_explode(ldb, options->basedn);
182 if (basedn == NULL) {
183 fprintf(stderr, "Invalid Base DN format\n");
188 if (options->interactive) {
190 while (fgets(line, sizeof(line), stdin)) {
191 if (do_search(ldb, basedn, options, line, attrs) == -1) {
196 ret = do_search(ldb, basedn, options, expression, attrs);