r5585: LDB interfaces change:
[bbaumbach/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
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("  -o options       pass options like modules to activate\n");
52         printf("              e.g: -o modules:timestamps\n");
53         exit(1);
54 }
55
56 static int do_search(struct ldb_context *ldb,
57                      const char *basedn,
58                      int scope,
59                      const char *expression,
60                      const char * const *attrs)
61 {
62         int ret, i;
63         struct ldb_message **msgs;
64
65         ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
66         if (ret == -1) {
67                 printf("search failed - %s\n", ldb_errstring(ldb));
68                 return -1;
69         }
70
71         printf("# returned %d records\n", ret);
72
73         for (i=0;i<ret;i++) {
74                 struct ldb_ldif ldif;
75                 printf("# record %d\n", i+1);
76
77                 ldif.changetype = LDB_CHANGETYPE_NONE;
78                 ldif.msg = msgs[i];
79
80                 ldb_ldif_write_file(ldb, stdout, &ldif);
81         }
82
83         if (ret > 0) {
84                 ret = ldb_search_free(ldb, msgs);
85                 if (ret == -1) {
86                         fprintf(stderr, "search_free failed\n");
87                         exit(1);
88                 }
89         }
90
91         return 0;
92 }
93
94  int main(int argc, char * const argv[])
95 {
96         struct ldb_context *ldb;
97         const char * const * attrs = NULL;
98         const char *ldb_url;
99         const char *basedn = NULL;
100         const char **options = NULL;
101         int opt, ldbopts;
102         enum ldb_scope scope = LDB_SCOPE_SUBTREE;
103         int interactive = 0, ret=0;
104
105         ldb_url = getenv("LDB_URL");
106
107         ldbopts = 0;
108         while ((opt = getopt(argc, argv, "b:H:s:o:hi")) != EOF) {
109                 switch (opt) {
110                 case 'b':
111                         basedn = optarg;
112                         break;
113
114                 case 'H':
115                         ldb_url = optarg;
116                         break;
117
118                 case 's':
119                         if (strcmp(optarg, "base") == 0) {
120                                 scope = LDB_SCOPE_BASE;
121                         } else if (strcmp(optarg, "sub") == 0) {
122                                 scope = LDB_SCOPE_SUBTREE;
123                         } else if (strcmp(optarg, "one") == 0) {
124                                 scope = LDB_SCOPE_ONELEVEL;
125                         }
126                         break;
127
128                 case 'i':
129                         interactive = 1;
130                         break;
131
132                 case 'o':
133                         options = ldb_options_parse(options, &ldbopts, optarg);
134                         break;
135
136                 case 'h':
137                 default:
138                         usage();
139                         break;
140                 }
141         }
142
143         if (!ldb_url) {
144                 fprintf(stderr, "You must specify a ldb URL\n\n");
145                 usage();
146         }
147
148         argc -= optind;
149         argv += optind;
150
151         if (argc < 1 && !interactive) {
152                 usage();
153                 exit(1);
154         }
155
156         if (argc > 1) {
157                 attrs = (const char * const *)(argv+1);
158         }
159
160         ldb = ldb_connect(ldb_url, LDB_FLG_RDONLY, options);
161         if (!ldb) {
162                 perror("ldb_connect");
163                 exit(1);
164         }
165
166         ldb_set_debug_stderr(ldb);
167
168         if (interactive) {
169                 char line[1024];
170                 while (fgets(line, sizeof(line), stdin)) {
171                         if (do_search(ldb, basedn, scope, line, attrs) == -1) {
172                                 ret = -1;
173                         }
174                 }
175         } else {
176                 ret = do_search(ldb, basedn, scope, argv[0], attrs);
177         }
178
179         talloc_free(ldb);
180         return ret;
181 }