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