r10299: remove the public (un)lock functions and introduce a transaction based
[gd/samba/.git] / source4 / 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         int ret;
47
48         ret = ldb_setup_wellknown_attributes(ldb);
49         if (ret != 0) {
50                 talloc_free(ldb);
51                 return NULL;
52         }
53
54         return ldb;
55 }
56
57 /* 
58  connect to a database. The URL can either be one of the following forms
59    ldb://path
60    ldapi://path
61
62    flags is made up of LDB_FLG_*
63
64    the options are passed uninterpreted to the backend, and are
65    backend specific
66 */
67 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
68 {
69         int ret;
70
71         if (strncmp(url, "tdb:", 4) == 0 ||
72             strchr(url, ':') == NULL) {
73                 ret = ltdb_connect(ldb, url, flags, options);
74         }
75
76 #if HAVE_ILDAP
77         else if (strncmp(url, "ldap", 4) == 0) {
78                 ret = ildb_connect(ldb, url, flags, options);
79         }
80 #elif HAVE_LDAP
81         else if (strncmp(url, "ldap", 4) == 0) {
82                 ret = lldb_connect(ldb, url, flags, options);
83         }
84 #endif
85 #if HAVE_SQLITE3
86         else if (strncmp(url, "sqlite:", 7) == 0) {
87                 ret = lsqlite3_connect(ldb, url, flags, options);
88         }
89 #endif
90         else {
91                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
92                 return -1;
93         }
94
95         if (ret != 0) {
96                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
97                 return ret;
98         }
99
100         if (ldb_load_modules(ldb, options) != 0) {
101                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'\n", url);
102                 return -1;
103         }
104
105         return 0;
106 }
107
108 /*
109   start a transaction
110 */
111 static int ldb_start_trans(struct ldb_context *ldb)
112 {
113         return ldb->modules->ops->start_transaction(ldb->modules);
114 }
115
116 /*
117   end a transaction
118 */
119 static int ldb_end_trans(struct ldb_context *ldb, int status)
120 {
121         return ldb->modules->ops->end_transaction(ldb->modules, status);
122 }
123
124 /*
125   search the database given a LDAP-like search expression
126
127   return the number of records found, or -1 on error
128
129   Use talloc_free to free the ldb_message returned in 'res'
130
131 */
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)
137 {
138         return ldb->modules->ops->search(ldb->modules, base, scope, expression, attrs, res);
139 }
140
141 /*
142   search the database given a LDAP-like search expression
143
144   return the number of records found, or -1 on error
145
146   Use talloc_free to free the ldb_message returned in 'res'
147
148 */
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)
154 {
155         return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res);
156 }
157
158 /*
159   add a record to the database. Will fail if a record with the given class and key
160   already exists
161 */
162 int ldb_add(struct ldb_context *ldb, 
163             const struct ldb_message *message)
164 {
165         int status = ldb_start_trans(ldb);
166         if (status != 0) return status;
167
168         status = ldb->modules->ops->add_record(ldb->modules, message);
169         return ldb_end_trans(ldb, status);
170 }
171
172 /*
173   modify the specified attributes of a record
174 */
175 int ldb_modify(struct ldb_context *ldb, 
176                const struct ldb_message *message)
177 {
178         int status = ldb_start_trans(ldb);
179         if (status != 0) return status;
180
181         status = ldb->modules->ops->modify_record(ldb->modules, message);
182         return ldb_end_trans(ldb, status);
183 }
184
185
186 /*
187   delete a record from the database
188 */
189 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
190 {
191         int status = ldb_start_trans(ldb);
192         if (status != 0) return status;
193
194         status = ldb->modules->ops->delete_record(ldb->modules, dn);
195         return ldb_end_trans(ldb, status);
196 }
197
198 /*
199   rename a record in the database
200 */
201 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
202 {
203         int status = ldb_start_trans(ldb);
204         if (status != 0) return status;
205
206         status = ldb->modules->ops->rename_record(ldb->modules, olddn, newdn);
207         return ldb_end_trans(ldb, status);
208 }
209
210 /*
211   return extended error information 
212 */
213 const char *ldb_errstring(struct ldb_context *ldb)
214 {
215         if (ldb->modules == NULL) {
216                 return "ldb not connected";
217         }
218         return ldb->modules->ops->errstring(ldb->modules);
219 }
220
221
222 /*
223   set backend specific opaque parameters
224 */
225 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
226 {
227         struct ldb_opaque *o = talloc(ldb, struct ldb_opaque);
228         if (o == NULL) {
229                 ldb_oom(ldb);
230                 return -1;
231         }
232         o->next = ldb->opaque;
233         o->name = name;
234         o->value = value;
235         ldb->opaque = o;
236         return 0;
237 }
238
239 /*
240   get a previously set opaque value
241 */
242 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
243 {
244         struct ldb_opaque *o;
245         for (o=ldb->opaque;o;o=o->next) {
246                 if (strcmp(o->name, name) == 0) {
247                         return o->value;
248                 }
249         }
250         return NULL;
251 }