4 Copyright (C) Andrew Tridgell 2004
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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb core API
30 * Description: core API routines interfacing to ldb backends
32 * Author: Andrew Tridgell
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
41 initialise a ldb context
42 The mem_ctx is optional
44 struct ldb_context *ldb_init(void *mem_ctx)
46 struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
49 ret = ldb_setup_wellknown_attributes(ldb);
59 connect to a database. The URL can either be one of the following forms
63 flags is made up of LDB_FLG_*
65 the options are passed uninterpreted to the backend, and are
68 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
72 if (strncmp(url, "tdb:", 4) == 0 ||
73 strchr(url, ':') == NULL) {
74 ret = ltdb_connect(ldb, url, flags, options);
78 else if (strncmp(url, "ldap", 4) == 0) {
79 ret = ildb_connect(ldb, url, flags, options);
82 else if (strncmp(url, "ldap", 4) == 0) {
83 ret = lldb_connect(ldb, url, flags, options);
87 else if (strncmp(url, "sqlite:", 7) == 0) {
88 ret = lsqlite3_connect(ldb, url, flags, options);
92 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
96 if (ret != LDB_SUCCESS) {
97 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
101 if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
102 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'\n", url);
103 return LDB_ERR_OTHER;
109 static void ldb_reset_err_string(struct ldb_context *ldb)
111 if (ldb->err_string) {
112 talloc_free(ldb->err_string);
113 ldb->err_string = NULL;
117 #define FIRST_OP(ldb, op) do { \
118 module = ldb->modules; \
119 while (module && module->ops->op == NULL) module = module->next; \
120 if (module == NULL) return -1; \
126 int ldb_transaction_start(struct ldb_context *ldb)
128 struct ldb_module *module;
129 FIRST_OP(ldb, start_transaction);
131 ldb->transaction_active++;
133 ldb_reset_err_string(ldb);
135 return module->ops->start_transaction(module);
141 int ldb_transaction_commit(struct ldb_context *ldb)
143 struct ldb_module *module;
144 FIRST_OP(ldb, end_transaction);
146 if (ldb->transaction_active > 0) {
147 ldb->transaction_active--;
149 return LDB_ERR_OPERATIONS_ERROR;
152 ldb_reset_err_string(ldb);
154 return module->ops->end_transaction(module);
160 int ldb_transaction_cancel(struct ldb_context *ldb)
162 struct ldb_module *module;
163 FIRST_OP(ldb, del_transaction);
165 if (ldb->transaction_active > 0) {
166 ldb->transaction_active--;
168 return LDB_ERR_OPERATIONS_ERROR;
171 return module->ops->del_transaction(module);
175 search the database given a LDAP-like search expression
177 return the number of records found, or -1 on error
179 Use talloc_free to free the ldb_message returned in 'res'
182 int ldb_search(struct ldb_context *ldb,
183 const struct ldb_dn *base,
184 enum ldb_scope scope,
185 const char *expression,
186 const char * const *attrs, struct ldb_message ***res)
188 struct ldb_parse_tree *tree;
191 tree = ldb_parse_tree(ldb, expression);
193 ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Unable to parse search expression"));
197 ret = ldb_search_bytree(ldb, base, scope, tree, attrs, res);
204 search the database given a search tree
206 return the number of records found, or -1 on error
208 Use talloc_free to free the ldb_message returned in 'res'
211 int ldb_search_bytree(struct ldb_context *ldb,
212 const struct ldb_dn *base,
213 enum ldb_scope scope,
214 struct ldb_parse_tree *tree,
215 const char * const *attrs, struct ldb_message ***res)
217 struct ldb_module *module;
218 FIRST_OP(ldb, search_bytree);
220 ldb_reset_err_string(ldb);
222 return module->ops->search_bytree(module, base, scope, tree, attrs, res);
226 check for an error return from an op
227 if an op fails, but has not setup an error string, then setup one now
229 static int ldb_op_finish(struct ldb_context *ldb, int status)
231 if (status == LDB_SUCCESS) {
232 return ldb_transaction_commit(ldb);
234 if (ldb->err_string == NULL) {
235 /* no error string was setup by the backend */
236 ldb_set_errstring(ldb->modules,
237 talloc_asprintf(ldb, "ldb error %d", status));
239 ldb_transaction_cancel(ldb);
244 add a record to the database. Will fail if a record with the given class and key
247 int ldb_add(struct ldb_context *ldb,
248 const struct ldb_message *message)
250 struct ldb_module *module;
253 FIRST_OP(ldb, add_record);
255 ldb_reset_err_string(ldb);
257 status = ldb_msg_sanity_check(message);
258 if (status != LDB_SUCCESS) return status;
260 if (! ldb->transaction_active) {
261 status = ldb_transaction_start(ldb);
262 if (status != LDB_SUCCESS) return status;
264 status = module->ops->add_record(module, message);
265 return ldb_op_finish(ldb, status);
268 return module->ops->add_record(module, message);
272 modify the specified attributes of a record
274 int ldb_modify(struct ldb_context *ldb,
275 const struct ldb_message *message)
277 struct ldb_module *module;
280 FIRST_OP(ldb, modify_record);
282 ldb_reset_err_string(ldb);
284 status = ldb_msg_sanity_check(message);
285 if (status != LDB_SUCCESS) return status;
287 if (! ldb->transaction_active) {
288 status = ldb_transaction_start(ldb);
289 if (status != LDB_SUCCESS) return status;
291 status = module->ops->modify_record(module, message);
292 return ldb_op_finish(ldb, status);
295 return module->ops->modify_record(module, message);
300 delete a record from the database
302 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
304 struct ldb_module *module;
307 FIRST_OP(ldb, delete_record);
309 ldb_reset_err_string(ldb);
311 if (! ldb->transaction_active) {
312 status = ldb_transaction_start(ldb);
313 if (status != LDB_SUCCESS) return status;
315 status = module->ops->delete_record(module, dn);
316 return ldb_op_finish(ldb, status);
319 return module->ops->delete_record(module, dn);
323 rename a record in the database
325 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
327 struct ldb_module *module;
330 FIRST_OP(ldb, rename_record);
332 ldb_reset_err_string(ldb);
334 if (! ldb->transaction_active) {
335 status = ldb_transaction_start(ldb);
336 if (status != LDB_SUCCESS) return status;
338 status = module->ops->rename_record(module, olddn, newdn);
339 return ldb_op_finish(ldb, status);
342 return module->ops->rename_record(module, olddn, newdn);
348 return extended error information
350 const char *ldb_errstring(struct ldb_context *ldb)
352 if (ldb->err_string) {
353 return ldb->err_string;
361 set backend specific opaque parameters
363 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
365 struct ldb_opaque *o;
367 /* allow updating an existing value */
368 for (o=ldb->opaque;o;o=o->next) {
369 if (strcmp(o->name, name) == 0) {
375 o = talloc(ldb, struct ldb_opaque);
378 return LDB_ERR_OTHER;
380 o->next = ldb->opaque;
388 get a previously set opaque value
390 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
392 struct ldb_opaque *o;
393 for (o=ldb->opaque;o;o=o->next) {
394 if (strcmp(o->name, name) == 0) {