r13998: From now on ldb_request() will require an alloced request
[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 static int do_search(struct ldb_context *ldb,
62                      const struct ldb_dn *basedn,
63                      struct ldb_cmdline *options,
64                      const char *expression,
65                      const char * const *attrs)
66 {
67         int ret, i, n;
68         int loop = 0;
69         int total = 0;
70         int refs = 0;
71         struct ldb_request *req;
72         struct ldb_result *result = NULL;
73
74         req = talloc(ldb, struct ldb_request);
75         req->operation = LDB_REQ_SEARCH;
76         req->op.search.base = basedn;
77         req->op.search.scope = options->scope;
78         req->op.search.tree = ldb_parse_tree(ldb, expression);
79         if (req->op.search.tree == NULL) return -1;
80         req->op.search.attrs = attrs;
81         req->op.search.res = NULL;
82         req->controls = parse_controls(ldb, options->controls);
83         if (options->controls != NULL && req->controls == NULL) return -1;
84         req->creds = NULL;
85
86         do {
87                 loop = 0;
88
89                 ret = ldb_request(ldb, req);
90                 if (ret != LDB_SUCCESS) {
91                         printf("search failed - %s\n", ldb_errstring(ldb));
92                         if (req->op.search.res && req->op.search.res->controls) {
93                                 handle_controls_reply(req->op.search.res->controls, req->controls);
94                         }
95                         return -1;
96                 }
97
98                 result = req->op.search.res;
99
100                 if (options->sorted) {
101                         ldb_qsort(result->msgs, result->count, sizeof(struct ldb_message *),
102                                   ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
103                 }
104
105                 for (i = 0; i < result->count; i++, total++) {
106                         struct ldb_ldif ldif;
107                         printf("# record %d\n", total + 1);
108
109                         ldif.changetype = LDB_CHANGETYPE_NONE;
110                         ldif.msg = result->msgs[i];
111
112                         if (options->sorted) {
113                                 /*
114                                  * Ensure attributes are always returned in the same
115                                  * order.  For testing, this makes comparison of old
116                                  * vs. new much easier.
117                                  */
118                                 ldb_msg_sort_elements(ldif.msg);
119                         }
120         
121                         ldb_ldif_write_file(ldb, stdout, &ldif);
122                 }
123
124                 if (result->refs) {
125                         for(n = 0;result->refs[n]; n++, refs++) {
126                                 printf("# referral %d\nref: %s\n\n", refs + 1, result->refs[n]);
127                         }
128                 }
129                 
130                 if (result->controls) {
131                         if (handle_controls_reply(result->controls, req->controls) == 1)
132                                 loop = 1;
133                 }
134
135                 if (result) {
136                         ret = talloc_free(result);
137                         if (ret == -1) {
138                                 fprintf(stderr, "talloc_free failed\n");
139                                 exit(1);
140                         }
141                 }
142
143                 req->op.search.res = NULL;
144                 
145         } while(loop);
146
147         printf("# returned %d records\n# %d entries\n# %d referrals\n", total + refs, total, refs);
148
149         return 0;
150 }
151
152 int main(int argc, const char **argv)
153 {
154         struct ldb_context *ldb;
155         struct ldb_dn *basedn = NULL;
156         const char * const * attrs = NULL;
157         struct ldb_cmdline *options;
158         int ret = -1;
159         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
160
161         ldb_global_init();
162
163         ldb = ldb_init(NULL);
164
165         options = ldb_cmdline_process(ldb, argc, argv, usage);
166
167         /* the check for '=' is for compatibility with ldapsearch */
168         if (!options->interactive &&
169             options->argc > 0 && 
170             strchr(options->argv[0], '=')) {
171                 expression = options->argv[0];
172                 options->argv++;
173                 options->argc--;
174         }
175
176         if (options->argc > 0) {
177                 attrs = (const char * const *)(options->argv);
178         }
179
180         if (options->basedn != NULL) {
181                 basedn = ldb_dn_explode(ldb, options->basedn);
182                 if (basedn == NULL) {
183                         fprintf(stderr, "Invalid Base DN format\n");
184                         exit(1);
185                 }
186         }
187
188         if (options->interactive) {
189                 char line[1024];
190                 while (fgets(line, sizeof(line), stdin)) {
191                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
192                                 ret = -1;
193                         }
194                 }
195         } else {
196                 ret = do_search(ldb, basedn, options, expression, attrs);
197         }
198
199         talloc_free(ldb);
200         return ret;
201 }