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);
50 connect to a database. The URL can either be one of the following forms
54 flags is made up of LDB_FLG_*
56 the options are passed uninterpreted to the backend, and are
59 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
63 if (strncmp(url, "tdb:", 4) == 0 ||
64 strchr(url, ':') == NULL) {
65 ret = ltdb_connect(ldb, url, flags, options);
69 else if (strncmp(url, "ldap", 4) == 0) {
70 ret = ildb_connect(ldb, url, flags, options);
73 else if (strncmp(url, "ldap", 4) == 0) {
74 ret = lldb_connect(ldb, url, flags, options);
78 else if (strncmp(url, "sqlite:", 7) == 0) {
79 ret = lsqlite3_connect(ldb, url, flags, options);
83 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'", url);
88 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'", url);
92 if (ldb_load_modules(ldb, options) != 0) {
93 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'", url);
101 search the database given a LDAP-like search expression
103 return the number of records found, or -1 on error
105 Use talloc_free to free the ldb_message returned in 'res'
108 int ldb_search(struct ldb_context *ldb,
110 enum ldb_scope scope,
111 const char *expression,
112 const char * const *attrs, struct ldb_message ***res)
114 return ldb->modules->ops->search(ldb->modules, base, scope, expression, attrs, res);
118 search the database given a LDAP-like search expression
120 return the number of records found, or -1 on error
122 Use talloc_free to free the ldb_message returned in 'res'
125 int ldb_search_bytree(struct ldb_context *ldb,
127 enum ldb_scope scope,
128 struct ldb_parse_tree *tree,
129 const char * const *attrs, struct ldb_message ***res)
131 return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res);
135 add a record to the database. Will fail if a record with the given class and key
138 int ldb_add(struct ldb_context *ldb,
139 const struct ldb_message *message)
141 return ldb->modules->ops->add_record(ldb->modules, message);
145 modify the specified attributes of a record
147 int ldb_modify(struct ldb_context *ldb,
148 const struct ldb_message *message)
150 return ldb->modules->ops->modify_record(ldb->modules, message);
155 delete a record from the database
157 int ldb_delete(struct ldb_context *ldb, const char *dn)
159 return ldb->modules->ops->delete_record(ldb->modules, dn);
163 rename a record in the database
165 int ldb_rename(struct ldb_context *ldb, const char *olddn, const char *newdn)
167 return ldb->modules->ops->rename_record(ldb->modules, olddn, newdn);
173 int ldb_lock(struct ldb_context *ldb, const char *lockname)
175 return ldb->modules->ops->named_lock(ldb->modules, lockname);
181 int ldb_unlock(struct ldb_context *ldb, const char *lockname)
183 return ldb->modules->ops->named_unlock(ldb->modules, lockname);
187 return extended error information
189 const char *ldb_errstring(struct ldb_context *ldb)
191 if (ldb->modules == NULL) {
192 return "ldb not connected";
194 return ldb->modules->ops->errstring(ldb->modules);
199 set backend specific opaque parameters
201 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
203 struct ldb_opaque *o = talloc(ldb, struct ldb_opaque);
208 o->next = ldb->opaque;
216 get a previously set opaque value
218 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
220 struct ldb_opaque *o;
221 for (o=ldb->opaque;o;o=o->next) {
222 if (strcmp(o->name, name) == 0) {