r456: - added -i option to ldbsearch
[samba.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 void 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;
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                 ldif_write_file(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
84  int main(int argc, char * const argv[])
85 {
86         struct ldb_context *ldb;
87         const char * const * attrs = NULL;
88         const char *ldb_url;
89         const char *basedn = NULL;
90         int opt;
91         enum ldb_scope scope = LDB_SCOPE_SUBTREE;
92         int interactive = 0;
93
94         ldb_url = getenv("LDB_URL");
95
96         while ((opt = getopt(argc, argv, "b:H:s:hi")) != EOF) {
97                 switch (opt) {
98                 case 'b':
99                         basedn = optarg;
100                         break;
101
102                 case 'H':
103                         ldb_url = optarg;
104                         break;
105
106                 case 's':
107                         if (strcmp(optarg, "base") == 0) {
108                                 scope = LDB_SCOPE_BASE;
109                         } else if (strcmp(optarg, "sub") == 0) {
110                                 scope = LDB_SCOPE_SUBTREE;
111                         } else if (strcmp(optarg, "one") == 0) {
112                                 scope = LDB_SCOPE_ONELEVEL;
113                         }
114                         break;
115
116                 case 'i':
117                         interactive = 1;
118                         break;
119
120                 case 'h':
121                 default:
122                         usage();
123                         break;
124                 }
125         }
126
127         if (!ldb_url) {
128                 fprintf(stderr, "You must specify a ldb URL\n\n");
129                 usage();
130         }
131
132         argc -= optind;
133         argv += optind;
134
135         if (argc < 1 && !interactive) {
136                 usage();
137                 exit(1);
138         }
139
140         if (argc > 1) {
141                 attrs = argv+1;
142         }
143
144         ldb = ldb_connect(ldb_url, 0, NULL);
145         if (!ldb) {
146                 perror("ldb_connect");
147                 exit(1);
148         }
149
150         if (interactive) {
151                 char line[1024];
152                 while (fgets(line, sizeof(line), stdin)) {
153                         do_search(ldb, basedn, scope, line, attrs);
154                 }
155         } else {
156                 do_search(ldb, basedn, scope, argv[0], attrs);
157         }
158
159         ldb_close(ldb);
160         return 0;
161 }