f8c06f0fa4a372a9136689fe200fc687d4136e4b
[kamenim/samba.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_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 struct ldb_context *ldbsearch_ldb;
60
61 static int do_compare_msg(struct ldb_message **el1,
62                           struct ldb_message **el2)
63 {
64         return ldb_dn_compare(ldbsearch_ldb, (*el1)->dn, (*el2)->dn);
65 }
66
67 static int do_search(struct ldb_context *ldb,
68                      const struct ldb_dn *basedn,
69                      int scope,
70                      int sort_attribs,
71                      const char *expression,
72                      const char * const *attrs)
73 {
74         int ret, i;
75         struct ldb_result *result = NULL;
76
77         ret = ldb_search(ldb, basedn, scope, expression, attrs, &result);
78         if (ret != LDB_SUCCESS) {
79                 printf("search failed - %s\n", ldb_errstring(ldb));
80                 return -1;
81         }
82
83         printf("# returned %d records\n", ret);
84
85         ldbsearch_ldb = ldb;
86         if (sort_attribs) {
87                 qsort(result->msgs, ret, sizeof(struct ldb_message *),
88                                 (comparison_fn_t)do_compare_msg);
89         }
90
91         for (i = 0; i < result->count; i++) {
92                 struct ldb_ldif ldif;
93                 printf("# record %d\n", i+1);
94
95                 ldif.changetype = LDB_CHANGETYPE_NONE;
96                 ldif.msg = result->msgs[i];
97
98                 if (sort_attribs) {
99                         /*
100                          * Ensure attributes are always returned in the same
101                          * order.  For testing, this makes comparison of old
102                          * vs. new much easier.
103                          */
104                         ldb_msg_sort_elements(ldif.msg);
105                 }
106                 
107                 ldb_ldif_write_file(ldb, stdout, &ldif);
108         }
109
110         if (result) {
111                 ret = talloc_free(result);
112                 if (ret == -1) {
113                         fprintf(stderr, "talloc_free failed\n");
114                         exit(1);
115                 }
116         }
117
118         return 0;
119 }
120
121  int main(int argc, const char **argv)
122 {
123         struct ldb_context *ldb;
124         struct ldb_dn *basedn = NULL;
125         const char * const * attrs = NULL;
126         struct ldb_cmdline *options;
127         int ret = -1;
128         const char *expression = "(objectclass=*)";
129
130         ldb = ldb_init(NULL);
131
132         options = ldb_cmdline_process(ldb, argc, argv, usage);
133
134         /* the check for '=' is for compatibility with ldapsearch */
135         if (!options->interactive &&
136             options->argc > 0 && 
137             strchr(options->argv[0], '=')) {
138                 expression = options->argv[0];
139                 options->argv++;
140                 options->argc--;
141         }
142
143         if (options->argc > 0) {
144                 attrs = (const char * const *)(options->argv);
145         }
146
147         if (options->basedn != NULL) {
148                 basedn = ldb_dn_explode(ldb, options->basedn);
149                 if (basedn == NULL) {
150                         fprintf(stderr, "Invalid Base DN format\n");
151                         exit(1);
152                 }
153         }
154
155         if (options->interactive) {
156                 char line[1024];
157                 while (fgets(line, sizeof(line), stdin)) {
158                         if (do_search(ldb, basedn, 
159                                       options->scope, options->sorted, line, attrs) == -1) {
160                                 ret = -1;
161                         }
162                 }
163         } else {
164                 ret = do_search(ldb, basedn, options->scope, options->sorted, 
165                                 expression, attrs);
166         }
167
168         talloc_free(ldb);
169         return ret;
170 }