r7438: work in progress
[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_private.h"
38
39 #ifdef _SAMBA_BUILD_
40 #include "system/filesys.h"
41 #endif
42
43 static void usage(void)
44 {
45         printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
46         printf("Options:\n");
47         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
48         printf("  -s base|sub|one  choose search scope\n");
49         printf("  -b basedn        choose baseDN\n");
50         printf("  -i               read search expressions from stdin\n");
51         printf("  -S               sort returned attributes\n");
52         printf("  -o options       pass options like modules to activate\n");
53         printf("              e.g: -o modules:timestamps\n");
54         exit(1);
55 }
56
57 static int do_compare_msg(struct ldb_message **el1,
58                 struct ldb_message **el2)
59 {
60         return ldb_dn_cmp((*el1)->dn, (*el2)->dn);
61 }
62
63 static int do_search(struct ldb_context *ldb,
64                      const char *basedn,
65                      int scope,
66                      int sort_attribs,
67                      const char *expression,
68                      const char * const *attrs)
69 {
70         int ret, i;
71         struct ldb_message **msgs;
72
73         ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
74         if (ret == -1) {
75                 printf("search failed - %s\n", ldb_errstring(ldb));
76                 return -1;
77         }
78
79         printf("# returned %d records\n", ret);
80
81         if (sort_attribs) {
82                 qsort(msgs, ret, sizeof(struct ldb_message *),
83                                 (comparison_fn_t)do_compare_msg);
84         }
85
86         for (i=0;i<ret;i++) {
87                 struct ldb_ldif ldif;
88                 printf("# record %d\n", i+1);
89
90                 ldif.changetype = LDB_CHANGETYPE_NONE;
91                 ldif.msg = msgs[i];
92
93                 if (sort_attribs) {
94                         /*
95                          * Ensure attributes are always returned in the same
96                          * order.  For testing, this makes comparison of old
97                          * vs. new much easier.
98                          */
99                         ldb_msg_sort_elements(ldif.msg);
100                 }
101                 
102                 ldb_ldif_write_file(ldb, stdout, &ldif);
103         }
104
105         if (ret > 0) {
106                 ret = talloc_free(msgs);
107                 if (ret == -1) {
108                         fprintf(stderr, "talloc_free failed\n");
109                         exit(1);
110                 }
111         }
112
113         return 0;
114 }
115
116  int main(int argc, char * const argv[])
117 {
118         struct ldb_context *ldb;
119         const char * const * attrs = NULL;
120         const char *ldb_url;
121         const char *basedn = NULL;
122         const char **options = NULL;
123         int opt, ldbopts;
124         enum ldb_scope scope = LDB_SCOPE_SUBTREE;
125         int interactive = 0, sort_attribs=0, ret=0;
126
127         ldb_url = getenv("LDB_URL");
128
129         ldbopts = 0;
130         while ((opt = getopt(argc, argv, "b:H:s:o:hiS")) != EOF) {
131                 switch (opt) {
132                 case 'b':
133                         basedn = optarg;
134                         break;
135
136                 case 'H':
137                         ldb_url = optarg;
138                         break;
139
140                 case 's':
141                         if (strcmp(optarg, "base") == 0) {
142                                 scope = LDB_SCOPE_BASE;
143                         } else if (strcmp(optarg, "sub") == 0) {
144                                 scope = LDB_SCOPE_SUBTREE;
145                         } else if (strcmp(optarg, "one") == 0) {
146                                 scope = LDB_SCOPE_ONELEVEL;
147                         }
148                         break;
149
150                 case 'i':
151                         interactive = 1;
152                         break;
153
154                 case 'S':
155                         sort_attribs = 1;
156                         break;
157
158                 case 'o':
159                         options = ldb_options_parse(options, &ldbopts, optarg);
160                         break;
161
162                 case 'h':
163                 default:
164                         usage();
165                         break;
166                 }
167         }
168
169         if (!ldb_url) {
170                 fprintf(stderr, "You must specify a ldb URL\n\n");
171                 usage();
172         }
173
174         argc -= optind;
175         argv += optind;
176
177         if (argc < 1 && !interactive) {
178                 usage();
179                 exit(1);
180         }
181
182         if (argc > 1) {
183                 attrs = (const char * const *)(argv+1);
184         }
185
186         ldb = ldb_connect(ldb_url, LDB_FLG_RDONLY, options);
187         if (!ldb) {
188                 perror("ldb_connect");
189                 exit(1);
190         }
191
192         ldb_set_debug_stderr(ldb);
193
194         if (interactive) {
195                 char line[1024];
196                 while (fgets(line, sizeof(line), stdin)) {
197                         if (do_search(ldb, basedn, scope, sort_attribs, line, attrs) == -1) {
198                                 ret = -1;
199                         }
200                 }
201         } else {
202                 ret = do_search(ldb, basedn, scope, sort_attribs, argv[0], attrs);
203         }
204
205         talloc_free(ldb);
206         return ret;
207 }