r12941: Add Attribute Scoped Search control
[ira/wip.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 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 struct ldb_control **parse_controls(void *mem_ctx, char **control_strings)
62 {
63         int i;
64         struct ldb_control **ctrl;
65
66         if (control_strings == NULL || control_strings[0] == NULL)
67                 return NULL;
68
69         for (i = 0; control_strings[i]; i++);
70
71         ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
72
73         for (i = 0; control_strings[i]; i++) {
74                 if (strncmp(control_strings[i], "asq:", 4) == 0) {
75                         struct ldb_asq_control *control;
76                         const char *p;
77                         char attr[256];
78                         int crit, ret;
79
80                         attr[0] = '\0';
81                         p = &(control_strings[i][4]);
82                         ret = sscanf(p, "%d:%255[^$]", &crit, attr);
83                         if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
84                                 fprintf(stderr, "invalid asq control syntax\n");
85                                 return NULL;
86                         }
87
88                         ctrl[i] = talloc(ctrl, struct ldb_control);
89                         ctrl[i]->oid = LDB_CONTROL_ASQ_OID;
90                         ctrl[i]->critical = crit;
91                         control = talloc(ctrl[i], struct ldb_asq_control);
92                         control->request = 1;
93                         control->source_attribute = talloc_strdup(control, attr);
94                         control->src_attr_len = strlen(attr);
95                         ctrl[i]->data = control;
96
97                         continue;
98                 }
99
100                 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
101                         struct ldb_extended_dn_control *control;
102                         const char *p;
103                         int crit, type, ret;
104
105                         p = &(control_strings[i][12]);
106                         ret = sscanf(p, "%d:%d", &crit, &type);
107                         if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
108                                 fprintf(stderr, "invalid extended_dn control syntax\n");
109                                 return NULL;
110                         }
111
112                         ctrl[i] = talloc(ctrl, struct ldb_control);
113                         ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
114                         ctrl[i]->critical = crit;
115                         control = talloc(ctrl[i], struct ldb_extended_dn_control);
116                         control->type = type;
117                         ctrl[i]->data = control;
118
119                         continue;
120                 }
121
122                 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
123                         struct ldb_paged_control *control;
124                         const char *p;
125                         int crit, size, ret;
126                        
127                         p = &(control_strings[i][14]);
128                         ret = sscanf(p, "%d:%d", &crit, &size);
129
130                         if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
131                                 fprintf(stderr, "invalid paged_results control syntax\n");
132                                 return NULL;
133                         }
134
135                         ctrl[i] = talloc(ctrl, struct ldb_control);
136                         ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
137                         ctrl[i]->critical = crit;
138                         control = talloc(ctrl[i], struct ldb_paged_control);
139                         control->size = size;
140                         control->cookie = NULL;
141                         control->cookie_len = 0;
142                         ctrl[i]->data = control;
143
144                         continue;
145                 }
146
147                 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
148                         struct ldb_server_sort_control **control;
149                         const char *p;
150                         char attr[256];
151                         char rule[128];
152                         int crit, rev, ret;
153
154                         attr[0] = '\0';
155                         rule[0] = '\0';
156                         p = &(control_strings[i][12]);
157                         ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
158                         if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
159                                 fprintf(stderr, "invalid server_sort control syntax\n");
160                                 return NULL;
161                         }
162                         ctrl[i] = talloc(ctrl, struct ldb_control);
163                         ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
164                         ctrl[i]->critical = crit;
165                         control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
166                         control[0] = talloc(control, struct ldb_server_sort_control);
167                         control[0]->attributeName = talloc_strdup(control, attr);
168                         if (rule[0])
169                                 control[0]->orderingRule = talloc_strdup(control, rule);
170                         else
171                                 control[0]->orderingRule = NULL;
172                         control[0]->reverse = rev;
173                         control[1] = NULL;
174                         ctrl[i]->data = control;
175
176                         continue;
177                 }
178
179                 /* no controls matched, throw an error */
180                 fprintf(stderr, "Invalid control name\n");
181                 return NULL;
182         }
183
184         ctrl[i] = NULL;
185
186         return ctrl;
187 }
188
189 /* this function check controls reply and determines if more
190  * processing is needed setting up the request controls correctly
191  *
192  * returns:
193  *      -1 error
194  *      0 all ok
195  *      1 all ok, more processing required
196  */
197 static int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
198 {
199         int i, j;
200         int ret = 0;
201
202         if (reply == NULL || request == NULL) return -1;
203         
204         for (i = 0; reply[i]; i++) {
205                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
206                         struct ldb_asq_control *rep_control;
207
208                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
209
210                         /* check the result */
211                         if (rep_control->result != 0) {
212                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
213                         }
214
215                         continue;
216                 }
217                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
218                         struct ldb_paged_control *rep_control, *req_control;
219
220                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
221                         if (rep_control->cookie_len == 0) /* we are done */
222                                 break;
223
224                         /* more processing required */
225                         /* let's fill in the request control with the new cookie */
226
227                         for (j = 0; request[j]; j++) {
228                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
229                                         break;
230                         }
231                         /* if there's a reply control we must find a request
232                          * control matching it */
233                         if (! request[j]) return -1;
234
235                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
236
237                         if (req_control->cookie)
238                                 talloc_free(req_control->cookie);
239                         req_control->cookie = talloc_memdup(req_control,
240                                                             rep_control->cookie,
241                                                             rep_control->cookie_len);
242                         req_control->cookie_len = rep_control->cookie_len;
243
244                         ret = 1;
245
246                         continue;
247                 }
248
249                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
250                         struct ldb_sort_resp_control *rep_control;
251
252                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
253
254                         /* check we have a matching control in the request */
255                         for (j = 0; request[j]; j++) {
256                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
257                                         break;
258                         }
259                         if (! request[j]) {
260                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
261                                 continue;
262                         }
263
264                         /* check the result */
265                         if (rep_control->result != 0) {
266                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
267                         }
268
269                         continue;
270                 }
271
272                 /* no controls matched, throw a warning */
273                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
274         }
275
276         return ret;
277 }
278
279
280 static int do_search(struct ldb_context *ldb,
281                      const struct ldb_dn *basedn,
282                      struct ldb_cmdline *options,
283                      const char *expression,
284                      const char * const *attrs)
285 {
286         int ret, i;
287         int loop = 0;
288         int total = 0;
289         struct ldb_request req;
290         struct ldb_result *result = NULL;
291
292         req.operation = LDB_REQ_SEARCH;
293         req.op.search.base = basedn;
294         req.op.search.scope = options->scope;
295         req.op.search.tree = ldb_parse_tree(ldb, expression);
296         req.op.search.attrs = attrs;
297         req.op.search.res = NULL;
298         req.controls = parse_controls(ldb, options->controls);
299         if (options->controls != NULL && req.controls == NULL) return -1;
300         req.creds = NULL;
301
302         do {
303                 loop = 0;
304
305                 ret = ldb_request(ldb, &req);
306                 if (ret != LDB_SUCCESS) {
307                         printf("search failed - %s\n", ldb_errstring(ldb));
308                         if (req.op.search.res && req.op.search.res->controls) {
309                                 /* TODO: handle_control */
310                                 ;       
311                         }
312                         return -1;
313                 }
314
315                 result = req.op.search.res;
316                 printf("# returned %d records\n", result->count);
317
318                 if (options->sorted) {
319                         ldb_qsort(result->msgs, ret, sizeof(struct ldb_message *),
320                                   ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
321                 }
322
323                 for (i = 0; i < result->count; i++, total++) {
324                         struct ldb_ldif ldif;
325                         printf("# record %d\n", total + 1);
326
327                         ldif.changetype = LDB_CHANGETYPE_NONE;
328                         ldif.msg = result->msgs[i];
329
330                         if (options->sorted) {
331                                 /*
332                                  * Ensure attributes are always returned in the same
333                                  * order.  For testing, this makes comparison of old
334                                  * vs. new much easier.
335                                  */
336                                 ldb_msg_sort_elements(ldif.msg);
337                         }
338         
339                         ldb_ldif_write_file(ldb, stdout, &ldif);
340                 }
341
342                 if (result->controls) {
343                         if (handle_controls_reply(result->controls, req.controls) == 1)
344                                 loop = 1;
345                 }
346
347                 if (result) {
348                         ret = talloc_free(result);
349                         if (ret == -1) {
350                                 fprintf(stderr, "talloc_free failed\n");
351                                 exit(1);
352                         }
353                 }
354
355                 req.op.search.res = NULL;
356                 
357         } while(loop);
358
359         return 0;
360 }
361
362  int main(int argc, const char **argv)
363 {
364         struct ldb_context *ldb;
365         struct ldb_dn *basedn = NULL;
366         const char * const * attrs = NULL;
367         struct ldb_cmdline *options;
368         int ret = -1;
369         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
370
371         ldb = ldb_init(NULL);
372
373         options = ldb_cmdline_process(ldb, argc, argv, usage);
374
375         /* the check for '=' is for compatibility with ldapsearch */
376         if (!options->interactive &&
377             options->argc > 0 && 
378             strchr(options->argv[0], '=')) {
379                 expression = options->argv[0];
380                 options->argv++;
381                 options->argc--;
382         }
383
384         if (options->argc > 0) {
385                 attrs = (const char * const *)(options->argv);
386         }
387
388         if (options->basedn != NULL) {
389                 basedn = ldb_dn_explode(ldb, options->basedn);
390                 if (basedn == NULL) {
391                         fprintf(stderr, "Invalid Base DN format\n");
392                         exit(1);
393                 }
394         }
395
396         if (options->interactive) {
397                 char line[1024];
398                 while (fgets(line, sizeof(line), stdin)) {
399                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
400                                 ret = -1;
401                         }
402                 }
403         } else {
404                 ret = do_search(ldb, basedn, options, expression, attrs);
405         }
406
407         talloc_free(ldb);
408         return ret;
409 }