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;
120 int ldb_transaction_start(struct ldb_context *ldb)
122 ldb->transaction_active++;
124 ldb_reset_err_string(ldb);
126 return ldb->modules->ops->start_transaction(ldb->modules);
132 int ldb_transaction_commit(struct ldb_context *ldb)
134 if (ldb->transaction_active > 0) {
135 ldb->transaction_active--;
137 return LDB_ERR_OPERATIONS_ERROR;
140 ldb_reset_err_string(ldb);
142 return ldb->modules->ops->end_transaction(ldb->modules);
148 int ldb_transaction_cancel(struct ldb_context *ldb)
150 if (ldb->transaction_active > 0) {
151 ldb->transaction_active--;
153 return LDB_ERR_OPERATIONS_ERROR;
156 ldb_reset_err_string(ldb);
158 return ldb->modules->ops->del_transaction(ldb->modules);
162 search the database given a LDAP-like search expression
164 return the number of records found, or -1 on error
166 Use talloc_free to free the ldb_message returned in 'res'
169 int ldb_search(struct ldb_context *ldb,
170 const struct ldb_dn *base,
171 enum ldb_scope scope,
172 const char *expression,
173 const char * const *attrs, struct ldb_message ***res)
175 struct ldb_parse_tree *tree;
178 tree = ldb_parse_tree(ldb, expression);
180 ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Unable to parse search expression"));
184 ret = ldb_search_bytree(ldb, base, scope, tree, attrs, res);
191 search the database given a search tree
193 return the number of records found, or -1 on error
195 Use talloc_free to free the ldb_message returned in 'res'
198 int ldb_search_bytree(struct ldb_context *ldb,
199 const struct ldb_dn *base,
200 enum ldb_scope scope,
201 struct ldb_parse_tree *tree,
202 const char * const *attrs, struct ldb_message ***res)
204 ldb_reset_err_string(ldb);
206 return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res);
210 add a record to the database. Will fail if a record with the given class and key
213 int ldb_add(struct ldb_context *ldb,
214 const struct ldb_message *message)
218 ldb_reset_err_string(ldb);
220 status = ldb_msg_sanity_check(message);
221 if (status != LDB_SUCCESS) return status;
223 if (! ldb->transaction_active) {
224 status = ldb_transaction_start(ldb);
225 if (status != LDB_SUCCESS) return status;
227 status = ldb->modules->ops->add_record(ldb->modules, message);
228 if (status != LDB_SUCCESS) return ldb_transaction_cancel(ldb);
229 return ldb_transaction_commit(ldb);
232 return ldb->modules->ops->add_record(ldb->modules, message);
236 modify the specified attributes of a record
238 int ldb_modify(struct ldb_context *ldb,
239 const struct ldb_message *message)
243 ldb_reset_err_string(ldb);
245 status = ldb_msg_sanity_check(message);
246 if (status != LDB_SUCCESS) return status;
248 if (! ldb->transaction_active) {
249 status = ldb_transaction_start(ldb);
250 if (status != LDB_SUCCESS) return status;
252 status = ldb->modules->ops->modify_record(ldb->modules, message);
253 if (status != LDB_SUCCESS) return ldb_transaction_cancel(ldb);
254 return ldb_transaction_commit(ldb);
257 return ldb->modules->ops->modify_record(ldb->modules, message);
262 delete a record from the database
264 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
268 ldb_reset_err_string(ldb);
270 if (! ldb->transaction_active) {
271 status = ldb_transaction_start(ldb);
272 if (status != LDB_SUCCESS) return status;
274 status = ldb->modules->ops->delete_record(ldb->modules, dn);
275 if (status != LDB_SUCCESS) return ldb_transaction_cancel(ldb);
276 return ldb_transaction_commit(ldb);
279 return ldb->modules->ops->delete_record(ldb->modules, dn);
283 rename a record in the database
285 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
289 ldb_reset_err_string(ldb);
291 if (! ldb->transaction_active) {
292 status = ldb_transaction_start(ldb);
293 if (status != LDB_SUCCESS) return status;
295 status = ldb->modules->ops->rename_record(ldb->modules, olddn, newdn);
296 if (status != LDB_SUCCESS) return ldb_transaction_cancel(ldb);
297 return ldb_transaction_commit(ldb);
300 return ldb->modules->ops->rename_record(ldb->modules, olddn, newdn);
306 return extended error information
308 const char *ldb_errstring(struct ldb_context *ldb)
310 if (ldb->err_string) {
311 return ldb->err_string;
319 set backend specific opaque parameters
321 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
323 struct ldb_opaque *o = talloc(ldb, struct ldb_opaque);
326 return LDB_ERR_OTHER;
328 o->next = ldb->opaque;
336 get a previously set opaque value
338 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
340 struct ldb_opaque *o;
341 for (o=ldb->opaque;o;o=o->next) {
342 if (strcmp(o->name, name) == 0) {