b7e2ce7f68698608c7fa84de95019334544a1e48
[tprouty/samba.git] / source3 / 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 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, 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_move(sctx->store, &msg);
85         sctx->num_stored++;
86         sctx->store[sctx->num_stored] = NULL;
87
88         return 0;
89 }
90
91 static int store_referral(char *referral, struct search_context *sctx) {
92
93         sctx->refs_store = talloc_realloc(sctx, sctx->refs_store, char *, sctx->refs + 2);
94         if (!sctx->refs_store) {
95                 fprintf(stderr, "talloc_realloc failed while storing referrals\n");
96                 return -1;
97         }
98
99         sctx->refs_store[sctx->refs] = talloc_move(sctx->refs_store, &referral);
100         sctx->refs++;
101         sctx->refs_store[sctx->refs] = NULL;
102
103         return 0;
104 }
105
106 static int display_message(struct ldb_context *ldb, struct ldb_message *msg, struct search_context *sctx) {
107         struct ldb_ldif ldif;
108
109         sctx->entries++;
110         printf("# record %d\n", sctx->entries);
111
112         ldif.changetype = LDB_CHANGETYPE_NONE;
113         ldif.msg = msg;
114
115         if (sctx->sort) {
116         /*
117          * Ensure attributes are always returned in the same
118          * order.  For testing, this makes comparison of old
119          * vs. new much easier.
120          */
121                 ldb_msg_sort_elements(ldif.msg);
122         }
123
124         ldb_ldif_write_file(ldb, stdout, &ldif);
125
126         return 0;
127 }
128
129 static int display_referral(char *referral, struct search_context *sctx)
130 {
131
132         sctx->refs++;
133         printf("# Referral\nref: %s\n\n", referral);
134
135         return 0;
136 }
137
138 static int search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
139 {
140         struct search_context *sctx = talloc_get_type(context, struct search_context);
141         int ret;
142         
143         switch (ares->type) {
144
145         case LDB_REPLY_ENTRY:
146                 if (sctx->sort) {
147                         ret = store_message(ares->message, sctx);
148                 } else {
149                         ret = display_message(ldb, ares->message, sctx);
150                 }
151                 break;
152
153         case LDB_REPLY_REFERRAL:
154                 if (sctx->sort) {
155                         ret = store_referral(ares->referral, sctx);
156                 } else {
157                         ret = display_referral(ares->referral, sctx);
158                 }
159                 break;
160
161         case LDB_REPLY_DONE:
162                 if (ares->controls) {
163                         if (handle_controls_reply(ares->controls, sctx->req_ctrls) == 1)
164                                 sctx->pending = 1;
165                 }
166                 ret = 0;
167                 break;
168                 
169         default:
170                 fprintf(stderr, "unknown Reply Type\n");
171                 return LDB_ERR_OTHER;
172         }
173
174         if (talloc_free(ares) == -1) {
175                 fprintf(stderr, "talloc_free failed\n");
176                 sctx->pending = 0;
177                 return LDB_ERR_OPERATIONS_ERROR;
178         }
179
180         if (ret) {
181                 return LDB_ERR_OPERATIONS_ERROR;
182         }
183
184         return LDB_SUCCESS;
185 }
186
187 static int do_search(struct ldb_context *ldb,
188                      const struct ldb_dn *basedn,
189                      struct ldb_cmdline *options,
190                      const char *expression,
191                      const char * const *attrs)
192 {
193         struct ldb_request *req;
194         struct search_context *sctx;
195         int ret;
196
197         req = talloc(ldb, struct ldb_request);
198         if (!req) return -1;
199         
200         sctx = talloc(req, struct search_context);
201         if (!sctx) return -1;
202
203         sctx->sort = options->sorted;
204         sctx->num_stored = 0;
205         sctx->store = NULL;
206         sctx->req_ctrls = parse_controls(ldb, options->controls);
207         if (options->controls != NULL &&  sctx->req_ctrls== NULL) return -1;
208         sctx->entries = 0;
209         sctx->refs = 0;
210
211         if (basedn == NULL) {
212                 basedn = ldb_get_default_basedn(ldb);
213         }
214
215         req->operation = LDB_SEARCH;
216         req->op.search.base = basedn;
217         req->op.search.scope = options->scope;
218         req->op.search.tree = ldb_parse_tree(req, expression);
219         if (req->op.search.tree == NULL) return -1;
220         req->op.search.attrs = attrs;
221         req->controls = sctx->req_ctrls;
222         req->context = sctx;
223         req->callback = &search_callback;
224         ldb_set_timeout(ldb, req, 0); /* TODO: make this settable by command line */
225
226 again:
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 -1;
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 -1;
239         }
240
241         if (sctx->pending)
242                 goto again;
243
244         if (sctx->sort && sctx->num_stored != 0) {
245                 int i;
246
247                 ldb_qsort(sctx->store, ret, sizeof(struct ldb_message *),
248                           ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
249
250                 if (ret != 0) {
251                         fprintf(stderr, "An error occurred while sorting messages\n");
252                         exit(1);
253                 }
254
255                 for (i = 0; i < sctx->num_stored; i++) {
256                         display_message(ldb, sctx->store[i], sctx);
257                 }
258
259                 for (i = 0; i < sctx->refs; i++) {
260                         display_referral(sctx->refs_store[i], sctx);
261                 }
262         }
263
264         printf("# returned %d records\n# %d entries\n# %d referrals\n",
265                 sctx->entries + sctx->refs, sctx->entries, sctx->refs);
266
267         talloc_free(req);
268
269         return 0;
270 }
271
272 int main(int argc, const char **argv)
273 {
274         struct ldb_context *ldb;
275         struct ldb_dn *basedn = NULL;
276         const char * const * attrs = NULL;
277         struct ldb_cmdline *options;
278         int ret = -1;
279         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
280
281         ldb_global_init();
282
283         ldb = ldb_init(NULL);
284
285         options = ldb_cmdline_process(ldb, argc, argv, usage);
286
287         /* the check for '=' is for compatibility with ldapsearch */
288         if (!options->interactive &&
289             options->argc > 0 && 
290             strchr(options->argv[0], '=')) {
291                 expression = options->argv[0];
292                 options->argv++;
293                 options->argc--;
294         }
295
296         if (options->argc > 0) {
297                 attrs = (const char * const *)(options->argv);
298         }
299
300         if (options->basedn != NULL) {
301                 basedn = ldb_dn_explode(ldb, options->basedn);
302                 if (basedn == NULL) {
303                         fprintf(stderr, "Invalid Base DN format\n");
304                         exit(1);
305                 }
306         }
307
308         if (options->interactive) {
309                 char line[1024];
310                 while (fgets(line, sizeof(line), stdin)) {
311                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
312                                 ret = -1;
313                         }
314                 }
315         } else {
316                 ret = do_search(ldb, basedn, options, expression, attrs);
317         }
318
319         talloc_free(ldb);
320         return ret;
321 }