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