r17821: changed ldb_search() and the ldbsearch command line utility to
[ira/wip.git] / source4 / lib / ldb / tools / ldbsearch.c
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 2 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, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldbsearch
29  *
30  *  Description: utility for ldb search - modelled on ldapsearch
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37 #include "ldb/tools/cmdline.h"
38
39 static void usage(void)
40 {
41         printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
42         printf("Options:\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");
50         exit(1);
51 }
52
53 static int do_compare_msg(struct ldb_message **el1,
54                           struct ldb_message **el2,
55                           void *opaque)
56 {
57         struct ldb_context *ldb = talloc_get_type(opaque, struct ldb_context);
58         return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
59 }
60
61 struct search_context {
62         struct ldb_control **req_ctrls;
63
64         int sort;
65         int num_stored;
66         struct ldb_message **store;
67         char **refs_store;
68
69         int entries;
70         int refs;
71
72         int pending;
73         int status;
74 };
75
76 static int store_message(struct ldb_message *msg, struct search_context *sctx) {
77
78         sctx->store = talloc_realloc(sctx, sctx->store, struct ldb_message *, sctx->num_stored + 2);
79         if (!sctx->store) {
80                 fprintf(stderr, "talloc_realloc failed while storing messages\n");
81                 return -1;
82         }
83
84         sctx->store[sctx->num_stored] = talloc_steal(sctx->store, msg);
85         if (!sctx->store[sctx->num_stored]) {
86                 fprintf(stderr, "talloc_steal failed while storing messages\n");
87                 return -1;
88         }
89
90         sctx->num_stored++;
91         sctx->store[sctx->num_stored] = NULL;
92
93         return 0;
94 }
95
96 static int store_referral(char *referral, struct search_context *sctx) {
97
98         sctx->refs_store = talloc_realloc(sctx, sctx->refs_store, char *, sctx->refs + 2);
99         if (!sctx->refs_store) {
100                 fprintf(stderr, "talloc_realloc failed while storing referrals\n");
101                 return -1;
102         }
103
104         sctx->refs_store[sctx->refs] = talloc_steal(sctx->refs_store, referral);
105         if (!sctx->refs_store[sctx->refs]) {
106                 fprintf(stderr, "talloc_steal failed while storing referrals\n");
107                 return -1;
108         }
109
110         sctx->refs++;
111         sctx->refs_store[sctx->refs] = NULL;
112
113         return 0;
114 }
115
116 static int display_message(struct ldb_context *ldb, struct ldb_message *msg, struct search_context *sctx) {
117         struct ldb_ldif ldif;
118
119         sctx->entries++;
120         printf("# record %d\n", sctx->entries);
121
122         ldif.changetype = LDB_CHANGETYPE_NONE;
123         ldif.msg = msg;
124
125         if (sctx->sort) {
126         /*
127          * Ensure attributes are always returned in the same
128          * order.  For testing, this makes comparison of old
129          * vs. new much easier.
130          */
131                 ldb_msg_sort_elements(ldif.msg);
132         }
133
134         ldb_ldif_write_file(ldb, stdout, &ldif);
135
136         return 0;
137 }
138
139 static int display_referral(char *referral, struct search_context *sctx)
140 {
141
142         sctx->refs++;
143         printf("# Referral\nref: %s\n\n", referral);
144
145         return 0;
146 }
147
148 static int search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
149 {
150         struct search_context *sctx = talloc_get_type(context, struct search_context);
151         int ret;
152         
153         switch (ares->type) {
154
155         case LDB_REPLY_ENTRY:
156                 if (sctx->sort) {
157                         ret = store_message(ares->message, sctx);
158                 } else {
159                         ret = display_message(ldb, ares->message, sctx);
160                 }
161                 break;
162
163         case LDB_REPLY_REFERRAL:
164                 if (sctx->sort) {
165                         ret = store_referral(ares->referral, sctx);
166                 } else {
167                         ret = display_referral(ares->referral, sctx);
168                 }
169                 break;
170
171         case LDB_REPLY_DONE:
172                 if (ares->controls) {
173                         if (handle_controls_reply(ares->controls, sctx->req_ctrls) == 1)
174                                 sctx->pending = 1;
175                 }
176                 ret = 0;
177                 break;
178                 
179         default:
180                 fprintf(stderr, "unknown Reply Type\n");
181                 return LDB_ERR_OTHER;
182         }
183
184         if (talloc_free(ares) == -1) {
185                 fprintf(stderr, "talloc_free failed\n");
186                 sctx->pending = 0;
187                 return LDB_ERR_OPERATIONS_ERROR;
188         }
189
190         if (ret) {
191                 return LDB_ERR_OPERATIONS_ERROR;
192         }
193
194         return LDB_SUCCESS;
195 }
196
197 static int do_search(struct ldb_context *ldb,
198                      const struct ldb_dn *basedn,
199                      struct ldb_cmdline *options,
200                      const char *expression,
201                      const char * const *attrs)
202 {
203         struct ldb_request *req;
204         struct search_context *sctx;
205         int ret;
206
207         req = talloc(ldb, struct ldb_request);
208         if (!req) return -1;
209         
210         sctx = talloc(req, struct search_context);
211         if (!sctx) return -1;
212
213         sctx->sort = options->sorted;
214         sctx->num_stored = 0;
215         sctx->store = NULL;
216         sctx->req_ctrls = parse_controls(ldb, options->controls);
217         if (options->controls != NULL &&  sctx->req_ctrls== NULL) return -1;
218         sctx->entries = 0;
219         sctx->refs = 0;
220
221         if (basedn == NULL) {
222                 basedn = ldb_auto_basedn(ldb);
223         }
224
225         req->operation = LDB_SEARCH;
226         req->op.search.base = basedn;
227         req->op.search.scope = options->scope;
228         req->op.search.tree = ldb_parse_tree(ldb, expression);
229         if (req->op.search.tree == NULL) return -1;
230         req->op.search.attrs = attrs;
231         req->controls = sctx->req_ctrls;
232         req->context = sctx;
233         req->callback = &search_callback;
234         ldb_set_timeout(ldb, req, 0); /* TODO: make this settable by command line */
235
236 again:
237         sctx->pending = 0;
238
239         ret = ldb_request(ldb, req);
240         if (ret != LDB_SUCCESS) {
241                 printf("search failed - %s\n", ldb_errstring(ldb));
242                 return -1;
243         }
244
245         ret = ldb_wait(req->handle, LDB_WAIT_ALL);
246         if (ret != LDB_SUCCESS) {
247                 printf("search error - %s\n", ldb_errstring(ldb));
248                 return -1;
249         }
250
251         if (sctx->pending)
252                 goto again;
253
254         if (sctx->sort && sctx->num_stored != 0) {
255                 int i;
256
257                 ldb_qsort(sctx->store, ret, sizeof(struct ldb_message *),
258                           ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
259
260                 if (ret != 0) {
261                         fprintf(stderr, "An error occurred while sorting messages\n");
262                         exit(1);
263                 }
264
265                 for (i = 0; i < sctx->num_stored; i++) {
266                         display_message(ldb, sctx->store[i], sctx);
267                 }
268
269                 for (i = 0; i < sctx->refs; i++) {
270                         display_referral(sctx->refs_store[i], sctx);
271                 }
272         }
273
274         printf("# returned %d records\n# %d entries\n# %d referrals\n",
275                 sctx->entries + sctx->refs, sctx->entries, sctx->refs);
276
277         talloc_free(req);
278
279         return 0;
280 }
281
282 int main(int argc, const char **argv)
283 {
284         struct ldb_context *ldb;
285         struct ldb_dn *basedn = NULL;
286         const char * const * attrs = NULL;
287         struct ldb_cmdline *options;
288         int ret = -1;
289         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
290
291         ldb_global_init();
292
293         ldb = ldb_init(NULL);
294
295         options = ldb_cmdline_process(ldb, argc, argv, usage);
296
297         /* the check for '=' is for compatibility with ldapsearch */
298         if (!options->interactive &&
299             options->argc > 0 && 
300             strchr(options->argv[0], '=')) {
301                 expression = options->argv[0];
302                 options->argv++;
303                 options->argc--;
304         }
305
306         if (options->argc > 0) {
307                 attrs = (const char * const *)(options->argv);
308         }
309
310         if (options->basedn != NULL) {
311                 basedn = ldb_dn_explode(ldb, options->basedn);
312                 if (basedn == NULL) {
313                         fprintf(stderr, "Invalid Base DN format\n");
314                         exit(1);
315                 }
316         }
317
318         if (options->interactive) {
319                 char line[1024];
320                 while (fgets(line, sizeof(line), stdin)) {
321                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
322                                 ret = -1;
323                         }
324                 }
325         } else {
326                 ret = do_search(ldb, basedn, options, expression, attrs);
327         }
328
329         talloc_free(ldb);
330         return ret;
331 }