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