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