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