2 example code for the ldb database library
4 Copyright (C) Brad Hards (bradh@frogmouth.net) 2005-2006
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
24 /** \example ldbreader.c
26 The code below shows a simple LDB application.
28 It lists / dumps the records in a LDB database to standard output.
35 ldb_ldif_write takes a function pointer to a custom output
36 function. This version is about as simple as the output function can
37 be. In a more complex example, you'd likely be doing something with
38 the private data function (e.g. holding a file handle).
40 static int vprintf_fn(void *private_data
, const char *fmt
, ...)
46 /* We just write to standard output */
47 retval
= vprintf(fmt
, ap
);
49 /* Note that the function should return the number of
50 bytes written, or a negative error code */
54 int main(int argc
, const char **argv
)
56 struct ldb_context
*ldb
;
57 const char *expression
= "(dn=*)";
58 struct ldb_result
*resultMsg
;
62 This is the always the first thing you want to do in an LDB
63 application - initialise up the context structure.
65 Note that you can use the context structure as a parent
66 for talloc allocations as well
68 ldb
= ldb_init(NULL
, NULL
);
71 We now open the database. In this example we just hard code the connection path.
73 Also note that the database is being opened read-only. This means that the
74 call will fail unless the database already exists.
76 if (LDB_SUCCESS
!= ldb_connect(ldb
, "tdb://tdbtest.ldb", LDB_FLG_RDONLY
, NULL
) ){
77 printf("Problem on connection\n");
82 At this stage we have an open database, and can start using it. It is opened
83 read-only, so a query is possible.
85 We construct a search that just returns all the (sensible) contents. You can do
86 quite fine grained results with the LDAP search syntax, however it is a bit
87 confusing to start with. See RFC2254.
89 if (LDB_SUCCESS
!= ldb_search(ldb
, ldb
, &resultMsg
,
90 NULL
, LDB_SCOPE_DEFAULT
, NULL
,
92 printf("Problem in search\n");
96 printf("%i records returned\n", resultMsg
->count
);
99 We can now iterate through the results, writing them out
100 (to standard output) with our custom output routine as defined
101 at the top of this file
103 for (i
= 0; i
< resultMsg
->count
; ++i
) {
104 struct ldb_ldif ldifMsg
;
106 printf("Message: %i\n", i
+1);
108 ldifMsg
.changetype
= LDB_CHANGETYPE_NONE
;
109 ldifMsg
.msg
= resultMsg
->msgs
[i
];
110 ldb_ldif_write(ldb
, vprintf_fn
, NULL
, &ldifMsg
);
114 There are two objects to clean up - the result from the
115 ldb_search() query, and the original ldb context.
117 talloc_free(resultMsg
);