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