r12745: Initial work to support a syntax to pass over controls via
[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/ldb.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
39 #include "ldb/tools/cmdline.h"
40
41 #ifdef _SAMBA_BUILD_
42 #include "system/filesys.h"
43 #endif
44
45 static void usage(void)
46 {
47         printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
48         printf("Options:\n");
49         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
50         printf("  -s base|sub|one  choose search scope\n");
51         printf("  -b basedn        choose baseDN\n");
52         printf("  -i               read search expressions from stdin\n");
53         printf("  -S               sort returned attributes\n");
54         printf("  -o options       pass options like modules to activate\n");
55         printf("              e.g: -o modules:timestamps\n");
56         exit(1);
57 }
58
59 static int do_compare_msg(struct ldb_message **el1,
60                           struct ldb_message **el2,
61                           void *opaque)
62 {
63         struct ldb_context *ldb = talloc_get_type(opaque, struct ldb_context);
64         return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
65 }
66
67 static struct ldb_control **parse_controls(void *mem_ctx, char **control_strings)
68 {
69         int i;
70         struct ldb_control **ctrl;
71
72         if (control_strings == NULL || control_strings[0] == NULL)
73                 return NULL;
74
75         for (i = 0; control_strings[i]; i++);
76
77         ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
78
79         for (i = 0; control_strings[i]; i++) {
80                 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
81                         struct ldb_extended_dn_control *control;
82
83                         ctrl[i] = talloc(ctrl, struct ldb_control);
84                         ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
85                         ctrl[i]->critical = control_strings[i][12]=='1'?1:0;
86                         control = talloc(ctrl[i], struct ldb_extended_dn_control);
87                         control->type = atoi(&control_strings[i][14]);
88                         ctrl[i]->data = control;
89                 }
90
91                 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
92                         struct ldb_paged_control *control;
93
94                         ctrl[i] = talloc(ctrl, struct ldb_control);
95                         ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
96                         ctrl[i]->critical = control_strings[i][14]=='1'?1:0;
97                         control = talloc(ctrl[i], struct ldb_paged_control);
98                         control->size = atoi(&control_strings[i][16]);
99                         control->cookie = NULL;
100                         control->cookie_len = 0;
101                         ctrl[i]->data = control;
102                 }
103
104                 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
105                         struct ldb_server_sort_control **control;
106
107                         ctrl[i] = talloc(ctrl, struct ldb_control);
108                         ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
109                         ctrl[i]->critical = control_strings[i][12]=='1'?1:0;
110                         control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
111                         control[0] = talloc(control, struct ldb_server_sort_control);
112                         control[0]->attributeName = talloc_strdup(control, &control_strings[i][16]);
113                         control[0]->orderingRule = NULL;
114                         control[0]->reverse = control_strings[i][14]=='1'?1:0;
115                         control[1] = NULL;
116                         ctrl[i]->data = control;
117                 }
118         }
119
120         ctrl[i + 1] = NULL;
121
122         return ctrl;
123 }
124
125 static int do_search(struct ldb_context *ldb,
126                      const struct ldb_dn *basedn,
127                      struct ldb_cmdline *options,
128                      const char *expression,
129                      const char * const *attrs)
130 {
131         int ret, i;
132         struct ldb_request req;
133         struct ldb_result *result = NULL;
134
135         req.operation = LDB_REQ_SEARCH;
136         req.op.search.base = basedn;
137         req.op.search.scope = options->scope;
138         req.op.search.tree = ldb_parse_tree(ldb, expression);
139         req.op.search.attrs = attrs;
140         req.op.search.res = NULL;
141         req.controls = parse_controls(ldb, options->controls);
142         req.creds = NULL;
143
144         ret = ldb_request(ldb, &req);
145         if (ret != LDB_SUCCESS) {
146                 printf("search failed - %s\n", ldb_errstring(ldb));
147                 return -1;
148         }
149
150         result = req.op.search.res;
151         printf("# returned %d records\n", ret);
152
153         if (options->sorted) {
154                 ldb_qsort(result->msgs, ret, sizeof(struct ldb_message *),
155                           ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
156         }
157
158         for (i = 0; i < result->count; i++) {
159                 struct ldb_ldif ldif;
160                 printf("# record %d\n", i+1);
161
162                 ldif.changetype = LDB_CHANGETYPE_NONE;
163                 ldif.msg = result->msgs[i];
164
165                 if (options->sorted) {
166                         /*
167                          * Ensure attributes are always returned in the same
168                          * order.  For testing, this makes comparison of old
169                          * vs. new much easier.
170                          */
171                         ldb_msg_sort_elements(ldif.msg);
172                 }
173                 
174                 ldb_ldif_write_file(ldb, stdout, &ldif);
175         }
176
177         if (result) {
178                 ret = talloc_free(result);
179                 if (ret == -1) {
180                         fprintf(stderr, "talloc_free failed\n");
181                         exit(1);
182                 }
183         }
184
185         return 0;
186 }
187
188  int main(int argc, const char **argv)
189 {
190         struct ldb_context *ldb;
191         struct ldb_dn *basedn = NULL;
192         const char * const * attrs = NULL;
193         struct ldb_cmdline *options;
194         int ret = -1;
195         const char *expression = "(objectclass=*)";
196
197         ldb = ldb_init(NULL);
198
199         options = ldb_cmdline_process(ldb, argc, argv, usage);
200
201         /* the check for '=' is for compatibility with ldapsearch */
202         if (!options->interactive &&
203             options->argc > 0 && 
204             strchr(options->argv[0], '=')) {
205                 expression = options->argv[0];
206                 options->argv++;
207                 options->argc--;
208         }
209
210         if (options->argc > 0) {
211                 attrs = (const char * const *)(options->argv);
212         }
213
214         if (options->basedn != NULL) {
215                 basedn = ldb_dn_explode(ldb, options->basedn);
216                 if (basedn == NULL) {
217                         fprintf(stderr, "Invalid Base DN format\n");
218                         exit(1);
219                 }
220         }
221
222         if (options->interactive) {
223                 char line[1024];
224                 while (fgets(line, sizeof(line), stdin)) {
225                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
226                                 ret = -1;
227                         }
228                 }
229         } else {
230                 ret = do_search(ldb, basedn, options, expression, attrs);
231         }
232
233         talloc_free(ldb);
234         return ret;
235 }