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_private.h"
40 initialise a ldb context
41 The mem_ctx is optional
43 struct ldb_context *ldb_init(void *mem_ctx)
45 struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
48 ret = ldb_setup_wellknown_attributes(ldb);
58 connect to a database. The URL can either be one of the following forms
62 flags is made up of LDB_FLG_*
64 the options are passed uninterpreted to the backend, and are
67 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
71 if (strncmp(url, "tdb:", 4) == 0 ||
72 strchr(url, ':') == NULL) {
73 ret = ltdb_connect(ldb, url, flags, options);
77 else if (strncmp(url, "ldap", 4) == 0) {
78 ret = ildb_connect(ldb, url, flags, options);
81 else if (strncmp(url, "ldap", 4) == 0) {
82 ret = lldb_connect(ldb, url, flags, options);
86 else if (strncmp(url, "sqlite:", 7) == 0) {
87 ret = lsqlite3_connect(ldb, url, flags, options);
91 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
96 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
100 if (ldb_load_modules(ldb, options) != 0) {
101 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'\n", url);
111 static int ldb_start_trans(struct ldb_context *ldb)
113 return ldb->modules->ops->start_transaction(ldb->modules);
119 static int ldb_end_trans(struct ldb_context *ldb, int status)
121 return ldb->modules->ops->end_transaction(ldb->modules, status);
125 search the database given a LDAP-like search expression
127 return the number of records found, or -1 on error
129 Use talloc_free to free the ldb_message returned in 'res'
132 int ldb_search(struct ldb_context *ldb,
133 const struct ldb_dn *base,
134 enum ldb_scope scope,
135 const char *expression,
136 const char * const *attrs, struct ldb_message ***res)
138 return ldb->modules->ops->search(ldb->modules, base, scope, expression, attrs, res);
142 search the database given a LDAP-like search expression
144 return the number of records found, or -1 on error
146 Use talloc_free to free the ldb_message returned in 'res'
149 int ldb_search_bytree(struct ldb_context *ldb,
150 const struct ldb_dn *base,
151 enum ldb_scope scope,
152 struct ldb_parse_tree *tree,
153 const char * const *attrs, struct ldb_message ***res)
155 return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res);
159 add a record to the database. Will fail if a record with the given class and key
162 int ldb_add(struct ldb_context *ldb,
163 const struct ldb_message *message)
165 int status = ldb_start_trans(ldb);
166 if (status != 0) return status;
168 status = ldb->modules->ops->add_record(ldb->modules, message);
169 return ldb_end_trans(ldb, status);
173 modify the specified attributes of a record
175 int ldb_modify(struct ldb_context *ldb,
176 const struct ldb_message *message)
178 int status = ldb_start_trans(ldb);
179 if (status != 0) return status;
181 status = ldb->modules->ops->modify_record(ldb->modules, message);
182 return ldb_end_trans(ldb, status);
187 delete a record from the database
189 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
191 int status = ldb_start_trans(ldb);
192 if (status != 0) return status;
194 status = ldb->modules->ops->delete_record(ldb->modules, dn);
195 return ldb_end_trans(ldb, status);
199 rename a record in the database
201 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
203 int status = ldb_start_trans(ldb);
204 if (status != 0) return status;
206 status = ldb->modules->ops->rename_record(ldb->modules, olddn, newdn);
207 return ldb_end_trans(ldb, status);
211 return extended error information
213 const char *ldb_errstring(struct ldb_context *ldb)
215 if (ldb->modules == NULL) {
216 return "ldb not connected";
218 return ldb->modules->ops->errstring(ldb->modules);
223 set backend specific opaque parameters
225 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
227 struct ldb_opaque *o = talloc(ldb, struct ldb_opaque);
232 o->next = ldb->opaque;
240 get a previously set opaque value
242 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
244 struct ldb_opaque *o;
245 for (o=ldb->opaque;o;o=o->next) {
246 if (strcmp(o->name, name) == 0) {