r19831: Big ldb_dn optimization and interfaces enhancement patch
[ira/wip.git] / source4 / lib / ldb / common / ldb.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5    Copyright (C) Simo Sorce  2005-2006
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb core API
30  *
31  *  Description: core API routines interfacing to ldb backends
32  *
33  *  Author: Andrew Tridgell
34  */
35
36 #include "includes.h"
37 #include "ldb/include/includes.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         ldb_set_utf8_default(ldb);
55         ldb_set_create_perms(ldb, 0666);
56
57         return ldb;
58 }
59
60 static struct ldb_backend {
61         const char *name;
62         ldb_connect_fn connect_fn;
63         struct ldb_backend *prev, *next;
64 } *ldb_backends = NULL;
65
66
67 static ldb_connect_fn ldb_find_backend(const char *url)
68 {
69         struct ldb_backend *backend;
70
71         for (backend = ldb_backends; backend; backend = backend->next) {
72                 if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
73                         return backend->connect_fn;
74                 }
75         }
76
77         return NULL;
78 }
79
80 /*
81  register a new ldb backend
82 */
83 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
84 {
85         struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
86
87         if (ldb_find_backend(url_prefix)) {
88                 return LDB_SUCCESS;
89         }
90
91         /* Maybe check for duplicity here later on? */
92
93         backend->name = talloc_strdup(backend, url_prefix);
94         backend->connect_fn = connectfn;
95         DLIST_ADD(ldb_backends, backend);
96
97         return LDB_SUCCESS;
98 }
99
100 /* 
101    Return the ldb module form of a database. The URL can either be one of the following forms
102    ldb://path
103    ldapi://path
104
105    flags is made up of LDB_FLG_*
106
107    the options are passed uninterpreted to the backend, and are
108    backend specific.
109
110   This allows modules to get at only the backend module, for example where a module 
111   may wish to direct certain requests at a particular backend.
112 */
113 int ldb_connect_backend(struct ldb_context *ldb, const char *url, const char *options[],
114                         struct ldb_module **backend_module)
115 {
116         int ret;
117         char *backend;
118         ldb_connect_fn fn;
119
120         if (strchr(url, ':') != NULL) {
121                 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
122         } else {
123                 /* Default to tdb */
124                 backend = talloc_strdup(ldb, "tdb");
125         }
126
127         fn = ldb_find_backend(backend);
128
129         if (fn == NULL) {
130                 if (ldb_try_load_dso(ldb, backend) == 0) {
131                         fn = ldb_find_backend(backend);
132                 }
133         }
134
135         talloc_free(backend);
136
137         if (fn == NULL) {
138                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
139                 return LDB_ERR_OTHER;
140         }
141
142         ret = fn(ldb, url, ldb->flags, options, backend_module);
143
144         if (ret != LDB_SUCCESS) {
145                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
146                 return ret;
147         }
148         return ret;
149 }
150
151 /*
152   try to autodetect a basedn if none specified. This fixes one of my
153   pet hates about ldapsearch, which is that you have to get a long,
154   complex basedn right to make any use of it.
155 */
156 static struct ldb_dn *ldb_set_default_basedn(struct ldb_context *ldb)
157 {
158         TALLOC_CTX *tmp_ctx;
159         int ret;
160         static const char *attrs[] = { "defaultNamingContext", NULL };
161         struct ldb_result *res;
162         struct ldb_dn *basedn=NULL;
163
164         basedn = (struct ldb_dn *)ldb_get_opaque(ldb, "default_baseDN");
165         if (basedn) {
166                 return basedn;
167         }
168
169         tmp_ctx = talloc_new(ldb);
170         ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, NULL), LDB_SCOPE_BASE, 
171                          "(objectClass=*)", attrs, &res);
172         if (ret == LDB_SUCCESS) {
173                 if (res->count == 1) {
174                         basedn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0], "defaultNamingContext");
175                         ldb_set_opaque(ldb, "default_baseDN", basedn);
176                 }
177                 talloc_free(res);
178         }
179
180         talloc_free(tmp_ctx);
181         return basedn;
182 }
183
184 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
185 {
186         return (struct ldb_dn *)ldb_get_opaque(ldb, "default_baseDN");
187 }
188
189 /* 
190  connect to a database. The URL can either be one of the following forms
191    ldb://path
192    ldapi://path
193
194    flags is made up of LDB_FLG_*
195
196    the options are passed uninterpreted to the backend, and are
197    backend specific
198 */
199 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
200 {
201         int ret;
202
203         ldb->flags = flags;
204
205         ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
206         if (ret != LDB_SUCCESS) {
207                 return ret;
208         }
209
210         if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
211                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for %s: %s\n",
212                           url, ldb_errstring(ldb));
213                 return LDB_ERR_OTHER;
214         }
215
216         /* TODO: get timeout from options if available there */
217         ldb->default_timeout = 300; /* set default to 5 minutes */
218
219         /* set the default base dn */
220         ldb_set_default_basedn(ldb);
221
222         return LDB_SUCCESS;
223 }
224
225 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
226 {
227         if (ldb->err_string) {
228                 talloc_free(ldb->err_string);
229         }
230         ldb->err_string = talloc_strdup(ldb, err_string);
231 }
232
233 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
234 {
235         va_list ap;
236
237         if (ldb->err_string) {
238                 talloc_free(ldb->err_string);
239         }
240
241         va_start(ap, format);
242         ldb->err_string = talloc_vasprintf(ldb, format, ap);
243         va_end(ap);
244 }
245
246 void ldb_reset_err_string(struct ldb_context *ldb)
247 {
248         if (ldb->err_string) {
249                 talloc_free(ldb->err_string);
250                 ldb->err_string = NULL;
251         }
252 }
253
254 #define FIRST_OP(ldb, op) do { \
255         module = ldb->modules;                                  \
256         while (module && module->ops->op == NULL) module = module->next; \
257         if (module == NULL) {                                           \
258                 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
259                 return LDB_ERR_OPERATIONS_ERROR;                        \
260         } \
261 } while (0)
262
263 /*
264   start a transaction
265 */
266 static int ldb_transaction_start_internal(struct ldb_context *ldb)
267 {
268         struct ldb_module *module;
269         int status;
270         FIRST_OP(ldb, start_transaction);
271
272         ldb_reset_err_string(ldb);
273
274         status = module->ops->start_transaction(module);
275         if (status != LDB_SUCCESS) {
276                 if (ldb->err_string == NULL) {
277                         /* no error string was setup by the backend */
278                         ldb_asprintf_errstring(ldb,
279                                                 "ldb transaction start: %s (%d)", 
280                                                 ldb_strerror(status), 
281                                                 status);
282                 }
283         }
284         return status;
285 }
286
287 /*
288   commit a transaction
289 */
290 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
291 {
292         struct ldb_module *module;
293         int status;
294         FIRST_OP(ldb, end_transaction);
295
296         ldb_reset_err_string(ldb);
297
298         status = module->ops->end_transaction(module);
299         if (status != LDB_SUCCESS) {
300                 if (ldb->err_string == NULL) {
301                         /* no error string was setup by the backend */
302                         ldb_asprintf_errstring(ldb, 
303                                                 "ldb transaction commit: %s (%d)", 
304                                                 ldb_strerror(status), 
305                                                 status);
306                 }
307         }
308         return status;
309 }
310
311 /*
312   cancel a transaction
313 */
314 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
315 {
316         struct ldb_module *module;
317         int status;
318         FIRST_OP(ldb, del_transaction);
319
320         status = module->ops->del_transaction(module);
321         if (status != LDB_SUCCESS) {
322                 if (ldb->err_string == NULL) {
323                         /* no error string was setup by the backend */
324                         ldb_asprintf_errstring(ldb, 
325                                                 "ldb transaction cancel: %s (%d)", 
326                                                 ldb_strerror(status), 
327                                                 status);
328                 }
329         }
330         return status;
331 }
332
333 int ldb_transaction_start(struct ldb_context *ldb)
334 {
335         /* disable autotransactions */
336         ldb->transaction_active++;
337
338         return ldb_transaction_start_internal(ldb);
339 }
340
341 int ldb_transaction_commit(struct ldb_context *ldb)
342 {
343         /* renable autotransactions (when we reach 0) */
344         if (ldb->transaction_active > 0)
345                 ldb->transaction_active--;
346
347         return ldb_transaction_commit_internal(ldb);
348 }
349
350 int ldb_transaction_cancel(struct ldb_context *ldb)
351 {
352         /* renable autotransactions (when we reach 0) */
353         if (ldb->transaction_active > 0)
354                 ldb->transaction_active--;
355
356         return ldb_transaction_cancel_internal(ldb);
357 }
358
359 static int ldb_autotransaction_start(struct ldb_context *ldb)
360 {
361         /* explicit transaction active, ignore autotransaction request */
362         if (ldb->transaction_active)
363                 return LDB_SUCCESS;
364
365         return ldb_transaction_start_internal(ldb);
366 }
367
368 static int ldb_autotransaction_commit(struct ldb_context *ldb)
369 {
370         /* explicit transaction active, ignore autotransaction request */
371         if (ldb->transaction_active)
372                 return LDB_SUCCESS;
373
374         return ldb_transaction_commit_internal(ldb);
375 }
376
377 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
378 {
379         /* explicit transaction active, ignore autotransaction request */
380         if (ldb->transaction_active)
381                 return LDB_SUCCESS;
382
383         return ldb_transaction_cancel_internal(ldb);
384 }
385
386 /* autostarts a transacion if none active */
387 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
388 {
389         int ret;
390
391         ret = ldb_autotransaction_start(ldb);
392         if (ret != LDB_SUCCESS) {
393                 return ret;
394         }
395
396         ret = ldb_request(ldb, req);
397         if (ret == LDB_SUCCESS) {
398                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
399         }
400
401         if (ret == LDB_SUCCESS) {
402                 return ldb_autotransaction_commit(ldb);
403         }
404         ldb_autotransaction_cancel(ldb);
405
406         if (ldb->err_string == NULL) {
407                 /* no error string was setup by the backend */
408                 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
409         }
410
411         return ret;
412 }
413
414 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
415 {
416         if (!handle) {
417                 return LDB_SUCCESS;
418         }
419
420         return handle->module->ops->wait(handle, type);
421 }
422
423 /* set the specified timeout or, if timeout is 0 set the default timeout */
424 /* timeout == -1 means no timeout */
425 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
426 {
427         if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
428         
429         if (timeout != 0) {
430                 req->timeout = timeout;
431         } else {
432                 req->timeout = ldb->default_timeout;
433         }
434         req->starttime = time(NULL);
435
436         return LDB_SUCCESS;
437 }
438
439 /* calculates the new timeout based on the previous starttime and timeout */
440 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
441 {
442         time_t now;
443
444         if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
445
446         now = time(NULL);
447
448         if (oldreq == NULL)
449                 return ldb_set_timeout(ldb, newreq, 0);
450
451         if ((now - oldreq->starttime) > oldreq->timeout) {
452                 return LDB_ERR_TIME_LIMIT_EXCEEDED;
453         }
454         newreq->starttime = oldreq->starttime;
455         newreq->timeout = oldreq->timeout - (now - oldreq->starttime);
456
457         return LDB_SUCCESS;
458 }
459
460
461 /* 
462    set the permissions for new files to be passed to open() in
463    backends that use local files
464  */
465 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
466 {
467         ldb->create_perms = perms;
468 }
469
470 /*
471   start an ldb request
472   NOTE: the request must be a talloc context.
473   returns LDB_ERR_* on errors.
474 */
475 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
476 {
477         struct ldb_module *module;
478         int ret;
479
480         ldb_reset_err_string(ldb);
481
482         /* call the first module in the chain */
483         switch (req->operation) {
484         case LDB_SEARCH:
485                 FIRST_OP(ldb, search);
486                 ret = module->ops->search(module, req);
487                 break;
488         case LDB_ADD:
489                 FIRST_OP(ldb, add);
490                 ret = module->ops->add(module, req);
491                 break;
492         case LDB_MODIFY:
493                 FIRST_OP(ldb, modify);
494                 ret = module->ops->modify(module, req);
495                 break;
496         case LDB_DELETE:
497                 FIRST_OP(ldb, del);
498                 ret = module->ops->del(module, req);
499                 break;
500         case LDB_RENAME:
501                 FIRST_OP(ldb, rename);
502                 ret = module->ops->rename(module, req);
503                 break;
504         case LDB_SEQUENCE_NUMBER:
505                 FIRST_OP(ldb, sequence_number);
506                 ret = module->ops->sequence_number(module, req);
507                 break;
508         default:
509                 FIRST_OP(ldb, request);
510                 ret = module->ops->request(module, req);
511                 break;
512         }
513
514         return ret;
515 }
516
517 /*
518   search the database given a LDAP-like search expression
519
520   returns an LDB error code
521
522   Use talloc_free to free the ldb_message returned in 'res', if successful
523
524 */
525 int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
526 {
527         struct ldb_result *res;
528         int n;
529         
530         if (!context) {
531                 ldb_set_errstring(ldb, "NULL Context in callback");
532                 return LDB_ERR_OPERATIONS_ERROR;
533         }       
534
535         res = talloc_get_type(context, struct ldb_result);
536
537         if (!res || !ares) {
538                 ldb_set_errstring(ldb, "NULL res or ares in callback");
539                 goto error;
540         }
541
542         switch (ares->type) {
543         case LDB_REPLY_ENTRY:
544                 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
545                 if (! res->msgs) {
546                         goto error;
547                 }
548
549                 res->msgs[res->count + 1] = NULL;
550
551                 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
552                 res->count++;
553                 break;
554         case LDB_REPLY_REFERRAL:
555                 if (res->refs) {
556                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
557                 } else {
558                         n = 0;
559                 }
560
561                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
562                 if (! res->refs) {
563                         goto error;
564                 }
565
566                 res->refs[n] = talloc_move(res->refs, &ares->referral);
567                 res->refs[n + 1] = NULL;
568         case LDB_REPLY_EXTENDED:
569         case LDB_REPLY_DONE:
570                 /* TODO: we should really support controls on entries and referrals too! */
571                 res->controls = talloc_move(res, &ares->controls);
572                 break;          
573         }
574         talloc_free(ares);
575         return LDB_SUCCESS;
576
577 error:
578         talloc_free(ares);
579         return LDB_ERR_OPERATIONS_ERROR;
580 }
581
582 int ldb_build_search_req(struct ldb_request **ret_req,
583                         struct ldb_context *ldb,
584                         void *mem_ctx,
585                         struct ldb_dn *base,
586                         enum ldb_scope scope,
587                         const char *expression,
588                         const char * const *attrs,
589                         struct ldb_control **controls,
590                         void *context,
591                         ldb_request_callback_t callback)
592 {
593         struct ldb_request *req;
594
595         *ret_req = NULL;
596
597         req = talloc(mem_ctx, struct ldb_request);
598         if (req == NULL) {
599                 ldb_set_errstring(ldb, "Out of Memory");
600                 return LDB_ERR_OPERATIONS_ERROR;
601         }
602
603         req->operation = LDB_SEARCH;
604         if (base == NULL) {
605                 req->op.search.base = ldb_dn_new(req, ldb, NULL);
606         } else {
607                 req->op.search.base = base;
608         }
609         req->op.search.scope = scope;
610
611         req->op.search.tree = ldb_parse_tree(req, expression);
612         if (req->op.search.tree == NULL) {
613                 ldb_set_errstring(ldb, "Unable to parse search expression");
614                 talloc_free(req);
615                 return LDB_ERR_OPERATIONS_ERROR;
616         }
617
618         req->op.search.attrs = attrs;
619         req->controls = controls;
620         req->context = context;
621         req->callback = callback;
622
623         *ret_req = req;
624         return LDB_SUCCESS;
625 }
626
627 int ldb_build_add_req(struct ldb_request **ret_req,
628                         struct ldb_context *ldb,
629                         void *mem_ctx,
630                         const struct ldb_message *message,
631                         struct ldb_control **controls,
632                         void *context,
633                         ldb_request_callback_t callback)
634 {
635         struct ldb_request *req;
636
637         *ret_req = NULL;
638
639         req = talloc(mem_ctx, struct ldb_request);
640         if (req == NULL) {
641                 ldb_set_errstring(ldb, "Out of Memory");
642                 return LDB_ERR_OPERATIONS_ERROR;
643         }
644
645         req->operation = LDB_ADD;
646         req->op.add.message = message;
647         req->controls = controls;
648         req->context = context;
649         req->callback = callback;
650
651         *ret_req = req;
652
653         return LDB_SUCCESS;
654 }
655
656 int ldb_build_mod_req(struct ldb_request **ret_req,
657                         struct ldb_context *ldb,
658                         void *mem_ctx,
659                         const struct ldb_message *message,
660                         struct ldb_control **controls,
661                         void *context,
662                         ldb_request_callback_t callback)
663 {
664         struct ldb_request *req;
665
666         *ret_req = NULL;
667
668         req = talloc(mem_ctx, struct ldb_request);
669         if (req == NULL) {
670                 ldb_set_errstring(ldb, "Out of Memory");
671                 return LDB_ERR_OPERATIONS_ERROR;
672         }
673
674         req->operation = LDB_MODIFY;
675         req->op.mod.message = message;
676         req->controls = controls;
677         req->context = context;
678         req->callback = callback;
679
680         *ret_req = req;
681
682         return LDB_SUCCESS;
683 }
684
685 int ldb_build_del_req(struct ldb_request **ret_req,
686                         struct ldb_context *ldb,
687                         void *mem_ctx,
688                         struct ldb_dn *dn,
689                         struct ldb_control **controls,
690                         void *context,
691                         ldb_request_callback_t callback)
692 {
693         struct ldb_request *req;
694
695         *ret_req = NULL;
696
697         req = talloc(mem_ctx, struct ldb_request);
698         if (req == NULL) {
699                 ldb_set_errstring(ldb, "Out of Memory");
700                 return LDB_ERR_OPERATIONS_ERROR;
701         }
702
703         req->operation = LDB_DELETE;
704         req->op.del.dn = dn;
705         req->controls = controls;
706         req->context = context;
707         req->callback = callback;
708
709         *ret_req = req;
710
711         return LDB_SUCCESS;
712 }
713
714 int ldb_build_rename_req(struct ldb_request **ret_req,
715                         struct ldb_context *ldb,
716                         void *mem_ctx,
717                         struct ldb_dn *olddn,
718                         struct ldb_dn *newdn,
719                         struct ldb_control **controls,
720                         void *context,
721                         ldb_request_callback_t callback)
722 {
723         struct ldb_request *req;
724
725         *ret_req = NULL;
726
727         req = talloc(mem_ctx, struct ldb_request);
728         if (req == NULL) {
729                 ldb_set_errstring(ldb, "Out of Memory");
730                 return LDB_ERR_OPERATIONS_ERROR;
731         }
732
733         req->operation = LDB_RENAME;
734         req->op.rename.olddn = olddn;
735         req->op.rename.newdn = newdn;
736         req->controls = controls;
737         req->context = context;
738         req->callback = callback;
739
740         *ret_req = req;
741
742         return LDB_SUCCESS;
743 }
744
745 /*
746   note that ldb_search() will automatically replace a NULL 'base' value with the 
747   defaultNamingContext from the rootDSE if available.
748 */
749 int ldb_search(struct ldb_context *ldb, 
750                struct ldb_dn *base,
751                enum ldb_scope scope,
752                const char *expression,
753                const char * const *attrs, 
754                struct ldb_result **_res)
755 {
756         struct ldb_request *req;
757         int ret;
758         struct ldb_result *res;
759
760         *_res = NULL;
761
762         res = talloc_zero(ldb, struct ldb_result);
763         if (!res) {
764                 return LDB_ERR_OPERATIONS_ERROR;
765         }
766
767         ret = ldb_build_search_req(&req, ldb, ldb,
768                                         base?base:ldb_get_default_basedn(ldb),
769                                         scope,
770                                         expression,
771                                         attrs,
772                                         NULL,
773                                         res,
774                                         ldb_search_default_callback);
775
776         if (ret != LDB_SUCCESS) goto done;
777
778         ldb_set_timeout(ldb, req, 0); /* use default timeout */
779
780         ret = ldb_request(ldb, req);
781         
782         if (ret == LDB_SUCCESS) {
783                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
784         }
785
786         talloc_free(req);
787
788 done:
789         if (ret != LDB_SUCCESS) {
790                 talloc_free(res);
791         }
792
793         *_res = res;
794         return ret;
795 }
796
797 /*
798   add a record to the database. Will fail if a record with the given class and key
799   already exists
800 */
801 int ldb_add(struct ldb_context *ldb, 
802             const struct ldb_message *message)
803 {
804         struct ldb_request *req;
805         int ret;
806
807         ret = ldb_msg_sanity_check(ldb, message);
808         if (ret != LDB_SUCCESS) {
809                 return ret;
810         }
811
812         ret = ldb_build_add_req(&req, ldb, ldb,
813                                         message,
814                                         NULL,
815                                         NULL,
816                                         NULL);
817
818         if (ret != LDB_SUCCESS) return ret;
819
820         ldb_set_timeout(ldb, req, 0); /* use default timeout */
821
822         /* do request and autostart a transaction */
823         ret = ldb_autotransaction_request(ldb, req);
824
825         talloc_free(req);
826         return ret;
827 }
828
829 /*
830   modify the specified attributes of a record
831 */
832 int ldb_modify(struct ldb_context *ldb, 
833                const struct ldb_message *message)
834 {
835         struct ldb_request *req;
836         int ret;
837
838         ret = ldb_msg_sanity_check(ldb, message);
839         if (ret != LDB_SUCCESS) {
840                 return ret;
841         }
842
843         ret = ldb_build_mod_req(&req, ldb, ldb,
844                                         message,
845                                         NULL,
846                                         NULL,
847                                         NULL);
848
849         if (ret != LDB_SUCCESS) return ret;
850
851         ldb_set_timeout(ldb, req, 0); /* use default timeout */
852
853         /* do request and autostart a transaction */
854         ret = ldb_autotransaction_request(ldb, req);
855
856         talloc_free(req);
857         return ret;
858 }
859
860
861 /*
862   delete a record from the database
863 */
864 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
865 {
866         struct ldb_request *req;
867         int ret;
868
869         ret = ldb_build_del_req(&req, ldb, ldb,
870                                         dn,
871                                         NULL,
872                                         NULL,
873                                         NULL);
874
875         if (ret != LDB_SUCCESS) return ret;
876
877         ldb_set_timeout(ldb, req, 0); /* use default timeout */
878
879         /* do request and autostart a transaction */
880         ret = ldb_autotransaction_request(ldb, req);
881
882         talloc_free(req);
883         return ret;
884 }
885
886 /*
887   rename a record in the database
888 */
889 int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn)
890 {
891         struct ldb_request *req;
892         int ret;
893
894         ret = ldb_build_rename_req(&req, ldb, ldb,
895                                         olddn,
896                                         newdn,
897                                         NULL,
898                                         NULL,
899                                         NULL);
900
901         if (ret != LDB_SUCCESS) return ret;
902
903         ldb_set_timeout(ldb, req, 0); /* use default timeout */
904
905         /* do request and autostart a transaction */
906         ret = ldb_autotransaction_request(ldb, req);
907
908         talloc_free(req);
909         return ret;
910 }
911
912
913 /*
914   return the global sequence number
915 */
916 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
917 {
918         struct ldb_request *req;
919         int ret;
920
921         req = talloc(ldb, struct ldb_request);
922         if (req == NULL) {
923                 ldb_set_errstring(ldb, "Out of Memory");
924                 return LDB_ERR_OPERATIONS_ERROR;
925         }
926
927         req->operation = LDB_SEQUENCE_NUMBER;
928         req->controls = NULL;
929         req->context = NULL;
930         req->callback = NULL;
931         ldb_set_timeout(ldb, req, 0); /* use default timeout */
932
933         req->op.seq_num.type = type;
934         /* do request and autostart a transaction */
935         ret = ldb_request(ldb, req);
936         
937         if (ret == LDB_SUCCESS) {
938                 *seq_num = req->op.seq_num.seq_num;
939         }
940
941         talloc_free(req);
942         return ret;
943 }
944
945
946
947 /*
948   return extended error information 
949 */
950 const char *ldb_errstring(struct ldb_context *ldb)
951 {
952         if (ldb->err_string) {
953                 return ldb->err_string;
954         }
955
956         return NULL;
957 }
958
959 /*
960   return a string explaining what a ldb error constant meancs
961 */
962 const char *ldb_strerror(int ldb_err)
963 {
964         switch (ldb_err) {
965         case LDB_SUCCESS:
966                 return "Success";
967         case LDB_ERR_OPERATIONS_ERROR:
968                 return "Operations error";
969         case LDB_ERR_PROTOCOL_ERROR:
970                 return "Protocol error";
971         case LDB_ERR_TIME_LIMIT_EXCEEDED:
972                 return "Time limit exceeded";
973         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
974                 return "Size limit exceeded";
975         case LDB_ERR_COMPARE_FALSE:
976                 return "Compare false";
977         case LDB_ERR_COMPARE_TRUE:
978                 return "Compare true";
979         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
980                 return "Auth method not supported";
981         case LDB_ERR_STRONG_AUTH_REQUIRED:
982                 return "Strong auth required";
983 /* 9 RESERVED */
984         case LDB_ERR_REFERRAL:
985                 return "Referral error";
986         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
987                 return "Admin limit exceeded";
988         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
989                 return "Unsupported critical extension";
990         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
991                 return "Confidentiality required";
992         case LDB_ERR_SASL_BIND_IN_PROGRESS:
993                 return "SASL bind in progress";
994         case LDB_ERR_NO_SUCH_ATTRIBUTE:
995                 return "No such attribute";
996         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
997                 return "Undefined attribute type";
998         case LDB_ERR_INAPPROPRIATE_MATCHING:
999                 return "Inappropriate matching";
1000         case LDB_ERR_CONSTRAINT_VIOLATION:
1001                 return "Constraint violation";
1002         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1003                 return "Attribute or value exists";
1004         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1005                 return "Invalid attribute syntax";
1006 /* 22-31 unused */
1007         case LDB_ERR_NO_SUCH_OBJECT:
1008                 return "No such object";
1009         case LDB_ERR_ALIAS_PROBLEM:
1010                 return "Alias problem";
1011         case LDB_ERR_INVALID_DN_SYNTAX:
1012                 return "Invalid DN syntax";
1013 /* 35 RESERVED */
1014         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1015                 return "Alias dereferencing problem";
1016 /* 37-47 unused */
1017         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1018                 return "Inappropriate authentication";
1019         case LDB_ERR_INVALID_CREDENTIALS:
1020                 return "Invalid credentials";
1021         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1022                 return "insufficient access rights";
1023         case LDB_ERR_BUSY:
1024                 return "Busy";
1025         case LDB_ERR_UNAVAILABLE:
1026                 return "Unavailable";
1027         case LDB_ERR_UNWILLING_TO_PERFORM:
1028                 return "Unwilling to perform";
1029         case LDB_ERR_LOOP_DETECT:
1030                 return "Loop detect";
1031 /* 55-63 unused */
1032         case LDB_ERR_NAMING_VIOLATION:
1033                 return "Naming violation";
1034         case LDB_ERR_OBJECT_CLASS_VIOLATION:
1035                 return "Object class violation";
1036         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1037                 return "Not allowed on non-leaf";
1038         case LDB_ERR_NOT_ALLOWED_ON_RDN:
1039                 return "Not allowed on RDN";
1040         case LDB_ERR_ENTRY_ALREADY_EXISTS:
1041                 return "Entry already exists";
1042         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1043                 return "Object class mods prohibited";
1044 /* 70 RESERVED FOR CLDAP */
1045         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1046                 return "Affects multiple DSAs";
1047 /* 72-79 unused */
1048         case LDB_ERR_OTHER:
1049                 return "Other";
1050         }
1051
1052         return "Unknown error";
1053 }
1054
1055 /*
1056   set backend specific opaque parameters
1057 */
1058 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1059 {
1060         struct ldb_opaque *o;
1061
1062         /* allow updating an existing value */
1063         for (o=ldb->opaque;o;o=o->next) {
1064                 if (strcmp(o->name, name) == 0) {
1065                         o->value = value;
1066                         return LDB_SUCCESS;
1067                 }
1068         }
1069
1070         o = talloc(ldb, struct ldb_opaque);
1071         if (o == NULL) {
1072                 ldb_oom(ldb);
1073                 return LDB_ERR_OTHER;
1074         }
1075         o->next = ldb->opaque;
1076         o->name = name;
1077         o->value = value;
1078         ldb->opaque = o;
1079         return LDB_SUCCESS;
1080 }
1081
1082 /*
1083   get a previously set opaque value
1084 */
1085 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1086 {
1087         struct ldb_opaque *o;
1088         for (o=ldb->opaque;o;o=o->next) {
1089                 if (strcmp(o->name, name) == 0) {
1090                         return o->value;
1091                 }
1092         }
1093         return NULL;
1094 }