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