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