r152: a quick airport commit ....
[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         exit(1);
45 }
46
47  int main(int argc, char * const argv[])
48 {
49         struct ldb_context *ldb;
50         struct ldb_message **msgs;
51         int ret, i;
52         const char *expression;
53         char * const *attrs = NULL;
54         const char *ldb_url;
55         const char *basedn = NULL;
56         int opt;
57         enum ldb_scope scope = LDB_SCOPE_SUBTREE;
58
59         ldb_url = getenv("LDB_URL");
60
61         while ((opt = getopt(argc, argv, "b:H:s:h")) != EOF) {
62                 switch (opt) {
63                 case 'b':
64                         basedn = optarg;
65                         break;
66
67                 case 'H':
68                         ldb_url = optarg;
69                         break;
70
71                 case 's':
72                         if (strcmp(optarg, "base") == 0) {
73                                 scope = LDB_SCOPE_BASE;
74                         } else if (strcmp(optarg, "sub") == 0) {
75                                 scope = LDB_SCOPE_SUBTREE;
76                         } else if (strcmp(optarg, "one") == 0) {
77                                 scope = LDB_SCOPE_ONELEVEL;
78                         }
79                         break;
80
81                 case 'h':
82                 default:
83                         usage();
84                         break;
85                 }
86         }
87
88         if (!ldb_url) {
89                 fprintf(stderr, "You must specify a ldb URL\n");
90                 exit(1);
91         }
92
93         argc -= optind;
94         argv += optind;
95
96         if (argc < 1) {
97                 usage();
98                 exit(1);
99         }
100
101         if (argc > 1) {
102                 attrs = argv+1;
103         }
104
105         expression = argv[0];
106
107         ldb = ldb_connect(ldb_url, 0, NULL);
108
109         if (!ldb) {
110                 perror("ldb_connect");
111                 exit(1);
112         }
113
114         ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
115
116         if (ret == -1) {
117                 printf("search failed - %s\n", ldb_errstring(ldb));
118                 exit(1);
119         }
120
121         printf("# returned %d records\n", ret);
122
123         for (i=0;i<ret;i++) {
124                 struct ldb_ldif ldif;
125                 printf("# record %d\n", i+1);
126
127                 ldif.changetype = LDB_CHANGETYPE_NONE;
128                 ldif.msg = *msgs[i];
129
130                 ldif_write_file(stdout, &ldif);
131         }
132
133         if (ret > 0) {
134                 ret = ldb_search_free(ldb, msgs);
135                 if (ret == -1) {
136                         fprintf(stderr, "search_free failed\n");
137                         exit(1);
138                 }
139         }
140
141         ldb_close(ldb);
142         return 0;
143 }