baf0e9ab6519f5ecba8e0fe7eab28d1e1837e030
[kai/samba-autobuild/.git] / source3 / lib / ldb / examples / ldbreader.c
1 /* 
2    example code for the ldb database library
3
4    Copyright (C) Brad Hards (bradh@frogmouth.net) 2005-2006
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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 /** \example ldbreader.c
25
26 The code below shows a simple LDB application.
27
28 It lists / dumps the records in a LDB database to standard output.
29
30 */
31
32 #include "includes.h"
33 #include "ldb/include/ldb.h"
34 #include "ldb/include/ldb_errors.h"
35
36 /*
37   ldb_ldif_write takes a function pointer to a custom output
38   function. This version is about as simple as the output function can
39   be. In a more complex example, you'd likely be doing something with
40   the private data function (e.g. holding a file handle).
41 */
42 static int vprintf_fn(void *private_data, const char *fmt, ...)
43 {
44         int retval;
45         va_list ap;
46
47         va_start(ap, fmt);
48         /* We just write to standard output */
49         retval = vprintf(fmt, ap);
50         va_end(ap);
51         /* Note that the function should return the number of 
52            bytes written, or a negative error code */
53         return retval;
54 }
55   
56 int main(int argc, const char **argv)
57 {
58         struct ldb_context *ldb;
59         const char *expression = "(dn=*)";
60         struct ldb_result *resultMsg;
61         int i;
62
63         /*
64           This is the always the first thing you want to do in an LDB
65           application - initialise up the context structure.
66
67           Note that you can use the context structure as a parent
68           for talloc allocations as well
69         */
70         ldb = ldb_init(NULL);
71
72         /*
73           We now open the database. In this example we just hard code the connection path.
74
75           Also note that the database is being opened read-only. This means that the 
76           call will fail unless the database already exists. 
77         */
78         if (LDB_SUCCESS != ldb_connect(ldb, "tdb://tdbtest.ldb", LDB_FLG_RDONLY, NULL) ){
79                 printf("Problem on connection\n");
80                 exit(-1);
81         }
82
83         /*
84           At this stage we have an open database, and can start using it. It is opened
85           read-only, so a query is possible. 
86
87           We construct a search that just returns all the (sensible) contents. You can do
88           quite fine grained results with the LDAP search syntax, however it is a bit
89           confusing to start with. See RFC2254.
90         */
91         if (LDB_SUCCESS != ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT,
92                                       expression, NULL, &resultMsg) ) {
93                 printf("Problem in search\n");
94                 exit(-1);
95         }
96         
97         printf("%i records returned\n", resultMsg->count);
98
99         /*
100           We can now iterate through the results, writing them out
101           (to standard output) with our custom output routine as defined
102           at the top of this file
103         */
104         for (i = 0; i < resultMsg->count; ++i) {
105                 struct ldb_ldif ldifMsg;
106
107                 printf("Message: %i\n", i+1);
108                 
109                 ldifMsg.changetype = LDB_CHANGETYPE_NONE;
110                 ldifMsg.msg = resultMsg->msgs[i];
111                 ldb_ldif_write(ldb, vprintf_fn, NULL, &ldifMsg);
112         }
113
114         /*
115           There are two objects to clean up - the result from the 
116           ldb_search() query, and the original ldb context.
117         */
118         talloc_free(resultMsg);
119
120         talloc_free(ldb);
121
122         return 0;
123 }