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);
109 search the database given a LDAP-like search expression
111 return the number of records found, or -1 on error
113 Use talloc_free to free the ldb_message returned in 'res'
116 int ldb_search(struct ldb_context *ldb,
117 const struct ldb_dn *base,
118 enum ldb_scope scope,
119 const char *expression,
120 const char * const *attrs, struct ldb_message ***res)
122 return ldb->modules->ops->search(ldb->modules, base, scope, expression, attrs, res);
126 search the database given a LDAP-like search expression
128 return the number of records found, or -1 on error
130 Use talloc_free to free the ldb_message returned in 'res'
133 int ldb_search_bytree(struct ldb_context *ldb,
134 const struct ldb_dn *base,
135 enum ldb_scope scope,
136 struct ldb_parse_tree *tree,
137 const char * const *attrs, struct ldb_message ***res)
139 return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res);
143 add a record to the database. Will fail if a record with the given class and key
146 int ldb_add(struct ldb_context *ldb,
147 const struct ldb_message *message)
149 return ldb->modules->ops->add_record(ldb->modules, message);
153 modify the specified attributes of a record
155 int ldb_modify(struct ldb_context *ldb,
156 const struct ldb_message *message)
158 return ldb->modules->ops->modify_record(ldb->modules, message);
163 delete a record from the database
165 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
167 return ldb->modules->ops->delete_record(ldb->modules, dn);
171 rename a record in the database
173 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
175 return ldb->modules->ops->rename_record(ldb->modules, olddn, newdn);
181 int ldb_lock(struct ldb_context *ldb, const char *lockname)
183 return ldb->modules->ops->named_lock(ldb->modules, lockname);
189 int ldb_unlock(struct ldb_context *ldb, const char *lockname)
191 return ldb->modules->ops->named_unlock(ldb->modules, lockname);
195 return extended error information
197 const char *ldb_errstring(struct ldb_context *ldb)
199 if (ldb->modules == NULL) {
200 return "ldb not connected";
202 return ldb->modules->ops->errstring(ldb->modules);
207 set backend specific opaque parameters
209 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
211 struct ldb_opaque *o = talloc(ldb, struct ldb_opaque);
216 o->next = ldb->opaque;
224 get a previously set opaque value
226 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
228 struct ldb_opaque *o;
229 for (o=ldb->opaque;o;o=o->next) {
230 if (strcmp(o->name, name) == 0) {