source: trunk/server/source4/lib/ldb/tools/ldbsearch.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 7.7 KB
Line 
1/*
2 ldb database library
3
4 Copyright (C) Andrew Tridgell 2004
5
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
9
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 3 of the License, or (at your option) any later version.
14
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.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
22*/
23
24/*
25 * Name: ldb
26 *
27 * Component: ldbsearch
28 *
29 * Description: utility for ldb search - modelled on ldapsearch
30 *
31 * Author: Andrew Tridgell
32 */
33
34#include "replace.h"
35#include "system/filesys.h"
36#include "system/time.h"
37#include "ldb.h"
38#include "tools/cmdline.h"
39
40static void usage(struct ldb_context *ldb)
41{
42 printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
43 ldb_cmdline_help(ldb, "ldbsearch", stdout);
44 exit(LDB_ERR_OPERATIONS_ERROR);
45}
46
47static int do_compare_msg(struct ldb_message **el1,
48 struct ldb_message **el2,
49 void *opaque)
50{
51 return ldb_dn_compare((*el1)->dn, (*el2)->dn);
52}
53
54struct search_context {
55 struct ldb_context *ldb;
56 struct ldb_control **req_ctrls;
57
58 int sort;
59 unsigned int num_stored;
60 struct ldb_message **store;
61 unsigned int refs_stored;
62 char **refs_store;
63
64 unsigned int entries;
65 unsigned int refs;
66
67 unsigned int pending;
68 int status;
69};
70
71static int store_message(struct ldb_message *msg, struct search_context *sctx) {
72
73 sctx->store = talloc_realloc(sctx, sctx->store, struct ldb_message *, sctx->num_stored + 2);
74 if (!sctx->store) {
75 fprintf(stderr, "talloc_realloc failed while storing messages\n");
76 return -1;
77 }
78
79 sctx->store[sctx->num_stored] = talloc_move(sctx->store, &msg);
80 sctx->num_stored++;
81 sctx->store[sctx->num_stored] = NULL;
82
83 return 0;
84}
85
86static int store_referral(char *referral, struct search_context *sctx) {
87
88 sctx->refs_store = talloc_realloc(sctx, sctx->refs_store, char *, sctx->refs_stored + 2);
89 if (!sctx->refs_store) {
90 fprintf(stderr, "talloc_realloc failed while storing referrals\n");
91 return -1;
92 }
93
94 sctx->refs_store[sctx->refs_stored] = talloc_move(sctx->refs_store, &referral);
95 sctx->refs_stored++;
96 sctx->refs_store[sctx->refs_stored] = NULL;
97
98 return 0;
99}
100
101static int display_message(struct ldb_message *msg, struct search_context *sctx) {
102 struct ldb_ldif ldif;
103
104 sctx->entries++;
105 printf("# record %d\n", sctx->entries);
106
107 ldif.changetype = LDB_CHANGETYPE_NONE;
108 ldif.msg = msg;
109
110 if (sctx->sort) {
111 /*
112 * Ensure attributes are always returned in the same
113 * order. For testing, this makes comparison of old
114 * vs. new much easier.
115 */
116 ldb_msg_sort_elements(ldif.msg);
117 }
118
119 ldb_ldif_write_file(sctx->ldb, stdout, &ldif);
120
121 return 0;
122}
123
124static int display_referral(char *referral, struct search_context *sctx)
125{
126
127 sctx->refs++;
128 printf("# Referral\nref: %s\n\n", referral);
129
130 return 0;
131}
132
133static int search_callback(struct ldb_request *req, struct ldb_reply *ares)
134{
135 struct search_context *sctx;
136 int ret = LDB_SUCCESS;
137
138 sctx = talloc_get_type(req->context, struct search_context);
139
140 if (!ares) {
141 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
142 }
143 if (ares->error != LDB_SUCCESS) {
144 return ldb_request_done(req, ares->error);
145 }
146
147 switch (ares->type) {
148 case LDB_REPLY_ENTRY:
149 if (sctx->sort) {
150 ret = store_message(ares->message, sctx);
151 } else {
152 ret = display_message(ares->message, sctx);
153 }
154 break;
155
156 case LDB_REPLY_REFERRAL:
157 if (sctx->sort) {
158 ret = store_referral(ares->referral, sctx);
159 } else {
160 ret = display_referral(ares->referral, sctx);
161 }
162 if (ret) {
163 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
164 }
165 break;
166
167 case LDB_REPLY_DONE:
168 if (ares->controls) {
169 if (handle_controls_reply(ares->controls, sctx->req_ctrls) == 1)
170 sctx->pending = 1;
171 }
172 talloc_free(ares);
173 return ldb_request_done(req, LDB_SUCCESS);
174 }
175
176 talloc_free(ares);
177 if (ret != LDB_SUCCESS) {
178 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
179 }
180
181 return LDB_SUCCESS;
182}
183
184static int do_search(struct ldb_context *ldb,
185 struct ldb_dn *basedn,
186 struct ldb_cmdline *options,
187 const char *expression,
188 const char * const *attrs)
189{
190 struct ldb_request *req;
191 struct search_context *sctx;
192 int ret;
193
194 req = NULL;
195
196 sctx = talloc_zero(ldb, struct search_context);
197 if (!sctx) return LDB_ERR_OPERATIONS_ERROR;
198
199 sctx->ldb = ldb;
200 sctx->sort = options->sorted;
201 sctx->req_ctrls = ldb_parse_control_strings(ldb, sctx, (const char **)options->controls);
202 if (options->controls != NULL && sctx->req_ctrls== NULL) {
203 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
204 return LDB_ERR_OPERATIONS_ERROR;
205 }
206
207 if (basedn == NULL) {
208 basedn = ldb_get_default_basedn(ldb);
209 }
210
211again:
212 /* free any previous requests */
213 if (req) talloc_free(req);
214
215 ret = ldb_build_search_req(&req, ldb, ldb,
216 basedn, options->scope,
217 expression, attrs,
218 sctx->req_ctrls,
219 sctx, search_callback,
220 NULL);
221 if (ret != LDB_SUCCESS) {
222 talloc_free(sctx);
223 printf("allocating request failed: %s\n", ldb_errstring(ldb));
224 return ret;
225 }
226
227 sctx->pending = 0;
228
229 ret = ldb_request(ldb, req);
230 if (ret != LDB_SUCCESS) {
231 printf("search failed - %s\n", ldb_errstring(ldb));
232 return ret;
233 }
234
235 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
236 if (ret != LDB_SUCCESS) {
237 printf("search error - %s\n", ldb_errstring(ldb));
238 return ret;
239 }
240
241 if (sctx->pending)
242 goto again;
243
244 if (sctx->sort && (sctx->num_stored != 0 || sctx->refs != 0)) {
245 unsigned int i;
246
247 if (sctx->num_stored) {
248 LDB_TYPESAFE_QSORT(sctx->store, sctx->num_stored, ldb, do_compare_msg);
249 }
250 for (i = 0; i < sctx->num_stored; i++) {
251 display_message(sctx->store[i], sctx);
252 }
253
254 for (i = 0; i < sctx->refs_stored; i++) {
255 display_referral(sctx->refs_store[i], sctx);
256 }
257 }
258
259 printf("# returned %u records\n# %u entries\n# %u referrals\n",
260 sctx->entries + sctx->refs, sctx->entries, sctx->refs);
261
262 talloc_free(sctx);
263 talloc_free(req);
264
265 return LDB_SUCCESS;
266}
267
268int main(int argc, const char **argv)
269{
270 struct ldb_context *ldb;
271 struct ldb_dn *basedn = NULL;
272 const char * const * attrs = NULL;
273 struct ldb_cmdline *options;
274 int ret = -1;
275 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
276 TALLOC_CTX *mem_ctx = talloc_new(NULL);
277
278 ldb = ldb_init(mem_ctx, NULL);
279 if (ldb == NULL) {
280 return LDB_ERR_OPERATIONS_ERROR;
281 }
282
283 options = ldb_cmdline_process(ldb, argc, argv, usage);
284
285 /* the check for '=' is for compatibility with ldapsearch */
286 if (!options->interactive &&
287 options->argc > 0 &&
288 strchr(options->argv[0], '=')) {
289 expression = options->argv[0];
290 options->argv++;
291 options->argc--;
292 }
293
294 if (options->argc > 0) {
295 attrs = (const char * const *)(options->argv);
296 }
297
298 if (options->basedn != NULL) {
299 basedn = ldb_dn_new(ldb, ldb, options->basedn);
300 if (basedn == NULL) {
301 return LDB_ERR_OPERATIONS_ERROR;
302 }
303 }
304
305 if (options->interactive) {
306 char line[1024];
307 while (fgets(line, sizeof(line), stdin)) {
308 ret = do_search(ldb, basedn, options, line, attrs);
309 }
310 } else {
311 ret = do_search(ldb, basedn, options, expression, attrs);
312 }
313
314 talloc_free(mem_ctx);
315
316 return ret;
317}
Note: See TracBrowser for help on using the repository browser.