r7828: Although there is still plenty to do, ldb_sqlite3 now passes the set of tests
[gd/samba-autobuild/.git] / source / lib / ldb / common / ldb.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb core API
29  *
30  *  Description: core API routines interfacing to ldb backends
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38
39 /* 
40    initialise a ldb context
41    The mem_ctx is optional
42 */
43 struct ldb_context *ldb_init(void *mem_ctx)
44 {
45         struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
46         return ldb;
47 }
48
49 /* 
50  connect to a database. The URL can either be one of the following forms
51    ldb://path
52    ldapi://path
53
54    flags is made up of LDB_FLG_*
55
56    the options are passed uninterpreted to the backend, and are
57    backend specific
58 */
59 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
60 {
61         int ret;
62
63         if (strncmp(url, "tdb:", 4) == 0 ||
64             strchr(url, ':') == NULL) {
65                 ret = ltdb_connect(ldb, url, flags, options);
66         }
67
68 #if HAVE_ILDAP
69         else if (strncmp(url, "ldap", 4) == 0) {
70                 ret = ildb_connect(ldb, url, flags, options);
71         }
72 #elif HAVE_LDAP
73         else if (strncmp(url, "ldap", 4) == 0) {
74                 ret = lldb_connect(ldb, url, flags, options);
75         }
76 #endif
77 #if HAVE_SQLITE3
78         else if (strncmp(url, "sqlite:", 7) == 0) {
79                 ret = lsqlite3_connect(ldb, url, flags, options);
80         }
81 #endif
82         else {
83                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'", url);
84                 return -1;
85         }
86
87         if (ret != 0) {
88                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'", url);
89                 return ret;
90         }
91
92         if (ldb_load_modules(ldb, options) != 0) {
93                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'", url);
94                 return -1;
95         }
96
97         return 0;
98 }
99
100 /*
101   search the database given a LDAP-like search expression
102
103   return the number of records found, or -1 on error
104
105   Use talloc_free to free the ldb_message returned in 'res'
106
107 */
108 int ldb_search(struct ldb_context *ldb, 
109                const char *base,
110                enum ldb_scope scope,
111                const char *expression,
112                const char * const *attrs, struct ldb_message ***res)
113 {
114         return ldb->modules->ops->search(ldb->modules, base, scope, expression, attrs, res);
115 }
116
117 /*
118   search the database given a LDAP-like search expression
119
120   return the number of records found, or -1 on error
121
122   Use talloc_free to free the ldb_message returned in 'res'
123
124 */
125 int ldb_search_bytree(struct ldb_context *ldb, 
126                       const char *base,
127                       enum ldb_scope scope,
128                       struct ldb_parse_tree *tree,
129                       const char * const *attrs, struct ldb_message ***res)
130 {
131         return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res);
132 }
133
134 /*
135   add a record to the database. Will fail if a record with the given class and key
136   already exists
137 */
138 int ldb_add(struct ldb_context *ldb, 
139             const struct ldb_message *message)
140 {
141         return ldb->modules->ops->add_record(ldb->modules, message);
142 }
143
144 /*
145   modify the specified attributes of a record
146 */
147 int ldb_modify(struct ldb_context *ldb, 
148                const struct ldb_message *message)
149 {
150         return ldb->modules->ops->modify_record(ldb->modules, message);
151 }
152
153
154 /*
155   delete a record from the database
156 */
157 int ldb_delete(struct ldb_context *ldb, const char *dn)
158 {
159         return ldb->modules->ops->delete_record(ldb->modules, dn);
160 }
161
162 /*
163   rename a record in the database
164 */
165 int ldb_rename(struct ldb_context *ldb, const char *olddn, const char *newdn)
166 {
167         return ldb->modules->ops->rename_record(ldb->modules, olddn, newdn);
168 }
169
170 /*
171   create a named lock
172 */
173 int ldb_lock(struct ldb_context *ldb, const char *lockname)
174 {
175         return ldb->modules->ops->named_lock(ldb->modules, lockname);
176 }
177
178 /*
179   release a named lock
180 */
181 int ldb_unlock(struct ldb_context *ldb, const char *lockname)
182 {
183         return ldb->modules->ops->named_unlock(ldb->modules, lockname);
184 }
185
186 /*
187   return extended error information 
188 */
189 const char *ldb_errstring(struct ldb_context *ldb)
190 {
191         if (ldb->modules == NULL) {
192                 return "ldb not connected";
193         }
194         return ldb->modules->ops->errstring(ldb->modules);
195 }
196
197
198 /*
199   set backend specific opaque parameters
200 */
201 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
202 {
203         struct ldb_opaque *o = talloc(ldb, struct ldb_opaque);
204         if (o == NULL) {
205                 ldb_oom(ldb);
206                 return -1;
207         }
208         o->next = ldb->opaque;
209         o->name = name;
210         o->value = value;
211         ldb->opaque = o;
212         return 0;
213 }
214
215 /*
216   get a previously set opaque value
217 */
218 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
219 {
220         struct ldb_opaque *o;
221         for (o=ldb->opaque;o;o=o->next) {
222                 if (strcmp(o->name, name) == 0) {
223                         return o->value;
224                 }
225         }
226         return NULL;
227 }