r11958: - fixed memory leaks in the ldb_result handling in ldb operations
[bbaumbach/samba-autobuild/.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_errors.h"
38 #include "ldb/include/ldb_private.h"
39
40 /* 
41    initialise a ldb context
42    The mem_ctx is optional
43 */
44 struct ldb_context *ldb_init(void *mem_ctx)
45 {
46         struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
47         int ret;
48
49         ret = ldb_setup_wellknown_attributes(ldb);
50         if (ret != 0) {
51                 talloc_free(ldb);
52                 return NULL;
53         }
54
55         return ldb;
56 }
57
58 /* 
59  connect to a database. The URL can either be one of the following forms
60    ldb://path
61    ldapi://path
62
63    flags is made up of LDB_FLG_*
64
65    the options are passed uninterpreted to the backend, and are
66    backend specific
67 */
68 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
69 {
70         int ret;
71
72         if (strncmp(url, "tdb:", 4) == 0 ||
73             strchr(url, ':') == NULL) {
74                 ret = ltdb_connect(ldb, url, flags, options);
75         }
76
77 #if HAVE_ILDAP
78         else if (strncmp(url, "ldap", 4) == 0) {
79                 ret = ildb_connect(ldb, url, flags, options);
80         }
81 #elif HAVE_LDAP
82         else if (strncmp(url, "ldap", 4) == 0) {
83                 ret = lldb_connect(ldb, url, flags, options);
84         }
85 #endif
86 #if HAVE_SQLITE3
87         else if (strncmp(url, "sqlite:", 7) == 0) {
88                 ret = lsqlite3_connect(ldb, url, flags, options);
89         }
90 #endif
91         else {
92                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
93                 return LDB_ERR_OTHER;
94         }
95
96         if (ret != LDB_SUCCESS) {
97                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
98                 return ret;
99         }
100
101         if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
102                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'\n", url);
103                 return LDB_ERR_OTHER;
104         }
105
106         return LDB_SUCCESS;
107 }
108
109 static void ldb_reset_err_string(struct ldb_context *ldb)
110 {
111         if (ldb->err_string) {
112                 talloc_free(ldb->err_string);
113                 ldb->err_string = NULL;
114         }
115 }
116
117 #define FIRST_OP(ldb, op) do { \
118         module = ldb->modules; \
119         while (module && module->ops->op == NULL) module = module->next; \
120         if (module == NULL) return -1; \
121 } while (0)
122
123 /*
124   start a transaction
125 */
126 int ldb_transaction_start(struct ldb_context *ldb)
127 {
128         struct ldb_module *module;
129         FIRST_OP(ldb, start_transaction);
130         
131         ldb->transaction_active++;
132
133         ldb_reset_err_string(ldb);
134
135         return module->ops->start_transaction(module);
136 }
137
138 /*
139   commit a transaction
140 */
141 int ldb_transaction_commit(struct ldb_context *ldb)
142 {
143         struct ldb_module *module;
144         FIRST_OP(ldb, end_transaction);
145
146         if (ldb->transaction_active > 0) {
147                 ldb->transaction_active--;
148         } else {
149                 return LDB_ERR_OPERATIONS_ERROR;
150         }
151
152         ldb_reset_err_string(ldb);
153
154         return module->ops->end_transaction(module);
155 }
156
157 /*
158   cancel a transaction
159 */
160 int ldb_transaction_cancel(struct ldb_context *ldb)
161 {
162         struct ldb_module *module;
163         FIRST_OP(ldb, del_transaction);
164
165         if (ldb->transaction_active > 0) {
166                 ldb->transaction_active--;
167         } else {
168                 return LDB_ERR_OPERATIONS_ERROR;
169         }
170
171         return module->ops->del_transaction(module);
172 }
173
174 /*
175   check for an error return from an op 
176   if an op fails, but has not setup an error string, then setup one now
177 */
178 static int ldb_op_finish(struct ldb_context *ldb, int status)
179 {
180         if (status == LDB_SUCCESS) {
181                 return ldb_transaction_commit(ldb);
182         }
183         if (ldb->err_string == NULL) {
184                 /* no error string was setup by the backend */
185                 ldb_set_errstring(ldb->modules, 
186                                   talloc_asprintf(ldb, "ldb error %d", status));
187         }
188         ldb_transaction_cancel(ldb);
189         return status;
190 }
191
192 /*
193   start an ldb request
194   autostarts a transacion if none active and the operation is not a search
195   returns LDB_ERR_* on errors.
196 */
197 int ldb_request(struct ldb_context *ldb, struct ldb_request *request)
198 {
199         int status, started_transaction=0;
200         struct ldb_request *r;
201
202         ldb_reset_err_string(ldb);
203
204         /* to allow ldb modules to assume they can use the request ptr
205            as a talloc context for the request, we have to copy the 
206            structure here */
207         r = talloc(ldb, struct ldb_request);
208         if (r == NULL) {
209                 ldb_oom(ldb);
210                 return LDB_ERR_OPERATIONS_ERROR;
211         }
212
213         *r = *request;
214
215         if (r->operation == LDB_REQ_SEARCH) {
216                 r->op.search.res = NULL;
217         }
218
219         /* start a transaction if needed */
220         if ((!ldb->transaction_active) &&
221             (request->operation == LDB_REQ_ADD ||
222              request->operation == LDB_REQ_MODIFY ||
223              request->operation == LDB_REQ_DELETE ||
224              request->operation == LDB_REQ_RENAME)) {
225                 status = ldb_transaction_start(ldb);
226                 if (status != LDB_SUCCESS) {
227                         talloc_free(r);
228                         return status;
229                 }
230                 started_transaction = 1;
231         }
232
233         /* call the first module in the chain */
234         status = ldb->modules->ops->request(ldb->modules, r);
235
236         /* the search call is the only one that returns something
237            other than a status code. We steal the results into
238            the context of the ldb before freeing the request */
239         if (request->operation == LDB_REQ_SEARCH) {
240                 request->op.search.res = talloc_steal(ldb, r->op.search.res);
241         }
242         talloc_free(r);
243
244         if (started_transaction) {
245                 return ldb_op_finish(ldb, status);
246         }
247
248         return status;
249 }
250
251 /*
252   search the database given a LDAP-like search expression
253
254   return the number of records found, or -1 on error
255
256   Use talloc_free to free the ldb_message returned in 'res'
257
258 */
259 int ldb_search(struct ldb_context *ldb, 
260                const struct ldb_dn *base,
261                enum ldb_scope scope,
262                const char *expression,
263                const char * const *attrs, 
264                struct ldb_result **res)
265 {
266         struct ldb_request request;
267         struct ldb_parse_tree *tree;
268         int ret;
269
270         (*res) = NULL;
271
272         tree = ldb_parse_tree(ldb, expression);
273         if (tree == NULL) {
274                 ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Unable to parse search expression"));
275                 return -1;
276         }
277
278         request.operation = LDB_REQ_SEARCH;
279         request.op.search.base = base;
280         request.op.search.scope = scope;
281         request.op.search.tree = tree;
282         request.op.search.attrs = attrs;
283
284         ret = ldb_request(ldb, &request);
285
286         (*res) = request.op.search.res;
287
288         talloc_free(tree);
289
290         return ret;
291 }
292
293 /*
294   add a record to the database. Will fail if a record with the given class and key
295   already exists
296 */
297 int ldb_add(struct ldb_context *ldb, 
298             const struct ldb_message *message)
299 {
300         struct ldb_request request;
301         int status;
302
303         status = ldb_msg_sanity_check(message);
304         if (status != LDB_SUCCESS) return status;
305
306         request.operation = LDB_REQ_ADD;
307         request.op.add.message = message;
308
309         return ldb_request(ldb, &request);
310 }
311
312 /*
313   modify the specified attributes of a record
314 */
315 int ldb_modify(struct ldb_context *ldb, 
316                const struct ldb_message *message)
317 {
318         struct ldb_request request;
319         int status;
320
321         status = ldb_msg_sanity_check(message);
322         if (status != LDB_SUCCESS) return status;
323
324         request.operation = LDB_REQ_MODIFY;
325         request.op.mod.message = message;
326
327         return ldb_request(ldb, &request);
328 }
329
330
331 /*
332   delete a record from the database
333 */
334 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
335 {
336         struct ldb_request request;
337
338         request.operation = LDB_REQ_DELETE;
339         request.op.del.dn = dn;
340
341         return ldb_request(ldb, &request);
342 }
343
344 /*
345   rename a record in the database
346 */
347 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
348 {
349         struct ldb_request request;
350
351         request.operation = LDB_REQ_RENAME;
352         request.op.rename.olddn = olddn;
353         request.op.rename.newdn = newdn;
354
355         return ldb_request(ldb, &request);
356 }
357
358
359
360 /*
361   return extended error information 
362 */
363 const char *ldb_errstring(struct ldb_context *ldb)
364 {
365         if (ldb->err_string) {
366                 return ldb->err_string;
367         }
368
369         return NULL;
370 }
371
372
373 /*
374   set backend specific opaque parameters
375 */
376 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
377 {
378         struct ldb_opaque *o;
379
380         /* allow updating an existing value */
381         for (o=ldb->opaque;o;o=o->next) {
382                 if (strcmp(o->name, name) == 0) {
383                         o->value = value;
384                         return LDB_SUCCESS;
385                 }
386         }
387
388         o = talloc(ldb, struct ldb_opaque);
389         if (o == NULL) {
390                 ldb_oom(ldb);
391                 return LDB_ERR_OTHER;
392         }
393         o->next = ldb->opaque;
394         o->name = name;
395         o->value = value;
396         ldb->opaque = o;
397         return LDB_SUCCESS;
398 }
399
400 /*
401   get a previously set opaque value
402 */
403 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
404 {
405         struct ldb_opaque *o;
406         for (o=ldb->opaque;o;o=o->next) {
407                 if (strcmp(o->name, name) == 0) {
408                         return o->value;
409                 }
410         }
411         return NULL;
412 }