ceabd5cd78d2eb5714f1dfb62f66dc7985ab18c2
[sfrench/samba-autobuild/.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 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_context *ldb;
60         struct ldb_control **req_ctrls;
61
62         int sort;
63         int num_stored;
64         struct ldb_message **store;
65         int refs_stored;
66         char **refs_store;
67
68         int entries;
69         int refs;
70
71         int pending;
72         int status;
73 };
74
75 static int store_message(struct ldb_message *msg, struct search_context *sctx) {
76
77         sctx->store = talloc_realloc(sctx, sctx->store, struct ldb_message *, sctx->num_stored + 2);
78         if (!sctx->store) {
79                 fprintf(stderr, "talloc_realloc failed while storing messages\n");
80                 return -1;
81         }
82
83         sctx->store[sctx->num_stored] = talloc_move(sctx->store, &msg);
84         sctx->num_stored++;
85         sctx->store[sctx->num_stored] = NULL;
86
87         return 0;
88 }
89
90 static int store_referral(char *referral, struct search_context *sctx) {
91
92         sctx->refs_store = talloc_realloc(sctx, sctx->refs_store, char *, sctx->refs_stored + 2);
93         if (!sctx->refs_store) {
94                 fprintf(stderr, "talloc_realloc failed while storing referrals\n");
95                 return -1;
96         }
97
98         sctx->refs_store[sctx->refs_stored] = talloc_move(sctx->refs_store, &referral);
99         sctx->refs_stored++;
100         sctx->refs_store[sctx->refs_stored] = NULL;
101
102         return 0;
103 }
104
105 static int display_message(struct ldb_message *msg, struct search_context *sctx) {
106         struct ldb_ldif ldif;
107
108         sctx->entries++;
109         printf("# record %d\n", sctx->entries);
110
111         ldif.changetype = LDB_CHANGETYPE_NONE;
112         ldif.msg = msg;
113
114         if (sctx->sort) {
115         /*
116          * Ensure attributes are always returned in the same
117          * order.  For testing, this makes comparison of old
118          * vs. new much easier.
119          */
120                 ldb_msg_sort_elements(ldif.msg);
121         }
122
123         ldb_ldif_write_file(sctx->ldb, stdout, &ldif);
124
125         return 0;
126 }
127
128 static int display_referral(char *referral, struct search_context *sctx)
129 {
130
131         sctx->refs++;
132         printf("# Referral\nref: %s\n\n", referral);
133
134         return 0;
135 }
136
137 static int search_callback(struct ldb_request *req, struct ldb_reply *ares)
138 {
139         struct search_context *sctx;
140         int ret;
141
142         sctx = talloc_get_type(req->context, struct search_context);
143
144         if (!ares) {
145                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
146         }
147         if (ares->error != LDB_SUCCESS) {
148                 return ldb_request_done(req, ares->error);
149         }
150         
151         switch (ares->type) {
152         case LDB_REPLY_ENTRY:
153                 if (sctx->sort) {
154                         ret = store_message(ares->message, sctx);
155                 } else {
156                         ret = display_message(ares->message, sctx);
157                 }
158                 break;
159
160         case LDB_REPLY_REFERRAL:
161                 if (sctx->sort) {
162                         ret = store_referral(ares->referral, sctx);
163                 } else {
164                         ret = display_referral(ares->referral, sctx);
165                 }
166                 if (ret) {
167                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
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                 talloc_free(ares);
177                 return ldb_request_done(req, LDB_SUCCESS);
178         }
179
180         talloc_free(ares);
181         if (ret) {
182                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
183         }
184
185         return LDB_SUCCESS;
186 }
187
188 static int do_search(struct ldb_context *ldb,
189                      struct ldb_dn *basedn,
190                      struct ldb_cmdline *options,
191                      const char *expression,
192                      const char * const *attrs)
193 {
194         struct ldb_request *req;
195         struct search_context *sctx;
196         int ret;
197
198         req = NULL;
199         
200         sctx = talloc(ldb, struct search_context);
201         if (!sctx) return -1;
202
203         sctx->ldb = ldb;
204         sctx->sort = options->sorted;
205         sctx->num_stored = 0;
206         sctx->refs_stored = 0;
207         sctx->store = NULL;
208         sctx->req_ctrls = ldb_parse_control_strings(ldb, sctx, (const char **)options->controls);
209         if (options->controls != NULL &&  sctx->req_ctrls== NULL) {
210                 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
211                 return -1;
212         }
213         sctx->entries = 0;
214         sctx->refs = 0;
215
216         if (basedn == NULL) {
217                 basedn = ldb_get_default_basedn(ldb);
218         }
219
220 again:
221         /* free any previous requests */
222         if (req) talloc_free(req);
223
224         ret = ldb_build_search_req(&req, ldb, ldb,
225                                    basedn, options->scope,
226                                    expression, attrs,
227                                    sctx->req_ctrls,
228                                    sctx, search_callback,
229                                    NULL);
230         if (ret != LDB_SUCCESS) {
231                 talloc_free(sctx);
232                 printf("allocating request failed: %s\n", ldb_errstring(ldb));
233                 return -1;
234         }
235
236         sctx->pending = 0;
237
238         ret = ldb_request(ldb, req);
239         if (ret != LDB_SUCCESS) {
240                 printf("search failed - %s\n", ldb_errstring(ldb));
241                 return -1;
242         }
243
244         ret = ldb_wait(req->handle, LDB_WAIT_ALL);
245         if (ret != LDB_SUCCESS) {
246                 printf("search error - %s\n", ldb_errstring(ldb));
247                 return -1;
248         }
249
250         if (sctx->pending)
251                 goto again;
252
253         if (sctx->sort && (sctx->num_stored != 0 || sctx->refs != 0)) {
254                 int i;
255
256                 if (sctx->num_stored) {
257                         ldb_qsort(sctx->store, sctx->num_stored, sizeof(struct ldb_message *),
258                                   ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
259                 }
260                 for (i = 0; i < sctx->num_stored; i++) {
261                         display_message(sctx->store[i], sctx);
262                 }
263
264                 for (i = 0; i < sctx->refs_stored; i++) {
265                         display_referral(sctx->refs_store[i], sctx);
266                 }
267         }
268
269         printf("# returned %d records\n# %d entries\n# %d referrals\n",
270                 sctx->entries + sctx->refs, sctx->entries, sctx->refs);
271
272         talloc_free(sctx);
273         talloc_free(req);
274
275         return 0;
276 }
277
278 int main(int argc, const char **argv)
279 {
280         struct ldb_context *ldb;
281         struct ldb_dn *basedn = NULL;
282         const char * const * attrs = NULL;
283         struct ldb_cmdline *options;
284         int ret = -1;
285         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
286
287         ldb = ldb_init(NULL, NULL);
288         if (ldb == NULL) {
289                 return -1;
290         }
291
292         options = ldb_cmdline_process(ldb, argc, argv, usage);
293
294         /* the check for '=' is for compatibility with ldapsearch */
295         if (!options->interactive &&
296             options->argc > 0 && 
297             strchr(options->argv[0], '=')) {
298                 expression = options->argv[0];
299                 options->argv++;
300                 options->argc--;
301         }
302
303         if (options->argc > 0) {
304                 attrs = (const char * const *)(options->argv);
305         }
306
307         if (options->basedn != NULL) {
308                 basedn = ldb_dn_new(ldb, ldb, options->basedn);
309                 if ( ! ldb_dn_validate(basedn)) {
310                         fprintf(stderr, "Invalid Base DN format\n");
311                         exit(1);
312                 }
313         }
314
315         if (options->interactive) {
316                 char line[1024];
317                 while (fgets(line, sizeof(line), stdin)) {
318                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
319                                 ret = -1;
320                         }
321                 }
322         } else {
323                 ret = do_search(ldb, basedn, options, expression, attrs);
324         }
325
326         talloc_free(ldb);
327         return ret;
328 }