s4-ldb: add a LDB_FLG_ENABLE_TRACING for full ldb tracing
[kamenim/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-2008
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 3 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, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb core API
29  *
30  *  Description: core API routines interfacing to ldb backends
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #define TEVENT_DEPRECATED 1
36 #include "ldb_private.h"
37
38 static int ldb_context_destructor(void *ptr)
39 {
40         struct ldb_context *ldb = talloc_get_type(ptr, struct ldb_context);
41
42         if (ldb->transaction_active) {
43                 ldb_debug(ldb, LDB_DEBUG_FATAL,
44                           "A transaction is still active in ldb context [%p] on %s",
45                           ldb, (const char *)ldb_get_opaque(ldb, "ldb_url"));
46         }
47
48         return 0;
49 }
50
51 /*
52   this is used to catch debug messages from events
53 */
54 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
55                              const char *fmt, va_list ap)  PRINTF_ATTRIBUTE(3,0);
56
57 static void ldb_tevent_debug(void *context, enum tevent_debug_level level,
58                              const char *fmt, va_list ap)
59 {
60         struct ldb_context *ldb = talloc_get_type(context, struct ldb_context);
61         enum ldb_debug_level ldb_level = LDB_DEBUG_FATAL;
62         char *s = NULL;
63
64         switch (level) {
65         case TEVENT_DEBUG_FATAL:
66                 ldb_level = LDB_DEBUG_FATAL;
67                 break;
68         case TEVENT_DEBUG_ERROR:
69                 ldb_level = LDB_DEBUG_ERROR;
70                 break;
71         case TEVENT_DEBUG_WARNING:
72                 ldb_level = LDB_DEBUG_WARNING;
73                 break;
74         case TEVENT_DEBUG_TRACE:
75                 ldb_level = LDB_DEBUG_TRACE;
76                 break;
77         };
78
79         vasprintf(&s, fmt, ap);
80         if (!s) return;
81         ldb_debug(ldb, ldb_level, "tevent: %s", s);
82         free(s);
83 }
84
85 /*
86    initialise a ldb context
87    The mem_ctx is required
88    The event_ctx is required
89 */
90 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx)
91 {
92         struct ldb_context *ldb;
93         int ret;
94
95         ldb = talloc_zero(mem_ctx, struct ldb_context);
96         /* FIXME: Hack a new event context so that CMD line utilities work
97          * until we have them all converted */
98         if (ev_ctx == NULL) {
99                 ev_ctx = tevent_context_init(talloc_autofree_context());
100                 tevent_set_debug(ev_ctx, ldb_tevent_debug, ldb);
101                 tevent_loop_allow_nesting(ev_ctx);
102         }
103
104         ret = ldb_setup_wellknown_attributes(ldb);
105         if (ret != 0) {
106                 talloc_free(ldb);
107                 return NULL;
108         }
109
110         ldb_set_utf8_default(ldb);
111         ldb_set_create_perms(ldb, 0666);
112         ldb_set_modules_dir(ldb, LDB_MODULESDIR);
113         ldb_set_event_context(ldb, ev_ctx);
114
115         /* TODO: get timeout from options if available there */
116         ldb->default_timeout = 300; /* set default to 5 minutes */
117
118         talloc_set_destructor((TALLOC_CTX *)ldb, ldb_context_destructor);
119
120         return ldb;
121 }
122
123 /*
124   try to autodetect a basedn if none specified. This fixes one of my
125   pet hates about ldapsearch, which is that you have to get a long,
126   complex basedn right to make any use of it.
127 */
128 void ldb_set_default_dns(struct ldb_context *ldb)
129 {
130         TALLOC_CTX *tmp_ctx;
131         int ret;
132         struct ldb_result *res;
133         struct ldb_dn *tmp_dn=NULL;
134         static const char *attrs[] = {
135                 "rootDomainNamingContext",
136                 "configurationNamingContext",
137                 "schemaNamingContext",
138                 "defaultNamingContext",
139                 NULL
140         };
141
142         tmp_ctx = talloc_new(ldb);
143         ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
144                          LDB_SCOPE_BASE, attrs, "(objectClass=*)");
145         if (ret != LDB_SUCCESS) {
146                 talloc_free(tmp_ctx);
147                 return;
148         }
149
150         if (res->count != 1) {
151                 talloc_free(tmp_ctx);
152                 return;
153         }
154
155         if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
156                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
157                                                  "rootDomainNamingContext");
158                 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
159         }
160
161         if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
162                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
163                                                  "configurationNamingContext");
164                 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
165         }
166
167         if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
168                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
169                                                  "schemaNamingContext");
170                 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
171         }
172
173         if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
174                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
175                                                  "defaultNamingContext");
176                 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
177         }
178
179         talloc_free(tmp_ctx);
180 }
181
182 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
183 {
184         void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
185         return talloc_get_type(opaque, struct ldb_dn);
186 }
187
188 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
189 {
190         void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
191         return talloc_get_type(opaque, struct ldb_dn);
192 }
193
194 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
195 {
196         void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
197         return talloc_get_type(opaque, struct ldb_dn);
198 }
199
200 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
201 {
202         void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
203         return talloc_get_type(opaque, struct ldb_dn);
204 }
205
206 /*
207    connect to a database. The URL can either be one of the following forms
208    ldb://path
209    ldapi://path
210
211    flags is made up of LDB_FLG_*
212
213    the options are passed uninterpreted to the backend, and are
214    backend specific
215 */
216 int ldb_connect(struct ldb_context *ldb, const char *url,
217                 unsigned int flags, const char *options[])
218 {
219         int ret;
220         const char *url2;
221         /* We seem to need to do this here, or else some utilities don't
222          * get ldb backends */
223
224         ldb->flags = flags;
225
226         url2 = talloc_strdup(ldb, url);
227         if (!url2) {
228                 ldb_oom(ldb);
229                 return LDB_ERR_OPERATIONS_ERROR;
230         }
231         ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
232         if (ret != LDB_SUCCESS) {
233                 return ret;
234         }
235
236         ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
237         if (ret != LDB_SUCCESS) {
238                 return ret;
239         }
240
241         if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
242                 ldb_debug(ldb, LDB_DEBUG_FATAL,
243                           "Unable to load modules for %s: %s",
244                           url, ldb_errstring(ldb));
245                 return LDB_ERR_OTHER;
246         }
247
248         /* set the default base dn */
249         ldb_set_default_dns(ldb);
250
251         return LDB_SUCCESS;
252 }
253
254 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
255 {
256         if (ldb->err_string) {
257                 talloc_free(ldb->err_string);
258         }
259         ldb->err_string = talloc_strdup(ldb, err_string);
260 }
261
262 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
263 {
264         va_list ap;
265         char *old_string = NULL;
266
267         if (ldb->err_string) {
268                 old_string = ldb->err_string;
269         }
270
271         va_start(ap, format);
272         ldb->err_string = talloc_vasprintf(ldb, format, ap);
273         va_end(ap);
274         talloc_free(old_string);
275 }
276
277 void ldb_reset_err_string(struct ldb_context *ldb)
278 {
279         if (ldb->err_string) {
280                 talloc_free(ldb->err_string);
281                 ldb->err_string = NULL;
282         }
283 }
284
285 #define FIRST_OP_NOERR(ldb, op) do { \
286         module = ldb->modules;                                  \
287         while (module && module->ops->op == NULL) module = module->next; \
288 } while (0)
289
290 #define FIRST_OP(ldb, op) do { \
291         FIRST_OP_NOERR(ldb, op); \
292         if (module == NULL) {                                   \
293                 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
294                 return LDB_ERR_OPERATIONS_ERROR;                        \
295         } \
296 } while (0)
297
298
299 /*
300   start a transaction
301 */
302 int ldb_transaction_start(struct ldb_context *ldb)
303 {
304         struct ldb_module *module;
305         int status;
306
307         ldb_debug(ldb, LDB_DEBUG_TRACE,
308                   "start ldb transaction (nesting: %d)",
309                   ldb->transaction_active);
310
311         /* explicit transaction active, count nested requests */
312         if (ldb->transaction_active) {
313                 ldb->transaction_active++;
314                 return LDB_SUCCESS;
315         }
316
317         /* start a new transaction */
318         ldb->transaction_active++;
319         ldb->prepare_commit_done = false;
320
321         FIRST_OP(ldb, start_transaction);
322
323         ldb_reset_err_string(ldb);
324
325         status = module->ops->start_transaction(module);
326         if (status != LDB_SUCCESS) {
327                 if (ldb->err_string == NULL) {
328                         /* no error string was setup by the backend */
329                         ldb_asprintf_errstring(ldb,
330                                 "ldb transaction start: %s (%d)",
331                                 ldb_strerror(status),
332                                 status);
333                 }
334         }
335         return status;
336 }
337
338 /*
339   prepare for transaction commit (first phase of two phase commit)
340 */
341 int ldb_transaction_prepare_commit(struct ldb_context *ldb)
342 {
343         struct ldb_module *module;
344         int status;
345
346         if (ldb->prepare_commit_done) {
347                 return LDB_SUCCESS;
348         }
349
350         /* commit only when all nested transactions are complete */
351         if (ldb->transaction_active > 1) {
352                 return LDB_SUCCESS;
353         }
354
355         ldb->prepare_commit_done = true;
356
357         if (ldb->transaction_active < 0) {
358                 ldb_debug(ldb, LDB_DEBUG_FATAL,
359                           "prepare commit called but no ldb transactions are active!");
360                 ldb->transaction_active = 0;
361                 return LDB_ERR_OPERATIONS_ERROR;
362         }
363
364         /* call prepare transaction if available */
365         FIRST_OP_NOERR(ldb, prepare_commit);
366         if (module == NULL) {
367                 return LDB_SUCCESS;
368         }
369
370         status = module->ops->prepare_commit(module);
371         if (status != LDB_SUCCESS) {
372                 /* if a module fails the prepare then we need
373                    to call the end transaction for everyone */
374                 FIRST_OP(ldb, end_transaction);
375                 module->ops->end_transaction(module);
376                 if (ldb->err_string == NULL) {
377                         /* no error string was setup by the backend */
378                         ldb_asprintf_errstring(ldb,
379                                                "ldb transaction prepare commit: %s (%d)",
380                                                ldb_strerror(status),
381                                                status);
382                 }
383         }
384
385         return status;
386 }
387
388
389 /*
390   commit a transaction
391 */
392 int ldb_transaction_commit(struct ldb_context *ldb)
393 {
394         struct ldb_module *module;
395         int status;
396
397         status = ldb_transaction_prepare_commit(ldb);
398         if (status != LDB_SUCCESS) {
399                 return status;
400         }
401
402         ldb->transaction_active--;
403
404         ldb_debug(ldb, LDB_DEBUG_TRACE,
405                   "commit ldb transaction (nesting: %d)",
406                   ldb->transaction_active);
407
408         /* commit only when all nested transactions are complete */
409         if (ldb->transaction_active > 0) {
410                 return LDB_SUCCESS;
411         }
412
413         if (ldb->transaction_active < 0) {
414                 ldb_debug(ldb, LDB_DEBUG_FATAL,
415                           "commit called but no ldb transactions are active!");
416                 ldb->transaction_active = 0;
417                 return LDB_ERR_OPERATIONS_ERROR;
418         }
419
420         ldb_reset_err_string(ldb);
421
422         FIRST_OP(ldb, end_transaction);
423         status = module->ops->end_transaction(module);
424         if (status != LDB_SUCCESS) {
425                 if (ldb->err_string == NULL) {
426                         /* no error string was setup by the backend */
427                         ldb_asprintf_errstring(ldb,
428                                 "ldb transaction commit: %s (%d)",
429                                 ldb_strerror(status),
430                                 status);
431                 }
432                 /* cancel the transaction */
433                 FIRST_OP(ldb, del_transaction);
434                 module->ops->del_transaction(module);
435         }
436         return status;
437 }
438
439
440 /*
441   cancel a transaction
442 */
443 int ldb_transaction_cancel(struct ldb_context *ldb)
444 {
445         struct ldb_module *module;
446         int status;
447
448         ldb->transaction_active--;
449
450         ldb_debug(ldb, LDB_DEBUG_TRACE,
451                   "cancel ldb transaction (nesting: %d)",
452                   ldb->transaction_active);
453
454         /* really cancel only if all nested transactions are complete */
455         if (ldb->transaction_active > 0) {
456                 return LDB_SUCCESS;
457         }
458
459         if (ldb->transaction_active < 0) {
460                 ldb_debug(ldb, LDB_DEBUG_FATAL,
461                           "cancel called but no ldb transactions are active!");
462                 ldb->transaction_active = 0;
463                 return LDB_ERR_OPERATIONS_ERROR;
464         }
465
466         FIRST_OP(ldb, del_transaction);
467
468         status = module->ops->del_transaction(module);
469         if (status != LDB_SUCCESS) {
470                 if (ldb->err_string == NULL) {
471                         /* no error string was setup by the backend */
472                         ldb_asprintf_errstring(ldb,
473                                 "ldb transaction cancel: %s (%d)",
474                                 ldb_strerror(status),
475                                 status);
476                 }
477         }
478         return status;
479 }
480
481 /* autostarts a transacion if none active */
482 static int ldb_autotransaction_request(struct ldb_context *ldb,
483                                        struct ldb_request *req)
484 {
485         int ret;
486
487         ret = ldb_transaction_start(ldb);
488         if (ret != LDB_SUCCESS) {
489                 return ret;
490         }
491
492         ret = ldb_request(ldb, req);
493         if (ret == LDB_SUCCESS) {
494                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
495         }
496
497         if (ret == LDB_SUCCESS) {
498                 return ldb_transaction_commit(ldb);
499         }
500         ldb_transaction_cancel(ldb);
501
502         if (ldb->err_string == NULL) {
503                 /* no error string was setup by the backend */
504                 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
505         }
506
507         return ret;
508 }
509
510 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
511 {
512         struct tevent_context *ev;
513         int ret;
514
515         if (!handle) {
516                 return LDB_ERR_UNAVAILABLE;
517         }
518
519         if (handle->state == LDB_ASYNC_DONE) {
520                 return handle->status;
521         }
522
523         ev = ldb_get_event_context(handle->ldb);
524         if (NULL == ev) {
525                 return LDB_ERR_OPERATIONS_ERROR;
526         }
527
528         switch (type) {
529         case LDB_WAIT_NONE:
530                 ret = tevent_loop_once(ev);
531                 if (ret != 0) {
532                         return LDB_ERR_OPERATIONS_ERROR;
533                 }
534                 if (handle->state == LDB_ASYNC_DONE ||
535                     handle->status != LDB_SUCCESS) {
536                         return handle->status;
537                 }
538                 break;
539
540         case LDB_WAIT_ALL:
541                 while (handle->state != LDB_ASYNC_DONE) {
542                         ret = tevent_loop_once(ev);
543                         if (ret != 0) {
544                                 return LDB_ERR_OPERATIONS_ERROR;
545                         }
546                         if (handle->status != LDB_SUCCESS) {
547                                 return handle->status;
548                         }
549                 }
550                 return handle->status;
551         }
552
553         return LDB_SUCCESS;
554 }
555
556 /* set the specified timeout or, if timeout is 0 set the default timeout */
557 int ldb_set_timeout(struct ldb_context *ldb,
558                     struct ldb_request *req,
559                     int timeout)
560 {
561         if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
562
563         if (timeout != 0) {
564                 req->timeout = timeout;
565         } else {
566                 req->timeout = ldb->default_timeout;
567         }
568         req->starttime = time(NULL);
569
570         return LDB_SUCCESS;
571 }
572
573 /* calculates the new timeout based on the previous starttime and timeout */
574 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
575                                   struct ldb_request *oldreq,
576                                   struct ldb_request *newreq)
577 {
578         if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
579
580         if (oldreq == NULL) {
581                 return ldb_set_timeout(ldb, newreq, 0);
582         }
583
584         newreq->starttime = oldreq->starttime;
585         newreq->timeout = oldreq->timeout;
586
587         return LDB_SUCCESS;
588 }
589
590
591 /*
592    set the permissions for new files to be passed to open() in
593    backends that use local files
594  */
595 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
596 {
597         ldb->create_perms = perms;
598 }
599
600 unsigned int ldb_get_create_perms(struct ldb_context *ldb)
601 {
602         return ldb->create_perms;
603 }
604
605 void ldb_set_event_context(struct ldb_context *ldb, struct tevent_context *ev)
606 {
607         ldb->ev_ctx = ev;
608 }
609
610 struct tevent_context * ldb_get_event_context(struct ldb_context *ldb)
611 {
612         return ldb->ev_ctx;
613 }
614
615 void ldb_request_set_state(struct ldb_request *req, int state)
616 {
617         req->handle->state = state;
618 }
619
620 int ldb_request_get_status(struct ldb_request *req)
621 {
622         return req->handle->status;
623 }
624
625
626 /*
627   trace a ldb request
628 */
629 static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
630 {
631         TALLOC_CTX *tmp_ctx = talloc_new(req);
632         int i;
633
634         switch (req->operation) {
635         case LDB_SEARCH:
636                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: SEARCH");
637                 ldb_debug(ldb, LDB_DEBUG_TRACE, " dn: %s",
638                           ldb_dn_get_linearized(req->op.search.base));
639                 ldb_debug(ldb, LDB_DEBUG_TRACE, " scope: %s", 
640                           req->op.search.scope==LDB_SCOPE_BASE?"base":
641                           req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
642                           req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN");
643                 ldb_debug(ldb, LDB_DEBUG_TRACE, " expr: %s", 
644                           ldb_filter_from_tree(tmp_ctx, req->op.search.tree));
645                 for (i=0; req->op.search.attrs && req->op.search.attrs[i]; i++) {
646                         ldb_debug(ldb, LDB_DEBUG_TRACE, " attr: %s", req->op.search.attrs[i]);
647                 }
648                 break;
649         case LDB_DELETE:
650                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: DELETE");
651                 ldb_debug(ldb, LDB_DEBUG_TRACE, " dn: %s", 
652                           ldb_dn_get_linearized(req->op.del.dn));
653                 break;
654         case LDB_RENAME:
655                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: RENAME");
656                 ldb_debug(ldb, LDB_DEBUG_TRACE, " olddn: %s", 
657                           ldb_dn_get_linearized(req->op.rename.olddn));
658                 ldb_debug(ldb, LDB_DEBUG_TRACE, " newdn: %s", 
659                           ldb_dn_get_linearized(req->op.rename.newdn));
660                 break;
661         case LDB_EXTENDED:
662                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: EXTENDED");
663                 ldb_debug(ldb, LDB_DEBUG_TRACE, " oid: %s", req->op.extended.oid);
664                 ldb_debug(ldb, LDB_DEBUG_TRACE, " data: %s", req->op.extended.data?"yes":"no");
665                 break;
666         case LDB_ADD:
667                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: ADD");
668                 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s", 
669                           ldb_ldif_message_string(req->handle->ldb, tmp_ctx, 
670                                                   LDB_CHANGETYPE_ADD, req->op.add.message));
671                 break;
672         case LDB_MODIFY:
673                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: MODIFY");
674                 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s", 
675                           ldb_ldif_message_string(req->handle->ldb, tmp_ctx, 
676                                                   LDB_CHANGETYPE_ADD, req->op.mod.message));
677                 break;
678         case LDB_REQ_REGISTER_CONTROL:
679                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: REGISTER_CONTROL");
680                 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s", 
681                           req->op.reg_control.oid);
682                 break;
683         case LDB_REQ_REGISTER_PARTITION:
684                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: REGISTER_PARTITION");
685                 ldb_debug(req->handle->ldb, LDB_DEBUG_TRACE, "%s", 
686                           ldb_dn_get_linearized(req->op.reg_partition.dn));
687                 break;
688         default:
689                 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_trace_request: UNKNOWN(%u)", 
690                           req->operation);
691                 break;
692         }
693
694         for (i=0; req->controls && req->controls[i]; i++) {
695                 ldb_debug(ldb, LDB_DEBUG_TRACE, " control: %s  crit:%u  data:%s", 
696                           req->controls[i]->oid, 
697                           req->controls[i]->critical, 
698                           req->controls[i]->data?"yes":"no");
699         }
700
701         talloc_free(tmp_ctx);
702 }
703
704
705 /*
706   start an ldb request
707   NOTE: the request must be a talloc context.
708   returns LDB_ERR_* on errors.
709 */
710 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
711 {
712         struct ldb_module *module;
713         int ret;
714
715         if (req->callback == NULL) {
716                 ldb_set_errstring(ldb, "Requests MUST define callbacks");
717                 return LDB_ERR_UNWILLING_TO_PERFORM;
718         }
719
720         ldb_reset_err_string(ldb);
721
722         if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
723                 ldb_trace_request(ldb, req);
724         }
725
726         /* call the first module in the chain */
727         switch (req->operation) {
728         case LDB_SEARCH:
729                 FIRST_OP(ldb, search);
730                 ret = module->ops->search(module, req);
731                 break;
732         case LDB_ADD:
733                 FIRST_OP(ldb, add);
734                 ret = module->ops->add(module, req);
735                 break;
736         case LDB_MODIFY:
737                 FIRST_OP(ldb, modify);
738                 ret = module->ops->modify(module, req);
739                 break;
740         case LDB_DELETE:
741                 FIRST_OP(ldb, del);
742                 ret = module->ops->del(module, req);
743                 break;
744         case LDB_RENAME:
745                 FIRST_OP(ldb, rename);
746                 ret = module->ops->rename(module, req);
747                 break;
748         case LDB_EXTENDED:
749                 FIRST_OP(ldb, extended);
750                 ret = module->ops->extended(module, req);
751                 break;
752         default:
753                 FIRST_OP(ldb, request);
754                 ret = module->ops->request(module, req);
755                 break;
756         }
757
758         return ret;
759 }
760
761 int ldb_request_done(struct ldb_request *req, int status)
762 {
763         req->handle->state = LDB_ASYNC_DONE;
764         req->handle->status = status;
765         return status;
766 }
767
768 /*
769   search the database given a LDAP-like search expression
770
771   returns an LDB error code
772
773   Use talloc_free to free the ldb_message returned in 'res', if successful
774
775 */
776 int ldb_search_default_callback(struct ldb_request *req,
777                                 struct ldb_reply *ares)
778 {
779         struct ldb_result *res;
780         int n;
781
782         res = talloc_get_type(req->context, struct ldb_result);
783
784         if (!ares) {
785                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
786         }
787         if (ares->error != LDB_SUCCESS) {
788                 return ldb_request_done(req, ares->error);
789         }
790
791         switch (ares->type) {
792         case LDB_REPLY_ENTRY:
793                 res->msgs = talloc_realloc(res, res->msgs,
794                                         struct ldb_message *, res->count + 2);
795                 if (! res->msgs) {
796                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
797                 }
798
799                 res->msgs[res->count + 1] = NULL;
800
801                 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
802                 res->count++;
803                 break;
804
805         case LDB_REPLY_REFERRAL:
806                 if (res->refs) {
807                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
808                 } else {
809                         n = 0;
810                 }
811
812                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
813                 if (! res->refs) {
814                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
815                 }
816
817                 res->refs[n] = talloc_move(res->refs, &ares->referral);
818                 res->refs[n + 1] = NULL;
819                 break;
820
821         case LDB_REPLY_DONE:
822                 /* TODO: we should really support controls on entries
823                  * and referrals too! */
824                 res->controls = talloc_move(res, &ares->controls);
825
826                 /* this is the last message, and means the request is done */
827                 /* we have to signal and eventual ldb_wait() waiting that the
828                  * async request operation was completed */
829                 talloc_free(ares);
830                 return ldb_request_done(req, LDB_SUCCESS);
831         }
832
833         talloc_free(ares);
834
835         return LDB_SUCCESS;
836 }
837
838 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
839 {
840         int ret;
841
842         if (!ares) {
843                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
844         }
845
846         if (ares->error != LDB_SUCCESS) {
847                 ret = ares->error;
848                 talloc_free(ares);
849                 return ldb_request_done(req, ret);
850         }
851
852         if (ares->type != LDB_REPLY_DONE) {
853                 talloc_free(ares);
854                 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
855                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
856         }
857
858         talloc_free(ares);
859         return ldb_request_done(req, LDB_SUCCESS);
860 }
861
862 int ldb_build_search_req_ex(struct ldb_request **ret_req,
863                         struct ldb_context *ldb,
864                         void *mem_ctx,
865                         struct ldb_dn *base,
866                         enum ldb_scope scope,
867                         struct ldb_parse_tree *tree,
868                         const char * const *attrs,
869                         struct ldb_control **controls,
870                         void *context,
871                         ldb_request_callback_t callback,
872                         struct ldb_request *parent)
873 {
874         struct ldb_request *req;
875
876         *ret_req = NULL;
877
878         req = talloc(mem_ctx, struct ldb_request);
879         if (req == NULL) {
880                 ldb_oom(ldb);
881                 return LDB_ERR_OPERATIONS_ERROR;
882         }
883
884         req->operation = LDB_SEARCH;
885         if (base == NULL) {
886                 req->op.search.base = ldb_dn_new(req, ldb, NULL);
887         } else {
888                 req->op.search.base = base;
889         }
890         req->op.search.scope = scope;
891
892         req->op.search.tree = tree;
893         if (req->op.search.tree == NULL) {
894                 ldb_set_errstring(ldb, "'tree' can't be NULL");
895                 talloc_free(req);
896                 return LDB_ERR_OPERATIONS_ERROR;
897         }
898
899         req->op.search.attrs = attrs;
900         req->controls = controls;
901         req->context = context;
902         req->callback = callback;
903
904         ldb_set_timeout_from_prev_req(ldb, parent, req);
905
906         req->handle = ldb_handle_new(req, ldb);
907         if (req->handle == NULL) {
908                 ldb_oom(ldb);
909                 return LDB_ERR_OPERATIONS_ERROR;
910         }
911
912         *ret_req = req;
913         return LDB_SUCCESS;
914 }
915
916 int ldb_build_search_req(struct ldb_request **ret_req,
917                         struct ldb_context *ldb,
918                         void *mem_ctx,
919                         struct ldb_dn *base,
920                         enum ldb_scope scope,
921                         const char *expression,
922                         const char * const *attrs,
923                         struct ldb_control **controls,
924                         void *context,
925                         ldb_request_callback_t callback,
926                         struct ldb_request *parent)
927 {
928         struct ldb_parse_tree *tree;
929         int ret;
930
931         tree = ldb_parse_tree(mem_ctx, expression);
932         if (tree == NULL) {
933                 ldb_set_errstring(ldb, "Unable to parse search expression");
934                 return LDB_ERR_OPERATIONS_ERROR;
935         }
936
937         ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
938                                       scope, tree, attrs, controls,
939                                       context, callback, parent);
940         if (ret == LDB_SUCCESS) {
941                 talloc_steal(*ret_req, tree);
942         }
943         return ret;
944 }
945
946 int ldb_build_add_req(struct ldb_request **ret_req,
947                         struct ldb_context *ldb,
948                         void *mem_ctx,
949                         const struct ldb_message *message,
950                         struct ldb_control **controls,
951                         void *context,
952                         ldb_request_callback_t callback,
953                         struct ldb_request *parent)
954 {
955         struct ldb_request *req;
956
957         *ret_req = NULL;
958
959         req = talloc(mem_ctx, struct ldb_request);
960         if (req == NULL) {
961                 ldb_set_errstring(ldb, "Out of Memory");
962                 return LDB_ERR_OPERATIONS_ERROR;
963         }
964
965         req->operation = LDB_ADD;
966         req->op.add.message = message;
967         req->controls = controls;
968         req->context = context;
969         req->callback = callback;
970
971         ldb_set_timeout_from_prev_req(ldb, parent, req);
972
973         req->handle = ldb_handle_new(req, ldb);
974         if (req->handle == NULL) {
975                 ldb_oom(ldb);
976                 return LDB_ERR_OPERATIONS_ERROR;
977         }
978
979         *ret_req = req;
980
981         return LDB_SUCCESS;
982 }
983
984 int ldb_build_mod_req(struct ldb_request **ret_req,
985                         struct ldb_context *ldb,
986                         void *mem_ctx,
987                         const struct ldb_message *message,
988                         struct ldb_control **controls,
989                         void *context,
990                         ldb_request_callback_t callback,
991                         struct ldb_request *parent)
992 {
993         struct ldb_request *req;
994
995         *ret_req = NULL;
996
997         req = talloc(mem_ctx, struct ldb_request);
998         if (req == NULL) {
999                 ldb_set_errstring(ldb, "Out of Memory");
1000                 return LDB_ERR_OPERATIONS_ERROR;
1001         }
1002
1003         req->operation = LDB_MODIFY;
1004         req->op.mod.message = message;
1005         req->controls = controls;
1006         req->context = context;
1007         req->callback = callback;
1008
1009         ldb_set_timeout_from_prev_req(ldb, parent, req);
1010
1011         req->handle = ldb_handle_new(req, ldb);
1012         if (req->handle == NULL) {
1013                 ldb_oom(ldb);
1014                 return LDB_ERR_OPERATIONS_ERROR;
1015         }
1016
1017         *ret_req = req;
1018
1019         return LDB_SUCCESS;
1020 }
1021
1022 int ldb_build_del_req(struct ldb_request **ret_req,
1023                         struct ldb_context *ldb,
1024                         void *mem_ctx,
1025                         struct ldb_dn *dn,
1026                         struct ldb_control **controls,
1027                         void *context,
1028                         ldb_request_callback_t callback,
1029                         struct ldb_request *parent)
1030 {
1031         struct ldb_request *req;
1032
1033         *ret_req = NULL;
1034
1035         req = talloc(mem_ctx, struct ldb_request);
1036         if (req == NULL) {
1037                 ldb_set_errstring(ldb, "Out of Memory");
1038                 return LDB_ERR_OPERATIONS_ERROR;
1039         }
1040
1041         req->operation = LDB_DELETE;
1042         req->op.del.dn = dn;
1043         req->controls = controls;
1044         req->context = context;
1045         req->callback = callback;
1046
1047         ldb_set_timeout_from_prev_req(ldb, parent, req);
1048
1049         req->handle = ldb_handle_new(req, ldb);
1050         if (req->handle == NULL) {
1051                 ldb_oom(ldb);
1052                 return LDB_ERR_OPERATIONS_ERROR;
1053         }
1054
1055         *ret_req = req;
1056
1057         return LDB_SUCCESS;
1058 }
1059
1060 int ldb_build_rename_req(struct ldb_request **ret_req,
1061                         struct ldb_context *ldb,
1062                         void *mem_ctx,
1063                         struct ldb_dn *olddn,
1064                         struct ldb_dn *newdn,
1065                         struct ldb_control **controls,
1066                         void *context,
1067                         ldb_request_callback_t callback,
1068                         struct ldb_request *parent)
1069 {
1070         struct ldb_request *req;
1071
1072         *ret_req = NULL;
1073
1074         req = talloc(mem_ctx, struct ldb_request);
1075         if (req == NULL) {
1076                 ldb_set_errstring(ldb, "Out of Memory");
1077                 return LDB_ERR_OPERATIONS_ERROR;
1078         }
1079
1080         req->operation = LDB_RENAME;
1081         req->op.rename.olddn = olddn;
1082         req->op.rename.newdn = newdn;
1083         req->controls = controls;
1084         req->context = context;
1085         req->callback = callback;
1086
1087         ldb_set_timeout_from_prev_req(ldb, parent, req);
1088
1089         req->handle = ldb_handle_new(req, ldb);
1090         if (req->handle == NULL) {
1091                 ldb_oom(ldb);
1092                 return LDB_ERR_OPERATIONS_ERROR;
1093         }
1094
1095         *ret_req = req;
1096
1097         return LDB_SUCCESS;
1098 }
1099
1100 int ldb_extended_default_callback(struct ldb_request *req,
1101                                   struct ldb_reply *ares)
1102 {
1103         struct ldb_result *res;
1104
1105         res = talloc_get_type(req->context, struct ldb_result);
1106
1107         if (!ares) {
1108                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1109         }
1110         if (ares->error != LDB_SUCCESS) {
1111                 return ldb_request_done(req, ares->error);
1112         }
1113
1114         if (ares->type == LDB_REPLY_DONE) {
1115
1116                 /* TODO: we should really support controls on entries and referrals too! */
1117                 res->extended = talloc_move(res, &ares->response);
1118                 res->controls = talloc_move(res, &ares->controls);
1119
1120                 talloc_free(ares);
1121                 return ldb_request_done(req, LDB_SUCCESS);
1122         }
1123
1124         talloc_free(ares);
1125         ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
1126         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
1127 }
1128
1129 int ldb_build_extended_req(struct ldb_request **ret_req,
1130                            struct ldb_context *ldb,
1131                            void *mem_ctx,
1132                            const char *oid,
1133                            void *data,
1134                            struct ldb_control **controls,
1135                            void *context,
1136                            ldb_request_callback_t callback,
1137                            struct ldb_request *parent)
1138 {
1139         struct ldb_request *req;
1140
1141         *ret_req = NULL;
1142
1143         req = talloc(mem_ctx, struct ldb_request);
1144         if (req == NULL) {
1145                 ldb_set_errstring(ldb, "Out of Memory");
1146                 return LDB_ERR_OPERATIONS_ERROR;
1147         }
1148
1149         req->operation = LDB_EXTENDED;
1150         req->op.extended.oid = oid;
1151         req->op.extended.data = data;
1152         req->controls = controls;
1153         req->context = context;
1154         req->callback = callback;
1155
1156         ldb_set_timeout_from_prev_req(ldb, parent, req);
1157
1158         req->handle = ldb_handle_new(req, ldb);
1159         if (req->handle == NULL) {
1160                 ldb_oom(ldb);
1161                 return LDB_ERR_OPERATIONS_ERROR;
1162         }
1163
1164         *ret_req = req;
1165
1166         return LDB_SUCCESS;
1167 }
1168
1169 int ldb_extended(struct ldb_context *ldb,
1170                  const char *oid,
1171                  void *data,
1172                  struct ldb_result **_res)
1173 {
1174         struct ldb_request *req;
1175         int ret;
1176         struct ldb_result *res;
1177
1178         *_res = NULL;
1179
1180         res = talloc_zero(ldb, struct ldb_result);
1181         if (!res) {
1182                 return LDB_ERR_OPERATIONS_ERROR;
1183         }
1184
1185         ret = ldb_build_extended_req(&req, ldb, ldb,
1186                                      oid, data, NULL,
1187                                      res, ldb_extended_default_callback,
1188                                      NULL);
1189         if (ret != LDB_SUCCESS) goto done;
1190
1191         ldb_set_timeout(ldb, req, 0); /* use default timeout */
1192
1193         ret = ldb_request(ldb, req);
1194
1195         if (ret == LDB_SUCCESS) {
1196                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1197         }
1198
1199         talloc_free(req);
1200
1201 done:
1202         if (ret != LDB_SUCCESS) {
1203                 talloc_free(res);
1204         }
1205
1206         *_res = res;
1207         return ret;
1208 }
1209
1210 /*
1211   note that ldb_search() will automatically replace a NULL 'base' value
1212   with the defaultNamingContext from the rootDSE if available.
1213 */
1214 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1215                 struct ldb_result **result, struct ldb_dn *base,
1216                 enum ldb_scope scope, const char * const *attrs,
1217                 const char *exp_fmt, ...)
1218 {
1219         struct ldb_request *req;
1220         struct ldb_result *res;
1221         char *expression;
1222         va_list ap;
1223         int ret;
1224
1225         expression = NULL;
1226         *result = NULL;
1227         req = NULL;
1228
1229         res = talloc_zero(mem_ctx, struct ldb_result);
1230         if (!res) {
1231                 return LDB_ERR_OPERATIONS_ERROR;
1232         }
1233
1234         if (exp_fmt) {
1235                 va_start(ap, exp_fmt);
1236                 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1237                 va_end(ap);
1238
1239                 if (!expression) {
1240                         talloc_free(res);
1241                         return LDB_ERR_OPERATIONS_ERROR;
1242                 }
1243         }
1244
1245         ret = ldb_build_search_req(&req, ldb, mem_ctx,
1246                                         base?base:ldb_get_default_basedn(ldb),
1247                                         scope,
1248                                         expression,
1249                                         attrs,
1250                                         NULL,
1251                                         res,
1252                                         ldb_search_default_callback,
1253                                         NULL);
1254
1255         if (ret != LDB_SUCCESS) goto done;
1256
1257         ret = ldb_request(ldb, req);
1258
1259         if (ret == LDB_SUCCESS) {
1260                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1261         }
1262
1263 done:
1264         if (ret != LDB_SUCCESS) {
1265                 talloc_free(res);
1266                 res = NULL;
1267         }
1268
1269         talloc_free(expression);
1270         talloc_free(req);
1271
1272         *result = res;
1273         return ret;
1274 }
1275
1276 /*
1277   add a record to the database. Will fail if a record with the given class
1278   and key already exists
1279 */
1280 int ldb_add(struct ldb_context *ldb,
1281             const struct ldb_message *message)
1282 {
1283         struct ldb_request *req;
1284         int ret;
1285
1286         ret = ldb_msg_sanity_check(ldb, message);
1287         if (ret != LDB_SUCCESS) {
1288                 return ret;
1289         }
1290
1291         ret = ldb_build_add_req(&req, ldb, ldb,
1292                                         message,
1293                                         NULL,
1294                                         NULL,
1295                                         ldb_op_default_callback,
1296                                         NULL);
1297
1298         if (ret != LDB_SUCCESS) return ret;
1299
1300         /* do request and autostart a transaction */
1301         ret = ldb_autotransaction_request(ldb, req);
1302
1303         talloc_free(req);
1304         return ret;
1305 }
1306
1307 /*
1308   modify the specified attributes of a record
1309 */
1310 int ldb_modify(struct ldb_context *ldb,
1311                const struct ldb_message *message)
1312 {
1313         struct ldb_request *req;
1314         int ret;
1315
1316         ret = ldb_msg_sanity_check(ldb, message);
1317         if (ret != LDB_SUCCESS) {
1318                 return ret;
1319         }
1320
1321         ret = ldb_build_mod_req(&req, ldb, ldb,
1322                                         message,
1323                                         NULL,
1324                                         NULL,
1325                                         ldb_op_default_callback,
1326                                         NULL);
1327
1328         if (ret != LDB_SUCCESS) return ret;
1329
1330         /* do request and autostart a transaction */
1331         ret = ldb_autotransaction_request(ldb, req);
1332
1333         talloc_free(req);
1334         return ret;
1335 }
1336
1337
1338 /*
1339   delete a record from the database
1340 */
1341 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1342 {
1343         struct ldb_request *req;
1344         int ret;
1345
1346         ret = ldb_build_del_req(&req, ldb, ldb,
1347                                         dn,
1348                                         NULL,
1349                                         NULL,
1350                                         ldb_op_default_callback,
1351                                         NULL);
1352
1353         if (ret != LDB_SUCCESS) return ret;
1354
1355         /* do request and autostart a transaction */
1356         ret = ldb_autotransaction_request(ldb, req);
1357
1358         talloc_free(req);
1359         return ret;
1360 }
1361
1362 /*
1363   rename a record in the database
1364 */
1365 int ldb_rename(struct ldb_context *ldb,
1366                 struct ldb_dn *olddn, struct ldb_dn *newdn)
1367 {
1368         struct ldb_request *req;
1369         int ret;
1370
1371         ret = ldb_build_rename_req(&req, ldb, ldb,
1372                                         olddn,
1373                                         newdn,
1374                                         NULL,
1375                                         NULL,
1376                                         ldb_op_default_callback,
1377                                         NULL);
1378
1379         if (ret != LDB_SUCCESS) return ret;
1380
1381         /* do request and autostart a transaction */
1382         ret = ldb_autotransaction_request(ldb, req);
1383
1384         talloc_free(req);
1385         return ret;
1386 }
1387
1388
1389 /*
1390   return the global sequence number
1391 */
1392 int ldb_sequence_number(struct ldb_context *ldb,
1393                         enum ldb_sequence_type type, uint64_t *seq_num)
1394 {
1395         struct ldb_seqnum_request *seq;
1396         struct ldb_seqnum_result *seqr;
1397         struct ldb_result *res;
1398         TALLOC_CTX *tmp_ctx;
1399         int ret;
1400
1401         *seq_num = 0;
1402
1403         tmp_ctx = talloc_zero(ldb, struct ldb_request);
1404         if (tmp_ctx == NULL) {
1405                 ldb_set_errstring(ldb, "Out of Memory");
1406                 return LDB_ERR_OPERATIONS_ERROR;
1407         }
1408         seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
1409         if (seq == NULL) {
1410                 ldb_set_errstring(ldb, "Out of Memory");
1411                 ret = LDB_ERR_OPERATIONS_ERROR;
1412                 goto done;
1413         }
1414         seq->type = type;
1415
1416         ret = ldb_extended(ldb, LDB_EXTENDED_SEQUENCE_NUMBER, seq, &res);
1417         if (ret != LDB_SUCCESS) {
1418                 goto done;
1419         }
1420         talloc_steal(tmp_ctx, res);
1421
1422         if (strcmp(LDB_EXTENDED_SEQUENCE_NUMBER, res->extended->oid) != 0) {
1423                 ldb_set_errstring(ldb, "Invalid OID in reply");
1424                 ret = LDB_ERR_OPERATIONS_ERROR;
1425                 goto done;
1426         }
1427         seqr = talloc_get_type(res->extended->data,
1428                                 struct ldb_seqnum_result);
1429         *seq_num = seqr->seq_num;
1430
1431 done:
1432         talloc_free(tmp_ctx);
1433         return ret;
1434 }
1435
1436 /*
1437   return extended error information
1438 */
1439 const char *ldb_errstring(struct ldb_context *ldb)
1440 {
1441         if (ldb->err_string) {
1442                 return ldb->err_string;
1443         }
1444
1445         return NULL;
1446 }
1447
1448 /*
1449   return a string explaining what a ldb error constant meancs
1450 */
1451 const char *ldb_strerror(int ldb_err)
1452 {
1453         switch (ldb_err) {
1454         case LDB_SUCCESS:
1455                 return "Success";
1456         case LDB_ERR_OPERATIONS_ERROR:
1457                 return "Operations error";
1458         case LDB_ERR_PROTOCOL_ERROR:
1459                 return "Protocol error";
1460         case LDB_ERR_TIME_LIMIT_EXCEEDED:
1461                 return "Time limit exceeded";
1462         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1463                 return "Size limit exceeded";
1464         case LDB_ERR_COMPARE_FALSE:
1465                 return "Compare false";
1466         case LDB_ERR_COMPARE_TRUE:
1467                 return "Compare true";
1468         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1469                 return "Auth method not supported";
1470         case LDB_ERR_STRONG_AUTH_REQUIRED:
1471                 return "Strong auth required";
1472 /* 9 RESERVED */
1473         case LDB_ERR_REFERRAL:
1474                 return "Referral error";
1475         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1476                 return "Admin limit exceeded";
1477         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1478                 return "Unsupported critical extension";
1479         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1480                 return "Confidentiality required";
1481         case LDB_ERR_SASL_BIND_IN_PROGRESS:
1482                 return "SASL bind in progress";
1483         case LDB_ERR_NO_SUCH_ATTRIBUTE:
1484                 return "No such attribute";
1485         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1486                 return "Undefined attribute type";
1487         case LDB_ERR_INAPPROPRIATE_MATCHING:
1488                 return "Inappropriate matching";
1489         case LDB_ERR_CONSTRAINT_VIOLATION:
1490                 return "Constraint violation";
1491         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1492                 return "Attribute or value exists";
1493         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1494                 return "Invalid attribute syntax";
1495 /* 22-31 unused */
1496         case LDB_ERR_NO_SUCH_OBJECT:
1497                 return "No such object";
1498         case LDB_ERR_ALIAS_PROBLEM:
1499                 return "Alias problem";
1500         case LDB_ERR_INVALID_DN_SYNTAX:
1501                 return "Invalid DN syntax";
1502 /* 35 RESERVED */
1503         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1504                 return "Alias dereferencing problem";
1505 /* 37-47 unused */
1506         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1507                 return "Inappropriate authentication";
1508         case LDB_ERR_INVALID_CREDENTIALS:
1509                 return "Invalid credentials";
1510         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1511                 return "insufficient access rights";
1512         case LDB_ERR_BUSY:
1513                 return "Busy";
1514         case LDB_ERR_UNAVAILABLE:
1515                 return "Unavailable";
1516         case LDB_ERR_UNWILLING_TO_PERFORM:
1517                 return "Unwilling to perform";
1518         case LDB_ERR_LOOP_DETECT:
1519                 return "Loop detect";
1520 /* 55-63 unused */
1521         case LDB_ERR_NAMING_VIOLATION:
1522                 return "Naming violation";
1523         case LDB_ERR_OBJECT_CLASS_VIOLATION:
1524                 return "Object class violation";
1525         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1526                 return "Not allowed on non-leaf";
1527         case LDB_ERR_NOT_ALLOWED_ON_RDN:
1528                 return "Not allowed on RDN";
1529         case LDB_ERR_ENTRY_ALREADY_EXISTS:
1530                 return "Entry already exists";
1531         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1532                 return "Object class mods prohibited";
1533 /* 70 RESERVED FOR CLDAP */
1534         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1535                 return "Affects multiple DSAs";
1536 /* 72-79 unused */
1537         case LDB_ERR_OTHER:
1538                 return "Other";
1539         }
1540
1541         return "Unknown error";
1542 }
1543
1544 /*
1545   set backend specific opaque parameters
1546 */
1547 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1548 {
1549         struct ldb_opaque *o;
1550
1551         /* allow updating an existing value */
1552         for (o=ldb->opaque;o;o=o->next) {
1553                 if (strcmp(o->name, name) == 0) {
1554                         o->value = value;
1555                         return LDB_SUCCESS;
1556                 }
1557         }
1558
1559         o = talloc(ldb, struct ldb_opaque);
1560         if (o == NULL) {
1561                 ldb_oom(ldb);
1562                 return LDB_ERR_OTHER;
1563         }
1564         o->next = ldb->opaque;
1565         o->name = name;
1566         o->value = value;
1567         ldb->opaque = o;
1568         return LDB_SUCCESS;
1569 }
1570
1571 /*
1572   get a previously set opaque value
1573 */
1574 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1575 {
1576         struct ldb_opaque *o;
1577         for (o=ldb->opaque;o;o=o->next) {
1578                 if (strcmp(o->name, name) == 0) {
1579                         return o->value;
1580                 }
1581         }
1582         return NULL;
1583 }
1584
1585 int ldb_global_init(void)
1586 {
1587         /* Provided for compatibility with some older versions of ldb */
1588         return 0;
1589 }
1590
1591 /* return the ldb flags */
1592 unsigned int ldb_get_flags(struct ldb_context *ldb)
1593 {
1594         return ldb->flags;
1595 }
1596
1597 /* set the ldb flags */
1598 void ldb_set_flags(struct ldb_context *ldb, unsigned flags)
1599 {
1600         ldb->flags = flags;
1601 }