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