r18830: ensure backends aren't added twice (needed for samba3)
[jelmer/samba4-debian.git] / source / 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) return ret;
823
824         ret = ldb_build_mod_req(&req, ldb, ldb,
825                                         message,
826                                         NULL,
827                                         NULL,
828                                         NULL);
829
830         if (ret != LDB_SUCCESS) return ret;
831
832         ldb_set_timeout(ldb, req, 0); /* use default timeout */
833
834         /* do request and autostart a transaction */
835         ret = ldb_autotransaction_request(ldb, req);
836
837         talloc_free(req);
838         return ret;
839 }
840
841
842 /*
843   delete a record from the database
844 */
845 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
846 {
847         struct ldb_request *req;
848         int ret;
849
850         ret = ldb_build_del_req(&req, ldb, ldb,
851                                         dn,
852                                         NULL,
853                                         NULL,
854                                         NULL);
855
856         if (ret != LDB_SUCCESS) return ret;
857
858         ldb_set_timeout(ldb, req, 0); /* use default timeout */
859
860         /* do request and autostart a transaction */
861         ret = ldb_autotransaction_request(ldb, req);
862
863         talloc_free(req);
864         return ret;
865 }
866
867 /*
868   rename a record in the database
869 */
870 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
871 {
872         struct ldb_request *req;
873         int ret;
874
875         ret = ldb_build_rename_req(&req, ldb, ldb,
876                                         olddn,
877                                         newdn,
878                                         NULL,
879                                         NULL,
880                                         NULL);
881
882         if (ret != LDB_SUCCESS) return ret;
883
884         ldb_set_timeout(ldb, req, 0); /* use default timeout */
885
886         /* do request and autostart a transaction */
887         ret = ldb_autotransaction_request(ldb, req);
888
889         talloc_free(req);
890         return ret;
891 }
892
893
894 /*
895   return the global sequence number
896 */
897 int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num)
898 {
899         struct ldb_request *req;
900         int ret;
901
902         req = talloc(ldb, struct ldb_request);
903         if (req == NULL) {
904                 ldb_set_errstring(ldb, "Out of Memory");
905                 return LDB_ERR_OPERATIONS_ERROR;
906         }
907
908         req->operation = LDB_SEQUENCE_NUMBER;
909         req->controls = NULL;
910         req->context = NULL;
911         req->callback = NULL;
912         ldb_set_timeout(ldb, req, 0); /* use default timeout */
913
914         req->op.seq_num.type = type;
915         /* do request and autostart a transaction */
916         ret = ldb_request(ldb, req);
917         
918         if (ret == LDB_SUCCESS) {
919                 *seq_num = req->op.seq_num.seq_num;
920         }
921
922         talloc_free(req);
923         return ret;
924 }
925
926
927
928 /*
929   return extended error information 
930 */
931 const char *ldb_errstring(struct ldb_context *ldb)
932 {
933         if (ldb->err_string) {
934                 return ldb->err_string;
935         }
936
937         return NULL;
938 }
939
940 /*
941   return a string explaining what a ldb error constant meancs
942 */
943 const char *ldb_strerror(int ldb_err)
944 {
945         switch (ldb_err) {
946         case LDB_SUCCESS:
947                 return "Success";
948         case LDB_ERR_OPERATIONS_ERROR:
949                 return "Operations error";
950         case LDB_ERR_PROTOCOL_ERROR:
951                 return "Protocol error";
952         case LDB_ERR_TIME_LIMIT_EXCEEDED:
953                 return "Time limit exceeded";
954         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
955                 return "Size limit exceeded";
956         case LDB_ERR_COMPARE_FALSE:
957                 return "Compare false";
958         case LDB_ERR_COMPARE_TRUE:
959                 return "Compare true";
960         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
961                 return "Auth method not supported";
962         case LDB_ERR_STRONG_AUTH_REQUIRED:
963                 return "Strong auth required";
964 /* 9 RESERVED */
965         case LDB_ERR_REFERRAL:
966                 return "Referral error";
967         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
968                 return "Admin limit exceeded";
969         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
970                 return "Unsupported critical extension";
971         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
972                 return "Confidentiality required";
973         case LDB_ERR_SASL_BIND_IN_PROGRESS:
974                 return "SASL bind in progress";
975         case LDB_ERR_NO_SUCH_ATTRIBUTE:
976                 return "No such attribute";
977         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
978                 return "Undefined attribute type";
979         case LDB_ERR_INAPPROPRIATE_MATCHING:
980                 return "Inappropriate matching";
981         case LDB_ERR_CONSTRAINT_VIOLATION:
982                 return "Constraint violation";
983         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
984                 return "Attribute or value exists";
985         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
986                 return "Invalid attribute syntax";
987 /* 22-31 unused */
988         case LDB_ERR_NO_SUCH_OBJECT:
989                 return "No such object";
990         case LDB_ERR_ALIAS_PROBLEM:
991                 return "Alias problem";
992         case LDB_ERR_INVALID_DN_SYNTAX:
993                 return "Invalid DN syntax";
994 /* 35 RESERVED */
995         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
996                 return "Alias dereferencing problem";
997 /* 37-47 unused */
998         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
999                 return "Inappropriate authentication";
1000         case LDB_ERR_INVALID_CREDENTIALS:
1001                 return "Invalid credentials";
1002         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1003                 return "insufficient access rights";
1004         case LDB_ERR_BUSY:
1005                 return "Busy";
1006         case LDB_ERR_UNAVAILABLE:
1007                 return "Unavailable";
1008         case LDB_ERR_UNWILLING_TO_PERFORM:
1009                 return "Unwilling to perform";
1010         case LDB_ERR_LOOP_DETECT:
1011                 return "Loop detect";
1012 /* 55-63 unused */
1013         case LDB_ERR_NAMING_VIOLATION:
1014                 return "Naming violation";
1015         case LDB_ERR_OBJECT_CLASS_VIOLATION:
1016                 return "Object class violation";
1017         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1018                 return "Not allowed on non-leaf";
1019         case LDB_ERR_NOT_ALLOWED_ON_RDN:
1020                 return "Not allowed on RDN";
1021         case LDB_ERR_ENTRY_ALREADY_EXISTS:
1022                 return "Entry already exists";
1023         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1024                 return "Object class mods prohibited";
1025 /* 70 RESERVED FOR CLDAP */
1026         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1027                 return "Affects multiple DSAs";
1028 /* 72-79 unused */
1029         case LDB_ERR_OTHER:
1030                 return "Other";
1031         }
1032
1033         return "Unknown error";
1034 }
1035
1036 /*
1037   set backend specific opaque parameters
1038 */
1039 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1040 {
1041         struct ldb_opaque *o;
1042
1043         /* allow updating an existing value */
1044         for (o=ldb->opaque;o;o=o->next) {
1045                 if (strcmp(o->name, name) == 0) {
1046                         o->value = value;
1047                         return LDB_SUCCESS;
1048                 }
1049         }
1050
1051         o = talloc(ldb, struct ldb_opaque);
1052         if (o == NULL) {
1053                 ldb_oom(ldb);
1054                 return LDB_ERR_OTHER;
1055         }
1056         o->next = ldb->opaque;
1057         o->name = name;
1058         o->value = value;
1059         ldb->opaque = o;
1060         return LDB_SUCCESS;
1061 }
1062
1063 /*
1064   get a previously set opaque value
1065 */
1066 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1067 {
1068         struct ldb_opaque *o;
1069         for (o=ldb->opaque;o;o=o->next) {
1070                 if (strcmp(o->name, name) == 0) {
1071                         return o->value;
1072                 }
1073         }
1074         return NULL;
1075 }