r7709: - convert ldb to use popt, so that it can interact with the samba
[samba.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 #include "ldb/tools/cmdline.h"
39
40 #ifdef _SAMBA_BUILD_
41 #include "system/filesys.h"
42 #endif
43
44 static void usage(void)
45 {
46         printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
47         printf("Options:\n");
48         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
49         printf("  -s base|sub|one  choose search scope\n");
50         printf("  -b basedn        choose baseDN\n");
51         printf("  -i               read search expressions from stdin\n");
52         printf("  -S               sort returned attributes\n");
53         printf("  -o options       pass options like modules to activate\n");
54         printf("              e.g: -o modules:timestamps\n");
55         exit(1);
56 }
57
58 static int do_compare_msg(struct ldb_message **el1,
59                 struct ldb_message **el2)
60 {
61         return ldb_dn_cmp((*el1)->dn, (*el2)->dn);
62 }
63
64 static int do_search(struct ldb_context *ldb,
65                      const char *basedn,
66                      int scope,
67                      int sort_attribs,
68                      const char *expression,
69                      const char * const *attrs)
70 {
71         int ret, i;
72         struct ldb_message **msgs;
73
74         ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
75         if (ret == -1) {
76                 printf("search failed - %s\n", ldb_errstring(ldb));
77                 return -1;
78         }
79
80         printf("# returned %d records\n", ret);
81
82         if (sort_attribs) {
83                 qsort(msgs, ret, sizeof(struct ldb_message *),
84                                 (comparison_fn_t)do_compare_msg);
85         }
86
87         for (i=0;i<ret;i++) {
88                 struct ldb_ldif ldif;
89                 printf("# record %d\n", i+1);
90
91                 ldif.changetype = LDB_CHANGETYPE_NONE;
92                 ldif.msg = msgs[i];
93
94                 if (sort_attribs) {
95                         /*
96                          * Ensure attributes are always returned in the same
97                          * order.  For testing, this makes comparison of old
98                          * vs. new much easier.
99                          */
100                         ldb_msg_sort_elements(ldif.msg);
101                 }
102                 
103                 ldb_ldif_write_file(ldb, stdout, &ldif);
104         }
105
106         if (ret > 0) {
107                 ret = talloc_free(msgs);
108                 if (ret == -1) {
109                         fprintf(stderr, "talloc_free failed\n");
110                         exit(1);
111                 }
112         }
113
114         return 0;
115 }
116
117  int main(int argc, const char **argv)
118 {
119         struct ldb_context *ldb;
120         const char * const * attrs = NULL;
121         struct ldb_cmdline *options;
122         int ret;
123
124         ldb = ldb_init(NULL);
125
126         options = ldb_cmdline_process(ldb, argc, argv, usage);
127         
128         if (options->argc < 1 && !options->interactive) {
129                 usage();
130                 exit(1);
131         }
132
133         if (options->argc > 1) {
134                 attrs = (const char * const *)(options->argv+1);
135         }
136
137         ret = ldb_connect(ldb, options->url, LDB_FLG_RDONLY, options->options);
138         if (ret != 0) {
139                 fprintf(stderr, "Failed to connect to %s - %s\n", 
140                         options->url, ldb_errstring(ldb));
141                 talloc_free(ldb);
142                 exit(1);
143         }
144
145         if (options->interactive) {
146                 char line[1024];
147                 while (fgets(line, sizeof(line), stdin)) {
148                         if (do_search(ldb, options->basedn, 
149                                       options->scope, options->sorted, line, attrs) == -1) {
150                                 ret = -1;
151                         }
152                 }
153         } else {
154                 ret = do_search(ldb, options->basedn, options->scope, options->sorted, 
155                                 options->argv[0], attrs);
156         }
157
158         talloc_free(ldb);
159         return ret;
160 }