Rework ldbsearch to avoid segfault when remote LDAP server returns
[ira/wip.git] / source / 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, 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 "ldb_includes.h"
35 #include "tools/cmdline.h"
36
37 static void usage(void)
38 {
39         printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
40         printf("Options:\n");
41         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
42         printf("  -s base|sub|one  choose search scope\n");
43         printf("  -b basedn        choose baseDN\n");
44         printf("  -i               read search expressions from stdin\n");
45         printf("  -S               sort returned attributes\n");
46         printf("  -o options       pass options like modules to activate\n");
47         printf("              e.g: -o modules:timestamps\n");
48         exit(1);
49 }
50
51 static int do_compare_msg(struct ldb_message **el1,
52                           struct ldb_message **el2,
53                           void *opaque)
54 {
55         return ldb_dn_compare((*el1)->dn, (*el2)->dn);
56 }
57
58 struct search_context {
59         struct ldb_control **req_ctrls;
60
61         int sort;
62         int num_stored;
63         struct ldb_message **store;
64         int refs_stored;
65         char **refs_store;
66
67         int entries;
68         int refs;
69
70         int pending;
71         int status;
72 };
73
74 static int store_message(struct ldb_message *msg, struct search_context *sctx) {
75
76         sctx->store = talloc_realloc(sctx, sctx->store, struct ldb_message *, sctx->num_stored + 2);
77         if (!sctx->store) {
78                 fprintf(stderr, "talloc_realloc failed while storing messages\n");
79                 return -1;
80         }
81
82         sctx->store[sctx->num_stored] = talloc_move(sctx->store, &msg);
83         sctx->num_stored++;
84         sctx->store[sctx->num_stored] = NULL;
85
86         return 0;
87 }
88
89 static int store_referral(char *referral, struct search_context *sctx) {
90
91         sctx->refs_store = talloc_realloc(sctx, sctx->refs_store, char *, sctx->refs_stored + 2);
92         if (!sctx->refs_store) {
93                 fprintf(stderr, "talloc_realloc failed while storing referrals\n");
94                 return -1;
95         }
96
97         sctx->refs_store[sctx->refs_stored] = talloc_move(sctx->refs_store, &referral);
98         sctx->refs_stored++;
99         sctx->refs_store[sctx->refs_stored] = NULL;
100
101         return 0;
102 }
103
104 static int display_message(struct ldb_context *ldb, struct ldb_message *msg, struct search_context *sctx) {
105         struct ldb_ldif ldif;
106
107         sctx->entries++;
108         printf("# record %d\n", sctx->entries);
109
110         ldif.changetype = LDB_CHANGETYPE_NONE;
111         ldif.msg = msg;
112
113         if (sctx->sort) {
114         /*
115          * Ensure attributes are always returned in the same
116          * order.  For testing, this makes comparison of old
117          * vs. new much easier.
118          */
119                 ldb_msg_sort_elements(ldif.msg);
120         }
121
122         ldb_ldif_write_file(ldb, stdout, &ldif);
123
124         return 0;
125 }
126
127 static int display_referral(char *referral, struct search_context *sctx)
128 {
129
130         sctx->refs++;
131         printf("# Referral\nref: %s\n\n", referral);
132
133         return 0;
134 }
135
136 static int search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
137 {
138         struct search_context *sctx = talloc_get_type(context, struct search_context);
139         int ret;
140         
141         switch (ares->type) {
142
143         case LDB_REPLY_ENTRY:
144                 if (sctx->sort) {
145                         ret = store_message(ares->message, sctx);
146                 } else {
147                         ret = display_message(ldb, ares->message, sctx);
148                 }
149                 break;
150
151         case LDB_REPLY_REFERRAL:
152                 if (sctx->sort) {
153                         ret = store_referral(ares->referral, sctx);
154                 } else {
155                         ret = display_referral(ares->referral, sctx);
156                 }
157                 break;
158
159         case LDB_REPLY_DONE:
160                 if (ares->controls) {
161                         if (handle_controls_reply(ares->controls, sctx->req_ctrls) == 1)
162                                 sctx->pending = 1;
163                 }
164                 ret = 0;
165                 break;
166                 
167         default:
168                 fprintf(stderr, "unknown Reply Type\n");
169                 return LDB_ERR_OTHER;
170         }
171
172         if (talloc_free(ares) == -1) {
173                 fprintf(stderr, "talloc_free failed\n");
174                 sctx->pending = 0;
175                 return LDB_ERR_OPERATIONS_ERROR;
176         }
177
178         if (ret) {
179                 return LDB_ERR_OPERATIONS_ERROR;
180         }
181
182         return LDB_SUCCESS;
183 }
184
185 static int do_search(struct ldb_context *ldb,
186                      struct ldb_dn *basedn,
187                      struct ldb_cmdline *options,
188                      const char *expression,
189                      const char * const *attrs)
190 {
191         struct ldb_request *req;
192         struct search_context *sctx;
193         int ret;
194
195         req = talloc(ldb, struct ldb_request);
196         if (!req) return -1;
197         
198         sctx = talloc(req, struct search_context);
199         if (!sctx) return -1;
200
201         sctx->sort = options->sorted;
202         sctx->num_stored = 0;
203         sctx->refs_stored = 0;
204         sctx->store = NULL;
205         sctx->req_ctrls = ldb_parse_control_strings(ldb, sctx, (const char **)options->controls);
206         if (options->controls != NULL &&  sctx->req_ctrls== NULL) {
207                 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
208                 return -1;
209         }
210         sctx->entries = 0;
211         sctx->refs = 0;
212
213         if (basedn == NULL) {
214                 basedn = ldb_get_default_basedn(ldb);
215         }
216
217         req->operation = LDB_SEARCH;
218         req->op.search.base = basedn;
219         req->op.search.scope = options->scope;
220         req->op.search.tree = ldb_parse_tree(req, expression);
221         if (req->op.search.tree == NULL) return -1;
222         req->op.search.attrs = attrs;
223         req->controls = sctx->req_ctrls;
224         req->context = sctx;
225         req->callback = &search_callback;
226         ldb_set_timeout(ldb, req, 0); /* TODO: make this settable by command line */
227
228 again:
229         sctx->pending = 0;
230
231         ret = ldb_request(ldb, req);
232         if (ret != LDB_SUCCESS) {
233                 printf("search failed - %s\n", ldb_errstring(ldb));
234                 return -1;
235         }
236
237         ret = ldb_wait(req->handle, LDB_WAIT_ALL);
238         if (ret != LDB_SUCCESS) {
239                 printf("search error - %s\n", ldb_errstring(ldb));
240                 return -1;
241         }
242
243         if (sctx->pending)
244                 goto again;
245
246         if (sctx->sort && (sctx->num_stored != 0 || sctx->refs != 0)) {
247                 int i;
248
249                 if (sctx->num_stored) {
250                         ldb_qsort(sctx->store, sctx->num_stored, sizeof(struct ldb_message *),
251                                   ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
252                 }
253                 for (i = 0; i < sctx->num_stored; i++) {
254                         display_message(ldb, sctx->store[i], sctx);
255                 }
256
257                 for (i = 0; i < sctx->refs_stored; i++) {
258                         display_referral(sctx->refs_store[i], sctx);
259                 }
260         }
261
262         printf("# returned %d records\n# %d entries\n# %d referrals\n",
263                 sctx->entries + sctx->refs, sctx->entries, sctx->refs);
264
265         talloc_free(req);
266
267         return 0;
268 }
269
270 int main(int argc, const char **argv)
271 {
272         struct ldb_context *ldb;
273         struct ldb_dn *basedn = NULL;
274         const char * const * attrs = NULL;
275         struct ldb_cmdline *options;
276         int ret = -1;
277         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
278
279         ldb_global_init();
280
281         ldb = ldb_init(NULL);
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 ( ! ldb_dn_validate(basedn)) {
301                         fprintf(stderr, "Invalid Base DN format\n");
302                         exit(1);
303                 }
304         }
305
306         if (options->interactive) {
307                 char line[1024];
308                 while (fgets(line, sizeof(line), stdin)) {
309                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
310                                 ret = -1;
311                         }
312                 }
313         } else {
314                 ret = do_search(ldb, basedn, options, expression, attrs);
315         }
316
317         talloc_free(ldb);
318         return ret;
319 }