r9391: Convert all the code to use struct ldb_dn to ohandle ldap like distinguished...
[ambi/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 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_private.h"
38 #include "ldb/tools/cmdline.h"
39
40 #ifdef _SAMBA_BUILD_
41 #include "system/filesys.h"
42 #endif
43
44 static void usage(void)
45 {
46         printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
47         printf("Options:\n");
48         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
49         printf("  -s base|sub|one  choose search scope\n");
50         printf("  -b basedn        choose baseDN\n");
51         printf("  -i               read search expressions from stdin\n");
52         printf("  -S               sort returned attributes\n");
53         printf("  -o options       pass options like modules to activate\n");
54         printf("              e.g: -o modules:timestamps\n");
55         exit(1);
56 }
57
58 struct ldb_context *ldbsearch_ldb;
59
60 static int do_compare_msg(struct ldb_message **el1,
61                           struct ldb_message **el2)
62 {
63         return ldb_dn_compare(ldbsearch_ldb, (*el1)->dn, (*el2)->dn);
64 }
65
66 static int do_search(struct ldb_context *ldb,
67                      const struct ldb_dn *basedn,
68                      int scope,
69                      int sort_attribs,
70                      const char *expression,
71                      const char * const *attrs)
72 {
73         int ret, i;
74         struct ldb_message **msgs;
75
76         ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
77         if (ret == -1) {
78                 printf("search failed - %s\n", ldb_errstring(ldb));
79                 return -1;
80         }
81
82         printf("# returned %d records\n", ret);
83
84         ldbsearch_ldb = ldb;
85         if (sort_attribs) {
86                 qsort(msgs, ret, sizeof(struct ldb_message *),
87                                 (comparison_fn_t)do_compare_msg);
88         }
89
90         for (i=0;i<ret;i++) {
91                 struct ldb_ldif ldif;
92                 printf("# record %d\n", i+1);
93
94                 ldif.changetype = LDB_CHANGETYPE_NONE;
95                 ldif.msg = msgs[i];
96
97                 if (sort_attribs) {
98                         /*
99                          * Ensure attributes are always returned in the same
100                          * order.  For testing, this makes comparison of old
101                          * vs. new much easier.
102                          */
103                         ldb_msg_sort_elements(ldif.msg);
104                 }
105                 
106                 ldb_ldif_write_file(ldb, stdout, &ldif);
107         }
108
109         if (ret > 0) {
110                 ret = talloc_free(msgs);
111                 if (ret == -1) {
112                         fprintf(stderr, "talloc_free failed\n");
113                         exit(1);
114                 }
115         }
116
117         return 0;
118 }
119
120  int main(int argc, const char **argv)
121 {
122         struct ldb_context *ldb;
123         struct ldb_dn *basedn = NULL;
124         const char * const * attrs = NULL;
125         struct ldb_cmdline *options;
126         int ret = -1;
127         const char *expression = "(|(objectclass=*)(dn=*))";
128
129         ldb = ldb_init(NULL);
130
131         options = ldb_cmdline_process(ldb, argc, argv, usage);
132
133         /* the check for '=' is for compatibility with ldapsearch */
134         if (!options->interactive &&
135             options->argc > 0 && 
136             strchr(options->argv[0], '=')) {
137                 expression = options->argv[0];
138                 options->argv++;
139                 options->argc--;
140         }
141
142         if (options->argc > 0) {
143                 attrs = (const char * const *)(options->argv);
144         }
145
146         if (options->basedn != NULL) {
147                 basedn = ldb_dn_explode(ldb, options->basedn);
148                 if (basedn == NULL) {
149                         fprintf(stderr, "Invalid Base DN format\n");
150                         exit(1);
151                 }
152         }
153
154         if (options->interactive) {
155                 char line[1024];
156                 while (fgets(line, sizeof(line), stdin)) {
157                         if (do_search(ldb, basedn, 
158                                       options->scope, options->sorted, line, attrs) == -1) {
159                                 ret = -1;
160                         }
161                 }
162         } else {
163                 ret = do_search(ldb, basedn, options->scope, options->sorted, 
164                                 expression, attrs);
165         }
166
167         talloc_free(ldb);
168         return ret;
169 }